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