xref: /netbsd-src/external/cddl/osnet/dev/systrace/systrace.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: systrace.c,v 1.4 2014/01/12 17:49:30 riz Exp $	*/
2 
3 /*
4  * CDDL HEADER START
5  *
6  * The contents of this file are subject to the terms of the
7  * Common Development and Distribution License (the "License").
8  * You may not use this file except in compliance with the License.
9  *
10  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11  * or http://www.opensolaris.org/os/licensing.
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  *
23  * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
24  *
25  * $FreeBSD: src/sys/cddl/dev/systrace/systrace.c,v 1.2.2.1 2009/08/03 08:13:06 kensmith Exp $
26  *
27  */
28 
29 /*
30  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
31  * Use is subject to license terms.
32  */
33 
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/cpuvar.h>
39 #include <sys/fcntl.h>
40 #include <sys/filio.h>
41 #ifdef __FreeBSD__
42 #include <sys/kdb.h>
43 #endif
44 #include <sys/kernel.h>
45 #include <sys/kmem.h>
46 #include <sys/kthread.h>
47 #include <sys/limits.h>
48 #include <sys/linker.h>
49 #include <sys/lock.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <sys/poll.h>
54 #include <sys/proc.h>
55 #include <sys/selinfo.h>
56 #ifdef __FreeBSD__
57 #include <sys/smp.h>
58 #include <sys/sysproto.h>
59 #include <sys/sysent.h>
60 #endif
61 #include <sys/syscallargs.h>
62 #include <sys/uio.h>
63 #include <sys/unistd.h>
64 
65 #include <sys/dtrace.h>
66 
67 #ifdef LINUX_SYSTRACE
68 #include <linux.h>
69 #include <linux_syscall.h>
70 #include <linux_proto.h>
71 #include <linux_syscallnames.c>
72 #include <linux_systrace.c>
73 extern struct sysent linux_sysent[];
74 #define	DEVNAME		"dtrace/linsystrace"
75 #define	PROVNAME	"linsyscall"
76 #define	MAXSYSCALL	LINUX_SYS_MAXSYSCALL
77 #define	SYSCALLNAMES	linux_syscallnames
78 #define	SYSENT		linux_sysent
79 #else
80 /*
81  * The syscall arguments are processed into a DTrace argument array
82  * using a generated function. See sys/kern/makesyscalls.sh.
83  */
84 #include <sys/syscall.h>
85 #include <kern/systrace_args.c>
86 extern const char	* const syscallnames[];
87 #define	DEVNAME		"dtrace/systrace"
88 #define	PROVNAME	"syscall"
89 #define	MAXSYSCALL	SYS_MAXSYSCALL
90 #define	SYSCALLNAMES	syscallnames
91 #define	SYSENT		sysent
92 #endif
93 
94 #define	SYSTRACE_ARTIFICIAL_FRAMES	1
95 
96 #define	SYSTRACE_SHIFT			16
97 #define	SYSTRACE_ISENTRY(x)		((int)(x) >> SYSTRACE_SHIFT)
98 #define	SYSTRACE_SYSNUM(x)		((int)(x) & ((1 << SYSTRACE_SHIFT) - 1))
99 #define	SYSTRACE_ENTRY(id)		((1 << SYSTRACE_SHIFT) | (id))
100 #define	SYSTRACE_RETURN(id)		(id)
101 
102 #if ((1 << SYSTRACE_SHIFT) <= MAXSYSCALL)
103 #error 1 << SYSTRACE_SHIFT must exceed number of system calls
104 #endif
105 
106 #ifdef __FreeBSD__
107 static d_open_t	systrace_open;
108 #endif
109 static int	systrace_unload(void);
110 static void	systrace_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
111 static void	systrace_provide(void *, const dtrace_probedesc_t *);
112 static void	systrace_destroy(void *, dtrace_id_t, void *);
113 static int	systrace_enable(void *, dtrace_id_t, void *);
114 static void	systrace_disable(void *, dtrace_id_t, void *);
115 static void	systrace_load(void *);
116 
117 #ifdef __FreeBSD__
118 static struct cdevsw systrace_cdevsw = {
119 	.d_version	= D_VERSION,
120 	.d_open		= systrace_open,
121 #ifdef LINUX_SYSTRACE
122 	.d_name		= "linsystrace",
123 #else
124 	.d_name		= "systrace",
125 #endif
126 };
127 #endif
128 
129 static union	{
130 	const char	* const *p_constnames;
131 	char		**pp_syscallnames;
132 } uglyhack = { SYSCALLNAMES };
133 
134 static dtrace_pattr_t systrace_attr = {
135 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
136 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
137 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
138 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
139 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
140 };
141 
142 static dtrace_pops_t systrace_pops = {
143 	systrace_provide,
144 	NULL,
145 	systrace_enable,
146 	systrace_disable,
147 	NULL,
148 	NULL,
149 	systrace_getargdesc,
150 	NULL,
151 	NULL,
152 	systrace_destroy
153 };
154 
155 #ifdef __FreeBSD__
156 static struct cdev		*systrace_cdev;
157 #endif
158 static dtrace_provider_id_t	systrace_id;
159 
160 #if !defined(LINUX_SYSTRACE)
161 /*
162  * Probe callback function.
163  *
164  * Note: This function is called for _all_ syscalls, regardless of which sysent
165  *       array the syscall comes from. It could be a standard syscall or a
166  *       compat syscall from something like Linux.
167  */
168 static void
169 systrace_probe(u_int32_t id, int sysnum, struct sysent *se, void *params)
170 {
171 	int		n_args	= 0;
172 	union systrace_probe_args_un	uargs[SYS_MAXSYSARGS];
173 
174 	/*
175 	 * Check if this syscall has an argument conversion function
176 	 * registered.
177 	 */
178 	if (se->sy_systrace_args_func != NULL)
179 		/*
180 		 * Convert the syscall parameters using the registered
181 		 * function.
182 		 */
183 		(*se->sy_systrace_args_func)(sysnum, params, uargs, &n_args);
184 	else
185 		/*
186 		 * Use the built-in system call argument conversion
187 		 * function to translate the syscall structure fields
188 		 * into the array of 64-bit values that DTrace
189 		 * expects.
190 		 */
191 		systrace_args(sysnum, params, uargs, &n_args);
192 
193 	/* Process the probe using the converted argments. */
194 	dtrace_probe(id, uargs[0].u, uargs[1].u, uargs[2].u, uargs[3].u,
195 		uargs[4].u);
196 }
197 #endif
198 
199 static void
200 systrace_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
201 {
202 	int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
203 
204 	systrace_setargdesc(sysnum, desc->dtargd_ndx, desc->dtargd_native,
205 	    sizeof(desc->dtargd_native));
206 
207 	if (desc->dtargd_native[0] == '\0')
208 		desc->dtargd_ndx = DTRACE_ARGNONE;
209 
210 	return;
211 }
212 
213 static void
214 systrace_provide(void *arg, const dtrace_probedesc_t *desc)
215 {
216 	int i;
217 
218 	if (desc != NULL)
219 		return;
220 
221 	for (i = 0; i < MAXSYSCALL; i++) {
222 		if (dtrace_probe_lookup(systrace_id, NULL,
223 		    uglyhack.pp_syscallnames[i], "entry") != 0)
224 			continue;
225 
226 		(void) dtrace_probe_create(systrace_id, NULL, uglyhack.pp_syscallnames[i],
227 		    "entry", SYSTRACE_ARTIFICIAL_FRAMES,
228 		    (void *)((uintptr_t)SYSTRACE_ENTRY(i)));
229 		(void) dtrace_probe_create(systrace_id, NULL, uglyhack.pp_syscallnames[i],
230 		    "return", SYSTRACE_ARTIFICIAL_FRAMES,
231 		    (void *)((uintptr_t)SYSTRACE_RETURN(i)));
232 	}
233 }
234 
235 static void
236 systrace_destroy(void *arg, dtrace_id_t id, void *parg)
237 {
238 #ifdef DEBUG
239 	int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
240 
241 	/*
242 	 * There's nothing to do here but assert that we have actually been
243 	 * disabled.
244 	 */
245 	if (SYSTRACE_ISENTRY((uintptr_t)parg)) {
246 		ASSERT(sysent[sysnum].sy_entry == 0);
247 	} else {
248 		ASSERT(sysent[sysnum].sy_return == 0);
249 	}
250 #endif
251 }
252 
253 static int
254 systrace_enable(void *arg, dtrace_id_t id, void *parg)
255 {
256 	int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
257 
258 	if (SYSENT[sysnum].sy_systrace_args_func == NULL)
259 		SYSENT[sysnum].sy_systrace_args_func = systrace_args;
260 
261 	if (SYSTRACE_ISENTRY((uintptr_t)parg))
262 		SYSENT[sysnum].sy_entry = id;
263 	else
264 		SYSENT[sysnum].sy_return = id;
265 	return 0;
266 }
267 
268 static void
269 systrace_disable(void *arg, dtrace_id_t id, void *parg)
270 {
271 	int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
272 
273 	SYSENT[sysnum].sy_entry = 0;
274 	SYSENT[sysnum].sy_return = 0;
275 }
276 
277 static void
278 systrace_load(void *dummy)
279 {
280 #ifdef __FreeBSD__
281 	/* Create the /dev/dtrace/systrace entry. */
282 	systrace_cdev = make_dev(&systrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
283 	   DEVNAME);
284 #endif
285 
286 	if (dtrace_register(PROVNAME, &systrace_attr, DTRACE_PRIV_USER,
287 	    NULL, &systrace_pops, NULL, &systrace_id) != 0)
288 		return;
289 
290 #if !defined(LINUX_SYSTRACE)
291 	systrace_probe_func = (systrace_probe_func_t)systrace_probe;
292 #endif
293 }
294 
295 
296 static int
297 systrace_unload()
298 {
299 	int error = 0;
300 
301 	if ((error = dtrace_unregister(systrace_id)) != 0)
302 		return (error);
303 
304 #if !defined(LINUX_SYSTRACE)
305 	systrace_probe_func = NULL;
306 #endif
307 
308 #ifdef __FreeBSD__
309 	destroy_dev(systrace_cdev);
310 #endif
311 
312 	return (error);
313 }
314 
315 #ifdef __FreeBSD__
316 static int
317 systrace_modevent(module_t mod __unused, int type, void *data __unused)
318 {
319 	int error = 0;
320 
321 	switch (type) {
322 	case MOD_LOAD:
323 		break;
324 
325 	case MOD_UNLOAD:
326 		break;
327 
328 	case MOD_SHUTDOWN:
329 		break;
330 
331 	default:
332 		error = EOPNOTSUPP;
333 		break;
334 
335 	}
336 	return (error);
337 }
338 
339 static int
340 systrace_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused)
341 {
342 	return (0);
343 }
344 
345 SYSINIT(systrace_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_load, NULL);
346 SYSUNINIT(systrace_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, systrace_unload, NULL);
347 
348 #ifdef LINUX_SYSTRACE
349 DEV_MODULE(linsystrace, systrace_modevent, NULL);
350 MODULE_VERSION(linsystrace, 1);
351 MODULE_DEPEND(linsystrace, linux, 1, 1, 1);
352 MODULE_DEPEND(linsystrace, systrace, 1, 1, 1);
353 MODULE_DEPEND(linsystrace, dtrace, 1, 1, 1);
354 MODULE_DEPEND(linsystrace, opensolaris, 1, 1, 1);
355 #else
356 DEV_MODULE(systrace, systrace_modevent, NULL);
357 MODULE_VERSION(systrace, 1);
358 MODULE_DEPEND(systrace, dtrace, 1, 1, 1);
359 MODULE_DEPEND(systrace, opensolaris, 1, 1, 1);
360 #endif
361 #endif /* __FreeBSD__ */
362 
363 #ifdef __NetBSD__
364 
365 static int
366 systrace_modcmd(modcmd_t cmd, void *data)
367 {
368 	switch (cmd) {
369 	case MODULE_CMD_INIT:
370 		systrace_load(NULL);
371 		return 0;
372 
373 	case MODULE_CMD_FINI:
374 		systrace_unload();
375 		return 0;
376 
377 	default:
378 		return ENOTTY;
379 	}
380 }
381 
382 MODULE(MODULE_CLASS_MISC, systrace, "dtrace");
383 
384 #endif
385