2 * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #define MACH__POSIX_C_SOURCE_PRIVATE 1 /* pulls in suitable savearea from mach/ppc/thread_status.h */
30 #include <kern/thread.h>
31 #include <mach/thread_status.h>
33 typedef x86_saved_state_t savearea_t
;
37 #include <sys/malloc.h>
39 #include <sys/systm.h>
41 #include <sys/proc_internal.h>
42 #include <sys/kauth.h>
43 #include <sys/dtrace.h>
44 #include <sys/dtrace_impl.h>
45 #include <libkern/OSAtomic.h>
46 #include <kern/thread_call.h>
47 #include <kern/task.h>
48 #include <kern/sched_prim.h>
49 #include <miscfs/devfs/devfs.h>
50 #include <mach/vm_param.h>
52 extern dtrace_id_t dtrace_probeid_error
; /* special ERROR probe */
55 dtrace_probe_error(dtrace_state_t
*state
, dtrace_epid_t epid
, int which
,
56 int fault
, int fltoffs
, uint64_t illval
)
59 * For the case of the error probe firing lets
60 * stash away "illval" here, and special-case retrieving it in DIF_VARIABLE_ARG.
62 state
->dts_arg_error_illval
= illval
;
63 dtrace_probe( dtrace_probeid_error
, (uint64_t)(uintptr_t)state
, epid
, which
, fault
, fltoffs
);
67 * Atomicity and synchronization
70 dtrace_membar_producer(void)
72 __asm__
volatile("sfence");
76 dtrace_membar_consumer(void)
78 __asm__
volatile("lfence");
82 * Interrupt manipulation
83 * XXX dtrace_getipl() can be called from probe context.
89 * XXX Drat, get_interrupt_level is MACH_KERNEL_PRIVATE
90 * in osfmk/kern/cpu_data.h
92 /* return get_interrupt_level(); */
93 return (ml_at_interrupt_context() ? 1: 0);
100 extern void mp_broadcast(
101 void (*action_func
)(void *),
104 typedef struct xcArg
{
111 xcRemote( void *foo
)
113 xcArg_t
*pArg
= (xcArg_t
*)foo
;
115 if ( pArg
->cpu
== CPU
->cpu_id
|| pArg
->cpu
== DTRACE_CPUALL
) {
116 (pArg
->f
)(pArg
->arg
);
121 * dtrace_xcall() is not called from probe context.
124 dtrace_xcall(processorid_t cpu
, dtrace_xcall_t f
, void *arg
)
132 mp_broadcast( xcRemote
, (void *)&xcArg
);
141 return (greg_t
)__builtin_frame_address(0);
145 dtrace_getreg(struct regs
*savearea
, uint_t reg
)
147 boolean_t is64Bit
= proc_is64bit(current_proc());
148 x86_saved_state_t
*regs
= (x86_saved_state_t
*)savearea
;
151 /* beyond register SS */
152 if (reg
> x86_SAVED_STATE64_COUNT
- 1) {
153 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
156 return ((uint64_t *)(&(regs
->ss_64
.gs
)))[reg
];
158 /* beyond register SS */
159 if (reg
> x86_SAVED_STATE32_COUNT
- 1) {
160 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
163 return (uint64_t)((unsigned int *)(&(regs
->ss_32
.gs
)))[reg
];
168 #define RETURN_OFFSET 4
169 #define RETURN_OFFSET64 8
172 dtrace_getustack_common(uint64_t *pcstack
, int pcstack_limit
, user_addr_t pc
,
176 volatile uint16_t *flags
=
177 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
179 uintptr_t oldcontext
= lwp
->lwp_oldcontext
; /* XXX signal stack crawl */
183 boolean_t is64Bit
= proc_is64bit(current_proc());
185 ASSERT(pcstack
== NULL
|| pcstack_limit
> 0);
187 #if 0 /* XXX signal stack crawl */
188 if (p
->p_model
== DATAMODEL_NATIVE
) {
189 s1
= sizeof (struct frame
) + 2 * sizeof (long);
190 s2
= s1
+ sizeof (siginfo_t
);
192 s1
= sizeof (struct frame32
) + 3 * sizeof (int);
193 s2
= s1
+ sizeof (siginfo32_t
);
199 if (pcstack
!= NULL
) {
200 *pcstack
++ = (uint64_t)pc
;
202 if (pcstack_limit
<= 0)
209 #if 0 /* XXX signal stack crawl */
210 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
211 if (p
->p_model
== DATAMODEL_NATIVE
) {
212 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
213 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
215 sp
= dtrace_fulword(&gregs
[REG_FP
]);
216 pc
= dtrace_fulword(&gregs
[REG_PC
]);
218 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
220 ucontext32_t
*ucp
= (ucontext32_t
*)oldcontext
;
221 greg32_t
*gregs
= ucp
->uc_mcontext
.gregs
;
223 sp
= dtrace_fuword32(&gregs
[EBP
]);
224 pc
= dtrace_fuword32(&gregs
[EIP
]);
226 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
233 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
234 sp
= dtrace_fuword64(sp
);
236 pc
= dtrace_fuword32((sp
+ RETURN_OFFSET
));
237 sp
= dtrace_fuword32(sp
);
243 * This is totally bogus: if we faulted, we're going to clear
244 * the fault and break. This is to deal with the apparently
245 * broken Java stacks on x86.
247 if (*flags
& CPU_DTRACE_FAULT
) {
248 *flags
&= ~CPU_DTRACE_FAULT
;
258 dtrace_getupcstack(uint64_t *pcstack
, int pcstack_limit
)
260 thread_t thread
= current_thread();
261 x86_saved_state_t
*regs
;
262 user_addr_t pc
, sp
, fp
;
263 volatile uint16_t *flags
=
264 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
266 boolean_t is64Bit
= proc_is64bit(current_proc());
268 if (*flags
& CPU_DTRACE_FAULT
)
271 if (pcstack_limit
<= 0)
275 * If there's no user context we still need to zero the stack.
280 regs
= (x86_saved_state_t
*)find_user_regs(thread
);
284 *pcstack
++ = (uint64_t)proc_selfpid();
287 if (pcstack_limit
<= 0)
291 pc
= regs
->ss_64
.isf
.rip
;
292 sp
= regs
->ss_64
.isf
.rsp
;
293 fp
= regs
->ss_64
.rbp
;
295 pc
= regs
->ss_32
.eip
;
296 sp
= regs
->ss_32
.uesp
;
297 fp
= regs
->ss_32
.ebp
;
300 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
301 *pcstack
++ = (uint64_t)pc
;
303 if (pcstack_limit
<= 0)
307 pc
= dtrace_fuword64(sp
);
309 pc
= dtrace_fuword32(sp
);
313 * Note that unlike ppc, the x86 code does not use
314 * CPU_DTRACE_USTACK_FP. This is because x86 always
315 * traces from the fp, even in syscall/profile/fbt
318 n
= dtrace_getustack_common(pcstack
, pcstack_limit
, pc
, fp
);
320 ASSERT(n
<= pcstack_limit
);
326 while (pcstack_limit
-- > 0)
331 dtrace_getustackdepth(void)
333 thread_t thread
= current_thread();
334 x86_saved_state_t
*regs
;
335 user_addr_t pc
, sp
, fp
;
337 boolean_t is64Bit
= proc_is64bit(current_proc());
342 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
))
345 regs
= (x86_saved_state_t
*)find_user_regs(thread
);
350 pc
= regs
->ss_64
.isf
.rip
;
351 sp
= regs
->ss_64
.isf
.rsp
;
352 fp
= regs
->ss_64
.rbp
;
354 pc
= regs
->ss_32
.eip
;
355 sp
= regs
->ss_32
.uesp
;
356 fp
= regs
->ss_32
.ebp
;
359 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
363 pc
= dtrace_fuword64(sp
);
365 pc
= dtrace_fuword32(sp
);
369 * Note that unlike ppc, the x86 code does not use
370 * CPU_DTRACE_USTACK_FP. This is because x86 always
371 * traces from the fp, even in syscall/profile/fbt
375 n
+= dtrace_getustack_common(NULL
, 0, pc
, fp
);
381 dtrace_getufpstack(uint64_t *pcstack
, uint64_t *fpstack
, int pcstack_limit
)
383 thread_t thread
= current_thread();
386 volatile uint16_t *flags
=
387 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
389 uintptr_t oldcontext
;
392 boolean_t is64Bit
= proc_is64bit(current_proc());
394 if (*flags
& CPU_DTRACE_FAULT
)
397 if (pcstack_limit
<= 0)
401 * If there's no user context we still need to zero the stack.
406 regs
= (savearea_t
*)find_user_regs(thread
);
410 *pcstack
++ = (uint64_t)proc_selfpid();
413 if (pcstack_limit
<= 0)
416 pc
= regs
->ss_32
.eip
;
417 sp
= regs
->ss_32
.ebp
;
419 #if 0 /* XXX signal stack crawl */
420 oldcontext
= lwp
->lwp_oldcontext
;
422 if (p
->p_model
== DATAMODEL_NATIVE
) {
423 s1
= sizeof (struct frame
) + 2 * sizeof (long);
424 s2
= s1
+ sizeof (siginfo_t
);
426 s1
= sizeof (struct frame32
) + 3 * sizeof (int);
427 s2
= s1
+ sizeof (siginfo32_t
);
431 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
432 *pcstack
++ = (uint64_t)pc
;
435 if (pcstack_limit
<= 0)
439 pc
= dtrace_fuword64(sp
);
441 pc
= dtrace_fuword32(sp
);
445 *pcstack
++ = (uint64_t)pc
;
448 if (pcstack_limit
<= 0)
454 #if 0 /* XXX signal stack crawl */
455 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
456 if (p
->p_model
== DATAMODEL_NATIVE
) {
457 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
458 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
460 sp
= dtrace_fulword(&gregs
[REG_FP
]);
461 pc
= dtrace_fulword(&gregs
[REG_PC
]);
463 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
465 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
466 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
468 sp
= dtrace_fuword32(&gregs
[EBP
]);
469 pc
= dtrace_fuword32(&gregs
[EIP
]);
471 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
478 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
479 sp
= dtrace_fuword64(sp
);
481 pc
= dtrace_fuword32((sp
+ RETURN_OFFSET
));
482 sp
= dtrace_fuword32(sp
);
488 * This is totally bogus: if we faulted, we're going to clear
489 * the fault and break. This is to deal with the apparently
490 * broken Java stacks on x86.
492 if (*flags
& CPU_DTRACE_FAULT
) {
493 *flags
&= ~CPU_DTRACE_FAULT
;
500 while (pcstack_limit
-- > 0)
505 dtrace_getpcstack(pc_t
*pcstack
, int pcstack_limit
, int aframes
,
508 struct frame
*fp
= (struct frame
*)dtrace_getfp();
509 struct frame
*nextfp
, *minfp
, *stacktop
;
513 uintptr_t caller
= CPU
->cpu_dtrace_caller
;
516 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
517 stacktop
= (struct frame
*)dtrace_get_cpu_int_stack_top();
519 stacktop
= (struct frame
*)(dtrace_get_kernel_stack(current_thread()) + KERNEL_STACK_SIZE
);
525 if (intrpc
!= NULL
&& depth
< pcstack_limit
)
526 pcstack
[depth
++] = (pc_t
)intrpc
;
528 while (depth
< pcstack_limit
) {
529 nextfp
= *(struct frame
**)fp
;
530 pc
= *(uintptr_t *)(((uint32_t)fp
) + RETURN_OFFSET
);
532 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
535 * Hop from interrupt stack to thread stack.
537 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
539 minfp
= (struct frame
*)kstack_base
;
540 stacktop
= (struct frame
*)(kstack_base
+ KERNEL_STACK_SIZE
);
546 * This is the last frame we can process; indicate
547 * that we should return after processing this frame.
553 if (--aframes
== 0 && caller
!= 0) {
555 * We've just run out of artificial frames,
556 * and we have a valid caller -- fill it in
559 ASSERT(depth
< pcstack_limit
);
560 pcstack
[depth
++] = (pc_t
)caller
;
564 if (depth
< pcstack_limit
)
565 pcstack
[depth
++] = (pc_t
)pc
;
569 while (depth
< pcstack_limit
)
570 pcstack
[depth
++] = 0;
580 struct frame
*backchain
;
585 dtrace_getarg(int arg
, int aframes
)
588 struct frame
*fp
= (struct frame
*)dtrace_getfp();
593 for (i
= 1; i
<= aframes
; i
++) {
597 if (pc
== (uintptr_t)dtrace_invop_callsite
) {
599 * If we pass through the invalid op handler, we will
600 * use the pointer that it passed to the stack as the
601 * second argument to dtrace_invop() as the pointer to
602 * the frame we're hunting for.
605 stack
= (uintptr_t *)&fp
[1]; /* Find marshalled arguments */
606 fp
= (struct frame
*)stack
[1]; /* Grab *second* argument */
607 stack
= (uintptr_t *)&fp
[1]; /* Find marshalled arguments */
608 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
609 val
= (uint64_t)(stack
[arg
]);
610 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
616 * Arrive here when provider has called dtrace_probe directly.
618 stack
= (uintptr_t *)&fp
[1]; /* Find marshalled arguments */
619 stack
++; /* Advance past probeID */
621 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
);
622 val
= *(((uint64_t *)stack
) + arg
); /* dtrace_probe arguments arg0 .. arg4 are 64bits wide */
623 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
);
632 dtrace_toxic_ranges(void (*func
)(uintptr_t base
, uintptr_t limit
))
635 * "base" is the smallest toxic address in the range, "limit" is the first
636 * VALID address greater than "base".
638 func(0x0, VM_MIN_KERNEL_ADDRESS
);
639 func(VM_MAX_KERNEL_ADDRESS
+ 1, ~(uintptr_t)0);
642 extern boolean_t
pmap_valid_page(ppnum_t pn
);
645 dtxnu_is_RAM_page(ppnum_t pn
)
647 return pmap_valid_page(pn
);