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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
28 #include <kern/thread.h>
29 #include <mach/thread_status.h>
31 /* XXX All of these should really be derived from syscall_sw.h */
32 #if defined (__x86_64__)
33 #define SYSCALL_CLASS_SHIFT 24
34 #define SYSCALL_CLASS_MASK (0xFF << SYSCALL_CLASS_SHIFT)
35 #define SYSCALL_NUMBER_MASK (~SYSCALL_CLASS_MASK)
36 #define I386_SYSCALL_NUMBER_MASK (0xFFFF)
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/ioctl.h>
45 #include <sys/fcntl.h>
46 #include <sys/syscall.h>
47 #include <miscfs/devfs/devfs.h>
49 #include <sys/dtrace.h>
50 #include <sys/dtrace_impl.h>
51 #include <sys/systrace_args.h>
54 #include <sys/systm.h>
58 #include <machine/pal_routines.h>
60 #if defined (__x86_64__)
61 #define SYSTRACE_ARTIFICIAL_FRAMES 2
62 #define MACHTRACE_ARTIFICIAL_FRAMES 3
63 #elif defined(__arm__) || defined(__arm64__)
64 #define SYSTRACE_ARTIFICIAL_FRAMES 2
65 #define MACHTRACE_ARTIFICIAL_FRAMES 3
67 #error Unknown Architecture
70 #define SYSTRACE_NARGS (int)(sizeof(((uthread_t)NULL)->uu_arg) / sizeof(((uthread_t)NULL)->uu_arg[0]))
72 #include <sys/sysent.h>
73 #define sy_callc sy_call /* Map Solaris slot name to Darwin's */
74 #define NSYSCALL nsysent /* and is less than 500 or so */
76 extern const char *syscallnames
[];
78 #include <sys/dtrace_glue.h>
79 #define casptr dtrace_casptr
80 #define membar_enter dtrace_membar_producer
82 #define LOADABLE_SYSCALL(a) 0 /* Not pertinent to Darwin. */
83 #define LOADED_SYSCALL(a) 1 /* Not pertinent to Darwin. */
85 extern lck_attr_t
* dtrace_lck_attr
;
86 extern lck_grp_t
* dtrace_lck_grp
;
87 static lck_mtx_t dtrace_systrace_lock
; /* probe state lock */
89 systrace_sysent_t
*systrace_sysent
= NULL
;
90 void (*systrace_probe
)(dtrace_id_t
, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
92 static uint64_t systrace_getargval(void *, dtrace_id_t
, void *, int, int);
93 static void systrace_getargdesc(void *, dtrace_id_t
, void *, dtrace_argdesc_t
*);
96 systrace_stub(dtrace_id_t id
, uint64_t arg0
, uint64_t arg1
,
97 uint64_t arg2
, uint64_t arg3
, uint64_t arg4
)
99 #pragma unused(id,arg0,arg1,arg2,arg3,arg4)
103 dtrace_systrace_syscall(struct proc
*pp
, void *uap
, int *rv
)
105 unsigned short code
; /* The system call number */
107 systrace_sysent_t
*sy
;
110 syscall_arg_t
*ip
= (syscall_arg_t
*)uap
;
111 uint64_t uargs
[SYSTRACE_NARGS
] = {0};
113 #if defined (__x86_64__)
115 pal_register_cache_state(current_thread(), VALID
);
116 x86_saved_state_t
*tagged_regs
= (x86_saved_state_t
*)find_user_regs(current_thread());
118 if (is_saved_state64(tagged_regs
)) {
119 x86_saved_state64_t
*regs
= saved_state64(tagged_regs
);
120 code
= regs
->rax
& SYSCALL_NUMBER_MASK
;
122 * Check for indirect system call... system call number
129 code
= saved_state32(tagged_regs
)->eax
& I386_SYSCALL_NUMBER_MASK
;
132 vm_offset_t params
= (vm_offset_t
) (saved_state32(tagged_regs
)->uesp
+ sizeof(int));
133 code
= fuword(params
);
137 #elif defined(__arm__)
140 * On arm, syscall numbers depend on a flavor (indirect or not)
141 * and can be in either r0 or r12 (always u32)
144 /* See bsd/dev/arm/systemcalls.c:arm_get_syscall_number */
145 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
147 /* Check for indirect system call */
148 if (arm_regs
->r
[12] != 0) {
149 code
= arm_regs
->r
[12];
151 code
= arm_regs
->r
[0];
154 #elif defined(__arm64__)
157 * On arm64, syscall numbers depend on a flavor (indirect or not)
158 * ... and for u32 can be in either r0 or r12
159 * ... and for u64 can be in either x0 or x16
162 /* see bsd/dev/arm/systemcalls.c:arm_get_syscall_number */
163 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
165 if (is_saved_state32(arm_regs
)) {
166 /* Check for indirect system call */
167 if (saved_state32(arm_regs
)->r
[12] != 0) {
168 code
= saved_state32(arm_regs
)->r
[12];
170 code
= saved_state32(arm_regs
)->r
[0];
173 /* Check for indirect system call */
174 if (saved_state64(arm_regs
)->x
[ARM64_SYSCALL_CODE_REG_NUM
] != 0) {
175 code
= saved_state64(arm_regs
)->x
[ARM64_SYSCALL_CODE_REG_NUM
];
177 code
= saved_state64(arm_regs
)->x
[0];
182 #error Unknown Architecture
185 // Bounds "check" the value of code a la unix_syscall
186 sy
= (code
>= nsysent
) ? &systrace_sysent
[SYS_invalid
] : &systrace_sysent
[code
];
188 systrace_args(code
, ip
, uargs
);
190 if ((id
= sy
->stsy_entry
) != DTRACE_IDNONE
) {
191 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
193 uthread
->t_dtrace_syscall_args
= uargs
;
196 static_assert(SYSTRACE_NARGS
>= 5, "not enough system call arguments");
197 (*systrace_probe
)(id
, uargs
[0], uargs
[1], uargs
[2], uargs
[3], uargs
[4]);
200 uthread
->t_dtrace_syscall_args
= NULL
;
208 * APPLE NOTE: Not implemented.
209 * We want to explicitly allow DTrace consumers to stop a process
210 * before it actually executes the meat of the syscall.
212 p
= ttoproc(curthread
);
213 mutex_enter(&p
->p_lock
);
214 if (curthread
->t_dtrace_stop
&& !curthread
->t_lwp
->lwp_nostop
) {
215 curthread
->t_dtrace_stop
= 0;
216 stop(PR_REQUESTED
, 0);
218 mutex_exit(&p
->p_lock
);
221 rval
= (*sy
->stsy_underlying
)(pp
, uap
, rv
);
223 if ((id
= sy
->stsy_return
) != DTRACE_IDNONE
) {
224 uint64_t munged_rv0
, munged_rv1
;
225 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
228 uthread
->t_dtrace_errno
= rval
; /* Establish t_dtrace_errno now in case this enabling refers to it. */
231 * "Decode" rv for use in the call to dtrace_probe()
233 if (rval
== ERESTART
) {
234 munged_rv0
= -1LL; /* System call will be reissued in user mode. Make DTrace report a -1 return. */
236 } else if (rval
!= EJUSTRETURN
) {
238 munged_rv0
= -1LL; /* Mimic what libc will do. */
241 switch (sy
->stsy_return_type
) {
242 case _SYSCALL_RET_INT_T
:
246 case _SYSCALL_RET_UINT_T
:
247 munged_rv0
= ((u_int
)rv
[0]);
248 munged_rv1
= ((u_int
)rv
[1]);
250 case _SYSCALL_RET_OFF_T
:
251 case _SYSCALL_RET_UINT64_T
:
252 munged_rv0
= *(u_int64_t
*)rv
;
255 case _SYSCALL_RET_ADDR_T
:
256 case _SYSCALL_RET_SIZE_T
:
257 case _SYSCALL_RET_SSIZE_T
:
258 munged_rv0
= *(user_addr_t
*)rv
;
261 case _SYSCALL_RET_NONE
:
277 * <http://mail.opensolaris.org/pipermail/dtrace-discuss/2007-January/003276.html> says:
279 * "This is a bit of an historical artifact. At first, the syscall provider just
280 * had its return value in arg0, and the fbt and pid providers had their return
281 * values in arg1 (so that we could use arg0 for the offset of the return site).
283 * We inevitably started writing scripts where we wanted to see the return
284 * values from probes in all three providers, and we made this script easier
285 * to write by replicating the syscall return values in arg1 to match fbt and
286 * pid. We debated briefly about removing the return value from arg0, but
287 * decided that it would be less confusing to have the same data in two places
288 * than to have some non-helpful, non-intuitive value in arg0.
290 * This change was made 4/23/2003 according to the DTrace project's putback log."
292 (*systrace_probe
)(id
, munged_rv0
, munged_rv0
, munged_rv1
, (uint64_t)rval
, 0);
299 dtrace_systrace_syscall_return(unsigned short code
, int rval
, int *rv
)
301 systrace_sysent_t
*sy
;
304 // Bounds "check" the value of code a la unix_syscall_return
305 sy
= (code
>= nsysent
) ? &systrace_sysent
[SYS_invalid
] : &systrace_sysent
[code
];
307 if ((id
= sy
->stsy_return
) != DTRACE_IDNONE
) {
308 uint64_t munged_rv0
, munged_rv1
;
309 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
312 uthread
->t_dtrace_errno
= rval
; /* Establish t_dtrace_errno now in case this enabling refers to it. */
315 * "Decode" rv for use in the call to dtrace_probe()
317 if (rval
== ERESTART
) {
318 munged_rv0
= -1LL; /* System call will be reissued in user mode. Make DTrace report a -1 return. */
320 } else if (rval
!= EJUSTRETURN
) {
322 munged_rv0
= -1LL; /* Mimic what libc will do. */
325 switch (sy
->stsy_return_type
) {
326 case _SYSCALL_RET_INT_T
:
330 case _SYSCALL_RET_UINT_T
:
331 munged_rv0
= ((u_int
)rv
[0]);
332 munged_rv1
= ((u_int
)rv
[1]);
334 case _SYSCALL_RET_OFF_T
:
335 case _SYSCALL_RET_UINT64_T
:
336 munged_rv0
= *(u_int64_t
*)rv
;
339 case _SYSCALL_RET_ADDR_T
:
340 case _SYSCALL_RET_SIZE_T
:
341 case _SYSCALL_RET_SSIZE_T
:
342 munged_rv0
= *(user_addr_t
*)rv
;
345 case _SYSCALL_RET_NONE
:
360 (*systrace_probe
)(id
, munged_rv0
, munged_rv0
, munged_rv1
, (uint64_t)rval
, 0);
364 #define SYSTRACE_SHIFT 16
365 #define SYSTRACE_ISENTRY(x) ((int)(x) >> SYSTRACE_SHIFT)
366 #define SYSTRACE_SYSNUM(x) ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1))
367 #define SYSTRACE_ENTRY(id) ((1 << SYSTRACE_SHIFT) | (id))
368 #define SYSTRACE_RETURN(id) (id)
370 #if ((1 << SYSTRACE_SHIFT) <= NSYSCALL)
371 #error 1 << SYSTRACE_SHIFT must exceed number of system calls
374 static dtrace_provider_id_t systrace_id
;
377 * APPLE NOTE: Avoid name clash with Darwin automagic conf symbol.
378 * See balanced undef below.
380 #define systrace_init _systrace_init
383 systrace_init(const struct sysent
*actual
, systrace_sysent_t
**interposed
)
385 systrace_sysent_t
*ssysent
= *interposed
; /* Avoid sysent shadow warning
386 * from bsd/sys/sysent.h */
389 if (ssysent
== NULL
) {
390 *interposed
= ssysent
= kmem_zalloc(sizeof(systrace_sysent_t
) *
394 for (i
= 0; i
< NSYSCALL
; i
++) {
395 const struct sysent
*a
= &actual
[i
];
396 systrace_sysent_t
*s
= &ssysent
[i
];
398 if (LOADABLE_SYSCALL(a
) && !LOADED_SYSCALL(a
)) {
402 if (a
->sy_callc
== dtrace_systrace_syscall
) {
406 s
->stsy_underlying
= a
->sy_callc
;
407 s
->stsy_return_type
= a
->sy_return_type
;
409 lck_mtx_init(&dtrace_systrace_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
415 systrace_provide(void *arg
, const dtrace_probedesc_t
*desc
)
417 #pragma unused(arg) /* __APPLE__ */
424 systrace_init(sysent
, &systrace_sysent
);
426 for (i
= 0; i
< NSYSCALL
; i
++) {
427 if (systrace_sysent
[i
].stsy_underlying
== NULL
) {
431 if (dtrace_probe_lookup(systrace_id
, NULL
,
432 syscallnames
[i
], "entry") != 0) {
436 (void) dtrace_probe_create(systrace_id
, NULL
, syscallnames
[i
],
437 "entry", SYSTRACE_ARTIFICIAL_FRAMES
,
438 (void *)((uintptr_t)SYSTRACE_ENTRY(i
)));
439 (void) dtrace_probe_create(systrace_id
, NULL
, syscallnames
[i
],
440 "return", SYSTRACE_ARTIFICIAL_FRAMES
,
441 (void *)((uintptr_t)SYSTRACE_RETURN(i
)));
443 systrace_sysent
[i
].stsy_entry
= DTRACE_IDNONE
;
444 systrace_sysent
[i
].stsy_return
= DTRACE_IDNONE
;
451 systrace_destroy(void *arg
, dtrace_id_t id
, void *parg
)
453 #pragma unused(arg,id) /* __APPLE__ */
455 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
457 #pragma unused(sysnum) /* __APPLE__ */
459 * There's nothing to do here but assert that we have actually been
462 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
463 ASSERT(systrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
);
465 ASSERT(systrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
471 systrace_enable(void *arg
, dtrace_id_t id
, void *parg
)
473 #pragma unused(arg) /* __APPLE__ */
475 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
476 int enabled
= (systrace_sysent
[sysnum
].stsy_entry
!= DTRACE_IDNONE
||
477 systrace_sysent
[sysnum
].stsy_return
!= DTRACE_IDNONE
);
479 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
480 systrace_sysent
[sysnum
].stsy_entry
= id
;
482 systrace_sysent
[sysnum
].stsy_return
= id
;
486 ASSERT(sysent
[sysnum
].sy_callc
== dtrace_systrace_syscall
);
490 lck_mtx_lock(&dtrace_systrace_lock
);
491 if (sysent
[sysnum
].sy_callc
== systrace_sysent
[sysnum
].stsy_underlying
) {
492 vm_offset_t dss
= ptrauth_nop_cast(vm_offset_t
, &dtrace_systrace_syscall
);
493 ml_nofault_copy((vm_offset_t
)&dss
, (vm_offset_t
)&sysent
[sysnum
].sy_callc
, sizeof(vm_offset_t
));
495 lck_mtx_unlock(&dtrace_systrace_lock
);
501 systrace_disable(void *arg
, dtrace_id_t id
, void *parg
)
503 #pragma unused(arg,id) /* __APPLE__ */
505 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
506 int disable
= (systrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
||
507 systrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
510 lck_mtx_lock(&dtrace_systrace_lock
);
511 if (sysent
[sysnum
].sy_callc
== dtrace_systrace_syscall
) {
512 ml_nofault_copy((vm_offset_t
)&systrace_sysent
[sysnum
].stsy_underlying
, (vm_offset_t
)&sysent
[sysnum
].sy_callc
, sizeof(systrace_sysent
[sysnum
].stsy_underlying
));
514 lck_mtx_unlock(&dtrace_systrace_lock
);
517 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
518 systrace_sysent
[sysnum
].stsy_entry
= DTRACE_IDNONE
;
520 systrace_sysent
[sysnum
].stsy_return
= DTRACE_IDNONE
;
524 static dtrace_pattr_t systrace_attr
= {
525 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
526 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
527 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
528 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
529 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
532 static dtrace_pops_t systrace_pops
= {
533 .dtps_provide
= systrace_provide
,
534 .dtps_provide_module
= NULL
,
535 .dtps_enable
= systrace_enable
,
536 .dtps_disable
= systrace_disable
,
537 .dtps_suspend
= NULL
,
539 .dtps_getargdesc
= systrace_getargdesc
,
540 .dtps_getargval
= systrace_getargval
,
541 .dtps_usermode
= NULL
,
542 .dtps_destroy
= systrace_destroy
546 systrace_attach(dev_info_t
*devi
)
548 systrace_probe
= (void*)&dtrace_probe
;
551 if (ddi_create_minor_node(devi
, "systrace", S_IFCHR
, 0,
552 DDI_PSEUDO
, 0) == DDI_FAILURE
||
553 dtrace_register("syscall", &systrace_attr
, DTRACE_PRIV_USER
, NULL
,
554 &systrace_pops
, NULL
, &systrace_id
) != 0) {
555 systrace_probe
= systrace_stub
;
556 ddi_remove_minor_node(devi
, NULL
);
565 * APPLE NOTE: systrace_detach not implemented
567 #if !defined(__APPLE__)
569 systrace_detach(dev_info_t
*devi
, ddi_detach_cmd_t cmd
)
580 if (dtrace_unregister(systrace_id
) != 0) {
584 ddi_remove_minor_node(devi
, NULL
);
585 systrace_probe
= systrace_stub
;
588 #endif /* __APPLE__ */
591 typedef kern_return_t (*mach_call_t
)(void *);
593 /* APPLE NOTE: From #include <kern/syscall_sw.h> which may be changed for 64 bit! */
594 typedef void mach_munge_t(void *);
597 int mach_trap_arg_count
;
598 kern_return_t (*mach_trap_function
)(void *);
599 #if defined(__arm64__) || defined(__x86_64__)
600 mach_munge_t
*mach_trap_arg_munge32
; /* system call arguments for 32-bit */
602 int mach_trap_u32_words
;
604 const char* mach_trap_name
;
605 #endif /* MACH_ASSERT */
608 extern const mach_trap_t mach_trap_table
[]; /* syscall_sw.h now declares this as const */
609 extern int mach_trap_count
;
611 extern const char *mach_syscall_name_table
[];
613 /* XXX From osfmk/i386/bsd_i386.c */
614 struct mach_call_args
{
627 #define NSYSCALL mach_trap_count
629 #if ((1 << SYSTRACE_SHIFT) <= NSYSCALL)
630 #error 1 << SYSTRACE_SHIFT must exceed number of Mach traps
633 typedef struct machtrace_sysent
{
634 dtrace_id_t stsy_entry
;
635 dtrace_id_t stsy_return
;
636 kern_return_t (*stsy_underlying
)(void *);
637 int32_t stsy_return_type
;
638 } machtrace_sysent_t
;
640 static machtrace_sysent_t
*machtrace_sysent
= NULL
;
642 void (*machtrace_probe
)(dtrace_id_t
, uint64_t, uint64_t,
643 uint64_t, uint64_t, uint64_t);
645 static uint64_t machtrace_getarg(void *, dtrace_id_t
, void *, int, int);
647 static dtrace_provider_id_t machtrace_id
;
650 dtrace_machtrace_syscall(struct mach_call_args
*args
)
652 int code
; /* The mach call number */
654 machtrace_sysent_t
*sy
;
660 syscall_arg_t
*ip
= (syscall_arg_t
*)args
;
661 mach_call_t mach_call
;
663 #if defined (__x86_64__)
665 pal_register_cache_state(current_thread(), VALID
);
666 x86_saved_state_t
*tagged_regs
= (x86_saved_state_t
*)find_user_regs(current_thread());
668 if (is_saved_state64(tagged_regs
)) {
669 code
= saved_state64(tagged_regs
)->rax
& SYSCALL_NUMBER_MASK
;
671 code
= -saved_state32(tagged_regs
)->eax
;
674 #elif defined(__arm__)
676 /* r12 has the machcall number, but it is -ve */
677 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
678 code
= (int)arm_regs
->r
[12];
679 ASSERT(code
< 0); /* Otherwise it would be a Unix syscall */
682 #elif defined(__arm64__)
684 /* From arm/thread_status.h:get_saved_state_svc_number */
685 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
686 if (is_saved_state32(arm_regs
)) {
687 code
= (int)saved_state32(arm_regs
)->r
[12];
689 code
= (int)saved_state64(arm_regs
)->x
[ARM64_SYSCALL_CODE_REG_NUM
];
692 /* From bsd/arm64.c:mach_syscall */
693 ASSERT(code
< 0); /* Otherwise it would be a Unix syscall */
697 #error Unknown Architecture
700 sy
= &machtrace_sysent
[code
];
702 if ((id
= sy
->stsy_entry
) != DTRACE_IDNONE
) {
703 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
706 uthread
->t_dtrace_syscall_args
= (void *)ip
;
709 (*machtrace_probe
)(id
, *ip
, *(ip
+ 1), *(ip
+ 2), *(ip
+ 3), *(ip
+ 4));
712 uthread
->t_dtrace_syscall_args
= (void *)0;
718 * APPLE NOTE: Not implemented.
719 * We want to explicitly allow DTrace consumers to stop a process
720 * before it actually executes the meat of the syscall.
722 p
= ttoproc(curthread
);
723 mutex_enter(&p
->p_lock
);
724 if (curthread
->t_dtrace_stop
&& !curthread
->t_lwp
->lwp_nostop
) {
725 curthread
->t_dtrace_stop
= 0;
726 stop(PR_REQUESTED
, 0);
728 mutex_exit(&p
->p_lock
);
731 mach_call
= (mach_call_t
)(*sy
->stsy_underlying
);
732 rval
= mach_call(args
);
734 if ((id
= sy
->stsy_return
) != DTRACE_IDNONE
) {
735 (*machtrace_probe
)(id
, (uint64_t)rval
, 0, 0, 0, 0);
742 machtrace_init(const mach_trap_t
*actual
, machtrace_sysent_t
**interposed
)
744 machtrace_sysent_t
*msysent
= *interposed
;
747 if (msysent
== NULL
) {
748 *interposed
= msysent
= kmem_zalloc(sizeof(machtrace_sysent_t
) *
752 for (i
= 0; i
< NSYSCALL
; i
++) {
753 const mach_trap_t
*a
= &actual
[i
];
754 machtrace_sysent_t
*s
= &msysent
[i
];
756 if (LOADABLE_SYSCALL(a
) && !LOADED_SYSCALL(a
)) {
760 if (a
->mach_trap_function
== (mach_call_t
)(dtrace_machtrace_syscall
)) {
764 s
->stsy_underlying
= a
->mach_trap_function
;
770 machtrace_provide(void *arg
, const dtrace_probedesc_t
*desc
)
772 #pragma unused(arg) /* __APPLE__ */
780 machtrace_init(mach_trap_table
, &machtrace_sysent
);
782 for (i
= 0; i
< NSYSCALL
; i
++) {
783 if (machtrace_sysent
[i
].stsy_underlying
== NULL
) {
787 if (dtrace_probe_lookup(machtrace_id
, NULL
,
788 mach_syscall_name_table
[i
], "entry") != 0) {
792 (void) dtrace_probe_create(machtrace_id
, NULL
, mach_syscall_name_table
[i
],
793 "entry", MACHTRACE_ARTIFICIAL_FRAMES
,
794 (void *)((uintptr_t)SYSTRACE_ENTRY(i
)));
795 (void) dtrace_probe_create(machtrace_id
, NULL
, mach_syscall_name_table
[i
],
796 "return", MACHTRACE_ARTIFICIAL_FRAMES
,
797 (void *)((uintptr_t)SYSTRACE_RETURN(i
)));
799 machtrace_sysent
[i
].stsy_entry
= DTRACE_IDNONE
;
800 machtrace_sysent
[i
].stsy_return
= DTRACE_IDNONE
;
806 machtrace_destroy(void *arg
, dtrace_id_t id
, void *parg
)
808 #pragma unused(arg,id) /* __APPLE__ */
809 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
811 #pragma unused(sysnum) /* __APPLE__ */
814 * There's nothing to do here but assert that we have actually been
817 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
818 ASSERT(machtrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
);
820 ASSERT(machtrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
826 machtrace_enable(void *arg
, dtrace_id_t id
, void *parg
)
828 #pragma unused(arg) /* __APPLE__ */
830 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
831 int enabled
= (machtrace_sysent
[sysnum
].stsy_entry
!= DTRACE_IDNONE
||
832 machtrace_sysent
[sysnum
].stsy_return
!= DTRACE_IDNONE
);
834 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
835 machtrace_sysent
[sysnum
].stsy_entry
= id
;
837 machtrace_sysent
[sysnum
].stsy_return
= id
;
841 ASSERT(mach_trap_table
[sysnum
].mach_trap_function
== (void *)dtrace_machtrace_syscall
);
845 lck_mtx_lock(&dtrace_systrace_lock
);
847 if (mach_trap_table
[sysnum
].mach_trap_function
== machtrace_sysent
[sysnum
].stsy_underlying
) {
848 vm_offset_t dss
= ptrauth_nop_cast(vm_offset_t
, &dtrace_machtrace_syscall
);
849 ml_nofault_copy((vm_offset_t
)&dss
, (vm_offset_t
)&mach_trap_table
[sysnum
].mach_trap_function
, sizeof(vm_offset_t
));
852 lck_mtx_unlock(&dtrace_systrace_lock
);
859 machtrace_disable(void *arg
, dtrace_id_t id
, void *parg
)
861 #pragma unused(arg,id) /* __APPLE__ */
863 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
864 int disable
= (machtrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
||
865 machtrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
868 lck_mtx_lock(&dtrace_systrace_lock
);
870 if (mach_trap_table
[sysnum
].mach_trap_function
== (mach_call_t
)dtrace_machtrace_syscall
) {
871 ml_nofault_copy((vm_offset_t
)&machtrace_sysent
[sysnum
].stsy_underlying
, (vm_offset_t
)&mach_trap_table
[sysnum
].mach_trap_function
, sizeof(vm_offset_t
));
873 lck_mtx_unlock(&dtrace_systrace_lock
);
876 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
877 machtrace_sysent
[sysnum
].stsy_entry
= DTRACE_IDNONE
;
879 machtrace_sysent
[sysnum
].stsy_return
= DTRACE_IDNONE
;
883 static dtrace_pattr_t machtrace_attr
= {
884 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
885 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
886 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
887 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
888 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
891 static dtrace_pops_t machtrace_pops
= {
892 .dtps_provide
= machtrace_provide
,
893 .dtps_provide_module
= NULL
,
894 .dtps_enable
= machtrace_enable
,
895 .dtps_disable
= machtrace_disable
,
896 .dtps_suspend
= NULL
,
898 .dtps_getargdesc
= NULL
,
899 .dtps_getargval
= machtrace_getarg
,
900 .dtps_usermode
= NULL
,
901 .dtps_destroy
= machtrace_destroy
905 machtrace_attach(dev_info_t
*devi
)
907 machtrace_probe
= dtrace_probe
;
910 if (ddi_create_minor_node(devi
, "machtrace", S_IFCHR
, 0,
911 DDI_PSEUDO
, 0) == DDI_FAILURE
||
912 dtrace_register("mach_trap", &machtrace_attr
, DTRACE_PRIV_USER
, NULL
,
913 &machtrace_pops
, NULL
, &machtrace_id
) != 0) {
914 machtrace_probe
= (void*)&systrace_stub
;
915 ddi_remove_minor_node(devi
, NULL
);
922 d_open_t _systrace_open
;
925 _systrace_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
927 #pragma unused(dev,flags,devtype,p)
931 #define SYSTRACE_MAJOR -24 /* let the kernel pick the device number */
933 static struct cdevsw systrace_cdevsw
=
935 .d_open
= _systrace_open
,
938 .d_write
= eno_rdwrt
,
939 .d_ioctl
= eno_ioctl
,
940 .d_stop
= (stop_fcn_t
*)nulldev
,
941 .d_reset
= (reset_fcn_t
*)nulldev
,
942 .d_select
= eno_select
,
944 .d_strategy
= eno_strat
,
945 .d_reserved_1
= eno_getc
,
946 .d_reserved_2
= eno_putc
,
949 void systrace_init( void );
952 systrace_init( void )
954 if (dtrace_sdt_probes_restricted()) {
958 int majdevno
= cdevsw_add(SYSTRACE_MAJOR
, &systrace_cdevsw
);
961 printf("systrace_init: failed to allocate a major number!\n");
965 systrace_attach((dev_info_t
*)(uintptr_t)majdevno
);
966 machtrace_attach((dev_info_t
*)(uintptr_t)majdevno
);
968 #undef SYSTRACE_MAJOR
971 systrace_getargval(void *arg
, dtrace_id_t id
, void *parg
, int argno
, int aframes
)
973 #pragma unused(arg,id,parg,aframes) /* __APPLE__ */
975 uint64_t *uargs
= NULL
;
977 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
980 uargs
= uthread
->t_dtrace_syscall_args
;
985 if (argno
< 0 || argno
>= SYSTRACE_NARGS
) {
989 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
991 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
996 systrace_getargdesc(void *arg
, dtrace_id_t id
, void *parg
,
997 dtrace_argdesc_t
*desc
)
999 #pragma unused(arg, id)
1000 int sysnum
= SYSTRACE_SYSNUM(parg
);
1001 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
1002 uint64_t *uargs
= NULL
;
1005 desc
->dtargd_ndx
= DTRACE_ARGNONE
;
1009 uargs
= uthread
->t_dtrace_syscall_args
;
1011 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
1012 systrace_entry_setargdesc(sysnum
, desc
->dtargd_ndx
,
1013 desc
->dtargd_native
, sizeof(desc
->dtargd_native
));
1015 systrace_return_setargdesc(sysnum
, desc
->dtargd_ndx
,
1016 desc
->dtargd_native
, sizeof(desc
->dtargd_native
));
1019 if (desc
->dtargd_native
[0] == '\0') {
1020 desc
->dtargd_ndx
= DTRACE_ARGNONE
;
1025 machtrace_getarg(void *arg
, dtrace_id_t id
, void *parg
, int argno
, int aframes
)
1027 #pragma unused(arg,id,parg,aframes) /* __APPLE__ */
1029 syscall_arg_t
*stack
= (syscall_arg_t
*)NULL
;
1031 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
1034 stack
= (syscall_arg_t
*)uthread
->t_dtrace_syscall_args
;
1041 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1042 /* dtrace_probe arguments arg0 .. arg4 are 64bits wide */
1043 val
= (uint64_t)*(stack
+ argno
);
1044 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);