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