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>
34 #include <sys/malloc.h>
36 #include <sys/systm.h>
38 #include <sys/proc_internal.h>
39 #include <sys/kauth.h>
40 #include <sys/dtrace.h>
41 #include <sys/dtrace_impl.h>
42 #include <libkern/OSAtomic.h>
43 #include <kern/thread_call.h>
44 #include <kern/task.h>
45 #include <kern/sched_prim.h>
46 #include <miscfs/devfs/devfs.h>
47 #include <mach/vm_param.h>
48 #include <machine/cpu_capabilities.h>
50 extern dtrace_id_t dtrace_probeid_error
; /* special ERROR probe */
53 dtrace_probe_error(dtrace_state_t
*state
, dtrace_epid_t epid
, int which
,
54 int fault
, int fltoffs
, uint64_t illval
)
57 * dtrace_getarg() is a lost cause on PPC. For the case of the error probe firing lets
58 * stash away "illval" here, and special-case retrieving it in DIF_VARIABLE_ARG.
60 state
->dts_arg_error_illval
= illval
;
61 dtrace_probe( dtrace_probeid_error
, (uint64_t)(uintptr_t)state
, epid
, which
, fault
, fltoffs
);
65 * Atomicity and synchronization
68 dtrace_membar_producer(void)
70 __asm__
volatile("sync");
74 dtrace_membar_consumer(void)
76 __asm__
volatile("isync");
80 * Interrupt manipulation
81 * XXX dtrace_getipl() can be called from probe context.
86 return (ml_at_interrupt_context() ? 1: 0);
92 typedef void (*broadcastFunc
) (uint32_t);
94 int32_t cpu_broadcast(uint32_t *, broadcastFunc
, uint32_t); /* osfmk/ppc/machine_cpu.h */
96 typedef struct xcArg
{
104 xcRemote( uint32_t foo
)
106 xcArg_t
*pArg
= (xcArg_t
*)foo
;
108 if ( pArg
->cpu
== CPU
->cpu_id
|| pArg
->cpu
== DTRACE_CPUALL
) {
109 (pArg
->f
)(pArg
->arg
);
112 if(!hw_atomic_sub(&(pArg
->waitVar
), 1)) { /* Drop the wait count */
113 thread_wakeup((event_t
)&(pArg
->waitVar
)); /* If we were the last, wake up the signaller */
118 * dtrace_xcall() is not called from probe context.
121 dtrace_xcall(processorid_t cpu
, dtrace_xcall_t f
, void *arg
)
125 /* Talking to ourselves, are we? */
126 if ( cpu
== CPU
->cpu_id
) {
131 if ( cpu
== DTRACE_CPUALL
) {
140 (void)cpu_broadcast(&(xcArg
.waitVar
), xcRemote
, (uint32_t)&xcArg
);
149 return (greg_t
)__builtin_frame_address(0);
153 dtrace_getreg(struct regs
*savearea
, uint_t reg
)
155 ppc_saved_state_t
*regs
= (ppc_saved_state_t
*)savearea
;
156 uint64_t mask
= (_cpu_capabilities
& k64Bit
) ? 0xffffffffffffffffULL
: 0x00000000ffffffffULL
;
158 /* See osfmk/ppc/savearea.h */
159 if (reg
> 68) { /* beyond mmcr2 */
160 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
);
165 /* First 38 registers are saved to 64 bits r0-r31, srr0, srr1, xer, lr, ctr, dar. */
167 return (((uint64_t *)(&(regs
->save_r0
)))[reg
]) & mask
;
169 /* Handle the 32-bit registers */
170 case 38: case 39: case 40: case 41: /* cr, dsisr, exception, vrsave */
171 case 42: case 43: case 44: case 45: /* vscr[4] */
172 case 46: case 47: case 48: case 49: /* fpscrpad, fpscr, save_1d8[2] */
173 case 50: case 51: case 52: case 53: /* save_1E0[8] */
174 case 54: case 55: case 56: case 57:
175 case 58: case 59: case 60: case 61: /* save_pmc[8] */
176 case 62: case 63: case 64: case 65:
177 return (uint64_t)(((unsigned int *)(&(regs
->save_cr
)))[reg
- 38]);
180 return regs
->save_mmcr0
& mask
;
182 return regs
->save_mmcr1
& mask
;
184 return regs
->save_mmcr2
& mask
;
188 #define RETURN_OFFSET 8
189 #define RETURN_OFFSET64 16
190 #define REGPC save_srr0
191 #define REGSP save_r1
194 * XXX dtrace_getustack_common() can be called from probe context.
197 dtrace_getustack_common(uint64_t *pcstack
, int pcstack_limit
, user_addr_t pc
,
201 volatile uint16_t *flags
=
202 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
204 uintptr_t oldcontext
= lwp
->lwp_oldcontext
; /* XXX signal stack crawl*/
208 boolean_t is64Bit
= proc_is64bit(current_proc());
210 ASSERT(pcstack
== NULL
|| pcstack_limit
> 0);
212 #if 0 /* XXX signal stack crawl*/
213 if (p
->p_model
== DATAMODEL_NATIVE
) {
214 s1
= sizeof (struct frame
) + 2 * sizeof (long);
215 s2
= s1
+ sizeof (siginfo_t
);
217 s1
= sizeof (struct frame32
) + 3 * sizeof (int);
218 s2
= s1
+ sizeof (siginfo32_t
);
224 if (pcstack
!= NULL
) {
225 *pcstack
++ = (uint64_t)pc
;
227 if (pcstack_limit
<= 0)
234 #if 0 /* XXX signal stack crawl*/
235 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
236 if (p
->p_model
== DATAMODEL_NATIVE
) {
237 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
238 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
240 sp
= dtrace_fulword(&gregs
[REG_FP
]);
241 pc
= dtrace_fulword(&gregs
[REG_PC
]);
243 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
245 ucontext32_t
*ucp
= (ucontext32_t
*)oldcontext
;
246 greg32_t
*gregs
= ucp
->uc_mcontext
.gregs
;
248 sp
= dtrace_fuword32(&gregs
[EBP
]);
249 pc
= dtrace_fuword32(&gregs
[EIP
]);
251 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
258 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
259 sp
= dtrace_fuword64(sp
);
261 pc
= dtrace_fuword32((sp
+ RETURN_OFFSET
));
262 sp
= dtrace_fuword32(sp
);
271 dtrace_getupcstack(uint64_t *pcstack
, int pcstack_limit
)
273 thread_t thread
= current_thread();
274 ppc_saved_state_t
*regs
;
276 volatile uint16_t *flags
=
277 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
279 boolean_t is64Bit
= proc_is64bit(current_proc());
281 if (*flags
& CPU_DTRACE_FAULT
)
284 if (pcstack_limit
<= 0)
288 * If there's no user context we still need to zero the stack.
293 regs
= (ppc_saved_state_t
*)find_user_regs(thread
);
297 *pcstack
++ = (uint64_t)proc_selfpid();
300 if (pcstack_limit
<= 0)
306 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
307 *pcstack
++ = (uint64_t)pc
;
309 if (pcstack_limit
<= 0)
315 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_USTACK_FP
)) {
317 * If the ustack fp flag is set, the stack frame from sp to
318 * fp contains no valid call information. Start with the fp.
321 sp
= dtrace_fuword64(sp
);
323 sp
= (user_addr_t
)dtrace_fuword32(sp
);
326 n
= dtrace_getustack_common(pcstack
, pcstack_limit
, pc
, sp
);
328 ASSERT(n
<= pcstack_limit
);
334 while (pcstack_limit
-- > 0)
339 dtrace_getustackdepth(void)
341 thread_t thread
= current_thread();
342 ppc_saved_state_t
*regs
;
345 boolean_t is64Bit
= proc_is64bit(current_proc());
350 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
))
353 regs
= (ppc_saved_state_t
*)find_user_regs(thread
);
360 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
365 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_USTACK_FP
)) {
367 * If the ustack fp flag is set, the stack frame from sp to
368 * fp contains no valid call information. Start with the fp.
371 sp
= dtrace_fuword64(sp
);
373 sp
= (user_addr_t
)dtrace_fuword32(sp
);
376 n
+= dtrace_getustack_common(NULL
, 0, pc
, sp
);
382 dtrace_getufpstack(uint64_t *pcstack
, uint64_t *fpstack
, int pcstack_limit
)
384 thread_t thread
= current_thread();
385 ppc_saved_state_t
*regs
;
387 volatile uint16_t *flags
=
388 (volatile uint16_t *)&cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
;
390 uintptr_t oldcontext
;
393 boolean_t is64Bit
= proc_is64bit(current_proc());
395 if (*flags
& CPU_DTRACE_FAULT
)
398 if (pcstack_limit
<= 0)
402 * If there's no user context we still need to zero the stack.
407 regs
= (ppc_saved_state_t
*)find_user_regs(thread
);
411 *pcstack
++ = (uint64_t)proc_selfpid();
414 if (pcstack_limit
<= 0)
420 #if 0 /* XXX signal stack crawl*/
421 oldcontext
= lwp
->lwp_oldcontext
;
423 if (p
->p_model
== DATAMODEL_NATIVE
) {
424 s1
= sizeof (struct frame
) + 2 * sizeof (long);
425 s2
= s1
+ sizeof (siginfo_t
);
427 s1
= sizeof (struct frame32
) + 3 * sizeof (int);
428 s2
= s1
+ sizeof (siginfo32_t
);
432 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) {
433 *pcstack
++ = (uint64_t)pc
;
436 if (pcstack_limit
<= 0)
440 * XXX This is wrong, but we do not yet support stack helpers.
443 pc
= dtrace_fuword64(sp
);
445 pc
= dtrace_fuword32(sp
);
449 *pcstack
++ = (uint64_t)pc
;
452 if (pcstack_limit
<= 0)
458 #if 0 /* XXX signal stack crawl*/
459 if (oldcontext
== sp
+ s1
|| oldcontext
== sp
+ s2
) {
460 if (p
->p_model
== DATAMODEL_NATIVE
) {
461 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
462 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
464 sp
= dtrace_fulword(&gregs
[REG_FP
]);
465 pc
= dtrace_fulword(&gregs
[REG_PC
]);
467 oldcontext
= dtrace_fulword(&ucp
->uc_link
);
469 ucontext_t
*ucp
= (ucontext_t
*)oldcontext
;
470 greg_t
*gregs
= ucp
->uc_mcontext
.gregs
;
472 sp
= dtrace_fuword32(&gregs
[EBP
]);
473 pc
= dtrace_fuword32(&gregs
[EIP
]);
475 oldcontext
= dtrace_fuword32(&ucp
->uc_link
);
482 pc
= dtrace_fuword64((sp
+ RETURN_OFFSET64
));
483 sp
= dtrace_fuword64(sp
);
485 pc
= dtrace_fuword32((sp
+ RETURN_OFFSET
));
486 sp
= dtrace_fuword32(sp
);
492 while (pcstack_limit
-- > 0)
497 dtrace_getpcstack(pc_t
*pcstack
, int pcstack_limit
, int aframes
,
500 struct frame
*fp
= (struct frame
*)dtrace_getfp();
501 struct frame
*nextfp
, *minfp
, *stacktop
;
505 uintptr_t caller
= CPU
->cpu_dtrace_caller
;
508 if ((on_intr
= CPU_ON_INTR(CPU
)) != 0)
509 stacktop
= (struct frame
*)dtrace_get_cpu_int_stack_top();
511 stacktop
= (struct frame
*)(dtrace_get_kernel_stack(current_thread()) + KERNEL_STACK_SIZE
);
517 if (intrpc
!= NULL
&& depth
< pcstack_limit
)
518 pcstack
[depth
++] = (pc_t
)intrpc
;
520 while (depth
< pcstack_limit
) {
521 nextfp
= *(struct frame
**)fp
;
522 pc
= *(uintptr_t *)(((uint32_t)fp
) + RETURN_OFFSET
);
524 if (nextfp
<= minfp
|| nextfp
>= stacktop
) {
527 * Hop from interrupt stack to thread stack.
529 vm_offset_t kstack_base
= dtrace_get_kernel_stack(current_thread());
531 minfp
= (struct frame
*)kstack_base
;
532 stacktop
= (struct frame
*)(kstack_base
+ KERNEL_STACK_SIZE
);
538 * This is the last frame we can process; indicate
539 * that we should return after processing this frame.
545 if (--aframes
== 0 && caller
!= 0) {
547 * We've just run out of artificial frames,
548 * and we have a valid caller -- fill it in
551 ASSERT(depth
< pcstack_limit
);
552 pcstack
[depth
++] = (pc_t
)caller
;
556 if (depth
< pcstack_limit
)
557 pcstack
[depth
++] = (pc_t
)pc
;
561 while (depth
< pcstack_limit
)
562 pcstack
[depth
++] = 0;
572 dtrace_getarg(int arg
, int aframes
)
574 #pragma unused(arg,aframes)
575 return 0xfeedfacedeafbeadLL
; /* XXX Only called for arg >= 5 */
583 dtrace_toxic_ranges(void (*func
)(uintptr_t base
, uintptr_t limit
))
586 * "base" is the smallest toxic address in the range, "limit" is the first
587 * VALID address greater than "base".
589 func(0x0, VM_MIN_KERNEL_ADDRESS
);
590 func(VM_MAX_KERNEL_ADDRESS
+ 1, ~(uintptr_t)0);
593 extern void *mapping_phys_lookup(ppnum_t
, unsigned int *);
596 dtxnu_is_RAM_page(ppnum_t pn
)
599 return (NULL
== mapping_phys_lookup(pn
, &ignore
)) ? FALSE
: TRUE
;