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 51677Sdp * Common Development and Distribution License (the "License"). 61677Sdp * 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 /* 221677Sdp * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/dtrace.h> 290Sstevel@tonic-gate #include <sys/systrace.h> 300Sstevel@tonic-gate #include <sys/stat.h> 310Sstevel@tonic-gate #include <sys/systm.h> 320Sstevel@tonic-gate #include <sys/conf.h> 330Sstevel@tonic-gate #include <sys/ddi.h> 340Sstevel@tonic-gate #include <sys/sunddi.h> 350Sstevel@tonic-gate #include <sys/atomic.h> 360Sstevel@tonic-gate 370Sstevel@tonic-gate #define SYSTRACE_ARTIFICIAL_FRAMES 1 380Sstevel@tonic-gate 390Sstevel@tonic-gate #define SYSTRACE_SHIFT 16 400Sstevel@tonic-gate #define SYSTRACE_ISENTRY(x) ((int)(x) >> SYSTRACE_SHIFT) 410Sstevel@tonic-gate #define SYSTRACE_SYSNUM(x) ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1)) 420Sstevel@tonic-gate #define SYSTRACE_ENTRY(id) ((1 << SYSTRACE_SHIFT) | (id)) 430Sstevel@tonic-gate #define SYSTRACE_RETURN(id) (id) 440Sstevel@tonic-gate 450Sstevel@tonic-gate #if ((1 << SYSTRACE_SHIFT) <= NSYSCALL) 460Sstevel@tonic-gate #error 1 << SYSTRACE_SHIFT must exceed number of system calls 470Sstevel@tonic-gate #endif 480Sstevel@tonic-gate 490Sstevel@tonic-gate static dev_info_t *systrace_devi; 500Sstevel@tonic-gate static dtrace_provider_id_t systrace_id; 510Sstevel@tonic-gate 520Sstevel@tonic-gate static void 530Sstevel@tonic-gate systrace_init(struct sysent *actual, systrace_sysent_t **interposed) 540Sstevel@tonic-gate { 550Sstevel@tonic-gate systrace_sysent_t *sysent = *interposed; 560Sstevel@tonic-gate int i; 570Sstevel@tonic-gate 580Sstevel@tonic-gate if (sysent == NULL) { 590Sstevel@tonic-gate *interposed = sysent = kmem_zalloc(sizeof (systrace_sysent_t) * 600Sstevel@tonic-gate NSYSCALL, KM_SLEEP); 610Sstevel@tonic-gate } 620Sstevel@tonic-gate 630Sstevel@tonic-gate for (i = 0; i < NSYSCALL; i++) { 640Sstevel@tonic-gate struct sysent *a = &actual[i]; 650Sstevel@tonic-gate systrace_sysent_t *s = &sysent[i]; 660Sstevel@tonic-gate 670Sstevel@tonic-gate if (LOADABLE_SYSCALL(a) && !LOADED_SYSCALL(a)) 680Sstevel@tonic-gate continue; 690Sstevel@tonic-gate 700Sstevel@tonic-gate if (a->sy_callc == dtrace_systrace_syscall) 710Sstevel@tonic-gate continue; 720Sstevel@tonic-gate 730Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 740Sstevel@tonic-gate if (a->sy_callc == dtrace_systrace_syscall32) 750Sstevel@tonic-gate continue; 760Sstevel@tonic-gate #endif 770Sstevel@tonic-gate 780Sstevel@tonic-gate s->stsy_underlying = a->sy_callc; 790Sstevel@tonic-gate } 800Sstevel@tonic-gate } 810Sstevel@tonic-gate 820Sstevel@tonic-gate /*ARGSUSED*/ 830Sstevel@tonic-gate static void 840Sstevel@tonic-gate systrace_provide(void *arg, const dtrace_probedesc_t *desc) 850Sstevel@tonic-gate { 860Sstevel@tonic-gate int i; 870Sstevel@tonic-gate 880Sstevel@tonic-gate if (desc != NULL) 890Sstevel@tonic-gate return; 900Sstevel@tonic-gate 910Sstevel@tonic-gate systrace_init(sysent, &systrace_sysent); 920Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 930Sstevel@tonic-gate systrace_init(sysent32, &systrace_sysent32); 940Sstevel@tonic-gate #endif 950Sstevel@tonic-gate 960Sstevel@tonic-gate for (i = 0; i < NSYSCALL; i++) { 970Sstevel@tonic-gate if (systrace_sysent[i].stsy_underlying == NULL) 980Sstevel@tonic-gate continue; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate if (dtrace_probe_lookup(systrace_id, NULL, 1010Sstevel@tonic-gate syscallnames[i], "entry") != 0) 1020Sstevel@tonic-gate continue; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate (void) dtrace_probe_create(systrace_id, NULL, syscallnames[i], 1050Sstevel@tonic-gate "entry", SYSTRACE_ARTIFICIAL_FRAMES, 1060Sstevel@tonic-gate (void *)((uintptr_t)SYSTRACE_ENTRY(i))); 1070Sstevel@tonic-gate (void) dtrace_probe_create(systrace_id, NULL, syscallnames[i], 1080Sstevel@tonic-gate "return", SYSTRACE_ARTIFICIAL_FRAMES, 1090Sstevel@tonic-gate (void *)((uintptr_t)SYSTRACE_RETURN(i))); 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate systrace_sysent[i].stsy_entry = DTRACE_IDNONE; 1120Sstevel@tonic-gate systrace_sysent[i].stsy_return = DTRACE_IDNONE; 1130Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1140Sstevel@tonic-gate systrace_sysent32[i].stsy_entry = DTRACE_IDNONE; 1150Sstevel@tonic-gate systrace_sysent32[i].stsy_return = DTRACE_IDNONE; 1160Sstevel@tonic-gate #endif 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /*ARGSUSED*/ 1210Sstevel@tonic-gate static void 1220Sstevel@tonic-gate systrace_destroy(void *arg, dtrace_id_t id, void *parg) 1230Sstevel@tonic-gate { 1240Sstevel@tonic-gate int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* 1270Sstevel@tonic-gate * There's nothing to do here but assert that we have actually been 1280Sstevel@tonic-gate * disabled. 1290Sstevel@tonic-gate */ 1300Sstevel@tonic-gate if (SYSTRACE_ISENTRY((uintptr_t)parg)) { 1310Sstevel@tonic-gate ASSERT(systrace_sysent[sysnum].stsy_entry == DTRACE_IDNONE); 1320Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1330Sstevel@tonic-gate ASSERT(systrace_sysent32[sysnum].stsy_entry == DTRACE_IDNONE); 1340Sstevel@tonic-gate #endif 1350Sstevel@tonic-gate } else { 1360Sstevel@tonic-gate ASSERT(systrace_sysent[sysnum].stsy_return == DTRACE_IDNONE); 1370Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1380Sstevel@tonic-gate ASSERT(systrace_sysent32[sysnum].stsy_return == DTRACE_IDNONE); 1390Sstevel@tonic-gate #endif 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate /*ARGSUSED*/ 1440Sstevel@tonic-gate static void 1450Sstevel@tonic-gate systrace_enable(void *arg, dtrace_id_t id, void *parg) 1460Sstevel@tonic-gate { 1470Sstevel@tonic-gate int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 1480Sstevel@tonic-gate int enabled = (systrace_sysent[sysnum].stsy_entry != DTRACE_IDNONE || 1490Sstevel@tonic-gate systrace_sysent[sysnum].stsy_return != DTRACE_IDNONE); 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate if (SYSTRACE_ISENTRY((uintptr_t)parg)) { 1520Sstevel@tonic-gate systrace_sysent[sysnum].stsy_entry = id; 1530Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1540Sstevel@tonic-gate systrace_sysent32[sysnum].stsy_entry = id; 1550Sstevel@tonic-gate #endif 1560Sstevel@tonic-gate } else { 1570Sstevel@tonic-gate systrace_sysent[sysnum].stsy_return = id; 1580Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1590Sstevel@tonic-gate systrace_sysent32[sysnum].stsy_return = id; 1600Sstevel@tonic-gate #endif 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate if (enabled) { 1640Sstevel@tonic-gate ASSERT(sysent[sysnum].sy_callc == dtrace_systrace_syscall); 1650Sstevel@tonic-gate return; 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate (void) casptr(&sysent[sysnum].sy_callc, 1690Sstevel@tonic-gate (void *)systrace_sysent[sysnum].stsy_underlying, 1700Sstevel@tonic-gate (void *)dtrace_systrace_syscall); 1710Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1720Sstevel@tonic-gate (void) casptr(&sysent32[sysnum].sy_callc, 1730Sstevel@tonic-gate (void *)systrace_sysent32[sysnum].stsy_underlying, 1740Sstevel@tonic-gate (void *)dtrace_systrace_syscall32); 1750Sstevel@tonic-gate #endif 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate /*ARGSUSED*/ 1790Sstevel@tonic-gate static void 1800Sstevel@tonic-gate systrace_disable(void *arg, dtrace_id_t id, void *parg) 1810Sstevel@tonic-gate { 1820Sstevel@tonic-gate int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 1830Sstevel@tonic-gate int disable = (systrace_sysent[sysnum].stsy_entry == DTRACE_IDNONE || 1840Sstevel@tonic-gate systrace_sysent[sysnum].stsy_return == DTRACE_IDNONE); 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate if (disable) { 1870Sstevel@tonic-gate (void) casptr(&sysent[sysnum].sy_callc, 1880Sstevel@tonic-gate (void *)dtrace_systrace_syscall, 1890Sstevel@tonic-gate (void *)systrace_sysent[sysnum].stsy_underlying); 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1920Sstevel@tonic-gate (void) casptr(&sysent32[sysnum].sy_callc, 1930Sstevel@tonic-gate (void *)dtrace_systrace_syscall32, 1940Sstevel@tonic-gate (void *)systrace_sysent32[sysnum].stsy_underlying); 1950Sstevel@tonic-gate #endif 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate if (SYSTRACE_ISENTRY((uintptr_t)parg)) { 1990Sstevel@tonic-gate systrace_sysent[sysnum].stsy_entry = DTRACE_IDNONE; 2000Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2010Sstevel@tonic-gate systrace_sysent32[sysnum].stsy_entry = DTRACE_IDNONE; 2020Sstevel@tonic-gate #endif 2030Sstevel@tonic-gate } else { 2040Sstevel@tonic-gate systrace_sysent[sysnum].stsy_return = DTRACE_IDNONE; 2050Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2060Sstevel@tonic-gate systrace_sysent32[sysnum].stsy_return = DTRACE_IDNONE; 2070Sstevel@tonic-gate #endif 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate static dtrace_pattr_t systrace_attr = { 2120Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 2130Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 2140Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 2150Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 2160Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 2170Sstevel@tonic-gate }; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate static dtrace_pops_t systrace_pops = { 2200Sstevel@tonic-gate systrace_provide, 2210Sstevel@tonic-gate NULL, 2220Sstevel@tonic-gate systrace_enable, 2230Sstevel@tonic-gate systrace_disable, 2240Sstevel@tonic-gate NULL, 2250Sstevel@tonic-gate NULL, 2260Sstevel@tonic-gate NULL, 2270Sstevel@tonic-gate NULL, 2280Sstevel@tonic-gate NULL, 2290Sstevel@tonic-gate systrace_destroy 2300Sstevel@tonic-gate }; 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate static int 2330Sstevel@tonic-gate systrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 2340Sstevel@tonic-gate { 2350Sstevel@tonic-gate switch (cmd) { 2360Sstevel@tonic-gate case DDI_ATTACH: 2370Sstevel@tonic-gate break; 2380Sstevel@tonic-gate case DDI_RESUME: 2390Sstevel@tonic-gate return (DDI_SUCCESS); 2400Sstevel@tonic-gate default: 2410Sstevel@tonic-gate return (DDI_FAILURE); 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 244*2769Sahl systrace_probe = (void (*)())dtrace_probe; 2450Sstevel@tonic-gate membar_enter(); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate if (ddi_create_minor_node(devi, "systrace", S_IFCHR, 0, 2480Sstevel@tonic-gate DDI_PSEUDO, NULL) == DDI_FAILURE || 2491677Sdp dtrace_register("syscall", &systrace_attr, DTRACE_PRIV_USER, NULL, 2500Sstevel@tonic-gate &systrace_pops, NULL, &systrace_id) != 0) { 2510Sstevel@tonic-gate systrace_probe = systrace_stub; 2520Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 2530Sstevel@tonic-gate return (DDI_FAILURE); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate ddi_report_dev(devi); 2570Sstevel@tonic-gate systrace_devi = devi; 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate return (DDI_SUCCESS); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate static int 2630Sstevel@tonic-gate systrace_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 2640Sstevel@tonic-gate { 2650Sstevel@tonic-gate switch (cmd) { 2660Sstevel@tonic-gate case DDI_DETACH: 2670Sstevel@tonic-gate break; 2680Sstevel@tonic-gate case DDI_SUSPEND: 2690Sstevel@tonic-gate return (DDI_SUCCESS); 2700Sstevel@tonic-gate default: 2710Sstevel@tonic-gate return (DDI_FAILURE); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate if (dtrace_unregister(systrace_id) != 0) 2750Sstevel@tonic-gate return (DDI_FAILURE); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 2780Sstevel@tonic-gate systrace_probe = systrace_stub; 2790Sstevel@tonic-gate return (DDI_SUCCESS); 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate /*ARGSUSED*/ 2830Sstevel@tonic-gate static int 2840Sstevel@tonic-gate systrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 2850Sstevel@tonic-gate { 2860Sstevel@tonic-gate int error; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate switch (infocmd) { 2890Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 2900Sstevel@tonic-gate *result = (void *)systrace_devi; 2910Sstevel@tonic-gate error = DDI_SUCCESS; 2920Sstevel@tonic-gate break; 2930Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 2940Sstevel@tonic-gate *result = (void *)0; 2950Sstevel@tonic-gate error = DDI_SUCCESS; 2960Sstevel@tonic-gate break; 2970Sstevel@tonic-gate default: 2980Sstevel@tonic-gate error = DDI_FAILURE; 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate return (error); 3010Sstevel@tonic-gate } 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate /*ARGSUSED*/ 3040Sstevel@tonic-gate static int 3050Sstevel@tonic-gate systrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 3060Sstevel@tonic-gate { 3070Sstevel@tonic-gate return (0); 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate static struct cb_ops systrace_cb_ops = { 3110Sstevel@tonic-gate systrace_open, /* open */ 3120Sstevel@tonic-gate nodev, /* close */ 3130Sstevel@tonic-gate nulldev, /* strategy */ 3140Sstevel@tonic-gate nulldev, /* print */ 3150Sstevel@tonic-gate nodev, /* dump */ 3160Sstevel@tonic-gate nodev, /* read */ 3170Sstevel@tonic-gate nodev, /* write */ 3180Sstevel@tonic-gate nodev, /* ioctl */ 3190Sstevel@tonic-gate nodev, /* devmap */ 3200Sstevel@tonic-gate nodev, /* mmap */ 3210Sstevel@tonic-gate nodev, /* segmap */ 3220Sstevel@tonic-gate nochpoll, /* poll */ 3230Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 3240Sstevel@tonic-gate 0, /* streamtab */ 3250Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */ 3260Sstevel@tonic-gate }; 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate static struct dev_ops systrace_ops = { 3290Sstevel@tonic-gate DEVO_REV, /* devo_rev, */ 3300Sstevel@tonic-gate 0, /* refcnt */ 3310Sstevel@tonic-gate systrace_info, /* get_dev_info */ 3320Sstevel@tonic-gate nulldev, /* identify */ 3330Sstevel@tonic-gate nulldev, /* probe */ 3340Sstevel@tonic-gate systrace_attach, /* attach */ 3350Sstevel@tonic-gate systrace_detach, /* detach */ 3360Sstevel@tonic-gate nodev, /* reset */ 3370Sstevel@tonic-gate &systrace_cb_ops, /* driver operations */ 3380Sstevel@tonic-gate NULL, /* bus operations */ 3390Sstevel@tonic-gate nodev /* dev power */ 3400Sstevel@tonic-gate }; 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate /* 3430Sstevel@tonic-gate * Module linkage information for the kernel. 3440Sstevel@tonic-gate */ 3450Sstevel@tonic-gate static struct modldrv modldrv = { 3460Sstevel@tonic-gate &mod_driverops, /* module type (this is a pseudo driver) */ 3470Sstevel@tonic-gate "System Call Tracing", /* name of module */ 3480Sstevel@tonic-gate &systrace_ops, /* driver ops */ 3490Sstevel@tonic-gate }; 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate static struct modlinkage modlinkage = { 3520Sstevel@tonic-gate MODREV_1, 3530Sstevel@tonic-gate (void *)&modldrv, 3540Sstevel@tonic-gate NULL 3550Sstevel@tonic-gate }; 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate int 3580Sstevel@tonic-gate _init(void) 3590Sstevel@tonic-gate { 3600Sstevel@tonic-gate return (mod_install(&modlinkage)); 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate int 3640Sstevel@tonic-gate _info(struct modinfo *modinfop) 3650Sstevel@tonic-gate { 3660Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate int 3700Sstevel@tonic-gate _fini(void) 3710Sstevel@tonic-gate { 3720Sstevel@tonic-gate return (mod_remove(&modlinkage)); 3730Sstevel@tonic-gate } 374