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