xref: /freebsd/sys/cddl/dev/systrace/systrace.c (revision 39beb93c)
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 #include <linux.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
73 #else
74 /*
75  * The syscall arguments are processed into a DTrace argument array
76  * using a generated function. See sys/kern/makesyscalls.sh.
77  */
78 #include <sys/syscall.h>
79 #include <kern/systrace_args.c>
80 extern const char	*syscallnames[];
81 #define	DEVNAME		"dtrace/systrace"
82 #define	PROVNAME	"syscall"
83 #define	MAXSYSCALL	SYS_MAXSYSCALL
84 #define	SYSCALLNAMES	syscallnames
85 #define	SYSENT		sysent
86 #endif
87 
88 #define	SYSTRACE_ARTIFICIAL_FRAMES	1
89 
90 #define	SYSTRACE_SHIFT			16
91 #define	SYSTRACE_ISENTRY(x)		((int)(x) >> SYSTRACE_SHIFT)
92 #define	SYSTRACE_SYSNUM(x)		((int)(x) & ((1 << SYSTRACE_SHIFT) - 1))
93 #define	SYSTRACE_ENTRY(id)		((1 << SYSTRACE_SHIFT) | (id))
94 #define	SYSTRACE_RETURN(id)		(id)
95 
96 #if ((1 << SYSTRACE_SHIFT) <= MAXSYSCALL)
97 #error 1 << SYSTRACE_SHIFT must exceed number of system calls
98 #endif
99 
100 static d_open_t	systrace_open;
101 static int	systrace_unload(void);
102 static void	systrace_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
103 static void	systrace_provide(void *, dtrace_probedesc_t *);
104 static void	systrace_destroy(void *, dtrace_id_t, void *);
105 static void	systrace_enable(void *, dtrace_id_t, void *);
106 static void	systrace_disable(void *, dtrace_id_t, void *);
107 static void	systrace_load(void *);
108 
109 static struct cdevsw systrace_cdevsw = {
110 	.d_version	= D_VERSION,
111 	.d_open		= systrace_open,
112 #ifdef LINUX_SYSTRACE
113 	.d_name		= "linsystrace",
114 #else
115 	.d_name		= "systrace",
116 #endif
117 };
118 
119 static union	{
120 	const char	**p_constnames;
121 	char		**pp_syscallnames;
122 } uglyhack = { SYSCALLNAMES };
123 
124 static dtrace_pattr_t systrace_attr = {
125 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
126 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
127 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
128 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
129 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
130 };
131 
132 static dtrace_pops_t systrace_pops = {
133 	systrace_provide,
134 	NULL,
135 	systrace_enable,
136 	systrace_disable,
137 	NULL,
138 	NULL,
139 	systrace_getargdesc,
140 	NULL,
141 	NULL,
142 	systrace_destroy
143 };
144 
145 static struct cdev		*systrace_cdev;
146 static dtrace_provider_id_t	systrace_id;
147 
148 #if !defined(LINUX_SYSTRACE)
149 /*
150  * Probe callback function.
151  *
152  * Note: This function is called for _all_ syscalls, regardless of which sysent
153  *       array the syscall comes from. It could be a standard syscall or a
154  *       compat syscall from something like Linux.
155  */
156 static void
157 systrace_probe(u_int32_t id, int sysnum, struct sysent *sysent, void *params)
158 {
159 	int		n_args	= 0;
160 	u_int64_t	uargs[8];
161 
162 	/*
163 	 * Check if this syscall has an argument conversion function
164 	 * registered.
165 	 */
166 	if (sysent->sy_systrace_args_func != NULL)
167 		/*
168 		 * Convert the syscall parameters using the registered
169 		 * function.
170 		 */
171 		(*sysent->sy_systrace_args_func)(sysnum, params, uargs, &n_args);
172 	else
173 		/*
174 		 * Use the built-in system call argument conversion
175 		 * function to translate the syscall structure fields
176 		 * into the array of 64-bit values that DTrace
177 		 * expects.
178 		 */
179 		systrace_args(sysnum, params, uargs, &n_args);
180 
181 	/* Process the probe using the converted argments. */
182 	dtrace_probe(id, uargs[0], uargs[1], uargs[2], uargs[3], uargs[4]);
183 }
184 #endif
185 
186 static void
187 systrace_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
188 {
189 	int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
190 
191 	systrace_setargdesc(sysnum, desc->dtargd_ndx, desc->dtargd_native,
192 	    sizeof(desc->dtargd_native));
193 
194 	if (desc->dtargd_native[0] == '\0')
195 		desc->dtargd_ndx = DTRACE_ARGNONE;
196 
197 	return;
198 }
199 
200 static void
201 systrace_provide(void *arg, dtrace_probedesc_t *desc)
202 {
203 	int i;
204 
205 	if (desc != NULL)
206 		return;
207 
208 	for (i = 0; i < MAXSYSCALL; i++) {
209 		if (dtrace_probe_lookup(systrace_id, NULL,
210 		    uglyhack.pp_syscallnames[i], "entry") != 0)
211 			continue;
212 
213 		(void) dtrace_probe_create(systrace_id, NULL, uglyhack.pp_syscallnames[i],
214 		    "entry", SYSTRACE_ARTIFICIAL_FRAMES,
215 		    (void *)((uintptr_t)SYSTRACE_ENTRY(i)));
216 		(void) dtrace_probe_create(systrace_id, NULL, uglyhack.pp_syscallnames[i],
217 		    "return", SYSTRACE_ARTIFICIAL_FRAMES,
218 		    (void *)((uintptr_t)SYSTRACE_RETURN(i)));
219 	}
220 }
221 
222 static void
223 systrace_destroy(void *arg, dtrace_id_t id, void *parg)
224 {
225 #ifdef DEBUG
226 	int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
227 
228 	/*
229 	 * There's nothing to do here but assert that we have actually been
230 	 * disabled.
231 	 */
232 	if (SYSTRACE_ISENTRY((uintptr_t)parg)) {
233 		ASSERT(sysent[sysnum].sy_entry == 0);
234 	} else {
235 		ASSERT(sysent[sysnum].sy_return == 0);
236 	}
237 #endif
238 }
239 
240 static void
241 systrace_enable(void *arg, dtrace_id_t id, void *parg)
242 {
243 	int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
244 
245 	if (SYSENT[sysnum].sy_systrace_args_func == NULL)
246 		SYSENT[sysnum].sy_systrace_args_func = systrace_args;
247 
248 	if (SYSTRACE_ISENTRY((uintptr_t)parg))
249 		SYSENT[sysnum].sy_entry = id;
250 	else
251 		SYSENT[sysnum].sy_return = id;
252 }
253 
254 static void
255 systrace_disable(void *arg, dtrace_id_t id, void *parg)
256 {
257 	int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
258 
259 	SYSENT[sysnum].sy_entry = 0;
260 	SYSENT[sysnum].sy_return = 0;
261 }
262 
263 static void
264 systrace_load(void *dummy)
265 {
266 	/* Create the /dev/dtrace/systrace entry. */
267 	systrace_cdev = make_dev(&systrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
268 	   DEVNAME);
269 
270 	if (dtrace_register(PROVNAME, &systrace_attr, DTRACE_PRIV_USER,
271 	    NULL, &systrace_pops, NULL, &systrace_id) != 0)
272 		return;
273 
274 #if !defined(LINUX_SYSTRACE)
275 	systrace_probe_func = systrace_probe;
276 #endif
277 }
278 
279 
280 static int
281 systrace_unload()
282 {
283 	int error = 0;
284 
285 	if ((error = dtrace_unregister(systrace_id)) != 0)
286 		return (error);
287 
288 #if !defined(LINUX_SYSTRACE)
289 	systrace_probe_func = NULL;
290 #endif
291 
292 	destroy_dev(systrace_cdev);
293 
294 	return (error);
295 }
296 
297 static int
298 systrace_modevent(module_t mod __unused, int type, void *data __unused)
299 {
300 	int error = 0;
301 
302 	switch (type) {
303 	case MOD_LOAD:
304 		break;
305 
306 	case MOD_UNLOAD:
307 		break;
308 
309 	case MOD_SHUTDOWN:
310 		break;
311 
312 	default:
313 		error = EOPNOTSUPP;
314 		break;
315 
316 	}
317 	return (error);
318 }
319 
320 static int
321 systrace_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
322 {
323 	return (0);
324 }
325 
326 SYSINIT(systrace_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_load, NULL);
327 SYSUNINIT(systrace_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_unload, NULL);
328 
329 #ifdef LINUX_SYSTRACE
330 DEV_MODULE(linsystrace, systrace_modevent, NULL);
331 MODULE_VERSION(linsystrace, 1);
332 MODULE_DEPEND(linsystrace, linux, 1, 1, 1);
333 MODULE_DEPEND(linsystrace, systrace, 1, 1, 1);
334 MODULE_DEPEND(linsystrace, dtrace, 1, 1, 1);
335 MODULE_DEPEND(linsystrace, opensolaris, 1, 1, 1);
336 #else
337 DEV_MODULE(systrace, systrace_modevent, NULL);
338 MODULE_VERSION(systrace, 1);
339 MODULE_DEPEND(systrace, dtrace, 1, 1, 1);
340 MODULE_DEPEND(systrace, opensolaris, 1, 1, 1);
341 #endif
342