1 /* $NetBSD: systrace.c,v 1.3 2011/07/17 20:54:33 joerg 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/kdb.h> 42 #include <sys/kernel.h> 43 #include <sys/kmem.h> 44 #include <sys/kthread.h> 45 #include <sys/limits.h> 46 #include <sys/linker.h> 47 #include <sys/lock.h> 48 #include <sys/malloc.h> 49 #include <sys/module.h> 50 #include <sys/mutex.h> 51 #include <sys/poll.h> 52 #include <sys/proc.h> 53 #include <sys/selinfo.h> 54 #include <sys/smp.h> 55 #include <sys/sysproto.h> 56 #include <sys/sysent.h> 57 #include <sys/uio.h> 58 #include <sys/unistd.h> 59 60 #include <sys/dtrace.h> 61 62 #ifdef LINUX_SYSTRACE 63 #include <linux.h> 64 #include <linux_syscall.h> 65 #include <linux_proto.h> 66 #include <linux_syscallnames.c> 67 #include <linux_systrace.c> 68 extern struct sysent linux_sysent[]; 69 #define DEVNAME "dtrace/linsystrace" 70 #define PROVNAME "linsyscall" 71 #define MAXSYSCALL LINUX_SYS_MAXSYSCALL 72 #define SYSCALLNAMES linux_syscallnames 73 #define SYSENT linux_sysent 74 #else 75 /* 76 * The syscall arguments are processed into a DTrace argument array 77 * using a generated function. See sys/kern/makesyscalls.sh. 78 */ 79 #include <sys/syscall.h> 80 #include <kern/systrace_args.c> 81 extern const char *syscallnames[]; 82 #define DEVNAME "dtrace/systrace" 83 #define PROVNAME "syscall" 84 #define MAXSYSCALL SYS_MAXSYSCALL 85 #define SYSCALLNAMES syscallnames 86 #define SYSENT sysent 87 #endif 88 89 #define SYSTRACE_ARTIFICIAL_FRAMES 1 90 91 #define SYSTRACE_SHIFT 16 92 #define SYSTRACE_ISENTRY(x) ((int)(x) >> SYSTRACE_SHIFT) 93 #define SYSTRACE_SYSNUM(x) ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1)) 94 #define SYSTRACE_ENTRY(id) ((1 << SYSTRACE_SHIFT) | (id)) 95 #define SYSTRACE_RETURN(id) (id) 96 97 #if ((1 << SYSTRACE_SHIFT) <= MAXSYSCALL) 98 #error 1 << SYSTRACE_SHIFT must exceed number of system calls 99 #endif 100 101 static d_open_t systrace_open; 102 static int systrace_unload(void); 103 static void systrace_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *); 104 static void systrace_provide(void *, dtrace_probedesc_t *); 105 static void systrace_destroy(void *, dtrace_id_t, void *); 106 static void systrace_enable(void *, dtrace_id_t, void *); 107 static void systrace_disable(void *, dtrace_id_t, void *); 108 static void systrace_load(void *); 109 110 static struct cdevsw systrace_cdevsw = { 111 .d_version = D_VERSION, 112 .d_open = systrace_open, 113 #ifdef LINUX_SYSTRACE 114 .d_name = "linsystrace", 115 #else 116 .d_name = "systrace", 117 #endif 118 }; 119 120 static union { 121 const char **p_constnames; 122 char **pp_syscallnames; 123 } uglyhack = { SYSCALLNAMES }; 124 125 static dtrace_pattr_t systrace_attr = { 126 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 127 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 128 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 129 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON }, 130 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 131 }; 132 133 static dtrace_pops_t systrace_pops = { 134 systrace_provide, 135 NULL, 136 systrace_enable, 137 systrace_disable, 138 NULL, 139 NULL, 140 systrace_getargdesc, 141 NULL, 142 NULL, 143 systrace_destroy 144 }; 145 146 static struct cdev *systrace_cdev; 147 static dtrace_provider_id_t systrace_id; 148 149 #if !defined(LINUX_SYSTRACE) 150 /* 151 * Probe callback function. 152 * 153 * Note: This function is called for _all_ syscalls, regardless of which sysent 154 * array the syscall comes from. It could be a standard syscall or a 155 * compat syscall from something like Linux. 156 */ 157 static void 158 systrace_probe(u_int32_t id, int sysnum, struct sysent *sysent, void *params) 159 { 160 int n_args = 0; 161 u_int64_t uargs[8]; 162 163 /* 164 * Check if this syscall has an argument conversion function 165 * registered. 166 */ 167 if (sysent->sy_systrace_args_func != NULL) 168 /* 169 * Convert the syscall parameters using the registered 170 * function. 171 */ 172 (*sysent->sy_systrace_args_func)(sysnum, params, uargs, &n_args); 173 else 174 /* 175 * Use the built-in system call argument conversion 176 * function to translate the syscall structure fields 177 * into the array of 64-bit values that DTrace 178 * expects. 179 */ 180 systrace_args(sysnum, params, uargs, &n_args); 181 182 /* Process the probe using the converted argments. */ 183 dtrace_probe(id, uargs[0], uargs[1], uargs[2], uargs[3], uargs[4]); 184 } 185 #endif 186 187 static void 188 systrace_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc) 189 { 190 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 191 192 systrace_setargdesc(sysnum, desc->dtargd_ndx, desc->dtargd_native, 193 sizeof(desc->dtargd_native)); 194 195 if (desc->dtargd_native[0] == '\0') 196 desc->dtargd_ndx = DTRACE_ARGNONE; 197 198 return; 199 } 200 201 static void 202 systrace_provide(void *arg, dtrace_probedesc_t *desc) 203 { 204 int i; 205 206 if (desc != NULL) 207 return; 208 209 for (i = 0; i < MAXSYSCALL; i++) { 210 if (dtrace_probe_lookup(systrace_id, NULL, 211 uglyhack.pp_syscallnames[i], "entry") != 0) 212 continue; 213 214 (void) dtrace_probe_create(systrace_id, NULL, uglyhack.pp_syscallnames[i], 215 "entry", SYSTRACE_ARTIFICIAL_FRAMES, 216 (void *)((uintptr_t)SYSTRACE_ENTRY(i))); 217 (void) dtrace_probe_create(systrace_id, NULL, uglyhack.pp_syscallnames[i], 218 "return", SYSTRACE_ARTIFICIAL_FRAMES, 219 (void *)((uintptr_t)SYSTRACE_RETURN(i))); 220 } 221 } 222 223 static void 224 systrace_destroy(void *arg, dtrace_id_t id, void *parg) 225 { 226 #ifdef DEBUG 227 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 228 229 /* 230 * There's nothing to do here but assert that we have actually been 231 * disabled. 232 */ 233 if (SYSTRACE_ISENTRY((uintptr_t)parg)) { 234 ASSERT(sysent[sysnum].sy_entry == 0); 235 } else { 236 ASSERT(sysent[sysnum].sy_return == 0); 237 } 238 #endif 239 } 240 241 static void 242 systrace_enable(void *arg, dtrace_id_t id, void *parg) 243 { 244 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 245 246 if (SYSENT[sysnum].sy_systrace_args_func == NULL) 247 SYSENT[sysnum].sy_systrace_args_func = systrace_args; 248 249 if (SYSTRACE_ISENTRY((uintptr_t)parg)) 250 SYSENT[sysnum].sy_entry = id; 251 else 252 SYSENT[sysnum].sy_return = id; 253 } 254 255 static void 256 systrace_disable(void *arg, dtrace_id_t id, void *parg) 257 { 258 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg); 259 260 SYSENT[sysnum].sy_entry = 0; 261 SYSENT[sysnum].sy_return = 0; 262 } 263 264 static void 265 systrace_load(void *dummy) 266 { 267 /* Create the /dev/dtrace/systrace entry. */ 268 systrace_cdev = make_dev(&systrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 269 DEVNAME); 270 271 if (dtrace_register(PROVNAME, &systrace_attr, DTRACE_PRIV_USER, 272 NULL, &systrace_pops, NULL, &systrace_id) != 0) 273 return; 274 275 #if !defined(LINUX_SYSTRACE) 276 systrace_probe_func = systrace_probe; 277 #endif 278 } 279 280 281 static int 282 systrace_unload() 283 { 284 int error = 0; 285 286 if ((error = dtrace_unregister(systrace_id)) != 0) 287 return (error); 288 289 #if !defined(LINUX_SYSTRACE) 290 systrace_probe_func = NULL; 291 #endif 292 293 destroy_dev(systrace_cdev); 294 295 return (error); 296 } 297 298 static int 299 systrace_modevent(module_t mod __unused, int type, void *data __unused) 300 { 301 int error = 0; 302 303 switch (type) { 304 case MOD_LOAD: 305 break; 306 307 case MOD_UNLOAD: 308 break; 309 310 case MOD_SHUTDOWN: 311 break; 312 313 default: 314 error = EOPNOTSUPP; 315 break; 316 317 } 318 return (error); 319 } 320 321 static int 322 systrace_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused) 323 { 324 return (0); 325 } 326 327 SYSINIT(systrace_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_load, NULL); 328 SYSUNINIT(systrace_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_unload, NULL); 329 330 #ifdef LINUX_SYSTRACE 331 DEV_MODULE(linsystrace, systrace_modevent, NULL); 332 MODULE_VERSION(linsystrace, 1); 333 MODULE_DEPEND(linsystrace, linux, 1, 1, 1); 334 MODULE_DEPEND(linsystrace, systrace, 1, 1, 1); 335 MODULE_DEPEND(linsystrace, dtrace, 1, 1, 1); 336 MODULE_DEPEND(linsystrace, opensolaris, 1, 1, 1); 337 #else 338 DEV_MODULE(systrace, systrace_modevent, NULL); 339 MODULE_VERSION(systrace, 1); 340 MODULE_DEPEND(systrace, dtrace, 1, 1, 1); 341 MODULE_DEPEND(systrace, opensolaris, 1, 1, 1); 342 #endif 343