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>
63 #include <linux_syscall.h>
64 #include <linux_proto.h>
65 #include <linux_syscallnames.c>
66 #include <linux_systrace.c>
67 extern struct sysent linux_sysent[];
68 #define DEVNAME "dtrace/linsystrace"
69 #define PROVNAME "linsyscall"
70 #define MAXSYSCALL LINUX_SYS_MAXSYSCALL
71 #define SYSCALLNAMES linux_syscallnames
72 #define SYSENT linux_sysent
75 * The syscall arguments are processed into a DTrace argument array
76 * using a generated function. See sys/kern/makesyscalls.sh.
78 #include <sys/syscall.h>
79 #include <kern/systrace_args.c>
80 #define DEVNAME "dtrace/systrace"
81 #define PROVNAME "syscall"
82 #define MAXSYSCALL SYS_MAXSYSCALL
83 #define SYSCALLNAMES syscallnames
87 #define SYSTRACE_ARTIFICIAL_FRAMES 1
89 #define SYSTRACE_SHIFT 16
90 #define SYSTRACE_ISENTRY(x) ((int)(x) >> SYSTRACE_SHIFT)
91 #define SYSTRACE_SYSNUM(x) ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1))
92 #define SYSTRACE_ENTRY(id) ((1 << SYSTRACE_SHIFT) | (id))
93 #define SYSTRACE_RETURN(id) (id)
95 #if ((1 << SYSTRACE_SHIFT) <= MAXSYSCALL)
96 #error 1 << SYSTRACE_SHIFT must exceed number of system calls
99 static d_open_t systrace_open;
100 static int systrace_unload(void);
101 static void systrace_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
102 static void systrace_provide(void *, dtrace_probedesc_t *);
103 static void systrace_destroy(void *, dtrace_id_t, void *);
104 static void systrace_enable(void *, dtrace_id_t, void *);
105 static void systrace_disable(void *, dtrace_id_t, void *);
106 static void systrace_load(void *);
108 static struct cdevsw systrace_cdevsw = {
109 .d_version = D_VERSION,
110 .d_open = systrace_open,
111 #ifdef LINUX_SYSTRACE
112 .d_name = "linsystrace",
114 .d_name = "systrace",
119 const char **p_constnames;
120 char **pp_syscallnames;
121 } uglyhack = { SYSCALLNAMES };
123 static dtrace_pattr_t systrace_attr = {
124 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
125 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
126 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
127 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
128 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
131 static dtrace_pops_t systrace_pops = {
144 static struct cdev *systrace_cdev;
145 static dtrace_provider_id_t systrace_id;
147 #if !defined(LINUX_SYSTRACE)
149 * Probe callback function.
151 * Note: This function is called for _all_ syscalls, regardless of which sysent
152 * array the syscall comes from. It could be a standard syscall or a
153 * compat syscall from something like Linux.
156 systrace_probe(u_int32_t id, int sysnum, struct sysent *sysent, void *params,
162 memset(uargs, 0, sizeof(uargs));
164 * Check if this syscall has an argument conversion function
167 if (params && sysent->sy_systrace_args_func != NULL) {
169 * Convert the syscall parameters using the registered
172 (*sysent->sy_systrace_args_func)(sysnum, params, uargs, &n_args);
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
180 systrace_args(sysnum, params, uargs, &n_args);
183 * Since params is NULL, this is a 'return' probe.
184 * Set arg0 and arg1 as the return value of this syscall.
186 uargs[0] = uargs[1] = ret;
189 /* Process the probe using the converted argments. */
190 dtrace_probe(id, uargs[0], uargs[1], uargs[2], uargs[3], uargs[4]);
195 systrace_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
197 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
199 systrace_setargdesc(sysnum, desc->dtargd_ndx, desc->dtargd_native,
200 sizeof(desc->dtargd_native));
202 if (desc->dtargd_native[0] == '\0')
203 desc->dtargd_ndx = DTRACE_ARGNONE;
209 systrace_provide(void *arg, dtrace_probedesc_t *desc)
216 for (i = 0; i < MAXSYSCALL; i++) {
217 if (dtrace_probe_lookup(systrace_id, NULL,
218 uglyhack.pp_syscallnames[i], "entry") != 0)
221 (void) dtrace_probe_create(systrace_id, NULL, uglyhack.pp_syscallnames[i],
222 "entry", SYSTRACE_ARTIFICIAL_FRAMES,
223 (void *)((uintptr_t)SYSTRACE_ENTRY(i)));
224 (void) dtrace_probe_create(systrace_id, NULL, uglyhack.pp_syscallnames[i],
225 "return", SYSTRACE_ARTIFICIAL_FRAMES,
226 (void *)((uintptr_t)SYSTRACE_RETURN(i)));
231 systrace_destroy(void *arg, dtrace_id_t id, void *parg)
234 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
237 * There's nothing to do here but assert that we have actually been
240 if (SYSTRACE_ISENTRY((uintptr_t)parg)) {
241 ASSERT(sysent[sysnum].sy_entry == 0);
243 ASSERT(sysent[sysnum].sy_return == 0);
249 systrace_enable(void *arg, dtrace_id_t id, void *parg)
251 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
253 if (SYSENT[sysnum].sy_systrace_args_func == NULL)
254 SYSENT[sysnum].sy_systrace_args_func = systrace_args;
256 if (SYSTRACE_ISENTRY((uintptr_t)parg))
257 SYSENT[sysnum].sy_entry = id;
259 SYSENT[sysnum].sy_return = id;
263 systrace_disable(void *arg, dtrace_id_t id, void *parg)
265 int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
267 SYSENT[sysnum].sy_entry = 0;
268 SYSENT[sysnum].sy_return = 0;
272 systrace_load(void *dummy)
274 /* Create the /dev/dtrace/systrace entry. */
275 systrace_cdev = make_dev(&systrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
278 if (dtrace_register(PROVNAME, &systrace_attr, DTRACE_PRIV_USER,
279 NULL, &systrace_pops, NULL, &systrace_id) != 0)
282 #if !defined(LINUX_SYSTRACE)
283 systrace_probe_func = systrace_probe;
293 if ((error = dtrace_unregister(systrace_id)) != 0)
296 #if !defined(LINUX_SYSTRACE)
297 systrace_probe_func = NULL;
300 destroy_dev(systrace_cdev);
306 systrace_modevent(module_t mod __unused, int type, void *data __unused)
329 systrace_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
334 SYSINIT(systrace_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_load, NULL);
335 SYSUNINIT(systrace_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_unload, NULL);
337 #ifdef LINUX_SYSTRACE
338 DEV_MODULE(linsystrace, systrace_modevent, NULL);
339 MODULE_VERSION(linsystrace, 1);
340 MODULE_DEPEND(linsystrace, linux, 1, 1, 1);
341 MODULE_DEPEND(linsystrace, systrace, 1, 1, 1);
342 MODULE_DEPEND(linsystrace, dtrace, 1, 1, 1);
343 MODULE_DEPEND(linsystrace, opensolaris, 1, 1, 1);
345 DEV_MODULE(systrace, systrace_modevent, NULL);
346 MODULE_VERSION(systrace, 1);
347 MODULE_DEPEND(systrace, dtrace, 1, 1, 1);
348 MODULE_DEPEND(systrace, opensolaris, 1, 1, 1);