xref: /onnv-gate/usr/src/uts/common/tnf/tnf.c (revision 7656:2621e50fdf4a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
6*7656SSherry.Moore@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * tnf driver - provides probe control and kernel trace buffer access
290Sstevel@tonic-gate  * to the user programs prex and tnfxtract.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/file.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
370Sstevel@tonic-gate #include <sys/fcntl.h>
380Sstevel@tonic-gate #include <sys/uio.h>
390Sstevel@tonic-gate #include <sys/kmem.h>
400Sstevel@tonic-gate #include <sys/cred.h>
410Sstevel@tonic-gate #include <sys/mman.h>
420Sstevel@tonic-gate #include <sys/errno.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <sys/conf.h>
450Sstevel@tonic-gate #include <sys/ddi.h>
460Sstevel@tonic-gate #include <sys/sunddi.h>
470Sstevel@tonic-gate #include <sys/modctl.h>
480Sstevel@tonic-gate #include <sys/tnf.h>
490Sstevel@tonic-gate #include <sys/debug.h>
500Sstevel@tonic-gate #include <sys/devops.h>
510Sstevel@tonic-gate #include <vm/as.h>
520Sstevel@tonic-gate #include <vm/seg_kp.h>
530Sstevel@tonic-gate #include <sys/tnf_probe.h>
540Sstevel@tonic-gate #include <sys/kobj.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include "tnf_buf.h"
570Sstevel@tonic-gate #include "tnf_types.h"
580Sstevel@tonic-gate #include "tnf_trace.h"
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #ifndef NPROBE
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * Each probe is independently put in the kernel, prex uses
640Sstevel@tonic-gate  * __tnf_probe_list_head and __tnf_tag_list_head as pointers to linked list
650Sstevel@tonic-gate  * for probes and static tnf_tag_data_t, respectively.
660Sstevel@tonic-gate  * tnf used the elf relocation record to build a separate linked list for
670Sstevel@tonic-gate  * the probes and tnf_tag_data_t. We will describe how the linked list for
680Sstevel@tonic-gate  * __tnf_tag_list_head is made, the probe list is very similar.
690Sstevel@tonic-gate  * During the dynamic relocation(in uts/sparc/krtld/kobj_reloc.c),
700Sstevel@tonic-gate  * the &__tnf_tag_version_1(the first member in tnf_tag_data_t data struct)
710Sstevel@tonic-gate  * (and since it is a global variable which was never defined) will be filled
720Sstevel@tonic-gate  * with 0. The following code in kobj_reloc.c will get the address of current
730Sstevel@tonic-gate  * __tnf_tag_list_head and put it in value_p:
740Sstevel@tonic-gate  *   #define TAG_MARKER_SYMBOL       "__tnf_tag_version_1"
750Sstevel@tonic-gate  *   if (strcmp(symname, TAG_MARKER_SYMBOL) == 0) {
760Sstevel@tonic-gate  *       *addend_p = 0;
770Sstevel@tonic-gate  *       *value_p = (Addr) __tnf_tag_list_head; (value_p points to list head)
780Sstevel@tonic-gate  *       __tnf_tag_list_head = (void *)*offset_p;(list head is the next record)
790Sstevel@tonic-gate  *       return (0);
800Sstevel@tonic-gate  *   }
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  * the function do_reloc(in the kobj_reloc.c) will put vlaue_p into
830Sstevel@tonic-gate  * &__tnf_tag_version_1
840Sstevel@tonic-gate  * Now the &__tnf_tag_version_1 points to the last list head
850Sstevel@tonic-gate  * and __tnf_tag_list_head points to the new list head.
860Sstevel@tonic-gate  * This is equivalent to attatch a node at the beginning of the list.
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate extern tnf_probe_control_t *__tnf_probe_list_head;
900Sstevel@tonic-gate extern tnf_tag_data_t *__tnf_tag_list_head;
910Sstevel@tonic-gate extern int tnf_changed_probe_list;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate static int tnf_attach(dev_info_t *, ddi_attach_cmd_t);
940Sstevel@tonic-gate static int tnf_detach(dev_info_t *, ddi_detach_cmd_t);
950Sstevel@tonic-gate static int tnf_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
960Sstevel@tonic-gate static int tnf_open(dev_t *, int, int, struct cred *);
970Sstevel@tonic-gate static int tnf_close(dev_t, int, int, struct cred *);
980Sstevel@tonic-gate #ifdef UNUSED
990Sstevel@tonic-gate static int tnf_mmap(dev_t, off_t, int);
1000Sstevel@tonic-gate #endif
1010Sstevel@tonic-gate static int tnf_ioctl(dev_t, int, intptr_t, int, struct cred *, int *);
1020Sstevel@tonic-gate #ifdef UNUSED
1030Sstevel@tonic-gate static int tnf_prop_op(dev_t, dev_info_t *, ddi_prop_op_t,
1040Sstevel@tonic-gate     int, char *, caddr_t, int *);
1050Sstevel@tonic-gate #endif
1060Sstevel@tonic-gate static dev_info_t *tnf_devi;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static struct {
1090Sstevel@tonic-gate 	int		tnf_probe_count;
1100Sstevel@tonic-gate 	boolean_t	tnf_pidfilter_mode;
1110Sstevel@tonic-gate 	boolean_t	ctldev_is_open;
1120Sstevel@tonic-gate 	int		mapdev_open_count;
1130Sstevel@tonic-gate 	kmutex_t 	tnf_mtx;
1140Sstevel@tonic-gate } tnf_drv_state = { 0, B_FALSE, B_FALSE, 0 };
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static int tnf_getmaxprobe(caddr_t, int);
1170Sstevel@tonic-gate static int tnf_getprobevals(caddr_t, int);
1180Sstevel@tonic-gate static int tnf_getprobestring(caddr_t, int);
1190Sstevel@tonic-gate static int tnf_setprobevals(caddr_t, int);
1200Sstevel@tonic-gate static int tnf_getstate(caddr_t, int);
1210Sstevel@tonic-gate static int tnf_allocbuf(intptr_t);
1220Sstevel@tonic-gate static int tnf_deallocbuf(void);
1230Sstevel@tonic-gate static int tnf_settracing(int);
1240Sstevel@tonic-gate static int tnf_pidfilterset(int);
1250Sstevel@tonic-gate static int tnf_pidfilterget(caddr_t, int);
1260Sstevel@tonic-gate static int tnf_getpidstate(caddr_t, int);
1270Sstevel@tonic-gate static int tnf_setpidstate(int, pid_t, int);
1280Sstevel@tonic-gate static int tnf_getheader(caddr_t, int);
1290Sstevel@tonic-gate static int tnf_getblock(caddr_t, int);
1300Sstevel@tonic-gate static int tnf_getfwzone(caddr_t, int);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate static void *tnf_test_1(void *, tnf_probe_control_t *, tnf_probe_setup_t *);
1330Sstevel@tonic-gate static void *tnf_test_2(void *, tnf_probe_control_t *, tnf_probe_setup_t *);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate #define	TNFCTL_MINOR 0
1360Sstevel@tonic-gate #define	TNFMAP_MINOR 1
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate struct cb_ops	tnf_cb_ops = {
1390Sstevel@tonic-gate 	tnf_open,		/* open */
1400Sstevel@tonic-gate 	tnf_close,		/* close */
1410Sstevel@tonic-gate 	nodev,			/* strategy */
1420Sstevel@tonic-gate 	nodev,			/* print */
1430Sstevel@tonic-gate 	nodev,			/* dump */
1440Sstevel@tonic-gate 	nodev,			/* read */
1450Sstevel@tonic-gate 	nodev,			/* write */
1460Sstevel@tonic-gate 	tnf_ioctl,		/* ioctl */
1470Sstevel@tonic-gate 	nodev,			/* devmap */
1480Sstevel@tonic-gate 	nodev,			/* mmap */
1490Sstevel@tonic-gate 	nodev,			/* segmap */
1500Sstevel@tonic-gate 	nochpoll,		/* poll */
1510Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
1520Sstevel@tonic-gate 	0,			/* streamtab  */
1530Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
1540Sstevel@tonic-gate };
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate struct dev_ops	tnf_ops = {
1570Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1580Sstevel@tonic-gate 	0,			/* refcnt  */
1590Sstevel@tonic-gate 	tnf_info,		/* info */
1600Sstevel@tonic-gate 	nulldev,		/* identify */
1610Sstevel@tonic-gate 	nulldev,		/* probe */
1620Sstevel@tonic-gate 	tnf_attach,		/* attach */
1630Sstevel@tonic-gate 	tnf_detach,		/* detach */
1640Sstevel@tonic-gate 	nodev,			/* reset */
1650Sstevel@tonic-gate 	&tnf_cb_ops,		/* driver operations */
166*7656SSherry.Moore@Sun.COM 	(struct bus_ops *)0,	/* no bus operations */
167*7656SSherry.Moore@Sun.COM 	NULL,			/* power */
168*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1690Sstevel@tonic-gate };
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static struct modldrv modldrv = {
1740Sstevel@tonic-gate 	&mod_driverops,
175*7656SSherry.Moore@Sun.COM 	"kernel probes driver",
1760Sstevel@tonic-gate 	&tnf_ops,
1770Sstevel@tonic-gate };
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate static struct modlinkage modlinkage = {
1800Sstevel@tonic-gate 	MODREV_1,
1810Sstevel@tonic-gate 	(void *)&modldrv,
1820Sstevel@tonic-gate 	NULL
1830Sstevel@tonic-gate };
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate int
_init()1860Sstevel@tonic-gate _init()
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	register int error;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	mutex_init(&tnf_drv_state.tnf_mtx, NULL, MUTEX_DEFAULT, NULL);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
1930Sstevel@tonic-gate 		mutex_destroy(&tnf_drv_state.tnf_mtx);
1940Sstevel@tonic-gate 		return (error);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/* Give t0 a tpdp */
1980Sstevel@tonic-gate 	if (!t0.t_tnf_tpdp)
1990Sstevel@tonic-gate 		t0.t_tnf_tpdp = kmem_zalloc(sizeof (tnf_ops_t), KM_SLEEP);
2000Sstevel@tonic-gate 	/* Initialize tag system */
2010Sstevel@tonic-gate 	tnf_tag_core_init();
2020Sstevel@tonic-gate 	tnf_tag_trace_init();
2030Sstevel@tonic-gate 	tnf_changed_probe_list = 1;
2040Sstevel@tonic-gate 	return (0);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate int
_fini()2080Sstevel@tonic-gate _fini()
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	/* Not safe to unload this module, currently */
2110Sstevel@tonic-gate 	return (EBUSY);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2150Sstevel@tonic-gate _info(struct modinfo *modinfop)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /* ARGSUSED */
2210Sstevel@tonic-gate static int
tnf_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2220Sstevel@tonic-gate tnf_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	register int error;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	switch (infocmd) {
2270Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2280Sstevel@tonic-gate 		*result = (void *)tnf_devi;
2290Sstevel@tonic-gate 		error = DDI_SUCCESS;
2300Sstevel@tonic-gate 		break;
2310Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
2320Sstevel@tonic-gate 		*result = (void *)0;
2330Sstevel@tonic-gate 		error = DDI_SUCCESS;
2340Sstevel@tonic-gate 		break;
2350Sstevel@tonic-gate 	default:
2360Sstevel@tonic-gate 		error = DDI_FAILURE;
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	return (error);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static int
tnf_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2420Sstevel@tonic-gate tnf_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
2450Sstevel@tonic-gate 		return (DDI_FAILURE);
2460Sstevel@tonic-gate 	if ((ddi_create_minor_node(devi, "tnfctl", S_IFCHR, TNFCTL_MINOR,
2470Sstevel@tonic-gate 	    DDI_PSEUDO, NULL) == DDI_FAILURE) ||
2480Sstevel@tonic-gate 	    (ddi_create_minor_node(devi, "tnfmap", S_IFCHR, TNFMAP_MINOR,
249*7656SSherry.Moore@Sun.COM 	    DDI_PSEUDO, NULL) == DDI_FAILURE)) {
2500Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
2510Sstevel@tonic-gate 		return (DDI_FAILURE);
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 	tnf_devi = devi;
2540Sstevel@tonic-gate 	return (DDI_SUCCESS);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate static int
tnf_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)2580Sstevel@tonic-gate tnf_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2610Sstevel@tonic-gate 		return (DDI_FAILURE);
2620Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
2630Sstevel@tonic-gate 	return (DDI_SUCCESS);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate  * property operations. Return the size of the kernel trace buffer.  We
2680Sstevel@tonic-gate  * only handle size property requests.  Others are passed on.
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate #ifdef UNUSED
2710Sstevel@tonic-gate static int
tnf_prop_op(dev_t dev,dev_info_t * di,ddi_prop_op_t prop,int m,char * name,caddr_t valuep,int * lengthp)2720Sstevel@tonic-gate tnf_prop_op(dev_t dev, dev_info_t *di, ddi_prop_op_t prop,
2730Sstevel@tonic-gate     int m, char *name, caddr_t valuep, int *lengthp)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	int length, *retbuf, size;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (strcmp(name, "size") == 0) {
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		/* Don't need tnf_mtx, since mapdev_open_count > 0 */
2800Sstevel@tonic-gate 		size = tnf_trace_file_size;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		length = *lengthp;		/* get caller's length */
2830Sstevel@tonic-gate 		*lengthp = sizeof (int);	/* set caller's length */
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 		switch (prop) {
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		case PROP_LEN:
2880Sstevel@tonic-gate 			return (DDI_PROP_SUCCESS);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 		case PROP_LEN_AND_VAL_ALLOC:
2910Sstevel@tonic-gate 			retbuf = kmem_alloc(sizeof (int),
2920Sstevel@tonic-gate 			    (m & DDI_PROP_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP);
2930Sstevel@tonic-gate 			if (retbuf == NULL)
2940Sstevel@tonic-gate 				return (DDI_PROP_NO_MEMORY);
2950Sstevel@tonic-gate 			*(int **)valuep = retbuf;	/* set caller's buf */
2960Sstevel@tonic-gate 			*retbuf = size;
2970Sstevel@tonic-gate 			return (DDI_PROP_SUCCESS);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		case PROP_LEN_AND_VAL_BUF:
3000Sstevel@tonic-gate 			if (length < sizeof (int))
3010Sstevel@tonic-gate 				return (DDI_PROP_BUF_TOO_SMALL);
3020Sstevel@tonic-gate 			*(int *)valuep = size;
3030Sstevel@tonic-gate 			return (DDI_PROP_SUCCESS);
3040Sstevel@tonic-gate 		}
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop, m, name, valuep, lengthp));
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate #endif
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /* ARGSUSED */
3110Sstevel@tonic-gate static int
tnf_open(dev_t * devp,int flag,int otyp,struct cred * cred)3120Sstevel@tonic-gate tnf_open(dev_t *devp, int flag, int otyp, struct cred *cred)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	int err = 0;
3150Sstevel@tonic-gate 	mutex_enter(&tnf_drv_state.tnf_mtx);
3160Sstevel@tonic-gate 	if (getminor(*devp) == TNFCTL_MINOR) {
3170Sstevel@tonic-gate 		if (tnf_drv_state.ctldev_is_open)
3180Sstevel@tonic-gate 			err = EBUSY;
3190Sstevel@tonic-gate 		else {
3200Sstevel@tonic-gate 			tnf_drv_state.ctldev_is_open = B_TRUE;
3210Sstevel@tonic-gate 			/* stop autounloading -- XXX temporary */
3220Sstevel@tonic-gate 			modunload_disable();
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 	} else {
3250Sstevel@tonic-gate 		/* ASSERT(getminor(*devp) == TNFMAP_MINOR) */
3260Sstevel@tonic-gate 		++tnf_drv_state.mapdev_open_count;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 	mutex_exit(&tnf_drv_state.tnf_mtx);
3290Sstevel@tonic-gate 	return (err);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /* ARGSUSED */
3330Sstevel@tonic-gate static int
tnf_close(dev_t dev,int flag,int otyp,struct cred * cred)3340Sstevel@tonic-gate tnf_close(dev_t dev, int flag, int otyp, struct cred *cred)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	if (getminor(dev) == TNFCTL_MINOR) {
3370Sstevel@tonic-gate 		/*
3380Sstevel@tonic-gate 		 * Request the reenablement of autounloading
3390Sstevel@tonic-gate 		 */
3400Sstevel@tonic-gate 		modunload_enable();
3410Sstevel@tonic-gate 		tnf_drv_state.ctldev_is_open = B_FALSE;
3420Sstevel@tonic-gate 	} else {
3430Sstevel@tonic-gate 		/* ASSERT(getminor(dev) == TNFMAP_MINOR) */
3440Sstevel@tonic-gate 		/*
3450Sstevel@tonic-gate 		 * Unconditionally zero the open count since close()
3460Sstevel@tonic-gate 		 * is called when last client closes the device.
3470Sstevel@tonic-gate 		 */
3480Sstevel@tonic-gate 		tnf_drv_state.mapdev_open_count = 0;
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 	return (0);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate  * return the address of the image referenced by dev.
3550Sstevel@tonic-gate  *
3560Sstevel@tonic-gate  * 1191344: aliasing problem on VAC machines.  It could be made to
3570Sstevel@tonic-gate  * work by ensuring that tnf_buf is allocated on a vac_size boundary.
3580Sstevel@tonic-gate  */
3590Sstevel@tonic-gate #ifdef UNUSED
3600Sstevel@tonic-gate /*ARGSUSED*/
3610Sstevel@tonic-gate static int
tnf_mmap(dev_t dev,off_t off,int prot)3620Sstevel@tonic-gate tnf_mmap(dev_t dev, off_t off, int prot)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	register caddr_t addr;
3650Sstevel@tonic-gate 	register caddr_t pg_offset;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	if (getminor(dev) != TNFMAP_MINOR)
3680Sstevel@tonic-gate 		return (-1);
3690Sstevel@tonic-gate 	if (tnf_buf == 0 || off >= tnf_trace_file_size) {
3700Sstevel@tonic-gate 		return (-1);
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	addr = tnf_buf;
3740Sstevel@tonic-gate 	pg_offset = (caddr_t)((ulong_t)addr + (ulong_t)off);
3750Sstevel@tonic-gate 	return ((int)hat_getpfnum(kas.a_hat, pg_offset));
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate #endif
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate /*ARGSUSED4*/
3800Sstevel@tonic-gate static int
tnf_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)3810Sstevel@tonic-gate tnf_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
3820Sstevel@tonic-gate 	cred_t *credp, int *rvalp)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	int filterval = 1;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	if ((mode & FMODELS) != FNATIVE)
387*7656SSherry.Moore@Sun.COM 		return (ENOTSUP);
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (getminor(dev) != TNFCTL_MINOR &&
3900Sstevel@tonic-gate 	    cmd != TIFIOCGSTATE &&
3910Sstevel@tonic-gate 	    cmd != TIFIOCGHEADER &&
3920Sstevel@tonic-gate 	    cmd != TIFIOCGBLOCK &&
3930Sstevel@tonic-gate 	    cmd != TIFIOCGFWZONE)
3940Sstevel@tonic-gate 		return (EINVAL);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	switch (cmd) {
3970Sstevel@tonic-gate 	case TIFIOCGMAXPROBE:
3980Sstevel@tonic-gate 		return (tnf_getmaxprobe((caddr_t)arg, mode));
3990Sstevel@tonic-gate 	case TIFIOCGPROBEVALS:
4000Sstevel@tonic-gate 		return (tnf_getprobevals((caddr_t)arg, mode));
4010Sstevel@tonic-gate 	case TIFIOCGPROBESTRING:
4020Sstevel@tonic-gate 		return (tnf_getprobestring((caddr_t)arg, mode));
4030Sstevel@tonic-gate 	case TIFIOCSPROBEVALS:
4040Sstevel@tonic-gate 		return (tnf_setprobevals((caddr_t)arg, mode));
4050Sstevel@tonic-gate 	case TIFIOCGSTATE:
4060Sstevel@tonic-gate 		return (tnf_getstate((caddr_t)arg, mode));
4070Sstevel@tonic-gate 	case TIFIOCALLOCBUF:
4080Sstevel@tonic-gate 		return (tnf_allocbuf(arg));
4090Sstevel@tonic-gate 	case TIFIOCDEALLOCBUF:
4100Sstevel@tonic-gate 		return (tnf_deallocbuf());
4110Sstevel@tonic-gate 	case TIFIOCSTRACING:
4120Sstevel@tonic-gate 		/* LINTED cast from 64-bit integer to 32-bit integer */
4130Sstevel@tonic-gate 		return (tnf_settracing((int)arg));
4140Sstevel@tonic-gate 	case TIFIOCSPIDFILTER:
4150Sstevel@tonic-gate 		/* LINTED cast from 64-bit integer to 32-bit integer */
4160Sstevel@tonic-gate 		return (tnf_pidfilterset((int)arg));
4170Sstevel@tonic-gate 	case TIFIOCGPIDSTATE:
4180Sstevel@tonic-gate 		return (tnf_getpidstate((caddr_t)arg, mode));
4190Sstevel@tonic-gate 	case TIFIOCSPIDOFF:
4200Sstevel@tonic-gate 		filterval = 0;
4210Sstevel@tonic-gate 		/*FALLTHROUGH*/
4220Sstevel@tonic-gate 	case TIFIOCSPIDON:
4230Sstevel@tonic-gate 		/* LINTED cast from 64-bit integer to 32-bit integer */
4240Sstevel@tonic-gate 		return (tnf_setpidstate(filterval, (pid_t)arg, mode));
4250Sstevel@tonic-gate 	case TIFIOCPIDFILTERGET:
4260Sstevel@tonic-gate 		return (tnf_pidfilterget((caddr_t)arg, mode));
4270Sstevel@tonic-gate 	case TIFIOCGHEADER:
4280Sstevel@tonic-gate 		return (tnf_getheader((caddr_t)arg, mode));
4290Sstevel@tonic-gate 	case TIFIOCGBLOCK:
4300Sstevel@tonic-gate 		return (tnf_getblock((caddr_t)arg, mode));
4310Sstevel@tonic-gate 	case TIFIOCGFWZONE:
4320Sstevel@tonic-gate 		return (tnf_getfwzone((caddr_t)arg, mode));
4330Sstevel@tonic-gate 	default:
4340Sstevel@tonic-gate 		return (EINVAL);
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate /*
4390Sstevel@tonic-gate  * ioctls
4400Sstevel@tonic-gate  */
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate static int
tnf_getmaxprobe(caddr_t arg,int mode)4430Sstevel@tonic-gate tnf_getmaxprobe(caddr_t arg, int mode)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	tnf_probe_control_t *p;
4460Sstevel@tonic-gate 	/*
4470Sstevel@tonic-gate 	 * XXX Still not right for module unload -- just counting
4480Sstevel@tonic-gate 	 * the probes is not enough
4490Sstevel@tonic-gate 	 */
4500Sstevel@tonic-gate 	if (tnf_changed_probe_list) {
4510Sstevel@tonic-gate 		mutex_enter(&mod_lock);
4520Sstevel@tonic-gate 		tnf_changed_probe_list = 0;
4530Sstevel@tonic-gate 		tnf_drv_state.tnf_probe_count = 0;
4540Sstevel@tonic-gate 		for (p = (tnf_probe_control_t *)__tnf_probe_list_head;
4550Sstevel@tonic-gate 		    p != 0; p = p->next)
4560Sstevel@tonic-gate 			++tnf_drv_state.tnf_probe_count;
4570Sstevel@tonic-gate 		mutex_exit(&mod_lock);
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 	if (ddi_copyout((caddr_t)&tnf_drv_state.tnf_probe_count,
4600Sstevel@tonic-gate 	    arg, sizeof (tnf_drv_state.tnf_probe_count), mode))
4610Sstevel@tonic-gate 		return (EFAULT);
4620Sstevel@tonic-gate 	return (0);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate static int
tnf_getprobevals(caddr_t arg,int mode)4660Sstevel@tonic-gate tnf_getprobevals(caddr_t arg, int mode)
4670Sstevel@tonic-gate {
4680Sstevel@tonic-gate 	tnf_probevals_t probebuf;
4690Sstevel@tonic-gate 	tnf_probe_control_t *p;
4700Sstevel@tonic-gate 	int i, retval = 0;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&probebuf, sizeof (probebuf), mode))
4730Sstevel@tonic-gate 		return (EFAULT);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	mutex_enter(&mod_lock);
4760Sstevel@tonic-gate 	for (i = 1, p = (tnf_probe_control_t *)__tnf_probe_list_head;
477*7656SSherry.Moore@Sun.COM 	    p != NULL && i != probebuf.probenum;
478*7656SSherry.Moore@Sun.COM 	    ++i, p = p->next)
4790Sstevel@tonic-gate 		;
4800Sstevel@tonic-gate 	if (p == NULL)
4810Sstevel@tonic-gate 		retval = ENOENT;
4820Sstevel@tonic-gate 	else {
4830Sstevel@tonic-gate 		probebuf.enabled = (p->test_func != NULL);
4840Sstevel@tonic-gate 		probebuf.traced = (p->probe_func == tnf_trace_commit);
4850Sstevel@tonic-gate 		/* LINTED assignment of 64-bit integer to 32-bit integer */
4860Sstevel@tonic-gate 		probebuf.attrsize = strlen(p->attrs) + 1;
4870Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&probebuf,
4880Sstevel@tonic-gate 		    arg, sizeof (probebuf), mode))
4890Sstevel@tonic-gate 			retval = EFAULT;
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 	mutex_exit(&mod_lock);
4920Sstevel@tonic-gate 	return (retval);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate static int
tnf_getprobestring(caddr_t arg,int mode)4960Sstevel@tonic-gate tnf_getprobestring(caddr_t arg, int mode)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	tnf_probevals_t probebuf;
4990Sstevel@tonic-gate 	tnf_probe_control_t *p;
5000Sstevel@tonic-gate 	int i, retval = 0;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&probebuf, sizeof (probebuf), mode))
5030Sstevel@tonic-gate 		return (EFAULT);
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	mutex_enter(&mod_lock);
5060Sstevel@tonic-gate 	for (i = 1, p = (tnf_probe_control_t *)__tnf_probe_list_head;
507*7656SSherry.Moore@Sun.COM 	    p != NULL && i != probebuf.probenum;
508*7656SSherry.Moore@Sun.COM 	    ++i, p = p->next)
5090Sstevel@tonic-gate 		;
5100Sstevel@tonic-gate 	if (p == NULL)
5110Sstevel@tonic-gate 		retval = ENOENT;
5120Sstevel@tonic-gate 	else if (ddi_copyout((caddr_t)p->attrs,
5130Sstevel@tonic-gate 	    arg, strlen(p->attrs) + 1, mode))
5140Sstevel@tonic-gate 		retval = EFAULT;
5150Sstevel@tonic-gate 	mutex_exit(&mod_lock);
5160Sstevel@tonic-gate 	return (retval);
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate static int
tnf_setprobevals(caddr_t arg,int mode)5200Sstevel@tonic-gate tnf_setprobevals(caddr_t arg, int mode)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	tnf_probevals_t probebuf;
5230Sstevel@tonic-gate 	tnf_probe_control_t *p;
5240Sstevel@tonic-gate 	int i, retval = 0;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&probebuf, sizeof (probebuf), mode))
5270Sstevel@tonic-gate 		return (EFAULT);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	mutex_enter(&mod_lock);
5300Sstevel@tonic-gate 	for (i = 1, p = (tnf_probe_control_t *)__tnf_probe_list_head;
531*7656SSherry.Moore@Sun.COM 	    p != NULL && i != probebuf.probenum;
532*7656SSherry.Moore@Sun.COM 	    ++i, p = p->next)
5330Sstevel@tonic-gate 		;
5340Sstevel@tonic-gate 	if (p == NULL)
5350Sstevel@tonic-gate 		retval = ENOENT;
5360Sstevel@tonic-gate 	else {
5370Sstevel@tonic-gate 		/*
5380Sstevel@tonic-gate 		 * First do trace, then enable.
5390Sstevel@tonic-gate 		 * Set test_func last.
5400Sstevel@tonic-gate 		 */
5410Sstevel@tonic-gate 		if (probebuf.traced)
5420Sstevel@tonic-gate 			p->probe_func = tnf_trace_commit;
5430Sstevel@tonic-gate 		else
5440Sstevel@tonic-gate 			p->probe_func = tnf_trace_rollback;
5450Sstevel@tonic-gate 		if (probebuf.enabled) {
5460Sstevel@tonic-gate 			p->alloc_func = tnf_trace_alloc;
5470Sstevel@tonic-gate 			/* this must be set last */
5480Sstevel@tonic-gate 			if (tnf_drv_state.tnf_pidfilter_mode)
5490Sstevel@tonic-gate 				p->test_func = tnf_test_2;
5500Sstevel@tonic-gate 			else
5510Sstevel@tonic-gate 				p->test_func = tnf_test_1;
5520Sstevel@tonic-gate 		} else
5530Sstevel@tonic-gate 			p->test_func = NULL;
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 	mutex_exit(&mod_lock);
5560Sstevel@tonic-gate 	return (retval);
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate static int
tnf_getstate(caddr_t arg,int mode)5600Sstevel@tonic-gate tnf_getstate(caddr_t arg, int mode)
5610Sstevel@tonic-gate {
5620Sstevel@tonic-gate 	tifiocstate_t	tstate;
5630Sstevel@tonic-gate 	proc_t		*procp;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	if (tnf_buf == NULL) {
5660Sstevel@tonic-gate 		tstate.buffer_state = TIFIOCBUF_NONE;
5670Sstevel@tonic-gate 		tstate.buffer_size = 0;
5680Sstevel@tonic-gate 	} else {
5690Sstevel@tonic-gate 		switch (tnfw_b_state & ~TNFW_B_STOPPED) {
5700Sstevel@tonic-gate 		case TNFW_B_RUNNING:
5710Sstevel@tonic-gate 			tstate.buffer_state = TIFIOCBUF_OK;
5720Sstevel@tonic-gate 			break;
5730Sstevel@tonic-gate 		case TNFW_B_NOBUFFER:
5740Sstevel@tonic-gate 			tstate.buffer_state = TIFIOCBUF_UNINIT;
5750Sstevel@tonic-gate 			break;
5760Sstevel@tonic-gate 		case TNFW_B_BROKEN:
5770Sstevel@tonic-gate 			tstate.buffer_state = TIFIOCBUF_BROKEN;
5780Sstevel@tonic-gate 			break;
5790Sstevel@tonic-gate 		}
5800Sstevel@tonic-gate 		/* LINTED assignment of 64-bit integer to 32-bit integer */
5810Sstevel@tonic-gate 		tstate.buffer_size = tnf_trace_file_size;
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 	tstate.trace_stopped = tnfw_b_state & TNFW_B_STOPPED;
5840Sstevel@tonic-gate 	tstate.pidfilter_mode = tnf_drv_state.tnf_pidfilter_mode;
5850Sstevel@tonic-gate 	tstate.pidfilter_size = 0;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	mutex_enter(&pidlock);
5880Sstevel@tonic-gate 	for (procp = practive; procp != NULL; procp = procp->p_next)
5890Sstevel@tonic-gate 		if (PROC_IS_FILTER(procp))
5900Sstevel@tonic-gate 			tstate.pidfilter_size++;
5910Sstevel@tonic-gate 	mutex_exit(&pidlock);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	if (ddi_copyout((caddr_t)&tstate, arg, sizeof (tstate), mode))
5940Sstevel@tonic-gate 		return (EFAULT);
5950Sstevel@tonic-gate 	return (0);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate static int
tnf_allocbuf(intptr_t arg)5990Sstevel@tonic-gate tnf_allocbuf(intptr_t arg)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	size_t bufsz;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if (tnf_buf != NULL)
6040Sstevel@tonic-gate 		return (EBUSY);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	bufsz = roundup((size_t)arg, PAGESIZE);
6070Sstevel@tonic-gate 	/*
6080Sstevel@tonic-gate 	 * Validate size
6090Sstevel@tonic-gate 	 * XXX Take kernel VM into consideration as well
6100Sstevel@tonic-gate 	 */
6110Sstevel@tonic-gate 	/* bug fix #4057599 if (bufsz > (physmem << PAGESHIFT) / 2) */
6120Sstevel@tonic-gate 	if (btop(bufsz) > (physmem / 2))
6130Sstevel@tonic-gate 		return (ENOMEM);
6140Sstevel@tonic-gate 	if (bufsz < TNF_TRACE_FILE_MIN)
6150Sstevel@tonic-gate 		bufsz = TNF_TRACE_FILE_MIN;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate #if TNF_USE_KMA
6180Sstevel@tonic-gate 	tnf_buf = kmem_zalloc(bufsz, KM_SLEEP);
6190Sstevel@tonic-gate #else
6200Sstevel@tonic-gate 	/* LINTED cast from 64-bit integer to 32-bit intege */
6210Sstevel@tonic-gate 	tnf_buf = segkp_get(segkp, (int)bufsz,
622*7656SSherry.Moore@Sun.COM 	    KPD_ZERO | KPD_LOCKED | KPD_NO_ANON);
6230Sstevel@tonic-gate #endif
6240Sstevel@tonic-gate 	if (tnf_buf == NULL)
6250Sstevel@tonic-gate 		return (ENOMEM);
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	tnf_trace_file_size = bufsz;
6280Sstevel@tonic-gate 	tnf_trace_init();
6290Sstevel@tonic-gate 	return (0);
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate /*
6330Sstevel@tonic-gate  * Process a "deallocate buffer" ioctl request.  Tracing must be turned
6340Sstevel@tonic-gate  * off.  We must clear references to the buffer from the tag sites;
6350Sstevel@tonic-gate  * invalidate all threads' notions of block ownership; make sure nobody
6360Sstevel@tonic-gate  * is executing a probe (they might have started before tracing was
6370Sstevel@tonic-gate  * turned off); and free the buffer.
6380Sstevel@tonic-gate  */
6390Sstevel@tonic-gate static int
tnf_deallocbuf(void)6400Sstevel@tonic-gate tnf_deallocbuf(void)
6410Sstevel@tonic-gate {
6420Sstevel@tonic-gate 	tnf_ops_t *tpdp;
6430Sstevel@tonic-gate 	kthread_t *t;
6440Sstevel@tonic-gate 	tnf_probe_control_t *probep;
6450Sstevel@tonic-gate 	tnf_tag_data_t *tagp;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	if (tnf_drv_state.mapdev_open_count > 0 || tnf_tracing_active)
6480Sstevel@tonic-gate 		return (EBUSY);
6490Sstevel@tonic-gate 	if (tnf_buf == NULL)
6500Sstevel@tonic-gate 		return (ENOMEM);
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	/*
6530Sstevel@tonic-gate 	 * Make sure nobody is executing a probe.
6540Sstevel@tonic-gate 	 * (They could be if they got started while
6550Sstevel@tonic-gate 	 * tnf_tracing_active was still on.)  Grab
6560Sstevel@tonic-gate 	 * pidlock, and check the busy flag in all
6570Sstevel@tonic-gate 	 * TPDP's.
6580Sstevel@tonic-gate 	 */
6590Sstevel@tonic-gate 	mutex_enter(&pidlock);
6600Sstevel@tonic-gate 	t = curthread;
6610Sstevel@tonic-gate 	do {
6620Sstevel@tonic-gate 		if (t->t_tnf_tpdp != NULL) {
6630Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
6640Sstevel@tonic-gate 			tpdp = (tnf_ops_t *)t->t_tnf_tpdp;
6650Sstevel@tonic-gate 			if (LOCK_HELD(&tpdp->busy)) {
6660Sstevel@tonic-gate 				mutex_exit(&pidlock);
6670Sstevel@tonic-gate 				return (EBUSY);
6680Sstevel@tonic-gate 			}
6690Sstevel@tonic-gate 			tpdp->wcb.tnfw_w_pos.tnfw_w_block = NULL;
6700Sstevel@tonic-gate 			tpdp->wcb.tnfw_w_tag_pos.tnfw_w_block = NULL;
6710Sstevel@tonic-gate 			tpdp->schedule.record_p = NULL;
6720Sstevel@tonic-gate 		}
6730Sstevel@tonic-gate 		t = t->t_next;
6740Sstevel@tonic-gate 	} while (t != curthread);
6750Sstevel@tonic-gate 	mutex_exit(&pidlock);
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	/*
6780Sstevel@tonic-gate 	 * Zap all references to the buffer we're freeing.
6790Sstevel@tonic-gate 	 * Grab mod_lock while walking list to keep it
6800Sstevel@tonic-gate 	 * consistent.
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	mutex_enter(&mod_lock);
6830Sstevel@tonic-gate 	tagp = (tnf_tag_data_t *)__tnf_tag_list_head;
6840Sstevel@tonic-gate 	while (tagp != NULL) {
6850Sstevel@tonic-gate 		tagp->tag_index = 0;
6860Sstevel@tonic-gate 		tagp = (tnf_tag_data_t *)tagp->tag_version;
6870Sstevel@tonic-gate 	}
6880Sstevel@tonic-gate 	probep = (tnf_probe_control_t *)__tnf_probe_list_head;
6890Sstevel@tonic-gate 	while (probep != NULL) {
6900Sstevel@tonic-gate 		probep->index = 0;
6910Sstevel@tonic-gate 		probep = probep->next;
6920Sstevel@tonic-gate 	}
6930Sstevel@tonic-gate 	mutex_exit(&mod_lock);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	tnfw_b_state = TNFW_B_NOBUFFER | TNFW_B_STOPPED;
6960Sstevel@tonic-gate #if TNF_USE_KMA
6970Sstevel@tonic-gate 	kmem_free(tnf_buf, tnf_trace_file_size);
6980Sstevel@tonic-gate #else
6990Sstevel@tonic-gate 	segkp_release(segkp, tnf_buf);
7000Sstevel@tonic-gate #endif
7010Sstevel@tonic-gate 	tnf_buf = NULL;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	return (0);
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate static int
tnf_settracing(int arg)7070Sstevel@tonic-gate tnf_settracing(int arg)
7080Sstevel@tonic-gate {
7090Sstevel@tonic-gate 	if (arg)
7100Sstevel@tonic-gate 		if (tnf_buf == NULL)
7110Sstevel@tonic-gate 			return (ENOMEM);
7120Sstevel@tonic-gate 		else
7130Sstevel@tonic-gate 			tnf_trace_on();
7140Sstevel@tonic-gate 	else
7150Sstevel@tonic-gate 		tnf_trace_off();
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate #ifdef _TNF_SPEED_TEST
7180Sstevel@tonic-gate #define	NITER	255
7190Sstevel@tonic-gate 	{
7200Sstevel@tonic-gate 		int i;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 		for (i = 0; i < NITER; i++)
7230Sstevel@tonic-gate 			TNF_PROBE_0(tnf_speed_0, "tnf", /* CSTYLED */);
7240Sstevel@tonic-gate 		for (i = 0; i < NITER; i++)
7250Sstevel@tonic-gate 			TNF_PROBE_1(tnf_speed_1, "tnf", /* CSTYLED */,
7260Sstevel@tonic-gate 			    tnf_long,	long,	i);
7270Sstevel@tonic-gate 		for (i = 0; i < NITER; i++)
7280Sstevel@tonic-gate 			TNF_PROBE_2(tnf_speed_2, "tnf", /* CSTYLED */,
7290Sstevel@tonic-gate 			    tnf_long,	long1,	i,
7300Sstevel@tonic-gate 			    tnf_long,	long2,	i);
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate #endif /* _TNF_SPEED_TEST */
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	return (0);
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate static int
tnf_getpidstate(caddr_t arg,int mode)7380Sstevel@tonic-gate tnf_getpidstate(caddr_t arg, int mode)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	int	err = 0;
7410Sstevel@tonic-gate 	pid_t	pid;
7420Sstevel@tonic-gate 	proc_t	*procp;
7430Sstevel@tonic-gate 	int	result;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&pid, sizeof (pid), mode))
7460Sstevel@tonic-gate 		return (EFAULT);
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	mutex_enter(&pidlock);
7490Sstevel@tonic-gate 	if ((procp = prfind(pid)) != NULL)
7500Sstevel@tonic-gate 		result = PROC_IS_FILTER(procp);
7510Sstevel@tonic-gate 	else
7520Sstevel@tonic-gate 		err = ESRCH;
7530Sstevel@tonic-gate 	mutex_exit(&pidlock);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	if (!err)
7560Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&result, (caddr_t)arg,
7570Sstevel@tonic-gate 		    sizeof (result), mode))
7580Sstevel@tonic-gate 			return (EFAULT);
7590Sstevel@tonic-gate 	return (err);
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate /*ARGSUSED*/
7630Sstevel@tonic-gate static int
tnf_setpidstate(int filterval,pid_t pid,int mode)7640Sstevel@tonic-gate tnf_setpidstate(int filterval, pid_t pid, int mode)
7650Sstevel@tonic-gate {
7660Sstevel@tonic-gate 	int	err = 0;
7670Sstevel@tonic-gate 	proc_t	*procp;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	mutex_enter(&pidlock);
7700Sstevel@tonic-gate 	if ((procp = prfind(pid)) != NULL)
7710Sstevel@tonic-gate 		if (filterval)
7720Sstevel@tonic-gate 			PROC_FILTER_SET(procp);
7730Sstevel@tonic-gate 		else
7740Sstevel@tonic-gate 			PROC_FILTER_CLR(procp);
7750Sstevel@tonic-gate 	else
7760Sstevel@tonic-gate 		err = ESRCH;
7770Sstevel@tonic-gate 	mutex_exit(&pidlock);
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	return (err);
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate static int
tnf_pidfilterset(int mode)7830Sstevel@tonic-gate tnf_pidfilterset(int mode)
7840Sstevel@tonic-gate {
7850Sstevel@tonic-gate 	tnf_probe_control_t	*p;
7860Sstevel@tonic-gate 	tnf_probe_test_func_t	func;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	tnf_drv_state.tnf_pidfilter_mode = mode;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	/* Establish correct test func for each probe */
7910Sstevel@tonic-gate 	if (mode)
7920Sstevel@tonic-gate 		func = tnf_test_2;
7930Sstevel@tonic-gate 	else
7940Sstevel@tonic-gate 		func = tnf_test_1;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	mutex_enter(&mod_lock);
7970Sstevel@tonic-gate 	p = (tnf_probe_control_t *)__tnf_probe_list_head;
7980Sstevel@tonic-gate 	while (p != NULL) {
7990Sstevel@tonic-gate 		if (p->test_func != NULL)
8000Sstevel@tonic-gate 			p->test_func = func;
8010Sstevel@tonic-gate 		p = p->next;
8020Sstevel@tonic-gate 	}
8030Sstevel@tonic-gate 	mutex_exit(&mod_lock);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	return (0);
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate static int
tnf_pidfilterget(caddr_t dest,int mode)8090Sstevel@tonic-gate tnf_pidfilterget(caddr_t dest, int mode)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate 	int err = 0;
8120Sstevel@tonic-gate 	int filtercount = 0;
8130Sstevel@tonic-gate 	size_t	sz;
8140Sstevel@tonic-gate 	pid_t	*filterbuf, *bufp;
8150Sstevel@tonic-gate 	proc_t	*procp;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/* Count how many processes in filter set (upper bound) */
8180Sstevel@tonic-gate 	mutex_enter(&pidlock);
8190Sstevel@tonic-gate 	for (procp = practive; procp != NULL; procp = procp->p_next)
8200Sstevel@tonic-gate 		if (PROC_IS_FILTER(procp))
8210Sstevel@tonic-gate 			filtercount++;
8220Sstevel@tonic-gate 	mutex_exit(&pidlock);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	/* Allocate temp space to hold filter set (upper bound) */
8250Sstevel@tonic-gate 	sz = sizeof (pid_t) * (filtercount + 1);
8260Sstevel@tonic-gate 	filterbuf = kmem_zalloc(sz, KM_SLEEP);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	/*
8290Sstevel@tonic-gate 	 * NOTE: The filter set cannot grow between the first and
8300Sstevel@tonic-gate 	 * second acquisitions of pidlock.  This is currently true
8310Sstevel@tonic-gate 	 * because:
8320Sstevel@tonic-gate 	 *	1. /dev/tnfctl is exclusive open, so all driver
8330Sstevel@tonic-gate 	 *	   control operations, including changing the filter
8340Sstevel@tonic-gate 	 *	   set and this code, are effectively single-threaded.
8350Sstevel@tonic-gate 	 *	2. There is no in-kernel API to manipulate the filter
8360Sstevel@tonic-gate 	 *	   set (i.e. toggle the on/off bit in a proc struct).
8370Sstevel@tonic-gate 	 *	3. The proc filter bit is not inherited across a fork()
8380Sstevel@tonic-gate 	 *	   operation; the child starts with the bit off.
8390Sstevel@tonic-gate 	 * If any of these assumptions is invalidated, a possible
8400Sstevel@tonic-gate 	 * solution is to check whether we're overflowing the allocated
8410Sstevel@tonic-gate 	 * filterbuf below, and back out and restart from the beginning
8420Sstevel@tonic-gate 	 * if so.
8430Sstevel@tonic-gate 	 *
8440Sstevel@tonic-gate 	 * The code below handles the case when the filter set shrinks
8450Sstevel@tonic-gate 	 * due to processes exiting.
8460Sstevel@tonic-gate 	 */
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	/* Fill in filter set */
8490Sstevel@tonic-gate 	bufp = filterbuf + 1;	/* first word is for count */
8500Sstevel@tonic-gate 	filtercount = 0;	/* recomputed below */
8510Sstevel@tonic-gate 	mutex_enter(&pidlock);
8520Sstevel@tonic-gate 	for (procp = practive; procp != NULL; procp = procp->p_next) {
8530Sstevel@tonic-gate 		if (PROC_IS_FILTER(procp)) {
8540Sstevel@tonic-gate 			filtercount++;
8550Sstevel@tonic-gate 			*bufp++ = procp->p_pid;
8560Sstevel@tonic-gate 		}
8570Sstevel@tonic-gate 	}
8580Sstevel@tonic-gate 	mutex_exit(&pidlock);
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	/* Set filtercount */
8610Sstevel@tonic-gate 	*filterbuf = (pid_t)filtercount;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	/* Copy out result */
8640Sstevel@tonic-gate 	if (ddi_copyout((caddr_t)filterbuf, dest, sz, mode))
8650Sstevel@tonic-gate 		err = EFAULT;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	/* Free temp space */
8680Sstevel@tonic-gate 	kmem_free(filterbuf, sz);
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	return (err);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate static int
tnf_getheader(caddr_t arg,int mode)8740Sstevel@tonic-gate tnf_getheader(caddr_t arg, int mode)
8750Sstevel@tonic-gate {
8760Sstevel@tonic-gate 	if (tnf_buf == NULL)
8770Sstevel@tonic-gate 		return (ENOMEM);
8780Sstevel@tonic-gate 	if (ddi_copyout(tnf_buf, arg, TNF_BLOCK_SIZE, mode))
8790Sstevel@tonic-gate 		return (EFAULT);
8800Sstevel@tonic-gate 	return (0);
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate static int
tnf_getblock(caddr_t arg,int mode)8840Sstevel@tonic-gate tnf_getblock(caddr_t arg, int mode)
8850Sstevel@tonic-gate {
8860Sstevel@tonic-gate 	int		err = 0;
8870Sstevel@tonic-gate 	tifiocgblock_t	parms;
8880Sstevel@tonic-gate 	caddr_t		area;
8890Sstevel@tonic-gate 	tnf_block_header_t	*blk;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	if (tnf_buf == NULL)
8920Sstevel@tonic-gate 		return (ENOMEM);
8930Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&parms, sizeof (parms), mode))
8940Sstevel@tonic-gate 		return (EFAULT);
8950Sstevel@tonic-gate 	area = tnf_buf + TNF_DIRECTORY_SIZE +
8960Sstevel@tonic-gate 	    parms.block_num * TNF_BLOCK_SIZE;
8970Sstevel@tonic-gate 	if (area < tnf_buf + TNF_DIRECTORY_SIZE ||
8980Sstevel@tonic-gate 	    area >= tnf_buf + tnf_trace_file_size)
8990Sstevel@tonic-gate 		return (EFAULT);
9000Sstevel@tonic-gate 	/* LINTED pointer cast */
9010Sstevel@tonic-gate 	blk = (tnf_block_header_t *)area;
9020Sstevel@tonic-gate 	/*
9030Sstevel@tonic-gate 	 * B-lock the block while we're reading
9040Sstevel@tonic-gate 	 */
9050Sstevel@tonic-gate 	if (!lock_try(&blk->B_lock))
9060Sstevel@tonic-gate 		return (EBUSY);
9070Sstevel@tonic-gate 	if (ddi_copyout(area, parms.dst_addr, TNF_BLOCK_SIZE, mode))
9080Sstevel@tonic-gate 		err = EFAULT;
9090Sstevel@tonic-gate 	lock_clear(&blk->B_lock);
9100Sstevel@tonic-gate 	return (err);
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate static int
tnf_getfwzone(caddr_t arg,int mode)9140Sstevel@tonic-gate tnf_getfwzone(caddr_t arg, int mode)
9150Sstevel@tonic-gate {
9160Sstevel@tonic-gate 	tifiocgfw_t parms;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	if (tnf_buf == NULL)
9190Sstevel@tonic-gate 		return (ENOMEM);
9200Sstevel@tonic-gate 	if (ddi_copyin(arg, (caddr_t)&parms, sizeof (parms), mode))
9210Sstevel@tonic-gate 		return (EFAULT);
9220Sstevel@tonic-gate 	if (ddi_copyout(tnf_buf + TNF_BLOCK_SIZE + parms.start *
9230Sstevel@tonic-gate 	    sizeof (tnf_ref32_t), (caddr_t)parms.dst_addr,
9240Sstevel@tonic-gate 	    parms.slots * (int)(sizeof (tnf_ref32_t)), mode))
9250Sstevel@tonic-gate 		return (EFAULT);
9260Sstevel@tonic-gate 	return (0);
9270Sstevel@tonic-gate }
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate /*ARGSUSED*/
9300Sstevel@tonic-gate static void *
tnf_test_1(void * tpdp,tnf_probe_control_t * probe_p,tnf_probe_setup_t * sp)9310Sstevel@tonic-gate tnf_test_1(void *tpdp, tnf_probe_control_t *probe_p, tnf_probe_setup_t *sp)
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	tpdp = (void *)curthread->t_tnf_tpdp;
9340Sstevel@tonic-gate 	if (tpdp != NULL)
9350Sstevel@tonic-gate 		return (tnf_trace_alloc((tnf_ops_t *)tpdp, probe_p, sp));
9360Sstevel@tonic-gate 	return (NULL);
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /*ARGSUSED*/
9400Sstevel@tonic-gate static void *
tnf_test_2(void * tpdp,tnf_probe_control_t * probe_p,tnf_probe_setup_t * sp)9410Sstevel@tonic-gate tnf_test_2(void *tpdp, tnf_probe_control_t *probe_p, tnf_probe_setup_t *sp)
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate 	tpdp = (void *)curthread->t_tnf_tpdp;
9440Sstevel@tonic-gate 	if (tpdp != NULL && PROC_IS_FILTER(curproc))
9450Sstevel@tonic-gate 		return (tnf_trace_alloc((tnf_ops_t *)tpdp, probe_p, sp));
9460Sstevel@tonic-gate 	return (NULL);
9470Sstevel@tonic-gate }
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate #endif /* !NPROBE */
950