2  * Copyright (c) 2005-2018 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 #include <arm/caches_internal.h> 
  30 #include <arm/proc_reg.h> 
  32 #include <kern/thread.h> 
  33 #include <mach/thread_status.h> 
  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 <machine/atomic.h> 
  46 #include <kern/simple_lock.h> 
  47 #include <kern/sched_prim.h>            /* for thread_wakeup() */ 
  48 #include <kern/thread_call.h> 
  49 #include <kern/task.h> 
  50 #include <miscfs/devfs/devfs.h> 
  51 #include <mach/vm_param.h> 
  53 extern struct arm_saved_state 
*find_kern_regs(thread_t
); 
  55 extern dtrace_id_t      dtrace_probeid_error
;   /* special ERROR probe */ 
  56 typedef arm_saved_state_t savearea_t
; 
  58 extern lck_attr_t       
*dtrace_lck_attr
; 
  59 extern lck_grp_t        
*dtrace_lck_grp
; 
  61 int dtrace_arm_condition_true(int condition
, int cpsr
); 
  64  * Atomicity and synchronization 
  67 dtrace_membar_producer(void) 
  70         __asm__ 
volatile ("dmb ish" : : : "memory"); 
  72         __asm__ 
volatile ("nop" : : : "memory"); 
  77 dtrace_membar_consumer(void) 
  80         __asm__ 
volatile ("dmb ish" : : : "memory"); 
  82         __asm__ 
volatile ("nop" : : : "memory"); 
  87  * Interrupt manipulation 
  88  * XXX dtrace_getipl() can be called from probe context. 
  94          * XXX Drat, get_interrupt_level is MACH_KERNEL_PRIVATE 
  95          * in osfmk/kern/cpu_data.h 
  97         /* return get_interrupt_level(); */ 
  98         return ml_at_interrupt_context() ? 1 : 0; 
 106 decl_lck_mtx_data(static, dt_xc_lock
); 
 107 static uint32_t dt_xc_sync
; 
 109 typedef struct xcArg 
{ 
 118         xcArg_t 
*pArg 
= (xcArg_t 
*) foo
; 
 120         if (pArg
->cpu 
== CPU
->cpu_id 
|| pArg
->cpu 
== DTRACE_CPUALL
) { 
 121                 (pArg
->f
)(pArg
->arg
); 
 124         if (os_atomic_dec(&dt_xc_sync
, relaxed
) == 0) { 
 125                 thread_wakeup((event_t
) &dt_xc_sync
); 
 131  * dtrace_xcall() is not called from probe context. 
 134 dtrace_xcall(processorid_t cpu
, dtrace_xcall_t f
, void *arg
) 
 137         /* Only one dtrace_xcall in flight allowed */ 
 138         lck_mtx_lock(&dt_xc_lock
); 
 146         cpu_broadcast_xcall(&dt_xc_sync
, TRUE
, xcRemote
, (void*) &xcArg
); 
 148         lck_mtx_unlock(&dt_xc_lock
); 
 152         /* On uniprocessor systems, the cpu should always be either ourselves or all */ 
 153         ASSERT(cpu 
== CPU
->cpu_id 
|| cpu 
== DTRACE_CPUALL
); 
 164 dtrace_isa_init(void) 
 167         lck_mtx_init(&dt_xc_lock
, dtrace_lck_grp
, dtrace_lck_attr
); 
 176 dtrace_getreg(struct regs 
* savearea
, uint_t reg
) 
 178         struct arm_saved_state 
*regs 
= (struct arm_saved_state 
*) savearea
; 
 180                 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
); 
 183         /* beyond register limit? */ 
 184         if (reg 
> ARM_SAVED_STATE32_COUNT 
- 1) { 
 185                 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP
); 
 189         return (uint64_t) ((unsigned int *) (&(regs
->r
)))[reg
]; 
 192 #define RETURN_OFFSET 4 
 195 dtrace_getustack_common(uint64_t * pcstack
, int pcstack_limit
, user_addr_t pc
, 
 200         ASSERT(pcstack 
== NULL 
|| pcstack_limit 
> 0); 
 204                 if (pcstack 
!= NULL
) { 
 205                         *pcstack
++ = (uint64_t) pc
; 
 207                         if (pcstack_limit 
<= 0) { 
 216                 pc 
= dtrace_fuword32((sp 
+ RETURN_OFFSET
)); 
 217                 sp 
= dtrace_fuword32(sp
); 
 224 dtrace_getupcstack(uint64_t * pcstack
, int pcstack_limit
) 
 226         thread_t        thread 
= current_thread(); 
 229         volatile uint16_t *flags 
= (volatile uint16_t *) &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
; 
 232         if (*flags 
& CPU_DTRACE_FAULT
) { 
 236         if (pcstack_limit 
<= 0) { 
 241          * If there's no user context we still need to zero the stack. 
 243         if (thread 
== NULL
) { 
 247         regs 
= (savearea_t 
*) find_user_regs(thread
); 
 252         *pcstack
++ = (uint64_t)dtrace_proc_selfpid(); 
 255         if (pcstack_limit 
<= 0) { 
 262         if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) { 
 263                 *pcstack
++ = (uint64_t) pc
; 
 265                 if (pcstack_limit 
<= 0) { 
 272         n 
= dtrace_getustack_common(pcstack
, pcstack_limit
, pc
, regs
->r
[7]); 
 275         ASSERT(n 
<= pcstack_limit
); 
 281         while (pcstack_limit
-- > 0) { 
 287 dtrace_getustackdepth(void) 
 289         thread_t        thread 
= current_thread(); 
 294         if (thread 
== NULL
) { 
 298         if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT
)) { 
 302         regs 
= (savearea_t 
*) find_user_regs(thread
); 
 310         if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) { 
 316          * Note that unlike ppc, the arm code does not use 
 317          * CPU_DTRACE_USTACK_FP. This is because arm always 
 318          * traces from the sp, even in syscall/profile/fbt 
 322         n 
+= dtrace_getustack_common(NULL
, 0, pc
, regs
->r
[7]); 
 328 dtrace_getufpstack(uint64_t * pcstack
, uint64_t * fpstack
, int pcstack_limit
) 
 330         /* XXX ARMTODO 64vs32 */ 
 331         thread_t        thread 
= current_thread(); 
 335         volatile        uint16_t  *flags 
= (volatile uint16_t *) &cpu_core
[CPU
->cpu_id
].cpuc_dtrace_flags
; 
 338         uintptr_t oldcontext
; 
 342         if (*flags 
& CPU_DTRACE_FAULT
) { 
 346         if (pcstack_limit 
<= 0) { 
 351          * If there's no user context we still need to zero the stack. 
 353         if (thread 
== NULL
) { 
 357         regs 
= (savearea_t 
*) find_user_regs(thread
); 
 362         *pcstack
++ = (uint64_t)dtrace_proc_selfpid(); 
 365         if (pcstack_limit 
<= 0) { 
 372 #if 0                           /* XXX signal stack crawl */ 
 373         oldcontext 
= lwp
->lwp_oldcontext
; 
 375         if (p
->p_model 
== DATAMODEL_NATIVE
) { 
 376                 s1 
= sizeof(struct frame
) + 2 * sizeof(long); 
 377                 s2 
= s1 
+ sizeof(siginfo_t
); 
 379                 s1 
= sizeof(struct frame32
) + 3 * sizeof(int); 
 380                 s2 
= s1 
+ sizeof(siginfo32_t
); 
 384         if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY
)) { 
 385                 *pcstack
++ = (uint64_t) pc
; 
 388                 if (pcstack_limit 
<= 0) { 
 392                 pc 
= dtrace_fuword32(sp
); 
 394         while (pc 
!= 0 && sp 
!= 0) { 
 395                 *pcstack
++ = (uint64_t) pc
; 
 398                 if (pcstack_limit 
<= 0) { 
 402 #if 0                           /* XXX signal stack crawl */ 
 403                 if (oldcontext 
== sp 
+ s1 
|| oldcontext 
== sp 
+ s2
) { 
 404                         if (p
->p_model 
== DATAMODEL_NATIVE
) { 
 405                                 ucontext_t     
*ucp 
= (ucontext_t 
*) oldcontext
; 
 406                                 greg_t         
*gregs 
= ucp
->uc_mcontext
.gregs
; 
 408                                 sp 
= dtrace_fulword(&gregs
[REG_FP
]); 
 409                                 pc 
= dtrace_fulword(&gregs
[REG_PC
]); 
 411                                 oldcontext 
= dtrace_fulword(&ucp
->uc_link
); 
 413                                 ucontext_t     
*ucp 
= (ucontext_t 
*) oldcontext
; 
 414                                 greg_t         
*gregs 
= ucp
->uc_mcontext
.gregs
; 
 416                                 sp 
= dtrace_fuword32(&gregs
[EBP
]); 
 417                                 pc 
= dtrace_fuword32(&gregs
[EIP
]); 
 419                                 oldcontext 
= dtrace_fuword32(&ucp
->uc_link
); 
 424                         pc 
= dtrace_fuword32((sp 
+ RETURN_OFFSET
)); 
 425                         sp 
= dtrace_fuword32(sp
); 
 431                  * This is totally bogus:  if we faulted, we're going to clear 
 432                  * the fault and break.  This is to deal with the apparently 
 433                  * broken Java stacks on x86. 
 435                 if (*flags 
& CPU_DTRACE_FAULT
) { 
 436                         *flags 
&= ~CPU_DTRACE_FAULT
; 
 443         while (pcstack_limit
-- > 0) { 
 449 dtrace_getpcstack(pc_t 
* pcstack
, int pcstack_limit
, int aframes
, 
 452         struct frame   
*fp 
= (struct frame 
*) __builtin_frame_address(0); 
 453         struct frame   
*nextfp
, *minfp
, *stacktop
; 
 458         uintptr_t       caller 
= CPU
->cpu_dtrace_caller
; 
 460         if ((on_intr 
= CPU_ON_INTR(CPU
)) != 0) { 
 461                 stacktop 
= (struct frame 
*) dtrace_get_cpu_int_stack_top(); 
 463                 stacktop 
= (struct frame 
*) (dtrace_get_kernel_stack(current_thread()) + kernel_stack_size
); 
 470         if (intrpc 
!= NULL 
&& depth 
< pcstack_limit
) { 
 471                 pcstack
[depth
++] = (pc_t
) intrpc
; 
 474         while (depth 
< pcstack_limit
) { 
 475                 nextfp 
= *(struct frame 
**) fp
; 
 476                 pc 
= *(uintptr_t *) (((uint32_t) fp
) + RETURN_OFFSET
); 
 478                 if (nextfp 
<= minfp 
|| nextfp 
>= stacktop
) { 
 481                                  * Hop from interrupt stack to thread stack. 
 483                                 arm_saved_state_t 
*arm_kern_regs 
= (arm_saved_state_t 
*) find_kern_regs(current_thread()); 
 485                                         nextfp 
= (struct frame 
*)arm_kern_regs
->r
[7]; 
 487                                         vm_offset_t kstack_base 
= dtrace_get_kernel_stack(current_thread()); 
 489                                         minfp 
= (struct frame 
*)kstack_base
; 
 490                                         stacktop 
= (struct frame 
*)(kstack_base 
+ kernel_stack_size
); 
 494                                         if (nextfp 
<= minfp 
|| nextfp 
>= stacktop
) { 
 499                                          * If this thread was on the interrupt stack, but did not 
 500                                          * take an interrupt (i.e, the idle thread), there is no 
 501                                          * explicit saved state for us to use. 
 507                                  * This is the last frame we can process; indicate 
 508                                  * that we should return after processing this frame. 
 514                         if (--aframes 
== 0 && caller 
!= (uintptr_t)NULL
) { 
 516                                  * We've just run out of artificial frames, 
 517                                  * and we have a valid caller -- fill it in 
 520                                 ASSERT(depth 
< pcstack_limit
); 
 521                                 pcstack
[depth
++] = (pc_t
) caller
; 
 522                                 caller 
= (uintptr_t)NULL
; 
 525                         if (depth 
< pcstack_limit
) { 
 526                                 pcstack
[depth
++] = (pc_t
) pc
; 
 531                         while (depth 
< pcstack_limit
) { 
 532                                 pcstack
[depth
++] = (pc_t
) NULL
; 
 542 dtrace_instr_size(uint32_t instr
, int thumb_mode
) 
 545                 uint16_t instr16 
= *(uint16_t*) &instr
; 
 546                 if (((instr16 
>> 11) & 0x1F) > 0x1C) { 
 557 dtrace_getarg(int arg
, int aframes
, dtrace_mstate_t 
*mstate
, dtrace_vstate_t 
*vstate
) 
 559 #pragma unused(arg, aframes, mstate, vstate) 
 563         uintptr_t *fp 
= (uintptr_t *)__builtin_frame_address(0); 
 568         for (i 
= 1; i 
<= aframes
; i
++) { 
 572                 if (dtrace_invop_callsite_pre 
!= NULL
 
 573                     && pc 
> (uintptr_t)dtrace_invop_callsite_pre
 
 574                     && pc 
<= (uintptr_t)dtrace_invop_callsite_post
) { 
 576                          * If we pass through the invalid op handler, we will 
 577                          * use the pointer that it passed to the stack as the 
 578                          * second argument to dtrace_invop() as the pointer to 
 579                          * the frame we're hunting for. 
 582                         stack 
= (uintptr_t *)&fp
[1]; /* Find marshalled arguments */ 
 583                         fp 
= (struct frame 
*)stack
[1]; /* Grab *second* argument */ 
 584                         stack 
= (uintptr_t *)&fp
[1]; /* Find marshalled arguments */ 
 585                         DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
); 
 586                         val 
= (uint64_t)(stack
[arg
]); 
 587                         DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
); 
 593          * Arrive here when provider has called dtrace_probe directly. 
 595         stack 
= (uintptr_t *)&fp
[1]; /* Find marshalled arguments */ 
 596         stack
++; /* Advance past probeID */ 
 598         DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT
); 
 599         val 
= *(((uint64_t *)stack
) + arg
); /* dtrace_probe arguments arg0 .. arg4 are 64bits wide */ 
 600         DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT
); 
 603         return 0xfeedfacedeafbeadLL
; 
 607 dtrace_probe_error(dtrace_state_t 
*state
, dtrace_epid_t epid
, int which
, 
 608     int fltoffs
, int fault
, uint64_t illval
) 
 612          * For the case of the error probe firing lets 
 613          * stash away "illval" here, and special-case retrieving it in DIF_VARIABLE_ARG. 
 615         state
->dts_arg_error_illval 
= illval
; 
 616         dtrace_probe( dtrace_probeid_error
, (uint64_t)(uintptr_t)state
, epid
, which
, fltoffs
, fault 
); 
 620 dtrace_toxic_ranges(void (*func
)(uintptr_t base
, uintptr_t limit
)) 
 622         /* XXX ARMTODO check copied from ppc/x86*/ 
 624          * "base" is the smallest toxic address in the range, "limit" is the first 
 625          * VALID address greater than "base". 
 627         func(0x0, VM_MIN_KERNEL_ADDRESS
); 
 628         if (VM_MAX_KERNEL_ADDRESS 
< ~(uintptr_t)0) { 
 629                 func(VM_MAX_KERNEL_ADDRESS 
+ 1, ~(uintptr_t)0); 
 634 dtrace_arm_condition_true(int cond
, int cpsr
) 
 637         int zf 
= (cpsr 
& PSR_ZF
) ? 1 : 0, 
 638             nf 
= (cpsr 
& PSR_NF
) ? 1 : 0, 
 639             cf 
= (cpsr 
& PSR_CF
) ? 1 : 0, 
 640             vf 
= (cpsr 
& PSR_VF
) ? 1 : 0; 
 643         case 0: taken 
= zf
; break; 
 644         case 1: taken 
= !zf
; break; 
 645         case 2: taken 
= cf
; break; 
 646         case 3: taken 
= !cf
; break; 
 647         case 4: taken 
= nf
; break; 
 648         case 5: taken 
= !nf
; break; 
 649         case 6: taken 
= vf
; break; 
 650         case 7: taken 
= !vf
; break; 
 651         case 8: taken 
= (cf 
&& !zf
); break; 
 652         case 9: taken 
= (!cf 
|| zf
); break; 
 653         case 10: taken 
= (nf 
== vf
); break; 
 654         case 11: taken 
= (nf 
!= vf
); break; 
 655         case 12: taken 
= (!zf 
&& (nf 
== vf
)); break; 
 656         case 13: taken 
= (zf 
|| (nf 
!= vf
)); break; 
 657         case 14: taken 
= 1; break; 
 658         case 15: taken 
= 1; break;         /* always "true" for ARM, unpredictable for THUMB. */ 
 665 dtrace_flush_caches(void) 
 667         /* TODO There were some problems with flushing just the cache line that had been modified. 
 668          * For now, we'll flush the entire cache, until we figure out how to flush just the patched block. 
 671         InvalidatePoU_Icache();