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