1 /* $NetBSD: systrace.c,v 1.9 2017/01/07 21:39:52 christos Exp $ */ 2 3 /* 4 * CDDL HEADER START 5 * 6 * The contents of this file are subject to the terms of the 7 * Common Development and Distribution License (the "License"). 8 * You may not use this file except in compliance with the License. 9 * 10 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11 * or http://www.opensolaris.org/os/licensing. 12 * See the License for the specific language governing permissions 13 * and limitations under the License. 14 * 15 * When distributing Covered Code, include this CDDL HEADER in each 16 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17 * If applicable, add the following below this CDDL HEADER, with the 18 * fields enclosed by brackets "[]" replaced with your own identifying 19 * information: Portions Copyright [yyyy] [name of copyright owner] 20 * 21 * CDDL HEADER END 22 * 23 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org 24 * 25 * $FreeBSD: src/sys/cddl/dev/systrace/systrace.c,v 1.2.2.1 2009/08/03 08:13:06 kensmith Exp $ 26 * 27 */ 28 29 /* 30 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 31 * Use is subject to license terms. 32 */ 33 34 #include <sys/cdefs.h> 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/conf.h> 38 #include <sys/cpuvar.h> 39 #include <sys/fcntl.h> 40 #include <sys/filio.h> 41 #include <sys/kernel.h> 42 #include <sys/kmem.h> 43 #include <sys/kthread.h> 44 #include <sys/syslimits.h> 45 #include <sys/linker.h> 46 #include <sys/lock.h> 47 #include <sys/malloc.h> 48 #include <sys/module.h> 49 #include <sys/mutex.h> 50 #include <sys/poll.h> 51 #include <sys/proc.h> 52 #include <sys/selinfo.h> 53 #include <sys/syscallargs.h> 54 #include <sys/uio.h> 55 #include <sys/unistd.h> 56 57 #include <sys/dtrace.h> 58 59 #include "emultrace.h" 60 61 #define CONCAT(x,y) __CONCAT(x,y) 62 #define STRING(s) __STRING(s) 63 64 #ifndef NATIVE 65 extern const char * const CONCAT(emulname,_syscallnames)[]; 66 extern const char * const CONCAT(alt,CONCAT(emulname,_syscallnames))[]; 67 extern struct sysent CONCAT(emulname,_sysent)[]; 68 #define MODNAME CONCAT(dtrace_syscall_,emulname) 69 #define MODDEP "dtrace_syscall,compat_" STRING(emulname) 70 #define MAXSYSCALL CONCAT(EMULNAME,_SYS_MAXSYSCALL) 71 #define SYSCALLNAMES CONCAT(emulname,_syscallnames) 72 #define ALTSYSCALLNAMES CONCAT(alt,CONCAT(emulname,_syscallnames)) 73 #define SYSENT CONCAT(emulname,_sysent) 74 #define PROVNAME STRING(emulname) "_syscall" 75 #else 76 extern const char * const syscallnames[]; 77 extern const char * const altsyscallnames[]; 78 #define MODNAME dtrace_syscall 79 #define MODDEP "dtrace" 80 #define MAXSYSCALL SYS_MAXSYSCALL 81 #define SYSCALLNAMES syscallnames 82 #define ALTSYSCALLNAMES altsyscallnames 83 #define SYSENT sysent 84 #define PROVNAME "syscall" 85 #endif 86 87 #define MODCMD CONCAT(MODNAME,_modcmd) 88 #define EMUL CONCAT(emul_,emulname) 89 extern struct emul EMUL; 90 91 #define SYSTRACE_ARTIFICIAL_FRAMES 1 92 93 #define SYSTRACE_SHIFT 16 94 #define SYSTRACE_ISENTRY(x) ((int)(x) >> SYSTRACE_SHIFT) 95 #define SYSTRACE_SYSNUM(x) ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1)) 96 #define SYSTRACE_ENTRY(id) ((1 << SYSTRACE_SHIFT) | (id)) 97 #define SYSTRACE_RETURN(id) (id) 98 99 #if ((1 << SYSTRACE_SHIFT) <= MAXSYSCALL) 100 #error 1 << SYSTRACE_SHIFT must exceed number of system calls 101 #endif 102 103 static int systrace_unload(void); 104 static void systrace_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *); 105 static void systrace_provide(void *, const dtrace_probedesc_t *); 106 static void systrace_destroy(void *, dtrace_id_t, void *); 107 static int systrace_enable(void *, dtrace_id_t, void *); 108 static void systrace_disable(void *, dtrace_id_t, void *); 109 static void systrace_load(void *); 110 111 static dtrace_pattr_t systrace_attr = { 112 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 113 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 114 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 115 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 116 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 117 }; 118 119 static dtrace_pops_t systrace_pops = { 120 systrace_provide, 121 NULL, 122 systrace_enable, 123 systrace_disable, 124 NULL, 125 NULL, 126 systrace_getargdesc, 127 NULL, 128 NULL, 129 systrace_destroy 130 }; 131 132 static dtrace_provider_id_t systrace_id; 133 134 /* 135 * Probe callback function. 136 * 137 * Note: This function is called for _all_ syscalls, regardless of which sysent 138 * array the syscall comes from. It could be a standard syscall or a 139 * compat syscall from something like Linux. 140 */ 141 static void 142 systrace_probe(uint32_t id, register_t sysnum, const struct sysent *se, 143 const void *params, const register_t *ret, int error) 144 { 145 size_t n_args = 0; 146 uintptr_t uargs[SYS_MAXSYSARGS + 3]; 147 148 memset(uargs, 0, sizeof(uargs)); 149 if (ret == NULL) { 150 /* entry syscall, convert params */ 151 systrace_args(sysnum, params, uargs, &n_args); 152 } else { 153 /* return syscall, set values and params: */ 154 uargs[0] = ret[0]; 155 uargs[1] = ret[1]; 156 uargs[2] = error; 157 systrace_args(sysnum, params, uargs + 3, &n_args); 158 } 159 /* Process the probe using the converted argments. */ 160 /* XXX: fix for more arguments! */ 161 dtrace_probe(id, uargs[0], uargs[1], uargs[2], uargs[3], uargs[4]); 162 } 163 164 static void 165 systrace_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc) 166 { 167 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 168 if (SYSTRACE_ISENTRY((uintptr_t)parg)) 169 systrace_entry_setargdesc(sysnum, desc->dtargd_ndx, 170 desc->dtargd_native, sizeof(desc->dtargd_native)); 171 else 172 systrace_return_setargdesc(sysnum, desc->dtargd_ndx, 173 desc->dtargd_native, sizeof(desc->dtargd_native)); 174 175 if (desc->dtargd_native[0] == '\0') 176 desc->dtargd_ndx = DTRACE_ARGNONE; 177 } 178 179 static void 180 systrace_provide(void *arg, const dtrace_probedesc_t *desc) 181 { 182 int i; 183 184 if (desc != NULL) 185 return; 186 187 for (i = 0; i < MAXSYSCALL; i++) { 188 const char *name = ALTSYSCALLNAMES[i] ? ALTSYSCALLNAMES[i] : 189 SYSCALLNAMES[i]; 190 if (dtrace_probe_lookup(systrace_id, NULL, name, "entry") != 0) 191 continue; 192 193 (void) dtrace_probe_create(systrace_id, NULL, 194 name, "entry", SYSTRACE_ARTIFICIAL_FRAMES, 195 (void *)(intptr_t)SYSTRACE_ENTRY(i)); 196 (void) dtrace_probe_create(systrace_id, NULL, 197 name, "return", SYSTRACE_ARTIFICIAL_FRAMES, 198 (void *)(intptr_t)SYSTRACE_RETURN(i)); 199 } 200 } 201 202 static void 203 systrace_destroy(void *arg, dtrace_id_t id, void *parg) 204 { 205 #ifdef DEBUG 206 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 207 208 /* 209 * There's nothing to do here but assert that we have actually been 210 * disabled. 211 */ 212 if (SYSTRACE_ISENTRY((uintptr_t)parg)) { 213 ASSERT(sysent[sysnum].sy_entry == 0); 214 } else { 215 ASSERT(sysent[sysnum].sy_return == 0); 216 } 217 #endif 218 } 219 220 static int 221 systrace_enable(void *arg, dtrace_id_t id, void *parg) 222 { 223 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 224 225 if (SYSTRACE_ISENTRY((uintptr_t)parg)) 226 SYSENT[sysnum].sy_entry = id; 227 else 228 SYSENT[sysnum].sy_return = id; 229 return 0; 230 } 231 232 static void 233 systrace_disable(void *arg, dtrace_id_t id, void *parg) 234 { 235 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 236 237 SYSENT[sysnum].sy_entry = 0; 238 SYSENT[sysnum].sy_return = 0; 239 } 240 241 static void 242 systrace_load(void *dummy) 243 { 244 if (dtrace_register(PROVNAME, &systrace_attr, DTRACE_PRIV_USER, 245 NULL, &systrace_pops, NULL, &systrace_id) != 0) 246 return; 247 248 EMUL.e_dtrace_syscall = systrace_probe; 249 } 250 251 252 static int 253 systrace_unload() 254 { 255 int error; 256 257 if ((error = dtrace_unregister(systrace_id)) != 0) 258 return (error); 259 260 EMUL.e_dtrace_syscall = NULL; 261 262 return error; 263 } 264 265 static int 266 MODCMD(modcmd_t cmd, void *data) 267 { 268 switch (cmd) { 269 case MODULE_CMD_INIT: 270 systrace_load(NULL); 271 return 0; 272 273 case MODULE_CMD_FINI: 274 return systrace_unload(); 275 276 case MODULE_CMD_AUTOUNLOAD: 277 return EBUSY; 278 279 default: 280 return ENOTTY; 281 } 282 } 283 284 MODULE(MODULE_CLASS_MISC, MODNAME, MODDEP) 285