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 #include <kern/thread.h>
27 #include <mach/thread_status.h>
29 /* XXX All of these should really be derived from syscall_sw.h */
30 #if defined (__x86_64__)
31 #define SYSCALL_CLASS_SHIFT 24
32 #define SYSCALL_CLASS_MASK (0xFF << SYSCALL_CLASS_SHIFT)
33 #define SYSCALL_NUMBER_MASK (~SYSCALL_CLASS_MASK)
34 #define I386_SYSCALL_NUMBER_MASK (0xFFFF)
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/ioctl.h>
43 #include <sys/fcntl.h>
44 #include <sys/syscall.h>
45 #include <miscfs/devfs/devfs.h>
47 #include <sys/dtrace.h>
48 #include <sys/dtrace_impl.h>
49 #include <sys/systrace_args.h>
52 #include <sys/systm.h>
56 #include <machine/pal_routines.h>
58 #if defined (__x86_64__)
59 #define SYSTRACE_ARTIFICIAL_FRAMES 2
60 #define MACHTRACE_ARTIFICIAL_FRAMES 3
61 #elif defined(__arm__) || defined(__arm64__)
62 #define SYSTRACE_ARTIFICIAL_FRAMES 2
63 #define MACHTRACE_ARTIFICIAL_FRAMES 3
65 #error Unknown Architecture
68 #define SYSTRACE_NARGS (int)(sizeof(((uthread_t)NULL)->uu_arg) / sizeof(((uthread_t)NULL)->uu_arg[0]))
70 #include <sys/sysent.h>
71 #define sy_callc sy_call /* Map Solaris slot name to Darwin's */
72 #define NSYSCALL nsysent /* and is less than 500 or so */
74 extern const char *syscallnames
[];
76 #include <sys/dtrace_glue.h>
77 #define casptr dtrace_casptr
78 #define membar_enter dtrace_membar_producer
80 #define LOADABLE_SYSCALL(a) 0 /* Not pertinent to Darwin. */
81 #define LOADED_SYSCALL(a) 1 /* Not pertinent to Darwin. */
83 extern lck_attr_t
* dtrace_lck_attr
;
84 extern lck_grp_t
* dtrace_lck_grp
;
85 static lck_mtx_t dtrace_systrace_lock
; /* probe state lock */
87 systrace_sysent_t
*systrace_sysent
= NULL
;
88 void (*systrace_probe
)(dtrace_id_t
, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
90 static uint64_t systrace_getargval(void *, dtrace_id_t
, void *, int, int);
91 static void systrace_getargdesc(void *, dtrace_id_t
, void *, dtrace_argdesc_t
*);
94 systrace_stub(dtrace_id_t id
, uint64_t arg0
, uint64_t arg1
,
95 uint64_t arg2
, uint64_t arg3
, uint64_t arg4
)
97 #pragma unused(id,arg0,arg1,arg2,arg3,arg4)
101 dtrace_systrace_syscall(struct proc
*pp
, void *uap
, int *rv
)
103 unsigned short code
; /* The system call number */
105 systrace_sysent_t
*sy
;
108 syscall_arg_t
*ip
= (syscall_arg_t
*)uap
;
109 uint64_t uargs
[SYSTRACE_NARGS
] = {0};
111 #if defined (__x86_64__)
113 pal_register_cache_state(current_thread(), VALID
);
114 x86_saved_state_t
*tagged_regs
= (x86_saved_state_t
*)find_user_regs(current_thread());
116 if (is_saved_state64(tagged_regs
)) {
117 x86_saved_state64_t
*regs
= saved_state64(tagged_regs
);
118 code
= regs
->rax
& SYSCALL_NUMBER_MASK
;
120 * Check for indirect system call... system call number
127 code
= saved_state32(tagged_regs
)->eax
& I386_SYSCALL_NUMBER_MASK
;
130 vm_offset_t params
= (vm_offset_t
) (saved_state32(tagged_regs
)->uesp
+ sizeof(int));
131 code
= fuword(params
);
135 #elif defined(__arm__)
138 * On arm, syscall numbers depend on a flavor (indirect or not)
139 * and can be in either r0 or r12 (always u32)
142 /* See bsd/dev/arm/systemcalls.c:arm_get_syscall_number */
143 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
145 /* Check for indirect system call */
146 if (arm_regs
->r
[12] != 0) {
147 code
= arm_regs
->r
[12];
149 code
= arm_regs
->r
[0];
152 #elif defined(__arm64__)
155 * On arm64, syscall numbers depend on a flavor (indirect or not)
156 * ... and for u32 can be in either r0 or r12
157 * ... and for u64 can be in either x0 or x16
160 /* see bsd/dev/arm/systemcalls.c:arm_get_syscall_number */
161 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
163 if (is_saved_state32(arm_regs
)) {
164 /* Check for indirect system call */
165 if (saved_state32(arm_regs
)->r
[12] != 0) {
166 code
= saved_state32(arm_regs
)->r
[12];
168 code
= saved_state32(arm_regs
)->r
[0];
171 /* Check for indirect system call */
172 if (saved_state64(arm_regs
)->x
[ARM64_SYSCALL_CODE_REG_NUM
] != 0) {
173 code
= saved_state64(arm_regs
)->x
[ARM64_SYSCALL_CODE_REG_NUM
];
175 code
= saved_state64(arm_regs
)->x
[0];
180 #error Unknown Architecture
183 // Bounds "check" the value of code a la unix_syscall
184 sy
= (code
>= nsysent
) ? &systrace_sysent
[SYS_invalid
] : &systrace_sysent
[code
];
186 systrace_args(code
, ip
, uargs
);
188 if ((id
= sy
->stsy_entry
) != DTRACE_IDNONE
) {
189 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
191 uthread
->t_dtrace_syscall_args
= uargs
;
194 static_assert(SYSTRACE_NARGS
>= 5, "not enough system call arguments");
195 (*systrace_probe
)(id
, uargs
[0], uargs
[1], uargs
[2], uargs
[3], uargs
[4]);
198 uthread
->t_dtrace_syscall_args
= NULL
;
206 * APPLE NOTE: Not implemented.
207 * We want to explicitly allow DTrace consumers to stop a process
208 * before it actually executes the meat of the syscall.
210 p
= ttoproc(curthread
);
211 mutex_enter(&p
->p_lock
);
212 if (curthread
->t_dtrace_stop
&& !curthread
->t_lwp
->lwp_nostop
) {
213 curthread
->t_dtrace_stop
= 0;
214 stop(PR_REQUESTED
, 0);
216 mutex_exit(&p
->p_lock
);
219 rval
= (*sy
->stsy_underlying
)(pp
, uap
, rv
);
221 if ((id
= sy
->stsy_return
) != DTRACE_IDNONE
) {
222 uint64_t munged_rv0
, munged_rv1
;
223 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
226 uthread
->t_dtrace_errno
= rval
; /* Establish t_dtrace_errno now in case this enabling refers to it. */
229 * "Decode" rv for use in the call to dtrace_probe()
231 if (rval
== ERESTART
) {
232 munged_rv0
= -1LL; /* System call will be reissued in user mode. Make DTrace report a -1 return. */
234 } else if (rval
!= EJUSTRETURN
) {
236 munged_rv0
= -1LL; /* Mimic what libc will do. */
239 switch (sy
->stsy_return_type
) {
240 case _SYSCALL_RET_INT_T
:
244 case _SYSCALL_RET_UINT_T
:
245 munged_rv0
= ((u_int
)rv
[0]);
246 munged_rv1
= ((u_int
)rv
[1]);
248 case _SYSCALL_RET_OFF_T
:
249 case _SYSCALL_RET_UINT64_T
:
250 munged_rv0
= *(u_int64_t
*)rv
;
253 case _SYSCALL_RET_ADDR_T
:
254 case _SYSCALL_RET_SIZE_T
:
255 case _SYSCALL_RET_SSIZE_T
:
256 munged_rv0
= *(user_addr_t
*)rv
;
259 case _SYSCALL_RET_NONE
:
275 * <http://mail.opensolaris.org/pipermail/dtrace-discuss/2007-January/003276.html> says:
277 * "This is a bit of an historical artifact. At first, the syscall provider just
278 * had its return value in arg0, and the fbt and pid providers had their return
279 * values in arg1 (so that we could use arg0 for the offset of the return site).
281 * We inevitably started writing scripts where we wanted to see the return
282 * values from probes in all three providers, and we made this script easier
283 * to write by replicating the syscall return values in arg1 to match fbt and
284 * pid. We debated briefly about removing the return value from arg0, but
285 * decided that it would be less confusing to have the same data in two places
286 * than to have some non-helpful, non-intuitive value in arg0.
288 * This change was made 4/23/2003 according to the DTrace project's putback log."
290 (*systrace_probe
)(id
, munged_rv0
, munged_rv0
, munged_rv1
, (uint64_t)rval
, 0);
297 dtrace_systrace_syscall_return(unsigned short code
, int rval
, int *rv
)
299 systrace_sysent_t
*sy
;
302 // Bounds "check" the value of code a la unix_syscall_return
303 sy
= (code
>= nsysent
) ? &systrace_sysent
[SYS_invalid
] : &systrace_sysent
[code
];
305 if ((id
= sy
->stsy_return
) != DTRACE_IDNONE
) {
306 uint64_t munged_rv0
, munged_rv1
;
307 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
310 uthread
->t_dtrace_errno
= rval
; /* Establish t_dtrace_errno now in case this enabling refers to it. */
313 * "Decode" rv for use in the call to dtrace_probe()
315 if (rval
== ERESTART
) {
316 munged_rv0
= -1LL; /* System call will be reissued in user mode. Make DTrace report a -1 return. */
318 } else if (rval
!= EJUSTRETURN
) {
320 munged_rv0
= -1LL; /* Mimic what libc will do. */
323 switch (sy
->stsy_return_type
) {
324 case _SYSCALL_RET_INT_T
:
328 case _SYSCALL_RET_UINT_T
:
329 munged_rv0
= ((u_int
)rv
[0]);
330 munged_rv1
= ((u_int
)rv
[1]);
332 case _SYSCALL_RET_OFF_T
:
333 case _SYSCALL_RET_UINT64_T
:
334 munged_rv0
= *(u_int64_t
*)rv
;
337 case _SYSCALL_RET_ADDR_T
:
338 case _SYSCALL_RET_SIZE_T
:
339 case _SYSCALL_RET_SSIZE_T
:
340 munged_rv0
= *(user_addr_t
*)rv
;
343 case _SYSCALL_RET_NONE
:
358 (*systrace_probe
)(id
, munged_rv0
, munged_rv0
, munged_rv1
, (uint64_t)rval
, 0);
362 #define SYSTRACE_SHIFT 16
363 #define SYSTRACE_ISENTRY(x) ((int)(x) >> SYSTRACE_SHIFT)
364 #define SYSTRACE_SYSNUM(x) ((int)(x) & ((1 << SYSTRACE_SHIFT) - 1))
365 #define SYSTRACE_ENTRY(id) ((1 << SYSTRACE_SHIFT) | (id))
366 #define SYSTRACE_RETURN(id) (id)
368 #if ((1 << SYSTRACE_SHIFT) <= NSYSCALL)
369 #error 1 << SYSTRACE_SHIFT must exceed number of system calls
372 static dtrace_provider_id_t systrace_id
;
375 * APPLE NOTE: Avoid name clash with Darwin automagic conf symbol.
376 * See balanced undef below.
378 #define systrace_init _systrace_init
381 systrace_init(struct sysent
*actual
, systrace_sysent_t
**interposed
)
383 systrace_sysent_t
*ssysent
= *interposed
; /* Avoid sysent shadow warning
384 * from bsd/sys/sysent.h */
387 if (ssysent
== NULL
) {
388 *interposed
= ssysent
= kmem_zalloc(sizeof(systrace_sysent_t
) *
392 for (i
= 0; i
< NSYSCALL
; i
++) {
393 struct sysent
*a
= &actual
[i
];
394 systrace_sysent_t
*s
= &ssysent
[i
];
396 if (LOADABLE_SYSCALL(a
) && !LOADED_SYSCALL(a
)) {
400 if (a
->sy_callc
== dtrace_systrace_syscall
) {
404 s
->stsy_underlying
= a
->sy_callc
;
405 s
->stsy_return_type
= a
->sy_return_type
;
407 lck_mtx_init(&dtrace_systrace_lock
, dtrace_lck_grp
, dtrace_lck_attr
);
413 systrace_provide(void *arg
, const dtrace_probedesc_t
*desc
)
415 #pragma unused(arg) /* __APPLE__ */
422 systrace_init(sysent
, &systrace_sysent
);
424 for (i
= 0; i
< NSYSCALL
; i
++) {
425 if (systrace_sysent
[i
].stsy_underlying
== NULL
) {
429 if (dtrace_probe_lookup(systrace_id
, NULL
,
430 syscallnames
[i
], "entry") != 0) {
434 (void) dtrace_probe_create(systrace_id
, NULL
, syscallnames
[i
],
435 "entry", SYSTRACE_ARTIFICIAL_FRAMES
,
436 (void *)((uintptr_t)SYSTRACE_ENTRY(i
)));
437 (void) dtrace_probe_create(systrace_id
, NULL
, syscallnames
[i
],
438 "return", SYSTRACE_ARTIFICIAL_FRAMES
,
439 (void *)((uintptr_t)SYSTRACE_RETURN(i
)));
441 systrace_sysent
[i
].stsy_entry
= DTRACE_IDNONE
;
442 systrace_sysent
[i
].stsy_return
= DTRACE_IDNONE
;
449 systrace_destroy(void *arg
, dtrace_id_t id
, void *parg
)
451 #pragma unused(arg,id) /* __APPLE__ */
453 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
455 #pragma unused(sysnum) /* __APPLE__ */
457 * There's nothing to do here but assert that we have actually been
460 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
461 ASSERT(systrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
);
463 ASSERT(systrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
469 systrace_enable(void *arg
, dtrace_id_t id
, void *parg
)
471 #pragma unused(arg) /* __APPLE__ */
473 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
474 int enabled
= (systrace_sysent
[sysnum
].stsy_entry
!= DTRACE_IDNONE
||
475 systrace_sysent
[sysnum
].stsy_return
!= DTRACE_IDNONE
);
477 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
478 systrace_sysent
[sysnum
].stsy_entry
= id
;
480 systrace_sysent
[sysnum
].stsy_return
= id
;
484 ASSERT(sysent
[sysnum
].sy_callc
== dtrace_systrace_syscall
);
488 lck_mtx_lock(&dtrace_systrace_lock
);
489 if (sysent
[sysnum
].sy_callc
== systrace_sysent
[sysnum
].stsy_underlying
) {
490 vm_offset_t dss
= (vm_offset_t
)&dtrace_systrace_syscall
;
491 ml_nofault_copy((vm_offset_t
)&dss
, (vm_offset_t
)&sysent
[sysnum
].sy_callc
, sizeof(vm_offset_t
));
493 lck_mtx_unlock(&dtrace_systrace_lock
);
499 systrace_disable(void *arg
, dtrace_id_t id
, void *parg
)
501 #pragma unused(arg,id) /* __APPLE__ */
503 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
504 int disable
= (systrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
||
505 systrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
508 lck_mtx_lock(&dtrace_systrace_lock
);
509 if (sysent
[sysnum
].sy_callc
== dtrace_systrace_syscall
) {
510 ml_nofault_copy((vm_offset_t
)&systrace_sysent
[sysnum
].stsy_underlying
, (vm_offset_t
)&sysent
[sysnum
].sy_callc
, sizeof(systrace_sysent
[sysnum
].stsy_underlying
));
512 lck_mtx_unlock(&dtrace_systrace_lock
);
515 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
516 systrace_sysent
[sysnum
].stsy_entry
= DTRACE_IDNONE
;
518 systrace_sysent
[sysnum
].stsy_return
= DTRACE_IDNONE
;
522 static dtrace_pattr_t systrace_attr
= {
523 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
524 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
525 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
526 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
527 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
530 static dtrace_pops_t systrace_pops
= {
531 .dtps_provide
= systrace_provide
,
532 .dtps_provide_module
= NULL
,
533 .dtps_enable
= systrace_enable
,
534 .dtps_disable
= systrace_disable
,
535 .dtps_suspend
= NULL
,
537 .dtps_getargdesc
= systrace_getargdesc
,
538 .dtps_getargval
= systrace_getargval
,
539 .dtps_usermode
= NULL
,
540 .dtps_destroy
= systrace_destroy
544 systrace_attach(dev_info_t
*devi
)
546 systrace_probe
= (void*)&dtrace_probe
;
549 if (ddi_create_minor_node(devi
, "systrace", S_IFCHR
, 0,
550 DDI_PSEUDO
, 0) == DDI_FAILURE
||
551 dtrace_register("syscall", &systrace_attr
, DTRACE_PRIV_USER
, NULL
,
552 &systrace_pops
, NULL
, &systrace_id
) != 0) {
553 systrace_probe
= systrace_stub
;
554 ddi_remove_minor_node(devi
, NULL
);
563 * APPLE NOTE: systrace_detach not implemented
565 #if !defined(__APPLE__)
567 systrace_detach(dev_info_t
*devi
, ddi_detach_cmd_t cmd
)
578 if (dtrace_unregister(systrace_id
) != 0) {
582 ddi_remove_minor_node(devi
, NULL
);
583 systrace_probe
= systrace_stub
;
586 #endif /* __APPLE__ */
589 typedef kern_return_t (*mach_call_t
)(void *);
591 /* APPLE NOTE: From #include <kern/syscall_sw.h> which may be changed for 64 bit! */
592 typedef void mach_munge_t(void *);
595 int mach_trap_arg_count
;
596 kern_return_t (*mach_trap_function
)(void *);
597 #if defined(__arm64__) || defined(__x86_64__)
598 mach_munge_t
*mach_trap_arg_munge32
; /* system call arguments for 32-bit */
600 int mach_trap_u32_words
;
602 const char* mach_trap_name
;
603 #endif /* MACH_ASSERT */
606 extern const mach_trap_t mach_trap_table
[]; /* syscall_sw.h now declares this as const */
607 extern int mach_trap_count
;
609 extern const char *mach_syscall_name_table
[];
611 /* XXX From osfmk/i386/bsd_i386.c */
612 struct mach_call_args
{
625 #define NSYSCALL mach_trap_count
627 #if ((1 << SYSTRACE_SHIFT) <= NSYSCALL)
628 #error 1 << SYSTRACE_SHIFT must exceed number of Mach traps
631 typedef struct machtrace_sysent
{
632 dtrace_id_t stsy_entry
;
633 dtrace_id_t stsy_return
;
634 kern_return_t (*stsy_underlying
)(void *);
635 int32_t stsy_return_type
;
636 } machtrace_sysent_t
;
638 static machtrace_sysent_t
*machtrace_sysent
= NULL
;
640 void (*machtrace_probe
)(dtrace_id_t
, uint64_t, uint64_t,
641 uint64_t, uint64_t, uint64_t);
643 static uint64_t machtrace_getarg(void *, dtrace_id_t
, void *, int, int);
645 static dtrace_provider_id_t machtrace_id
;
648 dtrace_machtrace_syscall(struct mach_call_args
*args
)
650 int code
; /* The mach call number */
652 machtrace_sysent_t
*sy
;
658 syscall_arg_t
*ip
= (syscall_arg_t
*)args
;
659 mach_call_t mach_call
;
661 #if defined (__x86_64__)
663 pal_register_cache_state(current_thread(), VALID
);
664 x86_saved_state_t
*tagged_regs
= (x86_saved_state_t
*)find_user_regs(current_thread());
666 if (is_saved_state64(tagged_regs
)) {
667 code
= saved_state64(tagged_regs
)->rax
& SYSCALL_NUMBER_MASK
;
669 code
= -saved_state32(tagged_regs
)->eax
;
672 #elif defined(__arm__)
674 /* r12 has the machcall number, but it is -ve */
675 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
676 code
= (int)arm_regs
->r
[12];
677 ASSERT(code
< 0); /* Otherwise it would be a Unix syscall */
680 #elif defined(__arm64__)
682 /* From arm/thread_status.h:get_saved_state_svc_number */
683 arm_saved_state_t
*arm_regs
= (arm_saved_state_t
*) find_user_regs(current_thread());
684 if (is_saved_state32(arm_regs
)) {
685 code
= (int)saved_state32(arm_regs
)->r
[12];
687 code
= (int)saved_state64(arm_regs
)->x
[ARM64_SYSCALL_CODE_REG_NUM
];
690 /* From bsd/arm64.c:mach_syscall */
691 ASSERT(code
< 0); /* Otherwise it would be a Unix syscall */
695 #error Unknown Architecture
698 sy
= &machtrace_sysent
[code
];
700 if ((id
= sy
->stsy_entry
) != DTRACE_IDNONE
) {
701 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
704 uthread
->t_dtrace_syscall_args
= (void *)ip
;
707 (*machtrace_probe
)(id
, *ip
, *(ip
+ 1), *(ip
+ 2), *(ip
+ 3), *(ip
+ 4));
710 uthread
->t_dtrace_syscall_args
= (void *)0;
716 * APPLE NOTE: Not implemented.
717 * We want to explicitly allow DTrace consumers to stop a process
718 * before it actually executes the meat of the syscall.
720 p
= ttoproc(curthread
);
721 mutex_enter(&p
->p_lock
);
722 if (curthread
->t_dtrace_stop
&& !curthread
->t_lwp
->lwp_nostop
) {
723 curthread
->t_dtrace_stop
= 0;
724 stop(PR_REQUESTED
, 0);
726 mutex_exit(&p
->p_lock
);
729 mach_call
= (mach_call_t
)(*sy
->stsy_underlying
);
730 rval
= mach_call(args
);
732 if ((id
= sy
->stsy_return
) != DTRACE_IDNONE
) {
733 (*machtrace_probe
)(id
, (uint64_t)rval
, 0, 0, 0, 0);
740 machtrace_init(const mach_trap_t
*actual
, machtrace_sysent_t
**interposed
)
742 machtrace_sysent_t
*msysent
= *interposed
;
745 if (msysent
== NULL
) {
746 *interposed
= msysent
= kmem_zalloc(sizeof(machtrace_sysent_t
) *
750 for (i
= 0; i
< NSYSCALL
; i
++) {
751 const mach_trap_t
*a
= &actual
[i
];
752 machtrace_sysent_t
*s
= &msysent
[i
];
754 if (LOADABLE_SYSCALL(a
) && !LOADED_SYSCALL(a
)) {
758 if (a
->mach_trap_function
== (mach_call_t
)(dtrace_machtrace_syscall
)) {
762 s
->stsy_underlying
= a
->mach_trap_function
;
768 machtrace_provide(void *arg
, const dtrace_probedesc_t
*desc
)
770 #pragma unused(arg) /* __APPLE__ */
778 machtrace_init(mach_trap_table
, &machtrace_sysent
);
780 for (i
= 0; i
< NSYSCALL
; i
++) {
781 if (machtrace_sysent
[i
].stsy_underlying
== NULL
) {
785 if (dtrace_probe_lookup(machtrace_id
, NULL
,
786 mach_syscall_name_table
[i
], "entry") != 0) {
790 (void) dtrace_probe_create(machtrace_id
, NULL
, mach_syscall_name_table
[i
],
791 "entry", MACHTRACE_ARTIFICIAL_FRAMES
,
792 (void *)((uintptr_t)SYSTRACE_ENTRY(i
)));
793 (void) dtrace_probe_create(machtrace_id
, NULL
, mach_syscall_name_table
[i
],
794 "return", MACHTRACE_ARTIFICIAL_FRAMES
,
795 (void *)((uintptr_t)SYSTRACE_RETURN(i
)));
797 machtrace_sysent
[i
].stsy_entry
= DTRACE_IDNONE
;
798 machtrace_sysent
[i
].stsy_return
= DTRACE_IDNONE
;
804 machtrace_destroy(void *arg
, dtrace_id_t id
, void *parg
)
806 #pragma unused(arg,id) /* __APPLE__ */
807 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
809 #pragma unused(sysnum) /* __APPLE__ */
812 * There's nothing to do here but assert that we have actually been
815 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
816 ASSERT(machtrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
);
818 ASSERT(machtrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
824 machtrace_enable(void *arg
, dtrace_id_t id
, void *parg
)
826 #pragma unused(arg) /* __APPLE__ */
828 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
829 int enabled
= (machtrace_sysent
[sysnum
].stsy_entry
!= DTRACE_IDNONE
||
830 machtrace_sysent
[sysnum
].stsy_return
!= DTRACE_IDNONE
);
832 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
833 machtrace_sysent
[sysnum
].stsy_entry
= id
;
835 machtrace_sysent
[sysnum
].stsy_return
= id
;
839 ASSERT(mach_trap_table
[sysnum
].mach_trap_function
== (void *)dtrace_machtrace_syscall
);
843 lck_mtx_lock(&dtrace_systrace_lock
);
845 if (mach_trap_table
[sysnum
].mach_trap_function
== machtrace_sysent
[sysnum
].stsy_underlying
) {
846 vm_offset_t dss
= (vm_offset_t
)&dtrace_machtrace_syscall
;
847 ml_nofault_copy((vm_offset_t
)&dss
, (vm_offset_t
)&mach_trap_table
[sysnum
].mach_trap_function
, sizeof(vm_offset_t
));
850 lck_mtx_unlock(&dtrace_systrace_lock
);
857 machtrace_disable(void *arg
, dtrace_id_t id
, void *parg
)
859 #pragma unused(arg,id) /* __APPLE__ */
861 int sysnum
= SYSTRACE_SYSNUM((uintptr_t)parg
);
862 int disable
= (machtrace_sysent
[sysnum
].stsy_entry
== DTRACE_IDNONE
||
863 machtrace_sysent
[sysnum
].stsy_return
== DTRACE_IDNONE
);
866 lck_mtx_lock(&dtrace_systrace_lock
);
868 if (mach_trap_table
[sysnum
].mach_trap_function
== (mach_call_t
)dtrace_machtrace_syscall
) {
869 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
));
871 lck_mtx_unlock(&dtrace_systrace_lock
);
874 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
875 machtrace_sysent
[sysnum
].stsy_entry
= DTRACE_IDNONE
;
877 machtrace_sysent
[sysnum
].stsy_return
= DTRACE_IDNONE
;
881 static dtrace_pattr_t machtrace_attr
= {
882 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
883 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_UNKNOWN
},
884 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
885 { DTRACE_STABILITY_EVOLVING
, DTRACE_STABILITY_EVOLVING
, DTRACE_CLASS_COMMON
},
886 { DTRACE_STABILITY_PRIVATE
, DTRACE_STABILITY_PRIVATE
, DTRACE_CLASS_ISA
},
889 static dtrace_pops_t machtrace_pops
= {
890 .dtps_provide
= machtrace_provide
,
891 .dtps_provide_module
= NULL
,
892 .dtps_enable
= machtrace_enable
,
893 .dtps_disable
= machtrace_disable
,
894 .dtps_suspend
= NULL
,
896 .dtps_getargdesc
= NULL
,
897 .dtps_getargval
= machtrace_getarg
,
898 .dtps_usermode
= NULL
,
899 .dtps_destroy
= machtrace_destroy
903 machtrace_attach(dev_info_t
*devi
)
905 machtrace_probe
= dtrace_probe
;
908 if (ddi_create_minor_node(devi
, "machtrace", S_IFCHR
, 0,
909 DDI_PSEUDO
, 0) == DDI_FAILURE
||
910 dtrace_register("mach_trap", &machtrace_attr
, DTRACE_PRIV_USER
, NULL
,
911 &machtrace_pops
, NULL
, &machtrace_id
) != 0) {
912 machtrace_probe
= (void*)&systrace_stub
;
913 ddi_remove_minor_node(devi
, NULL
);
920 d_open_t _systrace_open
;
923 _systrace_open(dev_t dev
, int flags
, int devtype
, struct proc
*p
)
925 #pragma unused(dev,flags,devtype,p)
929 #define SYSTRACE_MAJOR -24 /* let the kernel pick the device number */
932 * A struct describing which functions will get invoked for certain
935 static struct cdevsw systrace_cdevsw
=
937 _systrace_open
, /* open */
938 eno_opcl
, /* close */
939 eno_rdwrt
, /* read */
940 eno_rdwrt
, /* write */
941 eno_ioctl
, /* ioctl */
942 (stop_fcn_t
*)nulldev
, /* stop */
943 (reset_fcn_t
*)nulldev
, /* reset */
945 eno_select
, /* select */
947 eno_strat
, /* strategy */
953 void systrace_init( void );
956 systrace_init( void )
958 if (dtrace_sdt_probes_restricted()) {
962 int majdevno
= cdevsw_add(SYSTRACE_MAJOR
, &systrace_cdevsw
);
965 printf("systrace_init: failed to allocate a major number!\n");
969 systrace_attach((dev_info_t
*)(uintptr_t)majdevno
);
970 machtrace_attach((dev_info_t
*)(uintptr_t)majdevno
);
972 #undef SYSTRACE_MAJOR
975 systrace_getargval(void *arg
, dtrace_id_t id
, void *parg
, int argno
, int aframes
)
977 #pragma unused(arg,id,parg,aframes) /* __APPLE__ */
979 uint64_t *uargs
= NULL
;
981 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
984 uargs
= uthread
->t_dtrace_syscall_args
;
989 if (argno
< 0 || argno
>= SYSTRACE_NARGS
) {
993 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
995 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
1000 systrace_getargdesc(void *arg
, dtrace_id_t id
, void *parg
,
1001 dtrace_argdesc_t
*desc
)
1003 #pragma unused(arg, id)
1004 int sysnum
= SYSTRACE_SYSNUM(parg
);
1005 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
1006 uint64_t *uargs
= NULL
;
1009 desc
->dtargd_ndx
= DTRACE_ARGNONE
;
1013 uargs
= uthread
->t_dtrace_syscall_args
;
1015 if (SYSTRACE_ISENTRY((uintptr_t)parg
)) {
1016 systrace_entry_setargdesc(sysnum
, desc
->dtargd_ndx
,
1017 desc
->dtargd_native
, sizeof(desc
->dtargd_native
));
1019 systrace_return_setargdesc(sysnum
, desc
->dtargd_ndx
,
1020 desc
->dtargd_native
, sizeof(desc
->dtargd_native
));
1023 if (desc
->dtargd_native
[0] == '\0') {
1024 desc
->dtargd_ndx
= DTRACE_ARGNONE
;
1029 machtrace_getarg(void *arg
, dtrace_id_t id
, void *parg
, int argno
, int aframes
)
1031 #pragma unused(arg,id,parg,aframes) /* __APPLE__ */
1033 syscall_arg_t
*stack
= (syscall_arg_t
*)NULL
;
1035 uthread_t uthread
= (uthread_t
)get_bsdthread_info(current_thread());
1038 stack
= (syscall_arg_t
*)uthread
->t_dtrace_syscall_args
;
1045 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
1046 /* dtrace_probe arguments arg0 .. arg4 are 64bits wide */
1047 val
= (uint64_t)*(stack
+ argno
);
1048 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);