1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 * 21 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org 22 */ 23 24 /* 25 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/conf.h> 33 #include <sys/cpuvar.h> 34 #include <sys/dtrace.h> 35 #include <sys/fcntl.h> 36 #include <sys/filio.h> 37 #include <sys/kdb.h> 38 #include <sys/kernel.h> 39 #include <sys/kmem.h> 40 #include <sys/kthread.h> 41 #include <sys/limits.h> 42 #include <sys/linker.h> 43 #include <sys/lock.h> 44 #include <sys/malloc.h> 45 #include <sys/module.h> 46 #include <sys/mutex.h> 47 #include <sys/poll.h> 48 #include <sys/proc.h> 49 #include <sys/selinfo.h> 50 #include <sys/smp.h> 51 #include <sys/sysent.h> 52 #include <sys/sysproto.h> 53 #include <sys/uio.h> 54 #include <sys/unistd.h> 55 56 #include <cddl/dev/dtrace/dtrace_cddl.h> 57 58 #include <machine/stdarg.h> 59 60 #ifdef LINUX_SYSTRACE 61 #if defined(__amd64__) 62 #include <amd64/linux/linux.h> 63 #include <amd64/linux/linux_proto.h> 64 #include <amd64/linux/linux_syscalls.c> 65 #include <amd64/linux/linux_systrace_args.c> 66 #elif defined(__i386__) 67 #include <i386/linux/linux.h> 68 #include <i386/linux/linux_proto.h> 69 #include <i386/linux/linux_syscalls.c> 70 #include <i386/linux/linux_systrace_args.c> 71 #else 72 #error Only i386 and amd64 are supported. 73 #endif 74 #define MODNAME "linux" 75 extern struct sysent linux_sysent[]; 76 #define MAXSYSCALL LINUX_SYS_MAXSYSCALL 77 #define SYSCALLNAMES linux_syscallnames 78 #define SYSENT linux_sysent 79 #elif defined(LINUX32_SYSTRACE) 80 #if defined(__amd64__) 81 #include <amd64/linux32/linux.h> 82 #include <amd64/linux32/linux32_proto.h> 83 #include <amd64/linux32/linux32_syscalls.c> 84 #include <amd64/linux32/linux32_systrace_args.c> 85 #else 86 #error Only amd64 is supported. 87 #endif 88 #define MODNAME "linux32" 89 extern struct sysent linux32_sysent[]; 90 #define MAXSYSCALL LINUX32_SYS_MAXSYSCALL 91 #define SYSCALLNAMES linux32_syscallnames 92 #define SYSENT linux32_sysent 93 #elif defined(FREEBSD32_SYSTRACE) 94 /* 95 * The syscall arguments are processed into a DTrace argument array 96 * using a generated function. See sys/tools/makesyscalls.lua. 97 */ 98 #include <compat/freebsd32/freebsd32_proto.h> 99 #include <compat/freebsd32/freebsd32_util.h> 100 #include <compat/freebsd32/freebsd32_syscall.h> 101 #include <compat/freebsd32/freebsd32_systrace_args.c> 102 extern const char *freebsd32_syscallnames[]; 103 #define MODNAME "freebsd32" 104 #define MAXSYSCALL FREEBSD32_SYS_MAXSYSCALL 105 #define SYSCALLNAMES freebsd32_syscallnames 106 #define SYSENT freebsd32_sysent 107 #else 108 /* 109 * The syscall arguments are processed into a DTrace argument array 110 * using a generated function. See sys/tools/makesyscalls.lua. 111 */ 112 #include <sys/syscall.h> 113 #include <kern/systrace_args.c> 114 #define MODNAME "freebsd" 115 #define MAXSYSCALL SYS_MAXSYSCALL 116 #define SYSCALLNAMES syscallnames 117 #define SYSENT sysent 118 #define NATIVE_ABI 119 #endif 120 121 #define PROVNAME "syscall" 122 #define DEVNAME "dtrace/systrace/" MODNAME 123 124 #define SYSTRACE_ARTIFICIAL_FRAMES 1 125 126 #define SYSTRACE_SHIFT 16 127 #define SYSTRACE_ISENTRY(x) ((int)(x) >> SYSTRACE_SHIFT) 128 #define SYSTRACE_SYSNUM(x) ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1)) 129 #define SYSTRACE_ENTRY(id) ((1 << SYSTRACE_SHIFT) | (id)) 130 #define SYSTRACE_RETURN(id) (id) 131 132 #if ((1 << SYSTRACE_SHIFT) <= MAXSYSCALL) 133 #error 1 << SYSTRACE_SHIFT must exceed number of system calls 134 #endif 135 136 static int systrace_enabled_count; 137 138 static void systrace_load(void *); 139 static void systrace_unload(void *); 140 141 static void systrace_getargdesc(void *, dtrace_id_t, void *, 142 dtrace_argdesc_t *); 143 static uint64_t systrace_getargval(void *, dtrace_id_t, void *, int, int); 144 static void systrace_provide(void *, dtrace_probedesc_t *); 145 static void systrace_destroy(void *, dtrace_id_t, void *); 146 static void systrace_enable(void *, dtrace_id_t, void *); 147 static void systrace_disable(void *, dtrace_id_t, void *); 148 149 static union { 150 const char **p_constnames; 151 char **pp_syscallnames; 152 } uglyhack = { SYSCALLNAMES }; 153 154 static dtrace_pattr_t systrace_attr = { 155 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 156 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 157 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 158 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 159 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 160 }; 161 162 static dtrace_pops_t systrace_pops = { 163 .dtps_provide = systrace_provide, 164 .dtps_provide_module = NULL, 165 .dtps_enable = systrace_enable, 166 .dtps_disable = systrace_disable, 167 .dtps_suspend = NULL, 168 .dtps_resume = NULL, 169 .dtps_getargdesc = systrace_getargdesc, 170 .dtps_getargval = systrace_getargval, 171 .dtps_usermode = NULL, 172 .dtps_destroy = systrace_destroy 173 }; 174 175 static dtrace_provider_id_t systrace_id; 176 177 #ifdef NATIVE_ABI 178 /* 179 * Probe callback function. 180 * 181 * Note: This function is called for _all_ syscalls, regardless of which sysent 182 * array the syscall comes from. It could be a standard syscall or a 183 * compat syscall from something like Linux. 184 */ 185 static void 186 systrace_probe(struct syscall_args *sa, enum systrace_probe_t type, int retval) 187 { 188 uint64_t uargs[nitems(sa->args)]; 189 dtrace_id_t id; 190 int n_args, sysnum; 191 192 sysnum = sa->code; 193 memset(uargs, 0, sizeof(uargs)); 194 195 if (type == SYSTRACE_ENTRY) { 196 if ((id = sa->callp->sy_entry) == DTRACE_IDNONE) 197 return; 198 199 if (sa->callp->sy_systrace_args_func != NULL) 200 /* 201 * Convert the syscall parameters using the registered 202 * function. 203 */ 204 (*sa->callp->sy_systrace_args_func)(sysnum, sa->args, 205 uargs, &n_args); 206 else 207 /* 208 * Use the built-in system call argument conversion 209 * function to translate the syscall structure fields 210 * into the array of 64-bit values that DTrace expects. 211 */ 212 systrace_args(sysnum, sa->args, uargs, &n_args); 213 /* 214 * Save probe arguments now so that we can retrieve them if 215 * the getargval method is called from further down the stack. 216 */ 217 curthread->t_dtrace_systrace_args = uargs; 218 } else { 219 if ((id = sa->callp->sy_return) == DTRACE_IDNONE) 220 return; 221 222 curthread->t_dtrace_systrace_args = NULL; 223 /* Set arg0 and arg1 as the return value of this syscall. */ 224 uargs[0] = uargs[1] = retval; 225 } 226 227 /* Process the probe using the converted argments. */ 228 dtrace_probe(id, uargs[0], uargs[1], uargs[2], uargs[3], uargs[4]); 229 } 230 #endif 231 232 static void 233 systrace_getargdesc(void *arg, dtrace_id_t id, void *parg, 234 dtrace_argdesc_t *desc) 235 { 236 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 237 238 if (SYSTRACE_ISENTRY((uintptr_t)parg)) 239 systrace_entry_setargdesc(sysnum, desc->dtargd_ndx, 240 desc->dtargd_native, sizeof(desc->dtargd_native)); 241 else 242 systrace_return_setargdesc(sysnum, desc->dtargd_ndx, 243 desc->dtargd_native, sizeof(desc->dtargd_native)); 244 245 if (desc->dtargd_native[0] == '\0') 246 desc->dtargd_ndx = DTRACE_ARGNONE; 247 } 248 249 static uint64_t 250 systrace_getargval(void *arg __unused, dtrace_id_t id __unused, 251 void *parg __unused, int argno, int aframes __unused) 252 { 253 uint64_t *uargs; 254 255 uargs = curthread->t_dtrace_systrace_args; 256 if (uargs == NULL) 257 /* This is a return probe. */ 258 return (0); 259 if (argno >= nitems(((struct syscall_args *)NULL)->args)) 260 return (0); 261 return (uargs[argno]); 262 } 263 264 static void 265 systrace_provide(void *arg, dtrace_probedesc_t *desc) 266 { 267 int i; 268 269 if (desc != NULL) 270 return; 271 272 for (i = 0; i < MAXSYSCALL; i++) { 273 if (dtrace_probe_lookup(systrace_id, MODNAME, 274 uglyhack.pp_syscallnames[i], "entry") != 0) 275 continue; 276 277 (void)dtrace_probe_create(systrace_id, MODNAME, 278 uglyhack.pp_syscallnames[i], "entry", 279 SYSTRACE_ARTIFICIAL_FRAMES, 280 (void *)((uintptr_t)SYSTRACE_ENTRY(i))); 281 (void)dtrace_probe_create(systrace_id, MODNAME, 282 uglyhack.pp_syscallnames[i], "return", 283 SYSTRACE_ARTIFICIAL_FRAMES, 284 (void *)((uintptr_t)SYSTRACE_RETURN(i))); 285 } 286 } 287 288 static void 289 systrace_destroy(void *arg, dtrace_id_t id, void *parg) 290 { 291 #ifdef SYSTRACE_DEBUG 292 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 293 294 /* 295 * There's nothing to do here but assert that we have actually been 296 * disabled. 297 */ 298 if (SYSTRACE_ISENTRY((uintptr_t)parg)) { 299 ASSERT(sysent[sysnum].sy_entry == DTRACE_IDNONE); 300 } else { 301 ASSERT(sysent[sysnum].sy_return == DTRACE_IDNONE); 302 } 303 #endif 304 } 305 306 static void 307 systrace_enable(void *arg, dtrace_id_t id, void *parg) 308 { 309 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 310 311 SYSENT[sysnum].sy_systrace_args_func = systrace_args; 312 313 if (SYSTRACE_ISENTRY((uintptr_t)parg)) 314 SYSENT[sysnum].sy_entry = id; 315 else 316 SYSENT[sysnum].sy_return = id; 317 systrace_enabled_count++; 318 if (systrace_enabled_count == 1) 319 systrace_enabled = true; 320 } 321 322 static void 323 systrace_disable(void *arg, dtrace_id_t id, void *parg) 324 { 325 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 326 327 SYSENT[sysnum].sy_systrace_args_func = NULL; 328 SYSENT[sysnum].sy_entry = DTRACE_IDNONE; 329 SYSENT[sysnum].sy_return = DTRACE_IDNONE; 330 systrace_enabled_count--; 331 if (systrace_enabled_count == 0) 332 systrace_enabled = false; 333 } 334 335 static void 336 systrace_load(void *dummy __unused) 337 { 338 339 if (dtrace_register(PROVNAME, &systrace_attr, DTRACE_PRIV_USER, NULL, 340 &systrace_pops, NULL, &systrace_id) != 0) 341 return; 342 343 #ifdef NATIVE_ABI 344 systrace_probe_func = systrace_probe; 345 #endif 346 } 347 348 static void 349 systrace_unload(void *dummy __unused) 350 { 351 352 #ifdef NATIVE_ABI 353 systrace_probe_func = NULL; 354 #endif 355 356 if (dtrace_unregister(systrace_id) != 0) 357 return; 358 } 359 360 static int 361 systrace_modevent(module_t mod __unused, int type, void *data __unused) 362 { 363 int error; 364 365 error = 0; 366 switch (type) { 367 case MOD_LOAD: 368 break; 369 370 case MOD_UNLOAD: 371 break; 372 373 case MOD_SHUTDOWN: 374 break; 375 376 default: 377 error = EOPNOTSUPP; 378 break; 379 380 } 381 return (error); 382 } 383 384 SYSINIT(systrace_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, 385 systrace_load, NULL); 386 SYSUNINIT(systrace_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, 387 systrace_unload, NULL); 388 389 #ifdef LINUX_SYSTRACE 390 DEV_MODULE(systrace_linux, systrace_modevent, NULL); 391 MODULE_VERSION(systrace_linux, 1); 392 #ifdef __amd64__ 393 MODULE_DEPEND(systrace_linux, linux64, 1, 1, 1); 394 #else 395 MODULE_DEPEND(systrace_linux, linux, 1, 1, 1); 396 #endif 397 MODULE_DEPEND(systrace_linux, dtrace, 1, 1, 1); 398 MODULE_DEPEND(systrace_linux, opensolaris, 1, 1, 1); 399 #elif defined(LINUX32_SYSTRACE) 400 DEV_MODULE(systrace_linux32, systrace_modevent, NULL); 401 MODULE_VERSION(systrace_linux32, 1); 402 MODULE_DEPEND(systrace_linux32, linux, 1, 1, 1); 403 MODULE_DEPEND(systrace_linux32, dtrace, 1, 1, 1); 404 MODULE_DEPEND(systrace_linux32, opensolaris, 1, 1, 1); 405 #elif defined(FREEBSD32_SYSTRACE) 406 DEV_MODULE(systrace_freebsd32, systrace_modevent, NULL); 407 MODULE_VERSION(systrace_freebsd32, 1); 408 MODULE_DEPEND(systrace_freebsd32, dtrace, 1, 1, 1); 409 MODULE_DEPEND(systrace_freebsd32, opensolaris, 1, 1, 1); 410 #else 411 DEV_MODULE(systrace, systrace_modevent, NULL); 412 MODULE_VERSION(systrace, 1); 413 MODULE_DEPEND(systrace, dtrace, 1, 1, 1); 414 MODULE_DEPEND(systrace, opensolaris, 1, 1, 1); 415 #endif 416