xref: /freebsd/sys/cddl/dev/dtrace/dtrace_ioctl.c (revision f5f7c05209ca2c3748fd8b27c5e80ffad49120eb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  * $FreeBSD$
22  *
23  */
24 
25 static int dtrace_verbose_ioctl;
26 SYSCTL_INT(_debug_dtrace, OID_AUTO, verbose_ioctl, CTLFLAG_RW, &dtrace_verbose_ioctl, 0, "");
27 
28 #define DTRACE_IOCTL_PRINTF(fmt, ...)	if (dtrace_verbose_ioctl) printf(fmt, ## __VA_ARGS__ )
29 
30 static int
31 dtrace_ioctl_helper(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
32     struct thread *td)
33 {
34 	int rval;
35 	dof_helper_t *dhp = NULL;
36 	dof_hdr_t *dof = NULL;
37 
38 	switch (cmd) {
39 	case DTRACEHIOC_ADDDOF:
40 		dhp = (dof_helper_t *)addr;
41 		/* XXX all because dofhp_dof is 64 bit */
42 		addr = (caddr_t)(vm_offset_t)dhp->dofhp_dof;
43 		/* FALLTHROUGH */
44 	case DTRACEHIOC_ADD:
45 		dof = dtrace_dof_copyin((intptr_t)addr, &rval);
46 
47 		if (dof == NULL)
48 			return (rval);
49 
50 		mutex_enter(&dtrace_lock);
51 		if ((rval = dtrace_helper_slurp((dof_hdr_t *)dof, dhp)) != -1) {
52 			if (dhp) {
53 				dhp->gen = rval;
54 				copyout(dhp, addr, sizeof(*dhp));
55 			}
56 			rval = 0;
57 		} else {
58 			rval = EINVAL;
59 		}
60 		mutex_exit(&dtrace_lock);
61 		return (rval);
62 	case DTRACEHIOC_REMOVE:
63 		mutex_enter(&dtrace_lock);
64 		rval = dtrace_helper_destroygen((int)*addr);
65 		mutex_exit(&dtrace_lock);
66 
67 		return (rval);
68 	default:
69 		break;
70 	}
71 
72 	return (ENOTTY);
73 }
74 
75 /* ARGSUSED */
76 static int
77 dtrace_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
78     int flags __unused, struct thread *td)
79 {
80 #if __FreeBSD_version < 800039
81 	dtrace_state_t *state = dev->si_drv1;
82 #else
83 	dtrace_state_t *state;
84 	devfs_get_cdevpriv((void **) &state);
85 #endif
86 	int error = 0;
87 	if (state == NULL)
88 		return (EINVAL);
89 
90 	if (state->dts_anon) {
91 		ASSERT(dtrace_anon.dta_state == NULL);
92 		state = state->dts_anon;
93 	}
94 
95 	switch (cmd) {
96 	case DTRACEIOC_AGGDESC: {
97 		dtrace_aggdesc_t **paggdesc = (dtrace_aggdesc_t **) addr;
98 		dtrace_aggdesc_t aggdesc;
99 		dtrace_action_t *act;
100 		dtrace_aggregation_t *agg;
101 		int nrecs;
102 		uint32_t offs;
103 		dtrace_recdesc_t *lrec;
104 		void *buf;
105 		size_t size;
106 		uintptr_t dest;
107 
108 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_AGGDESC\n",__func__,__LINE__);
109 
110 		if (copyin((void *) *paggdesc, &aggdesc, sizeof (aggdesc)) != 0)
111 			return (EFAULT);
112 
113 		mutex_enter(&dtrace_lock);
114 
115 		if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) {
116 			mutex_exit(&dtrace_lock);
117 			return (EINVAL);
118 		}
119 
120 		aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid;
121 
122 		nrecs = aggdesc.dtagd_nrecs;
123 		aggdesc.dtagd_nrecs = 0;
124 
125 		offs = agg->dtag_base;
126 		lrec = &agg->dtag_action.dta_rec;
127 		aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs;
128 
129 		for (act = agg->dtag_first; ; act = act->dta_next) {
130 			ASSERT(act->dta_intuple ||
131 			    DTRACEACT_ISAGG(act->dta_kind));
132 
133 			/*
134 			 * If this action has a record size of zero, it
135 			 * denotes an argument to the aggregating action.
136 			 * Because the presence of this record doesn't (or
137 			 * shouldn't) affect the way the data is interpreted,
138 			 * we don't copy it out to save user-level the
139 			 * confusion of dealing with a zero-length record.
140 			 */
141 			if (act->dta_rec.dtrd_size == 0) {
142 				ASSERT(agg->dtag_hasarg);
143 				continue;
144 			}
145 
146 			aggdesc.dtagd_nrecs++;
147 
148 			if (act == &agg->dtag_action)
149 				break;
150 		}
151 
152 		/*
153 		 * Now that we have the size, we need to allocate a temporary
154 		 * buffer in which to store the complete description.  We need
155 		 * the temporary buffer to be able to drop dtrace_lock()
156 		 * across the copyout(), below.
157 		 */
158 		size = sizeof (dtrace_aggdesc_t) +
159 		    (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t));
160 
161 		buf = kmem_alloc(size, KM_SLEEP);
162 		dest = (uintptr_t)buf;
163 
164 		bcopy(&aggdesc, (void *)dest, sizeof (aggdesc));
165 		dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]);
166 
167 		for (act = agg->dtag_first; ; act = act->dta_next) {
168 			dtrace_recdesc_t rec = act->dta_rec;
169 
170 			/*
171 			 * See the comment in the above loop for why we pass
172 			 * over zero-length records.
173 			 */
174 			if (rec.dtrd_size == 0) {
175 				ASSERT(agg->dtag_hasarg);
176 				continue;
177 			}
178 
179 			if (nrecs-- == 0)
180 				break;
181 
182 			rec.dtrd_offset -= offs;
183 			bcopy(&rec, (void *)dest, sizeof (rec));
184 			dest += sizeof (dtrace_recdesc_t);
185 
186 			if (act == &agg->dtag_action)
187 				break;
188 		}
189 
190 		mutex_exit(&dtrace_lock);
191 
192 		if (copyout(buf, (void *) *paggdesc, dest - (uintptr_t)buf) != 0) {
193 			kmem_free(buf, size);
194 			return (EFAULT);
195 		}
196 
197 		kmem_free(buf, size);
198 		return (0);
199 	}
200 	case DTRACEIOC_AGGSNAP:
201 	case DTRACEIOC_BUFSNAP: {
202 		dtrace_bufdesc_t **pdesc = (dtrace_bufdesc_t **) addr;
203 		dtrace_bufdesc_t desc;
204 		caddr_t cached;
205 		dtrace_buffer_t *buf;
206 
207 		dtrace_debug_output();
208 
209 		if (copyin((void *) *pdesc, &desc, sizeof (desc)) != 0)
210 			return (EFAULT);
211 
212 		DTRACE_IOCTL_PRINTF("%s(%d): %s curcpu %d cpu %d\n",
213 		    __func__,__LINE__,
214 		    cmd == DTRACEIOC_AGGSNAP ?
215 		    "DTRACEIOC_AGGSNAP":"DTRACEIOC_BUFSNAP",
216 		    curcpu, desc.dtbd_cpu);
217 
218 		if (desc.dtbd_cpu >= NCPU)
219 			return (ENOENT);
220 		if (pcpu_find(desc.dtbd_cpu) == NULL)
221 			return (ENOENT);
222 
223 		mutex_enter(&dtrace_lock);
224 
225 		if (cmd == DTRACEIOC_BUFSNAP) {
226 			buf = &state->dts_buffer[desc.dtbd_cpu];
227 		} else {
228 			buf = &state->dts_aggbuffer[desc.dtbd_cpu];
229 		}
230 
231 		if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) {
232 			size_t sz = buf->dtb_offset;
233 
234 			if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) {
235 				mutex_exit(&dtrace_lock);
236 				return (EBUSY);
237 			}
238 
239 			/*
240 			 * If this buffer has already been consumed, we're
241 			 * going to indicate that there's nothing left here
242 			 * to consume.
243 			 */
244 			if (buf->dtb_flags & DTRACEBUF_CONSUMED) {
245 				mutex_exit(&dtrace_lock);
246 
247 				desc.dtbd_size = 0;
248 				desc.dtbd_drops = 0;
249 				desc.dtbd_errors = 0;
250 				desc.dtbd_oldest = 0;
251 				sz = sizeof (desc);
252 
253 				if (copyout(&desc, (void *) *pdesc, sz) != 0)
254 					return (EFAULT);
255 
256 				return (0);
257 			}
258 
259 			/*
260 			 * If this is a ring buffer that has wrapped, we want
261 			 * to copy the whole thing out.
262 			 */
263 			if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
264 				dtrace_buffer_polish(buf);
265 				sz = buf->dtb_size;
266 			}
267 
268 			if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) {
269 				mutex_exit(&dtrace_lock);
270 				return (EFAULT);
271 			}
272 
273 			desc.dtbd_size = sz;
274 			desc.dtbd_drops = buf->dtb_drops;
275 			desc.dtbd_errors = buf->dtb_errors;
276 			desc.dtbd_oldest = buf->dtb_xamot_offset;
277 
278 			mutex_exit(&dtrace_lock);
279 
280 			if (copyout(&desc, (void *) *pdesc, sizeof (desc)) != 0)
281 				return (EFAULT);
282 
283 			buf->dtb_flags |= DTRACEBUF_CONSUMED;
284 
285 			return (0);
286 		}
287 
288 		if (buf->dtb_tomax == NULL) {
289 			ASSERT(buf->dtb_xamot == NULL);
290 			mutex_exit(&dtrace_lock);
291 			return (ENOENT);
292 		}
293 
294 		cached = buf->dtb_tomax;
295 		ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
296 
297 		dtrace_xcall(desc.dtbd_cpu,
298 		    (dtrace_xcall_t)dtrace_buffer_switch, buf);
299 
300 		state->dts_errors += buf->dtb_xamot_errors;
301 
302 		/*
303 		 * If the buffers did not actually switch, then the cross call
304 		 * did not take place -- presumably because the given CPU is
305 		 * not in the ready set.  If this is the case, we'll return
306 		 * ENOENT.
307 		 */
308 		if (buf->dtb_tomax == cached) {
309 			ASSERT(buf->dtb_xamot != cached);
310 			mutex_exit(&dtrace_lock);
311 			return (ENOENT);
312 		}
313 
314 		ASSERT(cached == buf->dtb_xamot);
315 
316 		DTRACE_IOCTL_PRINTF("%s(%d): copyout the buffer snapshot\n",__func__,__LINE__);
317 
318 		/*
319 		 * We have our snapshot; now copy it out.
320 		 */
321 		if (copyout(buf->dtb_xamot, desc.dtbd_data,
322 		    buf->dtb_xamot_offset) != 0) {
323 			mutex_exit(&dtrace_lock);
324 			return (EFAULT);
325 		}
326 
327 		desc.dtbd_size = buf->dtb_xamot_offset;
328 		desc.dtbd_drops = buf->dtb_xamot_drops;
329 		desc.dtbd_errors = buf->dtb_xamot_errors;
330 		desc.dtbd_oldest = 0;
331 
332 		mutex_exit(&dtrace_lock);
333 
334 		DTRACE_IOCTL_PRINTF("%s(%d): copyout buffer desc: size %zd drops %lu errors %lu\n",__func__,__LINE__,(size_t) desc.dtbd_size,(u_long) desc.dtbd_drops,(u_long) desc.dtbd_errors);
335 
336 		/*
337 		 * Finally, copy out the buffer description.
338 		 */
339 		if (copyout(&desc, (void *) *pdesc, sizeof (desc)) != 0)
340 			return (EFAULT);
341 
342 		return (0);
343 	}
344 	case DTRACEIOC_CONF: {
345 		dtrace_conf_t conf;
346 
347 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_CONF\n",__func__,__LINE__);
348 
349 		bzero(&conf, sizeof (conf));
350 		conf.dtc_difversion = DIF_VERSION;
351 		conf.dtc_difintregs = DIF_DIR_NREGS;
352 		conf.dtc_diftupregs = DIF_DTR_NREGS;
353 		conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
354 
355 		*((dtrace_conf_t *) addr) = conf;
356 
357 		return (0);
358 	}
359 	case DTRACEIOC_DOFGET: {
360 		dof_hdr_t **pdof = (dof_hdr_t **) addr;
361 		dof_hdr_t hdr, *dof = *pdof;
362 		int rval;
363 		uint64_t len;
364 
365 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_DOFGET\n",__func__,__LINE__);
366 
367 		if (copyin((void *)dof, &hdr, sizeof (hdr)) != 0)
368 			return (EFAULT);
369 
370 		mutex_enter(&dtrace_lock);
371 		dof = dtrace_dof_create(state);
372 		mutex_exit(&dtrace_lock);
373 
374 		len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
375 		rval = copyout(dof, (void *) *pdof, len);
376 		dtrace_dof_destroy(dof);
377 
378 		return (rval == 0 ? 0 : EFAULT);
379 	}
380 	case DTRACEIOC_ENABLE: {
381 		dof_hdr_t *dof = NULL;
382 		dtrace_enabling_t *enab = NULL;
383 		dtrace_vstate_t *vstate;
384 		int err = 0;
385 		int rval;
386 		dtrace_enable_io_t *p = (dtrace_enable_io_t *) addr;
387 
388 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_ENABLE\n",__func__,__LINE__);
389 
390 		/*
391 		 * If a NULL argument has been passed, we take this as our
392 		 * cue to reevaluate our enablings.
393 		 */
394 		if (p->dof == NULL) {
395 			dtrace_enabling_matchall();
396 
397 			return (0);
398 		}
399 
400 		if ((dof = dtrace_dof_copyin((uintptr_t) p->dof, &rval)) == NULL)
401 			return (EINVAL);
402 
403 		mutex_enter(&cpu_lock);
404 		mutex_enter(&dtrace_lock);
405 		vstate = &state->dts_vstate;
406 
407 		if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
408 			mutex_exit(&dtrace_lock);
409 			mutex_exit(&cpu_lock);
410 			dtrace_dof_destroy(dof);
411 			return (EBUSY);
412 		}
413 
414 		if (dtrace_dof_slurp(dof, vstate, td->td_ucred, &enab, 0, B_TRUE) != 0) {
415 			mutex_exit(&dtrace_lock);
416 			mutex_exit(&cpu_lock);
417 			dtrace_dof_destroy(dof);
418 			return (EINVAL);
419 		}
420 
421 		if ((rval = dtrace_dof_options(dof, state)) != 0) {
422 			dtrace_enabling_destroy(enab);
423 			mutex_exit(&dtrace_lock);
424 			mutex_exit(&cpu_lock);
425 			dtrace_dof_destroy(dof);
426 			return (rval);
427 		}
428 
429 		if ((err = dtrace_enabling_match(enab, &p->n_matched)) == 0) {
430 			err = dtrace_enabling_retain(enab);
431 		} else {
432 			dtrace_enabling_destroy(enab);
433 		}
434 
435 		mutex_exit(&cpu_lock);
436 		mutex_exit(&dtrace_lock);
437 		dtrace_dof_destroy(dof);
438 
439 		return (err);
440 	}
441 	case DTRACEIOC_EPROBE: {
442 		dtrace_eprobedesc_t **pepdesc = (dtrace_eprobedesc_t **) addr;
443 		dtrace_eprobedesc_t epdesc;
444 		dtrace_ecb_t *ecb;
445 		dtrace_action_t *act;
446 		void *buf;
447 		size_t size;
448 		uintptr_t dest;
449 		int nrecs;
450 
451 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_EPROBE\n",__func__,__LINE__);
452 
453 		if (copyin((void *)*pepdesc, &epdesc, sizeof (epdesc)) != 0)
454 			return (EFAULT);
455 
456 		mutex_enter(&dtrace_lock);
457 
458 		if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
459 			mutex_exit(&dtrace_lock);
460 			return (EINVAL);
461 		}
462 
463 		if (ecb->dte_probe == NULL) {
464 			mutex_exit(&dtrace_lock);
465 			return (EINVAL);
466 		}
467 
468 		epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
469 		epdesc.dtepd_uarg = ecb->dte_uarg;
470 		epdesc.dtepd_size = ecb->dte_size;
471 
472 		nrecs = epdesc.dtepd_nrecs;
473 		epdesc.dtepd_nrecs = 0;
474 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
475 			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
476 				continue;
477 
478 			epdesc.dtepd_nrecs++;
479 		}
480 
481 		/*
482 		 * Now that we have the size, we need to allocate a temporary
483 		 * buffer in which to store the complete description.  We need
484 		 * the temporary buffer to be able to drop dtrace_lock()
485 		 * across the copyout(), below.
486 		 */
487 		size = sizeof (dtrace_eprobedesc_t) +
488 		    (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
489 
490 		buf = kmem_alloc(size, KM_SLEEP);
491 		dest = (uintptr_t)buf;
492 
493 		bcopy(&epdesc, (void *)dest, sizeof (epdesc));
494 		dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
495 
496 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
497 			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
498 				continue;
499 
500 			if (nrecs-- == 0)
501 				break;
502 
503 			bcopy(&act->dta_rec, (void *)dest,
504 			    sizeof (dtrace_recdesc_t));
505 			dest += sizeof (dtrace_recdesc_t);
506 		}
507 
508 		mutex_exit(&dtrace_lock);
509 
510 		if (copyout(buf, (void *) *pepdesc, dest - (uintptr_t)buf) != 0) {
511 			kmem_free(buf, size);
512 			return (EFAULT);
513 		}
514 
515 		kmem_free(buf, size);
516 		return (0);
517 	}
518 	case DTRACEIOC_FORMAT: {
519 		dtrace_fmtdesc_t *fmt = (dtrace_fmtdesc_t *) addr;
520 		char *str;
521 		int len;
522 
523 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_FORMAT\n",__func__,__LINE__);
524 
525 		mutex_enter(&dtrace_lock);
526 
527 		if (fmt->dtfd_format == 0 ||
528 		    fmt->dtfd_format > state->dts_nformats) {
529 			mutex_exit(&dtrace_lock);
530 			return (EINVAL);
531 		}
532 
533 		/*
534 		 * Format strings are allocated contiguously and they are
535 		 * never freed; if a format index is less than the number
536 		 * of formats, we can assert that the format map is non-NULL
537 		 * and that the format for the specified index is non-NULL.
538 		 */
539 		ASSERT(state->dts_formats != NULL);
540 		str = state->dts_formats[fmt->dtfd_format - 1];
541 		ASSERT(str != NULL);
542 
543 		len = strlen(str) + 1;
544 
545 		if (len > fmt->dtfd_length) {
546 			fmt->dtfd_length = len;
547 		} else {
548 			if (copyout(str, fmt->dtfd_string, len) != 0) {
549 				mutex_exit(&dtrace_lock);
550 				return (EINVAL);
551 			}
552 		}
553 
554 		mutex_exit(&dtrace_lock);
555 		return (0);
556 	}
557 	case DTRACEIOC_GO: {
558 		int rval;
559 		processorid_t *cpuid = (processorid_t *) addr;
560 
561 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_GO\n",__func__,__LINE__);
562 
563 		rval = dtrace_state_go(state, cpuid);
564 
565 		return (rval);
566 	}
567 	case DTRACEIOC_PROBEARG: {
568 		dtrace_argdesc_t *desc = (dtrace_argdesc_t *) addr;
569 		dtrace_probe_t *probe;
570 		dtrace_provider_t *prov;
571 
572 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_PROBEARG\n",__func__,__LINE__);
573 
574 		if (desc->dtargd_id == DTRACE_IDNONE)
575 			return (EINVAL);
576 
577 		if (desc->dtargd_ndx == DTRACE_ARGNONE)
578 			return (EINVAL);
579 
580 		mutex_enter(&dtrace_provider_lock);
581 		mutex_enter(&mod_lock);
582 		mutex_enter(&dtrace_lock);
583 
584 		if (desc->dtargd_id > dtrace_nprobes) {
585 			mutex_exit(&dtrace_lock);
586 			mutex_exit(&mod_lock);
587 			mutex_exit(&dtrace_provider_lock);
588 			return (EINVAL);
589 		}
590 
591 		if ((probe = dtrace_probes[desc->dtargd_id - 1]) == NULL) {
592 			mutex_exit(&dtrace_lock);
593 			mutex_exit(&mod_lock);
594 			mutex_exit(&dtrace_provider_lock);
595 			return (EINVAL);
596 		}
597 
598 		mutex_exit(&dtrace_lock);
599 
600 		prov = probe->dtpr_provider;
601 
602 		if (prov->dtpv_pops.dtps_getargdesc == NULL) {
603 			/*
604 			 * There isn't any typed information for this probe.
605 			 * Set the argument number to DTRACE_ARGNONE.
606 			 */
607 			desc->dtargd_ndx = DTRACE_ARGNONE;
608 		} else {
609 			desc->dtargd_native[0] = '\0';
610 			desc->dtargd_xlate[0] = '\0';
611 			desc->dtargd_mapping = desc->dtargd_ndx;
612 
613 			prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
614 			    probe->dtpr_id, probe->dtpr_arg, desc);
615 		}
616 
617 		mutex_exit(&mod_lock);
618 		mutex_exit(&dtrace_provider_lock);
619 
620 		return (0);
621 	}
622 	case DTRACEIOC_PROBEMATCH:
623 	case DTRACEIOC_PROBES: {
624 		dtrace_probedesc_t *p_desc = (dtrace_probedesc_t *) addr;
625 		dtrace_probe_t *probe = NULL;
626 		dtrace_probekey_t pkey;
627 		dtrace_id_t i;
628 		int m = 0;
629 		uint32_t priv = 0;
630 		uid_t uid = 0;
631 		zoneid_t zoneid = 0;
632 
633 		DTRACE_IOCTL_PRINTF("%s(%d): %s\n",__func__,__LINE__,
634 		    cmd == DTRACEIOC_PROBEMATCH ?
635 		    "DTRACEIOC_PROBEMATCH":"DTRACEIOC_PROBES");
636 
637 		p_desc->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
638 		p_desc->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
639 		p_desc->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
640 		p_desc->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
641 
642 		/*
643 		 * Before we attempt to match this probe, we want to give
644 		 * all providers the opportunity to provide it.
645 		 */
646 		if (p_desc->dtpd_id == DTRACE_IDNONE) {
647 			mutex_enter(&dtrace_provider_lock);
648 			dtrace_probe_provide(p_desc, NULL);
649 			mutex_exit(&dtrace_provider_lock);
650 			p_desc->dtpd_id++;
651 		}
652 
653 		if (cmd == DTRACEIOC_PROBEMATCH)  {
654 			dtrace_probekey(p_desc, &pkey);
655 			pkey.dtpk_id = DTRACE_IDNONE;
656 		}
657 
658 		dtrace_cred2priv(td->td_ucred, &priv, &uid, &zoneid);
659 
660 		mutex_enter(&dtrace_lock);
661 
662 		if (cmd == DTRACEIOC_PROBEMATCH) {
663 			for (i = p_desc->dtpd_id; i <= dtrace_nprobes; i++) {
664 				if ((probe = dtrace_probes[i - 1]) != NULL &&
665 				    (m = dtrace_match_probe(probe, &pkey,
666 				    priv, uid, zoneid)) != 0)
667 					break;
668 			}
669 
670 			if (m < 0) {
671 				mutex_exit(&dtrace_lock);
672 				return (EINVAL);
673 			}
674 
675 		} else {
676 			for (i = p_desc->dtpd_id; i <= dtrace_nprobes; i++) {
677 				if ((probe = dtrace_probes[i - 1]) != NULL &&
678 				    dtrace_match_priv(probe, priv, uid, zoneid))
679 					break;
680 			}
681 		}
682 
683 		if (probe == NULL) {
684 			mutex_exit(&dtrace_lock);
685 			return (ESRCH);
686 		}
687 
688 		dtrace_probe_description(probe, p_desc);
689 		mutex_exit(&dtrace_lock);
690 
691 		return (0);
692 	}
693 	case DTRACEIOC_PROVIDER: {
694 		dtrace_providerdesc_t *pvd = (dtrace_providerdesc_t *) addr;
695 		dtrace_provider_t *pvp;
696 
697 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_PROVIDER\n",__func__,__LINE__);
698 
699 		pvd->dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
700 		mutex_enter(&dtrace_provider_lock);
701 
702 		for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
703 			if (strcmp(pvp->dtpv_name, pvd->dtvd_name) == 0)
704 				break;
705 		}
706 
707 		mutex_exit(&dtrace_provider_lock);
708 
709 		if (pvp == NULL)
710 			return (ESRCH);
711 
712 		bcopy(&pvp->dtpv_priv, &pvd->dtvd_priv, sizeof (dtrace_ppriv_t));
713 		bcopy(&pvp->dtpv_attr, &pvd->dtvd_attr, sizeof (dtrace_pattr_t));
714 
715 		return (0);
716 	}
717 	case DTRACEIOC_REPLICATE: {
718 		dtrace_repldesc_t *desc = (dtrace_repldesc_t *) addr;
719 		dtrace_probedesc_t *match = &desc->dtrpd_match;
720 		dtrace_probedesc_t *create = &desc->dtrpd_create;
721 		int err;
722 
723 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_REPLICATE\n",__func__,__LINE__);
724 
725 		match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
726 		match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
727 		match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
728 		match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
729 
730 		create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
731 		create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
732 		create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
733 		create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
734 
735 		mutex_enter(&dtrace_lock);
736 		err = dtrace_enabling_replicate(state, match, create);
737 		mutex_exit(&dtrace_lock);
738 
739 		return (err);
740 	}
741 	case DTRACEIOC_STATUS: {
742 		dtrace_status_t *stat = (dtrace_status_t *) addr;
743 		dtrace_dstate_t *dstate;
744 		int i, j;
745 		uint64_t nerrs;
746 
747 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_STATUS\n",__func__,__LINE__);
748 
749 		/*
750 		 * See the comment in dtrace_state_deadman() for the reason
751 		 * for setting dts_laststatus to INT64_MAX before setting
752 		 * it to the correct value.
753 		 */
754 		state->dts_laststatus = INT64_MAX;
755 		dtrace_membar_producer();
756 		state->dts_laststatus = dtrace_gethrtime();
757 
758 		bzero(stat, sizeof (*stat));
759 
760 		mutex_enter(&dtrace_lock);
761 
762 		if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
763 			mutex_exit(&dtrace_lock);
764 			return (ENOENT);
765 		}
766 
767 		if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
768 			stat->dtst_exiting = 1;
769 
770 		nerrs = state->dts_errors;
771 		dstate = &state->dts_vstate.dtvs_dynvars;
772 
773 		for (i = 0; i < NCPU; i++) {
774 #if !defined(sun)
775 			if (pcpu_find(i) == NULL)
776 				continue;
777 #endif
778 			dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i];
779 
780 			stat->dtst_dyndrops += dcpu->dtdsc_drops;
781 			stat->dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
782 			stat->dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
783 
784 			if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
785 				stat->dtst_filled++;
786 
787 			nerrs += state->dts_buffer[i].dtb_errors;
788 
789 			for (j = 0; j < state->dts_nspeculations; j++) {
790 				dtrace_speculation_t *spec;
791 				dtrace_buffer_t *buf;
792 
793 				spec = &state->dts_speculations[j];
794 				buf = &spec->dtsp_buffer[i];
795 				stat->dtst_specdrops += buf->dtb_xamot_drops;
796 			}
797 		}
798 
799 		stat->dtst_specdrops_busy = state->dts_speculations_busy;
800 		stat->dtst_specdrops_unavail = state->dts_speculations_unavail;
801 		stat->dtst_stkstroverflows = state->dts_stkstroverflows;
802 		stat->dtst_dblerrors = state->dts_dblerrors;
803 		stat->dtst_killed =
804 		    (state->dts_activity == DTRACE_ACTIVITY_KILLED);
805 		stat->dtst_errors = nerrs;
806 
807 		mutex_exit(&dtrace_lock);
808 
809 		return (0);
810 	}
811 	case DTRACEIOC_STOP: {
812 		int rval;
813 		processorid_t *cpuid = (processorid_t *) addr;
814 
815 		DTRACE_IOCTL_PRINTF("%s(%d): DTRACEIOC_STOP\n",__func__,__LINE__);
816 
817 		mutex_enter(&dtrace_lock);
818 		rval = dtrace_state_stop(state, cpuid);
819 		mutex_exit(&dtrace_lock);
820 
821 		return (rval);
822 	}
823 	default:
824 		error = ENOTTY;
825 	}
826 	return (error);
827 }
828