1 /*        $NetBSD: systrace.c,v 1.12 2018/06/29 11:33:46 kamil 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  */
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 /* __FBSDID("$FreeBSD: head/sys/cddl/dev/systrace/systrace.c 306220 2016-09-22 23:22:53Z markj $"); */
34 
35 #include <sys/proc.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/conf.h>
39 #include <sys/cpuvar.h>
40 #include <sys/fcntl.h>
41 #include <sys/filio.h>
42 #include <sys/kernel.h>
43 #include <sys/kmem.h>
44 #include <sys/kthread.h>
45 #include <sys/syslimits.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/syscallargs.h>
55 #include <sys/uio.h>
56 #include <sys/unistd.h>
57 
58 #include <sys/dtrace.h>
59 #include "dtrace_cddl.h"
60 
61 #include "emultrace.h"
62 
63 #define   CONCAT(x,y)         __CONCAT(x,y)
64 #define   STRING(s) __STRING(s)
65 
66 #ifdef __FreeBSD__
67 #ifdef LINUX_SYSTRACE
68 #if defined(__amd64__)
69 #include <amd64/linux/linux.h>
70 #include <amd64/linux/linux_proto.h>
71 #include <amd64/linux/linux_syscalls.c>
72 #include <amd64/linux/linux_systrace_args.c>
73 #elif defined(__i386__)
74 #include <i386/linux/linux.h>
75 #include <i386/linux/linux_proto.h>
76 #include <i386/linux/linux_syscalls.c>
77 #include <i386/linux/linux_systrace_args.c>
78 #else
79 #error Only i386 and amd64 are supported.
80 #endif
81 #define   MODNAME             "linux"
82 extern struct sysent linux_sysent[];
83 #define   MAXSYSCALL          LINUX_SYS_MAXSYSCALL
84 #define   SYSCALLNAMES        linux_syscallnames
85 #define   SYSENT              linux_sysent
86 #elif defined(LINUX32_SYSTRACE)
87 #if defined(__amd64__)
88 #include <amd64/linux32/linux.h>
89 #include <amd64/linux32/linux32_proto.h>
90 #include <amd64/linux32/linux32_syscalls.c>
91 #include <amd64/linux32/linux32_systrace_args.c>
92 #else
93 #error Only amd64 is supported.
94 #endif
95 #define   MODNAME             "linux32"
96 extern struct sysent linux32_sysent[];
97 #define   MAXSYSCALL          LINUX32_SYS_MAXSYSCALL
98 #define   SYSCALLNAMES        linux32_syscallnames
99 #define   SYSENT              linux32_sysent
100 #elif defined(FREEBSD32_SYSTRACE)
101 /*
102  * The syscall arguments are processed into a DTrace argument array
103  * using a generated function. See sys/kern/makesyscalls.sh.
104  */
105 #include <compat/freebsd32/freebsd32_proto.h>
106 #include <compat/freebsd32/freebsd32_util.h>
107 #include <compat/freebsd32/freebsd32_syscall.h>
108 #include <compat/freebsd32/freebsd32_systrace_args.c>
109 extern const char *freebsd32_syscallnames[];
110 #define   MODNAME             "freebsd32"
111 #define   MAXSYSCALL          FREEBSD32_SYS_MAXSYSCALL
112 #define   SYSCALLNAMES        freebsd32_syscallnames
113 #define   SYSENT              freebsd32_sysent
114 #else
115 /*
116  * The syscall arguments are processed into a DTrace argument array
117  * using a generated function. See sys/kern/makesyscalls.sh.
118  */
119 #include <sys/syscall.h>
120 #include <kern/systrace_args.c>
121 #define   MODNAME             "freebsd"
122 #define   MAXSYSCALL          SYS_MAXSYSCALL
123 #define   SYSCALLNAMES        syscallnames
124 #define   SYSENT              sysent
125 #define   NATIVE_ABI
126 #endif
127 
128 #define   PROVNAME  "syscall"
129 #define   DEVNAME           "dtrace/systrace/" MODNAME
130 #endif /* __FreeBSD__ */
131 
132 #ifdef __NetBSD__
133 #include <sys/syscallargs.h>
134 
135 #ifndef NATIVE
136 extern const char   * const CONCAT(emulname,_syscallnames)[];
137 extern const char   * const CONCAT(alt,CONCAT(emulname,_syscallnames))[];
138 extern    struct sysent       CONCAT(emulname,_sysent)[];
139 #define   MODNAME             CONCAT(dtrace_syscall_,emulname)
140 #define   MODDEP              "dtrace_syscall,compat_" STRING(emulname)
141 #define   MAXSYSCALL          CONCAT(EMULNAME,_SYS_MAXSYSCALL)
142 #define   SYSCALLNAMES        CONCAT(emulname,_syscallnames)
143 #define   ALTSYSCALLNAMES     CONCAT(alt,CONCAT(emulname,_syscallnames))
144 #define   SYSENT              CONCAT(emulname,_sysent)
145 #define   PROVNAME  STRING(emulname) "_syscall"
146 #else
147 extern const char   * const syscallnames[];
148 extern const char   * const altsyscallnames[];
149 #define   MODNAME             dtrace_syscall
150 #define   MODDEP              "dtrace"
151 #define   MAXSYSCALL          SYS_MAXSYSCALL
152 #define   SYSCALLNAMES        syscallnames
153 #define   ALTSYSCALLNAMES     altsyscallnames
154 #define   SYSENT              sysent
155 #define   PROVNAME  "syscall"
156 #endif
157 
158 #define   MODCMD              CONCAT(MODNAME,_modcmd)
159 #define EMUL                  CONCAT(emul_,emulname)
160 extern struct emul  EMUL;
161 #define curthread   curlwp
162 #endif /* __NetBSD__ */
163 
164 #define   SYSTRACE_ARTIFICIAL_FRAMES    1
165 
166 #define   SYSTRACE_SHIFT                          16
167 #define   SYSTRACE_ISENTRY(x)           ((int)(x) >> SYSTRACE_SHIFT)
168 #define   SYSTRACE_SYSNUM(x)            ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1))
169 #define   SYSTRACE_ENTRY(id)            ((1 << SYSTRACE_SHIFT) | (id))
170 #define   SYSTRACE_RETURN(id)           (id)
171 
172 #if ((1 << SYSTRACE_SHIFT) <= MAXSYSCALL)
173 #error 1 << SYSTRACE_SHIFT must exceed number of system calls
174 #endif
175 
176 static int          systrace_unload(void);
177 static void         systrace_getargdesc(void *, dtrace_id_t, void *, dtrace_argdesc_t *);
178 static uint64_t     systrace_getargval(void *, dtrace_id_t, void *, int, int);
179 static void         systrace_provide(void *, dtrace_probedesc_t *);
180 static void         systrace_destroy(void *, dtrace_id_t, void *);
181 static int          systrace_enable(void *, dtrace_id_t, void *);
182 static void         systrace_disable(void *, dtrace_id_t, void *);
183 static void         systrace_load(void *);
184 
185 #ifdef __FreeBSD__
186 static union {
187           const char          **p_constnames;
188           char                **pp_syscallnames;
189 } uglyhack = { SYSCALLNAMES };
190 #endif
191 
192 static dtrace_pattr_t systrace_attr = {
193 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
194 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
195 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
196 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
197 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
198 };
199 
200 static dtrace_pops_t systrace_pops = {
201           systrace_provide,
202           NULL,
203           systrace_enable,
204           systrace_disable,
205           NULL,
206           NULL,
207           systrace_getargdesc,
208           systrace_getargval,
209           NULL,
210           systrace_destroy
211 };
212 
213 static dtrace_provider_id_t   systrace_id;
214 
215 /*
216  * Probe callback function.
217  *
218  * Note: This function is called for _all_ syscalls, regardless of which sysent
219  *       array the syscall comes from. It could be a standard syscall or a
220  *       compat syscall from something like Linux.
221  */
222 #ifdef __FreeBSD__
223 #ifdef NATIVE_ABI
224 static void
systrace_probe(struct syscall_args * sa,enum systrace_probe_t type,int retval)225 systrace_probe(struct syscall_args *sa, enum systrace_probe_t type, int retval)
226 {
227           uint64_t uargs[nitems(sa->args)];
228           dtrace_id_t id;
229           int n_args, sysnum;
230 
231           sysnum = sa->code;
232           memset(uargs, 0, sizeof(uargs));
233 
234           if (type == SYSTRACE_ENTRY) {
235                     if ((id = sa->callp->sy_entry) == DTRACE_IDNONE)
236                               return;
237 
238                     if (sa->callp->sy_systrace_args_func != NULL)
239                               /*
240                                * Convert the syscall parameters using the registered
241                                * function.
242                                */
243                               (*sa->callp->sy_systrace_args_func)(sysnum, sa->args,
244                                   uargs, &n_args);
245                     else
246                               /*
247                                * Use the built-in system call argument conversion
248                                * function to translate the syscall structure fields
249                                * into the array of 64-bit values that DTrace expects.
250                                */
251                               systrace_args(sysnum, sa->args, uargs, &n_args);
252                     /*
253                      * Save probe arguments now so that we can retrieve them if
254                      * the getargval method is called from further down the stack.
255                      */
256                     curthread->t_dtrace_systrace_args = uargs;
257           } else {
258                     if ((id = sa->callp->sy_return) == DTRACE_IDNONE)
259                               return;
260 
261                     curthread->t_dtrace_systrace_args = NULL;
262                     /* Set arg0 and arg1 as the return value of this syscall. */
263                     uargs[0] = uargs[1] = retval;
264           }
265 
266           /* Process the probe using the converted argments. */
267           dtrace_probe(id, uargs[0], uargs[1], uargs[2], uargs[3], uargs[4]);
268 }
269 #endif /* NATIVE_ABI */
270 #endif /* __FreeBSD__ */
271 
272 #ifdef __NetBSD__
273 static void
systrace_probe(uint32_t id,register_t sysnum,const struct sysent * se,const void * params,const register_t * ret,int error)274 systrace_probe(uint32_t id, register_t sysnum, const struct sysent *se,
275     const void *params, const register_t *ret, int error)
276 {
277           size_t              n_args    = 0;
278           uintptr_t uargs[SYS_MAXSYSARGS + 3];
279 
280           memset(uargs, 0, sizeof(uargs));
281           if (ret == NULL) {
282                     /* entry syscall, convert params */
283                     systrace_args(sysnum, params, uargs, &n_args);
284           } else {
285                     /* return syscall, set values and params: */
286                     uargs[0] = ret[0];
287                     uargs[1] = ret[1];
288                     uargs[2] = error;
289                     systrace_args(sysnum, params, uargs + 3, &n_args);
290           }
291           /* Process the probe using the converted argments. */
292           /* XXX: fix for more arguments! */
293           dtrace_probe(id, uargs[0], uargs[1], uargs[2], uargs[3], uargs[4]);
294 }
295 #endif
296 
297 static void
systrace_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)298 systrace_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
299 {
300           int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
301 
302           if (SYSTRACE_ISENTRY((uintptr_t)parg))
303                     systrace_entry_setargdesc(sysnum, desc->dtargd_ndx,
304                         desc->dtargd_native, sizeof(desc->dtargd_native));
305           else
306                     systrace_return_setargdesc(sysnum, desc->dtargd_ndx,
307                         desc->dtargd_native, sizeof(desc->dtargd_native));
308 
309           if (desc->dtargd_native[0] == '\0')
310                     desc->dtargd_ndx = DTRACE_ARGNONE;
311 }
312 
313 static uint64_t
systrace_getargval(void * arg,dtrace_id_t id,void * parg,int argno,int aframes)314 systrace_getargval(void *arg, dtrace_id_t id, void *parg, int argno, int aframes)
315 {
316           uint64_t *uargs;
317 
318           uargs = curthread->t_dtrace_systrace_args;
319           if (uargs == NULL)
320                     /* This is a return probe. */
321                     return (0);
322 #ifdef __FreeBSD__
323           if (argno >= nitems(((struct syscall_args *)NULL)->args))
324                     return (0);
325 #endif
326 #ifdef __NetBSD__
327           if (argno >= SYS_MAXSYSARGS)
328                     return (0);
329 #endif
330 
331           return (uargs[argno]);
332 }
333 
334 static void
systrace_provide(void * arg,dtrace_probedesc_t * desc)335 systrace_provide(void *arg, dtrace_probedesc_t *desc)
336 {
337           int i;
338 
339           if (desc != NULL)
340                     return;
341 
342           for (i = 0; i < MAXSYSCALL; i++) {
343 #ifdef __FreeBSD__
344                     if (dtrace_probe_lookup(systrace_id, MODNAME,
345                         uglyhack.pp_syscallnames[i], "entry") != 0)
346                               continue;
347 
348                     (void)dtrace_probe_create(systrace_id, MODNAME,
349                         uglyhack.pp_syscallnames[i], "entry",
350                         SYSTRACE_ARTIFICIAL_FRAMES,
351                         (void *)((uintptr_t)SYSTRACE_ENTRY(i)));
352                     (void)dtrace_probe_create(systrace_id, MODNAME,
353                         uglyhack.pp_syscallnames[i], "return",
354                         SYSTRACE_ARTIFICIAL_FRAMES,
355                         (void *)((uintptr_t)SYSTRACE_RETURN(i)));
356 #else
357                     const char *name = ALTSYSCALLNAMES[i] ? ALTSYSCALLNAMES[i] :
358                         SYSCALLNAMES[i];
359                     if (dtrace_probe_lookup(systrace_id, NULL, name, "entry") != 0)
360                               continue;
361 
362                     (void) dtrace_probe_create(systrace_id, NULL,
363                         name, "entry", SYSTRACE_ARTIFICIAL_FRAMES,
364                         (void *)(intptr_t)SYSTRACE_ENTRY(i));
365                     (void) dtrace_probe_create(systrace_id, NULL,
366                         name, "return", SYSTRACE_ARTIFICIAL_FRAMES,
367                         (void *)(intptr_t)SYSTRACE_RETURN(i));
368 #endif
369           }
370 }
371 
372 static void
systrace_destroy(void * arg,dtrace_id_t id,void * parg)373 systrace_destroy(void *arg, dtrace_id_t id, void *parg)
374 {
375 #ifdef DEBUG
376           int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
377 
378           /*
379            * There's nothing to do here but assert that we have actually been
380            * disabled.
381            */
382           if (SYSTRACE_ISENTRY((uintptr_t)parg)) {
383                     ASSERT(sysent[sysnum].sy_entry == 0);
384           } else {
385                     ASSERT(sysent[sysnum].sy_return == 0);
386           }
387 #endif
388 }
389 
390 static int
systrace_enable(void * arg,dtrace_id_t id,void * parg)391 systrace_enable(void *arg, dtrace_id_t id, void *parg)
392 {
393           int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
394 
395 #ifdef __FreeBSD__
396           if (SYSENT[sysnum].sy_systrace_args_func == NULL)
397                     SYSENT[sysnum].sy_systrace_args_func = systrace_args;
398 #endif
399 
400           if (SYSTRACE_ISENTRY((uintptr_t)parg))
401                     SYSENT[sysnum].sy_entry = id;
402           else
403                     SYSENT[sysnum].sy_return = id;
404 
405           return 0;
406 }
407 
408 static void
systrace_disable(void * arg,dtrace_id_t id,void * parg)409 systrace_disable(void *arg, dtrace_id_t id, void *parg)
410 {
411           int sysnum = SYSTRACE_SYSNUM((uintptr_t)parg);
412 
413           SYSENT[sysnum].sy_entry = 0;
414           SYSENT[sysnum].sy_return = 0;
415 }
416 
417 static void
systrace_load(void * dummy)418 systrace_load(void *dummy)
419 {
420           if (dtrace_register(PROVNAME, &systrace_attr, DTRACE_PRIV_USER, NULL,
421               &systrace_pops, NULL, &systrace_id) != 0)
422                     return;
423 
424 #ifdef NATIVE_ABI
425           systrace_probe_func = systrace_probe;
426 #endif
427 #ifdef __NetBSD__
428           EMUL.e_dtrace_syscall = systrace_probe;
429 #endif
430 }
431 
432 
433 static int
systrace_unload()434 systrace_unload()
435 {
436           int error;
437 
438 #ifdef NATIVE_ABI
439           systrace_probe_func = NULL;
440 #endif
441 #ifdef __NetBSD__
442           EMUL.e_dtrace_syscall = NULL;
443 #endif
444 
445           if ((error = dtrace_unregister(systrace_id)) != 0)
446                     return (error);
447 
448           return error;
449 }
450 
451 #ifdef __FreeBSD__
452 static int
systrace_modevent(module_t mod __unused,int type,void * data __unused)453 systrace_modevent(module_t mod __unused, int type, void *data __unused)
454 {
455           int error;
456 
457           error = 0;
458           switch (type) {
459           case MOD_LOAD:
460                     break;
461 
462           case MOD_UNLOAD:
463                     break;
464 
465           case MOD_SHUTDOWN:
466                     break;
467 
468           default:
469                     error = EOPNOTSUPP;
470                     break;
471 
472           }
473           return (error);
474 }
475 
476 SYSINIT(systrace_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY,
477     systrace_load, NULL);
478 SYSUNINIT(systrace_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY,
479     systrace_unload, NULL);
480 
481 #ifdef LINUX_SYSTRACE
482 DEV_MODULE(systrace_linux, systrace_modevent, NULL);
483 MODULE_VERSION(systrace_linux, 1);
484 #ifdef __amd64__
485 MODULE_DEPEND(systrace_linux, linux64, 1, 1, 1);
486 #else
487 MODULE_DEPEND(systrace_linux, linux, 1, 1, 1);
488 #endif
489 MODULE_DEPEND(systrace_linux, dtrace, 1, 1, 1);
490 MODULE_DEPEND(systrace_linux, opensolaris, 1, 1, 1);
491 #elif defined(LINUX32_SYSTRACE)
492 DEV_MODULE(systrace_linux32, systrace_modevent, NULL);
493 MODULE_VERSION(systrace_linux32, 1);
494 MODULE_DEPEND(systrace_linux32, linux, 1, 1, 1);
495 MODULE_DEPEND(systrace_linux32, dtrace, 1, 1, 1);
496 MODULE_DEPEND(systrace_linux32, opensolaris, 1, 1, 1);
497 #elif defined(FREEBSD32_SYSTRACE)
498 DEV_MODULE(systrace_freebsd32, systrace_modevent, NULL);
499 MODULE_VERSION(systrace_freebsd32, 1);
500 MODULE_DEPEND(systrace_freebsd32, dtrace, 1, 1, 1);
501 MODULE_DEPEND(systrace_freebsd32, opensolaris, 1, 1, 1);
502 #else
503 DEV_MODULE(systrace, systrace_modevent, NULL);
504 MODULE_VERSION(systrace, 1);
505 MODULE_DEPEND(systrace, dtrace, 1, 1, 1);
506 MODULE_DEPEND(systrace, opensolaris, 1, 1, 1);
507 #endif
508 #endif /* __FreeBSD__ */
509 
510 #ifdef __NetBSD__
511 
512 static int
MODCMD(modcmd_t cmd,void * data)513 MODCMD(modcmd_t cmd, void *data)
514 {
515           switch (cmd) {
516           case MODULE_CMD_INIT:
517                     systrace_load(NULL);
518                     return 0;
519 
520           case MODULE_CMD_FINI:
521                     return systrace_unload();
522 
523           case MODULE_CMD_AUTOUNLOAD:
524                     return EBUSY;
525 
526           default:
527                     return ENOTTY;
528           }
529 }
530 
531 MODULE(MODULE_CLASS_MISC, MODNAME, MODDEP)
532 
533 #endif /* __NetBSD__ */
534