1179193Sjb/*
2179193Sjb * CDDL HEADER START
3179193Sjb *
4179193Sjb * The contents of this file are subject to the terms of the
5179193Sjb * Common Development and Distribution License (the "License").
6179193Sjb * You may not use this file except in compliance with the License.
7179193Sjb *
8179193Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9179193Sjb * or http://www.opensolaris.org/os/licensing.
10179193Sjb * See the License for the specific language governing permissions
11179193Sjb * and limitations under the License.
12179193Sjb *
13179193Sjb * When distributing Covered Code, include this CDDL HEADER in each
14179193Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15179193Sjb * If applicable, add the following below this CDDL HEADER, with the
16179193Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17179193Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18179193Sjb *
19179193Sjb * CDDL HEADER END
20179469Sjb *
21179469Sjb * $FreeBSD: stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c 361088 2020-05-15 20:03:57Z dim $
22179193Sjb */
23179193Sjb
24179193Sjb/*
25268290Spfg * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26296475Smarkj * Copyright (c) 2016, Joyent, Inc. All rights reserved.
27278167Spfg * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
28179193Sjb */
29179193Sjb
30179193Sjb/*
31179193Sjb * DTrace - Dynamic Tracing for Solaris
32179193Sjb *
33179193Sjb * This is the implementation of the Solaris Dynamic Tracing framework
34179193Sjb * (DTrace).  The user-visible interface to DTrace is described at length in
35179193Sjb * the "Solaris Dynamic Tracing Guide".  The interfaces between the libdtrace
36179193Sjb * library, the in-kernel DTrace framework, and the DTrace providers are
37179193Sjb * described in the block comments in the <sys/dtrace.h> header file.  The
38179193Sjb * internal architecture of DTrace is described in the block comments in the
39179193Sjb * <sys/dtrace_impl.h> header file.  The comments contained within the DTrace
40179193Sjb * implementation very much assume mastery of all of these sources; if one has
41179193Sjb * an unanswered question about the implementation, one should consult them
42179193Sjb * first.
43179193Sjb *
44179193Sjb * The functions here are ordered roughly as follows:
45179193Sjb *
46179193Sjb *   - Probe context functions
47179193Sjb *   - Probe hashing functions
48179193Sjb *   - Non-probe context utility functions
49179193Sjb *   - Matching functions
50179193Sjb *   - Provider-to-Framework API functions
51179193Sjb *   - Probe management functions
52179193Sjb *   - DIF object functions
53179193Sjb *   - Format functions
54179193Sjb *   - Predicate functions
55179193Sjb *   - ECB functions
56179193Sjb *   - Buffer functions
57179193Sjb *   - Enabling functions
58179193Sjb *   - DOF functions
59179193Sjb *   - Anonymous enabling functions
60179193Sjb *   - Consumer state functions
61179193Sjb *   - Helper functions
62179193Sjb *   - Hook functions
63179193Sjb *   - Driver cookbook functions
64179193Sjb *
65179193Sjb * Each group of functions begins with a block comment labelled the "DTrace
66179193Sjb * [Group] Functions", allowing one to find each block by searching forward
67179193Sjb * on capital-f functions.
68179193Sjb */
69179193Sjb#include <sys/errno.h>
70277300Ssmh#ifndef illumos
71179198Sjb#include <sys/time.h>
72179198Sjb#endif
73179193Sjb#include <sys/stat.h>
74179193Sjb#include <sys/modctl.h>
75179193Sjb#include <sys/conf.h>
76179193Sjb#include <sys/systm.h>
77277300Ssmh#ifdef illumos
78179193Sjb#include <sys/ddi.h>
79179193Sjb#include <sys/sunddi.h>
80179198Sjb#endif
81179193Sjb#include <sys/cpuvar.h>
82179193Sjb#include <sys/kmem.h>
83277300Ssmh#ifdef illumos
84179193Sjb#include <sys/strsubr.h>
85179198Sjb#endif
86179193Sjb#include <sys/sysmacros.h>
87179193Sjb#include <sys/dtrace_impl.h>
88179193Sjb#include <sys/atomic.h>
89179193Sjb#include <sys/cmn_err.h>
90277300Ssmh#ifdef illumos
91179193Sjb#include <sys/mutex_impl.h>
92179193Sjb#include <sys/rwlock_impl.h>
93179198Sjb#endif
94179193Sjb#include <sys/ctf_api.h>
95277300Ssmh#ifdef illumos
96179193Sjb#include <sys/panic.h>
97179193Sjb#include <sys/priv_impl.h>
98179198Sjb#endif
99179193Sjb#include <sys/policy.h>
100277300Ssmh#ifdef illumos
101179193Sjb#include <sys/cred_impl.h>
102179193Sjb#include <sys/procfs_isa.h>
103179198Sjb#endif
104179193Sjb#include <sys/taskq.h>
105277300Ssmh#ifdef illumos
106179193Sjb#include <sys/mkdev.h>
107179193Sjb#include <sys/kdi.h>
108179198Sjb#endif
109179193Sjb#include <sys/zone.h>
110179193Sjb#include <sys/socket.h>
111179193Sjb#include <netinet/in.h>
112267937Srpaulo#include "strtolctype.h"
113179193Sjb
114179198Sjb/* FreeBSD includes: */
115277300Ssmh#ifndef illumos
116179469Sjb#include <sys/callout.h>
117179198Sjb#include <sys/ctype.h>
118254268Smarkj#include <sys/eventhandler.h>
119179198Sjb#include <sys/limits.h>
120297773Smarkj#include <sys/linker.h>
121179198Sjb#include <sys/kdb.h>
122179198Sjb#include <sys/kernel.h>
123179198Sjb#include <sys/malloc.h>
124179198Sjb#include <sys/lock.h>
125179198Sjb#include <sys/mutex.h>
126291962Smarkj#include <sys/ptrace.h>
127316210Sgnn#include <sys/random.h>
128192853Ssson#include <sys/rwlock.h>
129179198Sjb#include <sys/sx.h>
130291962Smarkj#include <sys/sysctl.h>
131291962Smarkj
132179198Sjb#include <sys/dtrace_bsd.h>
133291962Smarkj
134179198Sjb#include <netinet/in.h>
135291962Smarkj
136179198Sjb#include "dtrace_cddl.h"
137179198Sjb#include "dtrace_debug.c"
138179198Sjb#endif
139179198Sjb
140316210Sgnn#include "dtrace_xoroshiro128_plus.h"
141316210Sgnn
142179193Sjb/*
143179193Sjb * DTrace Tunable Variables
144179193Sjb *
145179193Sjb * The following variables may be tuned by adding a line to /etc/system that
146179193Sjb * includes both the name of the DTrace module ("dtrace") and the name of the
147179193Sjb * variable.  For example:
148179193Sjb *
149179193Sjb *   set dtrace:dtrace_destructive_disallow = 1
150179193Sjb *
151179193Sjb * In general, the only variables that one should be tuning this way are those
152179193Sjb * that affect system-wide DTrace behavior, and for which the default behavior
153179193Sjb * is undesirable.  Most of these variables are tunable on a per-consumer
154179193Sjb * basis using DTrace options, and need not be tuned on a system-wide basis.
155179193Sjb * When tuning these variables, avoid pathological values; while some attempt
156179193Sjb * is made to verify the integrity of these variables, they are not considered
157179193Sjb * part of the supported interface to DTrace, and they are therefore not
158179193Sjb * checked comprehensively.  Further, these variables should not be tuned
159179193Sjb * dynamically via "mdb -kw" or other means; they should only be tuned via
160179193Sjb * /etc/system.
161179193Sjb */
162179193Sjbint		dtrace_destructive_disallow = 0;
163310328Sgnn#ifndef illumos
164310328Sgnn/* Positive logic version of dtrace_destructive_disallow for loader tunable */
165310328Sgnnint		dtrace_allow_destructive = 1;
166310328Sgnn#endif
167179193Sjbdtrace_optval_t	dtrace_nonroot_maxsize = (16 * 1024 * 1024);
168179193Sjbsize_t		dtrace_difo_maxsize = (256 * 1024);
169262596Smarkjdtrace_optval_t	dtrace_dof_maxsize = (8 * 1024 * 1024);
170288415Smarkjsize_t		dtrace_statvar_maxsize = (16 * 1024);
171179193Sjbsize_t		dtrace_actions_max = (16 * 1024);
172179193Sjbsize_t		dtrace_retain_max = 1024;
173237817Spfgdtrace_optval_t	dtrace_helper_actions_max = 128;
174179193Sjbdtrace_optval_t	dtrace_helper_providers_max = 32;
175179193Sjbdtrace_optval_t	dtrace_dstate_defsize = (1 * 1024 * 1024);
176179193Sjbsize_t		dtrace_strsize_default = 256;
177179193Sjbdtrace_optval_t	dtrace_cleanrate_default = 9900990;		/* 101 hz */
178179193Sjbdtrace_optval_t	dtrace_cleanrate_min = 200000;			/* 5000 hz */
179179193Sjbdtrace_optval_t	dtrace_cleanrate_max = (uint64_t)60 * NANOSEC;	/* 1/minute */
180179193Sjbdtrace_optval_t	dtrace_aggrate_default = NANOSEC;		/* 1 hz */
181179193Sjbdtrace_optval_t	dtrace_statusrate_default = NANOSEC;		/* 1 hz */
182179193Sjbdtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC;	 /* 6/minute */
183179193Sjbdtrace_optval_t	dtrace_switchrate_default = NANOSEC;		/* 1 hz */
184179193Sjbdtrace_optval_t	dtrace_nspec_default = 1;
185179193Sjbdtrace_optval_t	dtrace_specsize_default = 32 * 1024;
186179193Sjbdtrace_optval_t dtrace_stackframes_default = 20;
187179193Sjbdtrace_optval_t dtrace_ustackframes_default = 20;
188179193Sjbdtrace_optval_t dtrace_jstackframes_default = 50;
189179193Sjbdtrace_optval_t dtrace_jstackstrsize_default = 512;
190179193Sjbint		dtrace_msgdsize_max = 128;
191275562Sdelphijhrtime_t	dtrace_chill_max = MSEC2NSEC(500);		/* 500 ms */
192179193Sjbhrtime_t	dtrace_chill_interval = NANOSEC;		/* 1000 ms */
193179193Sjbint		dtrace_devdepth_max = 32;
194179193Sjbint		dtrace_err_verbose;
195179193Sjbhrtime_t	dtrace_deadman_interval = NANOSEC;
196179193Sjbhrtime_t	dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC;
197179193Sjbhrtime_t	dtrace_deadman_user = (hrtime_t)30 * NANOSEC;
198248983Spfghrtime_t	dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC;
199277300Ssmh#ifndef illumos
200256571Smarkjint		dtrace_memstr_max = 4096;
201256571Smarkj#endif
202179193Sjb
203179193Sjb/*
204179193Sjb * DTrace External Variables
205179193Sjb *
206179193Sjb * As dtrace(7D) is a kernel module, any DTrace variables are obviously
207179193Sjb * available to DTrace consumers via the backtick (`) syntax.  One of these,
208179193Sjb * dtrace_zero, is made deliberately so:  it is provided as a source of
209179193Sjb * well-known, zero-filled memory.  While this variable is not documented,
210179193Sjb * it is used by some translators as an implementation detail.
211179193Sjb */
212179193Sjbconst char	dtrace_zero[256] = { 0 };	/* zero-filled memory */
213179193Sjb
214179193Sjb/*
215179193Sjb * DTrace Internal Variables
216179193Sjb */
217277300Ssmh#ifdef illumos
218179193Sjbstatic dev_info_t	*dtrace_devi;		/* device info */
219179198Sjb#endif
220277300Ssmh#ifdef illumos
221260091Smarkjstatic vmem_t		*dtrace_arena;		/* probe ID arena */
222179193Sjbstatic vmem_t		*dtrace_minor;		/* minor number arena */
223248983Spfg#else
224179193Sjbstatic taskq_t		*dtrace_taskq;		/* task queue */
225260131Smarkjstatic struct unrhdr	*dtrace_arena;		/* Probe ID number.     */
226179198Sjb#endif
227179193Sjbstatic dtrace_probe_t	**dtrace_probes;	/* array of all probes */
228179193Sjbstatic int		dtrace_nprobes;		/* number of probes */
229179193Sjbstatic dtrace_provider_t *dtrace_provider;	/* provider list */
230179193Sjbstatic dtrace_meta_t	*dtrace_meta_pid;	/* user-land meta provider */
231179193Sjbstatic int		dtrace_opens;		/* number of opens */
232179193Sjbstatic int		dtrace_helpers;		/* number of helpers */
233267929Srpaulostatic int		dtrace_getf;		/* number of unpriv getf()s */
234277300Ssmh#ifdef illumos
235179193Sjbstatic void		*dtrace_softstate;	/* softstate pointer */
236179198Sjb#endif
237179193Sjbstatic dtrace_hash_t	*dtrace_bymod;		/* probes hashed by module */
238179193Sjbstatic dtrace_hash_t	*dtrace_byfunc;		/* probes hashed by function */
239179193Sjbstatic dtrace_hash_t	*dtrace_byname;		/* probes hashed by name */
240179193Sjbstatic dtrace_toxrange_t *dtrace_toxrange;	/* toxic range array */
241179193Sjbstatic int		dtrace_toxranges;	/* number of toxic ranges */
242179193Sjbstatic int		dtrace_toxranges_max;	/* size of toxic range array */
243179193Sjbstatic dtrace_anon_t	dtrace_anon;		/* anonymous enabling */
244179193Sjbstatic kmem_cache_t	*dtrace_state_cache;	/* cache for dynamic state */
245179193Sjbstatic uint64_t		dtrace_vtime_references; /* number of vtimestamp refs */
246179193Sjbstatic kthread_t	*dtrace_panicked;	/* panicking thread */
247179193Sjbstatic dtrace_ecb_t	*dtrace_ecb_create_cache; /* cached created ECB */
248179193Sjbstatic dtrace_genid_t	dtrace_probegen;	/* current probe generation */
249179193Sjbstatic dtrace_helpers_t *dtrace_deferred_pid;	/* deferred helper list */
250179193Sjbstatic dtrace_enabling_t *dtrace_retained;	/* list of retained enablings */
251268130Spfgstatic dtrace_genid_t	dtrace_retained_gen;	/* current retained enab gen */
252179193Sjbstatic dtrace_dynvar_t	dtrace_dynhash_sink;	/* end of dynamic hash chains */
253268290Spfgstatic int		dtrace_dynvar_failclean; /* dynvars failed to clean */
254277300Ssmh#ifndef illumos
255179198Sjbstatic struct mtx	dtrace_unr_mtx;
256179198SjbMTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF);
257254309Smarkjstatic eventhandler_tag	dtrace_kld_load_tag;
258254813Smarkjstatic eventhandler_tag	dtrace_kld_unload_try_tag;
259179198Sjb#endif
260179193Sjb
261179193Sjb/*
262179193Sjb * DTrace Locking
263179193Sjb * DTrace is protected by three (relatively coarse-grained) locks:
264179193Sjb *
265179193Sjb * (1) dtrace_lock is required to manipulate essentially any DTrace state,
266179193Sjb *     including enabling state, probes, ECBs, consumer state, helper state,
267179193Sjb *     etc.  Importantly, dtrace_lock is _not_ required when in probe context;
268179193Sjb *     probe context is lock-free -- synchronization is handled via the
269179193Sjb *     dtrace_sync() cross call mechanism.
270179193Sjb *
271179193Sjb * (2) dtrace_provider_lock is required when manipulating provider state, or
272179193Sjb *     when provider state must be held constant.
273179193Sjb *
274179193Sjb * (3) dtrace_meta_lock is required when manipulating meta provider state, or
275179193Sjb *     when meta provider state must be held constant.
276179193Sjb *
277179193Sjb * The lock ordering between these three locks is dtrace_meta_lock before
278179193Sjb * dtrace_provider_lock before dtrace_lock.  (In particular, there are
279179193Sjb * several places where dtrace_provider_lock is held by the framework as it
280179193Sjb * calls into the providers -- which then call back into the framework,
281179193Sjb * grabbing dtrace_lock.)
282179193Sjb *
283179193Sjb * There are two other locks in the mix:  mod_lock and cpu_lock.  With respect
284179193Sjb * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical
285179193Sjb * role as a coarse-grained lock; it is acquired before both of these locks.
286179193Sjb * With respect to dtrace_meta_lock, its behavior is stranger:  cpu_lock must
287179193Sjb * be acquired _between_ dtrace_meta_lock and any other DTrace locks.
288179193Sjb * mod_lock is similar with respect to dtrace_provider_lock in that it must be
289179193Sjb * acquired _between_ dtrace_provider_lock and dtrace_lock.
290179193Sjb */
291179193Sjbstatic kmutex_t		dtrace_lock;		/* probe state lock */
292179193Sjbstatic kmutex_t		dtrace_provider_lock;	/* provider state lock */
293179193Sjbstatic kmutex_t		dtrace_meta_lock;	/* meta-provider state lock */
294179193Sjb
295277300Ssmh#ifndef illumos
296179198Sjb/* XXX FreeBSD hacks. */
297179198Sjb#define cr_suid		cr_svuid
298179198Sjb#define cr_sgid		cr_svgid
299179198Sjb#define	ipaddr_t	in_addr_t
300179198Sjb#define mod_modname	pathname
301179198Sjb#define vuprintf	vprintf
302179198Sjb#define ttoproc(_a)	((_a)->td_proc)
303179198Sjb#define crgetzoneid(_a)	0
304179198Sjb#define SNOCD		0
305179198Sjb#define CPU_ON_INTR(_a)	0
306179198Sjb
307179198Sjb#define PRIV_EFFECTIVE		(1 << 0)
308179198Sjb#define PRIV_DTRACE_KERNEL	(1 << 1)
309179198Sjb#define PRIV_DTRACE_PROC	(1 << 2)
310179198Sjb#define PRIV_DTRACE_USER	(1 << 3)
311179198Sjb#define PRIV_PROC_OWNER		(1 << 4)
312179198Sjb#define PRIV_PROC_ZONE		(1 << 5)
313179198Sjb#define PRIV_ALL		~0
314179198Sjb
315262665SmarkjSYSCTL_DECL(_debug_dtrace);
316262665SmarkjSYSCTL_DECL(_kern_dtrace);
317179198Sjb#endif
318179198Sjb
319277300Ssmh#ifdef illumos
320179198Sjb#define curcpu	CPU->cpu_id
321179198Sjb#endif
322179198Sjb
323179198Sjb
324179193Sjb/*
325179193Sjb * DTrace Provider Variables
326179193Sjb *
327179193Sjb * These are the variables relating to DTrace as a provider (that is, the
328179193Sjb * provider of the BEGIN, END, and ERROR probes).
329179193Sjb */
330179193Sjbstatic dtrace_pattr_t	dtrace_provider_attr = {
331179193Sjb{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
332179193Sjb{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
333179193Sjb{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
334179193Sjb{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
335179193Sjb{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
336179193Sjb};
337179193Sjb
338179193Sjbstatic void
339179193Sjbdtrace_nullop(void)
340179193Sjb{}
341179193Sjb
342324282Smarkjstatic dtrace_pops_t dtrace_provider_ops = {
343324282Smarkj	.dtps_provide =	(void (*)(void *, dtrace_probedesc_t *))dtrace_nullop,
344324282Smarkj	.dtps_provide_module =	(void (*)(void *, modctl_t *))dtrace_nullop,
345324282Smarkj	.dtps_enable =	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
346324282Smarkj	.dtps_disable =	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
347324282Smarkj	.dtps_suspend =	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
348324282Smarkj	.dtps_resume =	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
349324282Smarkj	.dtps_getargdesc =	NULL,
350324282Smarkj	.dtps_getargval =	NULL,
351324282Smarkj	.dtps_usermode =	NULL,
352324282Smarkj	.dtps_destroy =	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
353179193Sjb};
354179193Sjb
355179193Sjbstatic dtrace_id_t	dtrace_probeid_begin;	/* special BEGIN probe */
356179193Sjbstatic dtrace_id_t	dtrace_probeid_end;	/* special END probe */
357179193Sjbdtrace_id_t		dtrace_probeid_error;	/* special ERROR probe */
358179193Sjb
359179193Sjb/*
360179193Sjb * DTrace Helper Tracing Variables
361278166Spfg *
362278166Spfg * These variables should be set dynamically to enable helper tracing.  The
363278166Spfg * only variables that should be set are dtrace_helptrace_enable (which should
364278166Spfg * be set to a non-zero value to allocate helper tracing buffers on the next
365278166Spfg * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a
366278166Spfg * non-zero value to deallocate helper tracing buffers on the next close of
367278166Spfg * /dev/dtrace).  When (and only when) helper tracing is disabled, the
368278166Spfg * buffer size may also be set via dtrace_helptrace_bufsize.
369179193Sjb */
370278166Spfgint			dtrace_helptrace_enable = 0;
371278166Spfgint			dtrace_helptrace_disable = 0;
372278166Spfgint			dtrace_helptrace_bufsize = 16 * 1024 * 1024;
373278166Spfguint32_t		dtrace_helptrace_nlocals;
374278166Spfgstatic dtrace_helptrace_t *dtrace_helptrace_buffer;
375278166Spfgstatic uint32_t		dtrace_helptrace_next = 0;
376278166Spfgstatic int		dtrace_helptrace_wrapped = 0;
377179193Sjb
378179193Sjb/*
379179193Sjb * DTrace Error Hashing
380179193Sjb *
381179193Sjb * On DEBUG kernels, DTrace will track the errors that has seen in a hash
382179193Sjb * table.  This is very useful for checking coverage of tests that are
383179193Sjb * expected to induce DIF or DOF processing errors, and may be useful for
384179193Sjb * debugging problems in the DIF code generator or in DOF generation .  The
385179193Sjb * error hash may be examined with the ::dtrace_errhash MDB dcmd.
386179193Sjb */
387179193Sjb#ifdef DEBUG
388179193Sjbstatic dtrace_errhash_t	dtrace_errhash[DTRACE_ERRHASHSZ];
389179193Sjbstatic const char *dtrace_errlast;
390179193Sjbstatic kthread_t *dtrace_errthread;
391179193Sjbstatic kmutex_t dtrace_errlock;
392179193Sjb#endif
393179193Sjb
394179193Sjb/*
395179193Sjb * DTrace Macros and Constants
396179193Sjb *
397179193Sjb * These are various macros that are useful in various spots in the
398179193Sjb * implementation, along with a few random constants that have no meaning
399179193Sjb * outside of the implementation.  There is no real structure to this cpp
400179193Sjb * mishmash -- but is there ever?
401179193Sjb */
402179193Sjb#define	DTRACE_HASHSTR(hash, probe)	\
403179193Sjb	dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs)))
404179193Sjb
405179193Sjb#define	DTRACE_HASHNEXT(hash, probe)	\
406179193Sjb	(dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs)
407179193Sjb
408179193Sjb#define	DTRACE_HASHPREV(hash, probe)	\
409179193Sjb	(dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs)
410179193Sjb
411179193Sjb#define	DTRACE_HASHEQ(hash, lhs, rhs)	\
412179193Sjb	(strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \
413179193Sjb	    *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0)
414179193Sjb
415179193Sjb#define	DTRACE_AGGHASHSIZE_SLEW		17
416179193Sjb
417179193Sjb#define	DTRACE_V4MAPPED_OFFSET		(sizeof (uint32_t) * 3)
418179193Sjb
419179193Sjb/*
420179193Sjb * The key for a thread-local variable consists of the lower 61 bits of the
421179193Sjb * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL.
422179193Sjb * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never
423179193Sjb * equal to a variable identifier.  This is necessary (but not sufficient) to
424179193Sjb * assure that global associative arrays never collide with thread-local
425179193Sjb * variables.  To guarantee that they cannot collide, we must also define the
426179193Sjb * order for keying dynamic variables.  That order is:
427179193Sjb *
428179193Sjb *   [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ]
429179193Sjb *
430179193Sjb * Because the variable-key and the tls-key are in orthogonal spaces, there is
431179193Sjb * no way for a global variable key signature to match a thread-local key
432179193Sjb * signature.
433179193Sjb */
434277300Ssmh#ifdef illumos
435179193Sjb#define	DTRACE_TLS_THRKEY(where) { \
436179193Sjb	uint_t intr = 0; \
437179193Sjb	uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \
438179193Sjb	for (; actv; actv >>= 1) \
439179193Sjb		intr++; \
440179193Sjb	ASSERT(intr < (1 << 3)); \
441179193Sjb	(where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \
442179193Sjb	    (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
443179193Sjb}
444179198Sjb#else
445179198Sjb#define	DTRACE_TLS_THRKEY(where) { \
446179198Sjb	solaris_cpu_t *_c = &solaris_cpu[curcpu]; \
447179198Sjb	uint_t intr = 0; \
448179198Sjb	uint_t actv = _c->cpu_intr_actv; \
449179198Sjb	for (; actv; actv >>= 1) \
450179198Sjb		intr++; \
451179198Sjb	ASSERT(intr < (1 << 3)); \
452179198Sjb	(where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \
453179198Sjb	    (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
454179198Sjb}
455179198Sjb#endif
456179193Sjb
457179193Sjb#define	DT_BSWAP_8(x)	((x) & 0xff)
458179193Sjb#define	DT_BSWAP_16(x)	((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8))
459179193Sjb#define	DT_BSWAP_32(x)	((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16))
460179193Sjb#define	DT_BSWAP_64(x)	((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32))
461179193Sjb
462179193Sjb#define	DT_MASK_LO 0x00000000FFFFFFFFULL
463179193Sjb
464179193Sjb#define	DTRACE_STORE(type, tomax, offset, what) \
465179193Sjb	*((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what);
466179193Sjb
467248752Spfg#ifndef __x86
468179193Sjb#define	DTRACE_ALIGNCHECK(addr, size, flags)				\
469179193Sjb	if (addr & (size - 1)) {					\
470179193Sjb		*flags |= CPU_DTRACE_BADALIGN;				\
471179198Sjb		cpu_core[curcpu].cpuc_dtrace_illval = addr;	\
472179193Sjb		return (0);						\
473179193Sjb	}
474179193Sjb#else
475179193Sjb#define	DTRACE_ALIGNCHECK(addr, size, flags)
476179193Sjb#endif
477179193Sjb
478179193Sjb/*
479179193Sjb * Test whether a range of memory starting at testaddr of size testsz falls
480179193Sjb * within the range of memory described by addr, sz.  We take care to avoid
481179193Sjb * problems with overflow and underflow of the unsigned quantities, and
482179193Sjb * disallow all negative sizes.  Ranges of size 0 are allowed.
483179193Sjb */
484179193Sjb#define	DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \
485267929Srpaulo	((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \
486267929Srpaulo	(testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \
487179193Sjb	(testaddr) + (testsz) >= (testaddr))
488179193Sjb
489179193Sjb/*
490179193Sjb * Test whether alloc_sz bytes will fit in the scratch region.  We isolate
491179193Sjb * alloc_sz on the righthand side of the comparison in order to avoid overflow
492179193Sjb * or underflow in the comparison with it.  This is simpler than the INRANGE
493179193Sjb * check above, because we know that the dtms_scratch_ptr is valid in the
494179193Sjb * range.  Allocations of size zero are allowed.
495179193Sjb */
496179193Sjb#define	DTRACE_INSCRATCH(mstate, alloc_sz) \
497179193Sjb	((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \
498179193Sjb	(mstate)->dtms_scratch_ptr >= (alloc_sz))
499179193Sjb
500179193Sjb#define	DTRACE_LOADFUNC(bits)						\
501179193Sjb/*CSTYLED*/								\
502179193Sjbuint##bits##_t								\
503179193Sjbdtrace_load##bits(uintptr_t addr)					\
504179193Sjb{									\
505179193Sjb	size_t size = bits / NBBY;					\
506179193Sjb	/*CSTYLED*/							\
507179193Sjb	uint##bits##_t rval;						\
508179193Sjb	int i;								\
509179193Sjb	volatile uint16_t *flags = (volatile uint16_t *)		\
510179198Sjb	    &cpu_core[curcpu].cpuc_dtrace_flags;			\
511179193Sjb									\
512179193Sjb	DTRACE_ALIGNCHECK(addr, size, flags);				\
513179193Sjb									\
514179193Sjb	for (i = 0; i < dtrace_toxranges; i++) {			\
515179193Sjb		if (addr >= dtrace_toxrange[i].dtt_limit)		\
516179193Sjb			continue;					\
517179193Sjb									\
518179193Sjb		if (addr + size <= dtrace_toxrange[i].dtt_base)		\
519179193Sjb			continue;					\
520179193Sjb									\
521179193Sjb		/*							\
522179193Sjb		 * This address falls within a toxic region; return 0.	\
523179193Sjb		 */							\
524179193Sjb		*flags |= CPU_DTRACE_BADADDR;				\
525179198Sjb		cpu_core[curcpu].cpuc_dtrace_illval = addr;		\
526179193Sjb		return (0);						\
527179193Sjb	}								\
528179193Sjb									\
529179193Sjb	*flags |= CPU_DTRACE_NOFAULT;					\
530179193Sjb	/*CSTYLED*/							\
531179193Sjb	rval = *((volatile uint##bits##_t *)addr);			\
532179193Sjb	*flags &= ~CPU_DTRACE_NOFAULT;					\
533179193Sjb									\
534179193Sjb	return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0);		\
535179193Sjb}
536179193Sjb
537179193Sjb#ifdef _LP64
538179193Sjb#define	dtrace_loadptr	dtrace_load64
539179193Sjb#else
540179193Sjb#define	dtrace_loadptr	dtrace_load32
541179193Sjb#endif
542179193Sjb
543179193Sjb#define	DTRACE_DYNHASH_FREE	0
544179193Sjb#define	DTRACE_DYNHASH_SINK	1
545179193Sjb#define	DTRACE_DYNHASH_VALID	2
546179193Sjb
547179193Sjb#define	DTRACE_MATCH_NEXT	0
548179193Sjb#define	DTRACE_MATCH_DONE	1
549179193Sjb#define	DTRACE_ANCHORED(probe)	((probe)->dtpr_func[0] != '\0')
550179193Sjb#define	DTRACE_STATE_ALIGN	64
551179193Sjb
552179193Sjb#define	DTRACE_FLAGS2FLT(flags)						\
553179193Sjb	(((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR :		\
554179193Sjb	((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP :		\
555179193Sjb	((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO :		\
556179193Sjb	((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV :		\
557179193Sjb	((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV :		\
558179193Sjb	((flags) & CPU_DTRACE_TUPOFLOW) ?  DTRACEFLT_TUPOFLOW :		\
559179193Sjb	((flags) & CPU_DTRACE_BADALIGN) ?  DTRACEFLT_BADALIGN :		\
560179193Sjb	((flags) & CPU_DTRACE_NOSCRATCH) ?  DTRACEFLT_NOSCRATCH :	\
561179193Sjb	((flags) & CPU_DTRACE_BADSTACK) ?  DTRACEFLT_BADSTACK :		\
562179193Sjb	DTRACEFLT_UNKNOWN)
563179193Sjb
564179193Sjb#define	DTRACEACT_ISSTRING(act)						\
565179193Sjb	((act)->dta_kind == DTRACEACT_DIFEXPR &&			\
566179193Sjb	(act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING)
567179193Sjb
568179198Sjb/* Function prototype definitions: */
569179193Sjbstatic size_t dtrace_strlen(const char *, size_t);
570179193Sjbstatic dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id);
571179193Sjbstatic void dtrace_enabling_provide(dtrace_provider_t *);
572179193Sjbstatic int dtrace_enabling_match(dtrace_enabling_t *, int *);
573179193Sjbstatic void dtrace_enabling_matchall(void);
574248983Spfgstatic void dtrace_enabling_reap(void);
575179193Sjbstatic dtrace_state_t *dtrace_anon_grab(void);
576179193Sjbstatic uint64_t dtrace_helper(int, dtrace_mstate_t *,
577179193Sjb    dtrace_state_t *, uint64_t, uint64_t);
578179193Sjbstatic dtrace_helpers_t *dtrace_helpers_create(proc_t *);
579179193Sjbstatic void dtrace_buffer_drop(dtrace_buffer_t *);
580248983Spfgstatic int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when);
581179193Sjbstatic intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t,
582179193Sjb    dtrace_state_t *, dtrace_mstate_t *);
583179193Sjbstatic int dtrace_state_option(dtrace_state_t *, dtrace_optid_t,
584179193Sjb    dtrace_optval_t);
585179193Sjbstatic int dtrace_ecb_create_enable(dtrace_probe_t *, void *);
586179193Sjbstatic void dtrace_helper_provider_destroy(dtrace_helper_provider_t *);
587179198Sjbuint16_t dtrace_load16(uintptr_t);
588179198Sjbuint32_t dtrace_load32(uintptr_t);
589179198Sjbuint64_t dtrace_load64(uintptr_t);
590179198Sjbuint8_t dtrace_load8(uintptr_t);
591179198Sjbvoid dtrace_dynvar_clean(dtrace_dstate_t *);
592179198Sjbdtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *,
593179198Sjb    size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *);
594179198Sjbuintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *);
595267929Srpaulostatic int dtrace_priv_proc(dtrace_state_t *);
596267929Srpaulostatic void dtrace_getf_barrier(void);
597179193Sjb
598179193Sjb/*
599179193Sjb * DTrace Probe Context Functions
600179193Sjb *
601179193Sjb * These functions are called from probe context.  Because probe context is
602179193Sjb * any context in which C may be called, arbitrarily locks may be held,
603179193Sjb * interrupts may be disabled, we may be in arbitrary dispatched state, etc.
604179193Sjb * As a result, functions called from probe context may only call other DTrace
605179193Sjb * support functions -- they may not interact at all with the system at large.
606179193Sjb * (Note that the ASSERT macro is made probe-context safe by redefining it in
607179193Sjb * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary
608179193Sjb * loads are to be performed from probe context, they _must_ be in terms of
609179193Sjb * the safe dtrace_load*() variants.
610179193Sjb *
611179193Sjb * Some functions in this block are not actually called from probe context;
612179193Sjb * for these functions, there will be a comment above the function reading
613179193Sjb * "Note:  not called from probe context."
614179193Sjb */
615179193Sjbvoid
616179193Sjbdtrace_panic(const char *format, ...)
617179193Sjb{
618179193Sjb	va_list alist;
619179193Sjb
620179193Sjb	va_start(alist, format);
621281916Smarkj#ifdef __FreeBSD__
622281916Smarkj	vpanic(format, alist);
623281916Smarkj#else
624179193Sjb	dtrace_vpanic(format, alist);
625281916Smarkj#endif
626179193Sjb	va_end(alist);
627179193Sjb}
628179193Sjb
629179193Sjbint
630179193Sjbdtrace_assfail(const char *a, const char *f, int l)
631179193Sjb{
632179193Sjb	dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l);
633179193Sjb
634179193Sjb	/*
635179193Sjb	 * We just need something here that even the most clever compiler
636179193Sjb	 * cannot optimize away.
637179193Sjb	 */
638179193Sjb	return (a[(uintptr_t)f]);
639179193Sjb}
640179193Sjb
641179193Sjb/*
642179193Sjb * Atomically increment a specified error counter from probe context.
643179193Sjb */
644179193Sjbstatic void
645179193Sjbdtrace_error(uint32_t *counter)
646179193Sjb{
647179193Sjb	/*
648179193Sjb	 * Most counters stored to in probe context are per-CPU counters.
649179193Sjb	 * However, there are some error conditions that are sufficiently
650179193Sjb	 * arcane that they don't merit per-CPU storage.  If these counters
651179193Sjb	 * are incremented concurrently on different CPUs, scalability will be
652179193Sjb	 * adversely affected -- but we don't expect them to be white-hot in a
653179193Sjb	 * correctly constructed enabling...
654179193Sjb	 */
655179193Sjb	uint32_t oval, nval;
656179193Sjb
657179193Sjb	do {
658179193Sjb		oval = *counter;
659179193Sjb
660179193Sjb		if ((nval = oval + 1) == 0) {
661179193Sjb			/*
662179193Sjb			 * If the counter would wrap, set it to 1 -- assuring
663179193Sjb			 * that the counter is never zero when we have seen
664179193Sjb			 * errors.  (The counter must be 32-bits because we
665179193Sjb			 * aren't guaranteed a 64-bit compare&swap operation.)
666179193Sjb			 * To save this code both the infamy of being fingered
667179193Sjb			 * by a priggish news story and the indignity of being
668179193Sjb			 * the target of a neo-puritan witch trial, we're
669179193Sjb			 * carefully avoiding any colorful description of the
670179193Sjb			 * likelihood of this condition -- but suffice it to
671179193Sjb			 * say that it is only slightly more likely than the
672179193Sjb			 * overflow of predicate cache IDs, as discussed in
673179193Sjb			 * dtrace_predicate_create().
674179193Sjb			 */
675179193Sjb			nval = 1;
676179193Sjb		}
677179193Sjb	} while (dtrace_cas32(counter, oval, nval) != oval);
678179193Sjb}
679179193Sjb
680179193Sjb/*
681179193Sjb * Use the DTRACE_LOADFUNC macro to define functions for each of loading a
682179193Sjb * uint8_t, a uint16_t, a uint32_t and a uint64_t.
683179193Sjb */
684296475Smarkj/* BEGIN CSTYLED */
685179193SjbDTRACE_LOADFUNC(8)
686179193SjbDTRACE_LOADFUNC(16)
687179193SjbDTRACE_LOADFUNC(32)
688179193SjbDTRACE_LOADFUNC(64)
689296475Smarkj/* END CSTYLED */
690179193Sjb
691179193Sjbstatic int
692179193Sjbdtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate)
693179193Sjb{
694179193Sjb	if (dest < mstate->dtms_scratch_base)
695179193Sjb		return (0);
696179193Sjb
697179193Sjb	if (dest + size < dest)
698179193Sjb		return (0);
699179193Sjb
700179193Sjb	if (dest + size > mstate->dtms_scratch_ptr)
701179193Sjb		return (0);
702179193Sjb
703179193Sjb	return (1);
704179193Sjb}
705179193Sjb
706179193Sjbstatic int
707179193Sjbdtrace_canstore_statvar(uint64_t addr, size_t sz,
708179193Sjb    dtrace_statvar_t **svars, int nsvars)
709179193Sjb{
710179193Sjb	int i;
711288415Smarkj	size_t maxglobalsize, maxlocalsize;
712179193Sjb
713288415Smarkj	if (nsvars == 0)
714288415Smarkj		return (0);
715288415Smarkj
716288415Smarkj	maxglobalsize = dtrace_statvar_maxsize;
717288415Smarkj	maxlocalsize = (maxglobalsize + sizeof (uint64_t)) * NCPU;
718288415Smarkj
719179193Sjb	for (i = 0; i < nsvars; i++) {
720179193Sjb		dtrace_statvar_t *svar = svars[i];
721288415Smarkj		uint8_t scope;
722288415Smarkj		size_t size;
723179193Sjb
724288415Smarkj		if (svar == NULL || (size = svar->dtsv_size) == 0)
725179193Sjb			continue;
726179193Sjb
727288415Smarkj		scope = svar->dtsv_var.dtdv_scope;
728288415Smarkj
729288415Smarkj		/*
730288415Smarkj		 * We verify that our size is valid in the spirit of providing
731288415Smarkj		 * defense in depth:  we want to prevent attackers from using
732288415Smarkj		 * DTrace to escalate an orthogonal kernel heap corruption bug
733288415Smarkj		 * into the ability to store to arbitrary locations in memory.
734288415Smarkj		 */
735288415Smarkj		VERIFY((scope == DIFV_SCOPE_GLOBAL && size < maxglobalsize) ||
736288415Smarkj		    (scope == DIFV_SCOPE_LOCAL && size < maxlocalsize));
737288415Smarkj
738179193Sjb		if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size))
739179193Sjb			return (1);
740179193Sjb	}
741179193Sjb
742179193Sjb	return (0);
743179193Sjb}
744179193Sjb
745179193Sjb/*
746179193Sjb * Check to see if the address is within a memory region to which a store may
747179193Sjb * be issued.  This includes the DTrace scratch areas, and any DTrace variable
748179193Sjb * region.  The caller of dtrace_canstore() is responsible for performing any
749179193Sjb * alignment checks that are needed before stores are actually executed.
750179193Sjb */
751179193Sjbstatic int
752179193Sjbdtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
753179193Sjb    dtrace_vstate_t *vstate)
754179193Sjb{
755179193Sjb	/*
756179193Sjb	 * First, check to see if the address is in scratch space...
757179193Sjb	 */
758179193Sjb	if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base,
759179193Sjb	    mstate->dtms_scratch_size))
760179193Sjb		return (1);
761179193Sjb
762179193Sjb	/*
763179193Sjb	 * Now check to see if it's a dynamic variable.  This check will pick
764179193Sjb	 * up both thread-local variables and any global dynamically-allocated
765179193Sjb	 * variables.
766179193Sjb	 */
767267929Srpaulo	if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base,
768179193Sjb	    vstate->dtvs_dynvars.dtds_size)) {
769179193Sjb		dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
770179193Sjb		uintptr_t base = (uintptr_t)dstate->dtds_base +
771179193Sjb		    (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t));
772179193Sjb		uintptr_t chunkoffs;
773296475Smarkj		dtrace_dynvar_t *dvar;
774179193Sjb
775179193Sjb		/*
776179193Sjb		 * Before we assume that we can store here, we need to make
777179193Sjb		 * sure that it isn't in our metadata -- storing to our
778179193Sjb		 * dynamic variable metadata would corrupt our state.  For
779179193Sjb		 * the range to not include any dynamic variable metadata,
780179193Sjb		 * it must:
781179193Sjb		 *
782179193Sjb		 *	(1) Start above the hash table that is at the base of
783179193Sjb		 *	the dynamic variable space
784179193Sjb		 *
785179193Sjb		 *	(2) Have a starting chunk offset that is beyond the
786179193Sjb		 *	dtrace_dynvar_t that is at the base of every chunk
787179193Sjb		 *
788179193Sjb		 *	(3) Not span a chunk boundary
789179193Sjb		 *
790296475Smarkj		 *	(4) Not be in the tuple space of a dynamic variable
791296475Smarkj		 *
792179193Sjb		 */
793179193Sjb		if (addr < base)
794179193Sjb			return (0);
795179193Sjb
796179193Sjb		chunkoffs = (addr - base) % dstate->dtds_chunksize;
797179193Sjb
798179193Sjb		if (chunkoffs < sizeof (dtrace_dynvar_t))
799179193Sjb			return (0);
800179193Sjb
801179193Sjb		if (chunkoffs + sz > dstate->dtds_chunksize)
802179193Sjb			return (0);
803179193Sjb
804296475Smarkj		dvar = (dtrace_dynvar_t *)((uintptr_t)addr - chunkoffs);
805296475Smarkj
806296475Smarkj		if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE)
807296475Smarkj			return (0);
808296475Smarkj
809296475Smarkj		if (chunkoffs < sizeof (dtrace_dynvar_t) +
810296475Smarkj		    ((dvar->dtdv_tuple.dtt_nkeys - 1) * sizeof (dtrace_key_t)))
811296475Smarkj			return (0);
812296475Smarkj
813179193Sjb		return (1);
814179193Sjb	}
815179193Sjb
816179193Sjb	/*
817179193Sjb	 * Finally, check the static local and global variables.  These checks
818179193Sjb	 * take the longest, so we perform them last.
819179193Sjb	 */
820179193Sjb	if (dtrace_canstore_statvar(addr, sz,
821179193Sjb	    vstate->dtvs_locals, vstate->dtvs_nlocals))
822179193Sjb		return (1);
823179193Sjb
824179193Sjb	if (dtrace_canstore_statvar(addr, sz,
825179193Sjb	    vstate->dtvs_globals, vstate->dtvs_nglobals))
826179193Sjb		return (1);
827179193Sjb
828179193Sjb	return (0);
829179193Sjb}
830179193Sjb
831179193Sjb
832179193Sjb/*
833179193Sjb * Convenience routine to check to see if the address is within a memory
834179193Sjb * region in which a load may be issued given the user's privilege level;
835179193Sjb * if not, it sets the appropriate error flags and loads 'addr' into the
836179193Sjb * illegal value slot.
837179193Sjb *
838179193Sjb * DTrace subroutines (DIF_SUBR_*) should use this helper to implement
839179193Sjb * appropriate memory access protection.
840179193Sjb */
841179193Sjbstatic int
842179193Sjbdtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
843179193Sjb    dtrace_vstate_t *vstate)
844179193Sjb{
845179198Sjb	volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
846267929Srpaulo	file_t *fp;
847179193Sjb
848179193Sjb	/*
849179193Sjb	 * If we hold the privilege to read from kernel memory, then
850179193Sjb	 * everything is readable.
851179193Sjb	 */
852179193Sjb	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
853179193Sjb		return (1);
854179193Sjb
855179193Sjb	/*
856179193Sjb	 * You can obviously read that which you can store.
857179193Sjb	 */
858179193Sjb	if (dtrace_canstore(addr, sz, mstate, vstate))
859179193Sjb		return (1);
860179193Sjb
861179193Sjb	/*
862179193Sjb	 * We're allowed to read from our own string table.
863179193Sjb	 */
864267929Srpaulo	if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab,
865179193Sjb	    mstate->dtms_difo->dtdo_strlen))
866179193Sjb		return (1);
867179193Sjb
868267929Srpaulo	if (vstate->dtvs_state != NULL &&
869267929Srpaulo	    dtrace_priv_proc(vstate->dtvs_state)) {
870267929Srpaulo		proc_t *p;
871267929Srpaulo
872267929Srpaulo		/*
873267929Srpaulo		 * When we have privileges to the current process, there are
874267929Srpaulo		 * several context-related kernel structures that are safe to
875267929Srpaulo		 * read, even absent the privilege to read from kernel memory.
876267929Srpaulo		 * These reads are safe because these structures contain only
877267929Srpaulo		 * state that (1) we're permitted to read, (2) is harmless or
878267929Srpaulo		 * (3) contains pointers to additional kernel state that we're
879267929Srpaulo		 * not permitted to read (and as such, do not present an
880267929Srpaulo		 * opportunity for privilege escalation).  Finally (and
881267929Srpaulo		 * critically), because of the nature of their relation with
882267929Srpaulo		 * the current thread context, the memory associated with these
883267929Srpaulo		 * structures cannot change over the duration of probe context,
884267929Srpaulo		 * and it is therefore impossible for this memory to be
885267929Srpaulo		 * deallocated and reallocated as something else while it's
886267929Srpaulo		 * being operated upon.
887267929Srpaulo		 */
888267929Srpaulo		if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t)))
889267929Srpaulo			return (1);
890267929Srpaulo
891267929Srpaulo		if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr,
892267929Srpaulo		    sz, curthread->t_procp, sizeof (proc_t))) {
893267929Srpaulo			return (1);
894267929Srpaulo		}
895267929Srpaulo
896267929Srpaulo		if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz,
897267929Srpaulo		    curthread->t_cred, sizeof (cred_t))) {
898267929Srpaulo			return (1);
899267929Srpaulo		}
900267929Srpaulo
901277300Ssmh#ifdef illumos
902267929Srpaulo		if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz,
903267929Srpaulo		    &(p->p_pidp->pid_id), sizeof (pid_t))) {
904267929Srpaulo			return (1);
905267929Srpaulo		}
906267929Srpaulo
907267929Srpaulo		if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz,
908267929Srpaulo		    curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) {
909267929Srpaulo			return (1);
910267929Srpaulo		}
911267929Srpaulo#endif
912267929Srpaulo	}
913267929Srpaulo
914267929Srpaulo	if ((fp = mstate->dtms_getf) != NULL) {
915267929Srpaulo		uintptr_t psz = sizeof (void *);
916267929Srpaulo		vnode_t *vp;
917267929Srpaulo		vnodeops_t *op;
918267929Srpaulo
919267929Srpaulo		/*
920267929Srpaulo		 * When getf() returns a file_t, the enabling is implicitly
921267929Srpaulo		 * granted the (transient) right to read the returned file_t
922267929Srpaulo		 * as well as the v_path and v_op->vnop_name of the underlying
923267929Srpaulo		 * vnode.  These accesses are allowed after a successful
924267929Srpaulo		 * getf() because the members that they refer to cannot change
925267929Srpaulo		 * once set -- and the barrier logic in the kernel's closef()
926267929Srpaulo		 * path assures that the file_t and its referenced vode_t
927267929Srpaulo		 * cannot themselves be stale (that is, it impossible for
928267929Srpaulo		 * either dtms_getf itself or its f_vnode member to reference
929267929Srpaulo		 * freed memory).
930267929Srpaulo		 */
931267929Srpaulo		if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t)))
932267929Srpaulo			return (1);
933267929Srpaulo
934267929Srpaulo		if ((vp = fp->f_vnode) != NULL) {
935277300Ssmh#ifdef illumos
936267929Srpaulo			if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz))
937267929Srpaulo				return (1);
938267929Srpaulo			if (vp->v_path != NULL && DTRACE_INRANGE(addr, sz,
939267929Srpaulo			    vp->v_path, strlen(vp->v_path) + 1)) {
940267929Srpaulo				return (1);
941267929Srpaulo			}
942267929Srpaulo#endif
943267929Srpaulo
944267929Srpaulo			if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz))
945267929Srpaulo				return (1);
946267929Srpaulo
947277300Ssmh#ifdef illumos
948267929Srpaulo			if ((op = vp->v_op) != NULL &&
949267929Srpaulo			    DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) {
950267929Srpaulo				return (1);
951267929Srpaulo			}
952267929Srpaulo
953267929Srpaulo			if (op != NULL && op->vnop_name != NULL &&
954267929Srpaulo			    DTRACE_INRANGE(addr, sz, op->vnop_name,
955267929Srpaulo			    strlen(op->vnop_name) + 1)) {
956267929Srpaulo				return (1);
957267929Srpaulo			}
958267929Srpaulo#endif
959267929Srpaulo		}
960267929Srpaulo	}
961267929Srpaulo
962179193Sjb	DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV);
963179193Sjb	*illval = addr;
964179193Sjb	return (0);
965179193Sjb}
966179193Sjb
967179193Sjb/*
968179193Sjb * Convenience routine to check to see if a given string is within a memory
969179193Sjb * region in which a load may be issued given the user's privilege level;
970179193Sjb * this exists so that we don't need to issue unnecessary dtrace_strlen()
971179193Sjb * calls in the event that the user has all privileges.
972179193Sjb */
973179193Sjbstatic int
974179193Sjbdtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
975179193Sjb    dtrace_vstate_t *vstate)
976179193Sjb{
977179193Sjb	size_t strsz;
978179193Sjb
979179193Sjb	/*
980179193Sjb	 * If we hold the privilege to read from kernel memory, then
981179193Sjb	 * everything is readable.
982179193Sjb	 */
983179193Sjb	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
984179193Sjb		return (1);
985179193Sjb
986179193Sjb	strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz);
987179193Sjb	if (dtrace_canload(addr, strsz, mstate, vstate))
988179193Sjb		return (1);
989179193Sjb
990179193Sjb	return (0);
991179193Sjb}
992179193Sjb
993179193Sjb/*
994179193Sjb * Convenience routine to check to see if a given variable is within a memory
995179193Sjb * region in which a load may be issued given the user's privilege level.
996179193Sjb */
997179193Sjbstatic int
998179193Sjbdtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate,
999179193Sjb    dtrace_vstate_t *vstate)
1000179193Sjb{
1001179193Sjb	size_t sz;
1002179193Sjb	ASSERT(type->dtdt_flags & DIF_TF_BYREF);
1003179193Sjb
1004179193Sjb	/*
1005179193Sjb	 * If we hold the privilege to read from kernel memory, then
1006179193Sjb	 * everything is readable.
1007179193Sjb	 */
1008179193Sjb	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
1009179193Sjb		return (1);
1010179193Sjb
1011179193Sjb	if (type->dtdt_kind == DIF_TYPE_STRING)
1012179193Sjb		sz = dtrace_strlen(src,
1013179193Sjb		    vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1;
1014179193Sjb	else
1015179193Sjb		sz = type->dtdt_size;
1016179193Sjb
1017179193Sjb	return (dtrace_canload((uintptr_t)src, sz, mstate, vstate));
1018179193Sjb}
1019179193Sjb
1020179193Sjb/*
1021267937Srpaulo * Convert a string to a signed integer using safe loads.
1022267937Srpaulo *
1023267937Srpaulo * NOTE: This function uses various macros from strtolctype.h to manipulate
1024267937Srpaulo * digit values, etc -- these have all been checked to ensure they make
1025267937Srpaulo * no additional function calls.
1026267937Srpaulo */
1027267937Srpaulostatic int64_t
1028267937Srpaulodtrace_strtoll(char *input, int base, size_t limit)
1029267937Srpaulo{
1030267937Srpaulo	uintptr_t pos = (uintptr_t)input;
1031267937Srpaulo	int64_t val = 0;
1032267937Srpaulo	int x;
1033267937Srpaulo	boolean_t neg = B_FALSE;
1034267937Srpaulo	char c, cc, ccc;
1035267937Srpaulo	uintptr_t end = pos + limit;
1036267937Srpaulo
1037267937Srpaulo	/*
1038267937Srpaulo	 * Consume any whitespace preceding digits.
1039267937Srpaulo	 */
1040267937Srpaulo	while ((c = dtrace_load8(pos)) == ' ' || c == '\t')
1041267937Srpaulo		pos++;
1042267937Srpaulo
1043267937Srpaulo	/*
1044267937Srpaulo	 * Handle an explicit sign if one is present.
1045267937Srpaulo	 */
1046267937Srpaulo	if (c == '-' || c == '+') {
1047267937Srpaulo		if (c == '-')
1048267937Srpaulo			neg = B_TRUE;
1049267937Srpaulo		c = dtrace_load8(++pos);
1050267937Srpaulo	}
1051267937Srpaulo
1052267937Srpaulo	/*
1053267937Srpaulo	 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it
1054267937Srpaulo	 * if present.
1055267937Srpaulo	 */
1056267937Srpaulo	if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' ||
1057267937Srpaulo	    cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) {
1058267937Srpaulo		pos += 2;
1059267937Srpaulo		c = ccc;
1060267937Srpaulo	}
1061267937Srpaulo
1062267937Srpaulo	/*
1063267937Srpaulo	 * Read in contiguous digits until the first non-digit character.
1064267937Srpaulo	 */
1065267937Srpaulo	for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base;
1066267937Srpaulo	    c = dtrace_load8(++pos))
1067267937Srpaulo		val = val * base + x;
1068267937Srpaulo
1069267937Srpaulo	return (neg ? -val : val);
1070267937Srpaulo}
1071267937Srpaulo
1072267937Srpaulo/*
1073179193Sjb * Compare two strings using safe loads.
1074179193Sjb */
1075179193Sjbstatic int
1076179193Sjbdtrace_strncmp(char *s1, char *s2, size_t limit)
1077179193Sjb{
1078179193Sjb	uint8_t c1, c2;
1079179193Sjb	volatile uint16_t *flags;
1080179193Sjb
1081179193Sjb	if (s1 == s2 || limit == 0)
1082179193Sjb		return (0);
1083179193Sjb
1084179198Sjb	flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
1085179193Sjb
1086179193Sjb	do {
1087179193Sjb		if (s1 == NULL) {
1088179193Sjb			c1 = '\0';
1089179193Sjb		} else {
1090179193Sjb			c1 = dtrace_load8((uintptr_t)s1++);
1091179193Sjb		}
1092179193Sjb
1093179193Sjb		if (s2 == NULL) {
1094179193Sjb			c2 = '\0';
1095179193Sjb		} else {
1096179193Sjb			c2 = dtrace_load8((uintptr_t)s2++);
1097179193Sjb		}
1098179193Sjb
1099179193Sjb		if (c1 != c2)
1100179193Sjb			return (c1 - c2);
1101179193Sjb	} while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT));
1102179193Sjb
1103179193Sjb	return (0);
1104179193Sjb}
1105179193Sjb
1106179193Sjb/*
1107179193Sjb * Compute strlen(s) for a string using safe memory accesses.  The additional
1108179193Sjb * len parameter is used to specify a maximum length to ensure completion.
1109179193Sjb */
1110179193Sjbstatic size_t
1111179193Sjbdtrace_strlen(const char *s, size_t lim)
1112179193Sjb{
1113179193Sjb	uint_t len;
1114179193Sjb
1115179193Sjb	for (len = 0; len != lim; len++) {
1116179193Sjb		if (dtrace_load8((uintptr_t)s++) == '\0')
1117179193Sjb			break;
1118179193Sjb	}
1119179193Sjb
1120179193Sjb	return (len);
1121179193Sjb}
1122179193Sjb
1123179193Sjb/*
1124179193Sjb * Check if an address falls within a toxic region.
1125179193Sjb */
1126179193Sjbstatic int
1127179193Sjbdtrace_istoxic(uintptr_t kaddr, size_t size)
1128179193Sjb{
1129179193Sjb	uintptr_t taddr, tsize;
1130179193Sjb	int i;
1131179193Sjb
1132179193Sjb	for (i = 0; i < dtrace_toxranges; i++) {
1133179193Sjb		taddr = dtrace_toxrange[i].dtt_base;
1134179193Sjb		tsize = dtrace_toxrange[i].dtt_limit - taddr;
1135179193Sjb
1136179193Sjb		if (kaddr - taddr < tsize) {
1137179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1138179198Sjb			cpu_core[curcpu].cpuc_dtrace_illval = kaddr;
1139179193Sjb			return (1);
1140179193Sjb		}
1141179193Sjb
1142179193Sjb		if (taddr - kaddr < size) {
1143179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
1144179198Sjb			cpu_core[curcpu].cpuc_dtrace_illval = taddr;
1145179193Sjb			return (1);
1146179193Sjb		}
1147179193Sjb	}
1148179193Sjb
1149179193Sjb	return (0);
1150179193Sjb}
1151179193Sjb
1152179193Sjb/*
1153179193Sjb * Copy src to dst using safe memory accesses.  The src is assumed to be unsafe
1154179193Sjb * memory specified by the DIF program.  The dst is assumed to be safe memory
1155179193Sjb * that we can store to directly because it is managed by DTrace.  As with
1156179193Sjb * standard bcopy, overlapping copies are handled properly.
1157179193Sjb */
1158179193Sjbstatic void
1159179193Sjbdtrace_bcopy(const void *src, void *dst, size_t len)
1160179193Sjb{
1161179193Sjb	if (len != 0) {
1162179193Sjb		uint8_t *s1 = dst;
1163179193Sjb		const uint8_t *s2 = src;
1164179193Sjb
1165179193Sjb		if (s1 <= s2) {
1166179193Sjb			do {
1167179193Sjb				*s1++ = dtrace_load8((uintptr_t)s2++);
1168179193Sjb			} while (--len != 0);
1169179193Sjb		} else {
1170179193Sjb			s2 += len;
1171179193Sjb			s1 += len;
1172179193Sjb
1173179193Sjb			do {
1174179193Sjb				*--s1 = dtrace_load8((uintptr_t)--s2);
1175179193Sjb			} while (--len != 0);
1176179193Sjb		}
1177179193Sjb	}
1178179193Sjb}
1179179193Sjb
1180179193Sjb/*
1181179193Sjb * Copy src to dst using safe memory accesses, up to either the specified
1182179193Sjb * length, or the point that a nul byte is encountered.  The src is assumed to
1183179193Sjb * be unsafe memory specified by the DIF program.  The dst is assumed to be
1184179193Sjb * safe memory that we can store to directly because it is managed by DTrace.
1185179193Sjb * Unlike dtrace_bcopy(), overlapping regions are not handled.
1186179193Sjb */
1187179193Sjbstatic void
1188179193Sjbdtrace_strcpy(const void *src, void *dst, size_t len)
1189179193Sjb{
1190179193Sjb	if (len != 0) {
1191179193Sjb		uint8_t *s1 = dst, c;
1192179193Sjb		const uint8_t *s2 = src;
1193179193Sjb
1194179193Sjb		do {
1195179193Sjb			*s1++ = c = dtrace_load8((uintptr_t)s2++);
1196179193Sjb		} while (--len != 0 && c != '\0');
1197179193Sjb	}
1198179193Sjb}
1199179193Sjb
1200179193Sjb/*
1201179193Sjb * Copy src to dst, deriving the size and type from the specified (BYREF)
1202179193Sjb * variable type.  The src is assumed to be unsafe memory specified by the DIF
1203179193Sjb * program.  The dst is assumed to be DTrace variable memory that is of the
1204179193Sjb * specified type; we assume that we can store to directly.
1205179193Sjb */
1206179193Sjbstatic void
1207179193Sjbdtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type)
1208179193Sjb{
1209179193Sjb	ASSERT(type->dtdt_flags & DIF_TF_BYREF);
1210179193Sjb
1211179193Sjb	if (type->dtdt_kind == DIF_TYPE_STRING) {
1212179193Sjb		dtrace_strcpy(src, dst, type->dtdt_size);
1213179193Sjb	} else {
1214179193Sjb		dtrace_bcopy(src, dst, type->dtdt_size);
1215179193Sjb	}
1216179193Sjb}
1217179193Sjb
1218179193Sjb/*
1219179193Sjb * Compare s1 to s2 using safe memory accesses.  The s1 data is assumed to be
1220179193Sjb * unsafe memory specified by the DIF program.  The s2 data is assumed to be
1221179193Sjb * safe memory that we can access directly because it is managed by DTrace.
1222179193Sjb */
1223179193Sjbstatic int
1224179193Sjbdtrace_bcmp(const void *s1, const void *s2, size_t len)
1225179193Sjb{
1226179193Sjb	volatile uint16_t *flags;
1227179193Sjb
1228179198Sjb	flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
1229179193Sjb
1230179193Sjb	if (s1 == s2)
1231179193Sjb		return (0);
1232179193Sjb
1233179193Sjb	if (s1 == NULL || s2 == NULL)
1234179193Sjb		return (1);
1235179193Sjb
1236179193Sjb	if (s1 != s2 && len != 0) {
1237179193Sjb		const uint8_t *ps1 = s1;
1238179193Sjb		const uint8_t *ps2 = s2;
1239179193Sjb
1240179193Sjb		do {
1241179193Sjb			if (dtrace_load8((uintptr_t)ps1++) != *ps2++)
1242179193Sjb				return (1);
1243179193Sjb		} while (--len != 0 && !(*flags & CPU_DTRACE_FAULT));
1244179193Sjb	}
1245179193Sjb	return (0);
1246179193Sjb}
1247179193Sjb
1248179193Sjb/*
1249179193Sjb * Zero the specified region using a simple byte-by-byte loop.  Note that this
1250179193Sjb * is for safe DTrace-managed memory only.
1251179193Sjb */
1252179193Sjbstatic void
1253179193Sjbdtrace_bzero(void *dst, size_t len)
1254179193Sjb{
1255179193Sjb	uchar_t *cp;
1256179193Sjb
1257179193Sjb	for (cp = dst; len != 0; len--)
1258179193Sjb		*cp++ = 0;
1259179193Sjb}
1260179193Sjb
1261179193Sjbstatic void
1262179193Sjbdtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
1263179193Sjb{
1264179193Sjb	uint64_t result[2];
1265179193Sjb
1266179193Sjb	result[0] = addend1[0] + addend2[0];
1267179193Sjb	result[1] = addend1[1] + addend2[1] +
1268179193Sjb	    (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
1269179193Sjb
1270179193Sjb	sum[0] = result[0];
1271179193Sjb	sum[1] = result[1];
1272179193Sjb}
1273179193Sjb
1274179193Sjb/*
1275179193Sjb * Shift the 128-bit value in a by b. If b is positive, shift left.
1276179193Sjb * If b is negative, shift right.
1277179193Sjb */
1278179193Sjbstatic void
1279179193Sjbdtrace_shift_128(uint64_t *a, int b)
1280179193Sjb{
1281179193Sjb	uint64_t mask;
1282179193Sjb
1283179193Sjb	if (b == 0)
1284179193Sjb		return;
1285179193Sjb
1286179193Sjb	if (b < 0) {
1287179193Sjb		b = -b;
1288179193Sjb		if (b >= 64) {
1289179193Sjb			a[0] = a[1] >> (b - 64);
1290179193Sjb			a[1] = 0;
1291179193Sjb		} else {
1292179193Sjb			a[0] >>= b;
1293179193Sjb			mask = 1LL << (64 - b);
1294179193Sjb			mask -= 1;
1295179193Sjb			a[0] |= ((a[1] & mask) << (64 - b));
1296179193Sjb			a[1] >>= b;
1297179193Sjb		}
1298179193Sjb	} else {
1299179193Sjb		if (b >= 64) {
1300179193Sjb			a[1] = a[0] << (b - 64);
1301179193Sjb			a[0] = 0;
1302179193Sjb		} else {
1303179193Sjb			a[1] <<= b;
1304179193Sjb			mask = a[0] >> (64 - b);
1305179193Sjb			a[1] |= mask;
1306179193Sjb			a[0] <<= b;
1307179193Sjb		}
1308179193Sjb	}
1309179193Sjb}
1310179193Sjb
1311179193Sjb/*
1312179193Sjb * The basic idea is to break the 2 64-bit values into 4 32-bit values,
1313179193Sjb * use native multiplication on those, and then re-combine into the
1314179193Sjb * resulting 128-bit value.
1315179193Sjb *
1316179193Sjb * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
1317179193Sjb *     hi1 * hi2 << 64 +
1318179193Sjb *     hi1 * lo2 << 32 +
1319179193Sjb *     hi2 * lo1 << 32 +
1320179193Sjb *     lo1 * lo2
1321179193Sjb */
1322179193Sjbstatic void
1323179193Sjbdtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
1324179193Sjb{
1325179193Sjb	uint64_t hi1, hi2, lo1, lo2;
1326179193Sjb	uint64_t tmp[2];
1327179193Sjb
1328179193Sjb	hi1 = factor1 >> 32;
1329179193Sjb	hi2 = factor2 >> 32;
1330179193Sjb
1331179193Sjb	lo1 = factor1 & DT_MASK_LO;
1332179193Sjb	lo2 = factor2 & DT_MASK_LO;
1333179193Sjb
1334179193Sjb	product[0] = lo1 * lo2;
1335179193Sjb	product[1] = hi1 * hi2;
1336179193Sjb
1337179193Sjb	tmp[0] = hi1 * lo2;
1338179193Sjb	tmp[1] = 0;
1339179193Sjb	dtrace_shift_128(tmp, 32);
1340179193Sjb	dtrace_add_128(product, tmp, product);
1341179193Sjb
1342179193Sjb	tmp[0] = hi2 * lo1;
1343179193Sjb	tmp[1] = 0;
1344179193Sjb	dtrace_shift_128(tmp, 32);
1345179193Sjb	dtrace_add_128(product, tmp, product);
1346179193Sjb}
1347179193Sjb
1348179193Sjb/*
1349179193Sjb * This privilege check should be used by actions and subroutines to
1350179193Sjb * verify that the user credentials of the process that enabled the
1351179193Sjb * invoking ECB match the target credentials
1352179193Sjb */
1353179193Sjbstatic int
1354179193Sjbdtrace_priv_proc_common_user(dtrace_state_t *state)
1355179193Sjb{
1356179193Sjb	cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1357179193Sjb
1358179193Sjb	/*
1359179193Sjb	 * We should always have a non-NULL state cred here, since if cred
1360179193Sjb	 * is null (anonymous tracing), we fast-path bypass this routine.
1361179193Sjb	 */
1362179193Sjb	ASSERT(s_cr != NULL);
1363179193Sjb
1364179193Sjb	if ((cr = CRED()) != NULL &&
1365179193Sjb	    s_cr->cr_uid == cr->cr_uid &&
1366179193Sjb	    s_cr->cr_uid == cr->cr_ruid &&
1367179193Sjb	    s_cr->cr_uid == cr->cr_suid &&
1368179193Sjb	    s_cr->cr_gid == cr->cr_gid &&
1369179193Sjb	    s_cr->cr_gid == cr->cr_rgid &&
1370179193Sjb	    s_cr->cr_gid == cr->cr_sgid)
1371179193Sjb		return (1);
1372179193Sjb
1373179193Sjb	return (0);
1374179193Sjb}
1375179193Sjb
1376179193Sjb/*
1377179193Sjb * This privilege check should be used by actions and subroutines to
1378179193Sjb * verify that the zone of the process that enabled the invoking ECB
1379179193Sjb * matches the target credentials
1380179193Sjb */
1381179193Sjbstatic int
1382179193Sjbdtrace_priv_proc_common_zone(dtrace_state_t *state)
1383179193Sjb{
1384277300Ssmh#ifdef illumos
1385179193Sjb	cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
1386179193Sjb
1387179193Sjb	/*
1388179193Sjb	 * We should always have a non-NULL state cred here, since if cred
1389179193Sjb	 * is null (anonymous tracing), we fast-path bypass this routine.
1390179193Sjb	 */
1391179193Sjb	ASSERT(s_cr != NULL);
1392179193Sjb
1393267929Srpaulo	if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone)
1394179193Sjb		return (1);
1395179193Sjb
1396179193Sjb	return (0);
1397179198Sjb#else
1398179198Sjb	return (1);
1399179198Sjb#endif
1400179193Sjb}
1401179193Sjb
1402179193Sjb/*
1403179193Sjb * This privilege check should be used by actions and subroutines to
1404179193Sjb * verify that the process has not setuid or changed credentials.
1405179193Sjb */
1406179193Sjbstatic int
1407179198Sjbdtrace_priv_proc_common_nocd(void)
1408179193Sjb{
1409179193Sjb	proc_t *proc;
1410179193Sjb
1411179193Sjb	if ((proc = ttoproc(curthread)) != NULL &&
1412179193Sjb	    !(proc->p_flag & SNOCD))
1413179193Sjb		return (1);
1414179193Sjb
1415179193Sjb	return (0);
1416179193Sjb}
1417179193Sjb
1418179193Sjbstatic int
1419179193Sjbdtrace_priv_proc_destructive(dtrace_state_t *state)
1420179193Sjb{
1421179193Sjb	int action = state->dts_cred.dcr_action;
1422179193Sjb
1423179193Sjb	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) &&
1424179193Sjb	    dtrace_priv_proc_common_zone(state) == 0)
1425179193Sjb		goto bad;
1426179193Sjb
1427179193Sjb	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) &&
1428179193Sjb	    dtrace_priv_proc_common_user(state) == 0)
1429179193Sjb		goto bad;
1430179193Sjb
1431179193Sjb	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) &&
1432179193Sjb	    dtrace_priv_proc_common_nocd() == 0)
1433179193Sjb		goto bad;
1434179193Sjb
1435179193Sjb	return (1);
1436179193Sjb
1437179193Sjbbad:
1438179198Sjb	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1439179193Sjb
1440179193Sjb	return (0);
1441179193Sjb}
1442179193Sjb
1443179193Sjbstatic int
1444179193Sjbdtrace_priv_proc_control(dtrace_state_t *state)
1445179193Sjb{
1446179193Sjb	if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL)
1447179193Sjb		return (1);
1448179193Sjb
1449179193Sjb	if (dtrace_priv_proc_common_zone(state) &&
1450179193Sjb	    dtrace_priv_proc_common_user(state) &&
1451179193Sjb	    dtrace_priv_proc_common_nocd())
1452179193Sjb		return (1);
1453179193Sjb
1454179198Sjb	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1455179193Sjb
1456179193Sjb	return (0);
1457179193Sjb}
1458179193Sjb
1459179193Sjbstatic int
1460179193Sjbdtrace_priv_proc(dtrace_state_t *state)
1461179193Sjb{
1462179193Sjb	if (state->dts_cred.dcr_action & DTRACE_CRA_PROC)
1463179193Sjb		return (1);
1464179193Sjb
1465179198Sjb	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
1466179193Sjb
1467179193Sjb	return (0);
1468179193Sjb}
1469179193Sjb
1470179193Sjbstatic int
1471179193Sjbdtrace_priv_kernel(dtrace_state_t *state)
1472179193Sjb{
1473179193Sjb	if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL)
1474179193Sjb		return (1);
1475179193Sjb
1476179198Sjb	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1477179193Sjb
1478179193Sjb	return (0);
1479179193Sjb}
1480179193Sjb
1481179193Sjbstatic int
1482179193Sjbdtrace_priv_kernel_destructive(dtrace_state_t *state)
1483179193Sjb{
1484179193Sjb	if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE)
1485179193Sjb		return (1);
1486179193Sjb
1487179198Sjb	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
1488179193Sjb
1489179193Sjb	return (0);
1490179193Sjb}
1491179193Sjb
1492179193Sjb/*
1493267929Srpaulo * Determine if the dte_cond of the specified ECB allows for processing of
1494267929Srpaulo * the current probe to continue.  Note that this routine may allow continued
1495267929Srpaulo * processing, but with access(es) stripped from the mstate's dtms_access
1496267929Srpaulo * field.
1497267929Srpaulo */
1498267929Srpaulostatic int
1499267929Srpaulodtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate,
1500267929Srpaulo    dtrace_ecb_t *ecb)
1501267929Srpaulo{
1502267929Srpaulo	dtrace_probe_t *probe = ecb->dte_probe;
1503267929Srpaulo	dtrace_provider_t *prov = probe->dtpr_provider;
1504267929Srpaulo	dtrace_pops_t *pops = &prov->dtpv_pops;
1505267929Srpaulo	int mode = DTRACE_MODE_NOPRIV_DROP;
1506267929Srpaulo
1507267929Srpaulo	ASSERT(ecb->dte_cond);
1508267929Srpaulo
1509277300Ssmh#ifdef illumos
1510267929Srpaulo	if (pops->dtps_mode != NULL) {
1511267929Srpaulo		mode = pops->dtps_mode(prov->dtpv_arg,
1512267929Srpaulo		    probe->dtpr_id, probe->dtpr_arg);
1513267929Srpaulo
1514267929Srpaulo		ASSERT((mode & DTRACE_MODE_USER) ||
1515267929Srpaulo		    (mode & DTRACE_MODE_KERNEL));
1516267929Srpaulo		ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) ||
1517267929Srpaulo		    (mode & DTRACE_MODE_NOPRIV_DROP));
1518267929Srpaulo	}
1519267929Srpaulo
1520267929Srpaulo	/*
1521267929Srpaulo	 * If the dte_cond bits indicate that this consumer is only allowed to
1522267929Srpaulo	 * see user-mode firings of this probe, call the provider's dtps_mode()
1523267929Srpaulo	 * entry point to check that the probe was fired while in a user
1524267929Srpaulo	 * context.  If that's not the case, use the policy specified by the
1525267929Srpaulo	 * provider to determine if we drop the probe or merely restrict
1526267929Srpaulo	 * operation.
1527267929Srpaulo	 */
1528267929Srpaulo	if (ecb->dte_cond & DTRACE_COND_USERMODE) {
1529267929Srpaulo		ASSERT(mode != DTRACE_MODE_NOPRIV_DROP);
1530267929Srpaulo
1531267929Srpaulo		if (!(mode & DTRACE_MODE_USER)) {
1532267929Srpaulo			if (mode & DTRACE_MODE_NOPRIV_DROP)
1533267929Srpaulo				return (0);
1534267929Srpaulo
1535267929Srpaulo			mstate->dtms_access &= ~DTRACE_ACCESS_ARGS;
1536267929Srpaulo		}
1537267929Srpaulo	}
1538267929Srpaulo#endif
1539267929Srpaulo
1540267929Srpaulo	/*
1541267929Srpaulo	 * This is more subtle than it looks. We have to be absolutely certain
1542267929Srpaulo	 * that CRED() isn't going to change out from under us so it's only
1543267929Srpaulo	 * legit to examine that structure if we're in constrained situations.
1544267929Srpaulo	 * Currently, the only times we'll this check is if a non-super-user
1545267929Srpaulo	 * has enabled the profile or syscall providers -- providers that
1546267929Srpaulo	 * allow visibility of all processes. For the profile case, the check
1547267929Srpaulo	 * above will ensure that we're examining a user context.
1548267929Srpaulo	 */
1549267929Srpaulo	if (ecb->dte_cond & DTRACE_COND_OWNER) {
1550267929Srpaulo		cred_t *cr;
1551267929Srpaulo		cred_t *s_cr = state->dts_cred.dcr_cred;
1552267929Srpaulo		proc_t *proc;
1553267929Srpaulo
1554267929Srpaulo		ASSERT(s_cr != NULL);
1555267929Srpaulo
1556267929Srpaulo		if ((cr = CRED()) == NULL ||
1557267929Srpaulo		    s_cr->cr_uid != cr->cr_uid ||
1558267929Srpaulo		    s_cr->cr_uid != cr->cr_ruid ||
1559267929Srpaulo		    s_cr->cr_uid != cr->cr_suid ||
1560267929Srpaulo		    s_cr->cr_gid != cr->cr_gid ||
1561267929Srpaulo		    s_cr->cr_gid != cr->cr_rgid ||
1562267929Srpaulo		    s_cr->cr_gid != cr->cr_sgid ||
1563267929Srpaulo		    (proc = ttoproc(curthread)) == NULL ||
1564267929Srpaulo		    (proc->p_flag & SNOCD)) {
1565267929Srpaulo			if (mode & DTRACE_MODE_NOPRIV_DROP)
1566267929Srpaulo				return (0);
1567267929Srpaulo
1568277300Ssmh#ifdef illumos
1569267929Srpaulo			mstate->dtms_access &= ~DTRACE_ACCESS_PROC;
1570267929Srpaulo#endif
1571267929Srpaulo		}
1572267929Srpaulo	}
1573267929Srpaulo
1574277300Ssmh#ifdef illumos
1575267929Srpaulo	/*
1576267929Srpaulo	 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not
1577267929Srpaulo	 * in our zone, check to see if our mode policy is to restrict rather
1578267929Srpaulo	 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC
1579267929Srpaulo	 * and DTRACE_ACCESS_ARGS
1580267929Srpaulo	 */
1581267929Srpaulo	if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) {
1582267929Srpaulo		cred_t *cr;
1583267929Srpaulo		cred_t *s_cr = state->dts_cred.dcr_cred;
1584267929Srpaulo
1585267929Srpaulo		ASSERT(s_cr != NULL);
1586267929Srpaulo
1587267929Srpaulo		if ((cr = CRED()) == NULL ||
1588267929Srpaulo		    s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) {
1589267929Srpaulo			if (mode & DTRACE_MODE_NOPRIV_DROP)
1590267929Srpaulo				return (0);
1591267929Srpaulo
1592267929Srpaulo			mstate->dtms_access &=
1593267929Srpaulo			    ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS);
1594267929Srpaulo		}
1595267929Srpaulo	}
1596267929Srpaulo#endif
1597267929Srpaulo
1598267929Srpaulo	return (1);
1599267929Srpaulo}
1600267929Srpaulo
1601267929Srpaulo/*
1602179193Sjb * Note:  not called from probe context.  This function is called
1603179193Sjb * asynchronously (and at a regular interval) from outside of probe context to
1604179193Sjb * clean the dirty dynamic variable lists on all CPUs.  Dynamic variable
1605179193Sjb * cleaning is explained in detail in <sys/dtrace_impl.h>.
1606179193Sjb */
1607179193Sjbvoid
1608179193Sjbdtrace_dynvar_clean(dtrace_dstate_t *dstate)
1609179193Sjb{
1610179193Sjb	dtrace_dynvar_t *dirty;
1611179193Sjb	dtrace_dstate_percpu_t *dcpu;
1612268290Spfg	dtrace_dynvar_t **rinsep;
1613268290Spfg	int i, j, work = 0;
1614179193Sjb
1615179193Sjb	for (i = 0; i < NCPU; i++) {
1616179193Sjb		dcpu = &dstate->dtds_percpu[i];
1617268290Spfg		rinsep = &dcpu->dtdsc_rinsing;
1618179193Sjb
1619179193Sjb		/*
1620179193Sjb		 * If the dirty list is NULL, there is no dirty work to do.
1621179193Sjb		 */
1622179193Sjb		if (dcpu->dtdsc_dirty == NULL)
1623179193Sjb			continue;
1624179193Sjb
1625268290Spfg		if (dcpu->dtdsc_rinsing != NULL) {
1626268290Spfg			/*
1627268290Spfg			 * If the rinsing list is non-NULL, then it is because
1628268290Spfg			 * this CPU was selected to accept another CPU's
1629268290Spfg			 * dirty list -- and since that time, dirty buffers
1630268290Spfg			 * have accumulated.  This is a highly unlikely
1631268290Spfg			 * condition, but we choose to ignore the dirty
1632268290Spfg			 * buffers -- they'll be picked up a future cleanse.
1633268290Spfg			 */
1634179193Sjb			continue;
1635268290Spfg		}
1636179193Sjb
1637268290Spfg		if (dcpu->dtdsc_clean != NULL) {
1638268290Spfg			/*
1639268290Spfg			 * If the clean list is non-NULL, then we're in a
1640268290Spfg			 * situation where a CPU has done deallocations (we
1641268290Spfg			 * have a non-NULL dirty list) but no allocations (we
1642268290Spfg			 * also have a non-NULL clean list).  We can't simply
1643268290Spfg			 * move the dirty list into the clean list on this
1644268290Spfg			 * CPU, yet we also don't want to allow this condition
1645268290Spfg			 * to persist, lest a short clean list prevent a
1646268290Spfg			 * massive dirty list from being cleaned (which in
1647268290Spfg			 * turn could lead to otherwise avoidable dynamic
1648268290Spfg			 * drops).  To deal with this, we look for some CPU
1649268290Spfg			 * with a NULL clean list, NULL dirty list, and NULL
1650268290Spfg			 * rinsing list -- and then we borrow this CPU to
1651268290Spfg			 * rinse our dirty list.
1652268290Spfg			 */
1653268290Spfg			for (j = 0; j < NCPU; j++) {
1654268290Spfg				dtrace_dstate_percpu_t *rinser;
1655268290Spfg
1656268290Spfg				rinser = &dstate->dtds_percpu[j];
1657268290Spfg
1658268290Spfg				if (rinser->dtdsc_rinsing != NULL)
1659268290Spfg					continue;
1660268290Spfg
1661268290Spfg				if (rinser->dtdsc_dirty != NULL)
1662268290Spfg					continue;
1663268290Spfg
1664268290Spfg				if (rinser->dtdsc_clean != NULL)
1665268290Spfg					continue;
1666268290Spfg
1667268290Spfg				rinsep = &rinser->dtdsc_rinsing;
1668268290Spfg				break;
1669268290Spfg			}
1670268290Spfg
1671268290Spfg			if (j == NCPU) {
1672268290Spfg				/*
1673268290Spfg				 * We were unable to find another CPU that
1674268290Spfg				 * could accept this dirty list -- we are
1675268290Spfg				 * therefore unable to clean it now.
1676268290Spfg				 */
1677268290Spfg				dtrace_dynvar_failclean++;
1678268290Spfg				continue;
1679268290Spfg			}
1680268290Spfg		}
1681268290Spfg
1682179193Sjb		work = 1;
1683179193Sjb
1684179193Sjb		/*
1685179193Sjb		 * Atomically move the dirty list aside.
1686179193Sjb		 */
1687179193Sjb		do {
1688179193Sjb			dirty = dcpu->dtdsc_dirty;
1689179193Sjb
1690179193Sjb			/*
1691179193Sjb			 * Before we zap the dirty list, set the rinsing list.
1692179193Sjb			 * (This allows for a potential assertion in
1693179193Sjb			 * dtrace_dynvar():  if a free dynamic variable appears
1694179193Sjb			 * on a hash chain, either the dirty list or the
1695179193Sjb			 * rinsing list for some CPU must be non-NULL.)
1696179193Sjb			 */
1697268290Spfg			*rinsep = dirty;
1698179193Sjb			dtrace_membar_producer();
1699179193Sjb		} while (dtrace_casptr(&dcpu->dtdsc_dirty,
1700179193Sjb		    dirty, NULL) != dirty);
1701179193Sjb	}
1702179193Sjb
1703179193Sjb	if (!work) {
1704179193Sjb		/*
1705179193Sjb		 * We have no work to do; we can simply return.
1706179193Sjb		 */
1707179193Sjb		return;
1708179193Sjb	}
1709179193Sjb
1710179193Sjb	dtrace_sync();
1711179193Sjb
1712179193Sjb	for (i = 0; i < NCPU; i++) {
1713179193Sjb		dcpu = &dstate->dtds_percpu[i];
1714179193Sjb
1715179193Sjb		if (dcpu->dtdsc_rinsing == NULL)
1716179193Sjb			continue;
1717179193Sjb
1718179193Sjb		/*
1719179193Sjb		 * We are now guaranteed that no hash chain contains a pointer
1720179193Sjb		 * into this dirty list; we can make it clean.
1721179193Sjb		 */
1722179193Sjb		ASSERT(dcpu->dtdsc_clean == NULL);
1723179193Sjb		dcpu->dtdsc_clean = dcpu->dtdsc_rinsing;
1724179193Sjb		dcpu->dtdsc_rinsing = NULL;
1725179193Sjb	}
1726179193Sjb
1727179193Sjb	/*
1728179193Sjb	 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make
1729179193Sjb	 * sure that all CPUs have seen all of the dtdsc_clean pointers.
1730179193Sjb	 * This prevents a race whereby a CPU incorrectly decides that
1731179193Sjb	 * the state should be something other than DTRACE_DSTATE_CLEAN
1732179193Sjb	 * after dtrace_dynvar_clean() has completed.
1733179193Sjb	 */
1734179193Sjb	dtrace_sync();
1735179193Sjb
1736179193Sjb	dstate->dtds_state = DTRACE_DSTATE_CLEAN;
1737179193Sjb}
1738179193Sjb
1739179193Sjb/*
1740179193Sjb * Depending on the value of the op parameter, this function looks-up,
1741179193Sjb * allocates or deallocates an arbitrarily-keyed dynamic variable.  If an
1742179193Sjb * allocation is requested, this function will return a pointer to a
1743179193Sjb * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no
1744179193Sjb * variable can be allocated.  If NULL is returned, the appropriate counter
1745179193Sjb * will be incremented.
1746179193Sjb */
1747179193Sjbdtrace_dynvar_t *
1748179193Sjbdtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys,
1749179193Sjb    dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op,
1750179193Sjb    dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
1751179193Sjb{
1752179193Sjb	uint64_t hashval = DTRACE_DYNHASH_VALID;
1753179193Sjb	dtrace_dynhash_t *hash = dstate->dtds_hash;
1754179193Sjb	dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL;
1755179198Sjb	processorid_t me = curcpu, cpu = me;
1756179193Sjb	dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me];
1757179193Sjb	size_t bucket, ksize;
1758179193Sjb	size_t chunksize = dstate->dtds_chunksize;
1759179193Sjb	uintptr_t kdata, lock, nstate;
1760179193Sjb	uint_t i;
1761179193Sjb
1762179193Sjb	ASSERT(nkeys != 0);
1763179193Sjb
1764179193Sjb	/*
1765179193Sjb	 * Hash the key.  As with aggregations, we use Jenkins' "One-at-a-time"
1766179193Sjb	 * algorithm.  For the by-value portions, we perform the algorithm in
1767179193Sjb	 * 16-bit chunks (as opposed to 8-bit chunks).  This speeds things up a
1768179193Sjb	 * bit, and seems to have only a minute effect on distribution.  For
1769179193Sjb	 * the by-reference data, we perform "One-at-a-time" iterating (safely)
1770179193Sjb	 * over each referenced byte.  It's painful to do this, but it's much
1771179193Sjb	 * better than pathological hash distribution.  The efficacy of the
1772179193Sjb	 * hashing algorithm (and a comparison with other algorithms) may be
1773179193Sjb	 * found by running the ::dtrace_dynstat MDB dcmd.
1774179193Sjb	 */
1775179193Sjb	for (i = 0; i < nkeys; i++) {
1776179193Sjb		if (key[i].dttk_size == 0) {
1777179193Sjb			uint64_t val = key[i].dttk_value;
1778179193Sjb
1779179193Sjb			hashval += (val >> 48) & 0xffff;
1780179193Sjb			hashval += (hashval << 10);
1781179193Sjb			hashval ^= (hashval >> 6);
1782179193Sjb
1783179193Sjb			hashval += (val >> 32) & 0xffff;
1784179193Sjb			hashval += (hashval << 10);
1785179193Sjb			hashval ^= (hashval >> 6);
1786179193Sjb
1787179193Sjb			hashval += (val >> 16) & 0xffff;
1788179193Sjb			hashval += (hashval << 10);
1789179193Sjb			hashval ^= (hashval >> 6);
1790179193Sjb
1791179193Sjb			hashval += val & 0xffff;
1792179193Sjb			hashval += (hashval << 10);
1793179193Sjb			hashval ^= (hashval >> 6);
1794179193Sjb		} else {
1795179193Sjb			/*
1796179193Sjb			 * This is incredibly painful, but it beats the hell
1797179193Sjb			 * out of the alternative.
1798179193Sjb			 */
1799179193Sjb			uint64_t j, size = key[i].dttk_size;
1800179193Sjb			uintptr_t base = (uintptr_t)key[i].dttk_value;
1801179193Sjb
1802179193Sjb			if (!dtrace_canload(base, size, mstate, vstate))
1803179193Sjb				break;
1804179193Sjb
1805179193Sjb			for (j = 0; j < size; j++) {
1806179193Sjb				hashval += dtrace_load8(base + j);
1807179193Sjb				hashval += (hashval << 10);
1808179193Sjb				hashval ^= (hashval >> 6);
1809179193Sjb			}
1810179193Sjb		}
1811179193Sjb	}
1812179193Sjb
1813179193Sjb	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
1814179193Sjb		return (NULL);
1815179193Sjb
1816179193Sjb	hashval += (hashval << 3);
1817179193Sjb	hashval ^= (hashval >> 11);
1818179193Sjb	hashval += (hashval << 15);
1819179193Sjb
1820179193Sjb	/*
1821179193Sjb	 * There is a remote chance (ideally, 1 in 2^31) that our hashval
1822179193Sjb	 * comes out to be one of our two sentinel hash values.  If this
1823179193Sjb	 * actually happens, we set the hashval to be a value known to be a
1824179193Sjb	 * non-sentinel value.
1825179193Sjb	 */
1826179193Sjb	if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK)
1827179193Sjb		hashval = DTRACE_DYNHASH_VALID;
1828179193Sjb
1829179193Sjb	/*
1830179193Sjb	 * Yes, it's painful to do a divide here.  If the cycle count becomes
1831179193Sjb	 * important here, tricks can be pulled to reduce it.  (However, it's
1832179193Sjb	 * critical that hash collisions be kept to an absolute minimum;
1833179193Sjb	 * they're much more painful than a divide.)  It's better to have a
1834179193Sjb	 * solution that generates few collisions and still keeps things
1835179193Sjb	 * relatively simple.
1836179193Sjb	 */
1837179193Sjb	bucket = hashval % dstate->dtds_hashsize;
1838179193Sjb
1839179193Sjb	if (op == DTRACE_DYNVAR_DEALLOC) {
1840179193Sjb		volatile uintptr_t *lockp = &hash[bucket].dtdh_lock;
1841179193Sjb
1842179193Sjb		for (;;) {
1843179193Sjb			while ((lock = *lockp) & 1)
1844179193Sjb				continue;
1845179193Sjb
1846179198Sjb			if (dtrace_casptr((volatile void *)lockp,
1847179198Sjb			    (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock)
1848179193Sjb				break;
1849179193Sjb		}
1850179193Sjb
1851179193Sjb		dtrace_membar_producer();
1852179193Sjb	}
1853179193Sjb
1854179193Sjbtop:
1855179193Sjb	prev = NULL;
1856179193Sjb	lock = hash[bucket].dtdh_lock;
1857179193Sjb
1858179193Sjb	dtrace_membar_consumer();
1859179193Sjb
1860179193Sjb	start = hash[bucket].dtdh_chain;
1861179193Sjb	ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK ||
1862179193Sjb	    start->dtdv_hashval != DTRACE_DYNHASH_FREE ||
1863179193Sjb	    op != DTRACE_DYNVAR_DEALLOC));
1864179193Sjb
1865179193Sjb	for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) {
1866179193Sjb		dtrace_tuple_t *dtuple = &dvar->dtdv_tuple;
1867179193Sjb		dtrace_key_t *dkey = &dtuple->dtt_key[0];
1868179193Sjb
1869179193Sjb		if (dvar->dtdv_hashval != hashval) {
1870179193Sjb			if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) {
1871179193Sjb				/*
1872179193Sjb				 * We've reached the sink, and therefore the
1873179193Sjb				 * end of the hash chain; we can kick out of
1874179193Sjb				 * the loop knowing that we have seen a valid
1875179193Sjb				 * snapshot of state.
1876179193Sjb				 */
1877179193Sjb				ASSERT(dvar->dtdv_next == NULL);
1878179193Sjb				ASSERT(dvar == &dtrace_dynhash_sink);
1879179193Sjb				break;
1880179193Sjb			}
1881179193Sjb
1882179193Sjb			if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) {
1883179193Sjb				/*
1884179193Sjb				 * We've gone off the rails:  somewhere along
1885179193Sjb				 * the line, one of the members of this hash
1886179193Sjb				 * chain was deleted.  Note that we could also
1887179193Sjb				 * detect this by simply letting this loop run
1888179193Sjb				 * to completion, as we would eventually hit
1889179193Sjb				 * the end of the dirty list.  However, we
1890179193Sjb				 * want to avoid running the length of the
1891179193Sjb				 * dirty list unnecessarily (it might be quite
1892179193Sjb				 * long), so we catch this as early as
1893179193Sjb				 * possible by detecting the hash marker.  In
1894179193Sjb				 * this case, we simply set dvar to NULL and
1895179193Sjb				 * break; the conditional after the loop will
1896179193Sjb				 * send us back to top.
1897179193Sjb				 */
1898179193Sjb				dvar = NULL;
1899179193Sjb				break;
1900179193Sjb			}
1901179193Sjb
1902179193Sjb			goto next;
1903179193Sjb		}
1904179193Sjb
1905179193Sjb		if (dtuple->dtt_nkeys != nkeys)
1906179193Sjb			goto next;
1907179193Sjb
1908179193Sjb		for (i = 0; i < nkeys; i++, dkey++) {
1909179193Sjb			if (dkey->dttk_size != key[i].dttk_size)
1910179193Sjb				goto next; /* size or type mismatch */
1911179193Sjb
1912179193Sjb			if (dkey->dttk_size != 0) {
1913179193Sjb				if (dtrace_bcmp(
1914179193Sjb				    (void *)(uintptr_t)key[i].dttk_value,
1915179193Sjb				    (void *)(uintptr_t)dkey->dttk_value,
1916179193Sjb				    dkey->dttk_size))
1917179193Sjb					goto next;
1918179193Sjb			} else {
1919179193Sjb				if (dkey->dttk_value != key[i].dttk_value)
1920179193Sjb					goto next;
1921179193Sjb			}
1922179193Sjb		}
1923179193Sjb
1924179193Sjb		if (op != DTRACE_DYNVAR_DEALLOC)
1925179193Sjb			return (dvar);
1926179193Sjb
1927179193Sjb		ASSERT(dvar->dtdv_next == NULL ||
1928179193Sjb		    dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE);
1929179193Sjb
1930179193Sjb		if (prev != NULL) {
1931179193Sjb			ASSERT(hash[bucket].dtdh_chain != dvar);
1932179193Sjb			ASSERT(start != dvar);
1933179193Sjb			ASSERT(prev->dtdv_next == dvar);
1934179193Sjb			prev->dtdv_next = dvar->dtdv_next;
1935179193Sjb		} else {
1936179193Sjb			if (dtrace_casptr(&hash[bucket].dtdh_chain,
1937179193Sjb			    start, dvar->dtdv_next) != start) {
1938179193Sjb				/*
1939179193Sjb				 * We have failed to atomically swing the
1940179193Sjb				 * hash table head pointer, presumably because
1941179193Sjb				 * of a conflicting allocation on another CPU.
1942179193Sjb				 * We need to reread the hash chain and try
1943179193Sjb				 * again.
1944179193Sjb				 */
1945179193Sjb				goto top;
1946179193Sjb			}
1947179193Sjb		}
1948179193Sjb
1949179193Sjb		dtrace_membar_producer();
1950179193Sjb
1951179193Sjb		/*
1952179193Sjb		 * Now set the hash value to indicate that it's free.
1953179193Sjb		 */
1954179193Sjb		ASSERT(hash[bucket].dtdh_chain != dvar);
1955179193Sjb		dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
1956179193Sjb
1957179193Sjb		dtrace_membar_producer();
1958179193Sjb
1959179193Sjb		/*
1960179193Sjb		 * Set the next pointer to point at the dirty list, and
1961179193Sjb		 * atomically swing the dirty pointer to the newly freed dvar.
1962179193Sjb		 */
1963179193Sjb		do {
1964179193Sjb			next = dcpu->dtdsc_dirty;
1965179193Sjb			dvar->dtdv_next = next;
1966179193Sjb		} while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next);
1967179193Sjb
1968179193Sjb		/*
1969179193Sjb		 * Finally, unlock this hash bucket.
1970179193Sjb		 */
1971179193Sjb		ASSERT(hash[bucket].dtdh_lock == lock);
1972179193Sjb		ASSERT(lock & 1);
1973179193Sjb		hash[bucket].dtdh_lock++;
1974179193Sjb
1975179193Sjb		return (NULL);
1976179193Sjbnext:
1977179193Sjb		prev = dvar;
1978179193Sjb		continue;
1979179193Sjb	}
1980179193Sjb
1981179193Sjb	if (dvar == NULL) {
1982179193Sjb		/*
1983179193Sjb		 * If dvar is NULL, it is because we went off the rails:
1984179193Sjb		 * one of the elements that we traversed in the hash chain
1985179193Sjb		 * was deleted while we were traversing it.  In this case,
1986179193Sjb		 * we assert that we aren't doing a dealloc (deallocs lock
1987179193Sjb		 * the hash bucket to prevent themselves from racing with
1988179193Sjb		 * one another), and retry the hash chain traversal.
1989179193Sjb		 */
1990179193Sjb		ASSERT(op != DTRACE_DYNVAR_DEALLOC);
1991179193Sjb		goto top;
1992179193Sjb	}
1993179193Sjb
1994179193Sjb	if (op != DTRACE_DYNVAR_ALLOC) {
1995179193Sjb		/*
1996179193Sjb		 * If we are not to allocate a new variable, we want to
1997179193Sjb		 * return NULL now.  Before we return, check that the value
1998179193Sjb		 * of the lock word hasn't changed.  If it has, we may have
1999179193Sjb		 * seen an inconsistent snapshot.
2000179193Sjb		 */
2001179193Sjb		if (op == DTRACE_DYNVAR_NOALLOC) {
2002179193Sjb			if (hash[bucket].dtdh_lock != lock)
2003179193Sjb				goto top;
2004179193Sjb		} else {
2005179193Sjb			ASSERT(op == DTRACE_DYNVAR_DEALLOC);
2006179193Sjb			ASSERT(hash[bucket].dtdh_lock == lock);
2007179193Sjb			ASSERT(lock & 1);
2008179193Sjb			hash[bucket].dtdh_lock++;
2009179193Sjb		}
2010179193Sjb
2011179193Sjb		return (NULL);
2012179193Sjb	}
2013179193Sjb
2014179193Sjb	/*
2015179193Sjb	 * We need to allocate a new dynamic variable.  The size we need is the
2016179193Sjb	 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the
2017179193Sjb	 * size of any auxiliary key data (rounded up to 8-byte alignment) plus
2018179193Sjb	 * the size of any referred-to data (dsize).  We then round the final
2019179193Sjb	 * size up to the chunksize for allocation.
2020179193Sjb	 */
2021179193Sjb	for (ksize = 0, i = 0; i < nkeys; i++)
2022179193Sjb		ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
2023179193Sjb
2024179193Sjb	/*
2025179193Sjb	 * This should be pretty much impossible, but could happen if, say,
2026179193Sjb	 * strange DIF specified the tuple.  Ideally, this should be an
2027179193Sjb	 * assertion and not an error condition -- but that requires that the
2028179193Sjb	 * chunksize calculation in dtrace_difo_chunksize() be absolutely
2029179193Sjb	 * bullet-proof.  (That is, it must not be able to be fooled by
2030179193Sjb	 * malicious DIF.)  Given the lack of backwards branches in DIF,
2031179193Sjb	 * solving this would presumably not amount to solving the Halting
2032179193Sjb	 * Problem -- but it still seems awfully hard.
2033179193Sjb	 */
2034179193Sjb	if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) +
2035179193Sjb	    ksize + dsize > chunksize) {
2036179193Sjb		dcpu->dtdsc_drops++;
2037179193Sjb		return (NULL);
2038179193Sjb	}
2039179193Sjb
2040179193Sjb	nstate = DTRACE_DSTATE_EMPTY;
2041179193Sjb
2042179193Sjb	do {
2043179193Sjbretry:
2044179193Sjb		free = dcpu->dtdsc_free;
2045179193Sjb
2046179193Sjb		if (free == NULL) {
2047179193Sjb			dtrace_dynvar_t *clean = dcpu->dtdsc_clean;
2048179193Sjb			void *rval;
2049179193Sjb
2050179193Sjb			if (clean == NULL) {
2051179193Sjb				/*
2052179193Sjb				 * We're out of dynamic variable space on
2053179193Sjb				 * this CPU.  Unless we have tried all CPUs,
2054179193Sjb				 * we'll try to allocate from a different
2055179193Sjb				 * CPU.
2056179193Sjb				 */
2057179193Sjb				switch (dstate->dtds_state) {
2058179193Sjb				case DTRACE_DSTATE_CLEAN: {
2059179193Sjb					void *sp = &dstate->dtds_state;
2060179193Sjb
2061179193Sjb					if (++cpu >= NCPU)
2062179193Sjb						cpu = 0;
2063179193Sjb
2064179193Sjb					if (dcpu->dtdsc_dirty != NULL &&
2065179193Sjb					    nstate == DTRACE_DSTATE_EMPTY)
2066179193Sjb						nstate = DTRACE_DSTATE_DIRTY;
2067179193Sjb
2068179193Sjb					if (dcpu->dtdsc_rinsing != NULL)
2069179193Sjb						nstate = DTRACE_DSTATE_RINSING;
2070179193Sjb
2071179193Sjb					dcpu = &dstate->dtds_percpu[cpu];
2072179193Sjb
2073179193Sjb					if (cpu != me)
2074179193Sjb						goto retry;
2075179193Sjb
2076179193Sjb					(void) dtrace_cas32(sp,
2077179193Sjb					    DTRACE_DSTATE_CLEAN, nstate);
2078179193Sjb
2079179193Sjb					/*
2080179193Sjb					 * To increment the correct bean
2081179193Sjb					 * counter, take another lap.
2082179193Sjb					 */
2083179193Sjb					goto retry;
2084179193Sjb				}
2085179193Sjb
2086179193Sjb				case DTRACE_DSTATE_DIRTY:
2087179193Sjb					dcpu->dtdsc_dirty_drops++;
2088179193Sjb					break;
2089179193Sjb
2090179193Sjb				case DTRACE_DSTATE_RINSING:
2091179193Sjb					dcpu->dtdsc_rinsing_drops++;
2092179193Sjb					break;
2093179193Sjb
2094179193Sjb				case DTRACE_DSTATE_EMPTY:
2095179193Sjb					dcpu->dtdsc_drops++;
2096179193Sjb					break;
2097179193Sjb				}
2098179193Sjb
2099179193Sjb				DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP);
2100179193Sjb				return (NULL);
2101179193Sjb			}
2102179193Sjb
2103179193Sjb			/*
2104179193Sjb			 * The clean list appears to be non-empty.  We want to
2105179193Sjb			 * move the clean list to the free list; we start by
2106179193Sjb			 * moving the clean pointer aside.
2107179193Sjb			 */
2108179193Sjb			if (dtrace_casptr(&dcpu->dtdsc_clean,
2109179193Sjb			    clean, NULL) != clean) {
2110179193Sjb				/*
2111179193Sjb				 * We are in one of two situations:
2112179193Sjb				 *
2113179193Sjb				 *  (a)	The clean list was switched to the
2114179193Sjb				 *	free list by another CPU.
2115179193Sjb				 *
2116179193Sjb				 *  (b)	The clean list was added to by the
2117179193Sjb				 *	cleansing cyclic.
2118179193Sjb				 *
2119179193Sjb				 * In either of these situations, we can
2120179193Sjb				 * just reattempt the free list allocation.
2121179193Sjb				 */
2122179193Sjb				goto retry;
2123179193Sjb			}
2124179193Sjb
2125179193Sjb			ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE);
2126179193Sjb
2127179193Sjb			/*
2128268290Spfg			 * Now we'll move the clean list to our free list.
2129179193Sjb			 * It's impossible for this to fail:  the only way
2130179193Sjb			 * the free list can be updated is through this
2131179193Sjb			 * code path, and only one CPU can own the clean list.
2132179193Sjb			 * Thus, it would only be possible for this to fail if
2133179193Sjb			 * this code were racing with dtrace_dynvar_clean().
2134179193Sjb			 * (That is, if dtrace_dynvar_clean() updated the clean
2135179193Sjb			 * list, and we ended up racing to update the free
2136179193Sjb			 * list.)  This race is prevented by the dtrace_sync()
2137179193Sjb			 * in dtrace_dynvar_clean() -- which flushes the
2138179193Sjb			 * owners of the clean lists out before resetting
2139179193Sjb			 * the clean lists.
2140179193Sjb			 */
2141268290Spfg			dcpu = &dstate->dtds_percpu[me];
2142179193Sjb			rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean);
2143179193Sjb			ASSERT(rval == NULL);
2144179193Sjb			goto retry;
2145179193Sjb		}
2146179193Sjb
2147179193Sjb		dvar = free;
2148179193Sjb		new_free = dvar->dtdv_next;
2149179193Sjb	} while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free);
2150179193Sjb
2151179193Sjb	/*
2152179193Sjb	 * We have now allocated a new chunk.  We copy the tuple keys into the
2153179193Sjb	 * tuple array and copy any referenced key data into the data space
2154179193Sjb	 * following the tuple array.  As we do this, we relocate dttk_value
2155179193Sjb	 * in the final tuple to point to the key data address in the chunk.
2156179193Sjb	 */
2157179193Sjb	kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys];
2158179193Sjb	dvar->dtdv_data = (void *)(kdata + ksize);
2159179193Sjb	dvar->dtdv_tuple.dtt_nkeys = nkeys;
2160179193Sjb
2161179193Sjb	for (i = 0; i < nkeys; i++) {
2162179193Sjb		dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i];
2163179193Sjb		size_t kesize = key[i].dttk_size;
2164179193Sjb
2165179193Sjb		if (kesize != 0) {
2166179193Sjb			dtrace_bcopy(
2167179193Sjb			    (const void *)(uintptr_t)key[i].dttk_value,
2168179193Sjb			    (void *)kdata, kesize);
2169179193Sjb			dkey->dttk_value = kdata;
2170179193Sjb			kdata += P2ROUNDUP(kesize, sizeof (uint64_t));
2171179193Sjb		} else {
2172179193Sjb			dkey->dttk_value = key[i].dttk_value;
2173179193Sjb		}
2174179193Sjb
2175179193Sjb		dkey->dttk_size = kesize;
2176179193Sjb	}
2177179193Sjb
2178179193Sjb	ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE);
2179179193Sjb	dvar->dtdv_hashval = hashval;
2180179193Sjb	dvar->dtdv_next = start;
2181179193Sjb
2182179193Sjb	if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start)
2183179193Sjb		return (dvar);
2184179193Sjb
2185179193Sjb	/*
2186179193Sjb	 * The cas has failed.  Either another CPU is adding an element to
2187179193Sjb	 * this hash chain, or another CPU is deleting an element from this
2188179193Sjb	 * hash chain.  The simplest way to deal with both of these cases
2189179193Sjb	 * (though not necessarily the most efficient) is to free our
2190287641Smarkj	 * allocated block and re-attempt it all.  Note that the free is
2191179193Sjb	 * to the dirty list and _not_ to the free list.  This is to prevent
2192179193Sjb	 * races with allocators, above.
2193179193Sjb	 */
2194179193Sjb	dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
2195179193Sjb
2196179193Sjb	dtrace_membar_producer();
2197179193Sjb
2198179193Sjb	do {
2199179193Sjb		free = dcpu->dtdsc_dirty;
2200179193Sjb		dvar->dtdv_next = free;
2201179193Sjb	} while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free);
2202179193Sjb
2203287641Smarkj	goto top;
2204179193Sjb}
2205179193Sjb
2206179193Sjb/*ARGSUSED*/
2207179193Sjbstatic void
2208179193Sjbdtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg)
2209179193Sjb{
2210179193Sjb	if ((int64_t)nval < (int64_t)*oval)
2211179193Sjb		*oval = nval;
2212179193Sjb}
2213179193Sjb
2214179193Sjb/*ARGSUSED*/
2215179193Sjbstatic void
2216179193Sjbdtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg)
2217179193Sjb{
2218179193Sjb	if ((int64_t)nval > (int64_t)*oval)
2219179193Sjb		*oval = nval;
2220179193Sjb}
2221179193Sjb
2222179193Sjbstatic void
2223179193Sjbdtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr)
2224179193Sjb{
2225179193Sjb	int i, zero = DTRACE_QUANTIZE_ZEROBUCKET;
2226179193Sjb	int64_t val = (int64_t)nval;
2227179193Sjb
2228179193Sjb	if (val < 0) {
2229179193Sjb		for (i = 0; i < zero; i++) {
2230179193Sjb			if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) {
2231179193Sjb				quanta[i] += incr;
2232179193Sjb				return;
2233179193Sjb			}
2234179193Sjb		}
2235179193Sjb	} else {
2236179193Sjb		for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) {
2237179193Sjb			if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) {
2238179193Sjb				quanta[i - 1] += incr;
2239179193Sjb				return;
2240179193Sjb			}
2241179193Sjb		}
2242179193Sjb
2243179193Sjb		quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr;
2244179193Sjb		return;
2245179193Sjb	}
2246179193Sjb
2247179193Sjb	ASSERT(0);
2248179193Sjb}
2249179193Sjb
2250179193Sjbstatic void
2251179193Sjbdtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr)
2252179193Sjb{
2253179193Sjb	uint64_t arg = *lquanta++;
2254179193Sjb	int32_t base = DTRACE_LQUANTIZE_BASE(arg);
2255179193Sjb	uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
2256179193Sjb	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
2257179193Sjb	int32_t val = (int32_t)nval, level;
2258179193Sjb
2259179193Sjb	ASSERT(step != 0);
2260179193Sjb	ASSERT(levels != 0);
2261179193Sjb
2262179193Sjb	if (val < base) {
2263179193Sjb		/*
2264179193Sjb		 * This is an underflow.
2265179193Sjb		 */
2266179193Sjb		lquanta[0] += incr;
2267179193Sjb		return;
2268179193Sjb	}
2269179193Sjb
2270179193Sjb	level = (val - base) / step;
2271179193Sjb
2272179193Sjb	if (level < levels) {
2273179193Sjb		lquanta[level + 1] += incr;
2274179193Sjb		return;
2275179193Sjb	}
2276179193Sjb
2277179193Sjb	/*
2278179193Sjb	 * This is an overflow.
2279179193Sjb	 */
2280179193Sjb	lquanta[levels + 1] += incr;
2281179193Sjb}
2282179193Sjb
2283237624Spfgstatic int
2284237624Spfgdtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low,
2285237624Spfg    uint16_t high, uint16_t nsteps, int64_t value)
2286237624Spfg{
2287237624Spfg	int64_t this = 1, last, next;
2288237624Spfg	int base = 1, order;
2289237624Spfg
2290237624Spfg	ASSERT(factor <= nsteps);
2291237624Spfg	ASSERT(nsteps % factor == 0);
2292237624Spfg
2293237624Spfg	for (order = 0; order < low; order++)
2294237624Spfg		this *= factor;
2295237624Spfg
2296237624Spfg	/*
2297237624Spfg	 * If our value is less than our factor taken to the power of the
2298237624Spfg	 * low order of magnitude, it goes into the zeroth bucket.
2299237624Spfg	 */
2300237624Spfg	if (value < (last = this))
2301237624Spfg		return (0);
2302237624Spfg
2303237624Spfg	for (this *= factor; order <= high; order++) {
2304237624Spfg		int nbuckets = this > nsteps ? nsteps : this;
2305237624Spfg
2306237624Spfg		if ((next = this * factor) < this) {
2307237624Spfg			/*
2308237624Spfg			 * We should not generally get log/linear quantizations
2309237624Spfg			 * with a high magnitude that allows 64-bits to
2310237624Spfg			 * overflow, but we nonetheless protect against this
2311237624Spfg			 * by explicitly checking for overflow, and clamping
2312237624Spfg			 * our value accordingly.
2313237624Spfg			 */
2314237624Spfg			value = this - 1;
2315237624Spfg		}
2316237624Spfg
2317237624Spfg		if (value < this) {
2318237624Spfg			/*
2319237624Spfg			 * If our value lies within this order of magnitude,
2320237624Spfg			 * determine its position by taking the offset within
2321237624Spfg			 * the order of magnitude, dividing by the bucket
2322237624Spfg			 * width, and adding to our (accumulated) base.
2323237624Spfg			 */
2324237624Spfg			return (base + (value - last) / (this / nbuckets));
2325237624Spfg		}
2326237624Spfg
2327237624Spfg		base += nbuckets - (nbuckets / factor);
2328237624Spfg		last = this;
2329237624Spfg		this = next;
2330237624Spfg	}
2331237624Spfg
2332237624Spfg	/*
2333237624Spfg	 * Our value is greater than or equal to our factor taken to the
2334237624Spfg	 * power of one plus the high magnitude -- return the top bucket.
2335237624Spfg	 */
2336237624Spfg	return (base);
2337237624Spfg}
2338237624Spfg
2339237624Spfgstatic void
2340237624Spfgdtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr)
2341237624Spfg{
2342237624Spfg	uint64_t arg = *llquanta++;
2343237624Spfg	uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg);
2344237624Spfg	uint16_t low = DTRACE_LLQUANTIZE_LOW(arg);
2345237624Spfg	uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg);
2346237624Spfg	uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
2347237624Spfg
2348237624Spfg	llquanta[dtrace_aggregate_llquantize_bucket(factor,
2349237624Spfg	    low, high, nsteps, nval)] += incr;
2350237624Spfg}
2351237624Spfg
2352179193Sjb/*ARGSUSED*/
2353179193Sjbstatic void
2354179193Sjbdtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg)
2355179193Sjb{
2356179193Sjb	data[0]++;
2357179193Sjb	data[1] += nval;
2358179193Sjb}
2359179193Sjb
2360179193Sjb/*ARGSUSED*/
2361179193Sjbstatic void
2362179193Sjbdtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg)
2363179193Sjb{
2364179193Sjb	int64_t snval = (int64_t)nval;
2365179193Sjb	uint64_t tmp[2];
2366179193Sjb
2367179193Sjb	data[0]++;
2368179193Sjb	data[1] += nval;
2369179193Sjb
2370179193Sjb	/*
2371179193Sjb	 * What we want to say here is:
2372179193Sjb	 *
2373179193Sjb	 * data[2] += nval * nval;
2374179193Sjb	 *
2375179193Sjb	 * But given that nval is 64-bit, we could easily overflow, so
2376179193Sjb	 * we do this as 128-bit arithmetic.
2377179193Sjb	 */
2378179193Sjb	if (snval < 0)
2379179193Sjb		snval = -snval;
2380179193Sjb
2381179193Sjb	dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp);
2382179193Sjb	dtrace_add_128(data + 2, tmp, data + 2);
2383179193Sjb}
2384179193Sjb
2385179193Sjb/*ARGSUSED*/
2386179193Sjbstatic void
2387179193Sjbdtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg)
2388179193Sjb{
2389179193Sjb	*oval = *oval + 1;
2390179193Sjb}
2391179193Sjb
2392179193Sjb/*ARGSUSED*/
2393179193Sjbstatic void
2394179193Sjbdtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg)
2395179193Sjb{
2396179193Sjb	*oval += nval;
2397179193Sjb}
2398179193Sjb
2399179193Sjb/*
2400179193Sjb * Aggregate given the tuple in the principal data buffer, and the aggregating
2401179193Sjb * action denoted by the specified dtrace_aggregation_t.  The aggregation
2402179193Sjb * buffer is specified as the buf parameter.  This routine does not return
2403179193Sjb * failure; if there is no space in the aggregation buffer, the data will be
2404179193Sjb * dropped, and a corresponding counter incremented.
2405179193Sjb */
2406179193Sjbstatic void
2407179193Sjbdtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf,
2408179193Sjb    intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg)
2409179193Sjb{
2410179193Sjb	dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec;
2411179193Sjb	uint32_t i, ndx, size, fsize;
2412179193Sjb	uint32_t align = sizeof (uint64_t) - 1;
2413179193Sjb	dtrace_aggbuffer_t *agb;
2414179193Sjb	dtrace_aggkey_t *key;
2415179193Sjb	uint32_t hashval = 0, limit, isstr;
2416179193Sjb	caddr_t tomax, data, kdata;
2417179193Sjb	dtrace_actkind_t action;
2418179193Sjb	dtrace_action_t *act;
2419179193Sjb	uintptr_t offs;
2420179193Sjb
2421179193Sjb	if (buf == NULL)
2422179193Sjb		return;
2423179193Sjb
2424179193Sjb	if (!agg->dtag_hasarg) {
2425179193Sjb		/*
2426179193Sjb		 * Currently, only quantize() and lquantize() take additional
2427179193Sjb		 * arguments, and they have the same semantics:  an increment
2428179193Sjb		 * value that defaults to 1 when not present.  If additional
2429179193Sjb		 * aggregating actions take arguments, the setting of the
2430179193Sjb		 * default argument value will presumably have to become more
2431179193Sjb		 * sophisticated...
2432179193Sjb		 */
2433179193Sjb		arg = 1;
2434179193Sjb	}
2435179193Sjb
2436179193Sjb	action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION;
2437179193Sjb	size = rec->dtrd_offset - agg->dtag_base;
2438179193Sjb	fsize = size + rec->dtrd_size;
2439179193Sjb
2440179193Sjb	ASSERT(dbuf->dtb_tomax != NULL);
2441179193Sjb	data = dbuf->dtb_tomax + offset + agg->dtag_base;
2442179193Sjb
2443179193Sjb	if ((tomax = buf->dtb_tomax) == NULL) {
2444179193Sjb		dtrace_buffer_drop(buf);
2445179193Sjb		return;
2446179193Sjb	}
2447179193Sjb
2448179193Sjb	/*
2449179193Sjb	 * The metastructure is always at the bottom of the buffer.
2450179193Sjb	 */
2451179193Sjb	agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size -
2452179193Sjb	    sizeof (dtrace_aggbuffer_t));
2453179193Sjb
2454179193Sjb	if (buf->dtb_offset == 0) {
2455179193Sjb		/*
2456179193Sjb		 * We just kludge up approximately 1/8th of the size to be
2457179193Sjb		 * buckets.  If this guess ends up being routinely
2458179193Sjb		 * off-the-mark, we may need to dynamically readjust this
2459179193Sjb		 * based on past performance.
2460179193Sjb		 */
2461179193Sjb		uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t);
2462179193Sjb
2463179193Sjb		if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) <
2464179193Sjb		    (uintptr_t)tomax || hashsize == 0) {
2465179193Sjb			/*
2466179193Sjb			 * We've been given a ludicrously small buffer;
2467179193Sjb			 * increment our drop count and leave.
2468179193Sjb			 */
2469179193Sjb			dtrace_buffer_drop(buf);
2470179193Sjb			return;
2471179193Sjb		}
2472179193Sjb
2473179193Sjb		/*
2474179193Sjb		 * And now, a pathetic attempt to try to get a an odd (or
2475179193Sjb		 * perchance, a prime) hash size for better hash distribution.
2476179193Sjb		 */
2477179193Sjb		if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3))
2478179193Sjb			hashsize -= DTRACE_AGGHASHSIZE_SLEW;
2479179193Sjb
2480179193Sjb		agb->dtagb_hashsize = hashsize;
2481179193Sjb		agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb -
2482179193Sjb		    agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *));
2483179193Sjb		agb->dtagb_free = (uintptr_t)agb->dtagb_hash;
2484179193Sjb
2485179193Sjb		for (i = 0; i < agb->dtagb_hashsize; i++)
2486179193Sjb			agb->dtagb_hash[i] = NULL;
2487179193Sjb	}
2488179193Sjb
2489179193Sjb	ASSERT(agg->dtag_first != NULL);
2490179193Sjb	ASSERT(agg->dtag_first->dta_intuple);
2491179193Sjb
2492179193Sjb	/*
2493179193Sjb	 * Calculate the hash value based on the key.  Note that we _don't_
2494179193Sjb	 * include the aggid in the hashing (but we will store it as part of
2495179193Sjb	 * the key).  The hashing algorithm is Bob Jenkins' "One-at-a-time"
2496179193Sjb	 * algorithm: a simple, quick algorithm that has no known funnels, and
2497179193Sjb	 * gets good distribution in practice.  The efficacy of the hashing
2498179193Sjb	 * algorithm (and a comparison with other algorithms) may be found by
2499179193Sjb	 * running the ::dtrace_aggstat MDB dcmd.
2500179193Sjb	 */
2501179193Sjb	for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2502179193Sjb		i = act->dta_rec.dtrd_offset - agg->dtag_base;
2503179193Sjb		limit = i + act->dta_rec.dtrd_size;
2504179193Sjb		ASSERT(limit <= size);
2505179193Sjb		isstr = DTRACEACT_ISSTRING(act);
2506179193Sjb
2507179193Sjb		for (; i < limit; i++) {
2508179193Sjb			hashval += data[i];
2509179193Sjb			hashval += (hashval << 10);
2510179193Sjb			hashval ^= (hashval >> 6);
2511179193Sjb
2512179193Sjb			if (isstr && data[i] == '\0')
2513179193Sjb				break;
2514179193Sjb		}
2515179193Sjb	}
2516179193Sjb
2517179193Sjb	hashval += (hashval << 3);
2518179193Sjb	hashval ^= (hashval >> 11);
2519179193Sjb	hashval += (hashval << 15);
2520179193Sjb
2521179193Sjb	/*
2522179193Sjb	 * Yes, the divide here is expensive -- but it's generally the least
2523179193Sjb	 * of the performance issues given the amount of data that we iterate
2524179193Sjb	 * over to compute hash values, compare data, etc.
2525179193Sjb	 */
2526179193Sjb	ndx = hashval % agb->dtagb_hashsize;
2527179193Sjb
2528179193Sjb	for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) {
2529179193Sjb		ASSERT((caddr_t)key >= tomax);
2530179193Sjb		ASSERT((caddr_t)key < tomax + buf->dtb_size);
2531179193Sjb
2532179193Sjb		if (hashval != key->dtak_hashval || key->dtak_size != size)
2533179193Sjb			continue;
2534179193Sjb
2535179193Sjb		kdata = key->dtak_data;
2536179193Sjb		ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size);
2537179193Sjb
2538179193Sjb		for (act = agg->dtag_first; act->dta_intuple;
2539179193Sjb		    act = act->dta_next) {
2540179193Sjb			i = act->dta_rec.dtrd_offset - agg->dtag_base;
2541179193Sjb			limit = i + act->dta_rec.dtrd_size;
2542179193Sjb			ASSERT(limit <= size);
2543179193Sjb			isstr = DTRACEACT_ISSTRING(act);
2544179193Sjb
2545179193Sjb			for (; i < limit; i++) {
2546179193Sjb				if (kdata[i] != data[i])
2547179193Sjb					goto next;
2548179193Sjb
2549179193Sjb				if (isstr && data[i] == '\0')
2550179193Sjb					break;
2551179193Sjb			}
2552179193Sjb		}
2553179193Sjb
2554179193Sjb		if (action != key->dtak_action) {
2555179193Sjb			/*
2556179193Sjb			 * We are aggregating on the same value in the same
2557179193Sjb			 * aggregation with two different aggregating actions.
2558179193Sjb			 * (This should have been picked up in the compiler,
2559179193Sjb			 * so we may be dealing with errant or devious DIF.)
2560179193Sjb			 * This is an error condition; we indicate as much,
2561179193Sjb			 * and return.
2562179193Sjb			 */
2563179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
2564179193Sjb			return;
2565179193Sjb		}
2566179193Sjb
2567179193Sjb		/*
2568179193Sjb		 * This is a hit:  we need to apply the aggregator to
2569179193Sjb		 * the value at this key.
2570179193Sjb		 */
2571179193Sjb		agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg);
2572179193Sjb		return;
2573179193Sjbnext:
2574179193Sjb		continue;
2575179193Sjb	}
2576179193Sjb
2577179193Sjb	/*
2578179193Sjb	 * We didn't find it.  We need to allocate some zero-filled space,
2579179193Sjb	 * link it into the hash table appropriately, and apply the aggregator
2580179193Sjb	 * to the (zero-filled) value.
2581179193Sjb	 */
2582179193Sjb	offs = buf->dtb_offset;
2583179193Sjb	while (offs & (align - 1))
2584179193Sjb		offs += sizeof (uint32_t);
2585179193Sjb
2586179193Sjb	/*
2587179193Sjb	 * If we don't have enough room to both allocate a new key _and_
2588179193Sjb	 * its associated data, increment the drop count and return.
2589179193Sjb	 */
2590179193Sjb	if ((uintptr_t)tomax + offs + fsize >
2591179193Sjb	    agb->dtagb_free - sizeof (dtrace_aggkey_t)) {
2592179193Sjb		dtrace_buffer_drop(buf);
2593179193Sjb		return;
2594179193Sjb	}
2595179193Sjb
2596179193Sjb	/*CONSTCOND*/
2597179193Sjb	ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1)));
2598179193Sjb	key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t));
2599179193Sjb	agb->dtagb_free -= sizeof (dtrace_aggkey_t);
2600179193Sjb
2601179193Sjb	key->dtak_data = kdata = tomax + offs;
2602179193Sjb	buf->dtb_offset = offs + fsize;
2603179193Sjb
2604179193Sjb	/*
2605179193Sjb	 * Now copy the data across.
2606179193Sjb	 */
2607179193Sjb	*((dtrace_aggid_t *)kdata) = agg->dtag_id;
2608179193Sjb
2609179193Sjb	for (i = sizeof (dtrace_aggid_t); i < size; i++)
2610179193Sjb		kdata[i] = data[i];
2611179193Sjb
2612179193Sjb	/*
2613179193Sjb	 * Because strings are not zeroed out by default, we need to iterate
2614179193Sjb	 * looking for actions that store strings, and we need to explicitly
2615179193Sjb	 * pad these strings out with zeroes.
2616179193Sjb	 */
2617179193Sjb	for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
2618179193Sjb		int nul;
2619179193Sjb
2620179193Sjb		if (!DTRACEACT_ISSTRING(act))
2621179193Sjb			continue;
2622179193Sjb
2623179193Sjb		i = act->dta_rec.dtrd_offset - agg->dtag_base;
2624179193Sjb		limit = i + act->dta_rec.dtrd_size;
2625179193Sjb		ASSERT(limit <= size);
2626179193Sjb
2627179193Sjb		for (nul = 0; i < limit; i++) {
2628179193Sjb			if (nul) {
2629179193Sjb				kdata[i] = '\0';
2630179193Sjb				continue;
2631179193Sjb			}
2632179193Sjb
2633179193Sjb			if (data[i] != '\0')
2634179193Sjb				continue;
2635179193Sjb
2636179193Sjb			nul = 1;
2637179193Sjb		}
2638179193Sjb	}
2639179193Sjb
2640179193Sjb	for (i = size; i < fsize; i++)
2641179193Sjb		kdata[i] = 0;
2642179193Sjb
2643179193Sjb	key->dtak_hashval = hashval;
2644179193Sjb	key->dtak_size = size;
2645179193Sjb	key->dtak_action = action;
2646179193Sjb	key->dtak_next = agb->dtagb_hash[ndx];
2647179193Sjb	agb->dtagb_hash[ndx] = key;
2648179193Sjb
2649179193Sjb	/*
2650179193Sjb	 * Finally, apply the aggregator.
2651179193Sjb	 */
2652179193Sjb	*((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial;
2653179193Sjb	agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg);
2654179193Sjb}
2655179193Sjb
2656179193Sjb/*
2657179193Sjb * Given consumer state, this routine finds a speculation in the INACTIVE
2658179193Sjb * state and transitions it into the ACTIVE state.  If there is no speculation
2659179193Sjb * in the INACTIVE state, 0 is returned.  In this case, no error counter is
2660179193Sjb * incremented -- it is up to the caller to take appropriate action.
2661179193Sjb */
2662179193Sjbstatic int
2663179193Sjbdtrace_speculation(dtrace_state_t *state)
2664179193Sjb{
2665179193Sjb	int i = 0;
2666179193Sjb	dtrace_speculation_state_t current;
2667179193Sjb	uint32_t *stat = &state->dts_speculations_unavail, count;
2668179193Sjb
2669179193Sjb	while (i < state->dts_nspeculations) {
2670179193Sjb		dtrace_speculation_t *spec = &state->dts_speculations[i];
2671179193Sjb
2672179193Sjb		current = spec->dtsp_state;
2673179193Sjb
2674179193Sjb		if (current != DTRACESPEC_INACTIVE) {
2675179193Sjb			if (current == DTRACESPEC_COMMITTINGMANY ||
2676179193Sjb			    current == DTRACESPEC_COMMITTING ||
2677179193Sjb			    current == DTRACESPEC_DISCARDING)
2678179193Sjb				stat = &state->dts_speculations_busy;
2679179193Sjb			i++;
2680179193Sjb			continue;
2681179193Sjb		}
2682179193Sjb
2683179193Sjb		if (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2684179193Sjb		    current, DTRACESPEC_ACTIVE) == current)
2685179193Sjb			return (i + 1);
2686179193Sjb	}
2687179193Sjb
2688179193Sjb	/*
2689179193Sjb	 * We couldn't find a speculation.  If we found as much as a single
2690179193Sjb	 * busy speculation buffer, we'll attribute this failure as "busy"
2691179193Sjb	 * instead of "unavail".
2692179193Sjb	 */
2693179193Sjb	do {
2694179193Sjb		count = *stat;
2695179193Sjb	} while (dtrace_cas32(stat, count, count + 1) != count);
2696179193Sjb
2697179193Sjb	return (0);
2698179193Sjb}
2699179193Sjb
2700179193Sjb/*
2701179193Sjb * This routine commits an active speculation.  If the specified speculation
2702179193Sjb * is not in a valid state to perform a commit(), this routine will silently do
2703179193Sjb * nothing.  The state of the specified speculation is transitioned according
2704179193Sjb * to the state transition diagram outlined in <sys/dtrace_impl.h>
2705179193Sjb */
2706179193Sjbstatic void
2707179193Sjbdtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu,
2708179193Sjb    dtrace_specid_t which)
2709179193Sjb{
2710179193Sjb	dtrace_speculation_t *spec;
2711179193Sjb	dtrace_buffer_t *src, *dest;
2712250574Smarkj	uintptr_t daddr, saddr, dlimit, slimit;
2713179198Sjb	dtrace_speculation_state_t current, new = 0;
2714179193Sjb	intptr_t offs;
2715250574Smarkj	uint64_t timestamp;
2716179193Sjb
2717179193Sjb	if (which == 0)
2718179193Sjb		return;
2719179193Sjb
2720179193Sjb	if (which > state->dts_nspeculations) {
2721179193Sjb		cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2722179193Sjb		return;
2723179193Sjb	}
2724179193Sjb
2725179193Sjb	spec = &state->dts_speculations[which - 1];
2726179193Sjb	src = &spec->dtsp_buffer[cpu];
2727179193Sjb	dest = &state->dts_buffer[cpu];
2728179193Sjb
2729179193Sjb	do {
2730179193Sjb		current = spec->dtsp_state;
2731179193Sjb
2732179193Sjb		if (current == DTRACESPEC_COMMITTINGMANY)
2733179193Sjb			break;
2734179193Sjb
2735179193Sjb		switch (current) {
2736179193Sjb		case DTRACESPEC_INACTIVE:
2737179193Sjb		case DTRACESPEC_DISCARDING:
2738179193Sjb			return;
2739179193Sjb
2740179193Sjb		case DTRACESPEC_COMMITTING:
2741179193Sjb			/*
2742179193Sjb			 * This is only possible if we are (a) commit()'ing
2743179193Sjb			 * without having done a prior speculate() on this CPU
2744179193Sjb			 * and (b) racing with another commit() on a different
2745179193Sjb			 * CPU.  There's nothing to do -- we just assert that
2746179193Sjb			 * our offset is 0.
2747179193Sjb			 */
2748179193Sjb			ASSERT(src->dtb_offset == 0);
2749179193Sjb			return;
2750179193Sjb
2751179193Sjb		case DTRACESPEC_ACTIVE:
2752179193Sjb			new = DTRACESPEC_COMMITTING;
2753179193Sjb			break;
2754179193Sjb
2755179193Sjb		case DTRACESPEC_ACTIVEONE:
2756179193Sjb			/*
2757179193Sjb			 * This speculation is active on one CPU.  If our
2758179193Sjb			 * buffer offset is non-zero, we know that the one CPU
2759179193Sjb			 * must be us.  Otherwise, we are committing on a
2760179193Sjb			 * different CPU from the speculate(), and we must
2761179193Sjb			 * rely on being asynchronously cleaned.
2762179193Sjb			 */
2763179193Sjb			if (src->dtb_offset != 0) {
2764179193Sjb				new = DTRACESPEC_COMMITTING;
2765179193Sjb				break;
2766179193Sjb			}
2767179193Sjb			/*FALLTHROUGH*/
2768179193Sjb
2769179193Sjb		case DTRACESPEC_ACTIVEMANY:
2770179193Sjb			new = DTRACESPEC_COMMITTINGMANY;
2771179193Sjb			break;
2772179193Sjb
2773179193Sjb		default:
2774179193Sjb			ASSERT(0);
2775179193Sjb		}
2776179193Sjb	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2777179193Sjb	    current, new) != current);
2778179193Sjb
2779179193Sjb	/*
2780179193Sjb	 * We have set the state to indicate that we are committing this
2781179193Sjb	 * speculation.  Now reserve the necessary space in the destination
2782179193Sjb	 * buffer.
2783179193Sjb	 */
2784179193Sjb	if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset,
2785179193Sjb	    sizeof (uint64_t), state, NULL)) < 0) {
2786179193Sjb		dtrace_buffer_drop(dest);
2787179193Sjb		goto out;
2788179193Sjb	}
2789179193Sjb
2790179193Sjb	/*
2791250574Smarkj	 * We have sufficient space to copy the speculative buffer into the
2792250574Smarkj	 * primary buffer.  First, modify the speculative buffer, filling
2793250574Smarkj	 * in the timestamp of all entries with the current time.  The data
2794250574Smarkj	 * must have the commit() time rather than the time it was traced,
2795250574Smarkj	 * so that all entries in the primary buffer are in timestamp order.
2796250574Smarkj	 */
2797250574Smarkj	timestamp = dtrace_gethrtime();
2798250574Smarkj	saddr = (uintptr_t)src->dtb_tomax;
2799250574Smarkj	slimit = saddr + src->dtb_offset;
2800250574Smarkj	while (saddr < slimit) {
2801250574Smarkj		size_t size;
2802250574Smarkj		dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr;
2803250574Smarkj
2804250574Smarkj		if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
2805250574Smarkj			saddr += sizeof (dtrace_epid_t);
2806250574Smarkj			continue;
2807250574Smarkj		}
2808250574Smarkj		ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs);
2809250574Smarkj		size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size;
2810250574Smarkj
2811250574Smarkj		ASSERT3U(saddr + size, <=, slimit);
2812250574Smarkj		ASSERT3U(size, >=, sizeof (dtrace_rechdr_t));
2813250574Smarkj		ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX);
2814250574Smarkj
2815250574Smarkj		DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp);
2816250574Smarkj
2817250574Smarkj		saddr += size;
2818250574Smarkj	}
2819250574Smarkj
2820250574Smarkj	/*
2821250574Smarkj	 * Copy the buffer across.  (Note that this is a
2822179193Sjb	 * highly subobtimal bcopy(); in the unlikely event that this becomes
2823179193Sjb	 * a serious performance issue, a high-performance DTrace-specific
2824179193Sjb	 * bcopy() should obviously be invented.)
2825179193Sjb	 */
2826179193Sjb	daddr = (uintptr_t)dest->dtb_tomax + offs;
2827179193Sjb	dlimit = daddr + src->dtb_offset;
2828179193Sjb	saddr = (uintptr_t)src->dtb_tomax;
2829179193Sjb
2830179193Sjb	/*
2831179193Sjb	 * First, the aligned portion.
2832179193Sjb	 */
2833179193Sjb	while (dlimit - daddr >= sizeof (uint64_t)) {
2834179193Sjb		*((uint64_t *)daddr) = *((uint64_t *)saddr);
2835179193Sjb
2836179193Sjb		daddr += sizeof (uint64_t);
2837179193Sjb		saddr += sizeof (uint64_t);
2838179193Sjb	}
2839179193Sjb
2840179193Sjb	/*
2841179193Sjb	 * Now any left-over bit...
2842179193Sjb	 */
2843179193Sjb	while (dlimit - daddr)
2844179193Sjb		*((uint8_t *)daddr++) = *((uint8_t *)saddr++);
2845179193Sjb
2846179193Sjb	/*
2847179193Sjb	 * Finally, commit the reserved space in the destination buffer.
2848179193Sjb	 */
2849179193Sjb	dest->dtb_offset = offs + src->dtb_offset;
2850179193Sjb
2851179193Sjbout:
2852179193Sjb	/*
2853179193Sjb	 * If we're lucky enough to be the only active CPU on this speculation
2854179193Sjb	 * buffer, we can just set the state back to DTRACESPEC_INACTIVE.
2855179193Sjb	 */
2856179193Sjb	if (current == DTRACESPEC_ACTIVE ||
2857179193Sjb	    (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) {
2858179193Sjb		uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state,
2859179193Sjb		    DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE);
2860179193Sjb
2861179193Sjb		ASSERT(rval == DTRACESPEC_COMMITTING);
2862179193Sjb	}
2863179193Sjb
2864179193Sjb	src->dtb_offset = 0;
2865179193Sjb	src->dtb_xamot_drops += src->dtb_drops;
2866179193Sjb	src->dtb_drops = 0;
2867179193Sjb}
2868179193Sjb
2869179193Sjb/*
2870179193Sjb * This routine discards an active speculation.  If the specified speculation
2871179193Sjb * is not in a valid state to perform a discard(), this routine will silently
2872179193Sjb * do nothing.  The state of the specified speculation is transitioned
2873179193Sjb * according to the state transition diagram outlined in <sys/dtrace_impl.h>
2874179193Sjb */
2875179193Sjbstatic void
2876179193Sjbdtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu,
2877179193Sjb    dtrace_specid_t which)
2878179193Sjb{
2879179193Sjb	dtrace_speculation_t *spec;
2880179198Sjb	dtrace_speculation_state_t current, new = 0;
2881179193Sjb	dtrace_buffer_t *buf;
2882179193Sjb
2883179193Sjb	if (which == 0)
2884179193Sjb		return;
2885179193Sjb
2886179193Sjb	if (which > state->dts_nspeculations) {
2887179193Sjb		cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
2888179193Sjb		return;
2889179193Sjb	}
2890179193Sjb
2891179193Sjb	spec = &state->dts_speculations[which - 1];
2892179193Sjb	buf = &spec->dtsp_buffer[cpu];
2893179193Sjb
2894179193Sjb	do {
2895179193Sjb		current = spec->dtsp_state;
2896179193Sjb
2897179193Sjb		switch (current) {
2898179193Sjb		case DTRACESPEC_INACTIVE:
2899179193Sjb		case DTRACESPEC_COMMITTINGMANY:
2900179193Sjb		case DTRACESPEC_COMMITTING:
2901179193Sjb		case DTRACESPEC_DISCARDING:
2902179193Sjb			return;
2903179193Sjb
2904179193Sjb		case DTRACESPEC_ACTIVE:
2905179193Sjb		case DTRACESPEC_ACTIVEMANY:
2906179193Sjb			new = DTRACESPEC_DISCARDING;
2907179193Sjb			break;
2908179193Sjb
2909179193Sjb		case DTRACESPEC_ACTIVEONE:
2910179193Sjb			if (buf->dtb_offset != 0) {
2911179193Sjb				new = DTRACESPEC_INACTIVE;
2912179193Sjb			} else {
2913179193Sjb				new = DTRACESPEC_DISCARDING;
2914179193Sjb			}
2915179193Sjb			break;
2916179193Sjb
2917179193Sjb		default:
2918179193Sjb			ASSERT(0);
2919179193Sjb		}
2920179193Sjb	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
2921179193Sjb	    current, new) != current);
2922179193Sjb
2923179193Sjb	buf->dtb_offset = 0;
2924179193Sjb	buf->dtb_drops = 0;
2925179193Sjb}
2926179193Sjb
2927179193Sjb/*
2928179193Sjb * Note:  not called from probe context.  This function is called
2929179193Sjb * asynchronously from cross call context to clean any speculations that are
2930179193Sjb * in the COMMITTINGMANY or DISCARDING states.  These speculations may not be
2931179193Sjb * transitioned back to the INACTIVE state until all CPUs have cleaned the
2932179193Sjb * speculation.
2933179193Sjb */
2934179193Sjbstatic void
2935179193Sjbdtrace_speculation_clean_here(dtrace_state_t *state)
2936179193Sjb{
2937179193Sjb	dtrace_icookie_t cookie;
2938179198Sjb	processorid_t cpu = curcpu;
2939179193Sjb	dtrace_buffer_t *dest = &state->dts_buffer[cpu];
2940179193Sjb	dtrace_specid_t i;
2941179193Sjb
2942179193Sjb	cookie = dtrace_interrupt_disable();
2943179193Sjb
2944179193Sjb	if (dest->dtb_tomax == NULL) {
2945179193Sjb		dtrace_interrupt_enable(cookie);
2946179193Sjb		return;
2947179193Sjb	}
2948179193Sjb
2949179193Sjb	for (i = 0; i < state->dts_nspeculations; i++) {
2950179193Sjb		dtrace_speculation_t *spec = &state->dts_speculations[i];
2951179193Sjb		dtrace_buffer_t *src = &spec->dtsp_buffer[cpu];
2952179193Sjb
2953179193Sjb		if (src->dtb_tomax == NULL)
2954179193Sjb			continue;
2955179193Sjb
2956179193Sjb		if (spec->dtsp_state == DTRACESPEC_DISCARDING) {
2957179193Sjb			src->dtb_offset = 0;
2958179193Sjb			continue;
2959179193Sjb		}
2960179193Sjb
2961179193Sjb		if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
2962179193Sjb			continue;
2963179193Sjb
2964179193Sjb		if (src->dtb_offset == 0)
2965179193Sjb			continue;
2966179193Sjb
2967179193Sjb		dtrace_speculation_commit(state, cpu, i + 1);
2968179193Sjb	}
2969179193Sjb
2970179193Sjb	dtrace_interrupt_enable(cookie);
2971179193Sjb}
2972179193Sjb
2973179193Sjb/*
2974179193Sjb * Note:  not called from probe context.  This function is called
2975179193Sjb * asynchronously (and at a regular interval) to clean any speculations that
2976179193Sjb * are in the COMMITTINGMANY or DISCARDING states.  If it discovers that there
2977179193Sjb * is work to be done, it cross calls all CPUs to perform that work;
2978179193Sjb * COMMITMANY and DISCARDING speculations may not be transitioned back to the
2979179193Sjb * INACTIVE state until they have been cleaned by all CPUs.
2980179193Sjb */
2981179193Sjbstatic void
2982179193Sjbdtrace_speculation_clean(dtrace_state_t *state)
2983179193Sjb{
2984179193Sjb	int work = 0, rv;
2985179193Sjb	dtrace_specid_t i;
2986179193Sjb
2987179193Sjb	for (i = 0; i < state->dts_nspeculations; i++) {
2988179193Sjb		dtrace_speculation_t *spec = &state->dts_speculations[i];
2989179193Sjb
2990179193Sjb		ASSERT(!spec->dtsp_cleaning);
2991179193Sjb
2992179193Sjb		if (spec->dtsp_state != DTRACESPEC_DISCARDING &&
2993179193Sjb		    spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
2994179193Sjb			continue;
2995179193Sjb
2996179193Sjb		work++;
2997179193Sjb		spec->dtsp_cleaning = 1;
2998179193Sjb	}
2999179193Sjb
3000179193Sjb	if (!work)
3001179193Sjb		return;
3002179193Sjb
3003179193Sjb	dtrace_xcall(DTRACE_CPUALL,
3004179193Sjb	    (dtrace_xcall_t)dtrace_speculation_clean_here, state);
3005179193Sjb
3006179193Sjb	/*
3007179193Sjb	 * We now know that all CPUs have committed or discarded their
3008179193Sjb	 * speculation buffers, as appropriate.  We can now set the state
3009179193Sjb	 * to inactive.
3010179193Sjb	 */
3011179193Sjb	for (i = 0; i < state->dts_nspeculations; i++) {
3012179193Sjb		dtrace_speculation_t *spec = &state->dts_speculations[i];
3013179193Sjb		dtrace_speculation_state_t current, new;
3014179193Sjb
3015179193Sjb		if (!spec->dtsp_cleaning)
3016179193Sjb			continue;
3017179193Sjb
3018179193Sjb		current = spec->dtsp_state;
3019179193Sjb		ASSERT(current == DTRACESPEC_DISCARDING ||
3020179193Sjb		    current == DTRACESPEC_COMMITTINGMANY);
3021179193Sjb
3022179193Sjb		new = DTRACESPEC_INACTIVE;
3023179193Sjb
3024179193Sjb		rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new);
3025179193Sjb		ASSERT(rv == current);
3026179193Sjb		spec->dtsp_cleaning = 0;
3027179193Sjb	}
3028179193Sjb}
3029179193Sjb
3030179193Sjb/*
3031179193Sjb * Called as part of a speculate() to get the speculative buffer associated
3032179193Sjb * with a given speculation.  Returns NULL if the specified speculation is not
3033179193Sjb * in an ACTIVE state.  If the speculation is in the ACTIVEONE state -- and
3034179193Sjb * the active CPU is not the specified CPU -- the speculation will be
3035179193Sjb * atomically transitioned into the ACTIVEMANY state.
3036179193Sjb */
3037179193Sjbstatic dtrace_buffer_t *
3038179193Sjbdtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid,
3039179193Sjb    dtrace_specid_t which)
3040179193Sjb{
3041179193Sjb	dtrace_speculation_t *spec;
3042179198Sjb	dtrace_speculation_state_t current, new = 0;
3043179193Sjb	dtrace_buffer_t *buf;
3044179193Sjb
3045179193Sjb	if (which == 0)
3046179193Sjb		return (NULL);
3047179193Sjb
3048179193Sjb	if (which > state->dts_nspeculations) {
3049179193Sjb		cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
3050179193Sjb		return (NULL);
3051179193Sjb	}
3052179193Sjb
3053179193Sjb	spec = &state->dts_speculations[which - 1];
3054179193Sjb	buf = &spec->dtsp_buffer[cpuid];
3055179193Sjb
3056179193Sjb	do {
3057179193Sjb		current = spec->dtsp_state;
3058179193Sjb
3059179193Sjb		switch (current) {
3060179193Sjb		case DTRACESPEC_INACTIVE:
3061179193Sjb		case DTRACESPEC_COMMITTINGMANY:
3062179193Sjb		case DTRACESPEC_DISCARDING:
3063179193Sjb			return (NULL);
3064179193Sjb
3065179193Sjb		case DTRACESPEC_COMMITTING:
3066179193Sjb			ASSERT(buf->dtb_offset == 0);
3067179193Sjb			return (NULL);
3068179193Sjb
3069179193Sjb		case DTRACESPEC_ACTIVEONE:
3070179193Sjb			/*
3071179193Sjb			 * This speculation is currently active on one CPU.
3072179193Sjb			 * Check the offset in the buffer; if it's non-zero,
3073179193Sjb			 * that CPU must be us (and we leave the state alone).
3074179193Sjb			 * If it's zero, assume that we're starting on a new
3075179193Sjb			 * CPU -- and change the state to indicate that the
3076179193Sjb			 * speculation is active on more than one CPU.
3077179193Sjb			 */
3078179193Sjb			if (buf->dtb_offset != 0)
3079179193Sjb				return (buf);
3080179193Sjb
3081179193Sjb			new = DTRACESPEC_ACTIVEMANY;
3082179193Sjb			break;
3083179193Sjb
3084179193Sjb		case DTRACESPEC_ACTIVEMANY:
3085179193Sjb			return (buf);
3086179193Sjb
3087179193Sjb		case DTRACESPEC_ACTIVE:
3088179193Sjb			new = DTRACESPEC_ACTIVEONE;
3089179193Sjb			break;
3090179193Sjb
3091179193Sjb		default:
3092179193Sjb			ASSERT(0);
3093179193Sjb		}
3094179193Sjb	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
3095179193Sjb	    current, new) != current);
3096179193Sjb
3097179193Sjb	ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY);
3098179193Sjb	return (buf);
3099179193Sjb}
3100179193Sjb
3101179193Sjb/*
3102179193Sjb * Return a string.  In the event that the user lacks the privilege to access
3103179193Sjb * arbitrary kernel memory, we copy the string out to scratch memory so that we
3104179193Sjb * don't fail access checking.
3105179193Sjb *
3106179193Sjb * dtrace_dif_variable() uses this routine as a helper for various
3107179193Sjb * builtin values such as 'execname' and 'probefunc.'
3108179193Sjb */
3109179193Sjbuintptr_t
3110179193Sjbdtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state,
3111179193Sjb    dtrace_mstate_t *mstate)
3112179193Sjb{
3113179193Sjb	uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
3114179193Sjb	uintptr_t ret;
3115179193Sjb	size_t strsz;
3116179193Sjb
3117179193Sjb	/*
3118179193Sjb	 * The easy case: this probe is allowed to read all of memory, so
3119179193Sjb	 * we can just return this as a vanilla pointer.
3120179193Sjb	 */
3121179193Sjb	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
3122179193Sjb		return (addr);
3123179193Sjb
3124179193Sjb	/*
3125179193Sjb	 * This is the tougher case: we copy the string in question from
3126179193Sjb	 * kernel memory into scratch memory and return it that way: this
3127179193Sjb	 * ensures that we won't trip up when access checking tests the
3128179193Sjb	 * BYREF return value.
3129179193Sjb	 */
3130179193Sjb	strsz = dtrace_strlen((char *)addr, size) + 1;
3131179193Sjb
3132179193Sjb	if (mstate->dtms_scratch_ptr + strsz >
3133179193Sjb	    mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
3134179193Sjb		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3135179198Sjb		return (0);
3136179193Sjb	}
3137179193Sjb
3138179193Sjb	dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
3139179193Sjb	    strsz);
3140179193Sjb	ret = mstate->dtms_scratch_ptr;
3141179193Sjb	mstate->dtms_scratch_ptr += strsz;
3142179193Sjb	return (ret);
3143179193Sjb}
3144179193Sjb
3145179193Sjb/*
3146179198Sjb * Return a string from a memoy address which is known to have one or
3147179198Sjb * more concatenated, individually zero terminated, sub-strings.
3148179198Sjb * In the event that the user lacks the privilege to access
3149179198Sjb * arbitrary kernel memory, we copy the string out to scratch memory so that we
3150179198Sjb * don't fail access checking.
3151179198Sjb *
3152179198Sjb * dtrace_dif_variable() uses this routine as a helper for various
3153179198Sjb * builtin values such as 'execargs'.
3154179198Sjb */
3155179198Sjbstatic uintptr_t
3156179198Sjbdtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state,
3157179198Sjb    dtrace_mstate_t *mstate)
3158179198Sjb{
3159179198Sjb	char *p;
3160179198Sjb	size_t i;
3161179198Sjb	uintptr_t ret;
3162179198Sjb
3163179198Sjb	if (mstate->dtms_scratch_ptr + strsz >
3164179198Sjb	    mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
3165179198Sjb		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
3166179198Sjb		return (0);
3167179198Sjb	}
3168179198Sjb
3169179198Sjb	dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
3170179198Sjb	    strsz);
3171179198Sjb
3172179198Sjb	/* Replace sub-string termination characters with a space. */
3173179198Sjb	for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1;
3174179198Sjb	    p++, i++)
3175179198Sjb		if (*p == '\0')
3176179198Sjb			*p = ' ';
3177179198Sjb
3178179198Sjb	ret = mstate->dtms_scratch_ptr;
3179179198Sjb	mstate->dtms_scratch_ptr += strsz;
3180179198Sjb	return (ret);
3181179198Sjb}
3182179198Sjb
3183179198Sjb/*
3184179193Sjb * This function implements the DIF emulator's variable lookups.  The emulator
3185179193Sjb * passes a reserved variable identifier and optional built-in array index.
3186179193Sjb */
3187179193Sjbstatic uint64_t
3188179193Sjbdtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v,
3189179193Sjb    uint64_t ndx)
3190179193Sjb{
3191179193Sjb	/*
3192179193Sjb	 * If we're accessing one of the uncached arguments, we'll turn this
3193179193Sjb	 * into a reference in the args array.
3194179193Sjb	 */
3195179193Sjb	if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) {
3196179193Sjb		ndx = v - DIF_VAR_ARG0;
3197179193Sjb		v = DIF_VAR_ARGS;
3198179193Sjb	}
3199179193Sjb
3200179193Sjb	switch (v) {
3201179193Sjb	case DIF_VAR_ARGS:
3202179193Sjb		ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS);
3203179193Sjb		if (ndx >= sizeof (mstate->dtms_arg) /
3204179193Sjb		    sizeof (mstate->dtms_arg[0])) {
3205179193Sjb			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3206179193Sjb			dtrace_provider_t *pv;
3207179193Sjb			uint64_t val;
3208179193Sjb
3209179193Sjb			pv = mstate->dtms_probe->dtpr_provider;
3210179193Sjb			if (pv->dtpv_pops.dtps_getargval != NULL)
3211179193Sjb				val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg,
3212179193Sjb				    mstate->dtms_probe->dtpr_id,
3213179193Sjb				    mstate->dtms_probe->dtpr_arg, ndx, aframes);
3214179193Sjb			else
3215179193Sjb				val = dtrace_getarg(ndx, aframes);
3216179193Sjb
3217179193Sjb			/*
3218179193Sjb			 * This is regrettably required to keep the compiler
3219179193Sjb			 * from tail-optimizing the call to dtrace_getarg().
3220179193Sjb			 * The condition always evaluates to true, but the
3221179193Sjb			 * compiler has no way of figuring that out a priori.
3222179193Sjb			 * (None of this would be necessary if the compiler
3223179193Sjb			 * could be relied upon to _always_ tail-optimize
3224179193Sjb			 * the call to dtrace_getarg() -- but it can't.)
3225179193Sjb			 */
3226179193Sjb			if (mstate->dtms_probe != NULL)
3227179193Sjb				return (val);
3228179193Sjb
3229179193Sjb			ASSERT(0);
3230179193Sjb		}
3231179193Sjb
3232179193Sjb		return (mstate->dtms_arg[ndx]);
3233179193Sjb
3234277300Ssmh#ifdef illumos
3235179193Sjb	case DIF_VAR_UREGS: {
3236179193Sjb		klwp_t *lwp;
3237179193Sjb
3238179193Sjb		if (!dtrace_priv_proc(state))
3239179193Sjb			return (0);
3240179193Sjb
3241179193Sjb		if ((lwp = curthread->t_lwp) == NULL) {
3242179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
3243179198Sjb			cpu_core[curcpu].cpuc_dtrace_illval = NULL;
3244179193Sjb			return (0);
3245179193Sjb		}
3246179193Sjb
3247179193Sjb		return (dtrace_getreg(lwp->lwp_regs, ndx));
3248179198Sjb		return (0);
3249179193Sjb	}
3250211608Srpaulo#else
3251211608Srpaulo	case DIF_VAR_UREGS: {
3252211608Srpaulo		struct trapframe *tframe;
3253211608Srpaulo
3254211608Srpaulo		if (!dtrace_priv_proc(state))
3255211608Srpaulo			return (0);
3256211608Srpaulo
3257211608Srpaulo		if ((tframe = curthread->td_frame) == NULL) {
3258211608Srpaulo			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
3259211608Srpaulo			cpu_core[curcpu].cpuc_dtrace_illval = 0;
3260211608Srpaulo			return (0);
3261211608Srpaulo		}
3262211608Srpaulo
3263211608Srpaulo		return (dtrace_getreg(tframe, ndx));
3264211608Srpaulo	}
3265179198Sjb#endif
3266179193Sjb
3267179193Sjb	case DIF_VAR_CURTHREAD:
3268267929Srpaulo		if (!dtrace_priv_proc(state))
3269179193Sjb			return (0);
3270179193Sjb		return ((uint64_t)(uintptr_t)curthread);
3271179193Sjb
3272179193Sjb	case DIF_VAR_TIMESTAMP:
3273179193Sjb		if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
3274179193Sjb			mstate->dtms_timestamp = dtrace_gethrtime();
3275179193Sjb			mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP;
3276179193Sjb		}
3277179193Sjb		return (mstate->dtms_timestamp);
3278179193Sjb
3279179193Sjb	case DIF_VAR_VTIMESTAMP:
3280179193Sjb		ASSERT(dtrace_vtime_references != 0);
3281179193Sjb		return (curthread->t_dtrace_vtime);
3282179193Sjb
3283179193Sjb	case DIF_VAR_WALLTIMESTAMP:
3284179193Sjb		if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) {
3285179193Sjb			mstate->dtms_walltimestamp = dtrace_gethrestime();
3286179193Sjb			mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP;
3287179193Sjb		}
3288179193Sjb		return (mstate->dtms_walltimestamp);
3289179193Sjb
3290277300Ssmh#ifdef illumos
3291179193Sjb	case DIF_VAR_IPL:
3292179193Sjb		if (!dtrace_priv_kernel(state))
3293179193Sjb			return (0);
3294179193Sjb		if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) {
3295179193Sjb			mstate->dtms_ipl = dtrace_getipl();
3296179193Sjb			mstate->dtms_present |= DTRACE_MSTATE_IPL;
3297179193Sjb		}
3298179193Sjb		return (mstate->dtms_ipl);
3299179198Sjb#endif
3300179193Sjb
3301179193Sjb	case DIF_VAR_EPID:
3302179193Sjb		ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID);
3303179193Sjb		return (mstate->dtms_epid);
3304179193Sjb
3305179193Sjb	case DIF_VAR_ID:
3306179193Sjb		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3307179193Sjb		return (mstate->dtms_probe->dtpr_id);
3308179193Sjb
3309179193Sjb	case DIF_VAR_STACKDEPTH:
3310179193Sjb		if (!dtrace_priv_kernel(state))
3311179193Sjb			return (0);
3312179193Sjb		if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) {
3313179193Sjb			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3314179193Sjb
3315179193Sjb			mstate->dtms_stackdepth = dtrace_getstackdepth(aframes);
3316179193Sjb			mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH;
3317179193Sjb		}
3318179193Sjb		return (mstate->dtms_stackdepth);
3319179193Sjb
3320179193Sjb	case DIF_VAR_USTACKDEPTH:
3321179193Sjb		if (!dtrace_priv_proc(state))
3322179193Sjb			return (0);
3323179193Sjb		if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) {
3324179193Sjb			/*
3325179193Sjb			 * See comment in DIF_VAR_PID.
3326179193Sjb			 */
3327179193Sjb			if (DTRACE_ANCHORED(mstate->dtms_probe) &&
3328179193Sjb			    CPU_ON_INTR(CPU)) {
3329179193Sjb				mstate->dtms_ustackdepth = 0;
3330179193Sjb			} else {
3331179193Sjb				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3332179193Sjb				mstate->dtms_ustackdepth =
3333179193Sjb				    dtrace_getustackdepth();
3334179193Sjb				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3335179193Sjb			}
3336179193Sjb			mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH;
3337179193Sjb		}
3338179193Sjb		return (mstate->dtms_ustackdepth);
3339179193Sjb
3340179193Sjb	case DIF_VAR_CALLER:
3341179193Sjb		if (!dtrace_priv_kernel(state))
3342179193Sjb			return (0);
3343179193Sjb		if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) {
3344179193Sjb			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
3345179193Sjb
3346179193Sjb			if (!DTRACE_ANCHORED(mstate->dtms_probe)) {
3347179193Sjb				/*
3348179193Sjb				 * If this is an unanchored probe, we are
3349179193Sjb				 * required to go through the slow path:
3350179193Sjb				 * dtrace_caller() only guarantees correct
3351179193Sjb				 * results for anchored probes.
3352179193Sjb				 */
3353179198Sjb				pc_t caller[2] = {0, 0};
3354179193Sjb
3355179193Sjb				dtrace_getpcstack(caller, 2, aframes,
3356179193Sjb				    (uint32_t *)(uintptr_t)mstate->dtms_arg[0]);
3357179193Sjb				mstate->dtms_caller = caller[1];
3358179193Sjb			} else if ((mstate->dtms_caller =
3359179193Sjb			    dtrace_caller(aframes)) == -1) {
3360179193Sjb				/*
3361179193Sjb				 * We have failed to do this the quick way;
3362179193Sjb				 * we must resort to the slower approach of
3363179193Sjb				 * calling dtrace_getpcstack().
3364179193Sjb				 */
3365179198Sjb				pc_t caller = 0;
3366179193Sjb
3367179193Sjb				dtrace_getpcstack(&caller, 1, aframes, NULL);
3368179193Sjb				mstate->dtms_caller = caller;
3369179193Sjb			}
3370179193Sjb
3371179193Sjb			mstate->dtms_present |= DTRACE_MSTATE_CALLER;
3372179193Sjb		}
3373179193Sjb		return (mstate->dtms_caller);
3374179193Sjb
3375179193Sjb	case DIF_VAR_UCALLER:
3376179193Sjb		if (!dtrace_priv_proc(state))
3377179193Sjb			return (0);
3378179193Sjb
3379179193Sjb		if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) {
3380179193Sjb			uint64_t ustack[3];
3381179193Sjb
3382179193Sjb			/*
3383179193Sjb			 * dtrace_getupcstack() fills in the first uint64_t
3384179193Sjb			 * with the current PID.  The second uint64_t will
3385179193Sjb			 * be the program counter at user-level.  The third
3386179193Sjb			 * uint64_t will contain the caller, which is what
3387179193Sjb			 * we're after.
3388179193Sjb			 */
3389179198Sjb			ustack[2] = 0;
3390179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
3391179193Sjb			dtrace_getupcstack(ustack, 3);
3392179193Sjb			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
3393179193Sjb			mstate->dtms_ucaller = ustack[2];
3394179193Sjb			mstate->dtms_present |= DTRACE_MSTATE_UCALLER;
3395179193Sjb		}
3396179193Sjb
3397179193Sjb		return (mstate->dtms_ucaller);
3398179193Sjb
3399179193Sjb	case DIF_VAR_PROBEPROV:
3400179193Sjb		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3401179193Sjb		return (dtrace_dif_varstr(
3402179193Sjb		    (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name,
3403179193Sjb		    state, mstate));
3404179193Sjb
3405179193Sjb	case DIF_VAR_PROBEMOD:
3406179193Sjb		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3407179193Sjb		return (dtrace_dif_varstr(
3408179193Sjb		    (uintptr_t)mstate->dtms_probe->dtpr_mod,
3409179193Sjb		    state, mstate));
3410179193Sjb
3411179193Sjb	case DIF_VAR_PROBEFUNC:
3412179193Sjb		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3413179193Sjb		return (dtrace_dif_varstr(
3414179193Sjb		    (uintptr_t)mstate->dtms_probe->dtpr_func,
3415179193Sjb		    state, mstate));
3416179193Sjb
3417179193Sjb	case DIF_VAR_PROBENAME:
3418179193Sjb		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
3419179193Sjb		return (dtrace_dif_varstr(
3420179193Sjb		    (uintptr_t)mstate->dtms_probe->dtpr_name,
3421179193Sjb		    state, mstate));
3422179193Sjb
3423179193Sjb	case DIF_VAR_PID:
3424179193Sjb		if (!dtrace_priv_proc(state))
3425179193Sjb			return (0);
3426179193Sjb
3427277300Ssmh#ifdef illumos
3428179193Sjb		/*
3429179193Sjb		 * Note that we are assuming that an unanchored probe is
3430179193Sjb		 * always due to a high-level interrupt.  (And we're assuming
3431179193Sjb		 * that there is only a single high level interrupt.)
3432179193Sjb		 */
3433179193Sjb		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3434179193Sjb			return (pid0.pid_id);
3435179193Sjb
3436179193Sjb		/*
3437179193Sjb		 * It is always safe to dereference one's own t_procp pointer:
3438179193Sjb		 * it always points to a valid, allocated proc structure.
3439179193Sjb		 * Further, it is always safe to dereference the p_pidp member
3440179193Sjb		 * of one's own proc structure.  (These are truisms becuase
3441179193Sjb		 * threads and processes don't clean up their own state --
3442179193Sjb		 * they leave that task to whomever reaps them.)
3443179193Sjb		 */
3444179193Sjb		return ((uint64_t)curthread->t_procp->p_pidp->pid_id);
3445179198Sjb#else
3446179198Sjb		return ((uint64_t)curproc->p_pid);
3447179198Sjb#endif
3448179193Sjb
3449179193Sjb	case DIF_VAR_PPID:
3450179193Sjb		if (!dtrace_priv_proc(state))
3451179193Sjb			return (0);
3452179193Sjb
3453277300Ssmh#ifdef illumos
3454179193Sjb		/*
3455179193Sjb		 * See comment in DIF_VAR_PID.
3456179193Sjb		 */
3457179193Sjb		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3458179193Sjb			return (pid0.pid_id);
3459179193Sjb
3460179193Sjb		/*
3461179193Sjb		 * It is always safe to dereference one's own t_procp pointer:
3462179193Sjb		 * it always points to a valid, allocated proc structure.
3463179193Sjb		 * (This is true because threads don't clean up their own
3464179193Sjb		 * state -- they leave that task to whomever reaps them.)
3465179193Sjb		 */
3466179193Sjb		return ((uint64_t)curthread->t_procp->p_ppid);
3467179198Sjb#else
3468269525Smarkj		if (curproc->p_pid == proc0.p_pid)
3469269525Smarkj			return (curproc->p_pid);
3470269525Smarkj		else
3471269525Smarkj			return (curproc->p_pptr->p_pid);
3472179198Sjb#endif
3473179193Sjb
3474179193Sjb	case DIF_VAR_TID:
3475277300Ssmh#ifdef illumos
3476179193Sjb		/*
3477179193Sjb		 * See comment in DIF_VAR_PID.
3478179193Sjb		 */
3479179193Sjb		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3480179193Sjb			return (0);
3481179198Sjb#endif
3482179193Sjb
3483179193Sjb		return ((uint64_t)curthread->t_tid);
3484179193Sjb
3485179198Sjb	case DIF_VAR_EXECARGS: {
3486179198Sjb		struct pargs *p_args = curthread->td_proc->p_args;
3487179198Sjb
3488184698Srodrigc		if (p_args == NULL)
3489184698Srodrigc			return(0);
3490184698Srodrigc
3491179198Sjb		return (dtrace_dif_varstrz(
3492179198Sjb		    (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate));
3493179198Sjb	}
3494179198Sjb
3495179193Sjb	case DIF_VAR_EXECNAME:
3496277300Ssmh#ifdef illumos
3497179193Sjb		if (!dtrace_priv_proc(state))
3498179193Sjb			return (0);
3499179193Sjb
3500179193Sjb		/*
3501179193Sjb		 * See comment in DIF_VAR_PID.
3502179193Sjb		 */
3503179193Sjb		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3504179193Sjb			return ((uint64_t)(uintptr_t)p0.p_user.u_comm);
3505179193Sjb
3506179193Sjb		/*
3507179193Sjb		 * It is always safe to dereference one's own t_procp pointer:
3508179193Sjb		 * it always points to a valid, allocated proc structure.
3509179193Sjb		 * (This is true because threads don't clean up their own
3510179193Sjb		 * state -- they leave that task to whomever reaps them.)
3511179193Sjb		 */
3512179193Sjb		return (dtrace_dif_varstr(
3513179193Sjb		    (uintptr_t)curthread->t_procp->p_user.u_comm,
3514179193Sjb		    state, mstate));
3515179198Sjb#else
3516179198Sjb		return (dtrace_dif_varstr(
3517179198Sjb		    (uintptr_t) curthread->td_proc->p_comm, state, mstate));
3518179198Sjb#endif
3519179193Sjb
3520179193Sjb	case DIF_VAR_ZONENAME:
3521277300Ssmh#ifdef illumos
3522179193Sjb		if (!dtrace_priv_proc(state))
3523179193Sjb			return (0);
3524179193Sjb
3525179193Sjb		/*
3526179193Sjb		 * See comment in DIF_VAR_PID.
3527179193Sjb		 */
3528179193Sjb		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3529179193Sjb			return ((uint64_t)(uintptr_t)p0.p_zone->zone_name);
3530179193Sjb
3531179193Sjb		/*
3532179193Sjb		 * It is always safe to dereference one's own t_procp pointer:
3533179193Sjb		 * it always points to a valid, allocated proc structure.
3534179193Sjb		 * (This is true because threads don't clean up their own
3535179193Sjb		 * state -- they leave that task to whomever reaps them.)
3536179193Sjb		 */
3537179193Sjb		return (dtrace_dif_varstr(
3538179193Sjb		    (uintptr_t)curthread->t_procp->p_zone->zone_name,
3539179193Sjb		    state, mstate));
3540329249Smarkj#elif defined(__FreeBSD__)
3541329249Smarkj	/*
3542329249Smarkj	 * On FreeBSD, we introduce compatibility to zonename by falling through
3543329249Smarkj	 * into jailname.
3544329249Smarkj	 */
3545329249Smarkj	case DIF_VAR_JAILNAME:
3546329249Smarkj		if (!dtrace_priv_kernel(state))
3547329249Smarkj			return (0);
3548329249Smarkj
3549329249Smarkj		return (dtrace_dif_varstr(
3550329249Smarkj		    (uintptr_t)curthread->td_ucred->cr_prison->pr_name,
3551329249Smarkj		    state, mstate));
3552329249Smarkj
3553329249Smarkj	case DIF_VAR_JID:
3554329249Smarkj		if (!dtrace_priv_kernel(state))
3555329249Smarkj			return (0);
3556329249Smarkj
3557329249Smarkj		return ((uint64_t)curthread->td_ucred->cr_prison->pr_id);
3558179198Sjb#else
3559179198Sjb		return (0);
3560179198Sjb#endif
3561179193Sjb
3562179193Sjb	case DIF_VAR_UID:
3563179193Sjb		if (!dtrace_priv_proc(state))
3564179193Sjb			return (0);
3565179193Sjb
3566277300Ssmh#ifdef illumos
3567179193Sjb		/*
3568179193Sjb		 * See comment in DIF_VAR_PID.
3569179193Sjb		 */
3570179193Sjb		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3571179193Sjb			return ((uint64_t)p0.p_cred->cr_uid);
3572179193Sjb
3573179193Sjb		/*
3574179193Sjb		 * It is always safe to dereference one's own t_procp pointer:
3575179193Sjb		 * it always points to a valid, allocated proc structure.
3576179193Sjb		 * (This is true because threads don't clean up their own
3577179193Sjb		 * state -- they leave that task to whomever reaps them.)
3578179193Sjb		 *
3579179193Sjb		 * Additionally, it is safe to dereference one's own process
3580179193Sjb		 * credential, since this is never NULL after process birth.
3581179193Sjb		 */
3582179193Sjb		return ((uint64_t)curthread->t_procp->p_cred->cr_uid);
3583286167Smarkj#else
3584286167Smarkj		return ((uint64_t)curthread->td_ucred->cr_uid);
3585286167Smarkj#endif
3586179193Sjb
3587179193Sjb	case DIF_VAR_GID:
3588179193Sjb		if (!dtrace_priv_proc(state))
3589179193Sjb			return (0);
3590179193Sjb
3591277300Ssmh#ifdef illumos
3592179193Sjb		/*
3593179193Sjb		 * See comment in DIF_VAR_PID.
3594179193Sjb		 */
3595179193Sjb		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3596179193Sjb			return ((uint64_t)p0.p_cred->cr_gid);
3597179193Sjb
3598179193Sjb		/*
3599179193Sjb		 * It is always safe to dereference one's own t_procp pointer:
3600179193Sjb		 * it always points to a valid, allocated proc structure.
3601179193Sjb		 * (This is true because threads don't clean up their own
3602179193Sjb		 * state -- they leave that task to whomever reaps them.)
3603179193Sjb		 *
3604179193Sjb		 * Additionally, it is safe to dereference one's own process
3605179193Sjb		 * credential, since this is never NULL after process birth.
3606179193Sjb		 */
3607179193Sjb		return ((uint64_t)curthread->t_procp->p_cred->cr_gid);
3608286167Smarkj#else
3609286167Smarkj		return ((uint64_t)curthread->td_ucred->cr_gid);
3610286167Smarkj#endif
3611179193Sjb
3612179193Sjb	case DIF_VAR_ERRNO: {
3613277300Ssmh#ifdef illumos
3614179193Sjb		klwp_t *lwp;
3615179193Sjb		if (!dtrace_priv_proc(state))
3616179193Sjb			return (0);
3617179193Sjb
3618179193Sjb		/*
3619179193Sjb		 * See comment in DIF_VAR_PID.
3620179193Sjb		 */
3621179193Sjb		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
3622179193Sjb			return (0);
3623179193Sjb
3624179193Sjb		/*
3625179193Sjb		 * It is always safe to dereference one's own t_lwp pointer in
3626179193Sjb		 * the event that this pointer is non-NULL.  (This is true
3627179193Sjb		 * because threads and lwps don't clean up their own state --
3628179193Sjb		 * they leave that task to whomever reaps them.)
3629179193Sjb		 */
3630179193Sjb		if ((lwp = curthread->t_lwp) == NULL)
3631179193Sjb			return (0);
3632179193Sjb
3633179193Sjb		return ((uint64_t)lwp->lwp_errno);
3634179198Sjb#else
3635179198Sjb		return (curthread->td_errno);
3636179198Sjb#endif
3637179193Sjb	}
3638277300Ssmh#ifndef illumos
3639234691Srstone	case DIF_VAR_CPU: {
3640234691Srstone		return curcpu;
3641234691Srstone	}
3642234691Srstone#endif
3643179193Sjb	default:
3644179193Sjb		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
3645179193Sjb		return (0);
3646179193Sjb	}
3647179193Sjb}
3648179193Sjb
3649267937Srpaulo
3650267937Srpaulotypedef enum dtrace_json_state {
3651267937Srpaulo	DTRACE_JSON_REST = 1,
3652267937Srpaulo	DTRACE_JSON_OBJECT,
3653267937Srpaulo	DTRACE_JSON_STRING,
3654267937Srpaulo	DTRACE_JSON_STRING_ESCAPE,
3655267937Srpaulo	DTRACE_JSON_STRING_ESCAPE_UNICODE,
3656267937Srpaulo	DTRACE_JSON_COLON,
3657267937Srpaulo	DTRACE_JSON_COMMA,
3658267937Srpaulo	DTRACE_JSON_VALUE,
3659267937Srpaulo	DTRACE_JSON_IDENTIFIER,
3660267937Srpaulo	DTRACE_JSON_NUMBER,
3661267937Srpaulo	DTRACE_JSON_NUMBER_FRAC,
3662267937Srpaulo	DTRACE_JSON_NUMBER_EXP,
3663267937Srpaulo	DTRACE_JSON_COLLECT_OBJECT
3664267937Srpaulo} dtrace_json_state_t;
3665267937Srpaulo
3666179193Sjb/*
3667267937Srpaulo * This function possesses just enough knowledge about JSON to extract a single
3668267937Srpaulo * value from a JSON string and store it in the scratch buffer.  It is able
3669267937Srpaulo * to extract nested object values, and members of arrays by index.
3670267937Srpaulo *
3671267937Srpaulo * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to
3672267937Srpaulo * be looked up as we descend into the object tree.  e.g.
3673267937Srpaulo *
3674267937Srpaulo *    foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL
3675267937Srpaulo *       with nelems = 5.
3676267937Srpaulo *
3677267937Srpaulo * The run time of this function must be bounded above by strsize to limit the
3678267937Srpaulo * amount of work done in probe context.  As such, it is implemented as a
3679267937Srpaulo * simple state machine, reading one character at a time using safe loads
3680267937Srpaulo * until we find the requested element, hit a parsing error or run off the
3681267937Srpaulo * end of the object or string.
3682267937Srpaulo *
3683267937Srpaulo * As there is no way for a subroutine to return an error without interrupting
3684267937Srpaulo * clause execution, we simply return NULL in the event of a missing key or any
3685267937Srpaulo * other error condition.  Each NULL return in this function is commented with
3686267937Srpaulo * the error condition it represents -- parsing or otherwise.
3687267937Srpaulo *
3688267937Srpaulo * The set of states for the state machine closely matches the JSON
3689267937Srpaulo * specification (http://json.org/).  Briefly:
3690267937Srpaulo *
3691267937Srpaulo *   DTRACE_JSON_REST:
3692267937Srpaulo *     Skip whitespace until we find either a top-level Object, moving
3693267937Srpaulo *     to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE.
3694267937Srpaulo *
3695267937Srpaulo *   DTRACE_JSON_OBJECT:
3696267937Srpaulo *     Locate the next key String in an Object.  Sets a flag to denote
3697267937Srpaulo *     the next String as a key string and moves to DTRACE_JSON_STRING.
3698267937Srpaulo *
3699267937Srpaulo *   DTRACE_JSON_COLON:
3700267937Srpaulo *     Skip whitespace until we find the colon that separates key Strings
3701267937Srpaulo *     from their values.  Once found, move to DTRACE_JSON_VALUE.
3702267937Srpaulo *
3703267937Srpaulo *   DTRACE_JSON_VALUE:
3704267937Srpaulo *     Detects the type of the next value (String, Number, Identifier, Object
3705267937Srpaulo *     or Array) and routes to the states that process that type.  Here we also
3706267937Srpaulo *     deal with the element selector list if we are requested to traverse down
3707267937Srpaulo *     into the object tree.
3708267937Srpaulo *
3709267937Srpaulo *   DTRACE_JSON_COMMA:
3710267937Srpaulo *     Skip whitespace until we find the comma that separates key-value pairs
3711267937Srpaulo *     in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays
3712267937Srpaulo *     (similarly DTRACE_JSON_VALUE).  All following literal value processing
3713267937Srpaulo *     states return to this state at the end of their value, unless otherwise
3714267937Srpaulo *     noted.
3715267937Srpaulo *
3716267937Srpaulo *   DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP:
3717267937Srpaulo *     Processes a Number literal from the JSON, including any exponent
3718267937Srpaulo *     component that may be present.  Numbers are returned as strings, which
3719267937Srpaulo *     may be passed to strtoll() if an integer is required.
3720267937Srpaulo *
3721267937Srpaulo *   DTRACE_JSON_IDENTIFIER:
3722267937Srpaulo *     Processes a "true", "false" or "null" literal in the JSON.
3723267937Srpaulo *
3724267937Srpaulo *   DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE,
3725267937Srpaulo *   DTRACE_JSON_STRING_ESCAPE_UNICODE:
3726267937Srpaulo *     Processes a String literal from the JSON, whether the String denotes
3727267937Srpaulo *     a key, a value or part of a larger Object.  Handles all escape sequences
3728267937Srpaulo *     present in the specification, including four-digit unicode characters,
3729267937Srpaulo *     but merely includes the escape sequence without converting it to the
3730267937Srpaulo *     actual escaped character.  If the String is flagged as a key, we
3731267937Srpaulo *     move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA.
3732267937Srpaulo *
3733267937Srpaulo *   DTRACE_JSON_COLLECT_OBJECT:
3734267937Srpaulo *     This state collects an entire Object (or Array), correctly handling
3735267937Srpaulo *     embedded strings.  If the full element selector list matches this nested
3736267937Srpaulo *     object, we return the Object in full as a string.  If not, we use this
3737267937Srpaulo *     state to skip to the next value at this level and continue processing.
3738267937Srpaulo *
3739267937Srpaulo * NOTE: This function uses various macros from strtolctype.h to manipulate
3740267937Srpaulo * digit values, etc -- these have all been checked to ensure they make
3741267937Srpaulo * no additional function calls.
3742267937Srpaulo */
3743267937Srpaulostatic char *
3744267937Srpaulodtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems,
3745267937Srpaulo    char *dest)
3746267937Srpaulo{
3747267937Srpaulo	dtrace_json_state_t state = DTRACE_JSON_REST;
3748267937Srpaulo	int64_t array_elem = INT64_MIN;
3749267937Srpaulo	int64_t array_pos = 0;
3750267937Srpaulo	uint8_t escape_unicount = 0;
3751267937Srpaulo	boolean_t string_is_key = B_FALSE;
3752267937Srpaulo	boolean_t collect_object = B_FALSE;
3753267937Srpaulo	boolean_t found_key = B_FALSE;
3754267937Srpaulo	boolean_t in_array = B_FALSE;
3755267937Srpaulo	uint32_t braces = 0, brackets = 0;
3756267937Srpaulo	char *elem = elemlist;
3757267937Srpaulo	char *dd = dest;
3758267937Srpaulo	uintptr_t cur;
3759267937Srpaulo
3760267937Srpaulo	for (cur = json; cur < json + size; cur++) {
3761267937Srpaulo		char cc = dtrace_load8(cur);
3762267937Srpaulo		if (cc == '\0')
3763267937Srpaulo			return (NULL);
3764267937Srpaulo
3765267937Srpaulo		switch (state) {
3766267937Srpaulo		case DTRACE_JSON_REST:
3767267937Srpaulo			if (isspace(cc))
3768267937Srpaulo				break;
3769267937Srpaulo
3770267937Srpaulo			if (cc == '{') {
3771267937Srpaulo				state = DTRACE_JSON_OBJECT;
3772267937Srpaulo				break;
3773267937Srpaulo			}
3774267937Srpaulo
3775267937Srpaulo			if (cc == '[') {
3776267937Srpaulo				in_array = B_TRUE;
3777267937Srpaulo				array_pos = 0;
3778267937Srpaulo				array_elem = dtrace_strtoll(elem, 10, size);
3779267937Srpaulo				found_key = array_elem == 0 ? B_TRUE : B_FALSE;
3780267937Srpaulo				state = DTRACE_JSON_VALUE;
3781267937Srpaulo				break;
3782267937Srpaulo			}
3783267937Srpaulo
3784267937Srpaulo			/*
3785267937Srpaulo			 * ERROR: expected to find a top-level object or array.
3786267937Srpaulo			 */
3787267937Srpaulo			return (NULL);
3788267937Srpaulo		case DTRACE_JSON_OBJECT:
3789267937Srpaulo			if (isspace(cc))
3790267937Srpaulo				break;
3791267937Srpaulo
3792267937Srpaulo			if (cc == '"') {
3793267937Srpaulo				state = DTRACE_JSON_STRING;
3794267937Srpaulo				string_is_key = B_TRUE;
3795267937Srpaulo				break;
3796267937Srpaulo			}
3797267937Srpaulo
3798267937Srpaulo			/*
3799267937Srpaulo			 * ERROR: either the object did not start with a key
3800267937Srpaulo			 * string, or we've run off the end of the object
3801267937Srpaulo			 * without finding the requested key.
3802267937Srpaulo			 */
3803267937Srpaulo			return (NULL);
3804267937Srpaulo		case DTRACE_JSON_STRING:
3805267937Srpaulo			if (cc == '\\') {
3806267937Srpaulo				*dd++ = '\\';
3807267937Srpaulo				state = DTRACE_JSON_STRING_ESCAPE;
3808267937Srpaulo				break;
3809267937Srpaulo			}
3810267937Srpaulo
3811267937Srpaulo			if (cc == '"') {
3812267937Srpaulo				if (collect_object) {
3813267937Srpaulo					/*
3814267937Srpaulo					 * We don't reset the dest here, as
3815267937Srpaulo					 * the string is part of a larger
3816267937Srpaulo					 * object being collected.
3817267937Srpaulo					 */
3818267937Srpaulo					*dd++ = cc;
3819267937Srpaulo					collect_object = B_FALSE;
3820267937Srpaulo					state = DTRACE_JSON_COLLECT_OBJECT;
3821267937Srpaulo					break;
3822267937Srpaulo				}
3823267937Srpaulo				*dd = '\0';
3824267937Srpaulo				dd = dest; /* reset string buffer */
3825267937Srpaulo				if (string_is_key) {
3826267937Srpaulo					if (dtrace_strncmp(dest, elem,
3827267937Srpaulo					    size) == 0)
3828267937Srpaulo						found_key = B_TRUE;
3829267937Srpaulo				} else if (found_key) {
3830267937Srpaulo					if (nelems > 1) {
3831267937Srpaulo						/*
3832267937Srpaulo						 * We expected an object, not
3833267937Srpaulo						 * this string.
3834267937Srpaulo						 */
3835267937Srpaulo						return (NULL);
3836267937Srpaulo					}
3837267937Srpaulo					return (dest);
3838267937Srpaulo				}
3839267937Srpaulo				state = string_is_key ? DTRACE_JSON_COLON :
3840267937Srpaulo				    DTRACE_JSON_COMMA;
3841267937Srpaulo				string_is_key = B_FALSE;
3842267937Srpaulo				break;
3843267937Srpaulo			}
3844267937Srpaulo
3845267937Srpaulo			*dd++ = cc;
3846267937Srpaulo			break;
3847267937Srpaulo		case DTRACE_JSON_STRING_ESCAPE:
3848267937Srpaulo			*dd++ = cc;
3849267937Srpaulo			if (cc == 'u') {
3850267937Srpaulo				escape_unicount = 0;
3851267937Srpaulo				state = DTRACE_JSON_STRING_ESCAPE_UNICODE;
3852267937Srpaulo			} else {
3853267937Srpaulo				state = DTRACE_JSON_STRING;
3854267937Srpaulo			}
3855267937Srpaulo			break;
3856267937Srpaulo		case DTRACE_JSON_STRING_ESCAPE_UNICODE:
3857267937Srpaulo			if (!isxdigit(cc)) {
3858267937Srpaulo				/*
3859267937Srpaulo				 * ERROR: invalid unicode escape, expected
3860267937Srpaulo				 * four valid hexidecimal digits.
3861267937Srpaulo				 */
3862267937Srpaulo				return (NULL);
3863267937Srpaulo			}
3864267937Srpaulo
3865267937Srpaulo			*dd++ = cc;
3866267937Srpaulo			if (++escape_unicount == 4)
3867267937Srpaulo				state = DTRACE_JSON_STRING;
3868267937Srpaulo			break;
3869267937Srpaulo		case DTRACE_JSON_COLON:
3870267937Srpaulo			if (isspace(cc))
3871267937Srpaulo				break;
3872267937Srpaulo
3873267937Srpaulo			if (cc == ':') {
3874267937Srpaulo				state = DTRACE_JSON_VALUE;
3875267937Srpaulo				break;
3876267937Srpaulo			}
3877267937Srpaulo
3878267937Srpaulo			/*
3879267937Srpaulo			 * ERROR: expected a colon.
3880267937Srpaulo			 */
3881267937Srpaulo			return (NULL);
3882267937Srpaulo		case DTRACE_JSON_COMMA:
3883267937Srpaulo			if (isspace(cc))
3884267937Srpaulo				break;
3885267937Srpaulo
3886267937Srpaulo			if (cc == ',') {
3887267937Srpaulo				if (in_array) {
3888267937Srpaulo					state = DTRACE_JSON_VALUE;
3889267937Srpaulo					if (++array_pos == array_elem)
3890267937Srpaulo						found_key = B_TRUE;
3891267937Srpaulo				} else {
3892267937Srpaulo					state = DTRACE_JSON_OBJECT;
3893267937Srpaulo				}
3894267937Srpaulo				break;
3895267937Srpaulo			}
3896267937Srpaulo
3897267937Srpaulo			/*
3898267937Srpaulo			 * ERROR: either we hit an unexpected character, or
3899267937Srpaulo			 * we reached the end of the object or array without
3900267937Srpaulo			 * finding the requested key.
3901267937Srpaulo			 */
3902267937Srpaulo			return (NULL);
3903267937Srpaulo		case DTRACE_JSON_IDENTIFIER:
3904267937Srpaulo			if (islower(cc)) {
3905267937Srpaulo				*dd++ = cc;
3906267937Srpaulo				break;
3907267937Srpaulo			}
3908267937Srpaulo
3909267937Srpaulo			*dd = '\0';
3910267937Srpaulo			dd = dest; /* reset string buffer */
3911267937Srpaulo
3912267937Srpaulo			if (dtrace_strncmp(dest, "true", 5) == 0 ||
3913267937Srpaulo			    dtrace_strncmp(dest, "false", 6) == 0 ||
3914267937Srpaulo			    dtrace_strncmp(dest, "null", 5) == 0) {
3915267937Srpaulo				if (found_key) {
3916267937Srpaulo					if (nelems > 1) {
3917267937Srpaulo						/*
3918267937Srpaulo						 * ERROR: We expected an object,
3919267937Srpaulo						 * not this identifier.
3920267937Srpaulo						 */
3921267937Srpaulo						return (NULL);
3922267937Srpaulo					}
3923267937Srpaulo					return (dest);
3924267937Srpaulo				} else {
3925267937Srpaulo					cur--;
3926267937Srpaulo					state = DTRACE_JSON_COMMA;
3927267937Srpaulo					break;
3928267937Srpaulo				}
3929267937Srpaulo			}
3930267937Srpaulo
3931267937Srpaulo			/*
3932267937Srpaulo			 * ERROR: we did not recognise the identifier as one
3933267937Srpaulo			 * of those in the JSON specification.
3934267937Srpaulo			 */
3935267937Srpaulo			return (NULL);
3936267937Srpaulo		case DTRACE_JSON_NUMBER:
3937267937Srpaulo			if (cc == '.') {
3938267937Srpaulo				*dd++ = cc;
3939267937Srpaulo				state = DTRACE_JSON_NUMBER_FRAC;
3940267937Srpaulo				break;
3941267937Srpaulo			}
3942267937Srpaulo
3943267937Srpaulo			if (cc == 'x' || cc == 'X') {
3944267937Srpaulo				/*
3945267937Srpaulo				 * ERROR: specification explicitly excludes
3946267937Srpaulo				 * hexidecimal or octal numbers.
3947267937Srpaulo				 */
3948267937Srpaulo				return (NULL);
3949267937Srpaulo			}
3950267937Srpaulo
3951267937Srpaulo			/* FALLTHRU */
3952267937Srpaulo		case DTRACE_JSON_NUMBER_FRAC:
3953267937Srpaulo			if (cc == 'e' || cc == 'E') {
3954267937Srpaulo				*dd++ = cc;
3955267937Srpaulo				state = DTRACE_JSON_NUMBER_EXP;
3956267937Srpaulo				break;
3957267937Srpaulo			}
3958267937Srpaulo
3959267937Srpaulo			if (cc == '+' || cc == '-') {
3960267937Srpaulo				/*
3961267937Srpaulo				 * ERROR: expect sign as part of exponent only.
3962267937Srpaulo				 */
3963267937Srpaulo				return (NULL);
3964267937Srpaulo			}
3965267937Srpaulo			/* FALLTHRU */
3966267937Srpaulo		case DTRACE_JSON_NUMBER_EXP:
3967267937Srpaulo			if (isdigit(cc) || cc == '+' || cc == '-') {
3968267937Srpaulo				*dd++ = cc;
3969267937Srpaulo				break;
3970267937Srpaulo			}
3971267937Srpaulo
3972267937Srpaulo			*dd = '\0';
3973267937Srpaulo			dd = dest; /* reset string buffer */
3974267937Srpaulo			if (found_key) {
3975267937Srpaulo				if (nelems > 1) {
3976267937Srpaulo					/*
3977267937Srpaulo					 * ERROR: We expected an object, not
3978267937Srpaulo					 * this number.
3979267937Srpaulo					 */
3980267937Srpaulo					return (NULL);
3981267937Srpaulo				}
3982267937Srpaulo				return (dest);
3983267937Srpaulo			}
3984267937Srpaulo
3985267937Srpaulo			cur--;
3986267937Srpaulo			state = DTRACE_JSON_COMMA;
3987267937Srpaulo			break;
3988267937Srpaulo		case DTRACE_JSON_VALUE:
3989267937Srpaulo			if (isspace(cc))
3990267937Srpaulo				break;
3991267937Srpaulo
3992267937Srpaulo			if (cc == '{' || cc == '[') {
3993267937Srpaulo				if (nelems > 1 && found_key) {
3994267937Srpaulo					in_array = cc == '[' ? B_TRUE : B_FALSE;
3995267937Srpaulo					/*
3996267937Srpaulo					 * If our element selector directs us
3997267937Srpaulo					 * to descend into this nested object,
3998267937Srpaulo					 * then move to the next selector
3999267937Srpaulo					 * element in the list and restart the
4000267937Srpaulo					 * state machine.
4001267937Srpaulo					 */
4002267937Srpaulo					while (*elem != '\0')
4003267937Srpaulo						elem++;
4004267937Srpaulo					elem++; /* skip the inter-element NUL */
4005267937Srpaulo					nelems--;
4006267937Srpaulo					dd = dest;
4007267937Srpaulo					if (in_array) {
4008267937Srpaulo						state = DTRACE_JSON_VALUE;
4009267937Srpaulo						array_pos = 0;
4010267937Srpaulo						array_elem = dtrace_strtoll(
4011267937Srpaulo						    elem, 10, size);
4012267937Srpaulo						found_key = array_elem == 0 ?
4013267937Srpaulo						    B_TRUE : B_FALSE;
4014267937Srpaulo					} else {
4015267937Srpaulo						found_key = B_FALSE;
4016267937Srpaulo						state = DTRACE_JSON_OBJECT;
4017267937Srpaulo					}
4018267937Srpaulo					break;
4019267937Srpaulo				}
4020267937Srpaulo
4021267937Srpaulo				/*
4022267937Srpaulo				 * Otherwise, we wish to either skip this
4023267937Srpaulo				 * nested object or return it in full.
4024267937Srpaulo				 */
4025267937Srpaulo				if (cc == '[')
4026267937Srpaulo					brackets = 1;
4027267937Srpaulo				else
4028267937Srpaulo					braces = 1;
4029267937Srpaulo				*dd++ = cc;
4030267937Srpaulo				state = DTRACE_JSON_COLLECT_OBJECT;
4031267937Srpaulo				break;
4032267937Srpaulo			}
4033267937Srpaulo
4034267937Srpaulo			if (cc == '"') {
4035267937Srpaulo				state = DTRACE_JSON_STRING;
4036267937Srpaulo				break;
4037267937Srpaulo			}
4038267937Srpaulo
4039267937Srpaulo			if (islower(cc)) {
4040267937Srpaulo				/*
4041267937Srpaulo				 * Here we deal with true, false and null.
4042267937Srpaulo				 */
4043267937Srpaulo				*dd++ = cc;
4044267937Srpaulo				state = DTRACE_JSON_IDENTIFIER;
4045267937Srpaulo				break;
4046267937Srpaulo			}
4047267937Srpaulo
4048267937Srpaulo			if (cc == '-' || isdigit(cc)) {
4049267937Srpaulo				*dd++ = cc;
4050267937Srpaulo				state = DTRACE_JSON_NUMBER;
4051267937Srpaulo				break;
4052267937Srpaulo			}
4053267937Srpaulo
4054267937Srpaulo			/*
4055267937Srpaulo			 * ERROR: unexpected character at start of value.
4056267937Srpaulo			 */
4057267937Srpaulo			return (NULL);
4058267937Srpaulo		case DTRACE_JSON_COLLECT_OBJECT:
4059267937Srpaulo			if (cc == '\0')
4060267937Srpaulo				/*
4061267937Srpaulo				 * ERROR: unexpected end of input.
4062267937Srpaulo				 */
4063267937Srpaulo				return (NULL);
4064267937Srpaulo
4065267937Srpaulo			*dd++ = cc;
4066267937Srpaulo			if (cc == '"') {
4067267937Srpaulo				collect_object = B_TRUE;
4068267937Srpaulo				state = DTRACE_JSON_STRING;
4069267937Srpaulo				break;
4070267937Srpaulo			}
4071267937Srpaulo
4072267937Srpaulo			if (cc == ']') {
4073267937Srpaulo				if (brackets-- == 0) {
4074267937Srpaulo					/*
4075267937Srpaulo					 * ERROR: unbalanced brackets.
4076267937Srpaulo					 */
4077267937Srpaulo					return (NULL);
4078267937Srpaulo				}
4079267937Srpaulo			} else if (cc == '}') {
4080267937Srpaulo				if (braces-- == 0) {
4081267937Srpaulo					/*
4082267937Srpaulo					 * ERROR: unbalanced braces.
4083267937Srpaulo					 */
4084267937Srpaulo					return (NULL);
4085267937Srpaulo				}
4086267937Srpaulo			} else if (cc == '{') {
4087267937Srpaulo				braces++;
4088267937Srpaulo			} else if (cc == '[') {
4089267937Srpaulo				brackets++;
4090267937Srpaulo			}
4091267937Srpaulo
4092267937Srpaulo			if (brackets == 0 && braces == 0) {
4093267937Srpaulo				if (found_key) {
4094267937Srpaulo					*dd = '\0';
4095267937Srpaulo					return (dest);
4096267937Srpaulo				}
4097267937Srpaulo				dd = dest; /* reset string buffer */
4098267937Srpaulo				state = DTRACE_JSON_COMMA;
4099267937Srpaulo			}
4100267937Srpaulo			break;
4101267937Srpaulo		}
4102267937Srpaulo	}
4103267937Srpaulo	return (NULL);
4104267937Srpaulo}
4105267937Srpaulo
4106267937Srpaulo/*
4107179193Sjb * Emulate the execution of DTrace ID subroutines invoked by the call opcode.
4108179193Sjb * Notice that we don't bother validating the proper number of arguments or
4109179193Sjb * their types in the tuple stack.  This isn't needed because all argument
4110179193Sjb * interpretation is safe because of our load safety -- the worst that can
4111179193Sjb * happen is that a bogus program can obtain bogus results.
4112179193Sjb */
4113179193Sjbstatic void
4114179193Sjbdtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs,
4115179193Sjb    dtrace_key_t *tupregs, int nargs,
4116179193Sjb    dtrace_mstate_t *mstate, dtrace_state_t *state)
4117179193Sjb{
4118179198Sjb	volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
4119179198Sjb	volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
4120179193Sjb	dtrace_vstate_t *vstate = &state->dts_vstate;
4121179193Sjb
4122277300Ssmh#ifdef illumos
4123179193Sjb	union {
4124179193Sjb		mutex_impl_t mi;
4125179193Sjb		uint64_t mx;
4126179193Sjb	} m;
4127179193Sjb
4128179193Sjb	union {
4129179193Sjb		krwlock_t ri;
4130179193Sjb		uintptr_t rw;
4131179193Sjb	} r;
4132179198Sjb#else
4133192853Ssson	struct thread *lowner;
4134179198Sjb	union {
4135192853Ssson		struct lock_object *li;
4136192853Ssson		uintptr_t lx;
4137192853Ssson	} l;
4138179198Sjb#endif
4139179193Sjb
4140179193Sjb	switch (subr) {
4141179193Sjb	case DIF_SUBR_RAND:
4142316210Sgnn		regs[rd] = dtrace_xoroshiro128_plus_next(
4143316210Sgnn		    state->dts_rstate[curcpu]);
4144179193Sjb		break;
4145179193Sjb
4146277300Ssmh#ifdef illumos
4147179193Sjb	case DIF_SUBR_MUTEX_OWNED:
4148179193Sjb		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4149179193Sjb		    mstate, vstate)) {
4150179198Sjb			regs[rd] = 0;
4151179193Sjb			break;
4152179193Sjb		}
4153179193Sjb
4154179193Sjb		m.mx = dtrace_load64(tupregs[0].dttk_value);
4155179193Sjb		if (MUTEX_TYPE_ADAPTIVE(&m.mi))
4156179193Sjb			regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER;
4157179193Sjb		else
4158179193Sjb			regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock);
4159179193Sjb		break;
4160179193Sjb
4161179193Sjb	case DIF_SUBR_MUTEX_OWNER:
4162179193Sjb		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4163179193Sjb		    mstate, vstate)) {
4164179198Sjb			regs[rd] = 0;
4165179193Sjb			break;
4166179193Sjb		}
4167179193Sjb
4168179193Sjb		m.mx = dtrace_load64(tupregs[0].dttk_value);
4169179193Sjb		if (MUTEX_TYPE_ADAPTIVE(&m.mi) &&
4170179193Sjb		    MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER)
4171179193Sjb			regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi);
4172179193Sjb		else
4173179193Sjb			regs[rd] = 0;
4174179193Sjb		break;
4175179193Sjb
4176179193Sjb	case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
4177179193Sjb		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4178179193Sjb		    mstate, vstate)) {
4179179198Sjb			regs[rd] = 0;
4180179193Sjb			break;
4181179193Sjb		}
4182179193Sjb
4183179193Sjb		m.mx = dtrace_load64(tupregs[0].dttk_value);
4184179193Sjb		regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi);
4185179193Sjb		break;
4186179193Sjb
4187179193Sjb	case DIF_SUBR_MUTEX_TYPE_SPIN:
4188179193Sjb		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
4189179193Sjb		    mstate, vstate)) {
4190179198Sjb			regs[rd] = 0;
4191179193Sjb			break;
4192179193Sjb		}
4193179193Sjb
4194179193Sjb		m.mx = dtrace_load64(tupregs[0].dttk_value);
4195179193Sjb		regs[rd] = MUTEX_TYPE_SPIN(&m.mi);
4196179193Sjb		break;
4197179193Sjb
4198179193Sjb	case DIF_SUBR_RW_READ_HELD: {
4199179193Sjb		uintptr_t tmp;
4200179193Sjb
4201179193Sjb		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
4202179193Sjb		    mstate, vstate)) {
4203179198Sjb			regs[rd] = 0;
4204179193Sjb			break;
4205179193Sjb		}
4206179193Sjb
4207179193Sjb		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
4208179193Sjb		regs[rd] = _RW_READ_HELD(&r.ri, tmp);
4209179193Sjb		break;
4210179193Sjb	}
4211179193Sjb
4212179193Sjb	case DIF_SUBR_RW_WRITE_HELD:
4213179193Sjb		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
4214179193Sjb		    mstate, vstate)) {
4215179198Sjb			regs[rd] = 0;
4216179193Sjb			break;
4217179193Sjb		}
4218179193Sjb
4219179193Sjb		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
4220179193Sjb		regs[rd] = _RW_WRITE_HELD(&r.ri);
4221179193Sjb		break;
4222179193Sjb
4223179193Sjb	case DIF_SUBR_RW_ISWRITER:
4224179193Sjb		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
4225179193Sjb		    mstate, vstate)) {
4226179198Sjb			regs[rd] = 0;
4227179193Sjb			break;
4228179193Sjb		}
4229179193Sjb
4230179193Sjb		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
4231179193Sjb		regs[rd] = _RW_ISWRITER(&r.ri);
4232179193Sjb		break;
4233179193Sjb
4234277300Ssmh#else /* !illumos */
4235179198Sjb	case DIF_SUBR_MUTEX_OWNED:
4236192853Ssson		if (!dtrace_canload(tupregs[0].dttk_value,
4237192853Ssson			sizeof (struct lock_object), mstate, vstate)) {
4238192853Ssson			regs[rd] = 0;
4239192853Ssson			break;
4240179198Sjb		}
4241192853Ssson		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
4242192853Ssson		regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
4243179198Sjb		break;
4244179198Sjb
4245179198Sjb	case DIF_SUBR_MUTEX_OWNER:
4246192853Ssson		if (!dtrace_canload(tupregs[0].dttk_value,
4247192853Ssson			sizeof (struct lock_object), mstate, vstate)) {
4248192853Ssson			regs[rd] = 0;
4249192853Ssson			break;
4250179198Sjb		}
4251192853Ssson		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
4252192853Ssson		LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
4253192853Ssson		regs[rd] = (uintptr_t)lowner;
4254179198Sjb		break;
4255179198Sjb
4256179198Sjb	case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
4257192853Ssson		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx),
4258192853Ssson		    mstate, vstate)) {
4259192853Ssson			regs[rd] = 0;
4260192853Ssson			break;
4261192853Ssson		}
4262192853Ssson		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
4263192853Ssson		/* XXX - should be only LC_SLEEPABLE? */
4264192853Ssson		regs[rd] = (LOCK_CLASS(l.li)->lc_flags &
4265192853Ssson		    (LC_SLEEPLOCK | LC_SLEEPABLE)) != 0;
4266179198Sjb		break;
4267179198Sjb
4268179198Sjb	case DIF_SUBR_MUTEX_TYPE_SPIN:
4269192853Ssson		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx),
4270192853Ssson		    mstate, vstate)) {
4271192853Ssson			regs[rd] = 0;
4272192853Ssson			break;
4273192853Ssson		}
4274192853Ssson		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
4275192853Ssson		regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0;
4276179198Sjb		break;
4277179198Sjb
4278179198Sjb	case DIF_SUBR_RW_READ_HELD:
4279179198Sjb	case DIF_SUBR_SX_SHARED_HELD:
4280192853Ssson		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
4281192853Ssson		    mstate, vstate)) {
4282192853Ssson			regs[rd] = 0;
4283192853Ssson			break;
4284192853Ssson		}
4285192853Ssson		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
4286192853Ssson		regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) &&
4287192853Ssson		    lowner == NULL;
4288179198Sjb		break;
4289179198Sjb
4290179198Sjb	case DIF_SUBR_RW_WRITE_HELD:
4291179198Sjb	case DIF_SUBR_SX_EXCLUSIVE_HELD:
4292192853Ssson		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
4293192853Ssson		    mstate, vstate)) {
4294192853Ssson			regs[rd] = 0;
4295192853Ssson			break;
4296192853Ssson		}
4297192853Ssson		l.lx = dtrace_loadptr(tupregs[0].dttk_value);
4298310335Sgnn		regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) &&
4299310335Sgnn		    lowner != NULL;
4300179198Sjb		break;
4301179198Sjb
4302179198Sjb	case DIF_SUBR_RW_ISWRITER:
4303179198Sjb	case DIF_SUBR_SX_ISEXCLUSIVE:
4304192853Ssson		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
4305192853Ssson		    mstate, vstate)) {
4306192853Ssson			regs[rd] = 0;
4307192853Ssson			break;
4308192853Ssson		}
4309192853Ssson		l.lx = dtrace_loadptr(tupregs[0].dttk_value);
4310310335Sgnn		LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
4311310335Sgnn		regs[rd] = (lowner == curthread);
4312179198Sjb		break;
4313277300Ssmh#endif /* illumos */
4314179198Sjb
4315179193Sjb	case DIF_SUBR_BCOPY: {
4316179193Sjb		/*
4317179193Sjb		 * We need to be sure that the destination is in the scratch
4318179193Sjb		 * region -- no other region is allowed.
4319179193Sjb		 */
4320179193Sjb		uintptr_t src = tupregs[0].dttk_value;
4321179193Sjb		uintptr_t dest = tupregs[1].dttk_value;
4322179193Sjb		size_t size = tupregs[2].dttk_value;
4323179193Sjb
4324179193Sjb		if (!dtrace_inscratch(dest, size, mstate)) {
4325179193Sjb			*flags |= CPU_DTRACE_BADADDR;
4326179193Sjb			*illval = regs[rd];
4327179193Sjb			break;
4328179193Sjb		}
4329179193Sjb
4330179193Sjb		if (!dtrace_canload(src, size, mstate, vstate)) {
4331179198Sjb			regs[rd] = 0;
4332179193Sjb			break;
4333179193Sjb		}
4334179193Sjb
4335179193Sjb		dtrace_bcopy((void *)src, (void *)dest, size);
4336179193Sjb		break;
4337179193Sjb	}
4338179193Sjb
4339179193Sjb	case DIF_SUBR_ALLOCA:
4340179193Sjb	case DIF_SUBR_COPYIN: {
4341179193Sjb		uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
4342179193Sjb		uint64_t size =
4343179193Sjb		    tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value;
4344179193Sjb		size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size;
4345179193Sjb
4346179193Sjb		/*
4347179193Sjb		 * This action doesn't require any credential checks since
4348179193Sjb		 * probes will not activate in user contexts to which the
4349179193Sjb		 * enabling user does not have permissions.
4350179193Sjb		 */
4351179193Sjb
4352179193Sjb		/*
4353179193Sjb		 * Rounding up the user allocation size could have overflowed
4354179193Sjb		 * a large, bogus allocation (like -1ULL) to 0.
4355179193Sjb		 */
4356179193Sjb		if (scratch_size < size ||
4357179193Sjb		    !DTRACE_INSCRATCH(mstate, scratch_size)) {
4358179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4359179198Sjb			regs[rd] = 0;
4360179193Sjb			break;
4361179193Sjb		}
4362179193Sjb
4363179193Sjb		if (subr == DIF_SUBR_COPYIN) {
4364179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4365179193Sjb			dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
4366179193Sjb			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4367179193Sjb		}
4368179193Sjb
4369179193Sjb		mstate->dtms_scratch_ptr += scratch_size;
4370179193Sjb		regs[rd] = dest;
4371179193Sjb		break;
4372179193Sjb	}
4373179193Sjb
4374179193Sjb	case DIF_SUBR_COPYINTO: {
4375179193Sjb		uint64_t size = tupregs[1].dttk_value;
4376179193Sjb		uintptr_t dest = tupregs[2].dttk_value;
4377179193Sjb
4378179193Sjb		/*
4379179193Sjb		 * This action doesn't require any credential checks since
4380179193Sjb		 * probes will not activate in user contexts to which the
4381179193Sjb		 * enabling user does not have permissions.
4382179193Sjb		 */
4383179193Sjb		if (!dtrace_inscratch(dest, size, mstate)) {
4384179193Sjb			*flags |= CPU_DTRACE_BADADDR;
4385179193Sjb			*illval = regs[rd];
4386179193Sjb			break;
4387179193Sjb		}
4388179193Sjb
4389179193Sjb		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4390179193Sjb		dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
4391179193Sjb		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4392179193Sjb		break;
4393179193Sjb	}
4394179193Sjb
4395179193Sjb	case DIF_SUBR_COPYINSTR: {
4396179193Sjb		uintptr_t dest = mstate->dtms_scratch_ptr;
4397179193Sjb		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4398179193Sjb
4399179193Sjb		if (nargs > 1 && tupregs[1].dttk_value < size)
4400179193Sjb			size = tupregs[1].dttk_value + 1;
4401179193Sjb
4402179193Sjb		/*
4403179193Sjb		 * This action doesn't require any credential checks since
4404179193Sjb		 * probes will not activate in user contexts to which the
4405179193Sjb		 * enabling user does not have permissions.
4406179193Sjb		 */
4407179193Sjb		if (!DTRACE_INSCRATCH(mstate, size)) {
4408179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4409179198Sjb			regs[rd] = 0;
4410179193Sjb			break;
4411179193Sjb		}
4412179193Sjb
4413179193Sjb		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4414179193Sjb		dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags);
4415179193Sjb		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4416179193Sjb
4417179193Sjb		((char *)dest)[size - 1] = '\0';
4418179193Sjb		mstate->dtms_scratch_ptr += size;
4419179193Sjb		regs[rd] = dest;
4420179193Sjb		break;
4421179193Sjb	}
4422179193Sjb
4423277300Ssmh#ifdef illumos
4424179193Sjb	case DIF_SUBR_MSGSIZE:
4425179193Sjb	case DIF_SUBR_MSGDSIZE: {
4426179193Sjb		uintptr_t baddr = tupregs[0].dttk_value, daddr;
4427179193Sjb		uintptr_t wptr, rptr;
4428179193Sjb		size_t count = 0;
4429179193Sjb		int cont = 0;
4430179193Sjb
4431179198Sjb		while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) {
4432179193Sjb
4433179193Sjb			if (!dtrace_canload(baddr, sizeof (mblk_t), mstate,
4434179193Sjb			    vstate)) {
4435179198Sjb				regs[rd] = 0;
4436179193Sjb				break;
4437179193Sjb			}
4438179193Sjb
4439179193Sjb			wptr = dtrace_loadptr(baddr +
4440179193Sjb			    offsetof(mblk_t, b_wptr));
4441179193Sjb
4442179193Sjb			rptr = dtrace_loadptr(baddr +
4443179193Sjb			    offsetof(mblk_t, b_rptr));
4444179193Sjb
4445179193Sjb			if (wptr < rptr) {
4446179193Sjb				*flags |= CPU_DTRACE_BADADDR;
4447179193Sjb				*illval = tupregs[0].dttk_value;
4448179193Sjb				break;
4449179193Sjb			}
4450179193Sjb
4451179193Sjb			daddr = dtrace_loadptr(baddr +
4452179193Sjb			    offsetof(mblk_t, b_datap));
4453179193Sjb
4454179193Sjb			baddr = dtrace_loadptr(baddr +
4455179193Sjb			    offsetof(mblk_t, b_cont));
4456179193Sjb
4457179193Sjb			/*
4458179193Sjb			 * We want to prevent against denial-of-service here,
4459179193Sjb			 * so we're only going to search the list for
4460179193Sjb			 * dtrace_msgdsize_max mblks.
4461179193Sjb			 */
4462179193Sjb			if (cont++ > dtrace_msgdsize_max) {
4463179193Sjb				*flags |= CPU_DTRACE_ILLOP;
4464179193Sjb				break;
4465179193Sjb			}
4466179193Sjb
4467179193Sjb			if (subr == DIF_SUBR_MSGDSIZE) {
4468179193Sjb				if (dtrace_load8(daddr +
4469179193Sjb				    offsetof(dblk_t, db_type)) != M_DATA)
4470179193Sjb					continue;
4471179193Sjb			}
4472179193Sjb
4473179193Sjb			count += wptr - rptr;
4474179193Sjb		}
4475179193Sjb
4476179193Sjb		if (!(*flags & CPU_DTRACE_FAULT))
4477179193Sjb			regs[rd] = count;
4478179193Sjb
4479179193Sjb		break;
4480179193Sjb	}
4481179198Sjb#endif
4482179193Sjb
4483179193Sjb	case DIF_SUBR_PROGENYOF: {
4484179193Sjb		pid_t pid = tupregs[0].dttk_value;
4485179193Sjb		proc_t *p;
4486179193Sjb		int rval = 0;
4487179193Sjb
4488179193Sjb		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4489179193Sjb
4490179193Sjb		for (p = curthread->t_procp; p != NULL; p = p->p_parent) {
4491277300Ssmh#ifdef illumos
4492179193Sjb			if (p->p_pidp->pid_id == pid) {
4493179198Sjb#else
4494179198Sjb			if (p->p_pid == pid) {
4495179198Sjb#endif
4496179193Sjb				rval = 1;
4497179193Sjb				break;
4498179193Sjb			}
4499179193Sjb		}
4500179193Sjb
4501179193Sjb		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4502179193Sjb
4503179193Sjb		regs[rd] = rval;
4504179193Sjb		break;
4505179193Sjb	}
4506179193Sjb
4507179193Sjb	case DIF_SUBR_SPECULATION:
4508179193Sjb		regs[rd] = dtrace_speculation(state);
4509179193Sjb		break;
4510179193Sjb
4511179193Sjb	case DIF_SUBR_COPYOUT: {
4512179193Sjb		uintptr_t kaddr = tupregs[0].dttk_value;
4513179193Sjb		uintptr_t uaddr = tupregs[1].dttk_value;
4514179193Sjb		uint64_t size = tupregs[2].dttk_value;
4515179193Sjb
4516179193Sjb		if (!dtrace_destructive_disallow &&
4517179193Sjb		    dtrace_priv_proc_control(state) &&
4518288415Smarkj		    !dtrace_istoxic(kaddr, size) &&
4519288415Smarkj		    dtrace_canload(kaddr, size, mstate, vstate)) {
4520179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4521179193Sjb			dtrace_copyout(kaddr, uaddr, size, flags);
4522179193Sjb			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4523179193Sjb		}
4524179193Sjb		break;
4525179193Sjb	}
4526179193Sjb
4527179193Sjb	case DIF_SUBR_COPYOUTSTR: {
4528179193Sjb		uintptr_t kaddr = tupregs[0].dttk_value;
4529179193Sjb		uintptr_t uaddr = tupregs[1].dttk_value;
4530179193Sjb		uint64_t size = tupregs[2].dttk_value;
4531179193Sjb
4532179193Sjb		if (!dtrace_destructive_disallow &&
4533179193Sjb		    dtrace_priv_proc_control(state) &&
4534288415Smarkj		    !dtrace_istoxic(kaddr, size) &&
4535288415Smarkj		    dtrace_strcanload(kaddr, size, mstate, vstate)) {
4536179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4537179193Sjb			dtrace_copyoutstr(kaddr, uaddr, size, flags);
4538179193Sjb			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4539179193Sjb		}
4540179193Sjb		break;
4541179193Sjb	}
4542179193Sjb
4543179193Sjb	case DIF_SUBR_STRLEN: {
4544179193Sjb		size_t sz;
4545179193Sjb		uintptr_t addr = (uintptr_t)tupregs[0].dttk_value;
4546179193Sjb		sz = dtrace_strlen((char *)addr,
4547179193Sjb		    state->dts_options[DTRACEOPT_STRSIZE]);
4548179193Sjb
4549179193Sjb		if (!dtrace_canload(addr, sz + 1, mstate, vstate)) {
4550179198Sjb			regs[rd] = 0;
4551179193Sjb			break;
4552179193Sjb		}
4553179193Sjb
4554179193Sjb		regs[rd] = sz;
4555179193Sjb
4556179193Sjb		break;
4557179193Sjb	}
4558179193Sjb
4559179193Sjb	case DIF_SUBR_STRCHR:
4560179193Sjb	case DIF_SUBR_STRRCHR: {
4561179193Sjb		/*
4562179193Sjb		 * We're going to iterate over the string looking for the
4563179193Sjb		 * specified character.  We will iterate until we have reached
4564179193Sjb		 * the string length or we have found the character.  If this
4565179193Sjb		 * is DIF_SUBR_STRRCHR, we will look for the last occurrence
4566179193Sjb		 * of the specified character instead of the first.
4567179193Sjb		 */
4568179193Sjb		uintptr_t saddr = tupregs[0].dttk_value;
4569179193Sjb		uintptr_t addr = tupregs[0].dttk_value;
4570179193Sjb		uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE];
4571179193Sjb		char c, target = (char)tupregs[1].dttk_value;
4572179193Sjb
4573179198Sjb		for (regs[rd] = 0; addr < limit; addr++) {
4574179193Sjb			if ((c = dtrace_load8(addr)) == target) {
4575179193Sjb				regs[rd] = addr;
4576179193Sjb
4577179193Sjb				if (subr == DIF_SUBR_STRCHR)
4578179193Sjb					break;
4579179193Sjb			}
4580179193Sjb
4581179193Sjb			if (c == '\0')
4582179193Sjb				break;
4583179193Sjb		}
4584179193Sjb
4585179193Sjb		if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) {
4586179198Sjb			regs[rd] = 0;
4587179193Sjb			break;
4588179193Sjb		}
4589179193Sjb
4590179193Sjb		break;
4591179193Sjb	}
4592179193Sjb
4593179193Sjb	case DIF_SUBR_STRSTR:
4594179193Sjb	case DIF_SUBR_INDEX:
4595179193Sjb	case DIF_SUBR_RINDEX: {
4596179193Sjb		/*
4597179193Sjb		 * We're going to iterate over the string looking for the
4598179193Sjb		 * specified string.  We will iterate until we have reached
4599179193Sjb		 * the string length or we have found the string.  (Yes, this
4600179193Sjb		 * is done in the most naive way possible -- but considering
4601179193Sjb		 * that the string we're searching for is likely to be
4602179193Sjb		 * relatively short, the complexity of Rabin-Karp or similar
4603179193Sjb		 * hardly seems merited.)
4604179193Sjb		 */
4605179193Sjb		char *addr = (char *)(uintptr_t)tupregs[0].dttk_value;
4606179193Sjb		char *substr = (char *)(uintptr_t)tupregs[1].dttk_value;
4607179193Sjb		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4608179193Sjb		size_t len = dtrace_strlen(addr, size);
4609179193Sjb		size_t sublen = dtrace_strlen(substr, size);
4610179193Sjb		char *limit = addr + len, *orig = addr;
4611179193Sjb		int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1;
4612179193Sjb		int inc = 1;
4613179193Sjb
4614179193Sjb		regs[rd] = notfound;
4615179193Sjb
4616179193Sjb		if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) {
4617179198Sjb			regs[rd] = 0;
4618179193Sjb			break;
4619179193Sjb		}
4620179193Sjb
4621179193Sjb		if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate,
4622179193Sjb		    vstate)) {
4623179198Sjb			regs[rd] = 0;
4624179193Sjb			break;
4625179193Sjb		}
4626179193Sjb
4627179193Sjb		/*
4628179193Sjb		 * strstr() and index()/rindex() have similar semantics if
4629179193Sjb		 * both strings are the empty string: strstr() returns a
4630179193Sjb		 * pointer to the (empty) string, and index() and rindex()
4631179193Sjb		 * both return index 0 (regardless of any position argument).
4632179193Sjb		 */
4633179193Sjb		if (sublen == 0 && len == 0) {
4634179193Sjb			if (subr == DIF_SUBR_STRSTR)
4635179193Sjb				regs[rd] = (uintptr_t)addr;
4636179193Sjb			else
4637179193Sjb				regs[rd] = 0;
4638179193Sjb			break;
4639179193Sjb		}
4640179193Sjb
4641179193Sjb		if (subr != DIF_SUBR_STRSTR) {
4642179193Sjb			if (subr == DIF_SUBR_RINDEX) {
4643179193Sjb				limit = orig - 1;
4644179193Sjb				addr += len;
4645179193Sjb				inc = -1;
4646179193Sjb			}
4647179193Sjb
4648179193Sjb			/*
4649179193Sjb			 * Both index() and rindex() take an optional position
4650179193Sjb			 * argument that denotes the starting position.
4651179193Sjb			 */
4652179193Sjb			if (nargs == 3) {
4653179193Sjb				int64_t pos = (int64_t)tupregs[2].dttk_value;
4654179193Sjb
4655179193Sjb				/*
4656179193Sjb				 * If the position argument to index() is
4657179193Sjb				 * negative, Perl implicitly clamps it at
4658179193Sjb				 * zero.  This semantic is a little surprising
4659179193Sjb				 * given the special meaning of negative
4660179193Sjb				 * positions to similar Perl functions like
4661179193Sjb				 * substr(), but it appears to reflect a
4662179193Sjb				 * notion that index() can start from a
4663179193Sjb				 * negative index and increment its way up to
4664179193Sjb				 * the string.  Given this notion, Perl's
4665179193Sjb				 * rindex() is at least self-consistent in
4666179193Sjb				 * that it implicitly clamps positions greater
4667179193Sjb				 * than the string length to be the string
4668179193Sjb				 * length.  Where Perl completely loses
4669179193Sjb				 * coherence, however, is when the specified
4670179193Sjb				 * substring is the empty string ("").  In
4671179193Sjb				 * this case, even if the position is
4672179193Sjb				 * negative, rindex() returns 0 -- and even if
4673179193Sjb				 * the position is greater than the length,
4674179193Sjb				 * index() returns the string length.  These
4675179193Sjb				 * semantics violate the notion that index()
4676179193Sjb				 * should never return a value less than the
4677179193Sjb				 * specified position and that rindex() should
4678179193Sjb				 * never return a value greater than the
4679179193Sjb				 * specified position.  (One assumes that
4680179193Sjb				 * these semantics are artifacts of Perl's
4681179193Sjb				 * implementation and not the results of
4682179193Sjb				 * deliberate design -- it beggars belief that
4683179193Sjb				 * even Larry Wall could desire such oddness.)
4684179193Sjb				 * While in the abstract one would wish for
4685179193Sjb				 * consistent position semantics across
4686179193Sjb				 * substr(), index() and rindex() -- or at the
4687179193Sjb				 * very least self-consistent position
4688179193Sjb				 * semantics for index() and rindex() -- we
4689179193Sjb				 * instead opt to keep with the extant Perl
4690179193Sjb				 * semantics, in all their broken glory.  (Do
4691179193Sjb				 * we have more desire to maintain Perl's
4692179193Sjb				 * semantics than Perl does?  Probably.)
4693179193Sjb				 */
4694179193Sjb				if (subr == DIF_SUBR_RINDEX) {
4695179193Sjb					if (pos < 0) {
4696179193Sjb						if (sublen == 0)
4697179193Sjb							regs[rd] = 0;
4698179193Sjb						break;
4699179193Sjb					}
4700179193Sjb
4701179193Sjb					if (pos > len)
4702179193Sjb						pos = len;
4703179193Sjb				} else {
4704179193Sjb					if (pos < 0)
4705179193Sjb						pos = 0;
4706179193Sjb
4707179193Sjb					if (pos >= len) {
4708179193Sjb						if (sublen == 0)
4709179193Sjb							regs[rd] = len;
4710179193Sjb						break;
4711179193Sjb					}
4712179193Sjb				}
4713179193Sjb
4714179193Sjb				addr = orig + pos;
4715179193Sjb			}
4716179193Sjb		}
4717179193Sjb
4718179193Sjb		for (regs[rd] = notfound; addr != limit; addr += inc) {
4719179193Sjb			if (dtrace_strncmp(addr, substr, sublen) == 0) {
4720179193Sjb				if (subr != DIF_SUBR_STRSTR) {
4721179193Sjb					/*
4722179193Sjb					 * As D index() and rindex() are
4723179193Sjb					 * modeled on Perl (and not on awk),
4724179193Sjb					 * we return a zero-based (and not a
4725179193Sjb					 * one-based) index.  (For you Perl
4726179193Sjb					 * weenies: no, we're not going to add
4727179193Sjb					 * $[ -- and shouldn't you be at a con
4728179193Sjb					 * or something?)
4729179193Sjb					 */
4730179193Sjb					regs[rd] = (uintptr_t)(addr - orig);
4731179193Sjb					break;
4732179193Sjb				}
4733179193Sjb
4734179193Sjb				ASSERT(subr == DIF_SUBR_STRSTR);
4735179193Sjb				regs[rd] = (uintptr_t)addr;
4736179193Sjb				break;
4737179193Sjb			}
4738179193Sjb		}
4739179193Sjb
4740179193Sjb		break;
4741179193Sjb	}
4742179193Sjb
4743179193Sjb	case DIF_SUBR_STRTOK: {
4744179193Sjb		uintptr_t addr = tupregs[0].dttk_value;
4745179193Sjb		uintptr_t tokaddr = tupregs[1].dttk_value;
4746179193Sjb		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4747179193Sjb		uintptr_t limit, toklimit = tokaddr + size;
4748179198Sjb		uint8_t c = 0, tokmap[32];	 /* 256 / 8 */
4749179193Sjb		char *dest = (char *)mstate->dtms_scratch_ptr;
4750179193Sjb		int i;
4751179193Sjb
4752179193Sjb		/*
4753179193Sjb		 * Check both the token buffer and (later) the input buffer,
4754179193Sjb		 * since both could be non-scratch addresses.
4755179193Sjb		 */
4756179193Sjb		if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) {
4757179198Sjb			regs[rd] = 0;
4758179193Sjb			break;
4759179193Sjb		}
4760179193Sjb
4761179193Sjb		if (!DTRACE_INSCRATCH(mstate, size)) {
4762179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4763179198Sjb			regs[rd] = 0;
4764179193Sjb			break;
4765179193Sjb		}
4766179193Sjb
4767179198Sjb		if (addr == 0) {
4768179193Sjb			/*
4769179193Sjb			 * If the address specified is NULL, we use our saved
4770179193Sjb			 * strtok pointer from the mstate.  Note that this
4771179193Sjb			 * means that the saved strtok pointer is _only_
4772179193Sjb			 * valid within multiple enablings of the same probe --
4773179193Sjb			 * it behaves like an implicit clause-local variable.
4774179193Sjb			 */
4775179193Sjb			addr = mstate->dtms_strtok;
4776179193Sjb		} else {
4777179193Sjb			/*
4778179193Sjb			 * If the user-specified address is non-NULL we must
4779179193Sjb			 * access check it.  This is the only time we have
4780179193Sjb			 * a chance to do so, since this address may reside
4781179193Sjb			 * in the string table of this clause-- future calls
4782179193Sjb			 * (when we fetch addr from mstate->dtms_strtok)
4783179193Sjb			 * would fail this access check.
4784179193Sjb			 */
4785179193Sjb			if (!dtrace_strcanload(addr, size, mstate, vstate)) {
4786179198Sjb				regs[rd] = 0;
4787179193Sjb				break;
4788179193Sjb			}
4789179193Sjb		}
4790179193Sjb
4791179193Sjb		/*
4792179193Sjb		 * First, zero the token map, and then process the token
4793179193Sjb		 * string -- setting a bit in the map for every character
4794179193Sjb		 * found in the token string.
4795179193Sjb		 */
4796179193Sjb		for (i = 0; i < sizeof (tokmap); i++)
4797179193Sjb			tokmap[i] = 0;
4798179193Sjb
4799179193Sjb		for (; tokaddr < toklimit; tokaddr++) {
4800179193Sjb			if ((c = dtrace_load8(tokaddr)) == '\0')
4801179193Sjb				break;
4802179193Sjb
4803179193Sjb			ASSERT((c >> 3) < sizeof (tokmap));
4804179193Sjb			tokmap[c >> 3] |= (1 << (c & 0x7));
4805179193Sjb		}
4806179193Sjb
4807179193Sjb		for (limit = addr + size; addr < limit; addr++) {
4808179193Sjb			/*
4809179193Sjb			 * We're looking for a character that is _not_ contained
4810179193Sjb			 * in the token string.
4811179193Sjb			 */
4812179193Sjb			if ((c = dtrace_load8(addr)) == '\0')
4813179193Sjb				break;
4814179193Sjb
4815179193Sjb			if (!(tokmap[c >> 3] & (1 << (c & 0x7))))
4816179193Sjb				break;
4817179193Sjb		}
4818179193Sjb
4819179193Sjb		if (c == '\0') {
4820179193Sjb			/*
4821179193Sjb			 * We reached the end of the string without finding
4822179193Sjb			 * any character that was not in the token string.
4823179193Sjb			 * We return NULL in this case, and we set the saved
4824179193Sjb			 * address to NULL as well.
4825179193Sjb			 */
4826179198Sjb			regs[rd] = 0;
4827179198Sjb			mstate->dtms_strtok = 0;
4828179193Sjb			break;
4829179193Sjb		}
4830179193Sjb
4831179193Sjb		/*
4832179193Sjb		 * From here on, we're copying into the destination string.
4833179193Sjb		 */
4834179193Sjb		for (i = 0; addr < limit && i < size - 1; addr++) {
4835179193Sjb			if ((c = dtrace_load8(addr)) == '\0')
4836179193Sjb				break;
4837179193Sjb
4838179193Sjb			if (tokmap[c >> 3] & (1 << (c & 0x7)))
4839179193Sjb				break;
4840179193Sjb
4841179193Sjb			ASSERT(i < size);
4842179193Sjb			dest[i++] = c;
4843179193Sjb		}
4844179193Sjb
4845179193Sjb		ASSERT(i < size);
4846179193Sjb		dest[i] = '\0';
4847179193Sjb		regs[rd] = (uintptr_t)dest;
4848179193Sjb		mstate->dtms_scratch_ptr += size;
4849179193Sjb		mstate->dtms_strtok = addr;
4850179193Sjb		break;
4851179193Sjb	}
4852179193Sjb
4853179193Sjb	case DIF_SUBR_SUBSTR: {
4854179193Sjb		uintptr_t s = tupregs[0].dttk_value;
4855179193Sjb		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4856179193Sjb		char *d = (char *)mstate->dtms_scratch_ptr;
4857179193Sjb		int64_t index = (int64_t)tupregs[1].dttk_value;
4858179193Sjb		int64_t remaining = (int64_t)tupregs[2].dttk_value;
4859179193Sjb		size_t len = dtrace_strlen((char *)s, size);
4860268290Spfg		int64_t i;
4861179193Sjb
4862179193Sjb		if (!dtrace_canload(s, len + 1, mstate, vstate)) {
4863179198Sjb			regs[rd] = 0;
4864179193Sjb			break;
4865179193Sjb		}
4866179193Sjb
4867179193Sjb		if (!DTRACE_INSCRATCH(mstate, size)) {
4868179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4869179198Sjb			regs[rd] = 0;
4870179193Sjb			break;
4871179193Sjb		}
4872179193Sjb
4873179198Sjb		if (nargs <= 2)
4874179198Sjb			remaining = (int64_t)size;
4875179198Sjb
4876179193Sjb		if (index < 0) {
4877179193Sjb			index += len;
4878179193Sjb
4879179193Sjb			if (index < 0 && index + remaining > 0) {
4880179193Sjb				remaining += index;
4881179193Sjb				index = 0;
4882179193Sjb			}
4883179193Sjb		}
4884179193Sjb
4885179198Sjb		if (index >= len || index < 0) {
4886179198Sjb			remaining = 0;
4887179198Sjb		} else if (remaining < 0) {
4888179198Sjb			remaining += len - index;
4889179198Sjb		} else if (index + remaining > size) {
4890179198Sjb			remaining = size - index;
4891179198Sjb		}
4892179193Sjb
4893179198Sjb		for (i = 0; i < remaining; i++) {
4894179198Sjb			if ((d[i] = dtrace_load8(s + index + i)) == '\0')
4895179193Sjb				break;
4896179193Sjb		}
4897179193Sjb
4898179198Sjb		d[i] = '\0';
4899179198Sjb
4900179193Sjb		mstate->dtms_scratch_ptr += size;
4901179193Sjb		regs[rd] = (uintptr_t)d;
4902179193Sjb		break;
4903179193Sjb	}
4904179193Sjb
4905267937Srpaulo	case DIF_SUBR_JSON: {
4906267937Srpaulo		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4907267937Srpaulo		uintptr_t json = tupregs[0].dttk_value;
4908267937Srpaulo		size_t jsonlen = dtrace_strlen((char *)json, size);
4909267937Srpaulo		uintptr_t elem = tupregs[1].dttk_value;
4910267937Srpaulo		size_t elemlen = dtrace_strlen((char *)elem, size);
4911267937Srpaulo
4912267937Srpaulo		char *dest = (char *)mstate->dtms_scratch_ptr;
4913267937Srpaulo		char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1;
4914267937Srpaulo		char *ee = elemlist;
4915267937Srpaulo		int nelems = 1;
4916267937Srpaulo		uintptr_t cur;
4917267937Srpaulo
4918267937Srpaulo		if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) ||
4919267937Srpaulo		    !dtrace_canload(elem, elemlen + 1, mstate, vstate)) {
4920267937Srpaulo			regs[rd] = 0;
4921267937Srpaulo			break;
4922267937Srpaulo		}
4923267937Srpaulo
4924267937Srpaulo		if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) {
4925267937Srpaulo			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4926267937Srpaulo			regs[rd] = 0;
4927267937Srpaulo			break;
4928267937Srpaulo		}
4929267937Srpaulo
4930267937Srpaulo		/*
4931267937Srpaulo		 * Read the element selector and split it up into a packed list
4932267937Srpaulo		 * of strings.
4933267937Srpaulo		 */
4934267937Srpaulo		for (cur = elem; cur < elem + elemlen; cur++) {
4935267937Srpaulo			char cc = dtrace_load8(cur);
4936267937Srpaulo
4937267937Srpaulo			if (cur == elem && cc == '[') {
4938267937Srpaulo				/*
4939267937Srpaulo				 * If the first element selector key is
4940267937Srpaulo				 * actually an array index then ignore the
4941267937Srpaulo				 * bracket.
4942267937Srpaulo				 */
4943267937Srpaulo				continue;
4944267937Srpaulo			}
4945267937Srpaulo
4946267937Srpaulo			if (cc == ']')
4947267937Srpaulo				continue;
4948267937Srpaulo
4949267937Srpaulo			if (cc == '.' || cc == '[') {
4950267937Srpaulo				nelems++;
4951267937Srpaulo				cc = '\0';
4952267937Srpaulo			}
4953267937Srpaulo
4954267937Srpaulo			*ee++ = cc;
4955267937Srpaulo		}
4956267937Srpaulo		*ee++ = '\0';
4957267937Srpaulo
4958267937Srpaulo		if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist,
4959267937Srpaulo		    nelems, dest)) != 0)
4960267937Srpaulo			mstate->dtms_scratch_ptr += jsonlen + 1;
4961267937Srpaulo		break;
4962267937Srpaulo	}
4963267937Srpaulo
4964248706Spfg	case DIF_SUBR_TOUPPER:
4965248706Spfg	case DIF_SUBR_TOLOWER: {
4966248706Spfg		uintptr_t s = tupregs[0].dttk_value;
4967248706Spfg		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
4968248706Spfg		char *dest = (char *)mstate->dtms_scratch_ptr, c;
4969248706Spfg		size_t len = dtrace_strlen((char *)s, size);
4970248706Spfg		char lower, upper, convert;
4971248706Spfg		int64_t i;
4972248706Spfg
4973248706Spfg		if (subr == DIF_SUBR_TOUPPER) {
4974248706Spfg			lower = 'a';
4975248706Spfg			upper = 'z';
4976248706Spfg			convert = 'A';
4977248706Spfg		} else {
4978248706Spfg			lower = 'A';
4979248706Spfg			upper = 'Z';
4980248706Spfg			convert = 'a';
4981248706Spfg		}
4982248706Spfg
4983248706Spfg		if (!dtrace_canload(s, len + 1, mstate, vstate)) {
4984248706Spfg			regs[rd] = 0;
4985248706Spfg			break;
4986248706Spfg		}
4987248706Spfg
4988248706Spfg		if (!DTRACE_INSCRATCH(mstate, size)) {
4989248706Spfg			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
4990248706Spfg			regs[rd] = 0;
4991248706Spfg			break;
4992248706Spfg		}
4993248706Spfg
4994248706Spfg		for (i = 0; i < size - 1; i++) {
4995248706Spfg			if ((c = dtrace_load8(s + i)) == '\0')
4996248706Spfg				break;
4997248706Spfg
4998248706Spfg			if (c >= lower && c <= upper)
4999248706Spfg				c = convert + (c - lower);
5000248706Spfg
5001248706Spfg			dest[i] = c;
5002248706Spfg		}
5003248706Spfg
5004248706Spfg		ASSERT(i < size);
5005248706Spfg		dest[i] = '\0';
5006248706Spfg		regs[rd] = (uintptr_t)dest;
5007248706Spfg		mstate->dtms_scratch_ptr += size;
5008248706Spfg		break;
5009248706Spfg	}
5010248706Spfg
5011277300Ssmh#ifdef illumos
5012179193Sjb	case DIF_SUBR_GETMAJOR:
5013179193Sjb#ifdef _LP64
5014179193Sjb		regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64;
5015179193Sjb#else
5016179193Sjb		regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ;
5017179193Sjb#endif
5018179193Sjb		break;
5019179193Sjb
5020179193Sjb	case DIF_SUBR_GETMINOR:
5021179193Sjb#ifdef _LP64
5022179193Sjb		regs[rd] = tupregs[0].dttk_value & MAXMIN64;
5023179193Sjb#else
5024179193Sjb		regs[rd] = tupregs[0].dttk_value & MAXMIN;
5025179193Sjb#endif
5026179193Sjb		break;
5027179193Sjb
5028179193Sjb	case DIF_SUBR_DDI_PATHNAME: {
5029179193Sjb		/*
5030179193Sjb		 * This one is a galactic mess.  We are going to roughly
5031179193Sjb		 * emulate ddi_pathname(), but it's made more complicated
5032179193Sjb		 * by the fact that we (a) want to include the minor name and
5033179193Sjb		 * (b) must proceed iteratively instead of recursively.
5034179193Sjb		 */
5035179193Sjb		uintptr_t dest = mstate->dtms_scratch_ptr;
5036179193Sjb		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5037179193Sjb		char *start = (char *)dest, *end = start + size - 1;
5038179193Sjb		uintptr_t daddr = tupregs[0].dttk_value;
5039179193Sjb		int64_t minor = (int64_t)tupregs[1].dttk_value;
5040179193Sjb		char *s;
5041179193Sjb		int i, len, depth = 0;
5042179193Sjb
5043179193Sjb		/*
5044179193Sjb		 * Due to all the pointer jumping we do and context we must
5045179193Sjb		 * rely upon, we just mandate that the user must have kernel
5046179193Sjb		 * read privileges to use this routine.
5047179193Sjb		 */
5048179193Sjb		if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) {
5049179193Sjb			*flags |= CPU_DTRACE_KPRIV;
5050179193Sjb			*illval = daddr;
5051179198Sjb			regs[rd] = 0;
5052179193Sjb		}
5053179193Sjb
5054179193Sjb		if (!DTRACE_INSCRATCH(mstate, size)) {
5055179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5056179198Sjb			regs[rd] = 0;
5057179193Sjb			break;
5058179193Sjb		}
5059179193Sjb
5060179193Sjb		*end = '\0';
5061179193Sjb
5062179193Sjb		/*
5063179193Sjb		 * We want to have a name for the minor.  In order to do this,
5064179193Sjb		 * we need to walk the minor list from the devinfo.  We want
5065179193Sjb		 * to be sure that we don't infinitely walk a circular list,
5066179193Sjb		 * so we check for circularity by sending a scout pointer
5067179193Sjb		 * ahead two elements for every element that we iterate over;
5068179193Sjb		 * if the list is circular, these will ultimately point to the
5069179193Sjb		 * same element.  You may recognize this little trick as the
5070179193Sjb		 * answer to a stupid interview question -- one that always
5071179193Sjb		 * seems to be asked by those who had to have it laboriously
5072179193Sjb		 * explained to them, and who can't even concisely describe
5073179193Sjb		 * the conditions under which one would be forced to resort to
5074179193Sjb		 * this technique.  Needless to say, those conditions are
5075179198Sjb		 * found here -- and probably only here.  Is this the only use
5076179198Sjb		 * of this infamous trick in shipping, production code?  If it
5077179198Sjb		 * isn't, it probably should be...
5078179193Sjb		 */
5079179193Sjb		if (minor != -1) {
5080179193Sjb			uintptr_t maddr = dtrace_loadptr(daddr +
5081179193Sjb			    offsetof(struct dev_info, devi_minor));
5082179193Sjb
5083179193Sjb			uintptr_t next = offsetof(struct ddi_minor_data, next);
5084179193Sjb			uintptr_t name = offsetof(struct ddi_minor_data,
5085179193Sjb			    d_minor) + offsetof(struct ddi_minor, name);
5086179193Sjb			uintptr_t dev = offsetof(struct ddi_minor_data,
5087179193Sjb			    d_minor) + offsetof(struct ddi_minor, dev);
5088179193Sjb			uintptr_t scout;
5089179193Sjb
5090179193Sjb			if (maddr != NULL)
5091179193Sjb				scout = dtrace_loadptr(maddr + next);
5092179193Sjb
5093179193Sjb			while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
5094179193Sjb				uint64_t m;
5095179193Sjb#ifdef _LP64
5096179193Sjb				m = dtrace_load64(maddr + dev) & MAXMIN64;
5097179193Sjb#else
5098179193Sjb				m = dtrace_load32(maddr + dev) & MAXMIN;
5099179193Sjb#endif
5100179193Sjb				if (m != minor) {
5101179193Sjb					maddr = dtrace_loadptr(maddr + next);
5102179193Sjb
5103179193Sjb					if (scout == NULL)
5104179193Sjb						continue;
5105179193Sjb
5106179193Sjb					scout = dtrace_loadptr(scout + next);
5107179193Sjb
5108179193Sjb					if (scout == NULL)
5109179193Sjb						continue;
5110179193Sjb
5111179193Sjb					scout = dtrace_loadptr(scout + next);
5112179193Sjb
5113179193Sjb					if (scout == NULL)
5114179193Sjb						continue;
5115179193Sjb
5116179193Sjb					if (scout == maddr) {
5117179193Sjb						*flags |= CPU_DTRACE_ILLOP;
5118179193Sjb						break;
5119179193Sjb					}
5120179193Sjb
5121179193Sjb					continue;
5122179193Sjb				}
5123179193Sjb
5124179193Sjb				/*
5125179193Sjb				 * We have the minor data.  Now we need to
5126179193Sjb				 * copy the minor's name into the end of the
5127179193Sjb				 * pathname.
5128179193Sjb				 */
5129179193Sjb				s = (char *)dtrace_loadptr(maddr + name);
5130179193Sjb				len = dtrace_strlen(s, size);
5131179193Sjb
5132179193Sjb				if (*flags & CPU_DTRACE_FAULT)
5133179193Sjb					break;
5134179193Sjb
5135179193Sjb				if (len != 0) {
5136179193Sjb					if ((end -= (len + 1)) < start)
5137179193Sjb						break;
5138179193Sjb
5139179193Sjb					*end = ':';
5140179193Sjb				}
5141179193Sjb
5142179193Sjb				for (i = 1; i <= len; i++)
5143179193Sjb					end[i] = dtrace_load8((uintptr_t)s++);
5144179193Sjb				break;
5145179193Sjb			}
5146179193Sjb		}
5147179193Sjb
5148179193Sjb		while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
5149179193Sjb			ddi_node_state_t devi_state;
5150179193Sjb
5151179193Sjb			devi_state = dtrace_load32(daddr +
5152179193Sjb			    offsetof(struct dev_info, devi_node_state));
5153179193Sjb
5154179193Sjb			if (*flags & CPU_DTRACE_FAULT)
5155179193Sjb				break;
5156179193Sjb
5157179193Sjb			if (devi_state >= DS_INITIALIZED) {
5158179193Sjb				s = (char *)dtrace_loadptr(daddr +
5159179193Sjb				    offsetof(struct dev_info, devi_addr));
5160179193Sjb				len = dtrace_strlen(s, size);
5161179193Sjb
5162179193Sjb				if (*flags & CPU_DTRACE_FAULT)
5163179193Sjb					break;
5164179193Sjb
5165179193Sjb				if (len != 0) {
5166179193Sjb					if ((end -= (len + 1)) < start)
5167179193Sjb						break;
5168179193Sjb
5169179193Sjb					*end = '@';
5170179193Sjb				}
5171179193Sjb
5172179193Sjb				for (i = 1; i <= len; i++)
5173179193Sjb					end[i] = dtrace_load8((uintptr_t)s++);
5174179193Sjb			}
5175179193Sjb
5176179193Sjb			/*
5177179193Sjb			 * Now for the node name...
5178179193Sjb			 */
5179179193Sjb			s = (char *)dtrace_loadptr(daddr +
5180179193Sjb			    offsetof(struct dev_info, devi_node_name));
5181179193Sjb
5182179193Sjb			daddr = dtrace_loadptr(daddr +
5183179193Sjb			    offsetof(struct dev_info, devi_parent));
5184179193Sjb
5185179193Sjb			/*
5186179193Sjb			 * If our parent is NULL (that is, if we're the root
5187179193Sjb			 * node), we're going to use the special path
5188179193Sjb			 * "devices".
5189179193Sjb			 */
5190179198Sjb			if (daddr == 0)
5191179193Sjb				s = "devices";
5192179193Sjb
5193179193Sjb			len = dtrace_strlen(s, size);
5194179193Sjb			if (*flags & CPU_DTRACE_FAULT)
5195179193Sjb				break;
5196179193Sjb
5197179193Sjb			if ((end -= (len + 1)) < start)
5198179193Sjb				break;
5199179193Sjb
5200179193Sjb			for (i = 1; i <= len; i++)
5201179193Sjb				end[i] = dtrace_load8((uintptr_t)s++);
5202179193Sjb			*end = '/';
5203179193Sjb
5204179193Sjb			if (depth++ > dtrace_devdepth_max) {
5205179193Sjb				*flags |= CPU_DTRACE_ILLOP;
5206179193Sjb				break;
5207179193Sjb			}
5208179193Sjb		}
5209179193Sjb
5210179193Sjb		if (end < start)
5211179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5212179193Sjb
5213179198Sjb		if (daddr == 0) {
5214179193Sjb			regs[rd] = (uintptr_t)end;
5215179193Sjb			mstate->dtms_scratch_ptr += size;
5216179193Sjb		}
5217179193Sjb
5218179193Sjb		break;
5219179193Sjb	}
5220179198Sjb#endif
5221179193Sjb
5222179193Sjb	case DIF_SUBR_STRJOIN: {
5223179193Sjb		char *d = (char *)mstate->dtms_scratch_ptr;
5224179193Sjb		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5225179193Sjb		uintptr_t s1 = tupregs[0].dttk_value;
5226179193Sjb		uintptr_t s2 = tupregs[1].dttk_value;
5227179193Sjb		int i = 0;
5228179193Sjb
5229179193Sjb		if (!dtrace_strcanload(s1, size, mstate, vstate) ||
5230179193Sjb		    !dtrace_strcanload(s2, size, mstate, vstate)) {
5231179198Sjb			regs[rd] = 0;
5232179193Sjb			break;
5233179193Sjb		}
5234179193Sjb
5235179193Sjb		if (!DTRACE_INSCRATCH(mstate, size)) {
5236179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5237179198Sjb			regs[rd] = 0;
5238179193Sjb			break;
5239179193Sjb		}
5240179193Sjb
5241179193Sjb		for (;;) {
5242179193Sjb			if (i >= size) {
5243179193Sjb				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5244179198Sjb				regs[rd] = 0;
5245179193Sjb				break;
5246179193Sjb			}
5247179193Sjb
5248179193Sjb			if ((d[i++] = dtrace_load8(s1++)) == '\0') {
5249179193Sjb				i--;
5250179193Sjb				break;
5251179193Sjb			}
5252179193Sjb		}
5253179193Sjb
5254179193Sjb		for (;;) {
5255179193Sjb			if (i >= size) {
5256179193Sjb				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5257179198Sjb				regs[rd] = 0;
5258179193Sjb				break;
5259179193Sjb			}
5260179193Sjb
5261179193Sjb			if ((d[i++] = dtrace_load8(s2++)) == '\0')
5262179193Sjb				break;
5263179193Sjb		}
5264179193Sjb
5265179193Sjb		if (i < size) {
5266179193Sjb			mstate->dtms_scratch_ptr += i;
5267179193Sjb			regs[rd] = (uintptr_t)d;
5268179193Sjb		}
5269179193Sjb
5270179193Sjb		break;
5271179193Sjb	}
5272179193Sjb
5273267937Srpaulo	case DIF_SUBR_STRTOLL: {
5274267937Srpaulo		uintptr_t s = tupregs[0].dttk_value;
5275267937Srpaulo		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5276267937Srpaulo		int base = 10;
5277267937Srpaulo
5278267937Srpaulo		if (nargs > 1) {
5279267937Srpaulo			if ((base = tupregs[1].dttk_value) <= 1 ||
5280267937Srpaulo			    base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
5281267937Srpaulo				*flags |= CPU_DTRACE_ILLOP;
5282267937Srpaulo				break;
5283267937Srpaulo			}
5284267937Srpaulo		}
5285267937Srpaulo
5286267937Srpaulo		if (!dtrace_strcanload(s, size, mstate, vstate)) {
5287267937Srpaulo			regs[rd] = INT64_MIN;
5288267937Srpaulo			break;
5289267937Srpaulo		}
5290267937Srpaulo
5291267937Srpaulo		regs[rd] = dtrace_strtoll((char *)s, base, size);
5292267937Srpaulo		break;
5293267937Srpaulo	}
5294267937Srpaulo
5295179193Sjb	case DIF_SUBR_LLTOSTR: {
5296179193Sjb		int64_t i = (int64_t)tupregs[0].dttk_value;
5297248706Spfg		uint64_t val, digit;
5298248706Spfg		uint64_t size = 65;	/* enough room for 2^64 in binary */
5299179193Sjb		char *end = (char *)mstate->dtms_scratch_ptr + size - 1;
5300248706Spfg		int base = 10;
5301179193Sjb
5302248706Spfg		if (nargs > 1) {
5303248706Spfg			if ((base = tupregs[1].dttk_value) <= 1 ||
5304248706Spfg			    base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
5305248706Spfg				*flags |= CPU_DTRACE_ILLOP;
5306248706Spfg				break;
5307248706Spfg			}
5308248706Spfg		}
5309248706Spfg
5310248706Spfg		val = (base == 10 && i < 0) ? i * -1 : i;
5311248706Spfg
5312179193Sjb		if (!DTRACE_INSCRATCH(mstate, size)) {
5313179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5314179198Sjb			regs[rd] = 0;
5315179193Sjb			break;
5316179193Sjb		}
5317179193Sjb
5318248706Spfg		for (*end-- = '\0'; val; val /= base) {
5319248706Spfg			if ((digit = val % base) <= '9' - '0') {
5320248706Spfg				*end-- = '0' + digit;
5321248706Spfg			} else {
5322248706Spfg				*end-- = 'a' + (digit - ('9' - '0') - 1);
5323248706Spfg			}
5324248706Spfg		}
5325179193Sjb
5326248706Spfg		if (i == 0 && base == 16)
5327179193Sjb			*end-- = '0';
5328179193Sjb
5329248706Spfg		if (base == 16)
5330248706Spfg			*end-- = 'x';
5331248706Spfg
5332248706Spfg		if (i == 0 || base == 8 || base == 16)
5333248706Spfg			*end-- = '0';
5334248706Spfg
5335248706Spfg		if (i < 0 && base == 10)
5336179193Sjb			*end-- = '-';
5337179193Sjb
5338179193Sjb		regs[rd] = (uintptr_t)end + 1;
5339179193Sjb		mstate->dtms_scratch_ptr += size;
5340179193Sjb		break;
5341179193Sjb	}
5342179193Sjb
5343179193Sjb	case DIF_SUBR_HTONS:
5344179193Sjb	case DIF_SUBR_NTOHS:
5345179198Sjb#if BYTE_ORDER == BIG_ENDIAN
5346179193Sjb		regs[rd] = (uint16_t)tupregs[0].dttk_value;
5347179193Sjb#else
5348179193Sjb		regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value);
5349179193Sjb#endif
5350179193Sjb		break;
5351179193Sjb
5352179193Sjb
5353179193Sjb	case DIF_SUBR_HTONL:
5354179193Sjb	case DIF_SUBR_NTOHL:
5355179198Sjb#if BYTE_ORDER == BIG_ENDIAN
5356179193Sjb		regs[rd] = (uint32_t)tupregs[0].dttk_value;
5357179193Sjb#else
5358179193Sjb		regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value);
5359179193Sjb#endif
5360179193Sjb		break;
5361179193Sjb
5362179193Sjb
5363179193Sjb	case DIF_SUBR_HTONLL:
5364179193Sjb	case DIF_SUBR_NTOHLL:
5365179198Sjb#if BYTE_ORDER == BIG_ENDIAN
5366179193Sjb		regs[rd] = (uint64_t)tupregs[0].dttk_value;
5367179193Sjb#else
5368179193Sjb		regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value);
5369179193Sjb#endif
5370179193Sjb		break;
5371179193Sjb
5372179193Sjb
5373179193Sjb	case DIF_SUBR_DIRNAME:
5374179193Sjb	case DIF_SUBR_BASENAME: {
5375179193Sjb		char *dest = (char *)mstate->dtms_scratch_ptr;
5376179193Sjb		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5377179193Sjb		uintptr_t src = tupregs[0].dttk_value;
5378179193Sjb		int i, j, len = dtrace_strlen((char *)src, size);
5379179193Sjb		int lastbase = -1, firstbase = -1, lastdir = -1;
5380179193Sjb		int start, end;
5381179193Sjb
5382179193Sjb		if (!dtrace_canload(src, len + 1, mstate, vstate)) {
5383179198Sjb			regs[rd] = 0;
5384179193Sjb			break;
5385179193Sjb		}
5386179193Sjb
5387179193Sjb		if (!DTRACE_INSCRATCH(mstate, size)) {
5388179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5389179198Sjb			regs[rd] = 0;
5390179193Sjb			break;
5391179193Sjb		}
5392179193Sjb
5393179193Sjb		/*
5394179193Sjb		 * The basename and dirname for a zero-length string is
5395179193Sjb		 * defined to be "."
5396179193Sjb		 */
5397179193Sjb		if (len == 0) {
5398179193Sjb			len = 1;
5399179193Sjb			src = (uintptr_t)".";
5400179193Sjb		}
5401179193Sjb
5402179193Sjb		/*
5403179193Sjb		 * Start from the back of the string, moving back toward the
5404179193Sjb		 * front until we see a character that isn't a slash.  That
5405179193Sjb		 * character is the last character in the basename.
5406179193Sjb		 */
5407179193Sjb		for (i = len - 1; i >= 0; i--) {
5408179193Sjb			if (dtrace_load8(src + i) != '/')
5409179193Sjb				break;
5410179193Sjb		}
5411179193Sjb
5412179193Sjb		if (i >= 0)
5413179193Sjb			lastbase = i;
5414179193Sjb
5415179193Sjb		/*
5416179193Sjb		 * Starting from the last character in the basename, move
5417179193Sjb		 * towards the front until we find a slash.  The character
5418179193Sjb		 * that we processed immediately before that is the first
5419179193Sjb		 * character in the basename.
5420179193Sjb		 */
5421179193Sjb		for (; i >= 0; i--) {
5422179193Sjb			if (dtrace_load8(src + i) == '/')
5423179193Sjb				break;
5424179193Sjb		}
5425179193Sjb
5426179193Sjb		if (i >= 0)
5427179193Sjb			firstbase = i + 1;
5428179193Sjb
5429179193Sjb		/*
5430179193Sjb		 * Now keep going until we find a non-slash character.  That
5431179193Sjb		 * character is the last character in the dirname.
5432179193Sjb		 */
5433179193Sjb		for (; i >= 0; i--) {
5434179193Sjb			if (dtrace_load8(src + i) != '/')
5435179193Sjb				break;
5436179193Sjb		}
5437179193Sjb
5438179193Sjb		if (i >= 0)
5439179193Sjb			lastdir = i;
5440179193Sjb
5441179193Sjb		ASSERT(!(lastbase == -1 && firstbase != -1));
5442179193Sjb		ASSERT(!(firstbase == -1 && lastdir != -1));
5443179193Sjb
5444179193Sjb		if (lastbase == -1) {
5445179193Sjb			/*
5446179193Sjb			 * We didn't find a non-slash character.  We know that
5447179193Sjb			 * the length is non-zero, so the whole string must be
5448179193Sjb			 * slashes.  In either the dirname or the basename
5449179193Sjb			 * case, we return '/'.
5450179193Sjb			 */
5451179193Sjb			ASSERT(firstbase == -1);
5452179193Sjb			firstbase = lastbase = lastdir = 0;
5453179193Sjb		}
5454179193Sjb
5455179193Sjb		if (firstbase == -1) {
5456179193Sjb			/*
5457179193Sjb			 * The entire string consists only of a basename
5458179193Sjb			 * component.  If we're looking for dirname, we need
5459179193Sjb			 * to change our string to be just "."; if we're
5460179193Sjb			 * looking for a basename, we'll just set the first
5461179193Sjb			 * character of the basename to be 0.
5462179193Sjb			 */
5463179193Sjb			if (subr == DIF_SUBR_DIRNAME) {
5464179193Sjb				ASSERT(lastdir == -1);
5465179193Sjb				src = (uintptr_t)".";
5466179193Sjb				lastdir = 0;
5467179193Sjb			} else {
5468179193Sjb				firstbase = 0;
5469179193Sjb			}
5470179193Sjb		}
5471179193Sjb
5472179193Sjb		if (subr == DIF_SUBR_DIRNAME) {
5473179193Sjb			if (lastdir == -1) {
5474179193Sjb				/*
5475179193Sjb				 * We know that we have a slash in the name --
5476179193Sjb				 * or lastdir would be set to 0, above.  And
5477179193Sjb				 * because lastdir is -1, we know that this
5478179193Sjb				 * slash must be the first character.  (That
5479179193Sjb				 * is, the full string must be of the form
5480179193Sjb				 * "/basename".)  In this case, the last
5481179193Sjb				 * character of the directory name is 0.
5482179193Sjb				 */
5483179193Sjb				lastdir = 0;
5484179193Sjb			}
5485179193Sjb
5486179193Sjb			start = 0;
5487179193Sjb			end = lastdir;
5488179193Sjb		} else {
5489179193Sjb			ASSERT(subr == DIF_SUBR_BASENAME);
5490179193Sjb			ASSERT(firstbase != -1 && lastbase != -1);
5491179193Sjb			start = firstbase;
5492179193Sjb			end = lastbase;
5493179193Sjb		}
5494179193Sjb
5495179193Sjb		for (i = start, j = 0; i <= end && j < size - 1; i++, j++)
5496179193Sjb			dest[j] = dtrace_load8(src + i);
5497179193Sjb
5498179193Sjb		dest[j] = '\0';
5499179193Sjb		regs[rd] = (uintptr_t)dest;
5500179193Sjb		mstate->dtms_scratch_ptr += size;
5501179193Sjb		break;
5502179193Sjb	}
5503179193Sjb
5504267929Srpaulo	case DIF_SUBR_GETF: {
5505267929Srpaulo		uintptr_t fd = tupregs[0].dttk_value;
5506267929Srpaulo		struct filedesc *fdp;
5507267929Srpaulo		file_t *fp;
5508267929Srpaulo
5509267929Srpaulo		if (!dtrace_priv_proc(state)) {
5510267929Srpaulo			regs[rd] = 0;
5511267929Srpaulo			break;
5512267929Srpaulo		}
5513267929Srpaulo		fdp = curproc->p_fd;
5514267929Srpaulo		FILEDESC_SLOCK(fdp);
5515267929Srpaulo		fp = fget_locked(fdp, fd);
5516267929Srpaulo		mstate->dtms_getf = fp;
5517267929Srpaulo		regs[rd] = (uintptr_t)fp;
5518267929Srpaulo		FILEDESC_SUNLOCK(fdp);
5519267929Srpaulo		break;
5520267929Srpaulo	}
5521267929Srpaulo
5522179193Sjb	case DIF_SUBR_CLEANPATH: {
5523179193Sjb		char *dest = (char *)mstate->dtms_scratch_ptr, c;
5524179193Sjb		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
5525179193Sjb		uintptr_t src = tupregs[0].dttk_value;
5526179193Sjb		int i = 0, j = 0;
5527277300Ssmh#ifdef illumos
5528267929Srpaulo		zone_t *z;
5529267929Srpaulo#endif
5530179193Sjb
5531179193Sjb		if (!dtrace_strcanload(src, size, mstate, vstate)) {
5532179198Sjb			regs[rd] = 0;
5533179193Sjb			break;
5534179193Sjb		}
5535179193Sjb
5536179193Sjb		if (!DTRACE_INSCRATCH(mstate, size)) {
5537179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5538179198Sjb			regs[rd] = 0;
5539179193Sjb			break;
5540179193Sjb		}
5541179193Sjb
5542179193Sjb		/*
5543179193Sjb		 * Move forward, loading each character.
5544179193Sjb		 */
5545179193Sjb		do {
5546179193Sjb			c = dtrace_load8(src + i++);
5547179193Sjbnext:
5548179193Sjb			if (j + 5 >= size)	/* 5 = strlen("/..c\0") */
5549179193Sjb				break;
5550179193Sjb
5551179193Sjb			if (c != '/') {
5552179193Sjb				dest[j++] = c;
5553179193Sjb				continue;
5554179193Sjb			}
5555179193Sjb
5556179193Sjb			c = dtrace_load8(src + i++);
5557179193Sjb
5558179193Sjb			if (c == '/') {
5559179193Sjb				/*
5560179193Sjb				 * We have two slashes -- we can just advance
5561179193Sjb				 * to the next character.
5562179193Sjb				 */
5563179193Sjb				goto next;
5564179193Sjb			}
5565179193Sjb
5566179193Sjb			if (c != '.') {
5567179193Sjb				/*
5568179193Sjb				 * This is not "." and it's not ".." -- we can
5569179193Sjb				 * just store the "/" and this character and
5570179193Sjb				 * drive on.
5571179193Sjb				 */
5572179193Sjb				dest[j++] = '/';
5573179193Sjb				dest[j++] = c;
5574179193Sjb				continue;
5575179193Sjb			}
5576179193Sjb
5577179193Sjb			c = dtrace_load8(src + i++);
5578179193Sjb
5579179193Sjb			if (c == '/') {
5580179193Sjb				/*
5581179193Sjb				 * This is a "/./" component.  We're not going
5582179193Sjb				 * to store anything in the destination buffer;
5583179193Sjb				 * we're just going to go to the next component.
5584179193Sjb				 */
5585179193Sjb				goto next;
5586179193Sjb			}
5587179193Sjb
5588179193Sjb			if (c != '.') {
5589179193Sjb				/*
5590179193Sjb				 * This is not ".." -- we can just store the
5591179193Sjb				 * "/." and this character and continue
5592179193Sjb				 * processing.
5593179193Sjb				 */
5594179193Sjb				dest[j++] = '/';
5595179193Sjb				dest[j++] = '.';
5596179193Sjb				dest[j++] = c;
5597179193Sjb				continue;
5598179193Sjb			}
5599179193Sjb
5600179193Sjb			c = dtrace_load8(src + i++);
5601179193Sjb
5602179193Sjb			if (c != '/' && c != '\0') {
5603179193Sjb				/*
5604179193Sjb				 * This is not ".." -- it's "..[mumble]".
5605179193Sjb				 * We'll store the "/.." and this character
5606179193Sjb				 * and continue processing.
5607179193Sjb				 */
5608179193Sjb				dest[j++] = '/';
5609179193Sjb				dest[j++] = '.';
5610179193Sjb				dest[j++] = '.';
5611179193Sjb				dest[j++] = c;
5612179193Sjb				continue;
5613179193Sjb			}
5614179193Sjb
5615179193Sjb			/*
5616179193Sjb			 * This is "/../" or "/..\0".  We need to back up
5617179193Sjb			 * our destination pointer until we find a "/".
5618179193Sjb			 */
5619179193Sjb			i--;
5620179193Sjb			while (j != 0 && dest[--j] != '/')
5621179193Sjb				continue;
5622179193Sjb
5623179193Sjb			if (c == '\0')
5624179193Sjb				dest[++j] = '/';
5625179193Sjb		} while (c != '\0');
5626179193Sjb
5627179193Sjb		dest[j] = '\0';
5628267929Srpaulo
5629277300Ssmh#ifdef illumos
5630267929Srpaulo		if (mstate->dtms_getf != NULL &&
5631267929Srpaulo		    !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) &&
5632267929Srpaulo		    (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) {
5633267929Srpaulo			/*
5634267929Srpaulo			 * If we've done a getf() as a part of this ECB and we
5635267929Srpaulo			 * don't have kernel access (and we're not in the global
5636267929Srpaulo			 * zone), check if the path we cleaned up begins with
5637267929Srpaulo			 * the zone's root path, and trim it off if so.  Note
5638267929Srpaulo			 * that this is an output cleanliness issue, not a
5639267929Srpaulo			 * security issue: knowing one's zone root path does
5640267929Srpaulo			 * not enable privilege escalation.
5641267929Srpaulo			 */
5642267929Srpaulo			if (strstr(dest, z->zone_rootpath) == dest)
5643267929Srpaulo				dest += strlen(z->zone_rootpath) - 1;
5644267929Srpaulo		}
5645267929Srpaulo#endif
5646267929Srpaulo
5647179193Sjb		regs[rd] = (uintptr_t)dest;
5648179193Sjb		mstate->dtms_scratch_ptr += size;
5649179193Sjb		break;
5650179193Sjb	}
5651179193Sjb
5652179193Sjb	case DIF_SUBR_INET_NTOA:
5653179193Sjb	case DIF_SUBR_INET_NTOA6:
5654179193Sjb	case DIF_SUBR_INET_NTOP: {
5655179193Sjb		size_t size;
5656179193Sjb		int af, argi, i;
5657179193Sjb		char *base, *end;
5658179193Sjb
5659179193Sjb		if (subr == DIF_SUBR_INET_NTOP) {
5660179193Sjb			af = (int)tupregs[0].dttk_value;
5661179193Sjb			argi = 1;
5662179193Sjb		} else {
5663179193Sjb			af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6;
5664179193Sjb			argi = 0;
5665179193Sjb		}
5666179193Sjb
5667179193Sjb		if (af == AF_INET) {
5668179193Sjb			ipaddr_t ip4;
5669179193Sjb			uint8_t *ptr8, val;
5670179193Sjb
5671296475Smarkj			if (!dtrace_canload(tupregs[argi].dttk_value,
5672296475Smarkj			    sizeof (ipaddr_t), mstate, vstate)) {
5673296475Smarkj				regs[rd] = 0;
5674296475Smarkj				break;
5675296475Smarkj			}
5676296475Smarkj
5677179193Sjb			/*
5678179193Sjb			 * Safely load the IPv4 address.
5679179193Sjb			 */
5680179193Sjb			ip4 = dtrace_load32(tupregs[argi].dttk_value);
5681179193Sjb
5682179193Sjb			/*
5683179193Sjb			 * Check an IPv4 string will fit in scratch.
5684179193Sjb			 */
5685179193Sjb			size = INET_ADDRSTRLEN;
5686179193Sjb			if (!DTRACE_INSCRATCH(mstate, size)) {
5687179193Sjb				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5688179198Sjb				regs[rd] = 0;
5689179193Sjb				break;
5690179193Sjb			}
5691179193Sjb			base = (char *)mstate->dtms_scratch_ptr;
5692179193Sjb			end = (char *)mstate->dtms_scratch_ptr + size - 1;
5693179193Sjb
5694179193Sjb			/*
5695179193Sjb			 * Stringify as a dotted decimal quad.
5696179193Sjb			 */
5697179193Sjb			*end-- = '\0';
5698179193Sjb			ptr8 = (uint8_t *)&ip4;
5699179193Sjb			for (i = 3; i >= 0; i--) {
5700179193Sjb				val = ptr8[i];
5701179193Sjb
5702179193Sjb				if (val == 0) {
5703179193Sjb					*end-- = '0';
5704179193Sjb				} else {
5705179193Sjb					for (; val; val /= 10) {
5706179193Sjb						*end-- = '0' + (val % 10);
5707179193Sjb					}
5708179193Sjb				}
5709179193Sjb
5710179193Sjb				if (i > 0)
5711179193Sjb					*end-- = '.';
5712179193Sjb			}
5713179193Sjb			ASSERT(end + 1 >= base);
5714179193Sjb
5715179193Sjb		} else if (af == AF_INET6) {
5716179193Sjb			struct in6_addr ip6;
5717179193Sjb			int firstzero, tryzero, numzero, v6end;
5718179193Sjb			uint16_t val;
5719179193Sjb			const char digits[] = "0123456789abcdef";
5720179193Sjb
5721179193Sjb			/*
5722179193Sjb			 * Stringify using RFC 1884 convention 2 - 16 bit
5723179193Sjb			 * hexadecimal values with a zero-run compression.
5724179193Sjb			 * Lower case hexadecimal digits are used.
5725179193Sjb			 * 	eg, fe80::214:4fff:fe0b:76c8.
5726179193Sjb			 * The IPv4 embedded form is returned for inet_ntop,
5727179193Sjb			 * just the IPv4 string is returned for inet_ntoa6.
5728179193Sjb			 */
5729179193Sjb
5730296475Smarkj			if (!dtrace_canload(tupregs[argi].dttk_value,
5731296475Smarkj			    sizeof (struct in6_addr), mstate, vstate)) {
5732296475Smarkj				regs[rd] = 0;
5733296475Smarkj				break;
5734296475Smarkj			}
5735296475Smarkj
5736179193Sjb			/*
5737179193Sjb			 * Safely load the IPv6 address.
5738179193Sjb			 */
5739179193Sjb			dtrace_bcopy(
5740179193Sjb			    (void *)(uintptr_t)tupregs[argi].dttk_value,
5741179193Sjb			    (void *)(uintptr_t)&ip6, sizeof (struct in6_addr));
5742179193Sjb
5743179193Sjb			/*
5744179193Sjb			 * Check an IPv6 string will fit in scratch.
5745179193Sjb			 */
5746179193Sjb			size = INET6_ADDRSTRLEN;
5747179193Sjb			if (!DTRACE_INSCRATCH(mstate, size)) {
5748179193Sjb				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5749179198Sjb				regs[rd] = 0;
5750179193Sjb				break;
5751179193Sjb			}
5752179193Sjb			base = (char *)mstate->dtms_scratch_ptr;
5753179193Sjb			end = (char *)mstate->dtms_scratch_ptr + size - 1;
5754179193Sjb			*end-- = '\0';
5755179193Sjb
5756179193Sjb			/*
5757179193Sjb			 * Find the longest run of 16 bit zero values
5758179193Sjb			 * for the single allowed zero compression - "::".
5759179193Sjb			 */
5760179193Sjb			firstzero = -1;
5761179193Sjb			tryzero = -1;
5762179193Sjb			numzero = 1;
5763179193Sjb			for (i = 0; i < sizeof (struct in6_addr); i++) {
5764277300Ssmh#ifdef illumos
5765179193Sjb				if (ip6._S6_un._S6_u8[i] == 0 &&
5766179198Sjb#else
5767179198Sjb				if (ip6.__u6_addr.__u6_addr8[i] == 0 &&
5768179198Sjb#endif
5769179193Sjb				    tryzero == -1 && i % 2 == 0) {
5770179193Sjb					tryzero = i;
5771179193Sjb					continue;
5772179193Sjb				}
5773179193Sjb
5774179193Sjb				if (tryzero != -1 &&
5775277300Ssmh#ifdef illumos
5776179193Sjb				    (ip6._S6_un._S6_u8[i] != 0 ||
5777179198Sjb#else
5778179198Sjb				    (ip6.__u6_addr.__u6_addr8[i] != 0 ||
5779179198Sjb#endif
5780179193Sjb				    i == sizeof (struct in6_addr) - 1)) {
5781179193Sjb
5782179193Sjb					if (i - tryzero <= numzero) {
5783179193Sjb						tryzero = -1;
5784179193Sjb						continue;
5785179193Sjb					}
5786179193Sjb
5787179193Sjb					firstzero = tryzero;
5788179193Sjb					numzero = i - i % 2 - tryzero;
5789179193Sjb					tryzero = -1;
5790179193Sjb
5791277300Ssmh#ifdef illumos
5792179193Sjb					if (ip6._S6_un._S6_u8[i] == 0 &&
5793179198Sjb#else
5794179198Sjb					if (ip6.__u6_addr.__u6_addr8[i] == 0 &&
5795179198Sjb#endif
5796179193Sjb					    i == sizeof (struct in6_addr) - 1)
5797179193Sjb						numzero += 2;
5798179193Sjb				}
5799179193Sjb			}
5800179193Sjb			ASSERT(firstzero + numzero <= sizeof (struct in6_addr));
5801179193Sjb
5802179193Sjb			/*
5803179193Sjb			 * Check for an IPv4 embedded address.
5804179193Sjb			 */
5805179193Sjb			v6end = sizeof (struct in6_addr) - 2;
5806179193Sjb			if (IN6_IS_ADDR_V4MAPPED(&ip6) ||
5807179193Sjb			    IN6_IS_ADDR_V4COMPAT(&ip6)) {
5808179193Sjb				for (i = sizeof (struct in6_addr) - 1;
5809179193Sjb				    i >= DTRACE_V4MAPPED_OFFSET; i--) {
5810179193Sjb					ASSERT(end >= base);
5811179193Sjb
5812277300Ssmh#ifdef illumos
5813179193Sjb					val = ip6._S6_un._S6_u8[i];
5814179198Sjb#else
5815179198Sjb					val = ip6.__u6_addr.__u6_addr8[i];
5816179198Sjb#endif
5817179193Sjb
5818179193Sjb					if (val == 0) {
5819179193Sjb						*end-- = '0';
5820179193Sjb					} else {
5821179193Sjb						for (; val; val /= 10) {
5822179193Sjb							*end-- = '0' + val % 10;
5823179193Sjb						}
5824179193Sjb					}
5825179193Sjb
5826179193Sjb					if (i > DTRACE_V4MAPPED_OFFSET)
5827179193Sjb						*end-- = '.';
5828179193Sjb				}
5829179193Sjb
5830179193Sjb				if (subr == DIF_SUBR_INET_NTOA6)
5831179193Sjb					goto inetout;
5832179193Sjb
5833179193Sjb				/*
5834179193Sjb				 * Set v6end to skip the IPv4 address that
5835179193Sjb				 * we have already stringified.
5836179193Sjb				 */
5837179193Sjb				v6end = 10;
5838179193Sjb			}
5839179193Sjb
5840179193Sjb			/*
5841179193Sjb			 * Build the IPv6 string by working through the
5842179193Sjb			 * address in reverse.
5843179193Sjb			 */
5844179193Sjb			for (i = v6end; i >= 0; i -= 2) {
5845179193Sjb				ASSERT(end >= base);
5846179193Sjb
5847179193Sjb				if (i == firstzero + numzero - 2) {
5848179193Sjb					*end-- = ':';
5849179193Sjb					*end-- = ':';
5850179193Sjb					i -= numzero - 2;
5851179193Sjb					continue;
5852179193Sjb				}
5853179193Sjb
5854179193Sjb				if (i < 14 && i != firstzero - 2)
5855179193Sjb					*end-- = ':';
5856179193Sjb
5857277300Ssmh#ifdef illumos
5858179193Sjb				val = (ip6._S6_un._S6_u8[i] << 8) +
5859179193Sjb				    ip6._S6_un._S6_u8[i + 1];
5860179198Sjb#else
5861179198Sjb				val = (ip6.__u6_addr.__u6_addr8[i] << 8) +
5862179198Sjb				    ip6.__u6_addr.__u6_addr8[i + 1];
5863179198Sjb#endif
5864179193Sjb
5865179193Sjb				if (val == 0) {
5866179193Sjb					*end-- = '0';
5867179193Sjb				} else {
5868179193Sjb					for (; val; val /= 16) {
5869179193Sjb						*end-- = digits[val % 16];
5870179193Sjb					}
5871179193Sjb				}
5872179193Sjb			}
5873179193Sjb			ASSERT(end + 1 >= base);
5874179193Sjb
5875179193Sjb		} else {
5876179193Sjb			/*
5877179193Sjb			 * The user didn't use AH_INET or AH_INET6.
5878179193Sjb			 */
5879179193Sjb			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5880179198Sjb			regs[rd] = 0;
5881179193Sjb			break;
5882179193Sjb		}
5883179193Sjb
5884179193Sjbinetout:	regs[rd] = (uintptr_t)end + 1;
5885179193Sjb		mstate->dtms_scratch_ptr += size;
5886179193Sjb		break;
5887179193Sjb	}
5888179193Sjb
5889179198Sjb	case DIF_SUBR_MEMREF: {
5890179198Sjb		uintptr_t size = 2 * sizeof(uintptr_t);
5891179198Sjb		uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t));
5892179198Sjb		size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size;
5893179198Sjb
5894179198Sjb		/* address and length */
5895179198Sjb		memref[0] = tupregs[0].dttk_value;
5896179198Sjb		memref[1] = tupregs[1].dttk_value;
5897179198Sjb
5898179198Sjb		regs[rd] = (uintptr_t) memref;
5899179198Sjb		mstate->dtms_scratch_ptr += scratch_size;
5900179198Sjb		break;
5901179193Sjb	}
5902179198Sjb
5903277300Ssmh#ifndef illumos
5904256571Smarkj	case DIF_SUBR_MEMSTR: {
5905256571Smarkj		char *str = (char *)mstate->dtms_scratch_ptr;
5906256571Smarkj		uintptr_t mem = tupregs[0].dttk_value;
5907256571Smarkj		char c = tupregs[1].dttk_value;
5908256571Smarkj		size_t size = tupregs[2].dttk_value;
5909256571Smarkj		uint8_t n;
5910256571Smarkj		int i;
5911256571Smarkj
5912256571Smarkj		regs[rd] = 0;
5913256571Smarkj
5914256571Smarkj		if (size == 0)
5915256571Smarkj			break;
5916256571Smarkj
5917256571Smarkj		if (!dtrace_canload(mem, size - 1, mstate, vstate))
5918256571Smarkj			break;
5919256571Smarkj
5920256571Smarkj		if (!DTRACE_INSCRATCH(mstate, size)) {
5921256571Smarkj			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
5922256571Smarkj			break;
5923256571Smarkj		}
5924256571Smarkj
5925256571Smarkj		if (dtrace_memstr_max != 0 && size > dtrace_memstr_max) {
5926256571Smarkj			*flags |= CPU_DTRACE_ILLOP;
5927256571Smarkj			break;
5928256571Smarkj		}
5929256571Smarkj
5930256571Smarkj		for (i = 0; i < size - 1; i++) {
5931256571Smarkj			n = dtrace_load8(mem++);
5932256571Smarkj			str[i] = (n == 0) ? c : n;
5933256571Smarkj		}
5934256571Smarkj		str[size - 1] = 0;
5935256571Smarkj
5936256571Smarkj		regs[rd] = (uintptr_t)str;
5937256571Smarkj		mstate->dtms_scratch_ptr += size;
5938256571Smarkj		break;
5939256571Smarkj	}
5940256571Smarkj#endif
5941256571Smarkj
5942179198Sjb	case DIF_SUBR_TYPEREF: {
5943179198Sjb		uintptr_t size = 4 * sizeof(uintptr_t);
5944179198Sjb		uintptr_t *typeref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t));
5945179198Sjb		size_t scratch_size = ((uintptr_t) typeref - mstate->dtms_scratch_ptr) + size;
5946179198Sjb
5947179198Sjb		/* address, num_elements, type_str, type_len */
5948179198Sjb		typeref[0] = tupregs[0].dttk_value;
5949179198Sjb		typeref[1] = tupregs[1].dttk_value;
5950179198Sjb		typeref[2] = tupregs[2].dttk_value;
5951179198Sjb		typeref[3] = tupregs[3].dttk_value;
5952179198Sjb
5953179198Sjb		regs[rd] = (uintptr_t) typeref;
5954179198Sjb		mstate->dtms_scratch_ptr += scratch_size;
5955179198Sjb		break;
5956179198Sjb	}
5957179198Sjb	}
5958179193Sjb}
5959179193Sjb
5960179193Sjb/*
5961179193Sjb * Emulate the execution of DTrace IR instructions specified by the given
5962179193Sjb * DIF object.  This function is deliberately void of assertions as all of
5963179193Sjb * the necessary checks are handled by a call to dtrace_difo_validate().
5964179193Sjb */
5965179193Sjbstatic uint64_t
5966179193Sjbdtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate,
5967179193Sjb    dtrace_vstate_t *vstate, dtrace_state_t *state)
5968179193Sjb{
5969179193Sjb	const dif_instr_t *text = difo->dtdo_buf;
5970179193Sjb	const uint_t textlen = difo->dtdo_len;
5971179193Sjb	const char *strtab = difo->dtdo_strtab;
5972179193Sjb	const uint64_t *inttab = difo->dtdo_inttab;
5973179193Sjb
5974179193Sjb	uint64_t rval = 0;
5975179193Sjb	dtrace_statvar_t *svar;
5976179193Sjb	dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
5977179193Sjb	dtrace_difv_t *v;
5978179198Sjb	volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
5979179198Sjb	volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
5980179193Sjb
5981179193Sjb	dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
5982179193Sjb	uint64_t regs[DIF_DIR_NREGS];
5983179193Sjb	uint64_t *tmp;
5984179193Sjb
5985179193Sjb	uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0;
5986179193Sjb	int64_t cc_r;
5987179198Sjb	uint_t pc = 0, id, opc = 0;
5988179193Sjb	uint8_t ttop = 0;
5989179193Sjb	dif_instr_t instr;
5990179193Sjb	uint_t r1, r2, rd;
5991179193Sjb
5992179193Sjb	/*
5993179193Sjb	 * We stash the current DIF object into the machine state: we need it
5994179193Sjb	 * for subsequent access checking.
5995179193Sjb	 */
5996179193Sjb	mstate->dtms_difo = difo;
5997179193Sjb
5998179193Sjb	regs[DIF_REG_R0] = 0; 		/* %r0 is fixed at zero */
5999179193Sjb
6000179193Sjb	while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) {
6001179193Sjb		opc = pc;
6002179193Sjb
6003179193Sjb		instr = text[pc++];
6004179193Sjb		r1 = DIF_INSTR_R1(instr);
6005179193Sjb		r2 = DIF_INSTR_R2(instr);
6006179193Sjb		rd = DIF_INSTR_RD(instr);
6007179193Sjb
6008179193Sjb		switch (DIF_INSTR_OP(instr)) {
6009179193Sjb		case DIF_OP_OR:
6010179193Sjb			regs[rd] = regs[r1] | regs[r2];
6011179193Sjb			break;
6012179193Sjb		case DIF_OP_XOR:
6013179193Sjb			regs[rd] = regs[r1] ^ regs[r2];
6014179193Sjb			break;
6015179193Sjb		case DIF_OP_AND:
6016179193Sjb			regs[rd] = regs[r1] & regs[r2];
6017179193Sjb			break;
6018179193Sjb		case DIF_OP_SLL:
6019179193Sjb			regs[rd] = regs[r1] << regs[r2];
6020179193Sjb			break;
6021179193Sjb		case DIF_OP_SRL:
6022179193Sjb			regs[rd] = regs[r1] >> regs[r2];
6023179193Sjb			break;
6024179193Sjb		case DIF_OP_SUB:
6025179193Sjb			regs[rd] = regs[r1] - regs[r2];
6026179193Sjb			break;
6027179193Sjb		case DIF_OP_ADD:
6028179193Sjb			regs[rd] = regs[r1] + regs[r2];
6029179193Sjb			break;
6030179193Sjb		case DIF_OP_MUL:
6031179193Sjb			regs[rd] = regs[r1] * regs[r2];
6032179193Sjb			break;
6033179193Sjb		case DIF_OP_SDIV:
6034179193Sjb			if (regs[r2] == 0) {
6035179193Sjb				regs[rd] = 0;
6036179193Sjb				*flags |= CPU_DTRACE_DIVZERO;
6037179193Sjb			} else {
6038179193Sjb				regs[rd] = (int64_t)regs[r1] /
6039179193Sjb				    (int64_t)regs[r2];
6040179193Sjb			}
6041179193Sjb			break;
6042179193Sjb
6043179193Sjb		case DIF_OP_UDIV:
6044179193Sjb			if (regs[r2] == 0) {
6045179193Sjb				regs[rd] = 0;
6046179193Sjb				*flags |= CPU_DTRACE_DIVZERO;
6047179193Sjb			} else {
6048179193Sjb				regs[rd] = regs[r1] / regs[r2];
6049179193Sjb			}
6050179193Sjb			break;
6051179193Sjb
6052179193Sjb		case DIF_OP_SREM:
6053179193Sjb			if (regs[r2] == 0) {
6054179193Sjb				regs[rd] = 0;
6055179193Sjb				*flags |= CPU_DTRACE_DIVZERO;
6056179193Sjb			} else {
6057179193Sjb				regs[rd] = (int64_t)regs[r1] %
6058179193Sjb				    (int64_t)regs[r2];
6059179193Sjb			}
6060179193Sjb			break;
6061179193Sjb
6062179193Sjb		case DIF_OP_UREM:
6063179193Sjb			if (regs[r2] == 0) {
6064179193Sjb				regs[rd] = 0;
6065179193Sjb				*flags |= CPU_DTRACE_DIVZERO;
6066179193Sjb			} else {
6067179193Sjb				regs[rd] = regs[r1] % regs[r2];
6068179193Sjb			}
6069179193Sjb			break;
6070179193Sjb
6071179193Sjb		case DIF_OP_NOT:
6072179193Sjb			regs[rd] = ~regs[r1];
6073179193Sjb			break;
6074179193Sjb		case DIF_OP_MOV:
6075179193Sjb			regs[rd] = regs[r1];
6076179193Sjb			break;
6077179193Sjb		case DIF_OP_CMP:
6078179193Sjb			cc_r = regs[r1] - regs[r2];
6079179193Sjb			cc_n = cc_r < 0;
6080179193Sjb			cc_z = cc_r == 0;
6081179193Sjb			cc_v = 0;
6082179193Sjb			cc_c = regs[r1] < regs[r2];
6083179193Sjb			break;
6084179193Sjb		case DIF_OP_TST:
6085179193Sjb			cc_n = cc_v = cc_c = 0;
6086179193Sjb			cc_z = regs[r1] == 0;
6087179193Sjb			break;
6088179193Sjb		case DIF_OP_BA:
6089179193Sjb			pc = DIF_INSTR_LABEL(instr);
6090179193Sjb			break;
6091179193Sjb		case DIF_OP_BE:
6092179193Sjb			if (cc_z)
6093179193Sjb				pc = DIF_INSTR_LABEL(instr);
6094179193Sjb			break;
6095179193Sjb		case DIF_OP_BNE:
6096179193Sjb			if (cc_z == 0)
6097179193Sjb				pc = DIF_INSTR_LABEL(instr);
6098179193Sjb			break;
6099179193Sjb		case DIF_OP_BG:
6100179193Sjb			if ((cc_z | (cc_n ^ cc_v)) == 0)
6101179193Sjb				pc = DIF_INSTR_LABEL(instr);
6102179193Sjb			break;
6103179193Sjb		case DIF_OP_BGU:
6104179193Sjb			if ((cc_c | cc_z) == 0)
6105179193Sjb				pc = DIF_INSTR_LABEL(instr);
6106179193Sjb			break;
6107179193Sjb		case DIF_OP_BGE:
6108179193Sjb			if ((cc_n ^ cc_v) == 0)
6109179193Sjb				pc = DIF_INSTR_LABEL(instr);
6110179193Sjb			break;
6111179193Sjb		case DIF_OP_BGEU:
6112179193Sjb			if (cc_c == 0)
6113179193Sjb				pc = DIF_INSTR_LABEL(instr);
6114179193Sjb			break;
6115179193Sjb		case DIF_OP_BL:
6116179193Sjb			if (cc_n ^ cc_v)
6117179193Sjb				pc = DIF_INSTR_LABEL(instr);
6118179193Sjb			break;
6119179193Sjb		case DIF_OP_BLU:
6120179193Sjb			if (cc_c)
6121179193Sjb				pc = DIF_INSTR_LABEL(instr);
6122179193Sjb			break;
6123179193Sjb		case DIF_OP_BLE:
6124179193Sjb			if (cc_z | (cc_n ^ cc_v))
6125179193Sjb				pc = DIF_INSTR_LABEL(instr);
6126179193Sjb			break;
6127179193Sjb		case DIF_OP_BLEU:
6128179193Sjb			if (cc_c | cc_z)
6129179193Sjb				pc = DIF_INSTR_LABEL(instr);
6130179193Sjb			break;
6131179193Sjb		case DIF_OP_RLDSB:
6132267929Srpaulo			if (!dtrace_canload(regs[r1], 1, mstate, vstate))
6133179193Sjb				break;
6134179193Sjb			/*FALLTHROUGH*/
6135179193Sjb		case DIF_OP_LDSB:
6136179193Sjb			regs[rd] = (int8_t)dtrace_load8(regs[r1]);
6137179193Sjb			break;
6138179193Sjb		case DIF_OP_RLDSH:
6139267929Srpaulo			if (!dtrace_canload(regs[r1], 2, mstate, vstate))
6140179193Sjb				break;
6141179193Sjb			/*FALLTHROUGH*/
6142179193Sjb		case DIF_OP_LDSH:
6143179193Sjb			regs[rd] = (int16_t)dtrace_load16(regs[r1]);
6144179193Sjb			break;
6145179193Sjb		case DIF_OP_RLDSW:
6146267929Srpaulo			if (!dtrace_canload(regs[r1], 4, mstate, vstate))
6147179193Sjb				break;
6148179193Sjb			/*FALLTHROUGH*/
6149179193Sjb		case DIF_OP_LDSW:
6150179193Sjb			regs[rd] = (int32_t)dtrace_load32(regs[r1]);
6151179193Sjb			break;
6152179193Sjb		case DIF_OP_RLDUB:
6153267929Srpaulo			if (!dtrace_canload(regs[r1], 1, mstate, vstate))
6154179193Sjb				break;
6155179193Sjb			/*FALLTHROUGH*/
6156179193Sjb		case DIF_OP_LDUB:
6157179193Sjb			regs[rd] = dtrace_load8(regs[r1]);
6158179193Sjb			break;
6159179193Sjb		case DIF_OP_RLDUH:
6160267929Srpaulo			if (!dtrace_canload(regs[r1], 2, mstate, vstate))
6161179193Sjb				break;
6162179193Sjb			/*FALLTHROUGH*/
6163179193Sjb		case DIF_OP_LDUH:
6164179193Sjb			regs[rd] = dtrace_load16(regs[r1]);
6165179193Sjb			break;
6166179193Sjb		case DIF_OP_RLDUW:
6167267929Srpaulo			if (!dtrace_canload(regs[r1], 4, mstate, vstate))
6168179193Sjb				break;
6169179193Sjb			/*FALLTHROUGH*/
6170179193Sjb		case DIF_OP_LDUW:
6171179193Sjb			regs[rd] = dtrace_load32(regs[r1]);
6172179193Sjb			break;
6173179193Sjb		case DIF_OP_RLDX:
6174267929Srpaulo			if (!dtrace_canload(regs[r1], 8, mstate, vstate))
6175179193Sjb				break;
6176179193Sjb			/*FALLTHROUGH*/
6177179193Sjb		case DIF_OP_LDX:
6178179193Sjb			regs[rd] = dtrace_load64(regs[r1]);
6179179193Sjb			break;
6180179193Sjb		case DIF_OP_ULDSB:
6181267941Srpaulo			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6182179193Sjb			regs[rd] = (int8_t)
6183179193Sjb			    dtrace_fuword8((void *)(uintptr_t)regs[r1]);
6184267941Srpaulo			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6185179193Sjb			break;
6186179193Sjb		case DIF_OP_ULDSH:
6187267941Srpaulo			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6188179193Sjb			regs[rd] = (int16_t)
6189179193Sjb			    dtrace_fuword16((void *)(uintptr_t)regs[r1]);
6190267941Srpaulo			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6191179193Sjb			break;
6192179193Sjb		case DIF_OP_ULDSW:
6193267941Srpaulo			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6194179193Sjb			regs[rd] = (int32_t)
6195179193Sjb			    dtrace_fuword32((void *)(uintptr_t)regs[r1]);
6196267941Srpaulo			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6197179193Sjb			break;
6198179193Sjb		case DIF_OP_ULDUB:
6199267941Srpaulo			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6200179193Sjb			regs[rd] =
6201179193Sjb			    dtrace_fuword8((void *)(uintptr_t)regs[r1]);
6202267941Srpaulo			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6203179193Sjb			break;
6204179193Sjb		case DIF_OP_ULDUH:
6205267941Srpaulo			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6206179193Sjb			regs[rd] =
6207179193Sjb			    dtrace_fuword16((void *)(uintptr_t)regs[r1]);
6208267941Srpaulo			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6209179193Sjb			break;
6210179193Sjb		case DIF_OP_ULDUW:
6211267941Srpaulo			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6212179193Sjb			regs[rd] =
6213179193Sjb			    dtrace_fuword32((void *)(uintptr_t)regs[r1]);
6214267941Srpaulo			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6215179193Sjb			break;
6216179193Sjb		case DIF_OP_ULDX:
6217267941Srpaulo			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6218179193Sjb			regs[rd] =
6219179193Sjb			    dtrace_fuword64((void *)(uintptr_t)regs[r1]);
6220267941Srpaulo			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6221179193Sjb			break;
6222179193Sjb		case DIF_OP_RET:
6223179193Sjb			rval = regs[rd];
6224179193Sjb			pc = textlen;
6225179193Sjb			break;
6226179193Sjb		case DIF_OP_NOP:
6227179193Sjb			break;
6228179193Sjb		case DIF_OP_SETX:
6229179193Sjb			regs[rd] = inttab[DIF_INSTR_INTEGER(instr)];
6230179193Sjb			break;
6231179193Sjb		case DIF_OP_SETS:
6232179193Sjb			regs[rd] = (uint64_t)(uintptr_t)
6233179193Sjb			    (strtab + DIF_INSTR_STRING(instr));
6234179193Sjb			break;
6235179193Sjb		case DIF_OP_SCMP: {
6236179193Sjb			size_t sz = state->dts_options[DTRACEOPT_STRSIZE];
6237179193Sjb			uintptr_t s1 = regs[r1];
6238179193Sjb			uintptr_t s2 = regs[r2];
6239179193Sjb
6240179198Sjb			if (s1 != 0 &&
6241179193Sjb			    !dtrace_strcanload(s1, sz, mstate, vstate))
6242179193Sjb				break;
6243179198Sjb			if (s2 != 0 &&
6244179193Sjb			    !dtrace_strcanload(s2, sz, mstate, vstate))
6245179193Sjb				break;
6246179193Sjb
6247179193Sjb			cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz);
6248179193Sjb
6249179193Sjb			cc_n = cc_r < 0;
6250179193Sjb			cc_z = cc_r == 0;
6251179193Sjb			cc_v = cc_c = 0;
6252179193Sjb			break;
6253179193Sjb		}
6254179193Sjb		case DIF_OP_LDGA:
6255179193Sjb			regs[rd] = dtrace_dif_variable(mstate, state,
6256179193Sjb			    r1, regs[r2]);
6257179193Sjb			break;
6258179193Sjb		case DIF_OP_LDGS:
6259179193Sjb			id = DIF_INSTR_VAR(instr);
6260179193Sjb
6261179193Sjb			if (id >= DIF_VAR_OTHER_UBASE) {
6262179193Sjb				uintptr_t a;
6263179193Sjb
6264179193Sjb				id -= DIF_VAR_OTHER_UBASE;
6265179193Sjb				svar = vstate->dtvs_globals[id];
6266179193Sjb				ASSERT(svar != NULL);
6267179193Sjb				v = &svar->dtsv_var;
6268179193Sjb
6269179193Sjb				if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) {
6270179193Sjb					regs[rd] = svar->dtsv_data;
6271179193Sjb					break;
6272179193Sjb				}
6273179193Sjb
6274179193Sjb				a = (uintptr_t)svar->dtsv_data;
6275179193Sjb
6276179193Sjb				if (*(uint8_t *)a == UINT8_MAX) {
6277179193Sjb					/*
6278179193Sjb					 * If the 0th byte is set to UINT8_MAX
6279179193Sjb					 * then this is to be treated as a
6280179193Sjb					 * reference to a NULL variable.
6281179193Sjb					 */
6282179198Sjb					regs[rd] = 0;
6283179193Sjb				} else {
6284179193Sjb					regs[rd] = a + sizeof (uint64_t);
6285179193Sjb				}
6286179193Sjb
6287179193Sjb				break;
6288179193Sjb			}
6289179193Sjb
6290179193Sjb			regs[rd] = dtrace_dif_variable(mstate, state, id, 0);
6291179193Sjb			break;
6292179193Sjb
6293179193Sjb		case DIF_OP_STGS:
6294179193Sjb			id = DIF_INSTR_VAR(instr);
6295179193Sjb
6296179193Sjb			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6297179193Sjb			id -= DIF_VAR_OTHER_UBASE;
6298179193Sjb
6299296475Smarkj			VERIFY(id < vstate->dtvs_nglobals);
6300179193Sjb			svar = vstate->dtvs_globals[id];
6301179193Sjb			ASSERT(svar != NULL);
6302179193Sjb			v = &svar->dtsv_var;
6303179193Sjb
6304179193Sjb			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6305179193Sjb				uintptr_t a = (uintptr_t)svar->dtsv_data;
6306179193Sjb
6307179198Sjb				ASSERT(a != 0);
6308179193Sjb				ASSERT(svar->dtsv_size != 0);
6309179193Sjb
6310179198Sjb				if (regs[rd] == 0) {
6311179193Sjb					*(uint8_t *)a = UINT8_MAX;
6312179193Sjb					break;
6313179193Sjb				} else {
6314179193Sjb					*(uint8_t *)a = 0;
6315179193Sjb					a += sizeof (uint64_t);
6316179193Sjb				}
6317179193Sjb				if (!dtrace_vcanload(
6318179193Sjb				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
6319179193Sjb				    mstate, vstate))
6320179193Sjb					break;
6321179193Sjb
6322179193Sjb				dtrace_vcopy((void *)(uintptr_t)regs[rd],
6323179193Sjb				    (void *)a, &v->dtdv_type);
6324179193Sjb				break;
6325179193Sjb			}
6326179193Sjb
6327179193Sjb			svar->dtsv_data = regs[rd];
6328179193Sjb			break;
6329179193Sjb
6330179193Sjb		case DIF_OP_LDTA:
6331179193Sjb			/*
6332179193Sjb			 * There are no DTrace built-in thread-local arrays at
6333179193Sjb			 * present.  This opcode is saved for future work.
6334179193Sjb			 */
6335179193Sjb			*flags |= CPU_DTRACE_ILLOP;
6336179193Sjb			regs[rd] = 0;
6337179193Sjb			break;
6338179193Sjb
6339179193Sjb		case DIF_OP_LDLS:
6340179193Sjb			id = DIF_INSTR_VAR(instr);
6341179193Sjb
6342179193Sjb			if (id < DIF_VAR_OTHER_UBASE) {
6343179193Sjb				/*
6344179193Sjb				 * For now, this has no meaning.
6345179193Sjb				 */
6346179193Sjb				regs[rd] = 0;
6347179193Sjb				break;
6348179193Sjb			}
6349179193Sjb
6350179193Sjb			id -= DIF_VAR_OTHER_UBASE;
6351179193Sjb
6352179193Sjb			ASSERT(id < vstate->dtvs_nlocals);
6353179193Sjb			ASSERT(vstate->dtvs_locals != NULL);
6354179193Sjb
6355179193Sjb			svar = vstate->dtvs_locals[id];
6356179193Sjb			ASSERT(svar != NULL);
6357179193Sjb			v = &svar->dtsv_var;
6358179193Sjb
6359179193Sjb			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6360179193Sjb				uintptr_t a = (uintptr_t)svar->dtsv_data;
6361179193Sjb				size_t sz = v->dtdv_type.dtdt_size;
6362179193Sjb
6363179193Sjb				sz += sizeof (uint64_t);
6364179193Sjb				ASSERT(svar->dtsv_size == NCPU * sz);
6365179198Sjb				a += curcpu * sz;
6366179193Sjb
6367179193Sjb				if (*(uint8_t *)a == UINT8_MAX) {
6368179193Sjb					/*
6369179193Sjb					 * If the 0th byte is set to UINT8_MAX
6370179193Sjb					 * then this is to be treated as a
6371179193Sjb					 * reference to a NULL variable.
6372179193Sjb					 */
6373179198Sjb					regs[rd] = 0;
6374179193Sjb				} else {
6375179193Sjb					regs[rd] = a + sizeof (uint64_t);
6376179193Sjb				}
6377179193Sjb
6378179193Sjb				break;
6379179193Sjb			}
6380179193Sjb
6381179193Sjb			ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
6382179193Sjb			tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
6383179198Sjb			regs[rd] = tmp[curcpu];
6384179193Sjb			break;
6385179193Sjb
6386179193Sjb		case DIF_OP_STLS:
6387179193Sjb			id = DIF_INSTR_VAR(instr);
6388179193Sjb
6389179193Sjb			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6390179193Sjb			id -= DIF_VAR_OTHER_UBASE;
6391296475Smarkj			VERIFY(id < vstate->dtvs_nlocals);
6392179193Sjb
6393179193Sjb			ASSERT(vstate->dtvs_locals != NULL);
6394179193Sjb			svar = vstate->dtvs_locals[id];
6395179193Sjb			ASSERT(svar != NULL);
6396179193Sjb			v = &svar->dtsv_var;
6397179193Sjb
6398179193Sjb			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6399179193Sjb				uintptr_t a = (uintptr_t)svar->dtsv_data;
6400179193Sjb				size_t sz = v->dtdv_type.dtdt_size;
6401179193Sjb
6402179193Sjb				sz += sizeof (uint64_t);
6403179193Sjb				ASSERT(svar->dtsv_size == NCPU * sz);
6404179198Sjb				a += curcpu * sz;
6405179193Sjb
6406179198Sjb				if (regs[rd] == 0) {
6407179193Sjb					*(uint8_t *)a = UINT8_MAX;
6408179193Sjb					break;
6409179193Sjb				} else {
6410179193Sjb					*(uint8_t *)a = 0;
6411179193Sjb					a += sizeof (uint64_t);
6412179193Sjb				}
6413179193Sjb
6414179193Sjb				if (!dtrace_vcanload(
6415179193Sjb				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
6416179193Sjb				    mstate, vstate))
6417179193Sjb					break;
6418179193Sjb
6419179193Sjb				dtrace_vcopy((void *)(uintptr_t)regs[rd],
6420179193Sjb				    (void *)a, &v->dtdv_type);
6421179193Sjb				break;
6422179193Sjb			}
6423179193Sjb
6424179193Sjb			ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
6425179193Sjb			tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
6426179198Sjb			tmp[curcpu] = regs[rd];
6427179193Sjb			break;
6428179193Sjb
6429179193Sjb		case DIF_OP_LDTS: {
6430179193Sjb			dtrace_dynvar_t *dvar;
6431179193Sjb			dtrace_key_t *key;
6432179193Sjb
6433179193Sjb			id = DIF_INSTR_VAR(instr);
6434179193Sjb			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6435179193Sjb			id -= DIF_VAR_OTHER_UBASE;
6436179193Sjb			v = &vstate->dtvs_tlocals[id];
6437179193Sjb
6438179193Sjb			key = &tupregs[DIF_DTR_NREGS];
6439179193Sjb			key[0].dttk_value = (uint64_t)id;
6440179193Sjb			key[0].dttk_size = 0;
6441179193Sjb			DTRACE_TLS_THRKEY(key[1].dttk_value);
6442179193Sjb			key[1].dttk_size = 0;
6443179193Sjb
6444179193Sjb			dvar = dtrace_dynvar(dstate, 2, key,
6445179193Sjb			    sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC,
6446179193Sjb			    mstate, vstate);
6447179193Sjb
6448179193Sjb			if (dvar == NULL) {
6449179193Sjb				regs[rd] = 0;
6450179193Sjb				break;
6451179193Sjb			}
6452179193Sjb
6453179193Sjb			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6454179193Sjb				regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
6455179193Sjb			} else {
6456179193Sjb				regs[rd] = *((uint64_t *)dvar->dtdv_data);
6457179193Sjb			}
6458179193Sjb
6459179193Sjb			break;
6460179193Sjb		}
6461179193Sjb
6462179193Sjb		case DIF_OP_STTS: {
6463179193Sjb			dtrace_dynvar_t *dvar;
6464179193Sjb			dtrace_key_t *key;
6465179193Sjb
6466179193Sjb			id = DIF_INSTR_VAR(instr);
6467179193Sjb			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6468179193Sjb			id -= DIF_VAR_OTHER_UBASE;
6469296475Smarkj			VERIFY(id < vstate->dtvs_ntlocals);
6470179193Sjb
6471179193Sjb			key = &tupregs[DIF_DTR_NREGS];
6472179193Sjb			key[0].dttk_value = (uint64_t)id;
6473179193Sjb			key[0].dttk_size = 0;
6474179193Sjb			DTRACE_TLS_THRKEY(key[1].dttk_value);
6475179193Sjb			key[1].dttk_size = 0;
6476179193Sjb			v = &vstate->dtvs_tlocals[id];
6477179193Sjb
6478179193Sjb			dvar = dtrace_dynvar(dstate, 2, key,
6479179193Sjb			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6480179193Sjb			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
6481179193Sjb			    regs[rd] ? DTRACE_DYNVAR_ALLOC :
6482179193Sjb			    DTRACE_DYNVAR_DEALLOC, mstate, vstate);
6483179193Sjb
6484179193Sjb			/*
6485179193Sjb			 * Given that we're storing to thread-local data,
6486179193Sjb			 * we need to flush our predicate cache.
6487179193Sjb			 */
6488179198Sjb			curthread->t_predcache = 0;
6489179193Sjb
6490179193Sjb			if (dvar == NULL)
6491179193Sjb				break;
6492179193Sjb
6493179193Sjb			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6494179193Sjb				if (!dtrace_vcanload(
6495179193Sjb				    (void *)(uintptr_t)regs[rd],
6496179193Sjb				    &v->dtdv_type, mstate, vstate))
6497179193Sjb					break;
6498179193Sjb
6499179193Sjb				dtrace_vcopy((void *)(uintptr_t)regs[rd],
6500179193Sjb				    dvar->dtdv_data, &v->dtdv_type);
6501179193Sjb			} else {
6502179193Sjb				*((uint64_t *)dvar->dtdv_data) = regs[rd];
6503179193Sjb			}
6504179193Sjb
6505179193Sjb			break;
6506179193Sjb		}
6507179193Sjb
6508179193Sjb		case DIF_OP_SRA:
6509179193Sjb			regs[rd] = (int64_t)regs[r1] >> regs[r2];
6510179193Sjb			break;
6511179193Sjb
6512179193Sjb		case DIF_OP_CALL:
6513179193Sjb			dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd,
6514179193Sjb			    regs, tupregs, ttop, mstate, state);
6515179193Sjb			break;
6516179193Sjb
6517179193Sjb		case DIF_OP_PUSHTR:
6518179193Sjb			if (ttop == DIF_DTR_NREGS) {
6519179193Sjb				*flags |= CPU_DTRACE_TUPOFLOW;
6520179193Sjb				break;
6521179193Sjb			}
6522179193Sjb
6523179193Sjb			if (r1 == DIF_TYPE_STRING) {
6524179193Sjb				/*
6525179193Sjb				 * If this is a string type and the size is 0,
6526179193Sjb				 * we'll use the system-wide default string
6527179193Sjb				 * size.  Note that we are _not_ looking at
6528179193Sjb				 * the value of the DTRACEOPT_STRSIZE option;
6529179193Sjb				 * had this been set, we would expect to have
6530179193Sjb				 * a non-zero size value in the "pushtr".
6531179193Sjb				 */
6532179193Sjb				tupregs[ttop].dttk_size =
6533179193Sjb				    dtrace_strlen((char *)(uintptr_t)regs[rd],
6534179193Sjb				    regs[r2] ? regs[r2] :
6535179193Sjb				    dtrace_strsize_default) + 1;
6536179193Sjb			} else {
6537288415Smarkj				if (regs[r2] > LONG_MAX) {
6538288415Smarkj					*flags |= CPU_DTRACE_ILLOP;
6539288415Smarkj					break;
6540288415Smarkj				}
6541288415Smarkj
6542179193Sjb				tupregs[ttop].dttk_size = regs[r2];
6543179193Sjb			}
6544179193Sjb
6545179193Sjb			tupregs[ttop++].dttk_value = regs[rd];
6546179193Sjb			break;
6547179193Sjb
6548179193Sjb		case DIF_OP_PUSHTV:
6549179193Sjb			if (ttop == DIF_DTR_NREGS) {
6550179193Sjb				*flags |= CPU_DTRACE_TUPOFLOW;
6551179193Sjb				break;
6552179193Sjb			}
6553179193Sjb
6554179193Sjb			tupregs[ttop].dttk_value = regs[rd];
6555179193Sjb			tupregs[ttop++].dttk_size = 0;
6556179193Sjb			break;
6557179193Sjb
6558179193Sjb		case DIF_OP_POPTS:
6559179193Sjb			if (ttop != 0)
6560179193Sjb				ttop--;
6561179193Sjb			break;
6562179193Sjb
6563179193Sjb		case DIF_OP_FLUSHTS:
6564179193Sjb			ttop = 0;
6565179193Sjb			break;
6566179193Sjb
6567179193Sjb		case DIF_OP_LDGAA:
6568179193Sjb		case DIF_OP_LDTAA: {
6569179193Sjb			dtrace_dynvar_t *dvar;
6570179193Sjb			dtrace_key_t *key = tupregs;
6571179193Sjb			uint_t nkeys = ttop;
6572179193Sjb
6573179193Sjb			id = DIF_INSTR_VAR(instr);
6574179193Sjb			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6575179193Sjb			id -= DIF_VAR_OTHER_UBASE;
6576179193Sjb
6577179193Sjb			key[nkeys].dttk_value = (uint64_t)id;
6578179193Sjb			key[nkeys++].dttk_size = 0;
6579179193Sjb
6580179193Sjb			if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) {
6581179193Sjb				DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
6582179193Sjb				key[nkeys++].dttk_size = 0;
6583296475Smarkj				VERIFY(id < vstate->dtvs_ntlocals);
6584179193Sjb				v = &vstate->dtvs_tlocals[id];
6585179193Sjb			} else {
6586296475Smarkj				VERIFY(id < vstate->dtvs_nglobals);
6587179193Sjb				v = &vstate->dtvs_globals[id]->dtsv_var;
6588179193Sjb			}
6589179193Sjb
6590179193Sjb			dvar = dtrace_dynvar(dstate, nkeys, key,
6591179193Sjb			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6592179193Sjb			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
6593179193Sjb			    DTRACE_DYNVAR_NOALLOC, mstate, vstate);
6594179193Sjb
6595179193Sjb			if (dvar == NULL) {
6596179193Sjb				regs[rd] = 0;
6597179193Sjb				break;
6598179193Sjb			}
6599179193Sjb
6600179193Sjb			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6601179193Sjb				regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
6602179193Sjb			} else {
6603179193Sjb				regs[rd] = *((uint64_t *)dvar->dtdv_data);
6604179193Sjb			}
6605179193Sjb
6606179193Sjb			break;
6607179193Sjb		}
6608179193Sjb
6609179193Sjb		case DIF_OP_STGAA:
6610179193Sjb		case DIF_OP_STTAA: {
6611179193Sjb			dtrace_dynvar_t *dvar;
6612179193Sjb			dtrace_key_t *key = tupregs;
6613179193Sjb			uint_t nkeys = ttop;
6614179193Sjb
6615179193Sjb			id = DIF_INSTR_VAR(instr);
6616179193Sjb			ASSERT(id >= DIF_VAR_OTHER_UBASE);
6617179193Sjb			id -= DIF_VAR_OTHER_UBASE;
6618179193Sjb
6619179193Sjb			key[nkeys].dttk_value = (uint64_t)id;
6620179193Sjb			key[nkeys++].dttk_size = 0;
6621179193Sjb
6622179193Sjb			if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) {
6623179193Sjb				DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
6624179193Sjb				key[nkeys++].dttk_size = 0;
6625296475Smarkj				VERIFY(id < vstate->dtvs_ntlocals);
6626179193Sjb				v = &vstate->dtvs_tlocals[id];
6627179193Sjb			} else {
6628296475Smarkj				VERIFY(id < vstate->dtvs_nglobals);
6629179193Sjb				v = &vstate->dtvs_globals[id]->dtsv_var;
6630179193Sjb			}
6631179193Sjb
6632179193Sjb			dvar = dtrace_dynvar(dstate, nkeys, key,
6633179193Sjb			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
6634179193Sjb			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
6635179193Sjb			    regs[rd] ? DTRACE_DYNVAR_ALLOC :
6636179193Sjb			    DTRACE_DYNVAR_DEALLOC, mstate, vstate);
6637179193Sjb
6638179193Sjb			if (dvar == NULL)
6639179193Sjb				break;
6640179193Sjb
6641179193Sjb			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
6642179193Sjb				if (!dtrace_vcanload(
6643179193Sjb				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
6644179193Sjb				    mstate, vstate))
6645179193Sjb					break;
6646179193Sjb
6647179193Sjb				dtrace_vcopy((void *)(uintptr_t)regs[rd],
6648179193Sjb				    dvar->dtdv_data, &v->dtdv_type);
6649179193Sjb			} else {
6650179193Sjb				*((uint64_t *)dvar->dtdv_data) = regs[rd];
6651179193Sjb			}
6652179193Sjb
6653179193Sjb			break;
6654179193Sjb		}
6655179193Sjb
6656179193Sjb		case DIF_OP_ALLOCS: {
6657179193Sjb			uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
6658179193Sjb			size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1];
6659179193Sjb
6660179193Sjb			/*
6661179193Sjb			 * Rounding up the user allocation size could have
6662179193Sjb			 * overflowed large, bogus allocations (like -1ULL) to
6663179193Sjb			 * 0.
6664179193Sjb			 */
6665179193Sjb			if (size < regs[r1] ||
6666179193Sjb			    !DTRACE_INSCRATCH(mstate, size)) {
6667179193Sjb				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
6668179198Sjb				regs[rd] = 0;
6669179193Sjb				break;
6670179193Sjb			}
6671179193Sjb
6672179193Sjb			dtrace_bzero((void *) mstate->dtms_scratch_ptr, size);
6673179193Sjb			mstate->dtms_scratch_ptr += size;
6674179193Sjb			regs[rd] = ptr;
6675179193Sjb			break;
6676179193Sjb		}
6677179193Sjb
6678179193Sjb		case DIF_OP_COPYS:
6679179193Sjb			if (!dtrace_canstore(regs[rd], regs[r2],
6680179193Sjb			    mstate, vstate)) {
6681179193Sjb				*flags |= CPU_DTRACE_BADADDR;
6682179193Sjb				*illval = regs[rd];
6683179193Sjb				break;
6684179193Sjb			}
6685179193Sjb
6686179193Sjb			if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate))
6687179193Sjb				break;
6688179193Sjb
6689179193Sjb			dtrace_bcopy((void *)(uintptr_t)regs[r1],
6690179193Sjb			    (void *)(uintptr_t)regs[rd], (size_t)regs[r2]);
6691179193Sjb			break;
6692179193Sjb
6693179193Sjb		case DIF_OP_STB:
6694179193Sjb			if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) {
6695179193Sjb				*flags |= CPU_DTRACE_BADADDR;
6696179193Sjb				*illval = regs[rd];
6697179193Sjb				break;
6698179193Sjb			}
6699179193Sjb			*((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1];
6700179193Sjb			break;
6701179193Sjb
6702179193Sjb		case DIF_OP_STH:
6703179193Sjb			if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) {
6704179193Sjb				*flags |= CPU_DTRACE_BADADDR;
6705179193Sjb				*illval = regs[rd];
6706179193Sjb				break;
6707179193Sjb			}
6708179193Sjb			if (regs[rd] & 1) {
6709179193Sjb				*flags |= CPU_DTRACE_BADALIGN;
6710179193Sjb				*illval = regs[rd];
6711179193Sjb				break;
6712179193Sjb			}
6713179193Sjb			*((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1];
6714179193Sjb			break;
6715179193Sjb
6716179193Sjb		case DIF_OP_STW:
6717179193Sjb			if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) {
6718179193Sjb				*flags |= CPU_DTRACE_BADADDR;
6719179193Sjb				*illval = regs[rd];
6720179193Sjb				break;
6721179193Sjb			}
6722179193Sjb			if (regs[rd] & 3) {
6723179193Sjb				*flags |= CPU_DTRACE_BADALIGN;
6724179193Sjb				*illval = regs[rd];
6725179193Sjb				break;
6726179193Sjb			}
6727179193Sjb			*((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1];
6728179193Sjb			break;
6729179193Sjb
6730179193Sjb		case DIF_OP_STX:
6731179193Sjb			if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) {
6732179193Sjb				*flags |= CPU_DTRACE_BADADDR;
6733179193Sjb				*illval = regs[rd];
6734179193Sjb				break;
6735179193Sjb			}
6736179193Sjb			if (regs[rd] & 7) {
6737179193Sjb				*flags |= CPU_DTRACE_BADALIGN;
6738179193Sjb				*illval = regs[rd];
6739179193Sjb				break;
6740179193Sjb			}
6741179193Sjb			*((uint64_t *)(uintptr_t)regs[rd]) = regs[r1];
6742179193Sjb			break;
6743179193Sjb		}
6744179193Sjb	}
6745179193Sjb
6746179193Sjb	if (!(*flags & CPU_DTRACE_FAULT))
6747179193Sjb		return (rval);
6748179193Sjb
6749179193Sjb	mstate->dtms_fltoffs = opc * sizeof (dif_instr_t);
6750179193Sjb	mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS;
6751179193Sjb
6752179193Sjb	return (0);
6753179193Sjb}
6754179193Sjb
6755179193Sjbstatic void
6756179193Sjbdtrace_action_breakpoint(dtrace_ecb_t *ecb)
6757179193Sjb{
6758179193Sjb	dtrace_probe_t *probe = ecb->dte_probe;
6759179193Sjb	dtrace_provider_t *prov = probe->dtpr_provider;
6760179193Sjb	char c[DTRACE_FULLNAMELEN + 80], *str;
6761179193Sjb	char *msg = "dtrace: breakpoint action at probe ";
6762179193Sjb	char *ecbmsg = " (ecb ";
6763179193Sjb	uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4));
6764179193Sjb	uintptr_t val = (uintptr_t)ecb;
6765179193Sjb	int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0;
6766179193Sjb
6767179193Sjb	if (dtrace_destructive_disallow)
6768179193Sjb		return;
6769179193Sjb
6770179193Sjb	/*
6771179193Sjb	 * It's impossible to be taking action on the NULL probe.
6772179193Sjb	 */
6773179193Sjb	ASSERT(probe != NULL);
6774179193Sjb
6775179193Sjb	/*
6776179193Sjb	 * This is a poor man's (destitute man's?) sprintf():  we want to
6777179193Sjb	 * print the provider name, module name, function name and name of
6778179193Sjb	 * the probe, along with the hex address of the ECB with the breakpoint
6779179193Sjb	 * action -- all of which we must place in the character buffer by
6780179193Sjb	 * hand.
6781179193Sjb	 */
6782179193Sjb	while (*msg != '\0')
6783179193Sjb		c[i++] = *msg++;
6784179193Sjb
6785179193Sjb	for (str = prov->dtpv_name; *str != '\0'; str++)
6786179193Sjb		c[i++] = *str;
6787179193Sjb	c[i++] = ':';
6788179193Sjb
6789179193Sjb	for (str = probe->dtpr_mod; *str != '\0'; str++)
6790179193Sjb		c[i++] = *str;
6791179193Sjb	c[i++] = ':';
6792179193Sjb
6793179193Sjb	for (str = probe->dtpr_func; *str != '\0'; str++)
6794179193Sjb		c[i++] = *str;
6795179193Sjb	c[i++] = ':';
6796179193Sjb
6797179193Sjb	for (str = probe->dtpr_name; *str != '\0'; str++)
6798179193Sjb		c[i++] = *str;
6799179193Sjb
6800179193Sjb	while (*ecbmsg != '\0')
6801179193Sjb		c[i++] = *ecbmsg++;
6802179193Sjb
6803179193Sjb	while (shift >= 0) {
6804179193Sjb		mask = (uintptr_t)0xf << shift;
6805179193Sjb
6806179193Sjb		if (val >= ((uintptr_t)1 << shift))
6807179193Sjb			c[i++] = "0123456789abcdef"[(val & mask) >> shift];
6808179193Sjb		shift -= 4;
6809179193Sjb	}
6810179193Sjb
6811179193Sjb	c[i++] = ')';
6812179193Sjb	c[i] = '\0';
6813179193Sjb
6814277300Ssmh#ifdef illumos
6815179193Sjb	debug_enter(c);
6816179198Sjb#else
6817179198Sjb	kdb_enter(KDB_WHY_DTRACE, "breakpoint action");
6818179198Sjb#endif
6819179193Sjb}
6820179193Sjb
6821179193Sjbstatic void
6822179193Sjbdtrace_action_panic(dtrace_ecb_t *ecb)
6823179193Sjb{
6824179193Sjb	dtrace_probe_t *probe = ecb->dte_probe;
6825179193Sjb
6826179193Sjb	/*
6827179193Sjb	 * It's impossible to be taking action on the NULL probe.
6828179193Sjb	 */
6829179193Sjb	ASSERT(probe != NULL);
6830179193Sjb
6831179193Sjb	if (dtrace_destructive_disallow)
6832179193Sjb		return;
6833179193Sjb
6834179193Sjb	if (dtrace_panicked != NULL)
6835179193Sjb		return;
6836179193Sjb
6837179193Sjb	if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL)
6838179193Sjb		return;
6839179193Sjb
6840179193Sjb	/*
6841179193Sjb	 * We won the right to panic.  (We want to be sure that only one
6842179193Sjb	 * thread calls panic() from dtrace_probe(), and that panic() is
6843179193Sjb	 * called exactly once.)
6844179193Sjb	 */
6845179193Sjb	dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)",
6846179193Sjb	    probe->dtpr_provider->dtpv_name, probe->dtpr_mod,
6847179193Sjb	    probe->dtpr_func, probe->dtpr_name, (void *)ecb);
6848179193Sjb}
6849179193Sjb
6850179193Sjbstatic void
6851179193Sjbdtrace_action_raise(uint64_t sig)
6852179193Sjb{
6853179193Sjb	if (dtrace_destructive_disallow)
6854179193Sjb		return;
6855179193Sjb
6856179193Sjb	if (sig >= NSIG) {
6857179193Sjb		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6858179193Sjb		return;
6859179193Sjb	}
6860179193Sjb
6861277300Ssmh#ifdef illumos
6862179193Sjb	/*
6863179193Sjb	 * raise() has a queue depth of 1 -- we ignore all subsequent
6864179193Sjb	 * invocations of the raise() action.
6865179193Sjb	 */
6866179193Sjb	if (curthread->t_dtrace_sig == 0)
6867179193Sjb		curthread->t_dtrace_sig = (uint8_t)sig;
6868179193Sjb
6869179193Sjb	curthread->t_sig_check = 1;
6870179193Sjb	aston(curthread);
6871179198Sjb#else
6872179198Sjb	struct proc *p = curproc;
6873179198Sjb	PROC_LOCK(p);
6874225617Skmacy	kern_psignal(p, sig);
6875179198Sjb	PROC_UNLOCK(p);
6876179198Sjb#endif
6877179193Sjb}
6878179193Sjb
6879179193Sjbstatic void
6880179193Sjbdtrace_action_stop(void)
6881179193Sjb{
6882179193Sjb	if (dtrace_destructive_disallow)
6883179193Sjb		return;
6884179193Sjb
6885277300Ssmh#ifdef illumos
6886179193Sjb	if (!curthread->t_dtrace_stop) {
6887179193Sjb		curthread->t_dtrace_stop = 1;
6888179193Sjb		curthread->t_sig_check = 1;
6889179193Sjb		aston(curthread);
6890179193Sjb	}
6891179198Sjb#else
6892179198Sjb	struct proc *p = curproc;
6893179198Sjb	PROC_LOCK(p);
6894225617Skmacy	kern_psignal(p, SIGSTOP);
6895179198Sjb	PROC_UNLOCK(p);
6896179198Sjb#endif
6897179193Sjb}
6898179193Sjb
6899179193Sjbstatic void
6900179193Sjbdtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val)
6901179193Sjb{
6902179193Sjb	hrtime_t now;
6903179193Sjb	volatile uint16_t *flags;
6904277300Ssmh#ifdef illumos
6905179193Sjb	cpu_t *cpu = CPU;
6906179198Sjb#else
6907179198Sjb	cpu_t *cpu = &solaris_cpu[curcpu];
6908179198Sjb#endif
6909179193Sjb
6910179193Sjb	if (dtrace_destructive_disallow)
6911179193Sjb		return;
6912179193Sjb
6913267941Srpaulo	flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
6914179193Sjb
6915179193Sjb	now = dtrace_gethrtime();
6916179193Sjb
6917179193Sjb	if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) {
6918179193Sjb		/*
6919179193Sjb		 * We need to advance the mark to the current time.
6920179193Sjb		 */
6921179193Sjb		cpu->cpu_dtrace_chillmark = now;
6922179193Sjb		cpu->cpu_dtrace_chilled = 0;
6923179193Sjb	}
6924179193Sjb
6925179193Sjb	/*
6926179193Sjb	 * Now check to see if the requested chill time would take us over
6927179193Sjb	 * the maximum amount of time allowed in the chill interval.  (Or
6928179193Sjb	 * worse, if the calculation itself induces overflow.)
6929179193Sjb	 */
6930179193Sjb	if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max ||
6931179193Sjb	    cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) {
6932179193Sjb		*flags |= CPU_DTRACE_ILLOP;
6933179193Sjb		return;
6934179193Sjb	}
6935179193Sjb
6936179193Sjb	while (dtrace_gethrtime() - now < val)
6937179193Sjb		continue;
6938179193Sjb
6939179193Sjb	/*
6940179193Sjb	 * Normally, we assure that the value of the variable "timestamp" does
6941179193Sjb	 * not change within an ECB.  The presence of chill() represents an
6942179193Sjb	 * exception to this rule, however.
6943179193Sjb	 */
6944179193Sjb	mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP;
6945179193Sjb	cpu->cpu_dtrace_chilled += val;
6946179193Sjb}
6947179193Sjb
6948179193Sjbstatic void
6949179193Sjbdtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state,
6950179193Sjb    uint64_t *buf, uint64_t arg)
6951179193Sjb{
6952179193Sjb	int nframes = DTRACE_USTACK_NFRAMES(arg);
6953179193Sjb	int strsize = DTRACE_USTACK_STRSIZE(arg);
6954179193Sjb	uint64_t *pcs = &buf[1], *fps;
6955179193Sjb	char *str = (char *)&pcs[nframes];
6956179193Sjb	int size, offs = 0, i, j;
6957179193Sjb	uintptr_t old = mstate->dtms_scratch_ptr, saved;
6958179198Sjb	uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
6959179193Sjb	char *sym;
6960179193Sjb
6961179193Sjb	/*
6962179193Sjb	 * Should be taking a faster path if string space has not been
6963179193Sjb	 * allocated.
6964179193Sjb	 */
6965179193Sjb	ASSERT(strsize != 0);
6966179193Sjb
6967179193Sjb	/*
6968179193Sjb	 * We will first allocate some temporary space for the frame pointers.
6969179193Sjb	 */
6970179193Sjb	fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
6971179193Sjb	size = (uintptr_t)fps - mstate->dtms_scratch_ptr +
6972179193Sjb	    (nframes * sizeof (uint64_t));
6973179193Sjb
6974179193Sjb	if (!DTRACE_INSCRATCH(mstate, size)) {
6975179193Sjb		/*
6976179193Sjb		 * Not enough room for our frame pointers -- need to indicate
6977179193Sjb		 * that we ran out of scratch space.
6978179193Sjb		 */
6979179193Sjb		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
6980179193Sjb		return;
6981179193Sjb	}
6982179193Sjb
6983179193Sjb	mstate->dtms_scratch_ptr += size;
6984179193Sjb	saved = mstate->dtms_scratch_ptr;
6985179193Sjb
6986179193Sjb	/*
6987179193Sjb	 * Now get a stack with both program counters and frame pointers.
6988179193Sjb	 */
6989179193Sjb	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6990179193Sjb	dtrace_getufpstack(buf, fps, nframes + 1);
6991179193Sjb	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6992179193Sjb
6993179193Sjb	/*
6994179193Sjb	 * If that faulted, we're cooked.
6995179193Sjb	 */
6996179193Sjb	if (*flags & CPU_DTRACE_FAULT)
6997179193Sjb		goto out;
6998179193Sjb
6999179193Sjb	/*
7000179193Sjb	 * Now we want to walk up the stack, calling the USTACK helper.  For
7001179193Sjb	 * each iteration, we restore the scratch pointer.
7002179193Sjb	 */
7003179193Sjb	for (i = 0; i < nframes; i++) {
7004179193Sjb		mstate->dtms_scratch_ptr = saved;
7005179193Sjb
7006179193Sjb		if (offs >= strsize)
7007179193Sjb			break;
7008179193Sjb
7009179193Sjb		sym = (char *)(uintptr_t)dtrace_helper(
7010179193Sjb		    DTRACE_HELPER_ACTION_USTACK,
7011179193Sjb		    mstate, state, pcs[i], fps[i]);
7012179193Sjb
7013179193Sjb		/*
7014179193Sjb		 * If we faulted while running the helper, we're going to
7015179193Sjb		 * clear the fault and null out the corresponding string.
7016179193Sjb		 */
7017179193Sjb		if (*flags & CPU_DTRACE_FAULT) {
7018179193Sjb			*flags &= ~CPU_DTRACE_FAULT;
7019179193Sjb			str[offs++] = '\0';
7020179193Sjb			continue;
7021179193Sjb		}
7022179193Sjb
7023179193Sjb		if (sym == NULL) {
7024179193Sjb			str[offs++] = '\0';
7025179193Sjb			continue;
7026179193Sjb		}
7027179193Sjb
7028179193Sjb		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7029179193Sjb
7030179193Sjb		/*
7031179193Sjb		 * Now copy in the string that the helper returned to us.
7032179193Sjb		 */
7033179193Sjb		for (j = 0; offs + j < strsize; j++) {
7034179193Sjb			if ((str[offs + j] = sym[j]) == '\0')
7035179193Sjb				break;
7036179193Sjb		}
7037179193Sjb
7038179193Sjb		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7039179193Sjb
7040179193Sjb		offs += j + 1;
7041179193Sjb	}
7042179193Sjb
7043179193Sjb	if (offs >= strsize) {
7044179193Sjb		/*
7045179193Sjb		 * If we didn't have room for all of the strings, we don't
7046179193Sjb		 * abort processing -- this needn't be a fatal error -- but we
7047179193Sjb		 * still want to increment a counter (dts_stkstroverflows) to
7048179193Sjb		 * allow this condition to be warned about.  (If this is from
7049179193Sjb		 * a jstack() action, it is easily tuned via jstackstrsize.)
7050179193Sjb		 */
7051179193Sjb		dtrace_error(&state->dts_stkstroverflows);
7052179193Sjb	}
7053179193Sjb
7054179193Sjb	while (offs < strsize)
7055179193Sjb		str[offs++] = '\0';
7056179193Sjb
7057179193Sjbout:
7058179193Sjb	mstate->dtms_scratch_ptr = old;
7059179193Sjb}
7060179193Sjb
7061267941Srpaulostatic void
7062267941Srpaulodtrace_store_by_ref(dtrace_difo_t *dp, caddr_t tomax, size_t size,
7063267941Srpaulo    size_t *valoffsp, uint64_t *valp, uint64_t end, int intuple, int dtkind)
7064267941Srpaulo{
7065267941Srpaulo	volatile uint16_t *flags;
7066267941Srpaulo	uint64_t val = *valp;
7067267941Srpaulo	size_t valoffs = *valoffsp;
7068267941Srpaulo
7069267941Srpaulo	flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
7070267941Srpaulo	ASSERT(dtkind == DIF_TF_BYREF || dtkind == DIF_TF_BYUREF);
7071267941Srpaulo
7072267941Srpaulo	/*
7073267941Srpaulo	 * If this is a string, we're going to only load until we find the zero
7074267941Srpaulo	 * byte -- after which we'll store zero bytes.
7075267941Srpaulo	 */
7076267941Srpaulo	if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
7077267941Srpaulo		char c = '\0' + 1;
7078267941Srpaulo		size_t s;
7079267941Srpaulo
7080267941Srpaulo		for (s = 0; s < size; s++) {
7081267941Srpaulo			if (c != '\0' && dtkind == DIF_TF_BYREF) {
7082267941Srpaulo				c = dtrace_load8(val++);
7083267941Srpaulo			} else if (c != '\0' && dtkind == DIF_TF_BYUREF) {
7084267941Srpaulo				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7085267941Srpaulo				c = dtrace_fuword8((void *)(uintptr_t)val++);
7086267941Srpaulo				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7087267941Srpaulo				if (*flags & CPU_DTRACE_FAULT)
7088267941Srpaulo					break;
7089267941Srpaulo			}
7090267941Srpaulo
7091267941Srpaulo			DTRACE_STORE(uint8_t, tomax, valoffs++, c);
7092267941Srpaulo
7093267941Srpaulo			if (c == '\0' && intuple)
7094267941Srpaulo				break;
7095267941Srpaulo		}
7096267941Srpaulo	} else {
7097267941Srpaulo		uint8_t c;
7098267941Srpaulo		while (valoffs < end) {
7099267941Srpaulo			if (dtkind == DIF_TF_BYREF) {
7100267941Srpaulo				c = dtrace_load8(val++);
7101267941Srpaulo			} else if (dtkind == DIF_TF_BYUREF) {
7102267941Srpaulo				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7103267941Srpaulo				c = dtrace_fuword8((void *)(uintptr_t)val++);
7104267941Srpaulo				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7105267941Srpaulo				if (*flags & CPU_DTRACE_FAULT)
7106267941Srpaulo					break;
7107267941Srpaulo			}
7108267941Srpaulo
7109267941Srpaulo			DTRACE_STORE(uint8_t, tomax,
7110267941Srpaulo			    valoffs++, c);
7111267941Srpaulo		}
7112267941Srpaulo	}
7113267941Srpaulo
7114267941Srpaulo	*valp = val;
7115267941Srpaulo	*valoffsp = valoffs;
7116267941Srpaulo}
7117267941Srpaulo
7118179193Sjb/*
7119332939Smarkj * Disables interrupts and sets the per-thread inprobe flag. When DEBUG is
7120332939Smarkj * defined, we also assert that we are not recursing unless the probe ID is an
7121332939Smarkj * error probe.
7122332939Smarkj */
7123332939Smarkjstatic dtrace_icookie_t
7124332939Smarkjdtrace_probe_enter(dtrace_id_t id)
7125332939Smarkj{
7126332939Smarkj	dtrace_icookie_t cookie;
7127332939Smarkj
7128332939Smarkj	cookie = dtrace_interrupt_disable();
7129332939Smarkj
7130332939Smarkj	/*
7131332939Smarkj	 * Unless this is an ERROR probe, we are not allowed to recurse in
7132332939Smarkj	 * dtrace_probe(). Recursing into DTrace probe usually means that a
7133332939Smarkj	 * function is instrumented that should not have been instrumented or
7134332939Smarkj	 * that the ordering guarantee of the records will be violated,
7135332939Smarkj	 * resulting in unexpected output. If there is an exception to this
7136332939Smarkj	 * assertion, a new case should be added.
7137332939Smarkj	 */
7138332939Smarkj	ASSERT(curthread->t_dtrace_inprobe == 0 ||
7139332939Smarkj	    id == dtrace_probeid_error);
7140332939Smarkj	curthread->t_dtrace_inprobe = 1;
7141332939Smarkj
7142332939Smarkj	return (cookie);
7143332939Smarkj}
7144332939Smarkj
7145332939Smarkj/*
7146332939Smarkj * Disables interrupts and clears the per-thread inprobe flag.
7147332939Smarkj */
7148332939Smarkjstatic void
7149332939Smarkjdtrace_probe_exit(dtrace_icookie_t cookie)
7150332939Smarkj{
7151332939Smarkj
7152332939Smarkj	curthread->t_dtrace_inprobe = 0;
7153332939Smarkj	dtrace_interrupt_enable(cookie);
7154332939Smarkj}
7155332939Smarkj
7156332939Smarkj/*
7157179193Sjb * If you're looking for the epicenter of DTrace, you just found it.  This
7158179193Sjb * is the function called by the provider to fire a probe -- from which all
7159179193Sjb * subsequent probe-context DTrace activity emanates.
7160179193Sjb */
7161179193Sjbvoid
7162179193Sjbdtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1,
7163179193Sjb    uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
7164179193Sjb{
7165179193Sjb	processorid_t cpuid;
7166179193Sjb	dtrace_icookie_t cookie;
7167179193Sjb	dtrace_probe_t *probe;
7168179193Sjb	dtrace_mstate_t mstate;
7169179193Sjb	dtrace_ecb_t *ecb;
7170179193Sjb	dtrace_action_t *act;
7171179193Sjb	intptr_t offs;
7172179193Sjb	size_t size;
7173179193Sjb	int vtime, onintr;
7174179193Sjb	volatile uint16_t *flags;
7175179193Sjb	hrtime_t now;
7176179193Sjb
7177228448Sattilio	if (panicstr != NULL)
7178228448Sattilio		return;
7179228448Sattilio
7180277300Ssmh#ifdef illumos
7181179193Sjb	/*
7182179193Sjb	 * Kick out immediately if this CPU is still being born (in which case
7183179193Sjb	 * curthread will be set to -1) or the current thread can't allow
7184179193Sjb	 * probes in its current context.
7185179193Sjb	 */
7186179193Sjb	if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE))
7187179193Sjb		return;
7188179198Sjb#endif
7189179193Sjb
7190332939Smarkj	cookie = dtrace_probe_enter(id);
7191179193Sjb	probe = dtrace_probes[id - 1];
7192179198Sjb	cpuid = curcpu;
7193179193Sjb	onintr = CPU_ON_INTR(CPU);
7194179193Sjb
7195179193Sjb	if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE &&
7196179193Sjb	    probe->dtpr_predcache == curthread->t_predcache) {
7197179193Sjb		/*
7198179193Sjb		 * We have hit in the predicate cache; we know that
7199179193Sjb		 * this predicate would evaluate to be false.
7200179193Sjb		 */
7201332939Smarkj		dtrace_probe_exit(cookie);
7202179193Sjb		return;
7203179193Sjb	}
7204179193Sjb
7205277300Ssmh#ifdef illumos
7206179193Sjb	if (panic_quiesce) {
7207179198Sjb#else
7208179198Sjb	if (panicstr != NULL) {
7209179198Sjb#endif
7210179193Sjb		/*
7211179193Sjb		 * We don't trace anything if we're panicking.
7212179193Sjb		 */
7213332939Smarkj		dtrace_probe_exit(cookie);
7214179193Sjb		return;
7215179193Sjb	}
7216179193Sjb
7217278167Spfg	now = mstate.dtms_timestamp = dtrace_gethrtime();
7218331553Smarkj	mstate.dtms_present = DTRACE_MSTATE_TIMESTAMP;
7219179193Sjb	vtime = dtrace_vtime_references != 0;
7220179193Sjb
7221179193Sjb	if (vtime && curthread->t_dtrace_start)
7222179193Sjb		curthread->t_dtrace_vtime += now - curthread->t_dtrace_start;
7223179193Sjb
7224179193Sjb	mstate.dtms_difo = NULL;
7225179193Sjb	mstate.dtms_probe = probe;
7226179198Sjb	mstate.dtms_strtok = 0;
7227179193Sjb	mstate.dtms_arg[0] = arg0;
7228179193Sjb	mstate.dtms_arg[1] = arg1;
7229179193Sjb	mstate.dtms_arg[2] = arg2;
7230179193Sjb	mstate.dtms_arg[3] = arg3;
7231179193Sjb	mstate.dtms_arg[4] = arg4;
7232179193Sjb
7233179193Sjb	flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags;
7234179193Sjb
7235179193Sjb	for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
7236179193Sjb		dtrace_predicate_t *pred = ecb->dte_predicate;
7237179193Sjb		dtrace_state_t *state = ecb->dte_state;
7238179193Sjb		dtrace_buffer_t *buf = &state->dts_buffer[cpuid];
7239179193Sjb		dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid];
7240179193Sjb		dtrace_vstate_t *vstate = &state->dts_vstate;
7241179193Sjb		dtrace_provider_t *prov = probe->dtpr_provider;
7242248690Spfg		uint64_t tracememsize = 0;
7243179193Sjb		int committed = 0;
7244179193Sjb		caddr_t tomax;
7245179193Sjb
7246179193Sjb		/*
7247179193Sjb		 * A little subtlety with the following (seemingly innocuous)
7248179193Sjb		 * declaration of the automatic 'val':  by looking at the
7249179193Sjb		 * code, you might think that it could be declared in the
7250179193Sjb		 * action processing loop, below.  (That is, it's only used in
7251179193Sjb		 * the action processing loop.)  However, it must be declared
7252179193Sjb		 * out of that scope because in the case of DIF expression
7253179193Sjb		 * arguments to aggregating actions, one iteration of the
7254179193Sjb		 * action loop will use the last iteration's value.
7255179193Sjb		 */
7256179193Sjb		uint64_t val = 0;
7257179193Sjb
7258179193Sjb		mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE;
7259267929Srpaulo		mstate.dtms_getf = NULL;
7260267929Srpaulo
7261179193Sjb		*flags &= ~CPU_DTRACE_ERROR;
7262179193Sjb
7263179193Sjb		if (prov == dtrace_provider) {
7264179193Sjb			/*
7265179193Sjb			 * If dtrace itself is the provider of this probe,
7266179193Sjb			 * we're only going to continue processing the ECB if
7267179193Sjb			 * arg0 (the dtrace_state_t) is equal to the ECB's
7268179193Sjb			 * creating state.  (This prevents disjoint consumers
7269179193Sjb			 * from seeing one another's metaprobes.)
7270179193Sjb			 */
7271179193Sjb			if (arg0 != (uint64_t)(uintptr_t)state)
7272179193Sjb				continue;
7273179193Sjb		}
7274179193Sjb
7275179193Sjb		if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) {
7276179193Sjb			/*
7277179193Sjb			 * We're not currently active.  If our provider isn't
7278179193Sjb			 * the dtrace pseudo provider, we're not interested.
7279179193Sjb			 */
7280179193Sjb			if (prov != dtrace_provider)
7281179193Sjb				continue;
7282179193Sjb
7283179193Sjb			/*
7284179193Sjb			 * Now we must further check if we are in the BEGIN
7285179193Sjb			 * probe.  If we are, we will only continue processing
7286179193Sjb			 * if we're still in WARMUP -- if one BEGIN enabling
7287179193Sjb			 * has invoked the exit() action, we don't want to
7288179193Sjb			 * evaluate subsequent BEGIN enablings.
7289179193Sjb			 */
7290179193Sjb			if (probe->dtpr_id == dtrace_probeid_begin &&
7291179193Sjb			    state->dts_activity != DTRACE_ACTIVITY_WARMUP) {
7292179193Sjb				ASSERT(state->dts_activity ==
7293179193Sjb				    DTRACE_ACTIVITY_DRAINING);
7294179193Sjb				continue;
7295179193Sjb			}
7296179193Sjb		}
7297179193Sjb
7298179193Sjb		if (ecb->dte_cond) {
7299179193Sjb			/*
7300179193Sjb			 * If the dte_cond bits indicate that this
7301179193Sjb			 * consumer is only allowed to see user-mode firings
7302179193Sjb			 * of this probe, call the provider's dtps_usermode()
7303179193Sjb			 * entry point to check that the probe was fired
7304179193Sjb			 * while in a user context. Skip this ECB if that's
7305179193Sjb			 * not the case.
7306179193Sjb			 */
7307179193Sjb			if ((ecb->dte_cond & DTRACE_COND_USERMODE) &&
7308179193Sjb			    prov->dtpv_pops.dtps_usermode(prov->dtpv_arg,
7309179193Sjb			    probe->dtpr_id, probe->dtpr_arg) == 0)
7310179193Sjb				continue;
7311179193Sjb
7312277300Ssmh#ifdef illumos
7313179193Sjb			/*
7314179193Sjb			 * This is more subtle than it looks. We have to be
7315179193Sjb			 * absolutely certain that CRED() isn't going to
7316179193Sjb			 * change out from under us so it's only legit to
7317179193Sjb			 * examine that structure if we're in constrained
7318179193Sjb			 * situations. Currently, the only times we'll this
7319179193Sjb			 * check is if a non-super-user has enabled the
7320179193Sjb			 * profile or syscall providers -- providers that
7321179193Sjb			 * allow visibility of all processes. For the
7322179193Sjb			 * profile case, the check above will ensure that
7323179193Sjb			 * we're examining a user context.
7324179193Sjb			 */
7325179193Sjb			if (ecb->dte_cond & DTRACE_COND_OWNER) {
7326179193Sjb				cred_t *cr;
7327179193Sjb				cred_t *s_cr =
7328179193Sjb				    ecb->dte_state->dts_cred.dcr_cred;
7329179193Sjb				proc_t *proc;
7330179193Sjb
7331179193Sjb				ASSERT(s_cr != NULL);
7332179193Sjb
7333179193Sjb				if ((cr = CRED()) == NULL ||
7334179193Sjb				    s_cr->cr_uid != cr->cr_uid ||
7335179193Sjb				    s_cr->cr_uid != cr->cr_ruid ||
7336179193Sjb				    s_cr->cr_uid != cr->cr_suid ||
7337179193Sjb				    s_cr->cr_gid != cr->cr_gid ||
7338179193Sjb				    s_cr->cr_gid != cr->cr_rgid ||
7339179193Sjb				    s_cr->cr_gid != cr->cr_sgid ||
7340179193Sjb				    (proc = ttoproc(curthread)) == NULL ||
7341179193Sjb				    (proc->p_flag & SNOCD))
7342179193Sjb					continue;
7343179193Sjb			}
7344179193Sjb
7345179193Sjb			if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) {
7346179193Sjb				cred_t *cr;
7347179193Sjb				cred_t *s_cr =
7348179193Sjb				    ecb->dte_state->dts_cred.dcr_cred;
7349179193Sjb
7350179193Sjb				ASSERT(s_cr != NULL);
7351179193Sjb
7352179193Sjb				if ((cr = CRED()) == NULL ||
7353179193Sjb				    s_cr->cr_zone->zone_id !=
7354179193Sjb				    cr->cr_zone->zone_id)
7355179193Sjb					continue;
7356179193Sjb			}
7357179198Sjb#endif
7358179193Sjb		}
7359179193Sjb
7360179193Sjb		if (now - state->dts_alive > dtrace_deadman_timeout) {
7361179193Sjb			/*
7362179193Sjb			 * We seem to be dead.  Unless we (a) have kernel
7363250574Smarkj			 * destructive permissions (b) have explicitly enabled
7364179193Sjb			 * destructive actions and (c) destructive actions have
7365179193Sjb			 * not been disabled, we're going to transition into
7366179193Sjb			 * the KILLED state, from which no further processing
7367179193Sjb			 * on this state will be performed.
7368179193Sjb			 */
7369179193Sjb			if (!dtrace_priv_kernel_destructive(state) ||
7370179193Sjb			    !state->dts_cred.dcr_destructive ||
7371179193Sjb			    dtrace_destructive_disallow) {
7372179193Sjb				void *activity = &state->dts_activity;
7373179193Sjb				dtrace_activity_t current;
7374179193Sjb
7375179193Sjb				do {
7376179193Sjb					current = state->dts_activity;
7377179193Sjb				} while (dtrace_cas32(activity, current,
7378179193Sjb				    DTRACE_ACTIVITY_KILLED) != current);
7379179193Sjb
7380179193Sjb				continue;
7381179193Sjb			}
7382179193Sjb		}
7383179193Sjb
7384179193Sjb		if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed,
7385179193Sjb		    ecb->dte_alignment, state, &mstate)) < 0)
7386179193Sjb			continue;
7387179193Sjb
7388179193Sjb		tomax = buf->dtb_tomax;
7389179193Sjb		ASSERT(tomax != NULL);
7390179193Sjb
7391250574Smarkj		if (ecb->dte_size != 0) {
7392250574Smarkj			dtrace_rechdr_t dtrh;
7393250574Smarkj			if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
7394250574Smarkj				mstate.dtms_timestamp = dtrace_gethrtime();
7395250574Smarkj				mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP;
7396250574Smarkj			}
7397250574Smarkj			ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t));
7398250574Smarkj			dtrh.dtrh_epid = ecb->dte_epid;
7399250574Smarkj			DTRACE_RECORD_STORE_TIMESTAMP(&dtrh,
7400250574Smarkj			    mstate.dtms_timestamp);
7401250574Smarkj			*((dtrace_rechdr_t *)(tomax + offs)) = dtrh;
7402250574Smarkj		}
7403179193Sjb
7404179193Sjb		mstate.dtms_epid = ecb->dte_epid;
7405179193Sjb		mstate.dtms_present |= DTRACE_MSTATE_EPID;
7406179193Sjb
7407179193Sjb		if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)
7408179193Sjb			mstate.dtms_access = DTRACE_ACCESS_KERNEL;
7409179193Sjb		else
7410179193Sjb			mstate.dtms_access = 0;
7411179193Sjb
7412179193Sjb		if (pred != NULL) {
7413179193Sjb			dtrace_difo_t *dp = pred->dtp_difo;
7414302913Smarkj			uint64_t rval;
7415179193Sjb
7416179193Sjb			rval = dtrace_dif_emulate(dp, &mstate, vstate, state);
7417179193Sjb
7418179193Sjb			if (!(*flags & CPU_DTRACE_ERROR) && !rval) {
7419179193Sjb				dtrace_cacheid_t cid = probe->dtpr_predcache;
7420179193Sjb
7421179193Sjb				if (cid != DTRACE_CACHEIDNONE && !onintr) {
7422179193Sjb					/*
7423179193Sjb					 * Update the predicate cache...
7424179193Sjb					 */
7425179193Sjb					ASSERT(cid == pred->dtp_cacheid);
7426179193Sjb					curthread->t_predcache = cid;
7427179193Sjb				}
7428179193Sjb
7429179193Sjb				continue;
7430179193Sjb			}
7431179193Sjb		}
7432179193Sjb
7433179193Sjb		for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) &&
7434179193Sjb		    act != NULL; act = act->dta_next) {
7435179193Sjb			size_t valoffs;
7436179193Sjb			dtrace_difo_t *dp;
7437179193Sjb			dtrace_recdesc_t *rec = &act->dta_rec;
7438179193Sjb
7439179193Sjb			size = rec->dtrd_size;
7440179193Sjb			valoffs = offs + rec->dtrd_offset;
7441179193Sjb
7442179193Sjb			if (DTRACEACT_ISAGG(act->dta_kind)) {
7443179193Sjb				uint64_t v = 0xbad;
7444179193Sjb				dtrace_aggregation_t *agg;
7445179193Sjb
7446179193Sjb				agg = (dtrace_aggregation_t *)act;
7447179193Sjb
7448179193Sjb				if ((dp = act->dta_difo) != NULL)
7449179193Sjb					v = dtrace_dif_emulate(dp,
7450179193Sjb					    &mstate, vstate, state);
7451179193Sjb
7452179193Sjb				if (*flags & CPU_DTRACE_ERROR)
7453179193Sjb					continue;
7454179193Sjb
7455179193Sjb				/*
7456179193Sjb				 * Note that we always pass the expression
7457179193Sjb				 * value from the previous iteration of the
7458179193Sjb				 * action loop.  This value will only be used
7459179193Sjb				 * if there is an expression argument to the
7460179193Sjb				 * aggregating action, denoted by the
7461179193Sjb				 * dtag_hasarg field.
7462179193Sjb				 */
7463179193Sjb				dtrace_aggregate(agg, buf,
7464179193Sjb				    offs, aggbuf, v, val);
7465179193Sjb				continue;
7466179193Sjb			}
7467179193Sjb
7468179193Sjb			switch (act->dta_kind) {
7469179193Sjb			case DTRACEACT_STOP:
7470179193Sjb				if (dtrace_priv_proc_destructive(state))
7471179193Sjb					dtrace_action_stop();
7472179193Sjb				continue;
7473179193Sjb
7474179193Sjb			case DTRACEACT_BREAKPOINT:
7475179193Sjb				if (dtrace_priv_kernel_destructive(state))
7476179193Sjb					dtrace_action_breakpoint(ecb);
7477179193Sjb				continue;
7478179193Sjb
7479179193Sjb			case DTRACEACT_PANIC:
7480179193Sjb				if (dtrace_priv_kernel_destructive(state))
7481179193Sjb					dtrace_action_panic(ecb);
7482179193Sjb				continue;
7483179193Sjb
7484179193Sjb			case DTRACEACT_STACK:
7485179193Sjb				if (!dtrace_priv_kernel(state))
7486179193Sjb					continue;
7487179193Sjb
7488179193Sjb				dtrace_getpcstack((pc_t *)(tomax + valoffs),
7489179193Sjb				    size / sizeof (pc_t), probe->dtpr_aframes,
7490179193Sjb				    DTRACE_ANCHORED(probe) ? NULL :
7491179193Sjb				    (uint32_t *)arg0);
7492179193Sjb				continue;
7493179193Sjb
7494179193Sjb			case DTRACEACT_JSTACK:
7495179193Sjb			case DTRACEACT_USTACK:
7496179193Sjb				if (!dtrace_priv_proc(state))
7497179193Sjb					continue;
7498179193Sjb
7499179193Sjb				/*
7500179193Sjb				 * See comment in DIF_VAR_PID.
7501179193Sjb				 */
7502179193Sjb				if (DTRACE_ANCHORED(mstate.dtms_probe) &&
7503179193Sjb				    CPU_ON_INTR(CPU)) {
7504179193Sjb					int depth = DTRACE_USTACK_NFRAMES(
7505179193Sjb					    rec->dtrd_arg) + 1;
7506179193Sjb
7507179193Sjb					dtrace_bzero((void *)(tomax + valoffs),
7508179193Sjb					    DTRACE_USTACK_STRSIZE(rec->dtrd_arg)
7509179193Sjb					    + depth * sizeof (uint64_t));
7510179193Sjb
7511179193Sjb					continue;
7512179193Sjb				}
7513179193Sjb
7514179193Sjb				if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 &&
7515179193Sjb				    curproc->p_dtrace_helpers != NULL) {
7516179193Sjb					/*
7517179193Sjb					 * This is the slow path -- we have
7518179193Sjb					 * allocated string space, and we're
7519179193Sjb					 * getting the stack of a process that
7520179193Sjb					 * has helpers.  Call into a separate
7521179193Sjb					 * routine to perform this processing.
7522179193Sjb					 */
7523179193Sjb					dtrace_action_ustack(&mstate, state,
7524179193Sjb					    (uint64_t *)(tomax + valoffs),
7525179193Sjb					    rec->dtrd_arg);
7526179193Sjb					continue;
7527179193Sjb				}
7528179193Sjb
7529179193Sjb				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7530179193Sjb				dtrace_getupcstack((uint64_t *)
7531179193Sjb				    (tomax + valoffs),
7532179193Sjb				    DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1);
7533179193Sjb				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7534179193Sjb				continue;
7535179193Sjb
7536179193Sjb			default:
7537179193Sjb				break;
7538179193Sjb			}
7539179193Sjb
7540179193Sjb			dp = act->dta_difo;
7541179193Sjb			ASSERT(dp != NULL);
7542179193Sjb
7543179193Sjb			val = dtrace_dif_emulate(dp, &mstate, vstate, state);
7544179193Sjb
7545179193Sjb			if (*flags & CPU_DTRACE_ERROR)
7546179193Sjb				continue;
7547179193Sjb
7548179193Sjb			switch (act->dta_kind) {
7549250574Smarkj			case DTRACEACT_SPECULATE: {
7550250574Smarkj				dtrace_rechdr_t *dtrh;
7551250574Smarkj
7552179193Sjb				ASSERT(buf == &state->dts_buffer[cpuid]);
7553179193Sjb				buf = dtrace_speculation_buffer(state,
7554179193Sjb				    cpuid, val);
7555179193Sjb
7556179193Sjb				if (buf == NULL) {
7557179193Sjb					*flags |= CPU_DTRACE_DROP;
7558179193Sjb					continue;
7559179193Sjb				}
7560179193Sjb
7561179193Sjb				offs = dtrace_buffer_reserve(buf,
7562179193Sjb				    ecb->dte_needed, ecb->dte_alignment,
7563179193Sjb				    state, NULL);
7564179193Sjb
7565179193Sjb				if (offs < 0) {
7566179193Sjb					*flags |= CPU_DTRACE_DROP;
7567179193Sjb					continue;
7568179193Sjb				}
7569179193Sjb
7570179193Sjb				tomax = buf->dtb_tomax;
7571179193Sjb				ASSERT(tomax != NULL);
7572179193Sjb
7573250574Smarkj				if (ecb->dte_size == 0)
7574250574Smarkj					continue;
7575250574Smarkj
7576250574Smarkj				ASSERT3U(ecb->dte_size, >=,
7577250574Smarkj				    sizeof (dtrace_rechdr_t));
7578250574Smarkj				dtrh = ((void *)(tomax + offs));
7579250574Smarkj				dtrh->dtrh_epid = ecb->dte_epid;
7580250574Smarkj				/*
7581250574Smarkj				 * When the speculation is committed, all of
7582250574Smarkj				 * the records in the speculative buffer will
7583250574Smarkj				 * have their timestamps set to the commit
7584250574Smarkj				 * time.  Until then, it is set to a sentinel
7585250574Smarkj				 * value, for debugability.
7586250574Smarkj				 */
7587250574Smarkj				DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX);
7588179193Sjb				continue;
7589250574Smarkj			}
7590179193Sjb
7591179198Sjb			case DTRACEACT_PRINTM: {
7592179198Sjb				/* The DIF returns a 'memref'. */
7593179198Sjb				uintptr_t *memref = (uintptr_t *)(uintptr_t) val;
7594179198Sjb
7595179198Sjb				/* Get the size from the memref. */
7596179198Sjb				size = memref[1];
7597179198Sjb
7598179198Sjb				/*
7599179198Sjb				 * Check if the size exceeds the allocated
7600179198Sjb				 * buffer size.
7601179198Sjb				 */
7602179198Sjb				if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) {
7603179198Sjb					/* Flag a drop! */
7604179198Sjb					*flags |= CPU_DTRACE_DROP;
7605179198Sjb					continue;
7606179198Sjb				}
7607179198Sjb
7608179198Sjb				/* Store the size in the buffer first. */
7609179198Sjb				DTRACE_STORE(uintptr_t, tomax,
7610179198Sjb				    valoffs, size);
7611179198Sjb
7612179198Sjb				/*
7613179198Sjb				 * Offset the buffer address to the start
7614179198Sjb				 * of the data.
7615179198Sjb				 */
7616179198Sjb				valoffs += sizeof(uintptr_t);
7617179198Sjb
7618179198Sjb				/*
7619179198Sjb				 * Reset to the memory address rather than
7620179198Sjb				 * the memref array, then let the BYREF
7621179198Sjb				 * code below do the work to store the
7622179198Sjb				 * memory data in the buffer.
7623179198Sjb				 */
7624179198Sjb				val = memref[0];
7625179198Sjb				break;
7626179198Sjb			}
7627179198Sjb
7628179198Sjb			case DTRACEACT_PRINTT: {
7629179198Sjb				/* The DIF returns a 'typeref'. */
7630179198Sjb				uintptr_t *typeref = (uintptr_t *)(uintptr_t) val;
7631179198Sjb				char c = '\0' + 1;
7632179198Sjb				size_t s;
7633179198Sjb
7634179198Sjb				/*
7635179198Sjb				 * Get the type string length and round it
7636179198Sjb				 * up so that the data that follows is
7637179198Sjb				 * aligned for easy access.
7638179198Sjb				 */
7639179198Sjb				size_t typs = strlen((char *) typeref[2]) + 1;
7640179198Sjb				typs = roundup(typs,  sizeof(uintptr_t));
7641179198Sjb
7642179198Sjb				/*
7643179198Sjb				 *Get the size from the typeref using the
7644179198Sjb				 * number of elements and the type size.
7645179198Sjb				 */
7646179198Sjb				size = typeref[1] * typeref[3];
7647179198Sjb
7648179198Sjb				/*
7649179198Sjb				 * Check if the size exceeds the allocated
7650179198Sjb				 * buffer size.
7651179198Sjb				 */
7652179198Sjb				if (size + typs + 2 * sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) {
7653179198Sjb					/* Flag a drop! */
7654179198Sjb					*flags |= CPU_DTRACE_DROP;
7655179198Sjb
7656179198Sjb				}
7657179198Sjb
7658179198Sjb				/* Store the size in the buffer first. */
7659179198Sjb				DTRACE_STORE(uintptr_t, tomax,
7660179198Sjb				    valoffs, size);
7661179198Sjb				valoffs += sizeof(uintptr_t);
7662179198Sjb
7663179198Sjb				/* Store the type size in the buffer. */
7664179198Sjb				DTRACE_STORE(uintptr_t, tomax,
7665179198Sjb				    valoffs, typeref[3]);
7666179198Sjb				valoffs += sizeof(uintptr_t);
7667179198Sjb
7668179198Sjb				val = typeref[2];
7669179198Sjb
7670179198Sjb				for (s = 0; s < typs; s++) {
7671179198Sjb					if (c != '\0')
7672179198Sjb						c = dtrace_load8(val++);
7673179198Sjb
7674179198Sjb					DTRACE_STORE(uint8_t, tomax,
7675179198Sjb					    valoffs++, c);
7676179198Sjb				}
7677179198Sjb
7678179198Sjb				/*
7679179198Sjb				 * Reset to the memory address rather than
7680179198Sjb				 * the typeref array, then let the BYREF
7681179198Sjb				 * code below do the work to store the
7682179198Sjb				 * memory data in the buffer.
7683179198Sjb				 */
7684179198Sjb				val = typeref[0];
7685179198Sjb				break;
7686179198Sjb			}
7687179198Sjb
7688179193Sjb			case DTRACEACT_CHILL:
7689179193Sjb				if (dtrace_priv_kernel_destructive(state))
7690179193Sjb					dtrace_action_chill(&mstate, val);
7691179193Sjb				continue;
7692179193Sjb
7693179193Sjb			case DTRACEACT_RAISE:
7694179193Sjb				if (dtrace_priv_proc_destructive(state))
7695179193Sjb					dtrace_action_raise(val);
7696179193Sjb				continue;
7697179193Sjb
7698179193Sjb			case DTRACEACT_COMMIT:
7699179193Sjb				ASSERT(!committed);
7700179193Sjb
7701179193Sjb				/*
7702179193Sjb				 * We need to commit our buffer state.
7703179193Sjb				 */
7704179193Sjb				if (ecb->dte_size)
7705179193Sjb					buf->dtb_offset = offs + ecb->dte_size;
7706179193Sjb				buf = &state->dts_buffer[cpuid];
7707179193Sjb				dtrace_speculation_commit(state, cpuid, val);
7708179193Sjb				committed = 1;
7709179193Sjb				continue;
7710179193Sjb
7711179193Sjb			case DTRACEACT_DISCARD:
7712179193Sjb				dtrace_speculation_discard(state, cpuid, val);
7713179193Sjb				continue;
7714179193Sjb
7715179193Sjb			case DTRACEACT_DIFEXPR:
7716179193Sjb			case DTRACEACT_LIBACT:
7717179193Sjb			case DTRACEACT_PRINTF:
7718179193Sjb			case DTRACEACT_PRINTA:
7719179193Sjb			case DTRACEACT_SYSTEM:
7720179193Sjb			case DTRACEACT_FREOPEN:
7721248690Spfg			case DTRACEACT_TRACEMEM:
7722179193Sjb				break;
7723179193Sjb
7724248690Spfg			case DTRACEACT_TRACEMEM_DYNSIZE:
7725248690Spfg				tracememsize = val;
7726248690Spfg				break;
7727248690Spfg
7728179193Sjb			case DTRACEACT_SYM:
7729179193Sjb			case DTRACEACT_MOD:
7730179193Sjb				if (!dtrace_priv_kernel(state))
7731179193Sjb					continue;
7732179193Sjb				break;
7733179193Sjb
7734179193Sjb			case DTRACEACT_USYM:
7735179193Sjb			case DTRACEACT_UMOD:
7736179193Sjb			case DTRACEACT_UADDR: {
7737277300Ssmh#ifdef illumos
7738179193Sjb				struct pid *pid = curthread->t_procp->p_pidp;
7739179198Sjb#endif
7740179193Sjb
7741179193Sjb				if (!dtrace_priv_proc(state))
7742179193Sjb					continue;
7743179193Sjb
7744179193Sjb				DTRACE_STORE(uint64_t, tomax,
7745277300Ssmh#ifdef illumos
7746179193Sjb				    valoffs, (uint64_t)pid->pid_id);
7747179198Sjb#else
7748179198Sjb				    valoffs, (uint64_t) curproc->p_pid);
7749179198Sjb#endif
7750179193Sjb				DTRACE_STORE(uint64_t, tomax,
7751179193Sjb				    valoffs + sizeof (uint64_t), val);
7752179193Sjb
7753179193Sjb				continue;
7754179193Sjb			}
7755179193Sjb
7756179193Sjb			case DTRACEACT_EXIT: {
7757179193Sjb				/*
7758179193Sjb				 * For the exit action, we are going to attempt
7759179193Sjb				 * to atomically set our activity to be
7760179193Sjb				 * draining.  If this fails (either because
7761179193Sjb				 * another CPU has beat us to the exit action,
7762179193Sjb				 * or because our current activity is something
7763179193Sjb				 * other than ACTIVE or WARMUP), we will
7764179193Sjb				 * continue.  This assures that the exit action
7765179193Sjb				 * can be successfully recorded at most once
7766179193Sjb				 * when we're in the ACTIVE state.  If we're
7767179193Sjb				 * encountering the exit() action while in
7768179193Sjb				 * COOLDOWN, however, we want to honor the new
7769179193Sjb				 * status code.  (We know that we're the only
7770179193Sjb				 * thread in COOLDOWN, so there is no race.)
7771179193Sjb				 */
7772179193Sjb				void *activity = &state->dts_activity;
7773179193Sjb				dtrace_activity_t current = state->dts_activity;
7774179193Sjb
7775179193Sjb				if (current == DTRACE_ACTIVITY_COOLDOWN)
7776179193Sjb					break;
7777179193Sjb
7778179193Sjb				if (current != DTRACE_ACTIVITY_WARMUP)
7779179193Sjb					current = DTRACE_ACTIVITY_ACTIVE;
7780179193Sjb
7781179193Sjb				if (dtrace_cas32(activity, current,
7782179193Sjb				    DTRACE_ACTIVITY_DRAINING) != current) {
7783179193Sjb					*flags |= CPU_DTRACE_DROP;
7784179193Sjb					continue;
7785179193Sjb				}
7786179193Sjb
7787179193Sjb				break;
7788179193Sjb			}
7789179193Sjb
7790179193Sjb			default:
7791179193Sjb				ASSERT(0);
7792179193Sjb			}
7793179193Sjb
7794267941Srpaulo			if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ||
7795267941Srpaulo			    dp->dtdo_rtype.dtdt_flags & DIF_TF_BYUREF) {
7796179193Sjb				uintptr_t end = valoffs + size;
7797179193Sjb
7798248690Spfg				if (tracememsize != 0 &&
7799248690Spfg				    valoffs + tracememsize < end) {
7800248690Spfg					end = valoffs + tracememsize;
7801248690Spfg					tracememsize = 0;
7802248690Spfg				}
7803248690Spfg
7804267941Srpaulo				if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF &&
7805267941Srpaulo				    !dtrace_vcanload((void *)(uintptr_t)val,
7806179193Sjb				    &dp->dtdo_rtype, &mstate, vstate))
7807179193Sjb					continue;
7808179193Sjb
7809267941Srpaulo				dtrace_store_by_ref(dp, tomax, size, &valoffs,
7810267941Srpaulo				    &val, end, act->dta_intuple,
7811267941Srpaulo				    dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ?
7812267941Srpaulo				    DIF_TF_BYREF: DIF_TF_BYUREF);
7813179193Sjb				continue;
7814179193Sjb			}
7815179193Sjb
7816179193Sjb			switch (size) {
7817179193Sjb			case 0:
7818179193Sjb				break;
7819179193Sjb
7820179193Sjb			case sizeof (uint8_t):
7821179193Sjb				DTRACE_STORE(uint8_t, tomax, valoffs, val);
7822179193Sjb				break;
7823179193Sjb			case sizeof (uint16_t):
7824179193Sjb				DTRACE_STORE(uint16_t, tomax, valoffs, val);
7825179193Sjb				break;
7826179193Sjb			case sizeof (uint32_t):
7827179193Sjb				DTRACE_STORE(uint32_t, tomax, valoffs, val);
7828179193Sjb				break;
7829179193Sjb			case sizeof (uint64_t):
7830179193Sjb				DTRACE_STORE(uint64_t, tomax, valoffs, val);
7831179193Sjb				break;
7832179193Sjb			default:
7833179193Sjb				/*
7834179193Sjb				 * Any other size should have been returned by
7835179193Sjb				 * reference, not by value.
7836179193Sjb				 */
7837179193Sjb				ASSERT(0);
7838179193Sjb				break;
7839179193Sjb			}
7840179193Sjb		}
7841179193Sjb
7842179193Sjb		if (*flags & CPU_DTRACE_DROP)
7843179193Sjb			continue;
7844179193Sjb
7845179193Sjb		if (*flags & CPU_DTRACE_FAULT) {
7846179193Sjb			int ndx;
7847179193Sjb			dtrace_action_t *err;
7848179193Sjb
7849179193Sjb			buf->dtb_errors++;
7850179193Sjb
7851179193Sjb			if (probe->dtpr_id == dtrace_probeid_error) {
7852179193Sjb				/*
7853179193Sjb				 * There's nothing we can do -- we had an
7854179193Sjb				 * error on the error probe.  We bump an
7855179193Sjb				 * error counter to at least indicate that
7856179193Sjb				 * this condition happened.
7857179193Sjb				 */
7858179193Sjb				dtrace_error(&state->dts_dblerrors);
7859179193Sjb				continue;
7860179193Sjb			}
7861179193Sjb
7862179193Sjb			if (vtime) {
7863179193Sjb				/*
7864179193Sjb				 * Before recursing on dtrace_probe(), we
7865179193Sjb				 * need to explicitly clear out our start
7866179193Sjb				 * time to prevent it from being accumulated
7867179193Sjb				 * into t_dtrace_vtime.
7868179193Sjb				 */
7869179193Sjb				curthread->t_dtrace_start = 0;
7870179193Sjb			}
7871179193Sjb
7872179193Sjb			/*
7873179193Sjb			 * Iterate over the actions to figure out which action
7874179193Sjb			 * we were processing when we experienced the error.
7875179193Sjb			 * Note that act points _past_ the faulting action; if
7876179193Sjb			 * act is ecb->dte_action, the fault was in the
7877179193Sjb			 * predicate, if it's ecb->dte_action->dta_next it's
7878179193Sjb			 * in action #1, and so on.
7879179193Sjb			 */
7880179193Sjb			for (err = ecb->dte_action, ndx = 0;
7881179193Sjb			    err != act; err = err->dta_next, ndx++)
7882179193Sjb				continue;
7883179193Sjb
7884179193Sjb			dtrace_probe_error(state, ecb->dte_epid, ndx,
7885179193Sjb			    (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ?
7886179193Sjb			    mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags),
7887179193Sjb			    cpu_core[cpuid].cpuc_dtrace_illval);
7888179193Sjb
7889179193Sjb			continue;
7890179193Sjb		}
7891179193Sjb
7892179193Sjb		if (!committed)
7893179193Sjb			buf->dtb_offset = offs + ecb->dte_size;
7894179193Sjb	}
7895179193Sjb
7896179193Sjb	if (vtime)
7897179193Sjb		curthread->t_dtrace_start = dtrace_gethrtime();
7898179193Sjb
7899332939Smarkj	dtrace_probe_exit(cookie);
7900179193Sjb}
7901179193Sjb
7902179193Sjb/*
7903179193Sjb * DTrace Probe Hashing Functions
7904179193Sjb *
7905179193Sjb * The functions in this section (and indeed, the functions in remaining
7906179193Sjb * sections) are not _called_ from probe context.  (Any exceptions to this are
7907179193Sjb * marked with a "Note:".)  Rather, they are called from elsewhere in the
7908179193Sjb * DTrace framework to look-up probes in, add probes to and remove probes from
7909179193Sjb * the DTrace probe hashes.  (Each probe is hashed by each element of the
7910179193Sjb * probe tuple -- allowing for fast lookups, regardless of what was
7911179193Sjb * specified.)
7912179193Sjb */
7913179193Sjbstatic uint_t
7914179198Sjbdtrace_hash_str(const char *p)
7915179193Sjb{
7916179193Sjb	unsigned int g;
7917179193Sjb	uint_t hval = 0;
7918179193Sjb
7919179193Sjb	while (*p) {
7920179193Sjb		hval = (hval << 4) + *p++;
7921179193Sjb		if ((g = (hval & 0xf0000000)) != 0)
7922179193Sjb			hval ^= g >> 24;
7923179193Sjb		hval &= ~g;
7924179193Sjb	}
7925179193Sjb	return (hval);
7926179193Sjb}
7927179193Sjb
7928179193Sjbstatic dtrace_hash_t *
7929179193Sjbdtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs)
7930179193Sjb{
7931179193Sjb	dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP);
7932179193Sjb
7933179193Sjb	hash->dth_stroffs = stroffs;
7934179193Sjb	hash->dth_nextoffs = nextoffs;
7935179193Sjb	hash->dth_prevoffs = prevoffs;
7936179193Sjb
7937179193Sjb	hash->dth_size = 1;
7938179193Sjb	hash->dth_mask = hash->dth_size - 1;
7939179193Sjb
7940179193Sjb	hash->dth_tab = kmem_zalloc(hash->dth_size *
7941179193Sjb	    sizeof (dtrace_hashbucket_t *), KM_SLEEP);
7942179193Sjb
7943179193Sjb	return (hash);
7944179193Sjb}
7945179193Sjb
7946179193Sjbstatic void
7947179193Sjbdtrace_hash_destroy(dtrace_hash_t *hash)
7948179193Sjb{
7949179193Sjb#ifdef DEBUG
7950179193Sjb	int i;
7951179193Sjb
7952179193Sjb	for (i = 0; i < hash->dth_size; i++)
7953179193Sjb		ASSERT(hash->dth_tab[i] == NULL);
7954179193Sjb#endif
7955179193Sjb
7956179193Sjb	kmem_free(hash->dth_tab,
7957179193Sjb	    hash->dth_size * sizeof (dtrace_hashbucket_t *));
7958179193Sjb	kmem_free(hash, sizeof (dtrace_hash_t));
7959179193Sjb}
7960179193Sjb
7961179193Sjbstatic void
7962179193Sjbdtrace_hash_resize(dtrace_hash_t *hash)
7963179193Sjb{
7964179193Sjb	int size = hash->dth_size, i, ndx;
7965179193Sjb	int new_size = hash->dth_size << 1;
7966179193Sjb	int new_mask = new_size - 1;
7967179193Sjb	dtrace_hashbucket_t **new_tab, *bucket, *next;
7968179193Sjb
7969179193Sjb	ASSERT((new_size & new_mask) == 0);
7970179193Sjb
7971179193Sjb	new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP);
7972179193Sjb
7973179193Sjb	for (i = 0; i < size; i++) {
7974179193Sjb		for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) {
7975179193Sjb			dtrace_probe_t *probe = bucket->dthb_chain;
7976179193Sjb
7977179193Sjb			ASSERT(probe != NULL);
7978179193Sjb			ndx = DTRACE_HASHSTR(hash, probe) & new_mask;
7979179193Sjb
7980179193Sjb			next = bucket->dthb_next;
7981179193Sjb			bucket->dthb_next = new_tab[ndx];
7982179193Sjb			new_tab[ndx] = bucket;
7983179193Sjb		}
7984179193Sjb	}
7985179193Sjb
7986179193Sjb	kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *));
7987179193Sjb	hash->dth_tab = new_tab;
7988179193Sjb	hash->dth_size = new_size;
7989179193Sjb	hash->dth_mask = new_mask;
7990179193Sjb}
7991179193Sjb
7992179193Sjbstatic void
7993179193Sjbdtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new)
7994179193Sjb{
7995179193Sjb	int hashval = DTRACE_HASHSTR(hash, new);
7996179193Sjb	int ndx = hashval & hash->dth_mask;
7997179193Sjb	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
7998179193Sjb	dtrace_probe_t **nextp, **prevp;
7999179193Sjb
8000179193Sjb	for (; bucket != NULL; bucket = bucket->dthb_next) {
8001179193Sjb		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new))
8002179193Sjb			goto add;
8003179193Sjb	}
8004179193Sjb
8005179193Sjb	if ((hash->dth_nbuckets >> 1) > hash->dth_size) {
8006179193Sjb		dtrace_hash_resize(hash);
8007179193Sjb		dtrace_hash_add(hash, new);
8008179193Sjb		return;
8009179193Sjb	}
8010179193Sjb
8011179193Sjb	bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP);
8012179193Sjb	bucket->dthb_next = hash->dth_tab[ndx];
8013179193Sjb	hash->dth_tab[ndx] = bucket;
8014179193Sjb	hash->dth_nbuckets++;
8015179193Sjb
8016179193Sjbadd:
8017179193Sjb	nextp = DTRACE_HASHNEXT(hash, new);
8018179193Sjb	ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL);
8019179193Sjb	*nextp = bucket->dthb_chain;
8020179193Sjb
8021179193Sjb	if (bucket->dthb_chain != NULL) {
8022179193Sjb		prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain);
8023179193Sjb		ASSERT(*prevp == NULL);
8024179193Sjb		*prevp = new;
8025179193Sjb	}
8026179193Sjb
8027179193Sjb	bucket->dthb_chain = new;
8028179193Sjb	bucket->dthb_len++;
8029179193Sjb}
8030179193Sjb
8031179193Sjbstatic dtrace_probe_t *
8032179193Sjbdtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template)
8033179193Sjb{
8034179193Sjb	int hashval = DTRACE_HASHSTR(hash, template);
8035179193Sjb	int ndx = hashval & hash->dth_mask;
8036179193Sjb	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
8037179193Sjb
8038179193Sjb	for (; bucket != NULL; bucket = bucket->dthb_next) {
8039179193Sjb		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
8040179193Sjb			return (bucket->dthb_chain);
8041179193Sjb	}
8042179193Sjb
8043179193Sjb	return (NULL);
8044179193Sjb}
8045179193Sjb
8046179193Sjbstatic int
8047179193Sjbdtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template)
8048179193Sjb{
8049179193Sjb	int hashval = DTRACE_HASHSTR(hash, template);
8050179193Sjb	int ndx = hashval & hash->dth_mask;
8051179193Sjb	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
8052179193Sjb
8053179193Sjb	for (; bucket != NULL; bucket = bucket->dthb_next) {
8054179193Sjb		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
8055179193Sjb			return (bucket->dthb_len);
8056179193Sjb	}
8057179193Sjb
8058179198Sjb	return (0);
8059179193Sjb}
8060179193Sjb
8061179193Sjbstatic void
8062179193Sjbdtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe)
8063179193Sjb{
8064179193Sjb	int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask;
8065179193Sjb	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
8066179193Sjb
8067179193Sjb	dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe);
8068179193Sjb	dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe);
8069179193Sjb
8070179193Sjb	/*
8071179193Sjb	 * Find the bucket that we're removing this probe from.
8072179193Sjb	 */
8073179193Sjb	for (; bucket != NULL; bucket = bucket->dthb_next) {
8074179193Sjb		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe))
8075179193Sjb			break;
8076179193Sjb	}
8077179193Sjb
8078179193Sjb	ASSERT(bucket != NULL);
8079179193Sjb
8080179193Sjb	if (*prevp == NULL) {
8081179193Sjb		if (*nextp == NULL) {
8082179193Sjb			/*
8083179193Sjb			 * The removed probe was the only probe on this
8084179193Sjb			 * bucket; we need to remove the bucket.
8085179193Sjb			 */
8086179193Sjb			dtrace_hashbucket_t *b = hash->dth_tab[ndx];
8087179193Sjb
8088179193Sjb			ASSERT(bucket->dthb_chain == probe);
8089179193Sjb			ASSERT(b != NULL);
8090179193Sjb
8091179193Sjb			if (b == bucket) {
8092179193Sjb				hash->dth_tab[ndx] = bucket->dthb_next;
8093179193Sjb			} else {
8094179193Sjb				while (b->dthb_next != bucket)
8095179193Sjb					b = b->dthb_next;
8096179193Sjb				b->dthb_next = bucket->dthb_next;
8097179193Sjb			}
8098179193Sjb
8099179193Sjb			ASSERT(hash->dth_nbuckets > 0);
8100179193Sjb			hash->dth_nbuckets--;
8101179193Sjb			kmem_free(bucket, sizeof (dtrace_hashbucket_t));
8102179193Sjb			return;
8103179193Sjb		}
8104179193Sjb
8105179193Sjb		bucket->dthb_chain = *nextp;
8106179193Sjb	} else {
8107179193Sjb		*(DTRACE_HASHNEXT(hash, *prevp)) = *nextp;
8108179193Sjb	}
8109179193Sjb
8110179193Sjb	if (*nextp != NULL)
8111179193Sjb		*(DTRACE_HASHPREV(hash, *nextp)) = *prevp;
8112179193Sjb}
8113179193Sjb
8114179193Sjb/*
8115179193Sjb * DTrace Utility Functions
8116179193Sjb *
8117179193Sjb * These are random utility functions that are _not_ called from probe context.
8118179193Sjb */
8119179193Sjbstatic int
8120179193Sjbdtrace_badattr(const dtrace_attribute_t *a)
8121179193Sjb{
8122179193Sjb	return (a->dtat_name > DTRACE_STABILITY_MAX ||
8123179193Sjb	    a->dtat_data > DTRACE_STABILITY_MAX ||
8124179193Sjb	    a->dtat_class > DTRACE_CLASS_MAX);
8125179193Sjb}
8126179193Sjb
8127179193Sjb/*
8128179193Sjb * Return a duplicate copy of a string.  If the specified string is NULL,
8129179193Sjb * this function returns a zero-length string.
8130179193Sjb */
8131179193Sjbstatic char *
8132179193Sjbdtrace_strdup(const char *str)
8133179193Sjb{
8134179193Sjb	char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP);
8135179193Sjb
8136179193Sjb	if (str != NULL)
8137179193Sjb		(void) strcpy(new, str);
8138179193Sjb
8139179193Sjb	return (new);
8140179193Sjb}
8141179193Sjb
8142179193Sjb#define	DTRACE_ISALPHA(c)	\
8143179193Sjb	(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
8144179193Sjb
8145179193Sjbstatic int
8146179193Sjbdtrace_badname(const char *s)
8147179193Sjb{
8148179193Sjb	char c;
8149179193Sjb
8150179193Sjb	if (s == NULL || (c = *s++) == '\0')
8151179193Sjb		return (0);
8152179193Sjb
8153179193Sjb	if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.')
8154179193Sjb		return (1);
8155179193Sjb
8156179193Sjb	while ((c = *s++) != '\0') {
8157179193Sjb		if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') &&
8158179193Sjb		    c != '-' && c != '_' && c != '.' && c != '`')
8159179193Sjb			return (1);
8160179193Sjb	}
8161179193Sjb
8162179193Sjb	return (0);
8163179193Sjb}
8164179193Sjb
8165179193Sjbstatic void
8166179193Sjbdtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp)
8167179193Sjb{
8168179193Sjb	uint32_t priv;
8169179193Sjb
8170277300Ssmh#ifdef illumos
8171179193Sjb	if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
8172179193Sjb		/*
8173179193Sjb		 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter.
8174179193Sjb		 */
8175179193Sjb		priv = DTRACE_PRIV_ALL;
8176179193Sjb	} else {
8177179193Sjb		*uidp = crgetuid(cr);
8178179193Sjb		*zoneidp = crgetzoneid(cr);
8179179193Sjb
8180179193Sjb		priv = 0;
8181179193Sjb		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE))
8182179193Sjb			priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER;
8183179193Sjb		else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE))
8184179193Sjb			priv |= DTRACE_PRIV_USER;
8185179193Sjb		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE))
8186179193Sjb			priv |= DTRACE_PRIV_PROC;
8187179193Sjb		if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
8188179193Sjb			priv |= DTRACE_PRIV_OWNER;
8189179193Sjb		if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
8190179193Sjb			priv |= DTRACE_PRIV_ZONEOWNER;
8191179193Sjb	}
8192179198Sjb#else
8193179198Sjb	priv = DTRACE_PRIV_ALL;
8194179198Sjb#endif
8195179193Sjb
8196179193Sjb	*privp = priv;
8197179193Sjb}
8198179193Sjb
8199179193Sjb#ifdef DTRACE_ERRDEBUG
8200179193Sjbstatic void
8201179193Sjbdtrace_errdebug(const char *str)
8202179193Sjb{
8203179198Sjb	int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ;
8204179193Sjb	int occupied = 0;
8205179193Sjb
8206179193Sjb	mutex_enter(&dtrace_errlock);
8207179193Sjb	dtrace_errlast = str;
8208179193Sjb	dtrace_errthread = curthread;
8209179193Sjb
8210179193Sjb	while (occupied++ < DTRACE_ERRHASHSZ) {
8211179193Sjb		if (dtrace_errhash[hval].dter_msg == str) {
8212179193Sjb			dtrace_errhash[hval].dter_count++;
8213179193Sjb			goto out;
8214179193Sjb		}
8215179193Sjb
8216179193Sjb		if (dtrace_errhash[hval].dter_msg != NULL) {
8217179193Sjb			hval = (hval + 1) % DTRACE_ERRHASHSZ;
8218179193Sjb			continue;
8219179193Sjb		}
8220179193Sjb
8221179193Sjb		dtrace_errhash[hval].dter_msg = str;
8222179193Sjb		dtrace_errhash[hval].dter_count = 1;
8223179193Sjb		goto out;
8224179193Sjb	}
8225179193Sjb
8226179193Sjb	panic("dtrace: undersized error hash");
8227179193Sjbout:
8228179193Sjb	mutex_exit(&dtrace_errlock);
8229179193Sjb}
8230179193Sjb#endif
8231179193Sjb
8232179193Sjb/*
8233179193Sjb * DTrace Matching Functions
8234179193Sjb *
8235179193Sjb * These functions are used to match groups of probes, given some elements of
8236179193Sjb * a probe tuple, or some globbed expressions for elements of a probe tuple.
8237179193Sjb */
8238179193Sjbstatic int
8239179193Sjbdtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid,
8240179193Sjb    zoneid_t zoneid)
8241179193Sjb{
8242179193Sjb	if (priv != DTRACE_PRIV_ALL) {
8243179193Sjb		uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags;
8244179193Sjb		uint32_t match = priv & ppriv;
8245179193Sjb
8246179193Sjb		/*
8247179193Sjb		 * No PRIV_DTRACE_* privileges...
8248179193Sjb		 */
8249179193Sjb		if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER |
8250179193Sjb		    DTRACE_PRIV_KERNEL)) == 0)
8251179193Sjb			return (0);
8252179193Sjb
8253179193Sjb		/*
8254179193Sjb		 * No matching bits, but there were bits to match...
8255179193Sjb		 */
8256179193Sjb		if (match == 0 && ppriv != 0)
8257179193Sjb			return (0);
8258179193Sjb
8259179193Sjb		/*
8260179193Sjb		 * Need to have permissions to the process, but don't...
8261179193Sjb		 */
8262179193Sjb		if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 &&
8263179193Sjb		    uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) {
8264179193Sjb			return (0);
8265179193Sjb		}
8266179193Sjb
8267179193Sjb		/*
8268179193Sjb		 * Need to be in the same zone unless we possess the
8269179193Sjb		 * privilege to examine all zones.
8270179193Sjb		 */
8271179193Sjb		if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 &&
8272179193Sjb		    zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) {
8273179193Sjb			return (0);
8274179193Sjb		}
8275179193Sjb	}
8276179193Sjb
8277179193Sjb	return (1);
8278179193Sjb}
8279179193Sjb
8280179193Sjb/*
8281179193Sjb * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which
8282179193Sjb * consists of input pattern strings and an ops-vector to evaluate them.
8283179193Sjb * This function returns >0 for match, 0 for no match, and <0 for error.
8284179193Sjb */
8285179193Sjbstatic int
8286179193Sjbdtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp,
8287179193Sjb    uint32_t priv, uid_t uid, zoneid_t zoneid)
8288179193Sjb{
8289179193Sjb	dtrace_provider_t *pvp = prp->dtpr_provider;
8290179193Sjb	int rv;
8291179193Sjb
8292179193Sjb	if (pvp->dtpv_defunct)
8293179193Sjb		return (0);
8294179193Sjb
8295179193Sjb	if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0)
8296179193Sjb		return (rv);
8297179193Sjb
8298179193Sjb	if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0)
8299179193Sjb		return (rv);
8300179193Sjb
8301179193Sjb	if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0)
8302179193Sjb		return (rv);
8303179193Sjb
8304179193Sjb	if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0)
8305179193Sjb		return (rv);
8306179193Sjb
8307179193Sjb	if (dtrace_match_priv(prp, priv, uid, zoneid) == 0)
8308179193Sjb		return (0);
8309179193Sjb
8310179193Sjb	return (rv);
8311179193Sjb}
8312179193Sjb
8313179193Sjb/*
8314179193Sjb * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN)
8315179193Sjb * interface for matching a glob pattern 'p' to an input string 's'.  Unlike
8316179193Sjb * libc's version, the kernel version only applies to 8-bit ASCII strings.
8317179193Sjb * In addition, all of the recursion cases except for '*' matching have been
8318179193Sjb * unwound.  For '*', we still implement recursive evaluation, but a depth
8319179193Sjb * counter is maintained and matching is aborted if we recurse too deep.
8320179193Sjb * The function returns 0 if no match, >0 if match, and <0 if recursion error.
8321179193Sjb */
8322179193Sjbstatic int
8323179193Sjbdtrace_match_glob(const char *s, const char *p, int depth)
8324179193Sjb{
8325179193Sjb	const char *olds;
8326179193Sjb	char s1, c;
8327179193Sjb	int gs;
8328179193Sjb
8329179193Sjb	if (depth > DTRACE_PROBEKEY_MAXDEPTH)
8330179193Sjb		return (-1);
8331179193Sjb
8332179193Sjb	if (s == NULL)
8333179193Sjb		s = ""; /* treat NULL as empty string */
8334179193Sjb
8335179193Sjbtop:
8336179193Sjb	olds = s;
8337179193Sjb	s1 = *s++;
8338179193Sjb
8339179193Sjb	if (p == NULL)
8340179193Sjb		return (0);
8341179193Sjb
8342179193Sjb	if ((c = *p++) == '\0')
8343179193Sjb		return (s1 == '\0');
8344179193Sjb
8345179193Sjb	switch (c) {
8346179193Sjb	case '[': {
8347179193Sjb		int ok = 0, notflag = 0;
8348179193Sjb		char lc = '\0';
8349179193Sjb
8350179193Sjb		if (s1 == '\0')
8351179193Sjb			return (0);
8352179193Sjb
8353179193Sjb		if (*p == '!') {
8354179193Sjb			notflag = 1;
8355179193Sjb			p++;
8356179193Sjb		}
8357179193Sjb
8358179193Sjb		if ((c = *p++) == '\0')
8359179193Sjb			return (0);
8360179193Sjb
8361179193Sjb		do {
8362179193Sjb			if (c == '-' && lc != '\0' && *p != ']') {
8363179193Sjb				if ((c = *p++) == '\0')
8364179193Sjb					return (0);
8365179193Sjb				if (c == '\\' && (c = *p++) == '\0')
8366179193Sjb					return (0);
8367179193Sjb
8368179193Sjb				if (notflag) {
8369179193Sjb					if (s1 < lc || s1 > c)
8370179193Sjb						ok++;
8371179193Sjb					else
8372179193Sjb						return (0);
8373179193Sjb				} else if (lc <= s1 && s1 <= c)
8374179193Sjb					ok++;
8375179193Sjb
8376179193Sjb			} else if (c == '\\' && (c = *p++) == '\0')
8377179193Sjb				return (0);
8378179193Sjb
8379179193Sjb			lc = c; /* save left-hand 'c' for next iteration */
8380179193Sjb
8381179193Sjb			if (notflag) {
8382179193Sjb				if (s1 != c)
8383179193Sjb					ok++;
8384179193Sjb				else
8385179193Sjb					return (0);
8386179193Sjb			} else if (s1 == c)
8387179193Sjb				ok++;
8388179193Sjb
8389179193Sjb			if ((c = *p++) == '\0')
8390179193Sjb				return (0);
8391179193Sjb
8392179193Sjb		} while (c != ']');
8393179193Sjb
8394179193Sjb		if (ok)
8395179193Sjb			goto top;
8396179193Sjb
8397179193Sjb		return (0);
8398179193Sjb	}
8399179193Sjb
8400179193Sjb	case '\\':
8401179193Sjb		if ((c = *p++) == '\0')
8402179193Sjb			return (0);
8403179193Sjb		/*FALLTHRU*/
8404179193Sjb
8405179193Sjb	default:
8406179193Sjb		if (c != s1)
8407179193Sjb			return (0);
8408179193Sjb		/*FALLTHRU*/
8409179193Sjb
8410179193Sjb	case '?':
8411179193Sjb		if (s1 != '\0')
8412179193Sjb			goto top;
8413179193Sjb		return (0);
8414179193Sjb
8415179193Sjb	case '*':
8416179193Sjb		while (*p == '*')
8417179193Sjb			p++; /* consecutive *'s are identical to a single one */
8418179193Sjb
8419179193Sjb		if (*p == '\0')
8420179193Sjb			return (1);
8421179193Sjb
8422179193Sjb		for (s = olds; *s != '\0'; s++) {
8423179193Sjb			if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0)
8424179193Sjb				return (gs);
8425179193Sjb		}
8426179193Sjb
8427179193Sjb		return (0);
8428179193Sjb	}
8429179193Sjb}
8430179193Sjb
8431179193Sjb/*ARGSUSED*/
8432179193Sjbstatic int
8433179193Sjbdtrace_match_string(const char *s, const char *p, int depth)
8434179193Sjb{
8435179193Sjb	return (s != NULL && strcmp(s, p) == 0);
8436179193Sjb}
8437179193Sjb
8438179193Sjb/*ARGSUSED*/
8439179193Sjbstatic int
8440179193Sjbdtrace_match_nul(const char *s, const char *p, int depth)
8441179193Sjb{
8442179193Sjb	return (1); /* always match the empty pattern */
8443179193Sjb}
8444179193Sjb
8445179193Sjb/*ARGSUSED*/
8446179193Sjbstatic int
8447179193Sjbdtrace_match_nonzero(const char *s, const char *p, int depth)
8448179193Sjb{
8449179193Sjb	return (s != NULL && s[0] != '\0');
8450179193Sjb}
8451179193Sjb
8452179193Sjbstatic int
8453179193Sjbdtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid,
8454179193Sjb    zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg)
8455179193Sjb{
8456179193Sjb	dtrace_probe_t template, *probe;
8457179193Sjb	dtrace_hash_t *hash = NULL;
8458179193Sjb	int len, best = INT_MAX, nmatched = 0;
8459179193Sjb	dtrace_id_t i;
8460179193Sjb
8461179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
8462179193Sjb
8463179193Sjb	/*
8464179193Sjb	 * If the probe ID is specified in the key, just lookup by ID and
8465179193Sjb	 * invoke the match callback once if a matching probe is found.
8466179193Sjb	 */
8467179193Sjb	if (pkp->dtpk_id != DTRACE_IDNONE) {
8468179193Sjb		if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL &&
8469179193Sjb		    dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) {
8470179193Sjb			(void) (*matched)(probe, arg);
8471179193Sjb			nmatched++;
8472179193Sjb		}
8473179193Sjb		return (nmatched);
8474179193Sjb	}
8475179193Sjb
8476179193Sjb	template.dtpr_mod = (char *)pkp->dtpk_mod;
8477179193Sjb	template.dtpr_func = (char *)pkp->dtpk_func;
8478179193Sjb	template.dtpr_name = (char *)pkp->dtpk_name;
8479179193Sjb
8480179193Sjb	/*
8481179193Sjb	 * We want to find the most distinct of the module name, function
8482179193Sjb	 * name, and name.  So for each one that is not a glob pattern or
8483179193Sjb	 * empty string, we perform a lookup in the corresponding hash and
8484179193Sjb	 * use the hash table with the fewest collisions to do our search.
8485179193Sjb	 */
8486179193Sjb	if (pkp->dtpk_mmatch == &dtrace_match_string &&
8487179193Sjb	    (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) {
8488179193Sjb		best = len;
8489179193Sjb		hash = dtrace_bymod;
8490179193Sjb	}
8491179193Sjb
8492179193Sjb	if (pkp->dtpk_fmatch == &dtrace_match_string &&
8493179193Sjb	    (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) {
8494179193Sjb		best = len;
8495179193Sjb		hash = dtrace_byfunc;
8496179193Sjb	}
8497179193Sjb
8498179193Sjb	if (pkp->dtpk_nmatch == &dtrace_match_string &&
8499179193Sjb	    (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) {
8500179193Sjb		best = len;
8501179193Sjb		hash = dtrace_byname;
8502179193Sjb	}
8503179193Sjb
8504179193Sjb	/*
8505179193Sjb	 * If we did not select a hash table, iterate over every probe and
8506179193Sjb	 * invoke our callback for each one that matches our input probe key.
8507179193Sjb	 */
8508179193Sjb	if (hash == NULL) {
8509179193Sjb		for (i = 0; i < dtrace_nprobes; i++) {
8510179193Sjb			if ((probe = dtrace_probes[i]) == NULL ||
8511179193Sjb			    dtrace_match_probe(probe, pkp, priv, uid,
8512179193Sjb			    zoneid) <= 0)
8513179193Sjb				continue;
8514179193Sjb
8515179193Sjb			nmatched++;
8516179193Sjb
8517179193Sjb			if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT)
8518179193Sjb				break;
8519179193Sjb		}
8520179193Sjb
8521179193Sjb		return (nmatched);
8522179193Sjb	}
8523179193Sjb
8524179193Sjb	/*
8525179193Sjb	 * If we selected a hash table, iterate over each probe of the same key
8526179193Sjb	 * name and invoke the callback for every probe that matches the other
8527179193Sjb	 * attributes of our input probe key.
8528179193Sjb	 */
8529179193Sjb	for (probe = dtrace_hash_lookup(hash, &template); probe != NULL;
8530179193Sjb	    probe = *(DTRACE_HASHNEXT(hash, probe))) {
8531179193Sjb
8532179193Sjb		if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0)
8533179193Sjb			continue;
8534179193Sjb
8535179193Sjb		nmatched++;
8536179193Sjb
8537179193Sjb		if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT)
8538179193Sjb			break;
8539179193Sjb	}
8540179193Sjb
8541179193Sjb	return (nmatched);
8542179193Sjb}
8543179193Sjb
8544179193Sjb/*
8545179193Sjb * Return the function pointer dtrace_probecmp() should use to compare the
8546179193Sjb * specified pattern with a string.  For NULL or empty patterns, we select
8547179193Sjb * dtrace_match_nul().  For glob pattern strings, we use dtrace_match_glob().
8548179193Sjb * For non-empty non-glob strings, we use dtrace_match_string().
8549179193Sjb */
8550179193Sjbstatic dtrace_probekey_f *
8551179193Sjbdtrace_probekey_func(const char *p)
8552179193Sjb{
8553179193Sjb	char c;
8554179193Sjb
8555179193Sjb	if (p == NULL || *p == '\0')
8556179193Sjb		return (&dtrace_match_nul);
8557179193Sjb
8558179193Sjb	while ((c = *p++) != '\0') {
8559179193Sjb		if (c == '[' || c == '?' || c == '*' || c == '\\')
8560179193Sjb			return (&dtrace_match_glob);
8561179193Sjb	}
8562179193Sjb
8563179193Sjb	return (&dtrace_match_string);
8564179193Sjb}
8565179193Sjb
8566179193Sjb/*
8567179193Sjb * Build a probe comparison key for use with dtrace_match_probe() from the
8568179193Sjb * given probe description.  By convention, a null key only matches anchored
8569179193Sjb * probes: if each field is the empty string, reset dtpk_fmatch to
8570179193Sjb * dtrace_match_nonzero().
8571179193Sjb */
8572179193Sjbstatic void
8573179198Sjbdtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp)
8574179193Sjb{
8575179193Sjb	pkp->dtpk_prov = pdp->dtpd_provider;
8576179193Sjb	pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider);
8577179193Sjb
8578179193Sjb	pkp->dtpk_mod = pdp->dtpd_mod;
8579179193Sjb	pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod);
8580179193Sjb
8581179193Sjb	pkp->dtpk_func = pdp->dtpd_func;
8582179193Sjb	pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func);
8583179193Sjb
8584179193Sjb	pkp->dtpk_name = pdp->dtpd_name;
8585179193Sjb	pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name);
8586179193Sjb
8587179193Sjb	pkp->dtpk_id = pdp->dtpd_id;
8588179193Sjb
8589179193Sjb	if (pkp->dtpk_id == DTRACE_IDNONE &&
8590179193Sjb	    pkp->dtpk_pmatch == &dtrace_match_nul &&
8591179193Sjb	    pkp->dtpk_mmatch == &dtrace_match_nul &&
8592179193Sjb	    pkp->dtpk_fmatch == &dtrace_match_nul &&
8593179193Sjb	    pkp->dtpk_nmatch == &dtrace_match_nul)
8594179193Sjb		pkp->dtpk_fmatch = &dtrace_match_nonzero;
8595179193Sjb}
8596179193Sjb
8597179193Sjb/*
8598179193Sjb * DTrace Provider-to-Framework API Functions
8599179193Sjb *
8600179193Sjb * These functions implement much of the Provider-to-Framework API, as
8601179193Sjb * described in <sys/dtrace.h>.  The parts of the API not in this section are
8602179193Sjb * the functions in the API for probe management (found below), and
8603179193Sjb * dtrace_probe() itself (found above).
8604179193Sjb */
8605179193Sjb
8606179193Sjb/*
8607179193Sjb * Register the calling provider with the DTrace framework.  This should
8608179193Sjb * generally be called by DTrace providers in their attach(9E) entry point.
8609179193Sjb */
8610179193Sjbint
8611179193Sjbdtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv,
8612179193Sjb    cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp)
8613179193Sjb{
8614179193Sjb	dtrace_provider_t *provider;
8615179193Sjb
8616179193Sjb	if (name == NULL || pap == NULL || pops == NULL || idp == NULL) {
8617179193Sjb		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8618179193Sjb		    "arguments", name ? name : "<NULL>");
8619179193Sjb		return (EINVAL);
8620179193Sjb	}
8621179193Sjb
8622179193Sjb	if (name[0] == '\0' || dtrace_badname(name)) {
8623179193Sjb		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8624179193Sjb		    "provider name", name);
8625179193Sjb		return (EINVAL);
8626179193Sjb	}
8627179193Sjb
8628179193Sjb	if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) ||
8629179193Sjb	    pops->dtps_enable == NULL || pops->dtps_disable == NULL ||
8630179193Sjb	    pops->dtps_destroy == NULL ||
8631179193Sjb	    ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) {
8632179193Sjb		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8633179193Sjb		    "provider ops", name);
8634179193Sjb		return (EINVAL);
8635179193Sjb	}
8636179193Sjb
8637179193Sjb	if (dtrace_badattr(&pap->dtpa_provider) ||
8638179193Sjb	    dtrace_badattr(&pap->dtpa_mod) ||
8639179193Sjb	    dtrace_badattr(&pap->dtpa_func) ||
8640179193Sjb	    dtrace_badattr(&pap->dtpa_name) ||
8641179193Sjb	    dtrace_badattr(&pap->dtpa_args)) {
8642179193Sjb		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8643179193Sjb		    "provider attributes", name);
8644179193Sjb		return (EINVAL);
8645179193Sjb	}
8646179193Sjb
8647179193Sjb	if (priv & ~DTRACE_PRIV_ALL) {
8648179193Sjb		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
8649179193Sjb		    "privilege attributes", name);
8650179193Sjb		return (EINVAL);
8651179193Sjb	}
8652179193Sjb
8653179193Sjb	if ((priv & DTRACE_PRIV_KERNEL) &&
8654179193Sjb	    (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) &&
8655179193Sjb	    pops->dtps_usermode == NULL) {
8656179193Sjb		cmn_err(CE_WARN, "failed to register provider '%s': need "
8657179193Sjb		    "dtps_usermode() op for given privilege attributes", name);
8658179193Sjb		return (EINVAL);
8659179193Sjb	}
8660179193Sjb
8661179193Sjb	provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP);
8662179193Sjb	provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
8663179193Sjb	(void) strcpy(provider->dtpv_name, name);
8664179193Sjb
8665179193Sjb	provider->dtpv_attr = *pap;
8666179193Sjb	provider->dtpv_priv.dtpp_flags = priv;
8667179193Sjb	if (cr != NULL) {
8668179193Sjb		provider->dtpv_priv.dtpp_uid = crgetuid(cr);
8669179193Sjb		provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr);
8670179193Sjb	}
8671179193Sjb	provider->dtpv_pops = *pops;
8672179193Sjb
8673179193Sjb	if (pops->dtps_provide == NULL) {
8674179193Sjb		ASSERT(pops->dtps_provide_module != NULL);
8675179193Sjb		provider->dtpv_pops.dtps_provide =
8676179198Sjb		    (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop;
8677179193Sjb	}
8678179193Sjb
8679179193Sjb	if (pops->dtps_provide_module == NULL) {
8680179193Sjb		ASSERT(pops->dtps_provide != NULL);
8681179193Sjb		provider->dtpv_pops.dtps_provide_module =
8682179198Sjb		    (void (*)(void *, modctl_t *))dtrace_nullop;
8683179193Sjb	}
8684179193Sjb
8685179193Sjb	if (pops->dtps_suspend == NULL) {
8686179193Sjb		ASSERT(pops->dtps_resume == NULL);
8687179193Sjb		provider->dtpv_pops.dtps_suspend =
8688179193Sjb		    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
8689179193Sjb		provider->dtpv_pops.dtps_resume =
8690179193Sjb		    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
8691179193Sjb	}
8692179193Sjb
8693179193Sjb	provider->dtpv_arg = arg;
8694179193Sjb	*idp = (dtrace_provider_id_t)provider;
8695179193Sjb
8696179193Sjb	if (pops == &dtrace_provider_ops) {
8697179193Sjb		ASSERT(MUTEX_HELD(&dtrace_provider_lock));
8698179193Sjb		ASSERT(MUTEX_HELD(&dtrace_lock));
8699179193Sjb		ASSERT(dtrace_anon.dta_enabling == NULL);
8700179193Sjb
8701179193Sjb		/*
8702179193Sjb		 * We make sure that the DTrace provider is at the head of
8703179193Sjb		 * the provider chain.
8704179193Sjb		 */
8705179193Sjb		provider->dtpv_next = dtrace_provider;
8706179193Sjb		dtrace_provider = provider;
8707179193Sjb		return (0);
8708179193Sjb	}
8709179193Sjb
8710179193Sjb	mutex_enter(&dtrace_provider_lock);
8711179193Sjb	mutex_enter(&dtrace_lock);
8712179193Sjb
8713179193Sjb	/*
8714179193Sjb	 * If there is at least one provider registered, we'll add this
8715179193Sjb	 * provider after the first provider.
8716179193Sjb	 */
8717179193Sjb	if (dtrace_provider != NULL) {
8718179193Sjb		provider->dtpv_next = dtrace_provider->dtpv_next;
8719179193Sjb		dtrace_provider->dtpv_next = provider;
8720179193Sjb	} else {
8721179193Sjb		dtrace_provider = provider;
8722179193Sjb	}
8723179193Sjb
8724179193Sjb	if (dtrace_retained != NULL) {
8725179193Sjb		dtrace_enabling_provide(provider);
8726179193Sjb
8727179193Sjb		/*
8728179193Sjb		 * Now we need to call dtrace_enabling_matchall() -- which
8729179193Sjb		 * will acquire cpu_lock and dtrace_lock.  We therefore need
8730179193Sjb		 * to drop all of our locks before calling into it...
8731179193Sjb		 */
8732179193Sjb		mutex_exit(&dtrace_lock);
8733179193Sjb		mutex_exit(&dtrace_provider_lock);
8734179193Sjb		dtrace_enabling_matchall();
8735179193Sjb
8736179193Sjb		return (0);
8737179193Sjb	}
8738179193Sjb
8739179193Sjb	mutex_exit(&dtrace_lock);
8740179193Sjb	mutex_exit(&dtrace_provider_lock);
8741179193Sjb
8742179193Sjb	return (0);
8743179193Sjb}
8744179193Sjb
8745179193Sjb/*
8746179193Sjb * Unregister the specified provider from the DTrace framework.  This should
8747179193Sjb * generally be called by DTrace providers in their detach(9E) entry point.
8748179193Sjb */
8749179193Sjbint
8750179193Sjbdtrace_unregister(dtrace_provider_id_t id)
8751179193Sjb{
8752179193Sjb	dtrace_provider_t *old = (dtrace_provider_t *)id;
8753179193Sjb	dtrace_provider_t *prev = NULL;
8754248983Spfg	int i, self = 0, noreap = 0;
8755179193Sjb	dtrace_probe_t *probe, *first = NULL;
8756179193Sjb
8757179193Sjb	if (old->dtpv_pops.dtps_enable ==
8758179193Sjb	    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) {
8759179193Sjb		/*
8760179193Sjb		 * If DTrace itself is the provider, we're called with locks
8761179193Sjb		 * already held.
8762179193Sjb		 */
8763179193Sjb		ASSERT(old == dtrace_provider);
8764277300Ssmh#ifdef illumos
8765179193Sjb		ASSERT(dtrace_devi != NULL);
8766179198Sjb#endif
8767179193Sjb		ASSERT(MUTEX_HELD(&dtrace_provider_lock));
8768179193Sjb		ASSERT(MUTEX_HELD(&dtrace_lock));
8769179193Sjb		self = 1;
8770179193Sjb
8771179193Sjb		if (dtrace_provider->dtpv_next != NULL) {
8772179193Sjb			/*
8773179193Sjb			 * There's another provider here; return failure.
8774179193Sjb			 */
8775179193Sjb			return (EBUSY);
8776179193Sjb		}
8777179193Sjb	} else {
8778179193Sjb		mutex_enter(&dtrace_provider_lock);
8779277300Ssmh#ifdef illumos
8780179193Sjb		mutex_enter(&mod_lock);
8781252850Smarkj#endif
8782179193Sjb		mutex_enter(&dtrace_lock);
8783179193Sjb	}
8784179193Sjb
8785179193Sjb	/*
8786179193Sjb	 * If anyone has /dev/dtrace open, or if there are anonymous enabled
8787179193Sjb	 * probes, we refuse to let providers slither away, unless this
8788179193Sjb	 * provider has already been explicitly invalidated.
8789179193Sjb	 */
8790179193Sjb	if (!old->dtpv_defunct &&
8791179193Sjb	    (dtrace_opens || (dtrace_anon.dta_state != NULL &&
8792179193Sjb	    dtrace_anon.dta_state->dts_necbs > 0))) {
8793179193Sjb		if (!self) {
8794179193Sjb			mutex_exit(&dtrace_lock);
8795277300Ssmh#ifdef illumos
8796179193Sjb			mutex_exit(&mod_lock);
8797252850Smarkj#endif
8798179193Sjb			mutex_exit(&dtrace_provider_lock);
8799179193Sjb		}
8800179193Sjb		return (EBUSY);
8801179193Sjb	}
8802179193Sjb
8803179193Sjb	/*
8804179193Sjb	 * Attempt to destroy the probes associated with this provider.
8805179193Sjb	 */
8806179193Sjb	for (i = 0; i < dtrace_nprobes; i++) {
8807179193Sjb		if ((probe = dtrace_probes[i]) == NULL)
8808179193Sjb			continue;
8809179193Sjb
8810179193Sjb		if (probe->dtpr_provider != old)
8811179193Sjb			continue;
8812179193Sjb
8813179193Sjb		if (probe->dtpr_ecb == NULL)
8814179193Sjb			continue;
8815179193Sjb
8816179193Sjb		/*
8817248983Spfg		 * If we are trying to unregister a defunct provider, and the
8818248983Spfg		 * provider was made defunct within the interval dictated by
8819248983Spfg		 * dtrace_unregister_defunct_reap, we'll (asynchronously)
8820248983Spfg		 * attempt to reap our enablings.  To denote that the provider
8821248983Spfg		 * should reattempt to unregister itself at some point in the
8822248983Spfg		 * future, we will return a differentiable error code (EAGAIN
8823248983Spfg		 * instead of EBUSY) in this case.
8824179193Sjb		 */
8825248983Spfg		if (dtrace_gethrtime() - old->dtpv_defunct >
8826248983Spfg		    dtrace_unregister_defunct_reap)
8827248983Spfg			noreap = 1;
8828248983Spfg
8829179193Sjb		if (!self) {
8830179193Sjb			mutex_exit(&dtrace_lock);
8831277300Ssmh#ifdef illumos
8832179193Sjb			mutex_exit(&mod_lock);
8833252850Smarkj#endif
8834179193Sjb			mutex_exit(&dtrace_provider_lock);
8835179193Sjb		}
8836248983Spfg
8837248983Spfg		if (noreap)
8838248983Spfg			return (EBUSY);
8839248983Spfg
8840248983Spfg		(void) taskq_dispatch(dtrace_taskq,
8841248983Spfg		    (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP);
8842248983Spfg
8843248983Spfg		return (EAGAIN);
8844179193Sjb	}
8845179193Sjb
8846179193Sjb	/*
8847179193Sjb	 * All of the probes for this provider are disabled; we can safely
8848179193Sjb	 * remove all of them from their hash chains and from the probe array.
8849179193Sjb	 */
8850179193Sjb	for (i = 0; i < dtrace_nprobes; i++) {
8851179193Sjb		if ((probe = dtrace_probes[i]) == NULL)
8852179193Sjb			continue;
8853179193Sjb
8854179193Sjb		if (probe->dtpr_provider != old)
8855179193Sjb			continue;
8856179193Sjb
8857179193Sjb		dtrace_probes[i] = NULL;
8858179193Sjb
8859179193Sjb		dtrace_hash_remove(dtrace_bymod, probe);
8860179193Sjb		dtrace_hash_remove(dtrace_byfunc, probe);
8861179193Sjb		dtrace_hash_remove(dtrace_byname, probe);
8862179193Sjb
8863179193Sjb		if (first == NULL) {
8864179193Sjb			first = probe;
8865179193Sjb			probe->dtpr_nextmod = NULL;
8866179193Sjb		} else {
8867179193Sjb			probe->dtpr_nextmod = first;
8868179193Sjb			first = probe;
8869179193Sjb		}
8870179193Sjb	}
8871179193Sjb
8872179193Sjb	/*
8873179193Sjb	 * The provider's probes have been removed from the hash chains and
8874179193Sjb	 * from the probe array.  Now issue a dtrace_sync() to be sure that
8875179193Sjb	 * everyone has cleared out from any probe array processing.
8876179193Sjb	 */
8877179193Sjb	dtrace_sync();
8878179193Sjb
8879179193Sjb	for (probe = first; probe != NULL; probe = first) {
8880179193Sjb		first = probe->dtpr_nextmod;
8881179193Sjb
8882179193Sjb		old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id,
8883179193Sjb		    probe->dtpr_arg);
8884179193Sjb		kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
8885179193Sjb		kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
8886179193Sjb		kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
8887277300Ssmh#ifdef illumos
8888179193Sjb		vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1);
8889179198Sjb#else
8890260131Smarkj		free_unr(dtrace_arena, probe->dtpr_id);
8891179198Sjb#endif
8892179193Sjb		kmem_free(probe, sizeof (dtrace_probe_t));
8893179193Sjb	}
8894179193Sjb
8895179193Sjb	if ((prev = dtrace_provider) == old) {
8896277300Ssmh#ifdef illumos
8897179193Sjb		ASSERT(self || dtrace_devi == NULL);
8898179193Sjb		ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL);
8899179198Sjb#endif
8900179193Sjb		dtrace_provider = old->dtpv_next;
8901179193Sjb	} else {
8902179193Sjb		while (prev != NULL && prev->dtpv_next != old)
8903179193Sjb			prev = prev->dtpv_next;
8904179193Sjb
8905179193Sjb		if (prev == NULL) {
8906179193Sjb			panic("attempt to unregister non-existent "
8907179193Sjb			    "dtrace provider %p\n", (void *)id);
8908179193Sjb		}
8909179193Sjb
8910179193Sjb		prev->dtpv_next = old->dtpv_next;
8911179193Sjb	}
8912179193Sjb
8913179193Sjb	if (!self) {
8914179193Sjb		mutex_exit(&dtrace_lock);
8915277300Ssmh#ifdef illumos
8916179193Sjb		mutex_exit(&mod_lock);
8917252850Smarkj#endif
8918179193Sjb		mutex_exit(&dtrace_provider_lock);
8919179193Sjb	}
8920179193Sjb
8921179193Sjb	kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1);
8922179193Sjb	kmem_free(old, sizeof (dtrace_provider_t));
8923179193Sjb
8924179193Sjb	return (0);
8925179193Sjb}
8926179193Sjb
8927179193Sjb/*
8928179193Sjb * Invalidate the specified provider.  All subsequent probe lookups for the
8929179193Sjb * specified provider will fail, but its probes will not be removed.
8930179193Sjb */
8931179193Sjbvoid
8932179193Sjbdtrace_invalidate(dtrace_provider_id_t id)
8933179193Sjb{
8934179193Sjb	dtrace_provider_t *pvp = (dtrace_provider_t *)id;
8935179193Sjb
8936179193Sjb	ASSERT(pvp->dtpv_pops.dtps_enable !=
8937179193Sjb	    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop);
8938179193Sjb
8939179193Sjb	mutex_enter(&dtrace_provider_lock);
8940179193Sjb	mutex_enter(&dtrace_lock);
8941179193Sjb
8942248983Spfg	pvp->dtpv_defunct = dtrace_gethrtime();
8943179193Sjb
8944179193Sjb	mutex_exit(&dtrace_lock);
8945179193Sjb	mutex_exit(&dtrace_provider_lock);
8946179193Sjb}
8947179193Sjb
8948179193Sjb/*
8949179193Sjb * Indicate whether or not DTrace has attached.
8950179193Sjb */
8951179193Sjbint
8952179193Sjbdtrace_attached(void)
8953179193Sjb{
8954179193Sjb	/*
8955179193Sjb	 * dtrace_provider will be non-NULL iff the DTrace driver has
8956179193Sjb	 * attached.  (It's non-NULL because DTrace is always itself a
8957179193Sjb	 * provider.)
8958179193Sjb	 */
8959179193Sjb	return (dtrace_provider != NULL);
8960179193Sjb}
8961179193Sjb
8962179193Sjb/*
8963179193Sjb * Remove all the unenabled probes for the given provider.  This function is
8964179193Sjb * not unlike dtrace_unregister(), except that it doesn't remove the provider
8965179193Sjb * -- just as many of its associated probes as it can.
8966179193Sjb */
8967179193Sjbint
8968179193Sjbdtrace_condense(dtrace_provider_id_t id)
8969179193Sjb{
8970179193Sjb	dtrace_provider_t *prov = (dtrace_provider_t *)id;
8971179193Sjb	int i;
8972179193Sjb	dtrace_probe_t *probe;
8973179193Sjb
8974179193Sjb	/*
8975179193Sjb	 * Make sure this isn't the dtrace provider itself.
8976179193Sjb	 */
8977179193Sjb	ASSERT(prov->dtpv_pops.dtps_enable !=
8978179193Sjb	    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop);
8979179193Sjb
8980179193Sjb	mutex_enter(&dtrace_provider_lock);
8981179193Sjb	mutex_enter(&dtrace_lock);
8982179193Sjb
8983179193Sjb	/*
8984179193Sjb	 * Attempt to destroy the probes associated with this provider.
8985179193Sjb	 */
8986179193Sjb	for (i = 0; i < dtrace_nprobes; i++) {
8987179193Sjb		if ((probe = dtrace_probes[i]) == NULL)
8988179193Sjb			continue;
8989179193Sjb
8990179193Sjb		if (probe->dtpr_provider != prov)
8991179193Sjb			continue;
8992179193Sjb
8993179193Sjb		if (probe->dtpr_ecb != NULL)
8994179193Sjb			continue;
8995179193Sjb
8996179193Sjb		dtrace_probes[i] = NULL;
8997179193Sjb
8998179193Sjb		dtrace_hash_remove(dtrace_bymod, probe);
8999179193Sjb		dtrace_hash_remove(dtrace_byfunc, probe);
9000179193Sjb		dtrace_hash_remove(dtrace_byname, probe);
9001179193Sjb
9002179193Sjb		prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1,
9003179193Sjb		    probe->dtpr_arg);
9004179193Sjb		kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
9005179193Sjb		kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
9006179193Sjb		kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
9007179193Sjb		kmem_free(probe, sizeof (dtrace_probe_t));
9008277300Ssmh#ifdef illumos
9009179193Sjb		vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1);
9010179198Sjb#else
9011260131Smarkj		free_unr(dtrace_arena, i + 1);
9012179198Sjb#endif
9013179193Sjb	}
9014179193Sjb
9015179193Sjb	mutex_exit(&dtrace_lock);
9016179193Sjb	mutex_exit(&dtrace_provider_lock);
9017179193Sjb
9018179193Sjb	return (0);
9019179193Sjb}
9020179193Sjb
9021179193Sjb/*
9022179193Sjb * DTrace Probe Management Functions
9023179193Sjb *
9024179193Sjb * The functions in this section perform the DTrace probe management,
9025179193Sjb * including functions to create probes, look-up probes, and call into the
9026179193Sjb * providers to request that probes be provided.  Some of these functions are
9027179193Sjb * in the Provider-to-Framework API; these functions can be identified by the
9028179193Sjb * fact that they are not declared "static".
9029179193Sjb */
9030179193Sjb
9031179193Sjb/*
9032179193Sjb * Create a probe with the specified module name, function name, and name.
9033179193Sjb */
9034179193Sjbdtrace_id_t
9035179193Sjbdtrace_probe_create(dtrace_provider_id_t prov, const char *mod,
9036179193Sjb    const char *func, const char *name, int aframes, void *arg)
9037179193Sjb{
9038179193Sjb	dtrace_probe_t *probe, **probes;
9039179193Sjb	dtrace_provider_t *provider = (dtrace_provider_t *)prov;
9040179193Sjb	dtrace_id_t id;
9041179193Sjb
9042179193Sjb	if (provider == dtrace_provider) {
9043179193Sjb		ASSERT(MUTEX_HELD(&dtrace_lock));
9044179193Sjb	} else {
9045179193Sjb		mutex_enter(&dtrace_lock);
9046179193Sjb	}
9047179193Sjb
9048277300Ssmh#ifdef illumos
9049179193Sjb	id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1,
9050260131Smarkj	    VM_BESTFIT | VM_SLEEP);
9051179198Sjb#else
9052260131Smarkj	id = alloc_unr(dtrace_arena);
9053179198Sjb#endif
9054179193Sjb	probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP);
9055179193Sjb
9056179193Sjb	probe->dtpr_id = id;
9057179193Sjb	probe->dtpr_gen = dtrace_probegen++;
9058179193Sjb	probe->dtpr_mod = dtrace_strdup(mod);
9059179193Sjb	probe->dtpr_func = dtrace_strdup(func);
9060179193Sjb	probe->dtpr_name = dtrace_strdup(name);
9061179193Sjb	probe->dtpr_arg = arg;
9062179193Sjb	probe->dtpr_aframes = aframes;
9063179193Sjb	probe->dtpr_provider = provider;
9064179193Sjb
9065179193Sjb	dtrace_hash_add(dtrace_bymod, probe);
9066179193Sjb	dtrace_hash_add(dtrace_byfunc, probe);
9067179193Sjb	dtrace_hash_add(dtrace_byname, probe);
9068179193Sjb
9069179193Sjb	if (id - 1 >= dtrace_nprobes) {
9070179193Sjb		size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *);
9071179193Sjb		size_t nsize = osize << 1;
9072179193Sjb
9073179193Sjb		if (nsize == 0) {
9074179193Sjb			ASSERT(osize == 0);
9075179193Sjb			ASSERT(dtrace_probes == NULL);
9076179193Sjb			nsize = sizeof (dtrace_probe_t *);
9077179193Sjb		}
9078179193Sjb
9079179193Sjb		probes = kmem_zalloc(nsize, KM_SLEEP);
9080179193Sjb
9081179193Sjb		if (dtrace_probes == NULL) {
9082179193Sjb			ASSERT(osize == 0);
9083179193Sjb			dtrace_probes = probes;
9084179193Sjb			dtrace_nprobes = 1;
9085179193Sjb		} else {
9086179193Sjb			dtrace_probe_t **oprobes = dtrace_probes;
9087179193Sjb
9088179193Sjb			bcopy(oprobes, probes, osize);
9089179193Sjb			dtrace_membar_producer();
9090179193Sjb			dtrace_probes = probes;
9091179193Sjb
9092179193Sjb			dtrace_sync();
9093179193Sjb
9094179193Sjb			/*
9095179193Sjb			 * All CPUs are now seeing the new probes array; we can
9096179193Sjb			 * safely free the old array.
9097179193Sjb			 */
9098179193Sjb			kmem_free(oprobes, osize);
9099179193Sjb			dtrace_nprobes <<= 1;
9100179193Sjb		}
9101179193Sjb
9102179193Sjb		ASSERT(id - 1 < dtrace_nprobes);
9103179193Sjb	}
9104179193Sjb
9105179193Sjb	ASSERT(dtrace_probes[id - 1] == NULL);
9106179193Sjb	dtrace_probes[id - 1] = probe;
9107179193Sjb
9108179193Sjb	if (provider != dtrace_provider)
9109179193Sjb		mutex_exit(&dtrace_lock);
9110179193Sjb
9111179193Sjb	return (id);
9112179193Sjb}
9113179193Sjb
9114179193Sjbstatic dtrace_probe_t *
9115179193Sjbdtrace_probe_lookup_id(dtrace_id_t id)
9116179193Sjb{
9117179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
9118179193Sjb
9119179193Sjb	if (id == 0 || id > dtrace_nprobes)
9120179193Sjb		return (NULL);
9121179193Sjb
9122179193Sjb	return (dtrace_probes[id - 1]);
9123179193Sjb}
9124179193Sjb
9125179193Sjbstatic int
9126179193Sjbdtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg)
9127179193Sjb{
9128179193Sjb	*((dtrace_id_t *)arg) = probe->dtpr_id;
9129179193Sjb
9130179193Sjb	return (DTRACE_MATCH_DONE);
9131179193Sjb}
9132179193Sjb
9133179193Sjb/*
9134179193Sjb * Look up a probe based on provider and one or more of module name, function
9135179193Sjb * name and probe name.
9136179193Sjb */
9137179193Sjbdtrace_id_t
9138179198Sjbdtrace_probe_lookup(dtrace_provider_id_t prid, char *mod,
9139179198Sjb    char *func, char *name)
9140179193Sjb{
9141179193Sjb	dtrace_probekey_t pkey;
9142179193Sjb	dtrace_id_t id;
9143179193Sjb	int match;
9144179193Sjb
9145179193Sjb	pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name;
9146179193Sjb	pkey.dtpk_pmatch = &dtrace_match_string;
9147179193Sjb	pkey.dtpk_mod = mod;
9148179193Sjb	pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul;
9149179193Sjb	pkey.dtpk_func = func;
9150179193Sjb	pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul;
9151179193Sjb	pkey.dtpk_name = name;
9152179193Sjb	pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul;
9153179193Sjb	pkey.dtpk_id = DTRACE_IDNONE;
9154179193Sjb
9155179193Sjb	mutex_enter(&dtrace_lock);
9156179193Sjb	match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0,
9157179193Sjb	    dtrace_probe_lookup_match, &id);
9158179193Sjb	mutex_exit(&dtrace_lock);
9159179193Sjb
9160179193Sjb	ASSERT(match == 1 || match == 0);
9161179193Sjb	return (match ? id : 0);
9162179193Sjb}
9163179193Sjb
9164179193Sjb/*
9165179193Sjb * Returns the probe argument associated with the specified probe.
9166179193Sjb */
9167179193Sjbvoid *
9168179193Sjbdtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid)
9169179193Sjb{
9170179193Sjb	dtrace_probe_t *probe;
9171179193Sjb	void *rval = NULL;
9172179193Sjb
9173179193Sjb	mutex_enter(&dtrace_lock);
9174179193Sjb
9175179193Sjb	if ((probe = dtrace_probe_lookup_id(pid)) != NULL &&
9176179193Sjb	    probe->dtpr_provider == (dtrace_provider_t *)id)
9177179193Sjb		rval = probe->dtpr_arg;
9178179193Sjb
9179179193Sjb	mutex_exit(&dtrace_lock);
9180179193Sjb
9181179193Sjb	return (rval);
9182179193Sjb}
9183179193Sjb
9184179193Sjb/*
9185179193Sjb * Copy a probe into a probe description.
9186179193Sjb */
9187179193Sjbstatic void
9188179193Sjbdtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp)
9189179193Sjb{
9190179193Sjb	bzero(pdp, sizeof (dtrace_probedesc_t));
9191179193Sjb	pdp->dtpd_id = prp->dtpr_id;
9192179193Sjb
9193179193Sjb	(void) strncpy(pdp->dtpd_provider,
9194179193Sjb	    prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1);
9195179193Sjb
9196179193Sjb	(void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1);
9197179193Sjb	(void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1);
9198179193Sjb	(void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1);
9199179193Sjb}
9200179193Sjb
9201179193Sjb/*
9202179193Sjb * Called to indicate that a probe -- or probes -- should be provided by a
9203179193Sjb * specfied provider.  If the specified description is NULL, the provider will
9204179193Sjb * be told to provide all of its probes.  (This is done whenever a new
9205179193Sjb * consumer comes along, or whenever a retained enabling is to be matched.) If
9206179193Sjb * the specified description is non-NULL, the provider is given the
9207179193Sjb * opportunity to dynamically provide the specified probe, allowing providers
9208179193Sjb * to support the creation of probes on-the-fly.  (So-called _autocreated_
9209179193Sjb * probes.)  If the provider is NULL, the operations will be applied to all
9210179193Sjb * providers; if the provider is non-NULL the operations will only be applied
9211179193Sjb * to the specified provider.  The dtrace_provider_lock must be held, and the
9212179193Sjb * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation
9213179193Sjb * will need to grab the dtrace_lock when it reenters the framework through
9214179193Sjb * dtrace_probe_lookup(), dtrace_probe_create(), etc.
9215179193Sjb */
9216179193Sjbstatic void
9217179193Sjbdtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv)
9218179193Sjb{
9219277300Ssmh#ifdef illumos
9220179198Sjb	modctl_t *ctl;
9221179198Sjb#endif
9222179193Sjb	int all = 0;
9223179193Sjb
9224179193Sjb	ASSERT(MUTEX_HELD(&dtrace_provider_lock));
9225179193Sjb
9226179193Sjb	if (prv == NULL) {
9227179193Sjb		all = 1;
9228179193Sjb		prv = dtrace_provider;
9229179193Sjb	}
9230179193Sjb
9231179193Sjb	do {
9232179193Sjb		/*
9233179193Sjb		 * First, call the blanket provide operation.
9234179193Sjb		 */
9235179193Sjb		prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc);
9236179193Sjb
9237277300Ssmh#ifdef illumos
9238179193Sjb		/*
9239179193Sjb		 * Now call the per-module provide operation.  We will grab
9240179193Sjb		 * mod_lock to prevent the list from being modified.  Note
9241179193Sjb		 * that this also prevents the mod_busy bits from changing.
9242179193Sjb		 * (mod_busy can only be changed with mod_lock held.)
9243179193Sjb		 */
9244179193Sjb		mutex_enter(&mod_lock);
9245179193Sjb
9246179193Sjb		ctl = &modules;
9247179193Sjb		do {
9248179193Sjb			if (ctl->mod_busy || ctl->mod_mp == NULL)
9249179193Sjb				continue;
9250179193Sjb
9251179193Sjb			prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
9252179193Sjb
9253179193Sjb		} while ((ctl = ctl->mod_next) != &modules);
9254252850Smarkj
9255252850Smarkj		mutex_exit(&mod_lock);
9256179198Sjb#endif
9257179193Sjb	} while (all && (prv = prv->dtpv_next) != NULL);
9258179193Sjb}
9259179193Sjb
9260277300Ssmh#ifdef illumos
9261179193Sjb/*
9262179193Sjb * Iterate over each probe, and call the Framework-to-Provider API function
9263179193Sjb * denoted by offs.
9264179193Sjb */
9265179193Sjbstatic void
9266179193Sjbdtrace_probe_foreach(uintptr_t offs)
9267179193Sjb{
9268179193Sjb	dtrace_provider_t *prov;
9269179193Sjb	void (*func)(void *, dtrace_id_t, void *);
9270179193Sjb	dtrace_probe_t *probe;
9271179193Sjb	dtrace_icookie_t cookie;
9272179193Sjb	int i;
9273179193Sjb
9274179193Sjb	/*
9275179193Sjb	 * We disable interrupts to walk through the probe array.  This is
9276179193Sjb	 * safe -- the dtrace_sync() in dtrace_unregister() assures that we
9277179193Sjb	 * won't see stale data.
9278179193Sjb	 */
9279179193Sjb	cookie = dtrace_interrupt_disable();
9280179193Sjb
9281179193Sjb	for (i = 0; i < dtrace_nprobes; i++) {
9282179193Sjb		if ((probe = dtrace_probes[i]) == NULL)
9283179193Sjb			continue;
9284179193Sjb
9285179193Sjb		if (probe->dtpr_ecb == NULL) {
9286179193Sjb			/*
9287179193Sjb			 * This probe isn't enabled -- don't call the function.
9288179193Sjb			 */
9289179193Sjb			continue;
9290179193Sjb		}
9291179193Sjb
9292179193Sjb		prov = probe->dtpr_provider;
9293179193Sjb		func = *((void(**)(void *, dtrace_id_t, void *))
9294179193Sjb		    ((uintptr_t)&prov->dtpv_pops + offs));
9295179193Sjb
9296179193Sjb		func(prov->dtpv_arg, i + 1, probe->dtpr_arg);
9297179193Sjb	}
9298179193Sjb
9299179193Sjb	dtrace_interrupt_enable(cookie);
9300179193Sjb}
9301179198Sjb#endif
9302179193Sjb
9303179193Sjbstatic int
9304179198Sjbdtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab)
9305179193Sjb{
9306179193Sjb	dtrace_probekey_t pkey;
9307179193Sjb	uint32_t priv;
9308179193Sjb	uid_t uid;
9309179193Sjb	zoneid_t zoneid;
9310179193Sjb
9311179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
9312179193Sjb	dtrace_ecb_create_cache = NULL;
9313179193Sjb
9314179193Sjb	if (desc == NULL) {
9315179193Sjb		/*
9316179193Sjb		 * If we're passed a NULL description, we're being asked to
9317179193Sjb		 * create an ECB with a NULL probe.
9318179193Sjb		 */
9319179193Sjb		(void) dtrace_ecb_create_enable(NULL, enab);
9320179193Sjb		return (0);
9321179193Sjb	}
9322179193Sjb
9323179193Sjb	dtrace_probekey(desc, &pkey);
9324179193Sjb	dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred,
9325179193Sjb	    &priv, &uid, &zoneid);
9326179193Sjb
9327179193Sjb	return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable,
9328179193Sjb	    enab));
9329179193Sjb}
9330179193Sjb
9331179193Sjb/*
9332179193Sjb * DTrace Helper Provider Functions
9333179193Sjb */
9334179193Sjbstatic void
9335179193Sjbdtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr)
9336179193Sjb{
9337179193Sjb	attr->dtat_name = DOF_ATTR_NAME(dofattr);
9338179193Sjb	attr->dtat_data = DOF_ATTR_DATA(dofattr);
9339179193Sjb	attr->dtat_class = DOF_ATTR_CLASS(dofattr);
9340179193Sjb}
9341179193Sjb
9342179193Sjbstatic void
9343179193Sjbdtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov,
9344179193Sjb    const dof_provider_t *dofprov, char *strtab)
9345179193Sjb{
9346179193Sjb	hprov->dthpv_provname = strtab + dofprov->dofpv_name;
9347179193Sjb	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider,
9348179193Sjb	    dofprov->dofpv_provattr);
9349179193Sjb	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod,
9350179193Sjb	    dofprov->dofpv_modattr);
9351179193Sjb	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func,
9352179193Sjb	    dofprov->dofpv_funcattr);
9353179193Sjb	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name,
9354179193Sjb	    dofprov->dofpv_nameattr);
9355179193Sjb	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args,
9356179193Sjb	    dofprov->dofpv_argsattr);
9357179193Sjb}
9358179193Sjb
9359179193Sjbstatic void
9360179193Sjbdtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
9361179193Sjb{
9362179193Sjb	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9363179193Sjb	dof_hdr_t *dof = (dof_hdr_t *)daddr;
9364179193Sjb	dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
9365179193Sjb	dof_provider_t *provider;
9366179193Sjb	dof_probe_t *probe;
9367179193Sjb	uint32_t *off, *enoff;
9368179193Sjb	uint8_t *arg;
9369179193Sjb	char *strtab;
9370179193Sjb	uint_t i, nprobes;
9371179193Sjb	dtrace_helper_provdesc_t dhpv;
9372179193Sjb	dtrace_helper_probedesc_t dhpb;
9373179193Sjb	dtrace_meta_t *meta = dtrace_meta_pid;
9374179193Sjb	dtrace_mops_t *mops = &meta->dtm_mops;
9375179193Sjb	void *parg;
9376179193Sjb
9377179193Sjb	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
9378179193Sjb	str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9379179193Sjb	    provider->dofpv_strtab * dof->dofh_secsize);
9380179193Sjb	prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9381179193Sjb	    provider->dofpv_probes * dof->dofh_secsize);
9382179193Sjb	arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9383179193Sjb	    provider->dofpv_prargs * dof->dofh_secsize);
9384179193Sjb	off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9385179193Sjb	    provider->dofpv_proffs * dof->dofh_secsize);
9386179193Sjb
9387179193Sjb	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
9388179193Sjb	off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset);
9389179193Sjb	arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
9390179193Sjb	enoff = NULL;
9391179193Sjb
9392179193Sjb	/*
9393179193Sjb	 * See dtrace_helper_provider_validate().
9394179193Sjb	 */
9395179193Sjb	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
9396179193Sjb	    provider->dofpv_prenoffs != DOF_SECT_NONE) {
9397179193Sjb		enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9398179193Sjb		    provider->dofpv_prenoffs * dof->dofh_secsize);
9399179193Sjb		enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset);
9400179193Sjb	}
9401179193Sjb
9402179193Sjb	nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
9403179193Sjb
9404179193Sjb	/*
9405179193Sjb	 * Create the provider.
9406179193Sjb	 */
9407179193Sjb	dtrace_dofprov2hprov(&dhpv, provider, strtab);
9408179193Sjb
9409179193Sjb	if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL)
9410179193Sjb		return;
9411179193Sjb
9412179193Sjb	meta->dtm_count++;
9413179193Sjb
9414179193Sjb	/*
9415179193Sjb	 * Create the probes.
9416179193Sjb	 */
9417179193Sjb	for (i = 0; i < nprobes; i++) {
9418179193Sjb		probe = (dof_probe_t *)(uintptr_t)(daddr +
9419179193Sjb		    prb_sec->dofs_offset + i * prb_sec->dofs_entsize);
9420179193Sjb
9421298589Smarkj		/* See the check in dtrace_helper_provider_validate(). */
9422298589Smarkj		if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN)
9423298589Smarkj			continue;
9424298589Smarkj
9425179193Sjb		dhpb.dthpb_mod = dhp->dofhp_mod;
9426179193Sjb		dhpb.dthpb_func = strtab + probe->dofpr_func;
9427179193Sjb		dhpb.dthpb_name = strtab + probe->dofpr_name;
9428179193Sjb		dhpb.dthpb_base = probe->dofpr_addr;
9429179193Sjb		dhpb.dthpb_offs = off + probe->dofpr_offidx;
9430179193Sjb		dhpb.dthpb_noffs = probe->dofpr_noffs;
9431179193Sjb		if (enoff != NULL) {
9432179193Sjb			dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx;
9433179193Sjb			dhpb.dthpb_nenoffs = probe->dofpr_nenoffs;
9434179193Sjb		} else {
9435179193Sjb			dhpb.dthpb_enoffs = NULL;
9436179193Sjb			dhpb.dthpb_nenoffs = 0;
9437179193Sjb		}
9438179193Sjb		dhpb.dthpb_args = arg + probe->dofpr_argidx;
9439179193Sjb		dhpb.dthpb_nargc = probe->dofpr_nargc;
9440179193Sjb		dhpb.dthpb_xargc = probe->dofpr_xargc;
9441179193Sjb		dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv;
9442179193Sjb		dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv;
9443179193Sjb
9444179193Sjb		mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb);
9445179193Sjb	}
9446179193Sjb}
9447179193Sjb
9448179193Sjbstatic void
9449179193Sjbdtrace_helper_provide(dof_helper_t *dhp, pid_t pid)
9450179193Sjb{
9451179193Sjb	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9452179193Sjb	dof_hdr_t *dof = (dof_hdr_t *)daddr;
9453179193Sjb	int i;
9454179193Sjb
9455179193Sjb	ASSERT(MUTEX_HELD(&dtrace_meta_lock));
9456179193Sjb
9457179193Sjb	for (i = 0; i < dof->dofh_secnum; i++) {
9458179193Sjb		dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
9459179193Sjb		    dof->dofh_secoff + i * dof->dofh_secsize);
9460179193Sjb
9461179193Sjb		if (sec->dofs_type != DOF_SECT_PROVIDER)
9462179193Sjb			continue;
9463179193Sjb
9464179193Sjb		dtrace_helper_provide_one(dhp, sec, pid);
9465179193Sjb	}
9466179193Sjb
9467179193Sjb	/*
9468179193Sjb	 * We may have just created probes, so we must now rematch against
9469179193Sjb	 * any retained enablings.  Note that this call will acquire both
9470179193Sjb	 * cpu_lock and dtrace_lock; the fact that we are holding
9471179193Sjb	 * dtrace_meta_lock now is what defines the ordering with respect to
9472179193Sjb	 * these three locks.
9473179193Sjb	 */
9474179193Sjb	dtrace_enabling_matchall();
9475179193Sjb}
9476179193Sjb
9477179193Sjbstatic void
9478179193Sjbdtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
9479179193Sjb{
9480179193Sjb	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9481179193Sjb	dof_hdr_t *dof = (dof_hdr_t *)daddr;
9482179193Sjb	dof_sec_t *str_sec;
9483179193Sjb	dof_provider_t *provider;
9484179193Sjb	char *strtab;
9485179193Sjb	dtrace_helper_provdesc_t dhpv;
9486179193Sjb	dtrace_meta_t *meta = dtrace_meta_pid;
9487179193Sjb	dtrace_mops_t *mops = &meta->dtm_mops;
9488179193Sjb
9489179193Sjb	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
9490179193Sjb	str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
9491179193Sjb	    provider->dofpv_strtab * dof->dofh_secsize);
9492179193Sjb
9493179193Sjb	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
9494179193Sjb
9495179193Sjb	/*
9496179193Sjb	 * Create the provider.
9497179193Sjb	 */
9498179193Sjb	dtrace_dofprov2hprov(&dhpv, provider, strtab);
9499179193Sjb
9500179193Sjb	mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid);
9501179193Sjb
9502179193Sjb	meta->dtm_count--;
9503179193Sjb}
9504179193Sjb
9505179193Sjbstatic void
9506179193Sjbdtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid)
9507179193Sjb{
9508179193Sjb	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
9509179193Sjb	dof_hdr_t *dof = (dof_hdr_t *)daddr;
9510179193Sjb	int i;
9511179193Sjb
9512179193Sjb	ASSERT(MUTEX_HELD(&dtrace_meta_lock));
9513179193Sjb
9514179193Sjb	for (i = 0; i < dof->dofh_secnum; i++) {
9515179193Sjb		dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
9516179193Sjb		    dof->dofh_secoff + i * dof->dofh_secsize);
9517179193Sjb
9518179193Sjb		if (sec->dofs_type != DOF_SECT_PROVIDER)
9519179193Sjb			continue;
9520179193Sjb
9521179193Sjb		dtrace_helper_provider_remove_one(dhp, sec, pid);
9522179193Sjb	}
9523179193Sjb}
9524179193Sjb
9525179193Sjb/*
9526179193Sjb * DTrace Meta Provider-to-Framework API Functions
9527179193Sjb *
9528179193Sjb * These functions implement the Meta Provider-to-Framework API, as described
9529179193Sjb * in <sys/dtrace.h>.
9530179193Sjb */
9531179193Sjbint
9532179193Sjbdtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg,
9533179193Sjb    dtrace_meta_provider_id_t *idp)
9534179193Sjb{
9535179193Sjb	dtrace_meta_t *meta;
9536179193Sjb	dtrace_helpers_t *help, *next;
9537179193Sjb	int i;
9538179193Sjb
9539179193Sjb	*idp = DTRACE_METAPROVNONE;
9540179193Sjb
9541179193Sjb	/*
9542179193Sjb	 * We strictly don't need the name, but we hold onto it for
9543179193Sjb	 * debuggability. All hail error queues!
9544179193Sjb	 */
9545179193Sjb	if (name == NULL) {
9546179193Sjb		cmn_err(CE_WARN, "failed to register meta-provider: "
9547179193Sjb		    "invalid name");
9548179193Sjb		return (EINVAL);
9549179193Sjb	}
9550179193Sjb
9551179193Sjb	if (mops == NULL ||
9552179193Sjb	    mops->dtms_create_probe == NULL ||
9553179193Sjb	    mops->dtms_provide_pid == NULL ||
9554179193Sjb	    mops->dtms_remove_pid == NULL) {
9555179193Sjb		cmn_err(CE_WARN, "failed to register meta-register %s: "
9556179193Sjb		    "invalid ops", name);
9557179193Sjb		return (EINVAL);
9558179193Sjb	}
9559179193Sjb
9560179193Sjb	meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP);
9561179193Sjb	meta->dtm_mops = *mops;
9562179193Sjb	meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
9563179193Sjb	(void) strcpy(meta->dtm_name, name);
9564179193Sjb	meta->dtm_arg = arg;
9565179193Sjb
9566179193Sjb	mutex_enter(&dtrace_meta_lock);
9567179193Sjb	mutex_enter(&dtrace_lock);
9568179193Sjb
9569179193Sjb	if (dtrace_meta_pid != NULL) {
9570179193Sjb		mutex_exit(&dtrace_lock);
9571179193Sjb		mutex_exit(&dtrace_meta_lock);
9572179193Sjb		cmn_err(CE_WARN, "failed to register meta-register %s: "
9573179193Sjb		    "user-land meta-provider exists", name);
9574179193Sjb		kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1);
9575179193Sjb		kmem_free(meta, sizeof (dtrace_meta_t));
9576179193Sjb		return (EINVAL);
9577179193Sjb	}
9578179193Sjb
9579179193Sjb	dtrace_meta_pid = meta;
9580179193Sjb	*idp = (dtrace_meta_provider_id_t)meta;
9581179193Sjb
9582179193Sjb	/*
9583179193Sjb	 * If there are providers and probes ready to go, pass them
9584179193Sjb	 * off to the new meta provider now.
9585179193Sjb	 */
9586179193Sjb
9587179193Sjb	help = dtrace_deferred_pid;
9588179193Sjb	dtrace_deferred_pid = NULL;
9589179193Sjb
9590179193Sjb	mutex_exit(&dtrace_lock);
9591179193Sjb
9592179193Sjb	while (help != NULL) {
9593179193Sjb		for (i = 0; i < help->dthps_nprovs; i++) {
9594179193Sjb			dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
9595179193Sjb			    help->dthps_pid);
9596179193Sjb		}
9597179193Sjb
9598179193Sjb		next = help->dthps_next;
9599179193Sjb		help->dthps_next = NULL;
9600179193Sjb		help->dthps_prev = NULL;
9601179193Sjb		help->dthps_deferred = 0;
9602179193Sjb		help = next;
9603179193Sjb	}
9604179193Sjb
9605179193Sjb	mutex_exit(&dtrace_meta_lock);
9606179193Sjb
9607179193Sjb	return (0);
9608179193Sjb}
9609179193Sjb
9610179193Sjbint
9611179193Sjbdtrace_meta_unregister(dtrace_meta_provider_id_t id)
9612179193Sjb{
9613179193Sjb	dtrace_meta_t **pp, *old = (dtrace_meta_t *)id;
9614179193Sjb
9615179193Sjb	mutex_enter(&dtrace_meta_lock);
9616179193Sjb	mutex_enter(&dtrace_lock);
9617179193Sjb
9618179193Sjb	if (old == dtrace_meta_pid) {
9619179193Sjb		pp = &dtrace_meta_pid;
9620179193Sjb	} else {
9621179193Sjb		panic("attempt to unregister non-existent "
9622179193Sjb		    "dtrace meta-provider %p\n", (void *)old);
9623179193Sjb	}
9624179193Sjb
9625179193Sjb	if (old->dtm_count != 0) {
9626179193Sjb		mutex_exit(&dtrace_lock);
9627179193Sjb		mutex_exit(&dtrace_meta_lock);
9628179193Sjb		return (EBUSY);
9629179193Sjb	}
9630179193Sjb
9631179193Sjb	*pp = NULL;
9632179193Sjb
9633179193Sjb	mutex_exit(&dtrace_lock);
9634179193Sjb	mutex_exit(&dtrace_meta_lock);
9635179193Sjb
9636179193Sjb	kmem_free(old->dtm_name, strlen(old->dtm_name) + 1);
9637179193Sjb	kmem_free(old, sizeof (dtrace_meta_t));
9638179193Sjb
9639179193Sjb	return (0);
9640179193Sjb}
9641179193Sjb
9642179193Sjb
9643179193Sjb/*
9644179193Sjb * DTrace DIF Object Functions
9645179193Sjb */
9646179193Sjbstatic int
9647179193Sjbdtrace_difo_err(uint_t pc, const char *format, ...)
9648179193Sjb{
9649179193Sjb	if (dtrace_err_verbose) {
9650179193Sjb		va_list alist;
9651179193Sjb
9652179193Sjb		(void) uprintf("dtrace DIF object error: [%u]: ", pc);
9653179193Sjb		va_start(alist, format);
9654179193Sjb		(void) vuprintf(format, alist);
9655179193Sjb		va_end(alist);
9656179193Sjb	}
9657179193Sjb
9658179193Sjb#ifdef DTRACE_ERRDEBUG
9659179193Sjb	dtrace_errdebug(format);
9660179193Sjb#endif
9661179193Sjb	return (1);
9662179193Sjb}
9663179193Sjb
9664179193Sjb/*
9665179193Sjb * Validate a DTrace DIF object by checking the IR instructions.  The following
9666179193Sjb * rules are currently enforced by dtrace_difo_validate():
9667179193Sjb *
9668179193Sjb * 1. Each instruction must have a valid opcode
9669179193Sjb * 2. Each register, string, variable, or subroutine reference must be valid
9670179193Sjb * 3. No instruction can modify register %r0 (must be zero)
9671179193Sjb * 4. All instruction reserved bits must be set to zero
9672179193Sjb * 5. The last instruction must be a "ret" instruction
9673179193Sjb * 6. All branch targets must reference a valid instruction _after_ the branch
9674179193Sjb */
9675179193Sjbstatic int
9676179193Sjbdtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs,
9677179193Sjb    cred_t *cr)
9678179193Sjb{
9679179193Sjb	int err = 0, i;
9680179193Sjb	int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
9681179193Sjb	int kcheckload;
9682179193Sjb	uint_t pc;
9683296475Smarkj	int maxglobal = -1, maxlocal = -1, maxtlocal = -1;
9684179193Sjb
9685179193Sjb	kcheckload = cr == NULL ||
9686179193Sjb	    (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0;
9687179193Sjb
9688179193Sjb	dp->dtdo_destructive = 0;
9689179193Sjb
9690179193Sjb	for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
9691179193Sjb		dif_instr_t instr = dp->dtdo_buf[pc];
9692179193Sjb
9693179193Sjb		uint_t r1 = DIF_INSTR_R1(instr);
9694179193Sjb		uint_t r2 = DIF_INSTR_R2(instr);
9695179193Sjb		uint_t rd = DIF_INSTR_RD(instr);
9696179193Sjb		uint_t rs = DIF_INSTR_RS(instr);
9697179193Sjb		uint_t label = DIF_INSTR_LABEL(instr);
9698179193Sjb		uint_t v = DIF_INSTR_VAR(instr);
9699179193Sjb		uint_t subr = DIF_INSTR_SUBR(instr);
9700179193Sjb		uint_t type = DIF_INSTR_TYPE(instr);
9701179193Sjb		uint_t op = DIF_INSTR_OP(instr);
9702179193Sjb
9703179193Sjb		switch (op) {
9704179193Sjb		case DIF_OP_OR:
9705179193Sjb		case DIF_OP_XOR:
9706179193Sjb		case DIF_OP_AND:
9707179193Sjb		case DIF_OP_SLL:
9708179193Sjb		case DIF_OP_SRL:
9709179193Sjb		case DIF_OP_SRA:
9710179193Sjb		case DIF_OP_SUB:
9711179193Sjb		case DIF_OP_ADD:
9712179193Sjb		case DIF_OP_MUL:
9713179193Sjb		case DIF_OP_SDIV:
9714179193Sjb		case DIF_OP_UDIV:
9715179193Sjb		case DIF_OP_SREM:
9716179193Sjb		case DIF_OP_UREM:
9717179193Sjb		case DIF_OP_COPYS:
9718179193Sjb			if (r1 >= nregs)
9719179193Sjb				err += efunc(pc, "invalid register %u\n", r1);
9720179193Sjb			if (r2 >= nregs)
9721179193Sjb				err += efunc(pc, "invalid register %u\n", r2);
9722179193Sjb			if (rd >= nregs)
9723179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9724179193Sjb			if (rd == 0)
9725179193Sjb				err += efunc(pc, "cannot write to %r0\n");
9726179193Sjb			break;
9727179193Sjb		case DIF_OP_NOT:
9728179193Sjb		case DIF_OP_MOV:
9729179193Sjb		case DIF_OP_ALLOCS:
9730179193Sjb			if (r1 >= nregs)
9731179193Sjb				err += efunc(pc, "invalid register %u\n", r1);
9732179193Sjb			if (r2 != 0)
9733179193Sjb				err += efunc(pc, "non-zero reserved bits\n");
9734179193Sjb			if (rd >= nregs)
9735179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9736179193Sjb			if (rd == 0)
9737179193Sjb				err += efunc(pc, "cannot write to %r0\n");
9738179193Sjb			break;
9739179193Sjb		case DIF_OP_LDSB:
9740179193Sjb		case DIF_OP_LDSH:
9741179193Sjb		case DIF_OP_LDSW:
9742179193Sjb		case DIF_OP_LDUB:
9743179193Sjb		case DIF_OP_LDUH:
9744179193Sjb		case DIF_OP_LDUW:
9745179193Sjb		case DIF_OP_LDX:
9746179193Sjb			if (r1 >= nregs)
9747179193Sjb				err += efunc(pc, "invalid register %u\n", r1);
9748179193Sjb			if (r2 != 0)
9749179193Sjb				err += efunc(pc, "non-zero reserved bits\n");
9750179193Sjb			if (rd >= nregs)
9751179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9752179193Sjb			if (rd == 0)
9753179193Sjb				err += efunc(pc, "cannot write to %r0\n");
9754179193Sjb			if (kcheckload)
9755179193Sjb				dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op +
9756179193Sjb				    DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd);
9757179193Sjb			break;
9758179193Sjb		case DIF_OP_RLDSB:
9759179193Sjb		case DIF_OP_RLDSH:
9760179193Sjb		case DIF_OP_RLDSW:
9761179193Sjb		case DIF_OP_RLDUB:
9762179193Sjb		case DIF_OP_RLDUH:
9763179193Sjb		case DIF_OP_RLDUW:
9764179193Sjb		case DIF_OP_RLDX:
9765179193Sjb			if (r1 >= nregs)
9766179193Sjb				err += efunc(pc, "invalid register %u\n", r1);
9767179193Sjb			if (r2 != 0)
9768179193Sjb				err += efunc(pc, "non-zero reserved bits\n");
9769179193Sjb			if (rd >= nregs)
9770179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9771179193Sjb			if (rd == 0)
9772179193Sjb				err += efunc(pc, "cannot write to %r0\n");
9773179193Sjb			break;
9774179193Sjb		case DIF_OP_ULDSB:
9775179193Sjb		case DIF_OP_ULDSH:
9776179193Sjb		case DIF_OP_ULDSW:
9777179193Sjb		case DIF_OP_ULDUB:
9778179193Sjb		case DIF_OP_ULDUH:
9779179193Sjb		case DIF_OP_ULDUW:
9780179193Sjb		case DIF_OP_ULDX:
9781179193Sjb			if (r1 >= nregs)
9782179193Sjb				err += efunc(pc, "invalid register %u\n", r1);
9783179193Sjb			if (r2 != 0)
9784179193Sjb				err += efunc(pc, "non-zero reserved bits\n");
9785179193Sjb			if (rd >= nregs)
9786179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9787179193Sjb			if (rd == 0)
9788179193Sjb				err += efunc(pc, "cannot write to %r0\n");
9789179193Sjb			break;
9790179193Sjb		case DIF_OP_STB:
9791179193Sjb		case DIF_OP_STH:
9792179193Sjb		case DIF_OP_STW:
9793179193Sjb		case DIF_OP_STX:
9794179193Sjb			if (r1 >= nregs)
9795179193Sjb				err += efunc(pc, "invalid register %u\n", r1);
9796179193Sjb			if (r2 != 0)
9797179193Sjb				err += efunc(pc, "non-zero reserved bits\n");
9798179193Sjb			if (rd >= nregs)
9799179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9800179193Sjb			if (rd == 0)
9801179193Sjb				err += efunc(pc, "cannot write to 0 address\n");
9802179193Sjb			break;
9803179193Sjb		case DIF_OP_CMP:
9804179193Sjb		case DIF_OP_SCMP:
9805179193Sjb			if (r1 >= nregs)
9806179193Sjb				err += efunc(pc, "invalid register %u\n", r1);
9807179193Sjb			if (r2 >= nregs)
9808179193Sjb				err += efunc(pc, "invalid register %u\n", r2);
9809179193Sjb			if (rd != 0)
9810179193Sjb				err += efunc(pc, "non-zero reserved bits\n");
9811179193Sjb			break;
9812179193Sjb		case DIF_OP_TST:
9813179193Sjb			if (r1 >= nregs)
9814179193Sjb				err += efunc(pc, "invalid register %u\n", r1);
9815179193Sjb			if (r2 != 0 || rd != 0)
9816179193Sjb				err += efunc(pc, "non-zero reserved bits\n");
9817179193Sjb			break;
9818179193Sjb		case DIF_OP_BA:
9819179193Sjb		case DIF_OP_BE:
9820179193Sjb		case DIF_OP_BNE:
9821179193Sjb		case DIF_OP_BG:
9822179193Sjb		case DIF_OP_BGU:
9823179193Sjb		case DIF_OP_BGE:
9824179193Sjb		case DIF_OP_BGEU:
9825179193Sjb		case DIF_OP_BL:
9826179193Sjb		case DIF_OP_BLU:
9827179193Sjb		case DIF_OP_BLE:
9828179193Sjb		case DIF_OP_BLEU:
9829179193Sjb			if (label >= dp->dtdo_len) {
9830179193Sjb				err += efunc(pc, "invalid branch target %u\n",
9831179193Sjb				    label);
9832179193Sjb			}
9833179193Sjb			if (label <= pc) {
9834179193Sjb				err += efunc(pc, "backward branch to %u\n",
9835179193Sjb				    label);
9836179193Sjb			}
9837179193Sjb			break;
9838179193Sjb		case DIF_OP_RET:
9839179193Sjb			if (r1 != 0 || r2 != 0)
9840179193Sjb				err += efunc(pc, "non-zero reserved bits\n");
9841179193Sjb			if (rd >= nregs)
9842179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9843179193Sjb			break;
9844179193Sjb		case DIF_OP_NOP:
9845179193Sjb		case DIF_OP_POPTS:
9846179193Sjb		case DIF_OP_FLUSHTS:
9847179193Sjb			if (r1 != 0 || r2 != 0 || rd != 0)
9848179193Sjb				err += efunc(pc, "non-zero reserved bits\n");
9849179193Sjb			break;
9850179193Sjb		case DIF_OP_SETX:
9851179193Sjb			if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) {
9852179193Sjb				err += efunc(pc, "invalid integer ref %u\n",
9853179193Sjb				    DIF_INSTR_INTEGER(instr));
9854179193Sjb			}
9855179193Sjb			if (rd >= nregs)
9856179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9857179193Sjb			if (rd == 0)
9858179193Sjb				err += efunc(pc, "cannot write to %r0\n");
9859179193Sjb			break;
9860179193Sjb		case DIF_OP_SETS:
9861179193Sjb			if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) {
9862179193Sjb				err += efunc(pc, "invalid string ref %u\n",
9863179193Sjb				    DIF_INSTR_STRING(instr));
9864179193Sjb			}
9865179193Sjb			if (rd >= nregs)
9866179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9867179193Sjb			if (rd == 0)
9868179193Sjb				err += efunc(pc, "cannot write to %r0\n");
9869179193Sjb			break;
9870179193Sjb		case DIF_OP_LDGA:
9871179193Sjb		case DIF_OP_LDTA:
9872179193Sjb			if (r1 > DIF_VAR_ARRAY_MAX)
9873179193Sjb				err += efunc(pc, "invalid array %u\n", r1);
9874179193Sjb			if (r2 >= nregs)
9875179193Sjb				err += efunc(pc, "invalid register %u\n", r2);
9876179193Sjb			if (rd >= nregs)
9877179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9878179193Sjb			if (rd == 0)
9879179193Sjb				err += efunc(pc, "cannot write to %r0\n");
9880179193Sjb			break;
9881179193Sjb		case DIF_OP_LDGS:
9882179193Sjb		case DIF_OP_LDTS:
9883179193Sjb		case DIF_OP_LDLS:
9884179193Sjb		case DIF_OP_LDGAA:
9885179193Sjb		case DIF_OP_LDTAA:
9886179193Sjb			if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX)
9887179193Sjb				err += efunc(pc, "invalid variable %u\n", v);
9888179193Sjb			if (rd >= nregs)
9889179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9890179193Sjb			if (rd == 0)
9891179193Sjb				err += efunc(pc, "cannot write to %r0\n");
9892179193Sjb			break;
9893179193Sjb		case DIF_OP_STGS:
9894179193Sjb		case DIF_OP_STTS:
9895179193Sjb		case DIF_OP_STLS:
9896179193Sjb		case DIF_OP_STGAA:
9897179193Sjb		case DIF_OP_STTAA:
9898179193Sjb			if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX)
9899179193Sjb				err += efunc(pc, "invalid variable %u\n", v);
9900179193Sjb			if (rs >= nregs)
9901179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9902179193Sjb			break;
9903179193Sjb		case DIF_OP_CALL:
9904179193Sjb			if (subr > DIF_SUBR_MAX)
9905179193Sjb				err += efunc(pc, "invalid subr %u\n", subr);
9906179193Sjb			if (rd >= nregs)
9907179193Sjb				err += efunc(pc, "invalid register %u\n", rd);
9908179193Sjb			if (rd == 0)
9909179193Sjb				err += efunc(pc, "cannot write to %r0\n");
9910179193Sjb
9911179193Sjb			if (subr == DIF_SUBR_COPYOUT ||
9912179193Sjb			    subr == DIF_SUBR_COPYOUTSTR) {
9913179193Sjb				dp->dtdo_destructive = 1;
9914179193Sjb			}
9915267929Srpaulo
9916267929Srpaulo			if (subr == DIF_SUBR_GETF) {
9917267929Srpaulo				/*
9918267929Srpaulo				 * If we have a getf() we need to record that
9919267929Srpaulo				 * in our state.  Note that our state can be
9920267929Srpaulo				 * NULL if this is a helper -- but in that
9921267929Srpaulo				 * case, the call to getf() is itself illegal,
9922267929Srpaulo				 * and will be caught (slightly later) when
9923267929Srpaulo				 * the helper is validated.
9924267929Srpaulo				 */
9925267929Srpaulo				if (vstate->dtvs_state != NULL)
9926267929Srpaulo					vstate->dtvs_state->dts_getf++;
9927267929Srpaulo			}
9928267929Srpaulo
9929179193Sjb			break;
9930179193Sjb		case DIF_OP_PUSHTR:
9931179193Sjb			if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF)
9932179193Sjb				err += efunc(pc, "invalid ref type %u\n", type);
9933179193Sjb			if (r2 >= nregs)
9934179193Sjb				err += efunc(pc, "invalid register %u\n", r2);
9935179193Sjb			if (rs >= nregs)
9936179193Sjb				err += efunc(pc, "invalid register %u\n", rs);
9937179193Sjb			break;
9938179193Sjb		case DIF_OP_PUSHTV:
9939179193Sjb			if (type != DIF_TYPE_CTF)
9940179193Sjb				err += efunc(pc, "invalid val type %u\n", type);
9941179193Sjb			if (r2 >= nregs)
9942179193Sjb				err += efunc(pc, "invalid register %u\n", r2);
9943179193Sjb			if (rs >= nregs)
9944179193Sjb				err += efunc(pc, "invalid register %u\n", rs);
9945179193Sjb			break;
9946179193Sjb		default:
9947179193Sjb			err += efunc(pc, "invalid opcode %u\n",
9948179193Sjb			    DIF_INSTR_OP(instr));
9949179193Sjb		}
9950179193Sjb	}
9951179193Sjb
9952179193Sjb	if (dp->dtdo_len != 0 &&
9953179193Sjb	    DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) {
9954179193Sjb		err += efunc(dp->dtdo_len - 1,
9955179193Sjb		    "expected 'ret' as last DIF instruction\n");
9956179193Sjb	}
9957179193Sjb
9958267941Srpaulo	if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) {
9959179193Sjb		/*
9960179193Sjb		 * If we're not returning by reference, the size must be either
9961179193Sjb		 * 0 or the size of one of the base types.
9962179193Sjb		 */
9963179193Sjb		switch (dp->dtdo_rtype.dtdt_size) {
9964179193Sjb		case 0:
9965179193Sjb		case sizeof (uint8_t):
9966179193Sjb		case sizeof (uint16_t):
9967179193Sjb		case sizeof (uint32_t):
9968179193Sjb		case sizeof (uint64_t):
9969179193Sjb			break;
9970179193Sjb
9971179193Sjb		default:
9972268224Spfg			err += efunc(dp->dtdo_len - 1, "bad return size\n");
9973179193Sjb		}
9974179193Sjb	}
9975179193Sjb
9976179193Sjb	for (i = 0; i < dp->dtdo_varlen && err == 0; i++) {
9977179193Sjb		dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL;
9978179193Sjb		dtrace_diftype_t *vt, *et;
9979179193Sjb		uint_t id, ndx;
9980179193Sjb
9981179193Sjb		if (v->dtdv_scope != DIFV_SCOPE_GLOBAL &&
9982179193Sjb		    v->dtdv_scope != DIFV_SCOPE_THREAD &&
9983179193Sjb		    v->dtdv_scope != DIFV_SCOPE_LOCAL) {
9984179193Sjb			err += efunc(i, "unrecognized variable scope %d\n",
9985179193Sjb			    v->dtdv_scope);
9986179193Sjb			break;
9987179193Sjb		}
9988179193Sjb
9989179193Sjb		if (v->dtdv_kind != DIFV_KIND_ARRAY &&
9990179193Sjb		    v->dtdv_kind != DIFV_KIND_SCALAR) {
9991179193Sjb			err += efunc(i, "unrecognized variable type %d\n",
9992179193Sjb			    v->dtdv_kind);
9993179193Sjb			break;
9994179193Sjb		}
9995179193Sjb
9996179193Sjb		if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) {
9997179193Sjb			err += efunc(i, "%d exceeds variable id limit\n", id);
9998179193Sjb			break;
9999179193Sjb		}
10000179193Sjb
10001179193Sjb		if (id < DIF_VAR_OTHER_UBASE)
10002179193Sjb			continue;
10003179193Sjb
10004179193Sjb		/*
10005179193Sjb		 * For user-defined variables, we need to check that this
10006179193Sjb		 * definition is identical to any previous definition that we
10007179193Sjb		 * encountered.
10008179193Sjb		 */
10009179193Sjb		ndx = id - DIF_VAR_OTHER_UBASE;
10010179193Sjb
10011179193Sjb		switch (v->dtdv_scope) {
10012179193Sjb		case DIFV_SCOPE_GLOBAL:
10013296475Smarkj			if (maxglobal == -1 || ndx > maxglobal)
10014296475Smarkj				maxglobal = ndx;
10015296475Smarkj
10016179193Sjb			if (ndx < vstate->dtvs_nglobals) {
10017179193Sjb				dtrace_statvar_t *svar;
10018179193Sjb
10019179193Sjb				if ((svar = vstate->dtvs_globals[ndx]) != NULL)
10020179193Sjb					existing = &svar->dtsv_var;
10021179193Sjb			}
10022179193Sjb
10023179193Sjb			break;
10024179193Sjb
10025179193Sjb		case DIFV_SCOPE_THREAD:
10026296475Smarkj			if (maxtlocal == -1 || ndx > maxtlocal)
10027296475Smarkj				maxtlocal = ndx;
10028296475Smarkj
10029179193Sjb			if (ndx < vstate->dtvs_ntlocals)
10030179193Sjb				existing = &vstate->dtvs_tlocals[ndx];
10031179193Sjb			break;
10032179193Sjb
10033179193Sjb		case DIFV_SCOPE_LOCAL:
10034296475Smarkj			if (maxlocal == -1 || ndx > maxlocal)
10035296475Smarkj				maxlocal = ndx;
10036296475Smarkj
10037179193Sjb			if (ndx < vstate->dtvs_nlocals) {
10038179193Sjb				dtrace_statvar_t *svar;
10039179193Sjb
10040179193Sjb				if ((svar = vstate->dtvs_locals[ndx]) != NULL)
10041179193Sjb					existing = &svar->dtsv_var;
10042179193Sjb			}
10043179193Sjb
10044179193Sjb			break;
10045179193Sjb		}
10046179193Sjb
10047179193Sjb		vt = &v->dtdv_type;
10048179193Sjb
10049179193Sjb		if (vt->dtdt_flags & DIF_TF_BYREF) {
10050179193Sjb			if (vt->dtdt_size == 0) {
10051179193Sjb				err += efunc(i, "zero-sized variable\n");
10052179193Sjb				break;
10053179193Sjb			}
10054179193Sjb
10055288415Smarkj			if ((v->dtdv_scope == DIFV_SCOPE_GLOBAL ||
10056288415Smarkj			    v->dtdv_scope == DIFV_SCOPE_LOCAL) &&
10057288415Smarkj			    vt->dtdt_size > dtrace_statvar_maxsize) {
10058288415Smarkj				err += efunc(i, "oversized by-ref static\n");
10059179193Sjb				break;
10060179193Sjb			}
10061179193Sjb		}
10062179193Sjb
10063179193Sjb		if (existing == NULL || existing->dtdv_id == 0)
10064179193Sjb			continue;
10065179193Sjb
10066179193Sjb		ASSERT(existing->dtdv_id == v->dtdv_id);
10067179193Sjb		ASSERT(existing->dtdv_scope == v->dtdv_scope);
10068179193Sjb
10069179193Sjb		if (existing->dtdv_kind != v->dtdv_kind)
10070179193Sjb			err += efunc(i, "%d changed variable kind\n", id);
10071179193Sjb
10072179193Sjb		et = &existing->dtdv_type;
10073179193Sjb
10074179193Sjb		if (vt->dtdt_flags != et->dtdt_flags) {
10075179193Sjb			err += efunc(i, "%d changed variable type flags\n", id);
10076179193Sjb			break;
10077179193Sjb		}
10078179193Sjb
10079179193Sjb		if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) {
10080179193Sjb			err += efunc(i, "%d changed variable type size\n", id);
10081179193Sjb			break;
10082179193Sjb		}
10083179193Sjb	}
10084179193Sjb
10085296475Smarkj	for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
10086296475Smarkj		dif_instr_t instr = dp->dtdo_buf[pc];
10087296475Smarkj
10088296475Smarkj		uint_t v = DIF_INSTR_VAR(instr);
10089296475Smarkj		uint_t op = DIF_INSTR_OP(instr);
10090296475Smarkj
10091296475Smarkj		switch (op) {
10092296475Smarkj		case DIF_OP_LDGS:
10093296475Smarkj		case DIF_OP_LDGAA:
10094296475Smarkj		case DIF_OP_STGS:
10095296475Smarkj		case DIF_OP_STGAA:
10096296475Smarkj			if (v > DIF_VAR_OTHER_UBASE + maxglobal)
10097296475Smarkj				err += efunc(pc, "invalid variable %u\n", v);
10098296475Smarkj			break;
10099296475Smarkj		case DIF_OP_LDTS:
10100296475Smarkj		case DIF_OP_LDTAA:
10101296475Smarkj		case DIF_OP_STTS:
10102296475Smarkj		case DIF_OP_STTAA:
10103296475Smarkj			if (v > DIF_VAR_OTHER_UBASE + maxtlocal)
10104296475Smarkj				err += efunc(pc, "invalid variable %u\n", v);
10105296475Smarkj			break;
10106296475Smarkj		case DIF_OP_LDLS:
10107296475Smarkj		case DIF_OP_STLS:
10108296475Smarkj			if (v > DIF_VAR_OTHER_UBASE + maxlocal)
10109296475Smarkj				err += efunc(pc, "invalid variable %u\n", v);
10110296475Smarkj			break;
10111296475Smarkj		default:
10112296475Smarkj			break;
10113296475Smarkj		}
10114296475Smarkj	}
10115296475Smarkj
10116179193Sjb	return (err);
10117179193Sjb}
10118179193Sjb
10119179193Sjb/*
10120179193Sjb * Validate a DTrace DIF object that it is to be used as a helper.  Helpers
10121179193Sjb * are much more constrained than normal DIFOs.  Specifically, they may
10122179193Sjb * not:
10123179193Sjb *
10124179193Sjb * 1. Make calls to subroutines other than copyin(), copyinstr() or
10125179193Sjb *    miscellaneous string routines
10126179193Sjb * 2. Access DTrace variables other than the args[] array, and the
10127179193Sjb *    curthread, pid, ppid, tid, execname, zonename, uid and gid variables.
10128179193Sjb * 3. Have thread-local variables.
10129179193Sjb * 4. Have dynamic variables.
10130179193Sjb */
10131179193Sjbstatic int
10132179193Sjbdtrace_difo_validate_helper(dtrace_difo_t *dp)
10133179193Sjb{
10134179193Sjb	int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
10135179193Sjb	int err = 0;
10136179193Sjb	uint_t pc;
10137179193Sjb
10138179193Sjb	for (pc = 0; pc < dp->dtdo_len; pc++) {
10139179193Sjb		dif_instr_t instr = dp->dtdo_buf[pc];
10140179193Sjb
10141179193Sjb		uint_t v = DIF_INSTR_VAR(instr);
10142179193Sjb		uint_t subr = DIF_INSTR_SUBR(instr);
10143179193Sjb		uint_t op = DIF_INSTR_OP(instr);
10144179193Sjb
10145179193Sjb		switch (op) {
10146179193Sjb		case DIF_OP_OR:
10147179193Sjb		case DIF_OP_XOR:
10148179193Sjb		case DIF_OP_AND:
10149179193Sjb		case DIF_OP_SLL:
10150179193Sjb		case DIF_OP_SRL:
10151179193Sjb		case DIF_OP_SRA:
10152179193Sjb		case DIF_OP_SUB:
10153179193Sjb		case DIF_OP_ADD:
10154179193Sjb		case DIF_OP_MUL:
10155179193Sjb		case DIF_OP_SDIV:
10156179193Sjb		case DIF_OP_UDIV:
10157179193Sjb		case DIF_OP_SREM:
10158179193Sjb		case DIF_OP_UREM:
10159179193Sjb		case DIF_OP_COPYS:
10160179193Sjb		case DIF_OP_NOT:
10161179193Sjb		case DIF_OP_MOV:
10162179193Sjb		case DIF_OP_RLDSB:
10163179193Sjb		case DIF_OP_RLDSH:
10164179193Sjb		case DIF_OP_RLDSW:
10165179193Sjb		case DIF_OP_RLDUB:
10166179193Sjb		case DIF_OP_RLDUH:
10167179193Sjb		case DIF_OP_RLDUW:
10168179193Sjb		case DIF_OP_RLDX:
10169179193Sjb		case DIF_OP_ULDSB:
10170179193Sjb		case DIF_OP_ULDSH:
10171179193Sjb		case DIF_OP_ULDSW:
10172179193Sjb		case DIF_OP_ULDUB:
10173179193Sjb		case DIF_OP_ULDUH:
10174179193Sjb		case DIF_OP_ULDUW:
10175179193Sjb		case DIF_OP_ULDX:
10176179193Sjb		case DIF_OP_STB:
10177179193Sjb		case DIF_OP_STH:
10178179193Sjb		case DIF_OP_STW:
10179179193Sjb		case DIF_OP_STX:
10180179193Sjb		case DIF_OP_ALLOCS:
10181179193Sjb		case DIF_OP_CMP:
10182179193Sjb		case DIF_OP_SCMP:
10183179193Sjb		case DIF_OP_TST:
10184179193Sjb		case DIF_OP_BA:
10185179193Sjb		case DIF_OP_BE:
10186179193Sjb		case DIF_OP_BNE:
10187179193Sjb		case DIF_OP_BG:
10188179193Sjb		case DIF_OP_BGU:
10189179193Sjb		case DIF_OP_BGE:
10190179193Sjb		case DIF_OP_BGEU:
10191179193Sjb		case DIF_OP_BL:
10192179193Sjb		case DIF_OP_BLU:
10193179193Sjb		case DIF_OP_BLE:
10194179193Sjb		case DIF_OP_BLEU:
10195179193Sjb		case DIF_OP_RET:
10196179193Sjb		case DIF_OP_NOP:
10197179193Sjb		case DIF_OP_POPTS:
10198179193Sjb		case DIF_OP_FLUSHTS:
10199179193Sjb		case DIF_OP_SETX:
10200179193Sjb		case DIF_OP_SETS:
10201179193Sjb		case DIF_OP_LDGA:
10202179193Sjb		case DIF_OP_LDLS:
10203179193Sjb		case DIF_OP_STGS:
10204179193Sjb		case DIF_OP_STLS:
10205179193Sjb		case DIF_OP_PUSHTR:
10206179193Sjb		case DIF_OP_PUSHTV:
10207179193Sjb			break;
10208179193Sjb
10209179193Sjb		case DIF_OP_LDGS:
10210179193Sjb			if (v >= DIF_VAR_OTHER_UBASE)
10211179193Sjb				break;
10212179193Sjb
10213179193Sjb			if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9)
10214179193Sjb				break;
10215179193Sjb
10216179193Sjb			if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID ||
10217179193Sjb			    v == DIF_VAR_PPID || v == DIF_VAR_TID ||
10218179198Sjb			    v == DIF_VAR_EXECARGS ||
10219179193Sjb			    v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME ||
10220179193Sjb			    v == DIF_VAR_UID || v == DIF_VAR_GID)
10221179193Sjb				break;
10222179193Sjb
10223179193Sjb			err += efunc(pc, "illegal variable %u\n", v);
10224179193Sjb			break;
10225179193Sjb
10226179193Sjb		case DIF_OP_LDTA:
10227179193Sjb		case DIF_OP_LDTS:
10228179193Sjb		case DIF_OP_LDGAA:
10229179193Sjb		case DIF_OP_LDTAA:
10230179193Sjb			err += efunc(pc, "illegal dynamic variable load\n");
10231179193Sjb			break;
10232179193Sjb
10233179193Sjb		case DIF_OP_STTS:
10234179193Sjb		case DIF_OP_STGAA:
10235179193Sjb		case DIF_OP_STTAA:
10236179193Sjb			err += efunc(pc, "illegal dynamic variable store\n");
10237179193Sjb			break;
10238179193Sjb
10239179193Sjb		case DIF_OP_CALL:
10240179193Sjb			if (subr == DIF_SUBR_ALLOCA ||
10241179193Sjb			    subr == DIF_SUBR_BCOPY ||
10242179193Sjb			    subr == DIF_SUBR_COPYIN ||
10243179193Sjb			    subr == DIF_SUBR_COPYINTO ||
10244179193Sjb			    subr == DIF_SUBR_COPYINSTR ||
10245179193Sjb			    subr == DIF_SUBR_INDEX ||
10246179193Sjb			    subr == DIF_SUBR_INET_NTOA ||
10247179193Sjb			    subr == DIF_SUBR_INET_NTOA6 ||
10248179193Sjb			    subr == DIF_SUBR_INET_NTOP ||
10249267937Srpaulo			    subr == DIF_SUBR_JSON ||
10250179193Sjb			    subr == DIF_SUBR_LLTOSTR ||
10251267937Srpaulo			    subr == DIF_SUBR_STRTOLL ||
10252179193Sjb			    subr == DIF_SUBR_RINDEX ||
10253179193Sjb			    subr == DIF_SUBR_STRCHR ||
10254179193Sjb			    subr == DIF_SUBR_STRJOIN ||
10255179193Sjb			    subr == DIF_SUBR_STRRCHR ||
10256179193Sjb			    subr == DIF_SUBR_STRSTR ||
10257179193Sjb			    subr == DIF_SUBR_HTONS ||
10258179193Sjb			    subr == DIF_SUBR_HTONL ||
10259179193Sjb			    subr == DIF_SUBR_HTONLL ||
10260179193Sjb			    subr == DIF_SUBR_NTOHS ||
10261179193Sjb			    subr == DIF_SUBR_NTOHL ||
10262179198Sjb			    subr == DIF_SUBR_NTOHLL ||
10263179198Sjb			    subr == DIF_SUBR_MEMREF ||
10264277300Ssmh#ifndef illumos
10265256571Smarkj			    subr == DIF_SUBR_MEMSTR ||
10266256571Smarkj#endif
10267179198Sjb			    subr == DIF_SUBR_TYPEREF)
10268179193Sjb				break;
10269179193Sjb
10270179193Sjb			err += efunc(pc, "invalid subr %u\n", subr);
10271179193Sjb			break;
10272179193Sjb
10273179193Sjb		default:
10274179193Sjb			err += efunc(pc, "invalid opcode %u\n",
10275179193Sjb			    DIF_INSTR_OP(instr));
10276179193Sjb		}
10277179193Sjb	}
10278179193Sjb
10279179193Sjb	return (err);
10280179193Sjb}
10281179193Sjb
10282179193Sjb/*
10283179193Sjb * Returns 1 if the expression in the DIF object can be cached on a per-thread
10284179193Sjb * basis; 0 if not.
10285179193Sjb */
10286179193Sjbstatic int
10287179193Sjbdtrace_difo_cacheable(dtrace_difo_t *dp)
10288179193Sjb{
10289179193Sjb	int i;
10290179193Sjb
10291179193Sjb	if (dp == NULL)
10292179193Sjb		return (0);
10293179193Sjb
10294179193Sjb	for (i = 0; i < dp->dtdo_varlen; i++) {
10295179193Sjb		dtrace_difv_t *v = &dp->dtdo_vartab[i];
10296179193Sjb
10297179193Sjb		if (v->dtdv_scope != DIFV_SCOPE_GLOBAL)
10298179193Sjb			continue;
10299179193Sjb
10300179193Sjb		switch (v->dtdv_id) {
10301179193Sjb		case DIF_VAR_CURTHREAD:
10302179193Sjb		case DIF_VAR_PID:
10303179193Sjb		case DIF_VAR_TID:
10304179198Sjb		case DIF_VAR_EXECARGS:
10305179193Sjb		case DIF_VAR_EXECNAME:
10306179193Sjb		case DIF_VAR_ZONENAME:
10307179193Sjb			break;
10308179193Sjb
10309179193Sjb		default:
10310179193Sjb			return (0);
10311179193Sjb		}
10312179193Sjb	}
10313179193Sjb
10314179193Sjb	/*
10315179193Sjb	 * This DIF object may be cacheable.  Now we need to look for any
10316179193Sjb	 * array loading instructions, any memory loading instructions, or
10317179193Sjb	 * any stores to thread-local variables.
10318179193Sjb	 */
10319179193Sjb	for (i = 0; i < dp->dtdo_len; i++) {
10320179193Sjb		uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]);
10321179193Sjb
10322179193Sjb		if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) ||
10323179193Sjb		    (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) ||
10324179193Sjb		    (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) ||
10325179193Sjb		    op == DIF_OP_LDGA || op == DIF_OP_STTS)
10326179193Sjb			return (0);
10327179193Sjb	}
10328179193Sjb
10329179193Sjb	return (1);
10330179193Sjb}
10331179193Sjb
10332179193Sjbstatic void
10333179193Sjbdtrace_difo_hold(dtrace_difo_t *dp)
10334179193Sjb{
10335179193Sjb	int i;
10336179193Sjb
10337179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
10338179193Sjb
10339179193Sjb	dp->dtdo_refcnt++;
10340179193Sjb	ASSERT(dp->dtdo_refcnt != 0);
10341179193Sjb
10342179193Sjb	/*
10343179193Sjb	 * We need to check this DIF object for references to the variable
10344179193Sjb	 * DIF_VAR_VTIMESTAMP.
10345179193Sjb	 */
10346179193Sjb	for (i = 0; i < dp->dtdo_varlen; i++) {
10347179193Sjb		dtrace_difv_t *v = &dp->dtdo_vartab[i];
10348179193Sjb
10349179193Sjb		if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
10350179193Sjb			continue;
10351179193Sjb
10352179193Sjb		if (dtrace_vtime_references++ == 0)
10353179193Sjb			dtrace_vtime_enable();
10354179193Sjb	}
10355179193Sjb}
10356179193Sjb
10357179193Sjb/*
10358179193Sjb * This routine calculates the dynamic variable chunksize for a given DIF
10359179193Sjb * object.  The calculation is not fool-proof, and can probably be tricked by
10360179193Sjb * malicious DIF -- but it works for all compiler-generated DIF.  Because this
10361179193Sjb * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail
10362179193Sjb * if a dynamic variable size exceeds the chunksize.
10363179193Sjb */
10364179193Sjbstatic void
10365179193Sjbdtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10366179193Sjb{
10367179198Sjb	uint64_t sval = 0;
10368179193Sjb	dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
10369179193Sjb	const dif_instr_t *text = dp->dtdo_buf;
10370179193Sjb	uint_t pc, srd = 0;
10371179193Sjb	uint_t ttop = 0;
10372179193Sjb	size_t size, ksize;
10373179193Sjb	uint_t id, i;
10374179193Sjb
10375179193Sjb	for (pc = 0; pc < dp->dtdo_len; pc++) {
10376179193Sjb		dif_instr_t instr = text[pc];
10377179193Sjb		uint_t op = DIF_INSTR_OP(instr);
10378179193Sjb		uint_t rd = DIF_INSTR_RD(instr);
10379179193Sjb		uint_t r1 = DIF_INSTR_R1(instr);
10380179193Sjb		uint_t nkeys = 0;
10381179198Sjb		uchar_t scope = 0;
10382179193Sjb
10383179193Sjb		dtrace_key_t *key = tupregs;
10384179193Sjb
10385179193Sjb		switch (op) {
10386179193Sjb		case DIF_OP_SETX:
10387179193Sjb			sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)];
10388179193Sjb			srd = rd;
10389179193Sjb			continue;
10390179193Sjb
10391179193Sjb		case DIF_OP_STTS:
10392179193Sjb			key = &tupregs[DIF_DTR_NREGS];
10393179193Sjb			key[0].dttk_size = 0;
10394179193Sjb			key[1].dttk_size = 0;
10395179193Sjb			nkeys = 2;
10396179193Sjb			scope = DIFV_SCOPE_THREAD;
10397179193Sjb			break;
10398179193Sjb
10399179193Sjb		case DIF_OP_STGAA:
10400179193Sjb		case DIF_OP_STTAA:
10401179193Sjb			nkeys = ttop;
10402179193Sjb
10403179193Sjb			if (DIF_INSTR_OP(instr) == DIF_OP_STTAA)
10404179193Sjb				key[nkeys++].dttk_size = 0;
10405179193Sjb
10406179193Sjb			key[nkeys++].dttk_size = 0;
10407179193Sjb
10408179193Sjb			if (op == DIF_OP_STTAA) {
10409179193Sjb				scope = DIFV_SCOPE_THREAD;
10410179193Sjb			} else {
10411179193Sjb				scope = DIFV_SCOPE_GLOBAL;
10412179193Sjb			}
10413179193Sjb
10414179193Sjb			break;
10415179193Sjb
10416179193Sjb		case DIF_OP_PUSHTR:
10417179193Sjb			if (ttop == DIF_DTR_NREGS)
10418179193Sjb				return;
10419179193Sjb
10420179193Sjb			if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) {
10421179193Sjb				/*
10422179193Sjb				 * If the register for the size of the "pushtr"
10423179193Sjb				 * is %r0 (or the value is 0) and the type is
10424179193Sjb				 * a string, we'll use the system-wide default
10425179193Sjb				 * string size.
10426179193Sjb				 */
10427179193Sjb				tupregs[ttop++].dttk_size =
10428179193Sjb				    dtrace_strsize_default;
10429179193Sjb			} else {
10430179193Sjb				if (srd == 0)
10431179193Sjb					return;
10432179193Sjb
10433288415Smarkj				if (sval > LONG_MAX)
10434288415Smarkj					return;
10435288415Smarkj
10436179193Sjb				tupregs[ttop++].dttk_size = sval;
10437179193Sjb			}
10438179193Sjb
10439179193Sjb			break;
10440179193Sjb
10441179193Sjb		case DIF_OP_PUSHTV:
10442179193Sjb			if (ttop == DIF_DTR_NREGS)
10443179193Sjb				return;
10444179193Sjb
10445179193Sjb			tupregs[ttop++].dttk_size = 0;
10446179193Sjb			break;
10447179193Sjb
10448179193Sjb		case DIF_OP_FLUSHTS:
10449179193Sjb			ttop = 0;
10450179193Sjb			break;
10451179193Sjb
10452179193Sjb		case DIF_OP_POPTS:
10453179193Sjb			if (ttop != 0)
10454179193Sjb				ttop--;
10455179193Sjb			break;
10456179193Sjb		}
10457179193Sjb
10458179193Sjb		sval = 0;
10459179193Sjb		srd = 0;
10460179193Sjb
10461179193Sjb		if (nkeys == 0)
10462179193Sjb			continue;
10463179193Sjb
10464179193Sjb		/*
10465179193Sjb		 * We have a dynamic variable allocation; calculate its size.
10466179193Sjb		 */
10467179193Sjb		for (ksize = 0, i = 0; i < nkeys; i++)
10468179193Sjb			ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
10469179193Sjb
10470179193Sjb		size = sizeof (dtrace_dynvar_t);
10471179193Sjb		size += sizeof (dtrace_key_t) * (nkeys - 1);
10472179193Sjb		size += ksize;
10473179193Sjb
10474179193Sjb		/*
10475179193Sjb		 * Now we need to determine the size of the stored data.
10476179193Sjb		 */
10477179193Sjb		id = DIF_INSTR_VAR(instr);
10478179193Sjb
10479179193Sjb		for (i = 0; i < dp->dtdo_varlen; i++) {
10480179193Sjb			dtrace_difv_t *v = &dp->dtdo_vartab[i];
10481179193Sjb
10482179193Sjb			if (v->dtdv_id == id && v->dtdv_scope == scope) {
10483179193Sjb				size += v->dtdv_type.dtdt_size;
10484179193Sjb				break;
10485179193Sjb			}
10486179193Sjb		}
10487179193Sjb
10488179193Sjb		if (i == dp->dtdo_varlen)
10489179193Sjb			return;
10490179193Sjb
10491179193Sjb		/*
10492179193Sjb		 * We have the size.  If this is larger than the chunk size
10493179193Sjb		 * for our dynamic variable state, reset the chunk size.
10494179193Sjb		 */
10495179193Sjb		size = P2ROUNDUP(size, sizeof (uint64_t));
10496179193Sjb
10497288415Smarkj		/*
10498288415Smarkj		 * Before setting the chunk size, check that we're not going
10499288415Smarkj		 * to set it to a negative value...
10500288415Smarkj		 */
10501288415Smarkj		if (size > LONG_MAX)
10502288415Smarkj			return;
10503288415Smarkj
10504288415Smarkj		/*
10505288415Smarkj		 * ...and make certain that we didn't badly overflow.
10506288415Smarkj		 */
10507288415Smarkj		if (size < ksize || size < sizeof (dtrace_dynvar_t))
10508288415Smarkj			return;
10509288415Smarkj
10510179193Sjb		if (size > vstate->dtvs_dynvars.dtds_chunksize)
10511179193Sjb			vstate->dtvs_dynvars.dtds_chunksize = size;
10512179193Sjb	}
10513179193Sjb}
10514179193Sjb
10515179193Sjbstatic void
10516179193Sjbdtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10517179193Sjb{
10518179193Sjb	int i, oldsvars, osz, nsz, otlocals, ntlocals;
10519179193Sjb	uint_t id;
10520179193Sjb
10521179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
10522179193Sjb	ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0);
10523179193Sjb
10524179193Sjb	for (i = 0; i < dp->dtdo_varlen; i++) {
10525179193Sjb		dtrace_difv_t *v = &dp->dtdo_vartab[i];
10526179198Sjb		dtrace_statvar_t *svar, ***svarp = NULL;
10527179193Sjb		size_t dsize = 0;
10528179193Sjb		uint8_t scope = v->dtdv_scope;
10529179198Sjb		int *np = NULL;
10530179193Sjb
10531179193Sjb		if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
10532179193Sjb			continue;
10533179193Sjb
10534179193Sjb		id -= DIF_VAR_OTHER_UBASE;
10535179193Sjb
10536179193Sjb		switch (scope) {
10537179193Sjb		case DIFV_SCOPE_THREAD:
10538179193Sjb			while (id >= (otlocals = vstate->dtvs_ntlocals)) {
10539179193Sjb				dtrace_difv_t *tlocals;
10540179193Sjb
10541179193Sjb				if ((ntlocals = (otlocals << 1)) == 0)
10542179193Sjb					ntlocals = 1;
10543179193Sjb
10544179193Sjb				osz = otlocals * sizeof (dtrace_difv_t);
10545179193Sjb				nsz = ntlocals * sizeof (dtrace_difv_t);
10546179193Sjb
10547179193Sjb				tlocals = kmem_zalloc(nsz, KM_SLEEP);
10548179193Sjb
10549179193Sjb				if (osz != 0) {
10550179193Sjb					bcopy(vstate->dtvs_tlocals,
10551179193Sjb					    tlocals, osz);
10552179193Sjb					kmem_free(vstate->dtvs_tlocals, osz);
10553179193Sjb				}
10554179193Sjb
10555179193Sjb				vstate->dtvs_tlocals = tlocals;
10556179193Sjb				vstate->dtvs_ntlocals = ntlocals;
10557179193Sjb			}
10558179193Sjb
10559179193Sjb			vstate->dtvs_tlocals[id] = *v;
10560179193Sjb			continue;
10561179193Sjb
10562179193Sjb		case DIFV_SCOPE_LOCAL:
10563179193Sjb			np = &vstate->dtvs_nlocals;
10564179193Sjb			svarp = &vstate->dtvs_locals;
10565179193Sjb
10566179193Sjb			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
10567179193Sjb				dsize = NCPU * (v->dtdv_type.dtdt_size +
10568179193Sjb				    sizeof (uint64_t));
10569179193Sjb			else
10570179193Sjb				dsize = NCPU * sizeof (uint64_t);
10571179193Sjb
10572179193Sjb			break;
10573179193Sjb
10574179193Sjb		case DIFV_SCOPE_GLOBAL:
10575179193Sjb			np = &vstate->dtvs_nglobals;
10576179193Sjb			svarp = &vstate->dtvs_globals;
10577179193Sjb
10578179193Sjb			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
10579179193Sjb				dsize = v->dtdv_type.dtdt_size +
10580179193Sjb				    sizeof (uint64_t);
10581179193Sjb
10582179193Sjb			break;
10583179193Sjb
10584179193Sjb		default:
10585179193Sjb			ASSERT(0);
10586179193Sjb		}
10587179193Sjb
10588179193Sjb		while (id >= (oldsvars = *np)) {
10589179193Sjb			dtrace_statvar_t **statics;
10590179193Sjb			int newsvars, oldsize, newsize;
10591179193Sjb
10592179193Sjb			if ((newsvars = (oldsvars << 1)) == 0)
10593179193Sjb				newsvars = 1;
10594179193Sjb
10595179193Sjb			oldsize = oldsvars * sizeof (dtrace_statvar_t *);
10596179193Sjb			newsize = newsvars * sizeof (dtrace_statvar_t *);
10597179193Sjb
10598179193Sjb			statics = kmem_zalloc(newsize, KM_SLEEP);
10599179193Sjb
10600179193Sjb			if (oldsize != 0) {
10601179193Sjb				bcopy(*svarp, statics, oldsize);
10602179193Sjb				kmem_free(*svarp, oldsize);
10603179193Sjb			}
10604179193Sjb
10605179193Sjb			*svarp = statics;
10606179193Sjb			*np = newsvars;
10607179193Sjb		}
10608179193Sjb
10609179193Sjb		if ((svar = (*svarp)[id]) == NULL) {
10610179193Sjb			svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP);
10611179193Sjb			svar->dtsv_var = *v;
10612179193Sjb
10613179193Sjb			if ((svar->dtsv_size = dsize) != 0) {
10614179193Sjb				svar->dtsv_data = (uint64_t)(uintptr_t)
10615179193Sjb				    kmem_zalloc(dsize, KM_SLEEP);
10616179193Sjb			}
10617179193Sjb
10618179193Sjb			(*svarp)[id] = svar;
10619179193Sjb		}
10620179193Sjb
10621179193Sjb		svar->dtsv_refcnt++;
10622179193Sjb	}
10623179193Sjb
10624179193Sjb	dtrace_difo_chunksize(dp, vstate);
10625179193Sjb	dtrace_difo_hold(dp);
10626179193Sjb}
10627179193Sjb
10628179193Sjbstatic dtrace_difo_t *
10629179193Sjbdtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10630179193Sjb{
10631179193Sjb	dtrace_difo_t *new;
10632179193Sjb	size_t sz;
10633179193Sjb
10634179193Sjb	ASSERT(dp->dtdo_buf != NULL);
10635179193Sjb	ASSERT(dp->dtdo_refcnt != 0);
10636179193Sjb
10637179193Sjb	new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
10638179193Sjb
10639179193Sjb	ASSERT(dp->dtdo_buf != NULL);
10640179193Sjb	sz = dp->dtdo_len * sizeof (dif_instr_t);
10641179193Sjb	new->dtdo_buf = kmem_alloc(sz, KM_SLEEP);
10642179193Sjb	bcopy(dp->dtdo_buf, new->dtdo_buf, sz);
10643179193Sjb	new->dtdo_len = dp->dtdo_len;
10644179193Sjb
10645179193Sjb	if (dp->dtdo_strtab != NULL) {
10646179193Sjb		ASSERT(dp->dtdo_strlen != 0);
10647179193Sjb		new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP);
10648179193Sjb		bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen);
10649179193Sjb		new->dtdo_strlen = dp->dtdo_strlen;
10650179193Sjb	}
10651179193Sjb
10652179193Sjb	if (dp->dtdo_inttab != NULL) {
10653179193Sjb		ASSERT(dp->dtdo_intlen != 0);
10654179193Sjb		sz = dp->dtdo_intlen * sizeof (uint64_t);
10655179193Sjb		new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP);
10656179193Sjb		bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz);
10657179193Sjb		new->dtdo_intlen = dp->dtdo_intlen;
10658179193Sjb	}
10659179193Sjb
10660179193Sjb	if (dp->dtdo_vartab != NULL) {
10661179193Sjb		ASSERT(dp->dtdo_varlen != 0);
10662179193Sjb		sz = dp->dtdo_varlen * sizeof (dtrace_difv_t);
10663179193Sjb		new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP);
10664179193Sjb		bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz);
10665179193Sjb		new->dtdo_varlen = dp->dtdo_varlen;
10666179193Sjb	}
10667179193Sjb
10668179193Sjb	dtrace_difo_init(new, vstate);
10669179193Sjb	return (new);
10670179193Sjb}
10671179193Sjb
10672179193Sjbstatic void
10673179193Sjbdtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10674179193Sjb{
10675179193Sjb	int i;
10676179193Sjb
10677179193Sjb	ASSERT(dp->dtdo_refcnt == 0);
10678179193Sjb
10679179193Sjb	for (i = 0; i < dp->dtdo_varlen; i++) {
10680179193Sjb		dtrace_difv_t *v = &dp->dtdo_vartab[i];
10681179198Sjb		dtrace_statvar_t *svar, **svarp = NULL;
10682179193Sjb		uint_t id;
10683179193Sjb		uint8_t scope = v->dtdv_scope;
10684179198Sjb		int *np = NULL;
10685179193Sjb
10686179193Sjb		switch (scope) {
10687179193Sjb		case DIFV_SCOPE_THREAD:
10688179193Sjb			continue;
10689179193Sjb
10690179193Sjb		case DIFV_SCOPE_LOCAL:
10691179193Sjb			np = &vstate->dtvs_nlocals;
10692179193Sjb			svarp = vstate->dtvs_locals;
10693179193Sjb			break;
10694179193Sjb
10695179193Sjb		case DIFV_SCOPE_GLOBAL:
10696179193Sjb			np = &vstate->dtvs_nglobals;
10697179193Sjb			svarp = vstate->dtvs_globals;
10698179193Sjb			break;
10699179193Sjb
10700179193Sjb		default:
10701179193Sjb			ASSERT(0);
10702179193Sjb		}
10703179193Sjb
10704179193Sjb		if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
10705179193Sjb			continue;
10706179193Sjb
10707179193Sjb		id -= DIF_VAR_OTHER_UBASE;
10708179193Sjb		ASSERT(id < *np);
10709179193Sjb
10710179193Sjb		svar = svarp[id];
10711179193Sjb		ASSERT(svar != NULL);
10712179193Sjb		ASSERT(svar->dtsv_refcnt > 0);
10713179193Sjb
10714179193Sjb		if (--svar->dtsv_refcnt > 0)
10715179193Sjb			continue;
10716179193Sjb
10717179193Sjb		if (svar->dtsv_size != 0) {
10718179198Sjb			ASSERT(svar->dtsv_data != 0);
10719179193Sjb			kmem_free((void *)(uintptr_t)svar->dtsv_data,
10720179193Sjb			    svar->dtsv_size);
10721179193Sjb		}
10722179193Sjb
10723179193Sjb		kmem_free(svar, sizeof (dtrace_statvar_t));
10724179193Sjb		svarp[id] = NULL;
10725179193Sjb	}
10726179193Sjb
10727179198Sjb	if (dp->dtdo_buf != NULL)
10728179198Sjb		kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
10729179198Sjb	if (dp->dtdo_inttab != NULL)
10730179198Sjb		kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
10731179198Sjb	if (dp->dtdo_strtab != NULL)
10732179198Sjb		kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
10733179198Sjb	if (dp->dtdo_vartab != NULL)
10734179198Sjb		kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
10735179193Sjb
10736179193Sjb	kmem_free(dp, sizeof (dtrace_difo_t));
10737179193Sjb}
10738179193Sjb
10739179193Sjbstatic void
10740179193Sjbdtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
10741179193Sjb{
10742179193Sjb	int i;
10743179193Sjb
10744179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
10745179193Sjb	ASSERT(dp->dtdo_refcnt != 0);
10746179193Sjb
10747179193Sjb	for (i = 0; i < dp->dtdo_varlen; i++) {
10748179193Sjb		dtrace_difv_t *v = &dp->dtdo_vartab[i];
10749179193Sjb
10750179193Sjb		if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
10751179193Sjb			continue;
10752179193Sjb
10753179193Sjb		ASSERT(dtrace_vtime_references > 0);
10754179193Sjb		if (--dtrace_vtime_references == 0)
10755179193Sjb			dtrace_vtime_disable();
10756179193Sjb	}
10757179193Sjb
10758179193Sjb	if (--dp->dtdo_refcnt == 0)
10759179193Sjb		dtrace_difo_destroy(dp, vstate);
10760179193Sjb}
10761179193Sjb
10762179193Sjb/*
10763179193Sjb * DTrace Format Functions
10764179193Sjb */
10765179193Sjbstatic uint16_t
10766179193Sjbdtrace_format_add(dtrace_state_t *state, char *str)
10767179193Sjb{
10768179193Sjb	char *fmt, **new;
10769179193Sjb	uint16_t ndx, len = strlen(str) + 1;
10770179193Sjb
10771179193Sjb	fmt = kmem_zalloc(len, KM_SLEEP);
10772179193Sjb	bcopy(str, fmt, len);
10773179193Sjb
10774179193Sjb	for (ndx = 0; ndx < state->dts_nformats; ndx++) {
10775179193Sjb		if (state->dts_formats[ndx] == NULL) {
10776179193Sjb			state->dts_formats[ndx] = fmt;
10777179193Sjb			return (ndx + 1);
10778179193Sjb		}
10779179193Sjb	}
10780179193Sjb
10781179193Sjb	if (state->dts_nformats == USHRT_MAX) {
10782179193Sjb		/*
10783179193Sjb		 * This is only likely if a denial-of-service attack is being
10784179193Sjb		 * attempted.  As such, it's okay to fail silently here.
10785179193Sjb		 */
10786179193Sjb		kmem_free(fmt, len);
10787179193Sjb		return (0);
10788179193Sjb	}
10789179193Sjb
10790179193Sjb	/*
10791179193Sjb	 * For simplicity, we always resize the formats array to be exactly the
10792179193Sjb	 * number of formats.
10793179193Sjb	 */
10794179193Sjb	ndx = state->dts_nformats++;
10795179193Sjb	new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP);
10796179193Sjb
10797179193Sjb	if (state->dts_formats != NULL) {
10798179193Sjb		ASSERT(ndx != 0);
10799179193Sjb		bcopy(state->dts_formats, new, ndx * sizeof (char *));
10800179193Sjb		kmem_free(state->dts_formats, ndx * sizeof (char *));
10801179193Sjb	}
10802179193Sjb
10803179193Sjb	state->dts_formats = new;
10804179193Sjb	state->dts_formats[ndx] = fmt;
10805179193Sjb
10806179193Sjb	return (ndx + 1);
10807179193Sjb}
10808179193Sjb
10809179193Sjbstatic void
10810179193Sjbdtrace_format_remove(dtrace_state_t *state, uint16_t format)
10811179193Sjb{
10812179193Sjb	char *fmt;
10813179193Sjb
10814179193Sjb	ASSERT(state->dts_formats != NULL);
10815179193Sjb	ASSERT(format <= state->dts_nformats);
10816179193Sjb	ASSERT(state->dts_formats[format - 1] != NULL);
10817179193Sjb
10818179193Sjb	fmt = state->dts_formats[format - 1];
10819179193Sjb	kmem_free(fmt, strlen(fmt) + 1);
10820179193Sjb	state->dts_formats[format - 1] = NULL;
10821179193Sjb}
10822179193Sjb
10823179193Sjbstatic void
10824179193Sjbdtrace_format_destroy(dtrace_state_t *state)
10825179193Sjb{
10826179193Sjb	int i;
10827179193Sjb
10828179193Sjb	if (state->dts_nformats == 0) {
10829179193Sjb		ASSERT(state->dts_formats == NULL);
10830179193Sjb		return;
10831179193Sjb	}
10832179193Sjb
10833179193Sjb	ASSERT(state->dts_formats != NULL);
10834179193Sjb
10835179193Sjb	for (i = 0; i < state->dts_nformats; i++) {
10836179193Sjb		char *fmt = state->dts_formats[i];
10837179193Sjb
10838179193Sjb		if (fmt == NULL)
10839179193Sjb			continue;
10840179193Sjb
10841179193Sjb		kmem_free(fmt, strlen(fmt) + 1);
10842179193Sjb	}
10843179193Sjb
10844179193Sjb	kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *));
10845179193Sjb	state->dts_nformats = 0;
10846179193Sjb	state->dts_formats = NULL;
10847179193Sjb}
10848179193Sjb
10849179193Sjb/*
10850179193Sjb * DTrace Predicate Functions
10851179193Sjb */
10852179193Sjbstatic dtrace_predicate_t *
10853179193Sjbdtrace_predicate_create(dtrace_difo_t *dp)
10854179193Sjb{
10855179193Sjb	dtrace_predicate_t *pred;
10856179193Sjb
10857179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
10858179193Sjb	ASSERT(dp->dtdo_refcnt != 0);
10859179193Sjb
10860179193Sjb	pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP);
10861179193Sjb	pred->dtp_difo = dp;
10862179193Sjb	pred->dtp_refcnt = 1;
10863179193Sjb
10864179193Sjb	if (!dtrace_difo_cacheable(dp))
10865179193Sjb		return (pred);
10866179193Sjb
10867179193Sjb	if (dtrace_predcache_id == DTRACE_CACHEIDNONE) {
10868179193Sjb		/*
10869179193Sjb		 * This is only theoretically possible -- we have had 2^32
10870179193Sjb		 * cacheable predicates on this machine.  We cannot allow any
10871179193Sjb		 * more predicates to become cacheable:  as unlikely as it is,
10872179193Sjb		 * there may be a thread caching a (now stale) predicate cache
10873179193Sjb		 * ID. (N.B.: the temptation is being successfully resisted to
10874179193Sjb		 * have this cmn_err() "Holy shit -- we executed this code!")
10875179193Sjb		 */
10876179193Sjb		return (pred);
10877179193Sjb	}
10878179193Sjb
10879179193Sjb	pred->dtp_cacheid = dtrace_predcache_id++;
10880179193Sjb
10881179193Sjb	return (pred);
10882179193Sjb}
10883179193Sjb
10884179193Sjbstatic void
10885179193Sjbdtrace_predicate_hold(dtrace_predicate_t *pred)
10886179193Sjb{
10887179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
10888179193Sjb	ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0);
10889179193Sjb	ASSERT(pred->dtp_refcnt > 0);
10890179193Sjb
10891179193Sjb	pred->dtp_refcnt++;
10892179193Sjb}
10893179193Sjb
10894179193Sjbstatic void
10895179193Sjbdtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate)
10896179193Sjb{
10897179193Sjb	dtrace_difo_t *dp = pred->dtp_difo;
10898179193Sjb
10899179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
10900179193Sjb	ASSERT(dp != NULL && dp->dtdo_refcnt != 0);
10901179193Sjb	ASSERT(pred->dtp_refcnt > 0);
10902179193Sjb
10903179193Sjb	if (--pred->dtp_refcnt == 0) {
10904179193Sjb		dtrace_difo_release(pred->dtp_difo, vstate);
10905179193Sjb		kmem_free(pred, sizeof (dtrace_predicate_t));
10906179193Sjb	}
10907179193Sjb}
10908179193Sjb
10909179193Sjb/*
10910179193Sjb * DTrace Action Description Functions
10911179193Sjb */
10912179193Sjbstatic dtrace_actdesc_t *
10913179193Sjbdtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple,
10914179193Sjb    uint64_t uarg, uint64_t arg)
10915179193Sjb{
10916179193Sjb	dtrace_actdesc_t *act;
10917179193Sjb
10918277300Ssmh#ifdef illumos
10919179193Sjb	ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL &&
10920179193Sjb	    arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA));
10921179198Sjb#endif
10922179193Sjb
10923179193Sjb	act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP);
10924179193Sjb	act->dtad_kind = kind;
10925179193Sjb	act->dtad_ntuple = ntuple;
10926179193Sjb	act->dtad_uarg = uarg;
10927179193Sjb	act->dtad_arg = arg;
10928179193Sjb	act->dtad_refcnt = 1;
10929179193Sjb
10930179193Sjb	return (act);
10931179193Sjb}
10932179193Sjb
10933179193Sjbstatic void
10934179193Sjbdtrace_actdesc_hold(dtrace_actdesc_t *act)
10935179193Sjb{
10936179193Sjb	ASSERT(act->dtad_refcnt >= 1);
10937179193Sjb	act->dtad_refcnt++;
10938179193Sjb}
10939179193Sjb
10940179193Sjbstatic void
10941179193Sjbdtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate)
10942179193Sjb{
10943179193Sjb	dtrace_actkind_t kind = act->dtad_kind;
10944179193Sjb	dtrace_difo_t *dp;
10945179193Sjb
10946179193Sjb	ASSERT(act->dtad_refcnt >= 1);
10947179193Sjb
10948179193Sjb	if (--act->dtad_refcnt != 0)
10949179193Sjb		return;
10950179193Sjb
10951179193Sjb	if ((dp = act->dtad_difo) != NULL)
10952179193Sjb		dtrace_difo_release(dp, vstate);
10953179193Sjb
10954179193Sjb	if (DTRACEACT_ISPRINTFLIKE(kind)) {
10955179193Sjb		char *str = (char *)(uintptr_t)act->dtad_arg;
10956179193Sjb
10957277300Ssmh#ifdef illumos
10958179193Sjb		ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) ||
10959179193Sjb		    (str == NULL && act->dtad_kind == DTRACEACT_PRINTA));
10960179198Sjb#endif
10961179193Sjb
10962179193Sjb		if (str != NULL)
10963179193Sjb			kmem_free(str, strlen(str) + 1);
10964179193Sjb	}
10965179193Sjb
10966179193Sjb	kmem_free(act, sizeof (dtrace_actdesc_t));
10967179193Sjb}
10968179193Sjb
10969179193Sjb/*
10970179193Sjb * DTrace ECB Functions
10971179193Sjb */
10972179193Sjbstatic dtrace_ecb_t *
10973179193Sjbdtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe)
10974179193Sjb{
10975179193Sjb	dtrace_ecb_t *ecb;
10976179193Sjb	dtrace_epid_t epid;
10977179193Sjb
10978179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
10979179193Sjb
10980179193Sjb	ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP);
10981179193Sjb	ecb->dte_predicate = NULL;
10982179193Sjb	ecb->dte_probe = probe;
10983179193Sjb
10984179193Sjb	/*
10985179193Sjb	 * The default size is the size of the default action: recording
10986250574Smarkj	 * the header.
10987179193Sjb	 */
10988250574Smarkj	ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t);
10989179193Sjb	ecb->dte_alignment = sizeof (dtrace_epid_t);
10990179193Sjb
10991179193Sjb	epid = state->dts_epid++;
10992179193Sjb
10993179193Sjb	if (epid - 1 >= state->dts_necbs) {
10994179193Sjb		dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs;
10995179193Sjb		int necbs = state->dts_necbs << 1;
10996179193Sjb
10997179193Sjb		ASSERT(epid == state->dts_necbs + 1);
10998179193Sjb
10999179193Sjb		if (necbs == 0) {
11000179193Sjb			ASSERT(oecbs == NULL);
11001179193Sjb			necbs = 1;
11002179193Sjb		}
11003179193Sjb
11004179193Sjb		ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP);
11005179193Sjb
11006179193Sjb		if (oecbs != NULL)
11007179193Sjb			bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs));
11008179193Sjb
11009179193Sjb		dtrace_membar_producer();
11010179193Sjb		state->dts_ecbs = ecbs;
11011179193Sjb
11012179193Sjb		if (oecbs != NULL) {
11013179193Sjb			/*
11014179193Sjb			 * If this state is active, we must dtrace_sync()
11015179193Sjb			 * before we can free the old dts_ecbs array:  we're
11016179193Sjb			 * coming in hot, and there may be active ring
11017179193Sjb			 * buffer processing (which indexes into the dts_ecbs
11018179193Sjb			 * array) on another CPU.
11019179193Sjb			 */
11020179193Sjb			if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
11021179193Sjb				dtrace_sync();
11022179193Sjb
11023179193Sjb			kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs));
11024179193Sjb		}
11025179193Sjb
11026179193Sjb		dtrace_membar_producer();
11027179193Sjb		state->dts_necbs = necbs;
11028179193Sjb	}
11029179193Sjb
11030179193Sjb	ecb->dte_state = state;
11031179193Sjb
11032179193Sjb	ASSERT(state->dts_ecbs[epid - 1] == NULL);
11033179193Sjb	dtrace_membar_producer();
11034179193Sjb	state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb;
11035179193Sjb
11036179193Sjb	return (ecb);
11037179193Sjb}
11038179193Sjb
11039179193Sjbstatic void
11040179193Sjbdtrace_ecb_enable(dtrace_ecb_t *ecb)
11041179193Sjb{
11042179193Sjb	dtrace_probe_t *probe = ecb->dte_probe;
11043179193Sjb
11044179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
11045179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
11046179193Sjb	ASSERT(ecb->dte_next == NULL);
11047179193Sjb
11048179193Sjb	if (probe == NULL) {
11049179193Sjb		/*
11050179193Sjb		 * This is the NULL probe -- there's nothing to do.
11051179193Sjb		 */
11052179193Sjb		return;
11053179193Sjb	}
11054179193Sjb
11055179193Sjb	if (probe->dtpr_ecb == NULL) {
11056179193Sjb		dtrace_provider_t *prov = probe->dtpr_provider;
11057179193Sjb
11058179193Sjb		/*
11059179193Sjb		 * We're the first ECB on this probe.
11060179193Sjb		 */
11061179193Sjb		probe->dtpr_ecb = probe->dtpr_ecb_last = ecb;
11062179193Sjb
11063179193Sjb		if (ecb->dte_predicate != NULL)
11064179193Sjb			probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid;
11065179193Sjb
11066179193Sjb		prov->dtpv_pops.dtps_enable(prov->dtpv_arg,
11067179193Sjb		    probe->dtpr_id, probe->dtpr_arg);
11068179193Sjb	} else {
11069179193Sjb		/*
11070179193Sjb		 * This probe is already active.  Swing the last pointer to
11071179193Sjb		 * point to the new ECB, and issue a dtrace_sync() to assure
11072179193Sjb		 * that all CPUs have seen the change.
11073179193Sjb		 */
11074179193Sjb		ASSERT(probe->dtpr_ecb_last != NULL);
11075179193Sjb		probe->dtpr_ecb_last->dte_next = ecb;
11076179193Sjb		probe->dtpr_ecb_last = ecb;
11077179193Sjb		probe->dtpr_predcache = 0;
11078179193Sjb
11079179193Sjb		dtrace_sync();
11080179193Sjb	}
11081179193Sjb}
11082179193Sjb
11083179193Sjbstatic void
11084179193Sjbdtrace_ecb_resize(dtrace_ecb_t *ecb)
11085179193Sjb{
11086179193Sjb	dtrace_action_t *act;
11087250574Smarkj	uint32_t curneeded = UINT32_MAX;
11088179193Sjb	uint32_t aggbase = UINT32_MAX;
11089179193Sjb
11090179193Sjb	/*
11091250574Smarkj	 * If we record anything, we always record the dtrace_rechdr_t.  (And
11092250574Smarkj	 * we always record it first.)
11093179193Sjb	 */
11094250574Smarkj	ecb->dte_size = sizeof (dtrace_rechdr_t);
11095250574Smarkj	ecb->dte_alignment = sizeof (dtrace_epid_t);
11096179193Sjb
11097179193Sjb	for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
11098179193Sjb		dtrace_recdesc_t *rec = &act->dta_rec;
11099250574Smarkj		ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1);
11100179193Sjb
11101250574Smarkj		ecb->dte_alignment = MAX(ecb->dte_alignment,
11102250574Smarkj		    rec->dtrd_alignment);
11103179193Sjb
11104179193Sjb		if (DTRACEACT_ISAGG(act->dta_kind)) {
11105179193Sjb			dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
11106179193Sjb
11107250574Smarkj			ASSERT(rec->dtrd_size != 0);
11108250574Smarkj			ASSERT(agg->dtag_first != NULL);
11109250574Smarkj			ASSERT(act->dta_prev->dta_intuple);
11110179193Sjb			ASSERT(aggbase != UINT32_MAX);
11111250574Smarkj			ASSERT(curneeded != UINT32_MAX);
11112179193Sjb
11113179193Sjb			agg->dtag_base = aggbase;
11114179193Sjb
11115250574Smarkj			curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
11116250574Smarkj			rec->dtrd_offset = curneeded;
11117250574Smarkj			curneeded += rec->dtrd_size;
11118250574Smarkj			ecb->dte_needed = MAX(ecb->dte_needed, curneeded);
11119179193Sjb
11120250574Smarkj			aggbase = UINT32_MAX;
11121250574Smarkj			curneeded = UINT32_MAX;
11122250574Smarkj		} else if (act->dta_intuple) {
11123250574Smarkj			if (curneeded == UINT32_MAX) {
11124250574Smarkj				/*
11125250574Smarkj				 * This is the first record in a tuple.  Align
11126250574Smarkj				 * curneeded to be at offset 4 in an 8-byte
11127250574Smarkj				 * aligned block.
11128250574Smarkj				 */
11129250574Smarkj				ASSERT(act->dta_prev == NULL ||
11130250574Smarkj				    !act->dta_prev->dta_intuple);
11131250574Smarkj				ASSERT3U(aggbase, ==, UINT32_MAX);
11132250574Smarkj				curneeded = P2PHASEUP(ecb->dte_size,
11133250574Smarkj				    sizeof (uint64_t), sizeof (dtrace_aggid_t));
11134250574Smarkj
11135250574Smarkj				aggbase = curneeded - sizeof (dtrace_aggid_t);
11136250574Smarkj				ASSERT(IS_P2ALIGNED(aggbase,
11137250574Smarkj				    sizeof (uint64_t)));
11138179193Sjb			}
11139250574Smarkj			curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
11140250574Smarkj			rec->dtrd_offset = curneeded;
11141250574Smarkj			curneeded += rec->dtrd_size;
11142179193Sjb		} else {
11143250574Smarkj			/* tuples must be followed by an aggregation */
11144250574Smarkj			ASSERT(act->dta_prev == NULL ||
11145250574Smarkj			    !act->dta_prev->dta_intuple);
11146179193Sjb
11147250574Smarkj			ecb->dte_size = P2ROUNDUP(ecb->dte_size,
11148250574Smarkj			    rec->dtrd_alignment);
11149250574Smarkj			rec->dtrd_offset = ecb->dte_size;
11150250574Smarkj			ecb->dte_size += rec->dtrd_size;
11151250574Smarkj			ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size);
11152179193Sjb		}
11153179193Sjb	}
11154179193Sjb
11155179193Sjb	if ((act = ecb->dte_action) != NULL &&
11156179193Sjb	    !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) &&
11157250574Smarkj	    ecb->dte_size == sizeof (dtrace_rechdr_t)) {
11158179193Sjb		/*
11159250574Smarkj		 * If the size is still sizeof (dtrace_rechdr_t), then all
11160179193Sjb		 * actions store no data; set the size to 0.
11161179193Sjb		 */
11162179193Sjb		ecb->dte_size = 0;
11163179193Sjb	}
11164179193Sjb
11165250574Smarkj	ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t));
11166250574Smarkj	ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t)));
11167250574Smarkj	ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed,
11168250574Smarkj	    ecb->dte_needed);
11169179193Sjb}
11170179193Sjb
11171179193Sjbstatic dtrace_action_t *
11172179193Sjbdtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
11173179193Sjb{
11174179193Sjb	dtrace_aggregation_t *agg;
11175179193Sjb	size_t size = sizeof (uint64_t);
11176179193Sjb	int ntuple = desc->dtad_ntuple;
11177179193Sjb	dtrace_action_t *act;
11178179193Sjb	dtrace_recdesc_t *frec;
11179179193Sjb	dtrace_aggid_t aggid;
11180179193Sjb	dtrace_state_t *state = ecb->dte_state;
11181179193Sjb
11182179193Sjb	agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP);
11183179193Sjb	agg->dtag_ecb = ecb;
11184179193Sjb
11185179193Sjb	ASSERT(DTRACEACT_ISAGG(desc->dtad_kind));
11186179193Sjb
11187179193Sjb	switch (desc->dtad_kind) {
11188179193Sjb	case DTRACEAGG_MIN:
11189179193Sjb		agg->dtag_initial = INT64_MAX;
11190179193Sjb		agg->dtag_aggregate = dtrace_aggregate_min;
11191179193Sjb		break;
11192179193Sjb
11193179193Sjb	case DTRACEAGG_MAX:
11194179193Sjb		agg->dtag_initial = INT64_MIN;
11195179193Sjb		agg->dtag_aggregate = dtrace_aggregate_max;
11196179193Sjb		break;
11197179193Sjb
11198179193Sjb	case DTRACEAGG_COUNT:
11199179193Sjb		agg->dtag_aggregate = dtrace_aggregate_count;
11200179193Sjb		break;
11201179193Sjb
11202179193Sjb	case DTRACEAGG_QUANTIZE:
11203179193Sjb		agg->dtag_aggregate = dtrace_aggregate_quantize;
11204179193Sjb		size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) *
11205179193Sjb		    sizeof (uint64_t);
11206179193Sjb		break;
11207179193Sjb
11208179193Sjb	case DTRACEAGG_LQUANTIZE: {
11209179193Sjb		uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg);
11210179193Sjb		uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg);
11211179193Sjb
11212179193Sjb		agg->dtag_initial = desc->dtad_arg;
11213179193Sjb		agg->dtag_aggregate = dtrace_aggregate_lquantize;
11214179193Sjb
11215179193Sjb		if (step == 0 || levels == 0)
11216179193Sjb			goto err;
11217179193Sjb
11218179193Sjb		size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t);
11219179193Sjb		break;
11220179193Sjb	}
11221179193Sjb
11222237624Spfg	case DTRACEAGG_LLQUANTIZE: {
11223237624Spfg		uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg);
11224237624Spfg		uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg);
11225237624Spfg		uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg);
11226237624Spfg		uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg);
11227237624Spfg		int64_t v;
11228237624Spfg
11229237624Spfg		agg->dtag_initial = desc->dtad_arg;
11230237624Spfg		agg->dtag_aggregate = dtrace_aggregate_llquantize;
11231237624Spfg
11232237624Spfg		if (factor < 2 || low >= high || nsteps < factor)
11233237624Spfg			goto err;
11234237624Spfg
11235237624Spfg		/*
11236237624Spfg		 * Now check that the number of steps evenly divides a power
11237237624Spfg		 * of the factor.  (This assures both integer bucket size and
11238237624Spfg		 * linearity within each magnitude.)
11239237624Spfg		 */
11240237624Spfg		for (v = factor; v < nsteps; v *= factor)
11241237624Spfg			continue;
11242237624Spfg
11243237624Spfg		if ((v % nsteps) || (nsteps % factor))
11244237624Spfg			goto err;
11245237624Spfg
11246237624Spfg		size = (dtrace_aggregate_llquantize_bucket(factor,
11247237624Spfg		    low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t);
11248237624Spfg		break;
11249237624Spfg	}
11250237624Spfg
11251179193Sjb	case DTRACEAGG_AVG:
11252179193Sjb		agg->dtag_aggregate = dtrace_aggregate_avg;
11253179193Sjb		size = sizeof (uint64_t) * 2;
11254179193Sjb		break;
11255179193Sjb
11256179193Sjb	case DTRACEAGG_STDDEV:
11257179193Sjb		agg->dtag_aggregate = dtrace_aggregate_stddev;
11258179193Sjb		size = sizeof (uint64_t) * 4;
11259179193Sjb		break;
11260179193Sjb
11261179193Sjb	case DTRACEAGG_SUM:
11262179193Sjb		agg->dtag_aggregate = dtrace_aggregate_sum;
11263179193Sjb		break;
11264179193Sjb
11265179193Sjb	default:
11266179193Sjb		goto err;
11267179193Sjb	}
11268179193Sjb
11269179193Sjb	agg->dtag_action.dta_rec.dtrd_size = size;
11270179193Sjb
11271179193Sjb	if (ntuple == 0)
11272179193Sjb		goto err;
11273179193Sjb
11274179193Sjb	/*
11275179193Sjb	 * We must make sure that we have enough actions for the n-tuple.
11276179193Sjb	 */
11277179193Sjb	for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) {
11278179193Sjb		if (DTRACEACT_ISAGG(act->dta_kind))
11279179193Sjb			break;
11280179193Sjb
11281179193Sjb		if (--ntuple == 0) {
11282179193Sjb			/*
11283179193Sjb			 * This is the action with which our n-tuple begins.
11284179193Sjb			 */
11285179193Sjb			agg->dtag_first = act;
11286179193Sjb			goto success;
11287179193Sjb		}
11288179193Sjb	}
11289179193Sjb
11290179193Sjb	/*
11291179193Sjb	 * This n-tuple is short by ntuple elements.  Return failure.
11292179193Sjb	 */
11293179193Sjb	ASSERT(ntuple != 0);
11294179193Sjberr:
11295179193Sjb	kmem_free(agg, sizeof (dtrace_aggregation_t));
11296179193Sjb	return (NULL);
11297179193Sjb
11298179193Sjbsuccess:
11299179193Sjb	/*
11300179193Sjb	 * If the last action in the tuple has a size of zero, it's actually
11301179193Sjb	 * an expression argument for the aggregating action.
11302179193Sjb	 */
11303179193Sjb	ASSERT(ecb->dte_action_last != NULL);
11304179193Sjb	act = ecb->dte_action_last;
11305179193Sjb
11306179193Sjb	if (act->dta_kind == DTRACEACT_DIFEXPR) {
11307179193Sjb		ASSERT(act->dta_difo != NULL);
11308179193Sjb
11309179193Sjb		if (act->dta_difo->dtdo_rtype.dtdt_size == 0)
11310179193Sjb			agg->dtag_hasarg = 1;
11311179193Sjb	}
11312179193Sjb
11313179193Sjb	/*
11314179193Sjb	 * We need to allocate an id for this aggregation.
11315179193Sjb	 */
11316277300Ssmh#ifdef illumos
11317179193Sjb	aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1,
11318179193Sjb	    VM_BESTFIT | VM_SLEEP);
11319179198Sjb#else
11320260131Smarkj	aggid = alloc_unr(state->dts_aggid_arena);
11321179198Sjb#endif
11322179193Sjb
11323179193Sjb	if (aggid - 1 >= state->dts_naggregations) {
11324179193Sjb		dtrace_aggregation_t **oaggs = state->dts_aggregations;
11325179193Sjb		dtrace_aggregation_t **aggs;
11326179193Sjb		int naggs = state->dts_naggregations << 1;
11327179193Sjb		int onaggs = state->dts_naggregations;
11328179193Sjb
11329179193Sjb		ASSERT(aggid == state->dts_naggregations + 1);
11330179193Sjb
11331179193Sjb		if (naggs == 0) {
11332179193Sjb			ASSERT(oaggs == NULL);
11333179193Sjb			naggs = 1;
11334179193Sjb		}
11335179193Sjb
11336179193Sjb		aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP);
11337179193Sjb
11338179193Sjb		if (oaggs != NULL) {
11339179193Sjb			bcopy(oaggs, aggs, onaggs * sizeof (*aggs));
11340179193Sjb			kmem_free(oaggs, onaggs * sizeof (*aggs));
11341179193Sjb		}
11342179193Sjb
11343179193Sjb		state->dts_aggregations = aggs;
11344179193Sjb		state->dts_naggregations = naggs;
11345179193Sjb	}
11346179193Sjb
11347179193Sjb	ASSERT(state->dts_aggregations[aggid - 1] == NULL);
11348179193Sjb	state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg;
11349179193Sjb
11350179193Sjb	frec = &agg->dtag_first->dta_rec;
11351179193Sjb	if (frec->dtrd_alignment < sizeof (dtrace_aggid_t))
11352179193Sjb		frec->dtrd_alignment = sizeof (dtrace_aggid_t);
11353179193Sjb
11354179193Sjb	for (act = agg->dtag_first; act != NULL; act = act->dta_next) {
11355179193Sjb		ASSERT(!act->dta_intuple);
11356179193Sjb		act->dta_intuple = 1;
11357179193Sjb	}
11358179193Sjb
11359179193Sjb	return (&agg->dtag_action);
11360179193Sjb}
11361179193Sjb
11362179193Sjbstatic void
11363179193Sjbdtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act)
11364179193Sjb{
11365179193Sjb	dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
11366179193Sjb	dtrace_state_t *state = ecb->dte_state;
11367179193Sjb	dtrace_aggid_t aggid = agg->dtag_id;
11368179193Sjb
11369179193Sjb	ASSERT(DTRACEACT_ISAGG(act->dta_kind));
11370277300Ssmh#ifdef illumos
11371179193Sjb	vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1);
11372179198Sjb#else
11373260131Smarkj	free_unr(state->dts_aggid_arena, aggid);
11374179198Sjb#endif
11375179193Sjb
11376179193Sjb	ASSERT(state->dts_aggregations[aggid - 1] == agg);
11377179193Sjb	state->dts_aggregations[aggid - 1] = NULL;
11378179193Sjb
11379179193Sjb	kmem_free(agg, sizeof (dtrace_aggregation_t));
11380179193Sjb}
11381179193Sjb
11382179193Sjbstatic int
11383179193Sjbdtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
11384179193Sjb{
11385179193Sjb	dtrace_action_t *action, *last;
11386179193Sjb	dtrace_difo_t *dp = desc->dtad_difo;
11387179193Sjb	uint32_t size = 0, align = sizeof (uint8_t), mask;
11388179193Sjb	uint16_t format = 0;
11389179193Sjb	dtrace_recdesc_t *rec;
11390179193Sjb	dtrace_state_t *state = ecb->dte_state;
11391179198Sjb	dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize;
11392179193Sjb	uint64_t arg = desc->dtad_arg;
11393179193Sjb
11394179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
11395179193Sjb	ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1);
11396179193Sjb
11397179193Sjb	if (DTRACEACT_ISAGG(desc->dtad_kind)) {
11398179193Sjb		/*
11399179193Sjb		 * If this is an aggregating action, there must be neither
11400179193Sjb		 * a speculate nor a commit on the action chain.
11401179193Sjb		 */
11402179193Sjb		dtrace_action_t *act;
11403179193Sjb
11404179193Sjb		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
11405179193Sjb			if (act->dta_kind == DTRACEACT_COMMIT)
11406179193Sjb				return (EINVAL);
11407179193Sjb
11408179193Sjb			if (act->dta_kind == DTRACEACT_SPECULATE)
11409179193Sjb				return (EINVAL);
11410179193Sjb		}
11411179193Sjb
11412179193Sjb		action = dtrace_ecb_aggregation_create(ecb, desc);
11413179193Sjb
11414179193Sjb		if (action == NULL)
11415179193Sjb			return (EINVAL);
11416179193Sjb	} else {
11417179193Sjb		if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) ||
11418179193Sjb		    (desc->dtad_kind == DTRACEACT_DIFEXPR &&
11419179193Sjb		    dp != NULL && dp->dtdo_destructive)) {
11420179193Sjb			state->dts_destructive = 1;
11421179193Sjb		}
11422179193Sjb
11423179193Sjb		switch (desc->dtad_kind) {
11424179193Sjb		case DTRACEACT_PRINTF:
11425179193Sjb		case DTRACEACT_PRINTA:
11426179193Sjb		case DTRACEACT_SYSTEM:
11427179193Sjb		case DTRACEACT_FREOPEN:
11428248708Spfg		case DTRACEACT_DIFEXPR:
11429179193Sjb			/*
11430179193Sjb			 * We know that our arg is a string -- turn it into a
11431179193Sjb			 * format.
11432179193Sjb			 */
11433179198Sjb			if (arg == 0) {
11434248708Spfg				ASSERT(desc->dtad_kind == DTRACEACT_PRINTA ||
11435248708Spfg				    desc->dtad_kind == DTRACEACT_DIFEXPR);
11436179193Sjb				format = 0;
11437179193Sjb			} else {
11438179198Sjb				ASSERT(arg != 0);
11439277300Ssmh#ifdef illumos
11440179193Sjb				ASSERT(arg > KERNELBASE);
11441179198Sjb#endif
11442179193Sjb				format = dtrace_format_add(state,
11443179193Sjb				    (char *)(uintptr_t)arg);
11444179193Sjb			}
11445179193Sjb
11446179193Sjb			/*FALLTHROUGH*/
11447179193Sjb		case DTRACEACT_LIBACT:
11448248690Spfg		case DTRACEACT_TRACEMEM:
11449248690Spfg		case DTRACEACT_TRACEMEM_DYNSIZE:
11450179193Sjb			if (dp == NULL)
11451179193Sjb				return (EINVAL);
11452179193Sjb
11453179193Sjb			if ((size = dp->dtdo_rtype.dtdt_size) != 0)
11454179193Sjb				break;
11455179193Sjb
11456179193Sjb			if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
11457179193Sjb				if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11458179193Sjb					return (EINVAL);
11459179193Sjb
11460179193Sjb				size = opt[DTRACEOPT_STRSIZE];
11461179193Sjb			}
11462179193Sjb
11463179193Sjb			break;
11464179193Sjb
11465179193Sjb		case DTRACEACT_STACK:
11466179193Sjb			if ((nframes = arg) == 0) {
11467179193Sjb				nframes = opt[DTRACEOPT_STACKFRAMES];
11468179193Sjb				ASSERT(nframes > 0);
11469179193Sjb				arg = nframes;
11470179193Sjb			}
11471179193Sjb
11472179193Sjb			size = nframes * sizeof (pc_t);
11473179193Sjb			break;
11474179193Sjb
11475179193Sjb		case DTRACEACT_JSTACK:
11476179193Sjb			if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0)
11477179193Sjb				strsize = opt[DTRACEOPT_JSTACKSTRSIZE];
11478179193Sjb
11479179193Sjb			if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0)
11480179193Sjb				nframes = opt[DTRACEOPT_JSTACKFRAMES];
11481179193Sjb
11482179193Sjb			arg = DTRACE_USTACK_ARG(nframes, strsize);
11483179193Sjb
11484179193Sjb			/*FALLTHROUGH*/
11485179193Sjb		case DTRACEACT_USTACK:
11486179193Sjb			if (desc->dtad_kind != DTRACEACT_JSTACK &&
11487179193Sjb			    (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) {
11488179193Sjb				strsize = DTRACE_USTACK_STRSIZE(arg);
11489179193Sjb				nframes = opt[DTRACEOPT_USTACKFRAMES];
11490179193Sjb				ASSERT(nframes > 0);
11491179193Sjb				arg = DTRACE_USTACK_ARG(nframes, strsize);
11492179193Sjb			}
11493179193Sjb
11494179193Sjb			/*
11495179193Sjb			 * Save a slot for the pid.
11496179193Sjb			 */
11497179193Sjb			size = (nframes + 1) * sizeof (uint64_t);
11498179193Sjb			size += DTRACE_USTACK_STRSIZE(arg);
11499179193Sjb			size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t)));
11500179193Sjb
11501179193Sjb			break;
11502179193Sjb
11503179193Sjb		case DTRACEACT_SYM:
11504179193Sjb		case DTRACEACT_MOD:
11505179193Sjb			if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) !=
11506179193Sjb			    sizeof (uint64_t)) ||
11507179193Sjb			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11508179193Sjb				return (EINVAL);
11509179193Sjb			break;
11510179193Sjb
11511179193Sjb		case DTRACEACT_USYM:
11512179193Sjb		case DTRACEACT_UMOD:
11513179193Sjb		case DTRACEACT_UADDR:
11514179193Sjb			if (dp == NULL ||
11515179193Sjb			    (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) ||
11516179193Sjb			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11517179193Sjb				return (EINVAL);
11518179193Sjb
11519179193Sjb			/*
11520179193Sjb			 * We have a slot for the pid, plus a slot for the
11521179193Sjb			 * argument.  To keep things simple (aligned with
11522179193Sjb			 * bitness-neutral sizing), we store each as a 64-bit
11523179193Sjb			 * quantity.
11524179193Sjb			 */
11525179193Sjb			size = 2 * sizeof (uint64_t);
11526179193Sjb			break;
11527179193Sjb
11528179193Sjb		case DTRACEACT_STOP:
11529179193Sjb		case DTRACEACT_BREAKPOINT:
11530179193Sjb		case DTRACEACT_PANIC:
11531179193Sjb			break;
11532179193Sjb
11533179193Sjb		case DTRACEACT_CHILL:
11534179193Sjb		case DTRACEACT_DISCARD:
11535179193Sjb		case DTRACEACT_RAISE:
11536179193Sjb			if (dp == NULL)
11537179193Sjb				return (EINVAL);
11538179193Sjb			break;
11539179193Sjb
11540179193Sjb		case DTRACEACT_EXIT:
11541179193Sjb			if (dp == NULL ||
11542179193Sjb			    (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) ||
11543179193Sjb			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
11544179193Sjb				return (EINVAL);
11545179193Sjb			break;
11546179193Sjb
11547179193Sjb		case DTRACEACT_SPECULATE:
11548250574Smarkj			if (ecb->dte_size > sizeof (dtrace_rechdr_t))
11549179193Sjb				return (EINVAL);
11550179193Sjb
11551179193Sjb			if (dp == NULL)
11552179193Sjb				return (EINVAL);
11553179193Sjb
11554179193Sjb			state->dts_speculates = 1;
11555179193Sjb			break;
11556179193Sjb
11557179198Sjb		case DTRACEACT_PRINTM:
11558179198Sjb		    	size = dp->dtdo_rtype.dtdt_size;
11559179198Sjb			break;
11560179198Sjb
11561179198Sjb		case DTRACEACT_PRINTT:
11562179198Sjb		    	size = dp->dtdo_rtype.dtdt_size;
11563179198Sjb			break;
11564179198Sjb
11565179193Sjb		case DTRACEACT_COMMIT: {
11566179193Sjb			dtrace_action_t *act = ecb->dte_action;
11567179193Sjb
11568179193Sjb			for (; act != NULL; act = act->dta_next) {
11569179193Sjb				if (act->dta_kind == DTRACEACT_COMMIT)
11570179193Sjb					return (EINVAL);
11571179193Sjb			}
11572179193Sjb
11573179193Sjb			if (dp == NULL)
11574179193Sjb				return (EINVAL);
11575179193Sjb			break;
11576179193Sjb		}
11577179193Sjb
11578179193Sjb		default:
11579179193Sjb			return (EINVAL);
11580179193Sjb		}
11581179193Sjb
11582179193Sjb		if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) {
11583179193Sjb			/*
11584179193Sjb			 * If this is a data-storing action or a speculate,
11585179193Sjb			 * we must be sure that there isn't a commit on the
11586179193Sjb			 * action chain.
11587179193Sjb			 */
11588179193Sjb			dtrace_action_t *act = ecb->dte_action;
11589179193Sjb
11590179193Sjb			for (; act != NULL; act = act->dta_next) {
11591179193Sjb				if (act->dta_kind == DTRACEACT_COMMIT)
11592179193Sjb					return (EINVAL);
11593179193Sjb			}
11594179193Sjb		}
11595179193Sjb
11596179193Sjb		action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP);
11597179193Sjb		action->dta_rec.dtrd_size = size;
11598179193Sjb	}
11599179193Sjb
11600179193Sjb	action->dta_refcnt = 1;
11601179193Sjb	rec = &action->dta_rec;
11602179193Sjb	size = rec->dtrd_size;
11603179193Sjb
11604179193Sjb	for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) {
11605179193Sjb		if (!(size & mask)) {
11606179193Sjb			align = mask + 1;
11607179193Sjb			break;
11608179193Sjb		}
11609179193Sjb	}
11610179193Sjb
11611179193Sjb	action->dta_kind = desc->dtad_kind;
11612179193Sjb
11613179193Sjb	if ((action->dta_difo = dp) != NULL)
11614179193Sjb		dtrace_difo_hold(dp);
11615179193Sjb
11616179193Sjb	rec->dtrd_action = action->dta_kind;
11617179193Sjb	rec->dtrd_arg = arg;
11618179193Sjb	rec->dtrd_uarg = desc->dtad_uarg;
11619179193Sjb	rec->dtrd_alignment = (uint16_t)align;
11620179193Sjb	rec->dtrd_format = format;
11621179193Sjb
11622179193Sjb	if ((last = ecb->dte_action_last) != NULL) {
11623179193Sjb		ASSERT(ecb->dte_action != NULL);
11624179193Sjb		action->dta_prev = last;
11625179193Sjb		last->dta_next = action;
11626179193Sjb	} else {
11627179193Sjb		ASSERT(ecb->dte_action == NULL);
11628179193Sjb		ecb->dte_action = action;
11629179193Sjb	}
11630179193Sjb
11631179193Sjb	ecb->dte_action_last = action;
11632179193Sjb
11633179193Sjb	return (0);
11634179193Sjb}
11635179193Sjb
11636179193Sjbstatic void
11637179193Sjbdtrace_ecb_action_remove(dtrace_ecb_t *ecb)
11638179193Sjb{
11639179193Sjb	dtrace_action_t *act = ecb->dte_action, *next;
11640179193Sjb	dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate;
11641179193Sjb	dtrace_difo_t *dp;
11642179193Sjb	uint16_t format;
11643179193Sjb
11644179193Sjb	if (act != NULL && act->dta_refcnt > 1) {
11645179193Sjb		ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1);
11646179193Sjb		act->dta_refcnt--;
11647179193Sjb	} else {
11648179193Sjb		for (; act != NULL; act = next) {
11649179193Sjb			next = act->dta_next;
11650179193Sjb			ASSERT(next != NULL || act == ecb->dte_action_last);
11651179193Sjb			ASSERT(act->dta_refcnt == 1);
11652179193Sjb
11653179193Sjb			if ((format = act->dta_rec.dtrd_format) != 0)
11654179193Sjb				dtrace_format_remove(ecb->dte_state, format);
11655179193Sjb
11656179193Sjb			if ((dp = act->dta_difo) != NULL)
11657179193Sjb				dtrace_difo_release(dp, vstate);
11658179193Sjb
11659179193Sjb			if (DTRACEACT_ISAGG(act->dta_kind)) {
11660179193Sjb				dtrace_ecb_aggregation_destroy(ecb, act);
11661179193Sjb			} else {
11662179193Sjb				kmem_free(act, sizeof (dtrace_action_t));
11663179193Sjb			}
11664179193Sjb		}
11665179193Sjb	}
11666179193Sjb
11667179193Sjb	ecb->dte_action = NULL;
11668179193Sjb	ecb->dte_action_last = NULL;
11669250574Smarkj	ecb->dte_size = 0;
11670179193Sjb}
11671179193Sjb
11672179193Sjbstatic void
11673179193Sjbdtrace_ecb_disable(dtrace_ecb_t *ecb)
11674179193Sjb{
11675179193Sjb	/*
11676179193Sjb	 * We disable the ECB by removing it from its probe.
11677179193Sjb	 */
11678179193Sjb	dtrace_ecb_t *pecb, *prev = NULL;
11679179193Sjb	dtrace_probe_t *probe = ecb->dte_probe;
11680179193Sjb
11681179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
11682179193Sjb
11683179193Sjb	if (probe == NULL) {
11684179193Sjb		/*
11685179193Sjb		 * This is the NULL probe; there is nothing to disable.
11686179193Sjb		 */
11687179193Sjb		return;
11688179193Sjb	}
11689179193Sjb
11690179193Sjb	for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) {
11691179193Sjb		if (pecb == ecb)
11692179193Sjb			break;
11693179193Sjb		prev = pecb;
11694179193Sjb	}
11695179193Sjb
11696179193Sjb	ASSERT(pecb != NULL);
11697179193Sjb
11698179193Sjb	if (prev == NULL) {
11699179193Sjb		probe->dtpr_ecb = ecb->dte_next;
11700179193Sjb	} else {
11701179193Sjb		prev->dte_next = ecb->dte_next;
11702179193Sjb	}
11703179193Sjb
11704179193Sjb	if (ecb == probe->dtpr_ecb_last) {
11705179193Sjb		ASSERT(ecb->dte_next == NULL);
11706179193Sjb		probe->dtpr_ecb_last = prev;
11707179193Sjb	}
11708179193Sjb
11709179193Sjb	/*
11710179193Sjb	 * The ECB has been disconnected from the probe; now sync to assure
11711179193Sjb	 * that all CPUs have seen the change before returning.
11712179193Sjb	 */
11713179193Sjb	dtrace_sync();
11714179193Sjb
11715179193Sjb	if (probe->dtpr_ecb == NULL) {
11716179193Sjb		/*
11717179193Sjb		 * That was the last ECB on the probe; clear the predicate
11718179193Sjb		 * cache ID for the probe, disable it and sync one more time
11719179193Sjb		 * to assure that we'll never hit it again.
11720179193Sjb		 */
11721179193Sjb		dtrace_provider_t *prov = probe->dtpr_provider;
11722179193Sjb
11723179193Sjb		ASSERT(ecb->dte_next == NULL);
11724179193Sjb		ASSERT(probe->dtpr_ecb_last == NULL);
11725179193Sjb		probe->dtpr_predcache = DTRACE_CACHEIDNONE;
11726179193Sjb		prov->dtpv_pops.dtps_disable(prov->dtpv_arg,
11727179193Sjb		    probe->dtpr_id, probe->dtpr_arg);
11728179193Sjb		dtrace_sync();
11729179193Sjb	} else {
11730179193Sjb		/*
11731179193Sjb		 * There is at least one ECB remaining on the probe.  If there
11732179193Sjb		 * is _exactly_ one, set the probe's predicate cache ID to be
11733179193Sjb		 * the predicate cache ID of the remaining ECB.
11734179193Sjb		 */
11735179193Sjb		ASSERT(probe->dtpr_ecb_last != NULL);
11736179193Sjb		ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE);
11737179193Sjb
11738179193Sjb		if (probe->dtpr_ecb == probe->dtpr_ecb_last) {
11739179193Sjb			dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate;
11740179193Sjb
11741179193Sjb			ASSERT(probe->dtpr_ecb->dte_next == NULL);
11742179193Sjb
11743179193Sjb			if (p != NULL)
11744179193Sjb				probe->dtpr_predcache = p->dtp_cacheid;
11745179193Sjb		}
11746179193Sjb
11747179193Sjb		ecb->dte_next = NULL;
11748179193Sjb	}
11749179193Sjb}
11750179193Sjb
11751179193Sjbstatic void
11752179193Sjbdtrace_ecb_destroy(dtrace_ecb_t *ecb)
11753179193Sjb{
11754179193Sjb	dtrace_state_t *state = ecb->dte_state;
11755179193Sjb	dtrace_vstate_t *vstate = &state->dts_vstate;
11756179193Sjb	dtrace_predicate_t *pred;
11757179193Sjb	dtrace_epid_t epid = ecb->dte_epid;
11758179193Sjb
11759179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
11760179193Sjb	ASSERT(ecb->dte_next == NULL);
11761179193Sjb	ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb);
11762179193Sjb
11763179193Sjb	if ((pred = ecb->dte_predicate) != NULL)
11764179193Sjb		dtrace_predicate_release(pred, vstate);
11765179193Sjb
11766179193Sjb	dtrace_ecb_action_remove(ecb);
11767179193Sjb
11768179193Sjb	ASSERT(state->dts_ecbs[epid - 1] == ecb);
11769179193Sjb	state->dts_ecbs[epid - 1] = NULL;
11770179193Sjb
11771179193Sjb	kmem_free(ecb, sizeof (dtrace_ecb_t));
11772179193Sjb}
11773179193Sjb
11774179193Sjbstatic dtrace_ecb_t *
11775179193Sjbdtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe,
11776179193Sjb    dtrace_enabling_t *enab)
11777179193Sjb{
11778179193Sjb	dtrace_ecb_t *ecb;
11779179193Sjb	dtrace_predicate_t *pred;
11780179193Sjb	dtrace_actdesc_t *act;
11781179193Sjb	dtrace_provider_t *prov;
11782179193Sjb	dtrace_ecbdesc_t *desc = enab->dten_current;
11783179193Sjb
11784179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
11785179193Sjb	ASSERT(state != NULL);
11786179193Sjb
11787179193Sjb	ecb = dtrace_ecb_add(state, probe);
11788179193Sjb	ecb->dte_uarg = desc->dted_uarg;
11789179193Sjb
11790179193Sjb	if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) {
11791179193Sjb		dtrace_predicate_hold(pred);
11792179193Sjb		ecb->dte_predicate = pred;
11793179193Sjb	}
11794179193Sjb
11795179193Sjb	if (probe != NULL) {
11796179193Sjb		/*
11797179193Sjb		 * If the provider shows more leg than the consumer is old
11798179193Sjb		 * enough to see, we need to enable the appropriate implicit
11799179193Sjb		 * predicate bits to prevent the ecb from activating at
11800179193Sjb		 * revealing times.
11801179193Sjb		 *
11802179193Sjb		 * Providers specifying DTRACE_PRIV_USER at register time
11803179193Sjb		 * are stating that they need the /proc-style privilege
11804179193Sjb		 * model to be enforced, and this is what DTRACE_COND_OWNER
11805179193Sjb		 * and DTRACE_COND_ZONEOWNER will then do at probe time.
11806179193Sjb		 */
11807179193Sjb		prov = probe->dtpr_provider;
11808179193Sjb		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) &&
11809179193Sjb		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
11810179193Sjb			ecb->dte_cond |= DTRACE_COND_OWNER;
11811179193Sjb
11812179193Sjb		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) &&
11813179193Sjb		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
11814179193Sjb			ecb->dte_cond |= DTRACE_COND_ZONEOWNER;
11815179193Sjb
11816179193Sjb		/*
11817179193Sjb		 * If the provider shows us kernel innards and the user
11818179193Sjb		 * is lacking sufficient privilege, enable the
11819179193Sjb		 * DTRACE_COND_USERMODE implicit predicate.
11820179193Sjb		 */
11821179193Sjb		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) &&
11822179193Sjb		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL))
11823179193Sjb			ecb->dte_cond |= DTRACE_COND_USERMODE;
11824179193Sjb	}
11825179193Sjb
11826179193Sjb	if (dtrace_ecb_create_cache != NULL) {
11827179193Sjb		/*
11828179193Sjb		 * If we have a cached ecb, we'll use its action list instead
11829179193Sjb		 * of creating our own (saving both time and space).
11830179193Sjb		 */
11831179193Sjb		dtrace_ecb_t *cached = dtrace_ecb_create_cache;
11832179193Sjb		dtrace_action_t *act = cached->dte_action;
11833179193Sjb
11834179193Sjb		if (act != NULL) {
11835179193Sjb			ASSERT(act->dta_refcnt > 0);
11836179193Sjb			act->dta_refcnt++;
11837179193Sjb			ecb->dte_action = act;
11838179193Sjb			ecb->dte_action_last = cached->dte_action_last;
11839179193Sjb			ecb->dte_needed = cached->dte_needed;
11840179193Sjb			ecb->dte_size = cached->dte_size;
11841179193Sjb			ecb->dte_alignment = cached->dte_alignment;
11842179193Sjb		}
11843179193Sjb
11844179193Sjb		return (ecb);
11845179193Sjb	}
11846179193Sjb
11847179193Sjb	for (act = desc->dted_action; act != NULL; act = act->dtad_next) {
11848179193Sjb		if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) {
11849179193Sjb			dtrace_ecb_destroy(ecb);
11850179193Sjb			return (NULL);
11851179193Sjb		}
11852179193Sjb	}
11853179193Sjb
11854179193Sjb	dtrace_ecb_resize(ecb);
11855179193Sjb
11856179193Sjb	return (dtrace_ecb_create_cache = ecb);
11857179193Sjb}
11858179193Sjb
11859179193Sjbstatic int
11860179193Sjbdtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg)
11861179193Sjb{
11862179193Sjb	dtrace_ecb_t *ecb;
11863179193Sjb	dtrace_enabling_t *enab = arg;
11864179193Sjb	dtrace_state_t *state = enab->dten_vstate->dtvs_state;
11865179193Sjb
11866179193Sjb	ASSERT(state != NULL);
11867179193Sjb
11868179193Sjb	if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) {
11869179193Sjb		/*
11870179193Sjb		 * This probe was created in a generation for which this
11871179193Sjb		 * enabling has previously created ECBs; we don't want to
11872179193Sjb		 * enable it again, so just kick out.
11873179193Sjb		 */
11874179193Sjb		return (DTRACE_MATCH_NEXT);
11875179193Sjb	}
11876179193Sjb
11877179193Sjb	if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL)
11878179193Sjb		return (DTRACE_MATCH_DONE);
11879179193Sjb
11880179193Sjb	dtrace_ecb_enable(ecb);
11881179193Sjb	return (DTRACE_MATCH_NEXT);
11882179193Sjb}
11883179193Sjb
11884179193Sjbstatic dtrace_ecb_t *
11885179193Sjbdtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id)
11886179193Sjb{
11887179193Sjb	dtrace_ecb_t *ecb;
11888179193Sjb
11889179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
11890179193Sjb
11891179193Sjb	if (id == 0 || id > state->dts_necbs)
11892179193Sjb		return (NULL);
11893179193Sjb
11894179193Sjb	ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL);
11895179193Sjb	ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id);
11896179193Sjb
11897179193Sjb	return (state->dts_ecbs[id - 1]);
11898179193Sjb}
11899179193Sjb
11900179193Sjbstatic dtrace_aggregation_t *
11901179193Sjbdtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id)
11902179193Sjb{
11903179193Sjb	dtrace_aggregation_t *agg;
11904179193Sjb
11905179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
11906179193Sjb
11907179193Sjb	if (id == 0 || id > state->dts_naggregations)
11908179193Sjb		return (NULL);
11909179193Sjb
11910179193Sjb	ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL);
11911179193Sjb	ASSERT((agg = state->dts_aggregations[id - 1]) == NULL ||
11912179193Sjb	    agg->dtag_id == id);
11913179193Sjb
11914179193Sjb	return (state->dts_aggregations[id - 1]);
11915179193Sjb}
11916179193Sjb
11917179193Sjb/*
11918179193Sjb * DTrace Buffer Functions
11919179193Sjb *
11920179193Sjb * The following functions manipulate DTrace buffers.  Most of these functions
11921179193Sjb * are called in the context of establishing or processing consumer state;
11922179193Sjb * exceptions are explicitly noted.
11923179193Sjb */
11924179193Sjb
11925179193Sjb/*
11926179193Sjb * Note:  called from cross call context.  This function switches the two
11927179193Sjb * buffers on a given CPU.  The atomicity of this operation is assured by
11928179193Sjb * disabling interrupts while the actual switch takes place; the disabling of
11929179193Sjb * interrupts serializes the execution with any execution of dtrace_probe() on
11930179193Sjb * the same CPU.
11931179193Sjb */
11932179193Sjbstatic void
11933179193Sjbdtrace_buffer_switch(dtrace_buffer_t *buf)
11934179193Sjb{
11935179193Sjb	caddr_t tomax = buf->dtb_tomax;
11936179193Sjb	caddr_t xamot = buf->dtb_xamot;
11937179193Sjb	dtrace_icookie_t cookie;
11938250574Smarkj	hrtime_t now;
11939179193Sjb
11940179193Sjb	ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
11941179193Sjb	ASSERT(!(buf->dtb_flags & DTRACEBUF_RING));
11942179193Sjb
11943179193Sjb	cookie = dtrace_interrupt_disable();
11944250574Smarkj	now = dtrace_gethrtime();
11945179193Sjb	buf->dtb_tomax = xamot;
11946179193Sjb	buf->dtb_xamot = tomax;
11947179193Sjb	buf->dtb_xamot_drops = buf->dtb_drops;
11948179193Sjb	buf->dtb_xamot_offset = buf->dtb_offset;
11949179193Sjb	buf->dtb_xamot_errors = buf->dtb_errors;
11950179193Sjb	buf->dtb_xamot_flags = buf->dtb_flags;
11951179193Sjb	buf->dtb_offset = 0;
11952179193Sjb	buf->dtb_drops = 0;
11953179193Sjb	buf->dtb_errors = 0;
11954179193Sjb	buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED);
11955248983Spfg	buf->dtb_interval = now - buf->dtb_switched;
11956248983Spfg	buf->dtb_switched = now;
11957179193Sjb	dtrace_interrupt_enable(cookie);
11958179193Sjb}
11959179193Sjb
11960179193Sjb/*
11961179193Sjb * Note:  called from cross call context.  This function activates a buffer
11962179193Sjb * on a CPU.  As with dtrace_buffer_switch(), the atomicity of the operation
11963179193Sjb * is guaranteed by the disabling of interrupts.
11964179193Sjb */
11965179193Sjbstatic void
11966179193Sjbdtrace_buffer_activate(dtrace_state_t *state)
11967179193Sjb{
11968179193Sjb	dtrace_buffer_t *buf;
11969179193Sjb	dtrace_icookie_t cookie = dtrace_interrupt_disable();
11970179193Sjb
11971179198Sjb	buf = &state->dts_buffer[curcpu];
11972179193Sjb
11973179193Sjb	if (buf->dtb_tomax != NULL) {
11974179193Sjb		/*
11975179193Sjb		 * We might like to assert that the buffer is marked inactive,
11976179193Sjb		 * but this isn't necessarily true:  the buffer for the CPU
11977179193Sjb		 * that processes the BEGIN probe has its buffer activated
11978179193Sjb		 * manually.  In this case, we take the (harmless) action
11979179193Sjb		 * re-clearing the bit INACTIVE bit.
11980179193Sjb		 */
11981179193Sjb		buf->dtb_flags &= ~DTRACEBUF_INACTIVE;
11982179193Sjb	}
11983179193Sjb
11984179193Sjb	dtrace_interrupt_enable(cookie);
11985179193Sjb}
11986179193Sjb
11987297773Smarkj#ifdef __FreeBSD__
11988297773Smarkj/*
11989297773Smarkj * Activate the specified per-CPU buffer.  This is used instead of
11990297773Smarkj * dtrace_buffer_activate() when APs have not yet started, i.e. when
11991297773Smarkj * activating anonymous state.
11992297773Smarkj */
11993297773Smarkjstatic void
11994297773Smarkjdtrace_buffer_activate_cpu(dtrace_state_t *state, int cpu)
11995297773Smarkj{
11996297773Smarkj
11997297773Smarkj	if (state->dts_buffer[cpu].dtb_tomax != NULL)
11998297773Smarkj		state->dts_buffer[cpu].dtb_flags &= ~DTRACEBUF_INACTIVE;
11999297773Smarkj}
12000297773Smarkj#endif
12001297773Smarkj
12002179193Sjbstatic int
12003179193Sjbdtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags,
12004262330Smarkj    processorid_t cpu, int *factor)
12005179193Sjb{
12006277300Ssmh#ifdef illumos
12007179193Sjb	cpu_t *cp;
12008179198Sjb#endif
12009179193Sjb	dtrace_buffer_t *buf;
12010262330Smarkj	int allocated = 0, desired = 0;
12011179193Sjb
12012277300Ssmh#ifdef illumos
12013179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
12014179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
12015179193Sjb
12016262330Smarkj	*factor = 1;
12017262330Smarkj
12018179193Sjb	if (size > dtrace_nonroot_maxsize &&
12019179193Sjb	    !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE))
12020179193Sjb		return (EFBIG);
12021179193Sjb
12022179193Sjb	cp = cpu_list;
12023179193Sjb
12024179193Sjb	do {
12025179193Sjb		if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
12026179193Sjb			continue;
12027179193Sjb
12028179193Sjb		buf = &bufs[cp->cpu_id];
12029179193Sjb
12030179193Sjb		/*
12031179193Sjb		 * If there is already a buffer allocated for this CPU, it
12032179193Sjb		 * is only possible that this is a DR event.  In this case,
12033179193Sjb		 */
12034179193Sjb		if (buf->dtb_tomax != NULL) {
12035179193Sjb			ASSERT(buf->dtb_size == size);
12036179193Sjb			continue;
12037179193Sjb		}
12038179193Sjb
12039179193Sjb		ASSERT(buf->dtb_xamot == NULL);
12040179193Sjb
12041262330Smarkj		if ((buf->dtb_tomax = kmem_zalloc(size,
12042262330Smarkj		    KM_NOSLEEP | KM_NORMALPRI)) == NULL)
12043179193Sjb			goto err;
12044179193Sjb
12045179193Sjb		buf->dtb_size = size;
12046179193Sjb		buf->dtb_flags = flags;
12047179193Sjb		buf->dtb_offset = 0;
12048179193Sjb		buf->dtb_drops = 0;
12049179193Sjb
12050179193Sjb		if (flags & DTRACEBUF_NOSWITCH)
12051179193Sjb			continue;
12052179193Sjb
12053262330Smarkj		if ((buf->dtb_xamot = kmem_zalloc(size,
12054262330Smarkj		    KM_NOSLEEP | KM_NORMALPRI)) == NULL)
12055179193Sjb			goto err;
12056179193Sjb	} while ((cp = cp->cpu_next) != cpu_list);
12057179193Sjb
12058179193Sjb	return (0);
12059179193Sjb
12060179193Sjberr:
12061179193Sjb	cp = cpu_list;
12062179193Sjb
12063179193Sjb	do {
12064179193Sjb		if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
12065179193Sjb			continue;
12066179193Sjb
12067179193Sjb		buf = &bufs[cp->cpu_id];
12068262330Smarkj		desired += 2;
12069179193Sjb
12070179193Sjb		if (buf->dtb_xamot != NULL) {
12071179193Sjb			ASSERT(buf->dtb_tomax != NULL);
12072179193Sjb			ASSERT(buf->dtb_size == size);
12073179193Sjb			kmem_free(buf->dtb_xamot, size);
12074262330Smarkj			allocated++;
12075179193Sjb		}
12076179193Sjb
12077179193Sjb		if (buf->dtb_tomax != NULL) {
12078179193Sjb			ASSERT(buf->dtb_size == size);
12079179193Sjb			kmem_free(buf->dtb_tomax, size);
12080262330Smarkj			allocated++;
12081179193Sjb		}
12082179193Sjb
12083179193Sjb		buf->dtb_tomax = NULL;
12084179193Sjb		buf->dtb_xamot = NULL;
12085179193Sjb		buf->dtb_size = 0;
12086179193Sjb	} while ((cp = cp->cpu_next) != cpu_list);
12087179198Sjb#else
12088179198Sjb	int i;
12089179198Sjb
12090262330Smarkj	*factor = 1;
12091285009Sbr#if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
12092300618Sbr    defined(__mips__) || defined(__powerpc__) || defined(__riscv__)
12093179198Sjb	/*
12094179198Sjb	 * FreeBSD isn't good at limiting the amount of memory we
12095179198Sjb	 * ask to malloc, so let's place a limit here before trying
12096179198Sjb	 * to do something that might well end in tears at bedtime.
12097179198Sjb	 */
12098179198Sjb	if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1)))
12099262330Smarkj		return (ENOMEM);
12100179198Sjb#endif
12101179198Sjb
12102179198Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
12103209059Sjhb	CPU_FOREACH(i) {
12104179198Sjb		if (cpu != DTRACE_CPUALL && cpu != i)
12105179198Sjb			continue;
12106179198Sjb
12107179198Sjb		buf = &bufs[i];
12108179198Sjb
12109179198Sjb		/*
12110179198Sjb		 * If there is already a buffer allocated for this CPU, it
12111179198Sjb		 * is only possible that this is a DR event.  In this case,
12112179198Sjb		 * the buffer size must match our specified size.
12113179198Sjb		 */
12114179198Sjb		if (buf->dtb_tomax != NULL) {
12115179198Sjb			ASSERT(buf->dtb_size == size);
12116179198Sjb			continue;
12117179198Sjb		}
12118179198Sjb
12119179198Sjb		ASSERT(buf->dtb_xamot == NULL);
12120179198Sjb
12121262330Smarkj		if ((buf->dtb_tomax = kmem_zalloc(size,
12122262330Smarkj		    KM_NOSLEEP | KM_NORMALPRI)) == NULL)
12123179198Sjb			goto err;
12124179198Sjb
12125179198Sjb		buf->dtb_size = size;
12126179198Sjb		buf->dtb_flags = flags;
12127179198Sjb		buf->dtb_offset = 0;
12128179198Sjb		buf->dtb_drops = 0;
12129179198Sjb
12130179198Sjb		if (flags & DTRACEBUF_NOSWITCH)
12131179198Sjb			continue;
12132179198Sjb
12133262330Smarkj		if ((buf->dtb_xamot = kmem_zalloc(size,
12134262330Smarkj		    KM_NOSLEEP | KM_NORMALPRI)) == NULL)
12135179198Sjb			goto err;
12136179198Sjb	}
12137179198Sjb
12138179198Sjb	return (0);
12139179198Sjb
12140179198Sjberr:
12141179198Sjb	/*
12142179198Sjb	 * Error allocating memory, so free the buffers that were
12143179198Sjb	 * allocated before the failed allocation.
12144179198Sjb	 */
12145209059Sjhb	CPU_FOREACH(i) {
12146179198Sjb		if (cpu != DTRACE_CPUALL && cpu != i)
12147179198Sjb			continue;
12148179198Sjb
12149179198Sjb		buf = &bufs[i];
12150262330Smarkj		desired += 2;
12151179198Sjb
12152179198Sjb		if (buf->dtb_xamot != NULL) {
12153179198Sjb			ASSERT(buf->dtb_tomax != NULL);
12154179198Sjb			ASSERT(buf->dtb_size == size);
12155179198Sjb			kmem_free(buf->dtb_xamot, size);
12156262330Smarkj			allocated++;
12157179198Sjb		}
12158179198Sjb
12159179198Sjb		if (buf->dtb_tomax != NULL) {
12160179198Sjb			ASSERT(buf->dtb_size == size);
12161179198Sjb			kmem_free(buf->dtb_tomax, size);
12162262330Smarkj			allocated++;
12163179198Sjb		}
12164179198Sjb
12165179198Sjb		buf->dtb_tomax = NULL;
12166179198Sjb		buf->dtb_xamot = NULL;
12167179198Sjb		buf->dtb_size = 0;
12168179198Sjb
12169179198Sjb	}
12170262330Smarkj#endif
12171262330Smarkj	*factor = desired / (allocated > 0 ? allocated : 1);
12172179198Sjb
12173179198Sjb	return (ENOMEM);
12174179193Sjb}
12175179193Sjb
12176179193Sjb/*
12177179193Sjb * Note:  called from probe context.  This function just increments the drop
12178179193Sjb * count on a buffer.  It has been made a function to allow for the
12179179193Sjb * possibility of understanding the source of mysterious drop counts.  (A
12180179193Sjb * problem for which one may be particularly disappointed that DTrace cannot
12181179193Sjb * be used to understand DTrace.)
12182179193Sjb */
12183179193Sjbstatic void
12184179193Sjbdtrace_buffer_drop(dtrace_buffer_t *buf)
12185179193Sjb{
12186179193Sjb	buf->dtb_drops++;
12187179193Sjb}
12188179193Sjb
12189179193Sjb/*
12190179193Sjb * Note:  called from probe context.  This function is called to reserve space
12191179193Sjb * in a buffer.  If mstate is non-NULL, sets the scratch base and size in the
12192179193Sjb * mstate.  Returns the new offset in the buffer, or a negative value if an
12193179193Sjb * error has occurred.
12194179193Sjb */
12195179193Sjbstatic intptr_t
12196179193Sjbdtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align,
12197179193Sjb    dtrace_state_t *state, dtrace_mstate_t *mstate)
12198179193Sjb{
12199179193Sjb	intptr_t offs = buf->dtb_offset, soffs;
12200179193Sjb	intptr_t woffs;
12201179193Sjb	caddr_t tomax;
12202179193Sjb	size_t total;
12203179193Sjb
12204179193Sjb	if (buf->dtb_flags & DTRACEBUF_INACTIVE)
12205179193Sjb		return (-1);
12206179193Sjb
12207179193Sjb	if ((tomax = buf->dtb_tomax) == NULL) {
12208179193Sjb		dtrace_buffer_drop(buf);
12209179193Sjb		return (-1);
12210179193Sjb	}
12211179193Sjb
12212179193Sjb	if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) {
12213179193Sjb		while (offs & (align - 1)) {
12214179193Sjb			/*
12215179193Sjb			 * Assert that our alignment is off by a number which
12216179193Sjb			 * is itself sizeof (uint32_t) aligned.
12217179193Sjb			 */
12218179193Sjb			ASSERT(!((align - (offs & (align - 1))) &
12219179193Sjb			    (sizeof (uint32_t) - 1)));
12220179193Sjb			DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
12221179193Sjb			offs += sizeof (uint32_t);
12222179193Sjb		}
12223179193Sjb
12224179193Sjb		if ((soffs = offs + needed) > buf->dtb_size) {
12225179193Sjb			dtrace_buffer_drop(buf);
12226179193Sjb			return (-1);
12227179193Sjb		}
12228179193Sjb
12229179193Sjb		if (mstate == NULL)
12230179193Sjb			return (offs);
12231179193Sjb
12232179193Sjb		mstate->dtms_scratch_base = (uintptr_t)tomax + soffs;
12233179193Sjb		mstate->dtms_scratch_size = buf->dtb_size - soffs;
12234179193Sjb		mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
12235179193Sjb
12236179193Sjb		return (offs);
12237179193Sjb	}
12238179193Sjb
12239179193Sjb	if (buf->dtb_flags & DTRACEBUF_FILL) {
12240179193Sjb		if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN &&
12241179193Sjb		    (buf->dtb_flags & DTRACEBUF_FULL))
12242179193Sjb			return (-1);
12243179193Sjb		goto out;
12244179193Sjb	}
12245179193Sjb
12246179193Sjb	total = needed + (offs & (align - 1));
12247179193Sjb
12248179193Sjb	/*
12249179193Sjb	 * For a ring buffer, life is quite a bit more complicated.  Before
12250179193Sjb	 * we can store any padding, we need to adjust our wrapping offset.
12251179193Sjb	 * (If we've never before wrapped or we're not about to, no adjustment
12252179193Sjb	 * is required.)
12253179193Sjb	 */
12254179193Sjb	if ((buf->dtb_flags & DTRACEBUF_WRAPPED) ||
12255179193Sjb	    offs + total > buf->dtb_size) {
12256179193Sjb		woffs = buf->dtb_xamot_offset;
12257179193Sjb
12258179193Sjb		if (offs + total > buf->dtb_size) {
12259179193Sjb			/*
12260179193Sjb			 * We can't fit in the end of the buffer.  First, a
12261179193Sjb			 * sanity check that we can fit in the buffer at all.
12262179193Sjb			 */
12263179193Sjb			if (total > buf->dtb_size) {
12264179193Sjb				dtrace_buffer_drop(buf);
12265179193Sjb				return (-1);
12266179193Sjb			}
12267179193Sjb
12268179193Sjb			/*
12269179193Sjb			 * We're going to be storing at the top of the buffer,
12270179193Sjb			 * so now we need to deal with the wrapped offset.  We
12271179193Sjb			 * only reset our wrapped offset to 0 if it is
12272179193Sjb			 * currently greater than the current offset.  If it
12273179193Sjb			 * is less than the current offset, it is because a
12274179193Sjb			 * previous allocation induced a wrap -- but the
12275179193Sjb			 * allocation didn't subsequently take the space due
12276179193Sjb			 * to an error or false predicate evaluation.  In this
12277179193Sjb			 * case, we'll just leave the wrapped offset alone: if
12278179193Sjb			 * the wrapped offset hasn't been advanced far enough
12279179193Sjb			 * for this allocation, it will be adjusted in the
12280179193Sjb			 * lower loop.
12281179193Sjb			 */
12282179193Sjb			if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
12283179193Sjb				if (woffs >= offs)
12284179193Sjb					woffs = 0;
12285179193Sjb			} else {
12286179193Sjb				woffs = 0;
12287179193Sjb			}
12288179193Sjb
12289179193Sjb			/*
12290179193Sjb			 * Now we know that we're going to be storing to the
12291179193Sjb			 * top of the buffer and that there is room for us
12292179193Sjb			 * there.  We need to clear the buffer from the current
12293179193Sjb			 * offset to the end (there may be old gunk there).
12294179193Sjb			 */
12295179193Sjb			while (offs < buf->dtb_size)
12296179193Sjb				tomax[offs++] = 0;
12297179193Sjb
12298179193Sjb			/*
12299179193Sjb			 * We need to set our offset to zero.  And because we
12300179193Sjb			 * are wrapping, we need to set the bit indicating as
12301179193Sjb			 * much.  We can also adjust our needed space back
12302179193Sjb			 * down to the space required by the ECB -- we know
12303179193Sjb			 * that the top of the buffer is aligned.
12304179193Sjb			 */
12305179193Sjb			offs = 0;
12306179193Sjb			total = needed;
12307179193Sjb			buf->dtb_flags |= DTRACEBUF_WRAPPED;
12308179193Sjb		} else {
12309179193Sjb			/*
12310179193Sjb			 * There is room for us in the buffer, so we simply
12311179193Sjb			 * need to check the wrapped offset.
12312179193Sjb			 */
12313179193Sjb			if (woffs < offs) {
12314179193Sjb				/*
12315179193Sjb				 * The wrapped offset is less than the offset.
12316179193Sjb				 * This can happen if we allocated buffer space
12317179193Sjb				 * that induced a wrap, but then we didn't
12318179193Sjb				 * subsequently take the space due to an error
12319179193Sjb				 * or false predicate evaluation.  This is
12320179193Sjb				 * okay; we know that _this_ allocation isn't
12321179193Sjb				 * going to induce a wrap.  We still can't
12322179193Sjb				 * reset the wrapped offset to be zero,
12323179193Sjb				 * however: the space may have been trashed in
12324179193Sjb				 * the previous failed probe attempt.  But at
12325179193Sjb				 * least the wrapped offset doesn't need to
12326179193Sjb				 * be adjusted at all...
12327179193Sjb				 */
12328179193Sjb				goto out;
12329179193Sjb			}
12330179193Sjb		}
12331179193Sjb
12332179193Sjb		while (offs + total > woffs) {
12333179193Sjb			dtrace_epid_t epid = *(uint32_t *)(tomax + woffs);
12334179193Sjb			size_t size;
12335179193Sjb
12336179193Sjb			if (epid == DTRACE_EPIDNONE) {
12337179193Sjb				size = sizeof (uint32_t);
12338179193Sjb			} else {
12339250574Smarkj				ASSERT3U(epid, <=, state->dts_necbs);
12340179193Sjb				ASSERT(state->dts_ecbs[epid - 1] != NULL);
12341179193Sjb
12342179193Sjb				size = state->dts_ecbs[epid - 1]->dte_size;
12343179193Sjb			}
12344179193Sjb
12345179193Sjb			ASSERT(woffs + size <= buf->dtb_size);
12346179193Sjb			ASSERT(size != 0);
12347179193Sjb
12348179193Sjb			if (woffs + size == buf->dtb_size) {
12349179193Sjb				/*
12350179193Sjb				 * We've reached the end of the buffer; we want
12351179193Sjb				 * to set the wrapped offset to 0 and break
12352179193Sjb				 * out.  However, if the offs is 0, then we're
12353179193Sjb				 * in a strange edge-condition:  the amount of
12354179193Sjb				 * space that we want to reserve plus the size
12355179193Sjb				 * of the record that we're overwriting is
12356179193Sjb				 * greater than the size of the buffer.  This
12357179193Sjb				 * is problematic because if we reserve the
12358179193Sjb				 * space but subsequently don't consume it (due
12359179193Sjb				 * to a failed predicate or error) the wrapped
12360179193Sjb				 * offset will be 0 -- yet the EPID at offset 0
12361179193Sjb				 * will not be committed.  This situation is
12362179193Sjb				 * relatively easy to deal with:  if we're in
12363179193Sjb				 * this case, the buffer is indistinguishable
12364179193Sjb				 * from one that hasn't wrapped; we need only
12365179193Sjb				 * finish the job by clearing the wrapped bit,
12366179193Sjb				 * explicitly setting the offset to be 0, and
12367179193Sjb				 * zero'ing out the old data in the buffer.
12368179193Sjb				 */
12369179193Sjb				if (offs == 0) {
12370179193Sjb					buf->dtb_flags &= ~DTRACEBUF_WRAPPED;
12371179193Sjb					buf->dtb_offset = 0;
12372179193Sjb					woffs = total;
12373179193Sjb
12374179193Sjb					while (woffs < buf->dtb_size)
12375179193Sjb						tomax[woffs++] = 0;
12376179193Sjb				}
12377179193Sjb
12378179193Sjb				woffs = 0;
12379179193Sjb				break;
12380179193Sjb			}
12381179193Sjb
12382179193Sjb			woffs += size;
12383179193Sjb		}
12384179193Sjb
12385179193Sjb		/*
12386179193Sjb		 * We have a wrapped offset.  It may be that the wrapped offset
12387179193Sjb		 * has become zero -- that's okay.
12388179193Sjb		 */
12389179193Sjb		buf->dtb_xamot_offset = woffs;
12390179193Sjb	}
12391179193Sjb
12392179193Sjbout:
12393179193Sjb	/*
12394179193Sjb	 * Now we can plow the buffer with any necessary padding.
12395179193Sjb	 */
12396179193Sjb	while (offs & (align - 1)) {
12397179193Sjb		/*
12398179193Sjb		 * Assert that our alignment is off by a number which
12399179193Sjb		 * is itself sizeof (uint32_t) aligned.
12400179193Sjb		 */
12401179193Sjb		ASSERT(!((align - (offs & (align - 1))) &
12402179193Sjb		    (sizeof (uint32_t) - 1)));
12403179193Sjb		DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
12404179193Sjb		offs += sizeof (uint32_t);
12405179193Sjb	}
12406179193Sjb
12407179193Sjb	if (buf->dtb_flags & DTRACEBUF_FILL) {
12408179193Sjb		if (offs + needed > buf->dtb_size - state->dts_reserve) {
12409179193Sjb			buf->dtb_flags |= DTRACEBUF_FULL;
12410179193Sjb			return (-1);
12411179193Sjb		}
12412179193Sjb	}
12413179193Sjb
12414179193Sjb	if (mstate == NULL)
12415179193Sjb		return (offs);
12416179193Sjb
12417179193Sjb	/*
12418179193Sjb	 * For ring buffers and fill buffers, the scratch space is always
12419179193Sjb	 * the inactive buffer.
12420179193Sjb	 */
12421179193Sjb	mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot;
12422179193Sjb	mstate->dtms_scratch_size = buf->dtb_size;
12423179193Sjb	mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
12424179193Sjb
12425179193Sjb	return (offs);
12426179193Sjb}
12427179193Sjb
12428179193Sjbstatic void
12429179193Sjbdtrace_buffer_polish(dtrace_buffer_t *buf)
12430179193Sjb{
12431179193Sjb	ASSERT(buf->dtb_flags & DTRACEBUF_RING);
12432179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
12433179193Sjb
12434179193Sjb	if (!(buf->dtb_flags & DTRACEBUF_WRAPPED))
12435179193Sjb		return;
12436179193Sjb
12437179193Sjb	/*
12438179193Sjb	 * We need to polish the ring buffer.  There are three cases:
12439179193Sjb	 *
12440179193Sjb	 * - The first (and presumably most common) is that there is no gap
12441179193Sjb	 *   between the buffer offset and the wrapped offset.  In this case,
12442179193Sjb	 *   there is nothing in the buffer that isn't valid data; we can
12443179193Sjb	 *   mark the buffer as polished and return.
12444179193Sjb	 *
12445179193Sjb	 * - The second (less common than the first but still more common
12446179193Sjb	 *   than the third) is that there is a gap between the buffer offset
12447179193Sjb	 *   and the wrapped offset, and the wrapped offset is larger than the
12448179193Sjb	 *   buffer offset.  This can happen because of an alignment issue, or
12449179193Sjb	 *   can happen because of a call to dtrace_buffer_reserve() that
12450179193Sjb	 *   didn't subsequently consume the buffer space.  In this case,
12451179193Sjb	 *   we need to zero the data from the buffer offset to the wrapped
12452179193Sjb	 *   offset.
12453179193Sjb	 *
12454179193Sjb	 * - The third (and least common) is that there is a gap between the
12455179193Sjb	 *   buffer offset and the wrapped offset, but the wrapped offset is
12456179193Sjb	 *   _less_ than the buffer offset.  This can only happen because a
12457179193Sjb	 *   call to dtrace_buffer_reserve() induced a wrap, but the space
12458179193Sjb	 *   was not subsequently consumed.  In this case, we need to zero the
12459179193Sjb	 *   space from the offset to the end of the buffer _and_ from the
12460179193Sjb	 *   top of the buffer to the wrapped offset.
12461179193Sjb	 */
12462179193Sjb	if (buf->dtb_offset < buf->dtb_xamot_offset) {
12463179193Sjb		bzero(buf->dtb_tomax + buf->dtb_offset,
12464179193Sjb		    buf->dtb_xamot_offset - buf->dtb_offset);
12465179193Sjb	}
12466179193Sjb
12467179193Sjb	if (buf->dtb_offset > buf->dtb_xamot_offset) {
12468179193Sjb		bzero(buf->dtb_tomax + buf->dtb_offset,
12469179193Sjb		    buf->dtb_size - buf->dtb_offset);
12470179193Sjb		bzero(buf->dtb_tomax, buf->dtb_xamot_offset);
12471179193Sjb	}
12472179193Sjb}
12473179193Sjb
12474248983Spfg/*
12475248983Spfg * This routine determines if data generated at the specified time has likely
12476248983Spfg * been entirely consumed at user-level.  This routine is called to determine
12477248983Spfg * if an ECB on a defunct probe (but for an active enabling) can be safely
12478248983Spfg * disabled and destroyed.
12479248983Spfg */
12480248983Spfgstatic int
12481248983Spfgdtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when)
12482248983Spfg{
12483248983Spfg	int i;
12484248983Spfg
12485248983Spfg	for (i = 0; i < NCPU; i++) {
12486248983Spfg		dtrace_buffer_t *buf = &bufs[i];
12487248983Spfg
12488248983Spfg		if (buf->dtb_size == 0)
12489248983Spfg			continue;
12490248983Spfg
12491248983Spfg		if (buf->dtb_flags & DTRACEBUF_RING)
12492248983Spfg			return (0);
12493248983Spfg
12494248983Spfg		if (!buf->dtb_switched && buf->dtb_offset != 0)
12495248983Spfg			return (0);
12496248983Spfg
12497248983Spfg		if (buf->dtb_switched - buf->dtb_interval < when)
12498248983Spfg			return (0);
12499248983Spfg	}
12500248983Spfg
12501248983Spfg	return (1);
12502248983Spfg}
12503248983Spfg
12504179193Sjbstatic void
12505179193Sjbdtrace_buffer_free(dtrace_buffer_t *bufs)
12506179193Sjb{
12507179193Sjb	int i;
12508179193Sjb
12509179193Sjb	for (i = 0; i < NCPU; i++) {
12510179193Sjb		dtrace_buffer_t *buf = &bufs[i];
12511179193Sjb
12512179193Sjb		if (buf->dtb_tomax == NULL) {
12513179193Sjb			ASSERT(buf->dtb_xamot == NULL);
12514179193Sjb			ASSERT(buf->dtb_size == 0);
12515179193Sjb			continue;
12516179193Sjb		}
12517179193Sjb
12518179193Sjb		if (buf->dtb_xamot != NULL) {
12519179193Sjb			ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
12520179193Sjb			kmem_free(buf->dtb_xamot, buf->dtb_size);
12521179193Sjb		}
12522179193Sjb
12523179193Sjb		kmem_free(buf->dtb_tomax, buf->dtb_size);
12524179193Sjb		buf->dtb_size = 0;
12525179193Sjb		buf->dtb_tomax = NULL;
12526179193Sjb		buf->dtb_xamot = NULL;
12527179193Sjb	}
12528179193Sjb}
12529179193Sjb
12530179193Sjb/*
12531179193Sjb * DTrace Enabling Functions
12532179193Sjb */
12533179193Sjbstatic dtrace_enabling_t *
12534179193Sjbdtrace_enabling_create(dtrace_vstate_t *vstate)
12535179193Sjb{
12536179193Sjb	dtrace_enabling_t *enab;
12537179193Sjb
12538179193Sjb	enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP);
12539179193Sjb	enab->dten_vstate = vstate;
12540179193Sjb
12541179193Sjb	return (enab);
12542179193Sjb}
12543179193Sjb
12544179193Sjbstatic void
12545179193Sjbdtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb)
12546179193Sjb{
12547179193Sjb	dtrace_ecbdesc_t **ndesc;
12548179193Sjb	size_t osize, nsize;
12549179193Sjb
12550179193Sjb	/*
12551179193Sjb	 * We can't add to enablings after we've enabled them, or after we've
12552179193Sjb	 * retained them.
12553179193Sjb	 */
12554179193Sjb	ASSERT(enab->dten_probegen == 0);
12555179193Sjb	ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
12556179193Sjb
12557179193Sjb	if (enab->dten_ndesc < enab->dten_maxdesc) {
12558179193Sjb		enab->dten_desc[enab->dten_ndesc++] = ecb;
12559179193Sjb		return;
12560179193Sjb	}
12561179193Sjb
12562179193Sjb	osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
12563179193Sjb
12564179193Sjb	if (enab->dten_maxdesc == 0) {
12565179193Sjb		enab->dten_maxdesc = 1;
12566179193Sjb	} else {
12567179193Sjb		enab->dten_maxdesc <<= 1;
12568179193Sjb	}
12569179193Sjb
12570179193Sjb	ASSERT(enab->dten_ndesc < enab->dten_maxdesc);
12571179193Sjb
12572179193Sjb	nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
12573179193Sjb	ndesc = kmem_zalloc(nsize, KM_SLEEP);
12574179193Sjb	bcopy(enab->dten_desc, ndesc, osize);
12575179198Sjb	if (enab->dten_desc != NULL)
12576179198Sjb		kmem_free(enab->dten_desc, osize);
12577179193Sjb
12578179193Sjb	enab->dten_desc = ndesc;
12579179193Sjb	enab->dten_desc[enab->dten_ndesc++] = ecb;
12580179193Sjb}
12581179193Sjb
12582179193Sjbstatic void
12583179193Sjbdtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb,
12584179193Sjb    dtrace_probedesc_t *pd)
12585179193Sjb{
12586179193Sjb	dtrace_ecbdesc_t *new;
12587179193Sjb	dtrace_predicate_t *pred;
12588179193Sjb	dtrace_actdesc_t *act;
12589179193Sjb
12590179193Sjb	/*
12591179193Sjb	 * We're going to create a new ECB description that matches the
12592179193Sjb	 * specified ECB in every way, but has the specified probe description.
12593179193Sjb	 */
12594179193Sjb	new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
12595179193Sjb
12596179193Sjb	if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL)
12597179193Sjb		dtrace_predicate_hold(pred);
12598179193Sjb
12599179193Sjb	for (act = ecb->dted_action; act != NULL; act = act->dtad_next)
12600179193Sjb		dtrace_actdesc_hold(act);
12601179193Sjb
12602179193Sjb	new->dted_action = ecb->dted_action;
12603179193Sjb	new->dted_pred = ecb->dted_pred;
12604179193Sjb	new->dted_probe = *pd;
12605179193Sjb	new->dted_uarg = ecb->dted_uarg;
12606179193Sjb
12607179193Sjb	dtrace_enabling_add(enab, new);
12608179193Sjb}
12609179193Sjb
12610179193Sjbstatic void
12611179193Sjbdtrace_enabling_dump(dtrace_enabling_t *enab)
12612179193Sjb{
12613179193Sjb	int i;
12614179193Sjb
12615179193Sjb	for (i = 0; i < enab->dten_ndesc; i++) {
12616179193Sjb		dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe;
12617179193Sjb
12618297773Smarkj#ifdef __FreeBSD__
12619297773Smarkj		printf("dtrace: enabling probe %d (%s:%s:%s:%s)\n", i,
12620297773Smarkj		    desc->dtpd_provider, desc->dtpd_mod,
12621297773Smarkj		    desc->dtpd_func, desc->dtpd_name);
12622297773Smarkj#else
12623179193Sjb		cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i,
12624179193Sjb		    desc->dtpd_provider, desc->dtpd_mod,
12625179193Sjb		    desc->dtpd_func, desc->dtpd_name);
12626297773Smarkj#endif
12627179193Sjb	}
12628179193Sjb}
12629179193Sjb
12630179193Sjbstatic void
12631179193Sjbdtrace_enabling_destroy(dtrace_enabling_t *enab)
12632179193Sjb{
12633179193Sjb	int i;
12634179193Sjb	dtrace_ecbdesc_t *ep;
12635179193Sjb	dtrace_vstate_t *vstate = enab->dten_vstate;
12636179193Sjb
12637179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
12638179193Sjb
12639179193Sjb	for (i = 0; i < enab->dten_ndesc; i++) {
12640179193Sjb		dtrace_actdesc_t *act, *next;
12641179193Sjb		dtrace_predicate_t *pred;
12642179193Sjb
12643179193Sjb		ep = enab->dten_desc[i];
12644179193Sjb
12645179193Sjb		if ((pred = ep->dted_pred.dtpdd_predicate) != NULL)
12646179193Sjb			dtrace_predicate_release(pred, vstate);
12647179193Sjb
12648179193Sjb		for (act = ep->dted_action; act != NULL; act = next) {
12649179193Sjb			next = act->dtad_next;
12650179193Sjb			dtrace_actdesc_release(act, vstate);
12651179193Sjb		}
12652179193Sjb
12653179193Sjb		kmem_free(ep, sizeof (dtrace_ecbdesc_t));
12654179193Sjb	}
12655179193Sjb
12656179198Sjb	if (enab->dten_desc != NULL)
12657179198Sjb		kmem_free(enab->dten_desc,
12658179198Sjb		    enab->dten_maxdesc * sizeof (dtrace_enabling_t *));
12659179193Sjb
12660179193Sjb	/*
12661179193Sjb	 * If this was a retained enabling, decrement the dts_nretained count
12662179193Sjb	 * and take it off of the dtrace_retained list.
12663179193Sjb	 */
12664179193Sjb	if (enab->dten_prev != NULL || enab->dten_next != NULL ||
12665179193Sjb	    dtrace_retained == enab) {
12666179193Sjb		ASSERT(enab->dten_vstate->dtvs_state != NULL);
12667179193Sjb		ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0);
12668179193Sjb		enab->dten_vstate->dtvs_state->dts_nretained--;
12669268130Spfg		dtrace_retained_gen++;
12670179193Sjb	}
12671179193Sjb
12672179193Sjb	if (enab->dten_prev == NULL) {
12673179193Sjb		if (dtrace_retained == enab) {
12674179193Sjb			dtrace_retained = enab->dten_next;
12675179193Sjb
12676179193Sjb			if (dtrace_retained != NULL)
12677179193Sjb				dtrace_retained->dten_prev = NULL;
12678179193Sjb		}
12679179193Sjb	} else {
12680179193Sjb		ASSERT(enab != dtrace_retained);
12681179193Sjb		ASSERT(dtrace_retained != NULL);
12682179193Sjb		enab->dten_prev->dten_next = enab->dten_next;
12683179193Sjb	}
12684179193Sjb
12685179193Sjb	if (enab->dten_next != NULL) {
12686179193Sjb		ASSERT(dtrace_retained != NULL);
12687179193Sjb		enab->dten_next->dten_prev = enab->dten_prev;
12688179193Sjb	}
12689179193Sjb
12690179193Sjb	kmem_free(enab, sizeof (dtrace_enabling_t));
12691179193Sjb}
12692179193Sjb
12693179193Sjbstatic int
12694179193Sjbdtrace_enabling_retain(dtrace_enabling_t *enab)
12695179193Sjb{
12696179193Sjb	dtrace_state_t *state;
12697179193Sjb
12698179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
12699179193Sjb	ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
12700179193Sjb	ASSERT(enab->dten_vstate != NULL);
12701179193Sjb
12702179193Sjb	state = enab->dten_vstate->dtvs_state;
12703179193Sjb	ASSERT(state != NULL);
12704179193Sjb
12705179193Sjb	/*
12706179193Sjb	 * We only allow each state to retain dtrace_retain_max enablings.
12707179193Sjb	 */
12708179193Sjb	if (state->dts_nretained >= dtrace_retain_max)
12709179193Sjb		return (ENOSPC);
12710179193Sjb
12711179193Sjb	state->dts_nretained++;
12712268130Spfg	dtrace_retained_gen++;
12713179193Sjb
12714179193Sjb	if (dtrace_retained == NULL) {
12715179193Sjb		dtrace_retained = enab;
12716179193Sjb		return (0);
12717179193Sjb	}
12718179193Sjb
12719179193Sjb	enab->dten_next = dtrace_retained;
12720179193Sjb	dtrace_retained->dten_prev = enab;
12721179193Sjb	dtrace_retained = enab;
12722179193Sjb
12723179193Sjb	return (0);
12724179193Sjb}
12725179193Sjb
12726179193Sjbstatic int
12727179193Sjbdtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match,
12728179193Sjb    dtrace_probedesc_t *create)
12729179193Sjb{
12730179193Sjb	dtrace_enabling_t *new, *enab;
12731179193Sjb	int found = 0, err = ENOENT;
12732179193Sjb
12733179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
12734179193Sjb	ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN);
12735179193Sjb	ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN);
12736179193Sjb	ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN);
12737179193Sjb	ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN);
12738179193Sjb
12739179193Sjb	new = dtrace_enabling_create(&state->dts_vstate);
12740179193Sjb
12741179193Sjb	/*
12742179193Sjb	 * Iterate over all retained enablings, looking for enablings that
12743179193Sjb	 * match the specified state.
12744179193Sjb	 */
12745179193Sjb	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
12746179193Sjb		int i;
12747179193Sjb
12748179193Sjb		/*
12749179193Sjb		 * dtvs_state can only be NULL for helper enablings -- and
12750179193Sjb		 * helper enablings can't be retained.
12751179193Sjb		 */
12752179193Sjb		ASSERT(enab->dten_vstate->dtvs_state != NULL);
12753179193Sjb
12754179193Sjb		if (enab->dten_vstate->dtvs_state != state)
12755179193Sjb			continue;
12756179193Sjb
12757179193Sjb		/*
12758179193Sjb		 * Now iterate over each probe description; we're looking for
12759179193Sjb		 * an exact match to the specified probe description.
12760179193Sjb		 */
12761179193Sjb		for (i = 0; i < enab->dten_ndesc; i++) {
12762179193Sjb			dtrace_ecbdesc_t *ep = enab->dten_desc[i];
12763179193Sjb			dtrace_probedesc_t *pd = &ep->dted_probe;
12764179193Sjb
12765179193Sjb			if (strcmp(pd->dtpd_provider, match->dtpd_provider))
12766179193Sjb				continue;
12767179193Sjb
12768179193Sjb			if (strcmp(pd->dtpd_mod, match->dtpd_mod))
12769179193Sjb				continue;
12770179193Sjb
12771179193Sjb			if (strcmp(pd->dtpd_func, match->dtpd_func))
12772179193Sjb				continue;
12773179193Sjb
12774179193Sjb			if (strcmp(pd->dtpd_name, match->dtpd_name))
12775179193Sjb				continue;
12776179193Sjb
12777179193Sjb			/*
12778179193Sjb			 * We have a winning probe!  Add it to our growing
12779179193Sjb			 * enabling.
12780179193Sjb			 */
12781179193Sjb			found = 1;
12782179193Sjb			dtrace_enabling_addlike(new, ep, create);
12783179193Sjb		}
12784179193Sjb	}
12785179193Sjb
12786179193Sjb	if (!found || (err = dtrace_enabling_retain(new)) != 0) {
12787179193Sjb		dtrace_enabling_destroy(new);
12788179193Sjb		return (err);
12789179193Sjb	}
12790179193Sjb
12791179193Sjb	return (0);
12792179193Sjb}
12793179193Sjb
12794179193Sjbstatic void
12795179193Sjbdtrace_enabling_retract(dtrace_state_t *state)
12796179193Sjb{
12797179193Sjb	dtrace_enabling_t *enab, *next;
12798179193Sjb
12799179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
12800179193Sjb
12801179193Sjb	/*
12802179193Sjb	 * Iterate over all retained enablings, destroy the enablings retained
12803179193Sjb	 * for the specified state.
12804179193Sjb	 */
12805179193Sjb	for (enab = dtrace_retained; enab != NULL; enab = next) {
12806179193Sjb		next = enab->dten_next;
12807179193Sjb
12808179193Sjb		/*
12809179193Sjb		 * dtvs_state can only be NULL for helper enablings -- and
12810179193Sjb		 * helper enablings can't be retained.
12811179193Sjb		 */
12812179193Sjb		ASSERT(enab->dten_vstate->dtvs_state != NULL);
12813179193Sjb
12814179193Sjb		if (enab->dten_vstate->dtvs_state == state) {
12815179193Sjb			ASSERT(state->dts_nretained > 0);
12816179193Sjb			dtrace_enabling_destroy(enab);
12817179193Sjb		}
12818179193Sjb	}
12819179193Sjb
12820179193Sjb	ASSERT(state->dts_nretained == 0);
12821179193Sjb}
12822179193Sjb
12823179193Sjbstatic int
12824179193Sjbdtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched)
12825179193Sjb{
12826179193Sjb	int i = 0;
12827179193Sjb	int matched = 0;
12828179193Sjb
12829179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
12830179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
12831179193Sjb
12832179193Sjb	for (i = 0; i < enab->dten_ndesc; i++) {
12833179193Sjb		dtrace_ecbdesc_t *ep = enab->dten_desc[i];
12834179193Sjb
12835179193Sjb		enab->dten_current = ep;
12836179193Sjb		enab->dten_error = 0;
12837179193Sjb
12838179193Sjb		matched += dtrace_probe_enable(&ep->dted_probe, enab);
12839179193Sjb
12840179193Sjb		if (enab->dten_error != 0) {
12841179193Sjb			/*
12842179193Sjb			 * If we get an error half-way through enabling the
12843179193Sjb			 * probes, we kick out -- perhaps with some number of
12844179193Sjb			 * them enabled.  Leaving enabled probes enabled may
12845179193Sjb			 * be slightly confusing for user-level, but we expect
12846179193Sjb			 * that no one will attempt to actually drive on in
12847179193Sjb			 * the face of such errors.  If this is an anonymous
12848179193Sjb			 * enabling (indicated with a NULL nmatched pointer),
12849179193Sjb			 * we cmn_err() a message.  We aren't expecting to
12850179193Sjb			 * get such an error -- such as it can exist at all,
12851179193Sjb			 * it would be a result of corrupted DOF in the driver
12852179193Sjb			 * properties.
12853179193Sjb			 */
12854179193Sjb			if (nmatched == NULL) {
12855179193Sjb				cmn_err(CE_WARN, "dtrace_enabling_match() "
12856179193Sjb				    "error on %p: %d", (void *)ep,
12857179193Sjb				    enab->dten_error);
12858179193Sjb			}
12859179193Sjb
12860179193Sjb			return (enab->dten_error);
12861179193Sjb		}
12862179193Sjb	}
12863179193Sjb
12864179193Sjb	enab->dten_probegen = dtrace_probegen;
12865179193Sjb	if (nmatched != NULL)
12866179193Sjb		*nmatched = matched;
12867179193Sjb
12868179193Sjb	return (0);
12869179193Sjb}
12870179193Sjb
12871179193Sjbstatic void
12872179193Sjbdtrace_enabling_matchall(void)
12873179193Sjb{
12874179193Sjb	dtrace_enabling_t *enab;
12875179193Sjb
12876179193Sjb	mutex_enter(&cpu_lock);
12877179193Sjb	mutex_enter(&dtrace_lock);
12878179193Sjb
12879179193Sjb	/*
12880179469Sjb	 * Iterate over all retained enablings to see if any probes match
12881179469Sjb	 * against them.  We only perform this operation on enablings for which
12882179469Sjb	 * we have sufficient permissions by virtue of being in the global zone
12883179469Sjb	 * or in the same zone as the DTrace client.  Because we can be called
12884179469Sjb	 * after dtrace_detach() has been called, we cannot assert that there
12885179469Sjb	 * are retained enablings.  We can safely load from dtrace_retained,
12886179469Sjb	 * however:  the taskq_destroy() at the end of dtrace_detach() will
12887179469Sjb	 * block pending our completion.
12888179193Sjb	 */
12889179469Sjb	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
12890277300Ssmh#ifdef illumos
12891179469Sjb		cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred;
12892179193Sjb
12893268125Spfg		if (INGLOBALZONE(curproc) ||
12894268125Spfg		    cr != NULL && getzoneid() == crgetzoneid(cr))
12895179469Sjb#endif
12896179469Sjb			(void) dtrace_enabling_match(enab, NULL);
12897179469Sjb	}
12898179469Sjb
12899179193Sjb	mutex_exit(&dtrace_lock);
12900179193Sjb	mutex_exit(&cpu_lock);
12901179193Sjb}
12902179193Sjb
12903179193Sjb/*
12904179193Sjb * If an enabling is to be enabled without having matched probes (that is, if
12905179193Sjb * dtrace_state_go() is to be called on the underlying dtrace_state_t), the
12906179193Sjb * enabling must be _primed_ by creating an ECB for every ECB description.
12907179193Sjb * This must be done to assure that we know the number of speculations, the
12908179193Sjb * number of aggregations, the minimum buffer size needed, etc. before we
12909179193Sjb * transition out of DTRACE_ACTIVITY_INACTIVE.  To do this without actually
12910179193Sjb * enabling any probes, we create ECBs for every ECB decription, but with a
12911179193Sjb * NULL probe -- which is exactly what this function does.
12912179193Sjb */
12913179193Sjbstatic void
12914179193Sjbdtrace_enabling_prime(dtrace_state_t *state)
12915179193Sjb{
12916179193Sjb	dtrace_enabling_t *enab;
12917179193Sjb	int i;
12918179193Sjb
12919179193Sjb	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
12920179193Sjb		ASSERT(enab->dten_vstate->dtvs_state != NULL);
12921179193Sjb
12922179193Sjb		if (enab->dten_vstate->dtvs_state != state)
12923179193Sjb			continue;
12924179193Sjb
12925179193Sjb		/*
12926179193Sjb		 * We don't want to prime an enabling more than once, lest
12927179193Sjb		 * we allow a malicious user to induce resource exhaustion.
12928179193Sjb		 * (The ECBs that result from priming an enabling aren't
12929179193Sjb		 * leaked -- but they also aren't deallocated until the
12930179193Sjb		 * consumer state is destroyed.)
12931179193Sjb		 */
12932179193Sjb		if (enab->dten_primed)
12933179193Sjb			continue;
12934179193Sjb
12935179193Sjb		for (i = 0; i < enab->dten_ndesc; i++) {
12936179193Sjb			enab->dten_current = enab->dten_desc[i];
12937179193Sjb			(void) dtrace_probe_enable(NULL, enab);
12938179193Sjb		}
12939179193Sjb
12940179193Sjb		enab->dten_primed = 1;
12941179193Sjb	}
12942179193Sjb}
12943179193Sjb
12944179193Sjb/*
12945179193Sjb * Called to indicate that probes should be provided due to retained
12946179193Sjb * enablings.  This is implemented in terms of dtrace_probe_provide(), but it
12947179193Sjb * must take an initial lap through the enabling calling the dtps_provide()
12948179193Sjb * entry point explicitly to allow for autocreated probes.
12949179193Sjb */
12950179193Sjbstatic void
12951179193Sjbdtrace_enabling_provide(dtrace_provider_t *prv)
12952179193Sjb{
12953179193Sjb	int i, all = 0;
12954179193Sjb	dtrace_probedesc_t desc;
12955268130Spfg	dtrace_genid_t gen;
12956179193Sjb
12957179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
12958179193Sjb	ASSERT(MUTEX_HELD(&dtrace_provider_lock));
12959179193Sjb
12960179193Sjb	if (prv == NULL) {
12961179193Sjb		all = 1;
12962179193Sjb		prv = dtrace_provider;
12963179193Sjb	}
12964179193Sjb
12965179193Sjb	do {
12966268130Spfg		dtrace_enabling_t *enab;
12967179193Sjb		void *parg = prv->dtpv_arg;
12968179193Sjb
12969268130Spfgretry:
12970268130Spfg		gen = dtrace_retained_gen;
12971268130Spfg		for (enab = dtrace_retained; enab != NULL;
12972268130Spfg		    enab = enab->dten_next) {
12973179193Sjb			for (i = 0; i < enab->dten_ndesc; i++) {
12974179193Sjb				desc = enab->dten_desc[i]->dted_probe;
12975179193Sjb				mutex_exit(&dtrace_lock);
12976179193Sjb				prv->dtpv_pops.dtps_provide(parg, &desc);
12977179193Sjb				mutex_enter(&dtrace_lock);
12978268130Spfg				/*
12979268130Spfg				 * Process the retained enablings again if
12980268130Spfg				 * they have changed while we weren't holding
12981268130Spfg				 * dtrace_lock.
12982268130Spfg				 */
12983268130Spfg				if (gen != dtrace_retained_gen)
12984268130Spfg					goto retry;
12985179193Sjb			}
12986179193Sjb		}
12987179193Sjb	} while (all && (prv = prv->dtpv_next) != NULL);
12988179193Sjb
12989179193Sjb	mutex_exit(&dtrace_lock);
12990179193Sjb	dtrace_probe_provide(NULL, all ? NULL : prv);
12991179193Sjb	mutex_enter(&dtrace_lock);
12992179193Sjb}
12993179193Sjb
12994179193Sjb/*
12995248983Spfg * Called to reap ECBs that are attached to probes from defunct providers.
12996248983Spfg */
12997248983Spfgstatic void
12998248983Spfgdtrace_enabling_reap(void)
12999248983Spfg{
13000248983Spfg	dtrace_provider_t *prov;
13001248983Spfg	dtrace_probe_t *probe;
13002248983Spfg	dtrace_ecb_t *ecb;
13003248983Spfg	hrtime_t when;
13004248983Spfg	int i;
13005248983Spfg
13006248983Spfg	mutex_enter(&cpu_lock);
13007248983Spfg	mutex_enter(&dtrace_lock);
13008248983Spfg
13009248983Spfg	for (i = 0; i < dtrace_nprobes; i++) {
13010248983Spfg		if ((probe = dtrace_probes[i]) == NULL)
13011248983Spfg			continue;
13012248983Spfg
13013248983Spfg		if (probe->dtpr_ecb == NULL)
13014248983Spfg			continue;
13015248983Spfg
13016248983Spfg		prov = probe->dtpr_provider;
13017248983Spfg
13018248983Spfg		if ((when = prov->dtpv_defunct) == 0)
13019248983Spfg			continue;
13020248983Spfg
13021248983Spfg		/*
13022248983Spfg		 * We have ECBs on a defunct provider:  we want to reap these
13023248983Spfg		 * ECBs to allow the provider to unregister.  The destruction
13024248983Spfg		 * of these ECBs must be done carefully:  if we destroy the ECB
13025248983Spfg		 * and the consumer later wishes to consume an EPID that
13026248983Spfg		 * corresponds to the destroyed ECB (and if the EPID metadata
13027248983Spfg		 * has not been previously consumed), the consumer will abort
13028248983Spfg		 * processing on the unknown EPID.  To reduce (but not, sadly,
13029248983Spfg		 * eliminate) the possibility of this, we will only destroy an
13030248983Spfg		 * ECB for a defunct provider if, for the state that
13031248983Spfg		 * corresponds to the ECB:
13032248983Spfg		 *
13033248983Spfg		 *  (a)	There is no speculative tracing (which can effectively
13034248983Spfg		 *	cache an EPID for an arbitrary amount of time).
13035248983Spfg		 *
13036248983Spfg		 *  (b)	The principal buffers have been switched twice since the
13037248983Spfg		 *	provider became defunct.
13038248983Spfg		 *
13039248983Spfg		 *  (c)	The aggregation buffers are of zero size or have been
13040248983Spfg		 *	switched twice since the provider became defunct.
13041248983Spfg		 *
13042248983Spfg		 * We use dts_speculates to determine (a) and call a function
13043248983Spfg		 * (dtrace_buffer_consumed()) to determine (b) and (c).  Note
13044248983Spfg		 * that as soon as we've been unable to destroy one of the ECBs
13045248983Spfg		 * associated with the probe, we quit trying -- reaping is only
13046248983Spfg		 * fruitful in as much as we can destroy all ECBs associated
13047248983Spfg		 * with the defunct provider's probes.
13048248983Spfg		 */
13049248983Spfg		while ((ecb = probe->dtpr_ecb) != NULL) {
13050248983Spfg			dtrace_state_t *state = ecb->dte_state;
13051248983Spfg			dtrace_buffer_t *buf = state->dts_buffer;
13052248983Spfg			dtrace_buffer_t *aggbuf = state->dts_aggbuffer;
13053248983Spfg
13054248983Spfg			if (state->dts_speculates)
13055248983Spfg				break;
13056248983Spfg
13057248983Spfg			if (!dtrace_buffer_consumed(buf, when))
13058248983Spfg				break;
13059248983Spfg
13060248983Spfg			if (!dtrace_buffer_consumed(aggbuf, when))
13061248983Spfg				break;
13062248983Spfg
13063248983Spfg			dtrace_ecb_disable(ecb);
13064248983Spfg			ASSERT(probe->dtpr_ecb != ecb);
13065248983Spfg			dtrace_ecb_destroy(ecb);
13066248983Spfg		}
13067248983Spfg	}
13068248983Spfg
13069248983Spfg	mutex_exit(&dtrace_lock);
13070248983Spfg	mutex_exit(&cpu_lock);
13071248983Spfg}
13072248983Spfg
13073248983Spfg/*
13074179193Sjb * DTrace DOF Functions
13075179193Sjb */
13076179193Sjb/*ARGSUSED*/
13077179193Sjbstatic void
13078179193Sjbdtrace_dof_error(dof_hdr_t *dof, const char *str)
13079179193Sjb{
13080179193Sjb	if (dtrace_err_verbose)
13081179193Sjb		cmn_err(CE_WARN, "failed to process DOF: %s", str);
13082179193Sjb
13083179193Sjb#ifdef DTRACE_ERRDEBUG
13084179193Sjb	dtrace_errdebug(str);
13085179193Sjb#endif
13086179193Sjb}
13087179193Sjb
13088179193Sjb/*
13089179193Sjb * Create DOF out of a currently enabled state.  Right now, we only create
13090179193Sjb * DOF containing the run-time options -- but this could be expanded to create
13091179193Sjb * complete DOF representing the enabled state.
13092179193Sjb */
13093179193Sjbstatic dof_hdr_t *
13094179193Sjbdtrace_dof_create(dtrace_state_t *state)
13095179193Sjb{
13096179193Sjb	dof_hdr_t *dof;
13097179193Sjb	dof_sec_t *sec;
13098179193Sjb	dof_optdesc_t *opt;
13099179193Sjb	int i, len = sizeof (dof_hdr_t) +
13100179193Sjb	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
13101179193Sjb	    sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
13102179193Sjb
13103179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
13104179193Sjb
13105179193Sjb	dof = kmem_zalloc(len, KM_SLEEP);
13106179193Sjb	dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
13107179193Sjb	dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
13108179193Sjb	dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
13109179193Sjb	dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
13110179193Sjb
13111179193Sjb	dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
13112179193Sjb	dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
13113179193Sjb	dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION;
13114179193Sjb	dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
13115179193Sjb	dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
13116179193Sjb	dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
13117179193Sjb
13118179193Sjb	dof->dofh_flags = 0;
13119179193Sjb	dof->dofh_hdrsize = sizeof (dof_hdr_t);
13120179193Sjb	dof->dofh_secsize = sizeof (dof_sec_t);
13121179193Sjb	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
13122179193Sjb	dof->dofh_secoff = sizeof (dof_hdr_t);
13123179193Sjb	dof->dofh_loadsz = len;
13124179193Sjb	dof->dofh_filesz = len;
13125179193Sjb	dof->dofh_pad = 0;
13126179193Sjb
13127179193Sjb	/*
13128179193Sjb	 * Fill in the option section header...
13129179193Sjb	 */
13130179193Sjb	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
13131179193Sjb	sec->dofs_type = DOF_SECT_OPTDESC;
13132179193Sjb	sec->dofs_align = sizeof (uint64_t);
13133179193Sjb	sec->dofs_flags = DOF_SECF_LOAD;
13134179193Sjb	sec->dofs_entsize = sizeof (dof_optdesc_t);
13135179193Sjb
13136179193Sjb	opt = (dof_optdesc_t *)((uintptr_t)sec +
13137179193Sjb	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
13138179193Sjb
13139179193Sjb	sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
13140179193Sjb	sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
13141179193Sjb
13142179193Sjb	for (i = 0; i < DTRACEOPT_MAX; i++) {
13143179193Sjb		opt[i].dofo_option = i;
13144179193Sjb		opt[i].dofo_strtab = DOF_SECIDX_NONE;
13145179193Sjb		opt[i].dofo_value = state->dts_options[i];
13146179193Sjb	}
13147179193Sjb
13148179193Sjb	return (dof);
13149179193Sjb}
13150179193Sjb
13151179193Sjbstatic dof_hdr_t *
13152179193Sjbdtrace_dof_copyin(uintptr_t uarg, int *errp)
13153179193Sjb{
13154179193Sjb	dof_hdr_t hdr, *dof;
13155179193Sjb
13156179193Sjb	ASSERT(!MUTEX_HELD(&dtrace_lock));
13157179193Sjb
13158179193Sjb	/*
13159179193Sjb	 * First, we're going to copyin() the sizeof (dof_hdr_t).
13160179193Sjb	 */
13161179193Sjb	if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) {
13162179193Sjb		dtrace_dof_error(NULL, "failed to copyin DOF header");
13163179193Sjb		*errp = EFAULT;
13164179193Sjb		return (NULL);
13165179193Sjb	}
13166179193Sjb
13167179193Sjb	/*
13168179193Sjb	 * Now we'll allocate the entire DOF and copy it in -- provided
13169179193Sjb	 * that the length isn't outrageous.
13170179193Sjb	 */
13171179193Sjb	if (hdr.dofh_loadsz >= dtrace_dof_maxsize) {
13172179193Sjb		dtrace_dof_error(&hdr, "load size exceeds maximum");
13173179193Sjb		*errp = E2BIG;
13174179193Sjb		return (NULL);
13175179193Sjb	}
13176179193Sjb
13177179193Sjb	if (hdr.dofh_loadsz < sizeof (hdr)) {
13178179193Sjb		dtrace_dof_error(&hdr, "invalid load size");
13179179193Sjb		*errp = EINVAL;
13180179193Sjb		return (NULL);
13181179193Sjb	}
13182179193Sjb
13183179193Sjb	dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP);
13184179193Sjb
13185268230Spfg	if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 ||
13186268230Spfg	    dof->dofh_loadsz != hdr.dofh_loadsz) {
13187179193Sjb		kmem_free(dof, hdr.dofh_loadsz);
13188179193Sjb		*errp = EFAULT;
13189179193Sjb		return (NULL);
13190179193Sjb	}
13191179193Sjb
13192179193Sjb	return (dof);
13193179193Sjb}
13194179193Sjb
13195291962Smarkj#ifdef __FreeBSD__
13196291962Smarkjstatic dof_hdr_t *
13197291962Smarkjdtrace_dof_copyin_proc(struct proc *p, uintptr_t uarg, int *errp)
13198291962Smarkj{
13199291962Smarkj	dof_hdr_t hdr, *dof;
13200291962Smarkj	struct thread *td;
13201291962Smarkj	size_t loadsz;
13202291962Smarkj
13203291962Smarkj	ASSERT(!MUTEX_HELD(&dtrace_lock));
13204291962Smarkj
13205291962Smarkj	td = curthread;
13206291962Smarkj
13207291962Smarkj	/*
13208291962Smarkj	 * First, we're going to copyin() the sizeof (dof_hdr_t).
13209291962Smarkj	 */
13210291962Smarkj	if (proc_readmem(td, p, uarg, &hdr, sizeof(hdr)) != sizeof(hdr)) {
13211291962Smarkj		dtrace_dof_error(NULL, "failed to copyin DOF header");
13212291962Smarkj		*errp = EFAULT;
13213291962Smarkj		return (NULL);
13214291962Smarkj	}
13215291962Smarkj
13216291962Smarkj	/*
13217291962Smarkj	 * Now we'll allocate the entire DOF and copy it in -- provided
13218291962Smarkj	 * that the length isn't outrageous.
13219291962Smarkj	 */
13220291962Smarkj	if (hdr.dofh_loadsz >= dtrace_dof_maxsize) {
13221291962Smarkj		dtrace_dof_error(&hdr, "load size exceeds maximum");
13222291962Smarkj		*errp = E2BIG;
13223291962Smarkj		return (NULL);
13224291962Smarkj	}
13225291962Smarkj	loadsz = (size_t)hdr.dofh_loadsz;
13226291962Smarkj
13227291962Smarkj	if (loadsz < sizeof (hdr)) {
13228291962Smarkj		dtrace_dof_error(&hdr, "invalid load size");
13229291962Smarkj		*errp = EINVAL;
13230291962Smarkj		return (NULL);
13231291962Smarkj	}
13232291962Smarkj
13233291962Smarkj	dof = kmem_alloc(loadsz, KM_SLEEP);
13234291962Smarkj
13235291962Smarkj	if (proc_readmem(td, p, uarg, dof, loadsz) != loadsz ||
13236291962Smarkj	    dof->dofh_loadsz != loadsz) {
13237291962Smarkj		kmem_free(dof, hdr.dofh_loadsz);
13238291962Smarkj		*errp = EFAULT;
13239291962Smarkj		return (NULL);
13240291962Smarkj	}
13241291962Smarkj
13242291962Smarkj	return (dof);
13243291962Smarkj}
13244291962Smarkj
13245179198Sjbstatic __inline uchar_t
13246291962Smarkjdtrace_dof_char(char c)
13247291962Smarkj{
13248291962Smarkj
13249179198Sjb	switch (c) {
13250179198Sjb	case '0':
13251179198Sjb	case '1':
13252179198Sjb	case '2':
13253179198Sjb	case '3':
13254179198Sjb	case '4':
13255179198Sjb	case '5':
13256179198Sjb	case '6':
13257179198Sjb	case '7':
13258179198Sjb	case '8':
13259179198Sjb	case '9':
13260179198Sjb		return (c - '0');
13261179198Sjb	case 'A':
13262179198Sjb	case 'B':
13263179198Sjb	case 'C':
13264179198Sjb	case 'D':
13265179198Sjb	case 'E':
13266179198Sjb	case 'F':
13267179198Sjb		return (c - 'A' + 10);
13268179198Sjb	case 'a':
13269179198Sjb	case 'b':
13270179198Sjb	case 'c':
13271179198Sjb	case 'd':
13272179198Sjb	case 'e':
13273179198Sjb	case 'f':
13274179198Sjb		return (c - 'a' + 10);
13275179198Sjb	}
13276179198Sjb	/* Should not reach here. */
13277297773Smarkj	return (UCHAR_MAX);
13278179198Sjb}
13279291962Smarkj#endif /* __FreeBSD__ */
13280179198Sjb
13281179193Sjbstatic dof_hdr_t *
13282179193Sjbdtrace_dof_property(const char *name)
13283179193Sjb{
13284297773Smarkj#ifdef __FreeBSD__
13285297773Smarkj	uint8_t *dofbuf;
13286297773Smarkj	u_char *data, *eol;
13287297773Smarkj	caddr_t doffile;
13288297773Smarkj	size_t bytes, len, i;
13289297773Smarkj	dof_hdr_t *dof;
13290297773Smarkj	u_char c1, c2;
13291297773Smarkj
13292297773Smarkj	dof = NULL;
13293297773Smarkj
13294297773Smarkj	doffile = preload_search_by_type("dtrace_dof");
13295297773Smarkj	if (doffile == NULL)
13296297773Smarkj		return (NULL);
13297297773Smarkj
13298297773Smarkj	data = preload_fetch_addr(doffile);
13299297773Smarkj	len = preload_fetch_size(doffile);
13300297773Smarkj	for (;;) {
13301297773Smarkj		/* Look for the end of the line. All lines end in a newline. */
13302297773Smarkj		eol = memchr(data, '\n', len);
13303297773Smarkj		if (eol == NULL)
13304297773Smarkj			return (NULL);
13305297773Smarkj
13306297773Smarkj		if (strncmp(name, data, strlen(name)) == 0)
13307297773Smarkj			break;
13308297773Smarkj
13309297773Smarkj		eol++; /* skip past the newline */
13310297773Smarkj		len -= eol - data;
13311297773Smarkj		data = eol;
13312297773Smarkj	}
13313297773Smarkj
13314297773Smarkj	/* We've found the data corresponding to the specified key. */
13315297773Smarkj
13316297773Smarkj	data += strlen(name) + 1; /* skip past the '=' */
13317297773Smarkj	len = eol - data;
13318297773Smarkj	bytes = len / 2;
13319297773Smarkj
13320297773Smarkj	if (bytes < sizeof(dof_hdr_t)) {
13321297773Smarkj		dtrace_dof_error(NULL, "truncated header");
13322297773Smarkj		goto doferr;
13323297773Smarkj	}
13324297773Smarkj
13325297773Smarkj	/*
13326297773Smarkj	 * Each byte is represented by the two ASCII characters in its hex
13327297773Smarkj	 * representation.
13328297773Smarkj	 */
13329297773Smarkj	dofbuf = malloc(bytes, M_SOLARIS, M_WAITOK);
13330297773Smarkj	for (i = 0; i < bytes; i++) {
13331297773Smarkj		c1 = dtrace_dof_char(data[i * 2]);
13332297773Smarkj		c2 = dtrace_dof_char(data[i * 2 + 1]);
13333297773Smarkj		if (c1 == UCHAR_MAX || c2 == UCHAR_MAX) {
13334297773Smarkj			dtrace_dof_error(NULL, "invalid hex char in DOF");
13335297773Smarkj			goto doferr;
13336297773Smarkj		}
13337297773Smarkj		dofbuf[i] = c1 * 16 + c2;
13338297773Smarkj	}
13339297773Smarkj
13340297773Smarkj	dof = (dof_hdr_t *)dofbuf;
13341297773Smarkj	if (bytes < dof->dofh_loadsz) {
13342297773Smarkj		dtrace_dof_error(NULL, "truncated DOF");
13343297773Smarkj		goto doferr;
13344297773Smarkj	}
13345297773Smarkj
13346297773Smarkj	if (dof->dofh_loadsz >= dtrace_dof_maxsize) {
13347297773Smarkj		dtrace_dof_error(NULL, "oversized DOF");
13348297773Smarkj		goto doferr;
13349297773Smarkj	}
13350297773Smarkj
13351297773Smarkj	return (dof);
13352297773Smarkj
13353297773Smarkjdoferr:
13354297773Smarkj	free(dof, M_SOLARIS);
13355297773Smarkj	return (NULL);
13356297773Smarkj#else /* __FreeBSD__ */
13357179193Sjb	uchar_t *buf;
13358179193Sjb	uint64_t loadsz;
13359179193Sjb	unsigned int len, i;
13360179193Sjb	dof_hdr_t *dof;
13361179193Sjb
13362179193Sjb	/*
13363179193Sjb	 * Unfortunately, array of values in .conf files are always (and
13364179193Sjb	 * only) interpreted to be integer arrays.  We must read our DOF
13365179193Sjb	 * as an integer array, and then squeeze it into a byte array.
13366179193Sjb	 */
13367179193Sjb	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0,
13368179193Sjb	    (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS)
13369179193Sjb		return (NULL);
13370179193Sjb
13371179193Sjb	for (i = 0; i < len; i++)
13372179193Sjb		buf[i] = (uchar_t)(((int *)buf)[i]);
13373179193Sjb
13374179193Sjb	if (len < sizeof (dof_hdr_t)) {
13375179193Sjb		ddi_prop_free(buf);
13376179193Sjb		dtrace_dof_error(NULL, "truncated header");
13377179193Sjb		return (NULL);
13378179193Sjb	}
13379179193Sjb
13380179193Sjb	if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) {
13381179193Sjb		ddi_prop_free(buf);
13382179193Sjb		dtrace_dof_error(NULL, "truncated DOF");
13383179193Sjb		return (NULL);
13384179193Sjb	}
13385179193Sjb
13386179193Sjb	if (loadsz >= dtrace_dof_maxsize) {
13387179193Sjb		ddi_prop_free(buf);
13388179193Sjb		dtrace_dof_error(NULL, "oversized DOF");
13389179193Sjb		return (NULL);
13390179193Sjb	}
13391179193Sjb
13392179193Sjb	dof = kmem_alloc(loadsz, KM_SLEEP);
13393179193Sjb	bcopy(buf, dof, loadsz);
13394179193Sjb	ddi_prop_free(buf);
13395179193Sjb
13396179193Sjb	return (dof);
13397297773Smarkj#endif /* !__FreeBSD__ */
13398179193Sjb}
13399179193Sjb
13400179193Sjbstatic void
13401179193Sjbdtrace_dof_destroy(dof_hdr_t *dof)
13402179193Sjb{
13403179193Sjb	kmem_free(dof, dof->dofh_loadsz);
13404179193Sjb}
13405179193Sjb
13406179193Sjb/*
13407179193Sjb * Return the dof_sec_t pointer corresponding to a given section index.  If the
13408179193Sjb * index is not valid, dtrace_dof_error() is called and NULL is returned.  If
13409179193Sjb * a type other than DOF_SECT_NONE is specified, the header is checked against
13410179193Sjb * this type and NULL is returned if the types do not match.
13411179193Sjb */
13412179193Sjbstatic dof_sec_t *
13413179193Sjbdtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i)
13414179193Sjb{
13415179193Sjb	dof_sec_t *sec = (dof_sec_t *)(uintptr_t)
13416179193Sjb	    ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize);
13417179193Sjb
13418179193Sjb	if (i >= dof->dofh_secnum) {
13419179193Sjb		dtrace_dof_error(dof, "referenced section index is invalid");
13420179193Sjb		return (NULL);
13421179193Sjb	}
13422179193Sjb
13423179193Sjb	if (!(sec->dofs_flags & DOF_SECF_LOAD)) {
13424179193Sjb		dtrace_dof_error(dof, "referenced section is not loadable");
13425179193Sjb		return (NULL);
13426179193Sjb	}
13427179193Sjb
13428179193Sjb	if (type != DOF_SECT_NONE && type != sec->dofs_type) {
13429179193Sjb		dtrace_dof_error(dof, "referenced section is the wrong type");
13430179193Sjb		return (NULL);
13431179193Sjb	}
13432179193Sjb
13433179193Sjb	return (sec);
13434179193Sjb}
13435179193Sjb
13436179193Sjbstatic dtrace_probedesc_t *
13437179193Sjbdtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc)
13438179193Sjb{
13439179193Sjb	dof_probedesc_t *probe;
13440179193Sjb	dof_sec_t *strtab;
13441179193Sjb	uintptr_t daddr = (uintptr_t)dof;
13442179193Sjb	uintptr_t str;
13443179193Sjb	size_t size;
13444179193Sjb
13445179193Sjb	if (sec->dofs_type != DOF_SECT_PROBEDESC) {
13446179193Sjb		dtrace_dof_error(dof, "invalid probe section");
13447179193Sjb		return (NULL);
13448179193Sjb	}
13449179193Sjb
13450179193Sjb	if (sec->dofs_align != sizeof (dof_secidx_t)) {
13451179193Sjb		dtrace_dof_error(dof, "bad alignment in probe description");
13452179193Sjb		return (NULL);
13453179193Sjb	}
13454179193Sjb
13455179193Sjb	if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) {
13456179193Sjb		dtrace_dof_error(dof, "truncated probe description");
13457179193Sjb		return (NULL);
13458179193Sjb	}
13459179193Sjb
13460179193Sjb	probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset);
13461179193Sjb	strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab);
13462179193Sjb
13463179193Sjb	if (strtab == NULL)
13464179193Sjb		return (NULL);
13465179193Sjb
13466179193Sjb	str = daddr + strtab->dofs_offset;
13467179193Sjb	size = strtab->dofs_size;
13468179193Sjb
13469179193Sjb	if (probe->dofp_provider >= strtab->dofs_size) {
13470179193Sjb		dtrace_dof_error(dof, "corrupt probe provider");
13471179193Sjb		return (NULL);
13472179193Sjb	}
13473179193Sjb
13474179193Sjb	(void) strncpy(desc->dtpd_provider,
13475179193Sjb	    (char *)(str + probe->dofp_provider),
13476179193Sjb	    MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider));
13477179193Sjb
13478179193Sjb	if (probe->dofp_mod >= strtab->dofs_size) {
13479179193Sjb		dtrace_dof_error(dof, "corrupt probe module");
13480179193Sjb		return (NULL);
13481179193Sjb	}
13482179193Sjb
13483179193Sjb	(void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod),
13484179193Sjb	    MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod));
13485179193Sjb
13486179193Sjb	if (probe->dofp_func >= strtab->dofs_size) {
13487179193Sjb		dtrace_dof_error(dof, "corrupt probe function");
13488179193Sjb		return (NULL);
13489179193Sjb	}
13490179193Sjb
13491179193Sjb	(void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func),
13492179193Sjb	    MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func));
13493179193Sjb
13494179193Sjb	if (probe->dofp_name >= strtab->dofs_size) {
13495179193Sjb		dtrace_dof_error(dof, "corrupt probe name");
13496179193Sjb		return (NULL);
13497179193Sjb	}
13498179193Sjb
13499179193Sjb	(void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name),
13500179193Sjb	    MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name));
13501179193Sjb
13502179193Sjb	return (desc);
13503179193Sjb}
13504179193Sjb
13505179193Sjbstatic dtrace_difo_t *
13506179193Sjbdtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13507179193Sjb    cred_t *cr)
13508179193Sjb{
13509179193Sjb	dtrace_difo_t *dp;
13510179193Sjb	size_t ttl = 0;
13511179193Sjb	dof_difohdr_t *dofd;
13512179193Sjb	uintptr_t daddr = (uintptr_t)dof;
13513179193Sjb	size_t max = dtrace_difo_maxsize;
13514179193Sjb	int i, l, n;
13515179193Sjb
13516179193Sjb	static const struct {
13517179193Sjb		int section;
13518179193Sjb		int bufoffs;
13519179193Sjb		int lenoffs;
13520179193Sjb		int entsize;
13521179193Sjb		int align;
13522179193Sjb		const char *msg;
13523179193Sjb	} difo[] = {
13524179193Sjb		{ DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf),
13525179193Sjb		offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t),
13526179193Sjb		sizeof (dif_instr_t), "multiple DIF sections" },
13527179193Sjb
13528179193Sjb		{ DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab),
13529179193Sjb		offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t),
13530179193Sjb		sizeof (uint64_t), "multiple integer tables" },
13531179193Sjb
13532179193Sjb		{ DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab),
13533179193Sjb		offsetof(dtrace_difo_t, dtdo_strlen), 0,
13534179193Sjb		sizeof (char), "multiple string tables" },
13535179193Sjb
13536179193Sjb		{ DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab),
13537179193Sjb		offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t),
13538179193Sjb		sizeof (uint_t), "multiple variable tables" },
13539179193Sjb
13540179198Sjb		{ DOF_SECT_NONE, 0, 0, 0, 0, NULL }
13541179193Sjb	};
13542179193Sjb
13543179193Sjb	if (sec->dofs_type != DOF_SECT_DIFOHDR) {
13544179193Sjb		dtrace_dof_error(dof, "invalid DIFO header section");
13545179193Sjb		return (NULL);
13546179193Sjb	}
13547179193Sjb
13548179193Sjb	if (sec->dofs_align != sizeof (dof_secidx_t)) {
13549179193Sjb		dtrace_dof_error(dof, "bad alignment in DIFO header");
13550179193Sjb		return (NULL);
13551179193Sjb	}
13552179193Sjb
13553179193Sjb	if (sec->dofs_size < sizeof (dof_difohdr_t) ||
13554179193Sjb	    sec->dofs_size % sizeof (dof_secidx_t)) {
13555179193Sjb		dtrace_dof_error(dof, "bad size in DIFO header");
13556179193Sjb		return (NULL);
13557179193Sjb	}
13558179193Sjb
13559179193Sjb	dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
13560179193Sjb	n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1;
13561179193Sjb
13562179193Sjb	dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
13563179193Sjb	dp->dtdo_rtype = dofd->dofd_rtype;
13564179193Sjb
13565179193Sjb	for (l = 0; l < n; l++) {
13566179193Sjb		dof_sec_t *subsec;
13567179193Sjb		void **bufp;
13568179193Sjb		uint32_t *lenp;
13569179193Sjb
13570179193Sjb		if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE,
13571179193Sjb		    dofd->dofd_links[l])) == NULL)
13572179193Sjb			goto err; /* invalid section link */
13573179193Sjb
13574179193Sjb		if (ttl + subsec->dofs_size > max) {
13575179193Sjb			dtrace_dof_error(dof, "exceeds maximum size");
13576179193Sjb			goto err;
13577179193Sjb		}
13578179193Sjb
13579179193Sjb		ttl += subsec->dofs_size;
13580179193Sjb
13581179193Sjb		for (i = 0; difo[i].section != DOF_SECT_NONE; i++) {
13582179193Sjb			if (subsec->dofs_type != difo[i].section)
13583179193Sjb				continue;
13584179193Sjb
13585179193Sjb			if (!(subsec->dofs_flags & DOF_SECF_LOAD)) {
13586179193Sjb				dtrace_dof_error(dof, "section not loaded");
13587179193Sjb				goto err;
13588179193Sjb			}
13589179193Sjb
13590179193Sjb			if (subsec->dofs_align != difo[i].align) {
13591179193Sjb				dtrace_dof_error(dof, "bad alignment");
13592179193Sjb				goto err;
13593179193Sjb			}
13594179193Sjb
13595179193Sjb			bufp = (void **)((uintptr_t)dp + difo[i].bufoffs);
13596179193Sjb			lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs);
13597179193Sjb
13598179193Sjb			if (*bufp != NULL) {
13599179193Sjb				dtrace_dof_error(dof, difo[i].msg);
13600179193Sjb				goto err;
13601179193Sjb			}
13602179193Sjb
13603179193Sjb			if (difo[i].entsize != subsec->dofs_entsize) {
13604179193Sjb				dtrace_dof_error(dof, "entry size mismatch");
13605179193Sjb				goto err;
13606179193Sjb			}
13607179193Sjb
13608179193Sjb			if (subsec->dofs_entsize != 0 &&
13609179193Sjb			    (subsec->dofs_size % subsec->dofs_entsize) != 0) {
13610179193Sjb				dtrace_dof_error(dof, "corrupt entry size");
13611179193Sjb				goto err;
13612179193Sjb			}
13613179193Sjb
13614179193Sjb			*lenp = subsec->dofs_size;
13615179193Sjb			*bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP);
13616179193Sjb			bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset),
13617179193Sjb			    *bufp, subsec->dofs_size);
13618179193Sjb
13619179193Sjb			if (subsec->dofs_entsize != 0)
13620179193Sjb				*lenp /= subsec->dofs_entsize;
13621179193Sjb
13622179193Sjb			break;
13623179193Sjb		}
13624179193Sjb
13625179193Sjb		/*
13626179193Sjb		 * If we encounter a loadable DIFO sub-section that is not
13627179193Sjb		 * known to us, assume this is a broken program and fail.
13628179193Sjb		 */
13629179193Sjb		if (difo[i].section == DOF_SECT_NONE &&
13630179193Sjb		    (subsec->dofs_flags & DOF_SECF_LOAD)) {
13631179193Sjb			dtrace_dof_error(dof, "unrecognized DIFO subsection");
13632179193Sjb			goto err;
13633179193Sjb		}
13634179193Sjb	}
13635179193Sjb
13636179193Sjb	if (dp->dtdo_buf == NULL) {
13637179193Sjb		/*
13638179193Sjb		 * We can't have a DIF object without DIF text.
13639179193Sjb		 */
13640179193Sjb		dtrace_dof_error(dof, "missing DIF text");
13641179193Sjb		goto err;
13642179193Sjb	}
13643179193Sjb
13644179193Sjb	/*
13645179193Sjb	 * Before we validate the DIF object, run through the variable table
13646179193Sjb	 * looking for the strings -- if any of their size are under, we'll set
13647179193Sjb	 * their size to be the system-wide default string size.  Note that
13648179193Sjb	 * this should _not_ happen if the "strsize" option has been set --
13649179193Sjb	 * in this case, the compiler should have set the size to reflect the
13650179193Sjb	 * setting of the option.
13651179193Sjb	 */
13652179193Sjb	for (i = 0; i < dp->dtdo_varlen; i++) {
13653179193Sjb		dtrace_difv_t *v = &dp->dtdo_vartab[i];
13654179193Sjb		dtrace_diftype_t *t = &v->dtdv_type;
13655179193Sjb
13656179193Sjb		if (v->dtdv_id < DIF_VAR_OTHER_UBASE)
13657179193Sjb			continue;
13658179193Sjb
13659179193Sjb		if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0)
13660179193Sjb			t->dtdt_size = dtrace_strsize_default;
13661179193Sjb	}
13662179193Sjb
13663179193Sjb	if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0)
13664179193Sjb		goto err;
13665179193Sjb
13666179193Sjb	dtrace_difo_init(dp, vstate);
13667179193Sjb	return (dp);
13668179193Sjb
13669179193Sjberr:
13670179193Sjb	kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
13671179193Sjb	kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
13672179193Sjb	kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
13673179193Sjb	kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
13674179193Sjb
13675179193Sjb	kmem_free(dp, sizeof (dtrace_difo_t));
13676179193Sjb	return (NULL);
13677179193Sjb}
13678179193Sjb
13679179193Sjbstatic dtrace_predicate_t *
13680179193Sjbdtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13681179193Sjb    cred_t *cr)
13682179193Sjb{
13683179193Sjb	dtrace_difo_t *dp;
13684179193Sjb
13685179193Sjb	if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL)
13686179193Sjb		return (NULL);
13687179193Sjb
13688179193Sjb	return (dtrace_predicate_create(dp));
13689179193Sjb}
13690179193Sjb
13691179193Sjbstatic dtrace_actdesc_t *
13692179193Sjbdtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13693179193Sjb    cred_t *cr)
13694179193Sjb{
13695179193Sjb	dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next;
13696179193Sjb	dof_actdesc_t *desc;
13697179193Sjb	dof_sec_t *difosec;
13698179193Sjb	size_t offs;
13699179193Sjb	uintptr_t daddr = (uintptr_t)dof;
13700179193Sjb	uint64_t arg;
13701179193Sjb	dtrace_actkind_t kind;
13702179193Sjb
13703179193Sjb	if (sec->dofs_type != DOF_SECT_ACTDESC) {
13704179193Sjb		dtrace_dof_error(dof, "invalid action section");
13705179193Sjb		return (NULL);
13706179193Sjb	}
13707179193Sjb
13708179193Sjb	if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) {
13709179193Sjb		dtrace_dof_error(dof, "truncated action description");
13710179193Sjb		return (NULL);
13711179193Sjb	}
13712179193Sjb
13713179193Sjb	if (sec->dofs_align != sizeof (uint64_t)) {
13714179193Sjb		dtrace_dof_error(dof, "bad alignment in action description");
13715179193Sjb		return (NULL);
13716179193Sjb	}
13717179193Sjb
13718179193Sjb	if (sec->dofs_size < sec->dofs_entsize) {
13719179193Sjb		dtrace_dof_error(dof, "section entry size exceeds total size");
13720179193Sjb		return (NULL);
13721179193Sjb	}
13722179193Sjb
13723179193Sjb	if (sec->dofs_entsize != sizeof (dof_actdesc_t)) {
13724179193Sjb		dtrace_dof_error(dof, "bad entry size in action description");
13725179193Sjb		return (NULL);
13726179193Sjb	}
13727179193Sjb
13728179193Sjb	if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) {
13729179193Sjb		dtrace_dof_error(dof, "actions exceed dtrace_actions_max");
13730179193Sjb		return (NULL);
13731179193Sjb	}
13732179193Sjb
13733179193Sjb	for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) {
13734179193Sjb		desc = (dof_actdesc_t *)(daddr +
13735179193Sjb		    (uintptr_t)sec->dofs_offset + offs);
13736179193Sjb		kind = (dtrace_actkind_t)desc->dofa_kind;
13737179193Sjb
13738248708Spfg		if ((DTRACEACT_ISPRINTFLIKE(kind) &&
13739179193Sjb		    (kind != DTRACEACT_PRINTA ||
13740248708Spfg		    desc->dofa_strtab != DOF_SECIDX_NONE)) ||
13741248708Spfg		    (kind == DTRACEACT_DIFEXPR &&
13742179193Sjb		    desc->dofa_strtab != DOF_SECIDX_NONE)) {
13743179193Sjb			dof_sec_t *strtab;
13744179193Sjb			char *str, *fmt;
13745179193Sjb			uint64_t i;
13746179193Sjb
13747179193Sjb			/*
13748248708Spfg			 * The argument to these actions is an index into the
13749248708Spfg			 * DOF string table.  For printf()-like actions, this
13750248708Spfg			 * is the format string.  For print(), this is the
13751248708Spfg			 * CTF type of the expression result.
13752179193Sjb			 */
13753179193Sjb			if ((strtab = dtrace_dof_sect(dof,
13754179193Sjb			    DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL)
13755179193Sjb				goto err;
13756179193Sjb
13757179193Sjb			str = (char *)((uintptr_t)dof +
13758179193Sjb			    (uintptr_t)strtab->dofs_offset);
13759179193Sjb
13760179193Sjb			for (i = desc->dofa_arg; i < strtab->dofs_size; i++) {
13761179193Sjb				if (str[i] == '\0')
13762179193Sjb					break;
13763179193Sjb			}
13764179193Sjb
13765179193Sjb			if (i >= strtab->dofs_size) {
13766179193Sjb				dtrace_dof_error(dof, "bogus format string");
13767179193Sjb				goto err;
13768179193Sjb			}
13769179193Sjb
13770179193Sjb			if (i == desc->dofa_arg) {
13771179193Sjb				dtrace_dof_error(dof, "empty format string");
13772179193Sjb				goto err;
13773179193Sjb			}
13774179193Sjb
13775179193Sjb			i -= desc->dofa_arg;
13776179193Sjb			fmt = kmem_alloc(i + 1, KM_SLEEP);
13777179193Sjb			bcopy(&str[desc->dofa_arg], fmt, i + 1);
13778179193Sjb			arg = (uint64_t)(uintptr_t)fmt;
13779179193Sjb		} else {
13780179193Sjb			if (kind == DTRACEACT_PRINTA) {
13781179193Sjb				ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE);
13782179193Sjb				arg = 0;
13783179193Sjb			} else {
13784179193Sjb				arg = desc->dofa_arg;
13785179193Sjb			}
13786179193Sjb		}
13787179193Sjb
13788179193Sjb		act = dtrace_actdesc_create(kind, desc->dofa_ntuple,
13789179193Sjb		    desc->dofa_uarg, arg);
13790179193Sjb
13791179193Sjb		if (last != NULL) {
13792179193Sjb			last->dtad_next = act;
13793179193Sjb		} else {
13794179193Sjb			first = act;
13795179193Sjb		}
13796179193Sjb
13797179193Sjb		last = act;
13798179193Sjb
13799179193Sjb		if (desc->dofa_difo == DOF_SECIDX_NONE)
13800179193Sjb			continue;
13801179193Sjb
13802179193Sjb		if ((difosec = dtrace_dof_sect(dof,
13803179193Sjb		    DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL)
13804179193Sjb			goto err;
13805179193Sjb
13806179193Sjb		act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr);
13807179193Sjb
13808179193Sjb		if (act->dtad_difo == NULL)
13809179193Sjb			goto err;
13810179193Sjb	}
13811179193Sjb
13812179193Sjb	ASSERT(first != NULL);
13813179193Sjb	return (first);
13814179193Sjb
13815179193Sjberr:
13816179193Sjb	for (act = first; act != NULL; act = next) {
13817179193Sjb		next = act->dtad_next;
13818179193Sjb		dtrace_actdesc_release(act, vstate);
13819179193Sjb	}
13820179193Sjb
13821179193Sjb	return (NULL);
13822179193Sjb}
13823179193Sjb
13824179193Sjbstatic dtrace_ecbdesc_t *
13825179193Sjbdtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
13826179193Sjb    cred_t *cr)
13827179193Sjb{
13828179193Sjb	dtrace_ecbdesc_t *ep;
13829179193Sjb	dof_ecbdesc_t *ecb;
13830179193Sjb	dtrace_probedesc_t *desc;
13831179193Sjb	dtrace_predicate_t *pred = NULL;
13832179193Sjb
13833179193Sjb	if (sec->dofs_size < sizeof (dof_ecbdesc_t)) {
13834179193Sjb		dtrace_dof_error(dof, "truncated ECB description");
13835179193Sjb		return (NULL);
13836179193Sjb	}
13837179193Sjb
13838179193Sjb	if (sec->dofs_align != sizeof (uint64_t)) {
13839179193Sjb		dtrace_dof_error(dof, "bad alignment in ECB description");
13840179193Sjb		return (NULL);
13841179193Sjb	}
13842179193Sjb
13843179193Sjb	ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset);
13844179193Sjb	sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes);
13845179193Sjb
13846179193Sjb	if (sec == NULL)
13847179193Sjb		return (NULL);
13848179193Sjb
13849179193Sjb	ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
13850179193Sjb	ep->dted_uarg = ecb->dofe_uarg;
13851179193Sjb	desc = &ep->dted_probe;
13852179193Sjb
13853179193Sjb	if (dtrace_dof_probedesc(dof, sec, desc) == NULL)
13854179193Sjb		goto err;
13855179193Sjb
13856179193Sjb	if (ecb->dofe_pred != DOF_SECIDX_NONE) {
13857179193Sjb		if ((sec = dtrace_dof_sect(dof,
13858179193Sjb		    DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL)
13859179193Sjb			goto err;
13860179193Sjb
13861179193Sjb		if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL)
13862179193Sjb			goto err;
13863179193Sjb
13864179193Sjb		ep->dted_pred.dtpdd_predicate = pred;
13865179193Sjb	}
13866179193Sjb
13867179193Sjb	if (ecb->dofe_actions != DOF_SECIDX_NONE) {
13868179193Sjb		if ((sec = dtrace_dof_sect(dof,
13869179193Sjb		    DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL)
13870179193Sjb			goto err;
13871179193Sjb
13872179193Sjb		ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr);
13873179193Sjb
13874179193Sjb		if (ep->dted_action == NULL)
13875179193Sjb			goto err;
13876179193Sjb	}
13877179193Sjb
13878179193Sjb	return (ep);
13879179193Sjb
13880179193Sjberr:
13881179193Sjb	if (pred != NULL)
13882179193Sjb		dtrace_predicate_release(pred, vstate);
13883179193Sjb	kmem_free(ep, sizeof (dtrace_ecbdesc_t));
13884179193Sjb	return (NULL);
13885179193Sjb}
13886179193Sjb
13887179193Sjb/*
13888179193Sjb * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the
13889361088Sdim * specified DOF.  SETX relocations are computed using 'ubase', the base load
13890361088Sdim * address of the object containing the DOF, and DOFREL relocations are relative
13891361088Sdim * to the relocation offset within the DOF.
13892179193Sjb */
13893179193Sjbstatic int
13894361088Sdimdtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase,
13895361088Sdim    uint64_t udaddr)
13896179193Sjb{
13897179193Sjb	uintptr_t daddr = (uintptr_t)dof;
13898326980Smarkj	uintptr_t ts_end;
13899179193Sjb	dof_relohdr_t *dofr =
13900179193Sjb	    (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
13901179193Sjb	dof_sec_t *ss, *rs, *ts;
13902179193Sjb	dof_relodesc_t *r;
13903179193Sjb	uint_t i, n;
13904179193Sjb
13905179193Sjb	if (sec->dofs_size < sizeof (dof_relohdr_t) ||
13906179193Sjb	    sec->dofs_align != sizeof (dof_secidx_t)) {
13907179193Sjb		dtrace_dof_error(dof, "invalid relocation header");
13908179193Sjb		return (-1);
13909179193Sjb	}
13910179193Sjb
13911179193Sjb	ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab);
13912179193Sjb	rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec);
13913179193Sjb	ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec);
13914326980Smarkj	ts_end = (uintptr_t)ts + sizeof (dof_sec_t);
13915179193Sjb
13916179193Sjb	if (ss == NULL || rs == NULL || ts == NULL)
13917179193Sjb		return (-1); /* dtrace_dof_error() has been called already */
13918179193Sjb
13919179193Sjb	if (rs->dofs_entsize < sizeof (dof_relodesc_t) ||
13920179193Sjb	    rs->dofs_align != sizeof (uint64_t)) {
13921179193Sjb		dtrace_dof_error(dof, "invalid relocation section");
13922179193Sjb		return (-1);
13923179193Sjb	}
13924179193Sjb
13925179193Sjb	r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset);
13926179193Sjb	n = rs->dofs_size / rs->dofs_entsize;
13927179193Sjb
13928179193Sjb	for (i = 0; i < n; i++) {
13929179193Sjb		uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset;
13930179193Sjb
13931179193Sjb		switch (r->dofr_type) {
13932179193Sjb		case DOF_RELO_NONE:
13933179193Sjb			break;
13934179193Sjb		case DOF_RELO_SETX:
13935361088Sdim		case DOF_RELO_DOFREL:
13936179193Sjb			if (r->dofr_offset >= ts->dofs_size || r->dofr_offset +
13937179193Sjb			    sizeof (uint64_t) > ts->dofs_size) {
13938179193Sjb				dtrace_dof_error(dof, "bad relocation offset");
13939179193Sjb				return (-1);
13940179193Sjb			}
13941179193Sjb
13942326980Smarkj			if (taddr >= (uintptr_t)ts && taddr < ts_end) {
13943326980Smarkj				dtrace_dof_error(dof, "bad relocation offset");
13944326980Smarkj				return (-1);
13945326980Smarkj			}
13946326980Smarkj
13947179193Sjb			if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) {
13948179193Sjb				dtrace_dof_error(dof, "misaligned setx relo");
13949179193Sjb				return (-1);
13950179193Sjb			}
13951179193Sjb
13952361088Sdim			if (r->dofr_type == DOF_RELO_SETX)
13953361088Sdim				*(uint64_t *)taddr += ubase;
13954361088Sdim			else
13955361088Sdim				*(uint64_t *)taddr +=
13956361088Sdim				    udaddr + ts->dofs_offset + r->dofr_offset;
13957179193Sjb			break;
13958179193Sjb		default:
13959179193Sjb			dtrace_dof_error(dof, "invalid relocation type");
13960179193Sjb			return (-1);
13961179193Sjb		}
13962179193Sjb
13963179193Sjb		r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize);
13964179193Sjb	}
13965179193Sjb
13966179193Sjb	return (0);
13967179193Sjb}
13968179193Sjb
13969179193Sjb/*
13970179193Sjb * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated
13971179193Sjb * header:  it should be at the front of a memory region that is at least
13972179193Sjb * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in
13973179193Sjb * size.  It need not be validated in any other way.
13974179193Sjb */
13975179193Sjbstatic int
13976179193Sjbdtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr,
13977361088Sdim    dtrace_enabling_t **enabp, uint64_t ubase, uint64_t udaddr, int noprobes)
13978179193Sjb{
13979179193Sjb	uint64_t len = dof->dofh_loadsz, seclen;
13980179193Sjb	uintptr_t daddr = (uintptr_t)dof;
13981179193Sjb	dtrace_ecbdesc_t *ep;
13982179193Sjb	dtrace_enabling_t *enab;
13983179193Sjb	uint_t i;
13984179193Sjb
13985179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
13986179193Sjb	ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t));
13987179193Sjb
13988179193Sjb	/*
13989179193Sjb	 * Check the DOF header identification bytes.  In addition to checking
13990179193Sjb	 * valid settings, we also verify that unused bits/bytes are zeroed so
13991179193Sjb	 * we can use them later without fear of regressing existing binaries.
13992179193Sjb	 */
13993179193Sjb	if (bcmp(&dof->dofh_ident[DOF_ID_MAG0],
13994179193Sjb	    DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) {
13995179193Sjb		dtrace_dof_error(dof, "DOF magic string mismatch");
13996179193Sjb		return (-1);
13997179193Sjb	}
13998179193Sjb
13999179193Sjb	if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 &&
14000179193Sjb	    dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) {
14001179193Sjb		dtrace_dof_error(dof, "DOF has invalid data model");
14002179193Sjb		return (-1);
14003179193Sjb	}
14004179193Sjb
14005179193Sjb	if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) {
14006179193Sjb		dtrace_dof_error(dof, "DOF encoding mismatch");
14007179193Sjb		return (-1);
14008179193Sjb	}
14009179193Sjb
14010179193Sjb	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
14011179193Sjb	    dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) {
14012179193Sjb		dtrace_dof_error(dof, "DOF version mismatch");
14013179193Sjb		return (-1);
14014179193Sjb	}
14015179193Sjb
14016179193Sjb	if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) {
14017179193Sjb		dtrace_dof_error(dof, "DOF uses unsupported instruction set");
14018179193Sjb		return (-1);
14019179193Sjb	}
14020179193Sjb
14021179193Sjb	if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) {
14022179193Sjb		dtrace_dof_error(dof, "DOF uses too many integer registers");
14023179193Sjb		return (-1);
14024179193Sjb	}
14025179193Sjb
14026179193Sjb	if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) {
14027179193Sjb		dtrace_dof_error(dof, "DOF uses too many tuple registers");
14028179193Sjb		return (-1);
14029179193Sjb	}
14030179193Sjb
14031179193Sjb	for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) {
14032179193Sjb		if (dof->dofh_ident[i] != 0) {
14033179193Sjb			dtrace_dof_error(dof, "DOF has invalid ident byte set");
14034179193Sjb			return (-1);
14035179193Sjb		}
14036179193Sjb	}
14037179193Sjb
14038179193Sjb	if (dof->dofh_flags & ~DOF_FL_VALID) {
14039179193Sjb		dtrace_dof_error(dof, "DOF has invalid flag bits set");
14040179193Sjb		return (-1);
14041179193Sjb	}
14042179193Sjb
14043179193Sjb	if (dof->dofh_secsize == 0) {
14044179193Sjb		dtrace_dof_error(dof, "zero section header size");
14045179193Sjb		return (-1);
14046179193Sjb	}
14047179193Sjb
14048179193Sjb	/*
14049179193Sjb	 * Check that the section headers don't exceed the amount of DOF
14050179193Sjb	 * data.  Note that we cast the section size and number of sections
14051179193Sjb	 * to uint64_t's to prevent possible overflow in the multiplication.
14052179193Sjb	 */
14053179193Sjb	seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize;
14054179193Sjb
14055179193Sjb	if (dof->dofh_secoff > len || seclen > len ||
14056179193Sjb	    dof->dofh_secoff + seclen > len) {
14057179193Sjb		dtrace_dof_error(dof, "truncated section headers");
14058179193Sjb		return (-1);
14059179193Sjb	}
14060179193Sjb
14061179193Sjb	if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) {
14062179193Sjb		dtrace_dof_error(dof, "misaligned section headers");
14063179193Sjb		return (-1);
14064179193Sjb	}
14065179193Sjb
14066179193Sjb	if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) {
14067179193Sjb		dtrace_dof_error(dof, "misaligned section size");
14068179193Sjb		return (-1);
14069179193Sjb	}
14070179193Sjb
14071179193Sjb	/*
14072179193Sjb	 * Take an initial pass through the section headers to be sure that
14073179193Sjb	 * the headers don't have stray offsets.  If the 'noprobes' flag is
14074179193Sjb	 * set, do not permit sections relating to providers, probes, or args.
14075179193Sjb	 */
14076179193Sjb	for (i = 0; i < dof->dofh_secnum; i++) {
14077179193Sjb		dof_sec_t *sec = (dof_sec_t *)(daddr +
14078179193Sjb		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
14079179193Sjb
14080179193Sjb		if (noprobes) {
14081179193Sjb			switch (sec->dofs_type) {
14082179193Sjb			case DOF_SECT_PROVIDER:
14083179193Sjb			case DOF_SECT_PROBES:
14084179193Sjb			case DOF_SECT_PRARGS:
14085179193Sjb			case DOF_SECT_PROFFS:
14086179193Sjb				dtrace_dof_error(dof, "illegal sections "
14087179193Sjb				    "for enabling");
14088179193Sjb				return (-1);
14089179193Sjb			}
14090179193Sjb		}
14091179193Sjb
14092268224Spfg		if (DOF_SEC_ISLOADABLE(sec->dofs_type) &&
14093268224Spfg		    !(sec->dofs_flags & DOF_SECF_LOAD)) {
14094268224Spfg			dtrace_dof_error(dof, "loadable section with load "
14095268224Spfg			    "flag unset");
14096268224Spfg			return (-1);
14097268224Spfg		}
14098268224Spfg
14099179193Sjb		if (!(sec->dofs_flags & DOF_SECF_LOAD))
14100179193Sjb			continue; /* just ignore non-loadable sections */
14101179193Sjb
14102275562Sdelphij		if (!ISP2(sec->dofs_align)) {
14103179193Sjb			dtrace_dof_error(dof, "bad section alignment");
14104179193Sjb			return (-1);
14105179193Sjb		}
14106179193Sjb
14107179193Sjb		if (sec->dofs_offset & (sec->dofs_align - 1)) {
14108179193Sjb			dtrace_dof_error(dof, "misaligned section");
14109179193Sjb			return (-1);
14110179193Sjb		}
14111179193Sjb
14112179193Sjb		if (sec->dofs_offset > len || sec->dofs_size > len ||
14113179193Sjb		    sec->dofs_offset + sec->dofs_size > len) {
14114179193Sjb			dtrace_dof_error(dof, "corrupt section header");
14115179193Sjb			return (-1);
14116179193Sjb		}
14117179193Sjb
14118179193Sjb		if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr +
14119179193Sjb		    sec->dofs_offset + sec->dofs_size - 1) != '\0') {
14120179193Sjb			dtrace_dof_error(dof, "non-terminating string table");
14121179193Sjb			return (-1);
14122179193Sjb		}
14123179193Sjb	}
14124179193Sjb
14125179193Sjb	/*
14126179193Sjb	 * Take a second pass through the sections and locate and perform any
14127179193Sjb	 * relocations that are present.  We do this after the first pass to
14128179193Sjb	 * be sure that all sections have had their headers validated.
14129179193Sjb	 */
14130179193Sjb	for (i = 0; i < dof->dofh_secnum; i++) {
14131179193Sjb		dof_sec_t *sec = (dof_sec_t *)(daddr +
14132179193Sjb		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
14133179193Sjb
14134179193Sjb		if (!(sec->dofs_flags & DOF_SECF_LOAD))
14135179193Sjb			continue; /* skip sections that are not loadable */
14136179193Sjb
14137179193Sjb		switch (sec->dofs_type) {
14138179193Sjb		case DOF_SECT_URELHDR:
14139361088Sdim			if (dtrace_dof_relocate(dof, sec, ubase, udaddr) != 0)
14140179193Sjb				return (-1);
14141179193Sjb			break;
14142179193Sjb		}
14143179193Sjb	}
14144179193Sjb
14145179193Sjb	if ((enab = *enabp) == NULL)
14146179193Sjb		enab = *enabp = dtrace_enabling_create(vstate);
14147179193Sjb
14148179193Sjb	for (i = 0; i < dof->dofh_secnum; i++) {
14149179193Sjb		dof_sec_t *sec = (dof_sec_t *)(daddr +
14150179193Sjb		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
14151179193Sjb
14152179193Sjb		if (sec->dofs_type != DOF_SECT_ECBDESC)
14153179193Sjb			continue;
14154179193Sjb
14155179193Sjb		if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) {
14156179193Sjb			dtrace_enabling_destroy(enab);
14157179193Sjb			*enabp = NULL;
14158179193Sjb			return (-1);
14159179193Sjb		}
14160179193Sjb
14161179193Sjb		dtrace_enabling_add(enab, ep);
14162179193Sjb	}
14163179193Sjb
14164179193Sjb	return (0);
14165179193Sjb}
14166179193Sjb
14167179193Sjb/*
14168179193Sjb * Process DOF for any options.  This routine assumes that the DOF has been
14169179193Sjb * at least processed by dtrace_dof_slurp().
14170179193Sjb */
14171179193Sjbstatic int
14172179193Sjbdtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state)
14173179193Sjb{
14174179193Sjb	int i, rval;
14175179193Sjb	uint32_t entsize;
14176179193Sjb	size_t offs;
14177179193Sjb	dof_optdesc_t *desc;
14178179193Sjb
14179179193Sjb	for (i = 0; i < dof->dofh_secnum; i++) {
14180179193Sjb		dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof +
14181179193Sjb		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
14182179193Sjb
14183179193Sjb		if (sec->dofs_type != DOF_SECT_OPTDESC)
14184179193Sjb			continue;
14185179193Sjb
14186179193Sjb		if (sec->dofs_align != sizeof (uint64_t)) {
14187179193Sjb			dtrace_dof_error(dof, "bad alignment in "
14188179193Sjb			    "option description");
14189179193Sjb			return (EINVAL);
14190179193Sjb		}
14191179193Sjb
14192179193Sjb		if ((entsize = sec->dofs_entsize) == 0) {
14193179193Sjb			dtrace_dof_error(dof, "zeroed option entry size");
14194179193Sjb			return (EINVAL);
14195179193Sjb		}
14196179193Sjb
14197179193Sjb		if (entsize < sizeof (dof_optdesc_t)) {
14198179193Sjb			dtrace_dof_error(dof, "bad option entry size");
14199179193Sjb			return (EINVAL);
14200179193Sjb		}
14201179193Sjb
14202179193Sjb		for (offs = 0; offs < sec->dofs_size; offs += entsize) {
14203179193Sjb			desc = (dof_optdesc_t *)((uintptr_t)dof +
14204179193Sjb			    (uintptr_t)sec->dofs_offset + offs);
14205179193Sjb
14206179193Sjb			if (desc->dofo_strtab != DOF_SECIDX_NONE) {
14207179193Sjb				dtrace_dof_error(dof, "non-zero option string");
14208179193Sjb				return (EINVAL);
14209179193Sjb			}
14210179193Sjb
14211179193Sjb			if (desc->dofo_value == DTRACEOPT_UNSET) {
14212179193Sjb				dtrace_dof_error(dof, "unset option");
14213179193Sjb				return (EINVAL);
14214179193Sjb			}
14215179193Sjb
14216179193Sjb			if ((rval = dtrace_state_option(state,
14217179193Sjb			    desc->dofo_option, desc->dofo_value)) != 0) {
14218179193Sjb				dtrace_dof_error(dof, "rejected option");
14219179193Sjb				return (rval);
14220179193Sjb			}
14221179193Sjb		}
14222179193Sjb	}
14223179193Sjb
14224179193Sjb	return (0);
14225179193Sjb}
14226179193Sjb
14227179193Sjb/*
14228179193Sjb * DTrace Consumer State Functions
14229179193Sjb */
14230179198Sjbstatic int
14231179193Sjbdtrace_dstate_init(dtrace_dstate_t *dstate, size_t size)
14232179193Sjb{
14233179193Sjb	size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize;
14234179193Sjb	void *base;
14235179193Sjb	uintptr_t limit;
14236179193Sjb	dtrace_dynvar_t *dvar, *next, *start;
14237179193Sjb	int i;
14238179193Sjb
14239179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
14240179193Sjb	ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL);
14241179193Sjb
14242179193Sjb	bzero(dstate, sizeof (dtrace_dstate_t));
14243179193Sjb
14244179193Sjb	if ((dstate->dtds_chunksize = chunksize) == 0)
14245179193Sjb		dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE;
14246179193Sjb
14247288415Smarkj	VERIFY(dstate->dtds_chunksize < LONG_MAX);
14248288415Smarkj
14249179193Sjb	if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)))
14250179193Sjb		size = min;
14251179193Sjb
14252262330Smarkj	if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL)
14253179193Sjb		return (ENOMEM);
14254179193Sjb
14255179193Sjb	dstate->dtds_size = size;
14256179193Sjb	dstate->dtds_base = base;
14257179193Sjb	dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP);
14258179193Sjb	bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t));
14259179193Sjb
14260179193Sjb	hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t));
14261179193Sjb
14262179193Sjb	if (hashsize != 1 && (hashsize & 1))
14263179193Sjb		hashsize--;
14264179193Sjb
14265179193Sjb	dstate->dtds_hashsize = hashsize;
14266179193Sjb	dstate->dtds_hash = dstate->dtds_base;
14267179193Sjb
14268179193Sjb	/*
14269179193Sjb	 * Set all of our hash buckets to point to the single sink, and (if
14270179193Sjb	 * it hasn't already been set), set the sink's hash value to be the
14271179193Sjb	 * sink sentinel value.  The sink is needed for dynamic variable
14272179193Sjb	 * lookups to know that they have iterated over an entire, valid hash
14273179193Sjb	 * chain.
14274179193Sjb	 */
14275179193Sjb	for (i = 0; i < hashsize; i++)
14276179193Sjb		dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink;
14277179193Sjb
14278179193Sjb	if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK)
14279179193Sjb		dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK;
14280179193Sjb
14281179193Sjb	/*
14282179193Sjb	 * Determine number of active CPUs.  Divide free list evenly among
14283179193Sjb	 * active CPUs.
14284179193Sjb	 */
14285179193Sjb	start = (dtrace_dynvar_t *)
14286179193Sjb	    ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t));
14287179193Sjb	limit = (uintptr_t)base + size;
14288179193Sjb
14289288415Smarkj	VERIFY((uintptr_t)start < limit);
14290288415Smarkj	VERIFY((uintptr_t)start >= (uintptr_t)base);
14291288415Smarkj
14292179193Sjb	maxper = (limit - (uintptr_t)start) / NCPU;
14293179193Sjb	maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize;
14294179193Sjb
14295277300Ssmh#ifndef illumos
14296209059Sjhb	CPU_FOREACH(i) {
14297209059Sjhb#else
14298179193Sjb	for (i = 0; i < NCPU; i++) {
14299179198Sjb#endif
14300179193Sjb		dstate->dtds_percpu[i].dtdsc_free = dvar = start;
14301179193Sjb
14302179193Sjb		/*
14303179193Sjb		 * If we don't even have enough chunks to make it once through
14304179193Sjb		 * NCPUs, we're just going to allocate everything to the first
14305179193Sjb		 * CPU.  And if we're on the last CPU, we're going to allocate
14306179193Sjb		 * whatever is left over.  In either case, we set the limit to
14307179193Sjb		 * be the limit of the dynamic variable space.
14308179193Sjb		 */
14309179193Sjb		if (maxper == 0 || i == NCPU - 1) {
14310179193Sjb			limit = (uintptr_t)base + size;
14311179193Sjb			start = NULL;
14312179193Sjb		} else {
14313179193Sjb			limit = (uintptr_t)start + maxper;
14314179193Sjb			start = (dtrace_dynvar_t *)limit;
14315179193Sjb		}
14316179193Sjb
14317288415Smarkj		VERIFY(limit <= (uintptr_t)base + size);
14318179193Sjb
14319179193Sjb		for (;;) {
14320179193Sjb			next = (dtrace_dynvar_t *)((uintptr_t)dvar +
14321179193Sjb			    dstate->dtds_chunksize);
14322179193Sjb
14323179193Sjb			if ((uintptr_t)next + dstate->dtds_chunksize >= limit)
14324179193Sjb				break;
14325179193Sjb
14326288415Smarkj			VERIFY((uintptr_t)dvar >= (uintptr_t)base &&
14327288415Smarkj			    (uintptr_t)dvar <= (uintptr_t)base + size);
14328179193Sjb			dvar->dtdv_next = next;
14329179193Sjb			dvar = next;
14330179193Sjb		}
14331179193Sjb
14332179193Sjb		if (maxper == 0)
14333179193Sjb			break;
14334179193Sjb	}
14335179193Sjb
14336179193Sjb	return (0);
14337179193Sjb}
14338179193Sjb
14339179198Sjbstatic void
14340179193Sjbdtrace_dstate_fini(dtrace_dstate_t *dstate)
14341179193Sjb{
14342179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
14343179193Sjb
14344179193Sjb	if (dstate->dtds_base == NULL)
14345179193Sjb		return;
14346179193Sjb
14347179193Sjb	kmem_free(dstate->dtds_base, dstate->dtds_size);
14348179193Sjb	kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu);
14349179193Sjb}
14350179193Sjb
14351179193Sjbstatic void
14352179193Sjbdtrace_vstate_fini(dtrace_vstate_t *vstate)
14353179193Sjb{
14354179193Sjb	/*
14355179193Sjb	 * Logical XOR, where are you?
14356179193Sjb	 */
14357179193Sjb	ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL));
14358179193Sjb
14359179193Sjb	if (vstate->dtvs_nglobals > 0) {
14360179193Sjb		kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals *
14361179193Sjb		    sizeof (dtrace_statvar_t *));
14362179193Sjb	}
14363179193Sjb
14364179193Sjb	if (vstate->dtvs_ntlocals > 0) {
14365179193Sjb		kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals *
14366179193Sjb		    sizeof (dtrace_difv_t));
14367179193Sjb	}
14368179193Sjb
14369179193Sjb	ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL));
14370179193Sjb
14371179193Sjb	if (vstate->dtvs_nlocals > 0) {
14372179193Sjb		kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals *
14373179193Sjb		    sizeof (dtrace_statvar_t *));
14374179193Sjb	}
14375179193Sjb}
14376179193Sjb
14377277300Ssmh#ifdef illumos
14378179193Sjbstatic void
14379179193Sjbdtrace_state_clean(dtrace_state_t *state)
14380179193Sjb{
14381179193Sjb	if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
14382179193Sjb		return;
14383179193Sjb
14384179193Sjb	dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
14385179193Sjb	dtrace_speculation_clean(state);
14386179193Sjb}
14387179193Sjb
14388179193Sjbstatic void
14389179193Sjbdtrace_state_deadman(dtrace_state_t *state)
14390179193Sjb{
14391179193Sjb	hrtime_t now;
14392179193Sjb
14393179193Sjb	dtrace_sync();
14394179193Sjb
14395179469Sjb	now = dtrace_gethrtime();
14396179469Sjb
14397179469Sjb	if (state != dtrace_anon.dta_state &&
14398179469Sjb	    now - state->dts_laststatus >= dtrace_deadman_user)
14399179469Sjb		return;
14400179469Sjb
14401179469Sjb	/*
14402179469Sjb	 * We must be sure that dts_alive never appears to be less than the
14403179469Sjb	 * value upon entry to dtrace_state_deadman(), and because we lack a
14404179469Sjb	 * dtrace_cas64(), we cannot store to it atomically.  We thus instead
14405179469Sjb	 * store INT64_MAX to it, followed by a memory barrier, followed by
14406179469Sjb	 * the new value.  This assures that dts_alive never appears to be
14407179469Sjb	 * less than its true value, regardless of the order in which the
14408179469Sjb	 * stores to the underlying storage are issued.
14409179469Sjb	 */
14410179469Sjb	state->dts_alive = INT64_MAX;
14411179469Sjb	dtrace_membar_producer();
14412179469Sjb	state->dts_alive = now;
14413179469Sjb}
14414277300Ssmh#else	/* !illumos */
14415179469Sjbstatic void
14416179469Sjbdtrace_state_clean(void *arg)
14417179469Sjb{
14418179469Sjb	dtrace_state_t *state = arg;
14419179469Sjb	dtrace_optval_t *opt = state->dts_options;
14420179469Sjb
14421179469Sjb	if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
14422179469Sjb		return;
14423179469Sjb
14424179469Sjb	dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
14425179469Sjb	dtrace_speculation_clean(state);
14426179469Sjb
14427179469Sjb	callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC,
14428179469Sjb	    dtrace_state_clean, state);
14429179469Sjb}
14430179469Sjb
14431179469Sjbstatic void
14432179469Sjbdtrace_state_deadman(void *arg)
14433179469Sjb{
14434179469Sjb	dtrace_state_t *state = arg;
14435179469Sjb	hrtime_t now;
14436179469Sjb
14437179469Sjb	dtrace_sync();
14438179469Sjb
14439179198Sjb	dtrace_debug_output();
14440179198Sjb
14441179193Sjb	now = dtrace_gethrtime();
14442179193Sjb
14443179193Sjb	if (state != dtrace_anon.dta_state &&
14444179193Sjb	    now - state->dts_laststatus >= dtrace_deadman_user)
14445179193Sjb		return;
14446179193Sjb
14447179193Sjb	/*
14448179193Sjb	 * We must be sure that dts_alive never appears to be less than the
14449179193Sjb	 * value upon entry to dtrace_state_deadman(), and because we lack a
14450179193Sjb	 * dtrace_cas64(), we cannot store to it atomically.  We thus instead
14451179193Sjb	 * store INT64_MAX to it, followed by a memory barrier, followed by
14452179193Sjb	 * the new value.  This assures that dts_alive never appears to be
14453179193Sjb	 * less than its true value, regardless of the order in which the
14454179193Sjb	 * stores to the underlying storage are issued.
14455179193Sjb	 */
14456179193Sjb	state->dts_alive = INT64_MAX;
14457179193Sjb	dtrace_membar_producer();
14458179193Sjb	state->dts_alive = now;
14459179469Sjb
14460179469Sjb	callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC,
14461179469Sjb	    dtrace_state_deadman, state);
14462179193Sjb}
14463277300Ssmh#endif	/* illumos */
14464179193Sjb
14465179198Sjbstatic dtrace_state_t *
14466277300Ssmh#ifdef illumos
14467179193Sjbdtrace_state_create(dev_t *devp, cred_t *cr)
14468179198Sjb#else
14469297773Smarkjdtrace_state_create(struct cdev *dev, struct ucred *cred __unused)
14470179198Sjb#endif
14471179193Sjb{
14472277300Ssmh#ifdef illumos
14473179193Sjb	minor_t minor;
14474179193Sjb	major_t major;
14475179198Sjb#else
14476179198Sjb	cred_t *cr = NULL;
14477179198Sjb	int m = 0;
14478179198Sjb#endif
14479179193Sjb	char c[30];
14480179193Sjb	dtrace_state_t *state;
14481179193Sjb	dtrace_optval_t *opt;
14482179193Sjb	int bufsize = NCPU * sizeof (dtrace_buffer_t), i;
14483316210Sgnn	int cpu_it;
14484179193Sjb
14485179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
14486179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
14487179193Sjb
14488277300Ssmh#ifdef illumos
14489179193Sjb	minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1,
14490179193Sjb	    VM_BESTFIT | VM_SLEEP);
14491179193Sjb
14492179193Sjb	if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) {
14493179193Sjb		vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
14494179193Sjb		return (NULL);
14495179193Sjb	}
14496179193Sjb
14497179193Sjb	state = ddi_get_soft_state(dtrace_softstate, minor);
14498179198Sjb#else
14499179198Sjb	if (dev != NULL) {
14500184698Srodrigc		cr = dev->si_cred;
14501183397Sed		m = dev2unit(dev);
14502278137Smarkj	}
14503179198Sjb
14504179198Sjb	/* Allocate memory for the state. */
14505179198Sjb	state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP);
14506179198Sjb#endif
14507179198Sjb
14508179193Sjb	state->dts_epid = DTRACE_EPIDNONE + 1;
14509179193Sjb
14510179198Sjb	(void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m);
14511277300Ssmh#ifdef illumos
14512179193Sjb	state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1,
14513179193Sjb	    NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
14514179193Sjb
14515179193Sjb	if (devp != NULL) {
14516179193Sjb		major = getemajor(*devp);
14517179193Sjb	} else {
14518179193Sjb		major = ddi_driver_major(dtrace_devi);
14519179193Sjb	}
14520179193Sjb
14521179193Sjb	state->dts_dev = makedevice(major, minor);
14522179193Sjb
14523179193Sjb	if (devp != NULL)
14524179193Sjb		*devp = state->dts_dev;
14525179198Sjb#else
14526260131Smarkj	state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx);
14527179198Sjb	state->dts_dev = dev;
14528179198Sjb#endif
14529179193Sjb
14530179193Sjb	/*
14531179193Sjb	 * We allocate NCPU buffers.  On the one hand, this can be quite
14532179193Sjb	 * a bit of memory per instance (nearly 36K on a Starcat).  On the
14533179193Sjb	 * other hand, it saves an additional memory reference in the probe
14534179193Sjb	 * path.
14535179193Sjb	 */
14536179193Sjb	state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP);
14537179193Sjb	state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP);
14538179469Sjb
14539316210Sgnn	/*
14540316210Sgnn         * Allocate and initialise the per-process per-CPU random state.
14541316210Sgnn	 * SI_SUB_RANDOM < SI_SUB_DTRACE_ANON therefore entropy device is
14542316210Sgnn         * assumed to be seeded at this point (if from Fortuna seed file).
14543316210Sgnn	 */
14544316210Sgnn	(void) read_random(&state->dts_rstate[0], 2 * sizeof(uint64_t));
14545316210Sgnn	for (cpu_it = 1; cpu_it < NCPU; cpu_it++) {
14546316210Sgnn		/*
14547316210Sgnn		 * Each CPU is assigned a 2^64 period, non-overlapping
14548316210Sgnn		 * subsequence.
14549316210Sgnn		 */
14550316210Sgnn		dtrace_xoroshiro128_plus_jump(state->dts_rstate[cpu_it-1],
14551316210Sgnn		    state->dts_rstate[cpu_it]);
14552316210Sgnn	}
14553316210Sgnn
14554277300Ssmh#ifdef illumos
14555179193Sjb	state->dts_cleaner = CYCLIC_NONE;
14556179193Sjb	state->dts_deadman = CYCLIC_NONE;
14557179469Sjb#else
14558283291Sjkim	callout_init(&state->dts_cleaner, 1);
14559283291Sjkim	callout_init(&state->dts_deadman, 1);
14560179469Sjb#endif
14561179193Sjb	state->dts_vstate.dtvs_state = state;
14562179193Sjb
14563179193Sjb	for (i = 0; i < DTRACEOPT_MAX; i++)
14564179193Sjb		state->dts_options[i] = DTRACEOPT_UNSET;
14565179193Sjb
14566179193Sjb	/*
14567179193Sjb	 * Set the default options.
14568179193Sjb	 */
14569179193Sjb	opt = state->dts_options;
14570179193Sjb	opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH;
14571179193Sjb	opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO;
14572179193Sjb	opt[DTRACEOPT_NSPEC] = dtrace_nspec_default;
14573179193Sjb	opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default;
14574179193Sjb	opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL;
14575179193Sjb	opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default;
14576179193Sjb	opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default;
14577179193Sjb	opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default;
14578179193Sjb	opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default;
14579179193Sjb	opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default;
14580179193Sjb	opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default;
14581179193Sjb	opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default;
14582179193Sjb	opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default;
14583179193Sjb	opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default;
14584179193Sjb
14585179193Sjb	state->dts_activity = DTRACE_ACTIVITY_INACTIVE;
14586179193Sjb
14587179193Sjb	/*
14588179193Sjb	 * Depending on the user credentials, we set flag bits which alter probe
14589179193Sjb	 * visibility or the amount of destructiveness allowed.  In the case of
14590179193Sjb	 * actual anonymous tracing, or the possession of all privileges, all of
14591179193Sjb	 * the normal checks are bypassed.
14592179193Sjb	 */
14593179193Sjb	if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
14594179193Sjb		state->dts_cred.dcr_visible = DTRACE_CRV_ALL;
14595179193Sjb		state->dts_cred.dcr_action = DTRACE_CRA_ALL;
14596179193Sjb	} else {
14597179193Sjb		/*
14598179193Sjb		 * Set up the credentials for this instantiation.  We take a
14599179193Sjb		 * hold on the credential to prevent it from disappearing on
14600179193Sjb		 * us; this in turn prevents the zone_t referenced by this
14601179193Sjb		 * credential from disappearing.  This means that we can
14602179193Sjb		 * examine the credential and the zone from probe context.
14603179193Sjb		 */
14604179193Sjb		crhold(cr);
14605179193Sjb		state->dts_cred.dcr_cred = cr;
14606179193Sjb
14607179193Sjb		/*
14608179193Sjb		 * CRA_PROC means "we have *some* privilege for dtrace" and
14609179193Sjb		 * unlocks the use of variables like pid, zonename, etc.
14610179193Sjb		 */
14611179193Sjb		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) ||
14612179193Sjb		    PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
14613179193Sjb			state->dts_cred.dcr_action |= DTRACE_CRA_PROC;
14614179193Sjb		}
14615179193Sjb
14616179193Sjb		/*
14617179193Sjb		 * dtrace_user allows use of syscall and profile providers.
14618179193Sjb		 * If the user also has proc_owner and/or proc_zone, we
14619179193Sjb		 * extend the scope to include additional visibility and
14620179193Sjb		 * destructive power.
14621179193Sjb		 */
14622179193Sjb		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) {
14623179193Sjb			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) {
14624179193Sjb				state->dts_cred.dcr_visible |=
14625179193Sjb				    DTRACE_CRV_ALLPROC;
14626179193Sjb
14627179193Sjb				state->dts_cred.dcr_action |=
14628179193Sjb				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
14629179193Sjb			}
14630179193Sjb
14631179193Sjb			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) {
14632179193Sjb				state->dts_cred.dcr_visible |=
14633179193Sjb				    DTRACE_CRV_ALLZONE;
14634179193Sjb
14635179193Sjb				state->dts_cred.dcr_action |=
14636179193Sjb				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
14637179193Sjb			}
14638179193Sjb
14639179193Sjb			/*
14640179193Sjb			 * If we have all privs in whatever zone this is,
14641179193Sjb			 * we can do destructive things to processes which
14642179193Sjb			 * have altered credentials.
14643179193Sjb			 */
14644277300Ssmh#ifdef illumos
14645179193Sjb			if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
14646179193Sjb			    cr->cr_zone->zone_privset)) {
14647179193Sjb				state->dts_cred.dcr_action |=
14648179193Sjb				    DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
14649179193Sjb			}
14650179198Sjb#endif
14651179193Sjb		}
14652179193Sjb
14653179193Sjb		/*
14654179193Sjb		 * Holding the dtrace_kernel privilege also implies that
14655179193Sjb		 * the user has the dtrace_user privilege from a visibility
14656179193Sjb		 * perspective.  But without further privileges, some
14657179193Sjb		 * destructive actions are not available.
14658179193Sjb		 */
14659179193Sjb		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) {
14660179193Sjb			/*
14661179193Sjb			 * Make all probes in all zones visible.  However,
14662179193Sjb			 * this doesn't mean that all actions become available
14663179193Sjb			 * to all zones.
14664179193Sjb			 */
14665179193Sjb			state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL |
14666179193Sjb			    DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE;
14667179193Sjb
14668179193Sjb			state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL |
14669179193Sjb			    DTRACE_CRA_PROC;
14670179193Sjb			/*
14671179193Sjb			 * Holding proc_owner means that destructive actions
14672179193Sjb			 * for *this* zone are allowed.
14673179193Sjb			 */
14674179193Sjb			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
14675179193Sjb				state->dts_cred.dcr_action |=
14676179193Sjb				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
14677179193Sjb
14678179193Sjb			/*
14679179193Sjb			 * Holding proc_zone means that destructive actions
14680179193Sjb			 * for this user/group ID in all zones is allowed.
14681179193Sjb			 */
14682179193Sjb			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
14683179193Sjb				state->dts_cred.dcr_action |=
14684179193Sjb				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
14685179193Sjb
14686277300Ssmh#ifdef illumos
14687179193Sjb			/*
14688179193Sjb			 * If we have all privs in whatever zone this is,
14689179193Sjb			 * we can do destructive things to processes which
14690179193Sjb			 * have altered credentials.
14691179193Sjb			 */
14692179193Sjb			if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
14693179193Sjb			    cr->cr_zone->zone_privset)) {
14694179193Sjb				state->dts_cred.dcr_action |=
14695179193Sjb				    DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
14696179193Sjb			}
14697179198Sjb#endif
14698179193Sjb		}
14699179193Sjb
14700179193Sjb		/*
14701179193Sjb		 * Holding the dtrace_proc privilege gives control over fasttrap
14702179193Sjb		 * and pid providers.  We need to grant wider destructive
14703179193Sjb		 * privileges in the event that the user has proc_owner and/or
14704179193Sjb		 * proc_zone.
14705179193Sjb		 */
14706179193Sjb		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
14707179193Sjb			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
14708179193Sjb				state->dts_cred.dcr_action |=
14709179193Sjb				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
14710179193Sjb
14711179193Sjb			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
14712179193Sjb				state->dts_cred.dcr_action |=
14713179193Sjb				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
14714179193Sjb		}
14715179193Sjb	}
14716179193Sjb
14717179193Sjb	return (state);
14718179193Sjb}
14719179193Sjb
14720179193Sjbstatic int
14721179193Sjbdtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which)
14722179193Sjb{
14723179193Sjb	dtrace_optval_t *opt = state->dts_options, size;
14724179198Sjb	processorid_t cpu = 0;;
14725262330Smarkj	int flags = 0, rval, factor, divisor = 1;
14726179193Sjb
14727179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
14728179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
14729179193Sjb	ASSERT(which < DTRACEOPT_MAX);
14730179193Sjb	ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE ||
14731179193Sjb	    (state == dtrace_anon.dta_state &&
14732179193Sjb	    state->dts_activity == DTRACE_ACTIVITY_ACTIVE));
14733179193Sjb
14734179193Sjb	if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0)
14735179193Sjb		return (0);
14736179193Sjb
14737179193Sjb	if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET)
14738179193Sjb		cpu = opt[DTRACEOPT_CPU];
14739179193Sjb
14740179193Sjb	if (which == DTRACEOPT_SPECSIZE)
14741179193Sjb		flags |= DTRACEBUF_NOSWITCH;
14742179193Sjb
14743179193Sjb	if (which == DTRACEOPT_BUFSIZE) {
14744179193Sjb		if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING)
14745179193Sjb			flags |= DTRACEBUF_RING;
14746179193Sjb
14747179193Sjb		if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL)
14748179193Sjb			flags |= DTRACEBUF_FILL;
14749179193Sjb
14750179193Sjb		if (state != dtrace_anon.dta_state ||
14751179193Sjb		    state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
14752179193Sjb			flags |= DTRACEBUF_INACTIVE;
14753179193Sjb	}
14754179193Sjb
14755262330Smarkj	for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) {
14756179193Sjb		/*
14757179193Sjb		 * The size must be 8-byte aligned.  If the size is not 8-byte
14758179193Sjb		 * aligned, drop it down by the difference.
14759179193Sjb		 */
14760179193Sjb		if (size & (sizeof (uint64_t) - 1))
14761179193Sjb			size -= size & (sizeof (uint64_t) - 1);
14762179193Sjb
14763179193Sjb		if (size < state->dts_reserve) {
14764179193Sjb			/*
14765179193Sjb			 * Buffers always must be large enough to accommodate
14766179193Sjb			 * their prereserved space.  We return E2BIG instead
14767179193Sjb			 * of ENOMEM in this case to allow for user-level
14768179193Sjb			 * software to differentiate the cases.
14769179193Sjb			 */
14770179193Sjb			return (E2BIG);
14771179193Sjb		}
14772179193Sjb
14773262330Smarkj		rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor);
14774179193Sjb
14775179193Sjb		if (rval != ENOMEM) {
14776179193Sjb			opt[which] = size;
14777179193Sjb			return (rval);
14778179193Sjb		}
14779179193Sjb
14780179193Sjb		if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
14781179193Sjb			return (rval);
14782262330Smarkj
14783262330Smarkj		for (divisor = 2; divisor < factor; divisor <<= 1)
14784262330Smarkj			continue;
14785179193Sjb	}
14786179193Sjb
14787179193Sjb	return (ENOMEM);
14788179193Sjb}
14789179193Sjb
14790179193Sjbstatic int
14791179193Sjbdtrace_state_buffers(dtrace_state_t *state)
14792179193Sjb{
14793179193Sjb	dtrace_speculation_t *spec = state->dts_speculations;
14794179193Sjb	int rval, i;
14795179193Sjb
14796179193Sjb	if ((rval = dtrace_state_buffer(state, state->dts_buffer,
14797179193Sjb	    DTRACEOPT_BUFSIZE)) != 0)
14798179193Sjb		return (rval);
14799179193Sjb
14800179193Sjb	if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer,
14801179193Sjb	    DTRACEOPT_AGGSIZE)) != 0)
14802179193Sjb		return (rval);
14803179193Sjb
14804179193Sjb	for (i = 0; i < state->dts_nspeculations; i++) {
14805179193Sjb		if ((rval = dtrace_state_buffer(state,
14806179193Sjb		    spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0)
14807179193Sjb			return (rval);
14808179193Sjb	}
14809179193Sjb
14810179193Sjb	return (0);
14811179193Sjb}
14812179193Sjb
14813179193Sjbstatic void
14814179193Sjbdtrace_state_prereserve(dtrace_state_t *state)
14815179193Sjb{
14816179193Sjb	dtrace_ecb_t *ecb;
14817179193Sjb	dtrace_probe_t *probe;
14818179193Sjb
14819179193Sjb	state->dts_reserve = 0;
14820179193Sjb
14821179193Sjb	if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
14822179193Sjb		return;
14823179193Sjb
14824179193Sjb	/*
14825179193Sjb	 * If our buffer policy is a "fill" buffer policy, we need to set the
14826179193Sjb	 * prereserved space to be the space required by the END probes.
14827179193Sjb	 */
14828179193Sjb	probe = dtrace_probes[dtrace_probeid_end - 1];
14829179193Sjb	ASSERT(probe != NULL);
14830179193Sjb
14831179193Sjb	for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
14832179193Sjb		if (ecb->dte_state != state)
14833179193Sjb			continue;
14834179193Sjb
14835179193Sjb		state->dts_reserve += ecb->dte_needed + ecb->dte_alignment;
14836179193Sjb	}
14837179193Sjb}
14838179193Sjb
14839179193Sjbstatic int
14840179193Sjbdtrace_state_go(dtrace_state_t *state, processorid_t *cpu)
14841179193Sjb{
14842179193Sjb	dtrace_optval_t *opt = state->dts_options, sz, nspec;
14843179193Sjb	dtrace_speculation_t *spec;
14844179193Sjb	dtrace_buffer_t *buf;
14845277300Ssmh#ifdef illumos
14846179193Sjb	cyc_handler_t hdlr;
14847179193Sjb	cyc_time_t when;
14848179469Sjb#endif
14849179193Sjb	int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t);
14850179193Sjb	dtrace_icookie_t cookie;
14851179193Sjb
14852179193Sjb	mutex_enter(&cpu_lock);
14853179193Sjb	mutex_enter(&dtrace_lock);
14854179193Sjb
14855179193Sjb	if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
14856179193Sjb		rval = EBUSY;
14857179193Sjb		goto out;
14858179193Sjb	}
14859179193Sjb
14860179193Sjb	/*
14861179193Sjb	 * Before we can perform any checks, we must prime all of the
14862179193Sjb	 * retained enablings that correspond to this state.
14863179193Sjb	 */
14864179193Sjb	dtrace_enabling_prime(state);
14865179193Sjb
14866179193Sjb	if (state->dts_destructive && !state->dts_cred.dcr_destructive) {
14867179193Sjb		rval = EACCES;
14868179193Sjb		goto out;
14869179193Sjb	}
14870179193Sjb
14871179193Sjb	dtrace_state_prereserve(state);
14872179193Sjb
14873179193Sjb	/*
14874179193Sjb	 * Now we want to do is try to allocate our speculations.
14875179193Sjb	 * We do not automatically resize the number of speculations; if
14876179193Sjb	 * this fails, we will fail the operation.
14877179193Sjb	 */
14878179193Sjb	nspec = opt[DTRACEOPT_NSPEC];
14879179193Sjb	ASSERT(nspec != DTRACEOPT_UNSET);
14880179193Sjb
14881179193Sjb	if (nspec > INT_MAX) {
14882179193Sjb		rval = ENOMEM;
14883179193Sjb		goto out;
14884179193Sjb	}
14885179193Sjb
14886262330Smarkj	spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t),
14887262330Smarkj	    KM_NOSLEEP | KM_NORMALPRI);
14888179193Sjb
14889179193Sjb	if (spec == NULL) {
14890179193Sjb		rval = ENOMEM;
14891179193Sjb		goto out;
14892179193Sjb	}
14893179193Sjb
14894179193Sjb	state->dts_speculations = spec;
14895179193Sjb	state->dts_nspeculations = (int)nspec;
14896179193Sjb
14897179193Sjb	for (i = 0; i < nspec; i++) {
14898262330Smarkj		if ((buf = kmem_zalloc(bufsize,
14899262330Smarkj		    KM_NOSLEEP | KM_NORMALPRI)) == NULL) {
14900179193Sjb			rval = ENOMEM;
14901179193Sjb			goto err;
14902179193Sjb		}
14903179193Sjb
14904179193Sjb		spec[i].dtsp_buffer = buf;
14905179193Sjb	}
14906179193Sjb
14907179193Sjb	if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) {
14908179193Sjb		if (dtrace_anon.dta_state == NULL) {
14909179193Sjb			rval = ENOENT;
14910179193Sjb			goto out;
14911179193Sjb		}
14912179193Sjb
14913179193Sjb		if (state->dts_necbs != 0) {
14914179193Sjb			rval = EALREADY;
14915179193Sjb			goto out;
14916179193Sjb		}
14917179193Sjb
14918179193Sjb		state->dts_anon = dtrace_anon_grab();
14919179193Sjb		ASSERT(state->dts_anon != NULL);
14920179193Sjb		state = state->dts_anon;
14921179193Sjb
14922179193Sjb		/*
14923179193Sjb		 * We want "grabanon" to be set in the grabbed state, so we'll
14924179193Sjb		 * copy that option value from the grabbing state into the
14925179193Sjb		 * grabbed state.
14926179193Sjb		 */
14927179193Sjb		state->dts_options[DTRACEOPT_GRABANON] =
14928179193Sjb		    opt[DTRACEOPT_GRABANON];
14929179193Sjb
14930179193Sjb		*cpu = dtrace_anon.dta_beganon;
14931179193Sjb
14932179193Sjb		/*
14933179193Sjb		 * If the anonymous state is active (as it almost certainly
14934179193Sjb		 * is if the anonymous enabling ultimately matched anything),
14935179193Sjb		 * we don't allow any further option processing -- but we
14936179193Sjb		 * don't return failure.
14937179193Sjb		 */
14938179193Sjb		if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
14939179193Sjb			goto out;
14940179193Sjb	}
14941179193Sjb
14942179193Sjb	if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET &&
14943179193Sjb	    opt[DTRACEOPT_AGGSIZE] != 0) {
14944179193Sjb		if (state->dts_aggregations == NULL) {
14945179193Sjb			/*
14946179193Sjb			 * We're not going to create an aggregation buffer
14947179193Sjb			 * because we don't have any ECBs that contain
14948179193Sjb			 * aggregations -- set this option to 0.
14949179193Sjb			 */
14950179193Sjb			opt[DTRACEOPT_AGGSIZE] = 0;
14951179193Sjb		} else {
14952179193Sjb			/*
14953179193Sjb			 * If we have an aggregation buffer, we must also have
14954179193Sjb			 * a buffer to use as scratch.
14955179193Sjb			 */
14956179193Sjb			if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET ||
14957179193Sjb			    opt[DTRACEOPT_BUFSIZE] < state->dts_needed) {
14958179193Sjb				opt[DTRACEOPT_BUFSIZE] = state->dts_needed;
14959179193Sjb			}
14960179193Sjb		}
14961179193Sjb	}
14962179193Sjb
14963179193Sjb	if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET &&
14964179193Sjb	    opt[DTRACEOPT_SPECSIZE] != 0) {
14965179193Sjb		if (!state->dts_speculates) {
14966179193Sjb			/*
14967179193Sjb			 * We're not going to create speculation buffers
14968179193Sjb			 * because we don't have any ECBs that actually
14969179193Sjb			 * speculate -- set the speculation size to 0.
14970179193Sjb			 */
14971179193Sjb			opt[DTRACEOPT_SPECSIZE] = 0;
14972179193Sjb		}
14973179193Sjb	}
14974179193Sjb
14975179193Sjb	/*
14976179193Sjb	 * The bare minimum size for any buffer that we're actually going to
14977179193Sjb	 * do anything to is sizeof (uint64_t).
14978179193Sjb	 */
14979179193Sjb	sz = sizeof (uint64_t);
14980179193Sjb
14981179193Sjb	if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) ||
14982179193Sjb	    (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) ||
14983179193Sjb	    (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) {
14984179193Sjb		/*
14985179193Sjb		 * A buffer size has been explicitly set to 0 (or to a size
14986179193Sjb		 * that will be adjusted to 0) and we need the space -- we
14987179193Sjb		 * need to return failure.  We return ENOSPC to differentiate
14988179193Sjb		 * it from failing to allocate a buffer due to failure to meet
14989179193Sjb		 * the reserve (for which we return E2BIG).
14990179193Sjb		 */
14991179193Sjb		rval = ENOSPC;
14992179193Sjb		goto out;
14993179193Sjb	}
14994179193Sjb
14995179193Sjb	if ((rval = dtrace_state_buffers(state)) != 0)
14996179193Sjb		goto err;
14997179193Sjb
14998179193Sjb	if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET)
14999179193Sjb		sz = dtrace_dstate_defsize;
15000179193Sjb
15001179193Sjb	do {
15002179193Sjb		rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz);
15003179193Sjb
15004179193Sjb		if (rval == 0)
15005179193Sjb			break;
15006179193Sjb
15007179193Sjb		if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
15008179193Sjb			goto err;
15009179193Sjb	} while (sz >>= 1);
15010179193Sjb
15011179193Sjb	opt[DTRACEOPT_DYNVARSIZE] = sz;
15012179193Sjb
15013179193Sjb	if (rval != 0)
15014179193Sjb		goto err;
15015179193Sjb
15016179193Sjb	if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max)
15017179193Sjb		opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max;
15018179193Sjb
15019179193Sjb	if (opt[DTRACEOPT_CLEANRATE] == 0)
15020179193Sjb		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
15021179193Sjb
15022179193Sjb	if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min)
15023179193Sjb		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min;
15024179193Sjb
15025179193Sjb	if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max)
15026179193Sjb		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
15027179193Sjb
15028179469Sjb	state->dts_alive = state->dts_laststatus = dtrace_gethrtime();
15029277300Ssmh#ifdef illumos
15030179193Sjb	hdlr.cyh_func = (cyc_func_t)dtrace_state_clean;
15031179193Sjb	hdlr.cyh_arg = state;
15032179193Sjb	hdlr.cyh_level = CY_LOW_LEVEL;
15033179193Sjb
15034179193Sjb	when.cyt_when = 0;
15035179193Sjb	when.cyt_interval = opt[DTRACEOPT_CLEANRATE];
15036179193Sjb
15037179193Sjb	state->dts_cleaner = cyclic_add(&hdlr, &when);
15038179193Sjb
15039179193Sjb	hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman;
15040179193Sjb	hdlr.cyh_arg = state;
15041179193Sjb	hdlr.cyh_level = CY_LOW_LEVEL;
15042179193Sjb
15043179193Sjb	when.cyt_when = 0;
15044179193Sjb	when.cyt_interval = dtrace_deadman_interval;
15045179193Sjb
15046179193Sjb	state->dts_deadman = cyclic_add(&hdlr, &when);
15047179469Sjb#else
15048179469Sjb	callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC,
15049179469Sjb	    dtrace_state_clean, state);
15050179469Sjb	callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC,
15051179469Sjb	    dtrace_state_deadman, state);
15052179469Sjb#endif
15053179193Sjb
15054179193Sjb	state->dts_activity = DTRACE_ACTIVITY_WARMUP;
15055179193Sjb
15056277300Ssmh#ifdef illumos
15057267929Srpaulo	if (state->dts_getf != 0 &&
15058267929Srpaulo	    !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) {
15059267929Srpaulo		/*
15060267929Srpaulo		 * We don't have kernel privs but we have at least one call
15061267929Srpaulo		 * to getf(); we need to bump our zone's count, and (if
15062267929Srpaulo		 * this is the first enabling to have an unprivileged call
15063267929Srpaulo		 * to getf()) we need to hook into closef().
15064267929Srpaulo		 */
15065267929Srpaulo		state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++;
15066267929Srpaulo
15067267929Srpaulo		if (dtrace_getf++ == 0) {
15068267929Srpaulo			ASSERT(dtrace_closef == NULL);
15069267929Srpaulo			dtrace_closef = dtrace_getf_barrier;
15070267929Srpaulo		}
15071267929Srpaulo	}
15072267929Srpaulo#endif
15073267929Srpaulo
15074179193Sjb	/*
15075179193Sjb	 * Now it's time to actually fire the BEGIN probe.  We need to disable
15076179193Sjb	 * interrupts here both to record the CPU on which we fired the BEGIN
15077179193Sjb	 * probe (the data from this CPU will be processed first at user
15078179193Sjb	 * level) and to manually activate the buffer for this CPU.
15079179193Sjb	 */
15080179193Sjb	cookie = dtrace_interrupt_disable();
15081179198Sjb	*cpu = curcpu;
15082179193Sjb	ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE);
15083179193Sjb	state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE;
15084179193Sjb
15085179193Sjb	dtrace_probe(dtrace_probeid_begin,
15086179193Sjb	    (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
15087179193Sjb	dtrace_interrupt_enable(cookie);
15088179193Sjb	/*
15089179193Sjb	 * We may have had an exit action from a BEGIN probe; only change our
15090179193Sjb	 * state to ACTIVE if we're still in WARMUP.
15091179193Sjb	 */
15092179193Sjb	ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP ||
15093179193Sjb	    state->dts_activity == DTRACE_ACTIVITY_DRAINING);
15094179193Sjb
15095179193Sjb	if (state->dts_activity == DTRACE_ACTIVITY_WARMUP)
15096179193Sjb		state->dts_activity = DTRACE_ACTIVITY_ACTIVE;
15097179193Sjb
15098297773Smarkj#ifdef __FreeBSD__
15099179193Sjb	/*
15100297773Smarkj	 * We enable anonymous tracing before APs are started, so we must
15101297773Smarkj	 * activate buffers using the current CPU.
15102297773Smarkj	 */
15103297773Smarkj	if (state == dtrace_anon.dta_state)
15104297773Smarkj		for (int i = 0; i < NCPU; i++)
15105297773Smarkj			dtrace_buffer_activate_cpu(state, i);
15106297773Smarkj	else
15107297773Smarkj		dtrace_xcall(DTRACE_CPUALL,
15108297773Smarkj		    (dtrace_xcall_t)dtrace_buffer_activate, state);
15109297773Smarkj#else
15110297773Smarkj	/*
15111179193Sjb	 * Regardless of whether or not now we're in ACTIVE or DRAINING, we
15112179193Sjb	 * want each CPU to transition its principal buffer out of the
15113179193Sjb	 * INACTIVE state.  Doing this assures that no CPU will suddenly begin
15114179193Sjb	 * processing an ECB halfway down a probe's ECB chain; all CPUs will
15115179193Sjb	 * atomically transition from processing none of a state's ECBs to
15116179193Sjb	 * processing all of them.
15117179193Sjb	 */
15118179193Sjb	dtrace_xcall(DTRACE_CPUALL,
15119179193Sjb	    (dtrace_xcall_t)dtrace_buffer_activate, state);
15120297773Smarkj#endif
15121179193Sjb	goto out;
15122179193Sjb
15123179193Sjberr:
15124179193Sjb	dtrace_buffer_free(state->dts_buffer);
15125179193Sjb	dtrace_buffer_free(state->dts_aggbuffer);
15126179193Sjb
15127179193Sjb	if ((nspec = state->dts_nspeculations) == 0) {
15128179193Sjb		ASSERT(state->dts_speculations == NULL);
15129179193Sjb		goto out;
15130179193Sjb	}
15131179193Sjb
15132179193Sjb	spec = state->dts_speculations;
15133179193Sjb	ASSERT(spec != NULL);
15134179193Sjb
15135179193Sjb	for (i = 0; i < state->dts_nspeculations; i++) {
15136179193Sjb		if ((buf = spec[i].dtsp_buffer) == NULL)
15137179193Sjb			break;
15138179193Sjb
15139179193Sjb		dtrace_buffer_free(buf);
15140179193Sjb		kmem_free(buf, bufsize);
15141179193Sjb	}
15142179193Sjb
15143179193Sjb	kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
15144179193Sjb	state->dts_nspeculations = 0;
15145179193Sjb	state->dts_speculations = NULL;
15146179193Sjb
15147179193Sjbout:
15148179193Sjb	mutex_exit(&dtrace_lock);
15149179193Sjb	mutex_exit(&cpu_lock);
15150179193Sjb
15151179193Sjb	return (rval);
15152179193Sjb}
15153179193Sjb
15154179193Sjbstatic int
15155179193Sjbdtrace_state_stop(dtrace_state_t *state, processorid_t *cpu)
15156179193Sjb{
15157179193Sjb	dtrace_icookie_t cookie;
15158179193Sjb
15159179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
15160179193Sjb
15161179193Sjb	if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE &&
15162179193Sjb	    state->dts_activity != DTRACE_ACTIVITY_DRAINING)
15163179193Sjb		return (EINVAL);
15164179193Sjb
15165179193Sjb	/*
15166179193Sjb	 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync
15167179193Sjb	 * to be sure that every CPU has seen it.  See below for the details
15168179193Sjb	 * on why this is done.
15169179193Sjb	 */
15170179193Sjb	state->dts_activity = DTRACE_ACTIVITY_DRAINING;
15171179193Sjb	dtrace_sync();
15172179193Sjb
15173179193Sjb	/*
15174179193Sjb	 * By this point, it is impossible for any CPU to be still processing
15175179193Sjb	 * with DTRACE_ACTIVITY_ACTIVE.  We can thus set our activity to
15176179193Sjb	 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any
15177179193Sjb	 * other CPU in dtrace_buffer_reserve().  This allows dtrace_probe()
15178179193Sjb	 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN
15179179193Sjb	 * iff we're in the END probe.
15180179193Sjb	 */
15181179193Sjb	state->dts_activity = DTRACE_ACTIVITY_COOLDOWN;
15182179193Sjb	dtrace_sync();
15183179193Sjb	ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN);
15184179193Sjb
15185179193Sjb	/*
15186179193Sjb	 * Finally, we can release the reserve and call the END probe.  We
15187179193Sjb	 * disable interrupts across calling the END probe to allow us to
15188179193Sjb	 * return the CPU on which we actually called the END probe.  This
15189179193Sjb	 * allows user-land to be sure that this CPU's principal buffer is
15190179193Sjb	 * processed last.
15191179193Sjb	 */
15192179193Sjb	state->dts_reserve = 0;
15193179193Sjb
15194179193Sjb	cookie = dtrace_interrupt_disable();
15195179198Sjb	*cpu = curcpu;
15196179193Sjb	dtrace_probe(dtrace_probeid_end,
15197179193Sjb	    (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
15198179193Sjb	dtrace_interrupt_enable(cookie);
15199179193Sjb
15200179193Sjb	state->dts_activity = DTRACE_ACTIVITY_STOPPED;
15201179193Sjb	dtrace_sync();
15202179193Sjb
15203277300Ssmh#ifdef illumos
15204267929Srpaulo	if (state->dts_getf != 0 &&
15205267929Srpaulo	    !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) {
15206267929Srpaulo		/*
15207267929Srpaulo		 * We don't have kernel privs but we have at least one call
15208267929Srpaulo		 * to getf(); we need to lower our zone's count, and (if
15209267929Srpaulo		 * this is the last enabling to have an unprivileged call
15210267929Srpaulo		 * to getf()) we need to clear the closef() hook.
15211267929Srpaulo		 */
15212267929Srpaulo		ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0);
15213267929Srpaulo		ASSERT(dtrace_closef == dtrace_getf_barrier);
15214267929Srpaulo		ASSERT(dtrace_getf > 0);
15215267929Srpaulo
15216267929Srpaulo		state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--;
15217267929Srpaulo
15218267929Srpaulo		if (--dtrace_getf == 0)
15219267929Srpaulo			dtrace_closef = NULL;
15220267929Srpaulo	}
15221267929Srpaulo#endif
15222267929Srpaulo
15223179193Sjb	return (0);
15224179193Sjb}
15225179193Sjb
15226179193Sjbstatic int
15227179193Sjbdtrace_state_option(dtrace_state_t *state, dtrace_optid_t option,
15228179193Sjb    dtrace_optval_t val)
15229179193Sjb{
15230179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
15231179193Sjb
15232179193Sjb	if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
15233179193Sjb		return (EBUSY);
15234179193Sjb
15235179193Sjb	if (option >= DTRACEOPT_MAX)
15236179193Sjb		return (EINVAL);
15237179193Sjb
15238179193Sjb	if (option != DTRACEOPT_CPU && val < 0)
15239179193Sjb		return (EINVAL);
15240179193Sjb
15241179193Sjb	switch (option) {
15242179193Sjb	case DTRACEOPT_DESTRUCTIVE:
15243179193Sjb		if (dtrace_destructive_disallow)
15244179193Sjb			return (EACCES);
15245179193Sjb
15246179193Sjb		state->dts_cred.dcr_destructive = 1;
15247179193Sjb		break;
15248179193Sjb
15249179193Sjb	case DTRACEOPT_BUFSIZE:
15250179193Sjb	case DTRACEOPT_DYNVARSIZE:
15251179193Sjb	case DTRACEOPT_AGGSIZE:
15252179193Sjb	case DTRACEOPT_SPECSIZE:
15253179193Sjb	case DTRACEOPT_STRSIZE:
15254179193Sjb		if (val < 0)
15255179193Sjb			return (EINVAL);
15256179193Sjb
15257179193Sjb		if (val >= LONG_MAX) {
15258179193Sjb			/*
15259179193Sjb			 * If this is an otherwise negative value, set it to
15260179193Sjb			 * the highest multiple of 128m less than LONG_MAX.
15261179193Sjb			 * Technically, we're adjusting the size without
15262179193Sjb			 * regard to the buffer resizing policy, but in fact,
15263179193Sjb			 * this has no effect -- if we set the buffer size to
15264179193Sjb			 * ~LONG_MAX and the buffer policy is ultimately set to
15265179193Sjb			 * be "manual", the buffer allocation is guaranteed to
15266179193Sjb			 * fail, if only because the allocation requires two
15267179193Sjb			 * buffers.  (We set the the size to the highest
15268179193Sjb			 * multiple of 128m because it ensures that the size
15269179193Sjb			 * will remain a multiple of a megabyte when
15270179193Sjb			 * repeatedly halved -- all the way down to 15m.)
15271179193Sjb			 */
15272179193Sjb			val = LONG_MAX - (1 << 27) + 1;
15273179193Sjb		}
15274179193Sjb	}
15275179193Sjb
15276179193Sjb	state->dts_options[option] = val;
15277179193Sjb
15278179193Sjb	return (0);
15279179193Sjb}
15280179193Sjb
15281179193Sjbstatic void
15282179193Sjbdtrace_state_destroy(dtrace_state_t *state)
15283179193Sjb{
15284179193Sjb	dtrace_ecb_t *ecb;
15285179193Sjb	dtrace_vstate_t *vstate = &state->dts_vstate;
15286277300Ssmh#ifdef illumos
15287179193Sjb	minor_t minor = getminor(state->dts_dev);
15288179198Sjb#endif
15289179193Sjb	int i, bufsize = NCPU * sizeof (dtrace_buffer_t);
15290179193Sjb	dtrace_speculation_t *spec = state->dts_speculations;
15291179193Sjb	int nspec = state->dts_nspeculations;
15292179193Sjb	uint32_t match;
15293179193Sjb
15294179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
15295179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
15296179193Sjb
15297179193Sjb	/*
15298179193Sjb	 * First, retract any retained enablings for this state.
15299179193Sjb	 */
15300179193Sjb	dtrace_enabling_retract(state);
15301179193Sjb	ASSERT(state->dts_nretained == 0);
15302179193Sjb
15303179193Sjb	if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE ||
15304179193Sjb	    state->dts_activity == DTRACE_ACTIVITY_DRAINING) {
15305179193Sjb		/*
15306179193Sjb		 * We have managed to come into dtrace_state_destroy() on a
15307179193Sjb		 * hot enabling -- almost certainly because of a disorderly
15308179193Sjb		 * shutdown of a consumer.  (That is, a consumer that is
15309179193Sjb		 * exiting without having called dtrace_stop().) In this case,
15310179193Sjb		 * we're going to set our activity to be KILLED, and then
15311179193Sjb		 * issue a sync to be sure that everyone is out of probe
15312179193Sjb		 * context before we start blowing away ECBs.
15313179193Sjb		 */
15314179193Sjb		state->dts_activity = DTRACE_ACTIVITY_KILLED;
15315179193Sjb		dtrace_sync();
15316179193Sjb	}
15317179193Sjb
15318179193Sjb	/*
15319179193Sjb	 * Release the credential hold we took in dtrace_state_create().
15320179193Sjb	 */
15321179193Sjb	if (state->dts_cred.dcr_cred != NULL)
15322179193Sjb		crfree(state->dts_cred.dcr_cred);
15323179193Sjb
15324179193Sjb	/*
15325179193Sjb	 * Now we can safely disable and destroy any enabled probes.  Because
15326179193Sjb	 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress
15327179193Sjb	 * (especially if they're all enabled), we take two passes through the
15328179193Sjb	 * ECBs:  in the first, we disable just DTRACE_PRIV_KERNEL probes, and
15329179193Sjb	 * in the second we disable whatever is left over.
15330179193Sjb	 */
15331179193Sjb	for (match = DTRACE_PRIV_KERNEL; ; match = 0) {
15332179193Sjb		for (i = 0; i < state->dts_necbs; i++) {
15333179193Sjb			if ((ecb = state->dts_ecbs[i]) == NULL)
15334179193Sjb				continue;
15335179193Sjb
15336179193Sjb			if (match && ecb->dte_probe != NULL) {
15337179193Sjb				dtrace_probe_t *probe = ecb->dte_probe;
15338179193Sjb				dtrace_provider_t *prov = probe->dtpr_provider;
15339179193Sjb
15340179193Sjb				if (!(prov->dtpv_priv.dtpp_flags & match))
15341179193Sjb					continue;
15342179193Sjb			}
15343179193Sjb
15344179193Sjb			dtrace_ecb_disable(ecb);
15345179193Sjb			dtrace_ecb_destroy(ecb);
15346179193Sjb		}
15347179193Sjb
15348179193Sjb		if (!match)
15349179193Sjb			break;
15350179193Sjb	}
15351179193Sjb
15352179193Sjb	/*
15353179193Sjb	 * Before we free the buffers, perform one more sync to assure that
15354179193Sjb	 * every CPU is out of probe context.
15355179193Sjb	 */
15356179193Sjb	dtrace_sync();
15357179193Sjb
15358179193Sjb	dtrace_buffer_free(state->dts_buffer);
15359179193Sjb	dtrace_buffer_free(state->dts_aggbuffer);
15360179193Sjb
15361179193Sjb	for (i = 0; i < nspec; i++)
15362179193Sjb		dtrace_buffer_free(spec[i].dtsp_buffer);
15363179193Sjb
15364277300Ssmh#ifdef illumos
15365179193Sjb	if (state->dts_cleaner != CYCLIC_NONE)
15366179193Sjb		cyclic_remove(state->dts_cleaner);
15367179193Sjb
15368179193Sjb	if (state->dts_deadman != CYCLIC_NONE)
15369179193Sjb		cyclic_remove(state->dts_deadman);
15370179469Sjb#else
15371179469Sjb	callout_stop(&state->dts_cleaner);
15372181879Sjb	callout_drain(&state->dts_cleaner);
15373179469Sjb	callout_stop(&state->dts_deadman);
15374181879Sjb	callout_drain(&state->dts_deadman);
15375179469Sjb#endif
15376179193Sjb
15377179193Sjb	dtrace_dstate_fini(&vstate->dtvs_dynvars);
15378179193Sjb	dtrace_vstate_fini(vstate);
15379179198Sjb	if (state->dts_ecbs != NULL)
15380179198Sjb		kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *));
15381179193Sjb
15382179193Sjb	if (state->dts_aggregations != NULL) {
15383179193Sjb#ifdef DEBUG
15384179193Sjb		for (i = 0; i < state->dts_naggregations; i++)
15385179193Sjb			ASSERT(state->dts_aggregations[i] == NULL);
15386179193Sjb#endif
15387179193Sjb		ASSERT(state->dts_naggregations > 0);
15388179193Sjb		kmem_free(state->dts_aggregations,
15389179193Sjb		    state->dts_naggregations * sizeof (dtrace_aggregation_t *));
15390179193Sjb	}
15391179193Sjb
15392179193Sjb	kmem_free(state->dts_buffer, bufsize);
15393179193Sjb	kmem_free(state->dts_aggbuffer, bufsize);
15394179193Sjb
15395179193Sjb	for (i = 0; i < nspec; i++)
15396179193Sjb		kmem_free(spec[i].dtsp_buffer, bufsize);
15397179193Sjb
15398179198Sjb	if (spec != NULL)
15399179198Sjb		kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
15400179193Sjb
15401179193Sjb	dtrace_format_destroy(state);
15402179193Sjb
15403179198Sjb	if (state->dts_aggid_arena != NULL) {
15404277300Ssmh#ifdef illumos
15405179198Sjb		vmem_destroy(state->dts_aggid_arena);
15406260131Smarkj#else
15407260131Smarkj		delete_unrhdr(state->dts_aggid_arena);
15408260131Smarkj#endif
15409179198Sjb		state->dts_aggid_arena = NULL;
15410179198Sjb	}
15411277300Ssmh#ifdef illumos
15412179193Sjb	ddi_soft_state_free(dtrace_softstate, minor);
15413179193Sjb	vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
15414179198Sjb#endif
15415179193Sjb}
15416179193Sjb
15417179193Sjb/*
15418179193Sjb * DTrace Anonymous Enabling Functions
15419179193Sjb */
15420179193Sjbstatic dtrace_state_t *
15421179193Sjbdtrace_anon_grab(void)
15422179193Sjb{
15423179193Sjb	dtrace_state_t *state;
15424179193Sjb
15425179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
15426179193Sjb
15427179193Sjb	if ((state = dtrace_anon.dta_state) == NULL) {
15428179193Sjb		ASSERT(dtrace_anon.dta_enabling == NULL);
15429179193Sjb		return (NULL);
15430179193Sjb	}
15431179193Sjb
15432179193Sjb	ASSERT(dtrace_anon.dta_enabling != NULL);
15433179193Sjb	ASSERT(dtrace_retained != NULL);
15434179193Sjb
15435179193Sjb	dtrace_enabling_destroy(dtrace_anon.dta_enabling);
15436179193Sjb	dtrace_anon.dta_enabling = NULL;
15437179193Sjb	dtrace_anon.dta_state = NULL;
15438179193Sjb
15439179193Sjb	return (state);
15440179193Sjb}
15441179193Sjb
15442179193Sjbstatic void
15443179193Sjbdtrace_anon_property(void)
15444179193Sjb{
15445179193Sjb	int i, rv;
15446179193Sjb	dtrace_state_t *state;
15447179193Sjb	dof_hdr_t *dof;
15448179193Sjb	char c[32];		/* enough for "dof-data-" + digits */
15449179193Sjb
15450179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
15451179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
15452179193Sjb
15453179193Sjb	for (i = 0; ; i++) {
15454179193Sjb		(void) snprintf(c, sizeof (c), "dof-data-%d", i);
15455179193Sjb
15456179193Sjb		dtrace_err_verbose = 1;
15457179193Sjb
15458179193Sjb		if ((dof = dtrace_dof_property(c)) == NULL) {
15459179193Sjb			dtrace_err_verbose = 0;
15460179193Sjb			break;
15461179193Sjb		}
15462179193Sjb
15463277300Ssmh#ifdef illumos
15464179193Sjb		/*
15465179193Sjb		 * We want to create anonymous state, so we need to transition
15466179193Sjb		 * the kernel debugger to indicate that DTrace is active.  If
15467179193Sjb		 * this fails (e.g. because the debugger has modified text in
15468179193Sjb		 * some way), we won't continue with the processing.
15469179193Sjb		 */
15470179193Sjb		if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
15471179193Sjb			cmn_err(CE_NOTE, "kernel debugger active; anonymous "
15472179193Sjb			    "enabling ignored.");
15473179193Sjb			dtrace_dof_destroy(dof);
15474179193Sjb			break;
15475179193Sjb		}
15476179198Sjb#endif
15477179193Sjb
15478179193Sjb		/*
15479179193Sjb		 * If we haven't allocated an anonymous state, we'll do so now.
15480179193Sjb		 */
15481179193Sjb		if ((state = dtrace_anon.dta_state) == NULL) {
15482179193Sjb			state = dtrace_state_create(NULL, NULL);
15483179193Sjb			dtrace_anon.dta_state = state;
15484179193Sjb
15485179193Sjb			if (state == NULL) {
15486179193Sjb				/*
15487179193Sjb				 * This basically shouldn't happen:  the only
15488179193Sjb				 * failure mode from dtrace_state_create() is a
15489179193Sjb				 * failure of ddi_soft_state_zalloc() that
15490179193Sjb				 * itself should never happen.  Still, the
15491179193Sjb				 * interface allows for a failure mode, and
15492179193Sjb				 * we want to fail as gracefully as possible:
15493179193Sjb				 * we'll emit an error message and cease
15494179193Sjb				 * processing anonymous state in this case.
15495179193Sjb				 */
15496179193Sjb				cmn_err(CE_WARN, "failed to create "
15497179193Sjb				    "anonymous state");
15498179193Sjb				dtrace_dof_destroy(dof);
15499179193Sjb				break;
15500179193Sjb			}
15501179193Sjb		}
15502179193Sjb
15503179193Sjb		rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(),
15504361088Sdim		    &dtrace_anon.dta_enabling, 0, 0, B_TRUE);
15505179193Sjb
15506179193Sjb		if (rv == 0)
15507179193Sjb			rv = dtrace_dof_options(dof, state);
15508179193Sjb
15509179193Sjb		dtrace_err_verbose = 0;
15510179193Sjb		dtrace_dof_destroy(dof);
15511179193Sjb
15512179193Sjb		if (rv != 0) {
15513179193Sjb			/*
15514179193Sjb			 * This is malformed DOF; chuck any anonymous state
15515179193Sjb			 * that we created.
15516179193Sjb			 */
15517179193Sjb			ASSERT(dtrace_anon.dta_enabling == NULL);
15518179193Sjb			dtrace_state_destroy(state);
15519179193Sjb			dtrace_anon.dta_state = NULL;
15520179193Sjb			break;
15521179193Sjb		}
15522179193Sjb
15523179193Sjb		ASSERT(dtrace_anon.dta_enabling != NULL);
15524179193Sjb	}
15525179193Sjb
15526179193Sjb	if (dtrace_anon.dta_enabling != NULL) {
15527179193Sjb		int rval;
15528179193Sjb
15529179193Sjb		/*
15530179193Sjb		 * dtrace_enabling_retain() can only fail because we are
15531179193Sjb		 * trying to retain more enablings than are allowed -- but
15532179193Sjb		 * we only have one anonymous enabling, and we are guaranteed
15533179193Sjb		 * to be allowed at least one retained enabling; we assert
15534179193Sjb		 * that dtrace_enabling_retain() returns success.
15535179193Sjb		 */
15536179193Sjb		rval = dtrace_enabling_retain(dtrace_anon.dta_enabling);
15537179193Sjb		ASSERT(rval == 0);
15538179193Sjb
15539179193Sjb		dtrace_enabling_dump(dtrace_anon.dta_enabling);
15540179193Sjb	}
15541179193Sjb}
15542179193Sjb
15543179193Sjb/*
15544179193Sjb * DTrace Helper Functions
15545179193Sjb */
15546179193Sjbstatic void
15547179193Sjbdtrace_helper_trace(dtrace_helper_action_t *helper,
15548179193Sjb    dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where)
15549179193Sjb{
15550179193Sjb	uint32_t size, next, nnext, i;
15551278166Spfg	dtrace_helptrace_t *ent, *buffer;
15552179198Sjb	uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags;
15553179193Sjb
15554278166Spfg	if ((buffer = dtrace_helptrace_buffer) == NULL)
15555179193Sjb		return;
15556179193Sjb
15557179193Sjb	ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals);
15558179193Sjb
15559179193Sjb	/*
15560179193Sjb	 * What would a tracing framework be without its own tracing
15561179193Sjb	 * framework?  (Well, a hell of a lot simpler, for starters...)
15562179193Sjb	 */
15563179193Sjb	size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals *
15564179193Sjb	    sizeof (uint64_t) - sizeof (uint64_t);
15565179193Sjb
15566179193Sjb	/*
15567179193Sjb	 * Iterate until we can allocate a slot in the trace buffer.
15568179193Sjb	 */
15569179193Sjb	do {
15570179193Sjb		next = dtrace_helptrace_next;
15571179193Sjb
15572179193Sjb		if (next + size < dtrace_helptrace_bufsize) {
15573179193Sjb			nnext = next + size;
15574179193Sjb		} else {
15575179193Sjb			nnext = size;
15576179193Sjb		}
15577179193Sjb	} while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next);
15578179193Sjb
15579179193Sjb	/*
15580179193Sjb	 * We have our slot; fill it in.
15581179193Sjb	 */
15582278166Spfg	if (nnext == size) {
15583278166Spfg		dtrace_helptrace_wrapped++;
15584179193Sjb		next = 0;
15585278166Spfg	}
15586179193Sjb
15587278166Spfg	ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next);
15588179193Sjb	ent->dtht_helper = helper;
15589179193Sjb	ent->dtht_where = where;
15590179193Sjb	ent->dtht_nlocals = vstate->dtvs_nlocals;
15591179193Sjb
15592179193Sjb	ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ?
15593179193Sjb	    mstate->dtms_fltoffs : -1;
15594179193Sjb	ent->dtht_fault = DTRACE_FLAGS2FLT(flags);
15595179198Sjb	ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval;
15596179193Sjb
15597179193Sjb	for (i = 0; i < vstate->dtvs_nlocals; i++) {
15598179193Sjb		dtrace_statvar_t *svar;
15599179193Sjb
15600179193Sjb		if ((svar = vstate->dtvs_locals[i]) == NULL)
15601179193Sjb			continue;
15602179193Sjb
15603179193Sjb		ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t));
15604179193Sjb		ent->dtht_locals[i] =
15605179198Sjb		    ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu];
15606179193Sjb	}
15607179193Sjb}
15608179193Sjb
15609179193Sjbstatic uint64_t
15610179193Sjbdtrace_helper(int which, dtrace_mstate_t *mstate,
15611179193Sjb    dtrace_state_t *state, uint64_t arg0, uint64_t arg1)
15612179193Sjb{
15613179198Sjb	uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
15614179193Sjb	uint64_t sarg0 = mstate->dtms_arg[0];
15615179193Sjb	uint64_t sarg1 = mstate->dtms_arg[1];
15616211608Srpaulo	uint64_t rval = 0;
15617179193Sjb	dtrace_helpers_t *helpers = curproc->p_dtrace_helpers;
15618179193Sjb	dtrace_helper_action_t *helper;
15619179193Sjb	dtrace_vstate_t *vstate;
15620179193Sjb	dtrace_difo_t *pred;
15621278166Spfg	int i, trace = dtrace_helptrace_buffer != NULL;
15622179193Sjb
15623179193Sjb	ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS);
15624179193Sjb
15625179193Sjb	if (helpers == NULL)
15626179193Sjb		return (0);
15627179193Sjb
15628179193Sjb	if ((helper = helpers->dthps_actions[which]) == NULL)
15629179193Sjb		return (0);
15630179193Sjb
15631179193Sjb	vstate = &helpers->dthps_vstate;
15632179193Sjb	mstate->dtms_arg[0] = arg0;
15633179193Sjb	mstate->dtms_arg[1] = arg1;
15634179193Sjb
15635179193Sjb	/*
15636179193Sjb	 * Now iterate over each helper.  If its predicate evaluates to 'true',
15637179193Sjb	 * we'll call the corresponding actions.  Note that the below calls
15638179193Sjb	 * to dtrace_dif_emulate() may set faults in machine state.  This is
15639179193Sjb	 * okay:  our caller (the outer dtrace_dif_emulate()) will simply plow
15640179193Sjb	 * the stored DIF offset with its own (which is the desired behavior).
15641179193Sjb	 * Also, note the calls to dtrace_dif_emulate() may allocate scratch
15642179193Sjb	 * from machine state; this is okay, too.
15643179193Sjb	 */
15644179193Sjb	for (; helper != NULL; helper = helper->dtha_next) {
15645179193Sjb		if ((pred = helper->dtha_predicate) != NULL) {
15646179193Sjb			if (trace)
15647179193Sjb				dtrace_helper_trace(helper, mstate, vstate, 0);
15648179193Sjb
15649179193Sjb			if (!dtrace_dif_emulate(pred, mstate, vstate, state))
15650179193Sjb				goto next;
15651179193Sjb
15652179193Sjb			if (*flags & CPU_DTRACE_FAULT)
15653179193Sjb				goto err;
15654179193Sjb		}
15655179193Sjb
15656179193Sjb		for (i = 0; i < helper->dtha_nactions; i++) {
15657179193Sjb			if (trace)
15658179193Sjb				dtrace_helper_trace(helper,
15659179193Sjb				    mstate, vstate, i + 1);
15660179193Sjb
15661179193Sjb			rval = dtrace_dif_emulate(helper->dtha_actions[i],
15662179193Sjb			    mstate, vstate, state);
15663179193Sjb
15664179193Sjb			if (*flags & CPU_DTRACE_FAULT)
15665179193Sjb				goto err;
15666179193Sjb		}
15667179193Sjb
15668179193Sjbnext:
15669179193Sjb		if (trace)
15670179193Sjb			dtrace_helper_trace(helper, mstate, vstate,
15671179193Sjb			    DTRACE_HELPTRACE_NEXT);
15672179193Sjb	}
15673179193Sjb
15674179193Sjb	if (trace)
15675179193Sjb		dtrace_helper_trace(helper, mstate, vstate,
15676179193Sjb		    DTRACE_HELPTRACE_DONE);
15677179193Sjb
15678179193Sjb	/*
15679179193Sjb	 * Restore the arg0 that we saved upon entry.
15680179193Sjb	 */
15681179193Sjb	mstate->dtms_arg[0] = sarg0;
15682179193Sjb	mstate->dtms_arg[1] = sarg1;
15683179193Sjb
15684179193Sjb	return (rval);
15685179193Sjb
15686179193Sjberr:
15687179193Sjb	if (trace)
15688179193Sjb		dtrace_helper_trace(helper, mstate, vstate,
15689179193Sjb		    DTRACE_HELPTRACE_ERR);
15690179193Sjb
15691179193Sjb	/*
15692179193Sjb	 * Restore the arg0 that we saved upon entry.
15693179193Sjb	 */
15694179193Sjb	mstate->dtms_arg[0] = sarg0;
15695179193Sjb	mstate->dtms_arg[1] = sarg1;
15696179193Sjb
15697179198Sjb	return (0);
15698179193Sjb}
15699179193Sjb
15700179193Sjbstatic void
15701179193Sjbdtrace_helper_action_destroy(dtrace_helper_action_t *helper,
15702179193Sjb    dtrace_vstate_t *vstate)
15703179193Sjb{
15704179193Sjb	int i;
15705179193Sjb
15706179193Sjb	if (helper->dtha_predicate != NULL)
15707179193Sjb		dtrace_difo_release(helper->dtha_predicate, vstate);
15708179193Sjb
15709179193Sjb	for (i = 0; i < helper->dtha_nactions; i++) {
15710179193Sjb		ASSERT(helper->dtha_actions[i] != NULL);
15711179193Sjb		dtrace_difo_release(helper->dtha_actions[i], vstate);
15712179193Sjb	}
15713179193Sjb
15714179193Sjb	kmem_free(helper->dtha_actions,
15715179193Sjb	    helper->dtha_nactions * sizeof (dtrace_difo_t *));
15716179193Sjb	kmem_free(helper, sizeof (dtrace_helper_action_t));
15717179193Sjb}
15718179193Sjb
15719179193Sjbstatic int
15720281257Smarkjdtrace_helper_destroygen(dtrace_helpers_t *help, int gen)
15721179193Sjb{
15722179193Sjb	proc_t *p = curproc;
15723179193Sjb	dtrace_vstate_t *vstate;
15724179193Sjb	int i;
15725179193Sjb
15726281257Smarkj	if (help == NULL)
15727281257Smarkj		help = p->p_dtrace_helpers;
15728281257Smarkj
15729179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
15730179193Sjb
15731179193Sjb	if (help == NULL || gen > help->dthps_generation)
15732179193Sjb		return (EINVAL);
15733179193Sjb
15734179193Sjb	vstate = &help->dthps_vstate;
15735179193Sjb
15736179193Sjb	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
15737179193Sjb		dtrace_helper_action_t *last = NULL, *h, *next;
15738179193Sjb
15739179193Sjb		for (h = help->dthps_actions[i]; h != NULL; h = next) {
15740179193Sjb			next = h->dtha_next;
15741179193Sjb
15742179193Sjb			if (h->dtha_generation == gen) {
15743179193Sjb				if (last != NULL) {
15744179193Sjb					last->dtha_next = next;
15745179193Sjb				} else {
15746179193Sjb					help->dthps_actions[i] = next;
15747179193Sjb				}
15748179193Sjb
15749179193Sjb				dtrace_helper_action_destroy(h, vstate);
15750179193Sjb			} else {
15751179193Sjb				last = h;
15752179193Sjb			}
15753179193Sjb		}
15754179193Sjb	}
15755179193Sjb
15756179193Sjb	/*
15757179193Sjb	 * Interate until we've cleared out all helper providers with the
15758179193Sjb	 * given generation number.
15759179193Sjb	 */
15760179193Sjb	for (;;) {
15761179193Sjb		dtrace_helper_provider_t *prov;
15762179193Sjb
15763179193Sjb		/*
15764179193Sjb		 * Look for a helper provider with the right generation. We
15765179193Sjb		 * have to start back at the beginning of the list each time
15766179193Sjb		 * because we drop dtrace_lock. It's unlikely that we'll make
15767179193Sjb		 * more than two passes.
15768179193Sjb		 */
15769179193Sjb		for (i = 0; i < help->dthps_nprovs; i++) {
15770179193Sjb			prov = help->dthps_provs[i];
15771179193Sjb
15772179193Sjb			if (prov->dthp_generation == gen)
15773179193Sjb				break;
15774179193Sjb		}
15775179193Sjb
15776179193Sjb		/*
15777179193Sjb		 * If there were no matches, we're done.
15778179193Sjb		 */
15779179193Sjb		if (i == help->dthps_nprovs)
15780179193Sjb			break;
15781179193Sjb
15782179193Sjb		/*
15783179193Sjb		 * Move the last helper provider into this slot.
15784179193Sjb		 */
15785179193Sjb		help->dthps_nprovs--;
15786179193Sjb		help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs];
15787179193Sjb		help->dthps_provs[help->dthps_nprovs] = NULL;
15788179193Sjb
15789179193Sjb		mutex_exit(&dtrace_lock);
15790179193Sjb
15791179193Sjb		/*
15792179193Sjb		 * If we have a meta provider, remove this helper provider.
15793179193Sjb		 */
15794179193Sjb		mutex_enter(&dtrace_meta_lock);
15795179193Sjb		if (dtrace_meta_pid != NULL) {
15796179193Sjb			ASSERT(dtrace_deferred_pid == NULL);
15797179193Sjb			dtrace_helper_provider_remove(&prov->dthp_prov,
15798179193Sjb			    p->p_pid);
15799179193Sjb		}
15800179193Sjb		mutex_exit(&dtrace_meta_lock);
15801179193Sjb
15802179193Sjb		dtrace_helper_provider_destroy(prov);
15803179193Sjb
15804179193Sjb		mutex_enter(&dtrace_lock);
15805179193Sjb	}
15806179193Sjb
15807179193Sjb	return (0);
15808179193Sjb}
15809179193Sjb
15810179193Sjbstatic int
15811179193Sjbdtrace_helper_validate(dtrace_helper_action_t *helper)
15812179193Sjb{
15813179193Sjb	int err = 0, i;
15814179193Sjb	dtrace_difo_t *dp;
15815179193Sjb
15816179193Sjb	if ((dp = helper->dtha_predicate) != NULL)
15817179193Sjb		err += dtrace_difo_validate_helper(dp);
15818179193Sjb
15819179193Sjb	for (i = 0; i < helper->dtha_nactions; i++)
15820179193Sjb		err += dtrace_difo_validate_helper(helper->dtha_actions[i]);
15821179193Sjb
15822179193Sjb	return (err == 0);
15823179193Sjb}
15824179193Sjb
15825179193Sjbstatic int
15826281257Smarkjdtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep,
15827281257Smarkj    dtrace_helpers_t *help)
15828179193Sjb{
15829179193Sjb	dtrace_helper_action_t *helper, *last;
15830179193Sjb	dtrace_actdesc_t *act;
15831179193Sjb	dtrace_vstate_t *vstate;
15832179193Sjb	dtrace_predicate_t *pred;
15833179193Sjb	int count = 0, nactions = 0, i;
15834179193Sjb
15835179193Sjb	if (which < 0 || which >= DTRACE_NHELPER_ACTIONS)
15836179193Sjb		return (EINVAL);
15837179193Sjb
15838179193Sjb	last = help->dthps_actions[which];
15839179193Sjb	vstate = &help->dthps_vstate;
15840179193Sjb
15841179193Sjb	for (count = 0; last != NULL; last = last->dtha_next) {
15842179193Sjb		count++;
15843179193Sjb		if (last->dtha_next == NULL)
15844179193Sjb			break;
15845179193Sjb	}
15846179193Sjb
15847179193Sjb	/*
15848179193Sjb	 * If we already have dtrace_helper_actions_max helper actions for this
15849179193Sjb	 * helper action type, we'll refuse to add a new one.
15850179193Sjb	 */
15851179193Sjb	if (count >= dtrace_helper_actions_max)
15852179193Sjb		return (ENOSPC);
15853179193Sjb
15854179193Sjb	helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP);
15855179193Sjb	helper->dtha_generation = help->dthps_generation;
15856179193Sjb
15857179193Sjb	if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) {
15858179193Sjb		ASSERT(pred->dtp_difo != NULL);
15859179193Sjb		dtrace_difo_hold(pred->dtp_difo);
15860179193Sjb		helper->dtha_predicate = pred->dtp_difo;
15861179193Sjb	}
15862179193Sjb
15863179193Sjb	for (act = ep->dted_action; act != NULL; act = act->dtad_next) {
15864179193Sjb		if (act->dtad_kind != DTRACEACT_DIFEXPR)
15865179193Sjb			goto err;
15866179193Sjb
15867179193Sjb		if (act->dtad_difo == NULL)
15868179193Sjb			goto err;
15869179193Sjb
15870179193Sjb		nactions++;
15871179193Sjb	}
15872179193Sjb
15873179193Sjb	helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) *
15874179193Sjb	    (helper->dtha_nactions = nactions), KM_SLEEP);
15875179193Sjb
15876179193Sjb	for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) {
15877179193Sjb		dtrace_difo_hold(act->dtad_difo);
15878179193Sjb		helper->dtha_actions[i++] = act->dtad_difo;
15879179193Sjb	}
15880179193Sjb
15881179193Sjb	if (!dtrace_helper_validate(helper))
15882179193Sjb		goto err;
15883179193Sjb
15884179193Sjb	if (last == NULL) {
15885179193Sjb		help->dthps_actions[which] = helper;
15886179193Sjb	} else {
15887179193Sjb		last->dtha_next = helper;
15888179193Sjb	}
15889179193Sjb
15890179193Sjb	if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) {
15891179193Sjb		dtrace_helptrace_nlocals = vstate->dtvs_nlocals;
15892179193Sjb		dtrace_helptrace_next = 0;
15893179193Sjb	}
15894179193Sjb
15895179193Sjb	return (0);
15896179193Sjberr:
15897179193Sjb	dtrace_helper_action_destroy(helper, vstate);
15898179193Sjb	return (EINVAL);
15899179193Sjb}
15900179193Sjb
15901179193Sjbstatic void
15902179193Sjbdtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help,
15903179193Sjb    dof_helper_t *dofhp)
15904179193Sjb{
15905179193Sjb	ASSERT(MUTEX_NOT_HELD(&dtrace_lock));
15906179193Sjb
15907179193Sjb	mutex_enter(&dtrace_meta_lock);
15908179193Sjb	mutex_enter(&dtrace_lock);
15909179193Sjb
15910179193Sjb	if (!dtrace_attached() || dtrace_meta_pid == NULL) {
15911179193Sjb		/*
15912179193Sjb		 * If the dtrace module is loaded but not attached, or if
15913179193Sjb		 * there aren't isn't a meta provider registered to deal with
15914179193Sjb		 * these provider descriptions, we need to postpone creating
15915179193Sjb		 * the actual providers until later.
15916179193Sjb		 */
15917179193Sjb
15918179193Sjb		if (help->dthps_next == NULL && help->dthps_prev == NULL &&
15919179193Sjb		    dtrace_deferred_pid != help) {
15920179193Sjb			help->dthps_deferred = 1;
15921179193Sjb			help->dthps_pid = p->p_pid;
15922179193Sjb			help->dthps_next = dtrace_deferred_pid;
15923179193Sjb			help->dthps_prev = NULL;
15924179193Sjb			if (dtrace_deferred_pid != NULL)
15925179193Sjb				dtrace_deferred_pid->dthps_prev = help;
15926179193Sjb			dtrace_deferred_pid = help;
15927179193Sjb		}
15928179193Sjb
15929179193Sjb		mutex_exit(&dtrace_lock);
15930179193Sjb
15931179193Sjb	} else if (dofhp != NULL) {
15932179193Sjb		/*
15933179193Sjb		 * If the dtrace module is loaded and we have a particular
15934179193Sjb		 * helper provider description, pass that off to the
15935179193Sjb		 * meta provider.
15936179193Sjb		 */
15937179193Sjb
15938179193Sjb		mutex_exit(&dtrace_lock);
15939179193Sjb
15940179193Sjb		dtrace_helper_provide(dofhp, p->p_pid);
15941179193Sjb
15942179193Sjb	} else {
15943179193Sjb		/*
15944179193Sjb		 * Otherwise, just pass all the helper provider descriptions
15945179193Sjb		 * off to the meta provider.
15946179193Sjb		 */
15947179193Sjb
15948179193Sjb		int i;
15949179193Sjb		mutex_exit(&dtrace_lock);
15950179193Sjb
15951179193Sjb		for (i = 0; i < help->dthps_nprovs; i++) {
15952179193Sjb			dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
15953179193Sjb			    p->p_pid);
15954179193Sjb		}
15955179193Sjb	}
15956179193Sjb
15957179193Sjb	mutex_exit(&dtrace_meta_lock);
15958179193Sjb}
15959179193Sjb
15960179193Sjbstatic int
15961281257Smarkjdtrace_helper_provider_add(dof_helper_t *dofhp, dtrace_helpers_t *help, int gen)
15962179193Sjb{
15963179193Sjb	dtrace_helper_provider_t *hprov, **tmp_provs;
15964179193Sjb	uint_t tmp_maxprovs, i;
15965179193Sjb
15966179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
15967179193Sjb	ASSERT(help != NULL);
15968179193Sjb
15969179193Sjb	/*
15970179193Sjb	 * If we already have dtrace_helper_providers_max helper providers,
15971179193Sjb	 * we're refuse to add a new one.
15972179193Sjb	 */
15973179193Sjb	if (help->dthps_nprovs >= dtrace_helper_providers_max)
15974179193Sjb		return (ENOSPC);
15975179193Sjb
15976179193Sjb	/*
15977179193Sjb	 * Check to make sure this isn't a duplicate.
15978179193Sjb	 */
15979179193Sjb	for (i = 0; i < help->dthps_nprovs; i++) {
15980291963Smarkj		if (dofhp->dofhp_addr ==
15981291963Smarkj		    help->dthps_provs[i]->dthp_prov.dofhp_addr)
15982179193Sjb			return (EALREADY);
15983179193Sjb	}
15984179193Sjb
15985179193Sjb	hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP);
15986179193Sjb	hprov->dthp_prov = *dofhp;
15987179193Sjb	hprov->dthp_ref = 1;
15988179193Sjb	hprov->dthp_generation = gen;
15989179193Sjb
15990179193Sjb	/*
15991179193Sjb	 * Allocate a bigger table for helper providers if it's already full.
15992179193Sjb	 */
15993179193Sjb	if (help->dthps_maxprovs == help->dthps_nprovs) {
15994179193Sjb		tmp_maxprovs = help->dthps_maxprovs;
15995179193Sjb		tmp_provs = help->dthps_provs;
15996179193Sjb
15997179193Sjb		if (help->dthps_maxprovs == 0)
15998179193Sjb			help->dthps_maxprovs = 2;
15999179193Sjb		else
16000179193Sjb			help->dthps_maxprovs *= 2;
16001179193Sjb		if (help->dthps_maxprovs > dtrace_helper_providers_max)
16002179193Sjb			help->dthps_maxprovs = dtrace_helper_providers_max;
16003179193Sjb
16004179193Sjb		ASSERT(tmp_maxprovs < help->dthps_maxprovs);
16005179193Sjb
16006179193Sjb		help->dthps_provs = kmem_zalloc(help->dthps_maxprovs *
16007179193Sjb		    sizeof (dtrace_helper_provider_t *), KM_SLEEP);
16008179193Sjb
16009179193Sjb		if (tmp_provs != NULL) {
16010179193Sjb			bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs *
16011179193Sjb			    sizeof (dtrace_helper_provider_t *));
16012179193Sjb			kmem_free(tmp_provs, tmp_maxprovs *
16013179193Sjb			    sizeof (dtrace_helper_provider_t *));
16014179193Sjb		}
16015179193Sjb	}
16016179193Sjb
16017179193Sjb	help->dthps_provs[help->dthps_nprovs] = hprov;
16018179193Sjb	help->dthps_nprovs++;
16019179193Sjb
16020179193Sjb	return (0);
16021179193Sjb}
16022179193Sjb
16023179193Sjbstatic void
16024179193Sjbdtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov)
16025179193Sjb{
16026179193Sjb	mutex_enter(&dtrace_lock);
16027179193Sjb
16028179193Sjb	if (--hprov->dthp_ref == 0) {
16029179193Sjb		dof_hdr_t *dof;
16030179193Sjb		mutex_exit(&dtrace_lock);
16031179193Sjb		dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof;
16032179193Sjb		dtrace_dof_destroy(dof);
16033179193Sjb		kmem_free(hprov, sizeof (dtrace_helper_provider_t));
16034179193Sjb	} else {
16035179193Sjb		mutex_exit(&dtrace_lock);
16036179193Sjb	}
16037179193Sjb}
16038179193Sjb
16039179193Sjbstatic int
16040179193Sjbdtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec)
16041179193Sjb{
16042179193Sjb	uintptr_t daddr = (uintptr_t)dof;
16043179193Sjb	dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
16044179193Sjb	dof_provider_t *provider;
16045179193Sjb	dof_probe_t *probe;
16046179193Sjb	uint8_t *arg;
16047179193Sjb	char *strtab, *typestr;
16048179193Sjb	dof_stridx_t typeidx;
16049179193Sjb	size_t typesz;
16050179193Sjb	uint_t nprobes, j, k;
16051179193Sjb
16052179193Sjb	ASSERT(sec->dofs_type == DOF_SECT_PROVIDER);
16053179193Sjb
16054179193Sjb	if (sec->dofs_offset & (sizeof (uint_t) - 1)) {
16055179193Sjb		dtrace_dof_error(dof, "misaligned section offset");
16056179193Sjb		return (-1);
16057179193Sjb	}
16058179193Sjb
16059179193Sjb	/*
16060179193Sjb	 * The section needs to be large enough to contain the DOF provider
16061179193Sjb	 * structure appropriate for the given version.
16062179193Sjb	 */
16063179193Sjb	if (sec->dofs_size <
16064179193Sjb	    ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ?
16065179193Sjb	    offsetof(dof_provider_t, dofpv_prenoffs) :
16066179193Sjb	    sizeof (dof_provider_t))) {
16067179193Sjb		dtrace_dof_error(dof, "provider section too small");
16068179193Sjb		return (-1);
16069179193Sjb	}
16070179193Sjb
16071179193Sjb	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
16072179193Sjb	str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab);
16073179193Sjb	prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes);
16074179193Sjb	arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs);
16075179193Sjb	off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs);
16076179193Sjb
16077179193Sjb	if (str_sec == NULL || prb_sec == NULL ||
16078179193Sjb	    arg_sec == NULL || off_sec == NULL)
16079179193Sjb		return (-1);
16080179193Sjb
16081179193Sjb	enoff_sec = NULL;
16082179193Sjb
16083179193Sjb	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
16084179193Sjb	    provider->dofpv_prenoffs != DOF_SECT_NONE &&
16085179193Sjb	    (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS,
16086179193Sjb	    provider->dofpv_prenoffs)) == NULL)
16087179193Sjb		return (-1);
16088179193Sjb
16089179193Sjb	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
16090179193Sjb
16091179193Sjb	if (provider->dofpv_name >= str_sec->dofs_size ||
16092179193Sjb	    strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) {
16093179193Sjb		dtrace_dof_error(dof, "invalid provider name");
16094179193Sjb		return (-1);
16095179193Sjb	}
16096179193Sjb
16097179193Sjb	if (prb_sec->dofs_entsize == 0 ||
16098179193Sjb	    prb_sec->dofs_entsize > prb_sec->dofs_size) {
16099179193Sjb		dtrace_dof_error(dof, "invalid entry size");
16100179193Sjb		return (-1);
16101179193Sjb	}
16102179193Sjb
16103179193Sjb	if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) {
16104179193Sjb		dtrace_dof_error(dof, "misaligned entry size");
16105179193Sjb		return (-1);
16106179193Sjb	}
16107179193Sjb
16108179193Sjb	if (off_sec->dofs_entsize != sizeof (uint32_t)) {
16109179193Sjb		dtrace_dof_error(dof, "invalid entry size");
16110179193Sjb		return (-1);
16111179193Sjb	}
16112179193Sjb
16113179193Sjb	if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) {
16114179193Sjb		dtrace_dof_error(dof, "misaligned section offset");
16115179193Sjb		return (-1);
16116179193Sjb	}
16117179193Sjb
16118179193Sjb	if (arg_sec->dofs_entsize != sizeof (uint8_t)) {
16119179193Sjb		dtrace_dof_error(dof, "invalid entry size");
16120179193Sjb		return (-1);
16121179193Sjb	}
16122179193Sjb
16123179193Sjb	arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
16124179193Sjb
16125179193Sjb	nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
16126179193Sjb
16127179193Sjb	/*
16128179193Sjb	 * Take a pass through the probes to check for errors.
16129179193Sjb	 */
16130179193Sjb	for (j = 0; j < nprobes; j++) {
16131179193Sjb		probe = (dof_probe_t *)(uintptr_t)(daddr +
16132179193Sjb		    prb_sec->dofs_offset + j * prb_sec->dofs_entsize);
16133179193Sjb
16134179193Sjb		if (probe->dofpr_func >= str_sec->dofs_size) {
16135179193Sjb			dtrace_dof_error(dof, "invalid function name");
16136179193Sjb			return (-1);
16137179193Sjb		}
16138179193Sjb
16139179193Sjb		if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) {
16140179193Sjb			dtrace_dof_error(dof, "function name too long");
16141298589Smarkj			/*
16142298589Smarkj			 * Keep going if the function name is too long.
16143298589Smarkj			 * Unlike provider and probe names, we cannot reasonably
16144298589Smarkj			 * impose restrictions on function names, since they're
16145298589Smarkj			 * a property of the code being instrumented. We will
16146298589Smarkj			 * skip this probe in dtrace_helper_provide_one().
16147298589Smarkj			 */
16148179193Sjb		}
16149179193Sjb
16150179193Sjb		if (probe->dofpr_name >= str_sec->dofs_size ||
16151179193Sjb		    strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) {
16152179193Sjb			dtrace_dof_error(dof, "invalid probe name");
16153179193Sjb			return (-1);
16154179193Sjb		}
16155179193Sjb
16156179193Sjb		/*
16157179193Sjb		 * The offset count must not wrap the index, and the offsets
16158179193Sjb		 * must also not overflow the section's data.
16159179193Sjb		 */
16160179193Sjb		if (probe->dofpr_offidx + probe->dofpr_noffs <
16161179193Sjb		    probe->dofpr_offidx ||
16162179193Sjb		    (probe->dofpr_offidx + probe->dofpr_noffs) *
16163179193Sjb		    off_sec->dofs_entsize > off_sec->dofs_size) {
16164179193Sjb			dtrace_dof_error(dof, "invalid probe offset");
16165179193Sjb			return (-1);
16166179193Sjb		}
16167179193Sjb
16168179193Sjb		if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) {
16169179193Sjb			/*
16170179193Sjb			 * If there's no is-enabled offset section, make sure
16171179193Sjb			 * there aren't any is-enabled offsets. Otherwise
16172179193Sjb			 * perform the same checks as for probe offsets
16173179193Sjb			 * (immediately above).
16174179193Sjb			 */
16175179193Sjb			if (enoff_sec == NULL) {
16176179193Sjb				if (probe->dofpr_enoffidx != 0 ||
16177179193Sjb				    probe->dofpr_nenoffs != 0) {
16178179193Sjb					dtrace_dof_error(dof, "is-enabled "
16179179193Sjb					    "offsets with null section");
16180179193Sjb					return (-1);
16181179193Sjb				}
16182179193Sjb			} else if (probe->dofpr_enoffidx +
16183179193Sjb			    probe->dofpr_nenoffs < probe->dofpr_enoffidx ||
16184179193Sjb			    (probe->dofpr_enoffidx + probe->dofpr_nenoffs) *
16185179193Sjb			    enoff_sec->dofs_entsize > enoff_sec->dofs_size) {
16186179193Sjb				dtrace_dof_error(dof, "invalid is-enabled "
16187179193Sjb				    "offset");
16188179193Sjb				return (-1);
16189179193Sjb			}
16190179193Sjb
16191179193Sjb			if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) {
16192179193Sjb				dtrace_dof_error(dof, "zero probe and "
16193179193Sjb				    "is-enabled offsets");
16194179193Sjb				return (-1);
16195179193Sjb			}
16196179193Sjb		} else if (probe->dofpr_noffs == 0) {
16197179193Sjb			dtrace_dof_error(dof, "zero probe offsets");
16198179193Sjb			return (-1);
16199179193Sjb		}
16200179193Sjb
16201179193Sjb		if (probe->dofpr_argidx + probe->dofpr_xargc <
16202179193Sjb		    probe->dofpr_argidx ||
16203179193Sjb		    (probe->dofpr_argidx + probe->dofpr_xargc) *
16204179193Sjb		    arg_sec->dofs_entsize > arg_sec->dofs_size) {
16205179193Sjb			dtrace_dof_error(dof, "invalid args");
16206179193Sjb			return (-1);
16207179193Sjb		}
16208179193Sjb
16209179193Sjb		typeidx = probe->dofpr_nargv;
16210179193Sjb		typestr = strtab + probe->dofpr_nargv;
16211179193Sjb		for (k = 0; k < probe->dofpr_nargc; k++) {
16212179193Sjb			if (typeidx >= str_sec->dofs_size) {
16213179193Sjb				dtrace_dof_error(dof, "bad "
16214179193Sjb				    "native argument type");
16215179193Sjb				return (-1);
16216179193Sjb			}
16217179193Sjb
16218179193Sjb			typesz = strlen(typestr) + 1;
16219179193Sjb			if (typesz > DTRACE_ARGTYPELEN) {
16220179193Sjb				dtrace_dof_error(dof, "native "
16221179193Sjb				    "argument type too long");
16222179193Sjb				return (-1);
16223179193Sjb			}
16224179193Sjb			typeidx += typesz;
16225179193Sjb			typestr += typesz;
16226179193Sjb		}
16227179193Sjb
16228179193Sjb		typeidx = probe->dofpr_xargv;
16229179193Sjb		typestr = strtab + probe->dofpr_xargv;
16230179193Sjb		for (k = 0; k < probe->dofpr_xargc; k++) {
16231179193Sjb			if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) {
16232179193Sjb				dtrace_dof_error(dof, "bad "
16233179193Sjb				    "native argument index");
16234179193Sjb				return (-1);
16235179193Sjb			}
16236179193Sjb
16237179193Sjb			if (typeidx >= str_sec->dofs_size) {
16238179193Sjb				dtrace_dof_error(dof, "bad "
16239179193Sjb				    "translated argument type");
16240179193Sjb				return (-1);
16241179193Sjb			}
16242179193Sjb
16243179193Sjb			typesz = strlen(typestr) + 1;
16244179193Sjb			if (typesz > DTRACE_ARGTYPELEN) {
16245179193Sjb				dtrace_dof_error(dof, "translated argument "
16246179193Sjb				    "type too long");
16247179193Sjb				return (-1);
16248179193Sjb			}
16249179193Sjb
16250179193Sjb			typeidx += typesz;
16251179193Sjb			typestr += typesz;
16252179193Sjb		}
16253179193Sjb	}
16254179193Sjb
16255179193Sjb	return (0);
16256179193Sjb}
16257179193Sjb
16258179193Sjbstatic int
16259291962Smarkjdtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp, struct proc *p)
16260179193Sjb{
16261179193Sjb	dtrace_helpers_t *help;
16262179193Sjb	dtrace_vstate_t *vstate;
16263179193Sjb	dtrace_enabling_t *enab = NULL;
16264179193Sjb	int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1;
16265179193Sjb	uintptr_t daddr = (uintptr_t)dof;
16266179193Sjb
16267179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
16268179193Sjb
16269281257Smarkj	if ((help = p->p_dtrace_helpers) == NULL)
16270281257Smarkj		help = dtrace_helpers_create(p);
16271281257Smarkj
16272179193Sjb	vstate = &help->dthps_vstate;
16273179193Sjb
16274361088Sdim	if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, dhp->dofhp_addr,
16275361088Sdim	    dhp->dofhp_dof, B_FALSE)) != 0) {
16276179193Sjb		dtrace_dof_destroy(dof);
16277179193Sjb		return (rv);
16278179193Sjb	}
16279179193Sjb
16280179193Sjb	/*
16281179193Sjb	 * Look for helper providers and validate their descriptions.
16282179193Sjb	 */
16283361088Sdim	for (i = 0; i < dof->dofh_secnum; i++) {
16284361088Sdim		dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
16285361088Sdim		    dof->dofh_secoff + i * dof->dofh_secsize);
16286179193Sjb
16287361088Sdim		if (sec->dofs_type != DOF_SECT_PROVIDER)
16288361088Sdim			continue;
16289179193Sjb
16290361088Sdim		if (dtrace_helper_provider_validate(dof, sec) != 0) {
16291361088Sdim			dtrace_enabling_destroy(enab);
16292361088Sdim			dtrace_dof_destroy(dof);
16293361088Sdim			return (-1);
16294361088Sdim		}
16295179193Sjb
16296361088Sdim		nprovs++;
16297179193Sjb	}
16298179193Sjb
16299179193Sjb	/*
16300179193Sjb	 * Now we need to walk through the ECB descriptions in the enabling.
16301179193Sjb	 */
16302179193Sjb	for (i = 0; i < enab->dten_ndesc; i++) {
16303179193Sjb		dtrace_ecbdesc_t *ep = enab->dten_desc[i];
16304179193Sjb		dtrace_probedesc_t *desc = &ep->dted_probe;
16305179193Sjb
16306179193Sjb		if (strcmp(desc->dtpd_provider, "dtrace") != 0)
16307179193Sjb			continue;
16308179193Sjb
16309179193Sjb		if (strcmp(desc->dtpd_mod, "helper") != 0)
16310179193Sjb			continue;
16311179193Sjb
16312179193Sjb		if (strcmp(desc->dtpd_func, "ustack") != 0)
16313179193Sjb			continue;
16314179193Sjb
16315179193Sjb		if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK,
16316281257Smarkj		    ep, help)) != 0) {
16317179193Sjb			/*
16318179193Sjb			 * Adding this helper action failed -- we are now going
16319179193Sjb			 * to rip out the entire generation and return failure.
16320179193Sjb			 */
16321281257Smarkj			(void) dtrace_helper_destroygen(help,
16322281257Smarkj			    help->dthps_generation);
16323179193Sjb			dtrace_enabling_destroy(enab);
16324179193Sjb			dtrace_dof_destroy(dof);
16325179193Sjb			return (-1);
16326179193Sjb		}
16327179193Sjb
16328179193Sjb		nhelpers++;
16329179193Sjb	}
16330179193Sjb
16331179193Sjb	if (nhelpers < enab->dten_ndesc)
16332179193Sjb		dtrace_dof_error(dof, "unmatched helpers");
16333179193Sjb
16334179193Sjb	gen = help->dthps_generation++;
16335179193Sjb	dtrace_enabling_destroy(enab);
16336179193Sjb
16337361088Sdim	if (nprovs > 0) {
16338291963Smarkj		/*
16339291963Smarkj		 * Now that this is in-kernel, we change the sense of the
16340291963Smarkj		 * members:  dofhp_dof denotes the in-kernel copy of the DOF
16341291963Smarkj		 * and dofhp_addr denotes the address at user-level.
16342291963Smarkj		 */
16343291963Smarkj		dhp->dofhp_addr = dhp->dofhp_dof;
16344179193Sjb		dhp->dofhp_dof = (uint64_t)(uintptr_t)dof;
16345291963Smarkj
16346281257Smarkj		if (dtrace_helper_provider_add(dhp, help, gen) == 0) {
16347179193Sjb			mutex_exit(&dtrace_lock);
16348281257Smarkj			dtrace_helper_provider_register(p, help, dhp);
16349179193Sjb			mutex_enter(&dtrace_lock);
16350179193Sjb
16351179193Sjb			destroy = 0;
16352179193Sjb		}
16353179193Sjb	}
16354179193Sjb
16355179193Sjb	if (destroy)
16356179193Sjb		dtrace_dof_destroy(dof);
16357179193Sjb
16358179193Sjb	return (gen);
16359179193Sjb}
16360179193Sjb
16361179193Sjbstatic dtrace_helpers_t *
16362179193Sjbdtrace_helpers_create(proc_t *p)
16363179193Sjb{
16364179193Sjb	dtrace_helpers_t *help;
16365179193Sjb
16366179193Sjb	ASSERT(MUTEX_HELD(&dtrace_lock));
16367179193Sjb	ASSERT(p->p_dtrace_helpers == NULL);
16368179193Sjb
16369179193Sjb	help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP);
16370179193Sjb	help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) *
16371179193Sjb	    DTRACE_NHELPER_ACTIONS, KM_SLEEP);
16372179193Sjb
16373179193Sjb	p->p_dtrace_helpers = help;
16374179193Sjb	dtrace_helpers++;
16375179193Sjb
16376179193Sjb	return (help);
16377179193Sjb}
16378179193Sjb
16379277300Ssmh#ifdef illumos
16380212357Srpaulostatic
16381212357Srpaulo#endif
16382212357Srpaulovoid
16383212357Srpaulodtrace_helpers_destroy(proc_t *p)
16384179193Sjb{
16385179193Sjb	dtrace_helpers_t *help;
16386179193Sjb	dtrace_vstate_t *vstate;
16387277300Ssmh#ifdef illumos
16388179193Sjb	proc_t *p = curproc;
16389212357Srpaulo#endif
16390179193Sjb	int i;
16391179193Sjb
16392179193Sjb	mutex_enter(&dtrace_lock);
16393179193Sjb
16394179193Sjb	ASSERT(p->p_dtrace_helpers != NULL);
16395179193Sjb	ASSERT(dtrace_helpers > 0);
16396179193Sjb
16397179193Sjb	help = p->p_dtrace_helpers;
16398179193Sjb	vstate = &help->dthps_vstate;
16399179193Sjb
16400179193Sjb	/*
16401179193Sjb	 * We're now going to lose the help from this process.
16402179193Sjb	 */
16403179193Sjb	p->p_dtrace_helpers = NULL;
16404179193Sjb	dtrace_sync();
16405179193Sjb
16406179193Sjb	/*
16407179193Sjb	 * Destory the helper actions.
16408179193Sjb	 */
16409179193Sjb	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
16410179193Sjb		dtrace_helper_action_t *h, *next;
16411179193Sjb
16412179193Sjb		for (h = help->dthps_actions[i]; h != NULL; h = next) {
16413179193Sjb			next = h->dtha_next;
16414179193Sjb			dtrace_helper_action_destroy(h, vstate);
16415179193Sjb			h = next;
16416179193Sjb		}
16417179193Sjb	}
16418179193Sjb
16419179193Sjb	mutex_exit(&dtrace_lock);
16420179193Sjb
16421179193Sjb	/*
16422179193Sjb	 * Destroy the helper providers.
16423179193Sjb	 */
16424179193Sjb	if (help->dthps_maxprovs > 0) {
16425179193Sjb		mutex_enter(&dtrace_meta_lock);
16426179193Sjb		if (dtrace_meta_pid != NULL) {
16427179193Sjb			ASSERT(dtrace_deferred_pid == NULL);
16428179193Sjb
16429179193Sjb			for (i = 0; i < help->dthps_nprovs; i++) {
16430179193Sjb				dtrace_helper_provider_remove(
16431179193Sjb				    &help->dthps_provs[i]->dthp_prov, p->p_pid);
16432179193Sjb			}
16433179193Sjb		} else {
16434179193Sjb			mutex_enter(&dtrace_lock);
16435179193Sjb			ASSERT(help->dthps_deferred == 0 ||
16436179193Sjb			    help->dthps_next != NULL ||
16437179193Sjb			    help->dthps_prev != NULL ||
16438179193Sjb			    help == dtrace_deferred_pid);
16439179193Sjb
16440179193Sjb			/*
16441179193Sjb			 * Remove the helper from the deferred list.
16442179193Sjb			 */
16443179193Sjb			if (help->dthps_next != NULL)
16444179193Sjb				help->dthps_next->dthps_prev = help->dthps_prev;
16445179193Sjb			if (help->dthps_prev != NULL)
16446179193Sjb				help->dthps_prev->dthps_next = help->dthps_next;
16447179193Sjb			if (dtrace_deferred_pid == help) {
16448179193Sjb				dtrace_deferred_pid = help->dthps_next;
16449179193Sjb				ASSERT(help->dthps_prev == NULL);
16450179193Sjb			}
16451179193Sjb
16452179193Sjb			mutex_exit(&dtrace_lock);
16453179193Sjb		}
16454179193Sjb
16455179193Sjb		mutex_exit(&dtrace_meta_lock);
16456179193Sjb
16457179193Sjb		for (i = 0; i < help->dthps_nprovs; i++) {
16458179193Sjb			dtrace_helper_provider_destroy(help->dthps_provs[i]);
16459179193Sjb		}
16460179193Sjb
16461179193Sjb		kmem_free(help->dthps_provs, help->dthps_maxprovs *
16462179193Sjb		    sizeof (dtrace_helper_provider_t *));
16463179193Sjb	}
16464179193Sjb
16465179193Sjb	mutex_enter(&dtrace_lock);
16466179193Sjb
16467179193Sjb	dtrace_vstate_fini(&help->dthps_vstate);
16468179193Sjb	kmem_free(help->dthps_actions,
16469179193Sjb	    sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS);
16470179193Sjb	kmem_free(help, sizeof (dtrace_helpers_t));
16471179193Sjb
16472179193Sjb	--dtrace_helpers;
16473179193Sjb	mutex_exit(&dtrace_lock);
16474179193Sjb}
16475179193Sjb
16476277300Ssmh#ifdef illumos
16477212357Srpaulostatic
16478212357Srpaulo#endif
16479212357Srpaulovoid
16480179193Sjbdtrace_helpers_duplicate(proc_t *from, proc_t *to)
16481179193Sjb{
16482179193Sjb	dtrace_helpers_t *help, *newhelp;
16483179193Sjb	dtrace_helper_action_t *helper, *new, *last;
16484179193Sjb	dtrace_difo_t *dp;
16485179193Sjb	dtrace_vstate_t *vstate;
16486179193Sjb	int i, j, sz, hasprovs = 0;
16487179193Sjb
16488179193Sjb	mutex_enter(&dtrace_lock);
16489179193Sjb	ASSERT(from->p_dtrace_helpers != NULL);
16490179193Sjb	ASSERT(dtrace_helpers > 0);
16491179193Sjb
16492179193Sjb	help = from->p_dtrace_helpers;
16493179193Sjb	newhelp = dtrace_helpers_create(to);
16494179193Sjb	ASSERT(to->p_dtrace_helpers != NULL);
16495179193Sjb
16496179193Sjb	newhelp->dthps_generation = help->dthps_generation;
16497179193Sjb	vstate = &newhelp->dthps_vstate;
16498179193Sjb
16499179193Sjb	/*
16500179193Sjb	 * Duplicate the helper actions.
16501179193Sjb	 */
16502179193Sjb	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
16503179193Sjb		if ((helper = help->dthps_actions[i]) == NULL)
16504179193Sjb			continue;
16505179193Sjb
16506179193Sjb		for (last = NULL; helper != NULL; helper = helper->dtha_next) {
16507179193Sjb			new = kmem_zalloc(sizeof (dtrace_helper_action_t),
16508179193Sjb			    KM_SLEEP);
16509179193Sjb			new->dtha_generation = helper->dtha_generation;
16510179193Sjb
16511179193Sjb			if ((dp = helper->dtha_predicate) != NULL) {
16512179193Sjb				dp = dtrace_difo_duplicate(dp, vstate);
16513179193Sjb				new->dtha_predicate = dp;
16514179193Sjb			}
16515179193Sjb
16516179193Sjb			new->dtha_nactions = helper->dtha_nactions;
16517179193Sjb			sz = sizeof (dtrace_difo_t *) * new->dtha_nactions;
16518179193Sjb			new->dtha_actions = kmem_alloc(sz, KM_SLEEP);
16519179193Sjb
16520179193Sjb			for (j = 0; j < new->dtha_nactions; j++) {
16521179193Sjb				dtrace_difo_t *dp = helper->dtha_actions[j];
16522179193Sjb
16523179193Sjb				ASSERT(dp != NULL);
16524179193Sjb				dp = dtrace_difo_duplicate(dp, vstate);
16525179193Sjb				new->dtha_actions[j] = dp;
16526179193Sjb			}
16527179193Sjb
16528179193Sjb			if (last != NULL) {
16529179193Sjb				last->dtha_next = new;
16530179193Sjb			} else {
16531179193Sjb				newhelp->dthps_actions[i] = new;
16532179193Sjb			}
16533179193Sjb
16534179193Sjb			last = new;
16535179193Sjb		}
16536179193Sjb	}
16537179193Sjb
16538179193Sjb	/*
16539179193Sjb	 * Duplicate the helper providers and register them with the
16540179193Sjb	 * DTrace framework.
16541179193Sjb	 */
16542179193Sjb	if (help->dthps_nprovs > 0) {
16543179193Sjb		newhelp->dthps_nprovs = help->dthps_nprovs;
16544179193Sjb		newhelp->dthps_maxprovs = help->dthps_nprovs;
16545179193Sjb		newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs *
16546179193Sjb		    sizeof (dtrace_helper_provider_t *), KM_SLEEP);
16547179193Sjb		for (i = 0; i < newhelp->dthps_nprovs; i++) {
16548179193Sjb			newhelp->dthps_provs[i] = help->dthps_provs[i];
16549179193Sjb			newhelp->dthps_provs[i]->dthp_ref++;
16550179193Sjb		}
16551179193Sjb
16552179193Sjb		hasprovs = 1;
16553179193Sjb	}
16554179193Sjb
16555179193Sjb	mutex_exit(&dtrace_lock);
16556179193Sjb
16557179193Sjb	if (hasprovs)
16558179193Sjb		dtrace_helper_provider_register(to, newhelp, NULL);
16559179193Sjb}
16560179193Sjb
16561179193Sjb/*
16562179193Sjb * DTrace Hook Functions
16563179193Sjb */
16564179193Sjbstatic void
16565179198Sjbdtrace_module_loaded(modctl_t *ctl)
16566179193Sjb{
16567179193Sjb	dtrace_provider_t *prv;
16568179193Sjb
16569179193Sjb	mutex_enter(&dtrace_provider_lock);
16570277300Ssmh#ifdef illumos
16571179193Sjb	mutex_enter(&mod_lock);
16572252850Smarkj#endif
16573179193Sjb
16574277300Ssmh#ifdef illumos
16575179193Sjb	ASSERT(ctl->mod_busy);
16576254268Smarkj#endif
16577179193Sjb
16578179193Sjb	/*
16579179193Sjb	 * We're going to call each providers per-module provide operation
16580179193Sjb	 * specifying only this module.
16581179193Sjb	 */
16582179193Sjb	for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next)
16583179193Sjb		prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
16584179193Sjb
16585277300Ssmh#ifdef illumos
16586179193Sjb	mutex_exit(&mod_lock);
16587252850Smarkj#endif
16588179193Sjb	mutex_exit(&dtrace_provider_lock);
16589179193Sjb
16590179193Sjb	/*
16591179193Sjb	 * If we have any retained enablings, we need to match against them.
16592179193Sjb	 * Enabling probes requires that cpu_lock be held, and we cannot hold
16593179193Sjb	 * cpu_lock here -- it is legal for cpu_lock to be held when loading a
16594179193Sjb	 * module.  (In particular, this happens when loading scheduling
16595179193Sjb	 * classes.)  So if we have any retained enablings, we need to dispatch
16596179193Sjb	 * our task queue to do the match for us.
16597179193Sjb	 */
16598179193Sjb	mutex_enter(&dtrace_lock);
16599179193Sjb
16600179193Sjb	if (dtrace_retained == NULL) {
16601179193Sjb		mutex_exit(&dtrace_lock);
16602179193Sjb		return;
16603179193Sjb	}
16604179193Sjb
16605179193Sjb	(void) taskq_dispatch(dtrace_taskq,
16606179193Sjb	    (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP);
16607179193Sjb
16608179193Sjb	mutex_exit(&dtrace_lock);
16609179193Sjb
16610179193Sjb	/*
16611179193Sjb	 * And now, for a little heuristic sleaze:  in general, we want to
16612179193Sjb	 * match modules as soon as they load.  However, we cannot guarantee
16613179193Sjb	 * this, because it would lead us to the lock ordering violation
16614179193Sjb	 * outlined above.  The common case, of course, is that cpu_lock is
16615179193Sjb	 * _not_ held -- so we delay here for a clock tick, hoping that that's
16616179193Sjb	 * long enough for the task queue to do its work.  If it's not, it's
16617179193Sjb	 * not a serious problem -- it just means that the module that we
16618179193Sjb	 * just loaded may not be immediately instrumentable.
16619179193Sjb	 */
16620179193Sjb	delay(1);
16621179193Sjb}
16622179193Sjb
16623179193Sjbstatic void
16624277300Ssmh#ifdef illumos
16625179198Sjbdtrace_module_unloaded(modctl_t *ctl)
16626254268Smarkj#else
16627254268Smarkjdtrace_module_unloaded(modctl_t *ctl, int *error)
16628254268Smarkj#endif
16629179193Sjb{
16630179193Sjb	dtrace_probe_t template, *probe, *first, *next;
16631179193Sjb	dtrace_provider_t *prov;
16632277300Ssmh#ifndef illumos
16633254268Smarkj	char modname[DTRACE_MODNAMELEN];
16634254268Smarkj	size_t len;
16635254268Smarkj#endif
16636179193Sjb
16637277300Ssmh#ifdef illumos
16638179193Sjb	template.dtpr_mod = ctl->mod_modname;
16639254268Smarkj#else
16640254268Smarkj	/* Handle the fact that ctl->filename may end in ".ko". */
16641254268Smarkj	strlcpy(modname, ctl->filename, sizeof(modname));
16642254268Smarkj	len = strlen(ctl->filename);
16643254268Smarkj	if (len > 3 && strcmp(modname + len - 3, ".ko") == 0)
16644254268Smarkj		modname[len - 3] = '\0';
16645254268Smarkj	template.dtpr_mod = modname;
16646254268Smarkj#endif
16647179193Sjb
16648179193Sjb	mutex_enter(&dtrace_provider_lock);
16649277300Ssmh#ifdef illumos
16650179193Sjb	mutex_enter(&mod_lock);
16651252850Smarkj#endif
16652179193Sjb	mutex_enter(&dtrace_lock);
16653179193Sjb
16654277300Ssmh#ifndef illumos
16655254268Smarkj	if (ctl->nenabled > 0) {
16656254268Smarkj		/* Don't allow unloads if a probe is enabled. */
16657254268Smarkj		mutex_exit(&dtrace_provider_lock);
16658254268Smarkj		mutex_exit(&dtrace_lock);
16659254268Smarkj		*error = -1;
16660254268Smarkj		printf(
16661254268Smarkj	"kldunload: attempt to unload module that has DTrace probes enabled\n");
16662254268Smarkj		return;
16663254268Smarkj	}
16664254268Smarkj#endif
16665254268Smarkj
16666179193Sjb	if (dtrace_bymod == NULL) {
16667179193Sjb		/*
16668179193Sjb		 * The DTrace module is loaded (obviously) but not attached;
16669179193Sjb		 * we don't have any work to do.
16670179193Sjb		 */
16671179193Sjb		mutex_exit(&dtrace_provider_lock);
16672277300Ssmh#ifdef illumos
16673179193Sjb		mutex_exit(&mod_lock);
16674252850Smarkj#endif
16675179193Sjb		mutex_exit(&dtrace_lock);
16676179193Sjb		return;
16677179193Sjb	}
16678179193Sjb
16679179193Sjb	for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template);
16680179193Sjb	    probe != NULL; probe = probe->dtpr_nextmod) {
16681179193Sjb		if (probe->dtpr_ecb != NULL) {
16682179193Sjb			mutex_exit(&dtrace_provider_lock);
16683277300Ssmh#ifdef illumos
16684179193Sjb			mutex_exit(&mod_lock);
16685252850Smarkj#endif
16686179193Sjb			mutex_exit(&dtrace_lock);
16687179193Sjb
16688179193Sjb			/*
16689179193Sjb			 * This shouldn't _actually_ be possible -- we're
16690179193Sjb			 * unloading a module that has an enabled probe in it.
16691179193Sjb			 * (It's normally up to the provider to make sure that
16692179193Sjb			 * this can't happen.)  However, because dtps_enable()
16693179193Sjb			 * doesn't have a failure mode, there can be an
16694179193Sjb			 * enable/unload race.  Upshot:  we don't want to
16695179193Sjb			 * assert, but we're not going to disable the
16696179193Sjb			 * probe, either.
16697179193Sjb			 */
16698179193Sjb			if (dtrace_err_verbose) {
16699277300Ssmh#ifdef illumos
16700179193Sjb				cmn_err(CE_WARN, "unloaded module '%s' had "
16701179193Sjb				    "enabled probes", ctl->mod_modname);
16702254268Smarkj#else
16703254268Smarkj				cmn_err(CE_WARN, "unloaded module '%s' had "
16704254268Smarkj				    "enabled probes", modname);
16705254268Smarkj#endif
16706179193Sjb			}
16707179193Sjb
16708179193Sjb			return;
16709179193Sjb		}
16710179193Sjb	}
16711179193Sjb
16712179193Sjb	probe = first;
16713179193Sjb
16714179193Sjb	for (first = NULL; probe != NULL; probe = next) {
16715179193Sjb		ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe);
16716179193Sjb
16717179193Sjb		dtrace_probes[probe->dtpr_id - 1] = NULL;
16718179193Sjb
16719179193Sjb		next = probe->dtpr_nextmod;
16720179193Sjb		dtrace_hash_remove(dtrace_bymod, probe);
16721179193Sjb		dtrace_hash_remove(dtrace_byfunc, probe);
16722179193Sjb		dtrace_hash_remove(dtrace_byname, probe);
16723179193Sjb
16724179193Sjb		if (first == NULL) {
16725179193Sjb			first = probe;
16726179193Sjb			probe->dtpr_nextmod = NULL;
16727179193Sjb		} else {
16728179193Sjb			probe->dtpr_nextmod = first;
16729179193Sjb			first = probe;
16730179193Sjb		}
16731179193Sjb	}
16732179193Sjb
16733179193Sjb	/*
16734179193Sjb	 * We've removed all of the module's probes from the hash chains and
16735179193Sjb	 * from the probe array.  Now issue a dtrace_sync() to be sure that
16736179193Sjb	 * everyone has cleared out from any probe array processing.
16737179193Sjb	 */
16738179193Sjb	dtrace_sync();
16739179193Sjb
16740179193Sjb	for (probe = first; probe != NULL; probe = first) {
16741179193Sjb		first = probe->dtpr_nextmod;
16742179193Sjb		prov = probe->dtpr_provider;
16743179193Sjb		prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id,
16744179193Sjb		    probe->dtpr_arg);
16745179193Sjb		kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
16746179193Sjb		kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
16747179193Sjb		kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
16748277300Ssmh#ifdef illumos
16749179193Sjb		vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1);
16750254268Smarkj#else
16751260131Smarkj		free_unr(dtrace_arena, probe->dtpr_id);
16752254268Smarkj#endif
16753179193Sjb		kmem_free(probe, sizeof (dtrace_probe_t));
16754179193Sjb	}
16755179193Sjb
16756179193Sjb	mutex_exit(&dtrace_lock);
16757277300Ssmh#ifdef illumos
16758179193Sjb	mutex_exit(&mod_lock);
16759252850Smarkj#endif
16760179193Sjb	mutex_exit(&dtrace_provider_lock);
16761179193Sjb}
16762179193Sjb
16763277300Ssmh#ifndef illumos
16764179198Sjbstatic void
16765254309Smarkjdtrace_kld_load(void *arg __unused, linker_file_t lf)
16766254268Smarkj{
16767254268Smarkj
16768254268Smarkj	dtrace_module_loaded(lf);
16769254268Smarkj}
16770254268Smarkj
16771254268Smarkjstatic void
16772254813Smarkjdtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error)
16773254268Smarkj{
16774254268Smarkj
16775254268Smarkj	if (*error != 0)
16776254268Smarkj		/* We already have an error, so don't do anything. */
16777254268Smarkj		return;
16778254268Smarkj	dtrace_module_unloaded(lf, error);
16779254268Smarkj}
16780254268Smarkj#endif
16781254268Smarkj
16782277300Ssmh#ifdef illumos
16783254268Smarkjstatic void
16784179193Sjbdtrace_suspend(void)
16785179193Sjb{
16786179193Sjb	dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend));
16787179193Sjb}
16788179193Sjb
16789179198Sjbstatic void
16790179193Sjbdtrace_resume(void)
16791179193Sjb{
16792179193Sjb	dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume));
16793179193Sjb}
16794179198Sjb#endif
16795179193Sjb
16796179193Sjbstatic int
16797179193Sjbdtrace_cpu_setup(cpu_setup_t what, processorid_t cpu)
16798179193Sjb{
16799179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
16800179193Sjb	mutex_enter(&dtrace_lock);
16801179193Sjb
16802179193Sjb	switch (what) {
16803179193Sjb	case CPU_CONFIG: {
16804179193Sjb		dtrace_state_t *state;
16805179193Sjb		dtrace_optval_t *opt, rs, c;
16806179193Sjb
16807179193Sjb		/*
16808179193Sjb		 * For now, we only allocate a new buffer for anonymous state.
16809179193Sjb		 */
16810179193Sjb		if ((state = dtrace_anon.dta_state) == NULL)
16811179193Sjb			break;
16812179193Sjb
16813179193Sjb		if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
16814179193Sjb			break;
16815179193Sjb
16816179193Sjb		opt = state->dts_options;
16817179193Sjb		c = opt[DTRACEOPT_CPU];
16818179193Sjb
16819179193Sjb		if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu)
16820179193Sjb			break;
16821179193Sjb
16822179193Sjb		/*
16823179193Sjb		 * Regardless of what the actual policy is, we're going to
16824179193Sjb		 * temporarily set our resize policy to be manual.  We're
16825179193Sjb		 * also going to temporarily set our CPU option to denote
16826179193Sjb		 * the newly configured CPU.
16827179193Sjb		 */
16828179193Sjb		rs = opt[DTRACEOPT_BUFRESIZE];
16829179193Sjb		opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL;
16830179193Sjb		opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu;
16831179193Sjb
16832179193Sjb		(void) dtrace_state_buffers(state);
16833179193Sjb
16834179193Sjb		opt[DTRACEOPT_BUFRESIZE] = rs;
16835179193Sjb		opt[DTRACEOPT_CPU] = c;
16836179193Sjb
16837179193Sjb		break;
16838179193Sjb	}
16839179193Sjb
16840179193Sjb	case CPU_UNCONFIG:
16841179193Sjb		/*
16842179193Sjb		 * We don't free the buffer in the CPU_UNCONFIG case.  (The
16843179193Sjb		 * buffer will be freed when the consumer exits.)
16844179193Sjb		 */
16845179193Sjb		break;
16846179193Sjb
16847179193Sjb	default:
16848179193Sjb		break;
16849179193Sjb	}
16850179193Sjb
16851179193Sjb	mutex_exit(&dtrace_lock);
16852179193Sjb	return (0);
16853179193Sjb}
16854179193Sjb
16855277300Ssmh#ifdef illumos
16856179193Sjbstatic void
16857179193Sjbdtrace_cpu_setup_initial(processorid_t cpu)
16858179193Sjb{
16859179193Sjb	(void) dtrace_cpu_setup(CPU_CONFIG, cpu);
16860179193Sjb}
16861179198Sjb#endif
16862179193Sjb
16863179193Sjbstatic void
16864179193Sjbdtrace_toxrange_add(uintptr_t base, uintptr_t limit)
16865179193Sjb{
16866179193Sjb	if (dtrace_toxranges >= dtrace_toxranges_max) {
16867179193Sjb		int osize, nsize;
16868179193Sjb		dtrace_toxrange_t *range;
16869179193Sjb
16870179193Sjb		osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
16871179193Sjb
16872179193Sjb		if (osize == 0) {
16873179193Sjb			ASSERT(dtrace_toxrange == NULL);
16874179193Sjb			ASSERT(dtrace_toxranges_max == 0);
16875179193Sjb			dtrace_toxranges_max = 1;
16876179193Sjb		} else {
16877179193Sjb			dtrace_toxranges_max <<= 1;
16878179193Sjb		}
16879179193Sjb
16880179193Sjb		nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
16881179193Sjb		range = kmem_zalloc(nsize, KM_SLEEP);
16882179193Sjb
16883179193Sjb		if (dtrace_toxrange != NULL) {
16884179193Sjb			ASSERT(osize != 0);
16885179193Sjb			bcopy(dtrace_toxrange, range, osize);
16886179193Sjb			kmem_free(dtrace_toxrange, osize);
16887179193Sjb		}
16888179193Sjb
16889179193Sjb		dtrace_toxrange = range;
16890179193Sjb	}
16891179193Sjb
16892179198Sjb	ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0);
16893179198Sjb	ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0);
16894179193Sjb
16895179193Sjb	dtrace_toxrange[dtrace_toxranges].dtt_base = base;
16896179193Sjb	dtrace_toxrange[dtrace_toxranges].dtt_limit = limit;
16897179193Sjb	dtrace_toxranges++;
16898179193Sjb}
16899179193Sjb
16900267929Srpaulostatic void
16901267929Srpaulodtrace_getf_barrier()
16902267929Srpaulo{
16903277300Ssmh#ifdef illumos
16904267929Srpaulo	/*
16905267929Srpaulo	 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings
16906267929Srpaulo	 * that contain calls to getf(), this routine will be called on every
16907267929Srpaulo	 * closef() before either the underlying vnode is released or the
16908267929Srpaulo	 * file_t itself is freed.  By the time we are here, it is essential
16909267929Srpaulo	 * that the file_t can no longer be accessed from a call to getf()
16910267929Srpaulo	 * in probe context -- that assures that a dtrace_sync() can be used
16911267929Srpaulo	 * to clear out any enablings referring to the old structures.
16912267929Srpaulo	 */
16913267929Srpaulo	if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 ||
16914267929Srpaulo	    kcred->cr_zone->zone_dtrace_getf != 0)
16915267929Srpaulo		dtrace_sync();
16916267929Srpaulo#endif
16917267929Srpaulo}
16918267929Srpaulo
16919179193Sjb/*
16920179193Sjb * DTrace Driver Cookbook Functions
16921179193Sjb */
16922277300Ssmh#ifdef illumos
16923179193Sjb/*ARGSUSED*/
16924179193Sjbstatic int
16925179193Sjbdtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
16926179193Sjb{
16927179193Sjb	dtrace_provider_id_t id;
16928179193Sjb	dtrace_state_t *state = NULL;
16929179193Sjb	dtrace_enabling_t *enab;
16930179193Sjb
16931179193Sjb	mutex_enter(&cpu_lock);
16932179193Sjb	mutex_enter(&dtrace_provider_lock);
16933179193Sjb	mutex_enter(&dtrace_lock);
16934179193Sjb
16935179193Sjb	if (ddi_soft_state_init(&dtrace_softstate,
16936179193Sjb	    sizeof (dtrace_state_t), 0) != 0) {
16937179193Sjb		cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state");
16938179193Sjb		mutex_exit(&cpu_lock);
16939179193Sjb		mutex_exit(&dtrace_provider_lock);
16940179193Sjb		mutex_exit(&dtrace_lock);
16941179193Sjb		return (DDI_FAILURE);
16942179193Sjb	}
16943179193Sjb
16944179193Sjb	if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR,
16945179193Sjb	    DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE ||
16946179193Sjb	    ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR,
16947179193Sjb	    DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) {
16948179193Sjb		cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes");
16949179193Sjb		ddi_remove_minor_node(devi, NULL);
16950179193Sjb		ddi_soft_state_fini(&dtrace_softstate);
16951179193Sjb		mutex_exit(&cpu_lock);
16952179193Sjb		mutex_exit(&dtrace_provider_lock);
16953179193Sjb		mutex_exit(&dtrace_lock);
16954179193Sjb		return (DDI_FAILURE);
16955179193Sjb	}
16956179193Sjb
16957179193Sjb	ddi_report_dev(devi);
16958179193Sjb	dtrace_devi = devi;
16959179193Sjb
16960179193Sjb	dtrace_modload = dtrace_module_loaded;
16961179193Sjb	dtrace_modunload = dtrace_module_unloaded;
16962179193Sjb	dtrace_cpu_init = dtrace_cpu_setup_initial;
16963179193Sjb	dtrace_helpers_cleanup = dtrace_helpers_destroy;
16964179193Sjb	dtrace_helpers_fork = dtrace_helpers_duplicate;
16965179193Sjb	dtrace_cpustart_init = dtrace_suspend;
16966179193Sjb	dtrace_cpustart_fini = dtrace_resume;
16967179193Sjb	dtrace_debugger_init = dtrace_suspend;
16968179193Sjb	dtrace_debugger_fini = dtrace_resume;
16969179193Sjb
16970179193Sjb	register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
16971179193Sjb
16972179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
16973179193Sjb
16974179193Sjb	dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1,
16975179193Sjb	    NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
16976179193Sjb	dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE,
16977179193Sjb	    UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0,
16978179193Sjb	    VM_SLEEP | VMC_IDENTIFIER);
16979179193Sjb	dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri,
16980179193Sjb	    1, INT_MAX, 0);
16981179193Sjb
16982179193Sjb	dtrace_state_cache = kmem_cache_create("dtrace_state_cache",
16983179193Sjb	    sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN,
16984179193Sjb	    NULL, NULL, NULL, NULL, NULL, 0);
16985179193Sjb
16986179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
16987179193Sjb	dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod),
16988179193Sjb	    offsetof(dtrace_probe_t, dtpr_nextmod),
16989179193Sjb	    offsetof(dtrace_probe_t, dtpr_prevmod));
16990179193Sjb
16991179193Sjb	dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func),
16992179193Sjb	    offsetof(dtrace_probe_t, dtpr_nextfunc),
16993179193Sjb	    offsetof(dtrace_probe_t, dtpr_prevfunc));
16994179193Sjb
16995179193Sjb	dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name),
16996179193Sjb	    offsetof(dtrace_probe_t, dtpr_nextname),
16997179193Sjb	    offsetof(dtrace_probe_t, dtpr_prevname));
16998179193Sjb
16999179193Sjb	if (dtrace_retain_max < 1) {
17000179193Sjb		cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; "
17001179193Sjb		    "setting to 1", dtrace_retain_max);
17002179193Sjb		dtrace_retain_max = 1;
17003179193Sjb	}
17004179193Sjb
17005179193Sjb	/*
17006179193Sjb	 * Now discover our toxic ranges.
17007179193Sjb	 */
17008179193Sjb	dtrace_toxic_ranges(dtrace_toxrange_add);
17009179193Sjb
17010179193Sjb	/*
17011179193Sjb	 * Before we register ourselves as a provider to our own framework,
17012179193Sjb	 * we would like to assert that dtrace_provider is NULL -- but that's
17013179193Sjb	 * not true if we were loaded as a dependency of a DTrace provider.
17014179193Sjb	 * Once we've registered, we can assert that dtrace_provider is our
17015179193Sjb	 * pseudo provider.
17016179193Sjb	 */
17017179193Sjb	(void) dtrace_register("dtrace", &dtrace_provider_attr,
17018179193Sjb	    DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
17019179193Sjb
17020179193Sjb	ASSERT(dtrace_provider != NULL);
17021179193Sjb	ASSERT((dtrace_provider_id_t)dtrace_provider == id);
17022179193Sjb
17023179193Sjb	dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
17024179193Sjb	    dtrace_provider, NULL, NULL, "BEGIN", 0, NULL);
17025179193Sjb	dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
17026179193Sjb	    dtrace_provider, NULL, NULL, "END", 0, NULL);
17027179193Sjb	dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
17028179193Sjb	    dtrace_provider, NULL, NULL, "ERROR", 1, NULL);
17029179193Sjb
17030179193Sjb	dtrace_anon_property();
17031179193Sjb	mutex_exit(&cpu_lock);
17032179193Sjb
17033179193Sjb	/*
17034179193Sjb	 * If there are already providers, we must ask them to provide their
17035179193Sjb	 * probes, and then match any anonymous enabling against them.  Note
17036179193Sjb	 * that there should be no other retained enablings at this time:
17037179193Sjb	 * the only retained enablings at this time should be the anonymous
17038179193Sjb	 * enabling.
17039179193Sjb	 */
17040179193Sjb	if (dtrace_anon.dta_enabling != NULL) {
17041179193Sjb		ASSERT(dtrace_retained == dtrace_anon.dta_enabling);
17042179193Sjb
17043179193Sjb		dtrace_enabling_provide(NULL);
17044179193Sjb		state = dtrace_anon.dta_state;
17045179193Sjb
17046179193Sjb		/*
17047179193Sjb		 * We couldn't hold cpu_lock across the above call to
17048179193Sjb		 * dtrace_enabling_provide(), but we must hold it to actually
17049179193Sjb		 * enable the probes.  We have to drop all of our locks, pick
17050179193Sjb		 * up cpu_lock, and regain our locks before matching the
17051179193Sjb		 * retained anonymous enabling.
17052179193Sjb		 */
17053179193Sjb		mutex_exit(&dtrace_lock);
17054179193Sjb		mutex_exit(&dtrace_provider_lock);
17055179193Sjb
17056179193Sjb		mutex_enter(&cpu_lock);
17057179193Sjb		mutex_enter(&dtrace_provider_lock);
17058179193Sjb		mutex_enter(&dtrace_lock);
17059179193Sjb
17060179193Sjb		if ((enab = dtrace_anon.dta_enabling) != NULL)
17061179193Sjb			(void) dtrace_enabling_match(enab, NULL);
17062179193Sjb
17063179193Sjb		mutex_exit(&cpu_lock);
17064179193Sjb	}
17065179193Sjb
17066179193Sjb	mutex_exit(&dtrace_lock);
17067179193Sjb	mutex_exit(&dtrace_provider_lock);
17068179193Sjb
17069179193Sjb	if (state != NULL) {
17070179193Sjb		/*
17071179193Sjb		 * If we created any anonymous state, set it going now.
17072179193Sjb		 */
17073179193Sjb		(void) dtrace_state_go(state, &dtrace_anon.dta_beganon);
17074179193Sjb	}
17075179193Sjb
17076179193Sjb	return (DDI_SUCCESS);
17077179193Sjb}
17078277300Ssmh#endif	/* illumos */
17079179193Sjb
17080277300Ssmh#ifndef illumos
17081239786Sedstatic void dtrace_dtr(void *);
17082184698Srodrigc#endif
17083184698Srodrigc
17084179193Sjb/*ARGSUSED*/
17085179193Sjbstatic int
17086277300Ssmh#ifdef illumos
17087179193Sjbdtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
17088179198Sjb#else
17089179198Sjbdtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
17090179198Sjb#endif
17091179193Sjb{
17092179193Sjb	dtrace_state_t *state;
17093179193Sjb	uint32_t priv;
17094179193Sjb	uid_t uid;
17095179193Sjb	zoneid_t zoneid;
17096179193Sjb
17097277300Ssmh#ifdef illumos
17098179193Sjb	if (getminor(*devp) == DTRACEMNRN_HELPER)
17099179193Sjb		return (0);
17100179193Sjb
17101179193Sjb	/*
17102179193Sjb	 * If this wasn't an open with the "helper" minor, then it must be
17103179193Sjb	 * the "dtrace" minor.
17104179193Sjb	 */
17105268125Spfg	if (getminor(*devp) == DTRACEMNRN_DTRACE)
17106268125Spfg		return (ENXIO);
17107179198Sjb#else
17108179198Sjb	cred_t *cred_p = NULL;
17109179198Sjb	cred_p = dev->si_cred;
17110179198Sjb
17111179198Sjb	/*
17112179193Sjb	 * If no DTRACE_PRIV_* bits are set in the credential, then the
17113179193Sjb	 * caller lacks sufficient permission to do anything with DTrace.
17114179193Sjb	 */
17115179193Sjb	dtrace_cred2priv(cred_p, &priv, &uid, &zoneid);
17116179198Sjb	if (priv == DTRACE_PRIV_NONE) {
17117179198Sjb#endif
17118179198Sjb
17119179193Sjb		return (EACCES);
17120179198Sjb	}
17121179193Sjb
17122179193Sjb	/*
17123179193Sjb	 * Ask all providers to provide all their probes.
17124179193Sjb	 */
17125179193Sjb	mutex_enter(&dtrace_provider_lock);
17126179193Sjb	dtrace_probe_provide(NULL, NULL);
17127179193Sjb	mutex_exit(&dtrace_provider_lock);
17128179193Sjb
17129179193Sjb	mutex_enter(&cpu_lock);
17130179193Sjb	mutex_enter(&dtrace_lock);
17131179193Sjb	dtrace_opens++;
17132179193Sjb	dtrace_membar_producer();
17133179193Sjb
17134277300Ssmh#ifdef illumos
17135179193Sjb	/*
17136179193Sjb	 * If the kernel debugger is active (that is, if the kernel debugger
17137179193Sjb	 * modified text in some way), we won't allow the open.
17138179193Sjb	 */
17139179193Sjb	if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
17140179193Sjb		dtrace_opens--;
17141179193Sjb		mutex_exit(&cpu_lock);
17142179193Sjb		mutex_exit(&dtrace_lock);
17143179193Sjb		return (EBUSY);
17144179193Sjb	}
17145179193Sjb
17146278166Spfg	if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) {
17147278166Spfg		/*
17148278166Spfg		 * If DTrace helper tracing is enabled, we need to allocate the
17149278166Spfg		 * trace buffer and initialize the values.
17150278166Spfg		 */
17151278166Spfg		dtrace_helptrace_buffer =
17152278166Spfg		    kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP);
17153278166Spfg		dtrace_helptrace_next = 0;
17154278166Spfg		dtrace_helptrace_wrapped = 0;
17155278166Spfg		dtrace_helptrace_enable = 0;
17156278166Spfg	}
17157278166Spfg
17158179193Sjb	state = dtrace_state_create(devp, cred_p);
17159179198Sjb#else
17160297773Smarkj	state = dtrace_state_create(dev, NULL);
17161184698Srodrigc	devfs_set_cdevpriv(state, dtrace_dtr);
17162179198Sjb#endif
17163179198Sjb
17164179193Sjb	mutex_exit(&cpu_lock);
17165179193Sjb
17166179193Sjb	if (state == NULL) {
17167277300Ssmh#ifdef illumos
17168268231Spfg		if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
17169179193Sjb			(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
17170179198Sjb#else
17171179198Sjb		--dtrace_opens;
17172179198Sjb#endif
17173179193Sjb		mutex_exit(&dtrace_lock);
17174179193Sjb		return (EAGAIN);
17175179193Sjb	}
17176179193Sjb
17177179193Sjb	mutex_exit(&dtrace_lock);
17178179193Sjb
17179179193Sjb	return (0);
17180179193Sjb}
17181179193Sjb
17182179193Sjb/*ARGSUSED*/
17183277300Ssmh#ifdef illumos
17184179193Sjbstatic int
17185179193Sjbdtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
17186179198Sjb#else
17187239786Sedstatic void
17188239786Seddtrace_dtr(void *data)
17189179198Sjb#endif
17190179193Sjb{
17191277300Ssmh#ifdef illumos
17192179193Sjb	minor_t minor = getminor(dev);
17193179193Sjb	dtrace_state_t *state;
17194278166Spfg#endif
17195278166Spfg	dtrace_helptrace_t *buf = NULL;
17196179193Sjb
17197278166Spfg#ifdef illumos
17198179193Sjb	if (minor == DTRACEMNRN_HELPER)
17199179193Sjb		return (0);
17200179193Sjb
17201179193Sjb	state = ddi_get_soft_state(dtrace_softstate, minor);
17202179198Sjb#else
17203239786Sed	dtrace_state_t *state = data;
17204184698Srodrigc#endif
17205179198Sjb
17206179193Sjb	mutex_enter(&cpu_lock);
17207179193Sjb	mutex_enter(&dtrace_lock);
17208179193Sjb
17209278137Smarkj#ifdef illumos
17210278137Smarkj	if (state->dts_anon)
17211278137Smarkj#else
17212278137Smarkj	if (state != NULL && state->dts_anon)
17213278137Smarkj#endif
17214278137Smarkj	{
17215278136Smarkj		/*
17216278136Smarkj		 * There is anonymous state. Destroy that first.
17217278136Smarkj		 */
17218278136Smarkj		ASSERT(dtrace_anon.dta_state == NULL);
17219278136Smarkj		dtrace_state_destroy(state->dts_anon);
17220278136Smarkj	}
17221179198Sjb
17222278166Spfg	if (dtrace_helptrace_disable) {
17223278166Spfg		/*
17224278166Spfg		 * If we have been told to disable helper tracing, set the
17225278166Spfg		 * buffer to NULL before calling into dtrace_state_destroy();
17226278166Spfg		 * we take advantage of its dtrace_sync() to know that no
17227278166Spfg		 * CPU is in probe context with enabled helper tracing
17228278166Spfg		 * after it returns.
17229278166Spfg		 */
17230278166Spfg		buf = dtrace_helptrace_buffer;
17231278166Spfg		dtrace_helptrace_buffer = NULL;
17232278166Spfg	}
17233278166Spfg
17234278137Smarkj#ifdef illumos
17235278136Smarkj	dtrace_state_destroy(state);
17236278137Smarkj#else
17237278370Smarkj	if (state != NULL) {
17238278137Smarkj		dtrace_state_destroy(state);
17239278137Smarkj		kmem_free(state, 0);
17240278137Smarkj	}
17241179198Sjb#endif
17242278136Smarkj	ASSERT(dtrace_opens > 0);
17243179193Sjb
17244277300Ssmh#ifdef illumos
17245268231Spfg	/*
17246268231Spfg	 * Only relinquish control of the kernel debugger interface when there
17247268231Spfg	 * are no consumers and no anonymous enablings.
17248268231Spfg	 */
17249268231Spfg	if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
17250179193Sjb		(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
17251179198Sjb#else
17252179198Sjb	--dtrace_opens;
17253179198Sjb#endif
17254179193Sjb
17255278166Spfg	if (buf != NULL) {
17256278166Spfg		kmem_free(buf, dtrace_helptrace_bufsize);
17257278166Spfg		dtrace_helptrace_disable = 0;
17258278166Spfg	}
17259278166Spfg
17260179193Sjb	mutex_exit(&dtrace_lock);
17261179193Sjb	mutex_exit(&cpu_lock);
17262179193Sjb
17263277300Ssmh#ifdef illumos
17264179193Sjb	return (0);
17265239786Sed#endif
17266179193Sjb}
17267179193Sjb
17268277300Ssmh#ifdef illumos
17269179193Sjb/*ARGSUSED*/
17270179193Sjbstatic int
17271179193Sjbdtrace_ioctl_helper(int cmd, intptr_t arg, int *rv)
17272179193Sjb{
17273179193Sjb	int rval;
17274179193Sjb	dof_helper_t help, *dhp = NULL;
17275179193Sjb
17276179193Sjb	switch (cmd) {
17277179193Sjb	case DTRACEHIOC_ADDDOF:
17278179193Sjb		if (copyin((void *)arg, &help, sizeof (help)) != 0) {
17279179193Sjb			dtrace_dof_error(NULL, "failed to copyin DOF helper");
17280179193Sjb			return (EFAULT);
17281179193Sjb		}
17282179193Sjb
17283179193Sjb		dhp = &help;
17284179193Sjb		arg = (intptr_t)help.dofhp_dof;
17285179193Sjb		/*FALLTHROUGH*/
17286179193Sjb
17287179193Sjb	case DTRACEHIOC_ADD: {
17288179193Sjb		dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval);
17289179193Sjb
17290179193Sjb		if (dof == NULL)
17291179193Sjb			return (rval);
17292179193Sjb
17293179193Sjb		mutex_enter(&dtrace_lock);
17294179193Sjb
17295179193Sjb		/*
17296179193Sjb		 * dtrace_helper_slurp() takes responsibility for the dof --
17297179193Sjb		 * it may free it now or it may save it and free it later.
17298179193Sjb		 */
17299179193Sjb		if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) {
17300179193Sjb			*rv = rval;
17301179193Sjb			rval = 0;
17302179193Sjb		} else {
17303179193Sjb			rval = EINVAL;
17304179193Sjb		}
17305179193Sjb
17306179193Sjb		mutex_exit(&dtrace_lock);
17307179193Sjb		return (rval);
17308179193Sjb	}
17309179193Sjb
17310179193Sjb	case DTRACEHIOC_REMOVE: {
17311179193Sjb		mutex_enter(&dtrace_lock);
17312281257Smarkj		rval = dtrace_helper_destroygen(NULL, arg);
17313179193Sjb		mutex_exit(&dtrace_lock);
17314179193Sjb
17315179193Sjb		return (rval);
17316179193Sjb	}
17317179193Sjb
17318179193Sjb	default:
17319179193Sjb		break;
17320179193Sjb	}
17321179193Sjb
17322179193Sjb	return (ENOTTY);
17323179193Sjb}
17324179193Sjb
17325179193Sjb/*ARGSUSED*/
17326179193Sjbstatic int
17327179193Sjbdtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
17328179193Sjb{
17329179193Sjb	minor_t minor = getminor(dev);
17330179193Sjb	dtrace_state_t *state;
17331179193Sjb	int rval;
17332179193Sjb
17333179193Sjb	if (minor == DTRACEMNRN_HELPER)
17334179193Sjb		return (dtrace_ioctl_helper(cmd, arg, rv));
17335179193Sjb
17336179193Sjb	state = ddi_get_soft_state(dtrace_softstate, minor);
17337179193Sjb
17338179193Sjb	if (state->dts_anon) {
17339179193Sjb		ASSERT(dtrace_anon.dta_state == NULL);
17340179193Sjb		state = state->dts_anon;
17341179193Sjb	}
17342179193Sjb
17343179193Sjb	switch (cmd) {
17344179193Sjb	case DTRACEIOC_PROVIDER: {
17345179193Sjb		dtrace_providerdesc_t pvd;
17346179193Sjb		dtrace_provider_t *pvp;
17347179193Sjb
17348179193Sjb		if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0)
17349179193Sjb			return (EFAULT);
17350179193Sjb
17351179193Sjb		pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
17352179193Sjb		mutex_enter(&dtrace_provider_lock);
17353179193Sjb
17354179193Sjb		for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
17355179193Sjb			if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0)
17356179193Sjb				break;
17357179193Sjb		}
17358179193Sjb
17359179193Sjb		mutex_exit(&dtrace_provider_lock);
17360179193Sjb
17361179193Sjb		if (pvp == NULL)
17362179193Sjb			return (ESRCH);
17363179193Sjb
17364179193Sjb		bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t));
17365179193Sjb		bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t));
17366179198Sjb
17367179193Sjb		if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0)
17368179193Sjb			return (EFAULT);
17369179193Sjb
17370179193Sjb		return (0);
17371179193Sjb	}
17372179193Sjb
17373179193Sjb	case DTRACEIOC_EPROBE: {
17374179193Sjb		dtrace_eprobedesc_t epdesc;
17375179193Sjb		dtrace_ecb_t *ecb;
17376179193Sjb		dtrace_action_t *act;
17377179193Sjb		void *buf;
17378179193Sjb		size_t size;
17379179193Sjb		uintptr_t dest;
17380179193Sjb		int nrecs;
17381179193Sjb
17382179193Sjb		if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0)
17383179193Sjb			return (EFAULT);
17384179193Sjb
17385179193Sjb		mutex_enter(&dtrace_lock);
17386179193Sjb
17387179193Sjb		if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
17388179193Sjb			mutex_exit(&dtrace_lock);
17389179193Sjb			return (EINVAL);
17390179193Sjb		}
17391179193Sjb
17392179193Sjb		if (ecb->dte_probe == NULL) {
17393179193Sjb			mutex_exit(&dtrace_lock);
17394179193Sjb			return (EINVAL);
17395179193Sjb		}
17396179193Sjb
17397179193Sjb		epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
17398179193Sjb		epdesc.dtepd_uarg = ecb->dte_uarg;
17399179193Sjb		epdesc.dtepd_size = ecb->dte_size;
17400179193Sjb
17401179193Sjb		nrecs = epdesc.dtepd_nrecs;
17402179193Sjb		epdesc.dtepd_nrecs = 0;
17403179193Sjb		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
17404179193Sjb			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
17405179193Sjb				continue;
17406179193Sjb
17407179193Sjb			epdesc.dtepd_nrecs++;
17408179193Sjb		}
17409179193Sjb
17410179193Sjb		/*
17411179193Sjb		 * Now that we have the size, we need to allocate a temporary
17412179193Sjb		 * buffer in which to store the complete description.  We need
17413179193Sjb		 * the temporary buffer to be able to drop dtrace_lock()
17414179193Sjb		 * across the copyout(), below.
17415179193Sjb		 */
17416179193Sjb		size = sizeof (dtrace_eprobedesc_t) +
17417179193Sjb		    (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
17418179193Sjb
17419179193Sjb		buf = kmem_alloc(size, KM_SLEEP);
17420179193Sjb		dest = (uintptr_t)buf;
17421179193Sjb
17422179193Sjb		bcopy(&epdesc, (void *)dest, sizeof (epdesc));
17423179193Sjb		dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
17424179193Sjb
17425179193Sjb		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
17426179193Sjb			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
17427179193Sjb				continue;
17428179193Sjb
17429179193Sjb			if (nrecs-- == 0)
17430179193Sjb				break;
17431179193Sjb
17432179193Sjb			bcopy(&act->dta_rec, (void *)dest,
17433179193Sjb			    sizeof (dtrace_recdesc_t));
17434179193Sjb			dest += sizeof (dtrace_recdesc_t);
17435179193Sjb		}
17436179193Sjb
17437179193Sjb		mutex_exit(&dtrace_lock);
17438179193Sjb
17439179193Sjb		if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
17440179193Sjb			kmem_free(buf, size);
17441179193Sjb			return (EFAULT);
17442179193Sjb		}
17443179193Sjb
17444179193Sjb		kmem_free(buf, size);
17445179193Sjb		return (0);
17446179193Sjb	}
17447179193Sjb
17448179193Sjb	case DTRACEIOC_AGGDESC: {
17449179193Sjb		dtrace_aggdesc_t aggdesc;
17450179193Sjb		dtrace_action_t *act;
17451179193Sjb		dtrace_aggregation_t *agg;
17452179193Sjb		int nrecs;
17453179193Sjb		uint32_t offs;
17454179193Sjb		dtrace_recdesc_t *lrec;
17455179193Sjb		void *buf;
17456179193Sjb		size_t size;
17457179193Sjb		uintptr_t dest;
17458179193Sjb
17459179193Sjb		if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0)
17460179193Sjb			return (EFAULT);
17461179193Sjb
17462179193Sjb		mutex_enter(&dtrace_lock);
17463179193Sjb
17464179193Sjb		if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) {
17465179193Sjb			mutex_exit(&dtrace_lock);
17466179193Sjb			return (EINVAL);
17467179193Sjb		}
17468179193Sjb
17469179193Sjb		aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid;
17470179193Sjb
17471179193Sjb		nrecs = aggdesc.dtagd_nrecs;
17472179193Sjb		aggdesc.dtagd_nrecs = 0;
17473179193Sjb
17474179193Sjb		offs = agg->dtag_base;
17475179193Sjb		lrec = &agg->dtag_action.dta_rec;
17476179193Sjb		aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs;
17477179193Sjb
17478179193Sjb		for (act = agg->dtag_first; ; act = act->dta_next) {
17479179193Sjb			ASSERT(act->dta_intuple ||
17480179193Sjb			    DTRACEACT_ISAGG(act->dta_kind));
17481179193Sjb
17482179193Sjb			/*
17483179193Sjb			 * If this action has a record size of zero, it
17484179193Sjb			 * denotes an argument to the aggregating action.
17485179193Sjb			 * Because the presence of this record doesn't (or
17486179193Sjb			 * shouldn't) affect the way the data is interpreted,
17487179193Sjb			 * we don't copy it out to save user-level the
17488179193Sjb			 * confusion of dealing with a zero-length record.
17489179193Sjb			 */
17490179193Sjb			if (act->dta_rec.dtrd_size == 0) {
17491179193Sjb				ASSERT(agg->dtag_hasarg);
17492179193Sjb				continue;
17493179193Sjb			}
17494179193Sjb
17495179193Sjb			aggdesc.dtagd_nrecs++;
17496179193Sjb
17497179193Sjb			if (act == &agg->dtag_action)
17498179193Sjb				break;
17499179193Sjb		}
17500179193Sjb
17501179193Sjb		/*
17502179193Sjb		 * Now that we have the size, we need to allocate a temporary
17503179193Sjb		 * buffer in which to store the complete description.  We need
17504179193Sjb		 * the temporary buffer to be able to drop dtrace_lock()
17505179193Sjb		 * across the copyout(), below.
17506179193Sjb		 */
17507179193Sjb		size = sizeof (dtrace_aggdesc_t) +
17508179193Sjb		    (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t));
17509179193Sjb
17510179193Sjb		buf = kmem_alloc(size, KM_SLEEP);
17511179193Sjb		dest = (uintptr_t)buf;
17512179193Sjb
17513179193Sjb		bcopy(&aggdesc, (void *)dest, sizeof (aggdesc));
17514179193Sjb		dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]);
17515179193Sjb
17516179193Sjb		for (act = agg->dtag_first; ; act = act->dta_next) {
17517179193Sjb			dtrace_recdesc_t rec = act->dta_rec;
17518179193Sjb
17519179193Sjb			/*
17520179193Sjb			 * See the comment in the above loop for why we pass
17521179193Sjb			 * over zero-length records.
17522179193Sjb			 */
17523179193Sjb			if (rec.dtrd_size == 0) {
17524179193Sjb				ASSERT(agg->dtag_hasarg);
17525179193Sjb				continue;
17526179193Sjb			}
17527179193Sjb
17528179193Sjb			if (nrecs-- == 0)
17529179193Sjb				break;
17530179193Sjb
17531179193Sjb			rec.dtrd_offset -= offs;
17532179193Sjb			bcopy(&rec, (void *)dest, sizeof (rec));
17533179193Sjb			dest += sizeof (dtrace_recdesc_t);
17534179193Sjb
17535179193Sjb			if (act == &agg->dtag_action)
17536179193Sjb				break;
17537179193Sjb		}
17538179193Sjb
17539179193Sjb		mutex_exit(&dtrace_lock);
17540179193Sjb
17541179193Sjb		if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
17542179193Sjb			kmem_free(buf, size);
17543179193Sjb			return (EFAULT);
17544179193Sjb		}
17545179193Sjb
17546179193Sjb		kmem_free(buf, size);
17547179193Sjb		return (0);
17548179193Sjb	}
17549179193Sjb
17550179193Sjb	case DTRACEIOC_ENABLE: {
17551179193Sjb		dof_hdr_t *dof;
17552179193Sjb		dtrace_enabling_t *enab = NULL;
17553179193Sjb		dtrace_vstate_t *vstate;
17554179193Sjb		int err = 0;
17555179193Sjb
17556179193Sjb		*rv = 0;
17557179193Sjb
17558179193Sjb		/*
17559179193Sjb		 * If a NULL argument has been passed, we take this as our
17560179193Sjb		 * cue to reevaluate our enablings.
17561179193Sjb		 */
17562179193Sjb		if (arg == NULL) {
17563179198Sjb			dtrace_enabling_matchall();
17564179193Sjb
17565179198Sjb			return (0);
17566179193Sjb		}
17567179193Sjb
17568179193Sjb		if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL)
17569179193Sjb			return (rval);
17570179193Sjb
17571179193Sjb		mutex_enter(&cpu_lock);
17572179193Sjb		mutex_enter(&dtrace_lock);
17573179193Sjb		vstate = &state->dts_vstate;
17574179193Sjb
17575179193Sjb		if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
17576179193Sjb			mutex_exit(&dtrace_lock);
17577179193Sjb			mutex_exit(&cpu_lock);
17578179193Sjb			dtrace_dof_destroy(dof);
17579179193Sjb			return (EBUSY);
17580179193Sjb		}
17581179193Sjb
17582179193Sjb		if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) {
17583179193Sjb			mutex_exit(&dtrace_lock);
17584179193Sjb			mutex_exit(&cpu_lock);
17585179193Sjb			dtrace_dof_destroy(dof);
17586179193Sjb			return (EINVAL);
17587179193Sjb		}
17588179193Sjb
17589179193Sjb		if ((rval = dtrace_dof_options(dof, state)) != 0) {
17590179193Sjb			dtrace_enabling_destroy(enab);
17591179193Sjb			mutex_exit(&dtrace_lock);
17592179193Sjb			mutex_exit(&cpu_lock);
17593179193Sjb			dtrace_dof_destroy(dof);
17594179193Sjb			return (rval);
17595179193Sjb		}
17596179193Sjb
17597179193Sjb		if ((err = dtrace_enabling_match(enab, rv)) == 0) {
17598179193Sjb			err = dtrace_enabling_retain(enab);
17599179193Sjb		} else {
17600179193Sjb			dtrace_enabling_destroy(enab);
17601179193Sjb		}
17602179193Sjb
17603179193Sjb		mutex_exit(&cpu_lock);
17604179193Sjb		mutex_exit(&dtrace_lock);
17605179193Sjb		dtrace_dof_destroy(dof);
17606179193Sjb
17607179193Sjb		return (err);
17608179193Sjb	}
17609179193Sjb
17610179193Sjb	case DTRACEIOC_REPLICATE: {
17611179193Sjb		dtrace_repldesc_t desc;
17612179193Sjb		dtrace_probedesc_t *match = &desc.dtrpd_match;
17613179193Sjb		dtrace_probedesc_t *create = &desc.dtrpd_create;
17614179193Sjb		int err;
17615179193Sjb
17616179193Sjb		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
17617179193Sjb			return (EFAULT);
17618179193Sjb
17619179193Sjb		match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
17620179193Sjb		match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
17621179193Sjb		match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
17622179193Sjb		match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
17623179193Sjb
17624179193Sjb		create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
17625179193Sjb		create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
17626179193Sjb		create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
17627179193Sjb		create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
17628179193Sjb
17629179193Sjb		mutex_enter(&dtrace_lock);
17630179193Sjb		err = dtrace_enabling_replicate(state, match, create);
17631179193Sjb		mutex_exit(&dtrace_lock);
17632179193Sjb
17633179193Sjb		return (err);
17634179193Sjb	}
17635179193Sjb
17636179193Sjb	case DTRACEIOC_PROBEMATCH:
17637179193Sjb	case DTRACEIOC_PROBES: {
17638179193Sjb		dtrace_probe_t *probe = NULL;
17639179193Sjb		dtrace_probedesc_t desc;
17640179193Sjb		dtrace_probekey_t pkey;
17641179193Sjb		dtrace_id_t i;
17642179193Sjb		int m = 0;
17643179193Sjb		uint32_t priv;
17644179193Sjb		uid_t uid;
17645179193Sjb		zoneid_t zoneid;
17646179193Sjb
17647179193Sjb		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
17648179193Sjb			return (EFAULT);
17649179193Sjb
17650179193Sjb		desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
17651179193Sjb		desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
17652179193Sjb		desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
17653179193Sjb		desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0';
17654179193Sjb
17655179193Sjb		/*
17656179193Sjb		 * Before we attempt to match this probe, we want to give
17657179193Sjb		 * all providers the opportunity to provide it.
17658179193Sjb		 */
17659179193Sjb		if (desc.dtpd_id == DTRACE_IDNONE) {
17660179193Sjb			mutex_enter(&dtrace_provider_lock);
17661179193Sjb			dtrace_probe_provide(&desc, NULL);
17662179193Sjb			mutex_exit(&dtrace_provider_lock);
17663179193Sjb			desc.dtpd_id++;
17664179193Sjb		}
17665179193Sjb
17666179193Sjb		if (cmd == DTRACEIOC_PROBEMATCH)  {
17667179193Sjb			dtrace_probekey(&desc, &pkey);
17668179193Sjb			pkey.dtpk_id = DTRACE_IDNONE;
17669179193Sjb		}
17670179193Sjb
17671179193Sjb		dtrace_cred2priv(cr, &priv, &uid, &zoneid);
17672179193Sjb
17673179193Sjb		mutex_enter(&dtrace_lock);
17674179193Sjb
17675179193Sjb		if (cmd == DTRACEIOC_PROBEMATCH) {
17676179193Sjb			for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
17677179193Sjb				if ((probe = dtrace_probes[i - 1]) != NULL &&
17678179193Sjb				    (m = dtrace_match_probe(probe, &pkey,
17679179193Sjb				    priv, uid, zoneid)) != 0)
17680179193Sjb					break;
17681179193Sjb			}
17682179193Sjb
17683179193Sjb			if (m < 0) {
17684179193Sjb				mutex_exit(&dtrace_lock);
17685179193Sjb				return (EINVAL);
17686179193Sjb			}
17687179193Sjb
17688179193Sjb		} else {
17689179193Sjb			for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
17690179193Sjb				if ((probe = dtrace_probes[i - 1]) != NULL &&
17691179193Sjb				    dtrace_match_priv(probe, priv, uid, zoneid))
17692179193Sjb					break;
17693179193Sjb			}
17694179193Sjb		}
17695179193Sjb
17696179193Sjb		if (probe == NULL) {
17697179193Sjb			mutex_exit(&dtrace_lock);
17698179193Sjb			return (ESRCH);
17699179193Sjb		}
17700179193Sjb
17701179193Sjb		dtrace_probe_description(probe, &desc);
17702179193Sjb		mutex_exit(&dtrace_lock);
17703179193Sjb
17704179193Sjb		if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
17705179193Sjb			return (EFAULT);
17706179193Sjb
17707179193Sjb		return (0);
17708179193Sjb	}
17709179193Sjb
17710179193Sjb	case DTRACEIOC_PROBEARG: {
17711179193Sjb		dtrace_argdesc_t desc;
17712179193Sjb		dtrace_probe_t *probe;
17713179193Sjb		dtrace_provider_t *prov;
17714179193Sjb
17715179193Sjb		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
17716179193Sjb			return (EFAULT);
17717179193Sjb
17718179193Sjb		if (desc.dtargd_id == DTRACE_IDNONE)
17719179193Sjb			return (EINVAL);
17720179193Sjb
17721179193Sjb		if (desc.dtargd_ndx == DTRACE_ARGNONE)
17722179193Sjb			return (EINVAL);
17723179193Sjb
17724179193Sjb		mutex_enter(&dtrace_provider_lock);
17725179193Sjb		mutex_enter(&mod_lock);
17726179193Sjb		mutex_enter(&dtrace_lock);
17727179193Sjb
17728179193Sjb		if (desc.dtargd_id > dtrace_nprobes) {
17729179193Sjb			mutex_exit(&dtrace_lock);
17730179193Sjb			mutex_exit(&mod_lock);
17731179193Sjb			mutex_exit(&dtrace_provider_lock);
17732179193Sjb			return (EINVAL);
17733179193Sjb		}
17734179193Sjb
17735179193Sjb		if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) {
17736179193Sjb			mutex_exit(&dtrace_lock);
17737179193Sjb			mutex_exit(&mod_lock);
17738179193Sjb			mutex_exit(&dtrace_provider_lock);
17739179193Sjb			return (EINVAL);
17740179193Sjb		}
17741179193Sjb
17742179193Sjb		mutex_exit(&dtrace_lock);
17743179193Sjb
17744179193Sjb		prov = probe->dtpr_provider;
17745179193Sjb
17746179193Sjb		if (prov->dtpv_pops.dtps_getargdesc == NULL) {
17747179193Sjb			/*
17748179193Sjb			 * There isn't any typed information for this probe.
17749179193Sjb			 * Set the argument number to DTRACE_ARGNONE.
17750179193Sjb			 */
17751179193Sjb			desc.dtargd_ndx = DTRACE_ARGNONE;
17752179193Sjb		} else {
17753179193Sjb			desc.dtargd_native[0] = '\0';
17754179193Sjb			desc.dtargd_xlate[0] = '\0';
17755179193Sjb			desc.dtargd_mapping = desc.dtargd_ndx;
17756179193Sjb
17757179193Sjb			prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
17758179193Sjb			    probe->dtpr_id, probe->dtpr_arg, &desc);
17759179193Sjb		}
17760179193Sjb
17761179193Sjb		mutex_exit(&mod_lock);
17762179193Sjb		mutex_exit(&dtrace_provider_lock);
17763179193Sjb
17764179193Sjb		if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
17765179193Sjb			return (EFAULT);
17766179193Sjb
17767179193Sjb		return (0);
17768179193Sjb	}
17769179193Sjb
17770179193Sjb	case DTRACEIOC_GO: {
17771179193Sjb		processorid_t cpuid;
17772179193Sjb		rval = dtrace_state_go(state, &cpuid);
17773179193Sjb
17774179193Sjb		if (rval != 0)
17775179193Sjb			return (rval);
17776179193Sjb
17777179193Sjb		if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
17778179193Sjb			return (EFAULT);
17779179193Sjb
17780179193Sjb		return (0);
17781179193Sjb	}
17782179193Sjb
17783179193Sjb	case DTRACEIOC_STOP: {
17784179193Sjb		processorid_t cpuid;
17785179193Sjb
17786179193Sjb		mutex_enter(&dtrace_lock);
17787179193Sjb		rval = dtrace_state_stop(state, &cpuid);
17788179193Sjb		mutex_exit(&dtrace_lock);
17789179193Sjb
17790179193Sjb		if (rval != 0)
17791179193Sjb			return (rval);
17792179193Sjb
17793179193Sjb		if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
17794179193Sjb			return (EFAULT);
17795179193Sjb
17796179193Sjb		return (0);
17797179193Sjb	}
17798179193Sjb
17799179193Sjb	case DTRACEIOC_DOFGET: {
17800179193Sjb		dof_hdr_t hdr, *dof;
17801179193Sjb		uint64_t len;
17802179193Sjb
17803179193Sjb		if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0)
17804179193Sjb			return (EFAULT);
17805179193Sjb
17806179193Sjb		mutex_enter(&dtrace_lock);
17807179193Sjb		dof = dtrace_dof_create(state);
17808179193Sjb		mutex_exit(&dtrace_lock);
17809179193Sjb
17810179193Sjb		len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
17811179193Sjb		rval = copyout(dof, (void *)arg, len);
17812179193Sjb		dtrace_dof_destroy(dof);
17813179193Sjb
17814179193Sjb		return (rval == 0 ? 0 : EFAULT);
17815179193Sjb	}
17816179193Sjb
17817179193Sjb	case DTRACEIOC_AGGSNAP:
17818179193Sjb	case DTRACEIOC_BUFSNAP: {
17819179193Sjb		dtrace_bufdesc_t desc;
17820179193Sjb		caddr_t cached;
17821179193Sjb		dtrace_buffer_t *buf;
17822179193Sjb
17823179193Sjb		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
17824179193Sjb			return (EFAULT);
17825179193Sjb
17826179193Sjb		if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU)
17827179193Sjb			return (EINVAL);
17828179193Sjb
17829179193Sjb		mutex_enter(&dtrace_lock);
17830179193Sjb
17831179193Sjb		if (cmd == DTRACEIOC_BUFSNAP) {
17832179193Sjb			buf = &state->dts_buffer[desc.dtbd_cpu];
17833179193Sjb		} else {
17834179193Sjb			buf = &state->dts_aggbuffer[desc.dtbd_cpu];
17835179193Sjb		}
17836179193Sjb
17837179193Sjb		if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) {
17838179193Sjb			size_t sz = buf->dtb_offset;
17839179193Sjb
17840179193Sjb			if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) {
17841179193Sjb				mutex_exit(&dtrace_lock);
17842179193Sjb				return (EBUSY);
17843179193Sjb			}
17844179193Sjb
17845179193Sjb			/*
17846179193Sjb			 * If this buffer has already been consumed, we're
17847179193Sjb			 * going to indicate that there's nothing left here
17848179193Sjb			 * to consume.
17849179193Sjb			 */
17850179193Sjb			if (buf->dtb_flags & DTRACEBUF_CONSUMED) {
17851179193Sjb				mutex_exit(&dtrace_lock);
17852179193Sjb
17853179193Sjb				desc.dtbd_size = 0;
17854179193Sjb				desc.dtbd_drops = 0;
17855179193Sjb				desc.dtbd_errors = 0;
17856179193Sjb				desc.dtbd_oldest = 0;
17857179193Sjb				sz = sizeof (desc);
17858179193Sjb
17859179193Sjb				if (copyout(&desc, (void *)arg, sz) != 0)
17860179193Sjb					return (EFAULT);
17861179193Sjb
17862179193Sjb				return (0);
17863179193Sjb			}
17864179193Sjb
17865179193Sjb			/*
17866179193Sjb			 * If this is a ring buffer that has wrapped, we want
17867179193Sjb			 * to copy the whole thing out.
17868179193Sjb			 */
17869179193Sjb			if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
17870179193Sjb				dtrace_buffer_polish(buf);
17871179193Sjb				sz = buf->dtb_size;
17872179193Sjb			}
17873179193Sjb
17874179193Sjb			if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) {
17875179193Sjb				mutex_exit(&dtrace_lock);
17876179193Sjb				return (EFAULT);
17877179193Sjb			}
17878179193Sjb
17879179193Sjb			desc.dtbd_size = sz;
17880179193Sjb			desc.dtbd_drops = buf->dtb_drops;
17881179193Sjb			desc.dtbd_errors = buf->dtb_errors;
17882179193Sjb			desc.dtbd_oldest = buf->dtb_xamot_offset;
17883250574Smarkj			desc.dtbd_timestamp = dtrace_gethrtime();
17884179193Sjb
17885179193Sjb			mutex_exit(&dtrace_lock);
17886179193Sjb
17887179193Sjb			if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
17888179193Sjb				return (EFAULT);
17889179193Sjb
17890179193Sjb			buf->dtb_flags |= DTRACEBUF_CONSUMED;
17891179193Sjb
17892179193Sjb			return (0);
17893179193Sjb		}
17894179193Sjb
17895179193Sjb		if (buf->dtb_tomax == NULL) {
17896179193Sjb			ASSERT(buf->dtb_xamot == NULL);
17897179193Sjb			mutex_exit(&dtrace_lock);
17898179193Sjb			return (ENOENT);
17899179193Sjb		}
17900179193Sjb
17901179193Sjb		cached = buf->dtb_tomax;
17902179193Sjb		ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
17903179193Sjb
17904179193Sjb		dtrace_xcall(desc.dtbd_cpu,
17905179193Sjb		    (dtrace_xcall_t)dtrace_buffer_switch, buf);
17906179193Sjb
17907179193Sjb		state->dts_errors += buf->dtb_xamot_errors;
17908179193Sjb
17909179193Sjb		/*
17910179193Sjb		 * If the buffers did not actually switch, then the cross call
17911179193Sjb		 * did not take place -- presumably because the given CPU is
17912179193Sjb		 * not in the ready set.  If this is the case, we'll return
17913179193Sjb		 * ENOENT.
17914179193Sjb		 */
17915179193Sjb		if (buf->dtb_tomax == cached) {
17916179193Sjb			ASSERT(buf->dtb_xamot != cached);
17917179193Sjb			mutex_exit(&dtrace_lock);
17918179193Sjb			return (ENOENT);
17919179193Sjb		}
17920179193Sjb
17921179193Sjb		ASSERT(cached == buf->dtb_xamot);
17922179193Sjb
17923179193Sjb		/*
17924179193Sjb		 * We have our snapshot; now copy it out.
17925179193Sjb		 */
17926179193Sjb		if (copyout(buf->dtb_xamot, desc.dtbd_data,
17927179193Sjb		    buf->dtb_xamot_offset) != 0) {
17928179193Sjb			mutex_exit(&dtrace_lock);
17929179193Sjb			return (EFAULT);
17930179193Sjb		}
17931179193Sjb
17932179193Sjb		desc.dtbd_size = buf->dtb_xamot_offset;
17933179193Sjb		desc.dtbd_drops = buf->dtb_xamot_drops;
17934179193Sjb		desc.dtbd_errors = buf->dtb_xamot_errors;
17935179193Sjb		desc.dtbd_oldest = 0;
17936250574Smarkj		desc.dtbd_timestamp = buf->dtb_switched;
17937179193Sjb
17938179193Sjb		mutex_exit(&dtrace_lock);
17939179193Sjb
17940179193Sjb		/*
17941179193Sjb		 * Finally, copy out the buffer description.
17942179193Sjb		 */
17943179193Sjb		if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
17944179193Sjb			return (EFAULT);
17945179193Sjb
17946179193Sjb		return (0);
17947179193Sjb	}
17948179193Sjb
17949179193Sjb	case DTRACEIOC_CONF: {
17950179193Sjb		dtrace_conf_t conf;
17951179193Sjb
17952179193Sjb		bzero(&conf, sizeof (conf));
17953179193Sjb		conf.dtc_difversion = DIF_VERSION;
17954179193Sjb		conf.dtc_difintregs = DIF_DIR_NREGS;
17955179193Sjb		conf.dtc_diftupregs = DIF_DTR_NREGS;
17956179193Sjb		conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
17957179193Sjb
17958179193Sjb		if (copyout(&conf, (void *)arg, sizeof (conf)) != 0)
17959179193Sjb			return (EFAULT);
17960179193Sjb
17961179193Sjb		return (0);
17962179193Sjb	}
17963179193Sjb
17964179193Sjb	case DTRACEIOC_STATUS: {
17965179193Sjb		dtrace_status_t stat;
17966179193Sjb		dtrace_dstate_t *dstate;
17967179193Sjb		int i, j;
17968179193Sjb		uint64_t nerrs;
17969179193Sjb
17970179193Sjb		/*
17971179193Sjb		 * See the comment in dtrace_state_deadman() for the reason
17972179193Sjb		 * for setting dts_laststatus to INT64_MAX before setting
17973179193Sjb		 * it to the correct value.
17974179193Sjb		 */
17975179193Sjb		state->dts_laststatus = INT64_MAX;
17976179193Sjb		dtrace_membar_producer();
17977179193Sjb		state->dts_laststatus = dtrace_gethrtime();
17978179193Sjb
17979179193Sjb		bzero(&stat, sizeof (stat));
17980179193Sjb
17981179193Sjb		mutex_enter(&dtrace_lock);
17982179193Sjb
17983179193Sjb		if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
17984179193Sjb			mutex_exit(&dtrace_lock);
17985179193Sjb			return (ENOENT);
17986179193Sjb		}
17987179193Sjb
17988179193Sjb		if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
17989179193Sjb			stat.dtst_exiting = 1;
17990179193Sjb
17991179193Sjb		nerrs = state->dts_errors;
17992179193Sjb		dstate = &state->dts_vstate.dtvs_dynvars;
17993179193Sjb
17994179193Sjb		for (i = 0; i < NCPU; i++) {
17995179193Sjb			dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i];
17996179193Sjb
17997179193Sjb			stat.dtst_dyndrops += dcpu->dtdsc_drops;
17998179193Sjb			stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
17999179193Sjb			stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
18000179193Sjb
18001179193Sjb			if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
18002179193Sjb				stat.dtst_filled++;
18003179193Sjb
18004179193Sjb			nerrs += state->dts_buffer[i].dtb_errors;
18005179193Sjb
18006179193Sjb			for (j = 0; j < state->dts_nspeculations; j++) {
18007179193Sjb				dtrace_speculation_t *spec;
18008179193Sjb				dtrace_buffer_t *buf;
18009179193Sjb
18010179193Sjb				spec = &state->dts_speculations[j];
18011179193Sjb				buf = &spec->dtsp_buffer[i];
18012179193Sjb				stat.dtst_specdrops += buf->dtb_xamot_drops;
18013179193Sjb			}
18014179193Sjb		}
18015179193Sjb
18016179193Sjb		stat.dtst_specdrops_busy = state->dts_speculations_busy;
18017179193Sjb		stat.dtst_specdrops_unavail = state->dts_speculations_unavail;
18018179193Sjb		stat.dtst_stkstroverflows = state->dts_stkstroverflows;
18019179193Sjb		stat.dtst_dblerrors = state->dts_dblerrors;
18020179193Sjb		stat.dtst_killed =
18021179193Sjb		    (state->dts_activity == DTRACE_ACTIVITY_KILLED);
18022179193Sjb		stat.dtst_errors = nerrs;
18023179193Sjb
18024179193Sjb		mutex_exit(&dtrace_lock);
18025179193Sjb
18026179193Sjb		if (copyout(&stat, (void *)arg, sizeof (stat)) != 0)
18027179193Sjb			return (EFAULT);
18028179193Sjb
18029179193Sjb		return (0);
18030179193Sjb	}
18031179193Sjb
18032179193Sjb	case DTRACEIOC_FORMAT: {
18033179193Sjb		dtrace_fmtdesc_t fmt;
18034179193Sjb		char *str;
18035179193Sjb		int len;
18036179193Sjb
18037179193Sjb		if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0)
18038179193Sjb			return (EFAULT);
18039179193Sjb
18040179193Sjb		mutex_enter(&dtrace_lock);
18041179193Sjb
18042179193Sjb		if (fmt.dtfd_format == 0 ||
18043179193Sjb		    fmt.dtfd_format > state->dts_nformats) {
18044179193Sjb			mutex_exit(&dtrace_lock);
18045179193Sjb			return (EINVAL);
18046179193Sjb		}
18047179193Sjb
18048179193Sjb		/*
18049179193Sjb		 * Format strings are allocated contiguously and they are
18050179193Sjb		 * never freed; if a format index is less than the number
18051179193Sjb		 * of formats, we can assert that the format map is non-NULL
18052179193Sjb		 * and that the format for the specified index is non-NULL.
18053179193Sjb		 */
18054179193Sjb		ASSERT(state->dts_formats != NULL);
18055179193Sjb		str = state->dts_formats[fmt.dtfd_format - 1];
18056179193Sjb		ASSERT(str != NULL);
18057179193Sjb
18058179193Sjb		len = strlen(str) + 1;
18059179193Sjb
18060179193Sjb		if (len > fmt.dtfd_length) {
18061179193Sjb			fmt.dtfd_length = len;
18062179193Sjb
18063179193Sjb			if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) {
18064179193Sjb				mutex_exit(&dtrace_lock);
18065179193Sjb				return (EINVAL);
18066179193Sjb			}
18067179193Sjb		} else {
18068179193Sjb			if (copyout(str, fmt.dtfd_string, len) != 0) {
18069179193Sjb				mutex_exit(&dtrace_lock);
18070179193Sjb				return (EINVAL);
18071179193Sjb			}
18072179193Sjb		}
18073179193Sjb
18074179193Sjb		mutex_exit(&dtrace_lock);
18075179193Sjb		return (0);
18076179193Sjb	}
18077179193Sjb
18078179193Sjb	default:
18079179193Sjb		break;
18080179193Sjb	}
18081179193Sjb
18082179193Sjb	return (ENOTTY);
18083179193Sjb}
18084179193Sjb
18085179193Sjb/*ARGSUSED*/
18086179193Sjbstatic int
18087179193Sjbdtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
18088179193Sjb{
18089179193Sjb	dtrace_state_t *state;
18090179193Sjb
18091179193Sjb	switch (cmd) {
18092179193Sjb	case DDI_DETACH:
18093179193Sjb		break;
18094179193Sjb
18095179193Sjb	case DDI_SUSPEND:
18096179193Sjb		return (DDI_SUCCESS);
18097179193Sjb
18098179193Sjb	default:
18099179193Sjb		return (DDI_FAILURE);
18100179193Sjb	}
18101179193Sjb
18102179193Sjb	mutex_enter(&cpu_lock);
18103179193Sjb	mutex_enter(&dtrace_provider_lock);
18104179193Sjb	mutex_enter(&dtrace_lock);
18105179193Sjb
18106179193Sjb	ASSERT(dtrace_opens == 0);
18107179193Sjb
18108179193Sjb	if (dtrace_helpers > 0) {
18109179193Sjb		mutex_exit(&dtrace_provider_lock);
18110179193Sjb		mutex_exit(&dtrace_lock);
18111179193Sjb		mutex_exit(&cpu_lock);
18112179193Sjb		return (DDI_FAILURE);
18113179193Sjb	}
18114179193Sjb
18115179193Sjb	if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) {
18116179193Sjb		mutex_exit(&dtrace_provider_lock);
18117179193Sjb		mutex_exit(&dtrace_lock);
18118179193Sjb		mutex_exit(&cpu_lock);
18119179193Sjb		return (DDI_FAILURE);
18120179193Sjb	}
18121179193Sjb
18122179193Sjb	dtrace_provider = NULL;
18123179193Sjb
18124179193Sjb	if ((state = dtrace_anon_grab()) != NULL) {
18125179193Sjb		/*
18126179193Sjb		 * If there were ECBs on this state, the provider should
18127179193Sjb		 * have not been allowed to detach; assert that there is
18128179193Sjb		 * none.
18129179193Sjb		 */
18130179193Sjb		ASSERT(state->dts_necbs == 0);
18131179193Sjb		dtrace_state_destroy(state);
18132179193Sjb
18133179193Sjb		/*
18134179193Sjb		 * If we're being detached with anonymous state, we need to
18135179193Sjb		 * indicate to the kernel debugger that DTrace is now inactive.
18136179193Sjb		 */
18137179193Sjb		(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
18138179193Sjb	}
18139179193Sjb
18140179193Sjb	bzero(&dtrace_anon, sizeof (dtrace_anon_t));
18141179193Sjb	unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
18142179193Sjb	dtrace_cpu_init = NULL;
18143179193Sjb	dtrace_helpers_cleanup = NULL;
18144179193Sjb	dtrace_helpers_fork = NULL;
18145179193Sjb	dtrace_cpustart_init = NULL;
18146179193Sjb	dtrace_cpustart_fini = NULL;
18147179193Sjb	dtrace_debugger_init = NULL;
18148179193Sjb	dtrace_debugger_fini = NULL;
18149179193Sjb	dtrace_modload = NULL;
18150179193Sjb	dtrace_modunload = NULL;
18151179193Sjb
18152267929Srpaulo	ASSERT(dtrace_getf == 0);
18153267929Srpaulo	ASSERT(dtrace_closef == NULL);
18154267929Srpaulo
18155179193Sjb	mutex_exit(&cpu_lock);
18156179193Sjb
18157179193Sjb	kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *));
18158179193Sjb	dtrace_probes = NULL;
18159179193Sjb	dtrace_nprobes = 0;
18160179193Sjb
18161179193Sjb	dtrace_hash_destroy(dtrace_bymod);
18162179193Sjb	dtrace_hash_destroy(dtrace_byfunc);
18163179193Sjb	dtrace_hash_destroy(dtrace_byname);
18164179193Sjb	dtrace_bymod = NULL;
18165179193Sjb	dtrace_byfunc = NULL;
18166179193Sjb	dtrace_byname = NULL;
18167179193Sjb
18168179193Sjb	kmem_cache_destroy(dtrace_state_cache);
18169179193Sjb	vmem_destroy(dtrace_minor);
18170179193Sjb	vmem_destroy(dtrace_arena);
18171179193Sjb
18172179193Sjb	if (dtrace_toxrange != NULL) {
18173179193Sjb		kmem_free(dtrace_toxrange,
18174179193Sjb		    dtrace_toxranges_max * sizeof (dtrace_toxrange_t));
18175179193Sjb		dtrace_toxrange = NULL;
18176179193Sjb		dtrace_toxranges = 0;
18177179193Sjb		dtrace_toxranges_max = 0;
18178179193Sjb	}
18179179193Sjb
18180179193Sjb	ddi_remove_minor_node(dtrace_devi, NULL);
18181179193Sjb	dtrace_devi = NULL;
18182179193Sjb
18183179193Sjb	ddi_soft_state_fini(&dtrace_softstate);
18184179193Sjb
18185179193Sjb	ASSERT(dtrace_vtime_references == 0);
18186179193Sjb	ASSERT(dtrace_opens == 0);
18187179193Sjb	ASSERT(dtrace_retained == NULL);
18188179193Sjb
18189179193Sjb	mutex_exit(&dtrace_lock);
18190179193Sjb	mutex_exit(&dtrace_provider_lock);
18191179193Sjb
18192179193Sjb	/*
18193179193Sjb	 * We don't destroy the task queue until after we have dropped our
18194179193Sjb	 * locks (taskq_destroy() may block on running tasks).  To prevent
18195179193Sjb	 * attempting to do work after we have effectively detached but before
18196179193Sjb	 * the task queue has been destroyed, all tasks dispatched via the
18197179193Sjb	 * task queue must check that DTrace is still attached before
18198179193Sjb	 * performing any operation.
18199179193Sjb	 */
18200179193Sjb	taskq_destroy(dtrace_taskq);
18201179193Sjb	dtrace_taskq = NULL;
18202179193Sjb
18203179193Sjb	return (DDI_SUCCESS);
18204179193Sjb}
18205179198Sjb#endif
18206179193Sjb
18207277300Ssmh#ifdef illumos
18208179193Sjb/*ARGSUSED*/
18209179193Sjbstatic int
18210179193Sjbdtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
18211179193Sjb{
18212179193Sjb	int error;
18213179193Sjb
18214179193Sjb	switch (infocmd) {
18215179193Sjb	case DDI_INFO_DEVT2DEVINFO:
18216179193Sjb		*result = (void *)dtrace_devi;
18217179193Sjb		error = DDI_SUCCESS;
18218179193Sjb		break;
18219179193Sjb	case DDI_INFO_DEVT2INSTANCE:
18220179193Sjb		*result = (void *)0;
18221179193Sjb		error = DDI_SUCCESS;
18222179193Sjb		break;
18223179193Sjb	default:
18224179193Sjb		error = DDI_FAILURE;
18225179193Sjb	}
18226179193Sjb	return (error);
18227179193Sjb}
18228179198Sjb#endif
18229179193Sjb
18230277300Ssmh#ifdef illumos
18231179193Sjbstatic struct cb_ops dtrace_cb_ops = {
18232179193Sjb	dtrace_open,		/* open */
18233179193Sjb	dtrace_close,		/* close */
18234179193Sjb	nulldev,		/* strategy */
18235179193Sjb	nulldev,		/* print */
18236179193Sjb	nodev,			/* dump */
18237179193Sjb	nodev,			/* read */
18238179193Sjb	nodev,			/* write */
18239179193Sjb	dtrace_ioctl,		/* ioctl */
18240179193Sjb	nodev,			/* devmap */
18241179193Sjb	nodev,			/* mmap */
18242179193Sjb	nodev,			/* segmap */
18243179193Sjb	nochpoll,		/* poll */
18244179193Sjb	ddi_prop_op,		/* cb_prop_op */
18245179193Sjb	0,			/* streamtab  */
18246179193Sjb	D_NEW | D_MP		/* Driver compatibility flag */
18247179193Sjb};
18248179193Sjb
18249179193Sjbstatic struct dev_ops dtrace_ops = {
18250179193Sjb	DEVO_REV,		/* devo_rev */
18251179193Sjb	0,			/* refcnt */
18252179193Sjb	dtrace_info,		/* get_dev_info */
18253179193Sjb	nulldev,		/* identify */
18254179193Sjb	nulldev,		/* probe */
18255179193Sjb	dtrace_attach,		/* attach */
18256179193Sjb	dtrace_detach,		/* detach */
18257179193Sjb	nodev,			/* reset */
18258179193Sjb	&dtrace_cb_ops,		/* driver operations */
18259179193Sjb	NULL,			/* bus operations */
18260179193Sjb	nodev			/* dev power */
18261179193Sjb};
18262179193Sjb
18263179193Sjbstatic struct modldrv modldrv = {
18264179193Sjb	&mod_driverops,		/* module type (this is a pseudo driver) */
18265179193Sjb	"Dynamic Tracing",	/* name of module */
18266179193Sjb	&dtrace_ops,		/* driver ops */
18267179193Sjb};
18268179193Sjb
18269179193Sjbstatic struct modlinkage modlinkage = {
18270179193Sjb	MODREV_1,
18271179193Sjb	(void *)&modldrv,
18272179193Sjb	NULL
18273179193Sjb};
18274179193Sjb
18275179193Sjbint
18276179193Sjb_init(void)
18277179193Sjb{
18278179193Sjb	return (mod_install(&modlinkage));
18279179193Sjb}
18280179193Sjb
18281179193Sjbint
18282179193Sjb_info(struct modinfo *modinfop)
18283179193Sjb{
18284179193Sjb	return (mod_info(&modlinkage, modinfop));
18285179193Sjb}
18286179193Sjb
18287179193Sjbint
18288179193Sjb_fini(void)
18289179193Sjb{
18290179193Sjb	return (mod_remove(&modlinkage));
18291179193Sjb}
18292179198Sjb#else
18293179198Sjb
18294179198Sjbstatic d_ioctl_t	dtrace_ioctl;
18295211608Srpaulostatic d_ioctl_t	dtrace_ioctl_helper;
18296179198Sjbstatic void		dtrace_load(void *);
18297179198Sjbstatic int		dtrace_unload(void);
18298184698Srodrigcstatic struct cdev	*dtrace_dev;
18299211608Srpaulostatic struct cdev	*helper_dev;
18300179198Sjb
18301179198Sjbvoid dtrace_invop_init(void);
18302179198Sjbvoid dtrace_invop_uninit(void);
18303179198Sjb
18304179198Sjbstatic struct cdevsw dtrace_cdevsw = {
18305179198Sjb	.d_version	= D_VERSION,
18306179198Sjb	.d_ioctl	= dtrace_ioctl,
18307179198Sjb	.d_open		= dtrace_open,
18308179198Sjb	.d_name		= "dtrace",
18309179198Sjb};
18310179198Sjb
18311211608Srpaulostatic struct cdevsw helper_cdevsw = {
18312211608Srpaulo	.d_version	= D_VERSION,
18313211608Srpaulo	.d_ioctl	= dtrace_ioctl_helper,
18314211608Srpaulo	.d_name		= "helper",
18315211608Srpaulo};
18316211608Srpaulo
18317179198Sjb#include <dtrace_anon.c>
18318179198Sjb#include <dtrace_ioctl.c>
18319179198Sjb#include <dtrace_load.c>
18320179198Sjb#include <dtrace_modevent.c>
18321179198Sjb#include <dtrace_sysctl.c>
18322179198Sjb#include <dtrace_unload.c>
18323179198Sjb#include <dtrace_vtime.c>
18324179198Sjb#include <dtrace_hacks.c>
18325179198Sjb#include <dtrace_isa.c>
18326179198Sjb
18327179198SjbSYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL);
18328179198SjbSYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL);
18329179198SjbSYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL);
18330179198Sjb
18331179198SjbDEV_MODULE(dtrace, dtrace_modevent, NULL);
18332179198SjbMODULE_VERSION(dtrace, 1);
18333179198SjbMODULE_DEPEND(dtrace, opensolaris, 1, 1, 1);
18334179198Sjb#endif
18335