/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* tnf driver - provides probe control and kernel trace buffer access
* to the user programs prex and tnfxtract.
*/
#include <sys/sysmacros.h>
#include <sys/tnf_probe.h>
#include "tnf_buf.h"
#include "tnf_types.h"
#include "tnf_trace.h"
#ifndef NPROBE
/*
* Each probe is independently put in the kernel, prex uses
* __tnf_probe_list_head and __tnf_tag_list_head as pointers to linked list
* for probes and static tnf_tag_data_t, respectively.
* tnf used the elf relocation record to build a separate linked list for
* the probes and tnf_tag_data_t. We will describe how the linked list for
* __tnf_tag_list_head is made, the probe list is very similar.
* During the dynamic relocation(in uts/sparc/krtld/kobj_reloc.c),
* the &__tnf_tag_version_1(the first member in tnf_tag_data_t data struct)
* (and since it is a global variable which was never defined) will be filled
* with 0. The following code in kobj_reloc.c will get the address of current
* __tnf_tag_list_head and put it in value_p:
* #define TAG_MARKER_SYMBOL "__tnf_tag_version_1"
* if (strcmp(symname, TAG_MARKER_SYMBOL) == 0) {
* *addend_p = 0;
* *value_p = (Addr) __tnf_tag_list_head; (value_p points to list head)
* __tnf_tag_list_head = (void *)*offset_p;(list head is the next record)
* return (0);
* }
*
* the function do_reloc(in the kobj_reloc.c) will put vlaue_p into
* &__tnf_tag_version_1
* Now the &__tnf_tag_version_1 points to the last list head
* and __tnf_tag_list_head points to the new list head.
* This is equivalent to attatch a node at the beginning of the list.
*
*/
extern tnf_probe_control_t *__tnf_probe_list_head;
extern tnf_tag_data_t *__tnf_tag_list_head;
extern int tnf_changed_probe_list;
#ifdef UNUSED
#endif
#ifdef UNUSED
int, char *, caddr_t, int *);
#endif
static struct {
int tnf_probe_count;
int mapdev_open_count;
static int tnf_getmaxprobe(caddr_t, int);
static int tnf_getprobevals(caddr_t, int);
static int tnf_getprobestring(caddr_t, int);
static int tnf_setprobevals(caddr_t, int);
static int tnf_getstate(caddr_t, int);
static int tnf_allocbuf(intptr_t);
static int tnf_deallocbuf(void);
static int tnf_settracing(int);
static int tnf_pidfilterset(int);
static int tnf_pidfilterget(caddr_t, int);
static int tnf_getpidstate(caddr_t, int);
static int tnf_setpidstate(int, pid_t, int);
static int tnf_getheader(caddr_t, int);
static int tnf_getblock(caddr_t, int);
static int tnf_getfwzone(caddr_t, int);
#define TNFCTL_MINOR 0
tnf_open, /* open */
tnf_close, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
nodev, /* read */
nodev, /* write */
tnf_ioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
nochpoll, /* poll */
ddi_prop_op, /* prop_op */
0, /* streamtab */
};
DEVO_REV, /* devo_rev, */
0, /* refcnt */
tnf_info, /* info */
nulldev, /* identify */
nulldev, /* probe */
tnf_attach, /* attach */
tnf_detach, /* detach */
nodev, /* reset */
&tnf_cb_ops, /* driver operations */
(struct bus_ops *)0, /* no bus operations */
NULL, /* power */
ddi_quiesce_not_needed, /* quiesce */
};
extern struct mod_ops mod_driverops;
"kernel probes driver",
&tnf_ops,
};
(void *)&modldrv,
};
int
_init()
{
register int error;
return (error);
}
/* Give t0 a tpdp */
if (!t0.t_tnf_tpdp)
/* Initialize tag system */
return (0);
}
int
_fini()
{
/* Not safe to unload this module, currently */
return (EBUSY);
}
int
{
}
/* ARGSUSED */
static int
{
register int error;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
error = DDI_SUCCESS;
break;
case DDI_INFO_DEVT2INSTANCE:
*result = (void *)0;
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
}
return (error);
}
static int
{
if (cmd != DDI_ATTACH)
return (DDI_FAILURE);
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static int
{
if (cmd != DDI_DETACH)
return (DDI_FAILURE);
return (DDI_SUCCESS);
}
/*
* property operations. Return the size of the kernel trace buffer. We
* only handle size property requests. Others are passed on.
*/
#ifdef UNUSED
static int
{
/* Don't need tnf_mtx, since mapdev_open_count > 0 */
*lengthp = sizeof (int); /* set caller's length */
switch (prop) {
case PROP_LEN:
return (DDI_PROP_SUCCESS);
case PROP_LEN_AND_VAL_ALLOC:
retbuf = kmem_alloc(sizeof (int),
return (DDI_PROP_NO_MEMORY);
return (DDI_PROP_SUCCESS);
case PROP_LEN_AND_VAL_BUF:
if (length < sizeof (int))
return (DDI_PROP_BUF_TOO_SMALL);
return (DDI_PROP_SUCCESS);
}
}
}
#endif
/* ARGSUSED */
static int
{
int err = 0;
else {
/* stop autounloading -- XXX temporary */
}
} else {
/* ASSERT(getminor(*devp) == TNFMAP_MINOR) */
}
return (err);
}
/* ARGSUSED */
static int
{
/*
* Request the reenablement of autounloading
*/
} else {
/* ASSERT(getminor(dev) == TNFMAP_MINOR) */
/*
* Unconditionally zero the open count since close()
* is called when last client closes the device.
*/
}
return (0);
}
/*
* return the address of the image referenced by dev.
*
* 1191344: aliasing problem on VAC machines. It could be made to
* work by ensuring that tnf_buf is allocated on a vac_size boundary.
*/
#ifdef UNUSED
/*ARGSUSED*/
static int
{
return (-1);
return (-1);
}
}
#endif
/*ARGSUSED4*/
static int
{
return (ENOTSUP);
cmd != TIFIOCGSTATE &&
cmd != TIFIOCGHEADER &&
cmd != TIFIOCGBLOCK &&
cmd != TIFIOCGFWZONE)
return (EINVAL);
switch (cmd) {
case TIFIOCGMAXPROBE:
case TIFIOCGPROBEVALS:
case TIFIOCGPROBESTRING:
case TIFIOCSPROBEVALS:
case TIFIOCGSTATE:
case TIFIOCALLOCBUF:
return (tnf_allocbuf(arg));
case TIFIOCDEALLOCBUF:
return (tnf_deallocbuf());
case TIFIOCSTRACING:
/* LINTED cast from 64-bit integer to 32-bit integer */
return (tnf_settracing((int)arg));
case TIFIOCSPIDFILTER:
/* LINTED cast from 64-bit integer to 32-bit integer */
return (tnf_pidfilterset((int)arg));
case TIFIOCGPIDSTATE:
case TIFIOCSPIDOFF:
filterval = 0;
/*FALLTHROUGH*/
case TIFIOCSPIDON:
/* LINTED cast from 64-bit integer to 32-bit integer */
case TIFIOCPIDFILTERGET:
case TIFIOCGHEADER:
case TIFIOCGBLOCK:
case TIFIOCGFWZONE:
default:
return (EINVAL);
}
}
/*
* ioctls
*/
static int
{
/*
* XXX Still not right for module unload -- just counting
* the probes is not enough
*/
if (tnf_changed_probe_list) {
for (p = (tnf_probe_control_t *)__tnf_probe_list_head;
p != 0; p = p->next)
}
return (EFAULT);
return (0);
}
static int
{
int i, retval = 0;
return (EFAULT);
++i, p = p->next)
;
if (p == NULL)
else {
/* LINTED assignment of 64-bit integer to 32-bit integer */
}
return (retval);
}
static int
{
int i, retval = 0;
return (EFAULT);
++i, p = p->next)
;
if (p == NULL)
return (retval);
}
static int
{
int i, retval = 0;
return (EFAULT);
++i, p = p->next)
;
if (p == NULL)
else {
/*
* First do trace, then enable.
* Set test_func last.
*/
p->probe_func = tnf_trace_commit;
else
p->probe_func = tnf_trace_rollback;
p->alloc_func = tnf_trace_alloc;
/* this must be set last */
p->test_func = tnf_test_2;
else
p->test_func = tnf_test_1;
} else
}
return (retval);
}
static int
{
tstate.buffer_size = 0;
} else {
switch (tnfw_b_state & ~TNFW_B_STOPPED) {
case TNFW_B_RUNNING:
break;
case TNFW_B_NOBUFFER:
break;
case TNFW_B_BROKEN:
break;
}
/* LINTED assignment of 64-bit integer to 32-bit integer */
}
tstate.pidfilter_size = 0;
if (PROC_IS_FILTER(procp))
return (EFAULT);
return (0);
}
static int
{
return (EBUSY);
/*
* Validate size
* XXX Take kernel VM into consideration as well
*/
/* bug fix #4057599 if (bufsz > (physmem << PAGESHIFT) / 2) */
return (ENOMEM);
if (bufsz < TNF_TRACE_FILE_MIN)
#if TNF_USE_KMA
#else
/* LINTED cast from 64-bit integer to 32-bit intege */
#endif
return (ENOMEM);
return (0);
}
/*
* Process a "deallocate buffer" ioctl request. Tracing must be turned
* off. We must clear references to the buffer from the tag sites;
* invalidate all threads' notions of block ownership; make sure nobody
* is executing a probe (they might have started before tracing was
* turned off); and free the buffer.
*/
static int
tnf_deallocbuf(void)
{
kthread_t *t;
return (EBUSY);
return (ENOMEM);
/*
* Make sure nobody is executing a probe.
* (They could be if they got started while
* tnf_tracing_active was still on.) Grab
* pidlock, and check the busy flag in all
* TPDP's.
*/
t = curthread;
do {
if (t->t_tnf_tpdp != NULL) {
/* LINTED pointer cast may result in improper alignment */
return (EBUSY);
}
}
t = t->t_next;
} while (t != curthread);
/*
* Zap all references to the buffer we're freeing.
* Grab mod_lock while walking list to keep it
* consistent.
*/
}
}
#if TNF_USE_KMA
#else
#endif
return (0);
}
static int
{
if (arg)
return (ENOMEM);
else
tnf_trace_on();
else
#ifdef _TNF_SPEED_TEST
{
int i;
for (i = 0; i < NITER; i++)
for (i = 0; i < NITER; i++)
tnf_long, long, i);
for (i = 0; i < NITER; i++)
}
#endif /* _TNF_SPEED_TEST */
return (0);
}
static int
{
int err = 0;
int result;
return (EFAULT);
else
if (!err)
return (EFAULT);
return (err);
}
/*ARGSUSED*/
static int
{
int err = 0;
if (filterval)
else
else
return (err);
}
static int
{
/* Establish correct test func for each probe */
if (mode)
func = tnf_test_2;
else
func = tnf_test_1;
while (p != NULL) {
p = p->next;
}
return (0);
}
static int
{
int err = 0;
int filtercount = 0;
/* Count how many processes in filter set (upper bound) */
if (PROC_IS_FILTER(procp))
filtercount++;
/* Allocate temp space to hold filter set (upper bound) */
/*
* NOTE: The filter set cannot grow between the first and
* second acquisitions of pidlock. This is currently true
* because:
* control operations, including changing the filter
* set and this code, are effectively single-threaded.
* 2. There is no in-kernel API to manipulate the filter
* 3. The proc filter bit is not inherited across a fork()
* operation; the child starts with the bit off.
* If any of these assumptions is invalidated, a possible
* solution is to check whether we're overflowing the allocated
* filterbuf below, and back out and restart from the beginning
* if so.
*
* The code below handles the case when the filter set shrinks
* due to processes exiting.
*/
/* Fill in filter set */
filtercount = 0; /* recomputed below */
if (PROC_IS_FILTER(procp)) {
filtercount++;
}
}
/* Set filtercount */
/* Copy out result */
/* Free temp space */
return (err);
}
static int
{
return (ENOMEM);
return (EFAULT);
return (0);
}
static int
{
int err = 0;
return (ENOMEM);
return (EFAULT);
return (EFAULT);
/* LINTED pointer cast */
/*
* B-lock the block while we're reading
*/
return (EBUSY);
return (err);
}
static int
{
return (ENOMEM);
return (EFAULT);
return (EFAULT);
return (0);
}
/*ARGSUSED*/
static void *
{
return (NULL);
}
/*ARGSUSED*/
static void *
{
return (NULL);
}
#endif /* !NPROBE */