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