#define MACH__POSIX_C_SOURCE_PRIVATE 1
#include <kern/thread.h>
#include <mach/thread_status.h>
#include <stdarg.h>
#include <string.h>
#include <sys/malloc.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/dtrace.h>
#include <sys/dtrace_impl.h>
#include <libkern/OSAtomic.h>
#include <kern/thread_call.h>
#include <kern/task.h>
#include <kern/sched_prim.h>
#include <miscfs/devfs/devfs.h>
#include <mach/vm_param.h>
#include <machine/cpu_capabilities.h>
extern dtrace_id_t dtrace_probeid_error;
void
dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
int fault, int fltoffs, uint64_t illval)
{
state->dts_arg_error_illval = illval;
dtrace_probe( dtrace_probeid_error, (uint64_t)(uintptr_t)state, epid, which, fault, fltoffs );
}
void
dtrace_membar_producer(void)
{
__asm__ volatile("sync");
}
void
dtrace_membar_consumer(void)
{
__asm__ volatile("isync");
}
int
dtrace_getipl(void)
{
return (ml_at_interrupt_context() ? 1: 0);
}
typedef void (*broadcastFunc) (uint32_t);
int32_t cpu_broadcast(uint32_t *, broadcastFunc, uint32_t);
typedef struct xcArg {
processorid_t cpu;
dtrace_xcall_t f;
void *arg;
uint32_t waitVar;
} xcArg_t;
static void
xcRemote( uint32_t foo )
{
xcArg_t *pArg = (xcArg_t *)foo;
if ( pArg->cpu == CPU->cpu_id || pArg->cpu == DTRACE_CPUALL ) {
(pArg->f)(pArg->arg);
}
if(!hw_atomic_sub(&(pArg->waitVar), 1)) {
thread_wakeup((event_t)&(pArg->waitVar));
}
}
void
dtrace_xcall(processorid_t cpu, dtrace_xcall_t f, void *arg)
{
xcArg_t xcArg;
if ( cpu == CPU->cpu_id ) {
(*f)(arg);
return;
}
if ( cpu == DTRACE_CPUALL ) {
(*f)(arg);
}
xcArg.cpu = cpu;
xcArg.f = f;
xcArg.arg = arg;
xcArg.waitVar = 0;
(void)cpu_broadcast(&(xcArg.waitVar), xcRemote, (uint32_t)&xcArg);
}
extern greg_t
dtrace_getfp(void)
{
return (greg_t)__builtin_frame_address(0);
}
uint64_t
dtrace_getreg(struct regs *savearea, uint_t reg)
{
ppc_saved_state_t *regs = (ppc_saved_state_t *)savearea;
uint64_t mask = (_cpu_capabilities & k64Bit) ? 0xffffffffffffffffULL : 0x00000000ffffffffULL;
if (reg > 68) {
DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
return (0);
}
switch (reg) {
default:
return (((uint64_t *)(&(regs->save_r0)))[reg]) & mask;
case 38: case 39: case 40: case 41:
case 42: case 43: case 44: case 45:
case 46: case 47: case 48: case 49:
case 50: case 51: case 52: case 53:
case 54: case 55: case 56: case 57:
case 58: case 59: case 60: case 61:
case 62: case 63: case 64: case 65:
return (uint64_t)(((unsigned int *)(&(regs->save_cr)))[reg - 38]);
case 66:
return regs->save_mmcr0 & mask;
case 67:
return regs->save_mmcr1 & mask;
case 68:
return regs->save_mmcr2 & mask;
}
}
#define RETURN_OFFSET 8
#define RETURN_OFFSET64 16
#define REGPC save_srr0
#define REGSP save_r1
static int
dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, user_addr_t pc,
user_addr_t sp)
{
#if 0
volatile uint16_t *flags =
(volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
uintptr_t oldcontext = lwp->lwp_oldcontext;
size_t s1, s2;
#endif
int ret = 0;
boolean_t is64Bit = proc_is64bit(current_proc());
ASSERT(pcstack == NULL || pcstack_limit > 0);
#if 0
if (p->p_model == DATAMODEL_NATIVE) {
s1 = sizeof (struct frame) + 2 * sizeof (long);
s2 = s1 + sizeof (siginfo_t);
} else {
s1 = sizeof (struct frame32) + 3 * sizeof (int);
s2 = s1 + sizeof (siginfo32_t);
}
#endif
while (pc != 0) {
ret++;
if (pcstack != NULL) {
*pcstack++ = (uint64_t)pc;
pcstack_limit--;
if (pcstack_limit <= 0)
break;
}
if (sp == 0)
break;
#if 0
if (oldcontext == sp + s1 || oldcontext == sp + s2) {
if (p->p_model == DATAMODEL_NATIVE) {
ucontext_t *ucp = (ucontext_t *)oldcontext;
greg_t *gregs = ucp->uc_mcontext.gregs;
sp = dtrace_fulword(&gregs[REG_FP]);
pc = dtrace_fulword(&gregs[REG_PC]);
oldcontext = dtrace_fulword(&ucp->uc_link);
} else {
ucontext32_t *ucp = (ucontext32_t *)oldcontext;
greg32_t *gregs = ucp->uc_mcontext.gregs;
sp = dtrace_fuword32(&gregs[EBP]);
pc = dtrace_fuword32(&gregs[EIP]);
oldcontext = dtrace_fuword32(&ucp->uc_link);
}
}
else
#endif
{
if (is64Bit) {
pc = dtrace_fuword64((sp + RETURN_OFFSET64));
sp = dtrace_fuword64(sp);
} else {
pc = dtrace_fuword32((sp + RETURN_OFFSET));
sp = dtrace_fuword32(sp);
}
}
}
return (ret);
}
void
dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
{
thread_t thread = current_thread();
ppc_saved_state_t *regs;
user_addr_t pc, sp;
volatile uint16_t *flags =
(volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
int n;
boolean_t is64Bit = proc_is64bit(current_proc());
if (*flags & CPU_DTRACE_FAULT)
return;
if (pcstack_limit <= 0)
return;
if (thread == NULL)
goto zero;
regs = (ppc_saved_state_t *)find_user_regs(thread);
if (regs == NULL)
goto zero;
*pcstack++ = (uint64_t)proc_selfpid();
pcstack_limit--;
if (pcstack_limit <= 0)
return;
pc = regs->REGPC;
sp = regs->REGSP;
if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
*pcstack++ = (uint64_t)pc;
pcstack_limit--;
if (pcstack_limit <= 0)
return;
pc = regs->save_lr;
}
if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_USTACK_FP)) {
if (is64Bit)
sp = dtrace_fuword64(sp);
else
sp = (user_addr_t)dtrace_fuword32(sp);
}
n = dtrace_getustack_common(pcstack, pcstack_limit, pc, sp);
ASSERT(n >= 0);
ASSERT(n <= pcstack_limit);
pcstack += n;
pcstack_limit -= n;
zero:
while (pcstack_limit-- > 0)
*pcstack++ = 0;
}
int
dtrace_getustackdepth(void)
{
thread_t thread = current_thread();
ppc_saved_state_t *regs;
user_addr_t pc, sp;
int n = 0;
boolean_t is64Bit = proc_is64bit(current_proc());
if (thread == NULL)
return 0;
if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
return (-1);
regs = (ppc_saved_state_t *)find_user_regs(thread);
if (regs == NULL)
return 0;
pc = regs->REGPC;
sp = regs->REGSP;
if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
n++;
pc = regs->save_lr;
}
if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_USTACK_FP)) {
if (is64Bit)
sp = dtrace_fuword64(sp);
else
sp = (user_addr_t)dtrace_fuword32(sp);
}
n += dtrace_getustack_common(NULL, 0, pc, sp);
return (n);
}
void
dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
{
thread_t thread = current_thread();
ppc_saved_state_t *regs;
user_addr_t pc, sp;
volatile uint16_t *flags =
(volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
#if 0
uintptr_t oldcontext;
size_t s1, s2;
#endif
boolean_t is64Bit = proc_is64bit(current_proc());
if (*flags & CPU_DTRACE_FAULT)
return;
if (pcstack_limit <= 0)
return;
if (thread == NULL)
goto zero;
regs = (ppc_saved_state_t *)find_user_regs(thread);
if (regs == NULL)
goto zero;
*pcstack++ = (uint64_t)proc_selfpid();
pcstack_limit--;
if (pcstack_limit <= 0)
return;
pc = regs->REGPC;
sp = regs->REGSP;
#if 0
oldcontext = lwp->lwp_oldcontext;
if (p->p_model == DATAMODEL_NATIVE) {
s1 = sizeof (struct frame) + 2 * sizeof (long);
s2 = s1 + sizeof (siginfo_t);
} else {
s1 = sizeof (struct frame32) + 3 * sizeof (int);
s2 = s1 + sizeof (siginfo32_t);
}
#endif
if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
*pcstack++ = (uint64_t)pc;
*fpstack++ = 0;
pcstack_limit--;
if (pcstack_limit <= 0)
return;
if (is64Bit)
pc = dtrace_fuword64(sp);
else
pc = dtrace_fuword32(sp);
}
while (pc != 0) {
*pcstack++ = (uint64_t)pc;
*fpstack++ = sp;
pcstack_limit--;
if (pcstack_limit <= 0)
break;
if (sp == 0)
break;
#if 0
if (oldcontext == sp + s1 || oldcontext == sp + s2) {
if (p->p_model == DATAMODEL_NATIVE) {
ucontext_t *ucp = (ucontext_t *)oldcontext;
greg_t *gregs = ucp->uc_mcontext.gregs;
sp = dtrace_fulword(&gregs[REG_FP]);
pc = dtrace_fulword(&gregs[REG_PC]);
oldcontext = dtrace_fulword(&ucp->uc_link);
} else {
ucontext_t *ucp = (ucontext_t *)oldcontext;
greg_t *gregs = ucp->uc_mcontext.gregs;
sp = dtrace_fuword32(&gregs[EBP]);
pc = dtrace_fuword32(&gregs[EIP]);
oldcontext = dtrace_fuword32(&ucp->uc_link);
}
}
else
#endif
{
if (is64Bit) {
pc = dtrace_fuword64((sp + RETURN_OFFSET64));
sp = dtrace_fuword64(sp);
} else {
pc = dtrace_fuword32((sp + RETURN_OFFSET));
sp = dtrace_fuword32(sp);
}
}
}
zero:
while (pcstack_limit-- > 0)
*pcstack++ = 0;
}
void
dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes,
uint32_t *intrpc)
{
struct frame *fp = (struct frame *)dtrace_getfp();
struct frame *nextfp, *minfp, *stacktop;
int depth = 0;
int last = 0;
uintptr_t pc;
uintptr_t caller = CPU->cpu_dtrace_caller;
int on_intr;
if ((on_intr = CPU_ON_INTR(CPU)) != 0)
stacktop = (struct frame *)dtrace_get_cpu_int_stack_top();
else
stacktop = (struct frame *)(dtrace_get_kernel_stack(current_thread()) + KERNEL_STACK_SIZE);
minfp = fp;
aframes++;
if (intrpc != NULL && depth < pcstack_limit)
pcstack[depth++] = (pc_t)intrpc;
while (depth < pcstack_limit) {
nextfp = *(struct frame **)fp;
pc = *(uintptr_t *)(((uint32_t)fp) + RETURN_OFFSET);
if (nextfp <= minfp || nextfp >= stacktop) {
if (on_intr) {
vm_offset_t kstack_base = dtrace_get_kernel_stack(current_thread());
minfp = (struct frame *)kstack_base;
stacktop = (struct frame *)(kstack_base + KERNEL_STACK_SIZE);
on_intr = 0;
continue;
}
last = 1;
}
if (aframes > 0) {
if (--aframes == 0 && caller != 0) {
ASSERT(depth < pcstack_limit);
pcstack[depth++] = (pc_t)caller;
caller = 0;
}
} else {
if (depth < pcstack_limit)
pcstack[depth++] = (pc_t)pc;
}
if (last) {
while (depth < pcstack_limit)
pcstack[depth++] = 0;
return;
}
fp = nextfp;
minfp = fp;
}
}
uint64_t
dtrace_getarg(int arg, int aframes)
{
#pragma unused(arg,aframes)
return 0xfeedfacedeafbeadLL;
}
void
dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
{
func(0x0, VM_MIN_KERNEL_ADDRESS);
func(VM_MAX_KERNEL_ADDRESS + 1, ~(uintptr_t)0);
}
extern void *mapping_phys_lookup(ppnum_t, unsigned int *);
boolean_t
dtxnu_is_RAM_page(ppnum_t pn)
{
unsigned int ignore;
return (NULL == mapping_phys_lookup(pn, &ignore)) ? FALSE : TRUE;
}