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.
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.
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]
21 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
28 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
29 * Use is subject to license terms.
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/cpuvar.h>
37 #include <sys/fcntl.h>
38 #include <sys/filio.h>
40 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/limits.h>
44 #include <sys/linker.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
51 #include <sys/selinfo.h>
53 #include <sys/sysproto.h>
54 #include <sys/sysent.h>
56 #include <sys/unistd.h>
57 #include <machine/stdarg.h>
59 #include <sys/dtrace.h>
62 #if defined(__amd64__)
63 #include <amd64/linux/linux.h>
64 #include <amd64/linux/linux_proto.h>
65 #include <amd64/linux/linux_syscalls.c>
66 #include <amd64/linux/linux_systrace_args.c>
67 #elif defined(__i386__)
68 #include <i386/linux/linux.h>
69 #include <i386/linux/linux_proto.h>
70 #include <i386/linux/linux_syscalls.c>
71 #include <i386/linux/linux_systrace_args.c>
73 #error Only i386 and amd64 are supported.
75 #define MODNAME "linux"
76 extern struct sysent linux_sysent[];
77 #define MAXSYSCALL LINUX_SYS_MAXSYSCALL
78 #define SYSCALLNAMES linux_syscallnames
79 #define SYSENT linux_sysent
80 #elif defined(LINUX32_SYSTRACE)
81 #if defined(__amd64__)
82 #include <amd64/linux32/linux.h>
83 #include <amd64/linux32/linux32_proto.h>
84 #include <amd64/linux32/linux32_syscalls.c>
85 #include <amd64/linux32/linux32_systrace_args.c>
87 #error Only amd64 is supported.
89 #define MODNAME "linux32"
90 extern struct sysent linux32_sysent[];
91 #define MAXSYSCALL LINUX32_SYS_MAXSYSCALL
92 #define SYSCALLNAMES linux32_syscallnames
93 #define SYSENT linux32_sysent
94 #elif defined(FREEBSD32_SYSTRACE)
96 * The syscall arguments are processed into a DTrace argument array
97 * using a generated function. See sys/kern/makesyscalls.sh.
99 #include <compat/freebsd32/freebsd32_proto.h>
100 #include <compat/freebsd32/freebsd32_util.h>
101 #include <compat/freebsd32/freebsd32_syscall.h>
102 #include <compat/freebsd32/freebsd32_systrace_args.c>
103 extern const char *freebsd32_syscallnames[];
104 #define MODNAME "freebsd32"
105 #define MAXSYSCALL FREEBSD32_SYS_MAXSYSCALL
106 #define SYSCALLNAMES freebsd32_syscallnames
107 #define SYSENT freebsd32_sysent
110 * The syscall arguments are processed into a DTrace argument array
111 * using a generated function. See sys/kern/makesyscalls.sh.
113 #include <sys/syscall.h>
114 #include <kern/systrace_args.c>
115 #define MODNAME "freebsd"
116 #define MAXSYSCALL SYS_MAXSYSCALL
117 #define SYSCALLNAMES syscallnames
118 #define SYSENT sysent
122 #define PROVNAME "syscall"
123 #define DEVNAME "dtrace/systrace/" MODNAME
125 #define SYSTRACE_ARTIFICIAL_FRAMES 1
127 #define SYSTRACE_SHIFT 16
128 #define SYSTRACE_ISENTRY(x) ((int)(x) >> SYSTRACE_SHIFT)
129 #define SYSTRACE_SYSNUM(x) ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1))
130 #define SYSTRACE_ENTRY(id) ((1 << SYSTRACE_SHIFT) | (id))
131 #define SYSTRACE_RETURN(id) (id)
133 #if ((1 << SYSTRACE_SHIFT) <= MAXSYSCALL)
134 #error 1 << SYSTRACE_SHIFT must exceed number of system calls
137 static d_open_t systrace_open;
138 static int systrace_unload(void);
139 static void systrace_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
140 static void systrace_provide(void *, dtrace_probedesc_t *);
141 static void systrace_destroy(void *, dtrace_id_t, void *);
142 static void systrace_enable(void *, dtrace_id_t, void *);
143 static void systrace_disable(void *, dtrace_id_t, void *);
144 static void systrace_load(void *);
146 static struct cdevsw systrace_cdevsw = {
147 .d_version = D_VERSION,
148 .d_open = systrace_open,
150 .d_name = "systrace_" MODNAME,
152 .d_name = "systrace",
157 const char **p_constnames;
158 char **pp_syscallnames;
159 } uglyhack = { SYSCALLNAMES };
161 static dtrace_pattr_t systrace_attr = {
162 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
163 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
164 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
165 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
166 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
169 static dtrace_pops_t systrace_pops = {
182 static struct cdev *systrace_cdev;
183 static dtrace_provider_id_t systrace_id;
185 typedef void (*systrace_dtrace_probe_t)(dtrace_id_t, uintptr_t, uintptr_t,
186 uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
190 * Probe callback function.
192 * Note: This function is called for _all_ syscalls, regardless of which sysent
193 * array the syscall comes from. It could be a standard syscall or a
194 * compat syscall from something like Linux.
197 systrace_probe(u_int32_t id, int sysnum, struct sysent *sysent, void *params,
200 systrace_dtrace_probe_t probe;
204 memset(uargs, 0, sizeof(uargs));
206 * Check if this syscall has an argument conversion function
209 if (params && sysent->sy_systrace_args_func != NULL) {
211 * Convert the syscall parameters using the registered
214 (*sysent->sy_systrace_args_func)(sysnum, params, uargs, &n_args);
217 * Use the built-in system call argument conversion
218 * function to translate the syscall structure fields
219 * into the array of 64-bit values that DTrace
222 systrace_args(sysnum, params, uargs, &n_args);
225 * Since params is NULL, this is a 'return' probe.
226 * Set arg0 and arg1 as the return value of this syscall.
228 uargs[0] = uargs[1] = ret;
231 /* Process the probe using the converted argments. */
232 probe = (systrace_dtrace_probe_t)dtrace_probe;
233 probe(id, uargs[0], uargs[1], uargs[2], uargs[3], uargs[4], uargs[5],
240 systrace_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
242 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
244 if (SYSTRACE_ISENTRY((uintptr_t)parg))
245 systrace_entry_setargdesc(sysnum, desc->dtargd_ndx,
246 desc->dtargd_native, sizeof(desc->dtargd_native));
248 systrace_return_setargdesc(sysnum, desc->dtargd_ndx,
249 desc->dtargd_native, sizeof(desc->dtargd_native));
251 if (desc->dtargd_native[0] == '\0')
252 desc->dtargd_ndx = DTRACE_ARGNONE;
258 systrace_provide(void *arg, dtrace_probedesc_t *desc)
265 for (i = 0; i < MAXSYSCALL; i++) {
266 if (dtrace_probe_lookup(systrace_id, MODNAME,
267 uglyhack.pp_syscallnames[i], "entry") != 0)
270 (void) dtrace_probe_create(systrace_id, MODNAME, uglyhack.pp_syscallnames[i],
271 "entry", SYSTRACE_ARTIFICIAL_FRAMES,
272 (void *)((uintptr_t)SYSTRACE_ENTRY(i)));
273 (void) dtrace_probe_create(systrace_id, MODNAME, uglyhack.pp_syscallnames[i],
274 "return", SYSTRACE_ARTIFICIAL_FRAMES,
275 (void *)((uintptr_t)SYSTRACE_RETURN(i)));
280 systrace_destroy(void *arg, dtrace_id_t id, void *parg)
283 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
286 * There's nothing to do here but assert that we have actually been
289 if (SYSTRACE_ISENTRY((uintptr_t)parg)) {
290 ASSERT(sysent[sysnum].sy_entry == 0);
292 ASSERT(sysent[sysnum].sy_return == 0);
298 systrace_enable(void *arg, dtrace_id_t id, void *parg)
300 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
302 if (SYSENT[sysnum].sy_systrace_args_func == NULL)
303 SYSENT[sysnum].sy_systrace_args_func = systrace_args;
305 if (SYSTRACE_ISENTRY((uintptr_t)parg))
306 SYSENT[sysnum].sy_entry = id;
308 SYSENT[sysnum].sy_return = id;
312 systrace_disable(void *arg, dtrace_id_t id, void *parg)
314 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
316 SYSENT[sysnum].sy_entry = 0;
317 SYSENT[sysnum].sy_return = 0;
321 systrace_load(void *dummy)
323 /* Create the /dev/dtrace/systrace entry. */
324 systrace_cdev = make_dev(&systrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
327 if (dtrace_register(PROVNAME, &systrace_attr, DTRACE_PRIV_USER,
328 NULL, &systrace_pops, NULL, &systrace_id) != 0)
332 systrace_probe_func = systrace_probe;
342 if ((error = dtrace_unregister(systrace_id)) != 0)
346 systrace_probe_func = NULL;
349 destroy_dev(systrace_cdev);
355 systrace_modevent(module_t mod __unused, int type, void *data __unused)
378 systrace_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
383 SYSINIT(systrace_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_load, NULL);
384 SYSUNINIT(systrace_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_unload, NULL);
386 #ifdef LINUX_SYSTRACE
387 DEV_MODULE(systrace_linux, systrace_modevent, NULL);
388 MODULE_VERSION(systrace_linux, 1);
390 MODULE_DEPEND(systrace_linux, linux64, 1, 1, 1);
392 MODULE_DEPEND(systrace_linux, linux, 1, 1, 1);
394 MODULE_DEPEND(systrace_linux, dtrace, 1, 1, 1);
395 MODULE_DEPEND(systrace_linux, opensolaris, 1, 1, 1);
396 #elif defined(LINUX32_SYSTRACE)
397 DEV_MODULE(systrace_linux32, systrace_modevent, NULL);
398 MODULE_VERSION(systrace_linux32, 1);
399 MODULE_DEPEND(systrace_linux32, linux, 1, 1, 1);
400 MODULE_DEPEND(systrace_linux32, dtrace, 1, 1, 1);
401 MODULE_DEPEND(systrace_linux32, opensolaris, 1, 1, 1);
402 #elif defined(FREEBSD32_SYSTRACE)
403 DEV_MODULE(systrace_freebsd32, systrace_modevent, NULL);
404 MODULE_VERSION(systrace_freebsd32, 1);
405 MODULE_DEPEND(systrace_freebsd32, dtrace, 1, 1, 1);
406 MODULE_DEPEND(systrace_freebsd32, opensolaris, 1, 1, 1);
408 DEV_MODULE(systrace, systrace_modevent, NULL);
409 MODULE_VERSION(systrace, 1);
410 MODULE_DEPEND(systrace, dtrace, 1, 1, 1);
411 MODULE_DEPEND(systrace, opensolaris, 1, 1, 1);