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