xref: /xnu-11215/bsd/dev/dtrace/dtrace.c (revision 8d741a5d)
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 /*
23  * Portions Copyright (c) 2013, 2016, Joyent, Inc. All rights reserved.
24  * Portions Copyright (c) 2013 by Delphix. All rights reserved.
25  */
26 
27 /*
28  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31 
32 /*
33  * DTrace - Dynamic Tracing for Solaris
34  *
35  * This is the implementation of the Solaris Dynamic Tracing framework
36  * (DTrace).  The user-visible interface to DTrace is described at length in
37  * the "Solaris Dynamic Tracing Guide".  The interfaces between the libdtrace
38  * library, the in-kernel DTrace framework, and the DTrace providers are
39  * described in the block comments in the <sys/dtrace.h> header file.  The
40  * internal architecture of DTrace is described in the block comments in the
41  * <sys/dtrace_impl.h> header file.  The comments contained within the DTrace
42  * implementation very much assume mastery of all of these sources; if one has
43  * an unanswered question about the implementation, one should consult them
44  * first.
45  *
46  * The functions here are ordered roughly as follows:
47  *
48  *   - Probe context functions
49  *   - Probe hashing functions
50  *   - Non-probe context utility functions
51  *   - Matching functions
52  *   - Provider-to-Framework API functions
53  *   - Probe management functions
54  *   - DIF object functions
55  *   - Format functions
56  *   - Predicate functions
57  *   - ECB functions
58  *   - Buffer functions
59  *   - Enabling functions
60  *   - DOF functions
61  *   - Anonymous enabling functions
62  *   - Process functions
63  *   - Consumer state functions
64  *   - Helper functions
65  *   - Hook functions
66  *   - Driver cookbook functions
67  *
68  * Each group of functions begins with a block comment labelled the "DTrace
69  * [Group] Functions", allowing one to find each block by searching forward
70  * on capital-f functions.
71  */
72 #include <sys/errno.h>
73 #include <sys/types.h>
74 #include <sys/stat.h>
75 #include <sys/conf.h>
76 #include <sys/random.h>
77 #include <sys/systm.h>
78 #include <sys/dtrace_impl.h>
79 #include <sys/param.h>
80 #include <sys/proc_internal.h>
81 #include <sys/ioctl.h>
82 #include <sys/fcntl.h>
83 #include <miscfs/devfs/devfs.h>
84 #include <sys/malloc.h>
85 #include <sys/kernel_types.h>
86 #include <sys/proc_internal.h>
87 #include <sys/uio_internal.h>
88 #include <sys/kauth.h>
89 #include <vm/pmap.h>
90 #include <sys/user.h>
91 #include <mach/exception_types.h>
92 #include <sys/signalvar.h>
93 #include <mach/task.h>
94 #include <kern/ast.h>
95 #include <kern/hvg_hypercall.h>
96 #include <kern/sched_prim.h>
97 #include <kern/processor.h>
98 #include <kern/task.h>
99 #include <kern/zalloc.h>
100 #include <netinet/in.h>
101 #include <libkern/sysctl.h>
102 #include <sys/kdebug.h>
103 #include <sys/sdt_impl.h>
104 
105 #if CONFIG_PERVASIVE_CPI
106 #include <kern/monotonic.h>
107 #include <machine/monotonic.h>
108 #endif /* CONFIG_PERVASIVE_CPI */
109 
110 #include "dtrace_xoroshiro128_plus.h"
111 
112 #include <IOKit/IOPlatformExpert.h>
113 
114 #include <kern/cpu_data.h>
115 
116 extern addr64_t kvtophys(vm_offset_t va);
117 
118 extern uint32_t pmap_find_phys(void *, uint64_t);
119 extern boolean_t pmap_valid_page(uint32_t);
120 extern void OSKextRegisterKextsWithDTrace(void);
121 extern kmod_info_t g_kernel_kmod_info;
122 extern void commpage_update_dof(boolean_t enabled);
123 
124 /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
125 #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
126 
127 #define t_predcache t_dtrace_predcache /* Cosmetic. Helps readability of thread.h */
128 
129 extern void dtrace_suspend(void);
130 extern void dtrace_resume(void);
131 extern void dtrace_early_init(void);
132 extern int dtrace_keep_kernel_symbols(void);
133 extern void dtrace_init(void);
134 extern void helper_init(void);
135 extern void fasttrap_init(void);
136 
137 static int  dtrace_lazy_dofs_duplicate(proc_t *, proc_t *);
138 extern void dtrace_lazy_dofs_destroy(proc_t *);
139 extern void dtrace_postinit(void);
140 
141 extern void dtrace_proc_fork(proc_t*, proc_t*, int);
142 extern void dtrace_proc_exec(proc_t*);
143 extern void dtrace_proc_exit(proc_t*);
144 
145 /*
146  * DTrace Tunable Variables
147  *
148  * The following variables may be dynamically tuned by using sysctl(8), the
149  * variables being stored in the kern.dtrace namespace.  For example:
150  * 	sysctl kern.dtrace.dof_maxsize = 1048575 	# 1M
151  *
152  * In general, the only variables that one should be tuning this way are those
153  * that affect system-wide DTrace behavior, and for which the default behavior
154  * is undesirable.  Most of these variables are tunable on a per-consumer
155  * basis using DTrace options, and need not be tuned on a system-wide basis.
156  * When tuning these variables, avoid pathological values; while some attempt
157  * is made to verify the integrity of these variables, they are not considered
158  * part of the supported interface to DTrace, and they are therefore not
159  * checked comprehensively.
160  */
161 uint64_t	dtrace_buffer_memory_maxsize = 0;		/* initialized in dtrace_init */
162 uint64_t	dtrace_buffer_memory_inuse = 0;
163 int		dtrace_destructive_disallow = 1;
164 dtrace_optval_t	dtrace_nonroot_maxsize = (16 * 1024 * 1024);
165 size_t		dtrace_difo_maxsize = (256 * 1024);
166 dtrace_optval_t	dtrace_dof_maxsize = (512 * 1024);
167 dtrace_optval_t	dtrace_statvar_maxsize = (16 * 1024);
168 dtrace_optval_t	dtrace_statvar_maxsize_max = (16 * 10 * 1024);
169 size_t		dtrace_actions_max = (16 * 1024);
170 size_t		dtrace_retain_max = 1024;
171 dtrace_optval_t	dtrace_helper_actions_max = 32;
172 dtrace_optval_t	dtrace_helper_providers_max = 64;
173 dtrace_optval_t	dtrace_dstate_defsize = (1 * 1024 * 1024);
174 size_t		dtrace_strsize_default = 256;
175 dtrace_optval_t	dtrace_strsize_min = 8;
176 dtrace_optval_t	dtrace_strsize_max = 65536;
177 dtrace_optval_t	dtrace_cleanrate_default = 990099000;		/* 1.1 hz */
178 dtrace_optval_t	dtrace_cleanrate_min = 20000000;			/* 50 hz */
179 dtrace_optval_t	dtrace_cleanrate_max = (uint64_t)60 * NANOSEC;	/* 1/minute */
180 dtrace_optval_t	dtrace_aggrate_default = NANOSEC;		/* 1 hz */
181 dtrace_optval_t	dtrace_statusrate_default = NANOSEC;		/* 1 hz */
182 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC;	 /* 6/minute */
183 dtrace_optval_t	dtrace_switchrate_default = NANOSEC;		/* 1 hz */
184 dtrace_optval_t	dtrace_nspec_default = 1;
185 dtrace_optval_t	dtrace_specsize_default = 32 * 1024;
186 dtrace_optval_t dtrace_stackframes_default = 20;
187 dtrace_optval_t dtrace_ustackframes_default = 20;
188 dtrace_optval_t dtrace_jstackframes_default = 50;
189 dtrace_optval_t dtrace_jstackstrsize_default = 512;
190 dtrace_optval_t dtrace_buflimit_default = 75;
191 dtrace_optval_t dtrace_buflimit_min = 1;
192 dtrace_optval_t dtrace_buflimit_max = 99;
193 size_t		dtrace_nprobes_default = 4;
194 int		dtrace_msgdsize_max = 128;
195 hrtime_t	dtrace_chill_max = 500 * (NANOSEC / MILLISEC);	/* 500 ms */
196 hrtime_t	dtrace_chill_interval = NANOSEC;		/* 1000 ms */
197 int		dtrace_devdepth_max = 32;
198 int		dtrace_err_verbose;
199 hrtime_t	dtrace_deadman_interval = NANOSEC;
200 hrtime_t	dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC;
201 hrtime_t	dtrace_deadman_user = (hrtime_t)30 * NANOSEC;
202 
203 /*
204  * DTrace External Variables
205  *
206  * As dtrace(7D) is a kernel module, any DTrace variables are obviously
207  * available to DTrace consumers via the backtick (`) syntax.  One of these,
208  * dtrace_zero, is made deliberately so:  it is provided as a source of
209  * well-known, zero-filled memory.  While this variable is not documented,
210  * it is used by some translators as an implementation detail.
211  */
212 const char	dtrace_zero[256] = { 0 };	/* zero-filled memory */
213 unsigned int	dtrace_max_cpus = 0;		/* number of enabled cpus */
214 /*
215  * DTrace Internal Variables
216  */
217 static dev_info_t	*dtrace_devi;		/* device info */
218 static vmem_t		*dtrace_arena;		/* probe ID arena */
219 static dtrace_probe_t	**dtrace_probes;	/* array of all probes */
220 static int		dtrace_nprobes;		/* number of probes */
221 static dtrace_provider_t *dtrace_provider;	/* provider list */
222 static dtrace_meta_t	*dtrace_meta_pid;	/* user-land meta provider */
223 static int		dtrace_opens;		/* number of opens */
224 static int		dtrace_helpers;		/* number of helpers */
225 static dtrace_hash_t	*dtrace_strings;
226 static dtrace_hash_t	*dtrace_byprov;		/* probes hashed by provider */
227 static dtrace_hash_t	*dtrace_bymod;		/* probes hashed by module */
228 static dtrace_hash_t	*dtrace_byfunc;		/* probes hashed by function */
229 static dtrace_hash_t	*dtrace_byname;		/* probes hashed by name */
230 static dtrace_toxrange_t *dtrace_toxrange;	/* toxic range array */
231 static int		dtrace_toxranges;	/* number of toxic ranges */
232 static int		dtrace_toxranges_max;	/* size of toxic range array */
233 static dtrace_anon_t	dtrace_anon;		/* anonymous enabling */
234 static uint64_t		dtrace_vtime_references; /* number of vtimestamp refs */
235 static kthread_t	*dtrace_panicked;	/* panicking thread */
236 static dtrace_ecb_t	*dtrace_ecb_create_cache; /* cached created ECB */
237 static dtrace_genid_t	dtrace_probegen;	/* current probe generation */
238 static dtrace_helpers_t *dtrace_deferred_pid;	/* deferred helper list */
239 static dtrace_enabling_t *dtrace_retained;	/* list of retained enablings */
240 static dtrace_genid_t   dtrace_retained_gen;    /* current retained enab gen */
241 static dtrace_dynvar_t	dtrace_dynhash_sink;	/* end of dynamic hash chains */
242 
243 static int		dtrace_dof_mode;	/* See dtrace_impl.h for a description of Darwin's dof modes. */
244 
245 			/*
246 			 * This does't quite fit as an internal variable, as it must be accessed in
247 			 * fbt_provide and sdt_provide. Its clearly not a dtrace tunable variable either...
248 			 */
249 int			dtrace_kernel_symbol_mode;	/* See dtrace_impl.h for a description of Darwin's kernel symbol modes. */
250 static uint32_t		dtrace_wake_clients;
251 static uint8_t      dtrace_kerneluuid[16];	/* the 128-bit uuid */
252 
253 /*
254  * To save memory, some common memory allocations are given a
255  * unique zone. For example, dtrace_probe_t is 72 bytes in size,
256  * which means it would fall into the kalloc.128 bucket. With
257  * 20k elements allocated, the space saved is substantial.
258  */
259 
260 static ZONE_DEFINE_TYPE(dtrace_probe_t_zone, "dtrace.dtrace_probe_t",
261     dtrace_probe_t, ZC_PGZ_USE_GUARDS);
262 
263 static ZONE_DEFINE(dtrace_state_pcpu_zone, "dtrace.dtrace_dstate_percpu_t",
264     sizeof(dtrace_dstate_percpu_t), ZC_PERCPU);
265 
266 static int dtrace_module_unloaded(struct kmod_info *kmod);
267 
268 /*
269  * DTrace Locking
270  * DTrace is protected by three (relatively coarse-grained) locks:
271  *
272  * (1) dtrace_lock is required to manipulate essentially any DTrace state,
273  *     including enabling state, probes, ECBs, consumer state, helper state,
274  *     etc.  Importantly, dtrace_lock is _not_ required when in probe context;
275  *     probe context is lock-free -- synchronization is handled via the
276  *     dtrace_sync() cross call mechanism.
277  *
278  * (2) dtrace_provider_lock is required when manipulating provider state, or
279  *     when provider state must be held constant.
280  *
281  * (3) dtrace_meta_lock is required when manipulating meta provider state, or
282  *     when meta provider state must be held constant.
283  *
284  * The lock ordering between these three locks is dtrace_meta_lock before
285  * dtrace_provider_lock before dtrace_lock.  (In particular, there are
286  * several places where dtrace_provider_lock is held by the framework as it
287  * calls into the providers -- which then call back into the framework,
288  * grabbing dtrace_lock.)
289  *
290  * There are two other locks in the mix:  mod_lock and cpu_lock.  With respect
291  * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical
292  * role as a coarse-grained lock; it is acquired before both of these locks.
293  * With respect to dtrace_meta_lock, its behavior is stranger:  cpu_lock must
294  * be acquired _between_ dtrace_meta_lock and any other DTrace locks.
295  * mod_lock is similar with respect to dtrace_provider_lock in that it must be
296  * acquired _between_ dtrace_provider_lock and dtrace_lock.
297  */
298 
299 
300 /*
301  * APPLE NOTE:
302  *
303  * For porting purposes, all kmutex_t vars have been changed
304  * to lck_mtx_t, which require explicit initialization.
305  *
306  * kmutex_t becomes lck_mtx_t
307  * mutex_enter() becomes lck_mtx_lock()
308  * mutex_exit() becomes lck_mtx_unlock()
309  *
310  * Lock asserts are changed like this:
311  *
312  * ASSERT(MUTEX_HELD(&cpu_lock));
313  *	becomes:
314  * LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
315  *
316  */
317 static LCK_MTX_DECLARE_ATTR(dtrace_lock,
318     &dtrace_lck_grp, &dtrace_lck_attr);		/* probe state lock */
319 static LCK_MTX_DECLARE_ATTR(dtrace_provider_lock,
320     &dtrace_lck_grp, &dtrace_lck_attr);	/* provider state lock */
321 static LCK_MTX_DECLARE_ATTR(dtrace_meta_lock,
322     &dtrace_lck_grp, &dtrace_lck_attr);	/* meta-provider state lock */
323 static LCK_RW_DECLARE_ATTR(dtrace_dof_mode_lock,
324     &dtrace_lck_grp, &dtrace_lck_attr);	/* dof mode lock */
325 
326 /*
327  * DTrace Provider Variables
328  *
329  * These are the variables relating to DTrace as a provider (that is, the
330  * provider of the BEGIN, END, and ERROR probes).
331  */
332 static dtrace_pattr_t	dtrace_provider_attr = {
333 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
334 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
335 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
336 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
337 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
338 };
339 
340 static void
dtrace_provide_nullop(void * arg,const dtrace_probedesc_t * desc)341 dtrace_provide_nullop(void *arg, const dtrace_probedesc_t *desc)
342 {
343 #pragma unused(arg, desc)
344 }
345 
346 static void
dtrace_provide_module_nullop(void * arg,struct modctl * ctl)347 dtrace_provide_module_nullop(void *arg, struct modctl *ctl)
348 {
349 #pragma unused(arg, ctl)
350 }
351 
352 static int
dtrace_enable_nullop(void * arg,dtrace_id_t id,void * parg)353 dtrace_enable_nullop(void *arg, dtrace_id_t id, void *parg)
354 {
355 #pragma unused(arg, id, parg)
356     return (0);
357 }
358 
359 static void
dtrace_disable_nullop(void * arg,dtrace_id_t id,void * parg)360 dtrace_disable_nullop(void *arg, dtrace_id_t id, void *parg)
361 {
362 #pragma unused(arg, id, parg)
363 }
364 
365 static void
dtrace_suspend_nullop(void * arg,dtrace_id_t id,void * parg)366 dtrace_suspend_nullop(void *arg, dtrace_id_t id, void *parg)
367 {
368 #pragma unused(arg, id, parg)
369 }
370 
371 static void
dtrace_resume_nullop(void * arg,dtrace_id_t id,void * parg)372 dtrace_resume_nullop(void *arg, dtrace_id_t id, void *parg)
373 {
374 #pragma unused(arg, id, parg)
375 }
376 
377 static void
dtrace_destroy_nullop(void * arg,dtrace_id_t id,void * parg)378 dtrace_destroy_nullop(void *arg, dtrace_id_t id, void *parg)
379 {
380 #pragma unused(arg, id, parg)
381 }
382 
383 
384 static dtrace_pops_t dtrace_provider_ops = {
385 	.dtps_provide = dtrace_provide_nullop,
386 	.dtps_provide_module =	dtrace_provide_module_nullop,
387 	.dtps_enable =	dtrace_enable_nullop,
388 	.dtps_disable =	dtrace_disable_nullop,
389 	.dtps_suspend =	dtrace_suspend_nullop,
390 	.dtps_resume =	dtrace_resume_nullop,
391 	.dtps_getargdesc =	NULL,
392 	.dtps_getargval =	NULL,
393 	.dtps_usermode =	NULL,
394 	.dtps_destroy =	dtrace_destroy_nullop,
395 };
396 
397 static dtrace_id_t	dtrace_probeid_begin;	/* special BEGIN probe */
398 static dtrace_id_t	dtrace_probeid_end;	/* special END probe */
399 dtrace_id_t		dtrace_probeid_error;	/* special ERROR probe */
400 
401 /*
402  * DTrace Helper Tracing Variables
403  */
404 uint32_t dtrace_helptrace_next = 0;
405 uint32_t dtrace_helptrace_nlocals;
406 char	*dtrace_helptrace_buffer;
407 size_t	dtrace_helptrace_bufsize = 512 * 1024;
408 
409 #if DEBUG
410 int	dtrace_helptrace_enabled = 1;
411 #else
412 int	dtrace_helptrace_enabled = 0;
413 #endif
414 
415 #if defined (__arm64__)
416 /*
417  * The ioctl for adding helper DOF is based on the
418  * size of a user_addr_t.  We need to recognize both
419  * U32 and U64 as the same action.
420  */
421 #define DTRACEHIOC_ADDDOF_U32       _IOW('h', 4, user32_addr_t)
422 #define DTRACEHIOC_ADDDOF_U64       _IOW('h', 4, user64_addr_t)
423 #endif  /* __arm64__ */
424 
425 /*
426  * DTrace Error Hashing
427  *
428  * On DEBUG kernels, DTrace will track the errors that has seen in a hash
429  * table.  This is very useful for checking coverage of tests that are
430  * expected to induce DIF or DOF processing errors, and may be useful for
431  * debugging problems in the DIF code generator or in DOF generation .  The
432  * error hash may be examined with the ::dtrace_errhash MDB dcmd.
433  */
434 #if DEBUG
435 static dtrace_errhash_t	dtrace_errhash[DTRACE_ERRHASHSZ];
436 static const char *dtrace_errlast;
437 static kthread_t *dtrace_errthread;
438 static LCK_MTX_DECLARE_ATTR(dtrace_errlock, &dtrace_lck_grp, &dtrace_lck_attr);
439 #endif
440 
441 /*
442  * DTrace Macros and Constants
443  *
444  * These are various macros that are useful in various spots in the
445  * implementation, along with a few random constants that have no meaning
446  * outside of the implementation.  There is no real structure to this cpp
447  * mishmash -- but is there ever?
448  */
449 
450 #define	DTRACE_GETSTR(hash, elm)	\
451 	(hash->dth_getstr(elm, hash->dth_stroffs))
452 
453 #define	DTRACE_HASHSTR(hash, elm)	\
454 	dtrace_hash_str(DTRACE_GETSTR(hash, elm))
455 
456 #define	DTRACE_HASHNEXT(hash, elm)	\
457 	(void**)((uintptr_t)(elm) + (hash)->dth_nextoffs)
458 
459 #define	DTRACE_HASHPREV(hash, elm)	\
460 	(void**)((uintptr_t)(elm) + (hash)->dth_prevoffs)
461 
462 #define	DTRACE_HASHEQ(hash, lhs, rhs)	\
463 	(strcmp(DTRACE_GETSTR(hash, lhs), \
464 	    DTRACE_GETSTR(hash, rhs)) == 0)
465 
466 #define	DTRACE_AGGHASHSIZE_SLEW		17
467 
468 #define	DTRACE_V4MAPPED_OFFSET		(sizeof (uint32_t) * 3)
469 
470 /*
471  * The key for a thread-local variable needs to be unique to a single
472  * thread over the lifetime of the system, and not overlap with any variable
473  * IDs. So we take thread's thread_id, a unique 64-bit number that is never
474  * reused after the thread exits, and add DIF_VARIABLE_MAX to it, which
475  * guarantees that it won’t overlap any variable IDs. We also want to treat
476  * running in interrupt context as independent of thread-context. So if
477  * interrupts are active, we set the 63rd bit, otherwise it’s cleared.
478  *
479  * This is necessary (but not sufficient) to assure that global associative
480  * arrays never collide with thread-local variables. To guarantee that they
481  * cannot collide, we must also define the order for keying dynamic variables.
482  *
483  * That order is:
484  *
485  *   [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ]
486  *
487  * Because the variable-key and the tls-key are in orthogonal spaces, there is
488  * no way for a global variable key signature to match a thread-local key
489  * signature.
490  */
491 #if defined (__x86_64__) || defined(__arm64__)
492 #define	DTRACE_TLS_THRKEY(where) {                                           \
493 	uint_t intr = ml_at_interrupt_context(); /* Note: just one measly bit */ \
494 	uint64_t thr = thread_tid(current_thread());                             \
495 	ASSERT(intr < 2);                                                        \
496 	(where) = ((thr + DIF_VARIABLE_MAX) & (~((uint64_t)1 << 63))) |          \
497 		((uint64_t)intr << 63);                                              \
498 }
499 #else
500 #error Unknown architecture
501 #endif
502 
503 #define	DT_BSWAP_8(x)	((x) & 0xff)
504 #define	DT_BSWAP_16(x)	((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8))
505 #define	DT_BSWAP_32(x)	((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16))
506 #define	DT_BSWAP_64(x)	((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32))
507 
508 #define	DT_MASK_LO 0x00000000FFFFFFFFULL
509 
510 #define	DTRACE_STORE(type, tomax, offset, what) \
511 	*((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what);
512 
513 
514 #define	DTRACE_ALIGNCHECK(addr, size, flags)				\
515 	if (addr & (MIN(size,4) - 1)) {					\
516 		*flags |= CPU_DTRACE_BADALIGN;				\
517 		cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr;	\
518 		return (0);						\
519 	}
520 
521 #define	DTRACE_RANGE_REMAIN(remp, addr, baseaddr, basesz)		\
522 do {									\
523 	if ((remp) != NULL) {						\
524 		*(remp) = (uintptr_t)(baseaddr) + (basesz) - (addr);	\
525 	}								\
526 } while (0)
527 
528 
529 /*
530  * Test whether a range of memory starting at testaddr of size testsz falls
531  * within the range of memory described by addr, sz.  We take care to avoid
532  * problems with overflow and underflow of the unsigned quantities, and
533  * disallow all negative sizes.  Ranges of size 0 are allowed.
534  */
535 #define	DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \
536 	((testaddr) - (baseaddr) < (basesz) && \
537 	(testaddr) + (testsz) - (baseaddr) <= (basesz) && \
538 	(testaddr) + (testsz) >= (testaddr))
539 
540 /*
541  * Test whether alloc_sz bytes will fit in the scratch region.  We isolate
542  * alloc_sz on the righthand side of the comparison in order to avoid overflow
543  * or underflow in the comparison with it.  This is simpler than the INRANGE
544  * check above, because we know that the dtms_scratch_ptr is valid in the
545  * range.  Allocations of size zero are allowed.
546  */
547 #define	DTRACE_INSCRATCH(mstate, alloc_sz) \
548 	((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \
549 	(mstate)->dtms_scratch_ptr >= (alloc_sz))
550 
551 #if defined (__x86_64__) || defined (__arm64__)
552 #define	DTRACE_LOADFUNC(bits)						\
553 /*CSTYLED*/								\
554 uint##bits##_t dtrace_load##bits(uintptr_t addr);			\
555 									\
556 extern int dtrace_nofault_copy##bits(uintptr_t, uint##bits##_t *);	\
557 									\
558 uint##bits##_t								\
559 dtrace_load##bits(uintptr_t addr)					\
560 {									\
561 	size_t size = bits / NBBY;					\
562 	/*CSTYLED*/							\
563 	uint##bits##_t rval = 0;					\
564 	int i;								\
565 	volatile uint16_t *flags = (volatile uint16_t *)		\
566 	    &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;			\
567 									\
568 	DTRACE_ALIGNCHECK(addr, size, flags);				\
569 									\
570 	for (i = 0; i < dtrace_toxranges; i++) {			\
571 		if (addr >= dtrace_toxrange[i].dtt_limit)		\
572 			continue;					\
573 									\
574 		if (addr + size <= dtrace_toxrange[i].dtt_base)		\
575 			continue;					\
576 									\
577 		/*							\
578 		 * This address falls within a toxic region; return 0.	\
579 		 */							\
580 		*flags |= CPU_DTRACE_BADADDR;				\
581 		cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr;	\
582 		return (0);						\
583 	}								\
584 									\
585 	{								\
586 	*flags |= CPU_DTRACE_NOFAULT;					\
587 	/*CSTYLED*/							\
588 	/*                                                              \
589 	* PR6394061 - avoid device memory that is unpredictably		\
590 	* mapped and unmapped                                   	\
591 	*/								\
592 	if (!pmap_valid_page(pmap_find_phys(kernel_pmap, addr)) ||	\
593 	    dtrace_nofault_copy##bits(addr, &rval)) {			\
594 		*flags |= CPU_DTRACE_BADADDR;				\
595 		cpu_core[CPU->cpu_id].cpuc_dtrace_illval = addr;	\
596 		return (0);						\
597 	}								\
598 									\
599 	*flags &= ~CPU_DTRACE_NOFAULT;					\
600 	}								\
601 									\
602 	return (rval);							\
603 }
604 #else /* all other architectures */
605 #error Unknown Architecture
606 #endif
607 
608 #ifdef __LP64__
609 #define	dtrace_loadptr	dtrace_load64
610 #else
611 #define	dtrace_loadptr	dtrace_load32
612 #endif
613 
614 #define	DTRACE_DYNHASH_FREE	0
615 #define	DTRACE_DYNHASH_SINK	1
616 #define	DTRACE_DYNHASH_VALID	2
617 
618 #define DTRACE_MATCH_FAIL       -1
619 #define	DTRACE_MATCH_NEXT	0
620 #define	DTRACE_MATCH_DONE	1
621 #define	DTRACE_ANCHORED(probe)	((probe)->dtpr_func[0] != '\0')
622 #define	DTRACE_STATE_ALIGN	64
623 
624 #define	DTRACE_FLAGS2FLT(flags)						\
625 	(((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR :		\
626 	((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP :		\
627 	((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO :		\
628 	((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV :		\
629 	((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV :		\
630 	((flags) & CPU_DTRACE_TUPOFLOW) ?  DTRACEFLT_TUPOFLOW :		\
631 	((flags) & CPU_DTRACE_BADALIGN) ?  DTRACEFLT_BADALIGN :		\
632 	((flags) & CPU_DTRACE_NOSCRATCH) ?  DTRACEFLT_NOSCRATCH :	\
633 	((flags) & CPU_DTRACE_BADSTACK) ?  DTRACEFLT_BADSTACK :		\
634 	DTRACEFLT_UNKNOWN)
635 
636 #define	DTRACEACT_ISSTRING(act)						\
637 	((act)->dta_kind == DTRACEACT_DIFEXPR &&			\
638 	(act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING)
639 
640 
641 static size_t dtrace_strlen(const char *, size_t);
642 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id);
643 static void dtrace_enabling_provide(dtrace_provider_t *);
644 static int dtrace_enabling_match(dtrace_enabling_t *, int *, dtrace_match_cond_t *cond);
645 static void dtrace_enabling_matchall_with_cond(dtrace_match_cond_t *cond);
646 static void dtrace_enabling_matchall(void);
647 static dtrace_state_t *dtrace_anon_grab(void);
648 static uint64_t dtrace_helper(int, dtrace_mstate_t *,
649     dtrace_state_t *, uint64_t, uint64_t);
650 static dtrace_helpers_t *dtrace_helpers_create(proc_t *);
651 static void dtrace_buffer_drop(dtrace_buffer_t *);
652 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t,
653     dtrace_state_t *, dtrace_mstate_t *);
654 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t,
655     dtrace_optval_t);
656 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *, void *);
657 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *);
658 static int dtrace_canload_remains(uint64_t, size_t, size_t *,
659 	dtrace_mstate_t *, dtrace_vstate_t *);
660 static int dtrace_canstore_remains(uint64_t, size_t, size_t *,
661 	dtrace_mstate_t *, dtrace_vstate_t *);
662 
663 
664 /*
665  * DTrace sysctl handlers
666  *
667  * These declarations and functions are used for a deeper DTrace configuration.
668  * Most of them are not per-consumer basis and may impact the other DTrace
669  * consumers.  Correctness may not be supported for all the variables, so you
670  * should be careful about what values you are using.
671  */
672 
673 SYSCTL_DECL(_kern_dtrace);
674 SYSCTL_NODE(_kern, OID_AUTO, dtrace, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "dtrace");
675 
676 static int
677 sysctl_dtrace_err_verbose SYSCTL_HANDLER_ARGS
678 {
679 #pragma unused(oidp, arg2)
680 	int changed, error;
681 	int value = *(int *) arg1;
682 
683 	error = sysctl_io_number(req, value, sizeof(value), &value, &changed);
684 	if (error || !changed)
685 		return (error);
686 
687 	if (value != 0 && value != 1)
688 		return (ERANGE);
689 
690 	lck_mtx_lock(&dtrace_lock);
691 		dtrace_err_verbose = value;
692 	lck_mtx_unlock(&dtrace_lock);
693 
694 	return (0);
695 }
696 
697 /*
698  * kern.dtrace.err_verbose
699  *
700  * Set DTrace verbosity when an error occured (0 = disabled, 1 = enabld).
701  * Errors are reported when a DIFO or a DOF has been rejected by the kernel.
702  */
703 SYSCTL_PROC(_kern_dtrace, OID_AUTO, err_verbose,
704 	CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
705 	&dtrace_err_verbose, 0,
706 	sysctl_dtrace_err_verbose, "I", "dtrace error verbose");
707 
708 static int
709 sysctl_dtrace_buffer_memory_maxsize SYSCTL_HANDLER_ARGS
710 {
711 #pragma unused(oidp, arg2, req)
712 	int changed, error;
713 	uint64_t value = *(uint64_t *) arg1;
714 
715 	error = sysctl_io_number(req, value, sizeof(value), &value, &changed);
716 	if (error || !changed)
717 		return (error);
718 
719 	if (value <= dtrace_buffer_memory_inuse)
720 		return (ERANGE);
721 
722 	lck_mtx_lock(&dtrace_lock);
723 		dtrace_buffer_memory_maxsize = value;
724 	lck_mtx_unlock(&dtrace_lock);
725 
726 	return (0);
727 }
728 
729 /*
730  * kern.dtrace.buffer_memory_maxsize
731  *
732  * Set DTrace maximal size in bytes used by all the consumers' state buffers.  By default
733  * the limit is PHYS_MEM / 3 for *all* consumers.  Attempting to set a null, a negative value
734  * or a value <= to dtrace_buffer_memory_inuse will result in a failure.
735  */
736 SYSCTL_PROC(_kern_dtrace, OID_AUTO, buffer_memory_maxsize,
737 	CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
738 	&dtrace_buffer_memory_maxsize, 0,
739 	sysctl_dtrace_buffer_memory_maxsize, "Q", "dtrace state buffer memory maxsize");
740 
741 /*
742  * kern.dtrace.buffer_memory_inuse
743  *
744  * Current state buffer memory used, in bytes, by all the DTrace consumers.
745  * This value is read-only.
746  */
747 SYSCTL_QUAD(_kern_dtrace, OID_AUTO, buffer_memory_inuse, CTLFLAG_RD | CTLFLAG_LOCKED,
748 	&dtrace_buffer_memory_inuse, "dtrace state buffer memory in-use");
749 
750 static int
751 sysctl_dtrace_difo_maxsize SYSCTL_HANDLER_ARGS
752 {
753 #pragma unused(oidp, arg2, req)
754 	int changed, error;
755 	size_t value = *(size_t*) arg1;
756 
757 	error = sysctl_io_number(req, value, sizeof(value), &value, &changed);
758 	if (error || !changed)
759 		return (error);
760 
761 	if (value <= 0)
762 		return (ERANGE);
763 
764 	lck_mtx_lock(&dtrace_lock);
765 		dtrace_difo_maxsize = value;
766 	lck_mtx_unlock(&dtrace_lock);
767 
768 	return (0);
769 }
770 
771 /*
772  * kern.dtrace.difo_maxsize
773  *
774  * Set the DIFO max size in bytes, check the definition of dtrace_difo_maxsize
775  * to get the default value.  Attempting to set a null or negative size will
776  * result in a failure.
777  */
778 SYSCTL_PROC(_kern_dtrace, OID_AUTO, difo_maxsize,
779 	CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
780 	&dtrace_difo_maxsize, 0,
781 	sysctl_dtrace_difo_maxsize, "Q", "dtrace difo maxsize");
782 
783 static int
784 sysctl_dtrace_dof_maxsize SYSCTL_HANDLER_ARGS
785 {
786 #pragma unused(oidp, arg2, req)
787 	int changed, error;
788 	dtrace_optval_t value = *(dtrace_optval_t *) arg1;
789 
790 	error = sysctl_io_number(req, value, sizeof(value), &value, &changed);
791 	if (error || !changed)
792 		return (error);
793 
794 	if (value <= 0)
795 		return (ERANGE);
796 
797 	if (value >= dtrace_copy_maxsize())
798 		return (ERANGE);
799 
800 	lck_mtx_lock(&dtrace_lock);
801 		dtrace_dof_maxsize = value;
802 	lck_mtx_unlock(&dtrace_lock);
803 
804 	return (0);
805 }
806 
807 /*
808  * kern.dtrace.dof_maxsize
809  *
810  * Set the DOF max size in bytes, check the definition of dtrace_dof_maxsize to
811  * get the default value.  Attempting to set a null or negative size will result
812  * in a failure.
813  */
814 SYSCTL_PROC(_kern_dtrace, OID_AUTO, dof_maxsize,
815 	CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
816 	&dtrace_dof_maxsize, 0,
817 	sysctl_dtrace_dof_maxsize, "Q", "dtrace dof maxsize");
818 
819 static int
820 sysctl_dtrace_statvar_maxsize SYSCTL_HANDLER_ARGS
821 {
822 #pragma unused(oidp, arg2, req)
823 	int changed, error;
824 	dtrace_optval_t value = *(dtrace_optval_t*) arg1;
825 
826 	error = sysctl_io_number(req, value, sizeof(value), &value, &changed);
827 	if (error || !changed)
828 		return (error);
829 
830 	if (value <= 0)
831 		return (ERANGE);
832 	if (value > dtrace_statvar_maxsize_max)
833 		return (ERANGE);
834 
835 	lck_mtx_lock(&dtrace_lock);
836 		dtrace_statvar_maxsize = value;
837 	lck_mtx_unlock(&dtrace_lock);
838 
839 	return (0);
840 }
841 
842 /*
843  * kern.dtrace.global_maxsize
844  *
845  * Set the variable max size in bytes, check the definition of
846  * dtrace_statvar_maxsize to get the default value.  Attempting to set a null,
847  * too high or negative size will result in a failure.
848  */
849 SYSCTL_PROC(_kern_dtrace, OID_AUTO, global_maxsize,
850 	CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
851 	&dtrace_statvar_maxsize, 0,
852 	sysctl_dtrace_statvar_maxsize, "Q", "dtrace statvar maxsize");
853 
854 
855 /*
856  * kern.dtrace.provide_private_probes
857  *
858  * Set whether the providers must provide the private probes.  This is
859  * kept as compatibility as they are always provided.
860  */
861 SYSCTL_INT(_kern_dtrace, OID_AUTO, provide_private_probes,
862 	CTLFLAG_RD | CTLFLAG_LOCKED,
863 	(int *)NULL, 1, "provider must provide the private probes");
864 
865 /*
866  * kern.dtrace.dof_mode
867  *
868  * Returns the current DOF mode.
869  * This value is read-only.
870  */
871 SYSCTL_INT(_kern_dtrace, OID_AUTO, dof_mode, CTLFLAG_RD | CTLFLAG_LOCKED,
872 	&dtrace_dof_mode, 0, "dtrace dof mode");
873 
874 /*
875  * DTrace Probe Context Functions
876  *
877  * These functions are called from probe context.  Because probe context is
878  * any context in which C may be called, arbitrarily locks may be held,
879  * interrupts may be disabled, we may be in arbitrary dispatched state, etc.
880  * As a result, functions called from probe context may only call other DTrace
881  * support functions -- they may not interact at all with the system at large.
882  * (Note that the ASSERT macro is made probe-context safe by redefining it in
883  * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary
884  * loads are to be performed from probe context, they _must_ be in terms of
885  * the safe dtrace_load*() variants.
886  *
887  * Some functions in this block are not actually called from probe context;
888  * for these functions, there will be a comment above the function reading
889  * "Note:  not called from probe context."
890  */
891 
892 int
dtrace_assfail(const char * a,const char * f,int l)893 dtrace_assfail(const char *a, const char *f, int l)
894 {
895 	panic("dtrace: assertion failed: %s, file: %s, line: %d", a, f, l);
896 
897 	/*
898 	 * We just need something here that even the most clever compiler
899 	 * cannot optimize away.
900 	 */
901 	return (a[(uintptr_t)f]);
902 }
903 
904 /*
905  * Atomically increment a specified error counter from probe context.
906  */
907 static void
dtrace_error(uint32_t * counter)908 dtrace_error(uint32_t *counter)
909 {
910 	/*
911 	 * Most counters stored to in probe context are per-CPU counters.
912 	 * However, there are some error conditions that are sufficiently
913 	 * arcane that they don't merit per-CPU storage.  If these counters
914 	 * are incremented concurrently on different CPUs, scalability will be
915 	 * adversely affected -- but we don't expect them to be white-hot in a
916 	 * correctly constructed enabling...
917 	 */
918 	uint32_t oval, nval;
919 
920 	do {
921 		oval = *counter;
922 
923 		if ((nval = oval + 1) == 0) {
924 			/*
925 			 * If the counter would wrap, set it to 1 -- assuring
926 			 * that the counter is never zero when we have seen
927 			 * errors.  (The counter must be 32-bits because we
928 			 * aren't guaranteed a 64-bit compare&swap operation.)
929 			 * To save this code both the infamy of being fingered
930 			 * by a priggish news story and the indignity of being
931 			 * the target of a neo-puritan witch trial, we're
932 			 * carefully avoiding any colorful description of the
933 			 * likelihood of this condition -- but suffice it to
934 			 * say that it is only slightly more likely than the
935 			 * overflow of predicate cache IDs, as discussed in
936 			 * dtrace_predicate_create().
937 			 */
938 			nval = 1;
939 		}
940 	} while (dtrace_cas32(counter, oval, nval) != oval);
941 }
942 
943 /*
944  * Use the DTRACE_LOADFUNC macro to define functions for each of loading a
945  * uint8_t, a uint16_t, a uint32_t and a uint64_t.
946  */
947 DTRACE_LOADFUNC(8)
948 DTRACE_LOADFUNC(16)
949 DTRACE_LOADFUNC(32)
950 DTRACE_LOADFUNC(64)
951 
952 static int
dtrace_inscratch(uintptr_t dest,size_t size,dtrace_mstate_t * mstate)953 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate)
954 {
955 	if (dest < mstate->dtms_scratch_base)
956 		return (0);
957 
958 	if (dest + size < dest)
959 		return (0);
960 
961 	if (dest + size > mstate->dtms_scratch_ptr)
962 		return (0);
963 
964 	return (1);
965 }
966 
967 static int
dtrace_canstore_statvar(uint64_t addr,size_t sz,size_t * remain,dtrace_statvar_t ** svars,int nsvars)968 dtrace_canstore_statvar(uint64_t addr, size_t sz, size_t *remain,
969     dtrace_statvar_t **svars, int nsvars)
970 {
971 	int i;
972 
973 	size_t maxglobalsize, maxlocalsize;
974 
975 	maxglobalsize = dtrace_statvar_maxsize + sizeof (uint64_t);
976 	maxlocalsize = (maxglobalsize) * NCPU;
977 
978 	if (nsvars == 0)
979 		return (0);
980 
981 	for (i = 0; i < nsvars; i++) {
982 		dtrace_statvar_t *svar = svars[i];
983 		uint8_t scope;
984 		size_t size;
985 
986 		if (svar == NULL || (size = svar->dtsv_size) == 0)
987 			continue;
988 
989 		scope = svar->dtsv_var.dtdv_scope;
990 
991 		/**
992 		 * We verify that our size is valid in the spirit of providing
993 		 * defense in depth:  we want to prevent attackers from using
994 		 * DTrace to escalate an orthogonal kernel heap corruption bug
995 		 * into the ability to store to arbitrary locations in memory.
996 		 */
997 		VERIFY((scope == DIFV_SCOPE_GLOBAL && size <= maxglobalsize) ||
998 			(scope == DIFV_SCOPE_LOCAL && size <= maxlocalsize));
999 
1000 		if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) {
1001 			DTRACE_RANGE_REMAIN(remain, addr, svar->dtsv_data,
1002 				svar->dtsv_size);
1003 			return (1);
1004 		}
1005 	}
1006 
1007 	return (0);
1008 }
1009 
1010 /*
1011  * Check to see if the address is within a memory region to which a store may
1012  * be issued.  This includes the DTrace scratch areas, and any DTrace variable
1013  * region.  The caller of dtrace_canstore() is responsible for performing any
1014  * alignment checks that are needed before stores are actually executed.
1015  */
1016 static int
dtrace_canstore(uint64_t addr,size_t sz,dtrace_mstate_t * mstate,dtrace_vstate_t * vstate)1017 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
1018     dtrace_vstate_t *vstate)
1019 {
1020 	return (dtrace_canstore_remains(addr, sz, NULL, mstate, vstate));
1021 }
1022 /*
1023  * Implementation of dtrace_canstore which communicates the upper bound of the
1024  * allowed memory region.
1025  */
1026 static int
dtrace_canstore_remains(uint64_t addr,size_t sz,size_t * remain,dtrace_mstate_t * mstate,dtrace_vstate_t * vstate)1027 dtrace_canstore_remains(uint64_t addr, size_t sz, size_t *remain,
1028 	dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1029 {
1030 	/*
1031 	 * First, check to see if the address is in allocated scratch space...
1032 	 */
1033 	if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base,
1034 	    mstate->dtms_scratch_ptr - mstate->dtms_scratch_base)) {
1035 		DTRACE_RANGE_REMAIN(remain, addr, mstate->dtms_scratch_base,
1036 		    mstate->dtms_scratch_ptr - mstate->dtms_scratch_base);
1037 		return (1);
1038 	}
1039 	/*
1040 	 * Now check to see if it's a dynamic variable.  This check will pick
1041 	 * up both thread-local variables and any global dynamically-allocated
1042 	 * variables.
1043 	 */
1044 	if (DTRACE_INRANGE(addr, sz, (uintptr_t)vstate->dtvs_dynvars.dtds_base,
1045 	    vstate->dtvs_dynvars.dtds_size)) {
1046 		dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
1047 		uintptr_t base = (uintptr_t)dstate->dtds_base +
1048 		    (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t));
1049 		uintptr_t chunkoffs;
1050 		dtrace_dynvar_t *dvar;
1051 
1052 		/*
1053 		 * Before we assume that we can store here, we need to make
1054 		 * sure that it isn't in our metadata -- storing to our
1055 		 * dynamic variable metadata would corrupt our state.  For
1056 		 * the range to not include any dynamic variable metadata,
1057 		 * it must:
1058 		 *
1059 		 *	(1) Start above the hash table that is at the base of
1060 		 *	the dynamic variable space
1061 		 *
1062 		 *	(2) Have a starting chunk offset that is beyond the
1063 		 *	dtrace_dynvar_t that is at the base of every chunk
1064 		 *
1065 		 *	(3) Not span a chunk boundary
1066 		 *
1067 		 *	(4) Not be in the tuple space of a dynamic variable
1068 		 *
1069 		 */
1070 		if (addr < base)
1071 			return (0);
1072 
1073 		chunkoffs = (addr - base) % dstate->dtds_chunksize;
1074 
1075 		if (chunkoffs < sizeof (dtrace_dynvar_t))
1076 			return (0);
1077 
1078 		if (chunkoffs + sz > dstate->dtds_chunksize)
1079 			return (0);
1080 
1081 		dvar = (dtrace_dynvar_t *)((uintptr_t)addr - chunkoffs);
1082 
1083 		if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE)
1084 			return (0);
1085 
1086 		if (chunkoffs < sizeof (dtrace_dynvar_t) +
1087 			((dvar->dtdv_tuple.dtt_nkeys - 1) * sizeof (dtrace_key_t)))
1088 			return (0);
1089 
1090 		return (1);
1091 	}
1092 
1093 	/*
1094 	 * Finally, check the static local and global variables.  These checks
1095 	 * take the longest, so we perform them last.
1096 	 */
1097 	if (dtrace_canstore_statvar(addr, sz, remain,
1098 	    vstate->dtvs_locals, vstate->dtvs_nlocals))
1099 		return (1);
1100 
1101 	if (dtrace_canstore_statvar(addr, sz, remain,
1102 	    vstate->dtvs_globals, vstate->dtvs_nglobals))
1103 		return (1);
1104 
1105 	return (0);
1106 }
1107 
1108 
1109 /*
1110  * Convenience routine to check to see if the address is within a memory
1111  * region in which a load may be issued given the user's privilege level;
1112  * if not, it sets the appropriate error flags and loads 'addr' into the
1113  * illegal value slot.
1114  *
1115  * DTrace subroutines (DIF_SUBR_*) should use this helper to implement
1116  * appropriate memory access protection.
1117  */
1118 int
dtrace_canload(uint64_t addr,size_t sz,dtrace_mstate_t * mstate,dtrace_vstate_t * vstate)1119 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
1120     dtrace_vstate_t *vstate)
1121 {
1122 	return (dtrace_canload_remains(addr, sz, NULL, mstate, vstate));
1123 }
1124 
1125 /*
1126  * Implementation of dtrace_canload which communicates the upper bound of the
1127  * allowed memory region.
1128  */
1129 static int
dtrace_canload_remains(uint64_t addr,size_t sz,size_t * remain,dtrace_mstate_t * mstate,dtrace_vstate_t * vstate)1130 dtrace_canload_remains(uint64_t addr, size_t sz, size_t *remain,
1131 	dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1132 {
1133 	volatile uint64_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
1134 
1135 	/*
1136 	 * If we hold the privilege to read from kernel memory, then
1137 	 * everything is readable.
1138 	 */
1139 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) {
1140 		DTRACE_RANGE_REMAIN(remain, addr, addr, sz);
1141 		return (1);
1142 	}
1143 
1144 	/*
1145 	 * You can obviously read that which you can store.
1146 	 */
1147 	if (dtrace_canstore_remains(addr, sz, remain, mstate, vstate))
1148 		return (1);
1149 
1150 	/*
1151 	 * We're allowed to read from our own string table.
1152 	 */
1153 	if (DTRACE_INRANGE(addr, sz, (uintptr_t)mstate->dtms_difo->dtdo_strtab,
1154 	    mstate->dtms_difo->dtdo_strlen)) {
1155 		DTRACE_RANGE_REMAIN(remain, addr,
1156 			mstate->dtms_difo->dtdo_strtab,
1157 			mstate->dtms_difo->dtdo_strlen);
1158 		return (1);
1159 	}
1160 
1161 	DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV);
1162 	*illval = addr;
1163 	return (0);
1164 }
1165 
1166 /*
1167  * Convenience routine to check to see if a given string is within a memory
1168  * region in which a load may be issued given the user's privilege level;
1169  * this exists so that we don't need to issue unnecessary dtrace_strlen()
1170  * calls in the event that the user has all privileges.
1171  */
1172 static int
dtrace_strcanload(uint64_t addr,size_t sz,size_t * remain,dtrace_mstate_t * mstate,dtrace_vstate_t * vstate)1173 dtrace_strcanload(uint64_t addr, size_t sz, size_t *remain,
1174 	dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1175 {
1176 	size_t rsize = 0;
1177 
1178 	/*
1179 	 * If we hold the privilege to read from kernel memory, then
1180 	 * everything is readable.
1181 	 */
1182 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) {
1183 		DTRACE_RANGE_REMAIN(remain, addr, addr, sz);
1184 		return (1);
1185 	}
1186 
1187 	/*
1188 	 * Even if the caller is uninterested in querying the remaining valid
1189 	 * range, it is required to ensure that the access is allowed.
1190 	 */
1191 	if (remain == NULL) {
1192 		remain = &rsize;
1193 	}
1194 	if (dtrace_canload_remains(addr, 0, remain, mstate, vstate)) {
1195 		size_t strsz;
1196 		/*
1197 		 * Perform the strlen after determining the length of the
1198 		 * memory region which is accessible.  This prevents timing
1199 		 * information from being used to find NULs in memory which is
1200 		 * not accessible to the caller.
1201 		 */
1202 		strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr,
1203 			MIN(sz, *remain));
1204 		if (strsz <= *remain) {
1205 			return (1);
1206 		}
1207 	}
1208 
1209 	return (0);
1210 }
1211 
1212 /*
1213  * Convenience routine to check to see if a given variable is within a memory
1214  * region in which a load may be issued given the user's privilege level.
1215  */
1216 static int
dtrace_vcanload(void * src,dtrace_diftype_t * type,size_t * remain,dtrace_mstate_t * mstate,dtrace_vstate_t * vstate)1217 dtrace_vcanload(void *src, dtrace_diftype_t *type, size_t *remain,
1218 	dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1219 {
1220 	size_t sz;
1221 	ASSERT(type->dtdt_flags & DIF_TF_BYREF);
1222 
1223 	/*
1224 	 * Calculate the max size before performing any checks since even
1225 	 * DTRACE_ACCESS_KERNEL-credentialed callers expect that this function
1226 	 * return the max length via 'remain'.
1227 	 */
1228 	if (type->dtdt_kind == DIF_TYPE_STRING) {
1229 		dtrace_state_t *state = vstate->dtvs_state;
1230 
1231 		if (state != NULL) {
1232 			sz = state->dts_options[DTRACEOPT_STRSIZE];
1233 		} else {
1234 			/*
1235 			 * In helper context, we have a NULL state; fall back
1236 			 * to using the system-wide default for the string size
1237 			 * in this case.
1238 			 */
1239 			sz = dtrace_strsize_default;
1240 		}
1241 	} else {
1242 		sz = type->dtdt_size;
1243 	}
1244 
1245 	/*
1246 	 * If we hold the privilege to read from kernel memory, then
1247 	 * everything is readable.
1248 	 */
1249 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) {
1250 		DTRACE_RANGE_REMAIN(remain, (uintptr_t)src, src, sz);
1251 		return (1);
1252 	}
1253 
1254 	if (type->dtdt_kind == DIF_TYPE_STRING) {
1255 		return (dtrace_strcanload((uintptr_t)src, sz, remain, mstate,
1256 			vstate));
1257 	}
1258 	return (dtrace_canload_remains((uintptr_t)src, sz, remain, mstate,
1259 		vstate));
1260 }
1261 
1262 #define	isdigit(ch)	((ch) >= '0' && (ch) <= '9')
1263 #define	islower(ch)	((ch) >= 'a' && (ch) <= 'z')
1264 #define	isspace(ch)	(((ch) == ' ') || ((ch) == '\r') || ((ch) == '\n') || \
1265 			((ch) == '\t') || ((ch) == '\f'))
1266 #define	isxdigit(ch)	(isdigit(ch) || ((ch) >= 'a' && (ch) <= 'f') || \
1267 			((ch) >= 'A' && (ch) <= 'F'))
1268 #define	lisalnum(x)	\
1269 	(isdigit(x) || ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
1270 
1271 #define	DIGIT(x)	\
1272 	(isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
1273 
1274 /*
1275  * Convert a string to a signed integer using safe loads.
1276  */
1277 static int64_t
dtrace_strtoll(char * input,int base,size_t limit)1278 dtrace_strtoll(char *input, int base, size_t limit)
1279 {
1280 	uintptr_t pos = (uintptr_t)input;
1281 	int64_t val = 0;
1282 	int x;
1283 	boolean_t neg = B_FALSE;
1284 	char c, cc, ccc;
1285 	uintptr_t end = pos + limit;
1286 
1287 	/*
1288 	 * Consume any whitespace preceding digits.
1289 	 */
1290 	while ((c = dtrace_load8(pos)) == ' ' || c == '\t')
1291 		pos++;
1292 
1293 	/*
1294 	 * Handle an explicit sign if one is present.
1295 	 */
1296 	if (c == '-' || c == '+') {
1297 		if (c == '-')
1298 			neg = B_TRUE;
1299 		c = dtrace_load8(++pos);
1300 	}
1301 
1302 	/*
1303 	 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it
1304 	 * if present.
1305 	 */
1306 	if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' ||
1307 	    cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) {
1308 		pos += 2;
1309 		c = ccc;
1310 	}
1311 
1312 	/*
1313 	 * Read in contiguous digits until the first non-digit character.
1314 	 */
1315 	for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base;
1316 	    c = dtrace_load8(++pos))
1317 		val = val * base + x;
1318 
1319 	return (neg ? -val : val);
1320 }
1321 
1322 
1323 /*
1324  * Compare two strings using safe loads.
1325  */
1326 static int
dtrace_strncmp(const char * s1,const char * s2,size_t limit)1327 dtrace_strncmp(const char *s1, const char *s2, size_t limit)
1328 {
1329 	uint8_t c1, c2;
1330 	volatile uint16_t *flags;
1331 
1332 	if (s1 == s2 || limit == 0)
1333 		return (0);
1334 
1335 	flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
1336 
1337 	do {
1338 		if (s1 == NULL) {
1339 			c1 = '\0';
1340 		} else {
1341 			c1 = dtrace_load8((uintptr_t)s1++);
1342 		}
1343 
1344 		if (s2 == NULL) {
1345 			c2 = '\0';
1346 		} else {
1347 			c2 = dtrace_load8((uintptr_t)s2++);
1348 		}
1349 
1350 		if (c1 != c2)
1351 			return (c1 - c2);
1352 	} while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT));
1353 
1354 	return (0);
1355 }
1356 
1357 /*
1358  * Compute strlen(s) for a string using safe memory accesses.  The additional
1359  * len parameter is used to specify a maximum length to ensure completion.
1360  */
1361 static size_t
dtrace_strlen(const char * s,size_t lim)1362 dtrace_strlen(const char *s, size_t lim)
1363 {
1364 	uint_t len;
1365 
1366 	for (len = 0; len != lim; len++) {
1367 		if (dtrace_load8((uintptr_t)s++) == '\0')
1368 			break;
1369 	}
1370 
1371 	return (len);
1372 }
1373 
1374 /*
1375  * Check if an address falls within a toxic region.
1376  */
1377 static int
dtrace_istoxic(uintptr_t kaddr,size_t size)1378 dtrace_istoxic(uintptr_t kaddr, size_t size)
1379 {
1380 	uintptr_t taddr, tsize;
1381 	int i;
1382 
1383 	for (i = 0; i < dtrace_toxranges; i++) {
1384 		taddr = dtrace_toxrange[i].dtt_base;
1385 		tsize = dtrace_toxrange[i].dtt_limit - taddr;
1386 
1387 		if (kaddr - taddr < tsize) {
1388 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1389 			cpu_core[CPU->cpu_id].cpuc_dtrace_illval = kaddr;
1390 			return (1);
1391 		}
1392 
1393 		if (taddr - kaddr < size) {
1394 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1395 			cpu_core[CPU->cpu_id].cpuc_dtrace_illval = taddr;
1396 			return (1);
1397 		}
1398 	}
1399 
1400 	return (0);
1401 }
1402 
1403 /*
1404  * Copy src to dst using safe memory accesses.  The src is assumed to be unsafe
1405  * memory specified by the DIF program.  The dst is assumed to be safe memory
1406  * that we can store to directly because it is managed by DTrace.  As with
1407  * standard bcopy, overlapping copies are handled properly.
1408  */
1409 static void
dtrace_bcopy(const void * src,void * dst,size_t len)1410 dtrace_bcopy(const void *src, void *dst, size_t len)
1411 {
1412 	if (len != 0) {
1413 		uint8_t *s1 = dst;
1414 		const uint8_t *s2 = src;
1415 
1416 		if (s1 <= s2) {
1417 			do {
1418 				*s1++ = dtrace_load8((uintptr_t)s2++);
1419 			} while (--len != 0);
1420 		} else {
1421 			s2 += len;
1422 			s1 += len;
1423 
1424 			do {
1425 				*--s1 = dtrace_load8((uintptr_t)--s2);
1426 			} while (--len != 0);
1427 		}
1428 	}
1429 }
1430 
1431 /*
1432  * Copy src to dst using safe memory accesses, up to either the specified
1433  * length, or the point that a nul byte is encountered.  The src is assumed to
1434  * be unsafe memory specified by the DIF program.  The dst is assumed to be
1435  * safe memory that we can store to directly because it is managed by DTrace.
1436  * Unlike dtrace_bcopy(), overlapping regions are not handled.
1437  */
1438 static void
dtrace_strcpy(const void * src,void * dst,size_t len)1439 dtrace_strcpy(const void *src, void *dst, size_t len)
1440 {
1441 	if (len != 0) {
1442 		uint8_t *s1 = dst, c;
1443 		const uint8_t *s2 = src;
1444 
1445 		do {
1446 			*s1++ = c = dtrace_load8((uintptr_t)s2++);
1447 		} while (--len != 0 && c != '\0');
1448 	}
1449 }
1450 
1451 /*
1452  * Copy src to dst, deriving the size and type from the specified (BYREF)
1453  * variable type.  The src is assumed to be unsafe memory specified by the DIF
1454  * program.  The dst is assumed to be DTrace variable memory that is of the
1455  * specified type; we assume that we can store to directly.
1456  */
1457 static void
dtrace_vcopy(void * src,void * dst,dtrace_diftype_t * type,size_t limit)1458 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type, size_t limit)
1459 {
1460 	ASSERT(type->dtdt_flags & DIF_TF_BYREF);
1461 
1462 	if (type->dtdt_kind == DIF_TYPE_STRING) {
1463 		dtrace_strcpy(src, dst, MIN(type->dtdt_size, limit));
1464 	} else {
1465 		dtrace_bcopy(src, dst, MIN(type->dtdt_size, limit));
1466 	}
1467 }
1468 
1469 /*
1470  * Compare s1 to s2 using safe memory accesses.  The s1 data is assumed to be
1471  * unsafe memory specified by the DIF program.  The s2 data is assumed to be
1472  * safe memory that we can access directly because it is managed by DTrace.
1473  */
1474 static int
dtrace_bcmp(const void * s1,const void * s2,size_t len)1475 dtrace_bcmp(const void *s1, const void *s2, size_t len)
1476 {
1477 	volatile uint16_t *flags;
1478 
1479 	flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
1480 
1481 	if (s1 == s2)
1482 		return (0);
1483 
1484 	if (s1 == NULL || s2 == NULL)
1485 		return (1);
1486 
1487 	if (s1 != s2 && len != 0) {
1488 		const uint8_t *ps1 = s1;
1489 		const uint8_t *ps2 = s2;
1490 
1491 		do {
1492 			if (dtrace_load8((uintptr_t)ps1++) != *ps2++)
1493 				return (1);
1494 		} while (--len != 0 && !(*flags & CPU_DTRACE_FAULT));
1495 	}
1496 	return (0);
1497 }
1498 
1499 /*
1500  * Zero the specified region using a simple byte-by-byte loop.  Note that this
1501  * is for safe DTrace-managed memory only.
1502  */
1503 static void
dtrace_bzero(void * dst,size_t len)1504 dtrace_bzero(void *dst, size_t len)
1505 {
1506 	uchar_t *cp;
1507 
1508 	for (cp = dst; len != 0; len--)
1509 		*cp++ = 0;
1510 }
1511 
1512 static void
dtrace_add_128(uint64_t * addend1,uint64_t * addend2,uint64_t * sum)1513 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
1514 {
1515 	uint64_t result[2];
1516 
1517 	result[0] = addend1[0] + addend2[0];
1518 	result[1] = addend1[1] + addend2[1] +
1519 	    (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
1520 
1521 	sum[0] = result[0];
1522 	sum[1] = result[1];
1523 }
1524 
1525 /*
1526  * Shift the 128-bit value in a by b. If b is positive, shift left.
1527  * If b is negative, shift right.
1528  */
1529 static void
dtrace_shift_128(uint64_t * a,int b)1530 dtrace_shift_128(uint64_t *a, int b)
1531 {
1532 	uint64_t mask;
1533 
1534 	if (b == 0)
1535 		return;
1536 
1537 	if (b < 0) {
1538 		b = -b;
1539 		if (b >= 64) {
1540 			a[0] = a[1] >> (b - 64);
1541 			a[1] = 0;
1542 		} else {
1543 			a[0] >>= b;
1544 			mask = 1LL << (64 - b);
1545 			mask -= 1;
1546 			a[0] |= ((a[1] & mask) << (64 - b));
1547 			a[1] >>= b;
1548 		}
1549 	} else {
1550 		if (b >= 64) {
1551 			a[1] = a[0] << (b - 64);
1552 			a[0] = 0;
1553 		} else {
1554 			a[1] <<= b;
1555 			mask = a[0] >> (64 - b);
1556 			a[1] |= mask;
1557 			a[0] <<= b;
1558 		}
1559 	}
1560 }
1561 
1562 /*
1563  * The basic idea is to break the 2 64-bit values into 4 32-bit values,
1564  * use native multiplication on those, and then re-combine into the
1565  * resulting 128-bit value.
1566  *
1567  * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
1568  *     hi1 * hi2 << 64 +
1569  *     hi1 * lo2 << 32 +
1570  *     hi2 * lo1 << 32 +
1571  *     lo1 * lo2
1572  */
1573 static void
dtrace_multiply_128(uint64_t factor1,uint64_t factor2,uint64_t * product)1574 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
1575 {
1576 	uint64_t hi1, hi2, lo1, lo2;
1577 	uint64_t tmp[2];
1578 
1579 	hi1 = factor1 >> 32;
1580 	hi2 = factor2 >> 32;
1581 
1582 	lo1 = factor1 & DT_MASK_LO;
1583 	lo2 = factor2 & DT_MASK_LO;
1584 
1585 	product[0] = lo1 * lo2;
1586 	product[1] = hi1 * hi2;
1587 
1588 	tmp[0] = hi1 * lo2;
1589 	tmp[1] = 0;
1590 	dtrace_shift_128(tmp, 32);
1591 	dtrace_add_128(product, tmp, product);
1592 
1593 	tmp[0] = hi2 * lo1;
1594 	tmp[1] = 0;
1595 	dtrace_shift_128(tmp, 32);
1596 	dtrace_add_128(product, tmp, product);
1597 }
1598 
1599 /*
1600  * This privilege check should be used by actions and subroutines to
1601  * verify that the user credentials of the process that enabled the
1602  * invoking ECB match the target credentials
1603  */
1604 static int
dtrace_priv_proc_common_user(dtrace_state_t * state)1605 dtrace_priv_proc_common_user(dtrace_state_t *state)
1606 {
1607 	cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1608 
1609 	/*
1610 	 * We should always have a non-NULL state cred here, since if cred
1611 	 * is null (anonymous tracing), we fast-path bypass this routine.
1612 	 */
1613 	ASSERT(s_cr != NULL);
1614 
1615 	if ((cr = dtrace_CRED()) != NULL &&
1616 	    posix_cred_get(s_cr)->cr_uid == posix_cred_get(cr)->cr_uid &&
1617 	    posix_cred_get(s_cr)->cr_uid == posix_cred_get(cr)->cr_ruid &&
1618 	    posix_cred_get(s_cr)->cr_uid == posix_cred_get(cr)->cr_suid &&
1619 	    posix_cred_get(s_cr)->cr_gid == posix_cred_get(cr)->cr_gid &&
1620 	    posix_cred_get(s_cr)->cr_gid == posix_cred_get(cr)->cr_rgid &&
1621 	    posix_cred_get(s_cr)->cr_gid == posix_cred_get(cr)->cr_sgid)
1622 		return (1);
1623 
1624 	return (0);
1625 }
1626 
1627 /*
1628  * This privilege check should be used by actions and subroutines to
1629  * verify that the zone of the process that enabled the invoking ECB
1630  * matches the target credentials
1631  */
1632 static int
dtrace_priv_proc_common_zone(dtrace_state_t * state)1633 dtrace_priv_proc_common_zone(dtrace_state_t *state)
1634 {
1635 	cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1636 #pragma unused(cr, s_cr, state) /* __APPLE__ */
1637 
1638 	/*
1639 	 * We should always have a non-NULL state cred here, since if cred
1640 	 * is null (anonymous tracing), we fast-path bypass this routine.
1641 	 */
1642 	ASSERT(s_cr != NULL);
1643 
1644 	return 1; /* APPLE NOTE: Darwin doesn't do zones. */
1645 }
1646 
1647 /*
1648  * This privilege check should be used by actions and subroutines to
1649  * verify that the process has not setuid or changed credentials.
1650  */
1651 static int
dtrace_priv_proc_common_nocd(void)1652 dtrace_priv_proc_common_nocd(void)
1653 {
1654 	return 1; /* Darwin omits "No Core Dump" flag. */
1655 }
1656 
1657 static int
dtrace_priv_proc_destructive(dtrace_state_t * state)1658 dtrace_priv_proc_destructive(dtrace_state_t *state)
1659 {
1660 	int action = state->dts_cred.dcr_action;
1661 
1662 	if (ISSET(current_proc()->p_lflag, P_LNOATTACH))
1663 		goto bad;
1664 
1665 	if (dtrace_is_restricted() && !dtrace_can_attach_to_proc(current_proc()))
1666 		goto bad;
1667 
1668 	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) &&
1669 	    dtrace_priv_proc_common_zone(state) == 0)
1670 		goto bad;
1671 
1672 	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) &&
1673 	    dtrace_priv_proc_common_user(state) == 0)
1674 		goto bad;
1675 
1676 	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) &&
1677 	    dtrace_priv_proc_common_nocd() == 0)
1678 		goto bad;
1679 
1680 	return (1);
1681 
1682 bad:
1683 	cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1684 
1685 	return (0);
1686 }
1687 
1688 static int
dtrace_priv_proc_control(dtrace_state_t * state)1689 dtrace_priv_proc_control(dtrace_state_t *state)
1690 {
1691 	if (ISSET(current_proc()->p_lflag, P_LNOATTACH))
1692 		goto bad;
1693 
1694 	if (dtrace_is_restricted() && !dtrace_can_attach_to_proc(current_proc()))
1695 		goto bad;
1696 
1697 	if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL)
1698 		return (1);
1699 
1700 	if (dtrace_priv_proc_common_zone(state) &&
1701 	    dtrace_priv_proc_common_user(state) &&
1702 	    dtrace_priv_proc_common_nocd())
1703 		return (1);
1704 
1705 bad:
1706 	cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1707 
1708 	return (0);
1709 }
1710 
1711 static int
dtrace_priv_proc(dtrace_state_t * state)1712 dtrace_priv_proc(dtrace_state_t *state)
1713 {
1714 	if (ISSET(current_proc()->p_lflag, P_LNOATTACH))
1715 		goto bad;
1716 
1717 	if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed() && !dtrace_can_attach_to_proc(current_proc()))
1718 		goto bad;
1719 
1720 	if (state->dts_cred.dcr_action & DTRACE_CRA_PROC)
1721 		return (1);
1722 
1723 bad:
1724 	cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1725 
1726 	return (0);
1727 }
1728 
1729 /*
1730  * The P_LNOATTACH check is an Apple specific check.
1731  * We need a version of dtrace_priv_proc() that omits
1732  * that check for PID and EXECNAME accesses
1733  */
1734 static int
dtrace_priv_proc_relaxed(dtrace_state_t * state)1735 dtrace_priv_proc_relaxed(dtrace_state_t *state)
1736 {
1737 
1738 	if (state->dts_cred.dcr_action & DTRACE_CRA_PROC)
1739 		return (1);
1740 
1741 	cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1742 
1743 	return (0);
1744 }
1745 
1746 static int
dtrace_priv_kernel(dtrace_state_t * state)1747 dtrace_priv_kernel(dtrace_state_t *state)
1748 {
1749 	if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed())
1750 		goto bad;
1751 
1752 	if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL)
1753 		return (1);
1754 
1755 bad:
1756 	cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1757 
1758 	return (0);
1759 }
1760 
1761 static int
dtrace_priv_kernel_destructive(dtrace_state_t * state)1762 dtrace_priv_kernel_destructive(dtrace_state_t *state)
1763 {
1764 	if (dtrace_is_restricted())
1765 		goto bad;
1766 
1767 	if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE)
1768 		return (1);
1769 
1770 bad:
1771 	cpu_core[CPU->cpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1772 
1773 	return (0);
1774 }
1775 
1776 /*
1777  * Note:  not called from probe context.  This function is called
1778  * asynchronously (and at a regular interval) from outside of probe context to
1779  * clean the dirty dynamic variable lists on all CPUs.  Dynamic variable
1780  * cleaning is explained in detail in <sys/dtrace_impl.h>.
1781  */
1782 static void
dtrace_dynvar_clean(dtrace_dstate_t * dstate)1783 dtrace_dynvar_clean(dtrace_dstate_t *dstate)
1784 {
1785 	dtrace_dynvar_t *dirty;
1786 	int work = 0;
1787 
1788 	zpercpu_foreach(dcpu, dstate->dtds_percpu) {
1789 		ASSERT(dcpu->dtdsc_rinsing == NULL);
1790 
1791 		/*
1792 		 * If the dirty list is NULL, there is no dirty work to do.
1793 		 */
1794 		if (dcpu->dtdsc_dirty == NULL)
1795 			continue;
1796 
1797 		/*
1798 		 * If the clean list is non-NULL, then we're not going to do
1799 		 * any work for this CPU -- it means that there has not been
1800 		 * a dtrace_dynvar() allocation on this CPU (or from this CPU)
1801 		 * since the last time we cleaned house.
1802 		 */
1803 		if (dcpu->dtdsc_clean != NULL)
1804 			continue;
1805 
1806 		work = 1;
1807 
1808 		/*
1809 		 * Atomically move the dirty list aside.
1810 		 */
1811 		do {
1812 			dirty = dcpu->dtdsc_dirty;
1813 
1814 			/*
1815 			 * Before we zap the dirty list, set the rinsing list.
1816 			 * (This allows for a potential assertion in
1817 			 * dtrace_dynvar():  if a free dynamic variable appears
1818 			 * on a hash chain, either the dirty list or the
1819 			 * rinsing list for some CPU must be non-NULL.)
1820 			 */
1821 			dcpu->dtdsc_rinsing = dirty;
1822 			dtrace_membar_producer();
1823 		} while (dtrace_casptr(&dcpu->dtdsc_dirty,
1824 		    dirty, NULL) != dirty);
1825 	}
1826 
1827 	if (!work) {
1828 		/*
1829 		 * We have no work to do; we can simply return.
1830 		 */
1831 		return;
1832 	}
1833 
1834 	dtrace_sync();
1835 
1836 	zpercpu_foreach(dcpu, dstate->dtds_percpu) {
1837 		if (dcpu->dtdsc_rinsing == NULL)
1838 			continue;
1839 
1840 		/*
1841 		 * We are now guaranteed that no hash chain contains a pointer
1842 		 * into this dirty list; we can make it clean.
1843 		 */
1844 		ASSERT(dcpu->dtdsc_clean == NULL);
1845 		dcpu->dtdsc_clean = dcpu->dtdsc_rinsing;
1846 		dcpu->dtdsc_rinsing = NULL;
1847 	}
1848 
1849 	/*
1850 	 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make
1851 	 * sure that all CPUs have seen all of the dtdsc_clean pointers.
1852 	 * This prevents a race whereby a CPU incorrectly decides that
1853 	 * the state should be something other than DTRACE_DSTATE_CLEAN
1854 	 * after dtrace_dynvar_clean() has completed.
1855 	 */
1856 	dtrace_sync();
1857 
1858 	dstate->dtds_state = DTRACE_DSTATE_CLEAN;
1859 }
1860 
1861 /*
1862  * Depending on the value of the op parameter, this function looks-up,
1863  * allocates or deallocates an arbitrarily-keyed dynamic variable.  If an
1864  * allocation is requested, this function will return a pointer to a
1865  * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no
1866  * variable can be allocated.  If NULL is returned, the appropriate counter
1867  * will be incremented.
1868  */
1869 static dtrace_dynvar_t *
dtrace_dynvar(dtrace_dstate_t * dstate,uint_t nkeys,dtrace_key_t * key,size_t dsize,dtrace_dynvar_op_t op,dtrace_mstate_t * mstate,dtrace_vstate_t * vstate)1870 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys,
1871     dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op,
1872     dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1873 {
1874 	uint64_t hashval = DTRACE_DYNHASH_VALID;
1875 	dtrace_dynhash_t *hash = dstate->dtds_hash;
1876 	dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL;
1877 	processorid_t me = CPU->cpu_id, cpu = me;
1878 	dtrace_dstate_percpu_t *dcpu = zpercpu_get_cpu(dstate->dtds_percpu, me);
1879 	size_t bucket, ksize;
1880 	size_t chunksize = dstate->dtds_chunksize;
1881 	uintptr_t kdata, lock, nstate;
1882 	uint_t i;
1883 
1884 	ASSERT(nkeys != 0);
1885 
1886 	/*
1887 	 * Hash the key.  As with aggregations, we use Jenkins' "One-at-a-time"
1888 	 * algorithm.  For the by-value portions, we perform the algorithm in
1889 	 * 16-bit chunks (as opposed to 8-bit chunks).  This speeds things up a
1890 	 * bit, and seems to have only a minute effect on distribution.  For
1891 	 * the by-reference data, we perform "One-at-a-time" iterating (safely)
1892 	 * over each referenced byte.  It's painful to do this, but it's much
1893 	 * better than pathological hash distribution.  The efficacy of the
1894 	 * hashing algorithm (and a comparison with other algorithms) may be
1895 	 * found by running the ::dtrace_dynstat MDB dcmd.
1896 	 */
1897 	for (i = 0; i < nkeys; i++) {
1898 		if (key[i].dttk_size == 0) {
1899 			uint64_t val = key[i].dttk_value;
1900 
1901 			hashval += (val >> 48) & 0xffff;
1902 			hashval += (hashval << 10);
1903 			hashval ^= (hashval >> 6);
1904 
1905 			hashval += (val >> 32) & 0xffff;
1906 			hashval += (hashval << 10);
1907 			hashval ^= (hashval >> 6);
1908 
1909 			hashval += (val >> 16) & 0xffff;
1910 			hashval += (hashval << 10);
1911 			hashval ^= (hashval >> 6);
1912 
1913 			hashval += val & 0xffff;
1914 			hashval += (hashval << 10);
1915 			hashval ^= (hashval >> 6);
1916 		} else {
1917 			/*
1918 			 * This is incredibly painful, but it beats the hell
1919 			 * out of the alternative.
1920 			 */
1921 			uint64_t j, size = key[i].dttk_size;
1922 			uintptr_t base = (uintptr_t)key[i].dttk_value;
1923 
1924 			if (!dtrace_canload(base, size, mstate, vstate))
1925 				break;
1926 
1927 			for (j = 0; j < size; j++) {
1928 				hashval += dtrace_load8(base + j);
1929 				hashval += (hashval << 10);
1930 				hashval ^= (hashval >> 6);
1931 			}
1932 		}
1933 	}
1934 
1935 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
1936 		return (NULL);
1937 
1938 	hashval += (hashval << 3);
1939 	hashval ^= (hashval >> 11);
1940 	hashval += (hashval << 15);
1941 
1942 	/*
1943 	 * There is a remote chance (ideally, 1 in 2^31) that our hashval
1944 	 * comes out to be one of our two sentinel hash values.  If this
1945 	 * actually happens, we set the hashval to be a value known to be a
1946 	 * non-sentinel value.
1947 	 */
1948 	if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK)
1949 		hashval = DTRACE_DYNHASH_VALID;
1950 
1951 	/*
1952 	 * Yes, it's painful to do a divide here.  If the cycle count becomes
1953 	 * important here, tricks can be pulled to reduce it.  (However, it's
1954 	 * critical that hash collisions be kept to an absolute minimum;
1955 	 * they're much more painful than a divide.)  It's better to have a
1956 	 * solution that generates few collisions and still keeps things
1957 	 * relatively simple.
1958 	 */
1959 	bucket = hashval % dstate->dtds_hashsize;
1960 
1961 	if (op == DTRACE_DYNVAR_DEALLOC) {
1962 		volatile uintptr_t *lockp = &hash[bucket].dtdh_lock;
1963 
1964 		for (;;) {
1965 			while ((lock = *lockp) & 1)
1966 				continue;
1967 
1968 			if (dtrace_casptr((void *)(uintptr_t)lockp,
1969 			    (void *)lock, (void *)(lock + 1)) == (void *)lock)
1970 				break;
1971 		}
1972 
1973 		dtrace_membar_producer();
1974 	}
1975 
1976 top:
1977 	prev = NULL;
1978 	lock = hash[bucket].dtdh_lock;
1979 
1980 	dtrace_membar_consumer();
1981 
1982 	start = hash[bucket].dtdh_chain;
1983 	ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK ||
1984 	    start->dtdv_hashval != DTRACE_DYNHASH_FREE ||
1985 	    op != DTRACE_DYNVAR_DEALLOC));
1986 
1987 	for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) {
1988 		dtrace_tuple_t *dtuple = &dvar->dtdv_tuple;
1989 		dtrace_key_t *dkey = &dtuple->dtt_key[0];
1990 
1991 		if (dvar->dtdv_hashval != hashval) {
1992 			if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) {
1993 				/*
1994 				 * We've reached the sink, and therefore the
1995 				 * end of the hash chain; we can kick out of
1996 				 * the loop knowing that we have seen a valid
1997 				 * snapshot of state.
1998 				 */
1999 				ASSERT(dvar->dtdv_next == NULL);
2000 				ASSERT(dvar == &dtrace_dynhash_sink);
2001 				break;
2002 			}
2003 
2004 			if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) {
2005 				/*
2006 				 * We've gone off the rails:  somewhere along
2007 				 * the line, one of the members of this hash
2008 				 * chain was deleted.  Note that we could also
2009 				 * detect this by simply letting this loop run
2010 				 * to completion, as we would eventually hit
2011 				 * the end of the dirty list.  However, we
2012 				 * want to avoid running the length of the
2013 				 * dirty list unnecessarily (it might be quite
2014 				 * long), so we catch this as early as
2015 				 * possible by detecting the hash marker.  In
2016 				 * this case, we simply set dvar to NULL and
2017 				 * break; the conditional after the loop will
2018 				 * send us back to top.
2019 				 */
2020 				dvar = NULL;
2021 				break;
2022 			}
2023 
2024 			goto next;
2025 		}
2026 
2027 		if (dtuple->dtt_nkeys != nkeys)
2028 			goto next;
2029 
2030 		for (i = 0; i < nkeys; i++, dkey++) {
2031 			if (dkey->dttk_size != key[i].dttk_size)
2032 				goto next; /* size or type mismatch */
2033 
2034 			if (dkey->dttk_size != 0) {
2035 				if (dtrace_bcmp(
2036 				    (void *)(uintptr_t)key[i].dttk_value,
2037 				    (void *)(uintptr_t)dkey->dttk_value,
2038 				    dkey->dttk_size))
2039 					goto next;
2040 			} else {
2041 				if (dkey->dttk_value != key[i].dttk_value)
2042 					goto next;
2043 			}
2044 		}
2045 
2046 		if (op != DTRACE_DYNVAR_DEALLOC)
2047 			return (dvar);
2048 
2049 		ASSERT(dvar->dtdv_next == NULL ||
2050 		    dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE);
2051 
2052 		if (prev != NULL) {
2053 			ASSERT(hash[bucket].dtdh_chain != dvar);
2054 			ASSERT(start != dvar);
2055 			ASSERT(prev->dtdv_next == dvar);
2056 			prev->dtdv_next = dvar->dtdv_next;
2057 		} else {
2058 			if (dtrace_casptr(&hash[bucket].dtdh_chain,
2059 			    start, dvar->dtdv_next) != start) {
2060 				/*
2061 				 * We have failed to atomically swing the
2062 				 * hash table head pointer, presumably because
2063 				 * of a conflicting allocation on another CPU.
2064 				 * We need to reread the hash chain and try
2065 				 * again.
2066 				 */
2067 				goto top;
2068 			}
2069 		}
2070 
2071 		dtrace_membar_producer();
2072 
2073 		/*
2074 		 * Now set the hash value to indicate that it's free.
2075 		 */
2076 		ASSERT(hash[bucket].dtdh_chain != dvar);
2077 		dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
2078 
2079 		dtrace_membar_producer();
2080 
2081 		/*
2082 		 * Set the next pointer to point at the dirty list, and
2083 		 * atomically swing the dirty pointer to the newly freed dvar.
2084 		 */
2085 		do {
2086 			next = dcpu->dtdsc_dirty;
2087 			dvar->dtdv_next = next;
2088 		} while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next);
2089 
2090 		/*
2091 		 * Finally, unlock this hash bucket.
2092 		 */
2093 		ASSERT(hash[bucket].dtdh_lock == lock);
2094 		ASSERT(lock & 1);
2095 		hash[bucket].dtdh_lock++;
2096 
2097 		return (NULL);
2098 next:
2099 		prev = dvar;
2100 		continue;
2101 	}
2102 
2103 	if (dvar == NULL) {
2104 		/*
2105 		 * If dvar is NULL, it is because we went off the rails:
2106 		 * one of the elements that we traversed in the hash chain
2107 		 * was deleted while we were traversing it.  In this case,
2108 		 * we assert that we aren't doing a dealloc (deallocs lock
2109 		 * the hash bucket to prevent themselves from racing with
2110 		 * one another), and retry the hash chain traversal.
2111 		 */
2112 		ASSERT(op != DTRACE_DYNVAR_DEALLOC);
2113 		goto top;
2114 	}
2115 
2116 	if (op != DTRACE_DYNVAR_ALLOC) {
2117 		/*
2118 		 * If we are not to allocate a new variable, we want to
2119 		 * return NULL now.  Before we return, check that the value
2120 		 * of the lock word hasn't changed.  If it has, we may have
2121 		 * seen an inconsistent snapshot.
2122 		 */
2123 		if (op == DTRACE_DYNVAR_NOALLOC) {
2124 			if (hash[bucket].dtdh_lock != lock)
2125 				goto top;
2126 		} else {
2127 			ASSERT(op == DTRACE_DYNVAR_DEALLOC);
2128 			ASSERT(hash[bucket].dtdh_lock == lock);
2129 			ASSERT(lock & 1);
2130 			hash[bucket].dtdh_lock++;
2131 		}
2132 
2133 		return (NULL);
2134 	}
2135 
2136 	/*
2137 	 * We need to allocate a new dynamic variable.  The size we need is the
2138 	 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the
2139 	 * size of any auxiliary key data (rounded up to 8-byte alignment) plus
2140 	 * the size of any referred-to data (dsize).  We then round the final
2141 	 * size up to the chunksize for allocation.
2142 	 */
2143 	for (ksize = 0, i = 0; i < nkeys; i++)
2144 		ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
2145 
2146 	/*
2147 	 * This should be pretty much impossible, but could happen if, say,
2148 	 * strange DIF specified the tuple.  Ideally, this should be an
2149 	 * assertion and not an error condition -- but that requires that the
2150 	 * chunksize calculation in dtrace_difo_chunksize() be absolutely
2151 	 * bullet-proof.  (That is, it must not be able to be fooled by
2152 	 * malicious DIF.)  Given the lack of backwards branches in DIF,
2153 	 * solving this would presumably not amount to solving the Halting
2154 	 * Problem -- but it still seems awfully hard.
2155 	 */
2156 	if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) +
2157 	    ksize + dsize > chunksize) {
2158 		dcpu->dtdsc_drops++;
2159 		return (NULL);
2160 	}
2161 
2162 	nstate = DTRACE_DSTATE_EMPTY;
2163 
2164 	do {
2165 retry:
2166 		free = dcpu->dtdsc_free;
2167 
2168 		if (free == NULL) {
2169 			dtrace_dynvar_t *clean = dcpu->dtdsc_clean;
2170 			void *rval;
2171 
2172 			if (clean == NULL) {
2173 				/*
2174 				 * We're out of dynamic variable space on
2175 				 * this CPU.  Unless we have tried all CPUs,
2176 				 * we'll try to allocate from a different
2177 				 * CPU.
2178 				 */
2179 				switch (dstate->dtds_state) {
2180 				case DTRACE_DSTATE_CLEAN: {
2181 					void *sp = &dstate->dtds_state;
2182 
2183 					if (++cpu >= (int)NCPU)
2184 						cpu = 0;
2185 
2186 					if (dcpu->dtdsc_dirty != NULL &&
2187 					    nstate == DTRACE_DSTATE_EMPTY)
2188 						nstate = DTRACE_DSTATE_DIRTY;
2189 
2190 					if (dcpu->dtdsc_rinsing != NULL)
2191 						nstate = DTRACE_DSTATE_RINSING;
2192 
2193 					dcpu = zpercpu_get_cpu(dstate->dtds_percpu, cpu);
2194 
2195 					if (cpu != me)
2196 						goto retry;
2197 
2198 					(void) dtrace_cas32(sp,
2199 					    DTRACE_DSTATE_CLEAN, nstate);
2200 
2201 					/*
2202 					 * To increment the correct bean
2203 					 * counter, take another lap.
2204 					 */
2205 					goto retry;
2206 				}
2207 
2208 				case DTRACE_DSTATE_DIRTY:
2209 					dcpu->dtdsc_dirty_drops++;
2210 					break;
2211 
2212 				case DTRACE_DSTATE_RINSING:
2213 					dcpu->dtdsc_rinsing_drops++;
2214 					break;
2215 
2216 				case DTRACE_DSTATE_EMPTY:
2217 					dcpu->dtdsc_drops++;
2218 					break;
2219 				}
2220 
2221 				DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP);
2222 				return (NULL);
2223 			}
2224 
2225 			/*
2226 			 * The clean list appears to be non-empty.  We want to
2227 			 * move the clean list to the free list; we start by
2228 			 * moving the clean pointer aside.
2229 			 */
2230 			if (dtrace_casptr(&dcpu->dtdsc_clean,
2231 			    clean, NULL) != clean) {
2232 				/*
2233 				 * We are in one of two situations:
2234 				 *
2235 				 *  (a)	The clean list was switched to the
2236 				 *	free list by another CPU.
2237 				 *
2238 				 *  (b)	The clean list was added to by the
2239 				 *	cleansing cyclic.
2240 				 *
2241 				 * In either of these situations, we can
2242 				 * just reattempt the free list allocation.
2243 				 */
2244 				goto retry;
2245 			}
2246 
2247 			ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE);
2248 
2249 			/*
2250 			 * Now we'll move the clean list to the free list.
2251 			 * It's impossible for this to fail:  the only way
2252 			 * the free list can be updated is through this
2253 			 * code path, and only one CPU can own the clean list.
2254 			 * Thus, it would only be possible for this to fail if
2255 			 * this code were racing with dtrace_dynvar_clean().
2256 			 * (That is, if dtrace_dynvar_clean() updated the clean
2257 			 * list, and we ended up racing to update the free
2258 			 * list.)  This race is prevented by the dtrace_sync()
2259 			 * in dtrace_dynvar_clean() -- which flushes the
2260 			 * owners of the clean lists out before resetting
2261 			 * the clean lists.
2262 			 */
2263 			rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean);
2264 			ASSERT(rval == NULL);
2265 			goto retry;
2266 		}
2267 
2268 		dvar = free;
2269 		new_free = dvar->dtdv_next;
2270 	} while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free);
2271 
2272 	/*
2273 	 * We have now allocated a new chunk.  We copy the tuple keys into the
2274 	 * tuple array and copy any referenced key data into the data space
2275 	 * following the tuple array.  As we do this, we relocate dttk_value
2276 	 * in the final tuple to point to the key data address in the chunk.
2277 	 */
2278 	kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys];
2279 	dvar->dtdv_data = (void *)(kdata + ksize);
2280 	dvar->dtdv_tuple.dtt_nkeys = nkeys;
2281 
2282 	for (i = 0; i < nkeys; i++) {
2283 		dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i];
2284 		size_t kesize = key[i].dttk_size;
2285 
2286 		if (kesize != 0) {
2287 			dtrace_bcopy(
2288 			    (const void *)(uintptr_t)key[i].dttk_value,
2289 			    (void *)kdata, kesize);
2290 			dkey->dttk_value = kdata;
2291 			kdata += P2ROUNDUP(kesize, sizeof (uint64_t));
2292 		} else {
2293 			dkey->dttk_value = key[i].dttk_value;
2294 		}
2295 
2296 		dkey->dttk_size = kesize;
2297 	}
2298 
2299 	ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE);
2300 	dvar->dtdv_hashval = hashval;
2301 	dvar->dtdv_next = start;
2302 
2303 	if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start)
2304 		return (dvar);
2305 
2306 	/*
2307 	 * The cas has failed.  Either another CPU is adding an element to
2308 	 * this hash chain, or another CPU is deleting an element from this
2309 	 * hash chain.  The simplest way to deal with both of these cases
2310 	 * (though not necessarily the most efficient) is to free our
2311 	 * allocated block and tail-call ourselves.  Note that the free is
2312 	 * to the dirty list and _not_ to the free list.  This is to prevent
2313 	 * races with allocators, above.
2314 	 */
2315 	dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
2316 
2317 	dtrace_membar_producer();
2318 
2319 	do {
2320 		free = dcpu->dtdsc_dirty;
2321 		dvar->dtdv_next = free;
2322 	} while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free);
2323 
2324 	return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate));
2325 }
2326 
2327 /*ARGSUSED*/
2328 static void
dtrace_aggregate_min(uint64_t * oval,uint64_t nval,uint64_t arg)2329 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg)
2330 {
2331 #pragma unused(arg) /* __APPLE__ */
2332 	if ((int64_t)nval < (int64_t)*oval)
2333 		*oval = nval;
2334 }
2335 
2336 /*ARGSUSED*/
2337 static void
dtrace_aggregate_max(uint64_t * oval,uint64_t nval,uint64_t arg)2338 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg)
2339 {
2340 #pragma unused(arg) /* __APPLE__ */
2341 	if ((int64_t)nval > (int64_t)*oval)
2342 		*oval = nval;
2343 }
2344 
2345 static void
dtrace_aggregate_quantize(uint64_t * quanta,uint64_t nval,uint64_t incr)2346 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr)
2347 {
2348 	int i, zero = DTRACE_QUANTIZE_ZEROBUCKET;
2349 	int64_t val = (int64_t)nval;
2350 
2351 	if (val < 0) {
2352 		for (i = 0; i < zero; i++) {
2353 			if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) {
2354 				quanta[i] += incr;
2355 				return;
2356 			}
2357 		}
2358 	} else {
2359 		for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) {
2360 			if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) {
2361 				quanta[i - 1] += incr;
2362 				return;
2363 			}
2364 		}
2365 
2366 		quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr;
2367 		return;
2368 	}
2369 
2370 	ASSERT(0);
2371 }
2372 
2373 static void
dtrace_aggregate_lquantize(uint64_t * lquanta,uint64_t nval,uint64_t incr)2374 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr)
2375 {
2376 	uint64_t arg = *lquanta++;
2377 	int32_t base = DTRACE_LQUANTIZE_BASE(arg);
2378 	uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
2379 	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
2380 	int32_t val = (int32_t)nval, level;
2381 
2382 	ASSERT(step != 0);
2383 	ASSERT(levels != 0);
2384 
2385 	if (val < base) {
2386 		/*
2387 		 * This is an underflow.
2388 		 */
2389 		lquanta[0] += incr;
2390 		return;
2391 	}
2392 
2393 	level = (val - base) / step;
2394 
2395 	if (level < levels) {
2396 		lquanta[level + 1] += incr;
2397 		return;
2398 	}
2399 
2400 	/*
2401 	 * This is an overflow.
2402 	 */
2403 	lquanta[levels + 1] += incr;
2404 }
2405 
2406 static int
dtrace_aggregate_llquantize_bucket(int16_t factor,int16_t low,int16_t high,int16_t nsteps,int64_t value)2407 dtrace_aggregate_llquantize_bucket(int16_t factor, int16_t low, int16_t high,
2408                                    int16_t nsteps, int64_t value)
2409 {
2410 	int64_t this = 1, last, next;
2411 	int base = 1, order;
2412 
2413 	for (order = 0; order < low; ++order)
2414 		this *= factor;
2415 
2416 	/*
2417 	 * If our value is less than our factor taken to the power of the
2418 	 * low order of magnitude, it goes into the zeroth bucket.
2419 	 */
2420 	if (value < this)
2421 		return 0;
2422 	else
2423 		last = this;
2424 
2425 	for (this *= factor; order <= high; ++order) {
2426 		int nbuckets = this > nsteps ? nsteps : this;
2427 
2428 		/*
2429 		 * We should not generally get log/linear quantizations
2430 		 * with a high magnitude that allows 64-bits to
2431 		 * overflow, but we nonetheless protect against this
2432 		 * by explicitly checking for overflow, and clamping
2433 		 * our value accordingly.
2434 		 */
2435 		next = this * factor;
2436 		if (next < this) {
2437 			value = this - 1;
2438 		}
2439 
2440 		/*
2441 		 * If our value lies within this order of magnitude,
2442 		 * determine its position by taking the offset within
2443 		 * the order of magnitude, dividing by the bucket
2444 		 * width, and adding to our (accumulated) base.
2445 		 */
2446 		if (value < this) {
2447 			return (base + (value - last) / (this / nbuckets));
2448 		}
2449 
2450 		base += nbuckets - (nbuckets / factor);
2451 		last = this;
2452 		this = next;
2453 	}
2454 
2455 	/*
2456 	 * Our value is greater than or equal to our factor taken to the
2457 	 * power of one plus the high magnitude -- return the top bucket.
2458 	 */
2459 	return base;
2460 }
2461 
2462 static void
dtrace_aggregate_llquantize(uint64_t * llquanta,uint64_t nval,uint64_t incr)2463 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr)
2464 {
2465 	uint64_t arg    = *llquanta++;
2466 	uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg);
2467 	uint16_t low    = DTRACE_LLQUANTIZE_LOW(arg);
2468 	uint16_t high   = DTRACE_LLQUANTIZE_HIGH(arg);
2469 	uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
2470 
2471 	llquanta[dtrace_aggregate_llquantize_bucket(factor, low, high, nsteps, nval)] += incr;
2472 }
2473 
2474 /*ARGSUSED*/
2475 static void
dtrace_aggregate_avg(uint64_t * data,uint64_t nval,uint64_t arg)2476 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg)
2477 {
2478 #pragma unused(arg) /* __APPLE__ */
2479 	data[0]++;
2480 	data[1] += nval;
2481 }
2482 
2483 /*ARGSUSED*/
2484 static void
dtrace_aggregate_stddev(uint64_t * data,uint64_t nval,uint64_t arg)2485 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg)
2486 {
2487 #pragma unused(arg) /* __APPLE__ */
2488 	int64_t snval = (int64_t)nval;
2489 	uint64_t tmp[2];
2490 
2491 	data[0]++;
2492 	data[1] += nval;
2493 
2494 	/*
2495 	 * What we want to say here is:
2496 	 *
2497 	 * data[2] += nval * nval;
2498 	 *
2499 	 * But given that nval is 64-bit, we could easily overflow, so
2500 	 * we do this as 128-bit arithmetic.
2501 	 */
2502 	if (snval < 0)
2503 		snval = -snval;
2504 
2505 	dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp);
2506 	dtrace_add_128(data + 2, tmp, data + 2);
2507 }
2508 
2509 /*ARGSUSED*/
2510 static void
dtrace_aggregate_count(uint64_t * oval,uint64_t nval,uint64_t arg)2511 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg)
2512 {
2513 #pragma unused(nval, arg) /* __APPLE__ */
2514 	*oval = *oval + 1;
2515 }
2516 
2517 /*ARGSUSED*/
2518 static void
dtrace_aggregate_sum(uint64_t * oval,uint64_t nval,uint64_t arg)2519 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg)
2520 {
2521 #pragma unused(arg) /* __APPLE__ */
2522 	*oval += nval;
2523 }
2524 
2525 /*
2526  * Aggregate given the tuple in the principal data buffer, and the aggregating
2527  * action denoted by the specified dtrace_aggregation_t.  The aggregation
2528  * buffer is specified as the buf parameter.  This routine does not return
2529  * failure; if there is no space in the aggregation buffer, the data will be
2530  * dropped, and a corresponding counter incremented.
2531  */
2532 __attribute__((noinline))
2533 static void
dtrace_aggregate(dtrace_aggregation_t * agg,dtrace_buffer_t * dbuf,intptr_t offset,dtrace_buffer_t * buf,uint64_t expr,uint64_t arg)2534 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf,
2535     intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg)
2536 {
2537 #pragma unused(arg)
2538 	dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec;
2539 	uint32_t i, ndx, size, fsize;
2540 	uint32_t align = sizeof (uint64_t) - 1;
2541 	dtrace_aggbuffer_t *agb;
2542 	dtrace_aggkey_t *key;
2543 	uint32_t hashval = 0, limit, isstr;
2544 	caddr_t tomax, data, kdata;
2545 	dtrace_actkind_t action;
2546 	dtrace_action_t *act;
2547 	uintptr_t offs;
2548 
2549 	if (buf == NULL)
2550 		return;
2551 
2552 	if (!agg->dtag_hasarg) {
2553 		/*
2554 		 * Currently, only quantize() and lquantize() take additional
2555 		 * arguments, and they have the same semantics:  an increment
2556 		 * value that defaults to 1 when not present.  If additional
2557 		 * aggregating actions take arguments, the setting of the
2558 		 * default argument value will presumably have to become more
2559 		 * sophisticated...
2560 		 */
2561 		arg = 1;
2562 	}
2563 
2564 	action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION;
2565 	size = rec->dtrd_offset - agg->dtag_base;
2566 	fsize = size + rec->dtrd_size;
2567 
2568 	ASSERT(dbuf->dtb_tomax != NULL);
2569 	data = dbuf->dtb_tomax + offset + agg->dtag_base;
2570 
2571 	if ((tomax = buf->dtb_tomax) == NULL) {
2572 		dtrace_buffer_drop(buf);
2573 		return;
2574 	}
2575 
2576 	/*
2577 	 * The metastructure is always at the bottom of the buffer.
2578 	 */
2579 	agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size -
2580 	    sizeof (dtrace_aggbuffer_t));
2581 
2582 	if (buf->dtb_offset == 0) {
2583 		/*
2584 		 * We just kludge up approximately 1/8th of the size to be
2585 		 * buckets.  If this guess ends up being routinely
2586 		 * off-the-mark, we may need to dynamically readjust this
2587 		 * based on past performance.
2588 		 */
2589 		uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t);
2590 
2591 		if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) <
2592 		    (uintptr_t)tomax || hashsize == 0) {
2593 			/*
2594 			 * We've been given a ludicrously small buffer;
2595 			 * increment our drop count and leave.
2596 			 */
2597 			dtrace_buffer_drop(buf);
2598 			return;
2599 		}
2600 
2601 		/*
2602 		 * And now, a pathetic attempt to try to get a an odd (or
2603 		 * perchance, a prime) hash size for better hash distribution.
2604 		 */
2605 		if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3))
2606 			hashsize -= DTRACE_AGGHASHSIZE_SLEW;
2607 
2608 		agb->dtagb_hashsize = hashsize;
2609 		agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb -
2610 		    agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *));
2611 		agb->dtagb_free = (uintptr_t)agb->dtagb_hash;
2612 
2613 		for (i = 0; i < agb->dtagb_hashsize; i++)
2614 			agb->dtagb_hash[i] = NULL;
2615 	}
2616 
2617 	ASSERT(agg->dtag_first != NULL);
2618 	ASSERT(agg->dtag_first->dta_intuple);
2619 
2620 	/*
2621 	 * Calculate the hash value based on the key.  Note that we _don't_
2622 	 * include the aggid in the hashing (but we will store it as part of
2623 	 * the key).  The hashing algorithm is Bob Jenkins' "One-at-a-time"
2624 	 * algorithm: a simple, quick algorithm that has no known funnels, and
2625 	 * gets good distribution in practice.  The efficacy of the hashing
2626 	 * algorithm (and a comparison with other algorithms) may be found by
2627 	 * running the ::dtrace_aggstat MDB dcmd.
2628 	 */
2629 	for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2630 		i = act->dta_rec.dtrd_offset - agg->dtag_base;
2631 		limit = i + act->dta_rec.dtrd_size;
2632 		ASSERT(limit <= size);
2633 		isstr = DTRACEACT_ISSTRING(act);
2634 
2635 		for (; i < limit; i++) {
2636 			hashval += data[i];
2637 			hashval += (hashval << 10);
2638 			hashval ^= (hashval >> 6);
2639 
2640 			if (isstr && data[i] == '\0')
2641 				break;
2642 		}
2643 	}
2644 
2645 	hashval += (hashval << 3);
2646 	hashval ^= (hashval >> 11);
2647 	hashval += (hashval << 15);
2648 
2649 	/*
2650 	 * Yes, the divide here is expensive -- but it's generally the least
2651 	 * of the performance issues given the amount of data that we iterate
2652 	 * over to compute hash values, compare data, etc.
2653 	 */
2654 	ndx = hashval % agb->dtagb_hashsize;
2655 
2656 	for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) {
2657 		ASSERT((caddr_t)key >= tomax);
2658 		ASSERT((caddr_t)key < tomax + buf->dtb_size);
2659 
2660 		if (hashval != key->dtak_hashval || key->dtak_size != size)
2661 			continue;
2662 
2663 		kdata = key->dtak_data;
2664 		ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size);
2665 
2666 		for (act = agg->dtag_first; act->dta_intuple;
2667 		    act = act->dta_next) {
2668 			i = act->dta_rec.dtrd_offset - agg->dtag_base;
2669 			limit = i + act->dta_rec.dtrd_size;
2670 			ASSERT(limit <= size);
2671 			isstr = DTRACEACT_ISSTRING(act);
2672 
2673 			for (; i < limit; i++) {
2674 				if (kdata[i] != data[i])
2675 					goto next;
2676 
2677 				if (isstr && data[i] == '\0')
2678 					break;
2679 			}
2680 		}
2681 
2682 		if (action != key->dtak_action) {
2683 			/*
2684 			 * We are aggregating on the same value in the same
2685 			 * aggregation with two different aggregating actions.
2686 			 * (This should have been picked up in the compiler,
2687 			 * so we may be dealing with errant or devious DIF.)
2688 			 * This is an error condition; we indicate as much,
2689 			 * and return.
2690 			 */
2691 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
2692 			return;
2693 		}
2694 
2695 		/*
2696 		 * This is a hit:  we need to apply the aggregator to
2697 		 * the value at this key.
2698 		 */
2699 		agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg);
2700 		return;
2701 next:
2702 		continue;
2703 	}
2704 
2705 	/*
2706 	 * We didn't find it.  We need to allocate some zero-filled space,
2707 	 * link it into the hash table appropriately, and apply the aggregator
2708 	 * to the (zero-filled) value.
2709 	 */
2710 	offs = buf->dtb_offset;
2711 	while (offs & (align - 1))
2712 		offs += sizeof (uint32_t);
2713 
2714 	/*
2715 	 * If we don't have enough room to both allocate a new key _and_
2716 	 * its associated data, increment the drop count and return.
2717 	 */
2718 	if ((uintptr_t)tomax + offs + fsize >
2719 	    agb->dtagb_free - sizeof (dtrace_aggkey_t)) {
2720 		dtrace_buffer_drop(buf);
2721 		return;
2722 	}
2723 
2724 	/*CONSTCOND*/
2725 	ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1)));
2726 	key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t));
2727 	agb->dtagb_free -= sizeof (dtrace_aggkey_t);
2728 
2729 	key->dtak_data = kdata = tomax + offs;
2730 	buf->dtb_offset = offs + fsize;
2731 
2732 	/*
2733 	 * Now copy the data across.
2734 	 */
2735 	*((dtrace_aggid_t *)kdata) = agg->dtag_id;
2736 
2737 	for (i = sizeof (dtrace_aggid_t); i < size; i++)
2738 		kdata[i] = data[i];
2739 
2740 	/*
2741 	 * Because strings are not zeroed out by default, we need to iterate
2742 	 * looking for actions that store strings, and we need to explicitly
2743 	 * pad these strings out with zeroes.
2744 	 */
2745 	for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2746 		int nul;
2747 
2748 		if (!DTRACEACT_ISSTRING(act))
2749 			continue;
2750 
2751 		i = act->dta_rec.dtrd_offset - agg->dtag_base;
2752 		limit = i + act->dta_rec.dtrd_size;
2753 		ASSERT(limit <= size);
2754 
2755 		for (nul = 0; i < limit; i++) {
2756 			if (nul) {
2757 				kdata[i] = '\0';
2758 				continue;
2759 			}
2760 
2761 			if (data[i] != '\0')
2762 				continue;
2763 
2764 			nul = 1;
2765 		}
2766 	}
2767 
2768 	for (i = size; i < fsize; i++)
2769 		kdata[i] = 0;
2770 
2771 	key->dtak_hashval = hashval;
2772 	key->dtak_size = size;
2773 	key->dtak_action = action;
2774 	key->dtak_next = agb->dtagb_hash[ndx];
2775 	agb->dtagb_hash[ndx] = key;
2776 
2777 	/*
2778 	 * Finally, apply the aggregator.
2779 	 */
2780 	*((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial;
2781 	agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg);
2782 }
2783 
2784 /*
2785  * Given consumer state, this routine finds a speculation in the INACTIVE
2786  * state and transitions it into the ACTIVE state.  If there is no speculation
2787  * in the INACTIVE state, 0 is returned.  In this case, no error counter is
2788  * incremented -- it is up to the caller to take appropriate action.
2789  */
2790 static int
dtrace_speculation(dtrace_state_t * state)2791 dtrace_speculation(dtrace_state_t *state)
2792 {
2793 	int i = 0;
2794 	dtrace_speculation_state_t current;
2795 	uint32_t *stat = &state->dts_speculations_unavail, count;
2796 
2797 	while (i < state->dts_nspeculations) {
2798 		dtrace_speculation_t *spec = &state->dts_speculations[i];
2799 
2800 		current = spec->dtsp_state;
2801 
2802 		if (current != DTRACESPEC_INACTIVE) {
2803 			if (current == DTRACESPEC_COMMITTINGMANY ||
2804 			    current == DTRACESPEC_COMMITTING ||
2805 			    current == DTRACESPEC_DISCARDING)
2806 				stat = &state->dts_speculations_busy;
2807 			i++;
2808 			continue;
2809 		}
2810 
2811 		if (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2812 		    current, DTRACESPEC_ACTIVE) == current)
2813 			return (i + 1);
2814 	}
2815 
2816 	/*
2817 	 * We couldn't find a speculation.  If we found as much as a single
2818 	 * busy speculation buffer, we'll attribute this failure as "busy"
2819 	 * instead of "unavail".
2820 	 */
2821 	do {
2822 		count = *stat;
2823 	} while (dtrace_cas32(stat, count, count + 1) != count);
2824 
2825 	return (0);
2826 }
2827 
2828 /*
2829  * This routine commits an active speculation.  If the specified speculation
2830  * is not in a valid state to perform a commit(), this routine will silently do
2831  * nothing.  The state of the specified speculation is transitioned according
2832  * to the state transition diagram outlined in <sys/dtrace_impl.h>
2833  */
2834 static void
dtrace_speculation_commit(dtrace_state_t * state,processorid_t cpu,dtrace_specid_t which)2835 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu,
2836     dtrace_specid_t which)
2837 {
2838 	dtrace_speculation_t *spec;
2839 	dtrace_buffer_t *src, *dest;
2840 	uintptr_t daddr, saddr, dlimit, slimit;
2841 	dtrace_speculation_state_t current,  new = DTRACESPEC_INACTIVE;
2842 	intptr_t offs;
2843 	uint64_t timestamp;
2844 
2845 	if (which == 0)
2846 		return;
2847 
2848 	if (which > (dtrace_specid_t)state->dts_nspeculations) {
2849 		cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2850 		return;
2851 	}
2852 
2853 	spec = &state->dts_speculations[which - 1];
2854 	src = &spec->dtsp_buffer[cpu];
2855 	dest = &state->dts_buffer[cpu];
2856 
2857 	do {
2858 		current = spec->dtsp_state;
2859 
2860 		if (current == DTRACESPEC_COMMITTINGMANY)
2861 			break;
2862 
2863 		switch (current) {
2864 		case DTRACESPEC_INACTIVE:
2865 		case DTRACESPEC_DISCARDING:
2866 			return;
2867 
2868 		case DTRACESPEC_COMMITTING:
2869 			/*
2870 			 * This is only possible if we are (a) commit()'ing
2871 			 * without having done a prior speculate() on this CPU
2872 			 * and (b) racing with another commit() on a different
2873 			 * CPU.  There's nothing to do -- we just assert that
2874 			 * our offset is 0.
2875 			 */
2876 			ASSERT(src->dtb_offset == 0);
2877 			return;
2878 
2879 		case DTRACESPEC_ACTIVE:
2880 			new = DTRACESPEC_COMMITTING;
2881 			break;
2882 
2883 		case DTRACESPEC_ACTIVEONE:
2884 			/*
2885 			 * This speculation is active on one CPU.  If our
2886 			 * buffer offset is non-zero, we know that the one CPU
2887 			 * must be us.  Otherwise, we are committing on a
2888 			 * different CPU from the speculate(), and we must
2889 			 * rely on being asynchronously cleaned.
2890 			 */
2891 			if (src->dtb_offset != 0) {
2892 				new = DTRACESPEC_COMMITTING;
2893 				break;
2894 			}
2895 			OS_FALLTHROUGH;
2896 
2897 		case DTRACESPEC_ACTIVEMANY:
2898 			new = DTRACESPEC_COMMITTINGMANY;
2899 			break;
2900 
2901 		default:
2902 			ASSERT(0);
2903 		}
2904 	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2905 	    current, new) != current);
2906 
2907 	/*
2908 	 * We have set the state to indicate that we are committing this
2909 	 * speculation.  Now reserve the necessary space in the destination
2910 	 * buffer.
2911 	 */
2912 	if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset,
2913 	    sizeof (uint64_t), state, NULL)) < 0) {
2914 		dtrace_buffer_drop(dest);
2915 		goto out;
2916 	}
2917 
2918 	/*
2919 	 * We have sufficient space to copy the speculative buffer into the
2920 	 * primary buffer.  First, modify the speculative buffer, filling
2921 	 * in the timestamp of all entries with the current time.  The data
2922 	 * must have the commit() time rather than the time it was traced,
2923 	 * so that all entries in the primary buffer are in timestamp order.
2924 	 */
2925 	timestamp = dtrace_gethrtime();
2926 	saddr = (uintptr_t)src->dtb_tomax;
2927 	slimit = saddr + src->dtb_offset;
2928 	while (saddr < slimit) {
2929 		size_t size;
2930 		dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr;
2931 
2932 		if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
2933 			saddr += sizeof (dtrace_epid_t);
2934 			continue;
2935 		}
2936 
2937 		ASSERT(dtrh->dtrh_epid <= ((dtrace_epid_t) state->dts_necbs));
2938 		size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size;
2939 
2940 		ASSERT(saddr + size <= slimit);
2941 		ASSERT(size >= sizeof(dtrace_rechdr_t));
2942 		ASSERT(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh) == UINT64_MAX);
2943 
2944 		DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp);
2945 
2946 		saddr += size;
2947 	}
2948 
2949 	/*
2950 	 * Copy the buffer across.  (Note that this is a
2951 	 * highly subobtimal bcopy(); in the unlikely event that this becomes
2952 	 * a serious performance issue, a high-performance DTrace-specific
2953 	 * bcopy() should obviously be invented.)
2954 	 */
2955 	daddr = (uintptr_t)dest->dtb_tomax + offs;
2956 	dlimit = daddr + src->dtb_offset;
2957 	saddr = (uintptr_t)src->dtb_tomax;
2958 
2959 	/*
2960 	 * First, the aligned portion.
2961 	 */
2962 	while (dlimit - daddr >= sizeof (uint64_t)) {
2963 		*((uint64_t *)daddr) = *((uint64_t *)saddr);
2964 
2965 		daddr += sizeof (uint64_t);
2966 		saddr += sizeof (uint64_t);
2967 	}
2968 
2969 	/*
2970 	 * Now any left-over bit...
2971 	 */
2972 	while (dlimit - daddr)
2973 		*((uint8_t *)daddr++) = *((uint8_t *)saddr++);
2974 
2975 	/*
2976 	 * Finally, commit the reserved space in the destination buffer.
2977 	 */
2978 	dest->dtb_offset = offs + src->dtb_offset;
2979 
2980 out:
2981 	/*
2982 	 * If we're lucky enough to be the only active CPU on this speculation
2983 	 * buffer, we can just set the state back to DTRACESPEC_INACTIVE.
2984 	 */
2985 	if (current == DTRACESPEC_ACTIVE ||
2986 	    (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) {
2987 		uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state,
2988 		    DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE);
2989 #pragma unused(rval) /* __APPLE__ */
2990 
2991 		ASSERT(rval == DTRACESPEC_COMMITTING);
2992 	}
2993 
2994 	src->dtb_offset = 0;
2995 	src->dtb_xamot_drops += src->dtb_drops;
2996 	src->dtb_drops = 0;
2997 }
2998 
2999 /*
3000  * This routine discards an active speculation.  If the specified speculation
3001  * is not in a valid state to perform a discard(), this routine will silently
3002  * do nothing.  The state of the specified speculation is transitioned
3003  * according to the state transition diagram outlined in <sys/dtrace_impl.h>
3004  */
3005 __attribute__((noinline))
3006 static void
dtrace_speculation_discard(dtrace_state_t * state,processorid_t cpu,dtrace_specid_t which)3007 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu,
3008     dtrace_specid_t which)
3009 {
3010 	dtrace_speculation_t *spec;
3011 	dtrace_speculation_state_t current, new = DTRACESPEC_INACTIVE;
3012 	dtrace_buffer_t *buf;
3013 
3014 	if (which == 0)
3015 		return;
3016 
3017 	if (which > (dtrace_specid_t)state->dts_nspeculations) {
3018 		cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
3019 		return;
3020 	}
3021 
3022 	spec = &state->dts_speculations[which - 1];
3023 	buf = &spec->dtsp_buffer[cpu];
3024 
3025 	do {
3026 		current = spec->dtsp_state;
3027 
3028 		switch (current) {
3029 		case DTRACESPEC_INACTIVE:
3030 		case DTRACESPEC_COMMITTINGMANY:
3031 		case DTRACESPEC_COMMITTING:
3032 		case DTRACESPEC_DISCARDING:
3033 			return;
3034 
3035 		case DTRACESPEC_ACTIVE:
3036 		case DTRACESPEC_ACTIVEMANY:
3037 			new = DTRACESPEC_DISCARDING;
3038 			break;
3039 
3040 		case DTRACESPEC_ACTIVEONE:
3041 			if (buf->dtb_offset != 0) {
3042 				new = DTRACESPEC_INACTIVE;
3043 			} else {
3044 				new = DTRACESPEC_DISCARDING;
3045 			}
3046 			break;
3047 
3048 		default:
3049 			ASSERT(0);
3050 		}
3051 	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
3052 	    current, new) != current);
3053 
3054 	buf->dtb_offset = 0;
3055 	buf->dtb_drops = 0;
3056 }
3057 
3058 /*
3059  * Note:  not called from probe context.  This function is called
3060  * asynchronously from cross call context to clean any speculations that are
3061  * in the COMMITTINGMANY or DISCARDING states.  These speculations may not be
3062  * transitioned back to the INACTIVE state until all CPUs have cleaned the
3063  * speculation.
3064  */
3065 static void
dtrace_speculation_clean_here(dtrace_state_t * state)3066 dtrace_speculation_clean_here(dtrace_state_t *state)
3067 {
3068 	dtrace_icookie_t cookie;
3069 	processorid_t cpu = CPU->cpu_id;
3070 	dtrace_buffer_t *dest = &state->dts_buffer[cpu];
3071 	dtrace_specid_t i;
3072 
3073 	cookie = dtrace_interrupt_disable();
3074 
3075 	if (dest->dtb_tomax == NULL) {
3076 		dtrace_interrupt_enable(cookie);
3077 		return;
3078 	}
3079 
3080 	for (i = 0; i < (dtrace_specid_t)state->dts_nspeculations; i++) {
3081 		dtrace_speculation_t *spec = &state->dts_speculations[i];
3082 		dtrace_buffer_t *src = &spec->dtsp_buffer[cpu];
3083 
3084 		if (src->dtb_tomax == NULL)
3085 			continue;
3086 
3087 		if (spec->dtsp_state == DTRACESPEC_DISCARDING) {
3088 			src->dtb_offset = 0;
3089 			continue;
3090 		}
3091 
3092 		if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
3093 			continue;
3094 
3095 		if (src->dtb_offset == 0)
3096 			continue;
3097 
3098 		dtrace_speculation_commit(state, cpu, i + 1);
3099 	}
3100 
3101 	dtrace_interrupt_enable(cookie);
3102 }
3103 
3104 /*
3105  * Note:  not called from probe context.  This function is called
3106  * asynchronously (and at a regular interval) to clean any speculations that
3107  * are in the COMMITTINGMANY or DISCARDING states.  If it discovers that there
3108  * is work to be done, it cross calls all CPUs to perform that work;
3109  * COMMITMANY and DISCARDING speculations may not be transitioned back to the
3110  * INACTIVE state until they have been cleaned by all CPUs.
3111  */
3112 static void
dtrace_speculation_clean(dtrace_state_t * state)3113 dtrace_speculation_clean(dtrace_state_t *state)
3114 {
3115 	int work = 0;
3116 	uint32_t rv;
3117 	dtrace_specid_t i;
3118 
3119 	for (i = 0; i < (dtrace_specid_t)state->dts_nspeculations; i++) {
3120 		dtrace_speculation_t *spec = &state->dts_speculations[i];
3121 
3122 		ASSERT(!spec->dtsp_cleaning);
3123 
3124 		if (spec->dtsp_state != DTRACESPEC_DISCARDING &&
3125 		    spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
3126 			continue;
3127 
3128 		work++;
3129 		spec->dtsp_cleaning = 1;
3130 	}
3131 
3132 	if (!work)
3133 		return;
3134 
3135 	dtrace_xcall(DTRACE_CPUALL,
3136 	    (dtrace_xcall_t)dtrace_speculation_clean_here, state);
3137 
3138 	/*
3139 	 * We now know that all CPUs have committed or discarded their
3140 	 * speculation buffers, as appropriate.  We can now set the state
3141 	 * to inactive.
3142 	 */
3143 	for (i = 0; i < (dtrace_specid_t)state->dts_nspeculations; i++) {
3144 		dtrace_speculation_t *spec = &state->dts_speculations[i];
3145 		dtrace_speculation_state_t current, new;
3146 
3147 		if (!spec->dtsp_cleaning)
3148 			continue;
3149 
3150 		current = spec->dtsp_state;
3151 		ASSERT(current == DTRACESPEC_DISCARDING ||
3152 		    current == DTRACESPEC_COMMITTINGMANY);
3153 
3154 		new = DTRACESPEC_INACTIVE;
3155 
3156 		rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new);
3157 		ASSERT(rv == current);
3158 		spec->dtsp_cleaning = 0;
3159 	}
3160 }
3161 
3162 /*
3163  * Called as part of a speculate() to get the speculative buffer associated
3164  * with a given speculation.  Returns NULL if the specified speculation is not
3165  * in an ACTIVE state.  If the speculation is in the ACTIVEONE state -- and
3166  * the active CPU is not the specified CPU -- the speculation will be
3167  * atomically transitioned into the ACTIVEMANY state.
3168  */
3169 __attribute__((noinline))
3170 static dtrace_buffer_t *
dtrace_speculation_buffer(dtrace_state_t * state,processorid_t cpuid,dtrace_specid_t which)3171 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid,
3172     dtrace_specid_t which)
3173 {
3174 	dtrace_speculation_t *spec;
3175 	dtrace_speculation_state_t current, new = DTRACESPEC_INACTIVE;
3176 	dtrace_buffer_t *buf;
3177 
3178 	if (which == 0)
3179 		return (NULL);
3180 
3181 	if (which > (dtrace_specid_t)state->dts_nspeculations) {
3182 		cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
3183 		return (NULL);
3184 	}
3185 
3186 	spec = &state->dts_speculations[which - 1];
3187 	buf = &spec->dtsp_buffer[cpuid];
3188 
3189 	do {
3190 		current = spec->dtsp_state;
3191 
3192 		switch (current) {
3193 		case DTRACESPEC_INACTIVE:
3194 		case DTRACESPEC_COMMITTINGMANY:
3195 		case DTRACESPEC_DISCARDING:
3196 			return (NULL);
3197 
3198 		case DTRACESPEC_COMMITTING:
3199 			ASSERT(buf->dtb_offset == 0);
3200 			return (NULL);
3201 
3202 		case DTRACESPEC_ACTIVEONE:
3203 			/*
3204 			 * This speculation is currently active on one CPU.
3205 			 * Check the offset in the buffer; if it's non-zero,
3206 			 * that CPU must be us (and we leave the state alone).
3207 			 * If it's zero, assume that we're starting on a new
3208 			 * CPU -- and change the state to indicate that the
3209 			 * speculation is active on more than one CPU.
3210 			 */
3211 			if (buf->dtb_offset != 0)
3212 				return (buf);
3213 
3214 			new = DTRACESPEC_ACTIVEMANY;
3215 			break;
3216 
3217 		case DTRACESPEC_ACTIVEMANY:
3218 			return (buf);
3219 
3220 		case DTRACESPEC_ACTIVE:
3221 			new = DTRACESPEC_ACTIVEONE;
3222 			break;
3223 
3224 		default:
3225 			ASSERT(0);
3226 		}
3227 	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
3228 	    current, new) != current);
3229 
3230 	ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY);
3231 	return (buf);
3232 }
3233 
3234 /*
3235  * Return a string.  In the event that the user lacks the privilege to access
3236  * arbitrary kernel memory, we copy the string out to scratch memory so that we
3237  * don't fail access checking.
3238  *
3239  * dtrace_dif_variable() uses this routine as a helper for various
3240  * builtin values such as 'execname' and 'probefunc.'
3241  */
3242 static
3243 uintptr_t
dtrace_dif_varstr(uintptr_t addr,dtrace_state_t * state,dtrace_mstate_t * mstate)3244 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state,
3245     dtrace_mstate_t *mstate)
3246 {
3247 	uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
3248 	uintptr_t ret;
3249 	size_t strsz;
3250 
3251 	/*
3252 	 * The easy case: this probe is allowed to read all of memory, so
3253 	 * we can just return this as a vanilla pointer.
3254 	 */
3255 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
3256 		return (addr);
3257 
3258 	/*
3259 	 * This is the tougher case: we copy the string in question from
3260 	 * kernel memory into scratch memory and return it that way: this
3261 	 * ensures that we won't trip up when access checking tests the
3262 	 * BYREF return value.
3263 	 */
3264 	strsz = dtrace_strlen((char *)addr, size) + 1;
3265 
3266 	if (mstate->dtms_scratch_ptr + strsz >
3267 	    mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
3268 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3269 		return (0);
3270 	}
3271 
3272 	dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
3273 	    strsz);
3274 	ret = mstate->dtms_scratch_ptr;
3275 	mstate->dtms_scratch_ptr += strsz;
3276 	return (ret);
3277 }
3278 
3279 /*
3280  * This function implements the DIF emulator's variable lookups.  The emulator
3281  * passes a reserved variable identifier and optional built-in array index.
3282  */
3283 static uint64_t
dtrace_dif_variable(dtrace_mstate_t * mstate,dtrace_state_t * state,uint64_t v,uint64_t ndx)3284 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v,
3285     uint64_t ndx)
3286 {
3287 	/*
3288 	 * If we're accessing one of the uncached arguments, we'll turn this
3289 	 * into a reference in the args array.
3290 	 */
3291 	if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) {
3292 		ndx = v - DIF_VAR_ARG0;
3293 		v = DIF_VAR_ARGS;
3294 	}
3295 
3296 	switch (v) {
3297 	case DIF_VAR_ARGS:
3298 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS);
3299 		if (ndx >= sizeof (mstate->dtms_arg) /
3300 		    sizeof (mstate->dtms_arg[0])) {
3301 			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3302 			dtrace_vstate_t *vstate = &state->dts_vstate;
3303 			dtrace_provider_t *pv;
3304 			uint64_t val;
3305 			int argndx = ndx;
3306 
3307 			if (argndx < 0) {
3308 				DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3309 				return (0);
3310 			}
3311 
3312 			pv = mstate->dtms_probe->dtpr_provider;
3313 			if (pv->dtpv_pops.dtps_getargval != NULL)
3314 				val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg,
3315 				    mstate->dtms_probe->dtpr_id,
3316 				    mstate->dtms_probe->dtpr_arg, argndx, aframes);
3317 			/* Special case access of arg5 as passed to dtrace_probe_error() (which see.) */
3318 			else if (mstate->dtms_probe->dtpr_id == dtrace_probeid_error && argndx == 5) {
3319 			        return ((dtrace_state_t *)(uintptr_t)(mstate->dtms_arg[0]))->dts_arg_error_illval;
3320 			}
3321 
3322 			else
3323 				val = dtrace_getarg(argndx, aframes, mstate, vstate);
3324 
3325 			/*
3326 			 * This is regrettably required to keep the compiler
3327 			 * from tail-optimizing the call to dtrace_getarg().
3328 			 * The condition always evaluates to true, but the
3329 			 * compiler has no way of figuring that out a priori.
3330 			 * (None of this would be necessary if the compiler
3331 			 * could be relied upon to _always_ tail-optimize
3332 			 * the call to dtrace_getarg() -- but it can't.)
3333 			 */
3334 			if (mstate->dtms_probe != NULL)
3335 				return (val);
3336 
3337 			ASSERT(0);
3338 		}
3339 
3340 		return (mstate->dtms_arg[ndx]);
3341 
3342 	case DIF_VAR_UREGS: {
3343 		thread_t thread;
3344 
3345 		if (!dtrace_priv_proc(state))
3346 			return (0);
3347 
3348 		if ((thread = current_thread()) == NULL) {
3349 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
3350 			cpu_core[CPU->cpu_id].cpuc_dtrace_illval = 0;
3351 			return (0);
3352 		}
3353 
3354 		return (dtrace_getreg(find_user_regs(thread), ndx));
3355 	}
3356 
3357 	case DIF_VAR_VMREGS: {
3358 		uint64_t rval;
3359 
3360 		if (!dtrace_priv_kernel(state))
3361 			return (0);
3362 
3363 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3364 
3365 		rval = dtrace_getvmreg(ndx);
3366 
3367 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3368 
3369 		return (rval);
3370 	}
3371 
3372 	case DIF_VAR_CURTHREAD:
3373 		if (!dtrace_priv_kernel(state))
3374 			return (0);
3375 
3376 		return ((uint64_t)(uintptr_t)current_thread());
3377 
3378 	case DIF_VAR_TIMESTAMP:
3379 		if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
3380 			mstate->dtms_timestamp = dtrace_gethrtime();
3381 			mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP;
3382 		}
3383 		return (mstate->dtms_timestamp);
3384 
3385 	case DIF_VAR_VTIMESTAMP:
3386 		ASSERT(dtrace_vtime_references != 0);
3387 		return (dtrace_get_thread_vtime(current_thread()));
3388 
3389 	case DIF_VAR_WALLTIMESTAMP:
3390 		if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) {
3391 			mstate->dtms_walltimestamp = dtrace_gethrestime();
3392 			mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP;
3393 		}
3394 		return (mstate->dtms_walltimestamp);
3395 
3396 	case DIF_VAR_MACHTIMESTAMP:
3397 		if (!(mstate->dtms_present & DTRACE_MSTATE_MACHTIMESTAMP)) {
3398 			mstate->dtms_machtimestamp = mach_absolute_time();
3399 			mstate->dtms_present |= DTRACE_MSTATE_MACHTIMESTAMP;
3400 		}
3401 		return (mstate->dtms_machtimestamp);
3402 
3403 	case DIF_VAR_MACHCTIMESTAMP:
3404 		if (!(mstate->dtms_present & DTRACE_MSTATE_MACHCTIMESTAMP)) {
3405 			mstate->dtms_machctimestamp = mach_continuous_time();
3406 			mstate->dtms_present |= DTRACE_MSTATE_MACHCTIMESTAMP;
3407 		}
3408 		return (mstate->dtms_machctimestamp);
3409 
3410 
3411 	case DIF_VAR_CPU:
3412 		return ((uint64_t) dtrace_get_thread_last_cpu_id(current_thread()));
3413 
3414 	case DIF_VAR_IPL:
3415 		if (!dtrace_priv_kernel(state))
3416 			return (0);
3417 		if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) {
3418 			mstate->dtms_ipl = dtrace_getipl();
3419 			mstate->dtms_present |= DTRACE_MSTATE_IPL;
3420 		}
3421 		return (mstate->dtms_ipl);
3422 
3423 	case DIF_VAR_EPID:
3424 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID);
3425 		return (mstate->dtms_epid);
3426 
3427 	case DIF_VAR_ID:
3428 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3429 		return (mstate->dtms_probe->dtpr_id);
3430 
3431 	case DIF_VAR_STACKDEPTH:
3432 		if (!dtrace_priv_kernel(state))
3433 			return (0);
3434 		if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) {
3435 			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3436 
3437 			mstate->dtms_stackdepth = dtrace_getstackdepth(aframes);
3438 			mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH;
3439 		}
3440 		return (mstate->dtms_stackdepth);
3441 
3442 	case DIF_VAR_USTACKDEPTH:
3443 		if (!dtrace_priv_proc(state))
3444 			return (0);
3445 		if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) {
3446 			/*
3447 			 * See comment in DIF_VAR_PID.
3448 			 */
3449 			if (DTRACE_ANCHORED(mstate->dtms_probe) &&
3450 			    CPU_ON_INTR(CPU)) {
3451 				mstate->dtms_ustackdepth = 0;
3452 			} else {
3453 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3454 				mstate->dtms_ustackdepth =
3455 				    dtrace_getustackdepth();
3456 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3457 			}
3458 			mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH;
3459 		}
3460 		return (mstate->dtms_ustackdepth);
3461 
3462 	case DIF_VAR_CALLER:
3463 		if (!dtrace_priv_kernel(state))
3464 			return (0);
3465 		if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) {
3466 			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3467 
3468 			if (!DTRACE_ANCHORED(mstate->dtms_probe)) {
3469 				/*
3470 				 * If this is an unanchored probe, we are
3471 				 * required to go through the slow path:
3472 				 * dtrace_caller() only guarantees correct
3473 				 * results for anchored probes.
3474 				 */
3475 				pc_t caller[2];
3476 
3477 				dtrace_getpcstack(caller, 2, aframes,
3478 				    (uint32_t *)(uintptr_t)mstate->dtms_arg[0]);
3479 				mstate->dtms_caller = caller[1];
3480 			} else if ((mstate->dtms_caller =
3481 				dtrace_caller(aframes)) == (uintptr_t)-1) {
3482 				/*
3483 				 * We have failed to do this the quick way;
3484 				 * we must resort to the slower approach of
3485 				 * calling dtrace_getpcstack().
3486 				 */
3487 				pc_t caller;
3488 
3489 				dtrace_getpcstack(&caller, 1, aframes, NULL);
3490 				mstate->dtms_caller = caller;
3491 			}
3492 
3493 			mstate->dtms_present |= DTRACE_MSTATE_CALLER;
3494 		}
3495 		return (mstate->dtms_caller);
3496 
3497 	case DIF_VAR_UCALLER:
3498 		if (!dtrace_priv_proc(state))
3499 			return (0);
3500 
3501 		if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) {
3502 			uint64_t ustack[3];
3503 
3504 			/*
3505 			 * dtrace_getupcstack() fills in the first uint64_t
3506 			 * with the current PID.  The second uint64_t will
3507 			 * be the program counter at user-level.  The third
3508 			 * uint64_t will contain the caller, which is what
3509 			 * we're after.
3510 			 */
3511 			ustack[2] = 0;
3512 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3513 			dtrace_getupcstack(ustack, 3);
3514 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3515 			mstate->dtms_ucaller = ustack[2];
3516 			mstate->dtms_present |= DTRACE_MSTATE_UCALLER;
3517 		}
3518 
3519 		return (mstate->dtms_ucaller);
3520 
3521 	case DIF_VAR_PROBEPROV:
3522 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3523 		return (dtrace_dif_varstr(
3524 		    (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name,
3525 		    state, mstate));
3526 
3527 	case DIF_VAR_PROBEMOD:
3528 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3529 		return (dtrace_dif_varstr(
3530 		    (uintptr_t)mstate->dtms_probe->dtpr_mod,
3531 		    state, mstate));
3532 
3533 	case DIF_VAR_PROBEFUNC:
3534 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3535 		return (dtrace_dif_varstr(
3536 		    (uintptr_t)mstate->dtms_probe->dtpr_func,
3537 		    state, mstate));
3538 
3539 	case DIF_VAR_PROBENAME:
3540 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3541 		return (dtrace_dif_varstr(
3542 		    (uintptr_t)mstate->dtms_probe->dtpr_name,
3543 		    state, mstate));
3544 
3545 	case DIF_VAR_PID:
3546 		if (!dtrace_priv_proc_relaxed(state))
3547 			return (0);
3548 
3549 		/*
3550 		 * Note that we are assuming that an unanchored probe is
3551 		 * always due to a high-level interrupt.  (And we're assuming
3552 		 * that there is only a single high level interrupt.)
3553 		 */
3554 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3555 			/* Anchored probe that fires while on an interrupt accrues to process 0 */
3556 			return 0;
3557 
3558 		return ((uint64_t)dtrace_proc_selfpid());
3559 
3560 	case DIF_VAR_PPID:
3561 		if (!dtrace_priv_proc_relaxed(state))
3562 			return (0);
3563 
3564 		/*
3565 		 * See comment in DIF_VAR_PID.
3566 		 */
3567 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3568 			return (0);
3569 
3570 		return ((uint64_t)dtrace_proc_selfppid());
3571 
3572 	case DIF_VAR_TID:
3573 		/* We do not need to check for null current_thread() */
3574 		return thread_tid(current_thread()); /* globally unique */
3575 
3576 	case DIF_VAR_PTHREAD_SELF:
3577 		if (!dtrace_priv_proc(state))
3578 			return (0);
3579 
3580 		/* Not currently supported, but we should be able to delta the dispatchqaddr and dispatchqoffset to get pthread_self */
3581 		return 0;
3582 
3583 	case DIF_VAR_DISPATCHQADDR:
3584 		if (!dtrace_priv_proc(state))
3585 			return (0);
3586 
3587 		/* We do not need to check for null current_thread() */
3588 		return thread_dispatchqaddr(current_thread());
3589 
3590 	case DIF_VAR_EXECNAME:
3591 	{
3592 		char *xname = (char *)mstate->dtms_scratch_ptr;
3593 		const char *pname = proc_best_name(curproc);
3594 		size_t scratch_size = sizeof(proc_name_t);
3595 
3596 		/* The scratch allocation's lifetime is that of the clause. */
3597 		if (!DTRACE_INSCRATCH(mstate, scratch_size)) {
3598 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3599 			return 0;
3600 		}
3601 
3602 		if (!dtrace_priv_proc_relaxed(state))
3603 			return (0);
3604 
3605 		mstate->dtms_scratch_ptr += scratch_size;
3606 		strlcpy(xname, pname, scratch_size);
3607 
3608 		return ((uint64_t)(uintptr_t)xname);
3609 	}
3610 
3611 
3612 	case DIF_VAR_ZONENAME:
3613 	{
3614 		/* scratch_size is equal to length('global') + 1 for the null-terminator. */
3615 		char *zname = (char *)mstate->dtms_scratch_ptr;
3616 		size_t scratch_size = 6 + 1;
3617 
3618 		if (!dtrace_priv_proc(state))
3619 			return (0);
3620 
3621 		/* The scratch allocation's lifetime is that of the clause. */
3622 		if (!DTRACE_INSCRATCH(mstate, scratch_size)) {
3623 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3624 			return 0;
3625 		}
3626 
3627 		mstate->dtms_scratch_ptr += scratch_size;
3628 
3629 		/* The kernel does not provide zonename, it will always return 'global'. */
3630 		strlcpy(zname, "global", scratch_size);
3631 
3632 		return ((uint64_t)(uintptr_t)zname);
3633 	}
3634 
3635 #if CONFIG_PERVASIVE_CPI && CONFIG_CPU_COUNTERS
3636 	case DIF_VAR_CPUINSTRS:
3637 		return mt_cur_cpu_instrs();
3638 
3639 	case DIF_VAR_CPUCYCLES:
3640 		return mt_cur_cpu_cycles();
3641 
3642 	case DIF_VAR_VINSTRS: {
3643 		struct recount_usage usage = { 0 };
3644 		recount_current_thread_usage(&usage);
3645 		return recount_usage_instructions(&usage);
3646 	}
3647 
3648 	case DIF_VAR_VCYCLES: {
3649         struct recount_usage usage = { 0 };
3650         recount_current_thread_usage(&usage);
3651 		return recount_usage_cycles(&usage);
3652     }
3653 
3654 #else /* CONFIG_PERVASIVE_CPI && CONFIG_CPU_COUNTERS */
3655 	case DIF_VAR_CPUINSTRS:
3656 	case DIF_VAR_CPUCYCLES:
3657 	case DIF_VAR_VINSTRS:
3658 	case DIF_VAR_VCYCLES:
3659 		return 0;
3660 #endif /* !CONFIG_PERVASIVE_CPI || !CONFIG_CPU_COUNTERS */
3661 
3662 	case DIF_VAR_UID:
3663 		if (!dtrace_priv_proc_relaxed(state))
3664 			return (0);
3665 
3666 		/*
3667 		 * See comment in DIF_VAR_PID.
3668 		 */
3669 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3670 			return (0);
3671 
3672 		return ((uint64_t) dtrace_proc_selfruid());
3673 
3674 	case DIF_VAR_GID:
3675 		if (!dtrace_priv_proc(state))
3676 			return (0);
3677 
3678 		/*
3679 		 * See comment in DIF_VAR_PID.
3680 		 */
3681 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3682 			return (0);
3683 
3684 		if (dtrace_CRED() != NULL)
3685 			/* Credential does not require lazy initialization. */
3686 			return ((uint64_t)kauth_getgid());
3687 		else {
3688 			/* proc_lock would be taken under kauth_cred_proc_ref() in kauth_cred_get(). */
3689 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3690 			return -1ULL;
3691 		}
3692 
3693 	case DIF_VAR_ERRNO: {
3694 		uthread_t uthread = current_uthread();
3695 		if (!dtrace_priv_proc(state))
3696 			return (0);
3697 
3698 		/*
3699 		 * See comment in DIF_VAR_PID.
3700 		 */
3701 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3702 			return (0);
3703 
3704 		if (uthread)
3705 			return (uint64_t)uthread->t_dtrace_errno;
3706 		else {
3707 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3708 			return -1ULL;
3709 		}
3710 	}
3711 
3712 	default:
3713 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3714 		return (0);
3715 	}
3716 }
3717 
3718 typedef enum dtrace_json_state {
3719 	DTRACE_JSON_REST = 1,
3720 	DTRACE_JSON_OBJECT,
3721 	DTRACE_JSON_STRING,
3722 	DTRACE_JSON_STRING_ESCAPE,
3723 	DTRACE_JSON_STRING_ESCAPE_UNICODE,
3724 	DTRACE_JSON_COLON,
3725 	DTRACE_JSON_COMMA,
3726 	DTRACE_JSON_VALUE,
3727 	DTRACE_JSON_IDENTIFIER,
3728 	DTRACE_JSON_NUMBER,
3729 	DTRACE_JSON_NUMBER_FRAC,
3730 	DTRACE_JSON_NUMBER_EXP,
3731 	DTRACE_JSON_COLLECT_OBJECT
3732 } dtrace_json_state_t;
3733 
3734 /*
3735  * This function possesses just enough knowledge about JSON to extract a single
3736  * value from a JSON string and store it in the scratch buffer.  It is able
3737  * to extract nested object values, and members of arrays by index.
3738  *
3739  * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to
3740  * be looked up as we descend into the object tree.  e.g.
3741  *
3742  *    foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL
3743  *       with nelems = 5.
3744  *
3745  * The run time of this function must be bounded above by strsize to limit the
3746  * amount of work done in probe context.  As such, it is implemented as a
3747  * simple state machine, reading one character at a time using safe loads
3748  * until we find the requested element, hit a parsing error or run off the
3749  * end of the object or string.
3750  *
3751  * As there is no way for a subroutine to return an error without interrupting
3752  * clause execution, we simply return NULL in the event of a missing key or any
3753  * other error condition.  Each NULL return in this function is commented with
3754  * the error condition it represents -- parsing or otherwise.
3755  *
3756  * The set of states for the state machine closely matches the JSON
3757  * specification (http://json.org/).  Briefly:
3758  *
3759  *   DTRACE_JSON_REST:
3760  *     Skip whitespace until we find either a top-level Object, moving
3761  *     to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE.
3762  *
3763  *   DTRACE_JSON_OBJECT:
3764  *     Locate the next key String in an Object.  Sets a flag to denote
3765  *     the next String as a key string and moves to DTRACE_JSON_STRING.
3766  *
3767  *   DTRACE_JSON_COLON:
3768  *     Skip whitespace until we find the colon that separates key Strings
3769  *     from their values.  Once found, move to DTRACE_JSON_VALUE.
3770  *
3771  *   DTRACE_JSON_VALUE:
3772  *     Detects the type of the next value (String, Number, Identifier, Object
3773  *     or Array) and routes to the states that process that type.  Here we also
3774  *     deal with the element selector list if we are requested to traverse down
3775  *     into the object tree.
3776  *
3777  *   DTRACE_JSON_COMMA:
3778  *     Skip whitespace until we find the comma that separates key-value pairs
3779  *     in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays
3780  *     (similarly DTRACE_JSON_VALUE).  All following literal value processing
3781  *     states return to this state at the end of their value, unless otherwise
3782  *     noted.
3783  *
3784  *   DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP:
3785  *     Processes a Number literal from the JSON, including any exponent
3786  *     component that may be present.  Numbers are returned as strings, which
3787  *     may be passed to strtoll() if an integer is required.
3788  *
3789  *   DTRACE_JSON_IDENTIFIER:
3790  *     Processes a "true", "false" or "null" literal in the JSON.
3791  *
3792  *   DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE,
3793  *   DTRACE_JSON_STRING_ESCAPE_UNICODE:
3794  *     Processes a String literal from the JSON, whether the String denotes
3795  *     a key, a value or part of a larger Object.  Handles all escape sequences
3796  *     present in the specification, including four-digit unicode characters,
3797  *     but merely includes the escape sequence without converting it to the
3798  *     actual escaped character.  If the String is flagged as a key, we
3799  *     move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA.
3800  *
3801  *   DTRACE_JSON_COLLECT_OBJECT:
3802  *     This state collects an entire Object (or Array), correctly handling
3803  *     embedded strings.  If the full element selector list matches this nested
3804  *     object, we return the Object in full as a string.  If not, we use this
3805  *     state to skip to the next value at this level and continue processing.
3806  */
3807 static char *
dtrace_json(uint64_t size,uintptr_t json,char * elemlist,int nelems,char * dest)3808 dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems,
3809     char *dest)
3810 {
3811 	dtrace_json_state_t state = DTRACE_JSON_REST;
3812 	int64_t array_elem = INT64_MIN;
3813 	int64_t array_pos = 0;
3814 	uint8_t escape_unicount = 0;
3815 	boolean_t string_is_key = B_FALSE;
3816 	boolean_t collect_object = B_FALSE;
3817 	boolean_t found_key = B_FALSE;
3818 	boolean_t in_array = B_FALSE;
3819 	uint32_t braces = 0, brackets = 0;
3820 	char *elem = elemlist;
3821 	char *dd = dest;
3822 	uintptr_t cur;
3823 
3824 	for (cur = json; cur < json + size; cur++) {
3825 		char cc = dtrace_load8(cur);
3826 		if (cc == '\0')
3827 			return (NULL);
3828 
3829 		switch (state) {
3830 		case DTRACE_JSON_REST:
3831 			if (isspace(cc))
3832 				break;
3833 
3834 			if (cc == '{') {
3835 				state = DTRACE_JSON_OBJECT;
3836 				break;
3837 			}
3838 
3839 			if (cc == '[') {
3840 				in_array = B_TRUE;
3841 				array_pos = 0;
3842 				array_elem = dtrace_strtoll(elem, 10, size);
3843 				found_key = array_elem == 0 ? B_TRUE : B_FALSE;
3844 				state = DTRACE_JSON_VALUE;
3845 				break;
3846 			}
3847 
3848 			/*
3849 			 * ERROR: expected to find a top-level object or array.
3850 			 */
3851 			return (NULL);
3852 		case DTRACE_JSON_OBJECT:
3853 			if (isspace(cc))
3854 				break;
3855 
3856 			if (cc == '"') {
3857 				state = DTRACE_JSON_STRING;
3858 				string_is_key = B_TRUE;
3859 				break;
3860 			}
3861 
3862 			/*
3863 			 * ERROR: either the object did not start with a key
3864 			 * string, or we've run off the end of the object
3865 			 * without finding the requested key.
3866 			 */
3867 			return (NULL);
3868 		case DTRACE_JSON_STRING:
3869 			if (cc == '\\') {
3870 				*dd++ = '\\';
3871 				state = DTRACE_JSON_STRING_ESCAPE;
3872 				break;
3873 			}
3874 
3875 			if (cc == '"') {
3876 				if (collect_object) {
3877 					/*
3878 					 * We don't reset the dest here, as
3879 					 * the string is part of a larger
3880 					 * object being collected.
3881 					 */
3882 					*dd++ = cc;
3883 					collect_object = B_FALSE;
3884 					state = DTRACE_JSON_COLLECT_OBJECT;
3885 					break;
3886 				}
3887 				*dd = '\0';
3888 				dd = dest; /* reset string buffer */
3889 				if (string_is_key) {
3890 					if (dtrace_strncmp(dest, elem,
3891 					    size) == 0)
3892 						found_key = B_TRUE;
3893 				} else if (found_key) {
3894 					if (nelems > 1) {
3895 						/*
3896 						 * We expected an object, not
3897 						 * this string.
3898 						 */
3899 						return (NULL);
3900 					}
3901 					return (dest);
3902 				}
3903 				state = string_is_key ? DTRACE_JSON_COLON :
3904 				    DTRACE_JSON_COMMA;
3905 				string_is_key = B_FALSE;
3906 				break;
3907 			}
3908 
3909 			*dd++ = cc;
3910 			break;
3911 		case DTRACE_JSON_STRING_ESCAPE:
3912 			*dd++ = cc;
3913 			if (cc == 'u') {
3914 				escape_unicount = 0;
3915 				state = DTRACE_JSON_STRING_ESCAPE_UNICODE;
3916 			} else {
3917 				state = DTRACE_JSON_STRING;
3918 			}
3919 			break;
3920 		case DTRACE_JSON_STRING_ESCAPE_UNICODE:
3921 			if (!isxdigit(cc)) {
3922 				/*
3923 				 * ERROR: invalid unicode escape, expected
3924 				 * four valid hexidecimal digits.
3925 				 */
3926 				return (NULL);
3927 			}
3928 
3929 			*dd++ = cc;
3930 			if (++escape_unicount == 4)
3931 				state = DTRACE_JSON_STRING;
3932 			break;
3933 		case DTRACE_JSON_COLON:
3934 			if (isspace(cc))
3935 				break;
3936 
3937 			if (cc == ':') {
3938 				state = DTRACE_JSON_VALUE;
3939 				break;
3940 			}
3941 
3942 			/*
3943 			 * ERROR: expected a colon.
3944 			 */
3945 			return (NULL);
3946 		case DTRACE_JSON_COMMA:
3947 			if (isspace(cc))
3948 				break;
3949 
3950 			if (cc == ',') {
3951 				if (in_array) {
3952 					state = DTRACE_JSON_VALUE;
3953 					if (++array_pos == array_elem)
3954 						found_key = B_TRUE;
3955 				} else {
3956 					state = DTRACE_JSON_OBJECT;
3957 				}
3958 				break;
3959 			}
3960 
3961 			/*
3962 			 * ERROR: either we hit an unexpected character, or
3963 			 * we reached the end of the object or array without
3964 			 * finding the requested key.
3965 			 */
3966 			return (NULL);
3967 		case DTRACE_JSON_IDENTIFIER:
3968 			if (islower(cc)) {
3969 				*dd++ = cc;
3970 				break;
3971 			}
3972 
3973 			*dd = '\0';
3974 			dd = dest; /* reset string buffer */
3975 
3976 			if (dtrace_strncmp(dest, "true", 5) == 0 ||
3977 			    dtrace_strncmp(dest, "false", 6) == 0 ||
3978 			    dtrace_strncmp(dest, "null", 5) == 0) {
3979 				if (found_key) {
3980 					if (nelems > 1) {
3981 						/*
3982 						 * ERROR: We expected an object,
3983 						 * not this identifier.
3984 						 */
3985 						return (NULL);
3986 					}
3987 					return (dest);
3988 				} else {
3989 					cur--;
3990 					state = DTRACE_JSON_COMMA;
3991 					break;
3992 				}
3993 			}
3994 
3995 			/*
3996 			 * ERROR: we did not recognise the identifier as one
3997 			 * of those in the JSON specification.
3998 			 */
3999 			return (NULL);
4000 		case DTRACE_JSON_NUMBER:
4001 			if (cc == '.') {
4002 				*dd++ = cc;
4003 				state = DTRACE_JSON_NUMBER_FRAC;
4004 				break;
4005 			}
4006 
4007 			if (cc == 'x' || cc == 'X') {
4008 				/*
4009 				 * ERROR: specification explicitly excludes
4010 				 * hexidecimal or octal numbers.
4011 				 */
4012 				return (NULL);
4013 			}
4014 
4015 			OS_FALLTHROUGH;
4016 		case DTRACE_JSON_NUMBER_FRAC:
4017 			if (cc == 'e' || cc == 'E') {
4018 				*dd++ = cc;
4019 				state = DTRACE_JSON_NUMBER_EXP;
4020 				break;
4021 			}
4022 
4023 			if (cc == '+' || cc == '-') {
4024 				/*
4025 				 * ERROR: expect sign as part of exponent only.
4026 				 */
4027 				return (NULL);
4028 			}
4029 			OS_FALLTHROUGH;
4030 		case DTRACE_JSON_NUMBER_EXP:
4031 			if (isdigit(cc) || cc == '+' || cc == '-') {
4032 				*dd++ = cc;
4033 				break;
4034 			}
4035 
4036 			*dd = '\0';
4037 			dd = dest; /* reset string buffer */
4038 			if (found_key) {
4039 				if (nelems > 1) {
4040 					/*
4041 					 * ERROR: We expected an object, not
4042 					 * this number.
4043 					 */
4044 					return (NULL);
4045 				}
4046 				return (dest);
4047 			}
4048 
4049 			cur--;
4050 			state = DTRACE_JSON_COMMA;
4051 			break;
4052 		case DTRACE_JSON_VALUE:
4053 			if (isspace(cc))
4054 				break;
4055 
4056 			if (cc == '{' || cc == '[') {
4057 				if (nelems > 1 && found_key) {
4058 					in_array = cc == '[' ? B_TRUE : B_FALSE;
4059 					/*
4060 					 * If our element selector directs us
4061 					 * to descend into this nested object,
4062 					 * then move to the next selector
4063 					 * element in the list and restart the
4064 					 * state machine.
4065 					 */
4066 					while (*elem != '\0')
4067 						elem++;
4068 					elem++; /* skip the inter-element NUL */
4069 					nelems--;
4070 					dd = dest;
4071 					if (in_array) {
4072 						state = DTRACE_JSON_VALUE;
4073 						array_pos = 0;
4074 						array_elem = dtrace_strtoll(
4075 						    elem, 10, size);
4076 						found_key = array_elem == 0 ?
4077 						    B_TRUE : B_FALSE;
4078 					} else {
4079 						found_key = B_FALSE;
4080 						state = DTRACE_JSON_OBJECT;
4081 					}
4082 					break;
4083 				}
4084 
4085 				/*
4086 				 * Otherwise, we wish to either skip this
4087 				 * nested object or return it in full.
4088 				 */
4089 				if (cc == '[')
4090 					brackets = 1;
4091 				else
4092 					braces = 1;
4093 				*dd++ = cc;
4094 				state = DTRACE_JSON_COLLECT_OBJECT;
4095 				break;
4096 			}
4097 
4098 			if (cc == '"') {
4099 				state = DTRACE_JSON_STRING;
4100 				break;
4101 			}
4102 
4103 			if (islower(cc)) {
4104 				/*
4105 				 * Here we deal with true, false and null.
4106 				 */
4107 				*dd++ = cc;
4108 				state = DTRACE_JSON_IDENTIFIER;
4109 				break;
4110 			}
4111 
4112 			if (cc == '-' || isdigit(cc)) {
4113 				*dd++ = cc;
4114 				state = DTRACE_JSON_NUMBER;
4115 				break;
4116 			}
4117 
4118 			/*
4119 			 * ERROR: unexpected character at start of value.
4120 			 */
4121 			return (NULL);
4122 		case DTRACE_JSON_COLLECT_OBJECT:
4123 			if (cc == '\0')
4124 				/*
4125 				 * ERROR: unexpected end of input.
4126 				 */
4127 				return (NULL);
4128 
4129 			*dd++ = cc;
4130 			if (cc == '"') {
4131 				collect_object = B_TRUE;
4132 				state = DTRACE_JSON_STRING;
4133 				break;
4134 			}
4135 
4136 			if (cc == ']') {
4137 				if (brackets-- == 0) {
4138 					/*
4139 					 * ERROR: unbalanced brackets.
4140 					 */
4141 					return (NULL);
4142 				}
4143 			} else if (cc == '}') {
4144 				if (braces-- == 0) {
4145 					/*
4146 					 * ERROR: unbalanced braces.
4147 					 */
4148 					return (NULL);
4149 				}
4150 			} else if (cc == '{') {
4151 				braces++;
4152 			} else if (cc == '[') {
4153 				brackets++;
4154 			}
4155 
4156 			if (brackets == 0 && braces == 0) {
4157 				if (found_key) {
4158 					*dd = '\0';
4159 					return (dest);
4160 				}
4161 				dd = dest; /* reset string buffer */
4162 				state = DTRACE_JSON_COMMA;
4163 			}
4164 			break;
4165 		}
4166 	}
4167 	return (NULL);
4168 }
4169 
4170 /*
4171  * Emulate the execution of DTrace ID subroutines invoked by the call opcode.
4172  * Notice that we don't bother validating the proper number of arguments or
4173  * their types in the tuple stack.  This isn't needed because all argument
4174  * interpretation is safe because of our load safety -- the worst that can
4175  * happen is that a bogus program can obtain bogus results.
4176  */
4177 static void
dtrace_dif_subr(uint_t subr,uint_t rd,uint64_t * regs,dtrace_key_t * tupregs,int nargs,dtrace_mstate_t * mstate,dtrace_state_t * state)4178 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs,
4179     dtrace_key_t *tupregs, int nargs,
4180     dtrace_mstate_t *mstate, dtrace_state_t *state)
4181 {
4182 	volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
4183 	volatile uint64_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
4184 	dtrace_vstate_t *vstate = &state->dts_vstate;
4185 
4186 #if !defined(__APPLE__)
4187 	union {
4188 		mutex_impl_t mi;
4189 		uint64_t mx;
4190 	} m;
4191 
4192 	union {
4193 		krwlock_t ri;
4194 		uintptr_t rw;
4195 	} r;
4196 #else
4197 /* FIXME: awaits lock/mutex work */
4198 #endif /* __APPLE__ */
4199 
4200 	switch (subr) {
4201 	case DIF_SUBR_RAND:
4202 		regs[rd] = dtrace_xoroshiro128_plus_next(
4203 		    state->dts_rstate[CPU->cpu_id]);
4204 		break;
4205 
4206 #if !defined(__APPLE__)
4207 	case DIF_SUBR_MUTEX_OWNED:
4208 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4209 		    mstate, vstate)) {
4210 			regs[rd] = 0;
4211 			break;
4212 		}
4213 
4214 		m.mx = dtrace_load64(tupregs[0].dttk_value);
4215 		if (MUTEX_TYPE_ADAPTIVE(&m.mi))
4216 			regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER;
4217 		else
4218 			regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock);
4219 		break;
4220 
4221 	case DIF_SUBR_MUTEX_OWNER:
4222 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4223 		    mstate, vstate)) {
4224 			regs[rd] = 0;
4225 			break;
4226 		}
4227 
4228 		m.mx = dtrace_load64(tupregs[0].dttk_value);
4229 		if (MUTEX_TYPE_ADAPTIVE(&m.mi) &&
4230 		    MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER)
4231 			regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi);
4232 		else
4233 			regs[rd] = 0;
4234 		break;
4235 
4236 	case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
4237 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4238 		    mstate, vstate)) {
4239 			regs[rd] = 0;
4240 			break;
4241 		}
4242 
4243 		m.mx = dtrace_load64(tupregs[0].dttk_value);
4244 		regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi);
4245 		break;
4246 
4247 	case DIF_SUBR_MUTEX_TYPE_SPIN:
4248 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4249 		    mstate, vstate)) {
4250 			regs[rd] = 0;
4251 			break;
4252 		}
4253 
4254 		m.mx = dtrace_load64(tupregs[0].dttk_value);
4255 		regs[rd] = MUTEX_TYPE_SPIN(&m.mi);
4256 		break;
4257 
4258 	case DIF_SUBR_RW_READ_HELD: {
4259 		uintptr_t tmp;
4260 
4261 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
4262 		    mstate, vstate)) {
4263 			regs[rd] = 0;
4264 			break;
4265 		}
4266 
4267 		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
4268 		regs[rd] = _RW_READ_HELD(&r.ri, tmp);
4269 		break;
4270 	}
4271 
4272 	case DIF_SUBR_RW_WRITE_HELD:
4273 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
4274 		    mstate, vstate)) {
4275 			regs[rd] = 0;
4276 			break;
4277 		}
4278 
4279 		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
4280 		regs[rd] = _RW_WRITE_HELD(&r.ri);
4281 		break;
4282 
4283 	case DIF_SUBR_RW_ISWRITER:
4284 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
4285 		    mstate, vstate)) {
4286 			regs[rd] = 0;
4287 			break;
4288 		}
4289 
4290 		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
4291 		regs[rd] = _RW_ISWRITER(&r.ri);
4292 		break;
4293 #else
4294 /* FIXME: awaits lock/mutex work */
4295 #endif /* __APPLE__ */
4296 
4297 	case DIF_SUBR_BCOPY: {
4298 		/*
4299 		 * We need to be sure that the destination is in the scratch
4300 		 * region -- no other region is allowed.
4301 		 */
4302 		uintptr_t src = tupregs[0].dttk_value;
4303 		uintptr_t dest = tupregs[1].dttk_value;
4304 		size_t size = tupregs[2].dttk_value;
4305 
4306 		if (!dtrace_inscratch(dest, size, mstate)) {
4307 			*flags |= CPU_DTRACE_BADADDR;
4308 			*illval = regs[rd];
4309 			break;
4310 		}
4311 
4312 		if (!dtrace_canload(src, size, mstate, vstate)) {
4313 			regs[rd] = 0;
4314 			break;
4315 		}
4316 
4317 		dtrace_bcopy((void *)src, (void *)dest, size);
4318 		break;
4319 	}
4320 
4321 	case DIF_SUBR_ALLOCA:
4322 	case DIF_SUBR_COPYIN: {
4323 		uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
4324 		uint64_t size =
4325 		    tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value;
4326 		size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size;
4327 
4328 		/*
4329 		 * Check whether the user can access kernel memory
4330 		 */
4331 		if (dtrace_priv_kernel(state) == 0) {
4332 			DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV);
4333 			regs[rd] = 0;
4334 			break;
4335 		}
4336 		/*
4337 		 * This action doesn't require any credential checks since
4338 		 * probes will not activate in user contexts to which the
4339 		 * enabling user does not have permissions.
4340 		 */
4341 
4342 		/*
4343 		 * Rounding up the user allocation size could have overflowed
4344 		 * a large, bogus allocation (like -1ULL) to 0.
4345 		 */
4346 		if (scratch_size < size ||
4347 		    !DTRACE_INSCRATCH(mstate, scratch_size)) {
4348 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4349 			regs[rd] = 0;
4350 			break;
4351 		}
4352 
4353 		if (subr == DIF_SUBR_COPYIN) {
4354 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4355 			if (dtrace_priv_proc(state))
4356 				dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
4357 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4358 		}
4359 
4360 		mstate->dtms_scratch_ptr += scratch_size;
4361 		regs[rd] = dest;
4362 		break;
4363 	}
4364 
4365 	case DIF_SUBR_COPYINTO: {
4366 		uint64_t size = tupregs[1].dttk_value;
4367 		uintptr_t dest = tupregs[2].dttk_value;
4368 
4369 		/*
4370 		 * This action doesn't require any credential checks since
4371 		 * probes will not activate in user contexts to which the
4372 		 * enabling user does not have permissions.
4373 		 */
4374 		if (!dtrace_inscratch(dest, size, mstate)) {
4375 			*flags |= CPU_DTRACE_BADADDR;
4376 			*illval = regs[rd];
4377 			break;
4378 		}
4379 
4380 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4381 		if (dtrace_priv_proc(state))
4382 			dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
4383 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4384 		break;
4385 	}
4386 
4387 	case DIF_SUBR_COPYINSTR: {
4388 		uintptr_t dest = mstate->dtms_scratch_ptr;
4389 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4390 
4391 		if (nargs > 1 && tupregs[1].dttk_value < size)
4392 			size = tupregs[1].dttk_value + 1;
4393 
4394 		/*
4395 		 * This action doesn't require any credential checks since
4396 		 * probes will not activate in user contexts to which the
4397 		 * enabling user does not have permissions.
4398 		 */
4399 		if (!DTRACE_INSCRATCH(mstate, size)) {
4400 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4401 			regs[rd] = 0;
4402 			break;
4403 		}
4404 
4405 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4406 		if (dtrace_priv_proc(state))
4407 			dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags);
4408 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4409 
4410 		((char *)dest)[size - 1] = '\0';
4411 		mstate->dtms_scratch_ptr += size;
4412 		regs[rd] = dest;
4413 		break;
4414 	}
4415 
4416 	case DIF_SUBR_MSGSIZE:
4417 	case DIF_SUBR_MSGDSIZE: {
4418 		/* Darwin does not implement SysV streams messages */
4419 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
4420 		regs[rd] = 0;
4421 		break;
4422 	}
4423 
4424 	case DIF_SUBR_PROGENYOF: {
4425 		pid_t pid = tupregs[0].dttk_value;
4426 		struct proc *p = current_proc();
4427 		int rval = 0, lim = nprocs;
4428 
4429 		while(p && (lim-- > 0)) {
4430 			pid_t ppid;
4431 
4432 			ppid = (pid_t)dtrace_load32((uintptr_t)&(p->p_pid));
4433 			if (*flags & CPU_DTRACE_FAULT)
4434 				break;
4435 
4436 			if (ppid == pid) {
4437 				rval = 1;
4438 				break;
4439 			}
4440 
4441 			if (ppid == 0)
4442 				break; /* Can't climb process tree any further. */
4443 
4444 			p = (struct proc *)dtrace_loadptr((uintptr_t)&(p->p_pptr));
4445 #if __has_feature(ptrauth_calls)
4446 			p = ptrauth_strip(p, ptrauth_key_process_independent_data);
4447 #endif
4448 			if (*flags & CPU_DTRACE_FAULT)
4449 				break;
4450 		}
4451 
4452 		regs[rd] = rval;
4453 		break;
4454 	}
4455 
4456 	case DIF_SUBR_SPECULATION:
4457 		regs[rd] = dtrace_speculation(state);
4458 		break;
4459 
4460 
4461 	case DIF_SUBR_COPYOUT: {
4462 		uintptr_t kaddr = tupregs[0].dttk_value;
4463 		user_addr_t uaddr = tupregs[1].dttk_value;
4464 		uint64_t size = tupregs[2].dttk_value;
4465 
4466 		if (!dtrace_destructive_disallow &&
4467 		    dtrace_priv_proc_control(state) &&
4468 		    !dtrace_istoxic(kaddr, size) &&
4469 		    dtrace_canload(kaddr, size, mstate, vstate)) {
4470 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4471 			dtrace_copyout(kaddr, uaddr, size, flags);
4472 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4473 		}
4474 		break;
4475 	}
4476 
4477 	case DIF_SUBR_COPYOUTSTR: {
4478 		uintptr_t kaddr = tupregs[0].dttk_value;
4479 		user_addr_t uaddr = tupregs[1].dttk_value;
4480 		uint64_t size = tupregs[2].dttk_value;
4481 		size_t lim;
4482 
4483 		if (!dtrace_destructive_disallow &&
4484 		    dtrace_priv_proc_control(state) &&
4485 		    !dtrace_istoxic(kaddr, size) &&
4486 		    dtrace_strcanload(kaddr, size, &lim, mstate, vstate)) {
4487 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4488 			dtrace_copyoutstr(kaddr, uaddr, lim, flags);
4489 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4490 		}
4491 		break;
4492 	}
4493 
4494 	case DIF_SUBR_STRLEN: {
4495 		size_t size = state->dts_options[DTRACEOPT_STRSIZE];
4496 		uintptr_t addr = (uintptr_t)tupregs[0].dttk_value;
4497 		size_t lim;
4498 
4499 		if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) {
4500 			regs[rd] = 0;
4501 			break;
4502 		}
4503 
4504 		regs[rd] = dtrace_strlen((char *)addr, lim);
4505 
4506 		break;
4507 	}
4508 
4509 	case DIF_SUBR_STRCHR:
4510 	case DIF_SUBR_STRRCHR: {
4511 		/*
4512 		 * We're going to iterate over the string looking for the
4513 		 * specified character.  We will iterate until we have reached
4514 		 * the string length or we have found the character.  If this
4515 		 * is DIF_SUBR_STRRCHR, we will look for the last occurrence
4516 		 * of the specified character instead of the first.
4517 		 */
4518 		uintptr_t addr = tupregs[0].dttk_value;
4519 		uintptr_t addr_limit;
4520 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4521 		size_t lim;
4522 		char c, target = (char)tupregs[1].dttk_value;
4523 
4524 		if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) {
4525 			regs[rd] = 0;
4526 			break;
4527 		}
4528 		addr_limit = addr + lim;
4529 
4530 		for (regs[rd] = 0; addr < addr_limit; addr++) {
4531 			if ((c = dtrace_load8(addr)) == target) {
4532 				regs[rd] = addr;
4533 
4534 				if (subr == DIF_SUBR_STRCHR)
4535 					break;
4536 			}
4537 
4538 			if (c == '\0')
4539 				break;
4540 		}
4541 
4542 		break;
4543 	}
4544 
4545 	case DIF_SUBR_STRSTR:
4546 	case DIF_SUBR_INDEX:
4547 	case DIF_SUBR_RINDEX: {
4548 		/*
4549 		 * We're going to iterate over the string looking for the
4550 		 * specified string.  We will iterate until we have reached
4551 		 * the string length or we have found the string.  (Yes, this
4552 		 * is done in the most naive way possible -- but considering
4553 		 * that the string we're searching for is likely to be
4554 		 * relatively short, the complexity of Rabin-Karp or similar
4555 		 * hardly seems merited.)
4556 		 */
4557 		char *addr = (char *)(uintptr_t)tupregs[0].dttk_value;
4558 		char *substr = (char *)(uintptr_t)tupregs[1].dttk_value;
4559 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4560 		size_t len = dtrace_strlen(addr, size);
4561 		size_t sublen = dtrace_strlen(substr, size);
4562 		char *limit = addr + len, *orig = addr;
4563 		int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1;
4564 		int inc = 1;
4565 
4566 		regs[rd] = notfound;
4567 
4568 		if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) {
4569 			regs[rd] = 0;
4570 			break;
4571 		}
4572 
4573 		if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate,
4574 		    vstate)) {
4575 			regs[rd] = 0;
4576 			break;
4577 		}
4578 
4579 		/*
4580 		 * strstr() and index()/rindex() have similar semantics if
4581 		 * both strings are the empty string: strstr() returns a
4582 		 * pointer to the (empty) string, and index() and rindex()
4583 		 * both return index 0 (regardless of any position argument).
4584 		 */
4585 		if (sublen == 0 && len == 0) {
4586 			if (subr == DIF_SUBR_STRSTR)
4587 				regs[rd] = (uintptr_t)addr;
4588 			else
4589 				regs[rd] = 0;
4590 			break;
4591 		}
4592 
4593 		if (subr != DIF_SUBR_STRSTR) {
4594 			if (subr == DIF_SUBR_RINDEX) {
4595 				limit = orig - 1;
4596 				addr += len;
4597 				inc = -1;
4598 			}
4599 
4600 			/*
4601 			 * Both index() and rindex() take an optional position
4602 			 * argument that denotes the starting position.
4603 			 */
4604 			if (nargs == 3) {
4605 				int64_t pos = (int64_t)tupregs[2].dttk_value;
4606 
4607 				/*
4608 				 * If the position argument to index() is
4609 				 * negative, Perl implicitly clamps it at
4610 				 * zero.  This semantic is a little surprising
4611 				 * given the special meaning of negative
4612 				 * positions to similar Perl functions like
4613 				 * substr(), but it appears to reflect a
4614 				 * notion that index() can start from a
4615 				 * negative index and increment its way up to
4616 				 * the string.  Given this notion, Perl's
4617 				 * rindex() is at least self-consistent in
4618 				 * that it implicitly clamps positions greater
4619 				 * than the string length to be the string
4620 				 * length.  Where Perl completely loses
4621 				 * coherence, however, is when the specified
4622 				 * substring is the empty string ("").  In
4623 				 * this case, even if the position is
4624 				 * negative, rindex() returns 0 -- and even if
4625 				 * the position is greater than the length,
4626 				 * index() returns the string length.  These
4627 				 * semantics violate the notion that index()
4628 				 * should never return a value less than the
4629 				 * specified position and that rindex() should
4630 				 * never return a value greater than the
4631 				 * specified position.  (One assumes that
4632 				 * these semantics are artifacts of Perl's
4633 				 * implementation and not the results of
4634 				 * deliberate design -- it beggars belief that
4635 				 * even Larry Wall could desire such oddness.)
4636 				 * While in the abstract one would wish for
4637 				 * consistent position semantics across
4638 				 * substr(), index() and rindex() -- or at the
4639 				 * very least self-consistent position
4640 				 * semantics for index() and rindex() -- we
4641 				 * instead opt to keep with the extant Perl
4642 				 * semantics, in all their broken glory.  (Do
4643 				 * we have more desire to maintain Perl's
4644 				 * semantics than Perl does?  Probably.)
4645 				 */
4646 				if (subr == DIF_SUBR_RINDEX) {
4647 					if (pos < 0) {
4648 						if (sublen == 0)
4649 							regs[rd] = 0;
4650 						break;
4651 					}
4652 
4653 					if ((size_t)pos > len)
4654 						pos = len;
4655 				} else {
4656 					if (pos < 0)
4657 						pos = 0;
4658 
4659 					if ((size_t)pos >= len) {
4660 						if (sublen == 0)
4661 							regs[rd] = len;
4662 						break;
4663 					}
4664 				}
4665 
4666 				addr = orig + pos;
4667 			}
4668 		}
4669 
4670 		for (regs[rd] = notfound; addr != limit; addr += inc) {
4671 			if (dtrace_strncmp(addr, substr, sublen) == 0) {
4672 				if (subr != DIF_SUBR_STRSTR) {
4673 					/*
4674 					 * As D index() and rindex() are
4675 					 * modeled on Perl (and not on awk),
4676 					 * we return a zero-based (and not a
4677 					 * one-based) index.  (For you Perl
4678 					 * weenies: no, we're not going to add
4679 					 * $[ -- and shouldn't you be at a con
4680 					 * or something?)
4681 					 */
4682 					regs[rd] = (uintptr_t)(addr - orig);
4683 					break;
4684 				}
4685 
4686 				ASSERT(subr == DIF_SUBR_STRSTR);
4687 				regs[rd] = (uintptr_t)addr;
4688 				break;
4689 			}
4690 		}
4691 
4692 		break;
4693 	}
4694 
4695 	case DIF_SUBR_STRTOK: {
4696 		uintptr_t addr = tupregs[0].dttk_value;
4697 		uintptr_t tokaddr = tupregs[1].dttk_value;
4698 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4699 		uintptr_t limit, toklimit;
4700 		size_t clim;
4701 		char *dest = (char *)mstate->dtms_scratch_ptr;
4702 		uint8_t c='\0', tokmap[32];	 /* 256 / 8 */
4703 		uint64_t i = 0;
4704 
4705 		/*
4706 		 * Check both the token buffer and (later) the input buffer,
4707 		 * since both could be non-scratch addresses.
4708 		 */
4709 		if (!dtrace_strcanload(tokaddr, size, &clim, mstate, vstate)) {
4710 			regs[rd] = 0;
4711 			break;
4712 		}
4713 		toklimit = tokaddr + clim;
4714 
4715 		if (!DTRACE_INSCRATCH(mstate, size)) {
4716 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4717 			regs[rd] = 0;
4718 			break;
4719 		}
4720 
4721 		if (addr == 0) {
4722 			/*
4723 			 * If the address specified is NULL, we use our saved
4724 			 * strtok pointer from the mstate.  Note that this
4725 			 * means that the saved strtok pointer is _only_
4726 			 * valid within multiple enablings of the same probe --
4727 			 * it behaves like an implicit clause-local variable.
4728 			 */
4729 			addr = mstate->dtms_strtok;
4730 			limit = mstate->dtms_strtok_limit;
4731 		} else {
4732 			/*
4733 			 * If the user-specified address is non-NULL we must
4734 			 * access check it.  This is the only time we have
4735 			 * a chance to do so, since this address may reside
4736 			 * in the string table of this clause-- future calls
4737 			 * (when we fetch addr from mstate->dtms_strtok)
4738 			 * would fail this access check.
4739 			 */
4740 			if (!dtrace_strcanload(addr, size, &clim, mstate,
4741 				vstate)) {
4742 				regs[rd] = 0;
4743 				break;
4744 			}
4745 			limit = addr + clim;
4746 		}
4747 
4748 		/*
4749 		 * First, zero the token map, and then process the token
4750 		 * string -- setting a bit in the map for every character
4751 		 * found in the token string.
4752 		 */
4753 		for (i = 0; i < (int)sizeof (tokmap); i++)
4754 			tokmap[i] = 0;
4755 
4756 		for (; tokaddr < toklimit; tokaddr++) {
4757 			if ((c = dtrace_load8(tokaddr)) == '\0')
4758 				break;
4759 
4760 			ASSERT((c >> 3) < sizeof (tokmap));
4761 			tokmap[c >> 3] |= (1 << (c & 0x7));
4762 		}
4763 
4764 		for (; addr < limit; addr++) {
4765 			/*
4766 			 * We're looking for a character that is _not_
4767 			 * contained in the token string.
4768 			 */
4769 			if ((c = dtrace_load8(addr)) == '\0')
4770 				break;
4771 
4772 			if (!(tokmap[c >> 3] & (1 << (c & 0x7))))
4773 				break;
4774 		}
4775 
4776 		if (c == '\0') {
4777 			/*
4778 			 * We reached the end of the string without finding
4779 			 * any character that was not in the token string.
4780 			 * We return NULL in this case, and we set the saved
4781 			 * address to NULL as well.
4782 			 */
4783 			regs[rd] = 0;
4784 			mstate->dtms_strtok = 0;
4785 			mstate->dtms_strtok_limit = 0;
4786 			break;
4787 		}
4788 
4789 		/*
4790 		 * From here on, we're copying into the destination string.
4791 		 */
4792 		for (i = 0; addr < limit && i < size - 1; addr++) {
4793 			if ((c = dtrace_load8(addr)) == '\0')
4794 				break;
4795 
4796 			if (tokmap[c >> 3] & (1 << (c & 0x7)))
4797 				break;
4798 
4799 			ASSERT(i < size);
4800 			dest[i++] = c;
4801 		}
4802 
4803 		ASSERT(i < size);
4804 		dest[i] = '\0';
4805 		regs[rd] = (uintptr_t)dest;
4806 		mstate->dtms_scratch_ptr += size;
4807 		mstate->dtms_strtok = addr;
4808 		mstate->dtms_strtok_limit = limit;
4809 		break;
4810 	}
4811 
4812 	case DIF_SUBR_SUBSTR: {
4813 		uintptr_t s = tupregs[0].dttk_value;
4814 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4815 		char *d = (char *)mstate->dtms_scratch_ptr;
4816 		int64_t index = (int64_t)tupregs[1].dttk_value;
4817 		int64_t remaining = (int64_t)tupregs[2].dttk_value;
4818 		size_t len = dtrace_strlen((char *)s, size);
4819 		int64_t i = 0;
4820 
4821 		if (!dtrace_canload(s, len + 1, mstate, vstate)) {
4822 			regs[rd] = 0;
4823 			break;
4824 		}
4825 
4826 		if (!DTRACE_INSCRATCH(mstate, size)) {
4827 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4828 			regs[rd] = 0;
4829 			break;
4830 		}
4831 
4832 		if (nargs <= 2)
4833 			remaining = (int64_t)size;
4834 
4835 		if (index < 0) {
4836 			index += len;
4837 
4838 			if (index < 0 && index + remaining > 0) {
4839 				remaining += index;
4840 				index = 0;
4841 			}
4842 		}
4843 
4844 		if ((size_t)index >= len || index < 0) {
4845 			remaining = 0;
4846 		} else if (remaining < 0) {
4847 			remaining += len - index;
4848 		} else if ((uint64_t)index + (uint64_t)remaining > size) {
4849 			remaining = size - index;
4850 		}
4851 
4852 		for (i = 0; i < remaining; i++) {
4853 			if ((d[i] = dtrace_load8(s + index + i)) == '\0')
4854 				break;
4855 			}
4856 
4857 		d[i] = '\0';
4858 
4859 		mstate->dtms_scratch_ptr += size;
4860 		regs[rd] = (uintptr_t)d;
4861 		break;
4862 	}
4863 
4864 	case DIF_SUBR_GETMAJOR:
4865 		regs[rd] = (uintptr_t)major( (dev_t)tupregs[0].dttk_value );
4866 		break;
4867 
4868 	case DIF_SUBR_GETMINOR:
4869 		regs[rd] = (uintptr_t)minor( (dev_t)tupregs[0].dttk_value );
4870 		break;
4871 
4872 	case DIF_SUBR_DDI_PATHNAME: {
4873 		/* APPLE NOTE: currently unsupported on Darwin */
4874 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
4875 		regs[rd] = 0;
4876 		break;
4877 	}
4878 
4879 	case DIF_SUBR_STRJOIN: {
4880 		char *d = (char *)mstate->dtms_scratch_ptr;
4881 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4882 		uintptr_t s1 = tupregs[0].dttk_value;
4883 		uintptr_t s2 = tupregs[1].dttk_value;
4884 		uint64_t i = 0, j = 0;
4885 		size_t lim1, lim2;
4886 		char c;
4887 
4888 		if (!dtrace_strcanload(s1, size, &lim1, mstate, vstate) ||
4889 		    !dtrace_strcanload(s2, size, &lim2, mstate, vstate)) {
4890 			regs[rd] = 0;
4891 			break;
4892 		}
4893 
4894 		if (!DTRACE_INSCRATCH(mstate, size)) {
4895 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4896 			regs[rd] = 0;
4897 			break;
4898 		}
4899 
4900 		for (;;) {
4901 			if (i >= size) {
4902 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4903 				regs[rd] = 0;
4904 				break;
4905 			}
4906 			c = (i >= lim1) ? '\0' : dtrace_load8(s1++);
4907 			if ((d[i++] = c) == '\0') {
4908 				i--;
4909 				break;
4910 			}
4911 		}
4912 
4913 		for (;;) {
4914 			if (i >= size) {
4915 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4916 				regs[rd] = 0;
4917 				break;
4918 			}
4919 			c = (j++ >= lim2) ? '\0' : dtrace_load8(s2++);
4920 			if ((d[i++] = c) == '\0')
4921 				break;
4922 		}
4923 
4924 		if (i < size) {
4925 			mstate->dtms_scratch_ptr += i;
4926 			regs[rd] = (uintptr_t)d;
4927 		}
4928 
4929 		break;
4930 	}
4931 
4932 	case DIF_SUBR_STRTOLL: {
4933 		uintptr_t s = tupregs[0].dttk_value;
4934 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4935 		size_t lim;
4936 		int base = 10;
4937 
4938 		if (nargs > 1) {
4939 			if ((base = tupregs[1].dttk_value) <= 1 ||
4940 			    base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
4941 				*flags |= CPU_DTRACE_ILLOP;
4942 				break;
4943 			}
4944 		}
4945 
4946 		if (!dtrace_strcanload(s, size, &lim, mstate, vstate)) {
4947 			regs[rd] = INT64_MIN;
4948 			break;
4949 		}
4950 
4951 		regs[rd] = dtrace_strtoll((char *)s, base, lim);
4952 		break;
4953 	}
4954 
4955 	case DIF_SUBR_LLTOSTR: {
4956 		int64_t i = (int64_t)tupregs[0].dttk_value;
4957 		uint64_t val, digit;
4958 		uint64_t size = 65;	/* enough room for 2^64 in binary */
4959 		char *end = (char *)mstate->dtms_scratch_ptr + size - 1;
4960 		int base = 10;
4961 
4962 		if (nargs > 1) {
4963 			if ((base = tupregs[1].dttk_value) <= 1 ||
4964 			     base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
4965 				*flags |= CPU_DTRACE_ILLOP;
4966 				break;
4967 			}
4968 		}
4969 
4970 		val = (base == 10 && i < 0) ? i * -1 : i;
4971 
4972 		if (!DTRACE_INSCRATCH(mstate, size)) {
4973 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4974 			regs[rd] = 0;
4975 			break;
4976 		}
4977 
4978 		for (*end-- = '\0'; val; val /= base) {
4979 			if ((digit = val % base) <= '9' - '0') {
4980 				*end-- = '0' + digit;
4981 			} else {
4982 				*end-- = 'a' + (digit - ('9' - '0') - 1);
4983 			}
4984 		}
4985 
4986 		if (i == 0 && base == 16)
4987 			*end-- = '0';
4988 
4989 		if (base == 16)
4990 			*end-- = 'x';
4991 
4992 		if (i == 0 || base == 8 || base == 16)
4993 			*end-- = '0';
4994 
4995 		if (i < 0 && base == 10)
4996 			*end-- = '-';
4997 
4998 		regs[rd] = (uintptr_t)end + 1;
4999 		mstate->dtms_scratch_ptr += size;
5000 		break;
5001 	}
5002 
5003 	case DIF_SUBR_HTONS:
5004 	case DIF_SUBR_NTOHS:
5005 #ifdef _BIG_ENDIAN
5006 		regs[rd] = (uint16_t)tupregs[0].dttk_value;
5007 #else
5008 		regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value);
5009 #endif
5010 		break;
5011 
5012 
5013 	case DIF_SUBR_HTONL:
5014 	case DIF_SUBR_NTOHL:
5015 #ifdef _BIG_ENDIAN
5016 		regs[rd] = (uint32_t)tupregs[0].dttk_value;
5017 #else
5018 		regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value);
5019 #endif
5020 		break;
5021 
5022 
5023 	case DIF_SUBR_HTONLL:
5024 	case DIF_SUBR_NTOHLL:
5025 #ifdef _BIG_ENDIAN
5026 		regs[rd] = (uint64_t)tupregs[0].dttk_value;
5027 #else
5028 		regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value);
5029 #endif
5030 		break;
5031 
5032 
5033 	case DIF_SUBR_DIRNAME:
5034 	case DIF_SUBR_BASENAME: {
5035 		char *dest = (char *)mstate->dtms_scratch_ptr;
5036 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5037 		uintptr_t src = tupregs[0].dttk_value;
5038 		int i, j, len = dtrace_strlen((char *)src, size);
5039 		int lastbase = -1, firstbase = -1, lastdir = -1;
5040 		int start, end;
5041 
5042 		if (!dtrace_canload(src, len + 1, mstate, vstate)) {
5043 			regs[rd] = 0;
5044 			break;
5045 		}
5046 
5047 		if (!DTRACE_INSCRATCH(mstate, size)) {
5048 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5049 			regs[rd] = 0;
5050 			break;
5051 		}
5052 
5053 		/*
5054 		 * The basename and dirname for a zero-length string is
5055 		 * defined to be "."
5056 		 */
5057 		if (len == 0) {
5058 			len = 1;
5059 			src = (uintptr_t)".";
5060 		}
5061 
5062 		/*
5063 		 * Start from the back of the string, moving back toward the
5064 		 * front until we see a character that isn't a slash.  That
5065 		 * character is the last character in the basename.
5066 		 */
5067 		for (i = len - 1; i >= 0; i--) {
5068 			if (dtrace_load8(src + i) != '/')
5069 				break;
5070 		}
5071 
5072 		if (i >= 0)
5073 			lastbase = i;
5074 
5075 		/*
5076 		 * Starting from the last character in the basename, move
5077 		 * towards the front until we find a slash.  The character
5078 		 * that we processed immediately before that is the first
5079 		 * character in the basename.
5080 		 */
5081 		for (; i >= 0; i--) {
5082 			if (dtrace_load8(src + i) == '/')
5083 				break;
5084 		}
5085 
5086 		if (i >= 0)
5087 			firstbase = i + 1;
5088 
5089 		/*
5090 		 * Now keep going until we find a non-slash character.  That
5091 		 * character is the last character in the dirname.
5092 		 */
5093 		for (; i >= 0; i--) {
5094 			if (dtrace_load8(src + i) != '/')
5095 				break;
5096 		}
5097 
5098 		if (i >= 0)
5099 			lastdir = i;
5100 
5101 		ASSERT(!(lastbase == -1 && firstbase != -1));
5102 		ASSERT(!(firstbase == -1 && lastdir != -1));
5103 
5104 		if (lastbase == -1) {
5105 			/*
5106 			 * We didn't find a non-slash character.  We know that
5107 			 * the length is non-zero, so the whole string must be
5108 			 * slashes.  In either the dirname or the basename
5109 			 * case, we return '/'.
5110 			 */
5111 			ASSERT(firstbase == -1);
5112 			firstbase = lastbase = lastdir = 0;
5113 		}
5114 
5115 		if (firstbase == -1) {
5116 			/*
5117 			 * The entire string consists only of a basename
5118 			 * component.  If we're looking for dirname, we need
5119 			 * to change our string to be just "."; if we're
5120 			 * looking for a basename, we'll just set the first
5121 			 * character of the basename to be 0.
5122 			 */
5123 			if (subr == DIF_SUBR_DIRNAME) {
5124 				ASSERT(lastdir == -1);
5125 				src = (uintptr_t)".";
5126 				lastdir = 0;
5127 			} else {
5128 				firstbase = 0;
5129 			}
5130 		}
5131 
5132 		if (subr == DIF_SUBR_DIRNAME) {
5133 			if (lastdir == -1) {
5134 				/*
5135 				 * We know that we have a slash in the name --
5136 				 * or lastdir would be set to 0, above.  And
5137 				 * because lastdir is -1, we know that this
5138 				 * slash must be the first character.  (That
5139 				 * is, the full string must be of the form
5140 				 * "/basename".)  In this case, the last
5141 				 * character of the directory name is 0.
5142 				 */
5143 				lastdir = 0;
5144 			}
5145 
5146 			start = 0;
5147 			end = lastdir;
5148 		} else {
5149 			ASSERT(subr == DIF_SUBR_BASENAME);
5150 			ASSERT(firstbase != -1 && lastbase != -1);
5151 			start = firstbase;
5152 			end = lastbase;
5153 		}
5154 
5155 		for (i = start, j = 0; i <= end && (uint64_t)j < size - 1; i++, j++)
5156 			dest[j] = dtrace_load8(src + i);
5157 
5158 		dest[j] = '\0';
5159 		regs[rd] = (uintptr_t)dest;
5160 		mstate->dtms_scratch_ptr += size;
5161 		break;
5162 	}
5163 
5164 	case DIF_SUBR_CLEANPATH: {
5165 		char *dest = (char *)mstate->dtms_scratch_ptr, c;
5166 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5167 		uintptr_t src = tupregs[0].dttk_value;
5168 		size_t lim;
5169 		size_t i = 0, j = 0;
5170 
5171 		if (!dtrace_strcanload(src, size, &lim, mstate, vstate)) {
5172 			regs[rd] = 0;
5173 			break;
5174 		}
5175 
5176 		if (!DTRACE_INSCRATCH(mstate, size)) {
5177 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5178 			regs[rd] = 0;
5179 			break;
5180 		}
5181 
5182 		/*
5183 		 * Move forward, loading each character.
5184 		 */
5185 		do {
5186 			c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
5187 next:
5188 			if ((uint64_t)(j + 5) >= size)	/* 5 = strlen("/..c\0") */
5189 				break;
5190 
5191 			if (c != '/') {
5192 				dest[j++] = c;
5193 				continue;
5194 			}
5195 
5196 			c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
5197 
5198 			if (c == '/') {
5199 				/*
5200 				 * We have two slashes -- we can just advance
5201 				 * to the next character.
5202 				 */
5203 				goto next;
5204 			}
5205 
5206 			if (c != '.') {
5207 				/*
5208 				 * This is not "." and it's not ".." -- we can
5209 				 * just store the "/" and this character and
5210 				 * drive on.
5211 				 */
5212 				dest[j++] = '/';
5213 				dest[j++] = c;
5214 				continue;
5215 			}
5216 
5217 			c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
5218 
5219 			if (c == '/') {
5220 				/*
5221 				 * This is a "/./" component.  We're not going
5222 				 * to store anything in the destination buffer;
5223 				 * we're just going to go to the next component.
5224 				 */
5225 				goto next;
5226 			}
5227 
5228 			if (c != '.') {
5229 				/*
5230 				 * This is not ".." -- we can just store the
5231 				 * "/." and this character and continue
5232 				 * processing.
5233 				 */
5234 				dest[j++] = '/';
5235 				dest[j++] = '.';
5236 				dest[j++] = c;
5237 				continue;
5238 			}
5239 
5240 			c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
5241 
5242 			if (c != '/' && c != '\0') {
5243 				/*
5244 				 * This is not ".." -- it's "..[mumble]".
5245 				 * We'll store the "/.." and this character
5246 				 * and continue processing.
5247 				 */
5248 				dest[j++] = '/';
5249 				dest[j++] = '.';
5250 				dest[j++] = '.';
5251 				dest[j++] = c;
5252 				continue;
5253 			}
5254 
5255 			/*
5256 			 * This is "/../" or "/..\0".  We need to back up
5257 			 * our destination pointer until we find a "/".
5258 			 */
5259 			i--;
5260 			while (j != 0 && dest[--j] != '/')
5261 				continue;
5262 
5263 			if (c == '\0')
5264 				dest[++j] = '/';
5265 		} while (c != '\0');
5266 
5267 		dest[j] = '\0';
5268 		regs[rd] = (uintptr_t)dest;
5269 		mstate->dtms_scratch_ptr += size;
5270 		break;
5271 	}
5272 
5273 	case DIF_SUBR_INET_NTOA:
5274 	case DIF_SUBR_INET_NTOA6:
5275 	case DIF_SUBR_INET_NTOP: {
5276 		size_t size;
5277 		int af, argi, i;
5278 		char *base, *end;
5279 
5280 		if (subr == DIF_SUBR_INET_NTOP) {
5281 			af = (int)tupregs[0].dttk_value;
5282 			argi = 1;
5283 		} else {
5284 			af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6;
5285 			argi = 0;
5286 		}
5287 
5288 		if (af == AF_INET) {
5289 #if !defined(__APPLE__)
5290 			ipaddr_t ip4;
5291 #else
5292 			uint32_t ip4;
5293 #endif /* __APPLE__ */
5294 			uint8_t *ptr8, val;
5295 
5296 			/*
5297 			 * Safely load the IPv4 address.
5298 			 */
5299 #if !defined(__APPLE__)
5300 			ip4 = dtrace_load32(tupregs[argi].dttk_value);
5301 #else
5302 			if (!dtrace_canload(tupregs[argi].dttk_value, sizeof(ip4),
5303 				mstate, vstate)) {
5304 				regs[rd] = 0;
5305 				break;
5306 			}
5307 
5308 			dtrace_bcopy(
5309 			    (void *)(uintptr_t)tupregs[argi].dttk_value,
5310 			    (void *)(uintptr_t)&ip4, sizeof (ip4));
5311 #endif /* __APPLE__ */
5312 			/*
5313 			 * Check an IPv4 string will fit in scratch.
5314 			 */
5315 #if !defined(__APPLE__)
5316 			size = INET_ADDRSTRLEN;
5317 #else
5318 			size = MAX_IPv4_STR_LEN;
5319 #endif /* __APPLE__ */
5320 			if (!DTRACE_INSCRATCH(mstate, size)) {
5321 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5322 				regs[rd] = 0;
5323 				break;
5324 			}
5325 			base = (char *)mstate->dtms_scratch_ptr;
5326 			end = (char *)mstate->dtms_scratch_ptr + size - 1;
5327 
5328 			/*
5329 			 * Stringify as a dotted decimal quad.
5330 			 */
5331 			*end-- = '\0';
5332 			ptr8 = (uint8_t *)&ip4;
5333 			for (i = 3; i >= 0; i--) {
5334 				val = ptr8[i];
5335 
5336 				if (val == 0) {
5337 					*end-- = '0';
5338 				} else {
5339 					for (; val; val /= 10) {
5340 						*end-- = '0' + (val % 10);
5341 					}
5342 				}
5343 
5344 				if (i > 0)
5345 					*end-- = '.';
5346 			}
5347 			ASSERT(end + 1 >= base);
5348 
5349 		} else if (af == AF_INET6) {
5350 #if defined(__APPLE__)
5351 #define _S6_un __u6_addr
5352 #define _S6_u8 __u6_addr8
5353 #endif /* __APPLE__ */
5354 			struct in6_addr ip6;
5355 			int firstzero, tryzero, numzero, v6end;
5356 			uint16_t val;
5357 			const char digits[] = "0123456789abcdef";
5358 
5359 			/*
5360 			 * Stringify using RFC 1884 convention 2 - 16 bit
5361 			 * hexadecimal values with a zero-run compression.
5362 			 * Lower case hexadecimal digits are used.
5363 			 * 	eg, fe80::214:4fff:fe0b:76c8.
5364 			 * The IPv4 embedded form is returned for inet_ntop,
5365 			 * just the IPv4 string is returned for inet_ntoa6.
5366 			 */
5367 
5368 			if (!dtrace_canload(tupregs[argi].dttk_value,
5369 				sizeof(struct in6_addr), mstate, vstate)) {
5370 				regs[rd] = 0;
5371 				break;
5372 			}
5373 
5374 			/*
5375 			 * Safely load the IPv6 address.
5376 			 */
5377 			dtrace_bcopy(
5378 			    (void *)(uintptr_t)tupregs[argi].dttk_value,
5379 			    (void *)(uintptr_t)&ip6, sizeof (struct in6_addr));
5380 
5381 			/*
5382 			 * Check an IPv6 string will fit in scratch.
5383 			 */
5384 			size = INET6_ADDRSTRLEN;
5385 			if (!DTRACE_INSCRATCH(mstate, size)) {
5386 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5387 				regs[rd] = 0;
5388 				break;
5389 			}
5390 			base = (char *)mstate->dtms_scratch_ptr;
5391 			end = (char *)mstate->dtms_scratch_ptr + size - 1;
5392 			*end-- = '\0';
5393 
5394 			/*
5395 			 * Find the longest run of 16 bit zero values
5396 			 * for the single allowed zero compression - "::".
5397 			 */
5398 			firstzero = -1;
5399 			tryzero = -1;
5400 			numzero = 1;
5401 			for (i = 0; i < (int)sizeof (struct in6_addr); i++) {
5402 				if (ip6._S6_un._S6_u8[i] == 0 &&
5403 				    tryzero == -1 && i % 2 == 0) {
5404 					tryzero = i;
5405 					continue;
5406 				}
5407 
5408 				if (tryzero != -1 &&
5409 				    (ip6._S6_un._S6_u8[i] != 0 ||
5410 				    i == sizeof (struct in6_addr) - 1)) {
5411 
5412 					if (i - tryzero <= numzero) {
5413 						tryzero = -1;
5414 						continue;
5415 					}
5416 
5417 					firstzero = tryzero;
5418 					numzero = i - i % 2 - tryzero;
5419 					tryzero = -1;
5420 
5421 					if (ip6._S6_un._S6_u8[i] == 0 &&
5422 					    i == sizeof (struct in6_addr) - 1)
5423 						numzero += 2;
5424 				}
5425 			}
5426 			ASSERT(firstzero + numzero <= (int)sizeof (struct in6_addr));
5427 
5428 			/*
5429 			 * Check for an IPv4 embedded address.
5430 			 */
5431 			v6end = sizeof (struct in6_addr) - 2;
5432 			if (IN6_IS_ADDR_V4MAPPED(&ip6) ||
5433 			    IN6_IS_ADDR_V4COMPAT(&ip6)) {
5434 				for (i = sizeof (struct in6_addr) - 1;
5435 				     i >= (int)DTRACE_V4MAPPED_OFFSET; i--) {
5436 					ASSERT(end >= base);
5437 
5438 					val = ip6._S6_un._S6_u8[i];
5439 
5440 					if (val == 0) {
5441 						*end-- = '0';
5442 					} else {
5443 						for (; val; val /= 10) {
5444 							*end-- = '0' + val % 10;
5445 						}
5446 					}
5447 
5448 					if (i > (int)DTRACE_V4MAPPED_OFFSET)
5449 						*end-- = '.';
5450 				}
5451 
5452 				if (subr == DIF_SUBR_INET_NTOA6)
5453 					goto inetout;
5454 
5455 				/*
5456 				 * Set v6end to skip the IPv4 address that
5457 				 * we have already stringified.
5458 				 */
5459 				v6end = 10;
5460 			}
5461 
5462 			/*
5463 			 * Build the IPv6 string by working through the
5464 			 * address in reverse.
5465 			 */
5466 			for (i = v6end; i >= 0; i -= 2) {
5467 				ASSERT(end >= base);
5468 
5469 				if (i == firstzero + numzero - 2) {
5470 					*end-- = ':';
5471 					*end-- = ':';
5472 					i -= numzero - 2;
5473 					continue;
5474 				}
5475 
5476 				if (i < 14 && i != firstzero - 2)
5477 					*end-- = ':';
5478 
5479 				val = (ip6._S6_un._S6_u8[i] << 8) +
5480 				    ip6._S6_un._S6_u8[i + 1];
5481 
5482 				if (val == 0) {
5483 					*end-- = '0';
5484 				} else {
5485 					for (; val; val /= 16) {
5486 						*end-- = digits[val % 16];
5487 					}
5488 				}
5489 			}
5490 			ASSERT(end + 1 >= base);
5491 
5492 #if defined(__APPLE__)
5493 #undef _S6_un
5494 #undef _S6_u8
5495 #endif /* __APPLE__ */
5496 		} else {
5497 			/*
5498 			 * The user didn't use AH_INET or AH_INET6.
5499 			 */
5500 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5501 			regs[rd] = 0;
5502 			break;
5503 		}
5504 
5505 inetout:	regs[rd] = (uintptr_t)end + 1;
5506 		mstate->dtms_scratch_ptr += size;
5507 		break;
5508 	}
5509 
5510 	case DIF_SUBR_JSON: {
5511 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5512 		uintptr_t json = tupregs[0].dttk_value;
5513 		size_t jsonlen = dtrace_strlen((char *)json, size);
5514 		uintptr_t elem = tupregs[1].dttk_value;
5515 		size_t elemlen = dtrace_strlen((char *)elem, size);
5516 
5517 		char *dest = (char *)mstate->dtms_scratch_ptr;
5518 		char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1;
5519 		char *ee = elemlist;
5520 		int nelems = 1;
5521 		uintptr_t cur;
5522 
5523 		if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) ||
5524 		    !dtrace_canload(elem, elemlen + 1, mstate, vstate)) {
5525 			regs[rd] = 0;
5526 			break;
5527 		}
5528 
5529 		if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) {
5530 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5531 			regs[rd] = 0;
5532 			break;
5533 		}
5534 
5535 		/*
5536 		 * Read the element selector and split it up into a packed list
5537 		 * of strings.
5538 		 */
5539 		for (cur = elem; cur < elem + elemlen; cur++) {
5540 			char cc = dtrace_load8(cur);
5541 
5542 			if (cur == elem && cc == '[') {
5543 				/*
5544 				 * If the first element selector key is
5545 				 * actually an array index then ignore the
5546 				 * bracket.
5547 				 */
5548 				continue;
5549 			}
5550 
5551 			if (cc == ']')
5552 				continue;
5553 
5554 			if (cc == '.' || cc == '[') {
5555 				nelems++;
5556 				cc = '\0';
5557 			}
5558 
5559 			*ee++ = cc;
5560 		}
5561 		*ee++ = '\0';
5562 
5563 		if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist,
5564 		    nelems, dest)) != 0)
5565 			mstate->dtms_scratch_ptr += jsonlen + 1;
5566 		break;
5567 	}
5568 
5569 	case DIF_SUBR_TOUPPER:
5570 	case DIF_SUBR_TOLOWER: {
5571 		uintptr_t src = tupregs[0].dttk_value;
5572 		char *dest = (char *)mstate->dtms_scratch_ptr;
5573 		char lower, upper, base, c;
5574 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5575 		size_t len = dtrace_strlen((char*) src, size);
5576 		size_t i = 0;
5577 
5578 		lower = (subr == DIF_SUBR_TOUPPER) ? 'a' : 'A';
5579 		upper = (subr == DIF_SUBR_TOUPPER) ? 'z' : 'Z';
5580 		base  = (subr == DIF_SUBR_TOUPPER) ? 'A' : 'a';
5581 
5582 		if (!dtrace_canload(src, len + 1, mstate, vstate)) {
5583 			regs[rd] = 0;
5584 			break;
5585 		}
5586 
5587 		if (!DTRACE_INSCRATCH(mstate, size)) {
5588 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5589 			regs[rd] = 0;
5590 			break;
5591 		}
5592 
5593 		for (i = 0; i < size - 1; ++i) {
5594 			if ((c = dtrace_load8(src + i)) == '\0')
5595 				break;
5596 			if (c >= lower && c <= upper)
5597 				c = base + (c - lower);
5598 			dest[i] = c;
5599 		}
5600 
5601 		ASSERT(i < size);
5602 
5603 		dest[i] = '\0';
5604 		regs[rd] = (uintptr_t) dest;
5605 		mstate->dtms_scratch_ptr += size;
5606 
5607 		break;
5608 	}
5609 
5610 	case DIF_SUBR_STRIP:
5611 		if (!dtrace_is_valid_ptrauth_key(tupregs[1].dttk_value)) {
5612 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5613 			break;
5614 		}
5615 		regs[rd] = (uint64_t)dtrace_ptrauth_strip(
5616 		    (void*)tupregs[0].dttk_value, tupregs[1].dttk_value);
5617 		break;
5618 
5619 #if defined(__APPLE__)
5620 	case DIF_SUBR_VM_KERNEL_ADDRPERM: {
5621 		if (!dtrace_priv_kernel(state)) {
5622 			regs[rd] = 0;
5623 		} else {
5624 			regs[rd] = VM_KERNEL_ADDRPERM((vm_offset_t) tupregs[0].dttk_value);
5625 		}
5626 
5627 		break;
5628 	}
5629 
5630 	case DIF_SUBR_KDEBUG_TRACE: {
5631 		uint32_t debugid;
5632 		uintptr_t args[4] = {0};
5633 		int i;
5634 
5635 		if (nargs < 2 || nargs > 5) {
5636 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5637 			break;
5638 		}
5639 
5640 		if (dtrace_destructive_disallow ||
5641 		    !dtrace_priv_kernel_destructive(state)) {
5642 			return;
5643 		}
5644 
5645 		debugid = tupregs[0].dttk_value;
5646 		for (i = 0; i < nargs - 1; i++)
5647 			args[i] = tupregs[i + 1].dttk_value;
5648 
5649 		kernel_debug(debugid, args[0], args[1], args[2], args[3], 0);
5650 
5651 		break;
5652 	}
5653 
5654 	case DIF_SUBR_KDEBUG_TRACE_STRING: {
5655 		if (nargs != 3) {
5656 			break;
5657 		}
5658 
5659 		if (dtrace_destructive_disallow ||
5660 		    !dtrace_priv_kernel_destructive(state)) {
5661 			return;
5662 		}
5663 
5664 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5665 		uint32_t debugid = tupregs[0].dttk_value;
5666 		uint64_t str_id = tupregs[1].dttk_value;
5667 		uintptr_t src = tupregs[2].dttk_value;
5668 		size_t lim;
5669 		char buf[size];
5670 		char* str = NULL;
5671 
5672 		if (src != (uintptr_t)0) {
5673 			str = buf;
5674 			if (!dtrace_strcanload(src, size, &lim, mstate, vstate)) {
5675 				break;
5676 			}
5677 			dtrace_strcpy((void*)src, buf, size);
5678 		}
5679 
5680 		(void)kernel_debug_string(debugid, &str_id, str);
5681 		regs[rd] = str_id;
5682 
5683 		break;
5684 	}
5685 
5686 	case DIF_SUBR_MTONS:
5687 		absolutetime_to_nanoseconds(tupregs[0].dttk_value, &regs[rd]);
5688 
5689 		break;
5690 	case DIF_SUBR_PHYSMEM_READ: {
5691 #if DEBUG || DEVELOPMENT
5692 		if (dtrace_destructive_disallow ||
5693 		    !dtrace_priv_kernel_destructive(state)) {
5694 			return;
5695 		}
5696 		regs[rd] = dtrace_physmem_read(tupregs[0].dttk_value,
5697 		    tupregs[1].dttk_value);
5698 #else
5699 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5700 #endif /* DEBUG || DEVELOPMENT */
5701 		break;
5702 	}
5703 	case DIF_SUBR_PHYSMEM_WRITE: {
5704 #if DEBUG || DEVELOPMENT
5705 		if (dtrace_destructive_disallow ||
5706 		    !dtrace_priv_kernel_destructive(state)) {
5707 			return;
5708 		}
5709 
5710 		dtrace_physmem_write(tupregs[0].dttk_value,
5711 		    tupregs[1].dttk_value, (size_t)tupregs[2].dttk_value);
5712 #else
5713 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5714 #endif /* DEBUG || DEVELOPMENT */
5715 		break;
5716 	}
5717 
5718 	case DIF_SUBR_KVTOPHYS: {
5719 #if DEBUG || DEVELOPMENT
5720 		regs[rd] = kvtophys(tupregs[0].dttk_value);
5721 #else
5722 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5723 #endif /* DEBUG || DEVELOPMENT */
5724 		break;
5725 	}
5726 
5727 	case DIF_SUBR_LIVEDUMP: {
5728 #if DEBUG || DEVELOPMENT
5729 		if (dtrace_destructive_disallow ||
5730 		    !dtrace_priv_kernel_destructive(state)) {
5731 			break;
5732 		}
5733 
5734 		/* For the moment, there is only one type of livedump. */
5735 		if (nargs != 1 || tupregs[0].dttk_value != 0) {
5736 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5737 			break;
5738 		}
5739 
5740 		char *dest = (char *)mstate->dtms_scratch_ptr;
5741 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5742 
5743 		if (!DTRACE_INSCRATCH(mstate, size)) {
5744 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5745 			regs[rd] = 0;
5746 			break;
5747 		}
5748 
5749 		dtrace_livedump(dest, size);
5750 		regs[rd] = (uintptr_t) dest;
5751 		mstate->dtms_scratch_ptr += strlen(dest) + 1;
5752 #else
5753 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5754 #endif /* DEBUG || DEVELOPMENT */
5755 		break;
5756 	}
5757 #endif /* defined(__APPLE__) */
5758 
5759 	}
5760 }
5761 
5762 /*
5763  * Emulate the execution of DTrace IR instructions specified by the given
5764  * DIF object.  This function is deliberately void of assertions as all of
5765  * the necessary checks are handled by a call to dtrace_difo_validate().
5766  */
5767 static uint64_t
dtrace_dif_emulate(dtrace_difo_t * difo,dtrace_mstate_t * mstate,dtrace_vstate_t * vstate,dtrace_state_t * state)5768 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate,
5769     dtrace_vstate_t *vstate, dtrace_state_t *state)
5770 {
5771 	const dif_instr_t *text = difo->dtdo_buf;
5772 	const uint_t textlen = difo->dtdo_len;
5773 	const char *strtab = difo->dtdo_strtab;
5774 	const uint64_t *inttab = difo->dtdo_inttab;
5775 
5776 	uint64_t rval = 0;
5777 	dtrace_statvar_t *svar;
5778 	dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
5779 	dtrace_difv_t *v;
5780 	volatile uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
5781 	volatile uint64_t *illval = &cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
5782 
5783 	dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
5784 	uint64_t regs[DIF_DIR_NREGS];
5785 	uint64_t *tmp;
5786 
5787 	uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0;
5788 	int64_t cc_r;
5789 	uint_t pc = 0, id, opc = 0;
5790 	uint8_t ttop = 0;
5791 	dif_instr_t instr;
5792 	uint_t r1, r2, rd;
5793 
5794 	/*
5795 	 * We stash the current DIF object into the machine state: we need it
5796 	 * for subsequent access checking.
5797 	 */
5798 	mstate->dtms_difo = difo;
5799 
5800 	regs[DIF_REG_R0] = 0; 		/* %r0 is fixed at zero */
5801 
5802 	while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) {
5803 		opc = pc;
5804 
5805 		instr = text[pc++];
5806 		r1 = DIF_INSTR_R1(instr);
5807 		r2 = DIF_INSTR_R2(instr);
5808 		rd = DIF_INSTR_RD(instr);
5809 
5810 		switch (DIF_INSTR_OP(instr)) {
5811 		case DIF_OP_OR:
5812 			regs[rd] = regs[r1] | regs[r2];
5813 			break;
5814 		case DIF_OP_XOR:
5815 			regs[rd] = regs[r1] ^ regs[r2];
5816 			break;
5817 		case DIF_OP_AND:
5818 			regs[rd] = regs[r1] & regs[r2];
5819 			break;
5820 		case DIF_OP_SLL:
5821 			regs[rd] = regs[r1] << regs[r2];
5822 			break;
5823 		case DIF_OP_SRL:
5824 			regs[rd] = regs[r1] >> regs[r2];
5825 			break;
5826 		case DIF_OP_SUB:
5827 			regs[rd] = regs[r1] - regs[r2];
5828 			break;
5829 		case DIF_OP_ADD:
5830 			regs[rd] = regs[r1] + regs[r2];
5831 			break;
5832 		case DIF_OP_MUL:
5833 			regs[rd] = regs[r1] * regs[r2];
5834 			break;
5835 		case DIF_OP_SDIV:
5836 			if (regs[r2] == 0) {
5837 				regs[rd] = 0;
5838 				*flags |= CPU_DTRACE_DIVZERO;
5839 			} else {
5840 				regs[rd] = (int64_t)regs[r1] /
5841 				    (int64_t)regs[r2];
5842 			}
5843 			break;
5844 
5845 		case DIF_OP_UDIV:
5846 			if (regs[r2] == 0) {
5847 				regs[rd] = 0;
5848 				*flags |= CPU_DTRACE_DIVZERO;
5849 			} else {
5850 				regs[rd] = regs[r1] / regs[r2];
5851 			}
5852 			break;
5853 
5854 		case DIF_OP_SREM:
5855 			if (regs[r2] == 0) {
5856 				regs[rd] = 0;
5857 				*flags |= CPU_DTRACE_DIVZERO;
5858 			} else {
5859 				regs[rd] = (int64_t)regs[r1] %
5860 				    (int64_t)regs[r2];
5861 			}
5862 			break;
5863 
5864 		case DIF_OP_UREM:
5865 			if (regs[r2] == 0) {
5866 				regs[rd] = 0;
5867 				*flags |= CPU_DTRACE_DIVZERO;
5868 			} else {
5869 				regs[rd] = regs[r1] % regs[r2];
5870 			}
5871 			break;
5872 
5873 		case DIF_OP_NOT:
5874 			regs[rd] = ~regs[r1];
5875 			break;
5876 		case DIF_OP_MOV:
5877 			regs[rd] = regs[r1];
5878 			break;
5879 		case DIF_OP_CMP:
5880 			cc_r = regs[r1] - regs[r2];
5881 			cc_n = cc_r < 0;
5882 			cc_z = cc_r == 0;
5883 			cc_v = 0;
5884 			cc_c = regs[r1] < regs[r2];
5885 			break;
5886 		case DIF_OP_TST:
5887 			cc_n = cc_v = cc_c = 0;
5888 			cc_z = regs[r1] == 0;
5889 			break;
5890 		case DIF_OP_BA:
5891 			pc = DIF_INSTR_LABEL(instr);
5892 			break;
5893 		case DIF_OP_BE:
5894 			if (cc_z)
5895 				pc = DIF_INSTR_LABEL(instr);
5896 			break;
5897 		case DIF_OP_BNE:
5898 			if (cc_z == 0)
5899 				pc = DIF_INSTR_LABEL(instr);
5900 			break;
5901 		case DIF_OP_BG:
5902 			if ((cc_z | (cc_n ^ cc_v)) == 0)
5903 				pc = DIF_INSTR_LABEL(instr);
5904 			break;
5905 		case DIF_OP_BGU:
5906 			if ((cc_c | cc_z) == 0)
5907 				pc = DIF_INSTR_LABEL(instr);
5908 			break;
5909 		case DIF_OP_BGE:
5910 			if ((cc_n ^ cc_v) == 0)
5911 				pc = DIF_INSTR_LABEL(instr);
5912 			break;
5913 		case DIF_OP_BGEU:
5914 			if (cc_c == 0)
5915 				pc = DIF_INSTR_LABEL(instr);
5916 			break;
5917 		case DIF_OP_BL:
5918 			if (cc_n ^ cc_v)
5919 				pc = DIF_INSTR_LABEL(instr);
5920 			break;
5921 		case DIF_OP_BLU:
5922 			if (cc_c)
5923 				pc = DIF_INSTR_LABEL(instr);
5924 			break;
5925 		case DIF_OP_BLE:
5926 			if (cc_z | (cc_n ^ cc_v))
5927 				pc = DIF_INSTR_LABEL(instr);
5928 			break;
5929 		case DIF_OP_BLEU:
5930 			if (cc_c | cc_z)
5931 				pc = DIF_INSTR_LABEL(instr);
5932 			break;
5933 		case DIF_OP_RLDSB:
5934 			if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) {
5935 				*flags |= CPU_DTRACE_KPRIV;
5936 				*illval = regs[r1];
5937 				break;
5938 			}
5939 			OS_FALLTHROUGH;
5940 		case DIF_OP_LDSB:
5941 			regs[rd] = (int8_t)dtrace_load8(regs[r1]);
5942 			break;
5943 		case DIF_OP_RLDSH:
5944 			if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) {
5945 				*flags |= CPU_DTRACE_KPRIV;
5946 				*illval = regs[r1];
5947 				break;
5948 			}
5949 			OS_FALLTHROUGH;
5950 		case DIF_OP_LDSH:
5951 			regs[rd] = (int16_t)dtrace_load16(regs[r1]);
5952 			break;
5953 		case DIF_OP_RLDSW:
5954 			if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) {
5955 				*flags |= CPU_DTRACE_KPRIV;
5956 				*illval = regs[r1];
5957 				break;
5958 			}
5959 			OS_FALLTHROUGH;
5960 		case DIF_OP_LDSW:
5961 			regs[rd] = (int32_t)dtrace_load32(regs[r1]);
5962 			break;
5963 		case DIF_OP_RLDUB:
5964 			if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) {
5965 				*flags |= CPU_DTRACE_KPRIV;
5966 				*illval = regs[r1];
5967 				break;
5968 			}
5969 			OS_FALLTHROUGH;
5970 		case DIF_OP_LDUB:
5971 			regs[rd] = dtrace_load8(regs[r1]);
5972 			break;
5973 		case DIF_OP_RLDUH:
5974 			if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) {
5975 				*flags |= CPU_DTRACE_KPRIV;
5976 				*illval = regs[r1];
5977 				break;
5978 			}
5979 			OS_FALLTHROUGH;
5980 		case DIF_OP_LDUH:
5981 			regs[rd] = dtrace_load16(regs[r1]);
5982 			break;
5983 		case DIF_OP_RLDUW:
5984 			if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) {
5985 				*flags |= CPU_DTRACE_KPRIV;
5986 				*illval = regs[r1];
5987 				break;
5988 			}
5989 			OS_FALLTHROUGH;
5990 		case DIF_OP_LDUW:
5991 			regs[rd] = dtrace_load32(regs[r1]);
5992 			break;
5993 		case DIF_OP_RLDX:
5994 			if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) {
5995 				*flags |= CPU_DTRACE_KPRIV;
5996 				*illval = regs[r1];
5997 				break;
5998 			}
5999 			OS_FALLTHROUGH;
6000 		case DIF_OP_LDX:
6001 			regs[rd] = dtrace_load64(regs[r1]);
6002 			break;
6003 /*
6004  * Darwin 32-bit kernel may fetch from 64-bit user.
6005  * Do not cast regs to uintptr_t
6006  * DIF_OP_ULDSB,DIF_OP_ULDSH, DIF_OP_ULDSW, DIF_OP_ULDUB
6007  * DIF_OP_ULDUH, DIF_OP_ULDUW, DIF_OP_ULDX
6008  */
6009 		case DIF_OP_ULDSB:
6010 			regs[rd] = (int8_t)
6011 			    dtrace_fuword8(regs[r1]);
6012 			break;
6013 		case DIF_OP_ULDSH:
6014 			regs[rd] = (int16_t)
6015 			    dtrace_fuword16(regs[r1]);
6016 			break;
6017 		case DIF_OP_ULDSW:
6018 			regs[rd] = (int32_t)
6019 			    dtrace_fuword32(regs[r1]);
6020 			break;
6021 		case DIF_OP_ULDUB:
6022 			regs[rd] =
6023 			    dtrace_fuword8(regs[r1]);
6024 			break;
6025 		case DIF_OP_ULDUH:
6026 			regs[rd] =
6027 			    dtrace_fuword16(regs[r1]);
6028 			break;
6029 		case DIF_OP_ULDUW:
6030 			regs[rd] =
6031 			    dtrace_fuword32(regs[r1]);
6032 			break;
6033 		case DIF_OP_ULDX:
6034 			regs[rd] =
6035 			    dtrace_fuword64(regs[r1]);
6036 			break;
6037 		case DIF_OP_RET:
6038 			rval = regs[rd];
6039 			pc = textlen;
6040 			break;
6041 		case DIF_OP_NOP:
6042 			break;
6043 		case DIF_OP_SETX:
6044 			regs[rd] = inttab[DIF_INSTR_INTEGER(instr)];
6045 			break;
6046 		case DIF_OP_SETS:
6047 			regs[rd] = (uint64_t)(uintptr_t)
6048 			    (strtab + DIF_INSTR_STRING(instr));
6049 			break;
6050 		case DIF_OP_SCMP: {
6051 			size_t sz = state->dts_options[DTRACEOPT_STRSIZE];
6052 			uintptr_t s1 = regs[r1];
6053 			uintptr_t s2 = regs[r2];
6054 			size_t lim1 = sz, lim2 = sz;
6055 
6056 			if (s1 != 0 &&
6057 			    !dtrace_strcanload(s1, sz, &lim1, mstate, vstate))
6058 				break;
6059 			if (s2 != 0 &&
6060 			    !dtrace_strcanload(s2, sz, &lim2, mstate, vstate))
6061 				break;
6062 
6063 			cc_r = dtrace_strncmp((char *)s1, (char *)s2,
6064 				MIN(lim1, lim2));
6065 
6066 			cc_n = cc_r < 0;
6067 			cc_z = cc_r == 0;
6068 			cc_v = cc_c = 0;
6069 			break;
6070 		}
6071 		case DIF_OP_LDGA:
6072 			regs[rd] = dtrace_dif_variable(mstate, state,
6073 			    r1, regs[r2]);
6074 			break;
6075 		case DIF_OP_LDGS:
6076 			id = DIF_INSTR_VAR(instr);
6077 
6078 			if (id >= DIF_VAR_OTHER_UBASE) {
6079 				uintptr_t a;
6080 
6081 				id -= DIF_VAR_OTHER_UBASE;
6082 				svar = vstate->dtvs_globals[id];
6083 				ASSERT(svar != NULL);
6084 				v = &svar->dtsv_var;
6085 
6086 				if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) {
6087 					regs[rd] = svar->dtsv_data;
6088 					break;
6089 				}
6090 
6091 				a = (uintptr_t)svar->dtsv_data;
6092 
6093 				if (*(uint8_t *)a == UINT8_MAX) {
6094 					/*
6095 					 * If the 0th byte is set to UINT8_MAX
6096 					 * then this is to be treated as a
6097 					 * reference to a NULL variable.
6098 					 */
6099 					regs[rd] = 0;
6100 				} else {
6101 					regs[rd] = a + sizeof (uint64_t);
6102 				}
6103 
6104 				break;
6105 			}
6106 
6107 			regs[rd] = dtrace_dif_variable(mstate, state, id, 0);
6108 			break;
6109 
6110 		case DIF_OP_STGS:
6111 			id = DIF_INSTR_VAR(instr);
6112 
6113 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6114 			id -= DIF_VAR_OTHER_UBASE;
6115 
6116 			VERIFY(id < (uint_t)vstate->dtvs_nglobals);
6117 			svar = vstate->dtvs_globals[id];
6118 			ASSERT(svar != NULL);
6119 			v = &svar->dtsv_var;
6120 
6121 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6122 				uintptr_t a = (uintptr_t)svar->dtsv_data;
6123 				size_t lim = 0;
6124 
6125 				ASSERT(a != 0);
6126 				ASSERT(svar->dtsv_size != 0);
6127 
6128 				if (regs[rd] == 0) {
6129 					*(uint8_t *)a = UINT8_MAX;
6130 					break;
6131 				} else {
6132 					*(uint8_t *)a = 0;
6133 					a += sizeof (uint64_t);
6134 				}
6135 				if (!dtrace_vcanload(
6136 				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
6137 					&lim, mstate, vstate))
6138 					break;
6139 
6140 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
6141 				    (void *)a, &v->dtdv_type, lim);
6142 				break;
6143 			}
6144 
6145 			svar->dtsv_data = regs[rd];
6146 			break;
6147 
6148 		case DIF_OP_LDTA:
6149 			/*
6150 			 * There are no DTrace built-in thread-local arrays at
6151 			 * present.  This opcode is saved for future work.
6152 			 */
6153 			*flags |= CPU_DTRACE_ILLOP;
6154 			regs[rd] = 0;
6155 			break;
6156 
6157 		case DIF_OP_LDLS:
6158 			id = DIF_INSTR_VAR(instr);
6159 
6160 			if (id < DIF_VAR_OTHER_UBASE) {
6161 				/*
6162 				 * For now, this has no meaning.
6163 				 */
6164 				regs[rd] = 0;
6165 				break;
6166 			}
6167 
6168 			id -= DIF_VAR_OTHER_UBASE;
6169 
6170 			ASSERT(id < (uint_t)vstate->dtvs_nlocals);
6171 			ASSERT(vstate->dtvs_locals != NULL);
6172 			svar = vstate->dtvs_locals[id];
6173 			ASSERT(svar != NULL);
6174 			v = &svar->dtsv_var;
6175 
6176 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6177 				uintptr_t a = (uintptr_t)svar->dtsv_data;
6178 				size_t sz = v->dtdv_type.dtdt_size;
6179 
6180 				sz += sizeof (uint64_t);
6181 				ASSERT(svar->dtsv_size == (int)NCPU * sz);
6182 				a += CPU->cpu_id * sz;
6183 
6184 				if (*(uint8_t *)a == UINT8_MAX) {
6185 					/*
6186 					 * If the 0th byte is set to UINT8_MAX
6187 					 * then this is to be treated as a
6188 					 * reference to a NULL variable.
6189 					 */
6190 					regs[rd] = 0;
6191 				} else {
6192 					regs[rd] = a + sizeof (uint64_t);
6193 				}
6194 
6195 				break;
6196 			}
6197 
6198 			ASSERT(svar->dtsv_size == (int)NCPU * sizeof (uint64_t));
6199 			tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
6200 			regs[rd] = tmp[CPU->cpu_id];
6201 			break;
6202 
6203 		case DIF_OP_STLS:
6204 			id = DIF_INSTR_VAR(instr);
6205 
6206 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6207 			id -= DIF_VAR_OTHER_UBASE;
6208 			VERIFY(id < (uint_t)vstate->dtvs_nlocals);
6209 			ASSERT(vstate->dtvs_locals != NULL);
6210 			svar = vstate->dtvs_locals[id];
6211 			ASSERT(svar != NULL);
6212 			v = &svar->dtsv_var;
6213 
6214 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6215 				uintptr_t a = (uintptr_t)svar->dtsv_data;
6216 				size_t sz = v->dtdv_type.dtdt_size;
6217 				size_t lim = 0;
6218 
6219 				sz += sizeof (uint64_t);
6220 				ASSERT(svar->dtsv_size == (int)NCPU * sz);
6221 				a += CPU->cpu_id * sz;
6222 
6223 				if (regs[rd] == 0) {
6224 					*(uint8_t *)a = UINT8_MAX;
6225 					break;
6226 				} else {
6227 					*(uint8_t *)a = 0;
6228 					a += sizeof (uint64_t);
6229 				}
6230 
6231 				if (!dtrace_vcanload(
6232 				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
6233 				    &lim, mstate, vstate))
6234 					break;
6235 
6236 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
6237 				    (void *)a, &v->dtdv_type, lim);
6238 				break;
6239 			}
6240 
6241 			ASSERT(svar->dtsv_size == (int)NCPU * sizeof (uint64_t));
6242 			tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
6243 			tmp[CPU->cpu_id] = regs[rd];
6244 			break;
6245 
6246 		case DIF_OP_LDTS: {
6247 			dtrace_dynvar_t *dvar;
6248 			dtrace_key_t *key;
6249 
6250 			id = DIF_INSTR_VAR(instr);
6251 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6252 			id -= DIF_VAR_OTHER_UBASE;
6253 			v = &vstate->dtvs_tlocals[id];
6254 
6255 			key = &tupregs[DIF_DTR_NREGS];
6256 			key[0].dttk_value = (uint64_t)id;
6257 			key[0].dttk_size = 0;
6258 			DTRACE_TLS_THRKEY(key[1].dttk_value);
6259 			key[1].dttk_size = 0;
6260 
6261 			dvar = dtrace_dynvar(dstate, 2, key,
6262 			    sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC,
6263 			    mstate, vstate);
6264 
6265 			if (dvar == NULL) {
6266 				regs[rd] = 0;
6267 				break;
6268 			}
6269 
6270 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6271 				regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
6272 			} else {
6273 				regs[rd] = *((uint64_t *)dvar->dtdv_data);
6274 			}
6275 
6276 			break;
6277 		}
6278 
6279 		case DIF_OP_STTS: {
6280 			dtrace_dynvar_t *dvar;
6281 			dtrace_key_t *key;
6282 
6283 			id = DIF_INSTR_VAR(instr);
6284 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6285 			id -= DIF_VAR_OTHER_UBASE;
6286 			VERIFY(id < (uint_t)vstate->dtvs_ntlocals);
6287 
6288 			key = &tupregs[DIF_DTR_NREGS];
6289 			key[0].dttk_value = (uint64_t)id;
6290 			key[0].dttk_size = 0;
6291 			DTRACE_TLS_THRKEY(key[1].dttk_value);
6292 			key[1].dttk_size = 0;
6293 			v = &vstate->dtvs_tlocals[id];
6294 
6295 			dvar = dtrace_dynvar(dstate, 2, key,
6296 			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6297 			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
6298 			    regs[rd] ? DTRACE_DYNVAR_ALLOC :
6299 			    DTRACE_DYNVAR_DEALLOC, mstate, vstate);
6300 
6301 			/*
6302 			 * Given that we're storing to thread-local data,
6303 			 * we need to flush our predicate cache.
6304 			 */
6305 			dtrace_set_thread_predcache(current_thread(), 0);
6306 
6307 			if (dvar == NULL)
6308 				break;
6309 
6310 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6311 				size_t lim = 0;
6312 
6313 				if (!dtrace_vcanload(
6314 				    (void *)(uintptr_t)regs[rd],
6315 				    &v->dtdv_type, &lim, mstate, vstate))
6316 					break;
6317 
6318 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
6319 				    dvar->dtdv_data, &v->dtdv_type, lim);
6320 			} else {
6321 				*((uint64_t *)dvar->dtdv_data) = regs[rd];
6322 			}
6323 
6324 			break;
6325 		}
6326 
6327 		case DIF_OP_SRA:
6328 			regs[rd] = (int64_t)regs[r1] >> regs[r2];
6329 			break;
6330 
6331 		case DIF_OP_CALL:
6332 			dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd,
6333 			    regs, tupregs, ttop, mstate, state);
6334 			break;
6335 
6336 		case DIF_OP_PUSHTR:
6337 			if (ttop == DIF_DTR_NREGS) {
6338 				*flags |= CPU_DTRACE_TUPOFLOW;
6339 				break;
6340 			}
6341 
6342 			if (r1 == DIF_TYPE_STRING) {
6343 				/*
6344 				 * If this is a string type and the size is 0,
6345 				 * we'll use the system-wide default string
6346 				 * size.  Note that we are _not_ looking at
6347 				 * the value of the DTRACEOPT_STRSIZE option;
6348 				 * had this been set, we would expect to have
6349 				 * a non-zero size value in the "pushtr".
6350 				 */
6351 				tupregs[ttop].dttk_size =
6352 				    dtrace_strlen((char *)(uintptr_t)regs[rd],
6353 				    regs[r2] ? regs[r2] :
6354 				    dtrace_strsize_default) + 1;
6355 			} else {
6356 				if (regs[r2] > LONG_MAX) {
6357 					*flags |= CPU_DTRACE_ILLOP;
6358 					break;
6359 				}
6360 				tupregs[ttop].dttk_size = regs[r2];
6361 			}
6362 
6363 			tupregs[ttop++].dttk_value = regs[rd];
6364 			break;
6365 
6366 		case DIF_OP_PUSHTV:
6367 			if (ttop == DIF_DTR_NREGS) {
6368 				*flags |= CPU_DTRACE_TUPOFLOW;
6369 				break;
6370 			}
6371 
6372 			tupregs[ttop].dttk_value = regs[rd];
6373 			tupregs[ttop++].dttk_size = 0;
6374 			break;
6375 
6376 		case DIF_OP_POPTS:
6377 			if (ttop != 0)
6378 				ttop--;
6379 			break;
6380 
6381 		case DIF_OP_FLUSHTS:
6382 			ttop = 0;
6383 			break;
6384 
6385 		case DIF_OP_LDGAA:
6386 		case DIF_OP_LDTAA: {
6387 			dtrace_dynvar_t *dvar;
6388 			dtrace_key_t *key = tupregs;
6389 			uint_t nkeys = ttop;
6390 
6391 			id = DIF_INSTR_VAR(instr);
6392 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6393 			id -= DIF_VAR_OTHER_UBASE;
6394 
6395 			key[nkeys].dttk_value = (uint64_t)id;
6396 			key[nkeys++].dttk_size = 0;
6397 
6398 			if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) {
6399 				DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
6400 				key[nkeys++].dttk_size = 0;
6401 				VERIFY(id < (uint_t)vstate->dtvs_ntlocals);
6402 				v = &vstate->dtvs_tlocals[id];
6403 			} else {
6404 				VERIFY(id < (uint_t)vstate->dtvs_nglobals);
6405 				v = &vstate->dtvs_globals[id]->dtsv_var;
6406 			}
6407 
6408 			dvar = dtrace_dynvar(dstate, nkeys, key,
6409 			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6410 			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
6411 			    DTRACE_DYNVAR_NOALLOC, mstate, vstate);
6412 
6413 			if (dvar == NULL) {
6414 				regs[rd] = 0;
6415 				break;
6416 			}
6417 
6418 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6419 				regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
6420 			} else {
6421 				regs[rd] = *((uint64_t *)dvar->dtdv_data);
6422 			}
6423 
6424 			break;
6425 		}
6426 
6427 		case DIF_OP_STGAA:
6428 		case DIF_OP_STTAA: {
6429 			dtrace_dynvar_t *dvar;
6430 			dtrace_key_t *key = tupregs;
6431 			uint_t nkeys = ttop;
6432 
6433 			id = DIF_INSTR_VAR(instr);
6434 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6435 			id -= DIF_VAR_OTHER_UBASE;
6436 
6437 			key[nkeys].dttk_value = (uint64_t)id;
6438 			key[nkeys++].dttk_size = 0;
6439 
6440 			if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) {
6441 				DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
6442 				key[nkeys++].dttk_size = 0;
6443 				VERIFY(id < (uint_t)vstate->dtvs_ntlocals);
6444 				v = &vstate->dtvs_tlocals[id];
6445 			} else {
6446 				VERIFY(id < (uint_t)vstate->dtvs_nglobals);
6447 				v = &vstate->dtvs_globals[id]->dtsv_var;
6448 			}
6449 
6450 			dvar = dtrace_dynvar(dstate, nkeys, key,
6451 			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6452 			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
6453 			    regs[rd] ? DTRACE_DYNVAR_ALLOC :
6454 			    DTRACE_DYNVAR_DEALLOC, mstate, vstate);
6455 
6456 			if (dvar == NULL)
6457 				break;
6458 
6459 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6460 				size_t lim = 0;
6461 
6462 				if (!dtrace_vcanload(
6463 				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
6464 				    &lim, mstate, vstate))
6465 					break;
6466 
6467 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
6468 				    dvar->dtdv_data, &v->dtdv_type, lim);
6469 			} else {
6470 				*((uint64_t *)dvar->dtdv_data) = regs[rd];
6471 			}
6472 
6473 			break;
6474 		}
6475 
6476 		case DIF_OP_ALLOCS: {
6477 			uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
6478 			size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1];
6479 
6480 			/*
6481 			 * Rounding up the user allocation size could have
6482 			 * overflowed large, bogus allocations (like -1ULL) to
6483 			 * 0.
6484 			 */
6485 			if (size < regs[r1] ||
6486 			    !DTRACE_INSCRATCH(mstate, size)) {
6487 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
6488 				regs[rd] = 0;
6489 				break;
6490 			}
6491 
6492 			dtrace_bzero((void *) mstate->dtms_scratch_ptr, size);
6493 				mstate->dtms_scratch_ptr += size;
6494 				regs[rd] = ptr;
6495 			break;
6496 		}
6497 
6498 		case DIF_OP_COPYS:
6499 			if (!dtrace_canstore(regs[rd], regs[r2],
6500 			    mstate, vstate)) {
6501 				*flags |= CPU_DTRACE_BADADDR;
6502 				*illval = regs[rd];
6503 				break;
6504 			}
6505 
6506 			if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate))
6507 				break;
6508 
6509 			dtrace_bcopy((void *)(uintptr_t)regs[r1],
6510 			    (void *)(uintptr_t)regs[rd], (size_t)regs[r2]);
6511 			break;
6512 
6513 		case DIF_OP_STB:
6514 			if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) {
6515 				*flags |= CPU_DTRACE_BADADDR;
6516 				*illval = regs[rd];
6517 				break;
6518 			}
6519 			*((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1];
6520 			break;
6521 
6522 		case DIF_OP_STH:
6523 			if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) {
6524 				*flags |= CPU_DTRACE_BADADDR;
6525 				*illval = regs[rd];
6526 				break;
6527 			}
6528 			if (regs[rd] & 1) {
6529 				*flags |= CPU_DTRACE_BADALIGN;
6530 				*illval = regs[rd];
6531 				break;
6532 			}
6533 			*((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1];
6534 			break;
6535 
6536 		case DIF_OP_STW:
6537 			if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) {
6538 				*flags |= CPU_DTRACE_BADADDR;
6539 				*illval = regs[rd];
6540 				break;
6541 			}
6542 			if (regs[rd] & 3) {
6543 				*flags |= CPU_DTRACE_BADALIGN;
6544 				*illval = regs[rd];
6545 				break;
6546 			}
6547 			*((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1];
6548 			break;
6549 
6550 		case DIF_OP_STX:
6551 			if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) {
6552 				*flags |= CPU_DTRACE_BADADDR;
6553 				*illval = regs[rd];
6554 				break;
6555 			}
6556 
6557 			/*
6558 			* Darwin kmem_zalloc() called from
6559 			* dtrace_difo_init() is 4-byte aligned.
6560 			*/
6561 			if (regs[rd] & 3) {
6562 				*flags |= CPU_DTRACE_BADALIGN;
6563 				*illval = regs[rd];
6564 				break;
6565 			}
6566 			*((uint64_t *)(uintptr_t)regs[rd]) = regs[r1];
6567 			break;
6568 		case DIF_OP_STRIP:
6569 			regs[rd] = (uint64_t)dtrace_ptrauth_strip(
6570 			    (void*)regs[r1], r2);
6571 			break;
6572 		}
6573 	}
6574 
6575 	if (!(*flags & CPU_DTRACE_FAULT))
6576 		return (rval);
6577 
6578 	mstate->dtms_fltoffs = opc * sizeof (dif_instr_t);
6579 	mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS;
6580 
6581 	return (0);
6582 }
6583 
6584 __attribute__((noinline))
6585 static void
dtrace_action_breakpoint(dtrace_ecb_t * ecb)6586 dtrace_action_breakpoint(dtrace_ecb_t *ecb)
6587 {
6588 	dtrace_probe_t *probe = ecb->dte_probe;
6589 	dtrace_provider_t *prov = probe->dtpr_provider;
6590 	char c[DTRACE_FULLNAMELEN + 80], *str;
6591 	const char *msg = "dtrace: breakpoint action at probe ";
6592 	const char *ecbmsg = " (ecb ";
6593 	uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4));
6594 	uintptr_t val = (uintptr_t)ecb;
6595 	int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0;
6596 
6597 	if (dtrace_destructive_disallow)
6598 		return;
6599 
6600 	/*
6601 	 * It's impossible to be taking action on the NULL probe.
6602 	 */
6603 	ASSERT(probe != NULL);
6604 
6605 	/*
6606 	 * This is a poor man's (destitute man's?) sprintf():  we want to
6607 	 * print the provider name, module name, function name and name of
6608 	 * the probe, along with the hex address of the ECB with the breakpoint
6609 	 * action -- all of which we must place in the character buffer by
6610 	 * hand.
6611 	 */
6612 	while (*msg != '\0')
6613 		c[i++] = *msg++;
6614 
6615 	for (str = prov->dtpv_name; *str != '\0'; str++)
6616 		c[i++] = *str;
6617 	c[i++] = ':';
6618 
6619 	for (str = probe->dtpr_mod; *str != '\0'; str++)
6620 		c[i++] = *str;
6621 	c[i++] = ':';
6622 
6623 	for (str = probe->dtpr_func; *str != '\0'; str++)
6624 		c[i++] = *str;
6625 	c[i++] = ':';
6626 
6627 	for (str = probe->dtpr_name; *str != '\0'; str++)
6628 		c[i++] = *str;
6629 
6630 	while (*ecbmsg != '\0')
6631 		c[i++] = *ecbmsg++;
6632 
6633 	while (shift >= 0) {
6634 		mask = (uintptr_t)0xf << shift;
6635 
6636 		if (val >= ((uintptr_t)1 << shift))
6637 			c[i++] = "0123456789abcdef"[(val & mask) >> shift];
6638 		shift -= 4;
6639 	}
6640 
6641 	c[i++] = ')';
6642 	c[i] = '\0';
6643 
6644 	debug_enter(c);
6645 }
6646 
6647 __attribute__((noinline))
6648 static void
dtrace_action_panic(dtrace_ecb_t * ecb)6649 dtrace_action_panic(dtrace_ecb_t *ecb)
6650 {
6651 	dtrace_probe_t *probe = ecb->dte_probe;
6652 
6653 	/*
6654 	 * It's impossible to be taking action on the NULL probe.
6655 	 */
6656 	ASSERT(probe != NULL);
6657 
6658 	if (dtrace_destructive_disallow)
6659 		return;
6660 
6661 	if (dtrace_panicked != NULL)
6662 		return;
6663 
6664 	if (dtrace_casptr(&dtrace_panicked, NULL, current_thread()) != NULL)
6665 		return;
6666 
6667 	/*
6668 	 * We won the right to panic.  (We want to be sure that only one
6669 	 * thread calls panic() from dtrace_probe(), and that panic() is
6670 	 * called exactly once.)
6671 	 */
6672 	panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)",
6673 	    probe->dtpr_provider->dtpv_name, probe->dtpr_mod,
6674 	    probe->dtpr_func, probe->dtpr_name, (void *)ecb);
6675 
6676 	/*
6677 	 * APPLE NOTE: this was for an old Mac OS X debug feature
6678 	 * allowing a return from panic().  Revisit someday.
6679 	 */
6680 	dtrace_panicked = NULL;
6681 }
6682 
6683 static void
dtrace_action_raise(uint64_t sig)6684 dtrace_action_raise(uint64_t sig)
6685 {
6686 	if (dtrace_destructive_disallow)
6687 		return;
6688 
6689 	if (sig >= NSIG) {
6690 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6691 		return;
6692 	}
6693 
6694 	/*
6695 	 * raise() has a queue depth of 1 -- we ignore all subsequent
6696 	 * invocations of the raise() action.
6697 	 */
6698 
6699 	uthread_t uthread = current_uthread();
6700 
6701 	if (uthread && uthread->t_dtrace_sig == 0) {
6702 		uthread->t_dtrace_sig = sig;
6703 		act_set_astbsd(current_thread());
6704 	}
6705 }
6706 
6707 static void
dtrace_action_stop(void)6708 dtrace_action_stop(void)
6709 {
6710 	if (dtrace_destructive_disallow)
6711 		return;
6712 
6713         uthread_t uthread = current_uthread();
6714 	if (uthread) {
6715 		/*
6716 		 * The currently running process will be set to task_suspend
6717 		 * when it next leaves the kernel.
6718 		*/
6719 		uthread->t_dtrace_stop = 1;
6720 		act_set_astbsd(current_thread());
6721 	}
6722 }
6723 
6724 
6725 /*
6726  * APPLE NOTE: pidresume works in conjunction with the dtrace stop action.
6727  * Both activate only when the currently running process next leaves the
6728  * kernel.
6729  */
6730 static void
dtrace_action_pidresume(uint64_t pid)6731 dtrace_action_pidresume(uint64_t pid)
6732 {
6733 	if (dtrace_destructive_disallow)
6734 		return;
6735 
6736 	if (kauth_cred_issuser(kauth_cred_get()) == 0) {
6737 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6738 		return;
6739 	}
6740         uthread_t uthread = current_uthread();
6741 
6742 	/*
6743 	 * When the currently running process leaves the kernel, it attempts to
6744 	 * task_resume the process (denoted by pid), if that pid appears to have
6745 	 * been stopped by dtrace_action_stop().
6746 	 * The currently running process has a pidresume() queue depth of 1 --
6747 	 * subsequent invocations of the pidresume() action are ignored.
6748 	 */
6749 
6750 	if (pid != 0 && uthread && uthread->t_dtrace_resumepid == 0) {
6751 		uthread->t_dtrace_resumepid = pid;
6752 		act_set_astbsd(current_thread());
6753 	}
6754 }
6755 
6756 __attribute__((noinline))
6757 static void
dtrace_action_chill(dtrace_mstate_t * mstate,hrtime_t val)6758 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val)
6759 {
6760 	hrtime_t now;
6761 	volatile uint16_t *flags;
6762 	dtrace_cpu_t *cpu = CPU;
6763 
6764 	if (dtrace_destructive_disallow)
6765 		return;
6766 
6767 	flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags;
6768 
6769 	now = dtrace_gethrtime();
6770 
6771 	if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) {
6772 		/*
6773 		 * We need to advance the mark to the current time.
6774 		 */
6775 		cpu->cpu_dtrace_chillmark = now;
6776 		cpu->cpu_dtrace_chilled = 0;
6777 	}
6778 
6779 	/*
6780 	 * Now check to see if the requested chill time would take us over
6781 	 * the maximum amount of time allowed in the chill interval.  (Or
6782 	 * worse, if the calculation itself induces overflow.)
6783 	 */
6784 	if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max ||
6785 	    cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) {
6786 		*flags |= CPU_DTRACE_ILLOP;
6787 		return;
6788 	}
6789 
6790 	while (dtrace_gethrtime() - now < val)
6791 		continue;
6792 
6793 	/*
6794 	 * Normally, we assure that the value of the variable "timestamp" does
6795 	 * not change within an ECB.  The presence of chill() represents an
6796 	 * exception to this rule, however.
6797 	 */
6798 	mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP;
6799 	cpu->cpu_dtrace_chilled += val;
6800 }
6801 
6802 __attribute__((noinline))
6803 static void
dtrace_action_ustack(dtrace_mstate_t * mstate,dtrace_state_t * state,uint64_t * buf,uint64_t arg)6804 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state,
6805     uint64_t *buf, uint64_t arg)
6806 {
6807 	int nframes = DTRACE_USTACK_NFRAMES(arg);
6808 	int strsize = DTRACE_USTACK_STRSIZE(arg);
6809 	uint64_t *pcs = &buf[1], *fps;
6810 	char *str = (char *)&pcs[nframes];
6811 	int size, offs = 0, i, j;
6812 	uintptr_t old = mstate->dtms_scratch_ptr, saved;
6813 	uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
6814 	char *sym;
6815 
6816 	/*
6817 	 * Should be taking a faster path if string space has not been
6818 	 * allocated.
6819 	 */
6820 	ASSERT(strsize != 0);
6821 
6822 	/*
6823 	 * We will first allocate some temporary space for the frame pointers.
6824 	 */
6825 	fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
6826 	size = (uintptr_t)fps - mstate->dtms_scratch_ptr +
6827 	    (nframes * sizeof (uint64_t));
6828 
6829 	if (!DTRACE_INSCRATCH(mstate, (uintptr_t)size)) {
6830 		/*
6831 		 * Not enough room for our frame pointers -- need to indicate
6832 		 * that we ran out of scratch space.
6833 		 */
6834 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
6835 		return;
6836 	}
6837 
6838 	mstate->dtms_scratch_ptr += size;
6839 	saved = mstate->dtms_scratch_ptr;
6840 
6841 	/*
6842 	 * Now get a stack with both program counters and frame pointers.
6843 	 */
6844 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6845 	dtrace_getufpstack(buf, fps, nframes + 1);
6846 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6847 
6848 	/*
6849 	 * If that faulted, we're cooked.
6850 	 */
6851 	if (*flags & CPU_DTRACE_FAULT)
6852 		goto out;
6853 
6854 	/*
6855 	 * Now we want to walk up the stack, calling the USTACK helper.  For
6856 	 * each iteration, we restore the scratch pointer.
6857 	 */
6858 	for (i = 0; i < nframes; i++) {
6859 		mstate->dtms_scratch_ptr = saved;
6860 
6861 		if (offs >= strsize)
6862 			break;
6863 
6864 		sym = (char *)(uintptr_t)dtrace_helper(
6865 		    DTRACE_HELPER_ACTION_USTACK,
6866 		    mstate, state, pcs[i], fps[i]);
6867 
6868 		/*
6869 		 * If we faulted while running the helper, we're going to
6870 		 * clear the fault and null out the corresponding string.
6871 		 */
6872 		if (*flags & CPU_DTRACE_FAULT) {
6873 			*flags &= ~CPU_DTRACE_FAULT;
6874 			str[offs++] = '\0';
6875 			continue;
6876 		}
6877 
6878 		if (sym == NULL) {
6879 			str[offs++] = '\0';
6880 			continue;
6881 		}
6882 
6883 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6884 
6885 		/*
6886 		 * Now copy in the string that the helper returned to us.
6887 		 */
6888 		for (j = 0; offs + j < strsize; j++) {
6889 			if ((str[offs + j] = sym[j]) == '\0')
6890 				break;
6891 		}
6892 
6893 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6894 
6895 		offs += j + 1;
6896 	}
6897 
6898 	if (offs >= strsize) {
6899 		/*
6900 		 * If we didn't have room for all of the strings, we don't
6901 		 * abort processing -- this needn't be a fatal error -- but we
6902 		 * still want to increment a counter (dts_stkstroverflows) to
6903 		 * allow this condition to be warned about.  (If this is from
6904 		 * a jstack() action, it is easily tuned via jstackstrsize.)
6905 		 */
6906 		dtrace_error(&state->dts_stkstroverflows);
6907 	}
6908 
6909 	while (offs < strsize)
6910 		str[offs++] = '\0';
6911 
6912 out:
6913 	mstate->dtms_scratch_ptr = old;
6914 }
6915 
6916 __attribute__((noinline))
6917 static void
dtrace_store_by_ref(dtrace_difo_t * dp,caddr_t tomax,size_t size,size_t * valoffsp,uint64_t * valp,uint64_t end,int intuple,int dtkind)6918 dtrace_store_by_ref(dtrace_difo_t *dp, caddr_t tomax, size_t size,
6919     size_t *valoffsp, uint64_t *valp, uint64_t end, int intuple, int dtkind)
6920 {
6921 	volatile uint16_t *flags;
6922 	uint64_t val = *valp;
6923 	size_t valoffs = *valoffsp;
6924 
6925 	flags = (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
6926 	ASSERT(dtkind == DIF_TF_BYREF || dtkind == DIF_TF_BYUREF);
6927 
6928 	/*
6929 	 * If this is a string, we're going to only load until we find the zero
6930 	 * byte -- after which we'll store zero bytes.
6931 	 */
6932 	if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
6933 		char c = '\0' + 1;
6934 		size_t s;
6935 
6936 		for (s = 0; s < size; s++) {
6937 			if (c != '\0' && dtkind == DIF_TF_BYREF) {
6938 				c = dtrace_load8(val++);
6939 			} else if (c != '\0' && dtkind == DIF_TF_BYUREF) {
6940 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6941 				c = dtrace_fuword8((user_addr_t)(uintptr_t)val++);
6942 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6943 				if (*flags & CPU_DTRACE_FAULT)
6944 					break;
6945 			}
6946 
6947 			DTRACE_STORE(uint8_t, tomax, valoffs++, c);
6948 
6949 			if (c == '\0' && intuple)
6950 				break;
6951 		}
6952 	} else {
6953 		uint8_t c;
6954 		while (valoffs < end) {
6955 			if (dtkind == DIF_TF_BYREF) {
6956 				c = dtrace_load8(val++);
6957 			} else if (dtkind == DIF_TF_BYUREF) {
6958 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6959 				c = dtrace_fuword8((user_addr_t)(uintptr_t)val++);
6960 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6961 				if (*flags & CPU_DTRACE_FAULT)
6962 					break;
6963 			}
6964 
6965 			DTRACE_STORE(uint8_t, tomax,
6966 			    valoffs++, c);
6967 		}
6968 	}
6969 
6970 	*valp = val;
6971 	*valoffsp = valoffs;
6972 }
6973 
6974 /*
6975  * Disables interrupts and sets the per-thread inprobe flag. When DEBUG is
6976  * defined, we also assert that we are not recursing unless the probe ID is an
6977  * error probe.
6978  */
6979 static dtrace_icookie_t
dtrace_probe_enter(dtrace_id_t id)6980 dtrace_probe_enter(dtrace_id_t id)
6981 {
6982 	thread_t thread = current_thread();
6983 	uint16_t inprobe;
6984 
6985 	dtrace_icookie_t cookie;
6986 
6987 	cookie = dtrace_interrupt_disable();
6988 
6989 	/*
6990 	 * Unless this is an ERROR probe, we are not allowed to recurse in
6991 	 * dtrace_probe(). Recursing into DTrace probe usually means that a
6992 	 * function is instrumented that should not have been instrumented or
6993 	 * that the ordering guarantee of the records will be violated,
6994 	 * resulting in unexpected output. If there is an exception to this
6995 	 * assertion, a new case should be added.
6996 	 */
6997 	inprobe = dtrace_get_thread_inprobe(thread);
6998 	VERIFY(inprobe == 0 ||
6999 	    id == dtrace_probeid_error);
7000 	ASSERT(inprobe < UINT16_MAX);
7001 	dtrace_set_thread_inprobe(thread, inprobe + 1);
7002 
7003 	return (cookie);
7004 }
7005 
7006 /*
7007  * Clears the per-thread inprobe flag and enables interrupts.
7008  */
7009 static void
dtrace_probe_exit(dtrace_icookie_t cookie)7010 dtrace_probe_exit(dtrace_icookie_t cookie)
7011 {
7012 	thread_t thread = current_thread();
7013 	uint16_t inprobe = dtrace_get_thread_inprobe(thread);
7014 
7015 	ASSERT(inprobe > 0);
7016 	dtrace_set_thread_inprobe(thread, inprobe - 1);
7017 
7018 #if SCHED_HYGIENE_DEBUG
7019 	/*
7020 	 * Probes can take a relatively long time depending on what the user has
7021 	 * requested be done in probe context.
7022 	 * Probes can fire from places where interrupts are already disabled
7023 	 * (like an interrupt handler) or where preemption has been disabled.
7024 	 * In order to not trip the interrupt or preemption thresholds, it is
7025 	 * important to reset timestamps when leaving probe context.
7026 	 */
7027 
7028 	/* Interrupts were disabled for the duration of this probe. */
7029 	ml_spin_debug_reset(thread);
7030 
7031 	/* May have been called from an interrupt handler. */
7032 	ml_irq_debug_abandon();
7033 
7034 	/* May have been called with preemption disabled. */
7035 	abandon_preemption_disable_measurement();
7036 
7037 #endif /* SCHED_HYGIENE_DEBUG */
7038 
7039 	dtrace_interrupt_enable(cookie);
7040 }
7041 
7042 /*
7043  * If you're looking for the epicenter of DTrace, you just found it.  This
7044  * is the function called by the provider to fire a probe -- from which all
7045  * subsequent probe-context DTrace activity emanates.
7046  */
7047 void
dtrace_probe(dtrace_id_t id,uint64_t arg0,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4)7048 dtrace_probe(dtrace_id_t id, uint64_t arg0, uint64_t arg1,
7049     uint64_t arg2, uint64_t arg3, uint64_t arg4)
7050 {
7051 	processorid_t cpuid;
7052 	dtrace_icookie_t cookie;
7053 	dtrace_probe_t *probe;
7054 	dtrace_mstate_t mstate;
7055 	dtrace_ecb_t *ecb;
7056 	dtrace_action_t *act;
7057 	intptr_t offs;
7058 	size_t size;
7059 	int vtime, onintr;
7060 	volatile uint16_t *flags;
7061 	hrtime_t now;
7062 
7063 	cookie = dtrace_probe_enter(id);
7064 
7065 	/* Ensure that probe id is valid. */
7066 	if (id - 1 >= (dtrace_id_t)dtrace_nprobes) {
7067 		dtrace_probe_exit(cookie);
7068 		return;
7069 	}
7070 
7071 	probe = dtrace_probes[id - 1];
7072 	if (probe == NULL) {
7073 		dtrace_probe_exit(cookie);
7074 		return;
7075 	}
7076 
7077 	cpuid = CPU->cpu_id;
7078 	onintr = CPU_ON_INTR(CPU);
7079 
7080 	if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE &&
7081 	    probe->dtpr_predcache == dtrace_get_thread_predcache(current_thread())) {
7082 		/*
7083 		 * We have hit in the predicate cache; we know that
7084 		 * this predicate would evaluate to be false.
7085 		 */
7086 		dtrace_probe_exit(cookie);
7087 		return;
7088 	}
7089 
7090 	if (panic_quiesce) {
7091 		/*
7092 		 * We don't trace anything if we're panicking.
7093 		 */
7094 		dtrace_probe_exit(cookie);
7095 		return;
7096 	}
7097 
7098 #if !defined(__APPLE__)
7099 	now = dtrace_gethrtime();
7100 	vtime = dtrace_vtime_references != 0;
7101 
7102 	if (vtime && curthread->t_dtrace_start)
7103 		curthread->t_dtrace_vtime += now - curthread->t_dtrace_start;
7104 #else
7105 	/*
7106 	 * APPLE NOTE:  The time spent entering DTrace and arriving
7107 	 * to this point, is attributed to the current thread.
7108 	 * Instead it should accrue to DTrace.  FIXME
7109 	 */
7110 	vtime = dtrace_vtime_references != 0;
7111 
7112 	if (vtime)
7113 	{
7114 		int64_t dtrace_accum_time, recent_vtime;
7115 		thread_t thread = current_thread();
7116 
7117 		dtrace_accum_time = dtrace_get_thread_tracing(thread); /* Time spent inside DTrace so far (nanoseconds) */
7118 
7119 		if (dtrace_accum_time >= 0) {
7120 			recent_vtime = dtrace_abs_to_nano(dtrace_calc_thread_recent_vtime(thread)); /* up to the moment thread vtime */
7121 
7122 			recent_vtime = recent_vtime - dtrace_accum_time; /* Time without DTrace contribution */
7123 
7124 			dtrace_set_thread_vtime(thread, recent_vtime);
7125 		}
7126 	}
7127 
7128 	now = dtrace_gethrtime(); /* must not precede dtrace_calc_thread_recent_vtime() call! */
7129 #endif /* __APPLE__ */
7130 
7131 	/*
7132 	 * APPLE NOTE: A provider may call dtrace_probe_error() in lieu of
7133 	 * dtrace_probe() in some circumstances.   See, e.g. fasttrap_isa.c.
7134 	 * However the provider has no access to ECB context, so passes
7135 	 * 0 through "arg0" and the probe_id of the overridden probe as arg1.
7136 	 * Detect that here and cons up a viable state (from the probe_id).
7137 	 */
7138 	if (dtrace_probeid_error == id && 0 == arg0) {
7139 		dtrace_id_t ftp_id = (dtrace_id_t)arg1;
7140 		dtrace_probe_t *ftp_probe = dtrace_probes[ftp_id - 1];
7141 		dtrace_ecb_t *ftp_ecb = ftp_probe->dtpr_ecb;
7142 
7143 		if (NULL != ftp_ecb) {
7144 			dtrace_state_t *ftp_state = ftp_ecb->dte_state;
7145 
7146 			arg0 = (uint64_t)(uintptr_t)ftp_state;
7147 			arg1 = ftp_ecb->dte_epid;
7148 			/*
7149 			 * args[2-4] established by caller.
7150 			 */
7151 			ftp_state->dts_arg_error_illval = -1; /* arg5 */
7152 		}
7153 	}
7154 
7155 	mstate.dtms_difo = NULL;
7156 	mstate.dtms_probe = probe;
7157 	mstate.dtms_strtok = 0;
7158 	mstate.dtms_arg[0] = arg0;
7159 	mstate.dtms_arg[1] = arg1;
7160 	mstate.dtms_arg[2] = arg2;
7161 	mstate.dtms_arg[3] = arg3;
7162 	mstate.dtms_arg[4] = arg4;
7163 
7164 	flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags;
7165 
7166 	for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
7167 		dtrace_predicate_t *pred = ecb->dte_predicate;
7168 		dtrace_state_t *state = ecb->dte_state;
7169 		dtrace_buffer_t *buf = &state->dts_buffer[cpuid];
7170 		dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid];
7171 		dtrace_vstate_t *vstate = &state->dts_vstate;
7172 		dtrace_provider_t *prov = probe->dtpr_provider;
7173 		uint64_t tracememsize = 0;
7174 		int committed = 0;
7175 		caddr_t tomax;
7176 
7177 		/*
7178 		 * A little subtlety with the following (seemingly innocuous)
7179 		 * declaration of the automatic 'val':  by looking at the
7180 		 * code, you might think that it could be declared in the
7181 		 * action processing loop, below.  (That is, it's only used in
7182 		 * the action processing loop.)  However, it must be declared
7183 		 * out of that scope because in the case of DIF expression
7184 		 * arguments to aggregating actions, one iteration of the
7185 		 * action loop will use the last iteration's value.
7186 		 */
7187 #ifdef lint
7188 		uint64_t val = 0;
7189 #else
7190 		uint64_t val = 0;
7191 #endif
7192 
7193 		mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE;
7194 		*flags &= ~CPU_DTRACE_ERROR;
7195 
7196 		if (prov == dtrace_provider) {
7197 			/*
7198 			 * If dtrace itself is the provider of this probe,
7199 			 * we're only going to continue processing the ECB if
7200 			 * arg0 (the dtrace_state_t) is equal to the ECB's
7201 			 * creating state.  (This prevents disjoint consumers
7202 			 * from seeing one another's metaprobes.)
7203 			 */
7204 			if (arg0 != (uint64_t)(uintptr_t)state)
7205 				continue;
7206 		}
7207 
7208 		if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) {
7209 			/*
7210 			 * We're not currently active.  If our provider isn't
7211 			 * the dtrace pseudo provider, we're not interested.
7212 			 */
7213 			if (prov != dtrace_provider)
7214 				continue;
7215 
7216 			/*
7217 			 * Now we must further check if we are in the BEGIN
7218 			 * probe.  If we are, we will only continue processing
7219 			 * if we're still in WARMUP -- if one BEGIN enabling
7220 			 * has invoked the exit() action, we don't want to
7221 			 * evaluate subsequent BEGIN enablings.
7222 			 */
7223 			if (probe->dtpr_id == dtrace_probeid_begin &&
7224 			    state->dts_activity != DTRACE_ACTIVITY_WARMUP) {
7225 				ASSERT(state->dts_activity ==
7226 				    DTRACE_ACTIVITY_DRAINING);
7227 				continue;
7228 			}
7229 		}
7230 
7231 		if (ecb->dte_cond) {
7232 			/*
7233 			 * If the dte_cond bits indicate that this
7234 			 * consumer is only allowed to see user-mode firings
7235 			 * of this probe, call the provider's dtps_usermode()
7236 			 * entry point to check that the probe was fired
7237 			 * while in a user context. Skip this ECB if that's
7238 			 * not the case.
7239 			 */
7240 			if ((ecb->dte_cond & DTRACE_COND_USERMODE) &&
7241 			    prov->dtpv_pops.dtps_usermode &&
7242 			    prov->dtpv_pops.dtps_usermode(prov->dtpv_arg,
7243 			    probe->dtpr_id, probe->dtpr_arg) == 0)
7244 				continue;
7245 
7246 			/*
7247 			 * This is more subtle than it looks. We have to be
7248 			 * absolutely certain that CRED() isn't going to
7249 			 * change out from under us so it's only legit to
7250 			 * examine that structure if we're in constrained
7251 			 * situations. Currently, the only times we'll this
7252 			 * check is if a non-super-user has enabled the
7253 			 * profile or syscall providers -- providers that
7254 			 * allow visibility of all processes. For the
7255 			 * profile case, the check above will ensure that
7256 			 * we're examining a user context.
7257 			 */
7258 			if (ecb->dte_cond & DTRACE_COND_OWNER) {
7259 				cred_t *cr;
7260 				cred_t *s_cr =
7261 				    ecb->dte_state->dts_cred.dcr_cred;
7262 				proc_t *proc;
7263 #pragma unused(proc) /* __APPLE__ */
7264 
7265 				ASSERT(s_cr != NULL);
7266 
7267 			/*
7268 			 * XXX this is hackish, but so is setting a variable
7269 			 * XXX in a McCarthy OR...
7270 			 */
7271 				if ((cr = dtrace_CRED()) == NULL ||
7272 				    posix_cred_get(s_cr)->cr_uid != posix_cred_get(cr)->cr_uid ||
7273 				    posix_cred_get(s_cr)->cr_uid != posix_cred_get(cr)->cr_ruid ||
7274 				    posix_cred_get(s_cr)->cr_uid != posix_cred_get(cr)->cr_suid ||
7275 				    posix_cred_get(s_cr)->cr_gid != posix_cred_get(cr)->cr_gid ||
7276 				    posix_cred_get(s_cr)->cr_gid != posix_cred_get(cr)->cr_rgid ||
7277 				    posix_cred_get(s_cr)->cr_gid != posix_cred_get(cr)->cr_sgid ||
7278 #if !defined(__APPLE__)
7279 				    (proc = ttoproc(curthread)) == NULL ||
7280 				    (proc->p_flag & SNOCD))
7281 #else
7282 					1) /* APPLE NOTE: Darwin omits "No Core Dump" flag */
7283 #endif /* __APPLE__ */
7284 					continue;
7285 			}
7286 
7287 			if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) {
7288 				cred_t *cr;
7289 				cred_t *s_cr =
7290 				    ecb->dte_state->dts_cred.dcr_cred;
7291 #pragma unused(cr, s_cr) /* __APPLE__ */
7292 
7293 				ASSERT(s_cr != NULL);
7294 
7295 #if !defined(__APPLE__)
7296 				if ((cr = CRED()) == NULL ||
7297 				    s_cr->cr_zone->zone_id !=
7298 				    cr->cr_zone->zone_id)
7299 					continue;
7300 #else
7301 				/* APPLE NOTE: Darwin doesn't do zones. */
7302 #endif /* __APPLE__ */
7303 			}
7304 		}
7305 
7306 		if (now - state->dts_alive > dtrace_deadman_timeout) {
7307 			/*
7308 			 * We seem to be dead.  Unless we (a) have kernel
7309 			 * destructive permissions (b) have expicitly enabled
7310 			 * destructive actions and (c) destructive actions have
7311 			 * not been disabled, we're going to transition into
7312 			 * the KILLED state, from which no further processing
7313 			 * on this state will be performed.
7314 			 */
7315 			if (!dtrace_priv_kernel_destructive(state) ||
7316 			    !state->dts_cred.dcr_destructive ||
7317 			    dtrace_destructive_disallow) {
7318 				void *activity = &state->dts_activity;
7319 				dtrace_activity_t current;
7320 
7321 				do {
7322 					current = state->dts_activity;
7323 				} while (dtrace_cas32(activity, current,
7324 				    DTRACE_ACTIVITY_KILLED) != current);
7325 
7326 				continue;
7327 			}
7328 		}
7329 
7330 		if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed,
7331 		    ecb->dte_alignment, state, &mstate)) < 0)
7332 			continue;
7333 
7334 		tomax = buf->dtb_tomax;
7335 		ASSERT(tomax != NULL);
7336 
7337 		/*
7338 		 * Build and store the record header corresponding to the ECB.
7339 		 */
7340 		if (ecb->dte_size != 0) {
7341 			dtrace_rechdr_t dtrh;
7342 
7343 			if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
7344 				mstate.dtms_timestamp = dtrace_gethrtime();
7345 				mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP;
7346 			}
7347 
7348 			ASSERT(ecb->dte_size >= sizeof(dtrace_rechdr_t));
7349 
7350 			dtrh.dtrh_epid = ecb->dte_epid;
7351 			DTRACE_RECORD_STORE_TIMESTAMP(&dtrh, mstate.dtms_timestamp);
7352 			DTRACE_STORE(dtrace_rechdr_t, tomax, offs, dtrh);
7353 		}
7354 
7355 		mstate.dtms_epid = ecb->dte_epid;
7356 		mstate.dtms_present |= DTRACE_MSTATE_EPID;
7357 
7358 		if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)
7359 			mstate.dtms_access = DTRACE_ACCESS_KERNEL;
7360 		else
7361 			mstate.dtms_access = 0;
7362 
7363 		if (pred != NULL) {
7364 			dtrace_difo_t *dp = pred->dtp_difo;
7365 			uint64_t rval;
7366 
7367 			rval = dtrace_dif_emulate(dp, &mstate, vstate, state);
7368 
7369 			if (!(*flags & CPU_DTRACE_ERROR) && !rval) {
7370 				dtrace_cacheid_t cid = probe->dtpr_predcache;
7371 
7372 				if (cid != DTRACE_CACHEIDNONE && !onintr) {
7373 					/*
7374 					 * Update the predicate cache...
7375 					 */
7376 					ASSERT(cid == pred->dtp_cacheid);
7377 
7378 					dtrace_set_thread_predcache(current_thread(), cid);
7379 				}
7380 
7381 				continue;
7382 			}
7383 		}
7384 
7385 		for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) &&
7386 		    act != NULL; act = act->dta_next) {
7387 			size_t valoffs;
7388 			dtrace_difo_t *dp;
7389 			dtrace_recdesc_t *rec = &act->dta_rec;
7390 
7391 			size = rec->dtrd_size;
7392 			valoffs = offs + rec->dtrd_offset;
7393 
7394 			if (DTRACEACT_ISAGG(act->dta_kind)) {
7395 				uint64_t v = 0xbad;
7396 				dtrace_aggregation_t *agg;
7397 
7398 				agg = (dtrace_aggregation_t *)act;
7399 
7400 				if ((dp = act->dta_difo) != NULL)
7401 					v = dtrace_dif_emulate(dp,
7402 					    &mstate, vstate, state);
7403 
7404 				if (*flags & CPU_DTRACE_ERROR)
7405 					continue;
7406 
7407 				/*
7408 				 * Note that we always pass the expression
7409 				 * value from the previous iteration of the
7410 				 * action loop.  This value will only be used
7411 				 * if there is an expression argument to the
7412 				 * aggregating action, denoted by the
7413 				 * dtag_hasarg field.
7414 				 */
7415 				dtrace_aggregate(agg, buf,
7416 				    offs, aggbuf, v, val);
7417 				continue;
7418 			}
7419 
7420 			switch (act->dta_kind) {
7421 			case DTRACEACT_STOP:
7422 				if (dtrace_priv_proc_destructive(state))
7423 					dtrace_action_stop();
7424 				continue;
7425 
7426 			case DTRACEACT_BREAKPOINT:
7427 				if (dtrace_priv_kernel_destructive(state))
7428 					dtrace_action_breakpoint(ecb);
7429 				continue;
7430 
7431 			case DTRACEACT_PANIC:
7432 				if (dtrace_priv_kernel_destructive(state))
7433 					dtrace_action_panic(ecb);
7434 				continue;
7435 
7436 			case DTRACEACT_STACK:
7437 				if (!dtrace_priv_kernel(state))
7438 					continue;
7439 
7440 				dtrace_getpcstack((pc_t *)(tomax + valoffs),
7441 				    size / sizeof (pc_t), probe->dtpr_aframes,
7442 				    DTRACE_ANCHORED(probe) ? NULL :
7443 				  (uint32_t *)(uintptr_t)arg0);
7444 				continue;
7445 
7446 			case DTRACEACT_JSTACK:
7447 			case DTRACEACT_USTACK:
7448 				if (!dtrace_priv_proc(state))
7449 					continue;
7450 
7451 				/*
7452 				 * See comment in DIF_VAR_PID.
7453 				 */
7454 				if (DTRACE_ANCHORED(mstate.dtms_probe) &&
7455 				    CPU_ON_INTR(CPU)) {
7456 					int depth = DTRACE_USTACK_NFRAMES(
7457 					    rec->dtrd_arg) + 1;
7458 
7459 					dtrace_bzero((void *)(tomax + valoffs),
7460 					    DTRACE_USTACK_STRSIZE(rec->dtrd_arg)
7461 					    + depth * sizeof (uint64_t));
7462 
7463 					continue;
7464 				}
7465 
7466 				if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 &&
7467 				    curproc->p_dtrace_helpers != NULL) {
7468 					/*
7469 					 * This is the slow path -- we have
7470 					 * allocated string space, and we're
7471 					 * getting the stack of a process that
7472 					 * has helpers.  Call into a separate
7473 					 * routine to perform this processing.
7474 					 */
7475 					dtrace_action_ustack(&mstate, state,
7476 					    (uint64_t *)(tomax + valoffs),
7477 					    rec->dtrd_arg);
7478 					continue;
7479 				}
7480 
7481 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7482 				dtrace_getupcstack((uint64_t *)
7483 				    (tomax + valoffs),
7484 				    DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1);
7485 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7486 				continue;
7487 
7488 			default:
7489 				break;
7490 			}
7491 
7492 			dp = act->dta_difo;
7493 			ASSERT(dp != NULL);
7494 
7495 			val = dtrace_dif_emulate(dp, &mstate, vstate, state);
7496 
7497 			if (*flags & CPU_DTRACE_ERROR)
7498 				continue;
7499 
7500 			switch (act->dta_kind) {
7501 			case DTRACEACT_SPECULATE: {
7502 				dtrace_rechdr_t *dtrh = NULL;
7503 
7504 				ASSERT(buf == &state->dts_buffer[cpuid]);
7505 				buf = dtrace_speculation_buffer(state,
7506 				    cpuid, val);
7507 
7508 				if (buf == NULL) {
7509 					*flags |= CPU_DTRACE_DROP;
7510 					continue;
7511 				}
7512 
7513 				offs = dtrace_buffer_reserve(buf,
7514 				    ecb->dte_needed, ecb->dte_alignment,
7515 				    state, NULL);
7516 
7517 				if (offs < 0) {
7518 					*flags |= CPU_DTRACE_DROP;
7519 					continue;
7520 				}
7521 
7522 				tomax = buf->dtb_tomax;
7523 				ASSERT(tomax != NULL);
7524 
7525 				if (ecb->dte_size == 0)
7526 					continue;
7527 
7528 				ASSERT(ecb->dte_size >= sizeof(dtrace_rechdr_t));
7529 				dtrh = ((void *)(tomax + offs));
7530 				dtrh->dtrh_epid = ecb->dte_epid;
7531 
7532 				/*
7533 				 * When the speculation is committed, all of
7534 				 * the records in the speculative buffer will
7535 				 * have their timestamps set to the commit
7536 				 * time.  Until then, it is set to a sentinel
7537 				 * value, for debugability.
7538 				 */
7539 				DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX);
7540 
7541  				continue;
7542 			}
7543 
7544 			case DTRACEACT_CHILL:
7545 				if (dtrace_priv_kernel_destructive(state))
7546 					dtrace_action_chill(&mstate, val);
7547 				continue;
7548 
7549 			case DTRACEACT_RAISE:
7550 				if (dtrace_priv_proc_destructive(state))
7551 					dtrace_action_raise(val);
7552 				continue;
7553 
7554 			case DTRACEACT_PIDRESUME:   /* __APPLE__ */
7555 				if (dtrace_priv_proc_destructive(state))
7556 					dtrace_action_pidresume(val);
7557 				continue;
7558 
7559 			case DTRACEACT_COMMIT:
7560 				ASSERT(!committed);
7561 
7562 				/*
7563 				 * We need to commit our buffer state.
7564 				 */
7565 				if (ecb->dte_size)
7566 					buf->dtb_offset = offs + ecb->dte_size;
7567 				buf = &state->dts_buffer[cpuid];
7568 				dtrace_speculation_commit(state, cpuid, val);
7569 				committed = 1;
7570 				continue;
7571 
7572 			case DTRACEACT_DISCARD:
7573 				dtrace_speculation_discard(state, cpuid, val);
7574 				continue;
7575 
7576 			case DTRACEACT_DIFEXPR:
7577 			case DTRACEACT_LIBACT:
7578 			case DTRACEACT_PRINTF:
7579 			case DTRACEACT_PRINTA:
7580 			case DTRACEACT_SYSTEM:
7581 			case DTRACEACT_FREOPEN:
7582 			case DTRACEACT_APPLEBINARY:   /* __APPLE__ */
7583 			case DTRACEACT_TRACEMEM:
7584 				break;
7585 
7586 			case DTRACEACT_TRACEMEM_DYNSIZE:
7587 				tracememsize = val;
7588 				break;
7589 
7590 			case DTRACEACT_SYM:
7591 			case DTRACEACT_MOD:
7592 				if (!dtrace_priv_kernel(state))
7593 					continue;
7594 				break;
7595 
7596 			case DTRACEACT_USYM:
7597 			case DTRACEACT_UMOD:
7598 			case DTRACEACT_UADDR: {
7599 				if (!dtrace_priv_proc(state))
7600 					continue;
7601 
7602 				DTRACE_STORE(uint64_t, tomax,
7603 				    valoffs, (uint64_t)dtrace_proc_selfpid());
7604 				DTRACE_STORE(uint64_t, tomax,
7605 				    valoffs + sizeof (uint64_t), val);
7606 
7607 				continue;
7608 			}
7609 
7610 			case DTRACEACT_EXIT: {
7611 				/*
7612 				 * For the exit action, we are going to attempt
7613 				 * to atomically set our activity to be
7614 				 * draining.  If this fails (either because
7615 				 * another CPU has beat us to the exit action,
7616 				 * or because our current activity is something
7617 				 * other than ACTIVE or WARMUP), we will
7618 				 * continue.  This assures that the exit action
7619 				 * can be successfully recorded at most once
7620 				 * when we're in the ACTIVE state.  If we're
7621 				 * encountering the exit() action while in
7622 				 * COOLDOWN, however, we want to honor the new
7623 				 * status code.  (We know that we're the only
7624 				 * thread in COOLDOWN, so there is no race.)
7625 				 */
7626 				void *activity = &state->dts_activity;
7627 				dtrace_activity_t current = state->dts_activity;
7628 
7629 				if (current == DTRACE_ACTIVITY_COOLDOWN)
7630 					break;
7631 
7632 				if (current != DTRACE_ACTIVITY_WARMUP)
7633 					current = DTRACE_ACTIVITY_ACTIVE;
7634 
7635 				if (dtrace_cas32(activity, current,
7636 				    DTRACE_ACTIVITY_DRAINING) != current) {
7637 					*flags |= CPU_DTRACE_DROP;
7638 					continue;
7639 				}
7640 
7641 				break;
7642 			}
7643 
7644 			default:
7645 				ASSERT(0);
7646 			}
7647 
7648 			if (dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF)) {
7649 				uintptr_t end = valoffs + size;
7650 
7651 				if (tracememsize != 0 &&
7652                                     valoffs + tracememsize < end)
7653 				{
7654                                         end = valoffs + tracememsize;
7655                                         tracememsize = 0;
7656                                 }
7657 
7658 				if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF &&
7659 				    !dtrace_vcanload((void *)(uintptr_t)val,
7660 				    &dp->dtdo_rtype, NULL, &mstate, vstate))
7661 				{
7662 					continue;
7663 				}
7664 
7665 				dtrace_store_by_ref(dp, tomax, size, &valoffs,
7666 				    &val, end, act->dta_intuple,
7667 				    dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ?
7668 				    DIF_TF_BYREF: DIF_TF_BYUREF);
7669 
7670 				continue;
7671 			}
7672 
7673 			switch (size) {
7674 			case 0:
7675 				break;
7676 
7677 			case sizeof (uint8_t):
7678 				DTRACE_STORE(uint8_t, tomax, valoffs, val);
7679 				break;
7680 			case sizeof (uint16_t):
7681 				DTRACE_STORE(uint16_t, tomax, valoffs, val);
7682 				break;
7683 			case sizeof (uint32_t):
7684 				DTRACE_STORE(uint32_t, tomax, valoffs, val);
7685 				break;
7686 			case sizeof (uint64_t):
7687 				DTRACE_STORE(uint64_t, tomax, valoffs, val);
7688 				break;
7689 			default:
7690 				/*
7691 				 * Any other size should have been returned by
7692 				 * reference, not by value.
7693 				 */
7694 				ASSERT(0);
7695 				break;
7696 			}
7697 		}
7698 
7699 		if (*flags & CPU_DTRACE_DROP)
7700 			continue;
7701 
7702 		if (*flags & CPU_DTRACE_FAULT) {
7703 			int ndx;
7704 			dtrace_action_t *err;
7705 
7706 			buf->dtb_errors++;
7707 
7708 			if (probe->dtpr_id == dtrace_probeid_error) {
7709 				/*
7710 				 * There's nothing we can do -- we had an
7711 				 * error on the error probe.  We bump an
7712 				 * error counter to at least indicate that
7713 				 * this condition happened.
7714 				 */
7715 				dtrace_error(&state->dts_dblerrors);
7716 				continue;
7717 			}
7718 
7719 			if (vtime) {
7720 				/*
7721 				 * Before recursing on dtrace_probe(), we
7722 				 * need to explicitly clear out our start
7723 				 * time to prevent it from being accumulated
7724 				 * into t_dtrace_vtime.
7725 				 */
7726 
7727 				/*
7728 				 * Darwin sets the sign bit on t_dtrace_tracing
7729 				 * to suspend accumulation to it.
7730 				 */
7731 				dtrace_set_thread_tracing(current_thread(),
7732 				    (1ULL<<63) | dtrace_get_thread_tracing(current_thread()));
7733 			}
7734 
7735 			/*
7736 			 * Iterate over the actions to figure out which action
7737 			 * we were processing when we experienced the error.
7738 			 * Note that act points _past_ the faulting action; if
7739 			 * act is ecb->dte_action, the fault was in the
7740 			 * predicate, if it's ecb->dte_action->dta_next it's
7741 			 * in action #1, and so on.
7742 			 */
7743 			for (err = ecb->dte_action, ndx = 0;
7744 			    err != act; err = err->dta_next, ndx++)
7745 				continue;
7746 
7747 			dtrace_probe_error(state, ecb->dte_epid, ndx,
7748 			    (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ?
7749 			    mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags),
7750 			    cpu_core[cpuid].cpuc_dtrace_illval);
7751 
7752 			continue;
7753 		}
7754 
7755 		if (!committed)
7756 			buf->dtb_offset = offs + ecb->dte_size;
7757 	}
7758 
7759 	/* FIXME: On Darwin the time spent leaving DTrace from this point to the rti is attributed
7760 	   to the current thread. Instead it should accrue to DTrace. */
7761 	if (vtime) {
7762 		thread_t thread = current_thread();
7763 		int64_t t = dtrace_get_thread_tracing(thread);
7764 
7765 		if (t >= 0) {
7766 			/* Usual case, accumulate time spent here into t_dtrace_tracing */
7767 			dtrace_set_thread_tracing(thread, t + (dtrace_gethrtime() - now));
7768 		} else {
7769 			/* Return from error recursion. No accumulation, just clear the sign bit on t_dtrace_tracing. */
7770 			dtrace_set_thread_tracing(thread, (~(1ULL<<63)) & t);
7771 		}
7772 	}
7773 
7774 	dtrace_probe_exit(cookie);
7775 }
7776 
7777 /*
7778  * DTrace Probe Hashing Functions
7779  *
7780  * The functions in this section (and indeed, the functions in remaining
7781  * sections) are not _called_ from probe context.  (Any exceptions to this are
7782  * marked with a "Note:".)  Rather, they are called from elsewhere in the
7783  * DTrace framework to look-up probes in, add probes to and remove probes from
7784  * the DTrace probe hashes.  (Each probe is hashed by each element of the
7785  * probe tuple -- allowing for fast lookups, regardless of what was
7786  * specified.)
7787  */
7788 static uint_t
dtrace_hash_str(const char * p)7789 dtrace_hash_str(const char *p)
7790 {
7791 	unsigned int g;
7792 	uint_t hval = 0;
7793 
7794 	while (*p) {
7795 		hval = (hval << 4) + *p++;
7796 		if ((g = (hval & 0xf0000000)) != 0)
7797 			hval ^= g >> 24;
7798 		hval &= ~g;
7799 	}
7800 	return (hval);
7801 }
7802 
7803 static const char*
dtrace_strkey_probe_provider(void * elm,uintptr_t offs)7804 dtrace_strkey_probe_provider(void *elm, uintptr_t offs)
7805 {
7806 #pragma unused(offs)
7807 	dtrace_probe_t *probe = (dtrace_probe_t*)elm;
7808 	return probe->dtpr_provider->dtpv_name;
7809 }
7810 
7811 static const char*
dtrace_strkey_offset(void * elm,uintptr_t offs)7812 dtrace_strkey_offset(void *elm, uintptr_t offs)
7813 {
7814 	return ((char *)((uintptr_t)(elm) + offs));
7815 }
7816 
7817 static const char*
dtrace_strkey_deref_offset(void * elm,uintptr_t offs)7818 dtrace_strkey_deref_offset(void *elm, uintptr_t offs)
7819 {
7820 	return *((char **)((uintptr_t)(elm) + offs));
7821 }
7822 
7823 static dtrace_hash_t *
dtrace_hash_create(dtrace_strkey_f func,uintptr_t arg,uintptr_t nextoffs,uintptr_t prevoffs)7824 dtrace_hash_create(dtrace_strkey_f func, uintptr_t arg, uintptr_t nextoffs, uintptr_t prevoffs)
7825 {
7826 	dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP);
7827 
7828 	hash->dth_getstr = func;
7829 	hash->dth_stroffs = arg;
7830 	hash->dth_nextoffs = nextoffs;
7831 	hash->dth_prevoffs = prevoffs;
7832 
7833 	hash->dth_size = 1;
7834 	hash->dth_mask = hash->dth_size - 1;
7835 
7836 	hash->dth_tab = kmem_zalloc(hash->dth_size *
7837 	    sizeof (dtrace_hashbucket_t *), KM_SLEEP);
7838 
7839 	return (hash);
7840 }
7841 
7842 /*
7843  * APPLE NOTE: dtrace_hash_destroy is not used.
7844  * It is called by dtrace_detach which is not
7845  * currently implemented.  Revisit someday.
7846  */
7847 #if !defined(__APPLE__)
7848 static void
dtrace_hash_destroy(dtrace_hash_t * hash)7849 dtrace_hash_destroy(dtrace_hash_t *hash)
7850 {
7851 #if DEBUG
7852 	int i;
7853 
7854 	for (i = 0; i < hash->dth_size; i++)
7855 		ASSERT(hash->dth_tab[i] == NULL);
7856 #endif
7857 
7858 	kmem_free(hash->dth_tab,
7859 	    hash->dth_size * sizeof (dtrace_hashbucket_t *));
7860 	kmem_free(hash, sizeof (dtrace_hash_t));
7861 }
7862 #endif /* __APPLE__ */
7863 
7864 static void
dtrace_hash_resize(dtrace_hash_t * hash)7865 dtrace_hash_resize(dtrace_hash_t *hash)
7866 {
7867 	int size = hash->dth_size, i, ndx;
7868 	int new_size = hash->dth_size << 1;
7869 	int new_mask = new_size - 1;
7870 	dtrace_hashbucket_t **new_tab, *bucket, *next;
7871 
7872 	ASSERT((new_size & new_mask) == 0);
7873 
7874 	new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP);
7875 
7876 	for (i = 0; i < size; i++) {
7877 		for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) {
7878 			void *elm = bucket->dthb_chain;
7879 
7880 			ASSERT(elm != NULL);
7881 			ndx = DTRACE_HASHSTR(hash, elm) & new_mask;
7882 
7883 			next = bucket->dthb_next;
7884 			bucket->dthb_next = new_tab[ndx];
7885 			new_tab[ndx] = bucket;
7886 		}
7887 	}
7888 
7889 	kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *));
7890 	hash->dth_tab = new_tab;
7891 	hash->dth_size = new_size;
7892 	hash->dth_mask = new_mask;
7893 }
7894 
7895 static void
dtrace_hash_add(dtrace_hash_t * hash,void * new)7896 dtrace_hash_add(dtrace_hash_t *hash, void *new)
7897 {
7898 	int hashval = DTRACE_HASHSTR(hash, new);
7899 	int ndx = hashval & hash->dth_mask;
7900 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7901 	void **nextp, **prevp;
7902 
7903 	for (; bucket != NULL; bucket = bucket->dthb_next) {
7904 		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new))
7905 			goto add;
7906 	}
7907 
7908 	if ((hash->dth_nbuckets >> 1) > hash->dth_size) {
7909 		dtrace_hash_resize(hash);
7910 		dtrace_hash_add(hash, new);
7911 		return;
7912 	}
7913 
7914 	bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP);
7915 	bucket->dthb_next = hash->dth_tab[ndx];
7916 	hash->dth_tab[ndx] = bucket;
7917 	hash->dth_nbuckets++;
7918 
7919 add:
7920 	nextp = DTRACE_HASHNEXT(hash, new);
7921 	ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL);
7922 	*nextp = bucket->dthb_chain;
7923 
7924 	if (bucket->dthb_chain != NULL) {
7925 		prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain);
7926 		ASSERT(*prevp == NULL);
7927 		*prevp = new;
7928 	}
7929 
7930 	bucket->dthb_chain = new;
7931 	bucket->dthb_len++;
7932 }
7933 
7934 static void *
dtrace_hash_lookup_string(dtrace_hash_t * hash,const char * str)7935 dtrace_hash_lookup_string(dtrace_hash_t *hash, const char *str)
7936 {
7937 	int hashval = dtrace_hash_str(str);
7938 	int ndx = hashval & hash->dth_mask;
7939 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7940 
7941 	for (; bucket != NULL; bucket = bucket->dthb_next) {
7942 		if (strcmp(str, DTRACE_GETSTR(hash, bucket->dthb_chain)) == 0)
7943 			return (bucket->dthb_chain);
7944 	}
7945 
7946 	return (NULL);
7947 }
7948 
7949 static dtrace_probe_t *
dtrace_hash_lookup(dtrace_hash_t * hash,void * template)7950 dtrace_hash_lookup(dtrace_hash_t *hash, void *template)
7951 {
7952 	return dtrace_hash_lookup_string(hash, DTRACE_GETSTR(hash, template));
7953 }
7954 
7955 static int
dtrace_hash_collisions(dtrace_hash_t * hash,void * template)7956 dtrace_hash_collisions(dtrace_hash_t *hash, void *template)
7957 {
7958 	int hashval = DTRACE_HASHSTR(hash, template);
7959 	int ndx = hashval & hash->dth_mask;
7960 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7961 
7962 	for (; bucket != NULL; bucket = bucket->dthb_next) {
7963 		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
7964 			return (bucket->dthb_len);
7965 	}
7966 
7967 	return (0);
7968 }
7969 
7970 static void
dtrace_hash_remove(dtrace_hash_t * hash,void * elm)7971 dtrace_hash_remove(dtrace_hash_t *hash, void *elm)
7972 {
7973 	int ndx = DTRACE_HASHSTR(hash, elm) & hash->dth_mask;
7974 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7975 
7976 	void **prevp = DTRACE_HASHPREV(hash, elm);
7977 	void **nextp = DTRACE_HASHNEXT(hash, elm);
7978 
7979 	/*
7980 	 * Find the bucket that we're removing this elm from.
7981 	 */
7982 	for (; bucket != NULL; bucket = bucket->dthb_next) {
7983 		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, elm))
7984 			break;
7985 	}
7986 
7987 	ASSERT(bucket != NULL);
7988 
7989 	if (*prevp == NULL) {
7990 		if (*nextp == NULL) {
7991 			/*
7992 			 * The removed element was the only element on this
7993 			 * bucket; we need to remove the bucket.
7994 			 */
7995 			dtrace_hashbucket_t *b = hash->dth_tab[ndx];
7996 
7997 			ASSERT(bucket->dthb_chain == elm);
7998 			ASSERT(b != NULL);
7999 
8000 			if (b == bucket) {
8001 				hash->dth_tab[ndx] = bucket->dthb_next;
8002 			} else {
8003 				while (b->dthb_next != bucket)
8004 					b = b->dthb_next;
8005 				b->dthb_next = bucket->dthb_next;
8006 			}
8007 
8008 			ASSERT(hash->dth_nbuckets > 0);
8009 			hash->dth_nbuckets--;
8010 			kmem_free(bucket, sizeof (dtrace_hashbucket_t));
8011 			return;
8012 		}
8013 
8014 		bucket->dthb_chain = *nextp;
8015 	} else {
8016 		*(DTRACE_HASHNEXT(hash, *prevp)) = *nextp;
8017 	}
8018 
8019 	if (*nextp != NULL)
8020 		*(DTRACE_HASHPREV(hash, *nextp)) = *prevp;
8021 }
8022 
8023 /*
8024  * DTrace Utility Functions
8025  *
8026  * These are random utility functions that are _not_ called from probe context.
8027  */
8028 static int
dtrace_badattr(const dtrace_attribute_t * a)8029 dtrace_badattr(const dtrace_attribute_t *a)
8030 {
8031 	return (a->dtat_name > DTRACE_STABILITY_MAX ||
8032 	    a->dtat_data > DTRACE_STABILITY_MAX ||
8033 	    a->dtat_class > DTRACE_CLASS_MAX);
8034 }
8035 
8036 /*
8037  * Returns a dtrace-managed copy of a string, and will
8038  * deduplicate copies of the same string.
8039  * If the specified string is NULL, returns an empty string
8040  */
8041 static char *
dtrace_strref(const char * str)8042 dtrace_strref(const char *str)
8043 {
8044 	dtrace_string_t *s = NULL;
8045 	size_t bufsize = (str != NULL ? strlen(str) : 0) + 1;
8046 
8047 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
8048 
8049 	if (str == NULL)
8050 		str = "";
8051 
8052 	for (s = dtrace_hash_lookup_string(dtrace_strings, str); s != NULL;
8053 	     s = *(DTRACE_HASHNEXT(dtrace_strings, s)))  {
8054 		if (strncmp(str, s->dtst_str, bufsize) != 0) {
8055 			continue;
8056 		}
8057 		ASSERT(s->dtst_refcount != UINT32_MAX);
8058 		s->dtst_refcount++;
8059 		return s->dtst_str;
8060 	}
8061 
8062 	s = kmem_zalloc(sizeof(dtrace_string_t) + bufsize, KM_SLEEP);
8063 	s->dtst_refcount = 1;
8064 	(void) strlcpy(s->dtst_str, str, bufsize);
8065 
8066 	dtrace_hash_add(dtrace_strings, s);
8067 
8068 	return s->dtst_str;
8069 }
8070 
8071 static void
dtrace_strunref(const char * str)8072 dtrace_strunref(const char *str)
8073 {
8074 	ASSERT(str != NULL);
8075 	dtrace_string_t *s = NULL;
8076 	size_t bufsize = strlen(str) + 1;
8077 
8078 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
8079 
8080 	for (s = dtrace_hash_lookup_string(dtrace_strings, str); s != NULL;
8081 	     s = *(DTRACE_HASHNEXT(dtrace_strings, s)))  {
8082 		if (strncmp(str, s->dtst_str, bufsize) != 0) {
8083 			continue;
8084 		}
8085 		ASSERT(s->dtst_refcount != 0);
8086 		s->dtst_refcount--;
8087 		if (s->dtst_refcount == 0) {
8088 			dtrace_hash_remove(dtrace_strings, s);
8089 			kmem_free(s, sizeof(dtrace_string_t) + bufsize);
8090 		}
8091 		return;
8092 	}
8093 	panic("attempt to unref non-existent string %s", str);
8094 }
8095 
8096 #define	DTRACE_ISALPHA(c)	\
8097 	(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
8098 
8099 static int
dtrace_badname(const char * s)8100 dtrace_badname(const char *s)
8101 {
8102 	char c;
8103 
8104 	if (s == NULL || (c = *s++) == '\0')
8105 		return (0);
8106 
8107 	if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.')
8108 		return (1);
8109 
8110 	while ((c = *s++) != '\0') {
8111 		if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') &&
8112 		    c != '-' && c != '_' && c != '.' && c != '`')
8113 			return (1);
8114 	}
8115 
8116 	return (0);
8117 }
8118 
8119 static void
dtrace_cred2priv(cred_t * cr,uint32_t * privp,uid_t * uidp,zoneid_t * zoneidp)8120 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp)
8121 {
8122 	uint32_t priv;
8123 
8124 	if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
8125 		if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed()) {
8126 			priv = DTRACE_PRIV_USER | DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER;
8127 		}
8128 		else {
8129 			priv = DTRACE_PRIV_ALL;
8130 		}
8131 		*uidp = 0;
8132 		*zoneidp = 0;
8133 	} else {
8134 		*uidp = crgetuid(cr);
8135 		*zoneidp = crgetzoneid(cr);
8136 
8137 		priv = 0;
8138 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE))
8139 			priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER;
8140 		else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE))
8141 			priv |= DTRACE_PRIV_USER;
8142 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE))
8143 			priv |= DTRACE_PRIV_PROC;
8144 		if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
8145 			priv |= DTRACE_PRIV_OWNER;
8146 		if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
8147 			priv |= DTRACE_PRIV_ZONEOWNER;
8148 	}
8149 
8150 	*privp = priv;
8151 }
8152 
8153 #ifdef DTRACE_ERRDEBUG
8154 static void
dtrace_errdebug(const char * str)8155 dtrace_errdebug(const char *str)
8156 {
8157 	int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ;
8158 	int occupied = 0;
8159 
8160 	lck_mtx_lock(&dtrace_errlock);
8161 	dtrace_errlast = str;
8162 	dtrace_errthread = (kthread_t *)current_thread();
8163 
8164 	while (occupied++ < DTRACE_ERRHASHSZ) {
8165 		if (dtrace_errhash[hval].dter_msg == str) {
8166 			dtrace_errhash[hval].dter_count++;
8167 			goto out;
8168 		}
8169 
8170 		if (dtrace_errhash[hval].dter_msg != NULL) {
8171 			hval = (hval + 1) % DTRACE_ERRHASHSZ;
8172 			continue;
8173 		}
8174 
8175 		dtrace_errhash[hval].dter_msg = str;
8176 		dtrace_errhash[hval].dter_count = 1;
8177 		goto out;
8178 	}
8179 
8180 	panic("dtrace: undersized error hash");
8181 out:
8182 	lck_mtx_unlock(&dtrace_errlock);
8183 }
8184 #endif
8185 
8186 /*
8187  * DTrace Matching Functions
8188  *
8189  * These functions are used to match groups of probes, given some elements of
8190  * a probe tuple, or some globbed expressions for elements of a probe tuple.
8191  */
8192 static int
dtrace_match_priv(const dtrace_probe_t * prp,uint32_t priv,uid_t uid,zoneid_t zoneid)8193 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid,
8194     zoneid_t zoneid)
8195 {
8196 	if (priv != DTRACE_PRIV_ALL) {
8197 		uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags;
8198 		uint32_t match = priv & ppriv;
8199 
8200 		/*
8201 		 * No PRIV_DTRACE_* privileges...
8202 		 */
8203 		if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER |
8204 		    DTRACE_PRIV_KERNEL)) == 0)
8205 			return (0);
8206 
8207 		/*
8208 		 * No matching bits, but there were bits to match...
8209 		 */
8210 		if (match == 0 && ppriv != 0)
8211 			return (0);
8212 
8213 		/*
8214 		 * Need to have permissions to the process, but don't...
8215 		 */
8216 		if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 &&
8217 		    uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) {
8218 			return (0);
8219 		}
8220 
8221 		/*
8222 		 * Need to be in the same zone unless we possess the
8223 		 * privilege to examine all zones.
8224 		 */
8225 		if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 &&
8226 		    zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) {
8227 			return (0);
8228 		}
8229 	}
8230 
8231 	return (1);
8232 }
8233 
8234 /*
8235  * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which
8236  * consists of input pattern strings and an ops-vector to evaluate them.
8237  * This function returns >0 for match, 0 for no match, and <0 for error.
8238  */
8239 static int
dtrace_match_probe(const dtrace_probe_t * prp,const dtrace_probekey_t * pkp,uint32_t priv,uid_t uid,zoneid_t zoneid)8240 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp,
8241     uint32_t priv, uid_t uid, zoneid_t zoneid)
8242 {
8243 	dtrace_provider_t *pvp = prp->dtpr_provider;
8244 	int rv;
8245 
8246 	if (pvp->dtpv_defunct)
8247 		return (0);
8248 
8249 	if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0)
8250 		return (rv);
8251 
8252 	if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0)
8253 		return (rv);
8254 
8255 	if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0)
8256 		return (rv);
8257 
8258 	if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0)
8259 		return (rv);
8260 
8261 	if (dtrace_match_priv(prp, priv, uid, zoneid) == 0)
8262 		return (0);
8263 
8264 	return (rv);
8265 }
8266 
8267 /*
8268  * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN)
8269  * interface for matching a glob pattern 'p' to an input string 's'.  Unlike
8270  * libc's version, the kernel version only applies to 8-bit ASCII strings.
8271  * In addition, all of the recursion cases except for '*' matching have been
8272  * unwound.  For '*', we still implement recursive evaluation, but a depth
8273  * counter is maintained and matching is aborted if we recurse too deep.
8274  * The function returns 0 if no match, >0 if match, and <0 if recursion error.
8275  */
8276 static int
dtrace_match_glob(const char * s,const char * p,int depth)8277 dtrace_match_glob(const char *s, const char *p, int depth)
8278 {
8279 	const char *olds;
8280 	char s1, c;
8281 	int gs;
8282 
8283 	if (depth > DTRACE_PROBEKEY_MAXDEPTH)
8284 		return (-1);
8285 
8286 	if (s == NULL)
8287 		s = ""; /* treat NULL as empty string */
8288 
8289 top:
8290 	olds = s;
8291 	s1 = *s++;
8292 
8293 	if (p == NULL)
8294 		return (0);
8295 
8296 	if ((c = *p++) == '\0')
8297 		return (s1 == '\0');
8298 
8299 	switch (c) {
8300 	case '[': {
8301 		int ok = 0, notflag = 0;
8302 		char lc = '\0';
8303 
8304 		if (s1 == '\0')
8305 			return (0);
8306 
8307 		if (*p == '!') {
8308 			notflag = 1;
8309 			p++;
8310 		}
8311 
8312 		if ((c = *p++) == '\0')
8313 			return (0);
8314 
8315 		do {
8316 			if (c == '-' && lc != '\0' && *p != ']') {
8317 				if ((c = *p++) == '\0')
8318 					return (0);
8319 				if (c == '\\' && (c = *p++) == '\0')
8320 					return (0);
8321 
8322 				if (notflag) {
8323 					if (s1 < lc || s1 > c)
8324 						ok++;
8325 					else
8326 						return (0);
8327 				} else if (lc <= s1 && s1 <= c)
8328 					ok++;
8329 
8330 			} else if (c == '\\' && (c = *p++) == '\0')
8331 				return (0);
8332 
8333 			lc = c; /* save left-hand 'c' for next iteration */
8334 
8335 			if (notflag) {
8336 				if (s1 != c)
8337 					ok++;
8338 				else
8339 					return (0);
8340 			} else if (s1 == c)
8341 				ok++;
8342 
8343 			if ((c = *p++) == '\0')
8344 				return (0);
8345 
8346 		} while (c != ']');
8347 
8348 		if (ok)
8349 			goto top;
8350 
8351 		return (0);
8352 	}
8353 
8354 	case '\\':
8355 		if ((c = *p++) == '\0')
8356 			return (0);
8357 		OS_FALLTHROUGH;
8358 
8359 	default:
8360 		if (c != s1)
8361 			return (0);
8362 		OS_FALLTHROUGH;
8363 
8364 	case '?':
8365 		if (s1 != '\0')
8366 			goto top;
8367 		return (0);
8368 
8369 	case '*':
8370 		while (*p == '*')
8371 			p++; /* consecutive *'s are identical to a single one */
8372 
8373 		if (*p == '\0')
8374 			return (1);
8375 
8376 		for (s = olds; *s != '\0'; s++) {
8377 			if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0)
8378 				return (gs);
8379 		}
8380 
8381 		return (0);
8382 	}
8383 }
8384 
8385 /*ARGSUSED*/
8386 static int
dtrace_match_string(const char * s,const char * p,int depth)8387 dtrace_match_string(const char *s, const char *p, int depth)
8388 {
8389 #pragma unused(depth) /* __APPLE__ */
8390 	return (s != NULL && s == p);
8391 }
8392 
8393 /*ARGSUSED*/
8394 static int
dtrace_match_module(const char * s,const char * p,int depth)8395 dtrace_match_module(const char *s, const char *p, int depth)
8396 {
8397 #pragma unused(depth) /* __APPLE__ */
8398 	size_t len;
8399 	if (s == NULL || p == NULL)
8400 		return (0);
8401 
8402 	len = strlen(p);
8403 
8404 	if (strncmp(p, s, len) != 0)
8405 		return (0);
8406 
8407 	if (s[len] == '.' || s[len] == '\0')
8408 		return (1);
8409 
8410 	return (0);
8411 }
8412 
8413 /*ARGSUSED*/
8414 static int
dtrace_match_nul(const char * s,const char * p,int depth)8415 dtrace_match_nul(const char *s, const char *p, int depth)
8416 {
8417 #pragma unused(s, p, depth) /* __APPLE__ */
8418 	return (1); /* always match the empty pattern */
8419 }
8420 
8421 /*ARGSUSED*/
8422 static int
dtrace_match_nonzero(const char * s,const char * p,int depth)8423 dtrace_match_nonzero(const char *s, const char *p, int depth)
8424 {
8425 #pragma unused(p, depth) /* __APPLE__ */
8426 	return (s != NULL && s[0] != '\0');
8427 }
8428 
8429 static int
dtrace_match(const dtrace_probekey_t * pkp,uint32_t priv,uid_t uid,zoneid_t zoneid,int (* matched)(dtrace_probe_t *,void *,void *),void * arg1,void * arg2)8430 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid,
8431     zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *, void *), void *arg1, void *arg2)
8432 {
8433 	dtrace_probe_t *probe;
8434 	dtrace_provider_t prov_template = {
8435 		.dtpv_name = (char *)(uintptr_t)pkp->dtpk_prov
8436 	};
8437 
8438 	dtrace_probe_t template = {
8439 		.dtpr_provider = &prov_template,
8440 		.dtpr_mod = (char *)(uintptr_t)pkp->dtpk_mod,
8441 		.dtpr_func = (char *)(uintptr_t)pkp->dtpk_func,
8442 		.dtpr_name = (char *)(uintptr_t)pkp->dtpk_name
8443 	};
8444 
8445 	dtrace_hash_t *hash = NULL;
8446 	int len, rc, best = INT_MAX, nmatched = 0;
8447 	dtrace_id_t i;
8448 
8449 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
8450 
8451 	/*
8452 	 * If the probe ID is specified in the key, just lookup by ID and
8453 	 * invoke the match callback once if a matching probe is found.
8454 	 */
8455 	if (pkp->dtpk_id != DTRACE_IDNONE) {
8456 		if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL &&
8457 		    dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) {
8458 		        if ((*matched)(probe, arg1, arg2) == DTRACE_MATCH_FAIL)
8459                                return (DTRACE_MATCH_FAIL);
8460 			nmatched++;
8461 		}
8462 		return (nmatched);
8463 	}
8464 
8465 	/*
8466 	 * We want to find the most distinct of the provider name, module name,
8467 	 * function name, and name.  So for each one that is not a glob
8468 	 * pattern or empty string, we perform a lookup in the corresponding
8469 	 * hash and use the hash table with the fewest collisions to do our
8470 	 * search.
8471 	 */
8472 	if (pkp->dtpk_pmatch == &dtrace_match_string &&
8473 	    (len = dtrace_hash_collisions(dtrace_byprov, &template)) < best) {
8474 		best = len;
8475 		hash = dtrace_byprov;
8476 	}
8477 
8478 	if (pkp->dtpk_mmatch == &dtrace_match_string &&
8479 	    (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) {
8480 		best = len;
8481 		hash = dtrace_bymod;
8482 	}
8483 
8484 	if (pkp->dtpk_fmatch == &dtrace_match_string &&
8485 	    (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) {
8486 		best = len;
8487 		hash = dtrace_byfunc;
8488 	}
8489 
8490 	if (pkp->dtpk_nmatch == &dtrace_match_string &&
8491 	    (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) {
8492 		best = len;
8493 		hash = dtrace_byname;
8494 	}
8495 
8496 	/*
8497 	 * If we did not select a hash table, iterate over every probe and
8498 	 * invoke our callback for each one that matches our input probe key.
8499 	 */
8500 	if (hash == NULL) {
8501 		for (i = 0; i < (dtrace_id_t)dtrace_nprobes; i++) {
8502 			if ((probe = dtrace_probes[i]) == NULL ||
8503 			    dtrace_match_probe(probe, pkp, priv, uid,
8504 			    zoneid) <= 0)
8505 				continue;
8506 
8507 			nmatched++;
8508 
8509                        if ((rc = (*matched)(probe, arg1, arg2)) != DTRACE_MATCH_NEXT) {
8510 			       if (rc == DTRACE_MATCH_FAIL)
8511                                        return (DTRACE_MATCH_FAIL);
8512 			       break;
8513                        }
8514 		}
8515 
8516 		return (nmatched);
8517 	}
8518 
8519 	/*
8520 	 * If we selected a hash table, iterate over each probe of the same key
8521 	 * name and invoke the callback for every probe that matches the other
8522 	 * attributes of our input probe key.
8523 	 */
8524 	for (probe = dtrace_hash_lookup(hash, &template); probe != NULL;
8525 	    probe = *(DTRACE_HASHNEXT(hash, probe))) {
8526 
8527 		if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0)
8528 			continue;
8529 
8530 		nmatched++;
8531 
8532 		if ((rc = (*matched)(probe, arg1, arg2)) != DTRACE_MATCH_NEXT) {
8533 		    if (rc == DTRACE_MATCH_FAIL)
8534 			return (DTRACE_MATCH_FAIL);
8535 		    break;
8536 		}
8537 	}
8538 
8539 	return (nmatched);
8540 }
8541 
8542 /*
8543  * Return the function pointer dtrace_probecmp() should use to compare the
8544  * specified pattern with a string.  For NULL or empty patterns, we select
8545  * dtrace_match_nul().  For glob pattern strings, we use dtrace_match_glob().
8546  * For non-empty non-glob strings, we use dtrace_match_string().
8547  */
8548 static dtrace_probekey_f *
dtrace_probekey_func(const char * p)8549 dtrace_probekey_func(const char *p)
8550 {
8551 	char c;
8552 
8553 	if (p == NULL || *p == '\0')
8554 		return (&dtrace_match_nul);
8555 
8556 	while ((c = *p++) != '\0') {
8557 		if (c == '[' || c == '?' || c == '*' || c == '\\')
8558 			return (&dtrace_match_glob);
8559 	}
8560 
8561 	return (&dtrace_match_string);
8562 }
8563 
8564 static dtrace_probekey_f *
dtrace_probekey_module_func(const char * p)8565 dtrace_probekey_module_func(const char *p)
8566 {
8567 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
8568 
8569 	dtrace_probekey_f *f = dtrace_probekey_func(p);
8570 	if (f == &dtrace_match_string) {
8571 		dtrace_probe_t template = {
8572 			.dtpr_mod = (char *)(uintptr_t)p,
8573 		};
8574 		if (dtrace_hash_lookup(dtrace_bymod, &template) == NULL) {
8575 			return (&dtrace_match_module);
8576 		}
8577 		return (&dtrace_match_string);
8578 	}
8579 	return f;
8580 }
8581 
8582 /*
8583  * Build a probe comparison key for use with dtrace_match_probe() from the
8584  * given probe description.  By convention, a null key only matches anchored
8585  * probes: if each field is the empty string, reset dtpk_fmatch to
8586  * dtrace_match_nonzero().
8587  */
8588 static void
dtrace_probekey(const dtrace_probedesc_t * pdp,dtrace_probekey_t * pkp)8589 dtrace_probekey(const dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp)
8590 {
8591 
8592 	pkp->dtpk_prov = dtrace_strref(pdp->dtpd_provider);
8593 	pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider);
8594 
8595 	pkp->dtpk_mod = dtrace_strref(pdp->dtpd_mod);
8596 	pkp->dtpk_mmatch = dtrace_probekey_module_func(pdp->dtpd_mod);
8597 
8598 	pkp->dtpk_func = dtrace_strref(pdp->dtpd_func);
8599 	pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func);
8600 
8601 	pkp->dtpk_name = dtrace_strref(pdp->dtpd_name);
8602 	pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name);
8603 
8604 	pkp->dtpk_id = pdp->dtpd_id;
8605 
8606 	if (pkp->dtpk_id == DTRACE_IDNONE &&
8607 	    pkp->dtpk_pmatch == &dtrace_match_nul &&
8608 	    pkp->dtpk_mmatch == &dtrace_match_nul &&
8609 	    pkp->dtpk_fmatch == &dtrace_match_nul &&
8610 	    pkp->dtpk_nmatch == &dtrace_match_nul)
8611 		pkp->dtpk_fmatch = &dtrace_match_nonzero;
8612 }
8613 
8614 static void
dtrace_probekey_release(dtrace_probekey_t * pkp)8615 dtrace_probekey_release(dtrace_probekey_t *pkp)
8616 {
8617 	dtrace_strunref(pkp->dtpk_prov);
8618 	dtrace_strunref(pkp->dtpk_mod);
8619 	dtrace_strunref(pkp->dtpk_func);
8620 	dtrace_strunref(pkp->dtpk_name);
8621 }
8622 
8623 static int
dtrace_cond_provider_match(dtrace_probedesc_t * desc,void * data)8624 dtrace_cond_provider_match(dtrace_probedesc_t *desc, void *data)
8625 {
8626 	if (desc == NULL)
8627 		return 1;
8628 
8629 	dtrace_probekey_f *func = dtrace_probekey_func(desc->dtpd_provider);
8630 
8631 	return func((char*)data, desc->dtpd_provider, 0);
8632 }
8633 
8634 /*
8635  * DTrace Provider-to-Framework API Functions
8636  *
8637  * These functions implement much of the Provider-to-Framework API, as
8638  * described in <sys/dtrace.h>.  The parts of the API not in this section are
8639  * the functions in the API for probe management (found below), and
8640  * dtrace_probe() itself (found above).
8641  */
8642 
8643 /*
8644  * Register the calling provider with the DTrace framework.  This should
8645  * generally be called by DTrace providers in their attach(9E) entry point.
8646  */
8647 int
dtrace_register(const char * name,const dtrace_pattr_t * pap,uint32_t priv,cred_t * cr,const dtrace_pops_t * pops,void * arg,dtrace_provider_id_t * idp)8648 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv,
8649     cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp)
8650 {
8651 	dtrace_provider_t *provider;
8652 
8653 	if (name == NULL || pap == NULL || pops == NULL || idp == NULL) {
8654 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8655 		    "arguments", name ? name : "<NULL>");
8656 		return (EINVAL);
8657 	}
8658 
8659 	if (name[0] == '\0' || dtrace_badname(name)) {
8660 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8661 		    "provider name", name);
8662 		return (EINVAL);
8663 	}
8664 
8665 	if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) ||
8666 	    pops->dtps_enable == NULL || pops->dtps_disable == NULL ||
8667 	    pops->dtps_destroy == NULL ||
8668 	    ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) {
8669 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8670 		    "provider ops", name);
8671 		return (EINVAL);
8672 	}
8673 
8674 	if (dtrace_badattr(&pap->dtpa_provider) ||
8675 	    dtrace_badattr(&pap->dtpa_mod) ||
8676 	    dtrace_badattr(&pap->dtpa_func) ||
8677 	    dtrace_badattr(&pap->dtpa_name) ||
8678 	    dtrace_badattr(&pap->dtpa_args)) {
8679 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8680 		    "provider attributes", name);
8681 		return (EINVAL);
8682 	}
8683 
8684 	if (priv & ~DTRACE_PRIV_ALL) {
8685 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8686 		    "privilege attributes", name);
8687 		return (EINVAL);
8688 	}
8689 
8690 	if ((priv & DTRACE_PRIV_KERNEL) &&
8691 	    (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) &&
8692 	    pops->dtps_usermode == NULL) {
8693 		cmn_err(CE_WARN, "failed to register provider '%s': need "
8694 		    "dtps_usermode() op for given privilege attributes", name);
8695 		return (EINVAL);
8696 	}
8697 
8698 	provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP);
8699 
8700 	provider->dtpv_attr = *pap;
8701 	provider->dtpv_priv.dtpp_flags = priv;
8702 	if (cr != NULL) {
8703 		provider->dtpv_priv.dtpp_uid = crgetuid(cr);
8704 		provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr);
8705 	}
8706 	provider->dtpv_pops = *pops;
8707 
8708 	if (pops->dtps_provide == NULL) {
8709 		ASSERT(pops->dtps_provide_module != NULL);
8710 		provider->dtpv_pops.dtps_provide = dtrace_provide_nullop;
8711 	}
8712 
8713 	if (pops->dtps_provide_module == NULL) {
8714 		ASSERT(pops->dtps_provide != NULL);
8715 		provider->dtpv_pops.dtps_provide_module =
8716 		    dtrace_provide_module_nullop;
8717 	}
8718 
8719 	if (pops->dtps_suspend == NULL) {
8720 		ASSERT(pops->dtps_resume == NULL);
8721 		provider->dtpv_pops.dtps_suspend = dtrace_suspend_nullop;
8722 		provider->dtpv_pops.dtps_resume = dtrace_resume_nullop;
8723 	}
8724 
8725 	provider->dtpv_arg = arg;
8726 	*idp = (dtrace_provider_id_t)provider;
8727 
8728 	if (pops == &dtrace_provider_ops) {
8729 		LCK_MTX_ASSERT(&dtrace_provider_lock, LCK_MTX_ASSERT_OWNED);
8730 		LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
8731 
8732 		provider->dtpv_name = dtrace_strref(name);
8733 
8734 		ASSERT(dtrace_anon.dta_enabling == NULL);
8735 
8736 		/*
8737 		 * We make sure that the DTrace provider is at the head of
8738 		 * the provider chain.
8739 		 */
8740 		provider->dtpv_next = dtrace_provider;
8741 		dtrace_provider = provider;
8742 		return (0);
8743 	}
8744 
8745 	lck_mtx_lock(&dtrace_provider_lock);
8746 	lck_mtx_lock(&dtrace_lock);
8747 
8748 	provider->dtpv_name = dtrace_strref(name);
8749 
8750 	/*
8751 	 * If there is at least one provider registered, we'll add this
8752 	 * provider after the first provider.
8753 	 */
8754 	if (dtrace_provider != NULL) {
8755 		provider->dtpv_next = dtrace_provider->dtpv_next;
8756 		dtrace_provider->dtpv_next = provider;
8757 	} else {
8758 		dtrace_provider = provider;
8759 	}
8760 
8761 	if (dtrace_retained != NULL) {
8762 		dtrace_enabling_provide(provider);
8763 
8764 		/*
8765 		 * Now we need to call dtrace_enabling_matchall_with_cond() --
8766 		 * with a condition matching the provider name we just added,
8767 		 * which will acquire cpu_lock and dtrace_lock.  We therefore need
8768 		 * to drop all of our locks before calling into it...
8769 		 */
8770 		lck_mtx_unlock(&dtrace_lock);
8771 		lck_mtx_unlock(&dtrace_provider_lock);
8772 
8773 		dtrace_match_cond_t cond = {dtrace_cond_provider_match, provider->dtpv_name};
8774 		dtrace_enabling_matchall_with_cond(&cond);
8775 
8776 		return (0);
8777 	}
8778 
8779 	lck_mtx_unlock(&dtrace_lock);
8780 	lck_mtx_unlock(&dtrace_provider_lock);
8781 
8782 	return (0);
8783 }
8784 
8785 /*
8786  * Unregister the specified provider from the DTrace framework.  This should
8787  * generally be called by DTrace providers in their detach(9E) entry point.
8788  */
8789 int
dtrace_unregister(dtrace_provider_id_t id)8790 dtrace_unregister(dtrace_provider_id_t id)
8791 {
8792 	dtrace_provider_t *old = (dtrace_provider_t *)id;
8793 	dtrace_provider_t *prev = NULL;
8794 	int self = 0;
8795 	dtrace_probe_t *probe, *first = NULL, *next = NULL;
8796 	dtrace_probe_t template = {
8797 		.dtpr_provider = old
8798 	};
8799 
8800 	if (old->dtpv_pops.dtps_enable ==
8801 	    (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop) {
8802 		/*
8803 		 * If DTrace itself is the provider, we're called with locks
8804 		 * already held.
8805 		 */
8806 		ASSERT(old == dtrace_provider);
8807 		ASSERT(dtrace_devi != NULL);
8808 		LCK_MTX_ASSERT(&dtrace_provider_lock, LCK_MTX_ASSERT_OWNED);
8809 		LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
8810 		self = 1;
8811 
8812 		if (dtrace_provider->dtpv_next != NULL) {
8813 			/*
8814 			 * There's another provider here; return failure.
8815 			 */
8816 			return (EBUSY);
8817 		}
8818 	} else {
8819 		lck_mtx_lock(&dtrace_provider_lock);
8820 		lck_mtx_lock(&mod_lock);
8821 		lck_mtx_lock(&dtrace_lock);
8822 	}
8823 
8824 	/*
8825 	 * If anyone has /dev/dtrace open, or if there are anonymous enabled
8826 	 * probes, we refuse to let providers slither away, unless this
8827 	 * provider has already been explicitly invalidated.
8828 	 */
8829 	if (!old->dtpv_defunct &&
8830 	    (dtrace_opens || (dtrace_anon.dta_state != NULL &&
8831 	    dtrace_anon.dta_state->dts_necbs > 0))) {
8832 		if (!self) {
8833 			lck_mtx_unlock(&dtrace_lock);
8834 			lck_mtx_unlock(&mod_lock);
8835 			lck_mtx_unlock(&dtrace_provider_lock);
8836 		}
8837 		return (EBUSY);
8838 	}
8839 
8840 	/*
8841 	 * Attempt to destroy the probes associated with this provider.
8842 	 */
8843 	if (old->dtpv_ecb_count!=0) {
8844 		/*
8845 		 * We have at least one ECB; we can't remove this provider.
8846 		 */
8847 		if (!self) {
8848 			lck_mtx_unlock(&dtrace_lock);
8849 			lck_mtx_unlock(&mod_lock);
8850 			lck_mtx_unlock(&dtrace_provider_lock);
8851 		}
8852 		return (EBUSY);
8853 	}
8854 
8855 	/*
8856 	 * All of the probes for this provider are disabled; we can safely
8857 	 * remove all of them from their hash chains and from the probe array.
8858 	 */
8859 	for (probe = dtrace_hash_lookup(dtrace_byprov, &template); probe != NULL;
8860 	    probe = *(DTRACE_HASHNEXT(dtrace_byprov, probe))) {
8861 		if (probe->dtpr_provider != old)
8862 			continue;
8863 
8864 		dtrace_probes[probe->dtpr_id - 1] = NULL;
8865 		old->dtpv_probe_count--;
8866 
8867 		dtrace_hash_remove(dtrace_bymod, probe);
8868 		dtrace_hash_remove(dtrace_byfunc, probe);
8869 		dtrace_hash_remove(dtrace_byname, probe);
8870 
8871 		if (first == NULL) {
8872 			first = probe;
8873 			probe->dtpr_nextmod = NULL;
8874 		} else {
8875 			/*
8876 			 * Use nextmod as the chain of probes to remove
8877 			 */
8878 			probe->dtpr_nextmod = first;
8879 			first = probe;
8880 		}
8881 	}
8882 
8883 	for (probe = first; probe != NULL; probe = next) {
8884 		next = probe->dtpr_nextmod;
8885 		dtrace_hash_remove(dtrace_byprov, probe);
8886 	}
8887 
8888 	/*
8889 	 * The provider's probes have been removed from the hash chains and
8890 	 * from the probe array.  Now issue a dtrace_sync() to be sure that
8891 	 * everyone has cleared out from any probe array processing.
8892 	 */
8893 	dtrace_sync();
8894 
8895 	for (probe = first; probe != NULL; probe = next) {
8896 		next = probe->dtpr_nextmod;
8897 
8898 		old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id,
8899 		    probe->dtpr_arg);
8900 		dtrace_strunref(probe->dtpr_mod);
8901 		dtrace_strunref(probe->dtpr_func);
8902 		dtrace_strunref(probe->dtpr_name);
8903 		vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1);
8904 		zfree(dtrace_probe_t_zone, probe);
8905 	}
8906 
8907 	if ((prev = dtrace_provider) == old) {
8908 		ASSERT(self || dtrace_devi == NULL);
8909 		ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL);
8910 		dtrace_provider = old->dtpv_next;
8911 	} else {
8912 		while (prev != NULL && prev->dtpv_next != old)
8913 			prev = prev->dtpv_next;
8914 
8915 		if (prev == NULL) {
8916 			panic("attempt to unregister non-existent "
8917 			    "dtrace provider %p\n", (void *)id);
8918 		}
8919 
8920 		prev->dtpv_next = old->dtpv_next;
8921 	}
8922 
8923 	dtrace_strunref(old->dtpv_name);
8924 
8925 	if (!self) {
8926 		lck_mtx_unlock(&dtrace_lock);
8927 		lck_mtx_unlock(&mod_lock);
8928 		lck_mtx_unlock(&dtrace_provider_lock);
8929 	}
8930 
8931 	kmem_free(old, sizeof (dtrace_provider_t));
8932 
8933 	return (0);
8934 }
8935 
8936 /*
8937  * Invalidate the specified provider.  All subsequent probe lookups for the
8938  * specified provider will fail, but its probes will not be removed.
8939  */
8940 void
dtrace_invalidate(dtrace_provider_id_t id)8941 dtrace_invalidate(dtrace_provider_id_t id)
8942 {
8943 	dtrace_provider_t *pvp = (dtrace_provider_t *)id;
8944 
8945 	ASSERT(pvp->dtpv_pops.dtps_enable !=
8946 	    (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop);
8947 
8948 	lck_mtx_lock(&dtrace_provider_lock);
8949 	lck_mtx_lock(&dtrace_lock);
8950 
8951 	pvp->dtpv_defunct = 1;
8952 
8953 	lck_mtx_unlock(&dtrace_lock);
8954 	lck_mtx_unlock(&dtrace_provider_lock);
8955 }
8956 
8957 /*
8958  * Indicate whether or not DTrace has attached.
8959  */
8960 int
dtrace_attached(void)8961 dtrace_attached(void)
8962 {
8963 	/*
8964 	 * dtrace_provider will be non-NULL iff the DTrace driver has
8965 	 * attached.  (It's non-NULL because DTrace is always itself a
8966 	 * provider.)
8967 	 */
8968 	return (dtrace_provider != NULL);
8969 }
8970 
8971 /*
8972  * Remove all the unenabled probes for the given provider.  This function is
8973  * not unlike dtrace_unregister(), except that it doesn't remove the provider
8974  * -- just as many of its associated probes as it can.
8975  */
8976 int
dtrace_condense(dtrace_provider_id_t id)8977 dtrace_condense(dtrace_provider_id_t id)
8978 {
8979 	dtrace_provider_t *prov = (dtrace_provider_t *)id;
8980 	dtrace_probe_t *probe, *first = NULL;
8981 	dtrace_probe_t template = {
8982 		.dtpr_provider = prov
8983 	};
8984 
8985 	/*
8986 	 * Make sure this isn't the dtrace provider itself.
8987 	 */
8988 	ASSERT(prov->dtpv_pops.dtps_enable !=
8989 	  (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop);
8990 
8991 	lck_mtx_lock(&dtrace_provider_lock);
8992 	lck_mtx_lock(&dtrace_lock);
8993 
8994 	/*
8995 	 * Attempt to destroy the probes associated with this provider.
8996 	 */
8997 	for (probe = dtrace_hash_lookup(dtrace_byprov, &template); probe != NULL;
8998 	    probe = *(DTRACE_HASHNEXT(dtrace_byprov, probe))) {
8999 
9000 		if (probe->dtpr_provider != prov)
9001 			continue;
9002 
9003 		if (probe->dtpr_ecb != NULL)
9004 			continue;
9005 
9006 		dtrace_probes[probe->dtpr_id - 1] = NULL;
9007 		prov->dtpv_probe_count--;
9008 
9009 		dtrace_hash_remove(dtrace_bymod, probe);
9010 		dtrace_hash_remove(dtrace_byfunc, probe);
9011 		dtrace_hash_remove(dtrace_byname, probe);
9012 
9013 		prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id,
9014 		    probe->dtpr_arg);
9015 		dtrace_strunref(probe->dtpr_mod);
9016 		dtrace_strunref(probe->dtpr_func);
9017 		dtrace_strunref(probe->dtpr_name);
9018 		if (first == NULL) {
9019 			first = probe;
9020 			probe->dtpr_nextmod = NULL;
9021 		} else {
9022 			/*
9023 			 * Use nextmod as the chain of probes to remove
9024 			 */
9025 			probe->dtpr_nextmod = first;
9026 			first = probe;
9027 		}
9028 	}
9029 
9030 	for (probe = first; probe != NULL; probe = first) {
9031 		first = probe->dtpr_nextmod;
9032 		dtrace_hash_remove(dtrace_byprov, probe);
9033 		vmem_free(dtrace_arena, (void *)((uintptr_t)probe->dtpr_id), 1);
9034 		zfree(dtrace_probe_t_zone, probe);
9035 	}
9036 
9037 	lck_mtx_unlock(&dtrace_lock);
9038 	lck_mtx_unlock(&dtrace_provider_lock);
9039 
9040 	return (0);
9041 }
9042 
9043 /*
9044  * DTrace Probe Management Functions
9045  *
9046  * The functions in this section perform the DTrace probe management,
9047  * including functions to create probes, look-up probes, and call into the
9048  * providers to request that probes be provided.  Some of these functions are
9049  * in the Provider-to-Framework API; these functions can be identified by the
9050  * fact that they are not declared "static".
9051  */
9052 
9053 /*
9054  * Create a probe with the specified module name, function name, and name.
9055  */
9056 dtrace_id_t
dtrace_probe_create(dtrace_provider_id_t prov,const char * mod,const char * func,const char * name,int aframes,void * arg)9057 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod,
9058     const char *func, const char *name, int aframes, void *arg)
9059 {
9060 	dtrace_probe_t *probe, **probes;
9061 	dtrace_provider_t *provider = (dtrace_provider_t *)prov;
9062 	dtrace_id_t id;
9063 
9064 	if (provider == dtrace_provider) {
9065 		LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
9066 	} else {
9067 		lck_mtx_lock(&dtrace_lock);
9068 	}
9069 
9070 	id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1,
9071 	    VM_BESTFIT | VM_SLEEP);
9072 
9073 	probe = zalloc_flags(dtrace_probe_t_zone, Z_WAITOK | Z_ZERO);
9074 
9075 	probe->dtpr_id = id;
9076 	probe->dtpr_gen = dtrace_probegen++;
9077 	probe->dtpr_mod = dtrace_strref(mod);
9078 	probe->dtpr_func = dtrace_strref(func);
9079 	probe->dtpr_name = dtrace_strref(name);
9080 	probe->dtpr_arg = arg;
9081 	probe->dtpr_aframes = aframes;
9082 	probe->dtpr_provider = provider;
9083 
9084 	dtrace_hash_add(dtrace_byprov, probe);
9085 	dtrace_hash_add(dtrace_bymod, probe);
9086 	dtrace_hash_add(dtrace_byfunc, probe);
9087 	dtrace_hash_add(dtrace_byname, probe);
9088 
9089 	if (id - 1 >= (dtrace_id_t)dtrace_nprobes) {
9090 		size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *);
9091 		size_t nsize = osize * 2;
9092 
9093 		probes = kmem_zalloc(nsize, KM_SLEEP);
9094 
9095 		dtrace_probe_t **oprobes = dtrace_probes;
9096 
9097 		bcopy(oprobes, probes, osize);
9098 		dtrace_membar_producer();
9099 		dtrace_probes = probes;
9100 
9101 		dtrace_sync();
9102 
9103 		/*
9104 		 * All CPUs are now seeing the new probes array; we can
9105 		 * safely free the old array.
9106 		 */
9107 		kmem_free(oprobes, osize);
9108 		dtrace_nprobes *= 2;
9109 
9110 		ASSERT(id - 1 < (dtrace_id_t)dtrace_nprobes);
9111 	}
9112 
9113 	ASSERT(dtrace_probes[id - 1] == NULL);
9114 	dtrace_probes[id - 1] = probe;
9115 	provider->dtpv_probe_count++;
9116 
9117 	if (provider != dtrace_provider)
9118 		lck_mtx_unlock(&dtrace_lock);
9119 
9120 	return (id);
9121 }
9122 
9123 static dtrace_probe_t *
dtrace_probe_lookup_id(dtrace_id_t id)9124 dtrace_probe_lookup_id(dtrace_id_t id)
9125 {
9126 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
9127 
9128 	if (id == 0 || id > (dtrace_id_t)dtrace_nprobes)
9129 		return (NULL);
9130 
9131 	return (dtrace_probes[id - 1]);
9132 }
9133 
9134 static int
dtrace_probe_lookup_match(dtrace_probe_t * probe,void * arg1,void * arg2)9135 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg1, void *arg2)
9136 {
9137 #pragma unused(arg2)
9138 	*((dtrace_id_t *)arg1) = probe->dtpr_id;
9139 
9140 	return (DTRACE_MATCH_DONE);
9141 }
9142 
9143 /*
9144  * Look up a probe based on provider and one or more of module name, function
9145  * name and probe name.
9146  */
9147 dtrace_id_t
dtrace_probe_lookup(dtrace_provider_id_t prid,const char * mod,const char * func,const char * name)9148 dtrace_probe_lookup(dtrace_provider_id_t prid, const char *mod,
9149     const char *func, const char *name)
9150 {
9151 	dtrace_probekey_t pkey;
9152 	dtrace_id_t id;
9153 	int match;
9154 
9155 	lck_mtx_lock(&dtrace_lock);
9156 
9157 	pkey.dtpk_prov = dtrace_strref(((dtrace_provider_t *)prid)->dtpv_name);
9158 	pkey.dtpk_pmatch = &dtrace_match_string;
9159 	pkey.dtpk_mod = dtrace_strref(mod);
9160 	pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul;
9161 	pkey.dtpk_func = dtrace_strref(func);
9162 	pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul;
9163 	pkey.dtpk_name = dtrace_strref(name);
9164 	pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul;
9165 	pkey.dtpk_id = DTRACE_IDNONE;
9166 
9167 	match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0,
9168 	    dtrace_probe_lookup_match, &id, NULL);
9169 
9170 	dtrace_probekey_release(&pkey);
9171 
9172 	lck_mtx_unlock(&dtrace_lock);
9173 
9174 	ASSERT(match == 1 || match == 0);
9175 	return (match ? id : 0);
9176 }
9177 
9178 /*
9179  * Returns the probe argument associated with the specified probe.
9180  */
9181 void *
dtrace_probe_arg(dtrace_provider_id_t id,dtrace_id_t pid)9182 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid)
9183 {
9184 	dtrace_probe_t *probe;
9185 	void *rval = NULL;
9186 
9187 	lck_mtx_lock(&dtrace_lock);
9188 
9189 	if ((probe = dtrace_probe_lookup_id(pid)) != NULL &&
9190 	    probe->dtpr_provider == (dtrace_provider_t *)id)
9191 		rval = probe->dtpr_arg;
9192 
9193 	lck_mtx_unlock(&dtrace_lock);
9194 
9195 	return (rval);
9196 }
9197 
9198 /*
9199  * Copy a probe into a probe description.
9200  */
9201 static void
dtrace_probe_description(const dtrace_probe_t * prp,dtrace_probedesc_t * pdp)9202 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp)
9203 {
9204 	bzero(pdp, sizeof (dtrace_probedesc_t));
9205 	pdp->dtpd_id = prp->dtpr_id;
9206 
9207 	/* APPLE NOTE: Darwin employs size bounded string operation. */
9208 	(void) strlcpy(pdp->dtpd_provider,
9209 	    prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN);
9210 
9211 	(void) strlcpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN);
9212 	(void) strlcpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN);
9213 	(void) strlcpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN);
9214 }
9215 
9216 /*
9217  * Called to indicate that a probe -- or probes -- should be provided by a
9218  * specfied provider.  If the specified description is NULL, the provider will
9219  * be told to provide all of its probes.  (This is done whenever a new
9220  * consumer comes along, or whenever a retained enabling is to be matched.) If
9221  * the specified description is non-NULL, the provider is given the
9222  * opportunity to dynamically provide the specified probe, allowing providers
9223  * to support the creation of probes on-the-fly.  (So-called _autocreated_
9224  * probes.)  If the provider is NULL, the operations will be applied to all
9225  * providers; if the provider is non-NULL the operations will only be applied
9226  * to the specified provider.  The dtrace_provider_lock must be held, and the
9227  * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation
9228  * will need to grab the dtrace_lock when it reenters the framework through
9229  * dtrace_probe_lookup(), dtrace_probe_create(), etc.
9230  */
9231 static void
dtrace_probe_provide(dtrace_probedesc_t * desc,dtrace_provider_t * prv)9232 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv)
9233 {
9234 	struct modctl *ctl;
9235 	int all = 0;
9236 
9237 	LCK_MTX_ASSERT(&dtrace_provider_lock, LCK_MTX_ASSERT_OWNED);
9238 
9239 	if (prv == NULL) {
9240 		all = 1;
9241 		prv = dtrace_provider;
9242 	}
9243 
9244 	do {
9245 		/*
9246 		 * First, call the blanket provide operation.
9247 		 */
9248 		prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc);
9249 
9250 		/*
9251 		 * Now call the per-module provide operation.  We will grab
9252 		 * mod_lock to prevent the list from being modified.  Note
9253 		 * that this also prevents the mod_busy bits from changing.
9254 		 * (mod_busy can only be changed with mod_lock held.)
9255 		 */
9256 		lck_mtx_lock(&mod_lock);
9257 
9258 		ctl = dtrace_modctl_list;
9259 		while (ctl) {
9260 			prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
9261 			ctl = ctl->mod_next;
9262 		}
9263 
9264 		lck_mtx_unlock(&mod_lock);
9265 	} while (all && (prv = prv->dtpv_next) != NULL);
9266 }
9267 
9268 /*
9269  * Iterate over each probe, and call the Framework-to-Provider API function
9270  * denoted by offs.
9271  */
9272 static void
dtrace_probe_foreach(uintptr_t offs)9273 dtrace_probe_foreach(uintptr_t offs)
9274 {
9275 	dtrace_provider_t *prov;
9276 	void (*func)(void *, dtrace_id_t, void *);
9277 	dtrace_probe_t *probe;
9278 	dtrace_icookie_t cookie;
9279 	int i;
9280 
9281 	/*
9282 	 * We disable interrupts to walk through the probe array.  This is
9283 	 * safe -- the dtrace_sync() in dtrace_unregister() assures that we
9284 	 * won't see stale data.
9285 	 */
9286 	cookie = dtrace_interrupt_disable();
9287 
9288 	for (i = 0; i < dtrace_nprobes; i++) {
9289 		if ((probe = dtrace_probes[i]) == NULL)
9290 			continue;
9291 
9292 		if (probe->dtpr_ecb == NULL) {
9293 			/*
9294 			 * This probe isn't enabled -- don't call the function.
9295 			 */
9296 			continue;
9297 		}
9298 
9299 		prov = probe->dtpr_provider;
9300 		func = *((void(**)(void *, dtrace_id_t, void *))
9301 		    ((uintptr_t)&prov->dtpv_pops + offs));
9302 
9303 		func(prov->dtpv_arg, i + 1, probe->dtpr_arg);
9304 	}
9305 
9306 	dtrace_interrupt_enable(cookie);
9307 }
9308 
9309 static int
dtrace_probe_enable(const dtrace_probedesc_t * desc,dtrace_enabling_t * enab,dtrace_ecbdesc_t * ep)9310 dtrace_probe_enable(const dtrace_probedesc_t *desc, dtrace_enabling_t *enab, dtrace_ecbdesc_t *ep)
9311 {
9312 	dtrace_probekey_t pkey;
9313 	uint32_t priv;
9314 	uid_t uid;
9315 	zoneid_t zoneid;
9316 	int err;
9317 
9318 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
9319 
9320 	dtrace_ecb_create_cache = NULL;
9321 
9322 	if (desc == NULL) {
9323 		/*
9324 		 * If we're passed a NULL description, we're being asked to
9325 		 * create an ECB with a NULL probe.
9326 		 */
9327 		(void) dtrace_ecb_create_enable(NULL, enab, ep);
9328 		return (0);
9329 	}
9330 
9331 	dtrace_probekey(desc, &pkey);
9332 	dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred,
9333 	    &priv, &uid, &zoneid);
9334 
9335 	err = dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, enab, ep);
9336 
9337 	dtrace_probekey_release(&pkey);
9338 
9339 	return err;
9340 }
9341 
9342 /*
9343  * DTrace Helper Provider Functions
9344  */
9345 static void
dtrace_dofattr2attr(dtrace_attribute_t * attr,const dof_attr_t dofattr)9346 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr)
9347 {
9348 	attr->dtat_name = DOF_ATTR_NAME(dofattr);
9349 	attr->dtat_data = DOF_ATTR_DATA(dofattr);
9350 	attr->dtat_class = DOF_ATTR_CLASS(dofattr);
9351 }
9352 
9353 static void
dtrace_dofprov2hprov(dtrace_helper_provdesc_t * hprov,const dof_provider_t * dofprov,char * strtab)9354 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov,
9355     const dof_provider_t *dofprov, char *strtab)
9356 {
9357 	hprov->dthpv_provname = strtab + dofprov->dofpv_name;
9358 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider,
9359 	    dofprov->dofpv_provattr);
9360 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod,
9361 	    dofprov->dofpv_modattr);
9362 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func,
9363 	    dofprov->dofpv_funcattr);
9364 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name,
9365 	    dofprov->dofpv_nameattr);
9366 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args,
9367 	    dofprov->dofpv_argsattr);
9368 }
9369 
9370 static void
dtrace_helper_provide_one(dof_helper_t * dhp,dof_sec_t * sec,proc_t * p)9371 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, proc_t *p)
9372 {
9373 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9374 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
9375 	dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
9376 	dof_provider_t *provider;
9377 	dof_probe_t *probe;
9378 	uint32_t *off, *enoff;
9379 	uint8_t *arg;
9380 	char *strtab;
9381 	uint_t i, nprobes;
9382 	dtrace_helper_provdesc_t dhpv;
9383 	dtrace_helper_probedesc_t dhpb;
9384 	dtrace_meta_t *meta = dtrace_meta_pid;
9385 	dtrace_mops_t *mops = &meta->dtm_mops;
9386 	void *parg;
9387 
9388 	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
9389 	str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9390 	    provider->dofpv_strtab * dof->dofh_secsize);
9391 	prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9392 	    provider->dofpv_probes * dof->dofh_secsize);
9393 	arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9394 	    provider->dofpv_prargs * dof->dofh_secsize);
9395 	off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9396 	    provider->dofpv_proffs * dof->dofh_secsize);
9397 
9398 	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
9399 	off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset);
9400 	arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
9401 	enoff = NULL;
9402 
9403 	/*
9404 	 * See dtrace_helper_provider_validate().
9405 	 */
9406 	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
9407 	    provider->dofpv_prenoffs != DOF_SECT_NONE) {
9408 		enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9409 		    provider->dofpv_prenoffs * dof->dofh_secsize);
9410 		enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset);
9411 	}
9412 
9413 	nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
9414 
9415 	/*
9416 	 * Create the provider.
9417 	 */
9418 	dtrace_dofprov2hprov(&dhpv, provider, strtab);
9419 
9420 	if ((parg = mops->dtms_provide_proc(meta->dtm_arg, &dhpv, p)) == NULL)
9421 		return;
9422 
9423 	meta->dtm_count++;
9424 
9425 	/*
9426 	 * Create the probes.
9427 	 */
9428 	for (i = 0; i < nprobes; i++) {
9429 		probe = (dof_probe_t *)(uintptr_t)(daddr +
9430 		    prb_sec->dofs_offset + i * prb_sec->dofs_entsize);
9431 
9432 		dhpb.dthpb_mod = dhp->dofhp_mod;
9433 		dhpb.dthpb_func = strtab + probe->dofpr_func;
9434 		dhpb.dthpb_name = strtab + probe->dofpr_name;
9435 #if !defined(__APPLE__)
9436 		dhpb.dthpb_base = probe->dofpr_addr;
9437 #else
9438 		dhpb.dthpb_base = dhp->dofhp_addr; /* FIXME: James, why? */
9439 #endif
9440 		dhpb.dthpb_offs = (int32_t *)(off + probe->dofpr_offidx);
9441 		dhpb.dthpb_noffs = probe->dofpr_noffs;
9442 		if (enoff != NULL) {
9443 			dhpb.dthpb_enoffs = (int32_t *)(enoff + probe->dofpr_enoffidx);
9444 			dhpb.dthpb_nenoffs = probe->dofpr_nenoffs;
9445 		} else {
9446 			dhpb.dthpb_enoffs = NULL;
9447 			dhpb.dthpb_nenoffs = 0;
9448 		}
9449 		dhpb.dthpb_args = arg + probe->dofpr_argidx;
9450 		dhpb.dthpb_nargc = probe->dofpr_nargc;
9451 		dhpb.dthpb_xargc = probe->dofpr_xargc;
9452 		dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv;
9453 		dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv;
9454 
9455 		mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb);
9456 	}
9457 
9458 	/*
9459 	 * Since we just created probes, we need to match our enablings
9460 	 * against those, with a precondition knowing that we have only
9461 	 * added probes from this provider
9462 	 */
9463 	char *prov_name = mops->dtms_provider_name(parg);
9464 	ASSERT(prov_name != NULL);
9465 	dtrace_match_cond_t cond = {dtrace_cond_provider_match, (void*)prov_name};
9466 
9467 	dtrace_enabling_matchall_with_cond(&cond);
9468 }
9469 
9470 static void
dtrace_helper_provide(dof_helper_t * dhp,proc_t * p)9471 dtrace_helper_provide(dof_helper_t *dhp, proc_t *p)
9472 {
9473 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9474 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
9475 	uint32_t i;
9476 
9477 	LCK_MTX_ASSERT(&dtrace_meta_lock, LCK_MTX_ASSERT_OWNED);
9478 
9479 	for (i = 0; i < dof->dofh_secnum; i++) {
9480 		dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
9481 		    dof->dofh_secoff + i * dof->dofh_secsize);
9482 
9483 		if (sec->dofs_type != DOF_SECT_PROVIDER)
9484 			continue;
9485 
9486 		dtrace_helper_provide_one(dhp, sec, p);
9487 	}
9488 }
9489 
9490 static void
dtrace_helper_provider_remove_one(dof_helper_t * dhp,dof_sec_t * sec,proc_t * p)9491 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, proc_t *p)
9492 {
9493 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9494 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
9495 	dof_sec_t *str_sec;
9496 	dof_provider_t *provider;
9497 	char *strtab;
9498 	dtrace_helper_provdesc_t dhpv;
9499 	dtrace_meta_t *meta = dtrace_meta_pid;
9500 	dtrace_mops_t *mops = &meta->dtm_mops;
9501 
9502 	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
9503 	str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9504 	    provider->dofpv_strtab * dof->dofh_secsize);
9505 
9506 	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
9507 
9508 	/*
9509 	 * Create the provider.
9510 	 */
9511 	dtrace_dofprov2hprov(&dhpv, provider, strtab);
9512 
9513 	mops->dtms_remove_proc(meta->dtm_arg, &dhpv, p);
9514 
9515 	meta->dtm_count--;
9516 }
9517 
9518 static void
dtrace_helper_provider_remove(dof_helper_t * dhp,proc_t * p)9519 dtrace_helper_provider_remove(dof_helper_t *dhp, proc_t *p)
9520 {
9521 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9522 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
9523 	uint32_t i;
9524 
9525 	LCK_MTX_ASSERT(&dtrace_meta_lock, LCK_MTX_ASSERT_OWNED);
9526 
9527 	for (i = 0; i < dof->dofh_secnum; i++) {
9528 		dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
9529 		    dof->dofh_secoff + i * dof->dofh_secsize);
9530 
9531 		if (sec->dofs_type != DOF_SECT_PROVIDER)
9532 			continue;
9533 
9534 		dtrace_helper_provider_remove_one(dhp, sec, p);
9535 	}
9536 }
9537 
9538 /*
9539  * DTrace Meta Provider-to-Framework API Functions
9540  *
9541  * These functions implement the Meta Provider-to-Framework API, as described
9542  * in <sys/dtrace.h>.
9543  */
9544 int
dtrace_meta_register(const char * name,const dtrace_mops_t * mops,void * arg,dtrace_meta_provider_id_t * idp)9545 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg,
9546     dtrace_meta_provider_id_t *idp)
9547 {
9548 	dtrace_meta_t *meta;
9549 	dtrace_helpers_t *help, *next;
9550 	uint_t i;
9551 
9552 	*idp = DTRACE_METAPROVNONE;
9553 
9554 	/*
9555 	 * We strictly don't need the name, but we hold onto it for
9556 	 * debuggability. All hail error queues!
9557 	 */
9558 	if (name == NULL) {
9559 		cmn_err(CE_WARN, "failed to register meta-provider: "
9560 		    "invalid name");
9561 		return (EINVAL);
9562 	}
9563 
9564 	if (mops == NULL ||
9565 	    mops->dtms_create_probe == NULL ||
9566 	    mops->dtms_provide_proc == NULL ||
9567 	    mops->dtms_remove_proc == NULL) {
9568 		cmn_err(CE_WARN, "failed to register meta-register %s: "
9569 		    "invalid ops", name);
9570 		return (EINVAL);
9571 	}
9572 
9573 	meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP);
9574 	meta->dtm_mops = *mops;
9575 	meta->dtm_arg = arg;
9576 
9577 	lck_mtx_lock(&dtrace_meta_lock);
9578 	lck_mtx_lock(&dtrace_lock);
9579 
9580 	if (dtrace_meta_pid != NULL) {
9581 		lck_mtx_unlock(&dtrace_lock);
9582 		lck_mtx_unlock(&dtrace_meta_lock);
9583 		cmn_err(CE_WARN, "failed to register meta-register %s: "
9584 		    "user-land meta-provider exists", name);
9585 		kmem_free(meta, sizeof (dtrace_meta_t));
9586 		return (EINVAL);
9587 	}
9588 
9589 	meta->dtm_name = dtrace_strref(name);
9590 
9591 	dtrace_meta_pid = meta;
9592 	*idp = (dtrace_meta_provider_id_t)meta;
9593 
9594 	/*
9595 	 * If there are providers and probes ready to go, pass them
9596 	 * off to the new meta provider now.
9597 	 */
9598 
9599 	help = dtrace_deferred_pid;
9600 	dtrace_deferred_pid = NULL;
9601 
9602 	lck_mtx_unlock(&dtrace_lock);
9603 
9604 	while (help != NULL) {
9605 		for (i = 0; i < help->dthps_nprovs; i++) {
9606 			proc_t *p = proc_find(help->dthps_pid);
9607 			if (p == PROC_NULL)
9608 				continue;
9609 			dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
9610 			    p);
9611 			proc_rele(p);
9612 		}
9613 
9614 		next = help->dthps_next;
9615 		help->dthps_next = NULL;
9616 		help->dthps_prev = NULL;
9617 		help->dthps_deferred = 0;
9618 		help = next;
9619 	}
9620 
9621 	lck_mtx_unlock(&dtrace_meta_lock);
9622 
9623 	return (0);
9624 }
9625 
9626 int
dtrace_meta_unregister(dtrace_meta_provider_id_t id)9627 dtrace_meta_unregister(dtrace_meta_provider_id_t id)
9628 {
9629 	dtrace_meta_t **pp, *old = (dtrace_meta_t *)id;
9630 
9631 	lck_mtx_lock(&dtrace_meta_lock);
9632 	lck_mtx_lock(&dtrace_lock);
9633 
9634 	if (old == dtrace_meta_pid) {
9635 		pp = &dtrace_meta_pid;
9636 	} else {
9637 		panic("attempt to unregister non-existent "
9638 		    "dtrace meta-provider %p\n", (void *)old);
9639 	}
9640 
9641 	if (old->dtm_count != 0) {
9642 		lck_mtx_unlock(&dtrace_lock);
9643 		lck_mtx_unlock(&dtrace_meta_lock);
9644 		return (EBUSY);
9645 	}
9646 
9647 	*pp = NULL;
9648 
9649 	dtrace_strunref(old->dtm_name);
9650 
9651 	lck_mtx_unlock(&dtrace_lock);
9652 	lck_mtx_unlock(&dtrace_meta_lock);
9653 
9654 	kmem_free(old, sizeof (dtrace_meta_t));
9655 
9656 	return (0);
9657 }
9658 
9659 
9660 /*
9661  * DTrace DIF Object Functions
9662  */
9663 static int
dtrace_difo_err(uint_t pc,const char * format,...)9664 dtrace_difo_err(uint_t pc, const char *format, ...)
9665 {
9666 	if (dtrace_err_verbose) {
9667 		va_list alist;
9668 
9669 		(void) uprintf("dtrace DIF object error: [%u]: ", pc);
9670 		va_start(alist, format);
9671 		(void) vuprintf(format, alist);
9672 		va_end(alist);
9673 	}
9674 
9675 #ifdef DTRACE_ERRDEBUG
9676 	dtrace_errdebug(format);
9677 #endif
9678 	return (1);
9679 }
9680 
9681 /*
9682  * Validate a DTrace DIF object by checking the IR instructions.  The following
9683  * rules are currently enforced by dtrace_difo_validate():
9684  *
9685  * 1. Each instruction must have a valid opcode
9686  * 2. Each register, string, variable, or subroutine reference must be valid
9687  * 3. No instruction can modify register %r0 (must be zero)
9688  * 4. All instruction reserved bits must be set to zero
9689  * 5. The last instruction must be a "ret" instruction
9690  * 6. All branch targets must reference a valid instruction _after_ the branch
9691  */
9692 static int
dtrace_difo_validate(dtrace_difo_t * dp,dtrace_vstate_t * vstate,uint_t nregs,cred_t * cr)9693 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs,
9694     cred_t *cr)
9695 {
9696 	int err = 0;
9697 	uint_t i;
9698 
9699 	int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
9700 	int kcheckload;
9701 	uint_t pc;
9702 	int maxglobal = -1, maxlocal = -1, maxtlocal = -1;
9703 
9704 	kcheckload = cr == NULL ||
9705 	    (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0;
9706 
9707 	dp->dtdo_destructive = 0;
9708 
9709 	for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
9710 		dif_instr_t instr = dp->dtdo_buf[pc];
9711 
9712 		uint_t r1 = DIF_INSTR_R1(instr);
9713 		uint_t r2 = DIF_INSTR_R2(instr);
9714 		uint_t rd = DIF_INSTR_RD(instr);
9715 		uint_t rs = DIF_INSTR_RS(instr);
9716 		uint_t label = DIF_INSTR_LABEL(instr);
9717 		uint_t v = DIF_INSTR_VAR(instr);
9718 		uint_t subr = DIF_INSTR_SUBR(instr);
9719 		uint_t type = DIF_INSTR_TYPE(instr);
9720 		uint_t op = DIF_INSTR_OP(instr);
9721 
9722 		switch (op) {
9723 		case DIF_OP_OR:
9724 		case DIF_OP_XOR:
9725 		case DIF_OP_AND:
9726 		case DIF_OP_SLL:
9727 		case DIF_OP_SRL:
9728 		case DIF_OP_SRA:
9729 		case DIF_OP_SUB:
9730 		case DIF_OP_ADD:
9731 		case DIF_OP_MUL:
9732 		case DIF_OP_SDIV:
9733 		case DIF_OP_UDIV:
9734 		case DIF_OP_SREM:
9735 		case DIF_OP_UREM:
9736 		case DIF_OP_COPYS:
9737 			if (r1 >= nregs)
9738 				err += efunc(pc, "invalid register %u\n", r1);
9739 			if (r2 >= nregs)
9740 				err += efunc(pc, "invalid register %u\n", r2);
9741 			if (rd >= nregs)
9742 				err += efunc(pc, "invalid register %u\n", rd);
9743 			if (rd == 0)
9744 				err += efunc(pc, "cannot write to %%r0\n");
9745 			break;
9746 		case DIF_OP_NOT:
9747 		case DIF_OP_MOV:
9748 		case DIF_OP_ALLOCS:
9749 			if (r1 >= nregs)
9750 				err += efunc(pc, "invalid register %u\n", r1);
9751 			if (r2 != 0)
9752 				err += efunc(pc, "non-zero reserved bits\n");
9753 			if (rd >= nregs)
9754 				err += efunc(pc, "invalid register %u\n", rd);
9755 			if (rd == 0)
9756 				err += efunc(pc, "cannot write to %%r0\n");
9757 			break;
9758 		case DIF_OP_LDSB:
9759 		case DIF_OP_LDSH:
9760 		case DIF_OP_LDSW:
9761 		case DIF_OP_LDUB:
9762 		case DIF_OP_LDUH:
9763 		case DIF_OP_LDUW:
9764 		case DIF_OP_LDX:
9765 			if (r1 >= nregs)
9766 				err += efunc(pc, "invalid register %u\n", r1);
9767 			if (r2 != 0)
9768 				err += efunc(pc, "non-zero reserved bits\n");
9769 			if (rd >= nregs)
9770 				err += efunc(pc, "invalid register %u\n", rd);
9771 			if (rd == 0)
9772 				err += efunc(pc, "cannot write to %%r0\n");
9773 			if (kcheckload)
9774 				dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op +
9775 				    DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd);
9776 			break;
9777 		case DIF_OP_RLDSB:
9778 		case DIF_OP_RLDSH:
9779 		case DIF_OP_RLDSW:
9780 		case DIF_OP_RLDUB:
9781 		case DIF_OP_RLDUH:
9782 		case DIF_OP_RLDUW:
9783 		case DIF_OP_RLDX:
9784 			if (r1 >= nregs)
9785 				err += efunc(pc, "invalid register %u\n", r1);
9786 			if (r2 != 0)
9787 				err += efunc(pc, "non-zero reserved bits\n");
9788 			if (rd >= nregs)
9789 				err += efunc(pc, "invalid register %u\n", rd);
9790 			if (rd == 0)
9791 				err += efunc(pc, "cannot write to %%r0\n");
9792 			break;
9793 		case DIF_OP_ULDSB:
9794 		case DIF_OP_ULDSH:
9795 		case DIF_OP_ULDSW:
9796 		case DIF_OP_ULDUB:
9797 		case DIF_OP_ULDUH:
9798 		case DIF_OP_ULDUW:
9799 		case DIF_OP_ULDX:
9800 			if (r1 >= nregs)
9801 				err += efunc(pc, "invalid register %u\n", r1);
9802 			if (r2 != 0)
9803 				err += efunc(pc, "non-zero reserved bits\n");
9804 			if (rd >= nregs)
9805 				err += efunc(pc, "invalid register %u\n", rd);
9806 			if (rd == 0)
9807 				err += efunc(pc, "cannot write to %%r0\n");
9808 			break;
9809 		case DIF_OP_STB:
9810 		case DIF_OP_STH:
9811 		case DIF_OP_STW:
9812 		case DIF_OP_STX:
9813 			if (r1 >= nregs)
9814 				err += efunc(pc, "invalid register %u\n", r1);
9815 			if (r2 != 0)
9816 				err += efunc(pc, "non-zero reserved bits\n");
9817 			if (rd >= nregs)
9818 				err += efunc(pc, "invalid register %u\n", rd);
9819 			if (rd == 0)
9820 				err += efunc(pc, "cannot write to 0 address\n");
9821 			break;
9822 		case DIF_OP_CMP:
9823 		case DIF_OP_SCMP:
9824 			if (r1 >= nregs)
9825 				err += efunc(pc, "invalid register %u\n", r1);
9826 			if (r2 >= nregs)
9827 				err += efunc(pc, "invalid register %u\n", r2);
9828 			if (rd != 0)
9829 				err += efunc(pc, "non-zero reserved bits\n");
9830 			break;
9831 		case DIF_OP_TST:
9832 			if (r1 >= nregs)
9833 				err += efunc(pc, "invalid register %u\n", r1);
9834 			if (r2 != 0 || rd != 0)
9835 				err += efunc(pc, "non-zero reserved bits\n");
9836 			break;
9837 		case DIF_OP_BA:
9838 		case DIF_OP_BE:
9839 		case DIF_OP_BNE:
9840 		case DIF_OP_BG:
9841 		case DIF_OP_BGU:
9842 		case DIF_OP_BGE:
9843 		case DIF_OP_BGEU:
9844 		case DIF_OP_BL:
9845 		case DIF_OP_BLU:
9846 		case DIF_OP_BLE:
9847 		case DIF_OP_BLEU:
9848 			if (label >= dp->dtdo_len) {
9849 				err += efunc(pc, "invalid branch target %u\n",
9850 				    label);
9851 			}
9852 			if (label <= pc) {
9853 				err += efunc(pc, "backward branch to %u\n",
9854 				    label);
9855 			}
9856 			break;
9857 		case DIF_OP_RET:
9858 			if (r1 != 0 || r2 != 0)
9859 				err += efunc(pc, "non-zero reserved bits\n");
9860 			if (rd >= nregs)
9861 				err += efunc(pc, "invalid register %u\n", rd);
9862 			break;
9863 		case DIF_OP_NOP:
9864 		case DIF_OP_POPTS:
9865 		case DIF_OP_FLUSHTS:
9866 			if (r1 != 0 || r2 != 0 || rd != 0)
9867 				err += efunc(pc, "non-zero reserved bits\n");
9868 			break;
9869 		case DIF_OP_SETX:
9870 			if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) {
9871 				err += efunc(pc, "invalid integer ref %u\n",
9872 				    DIF_INSTR_INTEGER(instr));
9873 			}
9874 			if (rd >= nregs)
9875 				err += efunc(pc, "invalid register %u\n", rd);
9876 			if (rd == 0)
9877 				err += efunc(pc, "cannot write to %%r0\n");
9878 			break;
9879 		case DIF_OP_SETS:
9880 			if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) {
9881 				err += efunc(pc, "invalid string ref %u\n",
9882 				    DIF_INSTR_STRING(instr));
9883 			}
9884 			if (rd >= nregs)
9885 				err += efunc(pc, "invalid register %u\n", rd);
9886 			if (rd == 0)
9887 				err += efunc(pc, "cannot write to %%r0\n");
9888 			break;
9889 		case DIF_OP_LDGA:
9890 		case DIF_OP_LDTA:
9891 			if (r1 > DIF_VAR_ARRAY_MAX)
9892 				err += efunc(pc, "invalid array %u\n", r1);
9893 			if (r2 >= nregs)
9894 				err += efunc(pc, "invalid register %u\n", r2);
9895 			if (rd >= nregs)
9896 				err += efunc(pc, "invalid register %u\n", rd);
9897 			if (rd == 0)
9898 				err += efunc(pc, "cannot write to %%r0\n");
9899 			break;
9900 		case DIF_OP_LDGS:
9901 		case DIF_OP_LDTS:
9902 		case DIF_OP_LDLS:
9903 		case DIF_OP_LDGAA:
9904 		case DIF_OP_LDTAA:
9905 			if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX)
9906 				err += efunc(pc, "invalid variable %u\n", v);
9907 			if (rd >= nregs)
9908 				err += efunc(pc, "invalid register %u\n", rd);
9909 			if (rd == 0)
9910 				err += efunc(pc, "cannot write to %%r0\n");
9911 			break;
9912 		case DIF_OP_STGS:
9913 		case DIF_OP_STTS:
9914 		case DIF_OP_STLS:
9915 		case DIF_OP_STGAA:
9916 		case DIF_OP_STTAA:
9917 			if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX)
9918 				err += efunc(pc, "invalid variable %u\n", v);
9919 			if (rs >= nregs)
9920 				err += efunc(pc, "invalid register %u\n", rd);
9921 			break;
9922 		case DIF_OP_CALL:
9923 			if (subr > DIF_SUBR_MAX &&
9924 			   !(subr >= DIF_SUBR_APPLE_MIN && subr <= DIF_SUBR_APPLE_MAX))
9925 				err += efunc(pc, "invalid subr %u\n", subr);
9926 			if (rd >= nregs)
9927 				err += efunc(pc, "invalid register %u\n", rd);
9928 			if (rd == 0)
9929 				err += efunc(pc, "cannot write to %%r0\n");
9930 
9931 			switch (subr) {
9932 			case DIF_SUBR_COPYOUT:
9933 			case DIF_SUBR_COPYOUTSTR:
9934 			case DIF_SUBR_KDEBUG_TRACE:
9935 			case DIF_SUBR_KDEBUG_TRACE_STRING:
9936 			case DIF_SUBR_PHYSMEM_READ:
9937 			case DIF_SUBR_PHYSMEM_WRITE:
9938 			case DIF_SUBR_LIVEDUMP:
9939 				dp->dtdo_destructive = 1;
9940 				break;
9941 			default:
9942 				break;
9943 			}
9944 			break;
9945 		case DIF_OP_PUSHTR:
9946 			if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF)
9947 				err += efunc(pc, "invalid ref type %u\n", type);
9948 			if (r2 >= nregs)
9949 				err += efunc(pc, "invalid register %u\n", r2);
9950 			if (rs >= nregs)
9951 				err += efunc(pc, "invalid register %u\n", rs);
9952 			break;
9953 		case DIF_OP_PUSHTV:
9954 			if (type != DIF_TYPE_CTF)
9955 				err += efunc(pc, "invalid val type %u\n", type);
9956 			if (r2 >= nregs)
9957 				err += efunc(pc, "invalid register %u\n", r2);
9958 			if (rs >= nregs)
9959 				err += efunc(pc, "invalid register %u\n", rs);
9960 			break;
9961 		case DIF_OP_STRIP:
9962 			if (r1 >= nregs)
9963 				err += efunc(pc, "invalid register %u\n", r1);
9964 			if (!dtrace_is_valid_ptrauth_key(r2))
9965 				err += efunc(pc, "invalid key\n");
9966 			if (rd >= nregs)
9967 				err += efunc(pc, "invalid register %u\n", rd);
9968 			if (rd == 0)
9969 				err += efunc(pc, "cannot write to %%r0\n");
9970 			break;
9971 		default:
9972 			err += efunc(pc, "invalid opcode %u\n",
9973 			    DIF_INSTR_OP(instr));
9974 		}
9975 	}
9976 
9977 	if (dp->dtdo_len != 0 &&
9978 	    DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) {
9979 		err += efunc(dp->dtdo_len - 1,
9980 		    "expected 'ret' as last DIF instruction\n");
9981 	}
9982 
9983 	if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) {
9984 		/*
9985 		 * If we're not returning by reference, the size must be either
9986 		 * 0 or the size of one of the base types.
9987 		 */
9988 		switch (dp->dtdo_rtype.dtdt_size) {
9989 		case 0:
9990 		case sizeof (uint8_t):
9991 		case sizeof (uint16_t):
9992 		case sizeof (uint32_t):
9993 		case sizeof (uint64_t):
9994 			break;
9995 
9996 		default:
9997 			err += efunc(dp->dtdo_len - 1, "bad return size\n");
9998 		}
9999 	}
10000 
10001 	for (i = 0; i < dp->dtdo_varlen && err == 0; i++) {
10002 		dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL;
10003 		dtrace_diftype_t *vt, *et;
10004 		uint_t id;
10005 		int ndx;
10006 
10007 		if (v->dtdv_scope != DIFV_SCOPE_GLOBAL &&
10008 		    v->dtdv_scope != DIFV_SCOPE_THREAD &&
10009 		    v->dtdv_scope != DIFV_SCOPE_LOCAL) {
10010 			err += efunc(i, "unrecognized variable scope %d\n",
10011 			    v->dtdv_scope);
10012 			break;
10013 		}
10014 
10015 		if (v->dtdv_kind != DIFV_KIND_ARRAY &&
10016 		    v->dtdv_kind != DIFV_KIND_SCALAR) {
10017 			err += efunc(i, "unrecognized variable type %d\n",
10018 			    v->dtdv_kind);
10019 			break;
10020 		}
10021 
10022 		if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) {
10023 			err += efunc(i, "%d exceeds variable id limit\n", id);
10024 			break;
10025 		}
10026 
10027 		if (id < DIF_VAR_OTHER_UBASE)
10028 			continue;
10029 
10030 		/*
10031 		 * For user-defined variables, we need to check that this
10032 		 * definition is identical to any previous definition that we
10033 		 * encountered.
10034 		 */
10035 		ndx = id - DIF_VAR_OTHER_UBASE;
10036 
10037 		switch (v->dtdv_scope) {
10038 		case DIFV_SCOPE_GLOBAL:
10039 			if (maxglobal == -1 || ndx > maxglobal)
10040 				maxglobal = ndx;
10041 
10042 			if (ndx < vstate->dtvs_nglobals) {
10043 				dtrace_statvar_t *svar;
10044 
10045 				if ((svar = vstate->dtvs_globals[ndx]) != NULL)
10046 					existing = &svar->dtsv_var;
10047 			}
10048 
10049 			break;
10050 
10051 		case DIFV_SCOPE_THREAD:
10052 			if (maxtlocal == -1 || ndx > maxtlocal)
10053 				maxtlocal = ndx;
10054 
10055 			if (ndx < vstate->dtvs_ntlocals)
10056 				existing = &vstate->dtvs_tlocals[ndx];
10057 			break;
10058 
10059 		case DIFV_SCOPE_LOCAL:
10060 			if (maxlocal == -1 || ndx > maxlocal)
10061 				maxlocal = ndx;
10062 			if (ndx < vstate->dtvs_nlocals) {
10063 				dtrace_statvar_t *svar;
10064 
10065 				if ((svar = vstate->dtvs_locals[ndx]) != NULL)
10066 					existing = &svar->dtsv_var;
10067 			}
10068 
10069 			break;
10070 		}
10071 
10072 		vt = &v->dtdv_type;
10073 
10074 		if (vt->dtdt_flags & DIF_TF_BYREF) {
10075 			if (vt->dtdt_size == 0) {
10076 				err += efunc(i, "zero-sized variable\n");
10077 				break;
10078 			}
10079 
10080 			if ((v->dtdv_scope == DIFV_SCOPE_GLOBAL ||
10081 			    v->dtdv_scope == DIFV_SCOPE_LOCAL) &&
10082 			    vt->dtdt_size > dtrace_statvar_maxsize) {
10083 				err += efunc(i, "oversized by-ref static\n");
10084 				break;
10085 			}
10086 		}
10087 
10088 		if (existing == NULL || existing->dtdv_id == 0)
10089 			continue;
10090 
10091 		ASSERT(existing->dtdv_id == v->dtdv_id);
10092 		ASSERT(existing->dtdv_scope == v->dtdv_scope);
10093 
10094 		if (existing->dtdv_kind != v->dtdv_kind)
10095 			err += efunc(i, "%d changed variable kind\n", id);
10096 
10097 		et = &existing->dtdv_type;
10098 
10099 		if (vt->dtdt_flags != et->dtdt_flags) {
10100 			err += efunc(i, "%d changed variable type flags\n", id);
10101 			break;
10102 		}
10103 
10104 		if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) {
10105 			err += efunc(i, "%d changed variable type size\n", id);
10106 			break;
10107 		}
10108 	}
10109 
10110 	for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
10111 		dif_instr_t instr = dp->dtdo_buf[pc];
10112 
10113 		uint_t v = DIF_INSTR_VAR(instr);
10114 		uint_t op = DIF_INSTR_OP(instr);
10115 
10116 		switch (op) {
10117 		case DIF_OP_LDGS:
10118 		case DIF_OP_LDGAA:
10119 		case DIF_OP_STGS:
10120 		case DIF_OP_STGAA:
10121 			if (v > (uint_t)(DIF_VAR_OTHER_UBASE + maxglobal))
10122 				err += efunc(pc, "invalid variable %u\n", v);
10123 			break;
10124 		case DIF_OP_LDTS:
10125 		case DIF_OP_LDTAA:
10126 		case DIF_OP_STTS:
10127 		case DIF_OP_STTAA:
10128 			if (v > (uint_t)(DIF_VAR_OTHER_UBASE + maxtlocal))
10129 				err += efunc(pc, "invalid variable %u\n", v);
10130 			break;
10131 		case DIF_OP_LDLS:
10132 		case DIF_OP_STLS:
10133 			if (v > (uint_t)(DIF_VAR_OTHER_UBASE + maxlocal))
10134 				err += efunc(pc, "invalid variable %u\n", v);
10135 			break;
10136 		default:
10137 			break;
10138 		}
10139 	}
10140 
10141 	return (err);
10142 }
10143 
10144 /*
10145  * Validate a DTrace DIF object that it is to be used as a helper.  Helpers
10146  * are much more constrained than normal DIFOs.  Specifically, they may
10147  * not:
10148  *
10149  * 1. Make calls to subroutines other than copyin(), copyinstr() or
10150  *    miscellaneous string routines
10151  * 2. Access DTrace variables other than the args[] array, and the
10152  *    curthread, pid, ppid, tid, execname, zonename, uid and gid variables.
10153  * 3. Have thread-local variables.
10154  * 4. Have dynamic variables.
10155  */
10156 static int
dtrace_difo_validate_helper(dtrace_difo_t * dp)10157 dtrace_difo_validate_helper(dtrace_difo_t *dp)
10158 {
10159 	int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
10160 	int err = 0;
10161 	uint_t pc;
10162 
10163 	for (pc = 0; pc < dp->dtdo_len; pc++) {
10164 		dif_instr_t instr = dp->dtdo_buf[pc];
10165 
10166 		uint_t v = DIF_INSTR_VAR(instr);
10167 		uint_t subr = DIF_INSTR_SUBR(instr);
10168 		uint_t op = DIF_INSTR_OP(instr);
10169 
10170 		switch (op) {
10171 		case DIF_OP_OR:
10172 		case DIF_OP_XOR:
10173 		case DIF_OP_AND:
10174 		case DIF_OP_SLL:
10175 		case DIF_OP_SRL:
10176 		case DIF_OP_SRA:
10177 		case DIF_OP_SUB:
10178 		case DIF_OP_ADD:
10179 		case DIF_OP_MUL:
10180 		case DIF_OP_SDIV:
10181 		case DIF_OP_UDIV:
10182 		case DIF_OP_SREM:
10183 		case DIF_OP_UREM:
10184 		case DIF_OP_COPYS:
10185 		case DIF_OP_NOT:
10186 		case DIF_OP_MOV:
10187 		case DIF_OP_RLDSB:
10188 		case DIF_OP_RLDSH:
10189 		case DIF_OP_RLDSW:
10190 		case DIF_OP_RLDUB:
10191 		case DIF_OP_RLDUH:
10192 		case DIF_OP_RLDUW:
10193 		case DIF_OP_RLDX:
10194 		case DIF_OP_ULDSB:
10195 		case DIF_OP_ULDSH:
10196 		case DIF_OP_ULDSW:
10197 		case DIF_OP_ULDUB:
10198 		case DIF_OP_ULDUH:
10199 		case DIF_OP_ULDUW:
10200 		case DIF_OP_ULDX:
10201 		case DIF_OP_STB:
10202 		case DIF_OP_STH:
10203 		case DIF_OP_STW:
10204 		case DIF_OP_STX:
10205 		case DIF_OP_ALLOCS:
10206 		case DIF_OP_CMP:
10207 		case DIF_OP_SCMP:
10208 		case DIF_OP_TST:
10209 		case DIF_OP_BA:
10210 		case DIF_OP_BE:
10211 		case DIF_OP_BNE:
10212 		case DIF_OP_BG:
10213 		case DIF_OP_BGU:
10214 		case DIF_OP_BGE:
10215 		case DIF_OP_BGEU:
10216 		case DIF_OP_BL:
10217 		case DIF_OP_BLU:
10218 		case DIF_OP_BLE:
10219 		case DIF_OP_BLEU:
10220 		case DIF_OP_RET:
10221 		case DIF_OP_NOP:
10222 		case DIF_OP_POPTS:
10223 		case DIF_OP_FLUSHTS:
10224 		case DIF_OP_SETX:
10225 		case DIF_OP_SETS:
10226 		case DIF_OP_LDGA:
10227 		case DIF_OP_LDLS:
10228 		case DIF_OP_STGS:
10229 		case DIF_OP_STLS:
10230 		case DIF_OP_PUSHTR:
10231 		case DIF_OP_PUSHTV:
10232 			break;
10233 
10234 		case DIF_OP_LDGS:
10235 			if (v >= DIF_VAR_OTHER_UBASE)
10236 				break;
10237 
10238 			if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9)
10239 				break;
10240 
10241 			if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID ||
10242 			    v == DIF_VAR_PPID || v == DIF_VAR_TID ||
10243 			    v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME ||
10244 			    v == DIF_VAR_UID || v == DIF_VAR_GID)
10245 				break;
10246 
10247 			err += efunc(pc, "illegal variable %u\n", v);
10248 			break;
10249 
10250 		case DIF_OP_LDTA:
10251 		case DIF_OP_LDTS:
10252 		case DIF_OP_LDGAA:
10253 		case DIF_OP_LDTAA:
10254 			err += efunc(pc, "illegal dynamic variable load\n");
10255 			break;
10256 
10257 		case DIF_OP_STTS:
10258 		case DIF_OP_STGAA:
10259 		case DIF_OP_STTAA:
10260 			err += efunc(pc, "illegal dynamic variable store\n");
10261 			break;
10262 
10263 		case DIF_OP_CALL:
10264 			switch (subr) {
10265 			case DIF_SUBR_ALLOCA:
10266 			case DIF_SUBR_BCOPY:
10267 			case DIF_SUBR_COPYIN:
10268 			case DIF_SUBR_COPYINTO:
10269 			case DIF_SUBR_COPYINSTR:
10270 			case DIF_SUBR_HTONS:
10271 			case DIF_SUBR_HTONL:
10272 			case DIF_SUBR_HTONLL:
10273 			case DIF_SUBR_INDEX:
10274 			case DIF_SUBR_INET_NTOA:
10275 			case DIF_SUBR_INET_NTOA6:
10276 			case DIF_SUBR_INET_NTOP:
10277 			case DIF_SUBR_JSON:
10278 			case DIF_SUBR_LLTOSTR:
10279 			case DIF_SUBR_NTOHS:
10280 			case DIF_SUBR_NTOHL:
10281 			case DIF_SUBR_NTOHLL:
10282 			case DIF_SUBR_RINDEX:
10283 			case DIF_SUBR_STRCHR:
10284 			case DIF_SUBR_STRTOLL:
10285 			case DIF_SUBR_STRJOIN:
10286 			case DIF_SUBR_STRRCHR:
10287 			case DIF_SUBR_STRSTR:
10288 				break;
10289 			default:
10290 				err += efunc(pc, "invalid subr %u\n", subr);
10291 			}
10292 			break;
10293 
10294 		default:
10295 			err += efunc(pc, "invalid opcode %u\n",
10296 			    DIF_INSTR_OP(instr));
10297 		}
10298 	}
10299 
10300 	return (err);
10301 }
10302 
10303 /*
10304  * Returns 1 if the expression in the DIF object can be cached on a per-thread
10305  * basis; 0 if not.
10306  */
10307 static int
dtrace_difo_cacheable(dtrace_difo_t * dp)10308 dtrace_difo_cacheable(dtrace_difo_t *dp)
10309 {
10310 	uint_t i;
10311 
10312 	if (dp == NULL)
10313 		return (0);
10314 
10315 	for (i = 0; i < dp->dtdo_varlen; i++) {
10316 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
10317 
10318 		if (v->dtdv_scope != DIFV_SCOPE_GLOBAL)
10319 			continue;
10320 
10321 		switch (v->dtdv_id) {
10322 		case DIF_VAR_CURTHREAD:
10323 		case DIF_VAR_PID:
10324 		case DIF_VAR_TID:
10325 		case DIF_VAR_EXECNAME:
10326 		case DIF_VAR_ZONENAME:
10327 			break;
10328 
10329 		default:
10330 			return (0);
10331 		}
10332 	}
10333 
10334 	/*
10335 	 * This DIF object may be cacheable.  Now we need to look for any
10336 	 * array loading instructions, any memory loading instructions, or
10337 	 * any stores to thread-local variables.
10338 	 */
10339 	for (i = 0; i < dp->dtdo_len; i++) {
10340 		uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]);
10341 
10342 		if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) ||
10343 		    (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) ||
10344 		    (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) ||
10345 		    op == DIF_OP_LDGA || op == DIF_OP_STTS)
10346 			return (0);
10347 	}
10348 
10349 	return (1);
10350 }
10351 
10352 static void
dtrace_difo_hold(dtrace_difo_t * dp)10353 dtrace_difo_hold(dtrace_difo_t *dp)
10354 {
10355 	uint_t i;
10356 
10357 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
10358 
10359 	dp->dtdo_refcnt++;
10360 	ASSERT(dp->dtdo_refcnt != 0);
10361 
10362 	/*
10363 	 * We need to check this DIF object for references to the variable
10364 	 * DIF_VAR_VTIMESTAMP.
10365 	 */
10366 	for (i = 0; i < dp->dtdo_varlen; i++) {
10367 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
10368 
10369 		if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
10370 			continue;
10371 
10372 		if (dtrace_vtime_references++ == 0)
10373 			dtrace_vtime_enable();
10374 	}
10375 }
10376 
10377 /*
10378  * This routine calculates the dynamic variable chunksize for a given DIF
10379  * object.  The calculation is not fool-proof, and can probably be tricked by
10380  * malicious DIF -- but it works for all compiler-generated DIF.  Because this
10381  * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail
10382  * if a dynamic variable size exceeds the chunksize.
10383  */
10384 static void
dtrace_difo_chunksize(dtrace_difo_t * dp,dtrace_vstate_t * vstate)10385 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10386 {
10387 	uint64_t sval = 0;
10388 	dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
10389 	const dif_instr_t *text = dp->dtdo_buf;
10390 	uint_t pc, srd = 0;
10391 	uint_t ttop = 0;
10392 	size_t size, ksize;
10393 	uint_t id, i;
10394 
10395 	for (pc = 0; pc < dp->dtdo_len; pc++) {
10396 		dif_instr_t instr = text[pc];
10397 		uint_t op = DIF_INSTR_OP(instr);
10398 		uint_t rd = DIF_INSTR_RD(instr);
10399 		uint_t r1 = DIF_INSTR_R1(instr);
10400 		uint_t nkeys = 0;
10401 		uchar_t scope;
10402 
10403 		dtrace_key_t *key = tupregs;
10404 
10405 		switch (op) {
10406 		case DIF_OP_SETX:
10407 			sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)];
10408 			srd = rd;
10409 			continue;
10410 
10411 		case DIF_OP_STTS:
10412 			key = &tupregs[DIF_DTR_NREGS];
10413 			key[0].dttk_size = 0;
10414 			key[1].dttk_size = 0;
10415 			nkeys = 2;
10416 			scope = DIFV_SCOPE_THREAD;
10417 			break;
10418 
10419 		case DIF_OP_STGAA:
10420 		case DIF_OP_STTAA:
10421 			nkeys = ttop;
10422 
10423 			if (DIF_INSTR_OP(instr) == DIF_OP_STTAA)
10424 				key[nkeys++].dttk_size = 0;
10425 
10426 			key[nkeys++].dttk_size = 0;
10427 
10428 			if (op == DIF_OP_STTAA) {
10429 				scope = DIFV_SCOPE_THREAD;
10430 			} else {
10431 				scope = DIFV_SCOPE_GLOBAL;
10432 			}
10433 
10434 			break;
10435 
10436 		case DIF_OP_PUSHTR:
10437 			if (ttop == DIF_DTR_NREGS)
10438 				return;
10439 
10440 			if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) {
10441 				/*
10442 				 * If the register for the size of the "pushtr"
10443 				 * is %r0 (or the value is 0) and the type is
10444 				 * a string, we'll use the system-wide default
10445 				 * string size.
10446 				 */
10447 				tupregs[ttop++].dttk_size =
10448 				    dtrace_strsize_default;
10449 			} else {
10450 				if (srd == 0)
10451 					return;
10452 
10453 				if (sval > LONG_MAX)
10454 					return;
10455 
10456 				tupregs[ttop++].dttk_size = sval;
10457 			}
10458 
10459 			break;
10460 
10461 		case DIF_OP_PUSHTV:
10462 			if (ttop == DIF_DTR_NREGS)
10463 				return;
10464 
10465 			tupregs[ttop++].dttk_size = 0;
10466 			break;
10467 
10468 		case DIF_OP_FLUSHTS:
10469 			ttop = 0;
10470 			break;
10471 
10472 		case DIF_OP_POPTS:
10473 			if (ttop != 0)
10474 				ttop--;
10475 			break;
10476 		}
10477 
10478 		sval = 0;
10479 		srd = 0;
10480 
10481 		if (nkeys == 0)
10482 			continue;
10483 
10484 		/*
10485 		 * We have a dynamic variable allocation; calculate its size.
10486 		 */
10487 		for (ksize = 0, i = 0; i < nkeys; i++)
10488 			ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
10489 
10490 		size = sizeof (dtrace_dynvar_t);
10491 		size += sizeof (dtrace_key_t) * (nkeys - 1);
10492 		size += ksize;
10493 
10494 		/*
10495 		 * Now we need to determine the size of the stored data.
10496 		 */
10497 		id = DIF_INSTR_VAR(instr);
10498 
10499 		for (i = 0; i < dp->dtdo_varlen; i++) {
10500 			dtrace_difv_t *v = &dp->dtdo_vartab[i];
10501 
10502 			if (v->dtdv_id == id && v->dtdv_scope == scope) {
10503 				size += v->dtdv_type.dtdt_size;
10504 				break;
10505 			}
10506 		}
10507 
10508 		if (i == dp->dtdo_varlen)
10509 			return;
10510 
10511 		/*
10512 		 * We have the size.  If this is larger than the chunk size
10513 		 * for our dynamic variable state, reset the chunk size.
10514 		 */
10515 		size = P2ROUNDUP(size, sizeof (uint64_t));
10516 
10517 		/*
10518 		 * Before setting the chunk size, check that we're not going
10519 		 * to set it to a negative value...
10520 		 */
10521 		if (size > LONG_MAX)
10522 			return;
10523 
10524 		/*
10525 		 * ...and make certain that we didn't badly overflow.
10526 		 */
10527 		if (size < ksize || size < sizeof (dtrace_dynvar_t))
10528 			return;
10529 
10530 		if (size > vstate->dtvs_dynvars.dtds_chunksize)
10531 			vstate->dtvs_dynvars.dtds_chunksize = size;
10532 	}
10533 }
10534 
10535 static void
dtrace_difo_init(dtrace_difo_t * dp,dtrace_vstate_t * vstate)10536 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10537 {
10538 	int oldsvars, osz, nsz, otlocals, ntlocals;
10539 	uint_t i, id;
10540 
10541 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
10542 	ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0);
10543 
10544 	for (i = 0; i < dp->dtdo_varlen; i++) {
10545 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
10546 		dtrace_statvar_t *svar;
10547 		dtrace_statvar_t ***svarp = NULL;
10548 		size_t dsize = 0;
10549 		uint8_t scope = v->dtdv_scope;
10550 		int *np = (int *)NULL;
10551 
10552 		if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
10553 			continue;
10554 
10555 		id -= DIF_VAR_OTHER_UBASE;
10556 
10557 		switch (scope) {
10558 		case DIFV_SCOPE_THREAD:
10559 			while (id >= (uint_t)(otlocals = vstate->dtvs_ntlocals)) {
10560 				dtrace_difv_t *tlocals;
10561 
10562 				if ((ntlocals = (otlocals << 1)) == 0)
10563 					ntlocals = 1;
10564 
10565 				osz = otlocals * sizeof (dtrace_difv_t);
10566 				nsz = ntlocals * sizeof (dtrace_difv_t);
10567 
10568 				tlocals = kmem_zalloc(nsz, KM_SLEEP);
10569 
10570 				if (osz != 0) {
10571 					bcopy(vstate->dtvs_tlocals,
10572 					    tlocals, osz);
10573 					kmem_free(vstate->dtvs_tlocals, osz);
10574 				}
10575 
10576 				vstate->dtvs_tlocals = tlocals;
10577 				vstate->dtvs_ntlocals = ntlocals;
10578 			}
10579 
10580 			vstate->dtvs_tlocals[id] = *v;
10581 			continue;
10582 
10583 		case DIFV_SCOPE_LOCAL:
10584 			np = &vstate->dtvs_nlocals;
10585 			svarp = &vstate->dtvs_locals;
10586 
10587 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
10588 				dsize = (int)NCPU * (v->dtdv_type.dtdt_size +
10589 				    sizeof (uint64_t));
10590 			else
10591 				dsize = (int)NCPU * sizeof (uint64_t);
10592 
10593 			break;
10594 
10595 		case DIFV_SCOPE_GLOBAL:
10596 			np = &vstate->dtvs_nglobals;
10597 			svarp = &vstate->dtvs_globals;
10598 
10599 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
10600 				dsize = v->dtdv_type.dtdt_size +
10601 				    sizeof (uint64_t);
10602 
10603 			break;
10604 
10605 		default:
10606 			ASSERT(0);
10607 		}
10608 
10609 		while (id >= (uint_t)(oldsvars = *np)) {
10610 			dtrace_statvar_t **statics;
10611 			int newsvars, oldsize, newsize;
10612 
10613 			if ((newsvars = (oldsvars << 1)) == 0)
10614 				newsvars = 1;
10615 
10616 			oldsize = oldsvars * sizeof (dtrace_statvar_t *);
10617 			newsize = newsvars * sizeof (dtrace_statvar_t *);
10618 
10619 			statics = kmem_zalloc(newsize, KM_SLEEP);
10620 
10621 			if (oldsize != 0) {
10622 				bcopy(*svarp, statics, oldsize);
10623 				kmem_free(*svarp, oldsize);
10624 			}
10625 
10626 			*svarp = statics;
10627 			*np = newsvars;
10628 		}
10629 
10630 		if ((svar = (*svarp)[id]) == NULL) {
10631 			svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP);
10632 			svar->dtsv_var = *v;
10633 
10634 			if ((svar->dtsv_size = dsize) != 0) {
10635 				svar->dtsv_data = (uint64_t)(uintptr_t)
10636 				    kmem_zalloc(dsize, KM_SLEEP);
10637 			}
10638 
10639 			(*svarp)[id] = svar;
10640 		}
10641 
10642 		svar->dtsv_refcnt++;
10643 	}
10644 
10645 	dtrace_difo_chunksize(dp, vstate);
10646 	dtrace_difo_hold(dp);
10647 }
10648 
10649 static dtrace_difo_t *
dtrace_difo_duplicate(dtrace_difo_t * dp,dtrace_vstate_t * vstate)10650 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10651 {
10652 	dtrace_difo_t *new;
10653 	size_t sz;
10654 
10655 	ASSERT(dp->dtdo_buf != NULL);
10656 	ASSERT(dp->dtdo_refcnt != 0);
10657 
10658 	new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
10659 
10660 	ASSERT(dp->dtdo_buf != NULL);
10661 	sz = dp->dtdo_len * sizeof (dif_instr_t);
10662 	new->dtdo_buf = kmem_alloc(sz, KM_SLEEP);
10663 	bcopy(dp->dtdo_buf, new->dtdo_buf, sz);
10664 	new->dtdo_len = dp->dtdo_len;
10665 
10666 	if (dp->dtdo_strtab != NULL) {
10667 		ASSERT(dp->dtdo_strlen != 0);
10668 		new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP);
10669 		bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen);
10670 		new->dtdo_strlen = dp->dtdo_strlen;
10671 	}
10672 
10673 	if (dp->dtdo_inttab != NULL) {
10674 		ASSERT(dp->dtdo_intlen != 0);
10675 		sz = dp->dtdo_intlen * sizeof (uint64_t);
10676 		new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP);
10677 		bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz);
10678 		new->dtdo_intlen = dp->dtdo_intlen;
10679 	}
10680 
10681 	if (dp->dtdo_vartab != NULL) {
10682 		ASSERT(dp->dtdo_varlen != 0);
10683 		sz = dp->dtdo_varlen * sizeof (dtrace_difv_t);
10684 		new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP);
10685 		bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz);
10686 		new->dtdo_varlen = dp->dtdo_varlen;
10687 	}
10688 
10689 	dtrace_difo_init(new, vstate);
10690 	return (new);
10691 }
10692 
10693 static void
dtrace_difo_destroy(dtrace_difo_t * dp,dtrace_vstate_t * vstate)10694 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10695 {
10696 	uint_t i;
10697 
10698 	ASSERT(dp->dtdo_refcnt == 0);
10699 
10700 	for (i = 0; i < dp->dtdo_varlen; i++) {
10701 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
10702 		dtrace_statvar_t *svar;
10703 		dtrace_statvar_t **svarp = NULL;
10704 		uint_t id;
10705 		uint8_t scope = v->dtdv_scope;
10706 		int *np = NULL;
10707 
10708 		switch (scope) {
10709 		case DIFV_SCOPE_THREAD:
10710 			continue;
10711 
10712 		case DIFV_SCOPE_LOCAL:
10713 			np = &vstate->dtvs_nlocals;
10714 			svarp = vstate->dtvs_locals;
10715 			break;
10716 
10717 		case DIFV_SCOPE_GLOBAL:
10718 			np = &vstate->dtvs_nglobals;
10719 			svarp = vstate->dtvs_globals;
10720 			break;
10721 
10722 		default:
10723 			ASSERT(0);
10724 		}
10725 
10726 		if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
10727 			continue;
10728 
10729 		id -= DIF_VAR_OTHER_UBASE;
10730 
10731 		ASSERT(id < (uint_t)*np);
10732 
10733 		svar = svarp[id];
10734 		ASSERT(svar != NULL);
10735 		ASSERT(svar->dtsv_refcnt > 0);
10736 
10737 		if (--svar->dtsv_refcnt > 0)
10738 			continue;
10739 
10740 		if (svar->dtsv_size != 0) {
10741 			ASSERT(svar->dtsv_data != 0);
10742 			kmem_free((void *)(uintptr_t)svar->dtsv_data,
10743 			    svar->dtsv_size);
10744 		}
10745 
10746 		kmem_free(svar, sizeof (dtrace_statvar_t));
10747 		svarp[id] = NULL;
10748 	}
10749 
10750 	kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
10751 	kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
10752 	kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
10753 	kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
10754 
10755 	kmem_free(dp, sizeof (dtrace_difo_t));
10756 }
10757 
10758 static void
dtrace_difo_release(dtrace_difo_t * dp,dtrace_vstate_t * vstate)10759 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10760 {
10761 	uint_t i;
10762 
10763 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
10764 	ASSERT(dp->dtdo_refcnt != 0);
10765 
10766 	for (i = 0; i < dp->dtdo_varlen; i++) {
10767 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
10768 
10769 		if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
10770 			continue;
10771 
10772 		ASSERT(dtrace_vtime_references > 0);
10773 		if (--dtrace_vtime_references == 0)
10774 			dtrace_vtime_disable();
10775 	}
10776 
10777 	if (--dp->dtdo_refcnt == 0)
10778 		dtrace_difo_destroy(dp, vstate);
10779 }
10780 
10781 /*
10782  * DTrace Format Functions
10783  */
10784 
10785 static dtrace_format_t*
dtrace_format_new(char * str)10786 dtrace_format_new(char *str)
10787 {
10788 	dtrace_format_t *fmt = NULL;
10789 	size_t bufsize = strlen(str) + 1;
10790 
10791 	fmt = kmem_zalloc(sizeof(*fmt) + bufsize, KM_SLEEP);
10792 
10793 	fmt->dtf_refcount = 1;
10794 	(void) strlcpy(fmt->dtf_str, str, bufsize);
10795 
10796 	return fmt;
10797 }
10798 
10799 static uint16_t
dtrace_format_add(dtrace_state_t * state,char * str)10800 dtrace_format_add(dtrace_state_t *state, char *str)
10801 {
10802 	dtrace_format_t **new;
10803 	uint16_t ndx;
10804 
10805 	for (ndx = 0; ndx < state->dts_nformats; ndx++) {
10806 		if (state->dts_formats[ndx] == NULL) {
10807 			state->dts_formats[ndx] = dtrace_format_new(str);
10808 			return (ndx + 1);
10809 		}
10810 		else if (strcmp(state->dts_formats[ndx]->dtf_str, str) == 0) {
10811 			VERIFY(state->dts_formats[ndx]->dtf_refcount < UINT64_MAX);
10812 			state->dts_formats[ndx]->dtf_refcount++;
10813 			return (ndx + 1);
10814 		}
10815 	}
10816 
10817 	if (state->dts_nformats == USHRT_MAX) {
10818 		/*
10819 		 * This is only likely if a denial-of-service attack is being
10820 		 * attempted.  As such, it's okay to fail silently here.
10821 		 */
10822 		return (0);
10823 	}
10824 
10825 	/*
10826 	 * For simplicity, we always resize the formats array to be exactly the
10827 	 * number of formats.
10828 	 */
10829 	ndx = state->dts_nformats++;
10830 	new = kmem_alloc((ndx + 1) * sizeof (*state->dts_formats), KM_SLEEP);
10831 
10832 	if (state->dts_formats != NULL) {
10833 		ASSERT(ndx != 0);
10834 		bcopy(state->dts_formats, new, ndx * sizeof (*state->dts_formats));
10835 		kmem_free(state->dts_formats, ndx * sizeof (*state->dts_formats));
10836 	}
10837 
10838 	state->dts_formats = new;
10839 	state->dts_formats[ndx] = dtrace_format_new(str);
10840 
10841 	return (ndx + 1);
10842 }
10843 
10844 static void
dtrace_format_remove(dtrace_state_t * state,uint16_t format)10845 dtrace_format_remove(dtrace_state_t *state, uint16_t format)
10846 {
10847 	dtrace_format_t *fmt;
10848 
10849 	ASSERT(state->dts_formats != NULL);
10850 	ASSERT(format <= state->dts_nformats);
10851 
10852 	fmt = state->dts_formats[format - 1];
10853 
10854 	ASSERT(fmt != NULL);
10855 	VERIFY(fmt->dtf_refcount > 0);
10856 
10857 	fmt->dtf_refcount--;
10858 
10859 	if (fmt->dtf_refcount == 0) {
10860 		kmem_free(fmt, DTRACE_FORMAT_SIZE(fmt));
10861 		state->dts_formats[format - 1] = NULL;
10862 	}
10863 }
10864 
10865 static void
dtrace_format_destroy(dtrace_state_t * state)10866 dtrace_format_destroy(dtrace_state_t *state)
10867 {
10868 	int i;
10869 
10870 	if (state->dts_nformats == 0) {
10871 		ASSERT(state->dts_formats == NULL);
10872 		return;
10873 	}
10874 
10875 	ASSERT(state->dts_formats != NULL);
10876 
10877 	for (i = 0; i < state->dts_nformats; i++) {
10878 		dtrace_format_t *fmt = state->dts_formats[i];
10879 
10880 		if (fmt == NULL)
10881 			continue;
10882 
10883 		kmem_free(fmt, DTRACE_FORMAT_SIZE(fmt));
10884 	}
10885 
10886 	kmem_free(state->dts_formats, state->dts_nformats * sizeof (*state->dts_formats));
10887 	state->dts_nformats = 0;
10888 	state->dts_formats = NULL;
10889 }
10890 
10891 /*
10892  * DTrace Predicate Functions
10893  */
10894 static dtrace_predicate_t *
dtrace_predicate_create(dtrace_difo_t * dp)10895 dtrace_predicate_create(dtrace_difo_t *dp)
10896 {
10897 	dtrace_predicate_t *pred;
10898 
10899 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
10900 	ASSERT(dp->dtdo_refcnt != 0);
10901 
10902 	pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP);
10903 	pred->dtp_difo = dp;
10904 	pred->dtp_refcnt = 1;
10905 
10906 	if (!dtrace_difo_cacheable(dp))
10907 		return (pred);
10908 
10909 	if (dtrace_predcache_id == DTRACE_CACHEIDNONE) {
10910 		/*
10911 		 * This is only theoretically possible -- we have had 2^32
10912 		 * cacheable predicates on this machine.  We cannot allow any
10913 		 * more predicates to become cacheable:  as unlikely as it is,
10914 		 * there may be a thread caching a (now stale) predicate cache
10915 		 * ID. (N.B.: the temptation is being successfully resisted to
10916 		 * have this cmn_err() "Holy shit -- we executed this code!")
10917 		 */
10918 		return (pred);
10919 	}
10920 
10921 	pred->dtp_cacheid = dtrace_predcache_id++;
10922 
10923 	return (pred);
10924 }
10925 
10926 static void
dtrace_predicate_hold(dtrace_predicate_t * pred)10927 dtrace_predicate_hold(dtrace_predicate_t *pred)
10928 {
10929 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
10930 	ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0);
10931 	ASSERT(pred->dtp_refcnt > 0);
10932 
10933 	pred->dtp_refcnt++;
10934 }
10935 
10936 static void
dtrace_predicate_release(dtrace_predicate_t * pred,dtrace_vstate_t * vstate)10937 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate)
10938 {
10939 	dtrace_difo_t *dp = pred->dtp_difo;
10940 #pragma unused(dp) /* __APPLE__ */
10941 
10942 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
10943 	ASSERT(dp != NULL && dp->dtdo_refcnt != 0);
10944 	ASSERT(pred->dtp_refcnt > 0);
10945 
10946 	if (--pred->dtp_refcnt == 0) {
10947 		dtrace_difo_release(pred->dtp_difo, vstate);
10948 		kmem_free(pred, sizeof (dtrace_predicate_t));
10949 	}
10950 }
10951 
10952 /*
10953  * DTrace Action Description Functions
10954  */
10955 static dtrace_actdesc_t *
dtrace_actdesc_create(dtrace_actkind_t kind,uint32_t ntuple,uint64_t uarg,uint64_t arg)10956 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple,
10957     uint64_t uarg, uint64_t arg)
10958 {
10959 	dtrace_actdesc_t *act;
10960 
10961 	ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != 0 &&
10962 	    arg >= KERNELBASE) || (arg == 0 && kind == DTRACEACT_PRINTA));
10963 
10964 	act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP);
10965 	act->dtad_kind = kind;
10966 	act->dtad_ntuple = ntuple;
10967 	act->dtad_uarg = uarg;
10968 	act->dtad_arg = arg;
10969 	act->dtad_refcnt = 1;
10970 
10971 	return (act);
10972 }
10973 
10974 static void
dtrace_actdesc_hold(dtrace_actdesc_t * act)10975 dtrace_actdesc_hold(dtrace_actdesc_t *act)
10976 {
10977 	ASSERT(act->dtad_refcnt >= 1);
10978 	act->dtad_refcnt++;
10979 }
10980 
10981 static void
dtrace_actdesc_release(dtrace_actdesc_t * act,dtrace_vstate_t * vstate)10982 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate)
10983 {
10984 	dtrace_actkind_t kind = act->dtad_kind;
10985 	dtrace_difo_t *dp;
10986 
10987 	ASSERT(act->dtad_refcnt >= 1);
10988 
10989 	if (--act->dtad_refcnt != 0)
10990 		return;
10991 
10992 	if ((dp = act->dtad_difo) != NULL)
10993 		dtrace_difo_release(dp, vstate);
10994 
10995 	if (DTRACEACT_ISPRINTFLIKE(kind)) {
10996 		char *str = (char *)(uintptr_t)act->dtad_arg;
10997 
10998 		ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) ||
10999 		    (str == NULL && act->dtad_kind == DTRACEACT_PRINTA));
11000 
11001 		if (str != NULL)
11002 			kmem_free(str, strlen(str) + 1);
11003 	}
11004 
11005 	kmem_free(act, sizeof (dtrace_actdesc_t));
11006 }
11007 
11008 /*
11009  * DTrace ECB Functions
11010  */
11011 static dtrace_ecb_t *
dtrace_ecb_add(dtrace_state_t * state,dtrace_probe_t * probe)11012 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe)
11013 {
11014 	dtrace_ecb_t *ecb;
11015 	dtrace_epid_t epid;
11016 
11017 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
11018 
11019 	ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP);
11020 	ecb->dte_predicate = NULL;
11021 	ecb->dte_probe = probe;
11022 
11023 	/*
11024 	 * The default size is the size of the default action: recording
11025 	 * the header.
11026 	 */
11027 	ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t);
11028 	ecb->dte_alignment = sizeof (dtrace_epid_t);
11029 
11030 	epid = state->dts_epid++;
11031 
11032 	if (epid - 1 >= (dtrace_epid_t)state->dts_necbs) {
11033 		dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs;
11034 		int necbs = state->dts_necbs << 1;
11035 
11036 		ASSERT(epid == (dtrace_epid_t)state->dts_necbs + 1);
11037 
11038 		if (necbs == 0) {
11039 			ASSERT(oecbs == NULL);
11040 			necbs = 1;
11041 		}
11042 
11043 		ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP);
11044 
11045 		if (oecbs != NULL)
11046 			bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs));
11047 
11048 		dtrace_membar_producer();
11049 		state->dts_ecbs = ecbs;
11050 
11051 		if (oecbs != NULL) {
11052 			/*
11053 			 * If this state is active, we must dtrace_sync()
11054 			 * before we can free the old dts_ecbs array:  we're
11055 			 * coming in hot, and there may be active ring
11056 			 * buffer processing (which indexes into the dts_ecbs
11057 			 * array) on another CPU.
11058 			 */
11059 			if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
11060 				dtrace_sync();
11061 
11062 			kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs));
11063 		}
11064 
11065 		dtrace_membar_producer();
11066 		state->dts_necbs = necbs;
11067 	}
11068 
11069 	ecb->dte_state = state;
11070 
11071 	ASSERT(state->dts_ecbs[epid - 1] == NULL);
11072 	dtrace_membar_producer();
11073 	state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb;
11074 
11075 	return (ecb);
11076 }
11077 
11078 static int
dtrace_ecb_enable(dtrace_ecb_t * ecb)11079 dtrace_ecb_enable(dtrace_ecb_t *ecb)
11080 {
11081 	dtrace_probe_t *probe = ecb->dte_probe;
11082 
11083 	LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
11084 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
11085 	ASSERT(ecb->dte_next == NULL);
11086 
11087 	if (probe == NULL) {
11088 		/*
11089 		 * This is the NULL probe -- there's nothing to do.
11090 		 */
11091 	    return(0);
11092 	}
11093 
11094 	probe->dtpr_provider->dtpv_ecb_count++;
11095 	if (probe->dtpr_ecb == NULL) {
11096 		dtrace_provider_t *prov = probe->dtpr_provider;
11097 
11098 		/*
11099 		 * We're the first ECB on this probe.
11100 		 */
11101 		probe->dtpr_ecb = probe->dtpr_ecb_last = ecb;
11102 
11103 		if (ecb->dte_predicate != NULL)
11104 			probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid;
11105 
11106 		return (prov->dtpv_pops.dtps_enable(prov->dtpv_arg,
11107                     probe->dtpr_id, probe->dtpr_arg));
11108 	} else {
11109 		/*
11110 		 * This probe is already active.  Swing the last pointer to
11111 		 * point to the new ECB, and issue a dtrace_sync() to assure
11112 		 * that all CPUs have seen the change.
11113 		 */
11114 		ASSERT(probe->dtpr_ecb_last != NULL);
11115 		probe->dtpr_ecb_last->dte_next = ecb;
11116 		probe->dtpr_ecb_last = ecb;
11117 		probe->dtpr_predcache = 0;
11118 
11119 		dtrace_sync();
11120 		return(0);
11121 	}
11122 }
11123 
11124 static int
dtrace_ecb_resize(dtrace_ecb_t * ecb)11125 dtrace_ecb_resize(dtrace_ecb_t *ecb)
11126 {
11127 	dtrace_action_t *act;
11128 	uint32_t curneeded = UINT32_MAX;
11129 	uint32_t aggbase = UINT32_MAX;
11130 
11131 	/*
11132 	 * If we record anything, we always record the dtrace_rechdr_t.  (And
11133 	 * we always record it first.)
11134 	 */
11135 	ecb->dte_size = sizeof (dtrace_rechdr_t);
11136 	ecb->dte_alignment = sizeof (dtrace_epid_t);
11137 
11138 	for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
11139 		dtrace_recdesc_t *rec = &act->dta_rec;
11140 		ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1);
11141 
11142 		ecb->dte_alignment = MAX(ecb->dte_alignment, rec->dtrd_alignment);
11143 
11144 		if (DTRACEACT_ISAGG(act->dta_kind)) {
11145 			dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
11146 
11147 			ASSERT(rec->dtrd_size != 0);
11148 			ASSERT(agg->dtag_first != NULL);
11149 			ASSERT(act->dta_prev->dta_intuple);
11150 			ASSERT(aggbase != UINT32_MAX);
11151 			ASSERT(curneeded != UINT32_MAX);
11152 
11153 			agg->dtag_base = aggbase;
11154 			curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
11155 			rec->dtrd_offset = curneeded;
11156 			if (curneeded + rec->dtrd_size < curneeded)
11157 				return (EINVAL);
11158 			curneeded += rec->dtrd_size;
11159 			ecb->dte_needed = MAX(ecb->dte_needed, curneeded);
11160 
11161 			aggbase = UINT32_MAX;
11162 			curneeded = UINT32_MAX;
11163 		} else if (act->dta_intuple) {
11164 			if (curneeded == UINT32_MAX) {
11165 				/*
11166 				 * This is the first record in a tuple.  Align
11167 				 * curneeded to be at offset 4 in an 8-byte
11168 				 * aligned block.
11169 				 */
11170 				ASSERT(act->dta_prev == NULL || !act->dta_prev->dta_intuple);
11171 				ASSERT(aggbase == UINT32_MAX);
11172 
11173 				curneeded = P2PHASEUP(ecb->dte_size,
11174 				    sizeof (uint64_t), sizeof (dtrace_aggid_t));
11175 
11176 				aggbase = curneeded - sizeof (dtrace_aggid_t);
11177 				ASSERT(IS_P2ALIGNED(aggbase,
11178 				    sizeof (uint64_t)));
11179 			}
11180 
11181 			curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
11182 			rec->dtrd_offset = curneeded;
11183 			curneeded += rec->dtrd_size;
11184 			if (curneeded + rec->dtrd_size < curneeded)
11185 				return (EINVAL);
11186 		} else {
11187 			/* tuples must be followed by an aggregation */
11188 			ASSERT(act->dta_prev == NULL || !act->dta_prev->dta_intuple);
11189 			ecb->dte_size = P2ROUNDUP(ecb->dte_size, rec->dtrd_alignment);
11190 			rec->dtrd_offset = ecb->dte_size;
11191 			if (ecb->dte_size + rec->dtrd_size < ecb->dte_size)
11192 				return (EINVAL);
11193 			ecb->dte_size += rec->dtrd_size;
11194 			ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size);
11195 		}
11196 	}
11197 
11198 	if ((act = ecb->dte_action) != NULL &&
11199 	    !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) &&
11200 	    ecb->dte_size == sizeof (dtrace_rechdr_t)) {
11201 		/*
11202 		 * If the size is still sizeof (dtrace_rechdr_t), then all
11203 		 * actions store no data; set the size to 0.
11204 		 */
11205 		ecb->dte_size = 0;
11206 	}
11207 
11208 	ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t));
11209 	ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t)));
11210 	ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed, ecb->dte_needed);
11211 	return (0);
11212 }
11213 
11214 static dtrace_action_t *
dtrace_ecb_aggregation_create(dtrace_ecb_t * ecb,dtrace_actdesc_t * desc)11215 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
11216 {
11217 	dtrace_aggregation_t *agg;
11218 	size_t size = sizeof (uint64_t);
11219 	int ntuple = desc->dtad_ntuple;
11220 	dtrace_action_t *act;
11221 	dtrace_recdesc_t *frec;
11222 	dtrace_aggid_t aggid;
11223 	dtrace_state_t *state = ecb->dte_state;
11224 
11225 	agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP);
11226 	agg->dtag_ecb = ecb;
11227 
11228 	ASSERT(DTRACEACT_ISAGG(desc->dtad_kind));
11229 
11230 	switch (desc->dtad_kind) {
11231 	case DTRACEAGG_MIN:
11232 		agg->dtag_initial = INT64_MAX;
11233 		agg->dtag_aggregate = dtrace_aggregate_min;
11234 		break;
11235 
11236 	case DTRACEAGG_MAX:
11237 		agg->dtag_initial = INT64_MIN;
11238 		agg->dtag_aggregate = dtrace_aggregate_max;
11239 		break;
11240 
11241 	case DTRACEAGG_COUNT:
11242 		agg->dtag_aggregate = dtrace_aggregate_count;
11243 		break;
11244 
11245 	case DTRACEAGG_QUANTIZE:
11246 		agg->dtag_aggregate = dtrace_aggregate_quantize;
11247 		size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) *
11248 		    sizeof (uint64_t);
11249 		break;
11250 
11251 	case DTRACEAGG_LQUANTIZE: {
11252 		uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg);
11253 		uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg);
11254 
11255 		agg->dtag_initial = desc->dtad_arg;
11256 		agg->dtag_aggregate = dtrace_aggregate_lquantize;
11257 
11258 		if (step == 0 || levels == 0)
11259 			goto err;
11260 
11261 		size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t);
11262 		break;
11263 	}
11264 
11265 	case DTRACEAGG_LLQUANTIZE: {
11266 		uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg);
11267 		uint16_t low    = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg);
11268 		uint16_t high   = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg);
11269 		uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg);
11270 		int64_t v;
11271 
11272 		agg->dtag_initial = desc->dtad_arg;
11273 		agg->dtag_aggregate = dtrace_aggregate_llquantize;
11274 
11275 		if (factor < 2 || low >= high || nsteps < factor)
11276 			goto err;
11277 
11278 		/*
11279 		 * Now check that the number of steps evenly divides a power
11280 		 * of the factor.  (This assures both integer bucket size and
11281 		 * linearity within each magnitude.)
11282 		 */
11283 		for (v = factor; v < nsteps; v *= factor)
11284 			continue;
11285 
11286 		if ((v % nsteps) || (nsteps % factor))
11287 			goto err;
11288 
11289  		size = (dtrace_aggregate_llquantize_bucket(factor, low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t);
11290 		break;
11291   }
11292 
11293 	case DTRACEAGG_AVG:
11294 		agg->dtag_aggregate = dtrace_aggregate_avg;
11295 		size = sizeof (uint64_t) * 2;
11296 		break;
11297 
11298 	case DTRACEAGG_STDDEV:
11299 		agg->dtag_aggregate = dtrace_aggregate_stddev;
11300 		size = sizeof (uint64_t) * 4;
11301 		break;
11302 
11303 	case DTRACEAGG_SUM:
11304 		agg->dtag_aggregate = dtrace_aggregate_sum;
11305 		break;
11306 
11307 	default:
11308 		goto err;
11309 	}
11310 
11311 	agg->dtag_action.dta_rec.dtrd_size = size;
11312 
11313 	if (ntuple == 0)
11314 		goto err;
11315 
11316 	/*
11317 	 * We must make sure that we have enough actions for the n-tuple.
11318 	 */
11319 	for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) {
11320 		if (DTRACEACT_ISAGG(act->dta_kind))
11321 			break;
11322 
11323 		if (--ntuple == 0) {
11324 			/*
11325 			 * This is the action with which our n-tuple begins.
11326 			 */
11327 			agg->dtag_first = act;
11328 			goto success;
11329 		}
11330 	}
11331 
11332 	/*
11333 	 * This n-tuple is short by ntuple elements.  Return failure.
11334 	 */
11335 	ASSERT(ntuple != 0);
11336 err:
11337 	kmem_free(agg, sizeof (dtrace_aggregation_t));
11338 	return (NULL);
11339 
11340 success:
11341 	/*
11342 	 * If the last action in the tuple has a size of zero, it's actually
11343 	 * an expression argument for the aggregating action.
11344 	 */
11345 	ASSERT(ecb->dte_action_last != NULL);
11346 	act = ecb->dte_action_last;
11347 
11348 	if (act->dta_kind == DTRACEACT_DIFEXPR) {
11349 		ASSERT(act->dta_difo != NULL);
11350 
11351 		if (act->dta_difo->dtdo_rtype.dtdt_size == 0)
11352 			agg->dtag_hasarg = 1;
11353 	}
11354 
11355 	/*
11356 	 * We need to allocate an id for this aggregation.
11357 	 */
11358 	aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1,
11359 	    VM_BESTFIT | VM_SLEEP);
11360 
11361 	if (aggid - 1 >= (dtrace_aggid_t)state->dts_naggregations) {
11362 		dtrace_aggregation_t **oaggs = state->dts_aggregations;
11363 		dtrace_aggregation_t **aggs;
11364 		int naggs = state->dts_naggregations << 1;
11365 		int onaggs = state->dts_naggregations;
11366 
11367 		ASSERT(aggid == (dtrace_aggid_t)state->dts_naggregations + 1);
11368 
11369 		if (naggs == 0) {
11370 			ASSERT(oaggs == NULL);
11371 			naggs = 1;
11372 		}
11373 
11374 		aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP);
11375 
11376 		if (oaggs != NULL) {
11377 			bcopy(oaggs, aggs, onaggs * sizeof (*aggs));
11378 			kmem_free(oaggs, onaggs * sizeof (*aggs));
11379 		}
11380 
11381 		state->dts_aggregations = aggs;
11382 		state->dts_naggregations = naggs;
11383 	}
11384 
11385 	ASSERT(state->dts_aggregations[aggid - 1] == NULL);
11386 	state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg;
11387 
11388 	frec = &agg->dtag_first->dta_rec;
11389 	if (frec->dtrd_alignment < sizeof (dtrace_aggid_t))
11390 		frec->dtrd_alignment = sizeof (dtrace_aggid_t);
11391 
11392 	for (act = agg->dtag_first; act != NULL; act = act->dta_next) {
11393 		ASSERT(!act->dta_intuple);
11394 		act->dta_intuple = 1;
11395 	}
11396 
11397 	return (&agg->dtag_action);
11398 }
11399 
11400 static void
dtrace_ecb_aggregation_destroy(dtrace_ecb_t * ecb,dtrace_action_t * act)11401 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act)
11402 {
11403 	dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
11404 	dtrace_state_t *state = ecb->dte_state;
11405 	dtrace_aggid_t aggid = agg->dtag_id;
11406 
11407 	ASSERT(DTRACEACT_ISAGG(act->dta_kind));
11408 	vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1);
11409 
11410 	ASSERT(state->dts_aggregations[aggid - 1] == agg);
11411 	state->dts_aggregations[aggid - 1] = NULL;
11412 
11413 	kmem_free(agg, sizeof (dtrace_aggregation_t));
11414 }
11415 
11416 static int
dtrace_ecb_action_add(dtrace_ecb_t * ecb,dtrace_actdesc_t * desc)11417 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
11418 {
11419 	dtrace_action_t *action, *last;
11420 	dtrace_difo_t *dp = desc->dtad_difo;
11421 	uint32_t size = 0, align = sizeof (uint8_t), mask;
11422 	uint16_t format = 0;
11423 	dtrace_recdesc_t *rec;
11424 	dtrace_state_t *state = ecb->dte_state;
11425 	dtrace_optval_t *opt = state->dts_options;
11426 	dtrace_optval_t nframes=0, strsize;
11427 	uint64_t arg = desc->dtad_arg;
11428 
11429 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
11430 	ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1);
11431 
11432 	if (DTRACEACT_ISAGG(desc->dtad_kind)) {
11433 		/*
11434 		 * If this is an aggregating action, there must be neither
11435 		 * a speculate nor a commit on the action chain.
11436 		 */
11437 		dtrace_action_t *act;
11438 
11439 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
11440 			if (act->dta_kind == DTRACEACT_COMMIT)
11441 				return (EINVAL);
11442 
11443 			if (act->dta_kind == DTRACEACT_SPECULATE)
11444 				return (EINVAL);
11445 		}
11446 
11447 		action = dtrace_ecb_aggregation_create(ecb, desc);
11448 
11449 		if (action == NULL)
11450 			return (EINVAL);
11451 	} else {
11452 		if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) ||
11453 		    (desc->dtad_kind == DTRACEACT_DIFEXPR &&
11454 		    dp != NULL && dp->dtdo_destructive)) {
11455 			state->dts_destructive = 1;
11456 		}
11457 
11458 		switch (desc->dtad_kind) {
11459 		case DTRACEACT_PRINTF:
11460 		case DTRACEACT_PRINTA:
11461 		case DTRACEACT_SYSTEM:
11462 		case DTRACEACT_FREOPEN:
11463 		case DTRACEACT_DIFEXPR:
11464 			/*
11465 			 * We know that our arg is a string -- turn it into a
11466 			 * format.
11467 			 */
11468 			if (arg == 0) {
11469 				ASSERT(desc->dtad_kind == DTRACEACT_PRINTA ||
11470 				       desc->dtad_kind == DTRACEACT_DIFEXPR);
11471 				format = 0;
11472 			} else {
11473 				ASSERT(arg != 0);
11474 				ASSERT(arg > KERNELBASE);
11475 				format = dtrace_format_add(state,
11476 				    (char *)(uintptr_t)arg);
11477 			}
11478 
11479 			OS_FALLTHROUGH;
11480 		case DTRACEACT_LIBACT:
11481 		case DTRACEACT_TRACEMEM:
11482 		case DTRACEACT_TRACEMEM_DYNSIZE:
11483 		case DTRACEACT_APPLEBINARY:	/* __APPLE__ */
11484 			if (dp == NULL)
11485 				return (EINVAL);
11486 
11487 			if ((size = dp->dtdo_rtype.dtdt_size) != 0)
11488 				break;
11489 
11490 			if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
11491 				if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11492 					return (EINVAL);
11493 
11494 				size = opt[DTRACEOPT_STRSIZE];
11495 			}
11496 
11497 			break;
11498 
11499 		case DTRACEACT_STACK:
11500 			if ((nframes = arg) == 0) {
11501 				nframes = opt[DTRACEOPT_STACKFRAMES];
11502 				ASSERT(nframes > 0);
11503 				arg = nframes;
11504 			}
11505 
11506 			size = nframes * sizeof (pc_t);
11507 			break;
11508 
11509 		case DTRACEACT_JSTACK:
11510 			if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0)
11511 				strsize = opt[DTRACEOPT_JSTACKSTRSIZE];
11512 
11513 			if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0)
11514 				nframes = opt[DTRACEOPT_JSTACKFRAMES];
11515 
11516 			arg = DTRACE_USTACK_ARG(nframes, strsize);
11517 
11518 			OS_FALLTHROUGH;
11519 		case DTRACEACT_USTACK:
11520 			if (desc->dtad_kind != DTRACEACT_JSTACK &&
11521 			    (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) {
11522 				strsize = DTRACE_USTACK_STRSIZE(arg);
11523 				nframes = opt[DTRACEOPT_USTACKFRAMES];
11524 				ASSERT(nframes > 0);
11525 				arg = DTRACE_USTACK_ARG(nframes, strsize);
11526 			}
11527 
11528 			/*
11529 			 * Save a slot for the pid.
11530 			 */
11531 			size = (nframes + 1) * sizeof (uint64_t);
11532 			size += DTRACE_USTACK_STRSIZE(arg);
11533 			size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t)));
11534 
11535 			break;
11536 
11537 		case DTRACEACT_SYM:
11538 		case DTRACEACT_MOD:
11539 			if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) !=
11540 			    sizeof (uint64_t)) ||
11541 			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11542 				return (EINVAL);
11543 			break;
11544 
11545 		case DTRACEACT_USYM:
11546 		case DTRACEACT_UMOD:
11547 		case DTRACEACT_UADDR:
11548 			if (dp == NULL ||
11549 			    (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) ||
11550 			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11551 				return (EINVAL);
11552 
11553 			/*
11554 			 * We have a slot for the pid, plus a slot for the
11555 			 * argument.  To keep things simple (aligned with
11556 			 * bitness-neutral sizing), we store each as a 64-bit
11557 			 * quantity.
11558 			 */
11559 			size = 2 * sizeof (uint64_t);
11560 			break;
11561 
11562 		case DTRACEACT_STOP:
11563 		case DTRACEACT_BREAKPOINT:
11564 		case DTRACEACT_PANIC:
11565 			break;
11566 
11567 		case DTRACEACT_CHILL:
11568 		case DTRACEACT_DISCARD:
11569 		case DTRACEACT_RAISE:
11570 		case DTRACEACT_PIDRESUME:	/* __APPLE__ */
11571 			if (dp == NULL)
11572 				return (EINVAL);
11573 			break;
11574 
11575 		case DTRACEACT_EXIT:
11576 			if (dp == NULL ||
11577 			    (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) ||
11578 			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11579 				return (EINVAL);
11580 			break;
11581 
11582 		case DTRACEACT_SPECULATE:
11583 			if (ecb->dte_size > sizeof (dtrace_rechdr_t))
11584 				return (EINVAL);
11585 
11586 			if (dp == NULL)
11587 				return (EINVAL);
11588 
11589 			state->dts_speculates = 1;
11590 			break;
11591 
11592 		case DTRACEACT_COMMIT: {
11593 			dtrace_action_t *act = ecb->dte_action;
11594 
11595 			for (; act != NULL; act = act->dta_next) {
11596 				if (act->dta_kind == DTRACEACT_COMMIT)
11597 					return (EINVAL);
11598 			}
11599 
11600 			if (dp == NULL)
11601 				return (EINVAL);
11602 			break;
11603 		}
11604 
11605 		default:
11606 			return (EINVAL);
11607 		}
11608 
11609 		if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) {
11610 			/*
11611 			 * If this is a data-storing action or a speculate,
11612 			 * we must be sure that there isn't a commit on the
11613 			 * action chain.
11614 			 */
11615 			dtrace_action_t *act = ecb->dte_action;
11616 
11617 			for (; act != NULL; act = act->dta_next) {
11618 				if (act->dta_kind == DTRACEACT_COMMIT)
11619 					return (EINVAL);
11620 			}
11621 		}
11622 
11623 		action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP);
11624 		action->dta_rec.dtrd_size = size;
11625 	}
11626 
11627 	action->dta_refcnt = 1;
11628 	rec = &action->dta_rec;
11629 	size = rec->dtrd_size;
11630 
11631 	for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) {
11632 		if (!(size & mask)) {
11633 			align = mask + 1;
11634 			break;
11635 		}
11636 	}
11637 
11638 	action->dta_kind = desc->dtad_kind;
11639 
11640 	if ((action->dta_difo = dp) != NULL)
11641 		dtrace_difo_hold(dp);
11642 
11643 	rec->dtrd_action = action->dta_kind;
11644 	rec->dtrd_arg = arg;
11645 	rec->dtrd_uarg = desc->dtad_uarg;
11646 	rec->dtrd_alignment = (uint16_t)align;
11647 	rec->dtrd_format = format;
11648 
11649 	if ((last = ecb->dte_action_last) != NULL) {
11650 		ASSERT(ecb->dte_action != NULL);
11651 		action->dta_prev = last;
11652 		last->dta_next = action;
11653 	} else {
11654 		ASSERT(ecb->dte_action == NULL);
11655 		ecb->dte_action = action;
11656 	}
11657 
11658 	ecb->dte_action_last = action;
11659 
11660 	return (0);
11661 }
11662 
11663 static void
dtrace_ecb_action_remove(dtrace_ecb_t * ecb)11664 dtrace_ecb_action_remove(dtrace_ecb_t *ecb)
11665 {
11666 	dtrace_action_t *act = ecb->dte_action, *next;
11667 	dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate;
11668 	dtrace_difo_t *dp;
11669 	uint16_t format;
11670 
11671 	if (act != NULL && act->dta_refcnt > 1) {
11672 		ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1);
11673 		act->dta_refcnt--;
11674 	} else {
11675 		for (; act != NULL; act = next) {
11676 			next = act->dta_next;
11677 			ASSERT(next != NULL || act == ecb->dte_action_last);
11678 			ASSERT(act->dta_refcnt == 1);
11679 
11680 			if ((format = act->dta_rec.dtrd_format) != 0)
11681 				dtrace_format_remove(ecb->dte_state, format);
11682 
11683 			if ((dp = act->dta_difo) != NULL)
11684 				dtrace_difo_release(dp, vstate);
11685 
11686 			if (DTRACEACT_ISAGG(act->dta_kind)) {
11687 				dtrace_ecb_aggregation_destroy(ecb, act);
11688 			} else {
11689 				kmem_free(act, sizeof (dtrace_action_t));
11690 			}
11691 		}
11692 	}
11693 
11694 	ecb->dte_action = NULL;
11695 	ecb->dte_action_last = NULL;
11696 	ecb->dte_size = 0;
11697 }
11698 
11699 static void
dtrace_ecb_disable(dtrace_ecb_t * ecb)11700 dtrace_ecb_disable(dtrace_ecb_t *ecb)
11701 {
11702 	/*
11703 	 * We disable the ECB by removing it from its probe.
11704 	 */
11705 	dtrace_ecb_t *pecb, *prev = NULL;
11706 	dtrace_probe_t *probe = ecb->dte_probe;
11707 
11708 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
11709 
11710 	if (probe == NULL) {
11711 		/*
11712 		 * This is the NULL probe; there is nothing to disable.
11713 		 */
11714 		return;
11715 	}
11716 
11717 	for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) {
11718 		if (pecb == ecb)
11719 			break;
11720 		prev = pecb;
11721 	}
11722 
11723 	ASSERT(pecb != NULL);
11724 
11725 	if (prev == NULL) {
11726 		probe->dtpr_ecb = ecb->dte_next;
11727 	} else {
11728 		prev->dte_next = ecb->dte_next;
11729 	}
11730 
11731 	if (ecb == probe->dtpr_ecb_last) {
11732 		ASSERT(ecb->dte_next == NULL);
11733 		probe->dtpr_ecb_last = prev;
11734 	}
11735 
11736 	probe->dtpr_provider->dtpv_ecb_count--;
11737 	/*
11738 	 * The ECB has been disconnected from the probe; now sync to assure
11739 	 * that all CPUs have seen the change before returning.
11740 	 */
11741 	dtrace_sync();
11742 
11743 	if (probe->dtpr_ecb == NULL) {
11744 		/*
11745 		 * That was the last ECB on the probe; clear the predicate
11746 		 * cache ID for the probe, disable it and sync one more time
11747 		 * to assure that we'll never hit it again.
11748 		 */
11749 		dtrace_provider_t *prov = probe->dtpr_provider;
11750 
11751 		ASSERT(ecb->dte_next == NULL);
11752 		ASSERT(probe->dtpr_ecb_last == NULL);
11753 		probe->dtpr_predcache = DTRACE_CACHEIDNONE;
11754 		prov->dtpv_pops.dtps_disable(prov->dtpv_arg,
11755 		    probe->dtpr_id, probe->dtpr_arg);
11756 		dtrace_sync();
11757 	} else {
11758 		/*
11759 		 * There is at least one ECB remaining on the probe.  If there
11760 		 * is _exactly_ one, set the probe's predicate cache ID to be
11761 		 * the predicate cache ID of the remaining ECB.
11762 		 */
11763 		ASSERT(probe->dtpr_ecb_last != NULL);
11764 		ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE);
11765 
11766 		if (probe->dtpr_ecb == probe->dtpr_ecb_last) {
11767 			dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate;
11768 
11769 			ASSERT(probe->dtpr_ecb->dte_next == NULL);
11770 
11771 			if (p != NULL)
11772 				probe->dtpr_predcache = p->dtp_cacheid;
11773 		}
11774 
11775 		ecb->dte_next = NULL;
11776 	}
11777 }
11778 
11779 static void
dtrace_ecb_destroy(dtrace_ecb_t * ecb)11780 dtrace_ecb_destroy(dtrace_ecb_t *ecb)
11781 {
11782 	dtrace_state_t *state = ecb->dte_state;
11783 	dtrace_vstate_t *vstate = &state->dts_vstate;
11784 	dtrace_predicate_t *pred;
11785 	dtrace_epid_t epid = ecb->dte_epid;
11786 
11787 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
11788 	ASSERT(ecb->dte_next == NULL);
11789 	ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb);
11790 
11791 	if ((pred = ecb->dte_predicate) != NULL)
11792 		dtrace_predicate_release(pred, vstate);
11793 
11794 	dtrace_ecb_action_remove(ecb);
11795 
11796 	ASSERT(state->dts_ecbs[epid - 1] == ecb);
11797 	state->dts_ecbs[epid - 1] = NULL;
11798 
11799 	kmem_free(ecb, sizeof (dtrace_ecb_t));
11800 }
11801 
11802 static dtrace_ecb_t *
dtrace_ecb_create(dtrace_state_t * state,dtrace_probe_t * probe,dtrace_enabling_t * enab)11803 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe,
11804     dtrace_enabling_t *enab)
11805 {
11806 	dtrace_ecb_t *ecb;
11807 	dtrace_predicate_t *pred;
11808 	dtrace_actdesc_t *act;
11809 	dtrace_provider_t *prov;
11810 	dtrace_ecbdesc_t *desc = enab->dten_current;
11811 
11812 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
11813 	ASSERT(state != NULL);
11814 
11815 	ecb = dtrace_ecb_add(state, probe);
11816 	ecb->dte_uarg = desc->dted_uarg;
11817 
11818 	if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) {
11819 		dtrace_predicate_hold(pred);
11820 		ecb->dte_predicate = pred;
11821 	}
11822 
11823 	if (probe != NULL) {
11824 		/*
11825 		 * If the provider shows more leg than the consumer is old
11826 		 * enough to see, we need to enable the appropriate implicit
11827 		 * predicate bits to prevent the ecb from activating at
11828 		 * revealing times.
11829 		 *
11830 		 * Providers specifying DTRACE_PRIV_USER at register time
11831 		 * are stating that they need the /proc-style privilege
11832 		 * model to be enforced, and this is what DTRACE_COND_OWNER
11833 		 * and DTRACE_COND_ZONEOWNER will then do at probe time.
11834 		 */
11835 		prov = probe->dtpr_provider;
11836 		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) &&
11837 		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
11838 			ecb->dte_cond |= DTRACE_COND_OWNER;
11839 
11840 		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) &&
11841 		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
11842 			ecb->dte_cond |= DTRACE_COND_ZONEOWNER;
11843 
11844 		/*
11845 		 * If the provider shows us kernel innards and the user
11846 		 * is lacking sufficient privilege, enable the
11847 		 * DTRACE_COND_USERMODE implicit predicate.
11848 		 */
11849 		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) &&
11850 		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL))
11851 			ecb->dte_cond |= DTRACE_COND_USERMODE;
11852 	}
11853 
11854 	if (dtrace_ecb_create_cache != NULL) {
11855 		/*
11856 		 * If we have a cached ecb, we'll use its action list instead
11857 		 * of creating our own (saving both time and space).
11858 		 */
11859 		dtrace_ecb_t *cached = dtrace_ecb_create_cache;
11860 		dtrace_action_t *act_if = cached->dte_action;
11861 
11862 		if (act_if != NULL) {
11863 			ASSERT(act_if->dta_refcnt > 0);
11864 			act_if->dta_refcnt++;
11865 			ecb->dte_action = act_if;
11866 			ecb->dte_action_last = cached->dte_action_last;
11867 			ecb->dte_needed = cached->dte_needed;
11868 			ecb->dte_size = cached->dte_size;
11869 			ecb->dte_alignment = cached->dte_alignment;
11870 		}
11871 
11872 		return (ecb);
11873 	}
11874 
11875 	for (act = desc->dted_action; act != NULL; act = act->dtad_next) {
11876 		if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) {
11877 			dtrace_ecb_destroy(ecb);
11878 			return (NULL);
11879 		}
11880 	}
11881 
11882 	if ((enab->dten_error = dtrace_ecb_resize(ecb)) != 0) {
11883 		dtrace_ecb_destroy(ecb);
11884 		return (NULL);
11885 	}
11886 
11887 	return (dtrace_ecb_create_cache = ecb);
11888 }
11889 
11890 static int
dtrace_ecb_create_enable(dtrace_probe_t * probe,void * arg1,void * arg2)11891 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg1, void *arg2)
11892 {
11893 	dtrace_ecb_t *ecb;
11894 	dtrace_enabling_t *enab = arg1;
11895 	dtrace_ecbdesc_t *ep = arg2;
11896 	dtrace_state_t *state = enab->dten_vstate->dtvs_state;
11897 
11898 	ASSERT(state != NULL);
11899 
11900 	if (probe != NULL && ep != NULL && probe->dtpr_gen < ep->dted_probegen) {
11901 		/*
11902 		 * This probe was created in a generation for which this
11903 		 * enabling has previously created ECBs; we don't want to
11904 		 * enable it again, so just kick out.
11905 		 */
11906 		return (DTRACE_MATCH_NEXT);
11907 	}
11908 
11909 	if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL)
11910 		return (DTRACE_MATCH_DONE);
11911 
11912 	if (dtrace_ecb_enable(ecb) < 0)
11913                return (DTRACE_MATCH_FAIL);
11914 
11915 	return (DTRACE_MATCH_NEXT);
11916 }
11917 
11918 static dtrace_ecb_t *
dtrace_epid2ecb(dtrace_state_t * state,dtrace_epid_t id)11919 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id)
11920 {
11921 	dtrace_ecb_t *ecb;
11922 #pragma unused(ecb) /* __APPLE__ */
11923 
11924 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
11925 
11926 	if (id == 0 || id > (dtrace_epid_t)state->dts_necbs)
11927 		return (NULL);
11928 
11929 	ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL);
11930 	ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id);
11931 
11932 	return (state->dts_ecbs[id - 1]);
11933 }
11934 
11935 static dtrace_aggregation_t *
dtrace_aggid2agg(dtrace_state_t * state,dtrace_aggid_t id)11936 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id)
11937 {
11938 	dtrace_aggregation_t *agg;
11939 #pragma unused(agg) /* __APPLE__ */
11940 
11941 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
11942 
11943 	if (id == 0 || id > (dtrace_aggid_t)state->dts_naggregations)
11944 		return (NULL);
11945 
11946 	ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL);
11947 	ASSERT((agg = state->dts_aggregations[id - 1]) == NULL ||
11948 	    agg->dtag_id == id);
11949 
11950 	return (state->dts_aggregations[id - 1]);
11951 }
11952 
11953 /*
11954  * DTrace Buffer Functions
11955  *
11956  * The following functions manipulate DTrace buffers.  Most of these functions
11957  * are called in the context of establishing or processing consumer state;
11958  * exceptions are explicitly noted.
11959  */
11960 
11961 /*
11962  * Note:  called from cross call context.  This function switches the two
11963  * buffers on a given CPU.  The atomicity of this operation is assured by
11964  * disabling interrupts while the actual switch takes place; the disabling of
11965  * interrupts serializes the execution with any execution of dtrace_probe() on
11966  * the same CPU.
11967  */
11968 static void
dtrace_buffer_switch(dtrace_buffer_t * buf)11969 dtrace_buffer_switch(dtrace_buffer_t *buf)
11970 {
11971 	caddr_t tomax = buf->dtb_tomax;
11972 	caddr_t xamot = buf->dtb_xamot;
11973 	dtrace_icookie_t cookie;
11974 	hrtime_t now;
11975 
11976 	ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
11977 	ASSERT(!(buf->dtb_flags & DTRACEBUF_RING));
11978 
11979 	cookie = dtrace_interrupt_disable();
11980 	now = dtrace_gethrtime();
11981 	buf->dtb_tomax = xamot;
11982 	buf->dtb_xamot = tomax;
11983 	buf->dtb_xamot_drops = buf->dtb_drops;
11984 	buf->dtb_xamot_offset = buf->dtb_offset;
11985 	buf->dtb_xamot_errors = buf->dtb_errors;
11986 	buf->dtb_xamot_flags = buf->dtb_flags;
11987 	buf->dtb_offset = 0;
11988 	buf->dtb_drops = 0;
11989 	buf->dtb_errors = 0;
11990 	buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED);
11991 	buf->dtb_interval = now - buf->dtb_switched;
11992 	buf->dtb_switched = now;
11993 	buf->dtb_cur_limit = buf->dtb_limit;
11994 
11995 	dtrace_interrupt_enable(cookie);
11996 }
11997 
11998 /*
11999  * Note:  called from cross call context.  This function activates a buffer
12000  * on a CPU.  As with dtrace_buffer_switch(), the atomicity of the operation
12001  * is guaranteed by the disabling of interrupts.
12002  */
12003 static void
dtrace_buffer_activate(dtrace_state_t * state)12004 dtrace_buffer_activate(dtrace_state_t *state)
12005 {
12006 	dtrace_buffer_t *buf;
12007 	dtrace_icookie_t cookie = dtrace_interrupt_disable();
12008 
12009 	buf = &state->dts_buffer[CPU->cpu_id];
12010 
12011 	if (buf->dtb_tomax != NULL) {
12012 		/*
12013 		 * We might like to assert that the buffer is marked inactive,
12014 		 * but this isn't necessarily true:  the buffer for the CPU
12015 		 * that processes the BEGIN probe has its buffer activated
12016 		 * manually.  In this case, we take the (harmless) action
12017 		 * re-clearing the bit INACTIVE bit.
12018 		 */
12019 		buf->dtb_flags &= ~DTRACEBUF_INACTIVE;
12020 	}
12021 
12022 	dtrace_interrupt_enable(cookie);
12023 }
12024 
12025 static int
dtrace_buffer_canalloc(size_t size)12026 dtrace_buffer_canalloc(size_t size)
12027 {
12028 	if (size > (UINT64_MAX - dtrace_buffer_memory_inuse))
12029 		return (B_FALSE);
12030 	if ((size + dtrace_buffer_memory_inuse) > dtrace_buffer_memory_maxsize)
12031 		return (B_FALSE);
12032 
12033 	return (B_TRUE);
12034 }
12035 
12036 static int
dtrace_buffer_alloc(dtrace_buffer_t * bufs,size_t limit,size_t size,int flags,processorid_t cpu)12037 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t limit, size_t size, int flags,
12038     processorid_t cpu)
12039 {
12040 	dtrace_cpu_t *cp;
12041 	dtrace_buffer_t *buf;
12042 	size_t size_before_alloc = dtrace_buffer_memory_inuse;
12043 
12044 	LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
12045 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
12046 
12047 	if (size > (size_t)dtrace_nonroot_maxsize &&
12048 	    !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE))
12049 		return (EFBIG);
12050 
12051 	cp = cpu_list;
12052 
12053 	do {
12054 		if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
12055 			continue;
12056 
12057 		buf = &bufs[cp->cpu_id];
12058 
12059 		/*
12060 		 * If there is already a buffer allocated for this CPU, it
12061 		 * is only possible that this is a DR event.  In this case,
12062 		 * the buffer size must match our specified size.
12063 		 */
12064 		if (buf->dtb_tomax != NULL) {
12065 			ASSERT(buf->dtb_size == size);
12066 			continue;
12067 		}
12068 
12069 		ASSERT(buf->dtb_xamot == NULL);
12070 
12071 		/* DTrace, please do not eat all the memory. */
12072 		if (dtrace_buffer_canalloc(size) == B_FALSE)
12073 			goto err;
12074 		if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL)
12075 			goto err;
12076 		dtrace_buffer_memory_inuse += size;
12077 
12078 		/* Unsure that limit is always lower than size */
12079 		limit = limit == size ? limit - 1 : limit;
12080 		buf->dtb_cur_limit = limit;
12081 		buf->dtb_limit = limit;
12082 		buf->dtb_size = size;
12083 		buf->dtb_flags = flags;
12084 		buf->dtb_offset = 0;
12085 		buf->dtb_drops = 0;
12086 
12087 		if (flags & DTRACEBUF_NOSWITCH)
12088 			continue;
12089 
12090 		/* DTrace, please do not eat all the memory. */
12091 		if (dtrace_buffer_canalloc(size) == B_FALSE)
12092 			goto err;
12093 		if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL)
12094 			goto err;
12095 		dtrace_buffer_memory_inuse += size;
12096 	} while ((cp = cp->cpu_next) != cpu_list);
12097 
12098 	ASSERT(dtrace_buffer_memory_inuse <= dtrace_buffer_memory_maxsize);
12099 
12100 	return (0);
12101 
12102 err:
12103 	cp = cpu_list;
12104 
12105 	do {
12106 		if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
12107 			continue;
12108 
12109 		buf = &bufs[cp->cpu_id];
12110 
12111 		if (buf->dtb_xamot != NULL) {
12112 			ASSERT(buf->dtb_tomax != NULL);
12113 			ASSERT(buf->dtb_size == size);
12114 			kmem_free(buf->dtb_xamot, size);
12115 		}
12116 
12117 		if (buf->dtb_tomax != NULL) {
12118 			ASSERT(buf->dtb_size == size);
12119 			kmem_free(buf->dtb_tomax, size);
12120 		}
12121 
12122 		buf->dtb_tomax = NULL;
12123 		buf->dtb_xamot = NULL;
12124 		buf->dtb_size = 0;
12125 	} while ((cp = cp->cpu_next) != cpu_list);
12126 
12127 	/* Restore the size saved before allocating memory */
12128 	dtrace_buffer_memory_inuse = size_before_alloc;
12129 
12130 	return (ENOMEM);
12131 }
12132 
12133 /*
12134  * Note:  called from probe context.  This function just increments the drop
12135  * count on a buffer.  It has been made a function to allow for the
12136  * possibility of understanding the source of mysterious drop counts.  (A
12137  * problem for which one may be particularly disappointed that DTrace cannot
12138  * be used to understand DTrace.)
12139  */
12140 static void
dtrace_buffer_drop(dtrace_buffer_t * buf)12141 dtrace_buffer_drop(dtrace_buffer_t *buf)
12142 {
12143 	buf->dtb_drops++;
12144 }
12145 
12146 /*
12147  * Note:  called from probe context.  This function is called to reserve space
12148  * in a buffer.  If mstate is non-NULL, sets the scratch base and size in the
12149  * mstate.  Returns the new offset in the buffer, or a negative value if an
12150  * error has occurred.
12151  */
12152 static intptr_t
dtrace_buffer_reserve(dtrace_buffer_t * buf,size_t needed,size_t align,dtrace_state_t * state,dtrace_mstate_t * mstate)12153 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align,
12154     dtrace_state_t *state, dtrace_mstate_t *mstate)
12155 {
12156 	intptr_t offs = buf->dtb_offset, soffs;
12157 	intptr_t woffs;
12158 	caddr_t tomax;
12159 	size_t total_off;
12160 
12161 	if (buf->dtb_flags & DTRACEBUF_INACTIVE)
12162 		return (-1);
12163 
12164 	if ((tomax = buf->dtb_tomax) == NULL) {
12165 		dtrace_buffer_drop(buf);
12166 		return (-1);
12167 	}
12168 
12169 	if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) {
12170 		while (offs & (align - 1)) {
12171 			/*
12172 			 * Assert that our alignment is off by a number which
12173 			 * is itself sizeof (uint32_t) aligned.
12174 			 */
12175 			ASSERT(!((align - (offs & (align - 1))) &
12176 			    (sizeof (uint32_t) - 1)));
12177 			DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
12178 			offs += sizeof (uint32_t);
12179 		}
12180 
12181 		if ((uint64_t)(soffs = offs + needed) > buf->dtb_cur_limit) {
12182 			if (buf->dtb_cur_limit == buf->dtb_limit) {
12183 				buf->dtb_cur_limit = buf->dtb_size;
12184 
12185 				os_atomic_inc(&state->dts_buf_over_limit, relaxed);
12186 				/**
12187 				 * Set an AST on the current processor
12188 				 * so that we can wake up the process
12189 				 * outside of probe context, when we know
12190 				 * it is safe to do so
12191 				 */
12192 				minor_t minor = getminor(state->dts_dev);
12193 				ASSERT(minor < 32);
12194 
12195 				os_atomic_or(&dtrace_wake_clients, 1 << minor, relaxed);
12196 				ast_dtrace_on();
12197 			}
12198 			if ((uint64_t)soffs > buf->dtb_size) {
12199 				dtrace_buffer_drop(buf);
12200 				return (-1);
12201 			}
12202 		}
12203 
12204 		if (mstate == NULL)
12205 			return (offs);
12206 
12207 		mstate->dtms_scratch_base = (uintptr_t)tomax + soffs;
12208 		mstate->dtms_scratch_size = buf->dtb_size - soffs;
12209 		mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
12210 
12211 		return (offs);
12212 	}
12213 
12214 	if (buf->dtb_flags & DTRACEBUF_FILL) {
12215 		if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN &&
12216 		    (buf->dtb_flags & DTRACEBUF_FULL))
12217 			return (-1);
12218 		goto out;
12219 	}
12220 
12221 	total_off = needed + (offs & (align - 1));
12222 
12223 	/*
12224 	 * For a ring buffer, life is quite a bit more complicated.  Before
12225 	 * we can store any padding, we need to adjust our wrapping offset.
12226 	 * (If we've never before wrapped or we're not about to, no adjustment
12227 	 * is required.)
12228 	 */
12229 	if ((buf->dtb_flags & DTRACEBUF_WRAPPED) ||
12230 	    offs + total_off > buf->dtb_size) {
12231 		woffs = buf->dtb_xamot_offset;
12232 
12233 		if (offs + total_off > buf->dtb_size) {
12234 			/*
12235 			 * We can't fit in the end of the buffer.  First, a
12236 			 * sanity check that we can fit in the buffer at all.
12237 			 */
12238 			if (total_off > buf->dtb_size) {
12239 				dtrace_buffer_drop(buf);
12240 				return (-1);
12241 			}
12242 
12243 			/*
12244 			 * We're going to be storing at the top of the buffer,
12245 			 * so now we need to deal with the wrapped offset.  We
12246 			 * only reset our wrapped offset to 0 if it is
12247 			 * currently greater than the current offset.  If it
12248 			 * is less than the current offset, it is because a
12249 			 * previous allocation induced a wrap -- but the
12250 			 * allocation didn't subsequently take the space due
12251 			 * to an error or false predicate evaluation.  In this
12252 			 * case, we'll just leave the wrapped offset alone: if
12253 			 * the wrapped offset hasn't been advanced far enough
12254 			 * for this allocation, it will be adjusted in the
12255 			 * lower loop.
12256 			 */
12257 			if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
12258 				if (woffs >= offs)
12259 					woffs = 0;
12260 			} else {
12261 				woffs = 0;
12262 			}
12263 
12264 			/*
12265 			 * Now we know that we're going to be storing to the
12266 			 * top of the buffer and that there is room for us
12267 			 * there.  We need to clear the buffer from the current
12268 			 * offset to the end (there may be old gunk there).
12269 			 */
12270 			while ((uint64_t)offs < buf->dtb_size)
12271 				tomax[offs++] = 0;
12272 
12273 			/*
12274 			 * We need to set our offset to zero.  And because we
12275 			 * are wrapping, we need to set the bit indicating as
12276 			 * much.  We can also adjust our needed space back
12277 			 * down to the space required by the ECB -- we know
12278 			 * that the top of the buffer is aligned.
12279 			 */
12280 			offs = 0;
12281 			total_off = needed;
12282 			buf->dtb_flags |= DTRACEBUF_WRAPPED;
12283 		} else {
12284 			/*
12285 			 * There is room for us in the buffer, so we simply
12286 			 * need to check the wrapped offset.
12287 			 */
12288 			if (woffs < offs) {
12289 				/*
12290 				 * The wrapped offset is less than the offset.
12291 				 * This can happen if we allocated buffer space
12292 				 * that induced a wrap, but then we didn't
12293 				 * subsequently take the space due to an error
12294 				 * or false predicate evaluation.  This is
12295 				 * okay; we know that _this_ allocation isn't
12296 				 * going to induce a wrap.  We still can't
12297 				 * reset the wrapped offset to be zero,
12298 				 * however: the space may have been trashed in
12299 				 * the previous failed probe attempt.  But at
12300 				 * least the wrapped offset doesn't need to
12301 				 * be adjusted at all...
12302 				 */
12303 				goto out;
12304 			}
12305 		}
12306 
12307 		while (offs + total_off > (size_t)woffs) {
12308 			dtrace_epid_t epid = *(uint32_t *)(tomax + woffs);
12309 			size_t size;
12310 
12311 			if (epid == DTRACE_EPIDNONE) {
12312 				size = sizeof (uint32_t);
12313 			} else {
12314 				ASSERT(epid <= (dtrace_epid_t)state->dts_necbs);
12315 				ASSERT(state->dts_ecbs[epid - 1] != NULL);
12316 
12317 				size = state->dts_ecbs[epid - 1]->dte_size;
12318 			}
12319 
12320 			ASSERT(woffs + size <= buf->dtb_size);
12321 			ASSERT(size != 0);
12322 
12323 			if (woffs + size == buf->dtb_size) {
12324 				/*
12325 				 * We've reached the end of the buffer; we want
12326 				 * to set the wrapped offset to 0 and break
12327 				 * out.  However, if the offs is 0, then we're
12328 				 * in a strange edge-condition:  the amount of
12329 				 * space that we want to reserve plus the size
12330 				 * of the record that we're overwriting is
12331 				 * greater than the size of the buffer.  This
12332 				 * is problematic because if we reserve the
12333 				 * space but subsequently don't consume it (due
12334 				 * to a failed predicate or error) the wrapped
12335 				 * offset will be 0 -- yet the EPID at offset 0
12336 				 * will not be committed.  This situation is
12337 				 * relatively easy to deal with:  if we're in
12338 				 * this case, the buffer is indistinguishable
12339 				 * from one that hasn't wrapped; we need only
12340 				 * finish the job by clearing the wrapped bit,
12341 				 * explicitly setting the offset to be 0, and
12342 				 * zero'ing out the old data in the buffer.
12343 				 */
12344 				if (offs == 0) {
12345 					buf->dtb_flags &= ~DTRACEBUF_WRAPPED;
12346 					buf->dtb_offset = 0;
12347 					woffs = total_off;
12348 
12349 					while ((uint64_t)woffs < buf->dtb_size)
12350 						tomax[woffs++] = 0;
12351 				}
12352 
12353 				woffs = 0;
12354 				break;
12355 			}
12356 
12357 			woffs += size;
12358 		}
12359 
12360 		/*
12361 		 * We have a wrapped offset.  It may be that the wrapped offset
12362 		 * has become zero -- that's okay.
12363 		 */
12364 		buf->dtb_xamot_offset = woffs;
12365 	}
12366 
12367 out:
12368 	/*
12369 	 * Now we can plow the buffer with any necessary padding.
12370 	 */
12371 	while (offs & (align - 1)) {
12372 		/*
12373 		 * Assert that our alignment is off by a number which
12374 		 * is itself sizeof (uint32_t) aligned.
12375 		 */
12376 		ASSERT(!((align - (offs & (align - 1))) &
12377 		    (sizeof (uint32_t) - 1)));
12378 		DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
12379 		offs += sizeof (uint32_t);
12380 	}
12381 
12382 	if (buf->dtb_flags & DTRACEBUF_FILL) {
12383 		if (offs + needed > buf->dtb_size - state->dts_reserve) {
12384 			buf->dtb_flags |= DTRACEBUF_FULL;
12385 			return (-1);
12386 		}
12387 	}
12388 
12389 	if (mstate == NULL)
12390 		return (offs);
12391 
12392 	/*
12393 	 * For ring buffers and fill buffers, the scratch space is always
12394 	 * the inactive buffer.
12395 	 */
12396 	mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot;
12397 	mstate->dtms_scratch_size = buf->dtb_size;
12398 	mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
12399 
12400 	return (offs);
12401 }
12402 
12403 static void
dtrace_buffer_polish(dtrace_buffer_t * buf)12404 dtrace_buffer_polish(dtrace_buffer_t *buf)
12405 {
12406 	ASSERT(buf->dtb_flags & DTRACEBUF_RING);
12407 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
12408 
12409 	if (!(buf->dtb_flags & DTRACEBUF_WRAPPED))
12410 		return;
12411 
12412 	/*
12413 	 * We need to polish the ring buffer.  There are three cases:
12414 	 *
12415 	 * - The first (and presumably most common) is that there is no gap
12416 	 *   between the buffer offset and the wrapped offset.  In this case,
12417 	 *   there is nothing in the buffer that isn't valid data; we can
12418 	 *   mark the buffer as polished and return.
12419 	 *
12420 	 * - The second (less common than the first but still more common
12421 	 *   than the third) is that there is a gap between the buffer offset
12422 	 *   and the wrapped offset, and the wrapped offset is larger than the
12423 	 *   buffer offset.  This can happen because of an alignment issue, or
12424 	 *   can happen because of a call to dtrace_buffer_reserve() that
12425 	 *   didn't subsequently consume the buffer space.  In this case,
12426 	 *   we need to zero the data from the buffer offset to the wrapped
12427 	 *   offset.
12428 	 *
12429 	 * - The third (and least common) is that there is a gap between the
12430 	 *   buffer offset and the wrapped offset, but the wrapped offset is
12431 	 *   _less_ than the buffer offset.  This can only happen because a
12432 	 *   call to dtrace_buffer_reserve() induced a wrap, but the space
12433 	 *   was not subsequently consumed.  In this case, we need to zero the
12434 	 *   space from the offset to the end of the buffer _and_ from the
12435 	 *   top of the buffer to the wrapped offset.
12436 	 */
12437 	if (buf->dtb_offset < buf->dtb_xamot_offset) {
12438 		bzero(buf->dtb_tomax + buf->dtb_offset,
12439 		    buf->dtb_xamot_offset - buf->dtb_offset);
12440 	}
12441 
12442 	if (buf->dtb_offset > buf->dtb_xamot_offset) {
12443 		bzero(buf->dtb_tomax + buf->dtb_offset,
12444 		    buf->dtb_size - buf->dtb_offset);
12445 		bzero(buf->dtb_tomax, buf->dtb_xamot_offset);
12446 	}
12447 }
12448 
12449 static void
dtrace_buffer_free(dtrace_buffer_t * bufs)12450 dtrace_buffer_free(dtrace_buffer_t *bufs)
12451 {
12452 	int i;
12453 
12454 	for (i = 0; i < (int)NCPU; i++) {
12455 		dtrace_buffer_t *buf = &bufs[i];
12456 
12457 		if (buf->dtb_tomax == NULL) {
12458 			ASSERT(buf->dtb_xamot == NULL);
12459 			ASSERT(buf->dtb_size == 0);
12460 			continue;
12461 		}
12462 
12463 		if (buf->dtb_xamot != NULL) {
12464 			ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
12465 			kmem_free(buf->dtb_xamot, buf->dtb_size);
12466 
12467 			ASSERT(dtrace_buffer_memory_inuse >= buf->dtb_size);
12468 			dtrace_buffer_memory_inuse -= buf->dtb_size;
12469 		}
12470 
12471 		kmem_free(buf->dtb_tomax, buf->dtb_size);
12472 		ASSERT(dtrace_buffer_memory_inuse >= buf->dtb_size);
12473 		dtrace_buffer_memory_inuse -= buf->dtb_size;
12474 
12475 		buf->dtb_size = 0;
12476 		buf->dtb_tomax = NULL;
12477 		buf->dtb_xamot = NULL;
12478 	}
12479 }
12480 
12481 /*
12482  * DTrace Enabling Functions
12483  */
12484 static dtrace_enabling_t *
dtrace_enabling_create(dtrace_vstate_t * vstate)12485 dtrace_enabling_create(dtrace_vstate_t *vstate)
12486 {
12487 	dtrace_enabling_t *enab;
12488 
12489 	enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP);
12490 	enab->dten_vstate = vstate;
12491 
12492 	return (enab);
12493 }
12494 
12495 static void
dtrace_enabling_add(dtrace_enabling_t * enab,dtrace_ecbdesc_t * ecb)12496 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb)
12497 {
12498 	dtrace_ecbdesc_t **ndesc;
12499 	size_t osize, nsize;
12500 
12501 	/*
12502 	 * We can't add to enablings after we've enabled them, or after we've
12503 	 * retained them.
12504 	 */
12505 	ASSERT(enab->dten_probegen == 0);
12506 	ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
12507 
12508 	/* APPLE NOTE: this protects against gcc 4.0 botch on x86 */
12509 	if (ecb == NULL) return;
12510 
12511 	if (enab->dten_ndesc < enab->dten_maxdesc) {
12512 		enab->dten_desc[enab->dten_ndesc++] = ecb;
12513 		return;
12514 	}
12515 
12516 	osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
12517 
12518 	if (enab->dten_maxdesc == 0) {
12519 		enab->dten_maxdesc = 1;
12520 	} else {
12521 		enab->dten_maxdesc <<= 1;
12522 	}
12523 
12524 	ASSERT(enab->dten_ndesc < enab->dten_maxdesc);
12525 
12526 	nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
12527 	ndesc = kmem_zalloc(nsize, KM_SLEEP);
12528 	bcopy(enab->dten_desc, ndesc, osize);
12529 	kmem_free(enab->dten_desc, osize);
12530 
12531 	enab->dten_desc = ndesc;
12532 	enab->dten_desc[enab->dten_ndesc++] = ecb;
12533 }
12534 
12535 static void
dtrace_enabling_addlike(dtrace_enabling_t * enab,dtrace_ecbdesc_t * ecb,dtrace_probedesc_t * pd)12536 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb,
12537     dtrace_probedesc_t *pd)
12538 {
12539 	dtrace_ecbdesc_t *new;
12540 	dtrace_predicate_t *pred;
12541 	dtrace_actdesc_t *act;
12542 
12543 	/*
12544 	 * We're going to create a new ECB description that matches the
12545 	 * specified ECB in every way, but has the specified probe description.
12546 	 */
12547 	new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
12548 
12549 	if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL)
12550 		dtrace_predicate_hold(pred);
12551 
12552 	for (act = ecb->dted_action; act != NULL; act = act->dtad_next)
12553 		dtrace_actdesc_hold(act);
12554 
12555 	new->dted_action = ecb->dted_action;
12556 	new->dted_pred = ecb->dted_pred;
12557 	new->dted_probe = *pd;
12558 	new->dted_uarg = ecb->dted_uarg;
12559 
12560 	dtrace_enabling_add(enab, new);
12561 }
12562 
12563 static void
dtrace_enabling_dump(dtrace_enabling_t * enab)12564 dtrace_enabling_dump(dtrace_enabling_t *enab)
12565 {
12566 	int i;
12567 
12568 	for (i = 0; i < enab->dten_ndesc; i++) {
12569 		dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe;
12570 
12571 		cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i,
12572 		    desc->dtpd_provider, desc->dtpd_mod,
12573 		    desc->dtpd_func, desc->dtpd_name);
12574 	}
12575 }
12576 
12577 static void
dtrace_enabling_destroy(dtrace_enabling_t * enab)12578 dtrace_enabling_destroy(dtrace_enabling_t *enab)
12579 {
12580 	int i;
12581 	dtrace_ecbdesc_t *ep;
12582 	dtrace_vstate_t *vstate = enab->dten_vstate;
12583 
12584 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
12585 
12586 	for (i = 0; i < enab->dten_ndesc; i++) {
12587 		dtrace_actdesc_t *act, *next;
12588 		dtrace_predicate_t *pred;
12589 
12590 		ep = enab->dten_desc[i];
12591 
12592 		if ((pred = ep->dted_pred.dtpdd_predicate) != NULL)
12593 			dtrace_predicate_release(pred, vstate);
12594 
12595 		for (act = ep->dted_action; act != NULL; act = next) {
12596 			next = act->dtad_next;
12597 			dtrace_actdesc_release(act, vstate);
12598 		}
12599 
12600 		kmem_free(ep, sizeof (dtrace_ecbdesc_t));
12601 	}
12602 
12603 	kmem_free(enab->dten_desc,
12604 	    enab->dten_maxdesc * sizeof (dtrace_enabling_t *));
12605 
12606 	/*
12607 	 * If this was a retained enabling, decrement the dts_nretained count
12608 	 * and take it off of the dtrace_retained list.
12609 	 */
12610 	if (enab->dten_prev != NULL || enab->dten_next != NULL ||
12611 	    dtrace_retained == enab) {
12612 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
12613 		ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0);
12614 		enab->dten_vstate->dtvs_state->dts_nretained--;
12615                 dtrace_retained_gen++;
12616 	}
12617 
12618 	if (enab->dten_prev == NULL) {
12619 		if (dtrace_retained == enab) {
12620 			dtrace_retained = enab->dten_next;
12621 
12622 			if (dtrace_retained != NULL)
12623 				dtrace_retained->dten_prev = NULL;
12624 		}
12625 	} else {
12626 		ASSERT(enab != dtrace_retained);
12627 		ASSERT(dtrace_retained != NULL);
12628 		enab->dten_prev->dten_next = enab->dten_next;
12629 	}
12630 
12631 	if (enab->dten_next != NULL) {
12632 		ASSERT(dtrace_retained != NULL);
12633 		enab->dten_next->dten_prev = enab->dten_prev;
12634 	}
12635 
12636 	kmem_free(enab, sizeof (dtrace_enabling_t));
12637 }
12638 
12639 static int
dtrace_enabling_retain(dtrace_enabling_t * enab)12640 dtrace_enabling_retain(dtrace_enabling_t *enab)
12641 {
12642 	dtrace_state_t *state;
12643 
12644 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
12645 	ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
12646 	ASSERT(enab->dten_vstate != NULL);
12647 
12648 	state = enab->dten_vstate->dtvs_state;
12649 	ASSERT(state != NULL);
12650 
12651 	/*
12652 	 * We only allow each state to retain dtrace_retain_max enablings.
12653 	 */
12654 	if (state->dts_nretained >= dtrace_retain_max)
12655 		return (ENOSPC);
12656 
12657 	state->dts_nretained++;
12658         dtrace_retained_gen++;
12659 
12660 	if (dtrace_retained == NULL) {
12661 		dtrace_retained = enab;
12662 		return (0);
12663 	}
12664 
12665 	enab->dten_next = dtrace_retained;
12666 	dtrace_retained->dten_prev = enab;
12667 	dtrace_retained = enab;
12668 
12669 	return (0);
12670 }
12671 
12672 static int
dtrace_enabling_replicate(dtrace_state_t * state,dtrace_probedesc_t * match,dtrace_probedesc_t * create)12673 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match,
12674     dtrace_probedesc_t *create)
12675 {
12676 	dtrace_enabling_t *new, *enab;
12677 	int found = 0, err = ENOENT;
12678 
12679 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
12680 	ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN);
12681 	ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN);
12682 	ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN);
12683 	ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN);
12684 
12685 	new = dtrace_enabling_create(&state->dts_vstate);
12686 
12687 	/*
12688 	 * Iterate over all retained enablings, looking for enablings that
12689 	 * match the specified state.
12690 	 */
12691 	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
12692 		int i;
12693 
12694 		/*
12695 		 * dtvs_state can only be NULL for helper enablings -- and
12696 		 * helper enablings can't be retained.
12697 		 */
12698 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
12699 
12700 		if (enab->dten_vstate->dtvs_state != state)
12701 			continue;
12702 
12703 		/*
12704 		 * Now iterate over each probe description; we're looking for
12705 		 * an exact match to the specified probe description.
12706 		 */
12707 		for (i = 0; i < enab->dten_ndesc; i++) {
12708 			dtrace_ecbdesc_t *ep = enab->dten_desc[i];
12709 			dtrace_probedesc_t *pd = &ep->dted_probe;
12710 
12711 			/* APPLE NOTE: Darwin employs size bounded string operation. */
12712 			if (strncmp(pd->dtpd_provider, match->dtpd_provider, DTRACE_PROVNAMELEN))
12713 				continue;
12714 
12715 			if (strncmp(pd->dtpd_mod, match->dtpd_mod, DTRACE_MODNAMELEN))
12716 				continue;
12717 
12718 			if (strncmp(pd->dtpd_func, match->dtpd_func, DTRACE_FUNCNAMELEN))
12719 				continue;
12720 
12721 			if (strncmp(pd->dtpd_name, match->dtpd_name, DTRACE_NAMELEN))
12722 				continue;
12723 
12724 			/*
12725 			 * We have a winning probe!  Add it to our growing
12726 			 * enabling.
12727 			 */
12728 			found = 1;
12729 			dtrace_enabling_addlike(new, ep, create);
12730 		}
12731 	}
12732 
12733 	if (!found || (err = dtrace_enabling_retain(new)) != 0) {
12734 		dtrace_enabling_destroy(new);
12735 		return (err);
12736 	}
12737 
12738 	return (0);
12739 }
12740 
12741 static void
dtrace_enabling_retract(dtrace_state_t * state)12742 dtrace_enabling_retract(dtrace_state_t *state)
12743 {
12744 	dtrace_enabling_t *enab, *next;
12745 
12746 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
12747 
12748 	/*
12749 	 * Iterate over all retained enablings, destroy the enablings retained
12750 	 * for the specified state.
12751 	 */
12752 	for (enab = dtrace_retained; enab != NULL; enab = next) {
12753 		next = enab->dten_next;
12754 
12755 		/*
12756 		 * dtvs_state can only be NULL for helper enablings -- and
12757 		 * helper enablings can't be retained.
12758 		 */
12759 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
12760 
12761 		if (enab->dten_vstate->dtvs_state == state) {
12762 			ASSERT(state->dts_nretained > 0);
12763 			dtrace_enabling_destroy(enab);
12764 		}
12765 	}
12766 
12767 	ASSERT(state->dts_nretained == 0);
12768 }
12769 
12770 static int
dtrace_enabling_match(dtrace_enabling_t * enab,int * nmatched,dtrace_match_cond_t * cond)12771 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched, dtrace_match_cond_t *cond)
12772 {
12773 	int i = 0;
12774 	int total_matched = 0, matched = 0;
12775 
12776 	LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
12777 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
12778 
12779 	for (i = 0; i < enab->dten_ndesc; i++) {
12780 		dtrace_ecbdesc_t *ep = enab->dten_desc[i];
12781 
12782 		enab->dten_current = ep;
12783 		enab->dten_error = 0;
12784 
12785 		/**
12786 		 * Before doing a dtrace_probe_enable, which is really
12787 		 * expensive, check that this enabling matches the matching precondition
12788 		 * if we have one
12789 		 */
12790 		if (cond && (cond->dmc_func(&ep->dted_probe, cond->dmc_data) == 0)) {
12791 			continue;
12792 		}
12793 		/*
12794 		 * If a provider failed to enable a probe then get out and
12795 		 * let the consumer know we failed.
12796 		 */
12797 		if ((matched = dtrace_probe_enable(&ep->dted_probe, enab, ep)) < 0)
12798 			return (EBUSY);
12799 
12800 		total_matched += matched;
12801 
12802 		if (enab->dten_error != 0) {
12803 			/*
12804 			 * If we get an error half-way through enabling the
12805 			 * probes, we kick out -- perhaps with some number of
12806 			 * them enabled.  Leaving enabled probes enabled may
12807 			 * be slightly confusing for user-level, but we expect
12808 			 * that no one will attempt to actually drive on in
12809 			 * the face of such errors.  If this is an anonymous
12810 			 * enabling (indicated with a NULL nmatched pointer),
12811 			 * we cmn_err() a message.  We aren't expecting to
12812 			 * get such an error -- such as it can exist at all,
12813 			 * it would be a result of corrupted DOF in the driver
12814 			 * properties.
12815 			 */
12816 			if (nmatched == NULL) {
12817 				cmn_err(CE_WARN, "dtrace_enabling_match() "
12818 				    "error on %p: %d", (void *)ep,
12819 				    enab->dten_error);
12820 			}
12821 
12822 			return (enab->dten_error);
12823 		}
12824 
12825 		ep->dted_probegen = dtrace_probegen;
12826 	}
12827 
12828 	if (nmatched != NULL)
12829 		*nmatched = total_matched;
12830 
12831 	return (0);
12832 }
12833 
12834 static void
dtrace_enabling_matchall_with_cond(dtrace_match_cond_t * cond)12835 dtrace_enabling_matchall_with_cond(dtrace_match_cond_t *cond)
12836 {
12837 	dtrace_enabling_t *enab;
12838 
12839 	lck_mtx_lock(&cpu_lock);
12840 	lck_mtx_lock(&dtrace_lock);
12841 
12842 	/*
12843 	 * Iterate over all retained enablings to see if any probes match
12844 	 * against them.  We only perform this operation on enablings for which
12845 	 * we have sufficient permissions by virtue of being in the global zone
12846 	 * or in the same zone as the DTrace client.  Because we can be called
12847 	 * after dtrace_detach() has been called, we cannot assert that there
12848 	 * are retained enablings.  We can safely load from dtrace_retained,
12849 	 * however:  the taskq_destroy() at the end of dtrace_detach() will
12850 	 * block pending our completion.
12851 	 */
12852 
12853 	/*
12854 	 * Darwin doesn't do zones.
12855 	 * Behave as if always in "global" zone."
12856 	 */
12857 	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
12858 		(void) dtrace_enabling_match(enab, NULL, cond);
12859 	}
12860 
12861 	lck_mtx_unlock(&dtrace_lock);
12862 	lck_mtx_unlock(&cpu_lock);
12863 
12864 }
12865 
12866 static void
dtrace_enabling_matchall(void)12867 dtrace_enabling_matchall(void)
12868 {
12869 	dtrace_enabling_matchall_with_cond(NULL);
12870 }
12871 
12872 
12873 
12874 /*
12875  * If an enabling is to be enabled without having matched probes (that is, if
12876  * dtrace_state_go() is to be called on the underlying dtrace_state_t), the
12877  * enabling must be _primed_ by creating an ECB for every ECB description.
12878  * This must be done to assure that we know the number of speculations, the
12879  * number of aggregations, the minimum buffer size needed, etc. before we
12880  * transition out of DTRACE_ACTIVITY_INACTIVE.  To do this without actually
12881  * enabling any probes, we create ECBs for every ECB decription, but with a
12882  * NULL probe -- which is exactly what this function does.
12883  */
12884 static void
dtrace_enabling_prime(dtrace_state_t * state)12885 dtrace_enabling_prime(dtrace_state_t *state)
12886 {
12887 	dtrace_enabling_t *enab;
12888 	int i;
12889 
12890 	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
12891 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
12892 
12893 		if (enab->dten_vstate->dtvs_state != state)
12894 			continue;
12895 
12896 		/*
12897 		 * We don't want to prime an enabling more than once, lest
12898 		 * we allow a malicious user to induce resource exhaustion.
12899 		 * (The ECBs that result from priming an enabling aren't
12900 		 * leaked -- but they also aren't deallocated until the
12901 		 * consumer state is destroyed.)
12902 		 */
12903 		if (enab->dten_primed)
12904 			continue;
12905 
12906 		for (i = 0; i < enab->dten_ndesc; i++) {
12907 			enab->dten_current = enab->dten_desc[i];
12908 			(void) dtrace_probe_enable(NULL, enab, NULL);
12909 		}
12910 
12911 		enab->dten_primed = 1;
12912 	}
12913 }
12914 
12915 /*
12916  * Called to indicate that probes should be provided due to retained
12917  * enablings.  This is implemented in terms of dtrace_probe_provide(), but it
12918  * must take an initial lap through the enabling calling the dtps_provide()
12919  * entry point explicitly to allow for autocreated probes.
12920  */
12921 static void
dtrace_enabling_provide(dtrace_provider_t * prv)12922 dtrace_enabling_provide(dtrace_provider_t *prv)
12923 {
12924 	int i, all = 0;
12925 	dtrace_probedesc_t desc;
12926         dtrace_genid_t gen;
12927 
12928 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
12929 	LCK_MTX_ASSERT(&dtrace_provider_lock, LCK_MTX_ASSERT_OWNED);
12930 
12931 	if (prv == NULL) {
12932 		all = 1;
12933 		prv = dtrace_provider;
12934 	}
12935 
12936 	do {
12937 		dtrace_enabling_t *enab;
12938 		void *parg = prv->dtpv_arg;
12939 
12940 retry:
12941 		gen = dtrace_retained_gen;
12942 		for (enab = dtrace_retained; enab != NULL;
12943 		    enab = enab->dten_next) {
12944 			for (i = 0; i < enab->dten_ndesc; i++) {
12945 				desc = enab->dten_desc[i]->dted_probe;
12946 				lck_mtx_unlock(&dtrace_lock);
12947 				prv->dtpv_pops.dtps_provide(parg, &desc);
12948 				lck_mtx_lock(&dtrace_lock);
12949 				/*
12950 				 * Process the retained enablings again if
12951 				 * they have changed while we weren't holding
12952 				 * dtrace_lock.
12953 				 */
12954 				if (gen != dtrace_retained_gen)
12955 					goto retry;
12956 			}
12957 		}
12958 	} while (all && (prv = prv->dtpv_next) != NULL);
12959 
12960 	lck_mtx_unlock(&dtrace_lock);
12961 	dtrace_probe_provide(NULL, all ? NULL : prv);
12962 	lck_mtx_lock(&dtrace_lock);
12963 }
12964 
12965 /*
12966  * DTrace DOF Functions
12967  */
12968 /*ARGSUSED*/
12969 static void
dtrace_dof_error(dof_hdr_t * dof,const char * str)12970 dtrace_dof_error(dof_hdr_t *dof, const char *str)
12971 {
12972 #pragma unused(dof) /* __APPLE__ */
12973 	if (dtrace_err_verbose)
12974 		cmn_err(CE_WARN, "failed to process DOF: %s", str);
12975 
12976 #ifdef DTRACE_ERRDEBUG
12977 	dtrace_errdebug(str);
12978 #endif
12979 }
12980 
12981 /*
12982  * Create DOF out of a currently enabled state.  Right now, we only create
12983  * DOF containing the run-time options -- but this could be expanded to create
12984  * complete DOF representing the enabled state.
12985  */
12986 static dof_hdr_t *
dtrace_dof_create(dtrace_state_t * state)12987 dtrace_dof_create(dtrace_state_t *state)
12988 {
12989 	dof_hdr_t *dof;
12990 	dof_sec_t *sec;
12991 	dof_optdesc_t *opt;
12992 	int i, len = sizeof (dof_hdr_t) +
12993 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
12994 	    sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
12995 
12996 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
12997 
12998 	dof = kmem_zalloc_aligned(len, 8, KM_SLEEP);
12999 	dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
13000 	dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
13001 	dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
13002 	dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
13003 
13004 	dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
13005 	dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
13006 	dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION;
13007 	dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
13008 	dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
13009 	dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
13010 
13011 	dof->dofh_flags = 0;
13012 	dof->dofh_hdrsize = sizeof (dof_hdr_t);
13013 	dof->dofh_secsize = sizeof (dof_sec_t);
13014 	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
13015 	dof->dofh_secoff = sizeof (dof_hdr_t);
13016 	dof->dofh_loadsz = len;
13017 	dof->dofh_filesz = len;
13018 	dof->dofh_pad = 0;
13019 
13020 	/*
13021 	 * Fill in the option section header...
13022 	 */
13023 	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
13024 	sec->dofs_type = DOF_SECT_OPTDESC;
13025 	sec->dofs_align = sizeof (uint64_t);
13026 	sec->dofs_flags = DOF_SECF_LOAD;
13027 	sec->dofs_entsize = sizeof (dof_optdesc_t);
13028 
13029 	opt = (dof_optdesc_t *)((uintptr_t)sec +
13030 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
13031 
13032 	sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
13033 	sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
13034 
13035 	for (i = 0; i < DTRACEOPT_MAX; i++) {
13036 		opt[i].dofo_option = i;
13037 		opt[i].dofo_strtab = DOF_SECIDX_NONE;
13038 		opt[i].dofo_value = state->dts_options[i];
13039 	}
13040 
13041 	return (dof);
13042 }
13043 
13044 static dof_hdr_t *
dtrace_dof_copyin(user_addr_t uarg,int * errp)13045 dtrace_dof_copyin(user_addr_t uarg, int *errp)
13046 {
13047 	dof_hdr_t hdr, *dof;
13048 
13049 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_NOTOWNED);
13050 
13051 	/*
13052 	 * First, we're going to copyin() the sizeof (dof_hdr_t).
13053 	 */
13054 	if (copyin(uarg, &hdr, sizeof (hdr)) != 0) {
13055 		dtrace_dof_error(NULL, "failed to copyin DOF header");
13056 		*errp = EFAULT;
13057 		return (NULL);
13058 	}
13059 
13060 	/*
13061 	 * Now we'll allocate the entire DOF and copy it in -- provided
13062 	 * that the length isn't outrageous.
13063 	 */
13064 	if (hdr.dofh_loadsz >= (uint64_t)dtrace_dof_maxsize) {
13065 		dtrace_dof_error(&hdr, "load size exceeds maximum");
13066 		*errp = E2BIG;
13067 		return (NULL);
13068 	}
13069 
13070 	if (hdr.dofh_loadsz < sizeof (hdr)) {
13071 		dtrace_dof_error(&hdr, "invalid load size");
13072 		*errp = EINVAL;
13073 		return (NULL);
13074 	}
13075 
13076 	dof = kmem_alloc_aligned(hdr.dofh_loadsz, 8, KM_SLEEP);
13077 
13078 	if (copyin(uarg, dof, hdr.dofh_loadsz) != 0 ||
13079 	    dof->dofh_loadsz != hdr.dofh_loadsz) {
13080 		kmem_free_aligned(dof, hdr.dofh_loadsz);
13081 		*errp = EFAULT;
13082 		return (NULL);
13083 	}
13084 
13085 	return (dof);
13086 }
13087 
13088 static dof_hdr_t *
dtrace_dof_copyin_from_proc(proc_t * p,user_addr_t uarg,int * errp)13089 dtrace_dof_copyin_from_proc(proc_t* p, user_addr_t uarg, int *errp)
13090 {
13091 	dof_hdr_t hdr, *dof;
13092 
13093 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_NOTOWNED);
13094 
13095 	/*
13096 	 * First, we're going to copyin() the sizeof (dof_hdr_t).
13097 	 */
13098 	if (uread(p, &hdr, sizeof(hdr), uarg) != KERN_SUCCESS) {
13099 		dtrace_dof_error(NULL, "failed to copyin DOF header");
13100 		*errp = EFAULT;
13101 		return (NULL);
13102 	}
13103 
13104 	/*
13105 	 * Now we'll allocate the entire DOF and copy it in -- provided
13106 	 * that the length isn't outrageous.
13107 	 */
13108 	if (hdr.dofh_loadsz >= (uint64_t)dtrace_dof_maxsize) {
13109 		dtrace_dof_error(&hdr, "load size exceeds maximum");
13110 		*errp = E2BIG;
13111 		return (NULL);
13112 	}
13113 
13114 	if (hdr.dofh_loadsz < sizeof (hdr)) {
13115 		dtrace_dof_error(&hdr, "invalid load size");
13116 		*errp = EINVAL;
13117 		return (NULL);
13118 	}
13119 
13120 	dof = kmem_alloc_aligned(hdr.dofh_loadsz, 8, KM_SLEEP);
13121 
13122 	if (uread(p, dof, hdr.dofh_loadsz, uarg) != KERN_SUCCESS ||
13123 	    dof->dofh_loadsz != hdr.dofh_loadsz) {
13124 		kmem_free_aligned(dof, hdr.dofh_loadsz);
13125 		*errp = EFAULT;
13126 		return (NULL);
13127 	}
13128 
13129 	return (dof);
13130 }
13131 
13132 static void
dtrace_dof_destroy(dof_hdr_t * dof)13133 dtrace_dof_destroy(dof_hdr_t *dof)
13134 {
13135 	kmem_free_aligned(dof, dof->dofh_loadsz);
13136 }
13137 
13138 static dof_hdr_t *
dtrace_dof_property(const char * name)13139 dtrace_dof_property(const char *name)
13140 {
13141 	unsigned int len = 0;
13142 	dof_hdr_t *dof;
13143 
13144 	if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed()) {
13145 		return NULL;
13146 	}
13147 
13148 	if (!PEReadNVRAMProperty(name, NULL, &len)) {
13149 		return NULL;
13150 	}
13151 
13152 	dof = kmem_alloc_aligned(len, 8, KM_SLEEP);
13153 
13154 	if (!PEReadNVRAMProperty(name, dof, &len)) {
13155 		dtrace_dof_destroy(dof);
13156 		dtrace_dof_error(NULL, "unreadable DOF");
13157 		return NULL;
13158 	}
13159 
13160 	if (len < sizeof (dof_hdr_t)) {
13161 		dtrace_dof_destroy(dof);
13162 		dtrace_dof_error(NULL, "truncated header");
13163 		return (NULL);
13164 	}
13165 
13166 	if (len < dof->dofh_loadsz) {
13167 		dtrace_dof_destroy(dof);
13168 		dtrace_dof_error(NULL, "truncated DOF");
13169 		return (NULL);
13170 	}
13171 
13172 	if (len != dof->dofh_loadsz) {
13173 		dtrace_dof_destroy(dof);
13174 		dtrace_dof_error(NULL, "invalid DOF size");
13175 		return (NULL);
13176 	}
13177 
13178 	if (dof->dofh_loadsz >= (uint64_t)dtrace_dof_maxsize) {
13179 		dtrace_dof_destroy(dof);
13180 		dtrace_dof_error(NULL, "oversized DOF");
13181 		return (NULL);
13182 	}
13183 
13184 	return (dof);
13185 }
13186 
13187 /*
13188  * Return the dof_sec_t pointer corresponding to a given section index.  If the
13189  * index is not valid, dtrace_dof_error() is called and NULL is returned.  If
13190  * a type other than DOF_SECT_NONE is specified, the header is checked against
13191  * this type and NULL is returned if the types do not match.
13192  */
13193 static dof_sec_t *
dtrace_dof_sect(dof_hdr_t * dof,uint32_t type,dof_secidx_t i)13194 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i)
13195 {
13196 	dof_sec_t *sec = (dof_sec_t *)(uintptr_t)
13197 	    ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize);
13198 
13199 	if (i >= dof->dofh_secnum) {
13200 		dtrace_dof_error(dof, "referenced section index is invalid");
13201 		return (NULL);
13202 	}
13203 
13204 	if (!(sec->dofs_flags & DOF_SECF_LOAD)) {
13205 		dtrace_dof_error(dof, "referenced section is not loadable");
13206 		return (NULL);
13207 	}
13208 
13209 	if (type != DOF_SECT_NONE && type != sec->dofs_type) {
13210 		dtrace_dof_error(dof, "referenced section is the wrong type");
13211 		return (NULL);
13212 	}
13213 
13214 	return (sec);
13215 }
13216 
13217 static dtrace_probedesc_t *
dtrace_dof_probedesc(dof_hdr_t * dof,dof_sec_t * sec,dtrace_probedesc_t * desc)13218 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc)
13219 {
13220 	dof_probedesc_t *probe;
13221 	dof_sec_t *strtab;
13222 	uintptr_t daddr = (uintptr_t)dof;
13223 	uintptr_t str;
13224 	size_t size;
13225 
13226 	if (sec->dofs_type != DOF_SECT_PROBEDESC) {
13227 		dtrace_dof_error(dof, "invalid probe section");
13228 		return (NULL);
13229 	}
13230 
13231 	if (sec->dofs_align != sizeof (dof_secidx_t)) {
13232 		dtrace_dof_error(dof, "bad alignment in probe description");
13233 		return (NULL);
13234 	}
13235 
13236 	if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) {
13237 		dtrace_dof_error(dof, "truncated probe description");
13238 		return (NULL);
13239 	}
13240 
13241 	probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset);
13242 	strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab);
13243 
13244 	if (strtab == NULL)
13245 		return (NULL);
13246 
13247 	str = daddr + strtab->dofs_offset;
13248 	size = strtab->dofs_size;
13249 
13250 	if (probe->dofp_provider >= strtab->dofs_size) {
13251 		dtrace_dof_error(dof, "corrupt probe provider");
13252 		return (NULL);
13253 	}
13254 
13255 	(void) strncpy(desc->dtpd_provider,
13256 	    (char *)(str + probe->dofp_provider),
13257 	    MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider));
13258 
13259 	/* APPLE NOTE: Darwin employs size bounded string operation. */
13260 	desc->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
13261 
13262 	if (probe->dofp_mod >= strtab->dofs_size) {
13263 		dtrace_dof_error(dof, "corrupt probe module");
13264 		return (NULL);
13265 	}
13266 
13267 	(void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod),
13268 	    MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod));
13269 
13270 	/* APPLE NOTE: Darwin employs size bounded string operation. */
13271 	desc->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
13272 
13273 	if (probe->dofp_func >= strtab->dofs_size) {
13274 		dtrace_dof_error(dof, "corrupt probe function");
13275 		return (NULL);
13276 	}
13277 
13278 	(void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func),
13279 	    MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func));
13280 
13281 	/* APPLE NOTE: Darwin employs size bounded string operation. */
13282 	desc->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
13283 
13284 	if (probe->dofp_name >= strtab->dofs_size) {
13285 		dtrace_dof_error(dof, "corrupt probe name");
13286 		return (NULL);
13287 	}
13288 
13289 	(void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name),
13290 	    MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name));
13291 
13292 	/* APPLE NOTE: Darwin employs size bounded string operation. */
13293 	desc->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
13294 
13295 	return (desc);
13296 }
13297 
13298 static dtrace_difo_t *
dtrace_dof_difo(dof_hdr_t * dof,dof_sec_t * sec,dtrace_vstate_t * vstate,cred_t * cr)13299 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13300     cred_t *cr)
13301 {
13302 	dtrace_difo_t *dp;
13303 	size_t ttl = 0;
13304 	dof_difohdr_t *dofd;
13305 	uintptr_t daddr = (uintptr_t)dof;
13306 	size_t max_size = dtrace_difo_maxsize;
13307 	uint_t i;
13308 	int l, n;
13309 
13310 
13311 	static const struct {
13312 		int section;
13313 		int bufoffs;
13314 		int lenoffs;
13315 		int entsize;
13316 		int align;
13317 		const char *msg;
13318 	} difo[] = {
13319 		{ DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf),
13320 		offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t),
13321 		sizeof (dif_instr_t), "multiple DIF sections" },
13322 
13323 		{ DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab),
13324 		offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t),
13325 		sizeof (uint64_t), "multiple integer tables" },
13326 
13327 		{ DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab),
13328 		offsetof(dtrace_difo_t, dtdo_strlen), 0,
13329 		sizeof (char), "multiple string tables" },
13330 
13331 		{ DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab),
13332 		offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t),
13333 		sizeof (uint_t), "multiple variable tables" },
13334 
13335 		{ DOF_SECT_NONE, 0, 0, 0, 0, NULL }
13336 	};
13337 
13338 	if (sec->dofs_type != DOF_SECT_DIFOHDR) {
13339 		dtrace_dof_error(dof, "invalid DIFO header section");
13340 		return (NULL);
13341 	}
13342 
13343 	if (sec->dofs_align != sizeof (dof_secidx_t)) {
13344 		dtrace_dof_error(dof, "bad alignment in DIFO header");
13345 		return (NULL);
13346 	}
13347 
13348 	if (sec->dofs_size < sizeof (dof_difohdr_t) ||
13349 	    sec->dofs_size % sizeof (dof_secidx_t)) {
13350 		dtrace_dof_error(dof, "bad size in DIFO header");
13351 		return (NULL);
13352 	}
13353 
13354 	dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
13355 	n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1;
13356 
13357 	dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
13358 	dp->dtdo_rtype = dofd->dofd_rtype;
13359 
13360 	for (l = 0; l < n; l++) {
13361 		dof_sec_t *subsec;
13362 		void **bufp;
13363 		uint32_t *lenp;
13364 
13365 		if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE,
13366 		    dofd->dofd_links[l])) == NULL)
13367 			goto err; /* invalid section link */
13368 
13369 		if (ttl + subsec->dofs_size > max_size) {
13370 			dtrace_dof_error(dof, "exceeds maximum size");
13371 			goto err;
13372 		}
13373 
13374 		ttl += subsec->dofs_size;
13375 
13376 		for (i = 0; difo[i].section != DOF_SECT_NONE; i++) {
13377 
13378 			if (subsec->dofs_type != (uint32_t)difo[i].section)
13379 				continue;
13380 
13381 			if (!(subsec->dofs_flags & DOF_SECF_LOAD)) {
13382 				dtrace_dof_error(dof, "section not loaded");
13383 				goto err;
13384 			}
13385 
13386 			if (subsec->dofs_align != (uint32_t)difo[i].align) {
13387 				dtrace_dof_error(dof, "bad alignment");
13388 				goto err;
13389 			}
13390 
13391 			bufp = (void **)((uintptr_t)dp + difo[i].bufoffs);
13392 			lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs);
13393 
13394 			if (*bufp != NULL) {
13395 				dtrace_dof_error(dof, difo[i].msg);
13396 				goto err;
13397 			}
13398 
13399 			if ((uint32_t)difo[i].entsize != subsec->dofs_entsize) {
13400 				dtrace_dof_error(dof, "entry size mismatch");
13401 				goto err;
13402 			}
13403 
13404 			if (subsec->dofs_entsize != 0 &&
13405 			    (subsec->dofs_size % subsec->dofs_entsize) != 0) {
13406 				dtrace_dof_error(dof, "corrupt entry size");
13407 				goto err;
13408 			}
13409 
13410 			*lenp = subsec->dofs_size;
13411 			*bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP);
13412 			bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset),
13413 			    *bufp, subsec->dofs_size);
13414 
13415 			if (subsec->dofs_entsize != 0)
13416 				*lenp /= subsec->dofs_entsize;
13417 
13418 			break;
13419 		}
13420 
13421 		/*
13422 		 * If we encounter a loadable DIFO sub-section that is not
13423 		 * known to us, assume this is a broken program and fail.
13424 		 */
13425 		if (difo[i].section == DOF_SECT_NONE &&
13426 		    (subsec->dofs_flags & DOF_SECF_LOAD)) {
13427 			dtrace_dof_error(dof, "unrecognized DIFO subsection");
13428 			goto err;
13429 		}
13430 	}
13431 
13432 	if (dp->dtdo_buf == NULL) {
13433 		/*
13434 		 * We can't have a DIF object without DIF text.
13435 		 */
13436 		dtrace_dof_error(dof, "missing DIF text");
13437 		goto err;
13438 	}
13439 
13440 	/*
13441 	 * Before we validate the DIF object, run through the variable table
13442 	 * looking for the strings -- if any of their size are under, we'll set
13443 	 * their size to be the system-wide default string size.  Note that
13444 	 * this should _not_ happen if the "strsize" option has been set --
13445 	 * in this case, the compiler should have set the size to reflect the
13446 	 * setting of the option.
13447 	 */
13448 	for (i = 0; i < dp->dtdo_varlen; i++) {
13449 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
13450 		dtrace_diftype_t *t = &v->dtdv_type;
13451 
13452 		if (v->dtdv_id < DIF_VAR_OTHER_UBASE)
13453 			continue;
13454 
13455 		if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0)
13456 			t->dtdt_size = dtrace_strsize_default;
13457 	}
13458 
13459 	if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0)
13460 		goto err;
13461 
13462 	dtrace_difo_init(dp, vstate);
13463 	return (dp);
13464 
13465 err:
13466 	kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
13467 	kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
13468 	kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
13469 	kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
13470 
13471 	kmem_free(dp, sizeof (dtrace_difo_t));
13472 	return (NULL);
13473 }
13474 
13475 static dtrace_predicate_t *
dtrace_dof_predicate(dof_hdr_t * dof,dof_sec_t * sec,dtrace_vstate_t * vstate,cred_t * cr)13476 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13477     cred_t *cr)
13478 {
13479 	dtrace_difo_t *dp;
13480 
13481 	if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL)
13482 		return (NULL);
13483 
13484 	return (dtrace_predicate_create(dp));
13485 }
13486 
13487 static dtrace_actdesc_t *
dtrace_dof_actdesc(dof_hdr_t * dof,dof_sec_t * sec,dtrace_vstate_t * vstate,cred_t * cr)13488 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13489     cred_t *cr)
13490 {
13491 	dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next;
13492 	dof_actdesc_t *desc;
13493 	dof_sec_t *difosec;
13494 	size_t offs;
13495 	uintptr_t daddr = (uintptr_t)dof;
13496 	uint64_t arg;
13497 	dtrace_actkind_t kind;
13498 
13499 	if (sec->dofs_type != DOF_SECT_ACTDESC) {
13500 		dtrace_dof_error(dof, "invalid action section");
13501 		return (NULL);
13502 	}
13503 
13504 	if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) {
13505 		dtrace_dof_error(dof, "truncated action description");
13506 		return (NULL);
13507 	}
13508 
13509 	if (sec->dofs_align != sizeof (uint64_t)) {
13510 		dtrace_dof_error(dof, "bad alignment in action description");
13511 		return (NULL);
13512 	}
13513 
13514 	if (sec->dofs_size < sec->dofs_entsize) {
13515 		dtrace_dof_error(dof, "section entry size exceeds total size");
13516 		return (NULL);
13517 	}
13518 
13519 	if (sec->dofs_entsize != sizeof (dof_actdesc_t)) {
13520 		dtrace_dof_error(dof, "bad entry size in action description");
13521 		return (NULL);
13522 	}
13523 
13524 	if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) {
13525 		dtrace_dof_error(dof, "actions exceed dtrace_actions_max");
13526 		return (NULL);
13527 	}
13528 
13529 	for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) {
13530 		desc = (dof_actdesc_t *)(daddr +
13531 		    (uintptr_t)sec->dofs_offset + offs);
13532 		kind = (dtrace_actkind_t)desc->dofa_kind;
13533 
13534 		if ((DTRACEACT_ISPRINTFLIKE(kind) &&
13535 		    (kind != DTRACEACT_PRINTA || desc->dofa_strtab != DOF_SECIDX_NONE)) ||
13536 		    (kind == DTRACEACT_DIFEXPR && desc->dofa_strtab != DOF_SECIDX_NONE))
13537 		{
13538 			dof_sec_t *strtab;
13539 			char *str, *fmt;
13540 			uint64_t i;
13541 
13542 			/*
13543 			 * The argument to these actions is an index into the
13544 			 * DOF string table.  For printf()-like actions, this
13545 			 * is the format string.  For print(), this is the
13546 			 * CTF type of the expression result.
13547 			 */
13548 			if ((strtab = dtrace_dof_sect(dof,
13549 			    DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL)
13550 				goto err;
13551 
13552 			str = (char *)((uintptr_t)dof +
13553 			    (uintptr_t)strtab->dofs_offset);
13554 
13555 			for (i = desc->dofa_arg; i < strtab->dofs_size; i++) {
13556 				if (str[i] == '\0')
13557 					break;
13558 			}
13559 
13560 			if (i >= strtab->dofs_size) {
13561 				dtrace_dof_error(dof, "bogus format string");
13562 				goto err;
13563 			}
13564 
13565 			if (i == desc->dofa_arg) {
13566 				dtrace_dof_error(dof, "empty format string");
13567 				goto err;
13568 			}
13569 
13570 			i -= desc->dofa_arg;
13571 			fmt = kmem_alloc(i + 1, KM_SLEEP);
13572 			bcopy(&str[desc->dofa_arg], fmt, i + 1);
13573 			arg = (uint64_t)(uintptr_t)fmt;
13574 		} else {
13575 			if (kind == DTRACEACT_PRINTA) {
13576 				ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE);
13577 				arg = 0;
13578 			} else {
13579 				arg = desc->dofa_arg;
13580 			}
13581 		}
13582 
13583 		act = dtrace_actdesc_create(kind, desc->dofa_ntuple,
13584 		    desc->dofa_uarg, arg);
13585 
13586 		if (last != NULL) {
13587 			last->dtad_next = act;
13588 		} else {
13589 			first = act;
13590 		}
13591 
13592 		last = act;
13593 
13594 		if (desc->dofa_difo == DOF_SECIDX_NONE)
13595 			continue;
13596 
13597 		if ((difosec = dtrace_dof_sect(dof,
13598 		    DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL)
13599 			goto err;
13600 
13601 		act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr);
13602 
13603 		if (act->dtad_difo == NULL)
13604 			goto err;
13605 	}
13606 
13607 	ASSERT(first != NULL);
13608 	return (first);
13609 
13610 err:
13611 	for (act = first; act != NULL; act = next) {
13612 		next = act->dtad_next;
13613 		dtrace_actdesc_release(act, vstate);
13614 	}
13615 
13616 	return (NULL);
13617 }
13618 
13619 static dtrace_ecbdesc_t *
dtrace_dof_ecbdesc(dof_hdr_t * dof,dof_sec_t * sec,dtrace_vstate_t * vstate,cred_t * cr)13620 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13621     cred_t *cr)
13622 {
13623 	dtrace_ecbdesc_t *ep;
13624 	dof_ecbdesc_t *ecb;
13625 	dtrace_probedesc_t *desc;
13626 	dtrace_predicate_t *pred = NULL;
13627 
13628 	if (!(sec->dofs_flags & DOF_SECF_LOAD)) {
13629 		dtrace_dof_error(dof, "Non loadable section with ECB description");
13630 		return (NULL);
13631 	}
13632 
13633 	if (sec->dofs_size < sizeof (dof_ecbdesc_t)) {
13634 		dtrace_dof_error(dof, "truncated ECB description");
13635 		return (NULL);
13636 	}
13637 
13638 	if (sec->dofs_align != sizeof (uint64_t)) {
13639 		dtrace_dof_error(dof, "bad alignment in ECB description");
13640 		return (NULL);
13641 	}
13642 
13643 	ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset);
13644 	sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes);
13645 
13646 	if (sec == NULL)
13647 		return (NULL);
13648 
13649 	ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
13650 	ep->dted_uarg = ecb->dofe_uarg;
13651 	desc = &ep->dted_probe;
13652 
13653 	if (dtrace_dof_probedesc(dof, sec, desc) == NULL)
13654 		goto err;
13655 
13656 	if (ecb->dofe_pred != DOF_SECIDX_NONE) {
13657 		if ((sec = dtrace_dof_sect(dof,
13658 		    DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL)
13659 			goto err;
13660 
13661 		if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL)
13662 			goto err;
13663 
13664 		ep->dted_pred.dtpdd_predicate = pred;
13665 	}
13666 
13667 	if (ecb->dofe_actions != DOF_SECIDX_NONE) {
13668 		if ((sec = dtrace_dof_sect(dof,
13669 		    DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL)
13670 			goto err;
13671 
13672 		ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr);
13673 
13674 		if (ep->dted_action == NULL)
13675 			goto err;
13676 	}
13677 
13678 	return (ep);
13679 
13680 err:
13681 	if (pred != NULL)
13682 		dtrace_predicate_release(pred, vstate);
13683 	kmem_free(ep, sizeof (dtrace_ecbdesc_t));
13684 	return (NULL);
13685 }
13686 
13687 /*
13688  * APPLE NOTE: dyld handles dof relocation.
13689  * Darwin does not need dtrace_dof_relocate()
13690  */
13691 
13692 /*
13693  * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated
13694  * header:  it should be at the front of a memory region that is at least
13695  * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in
13696  * size.  It need not be validated in any other way.
13697  */
13698 static int
dtrace_dof_slurp(dof_hdr_t * dof,dtrace_vstate_t * vstate,cred_t * cr,dtrace_enabling_t ** enabp,uint64_t ubase,int noprobes)13699 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr,
13700     dtrace_enabling_t **enabp, uint64_t ubase, int noprobes)
13701 {
13702 #pragma unused(ubase) /* __APPLE__ */
13703 	uint64_t len = dof->dofh_loadsz, seclen;
13704 	uintptr_t daddr = (uintptr_t)dof;
13705 	dtrace_ecbdesc_t *ep;
13706 	dtrace_enabling_t *enab;
13707 	uint_t i;
13708 
13709 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
13710 	ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t));
13711 
13712 	/*
13713 	 * Check the DOF header identification bytes.  In addition to checking
13714 	 * valid settings, we also verify that unused bits/bytes are zeroed so
13715 	 * we can use them later without fear of regressing existing binaries.
13716 	 */
13717 	if (bcmp(&dof->dofh_ident[DOF_ID_MAG0],
13718 	    DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) {
13719 		dtrace_dof_error(dof, "DOF magic string mismatch");
13720 		return (-1);
13721 	}
13722 
13723 	if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 &&
13724 	    dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) {
13725 		dtrace_dof_error(dof, "DOF has invalid data model");
13726 		return (-1);
13727 	}
13728 
13729 	if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) {
13730 		dtrace_dof_error(dof, "DOF encoding mismatch");
13731 		return (-1);
13732 	}
13733 
13734 	/*
13735 	 * APPLE NOTE: Darwin only supports DOF_VERSION_3 for now.
13736 	 */
13737 	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_3) {
13738 		dtrace_dof_error(dof, "DOF version mismatch");
13739 		return (-1);
13740 	}
13741 
13742 	if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) {
13743 		dtrace_dof_error(dof, "DOF uses unsupported instruction set");
13744 		return (-1);
13745 	}
13746 
13747 	if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) {
13748 		dtrace_dof_error(dof, "DOF uses too many integer registers");
13749 		return (-1);
13750 	}
13751 
13752 	if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) {
13753 		dtrace_dof_error(dof, "DOF uses too many tuple registers");
13754 		return (-1);
13755 	}
13756 
13757 	for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) {
13758 		if (dof->dofh_ident[i] != 0) {
13759 			dtrace_dof_error(dof, "DOF has invalid ident byte set");
13760 			return (-1);
13761 		}
13762 	}
13763 
13764 	if (dof->dofh_flags & ~DOF_FL_VALID) {
13765 		dtrace_dof_error(dof, "DOF has invalid flag bits set");
13766 		return (-1);
13767 	}
13768 
13769 	if (dof->dofh_secsize < sizeof(dof_sec_t)) {
13770 		dtrace_dof_error(dof, "invalid section header size");
13771 		return (-1);
13772 	}
13773 
13774 	/*
13775 	 * Check that the section headers don't exceed the amount of DOF
13776 	 * data.  Note that we cast the section size and number of sections
13777 	 * to uint64_t's to prevent possible overflow in the multiplication.
13778 	 */
13779 	seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize;
13780 
13781 	if (dof->dofh_secoff > len || seclen > len ||
13782 	    dof->dofh_secoff + seclen > len) {
13783 		dtrace_dof_error(dof, "truncated section headers");
13784 		return (-1);
13785 	}
13786 
13787 	if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) {
13788 		dtrace_dof_error(dof, "misaligned section headers");
13789 		return (-1);
13790 	}
13791 
13792 	if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) {
13793 		dtrace_dof_error(dof, "misaligned section size");
13794 		return (-1);
13795 	}
13796 
13797 	/*
13798 	 * Take an initial pass through the section headers to be sure that
13799 	 * the headers don't have stray offsets.  If the 'noprobes' flag is
13800 	 * set, do not permit sections relating to providers, probes, or args.
13801 	 */
13802 	for (i = 0; i < dof->dofh_secnum; i++) {
13803 		dof_sec_t *sec = (dof_sec_t *)(daddr +
13804 		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
13805 
13806 		if (noprobes) {
13807 			switch (sec->dofs_type) {
13808 			case DOF_SECT_PROVIDER:
13809 			case DOF_SECT_PROBES:
13810 			case DOF_SECT_PRARGS:
13811 			case DOF_SECT_PROFFS:
13812 				dtrace_dof_error(dof, "illegal sections "
13813 				    "for enabling");
13814 				return (-1);
13815 			}
13816 		}
13817 
13818 		if (!(sec->dofs_flags & DOF_SECF_LOAD))
13819 			continue; /* just ignore non-loadable sections */
13820 
13821 		if (sec->dofs_align & (sec->dofs_align - 1)) {
13822 			dtrace_dof_error(dof, "bad section alignment");
13823 			return (-1);
13824 		}
13825 
13826 		if (sec->dofs_offset & (sec->dofs_align - 1)) {
13827 			dtrace_dof_error(dof, "misaligned section");
13828 			return (-1);
13829 		}
13830 
13831 		if (sec->dofs_offset > len || sec->dofs_size > len ||
13832 		    sec->dofs_offset + sec->dofs_size > len) {
13833 			dtrace_dof_error(dof, "corrupt section header");
13834 			return (-1);
13835 		}
13836 
13837 		if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr +
13838 		    sec->dofs_offset + sec->dofs_size - 1) != '\0') {
13839 			dtrace_dof_error(dof, "non-terminating string table");
13840 			return (-1);
13841 		}
13842 	}
13843 
13844 	/*
13845 	 * APPLE NOTE: We have no further relocation to perform.
13846 	 * All dof values are relative offsets.
13847 	 */
13848 
13849 	if ((enab = *enabp) == NULL)
13850 		enab = *enabp = dtrace_enabling_create(vstate);
13851 
13852 	for (i = 0; i < dof->dofh_secnum; i++) {
13853 		dof_sec_t *sec = (dof_sec_t *)(daddr +
13854 		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
13855 
13856 		if (sec->dofs_type != DOF_SECT_ECBDESC)
13857 			continue;
13858 
13859 		/*
13860 		 * APPLE NOTE: Defend against gcc 4.0 botch on x86.
13861 		 * not all paths out of inlined dtrace_dof_ecbdesc
13862 		 * are checked for the NULL return value.
13863 		 * Check for NULL explicitly here.
13864 		*/
13865 		ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr);
13866 		if (ep == NULL) {
13867 			dtrace_enabling_destroy(enab);
13868 			*enabp = NULL;
13869 			return (-1);
13870 		}
13871 
13872 		dtrace_enabling_add(enab, ep);
13873 	}
13874 
13875 	return (0);
13876 }
13877 
13878 /*
13879  * Process DOF for any options.  This routine assumes that the DOF has been
13880  * at least processed by dtrace_dof_slurp().
13881  */
13882 static int
dtrace_dof_options(dof_hdr_t * dof,dtrace_state_t * state)13883 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state)
13884 {
13885 	uint_t i;
13886 	int rval;
13887 	uint32_t entsize;
13888 	size_t offs;
13889 	dof_optdesc_t *desc;
13890 
13891 	for (i = 0; i < dof->dofh_secnum; i++) {
13892 		dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof +
13893 		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
13894 
13895 		if (sec->dofs_type != DOF_SECT_OPTDESC)
13896 			continue;
13897 
13898 		if (!(sec->dofs_flags & DOF_SECF_LOAD)) {
13899 			dtrace_dof_error(dof, "Non loadable option section");
13900 			return (EINVAL);
13901 		}
13902 
13903 		if (sec->dofs_align != sizeof (uint64_t)) {
13904 			dtrace_dof_error(dof, "bad alignment in "
13905 			    "option description");
13906 			return (EINVAL);
13907 		}
13908 
13909 		if ((entsize = sec->dofs_entsize) == 0) {
13910 			dtrace_dof_error(dof, "zeroed option entry size");
13911 			return (EINVAL);
13912 		}
13913 
13914 		if (entsize < sizeof (dof_optdesc_t)) {
13915 			dtrace_dof_error(dof, "bad option entry size");
13916 			return (EINVAL);
13917 		}
13918 
13919 		for (offs = 0; offs < sec->dofs_size; offs += entsize) {
13920 			desc = (dof_optdesc_t *)((uintptr_t)dof +
13921 			    (uintptr_t)sec->dofs_offset + offs);
13922 
13923 			if (desc->dofo_strtab != DOF_SECIDX_NONE) {
13924 				dtrace_dof_error(dof, "non-zero option string");
13925 				return (EINVAL);
13926 			}
13927 
13928 			if (desc->dofo_value == (uint64_t)DTRACEOPT_UNSET) {
13929 				dtrace_dof_error(dof, "unset option");
13930 				return (EINVAL);
13931 			}
13932 
13933 			if ((rval = dtrace_state_option(state,
13934 			    desc->dofo_option, desc->dofo_value)) != 0) {
13935 				dtrace_dof_error(dof, "rejected option");
13936 				return (rval);
13937 			}
13938 		}
13939 	}
13940 
13941 	return (0);
13942 }
13943 
13944 /*
13945  * DTrace Consumer State Functions
13946  */
13947 static int
dtrace_dstate_init(dtrace_dstate_t * dstate,size_t size)13948 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size)
13949 {
13950 	size_t hashsize, maxper, min_size, chunksize = dstate->dtds_chunksize;
13951 	void *base;
13952 	uintptr_t limit;
13953 	dtrace_dynvar_t *dvar, *next, *start;
13954 
13955 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
13956 	ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL);
13957 
13958 	bzero(dstate, sizeof (dtrace_dstate_t));
13959 
13960 	if ((dstate->dtds_chunksize = chunksize) == 0)
13961 		dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE;
13962 
13963 	VERIFY(dstate->dtds_chunksize < (LONG_MAX - sizeof (dtrace_dynhash_t)));
13964 
13965 	if (size < (min_size = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)))
13966 		size = min_size;
13967 
13968 	if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL)
13969 		return (ENOMEM);
13970 
13971 	dstate->dtds_size = size;
13972 	dstate->dtds_base = base;
13973 	dstate->dtds_percpu = zalloc_percpu(dtrace_state_pcpu_zone, Z_WAITOK | Z_ZERO);
13974 
13975 	hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t));
13976 
13977 	if (hashsize != 1 && (hashsize & 1))
13978 		hashsize--;
13979 
13980 	dstate->dtds_hashsize = hashsize;
13981 	dstate->dtds_hash = dstate->dtds_base;
13982 
13983 	/*
13984 	 * Set all of our hash buckets to point to the single sink, and (if
13985 	 * it hasn't already been set), set the sink's hash value to be the
13986 	 * sink sentinel value.  The sink is needed for dynamic variable
13987 	 * lookups to know that they have iterated over an entire, valid hash
13988 	 * chain.
13989 	 */
13990 	for (size_t i = 0; i < hashsize; i++)
13991 		dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink;
13992 
13993 	if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK)
13994 		dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK;
13995 
13996 	/*
13997 	 * Determine number of active CPUs.  Divide free list evenly among
13998 	 * active CPUs.
13999 	 */
14000 	start = (dtrace_dynvar_t *)
14001 	    ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t));
14002 	limit = (uintptr_t)base + size;
14003 
14004 	VERIFY((uintptr_t)start < limit);
14005 	VERIFY((uintptr_t)start >= (uintptr_t)base);
14006 
14007 	maxper = (limit - (uintptr_t)start) / (int)NCPU;
14008 	maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize;
14009 
14010 	zpercpu_foreach_cpu(i) {
14011 		dtrace_dstate_percpu_t *dcpu = zpercpu_get_cpu(dstate->dtds_percpu, i);
14012 
14013 		dcpu->dtdsc_free = dvar = start;
14014 
14015 		/*
14016 		 * If we don't even have enough chunks to make it once through
14017 		 * NCPUs, we're just going to allocate everything to the first
14018 		 * CPU.  And if we're on the last CPU, we're going to allocate
14019 		 * whatever is left over.  In either case, we set the limit to
14020 		 * be the limit of the dynamic variable space.
14021 		 */
14022 		if (maxper == 0 || i == NCPU - 1) {
14023 			limit = (uintptr_t)base + size;
14024 			start = NULL;
14025 		} else {
14026 			limit = (uintptr_t)start + maxper;
14027 			start = (dtrace_dynvar_t *)limit;
14028 		}
14029 
14030 		VERIFY(limit <= (uintptr_t)base + size);
14031 
14032 		for (;;) {
14033 			next = (dtrace_dynvar_t *)((uintptr_t)dvar +
14034 			    dstate->dtds_chunksize);
14035 
14036 			if ((uintptr_t)next + dstate->dtds_chunksize >= limit)
14037 				break;
14038 
14039 			VERIFY((uintptr_t)dvar >= (uintptr_t)base &&
14040 			    (uintptr_t)dvar <= (uintptr_t)base + size);
14041 			dvar->dtdv_next = next;
14042 			dvar = next;
14043 		}
14044 
14045 		if (maxper == 0)
14046 			break;
14047 	}
14048 
14049 	return (0);
14050 }
14051 
14052 static void
dtrace_dstate_fini(dtrace_dstate_t * dstate)14053 dtrace_dstate_fini(dtrace_dstate_t *dstate)
14054 {
14055 	LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
14056 
14057 	if (dstate->dtds_base == NULL)
14058 		return;
14059 
14060 	kmem_free(dstate->dtds_base, dstate->dtds_size);
14061 	zfree_percpu(dtrace_state_pcpu_zone, dstate->dtds_percpu);
14062 }
14063 
14064 static void
dtrace_vstate_fini(dtrace_vstate_t * vstate)14065 dtrace_vstate_fini(dtrace_vstate_t *vstate)
14066 {
14067 	/*
14068 	 * Logical XOR, where are you?
14069 	 */
14070 	ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL));
14071 
14072 	if (vstate->dtvs_nglobals > 0) {
14073 		kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals *
14074 		    sizeof (dtrace_statvar_t *));
14075 	}
14076 
14077 	if (vstate->dtvs_ntlocals > 0) {
14078 		kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals *
14079 		    sizeof (dtrace_difv_t));
14080 	}
14081 
14082 	ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL));
14083 
14084 	if (vstate->dtvs_nlocals > 0) {
14085 		kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals *
14086 		    sizeof (dtrace_statvar_t *));
14087 	}
14088 }
14089 
14090 static void
dtrace_state_clean(dtrace_state_t * state)14091 dtrace_state_clean(dtrace_state_t *state)
14092 {
14093 	if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
14094 		return;
14095 
14096 	dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
14097 	dtrace_speculation_clean(state);
14098 }
14099 
14100 static void
dtrace_state_deadman(dtrace_state_t * state)14101 dtrace_state_deadman(dtrace_state_t *state)
14102 {
14103 	hrtime_t now;
14104 
14105 	dtrace_sync();
14106 
14107 	now = dtrace_gethrtime();
14108 
14109 	if (state != dtrace_anon.dta_state &&
14110 	    now - state->dts_laststatus >= dtrace_deadman_user)
14111 		return;
14112 
14113 	/*
14114 	 * We must be sure that dts_alive never appears to be less than the
14115 	 * value upon entry to dtrace_state_deadman(), and because we lack a
14116 	 * dtrace_cas64(), we cannot store to it atomically.  We thus instead
14117 	 * store INT64_MAX to it, followed by a memory barrier, followed by
14118 	 * the new value.  This assures that dts_alive never appears to be
14119 	 * less than its true value, regardless of the order in which the
14120 	 * stores to the underlying storage are issued.
14121 	 */
14122 	state->dts_alive = INT64_MAX;
14123 	dtrace_membar_producer();
14124 	state->dts_alive = now;
14125 }
14126 
14127 static int
dtrace_state_create(dev_t * devp,cred_t * cr,dtrace_state_t ** new_state)14128 dtrace_state_create(dev_t *devp, cred_t *cr, dtrace_state_t **new_state)
14129 {
14130 	minor_t minor;
14131 	major_t major;
14132 	char c[30];
14133 	dtrace_state_t *state;
14134 	dtrace_optval_t *opt;
14135 	int bufsize = (int)NCPU * sizeof (dtrace_buffer_t), i;
14136 	unsigned int cpu_it;
14137 
14138 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
14139 	LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
14140 
14141 	/* Cause restart */
14142 	*new_state = NULL;
14143 
14144 	if (devp != NULL) {
14145 		minor = getminor(*devp);
14146 	}
14147 	else {
14148 		minor = DTRACE_NCLIENTS - 1;
14149 	}
14150 
14151 	state = dtrace_state_allocate(minor);
14152 	if (NULL == state) {
14153 		printf("dtrace_open: couldn't acquire minor number %d. This usually means that too many DTrace clients are in use at the moment", minor);
14154 		return (ERESTART);	/* can't reacquire */
14155 	}
14156 
14157 	state->dts_epid = DTRACE_EPIDNONE + 1;
14158 
14159 	(void) snprintf(c, sizeof (c), "dtrace_aggid_%d", minor);
14160 	state->dts_aggid_arena = vmem_create(c, (void *)1, INT32_MAX, 1,
14161 	    NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
14162 
14163 	if (devp != NULL) {
14164 		major = getemajor(*devp);
14165 	} else {
14166 		major = ddi_driver_major(dtrace_devi);
14167 	}
14168 
14169 	state->dts_dev = makedev(major, minor);
14170 
14171 	if (devp != NULL)
14172 		*devp = state->dts_dev;
14173 
14174 	/*
14175 	 * We allocate NCPU buffers.  On the one hand, this can be quite
14176 	 * a bit of memory per instance (nearly 36K on a Starcat).  On the
14177 	 * other hand, it saves an additional memory reference in the probe
14178 	 * path.
14179 	 */
14180 	state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP);
14181 	state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP);
14182 	state->dts_buf_over_limit = 0;
14183 
14184 	/*
14185          * Allocate and initialise the per-process per-CPU random state.
14186 	 * SI_SUB_RANDOM < SI_SUB_DTRACE_ANON therefore entropy device is
14187          * assumed to be seeded at this point (if from Fortuna seed file).
14188 	 */
14189 	state->dts_rstate = kmem_zalloc(NCPU * sizeof(uint64_t*), KM_SLEEP);
14190 	state->dts_rstate[0] = kmem_zalloc(2 * sizeof(uint64_t), KM_SLEEP);
14191 	(void) read_random(state->dts_rstate[0], 2 * sizeof(uint64_t));
14192 	for (cpu_it = 1; cpu_it < NCPU; cpu_it++) {
14193 		state->dts_rstate[cpu_it] = kmem_zalloc(2 * sizeof(uint64_t), KM_SLEEP);
14194 		/*
14195 		 * Each CPU is assigned a 2^64 period, non-overlapping
14196 		 * subsequence.
14197 		 */
14198 		dtrace_xoroshiro128_plus_jump(state->dts_rstate[cpu_it-1],
14199 		    state->dts_rstate[cpu_it]);
14200 	}
14201 
14202 	state->dts_cleaner = CYCLIC_NONE;
14203 	state->dts_deadman = CYCLIC_NONE;
14204 	state->dts_vstate.dtvs_state = state;
14205 
14206 	for (i = 0; i < DTRACEOPT_MAX; i++)
14207 		state->dts_options[i] = DTRACEOPT_UNSET;
14208 
14209 	/*
14210 	 * Set the default options.
14211 	 */
14212 	opt = state->dts_options;
14213 	opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH;
14214 	opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO;
14215 	opt[DTRACEOPT_NSPEC] = dtrace_nspec_default;
14216 	opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default;
14217 	opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL;
14218 	opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default;
14219 	opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default;
14220 	opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default;
14221 	opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default;
14222 	opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default;
14223 	opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default;
14224 	opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default;
14225 	opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default;
14226 	opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default;
14227 	opt[DTRACEOPT_BUFLIMIT] = dtrace_buflimit_default;
14228 
14229 	/*
14230 	 * Depending on the user credentials, we set flag bits which alter probe
14231 	 * visibility or the amount of destructiveness allowed.  In the case of
14232 	 * actual anonymous tracing, or the possession of all privileges, all of
14233 	 * the normal checks are bypassed.
14234 	 */
14235 #if defined(__APPLE__)
14236 	if (cr != NULL) {
14237 		kauth_cred_ref(cr);
14238 		state->dts_cred.dcr_cred = cr;
14239 	}
14240 	if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
14241 		if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed()) {
14242 			/*
14243 			 * Allow only proc credentials when DTrace is
14244 			 * restricted by the current security policy
14245 			 */
14246 			state->dts_cred.dcr_visible = DTRACE_CRV_ALLPROC;
14247 			state->dts_cred.dcr_action = DTRACE_CRA_PROC | DTRACE_CRA_PROC_CONTROL | DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
14248 		}
14249 		else {
14250 			state->dts_cred.dcr_visible = DTRACE_CRV_ALL;
14251 			state->dts_cred.dcr_action = DTRACE_CRA_ALL;
14252 		}
14253 	}
14254 
14255 #else
14256 	if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
14257 		state->dts_cred.dcr_visible = DTRACE_CRV_ALL;
14258 		state->dts_cred.dcr_action = DTRACE_CRA_ALL;
14259 	}
14260 	else {
14261 		/*
14262 		 * Set up the credentials for this instantiation.  We take a
14263 		 * hold on the credential to prevent it from disappearing on
14264 		 * us; this in turn prevents the zone_t referenced by this
14265 		 * credential from disappearing.  This means that we can
14266 		 * examine the credential and the zone from probe context.
14267 		 */
14268 		crhold(cr);
14269 		state->dts_cred.dcr_cred = cr;
14270 
14271 		/*
14272 		 * CRA_PROC means "we have *some* privilege for dtrace" and
14273 		 * unlocks the use of variables like pid, zonename, etc.
14274 		 */
14275 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) ||
14276 		    PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
14277 			state->dts_cred.dcr_action |= DTRACE_CRA_PROC;
14278 		}
14279 
14280 		/*
14281 		 * dtrace_user allows use of syscall and profile providers.
14282 		 * If the user also has proc_owner and/or proc_zone, we
14283 		 * extend the scope to include additional visibility and
14284 		 * destructive power.
14285 		 */
14286 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) {
14287 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) {
14288 				state->dts_cred.dcr_visible |=
14289 				    DTRACE_CRV_ALLPROC;
14290 
14291 				state->dts_cred.dcr_action |=
14292 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
14293 			}
14294 
14295 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) {
14296 				state->dts_cred.dcr_visible |=
14297 				    DTRACE_CRV_ALLZONE;
14298 
14299 				state->dts_cred.dcr_action |=
14300 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
14301 			}
14302 
14303 			/*
14304 			 * If we have all privs in whatever zone this is,
14305 			 * we can do destructive things to processes which
14306 			 * have altered credentials.
14307 			 *
14308 			 * APPLE NOTE: Darwin doesn't do zones.
14309 			 * Behave as if zone always has destructive privs.
14310 			 */
14311 
14312 			state->dts_cred.dcr_action |=
14313 				DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
14314 		}
14315 
14316 		/*
14317 		 * Holding the dtrace_kernel privilege also implies that
14318 		 * the user has the dtrace_user privilege from a visibility
14319 		 * perspective.  But without further privileges, some
14320 		 * destructive actions are not available.
14321 		 */
14322 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) {
14323 			/*
14324 			 * Make all probes in all zones visible.  However,
14325 			 * this doesn't mean that all actions become available
14326 			 * to all zones.
14327 			 */
14328 			state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL |
14329 			    DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE;
14330 
14331 			state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL |
14332 			    DTRACE_CRA_PROC;
14333 			/*
14334 			 * Holding proc_owner means that destructive actions
14335 			 * for *this* zone are allowed.
14336 			 */
14337 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
14338 				state->dts_cred.dcr_action |=
14339 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
14340 
14341 			/*
14342 			 * Holding proc_zone means that destructive actions
14343 			 * for this user/group ID in all zones is allowed.
14344 			 */
14345 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
14346 				state->dts_cred.dcr_action |=
14347 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
14348 
14349 			/*
14350 			 * If we have all privs in whatever zone this is,
14351 			 * we can do destructive things to processes which
14352 			 * have altered credentials.
14353 			 *
14354 			 * APPLE NOTE: Darwin doesn't do zones.
14355 			 * Behave as if zone always has destructive privs.
14356 			 */
14357 			state->dts_cred.dcr_action |=
14358 				DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
14359 		}
14360 
14361 		/*
14362 		 * Holding the dtrace_proc privilege gives control over fasttrap
14363 		 * and pid providers.  We need to grant wider destructive
14364 		 * privileges in the event that the user has proc_owner and/or
14365 		 * proc_zone.
14366 		 */
14367 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
14368 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
14369 				state->dts_cred.dcr_action |=
14370 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
14371 
14372 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
14373 				state->dts_cred.dcr_action |=
14374 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
14375 		}
14376 	}
14377 #endif
14378 
14379 	*new_state = state;
14380 	return(0);  /* Success */
14381 }
14382 
14383 static int
dtrace_state_buffer(dtrace_state_t * state,dtrace_buffer_t * buf,int which)14384 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which)
14385 {
14386 	dtrace_optval_t *opt = state->dts_options, size;
14387 	processorid_t cpu = 0;
14388 	size_t limit = buf->dtb_size;
14389 	int flags = 0, rval;
14390 
14391 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
14392 	LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
14393 	ASSERT(which < DTRACEOPT_MAX);
14394 	ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE ||
14395 	    (state == dtrace_anon.dta_state &&
14396 	    state->dts_activity == DTRACE_ACTIVITY_ACTIVE));
14397 
14398 	if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0)
14399 		return (0);
14400 
14401 	if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET)
14402 		cpu = opt[DTRACEOPT_CPU];
14403 
14404 	if (which == DTRACEOPT_SPECSIZE)
14405 		flags |= DTRACEBUF_NOSWITCH;
14406 
14407 	if (which == DTRACEOPT_BUFSIZE) {
14408 		if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING)
14409 			flags |= DTRACEBUF_RING;
14410 
14411 		if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL)
14412 			flags |= DTRACEBUF_FILL;
14413 
14414 		if (state != dtrace_anon.dta_state ||
14415 		    state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
14416 			flags |= DTRACEBUF_INACTIVE;
14417 	}
14418 
14419 	for (size = opt[which]; (size_t)size >= sizeof (uint64_t); size >>= 1) {
14420 		/*
14421 		 * The size must be 8-byte aligned.  If the size is not 8-byte
14422 		 * aligned, drop it down by the difference.
14423 		 */
14424 		if (size & (sizeof (uint64_t) - 1))
14425 			size -= size & (sizeof (uint64_t) - 1);
14426 
14427 		if (size < state->dts_reserve) {
14428 			/*
14429 			 * Buffers always must be large enough to accommodate
14430 			 * their prereserved space.  We return E2BIG instead
14431 			 * of ENOMEM in this case to allow for user-level
14432 			 * software to differentiate the cases.
14433 			 */
14434 			return (E2BIG);
14435 		}
14436 		limit = opt[DTRACEOPT_BUFLIMIT] * size / 100;
14437 		rval = dtrace_buffer_alloc(buf, limit, size, flags, cpu);
14438 
14439 		if (rval != ENOMEM) {
14440 			opt[which] = size;
14441 			return (rval);
14442 		}
14443 
14444 		if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
14445 			return (rval);
14446 	}
14447 
14448 	return (ENOMEM);
14449 }
14450 
14451 static int
dtrace_state_buffers(dtrace_state_t * state)14452 dtrace_state_buffers(dtrace_state_t *state)
14453 {
14454 	dtrace_speculation_t *spec = state->dts_speculations;
14455 	int rval, i;
14456 
14457 	if ((rval = dtrace_state_buffer(state, state->dts_buffer,
14458 	    DTRACEOPT_BUFSIZE)) != 0)
14459 		return (rval);
14460 
14461 	if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer,
14462 	    DTRACEOPT_AGGSIZE)) != 0)
14463 		return (rval);
14464 
14465 	for (i = 0; i < state->dts_nspeculations; i++) {
14466 		if ((rval = dtrace_state_buffer(state,
14467 		    spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0)
14468 			return (rval);
14469 	}
14470 
14471 	return (0);
14472 }
14473 
14474 static void
dtrace_state_prereserve(dtrace_state_t * state)14475 dtrace_state_prereserve(dtrace_state_t *state)
14476 {
14477 	dtrace_ecb_t *ecb;
14478 	dtrace_probe_t *probe;
14479 
14480 	state->dts_reserve = 0;
14481 
14482 	if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
14483 		return;
14484 
14485 	/*
14486 	 * If our buffer policy is a "fill" buffer policy, we need to set the
14487 	 * prereserved space to be the space required by the END probes.
14488 	 */
14489 	probe = dtrace_probes[dtrace_probeid_end - 1];
14490 	ASSERT(probe != NULL);
14491 
14492 	for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
14493 		if (ecb->dte_state != state)
14494 			continue;
14495 
14496 		state->dts_reserve += ecb->dte_needed + ecb->dte_alignment;
14497 	}
14498 }
14499 
14500 static int
dtrace_state_go(dtrace_state_t * state,processorid_t * cpu)14501 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu)
14502 {
14503 	dtrace_optval_t *opt = state->dts_options, sz, nspec;
14504 	dtrace_speculation_t *spec;
14505 	dtrace_buffer_t *buf;
14506 	cyc_handler_t hdlr;
14507 	cyc_time_t when;
14508 	int rval = 0, i, bufsize = (int)NCPU * sizeof (dtrace_buffer_t);
14509 	dtrace_icookie_t cookie;
14510 
14511 	lck_mtx_lock(&cpu_lock);
14512 	lck_mtx_lock(&dtrace_lock);
14513 
14514 	if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
14515 		rval = EBUSY;
14516 		goto out;
14517 	}
14518 
14519 	/*
14520 	 * Before we can perform any checks, we must prime all of the
14521 	 * retained enablings that correspond to this state.
14522 	 */
14523 	dtrace_enabling_prime(state);
14524 
14525 	if (state->dts_destructive && !state->dts_cred.dcr_destructive) {
14526 		rval = EACCES;
14527 		goto out;
14528 	}
14529 
14530 	dtrace_state_prereserve(state);
14531 
14532 	/*
14533 	 * Now we want to do is try to allocate our speculations.
14534 	 * We do not automatically resize the number of speculations; if
14535 	 * this fails, we will fail the operation.
14536 	 */
14537 	nspec = opt[DTRACEOPT_NSPEC];
14538 	ASSERT(nspec != DTRACEOPT_UNSET);
14539 
14540 	if (nspec > INT_MAX) {
14541 		rval = ENOMEM;
14542 		goto out;
14543 	}
14544 
14545 	spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP);
14546 
14547 	if (spec == NULL) {
14548 		rval = ENOMEM;
14549 		goto out;
14550 	}
14551 
14552 	state->dts_speculations = spec;
14553 	state->dts_nspeculations = (int)nspec;
14554 
14555 	for (i = 0; i < nspec; i++) {
14556 		if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) {
14557 			rval = ENOMEM;
14558 			goto err;
14559 		}
14560 
14561 		spec[i].dtsp_buffer = buf;
14562 	}
14563 
14564 	if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) {
14565 		if (dtrace_anon.dta_state == NULL) {
14566 			rval = ENOENT;
14567 			goto out;
14568 		}
14569 
14570 		if (state->dts_necbs != 0) {
14571 			rval = EALREADY;
14572 			goto out;
14573 		}
14574 
14575 		state->dts_anon = dtrace_anon_grab();
14576 		ASSERT(state->dts_anon != NULL);
14577 		state = state->dts_anon;
14578 
14579 		/*
14580 		 * We want "grabanon" to be set in the grabbed state, so we'll
14581 		 * copy that option value from the grabbing state into the
14582 		 * grabbed state.
14583 		 */
14584 		state->dts_options[DTRACEOPT_GRABANON] =
14585 		    opt[DTRACEOPT_GRABANON];
14586 
14587 		*cpu = dtrace_anon.dta_beganon;
14588 
14589 		/*
14590 		 * If the anonymous state is active (as it almost certainly
14591 		 * is if the anonymous enabling ultimately matched anything),
14592 		 * we don't allow any further option processing -- but we
14593 		 * don't return failure.
14594 		 */
14595 		if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
14596 			goto out;
14597 	}
14598 
14599 	if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET &&
14600 	    opt[DTRACEOPT_AGGSIZE] != 0) {
14601 		if (state->dts_aggregations == NULL) {
14602 			/*
14603 			 * We're not going to create an aggregation buffer
14604 			 * because we don't have any ECBs that contain
14605 			 * aggregations -- set this option to 0.
14606 			 */
14607 			opt[DTRACEOPT_AGGSIZE] = 0;
14608 		} else {
14609 			/*
14610 			 * If we have an aggregation buffer, we must also have
14611 			 * a buffer to use as scratch.
14612 			 */
14613 			if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET ||
14614 			  (size_t)opt[DTRACEOPT_BUFSIZE] < state->dts_needed) {
14615 				opt[DTRACEOPT_BUFSIZE] = state->dts_needed;
14616 			}
14617 		}
14618 	}
14619 
14620 	if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET &&
14621 	    opt[DTRACEOPT_SPECSIZE] != 0) {
14622 		if (!state->dts_speculates) {
14623 			/*
14624 			 * We're not going to create speculation buffers
14625 			 * because we don't have any ECBs that actually
14626 			 * speculate -- set the speculation size to 0.
14627 			 */
14628 			opt[DTRACEOPT_SPECSIZE] = 0;
14629 		}
14630 	}
14631 
14632 	/*
14633 	 * The bare minimum size for any buffer that we're actually going to
14634 	 * do anything to is sizeof (uint64_t).
14635 	 */
14636 	sz = sizeof (uint64_t);
14637 
14638 	if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) ||
14639 	    (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) ||
14640 	    (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) {
14641 		/*
14642 		 * A buffer size has been explicitly set to 0 (or to a size
14643 		 * that will be adjusted to 0) and we need the space -- we
14644 		 * need to return failure.  We return ENOSPC to differentiate
14645 		 * it from failing to allocate a buffer due to failure to meet
14646 		 * the reserve (for which we return E2BIG).
14647 		 */
14648 		rval = ENOSPC;
14649 		goto out;
14650 	}
14651 
14652 	if ((rval = dtrace_state_buffers(state)) != 0)
14653 		goto err;
14654 
14655 	if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET)
14656 		sz = dtrace_dstate_defsize;
14657 
14658 	do {
14659 		rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz);
14660 
14661 		if (rval == 0)
14662 			break;
14663 
14664 		if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
14665 			goto err;
14666 	} while (sz >>= 1);
14667 
14668 	opt[DTRACEOPT_DYNVARSIZE] = sz;
14669 
14670 	if (rval != 0)
14671 		goto err;
14672 
14673 	if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max)
14674 		opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max;
14675 
14676 	if (opt[DTRACEOPT_CLEANRATE] == 0)
14677 		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
14678 
14679 	if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min)
14680 		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min;
14681 
14682 	if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max)
14683 		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
14684 
14685 	if (opt[DTRACEOPT_STRSIZE] > dtrace_strsize_max)
14686 		opt[DTRACEOPT_STRSIZE] = dtrace_strsize_max;
14687 
14688 	if (opt[DTRACEOPT_STRSIZE] < dtrace_strsize_min)
14689 		opt[DTRACEOPT_STRSIZE] = dtrace_strsize_min;
14690 
14691 	if (opt[DTRACEOPT_BUFLIMIT] > dtrace_buflimit_max)
14692 		opt[DTRACEOPT_BUFLIMIT] = dtrace_buflimit_max;
14693 
14694 	if (opt[DTRACEOPT_BUFLIMIT] < dtrace_buflimit_min)
14695 		opt[DTRACEOPT_BUFLIMIT] = dtrace_buflimit_min;
14696 
14697 	hdlr.cyh_func = (cyc_func_t)dtrace_state_clean;
14698 	hdlr.cyh_arg = state;
14699 	hdlr.cyh_level = CY_LOW_LEVEL;
14700 
14701 	when.cyt_when = 0;
14702 	when.cyt_interval = opt[DTRACEOPT_CLEANRATE];
14703 
14704 	state->dts_cleaner = cyclic_add(&hdlr, &when);
14705 
14706 	hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman;
14707 	hdlr.cyh_arg = state;
14708 	hdlr.cyh_level = CY_LOW_LEVEL;
14709 
14710 	when.cyt_when = 0;
14711 	when.cyt_interval = dtrace_deadman_interval;
14712 
14713 	state->dts_alive = state->dts_laststatus = dtrace_gethrtime();
14714 	state->dts_deadman = cyclic_add(&hdlr, &when);
14715 
14716 	state->dts_activity = DTRACE_ACTIVITY_WARMUP;
14717 
14718 	/*
14719 	 * Now it's time to actually fire the BEGIN probe.  We need to disable
14720 	 * interrupts here both to record the CPU on which we fired the BEGIN
14721 	 * probe (the data from this CPU will be processed first at user
14722 	 * level) and to manually activate the buffer for this CPU.
14723 	 */
14724 	cookie = dtrace_interrupt_disable();
14725 	*cpu = CPU->cpu_id;
14726 	ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE);
14727 	state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE;
14728 
14729 	dtrace_probe(dtrace_probeid_begin,
14730 	    (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
14731 	dtrace_interrupt_enable(cookie);
14732 	/*
14733 	 * We may have had an exit action from a BEGIN probe; only change our
14734 	 * state to ACTIVE if we're still in WARMUP.
14735 	 */
14736 	ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP ||
14737 	    state->dts_activity == DTRACE_ACTIVITY_DRAINING);
14738 
14739 	if (state->dts_activity == DTRACE_ACTIVITY_WARMUP)
14740 		state->dts_activity = DTRACE_ACTIVITY_ACTIVE;
14741 
14742 	/*
14743 	 * Regardless of whether or not now we're in ACTIVE or DRAINING, we
14744 	 * want each CPU to transition its principal buffer out of the
14745 	 * INACTIVE state.  Doing this assures that no CPU will suddenly begin
14746 	 * processing an ECB halfway down a probe's ECB chain; all CPUs will
14747 	 * atomically transition from processing none of a state's ECBs to
14748 	 * processing all of them.
14749 	 */
14750 	dtrace_xcall(DTRACE_CPUALL,
14751 	    (dtrace_xcall_t)dtrace_buffer_activate, state);
14752 	goto out;
14753 
14754 err:
14755 	dtrace_buffer_free(state->dts_buffer);
14756 	dtrace_buffer_free(state->dts_aggbuffer);
14757 
14758 	if ((nspec = state->dts_nspeculations) == 0) {
14759 		ASSERT(state->dts_speculations == NULL);
14760 		goto out;
14761 	}
14762 
14763 	spec = state->dts_speculations;
14764 	ASSERT(spec != NULL);
14765 
14766 	for (i = 0; i < state->dts_nspeculations; i++) {
14767 		if ((buf = spec[i].dtsp_buffer) == NULL)
14768 			break;
14769 
14770 		dtrace_buffer_free(buf);
14771 		kmem_free(buf, bufsize);
14772 	}
14773 
14774 	kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
14775 	state->dts_nspeculations = 0;
14776 	state->dts_speculations = NULL;
14777 
14778 out:
14779 	lck_mtx_unlock(&dtrace_lock);
14780 	lck_mtx_unlock(&cpu_lock);
14781 
14782 	return (rval);
14783 }
14784 
14785 static int
dtrace_state_stop(dtrace_state_t * state,processorid_t * cpu)14786 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu)
14787 {
14788 	dtrace_icookie_t cookie;
14789 
14790 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
14791 
14792 	if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE &&
14793 	    state->dts_activity != DTRACE_ACTIVITY_DRAINING)
14794 		return (EINVAL);
14795 
14796 	/*
14797 	 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync
14798 	 * to be sure that every CPU has seen it.  See below for the details
14799 	 * on why this is done.
14800 	 */
14801 	state->dts_activity = DTRACE_ACTIVITY_DRAINING;
14802 	dtrace_sync();
14803 
14804 	/*
14805 	 * By this point, it is impossible for any CPU to be still processing
14806 	 * with DTRACE_ACTIVITY_ACTIVE.  We can thus set our activity to
14807 	 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any
14808 	 * other CPU in dtrace_buffer_reserve().  This allows dtrace_probe()
14809 	 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN
14810 	 * iff we're in the END probe.
14811 	 */
14812 	state->dts_activity = DTRACE_ACTIVITY_COOLDOWN;
14813 	dtrace_sync();
14814 	ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN);
14815 
14816 	/*
14817 	 * Finally, we can release the reserve and call the END probe.  We
14818 	 * disable interrupts across calling the END probe to allow us to
14819 	 * return the CPU on which we actually called the END probe.  This
14820 	 * allows user-land to be sure that this CPU's principal buffer is
14821 	 * processed last.
14822 	 */
14823 	state->dts_reserve = 0;
14824 
14825 	cookie = dtrace_interrupt_disable();
14826 	*cpu = CPU->cpu_id;
14827 	dtrace_probe(dtrace_probeid_end,
14828 	    (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
14829 	dtrace_interrupt_enable(cookie);
14830 
14831 	state->dts_activity = DTRACE_ACTIVITY_STOPPED;
14832 	dtrace_sync();
14833 
14834 	return (0);
14835 }
14836 
14837 static int
dtrace_state_option(dtrace_state_t * state,dtrace_optid_t option,dtrace_optval_t val)14838 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option,
14839     dtrace_optval_t val)
14840 {
14841 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
14842 
14843 	if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
14844 		return (EBUSY);
14845 
14846 	if (option >= DTRACEOPT_MAX)
14847 		return (EINVAL);
14848 
14849 	if (option != DTRACEOPT_CPU && val < 0)
14850 		return (EINVAL);
14851 
14852 	switch (option) {
14853 	case DTRACEOPT_DESTRUCTIVE:
14854 		if (dtrace_destructive_disallow)
14855 			return (EACCES);
14856 
14857 		state->dts_cred.dcr_destructive = 1;
14858 		break;
14859 
14860 	case DTRACEOPT_BUFSIZE:
14861 	case DTRACEOPT_DYNVARSIZE:
14862 	case DTRACEOPT_AGGSIZE:
14863 	case DTRACEOPT_SPECSIZE:
14864 	case DTRACEOPT_STRSIZE:
14865 		if (val < 0)
14866 			return (EINVAL);
14867 
14868 		if (val >= LONG_MAX) {
14869 			/*
14870 			 * If this is an otherwise negative value, set it to
14871 			 * the highest multiple of 128m less than LONG_MAX.
14872 			 * Technically, we're adjusting the size without
14873 			 * regard to the buffer resizing policy, but in fact,
14874 			 * this has no effect -- if we set the buffer size to
14875 			 * ~LONG_MAX and the buffer policy is ultimately set to
14876 			 * be "manual", the buffer allocation is guaranteed to
14877 			 * fail, if only because the allocation requires two
14878 			 * buffers.  (We set the the size to the highest
14879 			 * multiple of 128m because it ensures that the size
14880 			 * will remain a multiple of a megabyte when
14881 			 * repeatedly halved -- all the way down to 15m.)
14882 			 */
14883 			val = LONG_MAX - (1 << 27) + 1;
14884 		}
14885 	}
14886 
14887 	state->dts_options[option] = val;
14888 
14889 	return (0);
14890 }
14891 
14892 static void
dtrace_state_destroy(dtrace_state_t * state)14893 dtrace_state_destroy(dtrace_state_t *state)
14894 {
14895 	dtrace_ecb_t *ecb;
14896 	dtrace_vstate_t *vstate = &state->dts_vstate;
14897 	minor_t minor = getminor(state->dts_dev);
14898 	int i, bufsize = (int)NCPU * sizeof (dtrace_buffer_t);
14899 	dtrace_speculation_t *spec = state->dts_speculations;
14900 	int nspec = state->dts_nspeculations;
14901 	uint32_t match;
14902 
14903 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
14904 	LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
14905 
14906 	/*
14907 	 * First, retract any retained enablings for this state.
14908 	 */
14909 	dtrace_enabling_retract(state);
14910 	ASSERT(state->dts_nretained == 0);
14911 
14912 	if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE ||
14913 	    state->dts_activity == DTRACE_ACTIVITY_DRAINING) {
14914 		/*
14915 		 * We have managed to come into dtrace_state_destroy() on a
14916 		 * hot enabling -- almost certainly because of a disorderly
14917 		 * shutdown of a consumer.  (That is, a consumer that is
14918 		 * exiting without having called dtrace_stop().) In this case,
14919 		 * we're going to set our activity to be KILLED, and then
14920 		 * issue a sync to be sure that everyone is out of probe
14921 		 * context before we start blowing away ECBs.
14922 		 */
14923 		state->dts_activity = DTRACE_ACTIVITY_KILLED;
14924 		dtrace_sync();
14925 	}
14926 
14927 	/*
14928 	 * Release the credential hold we took in dtrace_state_create().
14929 	 */
14930 	if (state->dts_cred.dcr_cred != NULL)
14931 		kauth_cred_unref(&state->dts_cred.dcr_cred);
14932 
14933 	/*
14934 	 * Now we can safely disable and destroy any enabled probes.  Because
14935 	 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress
14936 	 * (especially if they're all enabled), we take two passes through the
14937 	 * ECBs:  in the first, we disable just DTRACE_PRIV_KERNEL probes, and
14938 	 * in the second we disable whatever is left over.
14939 	 */
14940 	for (match = DTRACE_PRIV_KERNEL; ; match = 0) {
14941 		for (i = 0; i < state->dts_necbs; i++) {
14942 			if ((ecb = state->dts_ecbs[i]) == NULL)
14943 				continue;
14944 
14945 			if (match && ecb->dte_probe != NULL) {
14946 				dtrace_probe_t *probe = ecb->dte_probe;
14947 				dtrace_provider_t *prov = probe->dtpr_provider;
14948 
14949 				if (!(prov->dtpv_priv.dtpp_flags & match))
14950 					continue;
14951 			}
14952 
14953 			dtrace_ecb_disable(ecb);
14954 			dtrace_ecb_destroy(ecb);
14955 		}
14956 
14957 		if (!match)
14958 			break;
14959 	}
14960 
14961 	/*
14962 	 * Before we free the buffers, perform one more sync to assure that
14963 	 * every CPU is out of probe context.
14964 	 */
14965 	dtrace_sync();
14966 
14967 	dtrace_buffer_free(state->dts_buffer);
14968 	dtrace_buffer_free(state->dts_aggbuffer);
14969 
14970 	for (i = 0; i < (int)NCPU; i++) {
14971 		kmem_free(state->dts_rstate[i], 2 * sizeof(uint64_t));
14972 	}
14973 	kmem_free(state->dts_rstate, NCPU * sizeof(uint64_t*));
14974 
14975 	for (i = 0; i < nspec; i++)
14976 		dtrace_buffer_free(spec[i].dtsp_buffer);
14977 
14978 	if (state->dts_cleaner != CYCLIC_NONE)
14979 		cyclic_remove(state->dts_cleaner);
14980 
14981 	if (state->dts_deadman != CYCLIC_NONE)
14982 		cyclic_remove(state->dts_deadman);
14983 
14984 	dtrace_dstate_fini(&vstate->dtvs_dynvars);
14985 	dtrace_vstate_fini(vstate);
14986 	kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *));
14987 
14988 	if (state->dts_aggregations != NULL) {
14989 #if DEBUG
14990 		for (i = 0; i < state->dts_naggregations; i++)
14991 			ASSERT(state->dts_aggregations[i] == NULL);
14992 #endif
14993 		ASSERT(state->dts_naggregations > 0);
14994 		kmem_free(state->dts_aggregations,
14995 		    state->dts_naggregations * sizeof (dtrace_aggregation_t *));
14996 	}
14997 
14998 	kmem_free(state->dts_buffer, bufsize);
14999 	kmem_free(state->dts_aggbuffer, bufsize);
15000 
15001 	for (i = 0; i < nspec; i++)
15002 		kmem_free(spec[i].dtsp_buffer, bufsize);
15003 
15004 	kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
15005 
15006 	dtrace_format_destroy(state);
15007 
15008 	vmem_destroy(state->dts_aggid_arena);
15009 	dtrace_state_free(minor);
15010 }
15011 
15012 /*
15013  * DTrace Anonymous Enabling Functions
15014  */
15015 
15016 int
dtrace_keep_kernel_symbols(void)15017 dtrace_keep_kernel_symbols(void)
15018 {
15019 	if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed()) {
15020 		return 0;
15021 	}
15022 
15023 	if (dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL)
15024 		return 1;
15025 
15026 	return 0;
15027 }
15028 
15029 static dtrace_state_t *
dtrace_anon_grab(void)15030 dtrace_anon_grab(void)
15031 {
15032 	dtrace_state_t *state;
15033 
15034 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
15035 
15036 	if ((state = dtrace_anon.dta_state) == NULL) {
15037 		ASSERT(dtrace_anon.dta_enabling == NULL);
15038 		return (NULL);
15039 	}
15040 
15041 	ASSERT(dtrace_anon.dta_enabling != NULL);
15042 	ASSERT(dtrace_retained != NULL);
15043 
15044 	dtrace_enabling_destroy(dtrace_anon.dta_enabling);
15045 	dtrace_anon.dta_enabling = NULL;
15046 	dtrace_anon.dta_state = NULL;
15047 
15048 	return (state);
15049 }
15050 
15051 static void
dtrace_anon_property(void)15052 dtrace_anon_property(void)
15053 {
15054 	int i, rv;
15055 	dtrace_state_t *state;
15056 	dof_hdr_t *dof;
15057 	char c[32];		/* enough for "dof-data-" + digits */
15058 
15059 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
15060 	LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
15061 
15062 	for (i = 0; ; i++) {
15063 		(void) snprintf(c, sizeof (c), "dof-data-%d", i);
15064 
15065 		dtrace_err_verbose = 1;
15066 
15067 		if ((dof = dtrace_dof_property(c)) == NULL) {
15068 			dtrace_err_verbose = 0;
15069 			break;
15070 		}
15071 
15072 #ifdef illumos
15073 		/*
15074 		 * We want to create anonymous state, so we need to transition
15075 		 * the kernel debugger to indicate that DTrace is active.  If
15076 		 * this fails (e.g. because the debugger has modified text in
15077 		 * some way), we won't continue with the processing.
15078 		 */
15079 		if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
15080 			cmn_err(CE_NOTE, "kernel debugger active; anonymous "
15081 			    "enabling ignored.");
15082 			dtrace_dof_destroy(dof);
15083 			break;
15084 		}
15085 #endif
15086 
15087 		/*
15088 		 * If we haven't allocated an anonymous state, we'll do so now.
15089 		 */
15090 		if ((state = dtrace_anon.dta_state) == NULL) {
15091 			rv = dtrace_state_create(NULL, NULL, &state);
15092 			dtrace_anon.dta_state = state;
15093 			if (rv != 0 || state == NULL) {
15094 				/*
15095 				 * This basically shouldn't happen:  the only
15096 				 * failure mode from dtrace_state_create() is a
15097 				 * failure of ddi_soft_state_zalloc() that
15098 				 * itself should never happen.  Still, the
15099 				 * interface allows for a failure mode, and
15100 				 * we want to fail as gracefully as possible:
15101 				 * we'll emit an error message and cease
15102 				 * processing anonymous state in this case.
15103 				 */
15104 				cmn_err(CE_WARN, "failed to create "
15105 				    "anonymous state");
15106 				dtrace_dof_destroy(dof);
15107 				break;
15108 			}
15109 		}
15110 
15111 		rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(),
15112 		    &dtrace_anon.dta_enabling, 0, B_TRUE);
15113 
15114 		if (rv == 0)
15115 			rv = dtrace_dof_options(dof, state);
15116 
15117 		dtrace_err_verbose = 0;
15118 		dtrace_dof_destroy(dof);
15119 
15120 		if (rv != 0) {
15121 			/*
15122 			 * This is malformed DOF; chuck any anonymous state
15123 			 * that we created.
15124 			 */
15125 			ASSERT(dtrace_anon.dta_enabling == NULL);
15126 			dtrace_state_destroy(state);
15127 			dtrace_anon.dta_state = NULL;
15128 			break;
15129 		}
15130 
15131 		ASSERT(dtrace_anon.dta_enabling != NULL);
15132 	}
15133 
15134 	if (dtrace_anon.dta_enabling != NULL) {
15135 		int rval;
15136 
15137 		/*
15138 		 * dtrace_enabling_retain() can only fail because we are
15139 		 * trying to retain more enablings than are allowed -- but
15140 		 * we only have one anonymous enabling, and we are guaranteed
15141 		 * to be allowed at least one retained enabling; we assert
15142 		 * that dtrace_enabling_retain() returns success.
15143 		 */
15144 		rval = dtrace_enabling_retain(dtrace_anon.dta_enabling);
15145 		ASSERT(rval == 0);
15146 
15147 		dtrace_enabling_dump(dtrace_anon.dta_enabling);
15148 	}
15149 }
15150 
15151 /*
15152  * DTrace Helper Functions
15153  */
15154 static void
dtrace_helper_trace(dtrace_helper_action_t * helper,dtrace_mstate_t * mstate,dtrace_vstate_t * vstate,int where)15155 dtrace_helper_trace(dtrace_helper_action_t *helper,
15156     dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where)
15157 {
15158 	uint32_t size, next, nnext;
15159 	int i;
15160 	dtrace_helptrace_t *ent;
15161 	uint16_t flags = cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
15162 
15163 	if (!dtrace_helptrace_enabled)
15164 		return;
15165 
15166 	ASSERT((uint32_t)vstate->dtvs_nlocals <= dtrace_helptrace_nlocals);
15167 
15168 	/*
15169 	 * What would a tracing framework be without its own tracing
15170 	 * framework?  (Well, a hell of a lot simpler, for starters...)
15171 	 */
15172 	size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals *
15173 	    sizeof (uint64_t) - sizeof (uint64_t);
15174 
15175 	/*
15176 	 * Iterate until we can allocate a slot in the trace buffer.
15177 	 */
15178 	do {
15179 		next = dtrace_helptrace_next;
15180 
15181 		if (next + size < dtrace_helptrace_bufsize) {
15182 			nnext = next + size;
15183 		} else {
15184 			nnext = size;
15185 		}
15186 	} while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next);
15187 
15188 	/*
15189 	 * We have our slot; fill it in.
15190 	 */
15191 	if (nnext == size)
15192 		next = 0;
15193 
15194 	ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next];
15195 	ent->dtht_helper = helper;
15196 	ent->dtht_where = where;
15197 	ent->dtht_nlocals = vstate->dtvs_nlocals;
15198 
15199 	ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ?
15200 	    mstate->dtms_fltoffs : -1;
15201 	ent->dtht_fault = DTRACE_FLAGS2FLT(flags);
15202 	ent->dtht_illval = cpu_core[CPU->cpu_id].cpuc_dtrace_illval;
15203 
15204 	for (i = 0; i < vstate->dtvs_nlocals; i++) {
15205 		dtrace_statvar_t *svar;
15206 
15207 		if ((svar = vstate->dtvs_locals[i]) == NULL)
15208 			continue;
15209 
15210 		ASSERT(svar->dtsv_size >= (int)NCPU * sizeof (uint64_t));
15211 		ent->dtht_locals[i] =
15212 		    ((uint64_t *)(uintptr_t)svar->dtsv_data)[CPU->cpu_id];
15213 	}
15214 }
15215 
15216 __attribute__((noinline))
15217 static uint64_t
dtrace_helper(int which,dtrace_mstate_t * mstate,dtrace_state_t * state,uint64_t arg0,uint64_t arg1)15218 dtrace_helper(int which, dtrace_mstate_t *mstate,
15219     dtrace_state_t *state, uint64_t arg0, uint64_t arg1)
15220 {
15221 	uint16_t *flags = &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
15222 	uint64_t sarg0 = mstate->dtms_arg[0];
15223 	uint64_t sarg1 = mstate->dtms_arg[1];
15224 	uint64_t rval = 0;
15225 	dtrace_helpers_t *helpers = curproc->p_dtrace_helpers;
15226 	dtrace_helper_action_t *helper;
15227 	dtrace_vstate_t *vstate;
15228 	dtrace_difo_t *pred;
15229 	int i, trace = dtrace_helptrace_enabled;
15230 
15231 	ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS);
15232 
15233 	if (helpers == NULL)
15234 		return (0);
15235 
15236 	if ((helper = helpers->dthps_actions[which]) == NULL)
15237 		return (0);
15238 
15239 	vstate = &helpers->dthps_vstate;
15240 	mstate->dtms_arg[0] = arg0;
15241 	mstate->dtms_arg[1] = arg1;
15242 
15243 	/*
15244 	 * Now iterate over each helper.  If its predicate evaluates to 'true',
15245 	 * we'll call the corresponding actions.  Note that the below calls
15246 	 * to dtrace_dif_emulate() may set faults in machine state.  This is
15247 	 * okay:  our caller (the outer dtrace_dif_emulate()) will simply plow
15248 	 * the stored DIF offset with its own (which is the desired behavior).
15249 	 * Also, note the calls to dtrace_dif_emulate() may allocate scratch
15250 	 * from machine state; this is okay, too.
15251 	 */
15252 	for (; helper != NULL; helper = helper->dtha_next) {
15253 		if ((pred = helper->dtha_predicate) != NULL) {
15254 			if (trace)
15255 				dtrace_helper_trace(helper, mstate, vstate, 0);
15256 
15257 			if (!dtrace_dif_emulate(pred, mstate, vstate, state))
15258 				goto next;
15259 
15260 			if (*flags & CPU_DTRACE_FAULT)
15261 				goto err;
15262 		}
15263 
15264 		for (i = 0; i < helper->dtha_nactions; i++) {
15265 			if (trace)
15266 				dtrace_helper_trace(helper,
15267 				    mstate, vstate, i + 1);
15268 
15269 			rval = dtrace_dif_emulate(helper->dtha_actions[i],
15270 			    mstate, vstate, state);
15271 
15272 			if (*flags & CPU_DTRACE_FAULT)
15273 				goto err;
15274 		}
15275 
15276 next:
15277 		if (trace)
15278 			dtrace_helper_trace(helper, mstate, vstate,
15279 			    DTRACE_HELPTRACE_NEXT);
15280 	}
15281 
15282 	if (trace)
15283 		dtrace_helper_trace(helper, mstate, vstate,
15284 		    DTRACE_HELPTRACE_DONE);
15285 
15286 	/*
15287 	 * Restore the arg0 that we saved upon entry.
15288 	 */
15289 	mstate->dtms_arg[0] = sarg0;
15290 	mstate->dtms_arg[1] = sarg1;
15291 
15292 	return (rval);
15293 
15294 err:
15295 	if (trace)
15296 		dtrace_helper_trace(helper, mstate, vstate,
15297 		    DTRACE_HELPTRACE_ERR);
15298 
15299 	/*
15300 	 * Restore the arg0 that we saved upon entry.
15301 	 */
15302 	mstate->dtms_arg[0] = sarg0;
15303 	mstate->dtms_arg[1] = sarg1;
15304 
15305 	return (0);
15306 }
15307 
15308 static void
dtrace_helper_action_destroy(dtrace_helper_action_t * helper,dtrace_vstate_t * vstate)15309 dtrace_helper_action_destroy(dtrace_helper_action_t *helper,
15310     dtrace_vstate_t *vstate)
15311 {
15312 	int i;
15313 
15314 	if (helper->dtha_predicate != NULL)
15315 		dtrace_difo_release(helper->dtha_predicate, vstate);
15316 
15317 	for (i = 0; i < helper->dtha_nactions; i++) {
15318 		ASSERT(helper->dtha_actions[i] != NULL);
15319 		dtrace_difo_release(helper->dtha_actions[i], vstate);
15320 	}
15321 
15322 	kmem_free(helper->dtha_actions,
15323 	    helper->dtha_nactions * sizeof (dtrace_difo_t *));
15324 	kmem_free(helper, sizeof (dtrace_helper_action_t));
15325 }
15326 
15327 static int
dtrace_helper_destroygen(proc_t * p,int gen)15328 dtrace_helper_destroygen(proc_t* p, int gen)
15329 {
15330 	dtrace_helpers_t *help = p->p_dtrace_helpers;
15331 	dtrace_vstate_t *vstate;
15332 	uint_t i;
15333 
15334 	LCK_MTX_ASSERT(&dtrace_meta_lock, LCK_MTX_ASSERT_OWNED);
15335 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
15336 
15337 	if (help == NULL || gen > help->dthps_generation)
15338 		return (EINVAL);
15339 
15340 	vstate = &help->dthps_vstate;
15341 
15342 	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
15343 		dtrace_helper_action_t *last = NULL, *h, *next;
15344 
15345 		for (h = help->dthps_actions[i]; h != NULL; h = next) {
15346 			next = h->dtha_next;
15347 
15348 			if (h->dtha_generation == gen) {
15349 				if (last != NULL) {
15350 					last->dtha_next = next;
15351 				} else {
15352 					help->dthps_actions[i] = next;
15353 				}
15354 
15355 				dtrace_helper_action_destroy(h, vstate);
15356 			} else {
15357 				last = h;
15358 			}
15359 		}
15360 	}
15361 
15362 	/*
15363 	 * Interate until we've cleared out all helper providers with the
15364 	 * given generation number.
15365 	 */
15366 	for (;;) {
15367 		dtrace_helper_provider_t *prov = NULL;
15368 
15369 		/*
15370 		 * Look for a helper provider with the right generation. We
15371 		 * have to start back at the beginning of the list each time
15372 		 * because we drop dtrace_lock. It's unlikely that we'll make
15373 		 * more than two passes.
15374 		 */
15375 		for (i = 0; i < help->dthps_nprovs; i++) {
15376 			prov = help->dthps_provs[i];
15377 
15378 			if (prov->dthp_generation == gen)
15379 				break;
15380 		}
15381 
15382 		/*
15383 		 * If there were no matches, we're done.
15384 		 */
15385 		if (i == help->dthps_nprovs)
15386 			break;
15387 
15388 		/*
15389 		 * Move the last helper provider into this slot.
15390 		 */
15391 		help->dthps_nprovs--;
15392 		help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs];
15393 		help->dthps_provs[help->dthps_nprovs] = NULL;
15394 
15395 		lck_mtx_unlock(&dtrace_lock);
15396 
15397 		/*
15398 		 * If we have a meta provider, remove this helper provider.
15399 		 */
15400 		if (dtrace_meta_pid != NULL) {
15401 			ASSERT(dtrace_deferred_pid == NULL);
15402 			dtrace_helper_provider_remove(&prov->dthp_prov,
15403 			    p);
15404 		}
15405 
15406 		dtrace_helper_provider_destroy(prov);
15407 
15408 		lck_mtx_lock(&dtrace_lock);
15409 	}
15410 
15411 	return (0);
15412 }
15413 
15414 static int
dtrace_helper_validate(dtrace_helper_action_t * helper)15415 dtrace_helper_validate(dtrace_helper_action_t *helper)
15416 {
15417 	int err = 0, i;
15418 	dtrace_difo_t *dp;
15419 
15420 	if ((dp = helper->dtha_predicate) != NULL)
15421 		err += dtrace_difo_validate_helper(dp);
15422 
15423 	for (i = 0; i < helper->dtha_nactions; i++)
15424 		err += dtrace_difo_validate_helper(helper->dtha_actions[i]);
15425 
15426 	return (err == 0);
15427 }
15428 
15429 static int
dtrace_helper_action_add(proc_t * p,int which,dtrace_ecbdesc_t * ep)15430 dtrace_helper_action_add(proc_t* p, int which, dtrace_ecbdesc_t *ep)
15431 {
15432 	dtrace_helpers_t *help;
15433 	dtrace_helper_action_t *helper, *last;
15434 	dtrace_actdesc_t *act;
15435 	dtrace_vstate_t *vstate;
15436 	dtrace_predicate_t *pred;
15437 	int count = 0, nactions = 0, i;
15438 
15439 	if (which < 0 || which >= DTRACE_NHELPER_ACTIONS)
15440 		return (EINVAL);
15441 
15442 	help = p->p_dtrace_helpers;
15443 	last = help->dthps_actions[which];
15444 	vstate = &help->dthps_vstate;
15445 
15446 	for (count = 0; last != NULL; last = last->dtha_next) {
15447 		count++;
15448 		if (last->dtha_next == NULL)
15449 			break;
15450 	}
15451 
15452 	/*
15453 	 * If we already have dtrace_helper_actions_max helper actions for this
15454 	 * helper action type, we'll refuse to add a new one.
15455 	 */
15456 	if (count >= dtrace_helper_actions_max)
15457 		return (ENOSPC);
15458 
15459 	helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP);
15460 	helper->dtha_generation = help->dthps_generation;
15461 
15462 	if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) {
15463 		ASSERT(pred->dtp_difo != NULL);
15464 		dtrace_difo_hold(pred->dtp_difo);
15465 		helper->dtha_predicate = pred->dtp_difo;
15466 	}
15467 
15468 	for (act = ep->dted_action; act != NULL; act = act->dtad_next) {
15469 		if (act->dtad_kind != DTRACEACT_DIFEXPR)
15470 			goto err;
15471 
15472 		if (act->dtad_difo == NULL)
15473 			goto err;
15474 
15475 		nactions++;
15476 	}
15477 
15478 	helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) *
15479 	    (helper->dtha_nactions = nactions), KM_SLEEP);
15480 
15481 	for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) {
15482 		dtrace_difo_hold(act->dtad_difo);
15483 		helper->dtha_actions[i++] = act->dtad_difo;
15484 	}
15485 
15486 	if (!dtrace_helper_validate(helper))
15487 		goto err;
15488 
15489 	if (last == NULL) {
15490 		help->dthps_actions[which] = helper;
15491 	} else {
15492 		last->dtha_next = helper;
15493 	}
15494 
15495 	if ((uint32_t)vstate->dtvs_nlocals > dtrace_helptrace_nlocals) {
15496 		dtrace_helptrace_nlocals = vstate->dtvs_nlocals;
15497 		dtrace_helptrace_next = 0;
15498 	}
15499 
15500 	return (0);
15501 err:
15502 	dtrace_helper_action_destroy(helper, vstate);
15503 	return (EINVAL);
15504 }
15505 
15506 static void
dtrace_helper_provider_register(proc_t * p,dtrace_helpers_t * help,dof_helper_t * dofhp)15507 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help,
15508     dof_helper_t *dofhp)
15509 {
15510 	LCK_MTX_ASSERT(&dtrace_meta_lock, LCK_MTX_ASSERT_OWNED);
15511 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_NOTOWNED);
15512 
15513 	lck_mtx_lock(&dtrace_lock);
15514 
15515 	if (!dtrace_attached() || dtrace_meta_pid == NULL) {
15516 		/*
15517 		 * If the dtrace module is loaded but not attached, or if
15518 		 * there aren't isn't a meta provider registered to deal with
15519 		 * these provider descriptions, we need to postpone creating
15520 		 * the actual providers until later.
15521 		 */
15522 
15523 		if (help->dthps_next == NULL && help->dthps_prev == NULL &&
15524 		    dtrace_deferred_pid != help) {
15525 			help->dthps_deferred = 1;
15526 			help->dthps_pid = proc_getpid(p);
15527 			help->dthps_next = dtrace_deferred_pid;
15528 			help->dthps_prev = NULL;
15529 			if (dtrace_deferred_pid != NULL)
15530 				dtrace_deferred_pid->dthps_prev = help;
15531 			dtrace_deferred_pid = help;
15532 		}
15533 
15534 		lck_mtx_unlock(&dtrace_lock);
15535 
15536 	} else if (dofhp != NULL) {
15537 		/*
15538 		 * If the dtrace module is loaded and we have a particular
15539 		 * helper provider description, pass that off to the
15540 		 * meta provider.
15541 		 */
15542 
15543 		lck_mtx_unlock(&dtrace_lock);
15544 
15545 		dtrace_helper_provide(dofhp, p);
15546 
15547 	} else {
15548 		/*
15549 		 * Otherwise, just pass all the helper provider descriptions
15550 		 * off to the meta provider.
15551 		 */
15552 
15553 		uint_t i;
15554 		lck_mtx_unlock(&dtrace_lock);
15555 
15556 		for (i = 0; i < help->dthps_nprovs; i++) {
15557 			dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
15558 				p);
15559 		}
15560 	}
15561 }
15562 
15563 static int
dtrace_helper_provider_add(proc_t * p,dof_helper_t * dofhp,int gen)15564 dtrace_helper_provider_add(proc_t* p, dof_helper_t *dofhp, int gen)
15565 {
15566 	dtrace_helpers_t *help;
15567 	dtrace_helper_provider_t *hprov, **tmp_provs;
15568 	uint_t tmp_maxprovs, i;
15569 
15570 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
15571 	help = p->p_dtrace_helpers;
15572 	ASSERT(help != NULL);
15573 
15574 	/*
15575 	 * If we already have dtrace_helper_providers_max helper providers,
15576 	 * we're refuse to add a new one.
15577 	 */
15578 	if (help->dthps_nprovs >= dtrace_helper_providers_max)
15579 		return (ENOSPC);
15580 
15581 	/*
15582 	 * Check to make sure this isn't a duplicate.
15583 	 */
15584 	for (i = 0; i < help->dthps_nprovs; i++) {
15585 		if (dofhp->dofhp_addr ==
15586 		    help->dthps_provs[i]->dthp_prov.dofhp_addr)
15587 			return (EALREADY);
15588 	}
15589 
15590 	hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP);
15591 	hprov->dthp_prov = *dofhp;
15592 	hprov->dthp_ref = 1;
15593 	hprov->dthp_generation = gen;
15594 
15595 	/*
15596 	 * Allocate a bigger table for helper providers if it's already full.
15597 	 */
15598 	if (help->dthps_maxprovs == help->dthps_nprovs) {
15599 		tmp_maxprovs = help->dthps_maxprovs;
15600 		tmp_provs = help->dthps_provs;
15601 
15602 		if (help->dthps_maxprovs == 0)
15603 			help->dthps_maxprovs = 2;
15604 		else
15605 			help->dthps_maxprovs *= 2;
15606 		if (help->dthps_maxprovs > dtrace_helper_providers_max)
15607 			help->dthps_maxprovs = dtrace_helper_providers_max;
15608 
15609 		ASSERT(tmp_maxprovs < help->dthps_maxprovs);
15610 
15611 		help->dthps_provs = kmem_zalloc(help->dthps_maxprovs *
15612 		    sizeof (dtrace_helper_provider_t *), KM_SLEEP);
15613 
15614 		if (tmp_provs != NULL) {
15615 			bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs *
15616 			    sizeof (dtrace_helper_provider_t *));
15617 			kmem_free(tmp_provs, tmp_maxprovs *
15618 			    sizeof (dtrace_helper_provider_t *));
15619 		}
15620 	}
15621 
15622 	help->dthps_provs[help->dthps_nprovs] = hprov;
15623 	help->dthps_nprovs++;
15624 
15625 	return (0);
15626 }
15627 
15628 static void
dtrace_helper_provider_destroy(dtrace_helper_provider_t * hprov)15629 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov)
15630 {
15631 	lck_mtx_lock(&dtrace_lock);
15632 
15633 	if (--hprov->dthp_ref == 0) {
15634 		dof_hdr_t *dof;
15635 		lck_mtx_unlock(&dtrace_lock);
15636 		dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof;
15637 		dtrace_dof_destroy(dof);
15638 		kmem_free(hprov, sizeof (dtrace_helper_provider_t));
15639 	} else {
15640 		lck_mtx_unlock(&dtrace_lock);
15641 	}
15642 }
15643 
15644 static int
dtrace_helper_provider_validate(dof_hdr_t * dof,dof_sec_t * sec)15645 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec)
15646 {
15647 	uintptr_t daddr = (uintptr_t)dof;
15648 	dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
15649 	dof_provider_t *provider;
15650 	dof_probe_t *probe;
15651 	uint8_t *arg;
15652 	char *strtab, *typestr;
15653 	dof_stridx_t typeidx;
15654 	size_t typesz;
15655 	uint_t nprobes, j, k;
15656 
15657 	ASSERT(sec->dofs_type == DOF_SECT_PROVIDER);
15658 
15659 	if (sec->dofs_offset & (sizeof (uint_t) - 1)) {
15660 		dtrace_dof_error(dof, "misaligned section offset");
15661 		return (-1);
15662 	}
15663 
15664 	/*
15665 	 * The section needs to be large enough to contain the DOF provider
15666 	 * structure appropriate for the given version.
15667 	 */
15668 	if (sec->dofs_size <
15669 	    ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ?
15670 	    offsetof(dof_provider_t, dofpv_prenoffs) :
15671 	    sizeof (dof_provider_t))) {
15672 		dtrace_dof_error(dof, "provider section too small");
15673 		return (-1);
15674 	}
15675 
15676 	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
15677 	str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab);
15678 	prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes);
15679 	arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs);
15680 	off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs);
15681 
15682 	if (str_sec == NULL || prb_sec == NULL ||
15683 	    arg_sec == NULL || off_sec == NULL)
15684 		return (-1);
15685 
15686 	enoff_sec = NULL;
15687 
15688 	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
15689 	    provider->dofpv_prenoffs != DOF_SECT_NONE &&
15690 	    (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS,
15691 	    provider->dofpv_prenoffs)) == NULL)
15692 		return (-1);
15693 
15694 	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
15695 
15696 	if (provider->dofpv_name >= str_sec->dofs_size ||
15697 	    strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) {
15698 		dtrace_dof_error(dof, "invalid provider name");
15699 		return (-1);
15700 	}
15701 
15702 	if (prb_sec->dofs_entsize == 0 ||
15703 	    prb_sec->dofs_entsize > prb_sec->dofs_size) {
15704 		dtrace_dof_error(dof, "invalid entry size");
15705 		return (-1);
15706 	}
15707 
15708 	if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) {
15709 		dtrace_dof_error(dof, "misaligned entry size");
15710 		return (-1);
15711 	}
15712 
15713 	if (off_sec->dofs_entsize != sizeof (uint32_t)) {
15714 		dtrace_dof_error(dof, "invalid entry size");
15715 		return (-1);
15716 	}
15717 
15718 	if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) {
15719 		dtrace_dof_error(dof, "misaligned section offset");
15720 		return (-1);
15721 	}
15722 
15723 	if (arg_sec->dofs_entsize != sizeof (uint8_t)) {
15724 		dtrace_dof_error(dof, "invalid entry size");
15725 		return (-1);
15726 	}
15727 
15728 	arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
15729 
15730 	nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
15731 
15732 	/*
15733 	 * Take a pass through the probes to check for errors.
15734 	 */
15735 	for (j = 0; j < nprobes; j++) {
15736 		probe = (dof_probe_t *)(uintptr_t)(daddr +
15737 		    prb_sec->dofs_offset + j * prb_sec->dofs_entsize);
15738 
15739 		if (probe->dofpr_func >= str_sec->dofs_size) {
15740 			dtrace_dof_error(dof, "invalid function name");
15741 			return (-1);
15742 		}
15743 
15744 		if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) {
15745 			dtrace_dof_error(dof, "function name too long");
15746 			return (-1);
15747 		}
15748 
15749 		if (probe->dofpr_name >= str_sec->dofs_size ||
15750 		    strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) {
15751 			dtrace_dof_error(dof, "invalid probe name");
15752 			return (-1);
15753 		}
15754 
15755 		/*
15756 		 * The offset count must not wrap the index, and the offsets
15757 		 * must also not overflow the section's data.
15758 		 */
15759 		if (probe->dofpr_offidx + probe->dofpr_noffs <
15760 		    probe->dofpr_offidx ||
15761 		    (probe->dofpr_offidx + probe->dofpr_noffs) *
15762 		    off_sec->dofs_entsize > off_sec->dofs_size) {
15763 			dtrace_dof_error(dof, "invalid probe offset");
15764 			return (-1);
15765 		}
15766 
15767 		if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) {
15768 			/*
15769 			 * If there's no is-enabled offset section, make sure
15770 			 * there aren't any is-enabled offsets. Otherwise
15771 			 * perform the same checks as for probe offsets
15772 			 * (immediately above).
15773 			 */
15774 			if (enoff_sec == NULL) {
15775 				if (probe->dofpr_enoffidx != 0 ||
15776 				    probe->dofpr_nenoffs != 0) {
15777 					dtrace_dof_error(dof, "is-enabled "
15778 					    "offsets with null section");
15779 					return (-1);
15780 				}
15781 			} else if (probe->dofpr_enoffidx +
15782 			    probe->dofpr_nenoffs < probe->dofpr_enoffidx ||
15783 			    (probe->dofpr_enoffidx + probe->dofpr_nenoffs) *
15784 			    enoff_sec->dofs_entsize > enoff_sec->dofs_size) {
15785 				dtrace_dof_error(dof, "invalid is-enabled "
15786 				    "offset");
15787 				return (-1);
15788 			}
15789 
15790 			if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) {
15791 				dtrace_dof_error(dof, "zero probe and "
15792 				    "is-enabled offsets");
15793 				return (-1);
15794 			}
15795 		} else if (probe->dofpr_noffs == 0) {
15796 			dtrace_dof_error(dof, "zero probe offsets");
15797 			return (-1);
15798 		}
15799 
15800 		if (probe->dofpr_argidx + probe->dofpr_xargc <
15801 		    probe->dofpr_argidx ||
15802 		    (probe->dofpr_argidx + probe->dofpr_xargc) *
15803 		    arg_sec->dofs_entsize > arg_sec->dofs_size) {
15804 			dtrace_dof_error(dof, "invalid args");
15805 			return (-1);
15806 		}
15807 
15808 		typeidx = probe->dofpr_nargv;
15809 		typestr = strtab + probe->dofpr_nargv;
15810 		for (k = 0; k < probe->dofpr_nargc; k++) {
15811 			if (typeidx >= str_sec->dofs_size) {
15812 				dtrace_dof_error(dof, "bad "
15813 				    "native argument type");
15814 				return (-1);
15815 			}
15816 
15817 			typesz = strlen(typestr) + 1;
15818 			if (typesz > DTRACE_ARGTYPELEN) {
15819 				dtrace_dof_error(dof, "native "
15820 				    "argument type too long");
15821 				return (-1);
15822 			}
15823 			typeidx += typesz;
15824 			typestr += typesz;
15825 		}
15826 
15827 		typeidx = probe->dofpr_xargv;
15828 		typestr = strtab + probe->dofpr_xargv;
15829 		for (k = 0; k < probe->dofpr_xargc; k++) {
15830 			if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) {
15831 				dtrace_dof_error(dof, "bad "
15832 				    "native argument index");
15833 				return (-1);
15834 			}
15835 
15836 			if (typeidx >= str_sec->dofs_size) {
15837 				dtrace_dof_error(dof, "bad "
15838 				    "translated argument type");
15839 				return (-1);
15840 			}
15841 
15842 			typesz = strlen(typestr) + 1;
15843 			if (typesz > DTRACE_ARGTYPELEN) {
15844 				dtrace_dof_error(dof, "translated argument "
15845 				    "type too long");
15846 				return (-1);
15847 			}
15848 
15849 			typeidx += typesz;
15850 			typestr += typesz;
15851 		}
15852 	}
15853 
15854 	return (0);
15855 }
15856 
15857 static int
dtrace_helper_slurp(proc_t * p,dof_hdr_t * dof,dof_helper_t * dhp)15858 dtrace_helper_slurp(proc_t* p, dof_hdr_t *dof, dof_helper_t *dhp)
15859 {
15860 	dtrace_helpers_t *help;
15861 	dtrace_vstate_t *vstate;
15862 	dtrace_enabling_t *enab = NULL;
15863 	int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1;
15864 	uintptr_t daddr = (uintptr_t)dof;
15865 
15866 	LCK_MTX_ASSERT(&dtrace_meta_lock, LCK_MTX_ASSERT_OWNED);
15867 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
15868 
15869 	if ((help = p->p_dtrace_helpers) == NULL)
15870 		help = dtrace_helpers_create(p);
15871 
15872 	vstate = &help->dthps_vstate;
15873 
15874 	if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab,
15875 	    dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) {
15876 		dtrace_dof_destroy(dof);
15877 		return (rv);
15878 	}
15879 
15880 	/*
15881 	 * Look for helper providers and validate their descriptions.
15882 	 */
15883 	if (dhp != NULL) {
15884 		for (i = 0; (uint32_t)i < dof->dofh_secnum; i++) {
15885 			dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
15886 			    dof->dofh_secoff + i * dof->dofh_secsize);
15887 
15888 			if (sec->dofs_type != DOF_SECT_PROVIDER)
15889 				continue;
15890 
15891 			if (dtrace_helper_provider_validate(dof, sec) != 0) {
15892 				dtrace_enabling_destroy(enab);
15893 				dtrace_dof_destroy(dof);
15894 				return (-1);
15895 			}
15896 
15897 			nprovs++;
15898 		}
15899 	}
15900 
15901 	/*
15902 	 * Now we need to walk through the ECB descriptions in the enabling.
15903 	 */
15904 	for (i = 0; i < enab->dten_ndesc; i++) {
15905 		dtrace_ecbdesc_t *ep = enab->dten_desc[i];
15906 		dtrace_probedesc_t *desc = &ep->dted_probe;
15907 
15908 		/* APPLE NOTE: Darwin employs size bounded string operation. */
15909 		if (!LIT_STRNEQL(desc->dtpd_provider, "dtrace"))
15910 			continue;
15911 
15912 		if (!LIT_STRNEQL(desc->dtpd_mod, "helper"))
15913 			continue;
15914 
15915 		if (!LIT_STRNEQL(desc->dtpd_func, "ustack"))
15916 			continue;
15917 
15918 		if ((rv = dtrace_helper_action_add(p, DTRACE_HELPER_ACTION_USTACK,
15919 		    ep)) != 0) {
15920 			/*
15921 			 * Adding this helper action failed -- we are now going
15922 			 * to rip out the entire generation and return failure.
15923 			 */
15924 			(void) dtrace_helper_destroygen(p, help->dthps_generation);
15925 			dtrace_enabling_destroy(enab);
15926 			dtrace_dof_destroy(dof);
15927 			return (-1);
15928 		}
15929 
15930 		nhelpers++;
15931 	}
15932 
15933 	if (nhelpers < enab->dten_ndesc)
15934 		dtrace_dof_error(dof, "unmatched helpers");
15935 
15936 	gen = help->dthps_generation++;
15937 	dtrace_enabling_destroy(enab);
15938 
15939 	if (dhp != NULL && nprovs > 0) {
15940 		dhp->dofhp_dof = (uint64_t)(uintptr_t)dof;
15941 		if (dtrace_helper_provider_add(p, dhp, gen) == 0) {
15942 			lck_mtx_unlock(&dtrace_lock);
15943 			dtrace_helper_provider_register(p, help, dhp);
15944 			lck_mtx_lock(&dtrace_lock);
15945 
15946 			destroy = 0;
15947 		}
15948 	}
15949 
15950 	if (destroy)
15951 		dtrace_dof_destroy(dof);
15952 
15953 	return (gen);
15954 }
15955 
15956 /*
15957  * APPLE NOTE:  DTrace lazy dof implementation
15958  *
15959  * DTrace user static probes (USDT probes) and helper actions are loaded
15960  * in a process by proccessing dof sections. The dof sections are passed
15961  * into the kernel by dyld, in a dof_ioctl_data_t block. It is rather
15962  * expensive to process dof for a process that will never use it. There
15963  * is a memory cost (allocating the providers/probes), and a cpu cost
15964  * (creating the providers/probes).
15965  *
15966  * To reduce this cost, we use "lazy dof". The normal proceedure for
15967  * dof processing is to copyin the dof(s) pointed to by the dof_ioctl_data_t
15968  * block, and invoke dof_slurp_helper() on them. When "lazy dof" is
15969  * used, each process retains the dof_ioctl_data_t block, instead of
15970  * copying in the data it points to.
15971  *
15972  * The dof_ioctl_data_t blocks are managed as if they were the actual
15973  * processed dof; on fork the block is copied to the child, on exec and
15974  * exit the block is freed.
15975  *
15976  * If the process loads library(s) containing additional dof, the
15977  * new dof_ioctl_data_t is merged with the existing block.
15978  *
15979  * There are a few catches that make this slightly more difficult.
15980  * When dyld registers dof_ioctl_data_t blocks, it expects a unique
15981  * identifier value for each dof in the block. In non-lazy dof terms,
15982  * this is the generation that dof was loaded in. If we hand back
15983  * a UID for a lazy dof, that same UID must be able to unload the
15984  * dof once it has become non-lazy. To meet this requirement, the
15985  * code that loads lazy dof requires that the UID's for dof(s) in
15986  * the lazy dof be sorted, and in ascending order. It is okay to skip
15987  * UID's, I.E., 1 -> 5 -> 6 is legal.
15988  *
15989  * Once a process has become non-lazy, it will stay non-lazy. All
15990  * future dof operations for that process will be non-lazy, even
15991  * if the dof mode transitions back to lazy.
15992  *
15993  * Always do lazy dof checks before non-lazy (I.E. In fork, exit, exec.).
15994  * That way if the lazy check fails due to transitioning to non-lazy, the
15995  * right thing is done with the newly faulted in dof.
15996  */
15997 
15998 /*
15999  * This method is a bit squicky. It must handle:
16000  *
16001  * dof should not be lazy.
16002  * dof should have been handled lazily, but there was an error
16003  * dof was handled lazily, and needs to be freed.
16004  * dof was handled lazily, and must not be freed.
16005  *
16006  *
16007  * Returns EACCESS if dof should be handled non-lazily.
16008  *
16009  * KERN_SUCCESS and all other return codes indicate lazy handling of dof.
16010  *
16011  * If the dofs data is claimed by this method, dofs_claimed will be set.
16012  * Callers should not free claimed dofs.
16013  */
16014 static int
dtrace_lazy_dofs_add(proc_t * p,dof_ioctl_data_t * incoming_dofs,int * dofs_claimed)16015 dtrace_lazy_dofs_add(proc_t *p, dof_ioctl_data_t* incoming_dofs, int *dofs_claimed)
16016 {
16017 	ASSERT(p);
16018 	ASSERT(incoming_dofs && incoming_dofs->dofiod_count > 0);
16019 
16020 	int rval = 0;
16021 	*dofs_claimed = 0;
16022 
16023 	lck_rw_lock_shared(&dtrace_dof_mode_lock);
16024 
16025 	ASSERT(p->p_dtrace_lazy_dofs == NULL || p->p_dtrace_helpers == NULL);
16026 	ASSERT(dtrace_dof_mode != DTRACE_DOF_MODE_NEVER);
16027 
16028 	/*
16029 	 * Any existing helpers force non-lazy behavior.
16030 	 */
16031 	if (dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_ON && (p->p_dtrace_helpers == NULL)) {
16032 		dtrace_sprlock(p);
16033 
16034 		dof_ioctl_data_t* existing_dofs = p->p_dtrace_lazy_dofs;
16035 		unsigned int existing_dofs_count = (existing_dofs) ? existing_dofs->dofiod_count : 0;
16036 		unsigned int i, merged_dofs_count = incoming_dofs->dofiod_count + existing_dofs_count;
16037 
16038 		/*
16039 		 * Range check...
16040 		 */
16041 		if (merged_dofs_count == 0 || merged_dofs_count > 1024) {
16042 			dtrace_dof_error(NULL, "lazy_dofs_add merged_dofs_count out of range");
16043 			rval = EINVAL;
16044 			goto unlock;
16045 		}
16046 
16047 		/*
16048 		 * Each dof being added must be assigned a unique generation.
16049 		 */
16050 		uint64_t generation = (existing_dofs) ? existing_dofs->dofiod_helpers[existing_dofs_count - 1].dofhp_dof + 1 : 1;
16051 		for (i=0; i<incoming_dofs->dofiod_count; i++) {
16052 			/*
16053 			 * We rely on these being the same so we can overwrite dofhp_dof and not lose info.
16054 			 */
16055 			ASSERT(incoming_dofs->dofiod_helpers[i].dofhp_dof == incoming_dofs->dofiod_helpers[i].dofhp_addr);
16056 			incoming_dofs->dofiod_helpers[i].dofhp_dof = generation++;
16057 		}
16058 
16059 
16060 		if (existing_dofs) {
16061 			/*
16062 			 * Merge the existing and incoming dofs
16063 			 */
16064 			size_t merged_dofs_size = DOF_IOCTL_DATA_T_SIZE(merged_dofs_count);
16065 			dof_ioctl_data_t* merged_dofs = kmem_alloc(merged_dofs_size, KM_SLEEP);
16066 
16067 			bcopy(&existing_dofs->dofiod_helpers[0],
16068 			      &merged_dofs->dofiod_helpers[0],
16069 			      sizeof(dof_helper_t) * existing_dofs_count);
16070 			bcopy(&incoming_dofs->dofiod_helpers[0],
16071 			      &merged_dofs->dofiod_helpers[existing_dofs_count],
16072 			      sizeof(dof_helper_t) * incoming_dofs->dofiod_count);
16073 
16074 			merged_dofs->dofiod_count = merged_dofs_count;
16075 
16076 			kmem_free(existing_dofs, DOF_IOCTL_DATA_T_SIZE(existing_dofs_count));
16077 
16078 			p->p_dtrace_lazy_dofs = merged_dofs;
16079 		} else {
16080 			/*
16081 			 * Claim the incoming dofs
16082 			 */
16083 			*dofs_claimed = 1;
16084 			p->p_dtrace_lazy_dofs = incoming_dofs;
16085 		}
16086 
16087 #if DEBUG
16088 		dof_ioctl_data_t* all_dofs = p->p_dtrace_lazy_dofs;
16089 		for (i=0; i<all_dofs->dofiod_count-1; i++) {
16090 			ASSERT(all_dofs->dofiod_helpers[i].dofhp_dof < all_dofs->dofiod_helpers[i+1].dofhp_dof);
16091 		}
16092 #endif /* DEBUG */
16093 
16094 unlock:
16095 		dtrace_sprunlock(p);
16096 	} else {
16097 		rval = EACCES;
16098 	}
16099 
16100  	lck_rw_unlock_shared(&dtrace_dof_mode_lock);
16101 
16102 	return rval;
16103 }
16104 
16105 /*
16106  * Returns:
16107  *
16108  * EINVAL: lazy dof is enabled, but the requested generation was not found.
16109  * EACCES: This removal needs to be handled non-lazily.
16110  */
16111 static int
dtrace_lazy_dofs_remove(proc_t * p,int generation)16112 dtrace_lazy_dofs_remove(proc_t *p, int generation)
16113 {
16114 	int rval = EINVAL;
16115 
16116 	lck_rw_lock_shared(&dtrace_dof_mode_lock);
16117 
16118 	ASSERT(p->p_dtrace_lazy_dofs == NULL || p->p_dtrace_helpers == NULL);
16119 	ASSERT(dtrace_dof_mode != DTRACE_DOF_MODE_NEVER);
16120 
16121 	/*
16122 	 * Any existing helpers force non-lazy behavior.
16123 	 */
16124 	if (dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_ON && (p->p_dtrace_helpers == NULL)) {
16125 		dtrace_sprlock(p);
16126 
16127 		dof_ioctl_data_t* existing_dofs = p->p_dtrace_lazy_dofs;
16128 
16129 		if (existing_dofs) {
16130 			int index, existing_dofs_count = existing_dofs->dofiod_count;
16131 			for (index=0; index<existing_dofs_count; index++) {
16132 				if ((int)existing_dofs->dofiod_helpers[index].dofhp_dof == generation) {
16133 					dof_ioctl_data_t* removed_dofs = NULL;
16134 
16135 					/*
16136 					 * If there is only 1 dof, we'll delete it and swap in NULL.
16137 					 */
16138 					if (existing_dofs_count > 1) {
16139 						int removed_dofs_count = existing_dofs_count - 1;
16140 						size_t removed_dofs_size = DOF_IOCTL_DATA_T_SIZE(removed_dofs_count);
16141 
16142 						removed_dofs = kmem_alloc(removed_dofs_size, KM_SLEEP);
16143 						removed_dofs->dofiod_count = removed_dofs_count;
16144 
16145 						/*
16146 						 * copy the remaining data.
16147 						 */
16148 						if (index > 0) {
16149 							bcopy(&existing_dofs->dofiod_helpers[0],
16150 							      &removed_dofs->dofiod_helpers[0],
16151 							      index * sizeof(dof_helper_t));
16152 						}
16153 
16154 						if (index < existing_dofs_count-1) {
16155 							bcopy(&existing_dofs->dofiod_helpers[index+1],
16156 							      &removed_dofs->dofiod_helpers[index],
16157 							      (existing_dofs_count - index - 1) * sizeof(dof_helper_t));
16158 						}
16159 					}
16160 
16161 					kmem_free(existing_dofs, DOF_IOCTL_DATA_T_SIZE(existing_dofs_count));
16162 
16163 					p->p_dtrace_lazy_dofs = removed_dofs;
16164 
16165 					rval = KERN_SUCCESS;
16166 
16167 					break;
16168 				}
16169 			}
16170 
16171 #if DEBUG
16172 			dof_ioctl_data_t* all_dofs = p->p_dtrace_lazy_dofs;
16173 			if (all_dofs) {
16174 				unsigned int i;
16175 				for (i=0; i<all_dofs->dofiod_count-1; i++) {
16176 					ASSERT(all_dofs->dofiod_helpers[i].dofhp_dof < all_dofs->dofiod_helpers[i+1].dofhp_dof);
16177 				}
16178 			}
16179 #endif
16180 
16181 		}
16182 		dtrace_sprunlock(p);
16183 	} else {
16184 		rval = EACCES;
16185 	}
16186 
16187 	lck_rw_unlock_shared(&dtrace_dof_mode_lock);
16188 
16189 	return rval;
16190 }
16191 
16192 void
dtrace_lazy_dofs_destroy(proc_t * p)16193 dtrace_lazy_dofs_destroy(proc_t *p)
16194 {
16195 	lck_rw_lock_shared(&dtrace_dof_mode_lock);
16196 	dtrace_sprlock(p);
16197 
16198 	ASSERT(p->p_dtrace_lazy_dofs == NULL || p->p_dtrace_helpers == NULL);
16199 
16200 	dof_ioctl_data_t* lazy_dofs = p->p_dtrace_lazy_dofs;
16201 	p->p_dtrace_lazy_dofs = NULL;
16202 
16203 	dtrace_sprunlock(p);
16204 	lck_rw_unlock_shared(&dtrace_dof_mode_lock);
16205 
16206 	if (lazy_dofs) {
16207 		kmem_free(lazy_dofs, DOF_IOCTL_DATA_T_SIZE(lazy_dofs->dofiod_count));
16208 	}
16209 }
16210 
16211 static int
dtrace_lazy_dofs_proc_iterate_filter(proc_t * p,void * ignored)16212 dtrace_lazy_dofs_proc_iterate_filter(proc_t *p, void* ignored)
16213 {
16214 #pragma unused(ignored)
16215 	/*
16216 	 * Okay to NULL test without taking the sprlock.
16217 	 */
16218 	return p->p_dtrace_lazy_dofs != NULL;
16219 }
16220 
16221 static void
dtrace_lazy_dofs_process(proc_t * p)16222 dtrace_lazy_dofs_process(proc_t *p) {
16223 	/*
16224 	 * It is possible this process may exit during our attempt to
16225 	 * fault in the dof. We could fix this by holding locks longer,
16226 	 * but the errors are benign.
16227 	 */
16228 	dtrace_sprlock(p);
16229 
16230 
16231 	ASSERT(p->p_dtrace_lazy_dofs == NULL || p->p_dtrace_helpers == NULL);
16232 	ASSERT(dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_OFF);
16233 
16234 	dof_ioctl_data_t* lazy_dofs = p->p_dtrace_lazy_dofs;
16235 	p->p_dtrace_lazy_dofs = NULL;
16236 
16237 	dtrace_sprunlock(p);
16238 	lck_mtx_lock(&dtrace_meta_lock);
16239 	/*
16240 	 * Process each dof_helper_t
16241 	 */
16242 	if (lazy_dofs != NULL) {
16243 		unsigned int i;
16244 		int rval;
16245 
16246 		for (i=0; i<lazy_dofs->dofiod_count; i++) {
16247 			/*
16248 			 * When loading lazy dof, we depend on the generations being sorted in ascending order.
16249 			 */
16250 			ASSERT(i >= (lazy_dofs->dofiod_count - 1) || lazy_dofs->dofiod_helpers[i].dofhp_dof < lazy_dofs->dofiod_helpers[i+1].dofhp_dof);
16251 
16252 			dof_helper_t *dhp = &lazy_dofs->dofiod_helpers[i];
16253 
16254 			/*
16255 			 * We stored the generation in dofhp_dof. Save it, and restore the original value.
16256 			 */
16257 			int generation = dhp->dofhp_dof;
16258 			dhp->dofhp_dof = dhp->dofhp_addr;
16259 
16260 			dof_hdr_t *dof = dtrace_dof_copyin_from_proc(p, dhp->dofhp_dof, &rval);
16261 
16262 			if (dof != NULL) {
16263 				dtrace_helpers_t *help;
16264 
16265 				lck_mtx_lock(&dtrace_lock);
16266 
16267 				/*
16268 				 * This must be done with the dtrace_lock held
16269 				 */
16270 				if ((help = p->p_dtrace_helpers) == NULL)
16271 					help = dtrace_helpers_create(p);
16272 
16273 				/*
16274 				 * If the generation value has been bumped, someone snuck in
16275 				 * when we released the dtrace lock. We have to dump this generation,
16276 				 * there is no safe way to load it.
16277 				 */
16278 				if (help->dthps_generation <= generation) {
16279 					help->dthps_generation = generation;
16280 
16281 					/*
16282 					 * dtrace_helper_slurp() takes responsibility for the dof --
16283 					 * it may free it now or it may save it and free it later.
16284 					 */
16285 					if ((rval = dtrace_helper_slurp(p, dof, dhp)) != generation) {
16286 						dtrace_dof_error(NULL, "returned value did not match expected generation");
16287 					}
16288 				}
16289 
16290 				lck_mtx_unlock(&dtrace_lock);
16291 			}
16292 		}
16293 		lck_mtx_unlock(&dtrace_meta_lock);
16294 		kmem_free(lazy_dofs, DOF_IOCTL_DATA_T_SIZE(lazy_dofs->dofiod_count));
16295 	} else {
16296 		lck_mtx_unlock(&dtrace_meta_lock);
16297 	}
16298 }
16299 
16300 static int
dtrace_lazy_dofs_proc_iterate_doit(proc_t * p,void * ignored)16301 dtrace_lazy_dofs_proc_iterate_doit(proc_t *p, void* ignored)
16302 {
16303 #pragma unused(ignored)
16304 
16305 	dtrace_lazy_dofs_process(p);
16306 
16307 	return PROC_RETURNED;
16308 }
16309 
16310 #define DTRACE_LAZY_DOFS_DUPLICATED 1
16311 
16312 static int
dtrace_lazy_dofs_duplicate(proc_t * parent,proc_t * child)16313 dtrace_lazy_dofs_duplicate(proc_t *parent, proc_t *child)
16314 {
16315 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_NOTOWNED);
16316 	LCK_MTX_ASSERT(&parent->p_dtrace_sprlock, LCK_MTX_ASSERT_NOTOWNED);
16317 	LCK_MTX_ASSERT(&child->p_dtrace_sprlock, LCK_MTX_ASSERT_NOTOWNED);
16318 
16319 	lck_rw_lock_shared(&dtrace_dof_mode_lock);
16320 	dtrace_sprlock(parent);
16321 
16322 	/*
16323 	 * We need to make sure that the transition to lazy dofs -> helpers
16324 	 * was atomic for our parent
16325 	 */
16326 	ASSERT(parent->p_dtrace_lazy_dofs == NULL || parent->p_dtrace_helpers == NULL);
16327 	/*
16328 	 * In theory we should hold the child sprlock, but this is safe...
16329 	 */
16330 	ASSERT(child->p_dtrace_lazy_dofs == NULL && child->p_dtrace_helpers == NULL);
16331 
16332 	dof_ioctl_data_t* parent_dofs = parent->p_dtrace_lazy_dofs;
16333 	dof_ioctl_data_t* child_dofs = NULL;
16334 	if (parent_dofs) {
16335 		size_t parent_dofs_size = DOF_IOCTL_DATA_T_SIZE(parent_dofs->dofiod_count);
16336 		child_dofs = kmem_alloc(parent_dofs_size, KM_SLEEP);
16337 		bcopy(parent_dofs, child_dofs, parent_dofs_size);
16338 	}
16339 
16340 	dtrace_sprunlock(parent);
16341 
16342 	if (child_dofs) {
16343 		dtrace_sprlock(child);
16344 		child->p_dtrace_lazy_dofs = child_dofs;
16345 		dtrace_sprunlock(child);
16346 		/**
16347 		 * We process the DOF at this point if the mode is set to
16348 		 * LAZY_OFF. This can happen if DTrace is still processing the
16349 		 * DOF of other process (which can happen because the
16350 		 * protected pager can have a huge latency)
16351 		 * but has not processed our parent yet
16352 		 */
16353 		if (dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_OFF) {
16354 			dtrace_lazy_dofs_process(child);
16355 		}
16356 		lck_rw_unlock_shared(&dtrace_dof_mode_lock);
16357 
16358 		return DTRACE_LAZY_DOFS_DUPLICATED;
16359 	}
16360 	lck_rw_unlock_shared(&dtrace_dof_mode_lock);
16361 
16362 	return 0;
16363 }
16364 
16365 static dtrace_helpers_t *
dtrace_helpers_create(proc_t * p)16366 dtrace_helpers_create(proc_t *p)
16367 {
16368 	dtrace_helpers_t *help;
16369 
16370 	LCK_MTX_ASSERT(&dtrace_lock, LCK_MTX_ASSERT_OWNED);
16371 	ASSERT(p->p_dtrace_helpers == NULL);
16372 
16373 	help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP);
16374 	help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) *
16375 	    DTRACE_NHELPER_ACTIONS, KM_SLEEP);
16376 
16377 	p->p_dtrace_helpers = help;
16378 	dtrace_helpers++;
16379 
16380 	return (help);
16381 }
16382 
16383 static void
dtrace_helpers_destroy(proc_t * p)16384 dtrace_helpers_destroy(proc_t* p)
16385 {
16386 	dtrace_helpers_t *help;
16387 	dtrace_vstate_t *vstate;
16388 	uint_t i;
16389 
16390 	lck_mtx_lock(&dtrace_meta_lock);
16391 	lck_mtx_lock(&dtrace_lock);
16392 
16393 	ASSERT(p->p_dtrace_helpers != NULL);
16394 	ASSERT(dtrace_helpers > 0);
16395 
16396 	help = p->p_dtrace_helpers;
16397 	vstate = &help->dthps_vstate;
16398 
16399 	/*
16400 	 * We're now going to lose the help from this process.
16401 	 */
16402 	p->p_dtrace_helpers = NULL;
16403 	dtrace_sync();
16404 
16405 	/*
16406 	 * Destory the helper actions.
16407 	 */
16408 	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
16409 		dtrace_helper_action_t *h, *next;
16410 
16411 		for (h = help->dthps_actions[i]; h != NULL; h = next) {
16412 			next = h->dtha_next;
16413 			dtrace_helper_action_destroy(h, vstate);
16414 			h = next;
16415 		}
16416 	}
16417 
16418 	lck_mtx_unlock(&dtrace_lock);
16419 
16420 	/*
16421 	 * Destroy the helper providers.
16422 	 */
16423 	if (help->dthps_maxprovs > 0) {
16424 		if (dtrace_meta_pid != NULL) {
16425 			ASSERT(dtrace_deferred_pid == NULL);
16426 
16427 			for (i = 0; i < help->dthps_nprovs; i++) {
16428 				dtrace_helper_provider_remove(
16429 				    &help->dthps_provs[i]->dthp_prov, p);
16430 			}
16431 		} else {
16432 			lck_mtx_lock(&dtrace_lock);
16433 			ASSERT(help->dthps_deferred == 0 ||
16434 			    help->dthps_next != NULL ||
16435 			    help->dthps_prev != NULL ||
16436 			    help == dtrace_deferred_pid);
16437 
16438 			/*
16439 			 * Remove the helper from the deferred list.
16440 			 */
16441 			if (help->dthps_next != NULL)
16442 				help->dthps_next->dthps_prev = help->dthps_prev;
16443 			if (help->dthps_prev != NULL)
16444 				help->dthps_prev->dthps_next = help->dthps_next;
16445 			if (dtrace_deferred_pid == help) {
16446 				dtrace_deferred_pid = help->dthps_next;
16447 				ASSERT(help->dthps_prev == NULL);
16448 			}
16449 
16450 			lck_mtx_unlock(&dtrace_lock);
16451 		}
16452 
16453 
16454 		for (i = 0; i < help->dthps_nprovs; i++) {
16455 			dtrace_helper_provider_destroy(help->dthps_provs[i]);
16456 		}
16457 
16458 		kmem_free(help->dthps_provs, help->dthps_maxprovs *
16459 		    sizeof (dtrace_helper_provider_t *));
16460 	}
16461 
16462 	lck_mtx_lock(&dtrace_lock);
16463 
16464 	dtrace_vstate_fini(&help->dthps_vstate);
16465 	kmem_free(help->dthps_actions,
16466 	    sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS);
16467 	kmem_free(help, sizeof (dtrace_helpers_t));
16468 
16469 	--dtrace_helpers;
16470 	lck_mtx_unlock(&dtrace_lock);
16471 	lck_mtx_unlock(&dtrace_meta_lock);
16472 }
16473 
16474 static void
dtrace_helpers_duplicate(proc_t * from,proc_t * to)16475 dtrace_helpers_duplicate(proc_t *from, proc_t *to)
16476 {
16477 	dtrace_helpers_t *help, *newhelp;
16478 	dtrace_helper_action_t *helper, *new, *last;
16479 	dtrace_difo_t *dp;
16480 	dtrace_vstate_t *vstate;
16481 	uint_t i;
16482 	int j, sz, hasprovs = 0;
16483 
16484 	lck_mtx_lock(&dtrace_meta_lock);
16485 	lck_mtx_lock(&dtrace_lock);
16486 	ASSERT(from->p_dtrace_helpers != NULL);
16487 	ASSERT(dtrace_helpers > 0);
16488 
16489 	help = from->p_dtrace_helpers;
16490 	newhelp = dtrace_helpers_create(to);
16491 	ASSERT(to->p_dtrace_helpers != NULL);
16492 
16493 	newhelp->dthps_generation = help->dthps_generation;
16494 	vstate = &newhelp->dthps_vstate;
16495 
16496 	/*
16497 	 * Duplicate the helper actions.
16498 	 */
16499 	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
16500 		if ((helper = help->dthps_actions[i]) == NULL)
16501 			continue;
16502 
16503 		for (last = NULL; helper != NULL; helper = helper->dtha_next) {
16504 			new = kmem_zalloc(sizeof (dtrace_helper_action_t),
16505 			    KM_SLEEP);
16506 			new->dtha_generation = helper->dtha_generation;
16507 
16508 			if ((dp = helper->dtha_predicate) != NULL) {
16509 				dp = dtrace_difo_duplicate(dp, vstate);
16510 				new->dtha_predicate = dp;
16511 			}
16512 
16513 			new->dtha_nactions = helper->dtha_nactions;
16514 			sz = sizeof (dtrace_difo_t *) * new->dtha_nactions;
16515 			new->dtha_actions = kmem_alloc(sz, KM_SLEEP);
16516 
16517 			for (j = 0; j < new->dtha_nactions; j++) {
16518 				dtrace_difo_t *dpj = helper->dtha_actions[j];
16519 
16520 				ASSERT(dpj != NULL);
16521 				dpj = dtrace_difo_duplicate(dpj, vstate);
16522 				new->dtha_actions[j] = dpj;
16523 			}
16524 
16525 			if (last != NULL) {
16526 				last->dtha_next = new;
16527 			} else {
16528 				newhelp->dthps_actions[i] = new;
16529 			}
16530 
16531 			last = new;
16532 		}
16533 	}
16534 
16535 	/*
16536 	 * Duplicate the helper providers and register them with the
16537 	 * DTrace framework.
16538 	 */
16539 	if (help->dthps_nprovs > 0) {
16540 		newhelp->dthps_nprovs = help->dthps_nprovs;
16541 		newhelp->dthps_maxprovs = help->dthps_nprovs;
16542 		newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs *
16543 		    sizeof (dtrace_helper_provider_t *), KM_SLEEP);
16544 		for (i = 0; i < newhelp->dthps_nprovs; i++) {
16545 			newhelp->dthps_provs[i] = help->dthps_provs[i];
16546 			newhelp->dthps_provs[i]->dthp_ref++;
16547 		}
16548 
16549 		hasprovs = 1;
16550 	}
16551 
16552 	lck_mtx_unlock(&dtrace_lock);
16553 
16554 	if (hasprovs)
16555 		dtrace_helper_provider_register(to, newhelp, NULL);
16556 
16557 	lck_mtx_unlock(&dtrace_meta_lock);
16558 }
16559 
16560 /**
16561  * DTrace Process functions
16562  */
16563 
16564 void
dtrace_proc_fork(proc_t * parent_proc,proc_t * child_proc,int spawn)16565 dtrace_proc_fork(proc_t *parent_proc, proc_t *child_proc, int spawn)
16566 {
16567 	/*
16568 	 * This code applies to new processes who are copying the task
16569 	 * and thread state and address spaces of their parent process.
16570 	 */
16571 	if (!spawn) {
16572 		/*
16573 		 * APPLE NOTE: Solaris does a sprlock() and drops the
16574 		 * proc_lock here. We're cheating a bit and only taking
16575 		 * the p_dtrace_sprlock lock. A full sprlock would
16576 		 * task_suspend the parent.
16577 		 */
16578 		dtrace_sprlock(parent_proc);
16579 
16580 		/*
16581 		 * Remove all DTrace tracepoints from the child process. We
16582 		 * need to do this _before_ duplicating USDT providers since
16583 		 * any associated probes may be immediately enabled.
16584 		 */
16585 		if (parent_proc->p_dtrace_count > 0) {
16586 			dtrace_fasttrap_fork(parent_proc, child_proc);
16587 		}
16588 
16589 		dtrace_sprunlock(parent_proc);
16590 
16591 		/*
16592 		 * Duplicate any lazy dof(s). This must be done while NOT
16593 		 * holding the parent sprlock! Lock ordering is
16594 		 * dtrace_dof_mode_lock, then sprlock.  It is imperative we
16595 		 * always call dtrace_lazy_dofs_duplicate, rather than null
16596 		 * check and call if !NULL. If we NULL test, during lazy dof
16597 		 * faulting we can race with the faulting code and proceed
16598 		 * from here to beyond the helpers copy. The lazy dof
16599 		 * faulting will then fail to copy the helpers to the child
16600 		 * process. We return if we duplicated lazy dofs as a process
16601 		 * can only have one at the same time to avoid a race between
16602 		 * a dtrace client and dtrace_proc_fork where a process would
16603 		 * end up with both lazy dofs and helpers.
16604 		 */
16605 		if (dtrace_lazy_dofs_duplicate(parent_proc, child_proc) == DTRACE_LAZY_DOFS_DUPLICATED) {
16606 			return;
16607 		}
16608 
16609 		/*
16610 		 * Duplicate any helper actions and providers if they haven't
16611 		 * already.
16612 		 */
16613 #if !defined(__APPLE__)
16614 		 /*
16615 		 * The SFORKING
16616 		 * we set above informs the code to enable USDT probes that
16617 		 * sprlock() may fail because the child is being forked.
16618 		 */
16619 #endif
16620 		/*
16621 		 * APPLE NOTE: As best I can tell, Apple's sprlock() equivalent
16622 		 * never fails to find the child. We do not set SFORKING.
16623 		 */
16624 		if (parent_proc->p_dtrace_helpers != NULL && dtrace_helpers_fork) {
16625 			(*dtrace_helpers_fork)(parent_proc, child_proc);
16626 		}
16627 	}
16628 }
16629 
16630 void
dtrace_proc_exec(proc_t * p)16631 dtrace_proc_exec(proc_t *p)
16632 {
16633 	/*
16634 	 * Invalidate any predicate evaluation already cached for this thread by DTrace.
16635 	 * That's because we've just stored to p_comm and DTrace refers to that when it
16636 	 * evaluates the "execname" special variable. uid and gid may have changed as well.
16637 	 */
16638 	dtrace_set_thread_predcache(current_thread(), 0);
16639 
16640 	/*
16641 	 * Free any outstanding lazy dof entries. It is imperative we
16642 	 * always call dtrace_lazy_dofs_destroy, rather than null check
16643 	 * and call if !NULL. If we NULL test, during lazy dof faulting
16644 	 * we can race with the faulting code and proceed from here to
16645 	 * beyond the helpers cleanup. The lazy dof faulting will then
16646 	 * install new helpers which no longer belong to this process!
16647 	 */
16648 	dtrace_lazy_dofs_destroy(p);
16649 
16650 
16651 	/*
16652 	 * Clean up any DTrace helpers for the process.
16653 	 */
16654 	if (p->p_dtrace_helpers != NULL && dtrace_helpers_cleanup) {
16655 		(*dtrace_helpers_cleanup)(p);
16656 	}
16657 
16658 	/*
16659 	 * Cleanup the DTrace provider associated with this process.
16660 	 */
16661 	proc_lock(p);
16662 	if (p->p_dtrace_probes && dtrace_fasttrap_exec_ptr) {
16663 		(*dtrace_fasttrap_exec_ptr)(p);
16664 	}
16665 	proc_unlock(p);
16666 }
16667 
16668 void
dtrace_proc_exit(proc_t * p)16669 dtrace_proc_exit(proc_t *p)
16670 {
16671 	/*
16672 	 * Free any outstanding lazy dof entries. It is imperative we
16673 	 * always call dtrace_lazy_dofs_destroy, rather than null check
16674 	 * and call if !NULL. If we NULL test, during lazy dof faulting
16675 	 * we can race with the faulting code and proceed from here to
16676 	 * beyond the helpers cleanup. The lazy dof faulting will then
16677 	 * install new helpers which will never be cleaned up, and leak.
16678 	 */
16679 	dtrace_lazy_dofs_destroy(p);
16680 
16681 	/*
16682 	 * Clean up any DTrace helper actions or probes for the process.
16683 	 */
16684 	if (p->p_dtrace_helpers != NULL) {
16685 		(*dtrace_helpers_cleanup)(p);
16686 	}
16687 
16688 	/*
16689 	 * Clean up any DTrace probes associated with this process.
16690 	 */
16691 	/*
16692 	 * APPLE NOTE: We release ptss pages/entries in dtrace_fasttrap_exit_ptr(),
16693 	 * call this after dtrace_helpers_cleanup()
16694 	 */
16695 	proc_lock(p);
16696 	if (p->p_dtrace_probes && dtrace_fasttrap_exit_ptr) {
16697 		(*dtrace_fasttrap_exit_ptr)(p);
16698 	}
16699 	proc_unlock(p);
16700 }
16701 
16702 /*
16703  * DTrace Hook Functions
16704  */
16705 
16706 /*
16707  * APPLE NOTE:  dtrace_modctl_* routines for kext support.
16708  * Used to manipulate the modctl list within dtrace xnu.
16709  */
16710 
16711 modctl_t *dtrace_modctl_list;
16712 
16713 static void
dtrace_modctl_add(struct modctl * newctl)16714 dtrace_modctl_add(struct modctl * newctl)
16715 {
16716 	struct modctl *nextp, *prevp;
16717 
16718 	ASSERT(newctl != NULL);
16719 	LCK_MTX_ASSERT(&mod_lock, LCK_MTX_ASSERT_OWNED);
16720 
16721 	// Insert new module at the front of the list,
16722 
16723 	newctl->mod_next = dtrace_modctl_list;
16724 	dtrace_modctl_list = newctl;
16725 
16726 	/*
16727 	 * If a module exists with the same name, then that module
16728 	 * must have been unloaded with enabled probes. We will move
16729 	 * the unloaded module to the new module's stale chain and
16730 	 * then stop traversing the list.
16731 	 */
16732 
16733 	prevp = newctl;
16734 	nextp = newctl->mod_next;
16735 
16736 	while (nextp != NULL) {
16737 		if (nextp->mod_loaded) {
16738 			/* This is a loaded module. Keep traversing. */
16739 			prevp = nextp;
16740 			nextp = nextp->mod_next;
16741 			continue;
16742 		}
16743 		else {
16744 			/* Found an unloaded module */
16745 			if (strncmp (newctl->mod_modname, nextp->mod_modname, KMOD_MAX_NAME)) {
16746 				/* Names don't match. Keep traversing. */
16747 				prevp = nextp;
16748 				nextp = nextp->mod_next;
16749 				continue;
16750 			}
16751 			else {
16752 				/* We found a stale entry, move it. We're done. */
16753 				prevp->mod_next = nextp->mod_next;
16754 				newctl->mod_stale = nextp;
16755 				nextp->mod_next = NULL;
16756 				break;
16757 			}
16758 		}
16759 	}
16760 }
16761 
16762 static modctl_t *
dtrace_modctl_lookup(struct kmod_info * kmod)16763 dtrace_modctl_lookup(struct kmod_info * kmod)
16764 {
16765     LCK_MTX_ASSERT(&mod_lock, LCK_MTX_ASSERT_OWNED);
16766 
16767     struct modctl * ctl;
16768 
16769     for (ctl = dtrace_modctl_list; ctl; ctl=ctl->mod_next) {
16770 	if (ctl->mod_id == kmod->id)
16771 	    return(ctl);
16772     }
16773     return (NULL);
16774 }
16775 
16776 /*
16777  * This routine is called from dtrace_module_unloaded().
16778  * It removes a modctl structure and its stale chain
16779  * from the kext shadow list.
16780  */
16781 static void
dtrace_modctl_remove(struct modctl * ctl)16782 dtrace_modctl_remove(struct modctl * ctl)
16783 {
16784 	ASSERT(ctl != NULL);
16785 	LCK_MTX_ASSERT(&mod_lock, LCK_MTX_ASSERT_OWNED);
16786 	modctl_t *prevp, *nextp, *curp;
16787 
16788 	// Remove stale chain first
16789 	for (curp=ctl->mod_stale; curp != NULL; curp=nextp) {
16790 		nextp = curp->mod_stale;
16791 		/* There should NEVER be user symbols allocated at this point */
16792 		ASSERT(curp->mod_user_symbols == NULL);
16793 		kmem_free(curp, sizeof(modctl_t));
16794 	}
16795 
16796 	prevp = NULL;
16797 	curp = dtrace_modctl_list;
16798 
16799 	while (curp != ctl) {
16800 		prevp = curp;
16801 		curp = curp->mod_next;
16802 	}
16803 
16804 	if (prevp != NULL) {
16805 		prevp->mod_next = ctl->mod_next;
16806 	}
16807 	else {
16808 		dtrace_modctl_list = ctl->mod_next;
16809 	}
16810 
16811 	/* There should NEVER be user symbols allocated at this point */
16812 	ASSERT(ctl->mod_user_symbols == NULL);
16813 
16814 	kmem_free (ctl, sizeof(modctl_t));
16815 }
16816 
16817 /*
16818  * APPLE NOTE: The kext loader will call dtrace_module_loaded
16819  * when the kext is loaded in memory, but before calling the
16820  * kext's start routine.
16821  *
16822  * Return 0 on success
16823  * Return -1 on failure
16824  */
16825 
16826 static int
dtrace_module_loaded(struct kmod_info * kmod,uint32_t flag)16827 dtrace_module_loaded(struct kmod_info *kmod, uint32_t flag)
16828 {
16829 	dtrace_provider_t *prv;
16830 
16831 	/*
16832 	 * If kernel symbols have been disabled, return immediately
16833 	 * DTRACE_KERNEL_SYMBOLS_NEVER is a permanent mode, it is safe to test without holding locks
16834 	 */
16835 	if (dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_NEVER)
16836 		return 0;
16837 
16838 #if CONFIG_SPTM
16839 	/* Opt-out the SPTM/TXM fake kexts from being loaded by DTrace. */
16840 	extern kmod_info_t g_sptm_kmod_info, g_txm_kmod_info;
16841 	if ((kmod == &g_sptm_kmod_info) || (kmod == &g_txm_kmod_info)) {
16842 		return 0;
16843 	}
16844 #endif
16845 
16846 	struct modctl *ctl = NULL;
16847 	if (!kmod || kmod->address == 0 || kmod->size == 0)
16848 		return(-1);
16849 
16850 	lck_mtx_lock(&dtrace_provider_lock);
16851 	lck_mtx_lock(&mod_lock);
16852 
16853 	/*
16854 	 * Have we seen this kext before?
16855 	 */
16856 
16857 	ctl = dtrace_modctl_lookup(kmod);
16858 
16859 	if (ctl != NULL) {
16860 		/* bail... we already have this kext in the modctl list */
16861 		lck_mtx_unlock(&mod_lock);
16862 		lck_mtx_unlock(&dtrace_provider_lock);
16863 		if (dtrace_err_verbose)
16864 			cmn_err(CE_WARN, "dtrace load module already exists '%s %u' is failing against '%s %u'", kmod->name, (uint_t)kmod->id, ctl->mod_modname, ctl->mod_id);
16865 		return(-1);
16866 	}
16867 	else {
16868 		ctl = kmem_alloc(sizeof(struct modctl), KM_SLEEP);
16869 		if (ctl == NULL) {
16870 			if (dtrace_err_verbose)
16871 				cmn_err(CE_WARN, "dtrace module load '%s %u' is failing ", kmod->name, (uint_t)kmod->id);
16872 			lck_mtx_unlock(&mod_lock);
16873 			lck_mtx_unlock(&dtrace_provider_lock);
16874 			return (-1);
16875 		}
16876 		ctl->mod_next = NULL;
16877 		ctl->mod_stale = NULL;
16878 		strlcpy (ctl->mod_modname, kmod->name, sizeof(ctl->mod_modname));
16879 		ctl->mod_loadcnt = kmod->id;
16880 		ctl->mod_nenabled = 0;
16881 		ctl->mod_address  = kmod->address;
16882 		ctl->mod_size = kmod->size;
16883 		ctl->mod_id = kmod->id;
16884 		ctl->mod_loaded = 1;
16885 		ctl->mod_flags = 0;
16886 		ctl->mod_user_symbols = NULL;
16887 		ctl->mod_sdtprobecnt = 0;
16888 		ctl->mod_sdtdesc = NULL;
16889 
16890 		/*
16891 		 * Find the UUID for this module, if it has one
16892 		 */
16893 		kernel_mach_header_t* header = (kernel_mach_header_t *)ctl->mod_address;
16894 		struct load_command* load_cmd = (struct load_command *)&header[1];
16895 		uint32_t i;
16896 		for (i = 0; i < header->ncmds; i++) {
16897 			if (load_cmd->cmd == LC_UUID) {
16898 				struct uuid_command* uuid_cmd = (struct uuid_command *)load_cmd;
16899 				memcpy(ctl->mod_uuid, uuid_cmd->uuid, sizeof(uuid_cmd->uuid));
16900 				ctl->mod_flags |= MODCTL_HAS_UUID;
16901 				break;
16902 			}
16903 			load_cmd = (struct load_command *)((caddr_t)load_cmd + load_cmd->cmdsize);
16904 		}
16905 
16906 		if (ctl->mod_address == g_kernel_kmod_info.address) {
16907 			ctl->mod_flags |= MODCTL_IS_MACH_KERNEL;
16908 			memcpy(dtrace_kerneluuid, ctl->mod_uuid, sizeof(dtrace_kerneluuid));
16909 		}
16910 		/*
16911 		 * Static kexts have a UUID that is not used for symbolication, as all their
16912 		 * symbols are in kernel
16913 		 */
16914 		else if ((flag & KMOD_DTRACE_STATIC_KEXT) == KMOD_DTRACE_STATIC_KEXT) {
16915 			memcpy(ctl->mod_uuid, dtrace_kerneluuid, sizeof(dtrace_kerneluuid));
16916 			ctl->mod_flags |= MODCTL_IS_STATIC_KEXT;
16917 		}
16918 	}
16919 	dtrace_modctl_add(ctl);
16920 
16921 	/*
16922 	 * We must hold the dtrace_lock to safely test non permanent dtrace_fbt_symbol_mode(s)
16923 	 */
16924 	lck_mtx_lock(&dtrace_lock);
16925 
16926 	/*
16927 	 * DTrace must decide if it will instrument modules lazily via
16928 	 * userspace symbols (default mode), or instrument immediately via
16929 	 * kernel symbols (non-default mode)
16930 	 *
16931 	 * When in default/lazy mode, DTrace will only support modules
16932 	 * built with a valid UUID.
16933 	 *
16934 	 * Overriding the default can be done explicitly in one of
16935 	 * the following two ways.
16936 	 *
16937 	 * A module can force symbols from kernel space using the plist key,
16938 	 * OSBundleForceDTraceInit (see kmod.h).  If this per kext state is set,
16939 	 * we fall through and instrument this module now.
16940 	 *
16941 	 * Or, the boot-arg, dtrace_kernel_symbol_mode, can be set to force symbols
16942 	 * from kernel space (see dtrace_impl.h).  If this system state is set
16943 	 * to a non-userspace mode, we fall through and instrument the module now.
16944 	 */
16945 
16946 	if ((dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE) &&
16947 	    (!(flag & KMOD_DTRACE_FORCE_INIT)))
16948 	{
16949 		/* Load SDT section for module. Symbol related data will be handled lazily. */
16950 		sdt_load_machsect(ctl);
16951 
16952 		/* We will instrument the module lazily -- this is the default */
16953 		lck_mtx_unlock(&dtrace_lock);
16954 		lck_mtx_unlock(&mod_lock);
16955 		lck_mtx_unlock(&dtrace_provider_lock);
16956 		return 0;
16957 	}
16958 
16959 	/* We will instrument the module immediately using kernel symbols */
16960 	if (!(flag & KMOD_DTRACE_NO_KERNEL_SYMS)) {
16961 		ctl->mod_flags |= MODCTL_HAS_KERNEL_SYMBOLS;
16962 	}
16963 
16964 	/* Load SDT section for module. Symbol related data will be handled lazily. */
16965 	sdt_load_machsect(ctl);
16966 
16967 	lck_mtx_unlock(&dtrace_lock);
16968 
16969 	/*
16970 	 * We're going to call each providers per-module provide operation
16971 	 * specifying only this module.
16972 	 */
16973 	for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next)
16974 		prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
16975 
16976 	/*
16977 	 * APPLE NOTE: The contract with the kext loader is that once this function
16978 	 * has completed, it may delete kernel symbols at will.
16979 	 * We must set this while still holding the mod_lock.
16980 	 */
16981 	ctl->mod_flags &= ~MODCTL_HAS_KERNEL_SYMBOLS;
16982 
16983 	lck_mtx_unlock(&mod_lock);
16984 	lck_mtx_unlock(&dtrace_provider_lock);
16985 
16986 	/*
16987 	 * If we have any retained enablings, we need to match against them.
16988 	 * Enabling probes requires that cpu_lock be held, and we cannot hold
16989 	 * cpu_lock here -- it is legal for cpu_lock to be held when loading a
16990 	 * module.  (In particular, this happens when loading scheduling
16991 	 * classes.)  So if we have any retained enablings, we need to dispatch
16992 	 * our task queue to do the match for us.
16993 	 */
16994 	lck_mtx_lock(&dtrace_lock);
16995 
16996 	if (dtrace_retained == NULL) {
16997 		lck_mtx_unlock(&dtrace_lock);
16998 		return 0;
16999 	}
17000 
17001 	/* APPLE NOTE!
17002 	 *
17003 	 * The cpu_lock mentioned above is only held by dtrace code, Apple's xnu never actually
17004 	 * holds it for any reason. Thus the comment above is invalid, we can directly invoke
17005 	 * dtrace_enabling_matchall without jumping through all the hoops, and we can avoid
17006 	 * the delay call as well.
17007 	 */
17008 	lck_mtx_unlock(&dtrace_lock);
17009 
17010 	dtrace_enabling_matchall();
17011 
17012 	return 0;
17013 }
17014 
17015 /*
17016  * Return 0 on success
17017  * Return -1 on failure
17018  */
17019 static int
dtrace_module_unloaded(struct kmod_info * kmod)17020 dtrace_module_unloaded(struct kmod_info *kmod)
17021 {
17022 	dtrace_probe_t template, *probe, *first, *next;
17023 	dtrace_provider_t *prov;
17024         struct modctl *ctl = NULL;
17025 	struct modctl *syncctl = NULL;
17026 	struct modctl *nextsyncctl = NULL;
17027 	int syncmode = 0;
17028 
17029         lck_mtx_lock(&dtrace_provider_lock);
17030 	lck_mtx_lock(&mod_lock);
17031 	lck_mtx_lock(&dtrace_lock);
17032 
17033 	if (kmod == NULL) {
17034 	    syncmode = 1;
17035 	}
17036 	else {
17037 	    ctl = dtrace_modctl_lookup(kmod);
17038 	    if (ctl == NULL)
17039 	    {
17040 		lck_mtx_unlock(&dtrace_lock);
17041 		lck_mtx_unlock(&mod_lock);
17042 		lck_mtx_unlock(&dtrace_provider_lock);
17043 		return (-1);
17044 	    }
17045 	    ctl->mod_loaded = 0;
17046 	    ctl->mod_address = 0;
17047 	    ctl->mod_size = 0;
17048 	}
17049 
17050 	if (dtrace_bymod == NULL) {
17051 		/*
17052 		 * The DTrace module is loaded (obviously) but not attached;
17053 		 * we don't have any work to do.
17054 		 */
17055 	         if (ctl != NULL)
17056 			 (void)dtrace_modctl_remove(ctl);
17057 		 lck_mtx_unlock(&dtrace_lock);
17058 		 lck_mtx_unlock(&mod_lock);
17059 		 lck_mtx_unlock(&dtrace_provider_lock);
17060 		 return(0);
17061 	}
17062 
17063 	/* Syncmode set means we target and traverse entire modctl list. */
17064         if (syncmode)
17065 	    nextsyncctl = dtrace_modctl_list;
17066 
17067 syncloop:
17068 	if (syncmode)
17069 	{
17070 	    /* find a stale modctl struct */
17071 	    for (syncctl = nextsyncctl; syncctl != NULL; syncctl=syncctl->mod_next) {
17072 		if (syncctl->mod_address == 0)
17073 		    break;
17074 	    }
17075 	    if (syncctl==NULL)
17076 	    {
17077 		/* We have no more work to do */
17078 		lck_mtx_unlock(&dtrace_lock);
17079 		lck_mtx_unlock(&mod_lock);
17080 		lck_mtx_unlock(&dtrace_provider_lock);
17081 		return(0);
17082 	    }
17083 	    else {
17084 		/* keep track of next syncctl in case this one is removed */
17085 		nextsyncctl = syncctl->mod_next;
17086 		ctl = syncctl;
17087 	    }
17088 	}
17089 
17090 	template.dtpr_mod = ctl->mod_modname;
17091 
17092 	for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template);
17093 	    probe != NULL; probe = probe->dtpr_nextmod) {
17094 	        if (probe->dtpr_ecb != NULL) {
17095 			/*
17096 			 * This shouldn't _actually_ be possible -- we're
17097 			 * unloading a module that has an enabled probe in it.
17098 			 * (It's normally up to the provider to make sure that
17099 			 * this can't happen.)  However, because dtps_enable()
17100 			 * doesn't have a failure mode, there can be an
17101 			 * enable/unload race.  Upshot:  we don't want to
17102 			 * assert, but we're not going to disable the
17103 			 * probe, either.
17104 			 */
17105 
17106 
17107 		        if (syncmode) {
17108 			    /* We're syncing, let's look at next in list */
17109 			    goto syncloop;
17110 			}
17111 
17112 			lck_mtx_unlock(&dtrace_lock);
17113 			lck_mtx_unlock(&mod_lock);
17114 			lck_mtx_unlock(&dtrace_provider_lock);
17115 
17116 			if (dtrace_err_verbose) {
17117 				cmn_err(CE_WARN, "unloaded module '%s' had "
17118 				    "enabled probes", ctl->mod_modname);
17119 			}
17120 			return(-1);
17121 		}
17122 	}
17123 
17124 	probe = first;
17125 
17126 	for (first = NULL; probe != NULL; probe = next) {
17127 		ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe);
17128 
17129 		dtrace_probes[probe->dtpr_id - 1] = NULL;
17130 		probe->dtpr_provider->dtpv_probe_count--;
17131 
17132 		next = probe->dtpr_nextmod;
17133 		dtrace_hash_remove(dtrace_byprov, probe);
17134 		dtrace_hash_remove(dtrace_bymod, probe);
17135 		dtrace_hash_remove(dtrace_byfunc, probe);
17136 		dtrace_hash_remove(dtrace_byname, probe);
17137 
17138 		if (first == NULL) {
17139 			first = probe;
17140 			probe->dtpr_nextmod = NULL;
17141 		} else {
17142 			probe->dtpr_nextmod = first;
17143 			first = probe;
17144 		}
17145 	}
17146 
17147 	/*
17148 	 * We've removed all of the module's probes from the hash chains and
17149 	 * from the probe array.  Now issue a dtrace_sync() to be sure that
17150 	 * everyone has cleared out from any probe array processing.
17151 	 */
17152 	dtrace_sync();
17153 
17154 	for (probe = first; probe != NULL; probe = first) {
17155 		first = probe->dtpr_nextmod;
17156 		prov = probe->dtpr_provider;
17157 		prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id,
17158 		    probe->dtpr_arg);
17159 		dtrace_strunref(probe->dtpr_mod);
17160 		dtrace_strunref(probe->dtpr_func);
17161 		dtrace_strunref(probe->dtpr_name);
17162 		vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1);
17163 
17164 		zfree(dtrace_probe_t_zone, probe);
17165 	}
17166 
17167 	dtrace_modctl_remove(ctl);
17168 
17169 	if (syncmode)
17170 	    goto syncloop;
17171 
17172 	lck_mtx_unlock(&dtrace_lock);
17173 	lck_mtx_unlock(&mod_lock);
17174 	lck_mtx_unlock(&dtrace_provider_lock);
17175 
17176 	return(0);
17177 }
17178 
17179 void
dtrace_suspend(void)17180 dtrace_suspend(void)
17181 {
17182 	dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend));
17183 }
17184 
17185 void
dtrace_resume(void)17186 dtrace_resume(void)
17187 {
17188 	dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume));
17189 }
17190 
17191 static int
dtrace_cpu_setup(cpu_setup_t what,processorid_t cpu)17192 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu)
17193 {
17194 	LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
17195 	lck_mtx_lock(&dtrace_lock);
17196 
17197 	switch (what) {
17198 	case CPU_CONFIG: {
17199 		dtrace_state_t *state;
17200 		dtrace_optval_t *opt, rs, c;
17201 
17202 		/*
17203 		 * For now, we only allocate a new buffer for anonymous state.
17204 		 */
17205 		if ((state = dtrace_anon.dta_state) == NULL)
17206 			break;
17207 
17208 		if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
17209 			break;
17210 
17211 		opt = state->dts_options;
17212 		c = opt[DTRACEOPT_CPU];
17213 
17214 		if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu)
17215 			break;
17216 
17217 		/*
17218 		 * Regardless of what the actual policy is, we're going to
17219 		 * temporarily set our resize policy to be manual.  We're
17220 		 * also going to temporarily set our CPU option to denote
17221 		 * the newly configured CPU.
17222 		 */
17223 		rs = opt[DTRACEOPT_BUFRESIZE];
17224 		opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL;
17225 		opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu;
17226 
17227 		(void) dtrace_state_buffers(state);
17228 
17229 		opt[DTRACEOPT_BUFRESIZE] = rs;
17230 		opt[DTRACEOPT_CPU] = c;
17231 
17232 		break;
17233 	}
17234 
17235 	case CPU_UNCONFIG:
17236 		/*
17237 		 * We don't free the buffer in the CPU_UNCONFIG case.  (The
17238 		 * buffer will be freed when the consumer exits.)
17239 		 */
17240 		break;
17241 
17242 	default:
17243 		break;
17244 	}
17245 
17246 	lck_mtx_unlock(&dtrace_lock);
17247 	return (0);
17248 }
17249 
17250 static void
dtrace_cpu_setup_initial(processorid_t cpu)17251 dtrace_cpu_setup_initial(processorid_t cpu)
17252 {
17253 	(void) dtrace_cpu_setup(CPU_CONFIG, cpu);
17254 }
17255 
17256 static void
dtrace_toxrange_add(uintptr_t base,uintptr_t limit)17257 dtrace_toxrange_add(uintptr_t base, uintptr_t limit)
17258 {
17259 	if (dtrace_toxranges >= dtrace_toxranges_max) {
17260 		int osize, nsize;
17261 		dtrace_toxrange_t *range;
17262 
17263 		osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
17264 
17265 		if (osize == 0) {
17266 			ASSERT(dtrace_toxrange == NULL);
17267 			ASSERT(dtrace_toxranges_max == 0);
17268 			dtrace_toxranges_max = 1;
17269 		} else {
17270 			dtrace_toxranges_max <<= 1;
17271 		}
17272 
17273 		nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
17274 		range = kmem_zalloc(nsize, KM_SLEEP);
17275 
17276 		if (dtrace_toxrange != NULL) {
17277 			ASSERT(osize != 0);
17278 			bcopy(dtrace_toxrange, range, osize);
17279 			kmem_free(dtrace_toxrange, osize);
17280 		}
17281 
17282 		dtrace_toxrange = range;
17283 	}
17284 
17285 	ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0);
17286 	ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0);
17287 
17288 	dtrace_toxrange[dtrace_toxranges].dtt_base = base;
17289 	dtrace_toxrange[dtrace_toxranges].dtt_limit = limit;
17290 	dtrace_toxranges++;
17291 }
17292 
17293 /*
17294  * DTrace Driver Cookbook Functions
17295  */
17296 /*ARGSUSED*/
17297 static int
dtrace_attach(dev_info_t * devi)17298 dtrace_attach(dev_info_t *devi)
17299 {
17300 	dtrace_provider_id_t id;
17301 	dtrace_state_t *state = NULL;
17302 	dtrace_enabling_t *enab;
17303 
17304 	lck_mtx_lock(&cpu_lock);
17305 	lck_mtx_lock(&dtrace_provider_lock);
17306 	lck_mtx_lock(&dtrace_lock);
17307 
17308 	/* Darwin uses BSD cloning device driver to automagically obtain minor device number. */
17309 	dtrace_devi = devi;
17310 
17311 	dtrace_modload = dtrace_module_loaded;
17312 	dtrace_modunload = dtrace_module_unloaded;
17313 	dtrace_cpu_init = dtrace_cpu_setup_initial;
17314 	dtrace_helpers_cleanup = dtrace_helpers_destroy;
17315 	dtrace_helpers_fork = dtrace_helpers_duplicate;
17316 	dtrace_cpustart_init = dtrace_suspend;
17317 	dtrace_cpustart_fini = dtrace_resume;
17318 	dtrace_debugger_init = dtrace_suspend;
17319 	dtrace_debugger_fini = dtrace_resume;
17320 
17321 	register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
17322 
17323 	LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
17324 
17325 	dtrace_arena = vmem_create("dtrace", (void *)1, INT32_MAX, 1,
17326 	    NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
17327 
17328 	LCK_MTX_ASSERT(&cpu_lock, LCK_MTX_ASSERT_OWNED);
17329 
17330 	dtrace_nprobes = dtrace_nprobes_default;
17331 	dtrace_probes = kmem_zalloc(sizeof(dtrace_probe_t*) * dtrace_nprobes,
17332 	    KM_SLEEP);
17333 
17334 	dtrace_byprov = dtrace_hash_create(dtrace_strkey_probe_provider,
17335 	    0, /* unused */
17336 	    offsetof(dtrace_probe_t, dtpr_nextprov),
17337 	    offsetof(dtrace_probe_t, dtpr_prevprov));
17338 
17339 	dtrace_bymod = dtrace_hash_create(dtrace_strkey_deref_offset,
17340 	    offsetof(dtrace_probe_t, dtpr_mod),
17341 	    offsetof(dtrace_probe_t, dtpr_nextmod),
17342 	    offsetof(dtrace_probe_t, dtpr_prevmod));
17343 
17344 	dtrace_byfunc = dtrace_hash_create(dtrace_strkey_deref_offset,
17345 	    offsetof(dtrace_probe_t, dtpr_func),
17346 	    offsetof(dtrace_probe_t, dtpr_nextfunc),
17347 	    offsetof(dtrace_probe_t, dtpr_prevfunc));
17348 
17349 	dtrace_byname = dtrace_hash_create(dtrace_strkey_deref_offset,
17350 	    offsetof(dtrace_probe_t, dtpr_name),
17351 	    offsetof(dtrace_probe_t, dtpr_nextname),
17352 	    offsetof(dtrace_probe_t, dtpr_prevname));
17353 
17354 	if (dtrace_retain_max < 1) {
17355 		cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; "
17356 		    "setting to 1", dtrace_retain_max);
17357 		dtrace_retain_max = 1;
17358 	}
17359 
17360 	/*
17361 	 * Now discover our toxic ranges.
17362 	 */
17363 	dtrace_toxic_ranges(dtrace_toxrange_add);
17364 
17365 	/*
17366 	 * Before we register ourselves as a provider to our own framework,
17367 	 * we would like to assert that dtrace_provider is NULL -- but that's
17368 	 * not true if we were loaded as a dependency of a DTrace provider.
17369 	 * Once we've registered, we can assert that dtrace_provider is our
17370 	 * pseudo provider.
17371 	 */
17372 	(void) dtrace_register("dtrace", &dtrace_provider_attr,
17373 	    DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
17374 
17375 	ASSERT(dtrace_provider != NULL);
17376 	ASSERT((dtrace_provider_id_t)dtrace_provider == id);
17377 
17378 #if defined (__x86_64__)
17379 	dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
17380 	    dtrace_provider, NULL, NULL, "BEGIN", 1, NULL);
17381 	dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
17382 	    dtrace_provider, NULL, NULL, "END", 0, NULL);
17383 	dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
17384 	    dtrace_provider, NULL, NULL, "ERROR", 3, NULL);
17385 #elif defined(__arm64__)
17386 	dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
17387 	    dtrace_provider, NULL, NULL, "BEGIN", 2, NULL);
17388 	dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
17389 	    dtrace_provider, NULL, NULL, "END", 1, NULL);
17390 	dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
17391 	    dtrace_provider, NULL, NULL, "ERROR", 4, NULL);
17392 #else
17393 #error Unknown Architecture
17394 #endif
17395 
17396 	dtrace_anon_property();
17397 	lck_mtx_unlock(&cpu_lock);
17398 
17399 	/*
17400 	 * If DTrace helper tracing is enabled, we need to allocate the
17401 	 * trace buffer and initialize the values.
17402 	 */
17403 	if (dtrace_helptrace_enabled) {
17404 		ASSERT(dtrace_helptrace_buffer == NULL);
17405 		dtrace_helptrace_buffer =
17406 		    kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP);
17407 		dtrace_helptrace_next = 0;
17408 	}
17409 
17410 	/*
17411 	 * If there are already providers, we must ask them to provide their
17412 	 * probes, and then match any anonymous enabling against them.  Note
17413 	 * that there should be no other retained enablings at this time:
17414 	 * the only retained enablings at this time should be the anonymous
17415 	 * enabling.
17416 	 */
17417 	if (dtrace_anon.dta_enabling != NULL) {
17418 		ASSERT(dtrace_retained == dtrace_anon.dta_enabling);
17419 
17420 		/*
17421 		 * APPLE NOTE: if handling anonymous dof, switch symbol modes.
17422 		 */
17423 		if (dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE) {
17424 			dtrace_kernel_symbol_mode = DTRACE_KERNEL_SYMBOLS_FROM_KERNEL;
17425 		}
17426 
17427 		dtrace_enabling_provide(NULL);
17428 		state = dtrace_anon.dta_state;
17429 
17430 		/*
17431 		 * We couldn't hold cpu_lock across the above call to
17432 		 * dtrace_enabling_provide(), but we must hold it to actually
17433 		 * enable the probes.  We have to drop all of our locks, pick
17434 		 * up cpu_lock, and regain our locks before matching the
17435 		 * retained anonymous enabling.
17436 		 */
17437 		lck_mtx_unlock(&dtrace_lock);
17438 		lck_mtx_unlock(&dtrace_provider_lock);
17439 
17440 		lck_mtx_lock(&cpu_lock);
17441 		lck_mtx_lock(&dtrace_provider_lock);
17442 		lck_mtx_lock(&dtrace_lock);
17443 
17444 		if ((enab = dtrace_anon.dta_enabling) != NULL)
17445 			(void) dtrace_enabling_match(enab, NULL, NULL);
17446 
17447 		lck_mtx_unlock(&cpu_lock);
17448 	}
17449 
17450 	lck_mtx_unlock(&dtrace_lock);
17451 	lck_mtx_unlock(&dtrace_provider_lock);
17452 
17453 	if (state != NULL) {
17454 		/*
17455 		 * If we created any anonymous state, set it going now.
17456 		 */
17457 		(void) dtrace_state_go(state, &dtrace_anon.dta_beganon);
17458 	}
17459 
17460 	return (DDI_SUCCESS);
17461 }
17462 
17463 /*ARGSUSED*/
17464 static int
dtrace_open(dev_t * devp,int flag,int otyp,cred_t * cred_p)17465 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
17466 {
17467 #pragma unused(flag, otyp)
17468 	dtrace_state_t *state;
17469 	uint32_t priv;
17470 	uid_t uid;
17471 	zoneid_t zoneid;
17472 	int rv;
17473 
17474 	if (minor(*devp) < 0 || minor(*devp) >= DTRACE_NCLIENTS)
17475 		return (ENXIO);
17476 
17477 	/* APPLE: Darwin puts Helper on its own major device. */
17478 
17479 	/*
17480 	 * If no DTRACE_PRIV_* bits are set in the credential, then the
17481 	 * caller lacks sufficient permission to do anything with DTrace.
17482 	 */
17483 	dtrace_cred2priv(cred_p, &priv, &uid, &zoneid);
17484 	if (priv == DTRACE_PRIV_NONE)
17485 		return (EACCES);
17486 
17487 	/*
17488 	 * APPLE NOTE: We delay the initialization of fasttrap as late as possible.
17489 	 * It certainly can't be later than now!
17490 	 */
17491 	fasttrap_init();
17492 
17493 	/*
17494 	 * Ask all providers to provide all their probes.
17495 	 */
17496 	lck_mtx_lock(&dtrace_provider_lock);
17497 	dtrace_probe_provide(NULL, NULL);
17498 	lck_mtx_unlock(&dtrace_provider_lock);
17499 
17500 	lck_mtx_lock(&cpu_lock);
17501 	lck_mtx_lock(&dtrace_lock);
17502 	dtrace_opens++;
17503 	dtrace_membar_producer();
17504 
17505 #ifdef illumos
17506 	/*
17507 	 * If the kernel debugger is active (that is, if the kernel debugger
17508 	 * modified text in some way), we won't allow the open.
17509 	 */
17510 	if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
17511 		dtrace_opens--;
17512 		lck_mtx_unlock(&dtrace_lock);
17513 		lck_mtx_unlock(&cpu_lock);
17514 		return (EBUSY);
17515 	}
17516 #endif
17517 
17518 	rv = dtrace_state_create(devp, cred_p, &state);
17519 	lck_mtx_unlock(&cpu_lock);
17520 
17521 	if (rv != 0 || state == NULL) {
17522 		if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) {
17523 #ifdef illumos
17524 			(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
17525 #endif
17526 		}
17527 		lck_mtx_unlock(&dtrace_lock);
17528 		/* propagate EAGAIN or ERESTART */
17529 		return (rv);
17530 	}
17531 
17532 	lck_mtx_unlock(&dtrace_lock);
17533 
17534 	lck_rw_lock_exclusive(&dtrace_dof_mode_lock);
17535 
17536 	/*
17537 	 * If we are currently lazy, transition states.
17538 	 *
17539 	 * Unlike dtrace_close, we do not need to check the
17540 	 * value of dtrace_opens, as any positive value (and
17541 	 * we count as 1) means we transition states.
17542 	 */
17543 	if (dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_ON) {
17544 		dtrace_dof_mode = DTRACE_DOF_MODE_LAZY_OFF;
17545 		/*
17546 		 * We do not need to hold the exclusive lock while processing
17547 		 * DOF on processes. We do need to make sure the mode does not get
17548 		 * changed to DTRACE_DOF_MODE_LAZY_ON during that stage though
17549 		 * (which should not happen anyway since it only happens in
17550 		 * dtrace_close). There is no way imcomplete USDT probes can be
17551 		 * activate by any DTrace clients here since they all have to
17552 		 * call dtrace_open and be blocked on dtrace_dof_mode_lock
17553 		 */
17554 		lck_rw_lock_exclusive_to_shared(&dtrace_dof_mode_lock);
17555 		/*
17556 		 * Iterate all existing processes and load lazy dofs.
17557 		 */
17558 		proc_iterate(PROC_ALLPROCLIST | PROC_NOWAITTRANS,
17559 			     dtrace_lazy_dofs_proc_iterate_doit,
17560 			     NULL,
17561 			     dtrace_lazy_dofs_proc_iterate_filter,
17562 			     NULL);
17563 
17564 		lck_rw_unlock_shared(&dtrace_dof_mode_lock);
17565 	}
17566 	else {
17567 		lck_rw_unlock_exclusive(&dtrace_dof_mode_lock);
17568 	}
17569 
17570 
17571 	/*
17572 	 * Update kernel symbol state.
17573 	 *
17574 	 * We must own the provider and dtrace locks.
17575 	 *
17576 	 * NOTE! It may appear there is a race by setting this value so late
17577 	 * after dtrace_probe_provide. However, any kext loaded after the
17578 	 * call to probe provide and before we set LAZY_OFF will be marked as
17579 	 * eligible for symbols from userspace. The same dtrace that is currently
17580 	 * calling dtrace_open() (this call!) will get a list of kexts needing
17581 	 * symbols and fill them in, thus closing the race window.
17582 	 *
17583 	 * We want to set this value only after it certain it will succeed, as
17584 	 * this significantly reduces the complexity of error exits.
17585 	 */
17586 	lck_mtx_lock(&dtrace_lock);
17587 	if (dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE) {
17588 		dtrace_kernel_symbol_mode = DTRACE_KERNEL_SYMBOLS_FROM_KERNEL;
17589 	}
17590 	lck_mtx_unlock(&dtrace_lock);
17591 
17592 	/* Suspend cluster powerdown while DTrace device is opened. */
17593 	suspend_cluster_powerdown();
17594 	return (0);
17595 }
17596 
17597 /*ARGSUSED*/
17598 static int
dtrace_close(dev_t dev,int flag,int otyp,cred_t * cred_p)17599 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
17600 {
17601 #pragma unused(flag, otyp, cred_p) /* __APPLE__ */
17602 	minor_t minor = getminor(dev);
17603 	dtrace_state_t *state;
17604 
17605 	/* APPLE NOTE: Darwin puts Helper on its own major device. */
17606 	state = dtrace_state_get(minor);
17607 
17608 	lck_mtx_lock(&cpu_lock);
17609 	lck_mtx_lock(&dtrace_lock);
17610 
17611 	if (state->dts_anon) {
17612 		/*
17613 		 * There is anonymous state. Destroy that first.
17614 		 */
17615 		ASSERT(dtrace_anon.dta_state == NULL);
17616 		dtrace_state_destroy(state->dts_anon);
17617 	}
17618 
17619 	dtrace_state_destroy(state);
17620 	ASSERT(dtrace_opens > 0);
17621 
17622 	/*
17623 	 * Only relinquish control of the kernel debugger interface when there
17624 	 * are no consumers and no anonymous enablings.
17625 	 */
17626 	if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) {
17627 #ifdef illumos
17628 		(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
17629 #endif
17630 	}
17631 
17632 	lck_mtx_unlock(&dtrace_lock);
17633 	lck_mtx_unlock(&cpu_lock);
17634 
17635 	/*
17636 	 * Lock ordering requires the dof mode lock be taken before
17637 	 * the dtrace_lock.
17638 	 */
17639 	lck_rw_lock_exclusive(&dtrace_dof_mode_lock);
17640 	lck_mtx_lock(&dtrace_lock);
17641 
17642 	if (dtrace_opens == 0) {
17643 		/*
17644 		 * If we are currently lazy-off, and this is the last close, transition to
17645 		 * lazy state.
17646 		 */
17647 		if (dtrace_dof_mode == DTRACE_DOF_MODE_LAZY_OFF) {
17648 			dtrace_dof_mode = DTRACE_DOF_MODE_LAZY_ON;
17649 		}
17650 
17651 		/*
17652 		 * If we are the last dtrace client, switch back to lazy (from userspace) symbols
17653 		 */
17654 		if (dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_FROM_KERNEL) {
17655 			dtrace_kernel_symbol_mode = DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE;
17656 		}
17657 	}
17658 
17659 	lck_mtx_unlock(&dtrace_lock);
17660 	lck_rw_unlock_exclusive(&dtrace_dof_mode_lock);
17661 
17662 	/*
17663 	 * Kext probes may be retained past the end of the kext's lifespan. The
17664 	 * probes are kept until the last reference to them has been removed.
17665 	 * Since closing an active dtrace context is likely to drop that last reference,
17666 	 * lets take a shot at cleaning out the orphaned probes now.
17667 	 */
17668 	dtrace_module_unloaded(NULL);
17669 
17670 	/* State is gone so resume cluster powerdown. */
17671 	resume_cluster_powerdown();
17672 	return (0);
17673 }
17674 
17675 /*ARGSUSED*/
17676 static int
dtrace_ioctl_helper(u_long cmd,caddr_t arg,int * rv)17677 dtrace_ioctl_helper(u_long cmd, caddr_t arg, int *rv)
17678 {
17679 #pragma unused(rv)
17680 	/*
17681 	 * Safe to check this outside the dof mode lock
17682 	 */
17683 	if (dtrace_dof_mode == DTRACE_DOF_MODE_NEVER)
17684 		return KERN_SUCCESS;
17685 
17686 	switch (cmd) {
17687 #if defined (__arm64__)
17688 	case DTRACEHIOC_ADDDOF_U32:
17689 	case DTRACEHIOC_ADDDOF_U64:
17690 #else
17691 	case DTRACEHIOC_ADDDOF:
17692 #endif /* __arm64__*/
17693 	                {
17694 			dof_helper_t *dhp = NULL;
17695 			size_t dof_ioctl_data_size;
17696 			dof_ioctl_data_t* multi_dof;
17697 			unsigned int i;
17698 			int rval = 0;
17699 			user_addr_t user_address = *(user_addr_t*)arg;
17700 			uint64_t dof_count;
17701 			int multi_dof_claimed = 0;
17702 			proc_t* p = current_proc();
17703 
17704 			/*
17705 			 * If this is a restricted process and dtrace is restricted,
17706 			 * do not allow DOFs to be registered
17707 			 */
17708 			if (dtrace_is_restricted() &&
17709 				!dtrace_are_restrictions_relaxed() &&
17710 				!dtrace_can_attach_to_proc(current_proc())) {
17711 				return (EACCES);
17712 			}
17713 
17714 			/*
17715 			 * Read the number of DOF sections being passed in.
17716 			 */
17717 			if (copyin(user_address + offsetof(dof_ioctl_data_t, dofiod_count),
17718 				   &dof_count,
17719 				   sizeof(dof_count))) {
17720 				dtrace_dof_error(NULL, "failed to copyin dofiod_count");
17721 				return (EFAULT);
17722 			}
17723 
17724 			/*
17725 			 * Range check the count.
17726 			 */
17727 			if (dof_count == 0 || dof_count > 1024) {
17728 				dtrace_dof_error(NULL, "dofiod_count is not valid");
17729 				return (EINVAL);
17730 			}
17731 
17732 			/*
17733 			 * Allocate a correctly sized structure and copyin the data.
17734 			 */
17735 			dof_ioctl_data_size = DOF_IOCTL_DATA_T_SIZE(dof_count);
17736 			if ((multi_dof = kmem_alloc(dof_ioctl_data_size, KM_SLEEP)) == NULL)
17737 				return (ENOMEM);
17738 
17739 			/* NOTE! We can no longer exit this method via return */
17740 			if (copyin(user_address, multi_dof, dof_ioctl_data_size) != 0) {
17741 				dtrace_dof_error(NULL, "failed copyin of dof_ioctl_data_t");
17742 				rval = EFAULT;
17743 				goto cleanup;
17744 			}
17745 
17746 			/*
17747 			 * Check that the count didn't change between the first copyin and the second.
17748 			 */
17749 			if (multi_dof->dofiod_count != dof_count) {
17750 				rval = EINVAL;
17751 				goto cleanup;
17752 			}
17753 
17754 			/*
17755 			 * Try to process lazily first.
17756 			 */
17757 			rval = dtrace_lazy_dofs_add(p, multi_dof, &multi_dof_claimed);
17758 
17759 			/*
17760 			 * If rval is EACCES, we must be non-lazy.
17761 			 */
17762 			if (rval == EACCES) {
17763 				rval = 0;
17764 				/*
17765 				 * Process each dof_helper_t
17766 				 */
17767 				i = 0;
17768 				do {
17769 					dhp = &multi_dof->dofiod_helpers[i];
17770 
17771 					dof_hdr_t *dof = dtrace_dof_copyin(dhp->dofhp_dof, &rval);
17772 
17773 					if (dof != NULL) {
17774 						lck_mtx_lock(&dtrace_meta_lock);
17775 						lck_mtx_lock(&dtrace_lock);
17776 
17777 						/*
17778 						 * dtrace_helper_slurp() takes responsibility for the dof --
17779 						 * it may free it now or it may save it and free it later.
17780 						 */
17781 						if ((dhp->dofhp_dof = (uint64_t)dtrace_helper_slurp(p, dof, dhp)) == -1ULL) {
17782 							rval = EINVAL;
17783 						}
17784 
17785 						lck_mtx_unlock(&dtrace_lock);
17786 						lck_mtx_unlock(&dtrace_meta_lock);
17787 					}
17788 				} while (++i < multi_dof->dofiod_count && rval == 0);
17789 			}
17790 
17791 			/*
17792 			 * We need to copyout the multi_dof struct, because it contains
17793 			 * the generation (unique id) values needed to call DTRACEHIOC_REMOVE
17794 			 *
17795 			 * This could certainly be better optimized.
17796 			 */
17797 			if (copyout(multi_dof, user_address, dof_ioctl_data_size) != 0) {
17798 				dtrace_dof_error(NULL, "failed copyout of dof_ioctl_data_t");
17799 				/* Don't overwrite pre-existing error code */
17800 				if (rval == 0) rval = EFAULT;
17801 			}
17802 
17803 		cleanup:
17804 			/*
17805 			 * If we had to allocate struct memory, free it.
17806 			 */
17807 			if (multi_dof != NULL && !multi_dof_claimed) {
17808 				kmem_free(multi_dof, dof_ioctl_data_size);
17809 			}
17810 
17811 			return rval;
17812 		}
17813 
17814 		case DTRACEHIOC_REMOVE: {
17815 			int generation = *(int*)arg;
17816 			proc_t* p = current_proc();
17817 
17818 			/*
17819 			 * Try lazy first.
17820 			 */
17821 			int rval = dtrace_lazy_dofs_remove(p, generation);
17822 
17823 			/*
17824 			 * EACCES means non-lazy
17825 			 */
17826 			if (rval == EACCES) {
17827 				lck_mtx_lock(&dtrace_meta_lock);
17828 				lck_mtx_lock(&dtrace_lock);
17829 				rval = dtrace_helper_destroygen(p, generation);
17830 				lck_mtx_unlock(&dtrace_lock);
17831 				lck_mtx_unlock(&dtrace_meta_lock);
17832 			}
17833 
17834 			return (rval);
17835 		}
17836 
17837 		default:
17838 			break;
17839 	}
17840 
17841 	return ENOTTY;
17842 }
17843 
17844 /*ARGSUSED*/
17845 static int
dtrace_ioctl(dev_t dev,u_long cmd,user_addr_t arg,int md,cred_t * cr,int * rv)17846 dtrace_ioctl(dev_t dev, u_long cmd, user_addr_t arg, int md, cred_t *cr, int *rv)
17847 {
17848 #pragma unused(md)
17849 	minor_t minor = getminor(dev);
17850 	dtrace_state_t *state;
17851 	int rval;
17852 
17853 	/* Darwin puts Helper on its own major device. */
17854 
17855 	state = dtrace_state_get(minor);
17856 
17857 	if (state->dts_anon) {
17858 	   ASSERT(dtrace_anon.dta_state == NULL);
17859 	   state = state->dts_anon;
17860 	}
17861 
17862 	switch (cmd) {
17863 	case DTRACEIOC_PROVIDER: {
17864 		dtrace_providerdesc_t pvd;
17865 		dtrace_provider_t *pvp;
17866 
17867 		if (copyin(arg, &pvd, sizeof (pvd)) != 0)
17868 			return (EFAULT);
17869 
17870 		pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
17871 		lck_mtx_lock(&dtrace_provider_lock);
17872 
17873 		for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
17874 			if (strncmp(pvp->dtpv_name, pvd.dtvd_name, DTRACE_PROVNAMELEN) == 0)
17875 				break;
17876 		}
17877 
17878 		lck_mtx_unlock(&dtrace_provider_lock);
17879 
17880 		if (pvp == NULL)
17881 			return (ESRCH);
17882 
17883 		bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t));
17884 		bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t));
17885 		if (copyout(&pvd, arg, sizeof (pvd)) != 0)
17886 			return (EFAULT);
17887 
17888 		return (0);
17889 	}
17890 
17891 	case DTRACEIOC_EPROBE: {
17892 		dtrace_eprobedesc_t epdesc;
17893 		dtrace_ecb_t *ecb;
17894 		dtrace_action_t *act;
17895 		void *buf;
17896 		size_t size;
17897 		uintptr_t dest;
17898 		int nrecs;
17899 
17900 		if (copyin(arg, &epdesc, sizeof (epdesc)) != 0)
17901 			return (EFAULT);
17902 
17903 		lck_mtx_lock(&dtrace_lock);
17904 
17905 		if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
17906 			lck_mtx_unlock(&dtrace_lock);
17907 			return (EINVAL);
17908 		}
17909 
17910 		if (ecb->dte_probe == NULL) {
17911 			lck_mtx_unlock(&dtrace_lock);
17912 			return (EINVAL);
17913 		}
17914 
17915 		epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
17916 		epdesc.dtepd_uarg = ecb->dte_uarg;
17917 		epdesc.dtepd_size = ecb->dte_size;
17918 
17919 		nrecs = epdesc.dtepd_nrecs;
17920 		epdesc.dtepd_nrecs = 0;
17921 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
17922 			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
17923 				continue;
17924 
17925 			epdesc.dtepd_nrecs++;
17926 		}
17927 
17928 		/*
17929 		 * Now that we have the size, we need to allocate a temporary
17930 		 * buffer in which to store the complete description.  We need
17931 		 * the temporary buffer to be able to drop dtrace_lock()
17932 		 * across the copyout(), below.
17933 		 */
17934 		size = sizeof (dtrace_eprobedesc_t) +
17935 			(epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
17936 
17937 		buf = kmem_alloc(size, KM_SLEEP);
17938 		dest = (uintptr_t)buf;
17939 
17940 		bcopy(&epdesc, (void *)dest, sizeof (epdesc));
17941 		dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
17942 
17943 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
17944 			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
17945 				continue;
17946 
17947 			if (nrecs-- == 0)
17948 				break;
17949 
17950 			bcopy(&act->dta_rec, (void *)dest,
17951 			sizeof (dtrace_recdesc_t));
17952 			dest += sizeof (dtrace_recdesc_t);
17953 		}
17954 
17955 		lck_mtx_unlock(&dtrace_lock);
17956 
17957 		if (copyout(buf, arg, dest - (uintptr_t)buf) != 0) {
17958 			kmem_free(buf, size);
17959 			return (EFAULT);
17960 		}
17961 
17962 		kmem_free(buf, size);
17963 		return (0);
17964 	}
17965 
17966 	case DTRACEIOC_AGGDESC: {
17967 		dtrace_aggdesc_t aggdesc;
17968 		dtrace_action_t *act;
17969 		dtrace_aggregation_t *agg;
17970 		int nrecs;
17971 		uint32_t offs;
17972 		dtrace_recdesc_t *lrec;
17973 		void *buf;
17974 		size_t size;
17975 		uintptr_t dest;
17976 
17977 		if (copyin(arg, &aggdesc, sizeof (aggdesc)) != 0)
17978 			return (EFAULT);
17979 
17980 		lck_mtx_lock(&dtrace_lock);
17981 
17982 		if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) {
17983 			lck_mtx_unlock(&dtrace_lock);
17984 			return (EINVAL);
17985 		}
17986 
17987 		aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid;
17988 
17989 		nrecs = aggdesc.dtagd_nrecs;
17990 		aggdesc.dtagd_nrecs = 0;
17991 
17992 		offs = agg->dtag_base;
17993 		lrec = &agg->dtag_action.dta_rec;
17994 		aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs;
17995 
17996 		for (act = agg->dtag_first; ; act = act->dta_next) {
17997 			ASSERT(act->dta_intuple ||
17998 			DTRACEACT_ISAGG(act->dta_kind));
17999 
18000 			/*
18001 			 * If this action has a record size of zero, it
18002 			 * denotes an argument to the aggregating action.
18003 			 * Because the presence of this record doesn't (or
18004 			 * shouldn't) affect the way the data is interpreted,
18005 			 * we don't copy it out to save user-level the
18006 			 * confusion of dealing with a zero-length record.
18007 			 */
18008 			if (act->dta_rec.dtrd_size == 0) {
18009 				ASSERT(agg->dtag_hasarg);
18010 				continue;
18011 			}
18012 
18013 			aggdesc.dtagd_nrecs++;
18014 
18015 			if (act == &agg->dtag_action)
18016 				break;
18017 		}
18018 
18019 		/*
18020 		 * Now that we have the size, we need to allocate a temporary
18021 		 * buffer in which to store the complete description.  We need
18022 		 * the temporary buffer to be able to drop dtrace_lock()
18023 		 * across the copyout(), below.
18024 		 */
18025 		size = sizeof (dtrace_aggdesc_t) +
18026 			(aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t));
18027 
18028 		buf = kmem_alloc(size, KM_SLEEP);
18029 		dest = (uintptr_t)buf;
18030 
18031 		bcopy(&aggdesc, (void *)dest, sizeof (aggdesc));
18032 		dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]);
18033 
18034 		for (act = agg->dtag_first; ; act = act->dta_next) {
18035 			dtrace_recdesc_t rec = act->dta_rec;
18036 
18037 			/*
18038 			 * See the comment in the above loop for why we pass
18039 			 * over zero-length records.
18040 			 */
18041 			if (rec.dtrd_size == 0) {
18042 				ASSERT(agg->dtag_hasarg);
18043 				continue;
18044 			}
18045 
18046 			if (nrecs-- == 0)
18047 				break;
18048 
18049 			rec.dtrd_offset -= offs;
18050 			bcopy(&rec, (void *)dest, sizeof (rec));
18051 			dest += sizeof (dtrace_recdesc_t);
18052 
18053 			if (act == &agg->dtag_action)
18054 				break;
18055 		}
18056 
18057 		lck_mtx_unlock(&dtrace_lock);
18058 
18059 		if (copyout(buf, arg, dest - (uintptr_t)buf) != 0) {
18060 			kmem_free(buf, size);
18061 			return (EFAULT);
18062 		}
18063 
18064 		kmem_free(buf, size);
18065 		return (0);
18066 	}
18067 
18068 	case DTRACEIOC_ENABLE: {
18069 		dof_hdr_t *dof;
18070 		dtrace_enabling_t *enab = NULL;
18071 		dtrace_vstate_t *vstate;
18072 		int err = 0;
18073 
18074 		*rv = 0;
18075 
18076 		/*
18077 		 * If a NULL argument has been passed, we take this as our
18078 		 * cue to reevaluate our enablings.
18079 		 */
18080 		if (arg == 0) {
18081 			dtrace_enabling_matchall();
18082 
18083 			return (0);
18084 		}
18085 
18086 		if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL)
18087 			return (rval);
18088 
18089 		lck_mtx_lock(&cpu_lock);
18090 		lck_mtx_lock(&dtrace_lock);
18091 		vstate = &state->dts_vstate;
18092 
18093 		if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
18094 			lck_mtx_unlock(&dtrace_lock);
18095 			lck_mtx_unlock(&cpu_lock);
18096 			dtrace_dof_destroy(dof);
18097 			return (EBUSY);
18098 		}
18099 
18100 		if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) {
18101 			lck_mtx_unlock(&dtrace_lock);
18102 			lck_mtx_unlock(&cpu_lock);
18103 			dtrace_dof_destroy(dof);
18104 			return (EINVAL);
18105 		}
18106 
18107 		if ((rval = dtrace_dof_options(dof, state)) != 0) {
18108 			dtrace_enabling_destroy(enab);
18109 			lck_mtx_unlock(&dtrace_lock);
18110 			lck_mtx_unlock(&cpu_lock);
18111 			dtrace_dof_destroy(dof);
18112 			return (rval);
18113 		}
18114 
18115 		if ((err = dtrace_enabling_match(enab, rv, NULL)) == 0) {
18116 			err = dtrace_enabling_retain(enab);
18117 		} else {
18118 			dtrace_enabling_destroy(enab);
18119 		}
18120 
18121 		lck_mtx_unlock(&dtrace_lock);
18122 		lck_mtx_unlock(&cpu_lock);
18123 		dtrace_dof_destroy(dof);
18124 
18125 		return (err);
18126 	}
18127 
18128 	case DTRACEIOC_REPLICATE: {
18129 		dtrace_repldesc_t desc;
18130 		dtrace_probedesc_t *match = &desc.dtrpd_match;
18131 		dtrace_probedesc_t *create = &desc.dtrpd_create;
18132 		int err;
18133 
18134 		if (copyin(arg, &desc, sizeof (desc)) != 0)
18135 			return (EFAULT);
18136 
18137 		match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
18138 		match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
18139 		match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
18140 		match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
18141 
18142 		create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
18143 		create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
18144 		create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
18145 		create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
18146 
18147 		lck_mtx_lock(&dtrace_lock);
18148 		err = dtrace_enabling_replicate(state, match, create);
18149 		lck_mtx_unlock(&dtrace_lock);
18150 
18151 		return (err);
18152 	}
18153 
18154 	case DTRACEIOC_PROBEMATCH:
18155 	case DTRACEIOC_PROBES: {
18156 		dtrace_probe_t *probe = NULL;
18157 		dtrace_probedesc_t desc;
18158 		dtrace_probekey_t pkey;
18159 		dtrace_id_t i;
18160 		int m = 0;
18161 		uint32_t priv;
18162 		uid_t uid;
18163 		zoneid_t zoneid;
18164 
18165 		if (copyin(arg, &desc, sizeof (desc)) != 0)
18166 			return (EFAULT);
18167 
18168 		desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
18169 		desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
18170 		desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
18171 		desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0';
18172 
18173 		/*
18174 		 * Before we attempt to match this probe, we want to give
18175 		 * all providers the opportunity to provide it.
18176 		 */
18177 		if (desc.dtpd_id == DTRACE_IDNONE) {
18178 			lck_mtx_lock(&dtrace_provider_lock);
18179 			dtrace_probe_provide(&desc, NULL);
18180 			lck_mtx_unlock(&dtrace_provider_lock);
18181 			desc.dtpd_id++;
18182 		}
18183 
18184 		dtrace_cred2priv(cr, &priv, &uid, &zoneid);
18185 
18186 		lck_mtx_lock(&dtrace_lock);
18187 
18188 		if (cmd == DTRACEIOC_PROBEMATCH)  {
18189 			dtrace_probekey(&desc, &pkey);
18190 			pkey.dtpk_id = DTRACE_IDNONE;
18191 
18192 			/* Quiet compiler warning */
18193 			for (i = desc.dtpd_id; i <= (dtrace_id_t)dtrace_nprobes; i++) {
18194 				if ((probe = dtrace_probes[i - 1]) != NULL &&
18195 					(m = dtrace_match_probe(probe, &pkey,
18196 					priv, uid, zoneid)) != 0)
18197 					break;
18198 			}
18199 
18200 			if (m < 0) {
18201 				lck_mtx_unlock(&dtrace_lock);
18202 				return (EINVAL);
18203 			}
18204 			dtrace_probekey_release(&pkey);
18205 
18206 		} else {
18207                         /* Quiet compiler warning */
18208 			for (i = desc.dtpd_id; i <= (dtrace_id_t)dtrace_nprobes; i++) {
18209 				if ((probe = dtrace_probes[i - 1]) != NULL &&
18210 					dtrace_match_priv(probe, priv, uid, zoneid))
18211 					break;
18212 			}
18213 		}
18214 
18215 		if (probe == NULL) {
18216 			lck_mtx_unlock(&dtrace_lock);
18217 			return (ESRCH);
18218 		}
18219 
18220 		dtrace_probe_description(probe, &desc);
18221 		lck_mtx_unlock(&dtrace_lock);
18222 
18223 		if (copyout(&desc, arg, sizeof (desc)) != 0)
18224 			return (EFAULT);
18225 
18226 		return (0);
18227 	}
18228 
18229 	case DTRACEIOC_PROBEARG: {
18230 		dtrace_argdesc_t desc;
18231 		dtrace_probe_t *probe;
18232 		dtrace_provider_t *prov;
18233 
18234 		if (copyin(arg, &desc, sizeof (desc)) != 0)
18235 			return (EFAULT);
18236 
18237 		if (desc.dtargd_id == DTRACE_IDNONE)
18238 			return (EINVAL);
18239 
18240 		if (desc.dtargd_ndx == DTRACE_ARGNONE)
18241 			return (EINVAL);
18242 
18243 		lck_mtx_lock(&dtrace_provider_lock);
18244 		lck_mtx_lock(&mod_lock);
18245 		lck_mtx_lock(&dtrace_lock);
18246 
18247                 /* Quiet compiler warning */
18248 		if (desc.dtargd_id > (dtrace_id_t)dtrace_nprobes) {
18249 			lck_mtx_unlock(&dtrace_lock);
18250 			lck_mtx_unlock(&mod_lock);
18251 			lck_mtx_unlock(&dtrace_provider_lock);
18252 			return (EINVAL);
18253 		}
18254 
18255 		if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) {
18256 			lck_mtx_unlock(&dtrace_lock);
18257 			lck_mtx_unlock(&mod_lock);
18258 			lck_mtx_unlock(&dtrace_provider_lock);
18259 			return (EINVAL);
18260 		}
18261 
18262 		lck_mtx_unlock(&dtrace_lock);
18263 
18264 		prov = probe->dtpr_provider;
18265 
18266 		if (prov->dtpv_pops.dtps_getargdesc == NULL) {
18267 		/*
18268 		 * There isn't any typed information for this probe.
18269 		 * Set the argument number to DTRACE_ARGNONE.
18270 		 */
18271 			desc.dtargd_ndx = DTRACE_ARGNONE;
18272 		} else {
18273 			desc.dtargd_native[0] = '\0';
18274 			desc.dtargd_xlate[0] = '\0';
18275 			desc.dtargd_mapping = desc.dtargd_ndx;
18276 
18277 			prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
18278 			probe->dtpr_id, probe->dtpr_arg, &desc);
18279 		}
18280 
18281 		lck_mtx_unlock(&mod_lock);
18282 		lck_mtx_unlock(&dtrace_provider_lock);
18283 
18284 		if (copyout(&desc, arg, sizeof (desc)) != 0)
18285 			return (EFAULT);
18286 
18287 		return (0);
18288 	}
18289 
18290 	case DTRACEIOC_GO: {
18291 		processorid_t cpuid;
18292 		rval = dtrace_state_go(state, &cpuid);
18293 
18294 		if (rval != 0)
18295 			return (rval);
18296 
18297 		if (copyout(&cpuid, arg, sizeof (cpuid)) != 0)
18298 			return (EFAULT);
18299 
18300 		return (0);
18301 	}
18302 
18303 	case DTRACEIOC_STOP: {
18304 		processorid_t cpuid;
18305 
18306 		lck_mtx_lock(&dtrace_lock);
18307 		rval = dtrace_state_stop(state, &cpuid);
18308 		lck_mtx_unlock(&dtrace_lock);
18309 
18310 		if (rval != 0)
18311 			return (rval);
18312 
18313 		if (copyout(&cpuid, arg, sizeof (cpuid)) != 0)
18314 			return (EFAULT);
18315 
18316 		return (0);
18317 	}
18318 
18319 	case DTRACEIOC_DOFGET: {
18320 		dof_hdr_t hdr, *dof;
18321 		uint64_t len;
18322 
18323 		if (copyin(arg, &hdr, sizeof (hdr)) != 0)
18324 			return (EFAULT);
18325 
18326 		lck_mtx_lock(&dtrace_lock);
18327 		dof = dtrace_dof_create(state);
18328 		lck_mtx_unlock(&dtrace_lock);
18329 
18330 		len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
18331 		rval = copyout(dof, arg, len);
18332 		dtrace_dof_destroy(dof);
18333 
18334 		return (rval == 0 ? 0 : EFAULT);
18335 	}
18336 
18337 	case DTRACEIOC_SLEEP: {
18338 		int64_t time;
18339 		uint64_t abstime;
18340 		uint64_t rvalue = DTRACE_WAKE_TIMEOUT;
18341 
18342 		if (copyin(arg, &time, sizeof(time)) != 0)
18343 			return (EFAULT);
18344 
18345 		nanoseconds_to_absolutetime((uint64_t)time, &abstime);
18346 		clock_absolutetime_interval_to_deadline(abstime, &abstime);
18347 
18348 		if (assert_wait_deadline(state, THREAD_ABORTSAFE, abstime) == THREAD_WAITING) {
18349 			if (state->dts_buf_over_limit > 0) {
18350 				clear_wait(current_thread(), THREAD_INTERRUPTED);
18351 				rvalue = DTRACE_WAKE_BUF_LIMIT;
18352 			} else {
18353 				thread_block(THREAD_CONTINUE_NULL);
18354 				if (state->dts_buf_over_limit > 0) {
18355 					rvalue = DTRACE_WAKE_BUF_LIMIT;
18356 				}
18357 			}
18358 		}
18359 
18360 		if (copyout(&rvalue, arg, sizeof(rvalue)) != 0)
18361 			return (EFAULT);
18362 
18363 		return (0);
18364 	}
18365 
18366 	case DTRACEIOC_SIGNAL: {
18367 		wakeup(state);
18368 		return (0);
18369 	}
18370 
18371 	case DTRACEIOC_AGGSNAP:
18372 	case DTRACEIOC_BUFSNAP: {
18373 		dtrace_bufdesc_t desc;
18374 		caddr_t cached;
18375 		boolean_t over_limit;
18376 		dtrace_buffer_t *buf;
18377 
18378 		if (copyin(arg, &desc, sizeof (desc)) != 0)
18379 			return (EFAULT);
18380 
18381 		if ((int)desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU)
18382 			return (EINVAL);
18383 
18384 		lck_mtx_lock(&dtrace_lock);
18385 
18386 		if (cmd == DTRACEIOC_BUFSNAP) {
18387 			buf = &state->dts_buffer[desc.dtbd_cpu];
18388 		} else {
18389 			buf = &state->dts_aggbuffer[desc.dtbd_cpu];
18390 		}
18391 
18392 		if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) {
18393 			size_t sz = buf->dtb_offset;
18394 
18395 			if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) {
18396 				lck_mtx_unlock(&dtrace_lock);
18397 				return (EBUSY);
18398 			}
18399 
18400 			/*
18401 			 * If this buffer has already been consumed, we're
18402 			 * going to indicate that there's nothing left here
18403 			 * to consume.
18404 			 */
18405 			if (buf->dtb_flags & DTRACEBUF_CONSUMED) {
18406 				lck_mtx_unlock(&dtrace_lock);
18407 
18408 				desc.dtbd_size = 0;
18409 				desc.dtbd_drops = 0;
18410 				desc.dtbd_errors = 0;
18411 				desc.dtbd_oldest = 0;
18412 				sz = sizeof (desc);
18413 
18414 				if (copyout(&desc, arg, sz) != 0)
18415 					return (EFAULT);
18416 
18417 				return (0);
18418 			}
18419 
18420 			/*
18421 			 * If this is a ring buffer that has wrapped, we want
18422 			 * to copy the whole thing out.
18423 			 */
18424 			if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
18425 				dtrace_buffer_polish(buf);
18426 				sz = buf->dtb_size;
18427 			}
18428 
18429 			if (copyout(buf->dtb_tomax, (user_addr_t)desc.dtbd_data, sz) != 0) {
18430 				lck_mtx_unlock(&dtrace_lock);
18431 				return (EFAULT);
18432 			}
18433 
18434 			desc.dtbd_size = sz;
18435 			desc.dtbd_drops = buf->dtb_drops;
18436 			desc.dtbd_errors = buf->dtb_errors;
18437 			desc.dtbd_oldest = buf->dtb_xamot_offset;
18438 			desc.dtbd_timestamp = dtrace_gethrtime();
18439 
18440 			lck_mtx_unlock(&dtrace_lock);
18441 
18442 			if (copyout(&desc, arg, sizeof (desc)) != 0)
18443 				return (EFAULT);
18444 
18445 			buf->dtb_flags |= DTRACEBUF_CONSUMED;
18446 
18447 			return (0);
18448 		}
18449 
18450 		if (buf->dtb_tomax == NULL) {
18451 			ASSERT(buf->dtb_xamot == NULL);
18452 			lck_mtx_unlock(&dtrace_lock);
18453 			return (ENOENT);
18454 		}
18455 
18456 		cached = buf->dtb_tomax;
18457 		over_limit = buf->dtb_cur_limit == buf->dtb_size;
18458 
18459 		ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
18460 
18461 		dtrace_xcall(desc.dtbd_cpu,
18462 			(dtrace_xcall_t)dtrace_buffer_switch, buf);
18463 
18464 		state->dts_errors += buf->dtb_xamot_errors;
18465 
18466 		/*
18467 		* If the buffers did not actually switch, then the cross call
18468 		* did not take place -- presumably because the given CPU is
18469 		* not in the ready set.  If this is the case, we'll return
18470 		* ENOENT.
18471 		*/
18472 		if (buf->dtb_tomax == cached) {
18473 			ASSERT(buf->dtb_xamot != cached);
18474 			lck_mtx_unlock(&dtrace_lock);
18475 			return (ENOENT);
18476 		}
18477 
18478 		ASSERT(cached == buf->dtb_xamot);
18479 		/*
18480 		 * At this point we know the buffer have switched, so we
18481 		 * can decrement the over limit count if the buffer was over
18482 		 * its limit. The new buffer might already be over its limit
18483 		 * yet, but we don't care since we're guaranteed not to be
18484 		 * checking the buffer over limit count  at this point.
18485 		 */
18486 		if (over_limit) {
18487 			uint32_t old = os_atomic_dec_orig(&state->dts_buf_over_limit, relaxed);
18488 			#pragma unused(old)
18489 
18490 			/*
18491 			 * Verify that we didn't underflow the value
18492 			 */
18493 			ASSERT(old != 0);
18494 		}
18495 
18496 		/*
18497 		* We have our snapshot; now copy it out.
18498 		*/
18499 		if (dtrace_buffer_copyout(buf->dtb_xamot,
18500 					(user_addr_t)desc.dtbd_data,
18501 					buf->dtb_xamot_offset) != 0) {
18502 			lck_mtx_unlock(&dtrace_lock);
18503 			return (EFAULT);
18504 		}
18505 
18506 		desc.dtbd_size = buf->dtb_xamot_offset;
18507 		desc.dtbd_drops = buf->dtb_xamot_drops;
18508 		desc.dtbd_errors = buf->dtb_xamot_errors;
18509 		desc.dtbd_oldest = 0;
18510 		desc.dtbd_timestamp = buf->dtb_switched;
18511 
18512 		lck_mtx_unlock(&dtrace_lock);
18513 
18514 		/*
18515 		 * Finally, copy out the buffer description.
18516 		 */
18517 		if (copyout(&desc, arg, sizeof (desc)) != 0)
18518 			return (EFAULT);
18519 
18520 		return (0);
18521 	}
18522 
18523 	case DTRACEIOC_CONF: {
18524 		dtrace_conf_t conf;
18525 
18526 		bzero(&conf, sizeof (conf));
18527 		conf.dtc_difversion = DIF_VERSION;
18528 		conf.dtc_difintregs = DIF_DIR_NREGS;
18529 		conf.dtc_diftupregs = DIF_DTR_NREGS;
18530 		conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
18531 
18532 		if (copyout(&conf, arg, sizeof (conf)) != 0)
18533 			return (EFAULT);
18534 
18535 		return (0);
18536 	}
18537 
18538 	case DTRACEIOC_STATUS: {
18539 		dtrace_status_t stat;
18540 		dtrace_dstate_t *dstate;
18541 		int j;
18542 		uint64_t nerrs;
18543 
18544 		/*
18545 		* See the comment in dtrace_state_deadman() for the reason
18546 		* for setting dts_laststatus to INT64_MAX before setting
18547 		* it to the correct value.
18548 		*/
18549 		state->dts_laststatus = INT64_MAX;
18550 		dtrace_membar_producer();
18551 		state->dts_laststatus = dtrace_gethrtime();
18552 
18553 		bzero(&stat, sizeof (stat));
18554 
18555 		lck_mtx_lock(&dtrace_lock);
18556 
18557 		if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
18558 			lck_mtx_unlock(&dtrace_lock);
18559 			return (ENOENT);
18560 		}
18561 
18562 		if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
18563 			stat.dtst_exiting = 1;
18564 
18565 		nerrs = state->dts_errors;
18566 		dstate = &state->dts_vstate.dtvs_dynvars;
18567 
18568 		zpercpu_foreach_cpu(i) {
18569 			dtrace_dstate_percpu_t *dcpu = zpercpu_get_cpu(dstate->dtds_percpu, i);
18570 
18571 			stat.dtst_dyndrops += dcpu->dtdsc_drops;
18572 			stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
18573 			stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
18574 
18575 			if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
18576 				stat.dtst_filled++;
18577 
18578 			nerrs += state->dts_buffer[i].dtb_errors;
18579 
18580 			for (j = 0; j < state->dts_nspeculations; j++) {
18581 				dtrace_speculation_t *spec;
18582 				dtrace_buffer_t *buf;
18583 
18584 				spec = &state->dts_speculations[j];
18585 				buf = &spec->dtsp_buffer[i];
18586 				stat.dtst_specdrops += buf->dtb_xamot_drops;
18587 			}
18588 		}
18589 
18590 		stat.dtst_specdrops_busy = state->dts_speculations_busy;
18591 		stat.dtst_specdrops_unavail = state->dts_speculations_unavail;
18592 		stat.dtst_stkstroverflows = state->dts_stkstroverflows;
18593 		stat.dtst_dblerrors = state->dts_dblerrors;
18594 		stat.dtst_killed =
18595 			(state->dts_activity == DTRACE_ACTIVITY_KILLED);
18596 		stat.dtst_errors = nerrs;
18597 
18598 		lck_mtx_unlock(&dtrace_lock);
18599 
18600 		if (copyout(&stat, arg, sizeof (stat)) != 0)
18601 			return (EFAULT);
18602 
18603 		return (0);
18604 	}
18605 
18606 	case DTRACEIOC_FORMAT: {
18607 		dtrace_fmtdesc_t fmt;
18608 		char *str;
18609 		int len;
18610 
18611 		if (copyin(arg, &fmt, sizeof (fmt)) != 0)
18612 			return (EFAULT);
18613 
18614 		lck_mtx_lock(&dtrace_lock);
18615 
18616 		if (fmt.dtfd_format == 0 ||
18617 			fmt.dtfd_format > state->dts_nformats) {
18618 			lck_mtx_unlock(&dtrace_lock);
18619 			return (EINVAL);
18620 		}
18621 
18622 		/*
18623 		 * Format strings are allocated contiguously and they are
18624 		 * never freed; if a format index is less than the number
18625 		 * of formats, we can assert that the format map is non-NULL
18626 		 * and that the format for the specified index is non-NULL.
18627 		 */
18628 		ASSERT(state->dts_formats != NULL);
18629 		str = state->dts_formats[fmt.dtfd_format - 1]->dtf_str;
18630 		ASSERT(str != NULL);
18631 
18632 		len = strlen(str) + 1;
18633 
18634 		if (len > fmt.dtfd_length) {
18635 			fmt.dtfd_length = len;
18636 
18637 			if (copyout(&fmt, arg, sizeof (fmt)) != 0) {
18638 				lck_mtx_unlock(&dtrace_lock);
18639 				return (EINVAL);
18640 			}
18641 		} else {
18642 			if (copyout(str, (user_addr_t)fmt.dtfd_string, len) != 0) {
18643 				lck_mtx_unlock(&dtrace_lock);
18644 				return (EINVAL);
18645 			}
18646 		}
18647 
18648 		lck_mtx_unlock(&dtrace_lock);
18649 		return (0);
18650 	}
18651 
18652 	case DTRACEIOC_MODUUIDSLIST: {
18653 		size_t module_uuids_list_size;
18654 		dtrace_module_uuids_list_t* uuids_list;
18655 		uint64_t dtmul_count;
18656 
18657 		/*
18658 		 * Security restrictions make this operation illegal, if this is enabled DTrace
18659 		 * must refuse to provide any fbt probes.
18660 		 */
18661 		if (dtrace_fbt_probes_restricted()) {
18662 			cmn_err(CE_WARN, "security restrictions disallow DTRACEIOC_MODUUIDSLIST");
18663 			return (EPERM);
18664 		}
18665 
18666 		/*
18667 		 * Fail if the kernel symbol mode makes this operation illegal.
18668 		 * Both NEVER & ALWAYS_FROM_KERNEL are permanent states, it is legal to check
18669 		 * for them without holding the dtrace_lock.
18670 		 */
18671 		if (dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_NEVER ||
18672 		    dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL) {
18673 			cmn_err(CE_WARN, "dtrace_kernel_symbol_mode of %u disallows DTRACEIOC_MODUUIDSLIST", dtrace_kernel_symbol_mode);
18674 			return (EPERM);
18675 		}
18676 
18677 		/*
18678 		 * Read the number of symbolsdesc structs being passed in.
18679 		 */
18680 		if (copyin(arg + offsetof(dtrace_module_uuids_list_t, dtmul_count),
18681 		    &dtmul_count, sizeof(dtmul_count)) != 0) {
18682 			cmn_err(CE_WARN, "failed to copyin dtmul_count");
18683 			return (EFAULT);
18684 		}
18685 
18686 		/*
18687 		 * Range check the count. More than 2k kexts is probably an error.
18688 		 */
18689 		if (dtmul_count > 2048) {
18690 			cmn_err(CE_WARN, "dtmul_count is not valid");
18691 			return (EINVAL);
18692 		}
18693 
18694 		/*
18695 		 * For all queries, we return EINVAL when the user specified
18696 		 * count does not match the actual number of modules we find
18697 		 * available.
18698 		 *
18699 		 * If the user specified count is zero, then this serves as a
18700 		 * simple query to count the available modules in need of symbols.
18701 		 */
18702 
18703 		rval = 0;
18704 
18705 		if (dtmul_count == 0)
18706 		{
18707 			lck_mtx_lock(&mod_lock);
18708 			struct modctl* ctl = dtrace_modctl_list;
18709 			while (ctl) {
18710 				ASSERT(!MOD_HAS_USERSPACE_SYMBOLS(ctl));
18711 				if (!MOD_SYMBOLS_DONE(ctl) && !MOD_IS_STATIC_KEXT(ctl)) {
18712 					dtmul_count++;
18713 					rval = EINVAL;
18714 				}
18715 				ctl = ctl->mod_next;
18716 			}
18717 			lck_mtx_unlock(&mod_lock);
18718 
18719 			if (copyout(&dtmul_count, arg, sizeof (dtmul_count)) != 0)
18720 				return (EFAULT);
18721 			else
18722 				return (rval);
18723 		}
18724 
18725 		/*
18726 		 * If we reach this point, then we have a request for full list data.
18727 		 * Allocate a correctly sized structure and copyin the data.
18728 		 */
18729 		module_uuids_list_size = DTRACE_MODULE_UUIDS_LIST_SIZE(dtmul_count);
18730 		if ((uuids_list = kmem_alloc(module_uuids_list_size, KM_SLEEP)) == NULL)
18731 			return (ENOMEM);
18732 
18733 		/* NOTE! We can no longer exit this method via return */
18734 		if (copyin(arg, uuids_list, module_uuids_list_size) != 0) {
18735 			cmn_err(CE_WARN, "failed copyin of dtrace_module_uuids_list_t");
18736 			rval = EFAULT;
18737 			goto moduuidslist_cleanup;
18738 		}
18739 
18740 		/*
18741 		 * Check that the count didn't change between the first copyin and the second.
18742 		 */
18743 		if (uuids_list->dtmul_count != dtmul_count) {
18744 			rval = EINVAL;
18745 			goto moduuidslist_cleanup;
18746 		}
18747 
18748 		/*
18749 		 * Build the list of UUID's that need symbols
18750 		 */
18751 		lck_mtx_lock(&mod_lock);
18752 
18753 		dtmul_count = 0;
18754 
18755 		struct modctl* ctl = dtrace_modctl_list;
18756 		while (ctl) {
18757 			/*
18758 			 * We assume that userspace symbols will be "better" than kernel level symbols,
18759 			 * as userspace can search for dSYM(s) and symbol'd binaries. Even if kernel syms
18760 			 * are available, add user syms if the module might use them.
18761 			 */
18762 			ASSERT(!MOD_HAS_USERSPACE_SYMBOLS(ctl));
18763 			if (!MOD_SYMBOLS_DONE(ctl) && !MOD_IS_STATIC_KEXT(ctl)) {
18764 				UUID* uuid = &uuids_list->dtmul_uuid[dtmul_count];
18765 				if (dtmul_count++ < uuids_list->dtmul_count) {
18766 					memcpy(uuid, ctl->mod_uuid, sizeof(UUID));
18767 				}
18768 			}
18769 			ctl = ctl->mod_next;
18770 		}
18771 
18772 		lck_mtx_unlock(&mod_lock);
18773 
18774 		if (uuids_list->dtmul_count < dtmul_count)
18775 			rval = EINVAL;
18776 
18777 		uuids_list->dtmul_count = dtmul_count;
18778 
18779 		/*
18780 		 * Copyout the symbols list (or at least the count!)
18781 		 */
18782 		if (copyout(uuids_list, arg, module_uuids_list_size) != 0) {
18783 			cmn_err(CE_WARN, "failed copyout of dtrace_symbolsdesc_list_t");
18784 			rval = EFAULT;
18785 		}
18786 
18787 	moduuidslist_cleanup:
18788 		/*
18789 		 * If we had to allocate struct memory, free it.
18790 		 */
18791 		if (uuids_list != NULL) {
18792 			kmem_free(uuids_list, module_uuids_list_size);
18793 		}
18794 
18795 		return rval;
18796 	}
18797 
18798 	case DTRACEIOC_PROVMODSYMS: {
18799 		size_t module_symbols_size;
18800 		dtrace_module_symbols_t* module_symbols;
18801 		uint64_t dtmodsyms_count;
18802 
18803 		/*
18804 		 * Security restrictions make this operation illegal, if this is enabled DTrace
18805 		 * must refuse to provide any fbt probes.
18806 		 */
18807 		if (dtrace_fbt_probes_restricted()) {
18808 			cmn_err(CE_WARN, "security restrictions disallow DTRACEIOC_MODUUIDSLIST");
18809 			return (EPERM);
18810 		}
18811 
18812 		/*
18813 		 * Fail if the kernel symbol mode makes this operation illegal.
18814 		 * Both NEVER & ALWAYS_FROM_KERNEL are permanent states, it is legal to check
18815 		 * for them without holding the dtrace_lock.
18816 		 */
18817 		if (dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_NEVER ||
18818 		    dtrace_kernel_symbol_mode == DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL) {
18819 			cmn_err(CE_WARN, "dtrace_kernel_symbol_mode of %u disallows DTRACEIOC_PROVMODSYMS", dtrace_kernel_symbol_mode);
18820 			return (EPERM);
18821 		}
18822 
18823 		/*
18824 		 * Read the number of module symbols structs being passed in.
18825 		 */
18826 		if (copyin(arg + offsetof(dtrace_module_symbols_t, dtmodsyms_count),
18827 		    &dtmodsyms_count, sizeof(dtmodsyms_count)) != 0) {
18828 			cmn_err(CE_WARN, "failed to copyin dtmodsyms_count");
18829 			return (EFAULT);
18830 		}
18831 
18832 		/* Ensure that we have at least one symbol. */
18833 		if (dtmodsyms_count == 0) {
18834 			cmn_err(CE_WARN, "Invalid dtmodsyms_count value");
18835 			return (EINVAL);
18836 		}
18837 
18838 		/* Safely calculate size we need for copyin buffer. */
18839 		module_symbols_size = DTRACE_MODULE_SYMBOLS_SIZE(dtmodsyms_count);
18840 		if (module_symbols_size == 0 || module_symbols_size > (size_t)dtrace_copy_maxsize()) {
18841 			cmn_err(CE_WARN, "Invalid module_symbols_size %ld", module_symbols_size);
18842 			return (EINVAL);
18843 		}
18844 
18845 		if ((module_symbols = kmem_alloc(module_symbols_size, KM_SLEEP)) == NULL)
18846 			return (ENOMEM);
18847 
18848 		rval = 0;
18849 
18850 		/* NOTE! We can no longer exit this method via return */
18851 		if (copyin(arg, module_symbols, module_symbols_size) != 0) {
18852 			cmn_err(CE_WARN, "failed copyin of dtrace_module_symbols_t");
18853 			rval = EFAULT;
18854 			goto module_symbols_cleanup;
18855 		}
18856 
18857 		/*
18858 		 * Check that the count didn't change between the first copyin and the second.
18859 		 */
18860 		if (module_symbols->dtmodsyms_count != dtmodsyms_count) {
18861 			rval = EINVAL;
18862 			goto module_symbols_cleanup;
18863 		}
18864 
18865 		/*
18866 		 * Find the modctl to add symbols to.
18867 		 */
18868 		lck_mtx_lock(&dtrace_provider_lock);
18869 		lck_mtx_lock(&mod_lock);
18870 
18871 		struct modctl* ctl = dtrace_modctl_list;
18872 		while (ctl) {
18873 			ASSERT(!MOD_HAS_USERSPACE_SYMBOLS(ctl));
18874 			if (MOD_HAS_UUID(ctl) && !MOD_SYMBOLS_DONE(ctl) && memcmp(module_symbols->dtmodsyms_uuid, ctl->mod_uuid, sizeof(UUID)) == 0) {
18875 				dtrace_provider_t *prv;
18876 				ctl->mod_user_symbols = module_symbols;
18877 
18878 				/*
18879 				 * We're going to call each providers per-module provide operation
18880 				 * specifying only this module.
18881 				 */
18882 				for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next)
18883 					prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
18884 				/*
18885 				 * We gave every provider a chance to provide with the user syms, go ahead and clear them
18886 				 */
18887 				ctl->mod_user_symbols = NULL; /* MUST reset this to clear HAS_USERSPACE_SYMBOLS */
18888 			}
18889 			ctl = ctl->mod_next;
18890 		}
18891 
18892 		lck_mtx_unlock(&mod_lock);
18893 		lck_mtx_unlock(&dtrace_provider_lock);
18894 
18895 	module_symbols_cleanup:
18896 		/*
18897 		 * If we had to allocate struct memory, free it.
18898 		 */
18899 		if (module_symbols != NULL) {
18900 			kmem_free(module_symbols, module_symbols_size);
18901 		}
18902 
18903 		return rval;
18904 	}
18905 
18906 	case DTRACEIOC_PROCWAITFOR: {
18907 		dtrace_procdesc_t pdesc = {
18908 			.p_name = {0},
18909 			.p_pid  = -1
18910 		};
18911 
18912 		if ((rval = copyin(arg, &pdesc, sizeof(pdesc))) != 0)
18913 			goto proc_waitfor_error;
18914 
18915 		if ((rval = dtrace_proc_waitfor(&pdesc)) != 0)
18916 			goto proc_waitfor_error;
18917 
18918 		if ((rval = copyout(&pdesc, arg, sizeof(pdesc))) != 0)
18919 			goto proc_waitfor_error;
18920 
18921 		return 0;
18922 
18923 	proc_waitfor_error:
18924 		/* The process was suspended, revert this since the client will not do it. */
18925 		if (pdesc.p_pid != -1) {
18926 			proc_t *proc = proc_find(pdesc.p_pid);
18927 			if (proc != PROC_NULL) {
18928 				task_pidresume(proc_task(proc));
18929 				proc_rele(proc);
18930 			}
18931 		}
18932 
18933 		return rval;
18934 	}
18935 
18936 	default:
18937 		break;
18938 	}
18939 
18940 	return (ENOTTY);
18941 }
18942 
18943 /*
18944  * APPLE NOTE:  dtrace_detach not implemented
18945  */
18946 #if !defined(__APPLE__)
18947 /*ARGSUSED*/
18948 static int
dtrace_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)18949 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
18950 {
18951 	dtrace_state_t *state;
18952 
18953 	switch (cmd) {
18954 	case DDI_DETACH:
18955 		break;
18956 
18957 	case DDI_SUSPEND:
18958 		return (DDI_SUCCESS);
18959 
18960 	default:
18961 		return (DDI_FAILURE);
18962 	}
18963 
18964 	lck_mtx_lock(&cpu_lock);
18965 	lck_mtx_lock(&dtrace_provider_lock);
18966 	lck_mtx_lock(&dtrace_lock);
18967 
18968 	ASSERT(dtrace_opens == 0);
18969 
18970 	if (dtrace_helpers > 0) {
18971 		lck_mtx_unlock(&dtrace_lock);
18972 		lck_mtx_unlock(&dtrace_provider_lock);
18973 		lck_mtx_unlock(&cpu_lock);
18974 		return (DDI_FAILURE);
18975 	}
18976 
18977 	if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) {
18978 		lck_mtx_unlock(&dtrace_lock);
18979 		lck_mtx_unlock(&dtrace_provider_lock);
18980 		lck_mtx_unlock(&cpu_lock);
18981 		return (DDI_FAILURE);
18982 	}
18983 
18984 	dtrace_provider = NULL;
18985 
18986 	if ((state = dtrace_anon_grab()) != NULL) {
18987 		/*
18988 		 * If there were ECBs on this state, the provider should
18989 		 * have not been allowed to detach; assert that there is
18990 		 * none.
18991 		 */
18992 		ASSERT(state->dts_necbs == 0);
18993 		dtrace_state_destroy(state);
18994 
18995 		/*
18996 		 * If we're being detached with anonymous state, we need to
18997 		 * indicate to the kernel debugger that DTrace is now inactive.
18998 		 */
18999 		(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
19000 	}
19001 
19002 	bzero(&dtrace_anon, sizeof (dtrace_anon_t));
19003 	unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
19004 	dtrace_cpu_init = NULL;
19005 	dtrace_helpers_cleanup = NULL;
19006 	dtrace_helpers_fork = NULL;
19007 	dtrace_cpustart_init = NULL;
19008 	dtrace_cpustart_fini = NULL;
19009 	dtrace_debugger_init = NULL;
19010 	dtrace_debugger_fini = NULL;
19011 	dtrace_kreloc_init = NULL;
19012 	dtrace_kreloc_fini = NULL;
19013 	dtrace_modload = NULL;
19014 	dtrace_modunload = NULL;
19015 
19016 	lck_mtx_unlock(&cpu_lock);
19017 
19018 	if (dtrace_helptrace_enabled) {
19019 		kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize);
19020 		dtrace_helptrace_buffer = NULL;
19021 	}
19022 
19023 	kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *));
19024 	dtrace_probes = NULL;
19025 	dtrace_nprobes = 0;
19026 
19027 	dtrace_hash_destroy(dtrace_strings);
19028 	dtrace_hash_destroy(dtrace_byprov);
19029 	dtrace_hash_destroy(dtrace_bymod);
19030 	dtrace_hash_destroy(dtrace_byfunc);
19031 	dtrace_hash_destroy(dtrace_byname);
19032 	dtrace_strings = NULL;
19033 	dtrace_byprov = NULL;
19034 	dtrace_bymod = NULL;
19035 	dtrace_byfunc = NULL;
19036 	dtrace_byname = NULL;
19037 
19038 	kmem_cache_destroy(dtrace_state_cache);
19039 	vmem_destroy(dtrace_arena);
19040 
19041 	if (dtrace_toxrange != NULL) {
19042 		kmem_free(dtrace_toxrange,
19043 		    dtrace_toxranges_max * sizeof (dtrace_toxrange_t));
19044 		dtrace_toxrange = NULL;
19045 		dtrace_toxranges = 0;
19046 		dtrace_toxranges_max = 0;
19047 	}
19048 
19049 	ddi_remove_minor_node(dtrace_devi, NULL);
19050 	dtrace_devi = NULL;
19051 
19052 	ddi_soft_state_fini(&dtrace_softstate);
19053 
19054 	ASSERT(dtrace_vtime_references == 0);
19055 	ASSERT(dtrace_opens == 0);
19056 	ASSERT(dtrace_retained == NULL);
19057 
19058 	lck_mtx_unlock(&dtrace_lock);
19059 	lck_mtx_unlock(&dtrace_provider_lock);
19060 
19061 #ifdef illumos
19062 	/*
19063 	 * We don't destroy the task queue until after we have dropped our
19064 	 * locks (taskq_destroy() may block on running tasks).  To prevent
19065 	 * attempting to do work after we have effectively detached but before
19066 	 * the task queue has been destroyed, all tasks dispatched via the
19067 	 * task queue must check that DTrace is still attached before
19068 	 * performing any operation.
19069 	 */
19070 	taskq_destroy(dtrace_taskq);
19071 	dtrace_taskq = NULL;
19072 #endif
19073 
19074 	return (DDI_SUCCESS);
19075 }
19076 #endif  /* __APPLE__ */
19077 
19078 d_open_t _dtrace_open, helper_open;
19079 d_close_t _dtrace_close, helper_close;
19080 d_ioctl_t _dtrace_ioctl, helper_ioctl;
19081 
19082 int
_dtrace_open(dev_t dev,int flags,int devtype,struct proc * p)19083 _dtrace_open(dev_t dev, int flags, int devtype, struct proc *p)
19084 {
19085 #pragma unused(p)
19086 	dev_t locdev = dev;
19087 
19088 	return  dtrace_open( &locdev, flags, devtype, CRED());
19089 }
19090 
19091 int
helper_open(dev_t dev,int flags,int devtype,struct proc * p)19092 helper_open(dev_t dev, int flags, int devtype, struct proc *p)
19093 {
19094 #pragma unused(dev,flags,devtype,p)
19095 	return 0;
19096 }
19097 
19098 int
_dtrace_close(dev_t dev,int flags,int devtype,struct proc * p)19099 _dtrace_close(dev_t dev, int flags, int devtype, struct proc *p)
19100 {
19101 #pragma unused(p)
19102 	return dtrace_close( dev, flags, devtype, CRED());
19103 }
19104 
19105 int
helper_close(dev_t dev,int flags,int devtype,struct proc * p)19106 helper_close(dev_t dev, int flags, int devtype, struct proc *p)
19107 {
19108 #pragma unused(dev,flags,devtype,p)
19109 	return 0;
19110 }
19111 
19112 int
_dtrace_ioctl(dev_t dev,u_long cmd,caddr_t data,int fflag,struct proc * p)19113 _dtrace_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
19114 {
19115 #pragma unused(p)
19116 	int err, rv = 0;
19117     user_addr_t uaddrp;
19118 
19119     if (proc_is64bit(p))
19120 		uaddrp = *(user_addr_t *)data;
19121 	else
19122 		uaddrp = (user_addr_t) *(uint32_t *)data;
19123 
19124 	err = dtrace_ioctl(dev, cmd, uaddrp, fflag, CRED(), &rv);
19125 
19126 	/* Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */
19127 	if (err != 0) {
19128 		ASSERT( (err & 0xfffff000) == 0 );
19129 		return (err & 0xfff); /* ioctl will return -1 and will set errno to an error code < 4096 */
19130 	} else if (rv != 0) {
19131 		ASSERT( (rv & 0xfff00000) == 0 );
19132 		return (((rv & 0xfffff) << 12)); /* ioctl will return -1 and will set errno to a value >= 4096 */
19133 	} else
19134 		return 0;
19135 }
19136 
19137 int
helper_ioctl(dev_t dev,u_long cmd,caddr_t data,int fflag,struct proc * p)19138 helper_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
19139 {
19140 #pragma unused(dev,fflag,p)
19141 	int err, rv = 0;
19142 
19143 	err = dtrace_ioctl_helper(cmd, data, &rv);
19144 	/* Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */
19145 	if (err != 0) {
19146 		ASSERT( (err & 0xfffff000) == 0 );
19147 		return (err & 0xfff); /* ioctl will return -1 and will set errno to an error code < 4096 */
19148 	} else if (rv != 0) {
19149 		ASSERT( (rv & 0xfff00000) == 0 );
19150 		return (((rv & 0xfffff) << 12)); /* ioctl will return -1 and will set errno to a value >= 4096 */
19151 	} else
19152 		return 0;
19153 }
19154 
19155 #define HELPER_MAJOR  -24 /* let the kernel pick the device number */
19156 
19157 #define nulldevfp        (void (*)(void))&nulldev
19158 
19159 const static struct cdevsw helper_cdevsw =
19160 {
19161 	.d_open = helper_open,
19162 	.d_close = helper_close,
19163 	.d_read = eno_rdwrt,
19164 	.d_write = eno_rdwrt,
19165 	.d_ioctl = helper_ioctl,
19166 	.d_stop = eno_stop,
19167 	.d_reset = eno_reset,
19168 	.d_select = eno_select,
19169 	.d_mmap = eno_mmap,
19170 	.d_strategy = eno_strat,
19171 	.d_reserved_1 = eno_getc,
19172 	.d_reserved_2 = eno_putc,
19173 };
19174 
19175 static int helper_majdevno = 0;
19176 
19177 static int gDTraceInited = 0;
19178 
19179 void
helper_init(void)19180 helper_init( void )
19181 {
19182 	/*
19183 	 * Once the "helper" is initialized, it can take ioctl calls that use locks
19184 	 * and zones initialized in dtrace_init. Make certain dtrace_init was called
19185 	 * before us.
19186 	 */
19187 
19188 	if (!gDTraceInited) {
19189 		panic("helper_init before dtrace_init");
19190 	}
19191 
19192 	if (0 >= helper_majdevno)
19193 	{
19194 		helper_majdevno = cdevsw_add(HELPER_MAJOR, &helper_cdevsw);
19195 
19196 		if (helper_majdevno < 0) {
19197 			printf("helper_init: failed to allocate a major number!\n");
19198 			return;
19199 		}
19200 
19201 		if (NULL == devfs_make_node( makedev(helper_majdevno, 0), DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666,
19202 					DTRACEMNR_HELPER )) {
19203 			printf("dtrace_init: failed to devfs_make_node for helper!\n");
19204 			return;
19205 		}
19206 	} else
19207 		panic("helper_init: called twice!");
19208 }
19209 
19210 #undef HELPER_MAJOR
19211 
19212 static int
dtrace_clone_func(dev_t dev,int action)19213 dtrace_clone_func(dev_t dev, int action)
19214 {
19215 #pragma unused(dev)
19216 
19217 	if (action == DEVFS_CLONE_ALLOC) {
19218 		return dtrace_state_reserve();
19219 	}
19220 	else if (action == DEVFS_CLONE_FREE) {
19221 		return 0;
19222 	}
19223 	else return -1;
19224 }
19225 
19226 void dtrace_ast(void);
19227 
19228 void
dtrace_ast(void)19229 dtrace_ast(void)
19230 {
19231 	int i;
19232 	uint32_t clients = os_atomic_xchg(&dtrace_wake_clients, 0, relaxed);
19233 	if (clients == 0)
19234 		return;
19235 	/**
19236 	 * We disable preemption here to be sure that we won't get
19237 	 * interrupted by a wakeup to a thread that is higher
19238 	 * priority than us, so that we do issue all wakeups
19239 	 */
19240 	disable_preemption();
19241 	for (i = 0; i < DTRACE_NCLIENTS; i++) {
19242 		if (clients & (1 << i)) {
19243 			dtrace_state_t *state = dtrace_state_get(i);
19244 			if (state) {
19245 				wakeup(state);
19246 			}
19247 
19248 		}
19249 	}
19250 	enable_preemption();
19251 }
19252 
19253 
19254 #define DTRACE_MAJOR  -24 /* let the kernel pick the device number */
19255 
19256 static const struct cdevsw dtrace_cdevsw =
19257 {
19258 	.d_open = _dtrace_open,
19259 	.d_close = _dtrace_close,
19260 	.d_read = eno_rdwrt,
19261 	.d_write = eno_rdwrt,
19262 	.d_ioctl = _dtrace_ioctl,
19263 	.d_stop = eno_stop,
19264 	.d_reset = eno_reset,
19265 	.d_select = eno_select,
19266 	.d_mmap = eno_mmap,
19267 	.d_strategy = eno_strat,
19268 	.d_reserved_1 = eno_getc,
19269 	.d_reserved_2 = eno_putc,
19270 };
19271 
19272 LCK_ATTR_DECLARE(dtrace_lck_attr, 0, 0);
19273 LCK_GRP_DECLARE(dtrace_lck_grp, "dtrace");
19274 
19275 static int gMajDevNo;
19276 
dtrace_early_init(void)19277 void dtrace_early_init (void)
19278 {
19279 	dtrace_restriction_policy_load();
19280 
19281 	/*
19282 	 * See dtrace_impl.h for a description of kernel symbol modes.
19283 	 * The default is to wait for symbols from userspace (lazy symbols).
19284 	 */
19285 	if (!PE_parse_boot_argn("dtrace_kernel_symbol_mode", &dtrace_kernel_symbol_mode, sizeof (dtrace_kernel_symbol_mode))) {
19286 		dtrace_kernel_symbol_mode = DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE;
19287 	}
19288 }
19289 
19290 void
dtrace_init(void)19291 dtrace_init( void )
19292 {
19293 	if (0 == gDTraceInited) {
19294 		unsigned int i, ncpu;
19295 		size_t size = sizeof(dtrace_buffer_memory_maxsize);
19296 
19297 		/*
19298 		 * Disable destructive actions when dtrace is running
19299 		 * in a restricted environment
19300 		 */
19301 		dtrace_destructive_disallow = dtrace_is_restricted() &&
19302 		    !dtrace_are_restrictions_relaxed();
19303 
19304 		/*
19305 		 * DTrace allocates buffers based on the maximum number
19306 		 * of enabled cpus. This call avoids any race when finding
19307 		 * that count.
19308 		 */
19309 		ASSERT(dtrace_max_cpus == 0);
19310 		ncpu = dtrace_max_cpus = ml_wait_max_cpus();
19311 
19312 		/*
19313 		 * Retrieve the size of the physical memory in order to define
19314 		 * the state buffer memory maximal size.  If we cannot retrieve
19315 		 * this value, we'll consider that we have 1Gb of memory per CPU, that's
19316 		 * still better than raising a kernel panic.
19317 		 */
19318 		if (0 != kernel_sysctlbyname("hw.memsize", &dtrace_buffer_memory_maxsize,
19319 		                             &size, NULL, 0))
19320 		{
19321 			dtrace_buffer_memory_maxsize = ncpu * 1024 * 1024 * 1024;
19322 			printf("dtrace_init: failed to retrieve the hw.memsize, defaulted to %lld bytes\n",
19323 			       dtrace_buffer_memory_maxsize);
19324 		}
19325 
19326 		/*
19327 		 * Finally, divide by three to prevent DTrace from eating too
19328 		 * much memory.
19329 		 */
19330 		dtrace_buffer_memory_maxsize /= 3;
19331 		ASSERT(dtrace_buffer_memory_maxsize > 0);
19332 
19333 		gMajDevNo = cdevsw_add(DTRACE_MAJOR, &dtrace_cdevsw);
19334 
19335 		if (gMajDevNo < 0) {
19336 			printf("dtrace_init: failed to allocate a major number!\n");
19337 			gDTraceInited = 0;
19338 			return;
19339 		}
19340 
19341 		if (NULL == devfs_make_node_clone( makedev(gMajDevNo, 0), DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666,
19342 					dtrace_clone_func, DTRACEMNR_DTRACE )) {
19343 			printf("dtrace_init: failed to devfs_make_node_clone for dtrace!\n");
19344 			gDTraceInited = 0;
19345 			return;
19346 		}
19347 
19348 		/*
19349 		 * The cpu_core structure consists of per-CPU state available in any context.
19350 		 * On some architectures, this may mean that the page(s) containing the
19351 		 * NCPU-sized array of cpu_core structures must be locked in the TLB -- it
19352 		 * is up to the platform to assure that this is performed properly.  Note that
19353 		 * the structure is sized to avoid false sharing.
19354 		 */
19355 
19356 		dtrace_modctl_list = NULL;
19357 
19358 		cpu_core = (cpu_core_t *)kmem_zalloc( ncpu * sizeof(cpu_core_t), KM_SLEEP );
19359 		for (i = 0; i < ncpu; ++i) {
19360 			lck_mtx_init(&cpu_core[i].cpuc_pid_lock, &dtrace_lck_grp, &dtrace_lck_attr);
19361 		}
19362 
19363 		cpu_list = (dtrace_cpu_t *)kmem_zalloc( ncpu * sizeof(dtrace_cpu_t), KM_SLEEP );
19364 		for (i = 0; i < ncpu; ++i) {
19365 			cpu_list[i].cpu_id = (processorid_t)i;
19366 			cpu_list[i].cpu_next = &(cpu_list[(i+1) % ncpu]);
19367 			LIST_INIT(&cpu_list[i].cpu_cyc_list);
19368 			lck_rw_init(&cpu_list[i].cpu_ft_lock, &dtrace_lck_grp, &dtrace_lck_attr);
19369 		}
19370 
19371 		/*
19372 		 * Initialize the CPU offline/online hooks.
19373 		 */
19374 		dtrace_install_cpu_hooks();
19375 
19376 		lck_mtx_lock(&cpu_lock);
19377 		for (i = 0; i < ncpu; ++i)
19378 			/* FIXME: track CPU configuration */
19379 			dtrace_cpu_setup_initial( (processorid_t)i ); /* In lieu of register_cpu_setup_func() callback */
19380 		lck_mtx_unlock(&cpu_lock);
19381 
19382 		(void)dtrace_abs_to_nano(0LL); /* Force once only call to clock_timebase_info (which can take a lock) */
19383 
19384 		dtrace_strings = dtrace_hash_create(dtrace_strkey_offset,
19385 		    offsetof(dtrace_string_t, dtst_str),
19386 		    offsetof(dtrace_string_t, dtst_next),
19387 		    offsetof(dtrace_string_t, dtst_prev));
19388 
19389 		/*
19390 		 * See dtrace_impl.h for a description of dof modes.
19391 		 * The default is lazy dof.
19392 		 *
19393 		 * FIXME: Warn if state is LAZY_OFF? It won't break anything, but
19394 		 * makes no sense...
19395 		 */
19396 		if (!PE_parse_boot_argn("dtrace_dof_mode", &dtrace_dof_mode, sizeof (dtrace_dof_mode))) {
19397 #if defined(XNU_TARGET_OS_OSX)
19398 			dtrace_dof_mode = DTRACE_DOF_MODE_LAZY_ON;
19399 #else
19400 			dtrace_dof_mode = DTRACE_DOF_MODE_NEVER;
19401 #endif
19402 		}
19403 
19404 		/*
19405 		 * Sanity check of dof mode value.
19406 		 */
19407 		switch (dtrace_dof_mode) {
19408 			case DTRACE_DOF_MODE_NEVER:
19409 			case DTRACE_DOF_MODE_LAZY_ON:
19410 				/* valid modes, but nothing else we need to do */
19411 				break;
19412 
19413 			case DTRACE_DOF_MODE_LAZY_OFF:
19414 			case DTRACE_DOF_MODE_NON_LAZY:
19415 				/* Cannot wait for a dtrace_open to init fasttrap */
19416 				fasttrap_init();
19417 				break;
19418 
19419 			default:
19420 				/* Invalid, clamp to non lazy */
19421 				dtrace_dof_mode = DTRACE_DOF_MODE_NON_LAZY;
19422 				fasttrap_init();
19423 				break;
19424 		}
19425 
19426 #if CONFIG_DTRACE
19427         if (dtrace_dof_mode != DTRACE_DOF_MODE_NEVER)
19428             commpage_update_dof(true);
19429 #endif
19430 
19431 		gDTraceInited = 1;
19432 
19433 	} else
19434 		panic("dtrace_init: called twice!");
19435 }
19436 
19437 void
dtrace_postinit(void)19438 dtrace_postinit(void)
19439 {
19440 	/*
19441 	 * Called from bsd_init after all provider's *_init() routines have been
19442 	 * run. That way, anonymous DOF enabled under dtrace_attach() is safe
19443 	 * to go.
19444 	 */
19445 	dtrace_attach( (dev_info_t *)(uintptr_t)makedev(gMajDevNo, 0)); /* Punning a dev_t to a dev_info_t* */
19446 
19447 	/*
19448 	 * Add the mach_kernel to the module list for lazy processing
19449 	 */
19450 	struct kmod_info fake_kernel_kmod;
19451 	memset(&fake_kernel_kmod, 0, sizeof(fake_kernel_kmod));
19452 
19453 	strlcpy(fake_kernel_kmod.name, "mach_kernel", sizeof(fake_kernel_kmod.name));
19454 	fake_kernel_kmod.id = 1;
19455 	fake_kernel_kmod.address = g_kernel_kmod_info.address;
19456 	fake_kernel_kmod.size = g_kernel_kmod_info.size;
19457 
19458 	/* Ensure we don't try to touch symbols if they are gone. */
19459 	boolean_t keepsyms = false;
19460 	PE_parse_boot_argn("keepsyms", &keepsyms, sizeof(keepsyms));
19461 
19462 	if (dtrace_module_loaded(&fake_kernel_kmod, (keepsyms) ? 0 : KMOD_DTRACE_NO_KERNEL_SYMS) != 0) {
19463 		printf("dtrace_postinit: Could not register mach_kernel modctl\n");
19464 	}
19465 
19466 	(void)OSKextRegisterKextsWithDTrace();
19467 }
19468 #undef DTRACE_MAJOR
19469 
19470 /*
19471  * Routines used to register interest in cpu's being added to or removed
19472  * from the system.
19473  */
19474 void
register_cpu_setup_func(cpu_setup_func_t * ignore1,void * ignore2)19475 register_cpu_setup_func(cpu_setup_func_t *ignore1, void *ignore2)
19476 {
19477 #pragma unused(ignore1,ignore2)
19478 }
19479 
19480 void
unregister_cpu_setup_func(cpu_setup_func_t * ignore1,void * ignore2)19481 unregister_cpu_setup_func(cpu_setup_func_t *ignore1, void *ignore2)
19482 {
19483 #pragma unused(ignore1,ignore2)
19484 }
19485