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