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.
26 /* #pragma ident "@(#)systrace.c 1.6 06/09/19 SMI" */
30 #define _KERNEL /* Solaris vs. Darwin */
34 #include <kern/thread.h>
35 #include <mach/thread_status.h>
37 /* XXX All of these should really be derived from syscall_sw.h */
38 #if defined (__x86_64__)
39 #define SYSCALL_CLASS_SHIFT 24
40 #define SYSCALL_CLASS_MASK (0xFF << SYSCALL_CLASS_SHIFT)
41 #define SYSCALL_NUMBER_MASK (~SYSCALL_CLASS_MASK)
42 #define I386_SYSCALL_NUMBER_MASK (0xFFFF)
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/errno.h>
49 #include <sys/ioctl.h>
51 #include <sys/fcntl.h>
52 #include <sys/syscall.h>
53 #include <miscfs/devfs/devfs.h>
55 #include <sys/dtrace.h>
56 #include <sys/dtrace_impl.h>
57 #include <sys/systrace_args.h>
60 #include <sys/systm.h>
64 #include <machine/pal_routines.h>
66 #if defined (__x86_64__)
67 #define SYSTRACE_ARTIFICIAL_FRAMES 2
68 #define MACHTRACE_ARTIFICIAL_FRAMES 3
69 #elif defined(__arm__) || defined(__arm64__)
70 #define SYSTRACE_ARTIFICIAL_FRAMES 2
71 #define MACHTRACE_ARTIFICIAL_FRAMES 3
73 #error Unknown Architecture
76 #define SYSTRACE_NARGS (int)(sizeof(((uthread_t)NULL)->uu_arg) / sizeof(((uthread_t)NULL)->uu_arg[0]))
78 #include <sys/sysent.h>
79 #define sy_callc sy_call /* Map Solaris slot name to Darwin's */
80 #define NSYSCALL nsysent /* and is less than 500 or so */
82 extern const char *syscallnames
[];
84 #include <sys/dtrace_glue.h>
85 #define casptr dtrace_casptr
86 #define membar_enter dtrace_membar_producer
88 #define LOADABLE_SYSCALL(a) 0 /* Not pertinent to Darwin. */
89 #define LOADED_SYSCALL(a) 1 /* Not pertinent to Darwin. */
91 extern lck_attr_t
* dtrace_lck_attr
;
92 extern lck_grp_t
* dtrace_lck_grp
;
93 static lck_mtx_t dtrace_systrace_lock
; /* probe state lock */
95 systrace_sysent_t
*systrace_sysent
= NULL
;
96 void (*systrace_probe
)(dtrace_id_t
, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
98 static uint64_t systrace_getargval(void *, dtrace_id_t
, void *, int, int);
99 static void systrace_getargdesc(void *, dtrace_id_t
, void *, dtrace_argdesc_t
*);
102 systrace_stub(dtrace_id_t id
, uint64_t arg0
, uint64_t arg1
,
103 uint64_t arg2
, uint64_t arg3
, uint64_t arg4
)
105 #pragma unused(id,arg0,arg1,arg2,arg3,arg4)
109 dtrace_systrace_syscall(struct proc
*pp
, void *uap
, int *rv
)
111 unsigned short code
; /* The system call number */
113 systrace_sysent_t
*sy
;
116 syscall_arg_t
*ip
= (syscall_arg_t
*)uap
;
117 uint64_t uargs
[SYSTRACE_NARGS
] = {0};
119 #if defined (__x86_64__)
121 pal_register_cache_state(current_thread(), VALID
);
122 x86_saved_state_t
*tagged_regs
= (x86_saved_state_t
*)find_user_regs(current_thread());
124 if (is_saved_state64(tagged_regs
)) {
125 x86_saved_state64_t
*regs
= saved_state64(tagged_regs
);
126 code
= regs
->rax
& SYSCALL_NUMBER_MASK
;
128 * Check for indirect system call... system call number
135 code
= saved_state32(tagged_regs
)->eax
& I386_SYSCALL_NUMBER_MASK
;
138 vm_offset_t params
= (vm_offset_t
) (saved_state32(tagged_regs
)->uesp
+ sizeof(int));
139 code
= fuword(params
);
143 #elif defined(__arm__)
146 * On arm, syscall numbers depend on a flavor (indirect or not)
147 * and can be in either r0 or r12 (always u32)
150 /* See bsd/dev/arm/systemcalls.c:arm_get_syscall_number */
151 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
153 /* Check for indirect system call */
154 if (arm_regs
->r
[12] != 0) {
155 code
= arm_regs
->r
[12];
157 code
= arm_regs
->r
[0];
160 #elif defined(__arm64__)
163 * On arm64, syscall numbers depend on a flavor (indirect or not)
164 * ... and for u32 can be in either r0 or r12
165 * ... and for u64 can be in either x0 or x16
168 /* see bsd/dev/arm/systemcalls.c:arm_get_syscall_number */
169 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
171 if (is_saved_state32(arm_regs
)) {
172 /* Check for indirect system call */
173 if (saved_state32(arm_regs
)->r
[12] != 0) {
174 code
= saved_state32(arm_regs
)->r
[12];
176 code
= saved_state32(arm_regs
)->r
[0];
179 /* Check for indirect system call */
180 if (saved_state64(arm_regs
)->x
[ARM64_SYSCALL_CODE_REG_NUM
] != 0) {
181 code
= saved_state64(arm_regs
)->x
[ARM64_SYSCALL_CODE_REG_NUM
];
183 code
= saved_state64(arm_regs
)->x
[0];
188 #error Unknown Architecture
191 // Bounds "check" the value of code a la unix_syscall
192 sy
= (code
>= nsysent
) ? &systrace_sysent
[SYS_invalid
] : &systrace_sysent
[code
];
194 systrace_args(code
, ip
, uargs
);
196 if ((id
= sy
->stsy_entry
) != DTRACE_IDNONE
) {
197 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
199 uthread
->t_dtrace_syscall_args
= uargs
;
202 static_assert(SYSTRACE_NARGS
>= 5, "not enough system call arguments");
203 (*systrace_probe
)(id
, uargs
[0], uargs
[1], uargs
[2], uargs
[3], uargs
[4]);
206 uthread
->t_dtrace_syscall_args
= NULL
;
214 * APPLE NOTE: Not implemented.
215 * We want to explicitly allow DTrace consumers to stop a process
216 * before it actually executes the meat of the syscall.
218 p
= ttoproc(curthread
);
219 mutex_enter(&p
->p_lock
);
220 if (curthread
->t_dtrace_stop
&& !curthread
->t_lwp
->lwp_nostop
) {
221 curthread
->t_dtrace_stop
= 0;
222 stop(PR_REQUESTED
, 0);
224 mutex_exit(&p
->p_lock
);
227 rval
= (*sy
->stsy_underlying
)(pp
, uap
, rv
);
229 if ((id
= sy
->stsy_return
) != DTRACE_IDNONE
) {
230 uint64_t munged_rv0
, munged_rv1
;
231 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
234 uthread
->t_dtrace_errno
= rval
; /* Establish t_dtrace_errno now in case this enabling refers to it. */
237 * "Decode" rv for use in the call to dtrace_probe()
239 if (rval
== ERESTART
) {
240 munged_rv0
= -1LL; /* System call will be reissued in user mode. Make DTrace report a -1 return. */
242 } else if (rval
!= EJUSTRETURN
) {
244 munged_rv0
= -1LL; /* Mimic what libc will do. */
247 switch (sy
->stsy_return_type
) {
248 case _SYSCALL_RET_INT_T
:
252 case _SYSCALL_RET_UINT_T
:
253 munged_rv0
= ((u_int
)rv
[0]);
254 munged_rv1
= ((u_int
)rv
[1]);
256 case _SYSCALL_RET_OFF_T
:
257 case _SYSCALL_RET_UINT64_T
:
258 munged_rv0
= *(u_int64_t
*)rv
;
261 case _SYSCALL_RET_ADDR_T
:
262 case _SYSCALL_RET_SIZE_T
:
263 case _SYSCALL_RET_SSIZE_T
:
264 munged_rv0
= *(user_addr_t
*)rv
;
267 case _SYSCALL_RET_NONE
:
283 * <http://mail.opensolaris.org/pipermail/dtrace-discuss/2007-January/003276.html> says:
285 * "This is a bit of an historical artifact. At first, the syscall provider just
286 * had its return value in arg0, and the fbt and pid providers had their return
287 * values in arg1 (so that we could use arg0 for the offset of the return site).
289 * We inevitably started writing scripts where we wanted to see the return
290 * values from probes in all three providers, and we made this script easier
291 * to write by replicating the syscall return values in arg1 to match fbt and
292 * pid. We debated briefly about removing the return value from arg0, but
293 * decided that it would be less confusing to have the same data in two places
294 * than to have some non-helpful, non-intuitive value in arg0.
296 * This change was made 4/23/2003 according to the DTrace project's putback log."
298 (*systrace_probe
)(id
, munged_rv0
, munged_rv0
, munged_rv1
, (uint64_t)rval
, 0);
305 dtrace_systrace_syscall_return(unsigned short code
, int rval
, int *rv
)
307 systrace_sysent_t
*sy
;
310 // Bounds "check" the value of code a la unix_syscall_return
311 sy
= (code
>= nsysent
) ? &systrace_sysent
[SYS_invalid
] : &systrace_sysent
[code
];
313 if ((id
= sy
->stsy_return
) != DTRACE_IDNONE
) {
314 uint64_t munged_rv0
, munged_rv1
;
315 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
318 uthread
->t_dtrace_errno
= rval
; /* Establish t_dtrace_errno now in case this enabling refers to it. */
321 * "Decode" rv for use in the call to dtrace_probe()
323 if (rval
== ERESTART
) {
324 munged_rv0
= -1LL; /* System call will be reissued in user mode. Make DTrace report a -1 return. */
326 } else if (rval
!= EJUSTRETURN
) {
328 munged_rv0
= -1LL; /* Mimic what libc will do. */
331 switch (sy
->stsy_return_type
) {
332 case _SYSCALL_RET_INT_T
:
336 case _SYSCALL_RET_UINT_T
:
337 munged_rv0
= ((u_int
)rv
[0]);
338 munged_rv1
= ((u_int
)rv
[1]);
340 case _SYSCALL_RET_OFF_T
:
341 case _SYSCALL_RET_UINT64_T
:
342 munged_rv0
= *(u_int64_t
*)rv
;
345 case _SYSCALL_RET_ADDR_T
:
346 case _SYSCALL_RET_SIZE_T
:
347 case _SYSCALL_RET_SSIZE_T
:
348 munged_rv0
= *(user_addr_t
*)rv
;
351 case _SYSCALL_RET_NONE
:
366 (*systrace_probe
)(id
, munged_rv0
, munged_rv0
, munged_rv1
, (uint64_t)rval
, 0);
370 #define SYSTRACE_SHIFT 16
371 #define SYSTRACE_ISENTRY(x) ((int)(x) >> SYSTRACE_SHIFT)
372 #define SYSTRACE_SYSNUM(x) ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1))
373 #define SYSTRACE_ENTRY(id) ((1 << SYSTRACE_SHIFT) | (id))
374 #define SYSTRACE_RETURN(id) (id)
376 #if ((1 << SYSTRACE_SHIFT) <= NSYSCALL)
377 #error 1 << SYSTRACE_SHIFT must exceed number of system calls
380 static dtrace_provider_id_t systrace_id
;
383 * APPLE NOTE: Avoid name clash with Darwin automagic conf symbol.
384 * See balanced undef below.
386 #define systrace_init _systrace_init
389 systrace_init(struct sysent
*actual
, systrace_sysent_t
**interposed
)
391 systrace_sysent_t
*ssysent
= *interposed
; /* Avoid sysent shadow warning
392 * from bsd/sys/sysent.h */
395 if (ssysent
== NULL
) {
396 *interposed
= ssysent
= kmem_zalloc(sizeof(systrace_sysent_t
) *
400 for (i
= 0; i
< NSYSCALL
; i
++) {
401 struct sysent
*a
= &actual
[i
];
402 systrace_sysent_t
*s
= &ssysent
[i
];
404 if (LOADABLE_SYSCALL(a
) && !LOADED_SYSCALL(a
)) {
408 if (a
->sy_callc
== dtrace_systrace_syscall
) {
412 s
->stsy_underlying
= a
->sy_callc
;
413 s
->stsy_return_type
= a
->sy_return_type
;
415 lck_mtx_init(&dtrace_systrace_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
421 systrace_provide(void *arg
, const dtrace_probedesc_t
*desc
)
423 #pragma unused(arg) /* __APPLE__ */
430 systrace_init(sysent
, &systrace_sysent
);
432 for (i
= 0; i
< NSYSCALL
; i
++) {
433 if (systrace_sysent
[i
].stsy_underlying
== NULL
) {
437 if (dtrace_probe_lookup(systrace_id
, NULL
,
438 syscallnames
[i
], "entry") != 0) {
442 (void) dtrace_probe_create(systrace_id
, NULL
, syscallnames
[i
],
443 "entry", SYSTRACE_ARTIFICIAL_FRAMES
,
444 (void *)((uintptr_t)SYSTRACE_ENTRY(i
)));
445 (void) dtrace_probe_create(systrace_id
, NULL
, syscallnames
[i
],
446 "return", SYSTRACE_ARTIFICIAL_FRAMES
,
447 (void *)((uintptr_t)SYSTRACE_RETURN(i
)));
449 systrace_sysent
[i
].stsy_entry
= DTRACE_IDNONE
;
450 systrace_sysent
[i
].stsy_return
= DTRACE_IDNONE
;
457 systrace_destroy(void *arg
, dtrace_id_t id
, void *parg
)
459 #pragma unused(arg,id) /* __APPLE__ */
461 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
463 #pragma unused(sysnum) /* __APPLE__ */
465 * There's nothing to do here but assert that we have actually been
468 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
469 ASSERT(systrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
);
471 ASSERT(systrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
477 systrace_enable(void *arg
, dtrace_id_t id
, void *parg
)
479 #pragma unused(arg) /* __APPLE__ */
481 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
482 int enabled
= (systrace_sysent
[sysnum
].stsy_entry
!= DTRACE_IDNONE
||
483 systrace_sysent
[sysnum
].stsy_return
!= DTRACE_IDNONE
);
485 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
486 systrace_sysent
[sysnum
].stsy_entry
= id
;
488 systrace_sysent
[sysnum
].stsy_return
= id
;
492 ASSERT(sysent
[sysnum
].sy_callc
== dtrace_systrace_syscall
);
496 lck_mtx_lock(&dtrace_systrace_lock
);
497 if (sysent
[sysnum
].sy_callc
== systrace_sysent
[sysnum
].stsy_underlying
) {
498 vm_offset_t dss
= (vm_offset_t
)&dtrace_systrace_syscall
;
499 ml_nofault_copy((vm_offset_t
)&dss
, (vm_offset_t
)&sysent
[sysnum
].sy_callc
, sizeof(vm_offset_t
));
501 lck_mtx_unlock(&dtrace_systrace_lock
);
507 systrace_disable(void *arg
, dtrace_id_t id
, void *parg
)
509 #pragma unused(arg,id) /* __APPLE__ */
511 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
512 int disable
= (systrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
||
513 systrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
516 lck_mtx_lock(&dtrace_systrace_lock
);
517 if (sysent
[sysnum
].sy_callc
== dtrace_systrace_syscall
) {
518 ml_nofault_copy((vm_offset_t
)&systrace_sysent
[sysnum
].stsy_underlying
, (vm_offset_t
)&sysent
[sysnum
].sy_callc
, sizeof(systrace_sysent
[sysnum
].stsy_underlying
));
520 lck_mtx_unlock(&dtrace_systrace_lock
);
523 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
524 systrace_sysent
[sysnum
].stsy_entry
= DTRACE_IDNONE
;
526 systrace_sysent
[sysnum
].stsy_return
= DTRACE_IDNONE
;
530 static dtrace_pattr_t systrace_attr
= {
531 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
532 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
533 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
534 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
535 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
538 static dtrace_pops_t systrace_pops
= {
539 .dtps_provide
= systrace_provide
,
540 .dtps_provide_module
= NULL
,
541 .dtps_enable
= systrace_enable
,
542 .dtps_disable
= systrace_disable
,
543 .dtps_suspend
= NULL
,
545 .dtps_getargdesc
= systrace_getargdesc
,
546 .dtps_getargval
= systrace_getargval
,
547 .dtps_usermode
= NULL
,
548 .dtps_destroy
= systrace_destroy
552 systrace_attach(dev_info_t
*devi
)
554 systrace_probe
= (void*)&dtrace_probe
;
557 if (ddi_create_minor_node(devi
, "systrace", S_IFCHR
, 0,
558 DDI_PSEUDO
, 0) == DDI_FAILURE
||
559 dtrace_register("syscall", &systrace_attr
, DTRACE_PRIV_USER
, NULL
,
560 &systrace_pops
, NULL
, &systrace_id
) != 0) {
561 systrace_probe
= systrace_stub
;
562 ddi_remove_minor_node(devi
, NULL
);
571 * APPLE NOTE: systrace_detach not implemented
573 #if !defined(__APPLE__)
575 systrace_detach(dev_info_t
*devi
, ddi_detach_cmd_t cmd
)
586 if (dtrace_unregister(systrace_id
) != 0) {
590 ddi_remove_minor_node(devi
, NULL
);
591 systrace_probe
= systrace_stub
;
594 #endif /* __APPLE__ */
597 typedef kern_return_t (*mach_call_t
)(void *);
599 /* APPLE NOTE: From #include <kern/syscall_sw.h> which may be changed for 64 bit! */
600 typedef void mach_munge_t(void *);
603 int mach_trap_arg_count
;
604 kern_return_t (*mach_trap_function
)(void *);
605 #if defined(__arm64__) || defined(__x86_64__)
606 mach_munge_t
*mach_trap_arg_munge32
; /* system call arguments for 32-bit */
608 int mach_trap_u32_words
;
610 const char* mach_trap_name
;
611 #endif /* MACH_ASSERT */
614 extern const mach_trap_t mach_trap_table
[]; /* syscall_sw.h now declares this as const */
615 extern int mach_trap_count
;
617 extern const char *mach_syscall_name_table
[];
619 /* XXX From osfmk/i386/bsd_i386.c */
620 struct mach_call_args
{
633 #define NSYSCALL mach_trap_count
635 #if ((1 << SYSTRACE_SHIFT) <= NSYSCALL)
636 #error 1 << SYSTRACE_SHIFT must exceed number of Mach traps
639 typedef struct machtrace_sysent
{
640 dtrace_id_t stsy_entry
;
641 dtrace_id_t stsy_return
;
642 kern_return_t (*stsy_underlying
)(void *);
643 int32_t stsy_return_type
;
644 } machtrace_sysent_t
;
646 static machtrace_sysent_t
*machtrace_sysent
= NULL
;
648 void (*machtrace_probe
)(dtrace_id_t
, uint64_t, uint64_t,
649 uint64_t, uint64_t, uint64_t);
651 static uint64_t machtrace_getarg(void *, dtrace_id_t
, void *, int, int);
653 static dtrace_provider_id_t machtrace_id
;
656 dtrace_machtrace_syscall(struct mach_call_args
*args
)
658 int code
; /* The mach call number */
660 machtrace_sysent_t
*sy
;
666 syscall_arg_t
*ip
= (syscall_arg_t
*)args
;
667 mach_call_t mach_call
;
669 #if defined (__x86_64__)
671 pal_register_cache_state(current_thread(), VALID
);
672 x86_saved_state_t
*tagged_regs
= (x86_saved_state_t
*)find_user_regs(current_thread());
674 if (is_saved_state64(tagged_regs
)) {
675 code
= saved_state64(tagged_regs
)->rax
& SYSCALL_NUMBER_MASK
;
677 code
= -saved_state32(tagged_regs
)->eax
;
680 #elif defined(__arm__)
682 /* r12 has the machcall number, but it is -ve */
683 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
684 code
= (int)arm_regs
->r
[12];
685 ASSERT(code
< 0); /* Otherwise it would be a Unix syscall */
688 #elif defined(__arm64__)
690 /* From arm/thread_status.h:get_saved_state_svc_number */
691 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
692 if (is_saved_state32(arm_regs
)) {
693 code
= (int)saved_state32(arm_regs
)->r
[12];
695 code
= (int)saved_state64(arm_regs
)->x
[ARM64_SYSCALL_CODE_REG_NUM
];
698 /* From bsd/arm64.c:mach_syscall */
699 ASSERT(code
< 0); /* Otherwise it would be a Unix syscall */
703 #error Unknown Architecture
706 sy
= &machtrace_sysent
[code
];
708 if ((id
= sy
->stsy_entry
) != DTRACE_IDNONE
) {
709 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
712 uthread
->t_dtrace_syscall_args
= (void *)ip
;
715 (*machtrace_probe
)(id
, *ip
, *(ip
+ 1), *(ip
+ 2), *(ip
+ 3), *(ip
+ 4));
718 uthread
->t_dtrace_syscall_args
= (void *)0;
724 * APPLE NOTE: Not implemented.
725 * We want to explicitly allow DTrace consumers to stop a process
726 * before it actually executes the meat of the syscall.
728 p
= ttoproc(curthread
);
729 mutex_enter(&p
->p_lock
);
730 if (curthread
->t_dtrace_stop
&& !curthread
->t_lwp
->lwp_nostop
) {
731 curthread
->t_dtrace_stop
= 0;
732 stop(PR_REQUESTED
, 0);
734 mutex_exit(&p
->p_lock
);
737 mach_call
= (mach_call_t
)(*sy
->stsy_underlying
);
738 rval
= mach_call(args
);
740 if ((id
= sy
->stsy_return
) != DTRACE_IDNONE
) {
741 (*machtrace_probe
)(id
, (uint64_t)rval
, 0, 0, 0, 0);
748 machtrace_init(const mach_trap_t
*actual
, machtrace_sysent_t
**interposed
)
750 machtrace_sysent_t
*msysent
= *interposed
;
753 if (msysent
== NULL
) {
754 *interposed
= msysent
= kmem_zalloc(sizeof(machtrace_sysent_t
) *
758 for (i
= 0; i
< NSYSCALL
; i
++) {
759 const mach_trap_t
*a
= &actual
[i
];
760 machtrace_sysent_t
*s
= &msysent
[i
];
762 if (LOADABLE_SYSCALL(a
) && !LOADED_SYSCALL(a
)) {
766 if (a
->mach_trap_function
== (mach_call_t
)(dtrace_machtrace_syscall
)) {
770 s
->stsy_underlying
= a
->mach_trap_function
;
776 machtrace_provide(void *arg
, const dtrace_probedesc_t
*desc
)
778 #pragma unused(arg) /* __APPLE__ */
786 machtrace_init(mach_trap_table
, &machtrace_sysent
);
788 for (i
= 0; i
< NSYSCALL
; i
++) {
789 if (machtrace_sysent
[i
].stsy_underlying
== NULL
) {
793 if (dtrace_probe_lookup(machtrace_id
, NULL
,
794 mach_syscall_name_table
[i
], "entry") != 0) {
798 (void) dtrace_probe_create(machtrace_id
, NULL
, mach_syscall_name_table
[i
],
799 "entry", MACHTRACE_ARTIFICIAL_FRAMES
,
800 (void *)((uintptr_t)SYSTRACE_ENTRY(i
)));
801 (void) dtrace_probe_create(machtrace_id
, NULL
, mach_syscall_name_table
[i
],
802 "return", MACHTRACE_ARTIFICIAL_FRAMES
,
803 (void *)((uintptr_t)SYSTRACE_RETURN(i
)));
805 machtrace_sysent
[i
].stsy_entry
= DTRACE_IDNONE
;
806 machtrace_sysent
[i
].stsy_return
= DTRACE_IDNONE
;
812 machtrace_destroy(void *arg
, dtrace_id_t id
, void *parg
)
814 #pragma unused(arg,id) /* __APPLE__ */
815 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
817 #pragma unused(sysnum) /* __APPLE__ */
820 * There's nothing to do here but assert that we have actually been
823 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
824 ASSERT(machtrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
);
826 ASSERT(machtrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
832 machtrace_enable(void *arg
, dtrace_id_t id
, void *parg
)
834 #pragma unused(arg) /* __APPLE__ */
836 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
837 int enabled
= (machtrace_sysent
[sysnum
].stsy_entry
!= DTRACE_IDNONE
||
838 machtrace_sysent
[sysnum
].stsy_return
!= DTRACE_IDNONE
);
840 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
841 machtrace_sysent
[sysnum
].stsy_entry
= id
;
843 machtrace_sysent
[sysnum
].stsy_return
= id
;
847 ASSERT(mach_trap_table
[sysnum
].mach_trap_function
== (void *)dtrace_machtrace_syscall
);
851 lck_mtx_lock(&dtrace_systrace_lock
);
853 if (mach_trap_table
[sysnum
].mach_trap_function
== machtrace_sysent
[sysnum
].stsy_underlying
) {
854 vm_offset_t dss
= (vm_offset_t
)&dtrace_machtrace_syscall
;
855 ml_nofault_copy((vm_offset_t
)&dss
, (vm_offset_t
)&mach_trap_table
[sysnum
].mach_trap_function
, sizeof(vm_offset_t
));
858 lck_mtx_unlock(&dtrace_systrace_lock
);
865 machtrace_disable(void *arg
, dtrace_id_t id
, void *parg
)
867 #pragma unused(arg,id) /* __APPLE__ */
869 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
870 int disable
= (machtrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
||
871 machtrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
874 lck_mtx_lock(&dtrace_systrace_lock
);
876 if (mach_trap_table
[sysnum
].mach_trap_function
== (mach_call_t
)dtrace_machtrace_syscall
) {
877 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
));
879 lck_mtx_unlock(&dtrace_systrace_lock
);
882 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
883 machtrace_sysent
[sysnum
].stsy_entry
= DTRACE_IDNONE
;
885 machtrace_sysent
[sysnum
].stsy_return
= DTRACE_IDNONE
;
889 static dtrace_pattr_t machtrace_attr
= {
890 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
891 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
892 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
893 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
894 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
897 static dtrace_pops_t machtrace_pops
= {
898 .dtps_provide
= machtrace_provide
,
899 .dtps_provide_module
= NULL
,
900 .dtps_enable
= machtrace_enable
,
901 .dtps_disable
= machtrace_disable
,
902 .dtps_suspend
= NULL
,
904 .dtps_getargdesc
= NULL
,
905 .dtps_getargval
= machtrace_getarg
,
906 .dtps_usermode
= NULL
,
907 .dtps_destroy
= machtrace_destroy
911 machtrace_attach(dev_info_t
*devi
)
913 machtrace_probe
= dtrace_probe
;
916 if (ddi_create_minor_node(devi
, "machtrace", S_IFCHR
, 0,
917 DDI_PSEUDO
, 0) == DDI_FAILURE
||
918 dtrace_register("mach_trap", &machtrace_attr
, DTRACE_PRIV_USER
, NULL
,
919 &machtrace_pops
, NULL
, &machtrace_id
) != 0) {
920 machtrace_probe
= (void*)&systrace_stub
;
921 ddi_remove_minor_node(devi
, NULL
);
928 d_open_t _systrace_open
;
931 _systrace_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
933 #pragma unused(dev,flags,devtype,p)
937 #define SYSTRACE_MAJOR -24 /* let the kernel pick the device number */
940 * A struct describing which functions will get invoked for certain
943 static struct cdevsw systrace_cdevsw
=
945 _systrace_open
, /* open */
946 eno_opcl
, /* close */
947 eno_rdwrt
, /* read */
948 eno_rdwrt
, /* write */
949 eno_ioctl
, /* ioctl */
950 (stop_fcn_t
*)nulldev
, /* stop */
951 (reset_fcn_t
*)nulldev
, /* reset */
953 eno_select
, /* select */
955 eno_strat
, /* strategy */
961 void systrace_init( void );
964 systrace_init( void )
966 if (dtrace_sdt_probes_restricted()) {
970 int majdevno
= cdevsw_add(SYSTRACE_MAJOR
, &systrace_cdevsw
);
973 printf("systrace_init: failed to allocate a major number!\n");
977 systrace_attach((dev_info_t
*)(uintptr_t)majdevno
);
978 machtrace_attach((dev_info_t
*)(uintptr_t)majdevno
);
980 #undef SYSTRACE_MAJOR
983 systrace_getargval(void *arg
, dtrace_id_t id
, void *parg
, int argno
, int aframes
)
985 #pragma unused(arg,id,parg,aframes) /* __APPLE__ */
987 uint64_t *uargs
= NULL
;
989 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
992 uargs
= uthread
->t_dtrace_syscall_args
;
997 if (argno
< 0 || argno
>= SYSTRACE_NARGS
) {
1001 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1003 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1008 systrace_getargdesc(void *arg
, dtrace_id_t id
, void *parg
,
1009 dtrace_argdesc_t
*desc
)
1011 #pragma unused(arg, id)
1012 int sysnum
= SYSTRACE_SYSNUM(parg
);
1013 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
1014 uint64_t *uargs
= NULL
;
1017 desc
->dtargd_ndx
= DTRACE_ARGNONE
;
1021 uargs
= uthread
->t_dtrace_syscall_args
;
1023 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
1024 systrace_entry_setargdesc(sysnum
, desc
->dtargd_ndx
,
1025 desc
->dtargd_native
, sizeof(desc
->dtargd_native
));
1027 systrace_return_setargdesc(sysnum
, desc
->dtargd_ndx
,
1028 desc
->dtargd_native
, sizeof(desc
->dtargd_native
));
1031 if (desc
->dtargd_native
[0] == '\0') {
1032 desc
->dtargd_ndx
= DTRACE_ARGNONE
;
1037 machtrace_getarg(void *arg
, dtrace_id_t id
, void *parg
, int argno
, int aframes
)
1039 #pragma unused(arg,id,parg,aframes) /* __APPLE__ */
1041 syscall_arg_t
*stack
= (syscall_arg_t
*)NULL
;
1043 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
1046 stack
= (syscall_arg_t
*)uthread
->t_dtrace_syscall_args
;
1053 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1054 /* dtrace_probe arguments arg0 .. arg4 are 64bits wide */
1055 val
= (uint64_t)*(stack
+ argno
);
1056 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);