xref: /titanic_53/usr/src/uts/common/tnf/tnf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * tnf driver - provides probe control and kernel trace buffer access
31*7c478bd9Sstevel@tonic-gate  * to the user programs prex and tnfxtract.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/uio.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/tnf.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/devops.h>
53*7c478bd9Sstevel@tonic-gate #include <vm/as.h>
54*7c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #include "tnf_buf.h"
59*7c478bd9Sstevel@tonic-gate #include "tnf_types.h"
60*7c478bd9Sstevel@tonic-gate #include "tnf_trace.h"
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate #ifndef NPROBE
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * Each probe is independently put in the kernel, prex uses
66*7c478bd9Sstevel@tonic-gate  * __tnf_probe_list_head and __tnf_tag_list_head as pointers to linked list
67*7c478bd9Sstevel@tonic-gate  * for probes and static tnf_tag_data_t, respectively.
68*7c478bd9Sstevel@tonic-gate  * tnf used the elf relocation record to build a separate linked list for
69*7c478bd9Sstevel@tonic-gate  * the probes and tnf_tag_data_t. We will describe how the linked list for
70*7c478bd9Sstevel@tonic-gate  * __tnf_tag_list_head is made, the probe list is very similar.
71*7c478bd9Sstevel@tonic-gate  * During the dynamic relocation(in uts/sparc/krtld/kobj_reloc.c),
72*7c478bd9Sstevel@tonic-gate  * the &__tnf_tag_version_1(the first member in tnf_tag_data_t data struct)
73*7c478bd9Sstevel@tonic-gate  * (and since it is a global variable which was never defined) will be filled
74*7c478bd9Sstevel@tonic-gate  * with 0. The following code in kobj_reloc.c will get the address of current
75*7c478bd9Sstevel@tonic-gate  * __tnf_tag_list_head and put it in value_p:
76*7c478bd9Sstevel@tonic-gate  *   #define TAG_MARKER_SYMBOL       "__tnf_tag_version_1"
77*7c478bd9Sstevel@tonic-gate  *   if (strcmp(symname, TAG_MARKER_SYMBOL) == 0) {
78*7c478bd9Sstevel@tonic-gate  *       *addend_p = 0;
79*7c478bd9Sstevel@tonic-gate  *       *value_p = (Addr) __tnf_tag_list_head; (value_p points to list head)
80*7c478bd9Sstevel@tonic-gate  *       __tnf_tag_list_head = (void *)*offset_p;(list head is the next record)
81*7c478bd9Sstevel@tonic-gate  *       return (0);
82*7c478bd9Sstevel@tonic-gate  *   }
83*7c478bd9Sstevel@tonic-gate  *
84*7c478bd9Sstevel@tonic-gate  * the function do_reloc(in the kobj_reloc.c) will put vlaue_p into
85*7c478bd9Sstevel@tonic-gate  * &__tnf_tag_version_1
86*7c478bd9Sstevel@tonic-gate  * Now the &__tnf_tag_version_1 points to the last list head
87*7c478bd9Sstevel@tonic-gate  * and __tnf_tag_list_head points to the new list head.
88*7c478bd9Sstevel@tonic-gate  * This is equivalent to attatch a node at the beginning of the list.
89*7c478bd9Sstevel@tonic-gate  *
90*7c478bd9Sstevel@tonic-gate  */
91*7c478bd9Sstevel@tonic-gate extern tnf_probe_control_t *__tnf_probe_list_head;
92*7c478bd9Sstevel@tonic-gate extern tnf_tag_data_t *__tnf_tag_list_head;
93*7c478bd9Sstevel@tonic-gate extern int tnf_changed_probe_list;
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate static int tnf_attach(dev_info_t *, ddi_attach_cmd_t);
96*7c478bd9Sstevel@tonic-gate static int tnf_detach(dev_info_t *, ddi_detach_cmd_t);
97*7c478bd9Sstevel@tonic-gate static int tnf_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
98*7c478bd9Sstevel@tonic-gate static int tnf_open(dev_t *, int, int, struct cred *);
99*7c478bd9Sstevel@tonic-gate static int tnf_close(dev_t, int, int, struct cred *);
100*7c478bd9Sstevel@tonic-gate #ifdef UNUSED
101*7c478bd9Sstevel@tonic-gate static int tnf_mmap(dev_t, off_t, int);
102*7c478bd9Sstevel@tonic-gate #endif
103*7c478bd9Sstevel@tonic-gate static int tnf_ioctl(dev_t, int, intptr_t, int, struct cred *, int *);
104*7c478bd9Sstevel@tonic-gate #ifdef UNUSED
105*7c478bd9Sstevel@tonic-gate static int tnf_prop_op(dev_t, dev_info_t *, ddi_prop_op_t,
106*7c478bd9Sstevel@tonic-gate     int, char *, caddr_t, int *);
107*7c478bd9Sstevel@tonic-gate #endif
108*7c478bd9Sstevel@tonic-gate static dev_info_t *tnf_devi;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate static struct {
111*7c478bd9Sstevel@tonic-gate 	int		tnf_probe_count;
112*7c478bd9Sstevel@tonic-gate 	boolean_t	tnf_pidfilter_mode;
113*7c478bd9Sstevel@tonic-gate 	boolean_t	ctldev_is_open;
114*7c478bd9Sstevel@tonic-gate 	int		mapdev_open_count;
115*7c478bd9Sstevel@tonic-gate 	kmutex_t 	tnf_mtx;
116*7c478bd9Sstevel@tonic-gate } tnf_drv_state = { 0, B_FALSE, B_FALSE, 0 };
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate static int tnf_getmaxprobe(caddr_t, int);
119*7c478bd9Sstevel@tonic-gate static int tnf_getprobevals(caddr_t, int);
120*7c478bd9Sstevel@tonic-gate static int tnf_getprobestring(caddr_t, int);
121*7c478bd9Sstevel@tonic-gate static int tnf_setprobevals(caddr_t, int);
122*7c478bd9Sstevel@tonic-gate static int tnf_getstate(caddr_t, int);
123*7c478bd9Sstevel@tonic-gate static int tnf_allocbuf(intptr_t);
124*7c478bd9Sstevel@tonic-gate static int tnf_deallocbuf(void);
125*7c478bd9Sstevel@tonic-gate static int tnf_settracing(int);
126*7c478bd9Sstevel@tonic-gate static int tnf_pidfilterset(int);
127*7c478bd9Sstevel@tonic-gate static int tnf_pidfilterget(caddr_t, int);
128*7c478bd9Sstevel@tonic-gate static int tnf_getpidstate(caddr_t, int);
129*7c478bd9Sstevel@tonic-gate static int tnf_setpidstate(int, pid_t, int);
130*7c478bd9Sstevel@tonic-gate static int tnf_getheader(caddr_t, int);
131*7c478bd9Sstevel@tonic-gate static int tnf_getblock(caddr_t, int);
132*7c478bd9Sstevel@tonic-gate static int tnf_getfwzone(caddr_t, int);
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate static void *tnf_test_1(void *, tnf_probe_control_t *, tnf_probe_setup_t *);
135*7c478bd9Sstevel@tonic-gate static void *tnf_test_2(void *, tnf_probe_control_t *, tnf_probe_setup_t *);
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate #define	TNFCTL_MINOR 0
138*7c478bd9Sstevel@tonic-gate #define	TNFMAP_MINOR 1
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate struct cb_ops	tnf_cb_ops = {
141*7c478bd9Sstevel@tonic-gate 	tnf_open,		/* open */
142*7c478bd9Sstevel@tonic-gate 	tnf_close,		/* close */
143*7c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
144*7c478bd9Sstevel@tonic-gate 	nodev,			/* print */
145*7c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
146*7c478bd9Sstevel@tonic-gate 	nodev,			/* read */
147*7c478bd9Sstevel@tonic-gate 	nodev,			/* write */
148*7c478bd9Sstevel@tonic-gate 	tnf_ioctl,		/* ioctl */
149*7c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
150*7c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
151*7c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
152*7c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
153*7c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
154*7c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
155*7c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
156*7c478bd9Sstevel@tonic-gate };
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate struct dev_ops	tnf_ops = {
159*7c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
160*7c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
161*7c478bd9Sstevel@tonic-gate 	tnf_info,		/* info */
162*7c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
163*7c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
164*7c478bd9Sstevel@tonic-gate 	tnf_attach,		/* attach */
165*7c478bd9Sstevel@tonic-gate 	tnf_detach,		/* detach */
166*7c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
167*7c478bd9Sstevel@tonic-gate 	&tnf_cb_ops,		/* driver operations */
168*7c478bd9Sstevel@tonic-gate 	(struct bus_ops *)0	/* no bus operations */
169*7c478bd9Sstevel@tonic-gate };
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
174*7c478bd9Sstevel@tonic-gate 	&mod_driverops,
175*7c478bd9Sstevel@tonic-gate 	"kernel probes driver %I%",
176*7c478bd9Sstevel@tonic-gate 	&tnf_ops,
177*7c478bd9Sstevel@tonic-gate };
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
180*7c478bd9Sstevel@tonic-gate 	MODREV_1,
181*7c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
182*7c478bd9Sstevel@tonic-gate 	NULL
183*7c478bd9Sstevel@tonic-gate };
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate int
186*7c478bd9Sstevel@tonic-gate _init()
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate 	register int error;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	mutex_init(&tnf_drv_state.tnf_mtx, NULL, MUTEX_DEFAULT, NULL);
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
193*7c478bd9Sstevel@tonic-gate 		mutex_destroy(&tnf_drv_state.tnf_mtx);
194*7c478bd9Sstevel@tonic-gate 		return (error);
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	/* Give t0 a tpdp */
198*7c478bd9Sstevel@tonic-gate 	if (!t0.t_tnf_tpdp)
199*7c478bd9Sstevel@tonic-gate 		t0.t_tnf_tpdp = kmem_zalloc(sizeof (tnf_ops_t), KM_SLEEP);
200*7c478bd9Sstevel@tonic-gate 	/* Initialize tag system */
201*7c478bd9Sstevel@tonic-gate 	tnf_tag_core_init();
202*7c478bd9Sstevel@tonic-gate 	tnf_tag_trace_init();
203*7c478bd9Sstevel@tonic-gate 	tnf_changed_probe_list = 1;
204*7c478bd9Sstevel@tonic-gate 	return (0);
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate int
208*7c478bd9Sstevel@tonic-gate _fini()
209*7c478bd9Sstevel@tonic-gate {
210*7c478bd9Sstevel@tonic-gate 	/* Not safe to unload this module, currently */
211*7c478bd9Sstevel@tonic-gate 	return (EBUSY);
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate int
215*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
216*7c478bd9Sstevel@tonic-gate {
217*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
218*7c478bd9Sstevel@tonic-gate }
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
221*7c478bd9Sstevel@tonic-gate static int
222*7c478bd9Sstevel@tonic-gate tnf_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
223*7c478bd9Sstevel@tonic-gate {
224*7c478bd9Sstevel@tonic-gate 	register int error;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	switch (infocmd) {
227*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
228*7c478bd9Sstevel@tonic-gate 		*result = (void *)tnf_devi;
229*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
230*7c478bd9Sstevel@tonic-gate 		break;
231*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
232*7c478bd9Sstevel@tonic-gate 		*result = (void *)0;
233*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
234*7c478bd9Sstevel@tonic-gate 		break;
235*7c478bd9Sstevel@tonic-gate 	default:
236*7c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 	return (error);
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate static int
242*7c478bd9Sstevel@tonic-gate tnf_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
243*7c478bd9Sstevel@tonic-gate {
244*7c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
245*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
246*7c478bd9Sstevel@tonic-gate 	if ((ddi_create_minor_node(devi, "tnfctl", S_IFCHR, TNFCTL_MINOR,
247*7c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL) == DDI_FAILURE) ||
248*7c478bd9Sstevel@tonic-gate 	    (ddi_create_minor_node(devi, "tnfmap", S_IFCHR, TNFMAP_MINOR,
249*7c478bd9Sstevel@tonic-gate 		DDI_PSEUDO, NULL) == DDI_FAILURE)) {
250*7c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
251*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
252*7c478bd9Sstevel@tonic-gate 	}
253*7c478bd9Sstevel@tonic-gate 	tnf_devi = devi;
254*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
255*7c478bd9Sstevel@tonic-gate }
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate static int
258*7c478bd9Sstevel@tonic-gate tnf_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
259*7c478bd9Sstevel@tonic-gate {
260*7c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
261*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
262*7c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
263*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
264*7c478bd9Sstevel@tonic-gate }
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate /*
267*7c478bd9Sstevel@tonic-gate  * property operations. Return the size of the kernel trace buffer.  We
268*7c478bd9Sstevel@tonic-gate  * only handle size property requests.  Others are passed on.
269*7c478bd9Sstevel@tonic-gate  */
270*7c478bd9Sstevel@tonic-gate #ifdef UNUSED
271*7c478bd9Sstevel@tonic-gate static int
272*7c478bd9Sstevel@tonic-gate tnf_prop_op(dev_t dev, dev_info_t *di, ddi_prop_op_t prop,
273*7c478bd9Sstevel@tonic-gate     int m, char *name, caddr_t valuep, int *lengthp)
274*7c478bd9Sstevel@tonic-gate {
275*7c478bd9Sstevel@tonic-gate 	int length, *retbuf, size;
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	if (strcmp(name, "size") == 0) {
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 		/* Don't need tnf_mtx, since mapdev_open_count > 0 */
280*7c478bd9Sstevel@tonic-gate 		size = tnf_trace_file_size;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 		length = *lengthp;		/* get caller's length */
283*7c478bd9Sstevel@tonic-gate 		*lengthp = sizeof (int);	/* set caller's length */
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 		switch (prop) {
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 		case PROP_LEN:
288*7c478bd9Sstevel@tonic-gate 			return (DDI_PROP_SUCCESS);
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 		case PROP_LEN_AND_VAL_ALLOC:
291*7c478bd9Sstevel@tonic-gate 			retbuf = kmem_alloc(sizeof (int),
292*7c478bd9Sstevel@tonic-gate 			    (m & DDI_PROP_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP);
293*7c478bd9Sstevel@tonic-gate 			if (retbuf == NULL)
294*7c478bd9Sstevel@tonic-gate 				return (DDI_PROP_NO_MEMORY);
295*7c478bd9Sstevel@tonic-gate 			*(int **)valuep = retbuf;	/* set caller's buf */
296*7c478bd9Sstevel@tonic-gate 			*retbuf = size;
297*7c478bd9Sstevel@tonic-gate 			return (DDI_PROP_SUCCESS);
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 		case PROP_LEN_AND_VAL_BUF:
300*7c478bd9Sstevel@tonic-gate 			if (length < sizeof (int))
301*7c478bd9Sstevel@tonic-gate 				return (DDI_PROP_BUF_TOO_SMALL);
302*7c478bd9Sstevel@tonic-gate 			*(int *)valuep = size;
303*7c478bd9Sstevel@tonic-gate 			return (DDI_PROP_SUCCESS);
304*7c478bd9Sstevel@tonic-gate 		}
305*7c478bd9Sstevel@tonic-gate 	}
306*7c478bd9Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop, m, name, valuep, lengthp));
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate #endif
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
311*7c478bd9Sstevel@tonic-gate static int
312*7c478bd9Sstevel@tonic-gate tnf_open(dev_t *devp, int flag, int otyp, struct cred *cred)
313*7c478bd9Sstevel@tonic-gate {
314*7c478bd9Sstevel@tonic-gate 	int err = 0;
315*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tnf_drv_state.tnf_mtx);
316*7c478bd9Sstevel@tonic-gate 	if (getminor(*devp) == TNFCTL_MINOR) {
317*7c478bd9Sstevel@tonic-gate 		if (tnf_drv_state.ctldev_is_open)
318*7c478bd9Sstevel@tonic-gate 			err = EBUSY;
319*7c478bd9Sstevel@tonic-gate 		else {
320*7c478bd9Sstevel@tonic-gate 			tnf_drv_state.ctldev_is_open = B_TRUE;
321*7c478bd9Sstevel@tonic-gate 			/* stop autounloading -- XXX temporary */
322*7c478bd9Sstevel@tonic-gate 			modunload_disable();
323*7c478bd9Sstevel@tonic-gate 		}
324*7c478bd9Sstevel@tonic-gate 	} else {
325*7c478bd9Sstevel@tonic-gate 		/* ASSERT(getminor(*devp) == TNFMAP_MINOR) */
326*7c478bd9Sstevel@tonic-gate 		++tnf_drv_state.mapdev_open_count;
327*7c478bd9Sstevel@tonic-gate 	}
328*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tnf_drv_state.tnf_mtx);
329*7c478bd9Sstevel@tonic-gate 	return (err);
330*7c478bd9Sstevel@tonic-gate }
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
333*7c478bd9Sstevel@tonic-gate static int
334*7c478bd9Sstevel@tonic-gate tnf_close(dev_t dev, int flag, int otyp, struct cred *cred)
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate 	if (getminor(dev) == TNFCTL_MINOR) {
337*7c478bd9Sstevel@tonic-gate 		/*
338*7c478bd9Sstevel@tonic-gate 		 * Request the reenablement of autounloading
339*7c478bd9Sstevel@tonic-gate 		 */
340*7c478bd9Sstevel@tonic-gate 		modunload_enable();
341*7c478bd9Sstevel@tonic-gate 		tnf_drv_state.ctldev_is_open = B_FALSE;
342*7c478bd9Sstevel@tonic-gate 	} else {
343*7c478bd9Sstevel@tonic-gate 		/* ASSERT(getminor(dev) == TNFMAP_MINOR) */
344*7c478bd9Sstevel@tonic-gate 		/*
345*7c478bd9Sstevel@tonic-gate 		 * Unconditionally zero the open count since close()
346*7c478bd9Sstevel@tonic-gate 		 * is called when last client closes the device.
347*7c478bd9Sstevel@tonic-gate 		 */
348*7c478bd9Sstevel@tonic-gate 		tnf_drv_state.mapdev_open_count = 0;
349*7c478bd9Sstevel@tonic-gate 	}
350*7c478bd9Sstevel@tonic-gate 	return (0);
351*7c478bd9Sstevel@tonic-gate }
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate /*
354*7c478bd9Sstevel@tonic-gate  * return the address of the image referenced by dev.
355*7c478bd9Sstevel@tonic-gate  *
356*7c478bd9Sstevel@tonic-gate  * 1191344: aliasing problem on VAC machines.  It could be made to
357*7c478bd9Sstevel@tonic-gate  * work by ensuring that tnf_buf is allocated on a vac_size boundary.
358*7c478bd9Sstevel@tonic-gate  */
359*7c478bd9Sstevel@tonic-gate #ifdef UNUSED
360*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
361*7c478bd9Sstevel@tonic-gate static int
362*7c478bd9Sstevel@tonic-gate tnf_mmap(dev_t dev, off_t off, int prot)
363*7c478bd9Sstevel@tonic-gate {
364*7c478bd9Sstevel@tonic-gate 	register caddr_t addr;
365*7c478bd9Sstevel@tonic-gate 	register caddr_t pg_offset;
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 	if (getminor(dev) != TNFMAP_MINOR)
368*7c478bd9Sstevel@tonic-gate 		return (-1);
369*7c478bd9Sstevel@tonic-gate 	if (tnf_buf == 0 || off >= tnf_trace_file_size) {
370*7c478bd9Sstevel@tonic-gate 		return (-1);
371*7c478bd9Sstevel@tonic-gate 	}
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	addr = tnf_buf;
374*7c478bd9Sstevel@tonic-gate 	pg_offset = (caddr_t)((ulong_t)addr + (ulong_t)off);
375*7c478bd9Sstevel@tonic-gate 	return ((int)hat_getpfnum(kas.a_hat, pg_offset));
376*7c478bd9Sstevel@tonic-gate }
377*7c478bd9Sstevel@tonic-gate #endif
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate /*ARGSUSED4*/
380*7c478bd9Sstevel@tonic-gate static int
381*7c478bd9Sstevel@tonic-gate tnf_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
382*7c478bd9Sstevel@tonic-gate 	cred_t *credp, int *rvalp)
383*7c478bd9Sstevel@tonic-gate {
384*7c478bd9Sstevel@tonic-gate 	int filterval = 1;
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	if ((mode & FMODELS) != FNATIVE)
387*7c478bd9Sstevel@tonic-gate 	    return (ENOTSUP);
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	if (getminor(dev) != TNFCTL_MINOR &&
390*7c478bd9Sstevel@tonic-gate 	    cmd != TIFIOCGSTATE &&
391*7c478bd9Sstevel@tonic-gate 	    cmd != TIFIOCGHEADER &&
392*7c478bd9Sstevel@tonic-gate 	    cmd != TIFIOCGBLOCK &&
393*7c478bd9Sstevel@tonic-gate 	    cmd != TIFIOCGFWZONE)
394*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
397*7c478bd9Sstevel@tonic-gate 	case TIFIOCGMAXPROBE:
398*7c478bd9Sstevel@tonic-gate 		return (tnf_getmaxprobe((caddr_t)arg, mode));
399*7c478bd9Sstevel@tonic-gate 	case TIFIOCGPROBEVALS:
400*7c478bd9Sstevel@tonic-gate 		return (tnf_getprobevals((caddr_t)arg, mode));
401*7c478bd9Sstevel@tonic-gate 	case TIFIOCGPROBESTRING:
402*7c478bd9Sstevel@tonic-gate 		return (tnf_getprobestring((caddr_t)arg, mode));
403*7c478bd9Sstevel@tonic-gate 	case TIFIOCSPROBEVALS:
404*7c478bd9Sstevel@tonic-gate 		return (tnf_setprobevals((caddr_t)arg, mode));
405*7c478bd9Sstevel@tonic-gate 	case TIFIOCGSTATE:
406*7c478bd9Sstevel@tonic-gate 		return (tnf_getstate((caddr_t)arg, mode));
407*7c478bd9Sstevel@tonic-gate 	case TIFIOCALLOCBUF:
408*7c478bd9Sstevel@tonic-gate 		return (tnf_allocbuf(arg));
409*7c478bd9Sstevel@tonic-gate 	case TIFIOCDEALLOCBUF:
410*7c478bd9Sstevel@tonic-gate 		return (tnf_deallocbuf());
411*7c478bd9Sstevel@tonic-gate 	case TIFIOCSTRACING:
412*7c478bd9Sstevel@tonic-gate 		/* LINTED cast from 64-bit integer to 32-bit integer */
413*7c478bd9Sstevel@tonic-gate 		return (tnf_settracing((int)arg));
414*7c478bd9Sstevel@tonic-gate 	case TIFIOCSPIDFILTER:
415*7c478bd9Sstevel@tonic-gate 		/* LINTED cast from 64-bit integer to 32-bit integer */
416*7c478bd9Sstevel@tonic-gate 		return (tnf_pidfilterset((int)arg));
417*7c478bd9Sstevel@tonic-gate 	case TIFIOCGPIDSTATE:
418*7c478bd9Sstevel@tonic-gate 		return (tnf_getpidstate((caddr_t)arg, mode));
419*7c478bd9Sstevel@tonic-gate 	case TIFIOCSPIDOFF:
420*7c478bd9Sstevel@tonic-gate 		filterval = 0;
421*7c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
422*7c478bd9Sstevel@tonic-gate 	case TIFIOCSPIDON:
423*7c478bd9Sstevel@tonic-gate 		/* LINTED cast from 64-bit integer to 32-bit integer */
424*7c478bd9Sstevel@tonic-gate 		return (tnf_setpidstate(filterval, (pid_t)arg, mode));
425*7c478bd9Sstevel@tonic-gate 	case TIFIOCPIDFILTERGET:
426*7c478bd9Sstevel@tonic-gate 		return (tnf_pidfilterget((caddr_t)arg, mode));
427*7c478bd9Sstevel@tonic-gate 	case TIFIOCGHEADER:
428*7c478bd9Sstevel@tonic-gate 		return (tnf_getheader((caddr_t)arg, mode));
429*7c478bd9Sstevel@tonic-gate 	case TIFIOCGBLOCK:
430*7c478bd9Sstevel@tonic-gate 		return (tnf_getblock((caddr_t)arg, mode));
431*7c478bd9Sstevel@tonic-gate 	case TIFIOCGFWZONE:
432*7c478bd9Sstevel@tonic-gate 		return (tnf_getfwzone((caddr_t)arg, mode));
433*7c478bd9Sstevel@tonic-gate 	default:
434*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
435*7c478bd9Sstevel@tonic-gate 	}
436*7c478bd9Sstevel@tonic-gate }
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate /*
439*7c478bd9Sstevel@tonic-gate  * ioctls
440*7c478bd9Sstevel@tonic-gate  */
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate static int
443*7c478bd9Sstevel@tonic-gate tnf_getmaxprobe(caddr_t arg, int mode)
444*7c478bd9Sstevel@tonic-gate {
445*7c478bd9Sstevel@tonic-gate 	tnf_probe_control_t *p;
446*7c478bd9Sstevel@tonic-gate 	/*
447*7c478bd9Sstevel@tonic-gate 	 * XXX Still not right for module unload -- just counting
448*7c478bd9Sstevel@tonic-gate 	 * the probes is not enough
449*7c478bd9Sstevel@tonic-gate 	 */
450*7c478bd9Sstevel@tonic-gate 	if (tnf_changed_probe_list) {
451*7c478bd9Sstevel@tonic-gate 		mutex_enter(&mod_lock);
452*7c478bd9Sstevel@tonic-gate 		tnf_changed_probe_list = 0;
453*7c478bd9Sstevel@tonic-gate 		tnf_drv_state.tnf_probe_count = 0;
454*7c478bd9Sstevel@tonic-gate 		for (p = (tnf_probe_control_t *)__tnf_probe_list_head;
455*7c478bd9Sstevel@tonic-gate 		    p != 0; p = p->next)
456*7c478bd9Sstevel@tonic-gate 			++tnf_drv_state.tnf_probe_count;
457*7c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
458*7c478bd9Sstevel@tonic-gate 	}
459*7c478bd9Sstevel@tonic-gate 	if (ddi_copyout((caddr_t)&tnf_drv_state.tnf_probe_count,
460*7c478bd9Sstevel@tonic-gate 	    arg, sizeof (tnf_drv_state.tnf_probe_count), mode))
461*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
462*7c478bd9Sstevel@tonic-gate 	return (0);
463*7c478bd9Sstevel@tonic-gate }
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate static int
466*7c478bd9Sstevel@tonic-gate tnf_getprobevals(caddr_t arg, int mode)
467*7c478bd9Sstevel@tonic-gate {
468*7c478bd9Sstevel@tonic-gate 	tnf_probevals_t probebuf;
469*7c478bd9Sstevel@tonic-gate 	tnf_probe_control_t *p;
470*7c478bd9Sstevel@tonic-gate 	int i, retval = 0;
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&probebuf, sizeof (probebuf), mode))
473*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
476*7c478bd9Sstevel@tonic-gate 	for (i = 1, p = (tnf_probe_control_t *)__tnf_probe_list_head;
477*7c478bd9Sstevel@tonic-gate 		p != NULL && i != probebuf.probenum;
478*7c478bd9Sstevel@tonic-gate 		++i, p = p->next)
479*7c478bd9Sstevel@tonic-gate 		;
480*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
481*7c478bd9Sstevel@tonic-gate 		retval = ENOENT;
482*7c478bd9Sstevel@tonic-gate 	else {
483*7c478bd9Sstevel@tonic-gate 		probebuf.enabled = (p->test_func != NULL);
484*7c478bd9Sstevel@tonic-gate 		probebuf.traced = (p->probe_func == tnf_trace_commit);
485*7c478bd9Sstevel@tonic-gate 		/* LINTED assignment of 64-bit integer to 32-bit integer */
486*7c478bd9Sstevel@tonic-gate 		probebuf.attrsize = strlen(p->attrs) + 1;
487*7c478bd9Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&probebuf,
488*7c478bd9Sstevel@tonic-gate 		    arg, sizeof (probebuf), mode))
489*7c478bd9Sstevel@tonic-gate 			retval = EFAULT;
490*7c478bd9Sstevel@tonic-gate 	}
491*7c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
492*7c478bd9Sstevel@tonic-gate 	return (retval);
493*7c478bd9Sstevel@tonic-gate }
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate static int
496*7c478bd9Sstevel@tonic-gate tnf_getprobestring(caddr_t arg, int mode)
497*7c478bd9Sstevel@tonic-gate {
498*7c478bd9Sstevel@tonic-gate 	tnf_probevals_t probebuf;
499*7c478bd9Sstevel@tonic-gate 	tnf_probe_control_t *p;
500*7c478bd9Sstevel@tonic-gate 	int i, retval = 0;
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&probebuf, sizeof (probebuf), mode))
503*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
504*7c478bd9Sstevel@tonic-gate 
505*7c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
506*7c478bd9Sstevel@tonic-gate 	for (i = 1, p = (tnf_probe_control_t *)__tnf_probe_list_head;
507*7c478bd9Sstevel@tonic-gate 		p != NULL && i != probebuf.probenum;
508*7c478bd9Sstevel@tonic-gate 		++i, p = p->next)
509*7c478bd9Sstevel@tonic-gate 		;
510*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
511*7c478bd9Sstevel@tonic-gate 		retval = ENOENT;
512*7c478bd9Sstevel@tonic-gate 	else if (ddi_copyout((caddr_t)p->attrs,
513*7c478bd9Sstevel@tonic-gate 	    arg, strlen(p->attrs) + 1, mode))
514*7c478bd9Sstevel@tonic-gate 		retval = EFAULT;
515*7c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
516*7c478bd9Sstevel@tonic-gate 	return (retval);
517*7c478bd9Sstevel@tonic-gate }
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate static int
520*7c478bd9Sstevel@tonic-gate tnf_setprobevals(caddr_t arg, int mode)
521*7c478bd9Sstevel@tonic-gate {
522*7c478bd9Sstevel@tonic-gate 	tnf_probevals_t probebuf;
523*7c478bd9Sstevel@tonic-gate 	tnf_probe_control_t *p;
524*7c478bd9Sstevel@tonic-gate 	int i, retval = 0;
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&probebuf, sizeof (probebuf), mode))
527*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
530*7c478bd9Sstevel@tonic-gate 	for (i = 1, p = (tnf_probe_control_t *)__tnf_probe_list_head;
531*7c478bd9Sstevel@tonic-gate 		p != NULL && i != probebuf.probenum;
532*7c478bd9Sstevel@tonic-gate 		++i, p = p->next)
533*7c478bd9Sstevel@tonic-gate 		;
534*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
535*7c478bd9Sstevel@tonic-gate 		retval = ENOENT;
536*7c478bd9Sstevel@tonic-gate 	else {
537*7c478bd9Sstevel@tonic-gate 		/*
538*7c478bd9Sstevel@tonic-gate 		 * First do trace, then enable.
539*7c478bd9Sstevel@tonic-gate 		 * Set test_func last.
540*7c478bd9Sstevel@tonic-gate 		 */
541*7c478bd9Sstevel@tonic-gate 		if (probebuf.traced)
542*7c478bd9Sstevel@tonic-gate 			p->probe_func = tnf_trace_commit;
543*7c478bd9Sstevel@tonic-gate 		else
544*7c478bd9Sstevel@tonic-gate 			p->probe_func = tnf_trace_rollback;
545*7c478bd9Sstevel@tonic-gate 		if (probebuf.enabled) {
546*7c478bd9Sstevel@tonic-gate 			p->alloc_func = tnf_trace_alloc;
547*7c478bd9Sstevel@tonic-gate 			/* this must be set last */
548*7c478bd9Sstevel@tonic-gate 			if (tnf_drv_state.tnf_pidfilter_mode)
549*7c478bd9Sstevel@tonic-gate 				p->test_func = tnf_test_2;
550*7c478bd9Sstevel@tonic-gate 			else
551*7c478bd9Sstevel@tonic-gate 				p->test_func = tnf_test_1;
552*7c478bd9Sstevel@tonic-gate 		} else
553*7c478bd9Sstevel@tonic-gate 			p->test_func = NULL;
554*7c478bd9Sstevel@tonic-gate 	}
555*7c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
556*7c478bd9Sstevel@tonic-gate 	return (retval);
557*7c478bd9Sstevel@tonic-gate }
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate static int
560*7c478bd9Sstevel@tonic-gate tnf_getstate(caddr_t arg, int mode)
561*7c478bd9Sstevel@tonic-gate {
562*7c478bd9Sstevel@tonic-gate 	tifiocstate_t	tstate;
563*7c478bd9Sstevel@tonic-gate 	proc_t		*procp;
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 	if (tnf_buf == NULL) {
566*7c478bd9Sstevel@tonic-gate 		tstate.buffer_state = TIFIOCBUF_NONE;
567*7c478bd9Sstevel@tonic-gate 		tstate.buffer_size = 0;
568*7c478bd9Sstevel@tonic-gate 	} else {
569*7c478bd9Sstevel@tonic-gate 		switch (tnfw_b_state & ~TNFW_B_STOPPED) {
570*7c478bd9Sstevel@tonic-gate 		case TNFW_B_RUNNING:
571*7c478bd9Sstevel@tonic-gate 			tstate.buffer_state = TIFIOCBUF_OK;
572*7c478bd9Sstevel@tonic-gate 			break;
573*7c478bd9Sstevel@tonic-gate 		case TNFW_B_NOBUFFER:
574*7c478bd9Sstevel@tonic-gate 			tstate.buffer_state = TIFIOCBUF_UNINIT;
575*7c478bd9Sstevel@tonic-gate 			break;
576*7c478bd9Sstevel@tonic-gate 		case TNFW_B_BROKEN:
577*7c478bd9Sstevel@tonic-gate 			tstate.buffer_state = TIFIOCBUF_BROKEN;
578*7c478bd9Sstevel@tonic-gate 			break;
579*7c478bd9Sstevel@tonic-gate 		}
580*7c478bd9Sstevel@tonic-gate 		/* LINTED assignment of 64-bit integer to 32-bit integer */
581*7c478bd9Sstevel@tonic-gate 		tstate.buffer_size = tnf_trace_file_size;
582*7c478bd9Sstevel@tonic-gate 	}
583*7c478bd9Sstevel@tonic-gate 	tstate.trace_stopped = tnfw_b_state & TNFW_B_STOPPED;
584*7c478bd9Sstevel@tonic-gate 	tstate.pidfilter_mode = tnf_drv_state.tnf_pidfilter_mode;
585*7c478bd9Sstevel@tonic-gate 	tstate.pidfilter_size = 0;
586*7c478bd9Sstevel@tonic-gate 
587*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
588*7c478bd9Sstevel@tonic-gate 	for (procp = practive; procp != NULL; procp = procp->p_next)
589*7c478bd9Sstevel@tonic-gate 		if (PROC_IS_FILTER(procp))
590*7c478bd9Sstevel@tonic-gate 			tstate.pidfilter_size++;
591*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 	if (ddi_copyout((caddr_t)&tstate, arg, sizeof (tstate), mode))
594*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
595*7c478bd9Sstevel@tonic-gate 	return (0);
596*7c478bd9Sstevel@tonic-gate }
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate static int
599*7c478bd9Sstevel@tonic-gate tnf_allocbuf(intptr_t arg)
600*7c478bd9Sstevel@tonic-gate {
601*7c478bd9Sstevel@tonic-gate 	size_t bufsz;
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate 	if (tnf_buf != NULL)
604*7c478bd9Sstevel@tonic-gate 		return (EBUSY);
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate 	bufsz = roundup((size_t)arg, PAGESIZE);
607*7c478bd9Sstevel@tonic-gate 	/*
608*7c478bd9Sstevel@tonic-gate 	 * Validate size
609*7c478bd9Sstevel@tonic-gate 	 * XXX Take kernel VM into consideration as well
610*7c478bd9Sstevel@tonic-gate 	 */
611*7c478bd9Sstevel@tonic-gate 	/* bug fix #4057599 if (bufsz > (physmem << PAGESHIFT) / 2) */
612*7c478bd9Sstevel@tonic-gate 	if (btop(bufsz) > (physmem / 2))
613*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
614*7c478bd9Sstevel@tonic-gate 	if (bufsz < TNF_TRACE_FILE_MIN)
615*7c478bd9Sstevel@tonic-gate 		bufsz = TNF_TRACE_FILE_MIN;
616*7c478bd9Sstevel@tonic-gate 
617*7c478bd9Sstevel@tonic-gate #if TNF_USE_KMA
618*7c478bd9Sstevel@tonic-gate 	tnf_buf = kmem_zalloc(bufsz, KM_SLEEP);
619*7c478bd9Sstevel@tonic-gate #else
620*7c478bd9Sstevel@tonic-gate 	/* LINTED cast from 64-bit integer to 32-bit intege */
621*7c478bd9Sstevel@tonic-gate 	tnf_buf = segkp_get(segkp, (int)bufsz,
622*7c478bd9Sstevel@tonic-gate 				KPD_ZERO | KPD_LOCKED | KPD_NO_ANON);
623*7c478bd9Sstevel@tonic-gate #endif
624*7c478bd9Sstevel@tonic-gate 	if (tnf_buf == NULL)
625*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
626*7c478bd9Sstevel@tonic-gate 
627*7c478bd9Sstevel@tonic-gate 	tnf_trace_file_size = bufsz;
628*7c478bd9Sstevel@tonic-gate 	tnf_trace_init();
629*7c478bd9Sstevel@tonic-gate 	return (0);
630*7c478bd9Sstevel@tonic-gate }
631*7c478bd9Sstevel@tonic-gate 
632*7c478bd9Sstevel@tonic-gate /*
633*7c478bd9Sstevel@tonic-gate  * Process a "deallocate buffer" ioctl request.  Tracing must be turned
634*7c478bd9Sstevel@tonic-gate  * off.  We must clear references to the buffer from the tag sites;
635*7c478bd9Sstevel@tonic-gate  * invalidate all threads' notions of block ownership; make sure nobody
636*7c478bd9Sstevel@tonic-gate  * is executing a probe (they might have started before tracing was
637*7c478bd9Sstevel@tonic-gate  * turned off); and free the buffer.
638*7c478bd9Sstevel@tonic-gate  */
639*7c478bd9Sstevel@tonic-gate static int
640*7c478bd9Sstevel@tonic-gate tnf_deallocbuf(void)
641*7c478bd9Sstevel@tonic-gate {
642*7c478bd9Sstevel@tonic-gate 	tnf_ops_t *tpdp;
643*7c478bd9Sstevel@tonic-gate 	kthread_t *t;
644*7c478bd9Sstevel@tonic-gate 	tnf_probe_control_t *probep;
645*7c478bd9Sstevel@tonic-gate 	tnf_tag_data_t *tagp;
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate 	if (tnf_drv_state.mapdev_open_count > 0 || tnf_tracing_active)
648*7c478bd9Sstevel@tonic-gate 		return (EBUSY);
649*7c478bd9Sstevel@tonic-gate 	if (tnf_buf == NULL)
650*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
651*7c478bd9Sstevel@tonic-gate 
652*7c478bd9Sstevel@tonic-gate 	/*
653*7c478bd9Sstevel@tonic-gate 	 * Make sure nobody is executing a probe.
654*7c478bd9Sstevel@tonic-gate 	 * (They could be if they got started while
655*7c478bd9Sstevel@tonic-gate 	 * tnf_tracing_active was still on.)  Grab
656*7c478bd9Sstevel@tonic-gate 	 * pidlock, and check the busy flag in all
657*7c478bd9Sstevel@tonic-gate 	 * TPDP's.
658*7c478bd9Sstevel@tonic-gate 	 */
659*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
660*7c478bd9Sstevel@tonic-gate 	t = curthread;
661*7c478bd9Sstevel@tonic-gate 	do {
662*7c478bd9Sstevel@tonic-gate 		if (t->t_tnf_tpdp != NULL) {
663*7c478bd9Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
664*7c478bd9Sstevel@tonic-gate 			tpdp = (tnf_ops_t *)t->t_tnf_tpdp;
665*7c478bd9Sstevel@tonic-gate 			if (LOCK_HELD(&tpdp->busy)) {
666*7c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
667*7c478bd9Sstevel@tonic-gate 				return (EBUSY);
668*7c478bd9Sstevel@tonic-gate 			}
669*7c478bd9Sstevel@tonic-gate 			tpdp->wcb.tnfw_w_pos.tnfw_w_block = NULL;
670*7c478bd9Sstevel@tonic-gate 			tpdp->wcb.tnfw_w_tag_pos.tnfw_w_block = NULL;
671*7c478bd9Sstevel@tonic-gate 			tpdp->schedule.record_p = NULL;
672*7c478bd9Sstevel@tonic-gate 		}
673*7c478bd9Sstevel@tonic-gate 		t = t->t_next;
674*7c478bd9Sstevel@tonic-gate 	} while (t != curthread);
675*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate 	/*
678*7c478bd9Sstevel@tonic-gate 	 * Zap all references to the buffer we're freeing.
679*7c478bd9Sstevel@tonic-gate 	 * Grab mod_lock while walking list to keep it
680*7c478bd9Sstevel@tonic-gate 	 * consistent.
681*7c478bd9Sstevel@tonic-gate 	 */
682*7c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
683*7c478bd9Sstevel@tonic-gate 	tagp = (tnf_tag_data_t *)__tnf_tag_list_head;
684*7c478bd9Sstevel@tonic-gate 	while (tagp != NULL) {
685*7c478bd9Sstevel@tonic-gate 		tagp->tag_index = 0;
686*7c478bd9Sstevel@tonic-gate 		tagp = (tnf_tag_data_t *)tagp->tag_version;
687*7c478bd9Sstevel@tonic-gate 	}
688*7c478bd9Sstevel@tonic-gate 	probep = (tnf_probe_control_t *)__tnf_probe_list_head;
689*7c478bd9Sstevel@tonic-gate 	while (probep != NULL) {
690*7c478bd9Sstevel@tonic-gate 		probep->index = 0;
691*7c478bd9Sstevel@tonic-gate 		probep = probep->next;
692*7c478bd9Sstevel@tonic-gate 	}
693*7c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
694*7c478bd9Sstevel@tonic-gate 
695*7c478bd9Sstevel@tonic-gate 	tnfw_b_state = TNFW_B_NOBUFFER | TNFW_B_STOPPED;
696*7c478bd9Sstevel@tonic-gate #if TNF_USE_KMA
697*7c478bd9Sstevel@tonic-gate 	kmem_free(tnf_buf, tnf_trace_file_size);
698*7c478bd9Sstevel@tonic-gate #else
699*7c478bd9Sstevel@tonic-gate 	segkp_release(segkp, tnf_buf);
700*7c478bd9Sstevel@tonic-gate #endif
701*7c478bd9Sstevel@tonic-gate 	tnf_buf = NULL;
702*7c478bd9Sstevel@tonic-gate 
703*7c478bd9Sstevel@tonic-gate 	return (0);
704*7c478bd9Sstevel@tonic-gate }
705*7c478bd9Sstevel@tonic-gate 
706*7c478bd9Sstevel@tonic-gate static int
707*7c478bd9Sstevel@tonic-gate tnf_settracing(int arg)
708*7c478bd9Sstevel@tonic-gate {
709*7c478bd9Sstevel@tonic-gate 	if (arg)
710*7c478bd9Sstevel@tonic-gate 		if (tnf_buf == NULL)
711*7c478bd9Sstevel@tonic-gate 			return (ENOMEM);
712*7c478bd9Sstevel@tonic-gate 		else
713*7c478bd9Sstevel@tonic-gate 			tnf_trace_on();
714*7c478bd9Sstevel@tonic-gate 	else
715*7c478bd9Sstevel@tonic-gate 		tnf_trace_off();
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate #ifdef _TNF_SPEED_TEST
718*7c478bd9Sstevel@tonic-gate #define	NITER	255
719*7c478bd9Sstevel@tonic-gate 	{
720*7c478bd9Sstevel@tonic-gate 		int i;
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < NITER; i++)
723*7c478bd9Sstevel@tonic-gate 			TNF_PROBE_0(tnf_speed_0, "tnf", /* CSTYLED */);
724*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < NITER; i++)
725*7c478bd9Sstevel@tonic-gate 			TNF_PROBE_1(tnf_speed_1, "tnf", /* CSTYLED */,
726*7c478bd9Sstevel@tonic-gate 			    tnf_long,	long,	i);
727*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < NITER; i++)
728*7c478bd9Sstevel@tonic-gate 			TNF_PROBE_2(tnf_speed_2, "tnf", /* CSTYLED */,
729*7c478bd9Sstevel@tonic-gate 			    tnf_long,	long1,	i,
730*7c478bd9Sstevel@tonic-gate 			    tnf_long,	long2,	i);
731*7c478bd9Sstevel@tonic-gate 	}
732*7c478bd9Sstevel@tonic-gate #endif /* _TNF_SPEED_TEST */
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate 	return (0);
735*7c478bd9Sstevel@tonic-gate }
736*7c478bd9Sstevel@tonic-gate 
737*7c478bd9Sstevel@tonic-gate static int
738*7c478bd9Sstevel@tonic-gate tnf_getpidstate(caddr_t arg, int mode)
739*7c478bd9Sstevel@tonic-gate {
740*7c478bd9Sstevel@tonic-gate 	int	err = 0;
741*7c478bd9Sstevel@tonic-gate 	pid_t	pid;
742*7c478bd9Sstevel@tonic-gate 	proc_t	*procp;
743*7c478bd9Sstevel@tonic-gate 	int	result;
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&pid, sizeof (pid), mode))
746*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
747*7c478bd9Sstevel@tonic-gate 
748*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
749*7c478bd9Sstevel@tonic-gate 	if ((procp = prfind(pid)) != NULL)
750*7c478bd9Sstevel@tonic-gate 		result = PROC_IS_FILTER(procp);
751*7c478bd9Sstevel@tonic-gate 	else
752*7c478bd9Sstevel@tonic-gate 		err = ESRCH;
753*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
754*7c478bd9Sstevel@tonic-gate 
755*7c478bd9Sstevel@tonic-gate 	if (!err)
756*7c478bd9Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&result, (caddr_t)arg,
757*7c478bd9Sstevel@tonic-gate 		    sizeof (result), mode))
758*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
759*7c478bd9Sstevel@tonic-gate 	return (err);
760*7c478bd9Sstevel@tonic-gate }
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
763*7c478bd9Sstevel@tonic-gate static int
764*7c478bd9Sstevel@tonic-gate tnf_setpidstate(int filterval, pid_t pid, int mode)
765*7c478bd9Sstevel@tonic-gate {
766*7c478bd9Sstevel@tonic-gate 	int	err = 0;
767*7c478bd9Sstevel@tonic-gate 	proc_t	*procp;
768*7c478bd9Sstevel@tonic-gate 
769*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
770*7c478bd9Sstevel@tonic-gate 	if ((procp = prfind(pid)) != NULL)
771*7c478bd9Sstevel@tonic-gate 		if (filterval)
772*7c478bd9Sstevel@tonic-gate 			PROC_FILTER_SET(procp);
773*7c478bd9Sstevel@tonic-gate 		else
774*7c478bd9Sstevel@tonic-gate 			PROC_FILTER_CLR(procp);
775*7c478bd9Sstevel@tonic-gate 	else
776*7c478bd9Sstevel@tonic-gate 		err = ESRCH;
777*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate 	return (err);
780*7c478bd9Sstevel@tonic-gate }
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate static int
783*7c478bd9Sstevel@tonic-gate tnf_pidfilterset(int mode)
784*7c478bd9Sstevel@tonic-gate {
785*7c478bd9Sstevel@tonic-gate 	tnf_probe_control_t	*p;
786*7c478bd9Sstevel@tonic-gate 	tnf_probe_test_func_t	func;
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 	tnf_drv_state.tnf_pidfilter_mode = mode;
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate 	/* Establish correct test func for each probe */
791*7c478bd9Sstevel@tonic-gate 	if (mode)
792*7c478bd9Sstevel@tonic-gate 		func = tnf_test_2;
793*7c478bd9Sstevel@tonic-gate 	else
794*7c478bd9Sstevel@tonic-gate 		func = tnf_test_1;
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
797*7c478bd9Sstevel@tonic-gate 	p = (tnf_probe_control_t *)__tnf_probe_list_head;
798*7c478bd9Sstevel@tonic-gate 	while (p != NULL) {
799*7c478bd9Sstevel@tonic-gate 		if (p->test_func != NULL)
800*7c478bd9Sstevel@tonic-gate 			p->test_func = func;
801*7c478bd9Sstevel@tonic-gate 		p = p->next;
802*7c478bd9Sstevel@tonic-gate 	}
803*7c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
804*7c478bd9Sstevel@tonic-gate 
805*7c478bd9Sstevel@tonic-gate 	return (0);
806*7c478bd9Sstevel@tonic-gate }
807*7c478bd9Sstevel@tonic-gate 
808*7c478bd9Sstevel@tonic-gate static int
809*7c478bd9Sstevel@tonic-gate tnf_pidfilterget(caddr_t dest, int mode)
810*7c478bd9Sstevel@tonic-gate {
811*7c478bd9Sstevel@tonic-gate 	int err = 0;
812*7c478bd9Sstevel@tonic-gate 	int filtercount = 0;
813*7c478bd9Sstevel@tonic-gate 	size_t	sz;
814*7c478bd9Sstevel@tonic-gate 	pid_t	*filterbuf, *bufp;
815*7c478bd9Sstevel@tonic-gate 	proc_t	*procp;
816*7c478bd9Sstevel@tonic-gate 
817*7c478bd9Sstevel@tonic-gate 	/* Count how many processes in filter set (upper bound) */
818*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
819*7c478bd9Sstevel@tonic-gate 	for (procp = practive; procp != NULL; procp = procp->p_next)
820*7c478bd9Sstevel@tonic-gate 		if (PROC_IS_FILTER(procp))
821*7c478bd9Sstevel@tonic-gate 			filtercount++;
822*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
823*7c478bd9Sstevel@tonic-gate 
824*7c478bd9Sstevel@tonic-gate 	/* Allocate temp space to hold filter set (upper bound) */
825*7c478bd9Sstevel@tonic-gate 	sz = sizeof (pid_t) * (filtercount + 1);
826*7c478bd9Sstevel@tonic-gate 	filterbuf = kmem_zalloc(sz, KM_SLEEP);
827*7c478bd9Sstevel@tonic-gate 
828*7c478bd9Sstevel@tonic-gate 	/*
829*7c478bd9Sstevel@tonic-gate 	 * NOTE: The filter set cannot grow between the first and
830*7c478bd9Sstevel@tonic-gate 	 * second acquisitions of pidlock.  This is currently true
831*7c478bd9Sstevel@tonic-gate 	 * because:
832*7c478bd9Sstevel@tonic-gate 	 *	1. /dev/tnfctl is exclusive open, so all driver
833*7c478bd9Sstevel@tonic-gate 	 *	   control operations, including changing the filter
834*7c478bd9Sstevel@tonic-gate 	 *	   set and this code, are effectively single-threaded.
835*7c478bd9Sstevel@tonic-gate 	 *	2. There is no in-kernel API to manipulate the filter
836*7c478bd9Sstevel@tonic-gate 	 *	   set (i.e. toggle the on/off bit in a proc struct).
837*7c478bd9Sstevel@tonic-gate 	 *	3. The proc filter bit is not inherited across a fork()
838*7c478bd9Sstevel@tonic-gate 	 *	   operation; the child starts with the bit off.
839*7c478bd9Sstevel@tonic-gate 	 * If any of these assumptions is invalidated, a possible
840*7c478bd9Sstevel@tonic-gate 	 * solution is to check whether we're overflowing the allocated
841*7c478bd9Sstevel@tonic-gate 	 * filterbuf below, and back out and restart from the beginning
842*7c478bd9Sstevel@tonic-gate 	 * if so.
843*7c478bd9Sstevel@tonic-gate 	 *
844*7c478bd9Sstevel@tonic-gate 	 * The code below handles the case when the filter set shrinks
845*7c478bd9Sstevel@tonic-gate 	 * due to processes exiting.
846*7c478bd9Sstevel@tonic-gate 	 */
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate 	/* Fill in filter set */
849*7c478bd9Sstevel@tonic-gate 	bufp = filterbuf + 1;	/* first word is for count */
850*7c478bd9Sstevel@tonic-gate 	filtercount = 0;	/* recomputed below */
851*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
852*7c478bd9Sstevel@tonic-gate 	for (procp = practive; procp != NULL; procp = procp->p_next) {
853*7c478bd9Sstevel@tonic-gate 		if (PROC_IS_FILTER(procp)) {
854*7c478bd9Sstevel@tonic-gate 			filtercount++;
855*7c478bd9Sstevel@tonic-gate 			*bufp++ = procp->p_pid;
856*7c478bd9Sstevel@tonic-gate 		}
857*7c478bd9Sstevel@tonic-gate 	}
858*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
859*7c478bd9Sstevel@tonic-gate 
860*7c478bd9Sstevel@tonic-gate 	/* Set filtercount */
861*7c478bd9Sstevel@tonic-gate 	*filterbuf = (pid_t)filtercount;
862*7c478bd9Sstevel@tonic-gate 
863*7c478bd9Sstevel@tonic-gate 	/* Copy out result */
864*7c478bd9Sstevel@tonic-gate 	if (ddi_copyout((caddr_t)filterbuf, dest, sz, mode))
865*7c478bd9Sstevel@tonic-gate 		err = EFAULT;
866*7c478bd9Sstevel@tonic-gate 
867*7c478bd9Sstevel@tonic-gate 	/* Free temp space */
868*7c478bd9Sstevel@tonic-gate 	kmem_free(filterbuf, sz);
869*7c478bd9Sstevel@tonic-gate 
870*7c478bd9Sstevel@tonic-gate 	return (err);
871*7c478bd9Sstevel@tonic-gate }
872*7c478bd9Sstevel@tonic-gate 
873*7c478bd9Sstevel@tonic-gate static int
874*7c478bd9Sstevel@tonic-gate tnf_getheader(caddr_t arg, int mode)
875*7c478bd9Sstevel@tonic-gate {
876*7c478bd9Sstevel@tonic-gate 	if (tnf_buf == NULL)
877*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
878*7c478bd9Sstevel@tonic-gate 	if (ddi_copyout(tnf_buf, arg, TNF_BLOCK_SIZE, mode))
879*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
880*7c478bd9Sstevel@tonic-gate 	return (0);
881*7c478bd9Sstevel@tonic-gate }
882*7c478bd9Sstevel@tonic-gate 
883*7c478bd9Sstevel@tonic-gate static int
884*7c478bd9Sstevel@tonic-gate tnf_getblock(caddr_t arg, int mode)
885*7c478bd9Sstevel@tonic-gate {
886*7c478bd9Sstevel@tonic-gate 	int		err = 0;
887*7c478bd9Sstevel@tonic-gate 	tifiocgblock_t	parms;
888*7c478bd9Sstevel@tonic-gate 	caddr_t		area;
889*7c478bd9Sstevel@tonic-gate 	tnf_block_header_t	*blk;
890*7c478bd9Sstevel@tonic-gate 
891*7c478bd9Sstevel@tonic-gate 	if (tnf_buf == NULL)
892*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
893*7c478bd9Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&parms, sizeof (parms), mode))
894*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
895*7c478bd9Sstevel@tonic-gate 	area = tnf_buf + TNF_DIRECTORY_SIZE +
896*7c478bd9Sstevel@tonic-gate 	    parms.block_num * TNF_BLOCK_SIZE;
897*7c478bd9Sstevel@tonic-gate 	if (area < tnf_buf + TNF_DIRECTORY_SIZE ||
898*7c478bd9Sstevel@tonic-gate 	    area >= tnf_buf + tnf_trace_file_size)
899*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
900*7c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast */
901*7c478bd9Sstevel@tonic-gate 	blk = (tnf_block_header_t *)area;
902*7c478bd9Sstevel@tonic-gate 	/*
903*7c478bd9Sstevel@tonic-gate 	 * B-lock the block while we're reading
904*7c478bd9Sstevel@tonic-gate 	 */
905*7c478bd9Sstevel@tonic-gate 	if (!lock_try(&blk->B_lock))
906*7c478bd9Sstevel@tonic-gate 		return (EBUSY);
907*7c478bd9Sstevel@tonic-gate 	if (ddi_copyout(area, parms.dst_addr, TNF_BLOCK_SIZE, mode))
908*7c478bd9Sstevel@tonic-gate 		err = EFAULT;
909*7c478bd9Sstevel@tonic-gate 	lock_clear(&blk->B_lock);
910*7c478bd9Sstevel@tonic-gate 	return (err);
911*7c478bd9Sstevel@tonic-gate }
912*7c478bd9Sstevel@tonic-gate 
913*7c478bd9Sstevel@tonic-gate static int
914*7c478bd9Sstevel@tonic-gate tnf_getfwzone(caddr_t arg, int mode)
915*7c478bd9Sstevel@tonic-gate {
916*7c478bd9Sstevel@tonic-gate 	tifiocgfw_t parms;
917*7c478bd9Sstevel@tonic-gate 
918*7c478bd9Sstevel@tonic-gate 	if (tnf_buf == NULL)
919*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
920*7c478bd9Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&parms, sizeof (parms), mode))
921*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
922*7c478bd9Sstevel@tonic-gate 	if (ddi_copyout(tnf_buf + TNF_BLOCK_SIZE + parms.start *
923*7c478bd9Sstevel@tonic-gate 	    sizeof (tnf_ref32_t), (caddr_t)parms.dst_addr,
924*7c478bd9Sstevel@tonic-gate 	    parms.slots * (int)(sizeof (tnf_ref32_t)), mode))
925*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
926*7c478bd9Sstevel@tonic-gate 	return (0);
927*7c478bd9Sstevel@tonic-gate }
928*7c478bd9Sstevel@tonic-gate 
929*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
930*7c478bd9Sstevel@tonic-gate static void *
931*7c478bd9Sstevel@tonic-gate tnf_test_1(void *tpdp, tnf_probe_control_t *probe_p, tnf_probe_setup_t *sp)
932*7c478bd9Sstevel@tonic-gate {
933*7c478bd9Sstevel@tonic-gate 	tpdp = (void *)curthread->t_tnf_tpdp;
934*7c478bd9Sstevel@tonic-gate 	if (tpdp != NULL)
935*7c478bd9Sstevel@tonic-gate 		return (tnf_trace_alloc((tnf_ops_t *)tpdp, probe_p, sp));
936*7c478bd9Sstevel@tonic-gate 	return (NULL);
937*7c478bd9Sstevel@tonic-gate }
938*7c478bd9Sstevel@tonic-gate 
939*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
940*7c478bd9Sstevel@tonic-gate static void *
941*7c478bd9Sstevel@tonic-gate tnf_test_2(void *tpdp, tnf_probe_control_t *probe_p, tnf_probe_setup_t *sp)
942*7c478bd9Sstevel@tonic-gate {
943*7c478bd9Sstevel@tonic-gate 	tpdp = (void *)curthread->t_tnf_tpdp;
944*7c478bd9Sstevel@tonic-gate 	if (tpdp != NULL && PROC_IS_FILTER(curproc))
945*7c478bd9Sstevel@tonic-gate 		return (tnf_trace_alloc((tnf_ops_t *)tpdp, probe_p, sp));
946*7c478bd9Sstevel@tonic-gate 	return (NULL);
947*7c478bd9Sstevel@tonic-gate }
948*7c478bd9Sstevel@tonic-gate 
949*7c478bd9Sstevel@tonic-gate #endif /* !NPROBE */
950