1 /*
2 * Copyright (c) 2005-2018 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <arm/caches_internal.h>
30 #include <kern/thread.h>
31
32 #if __has_include(<ptrauth.h>)
33 #include <ptrauth.h>
34 #endif
35 #include <stdarg.h>
36 #include <sys/time.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/proc_internal.h>
40 #include <sys/kauth.h>
41 #include <sys/dtrace.h>
42 #include <sys/dtrace_impl.h>
43 #include <machine/atomic.h>
44 #include <kern/cambria_layout.h>
45 #include <kern/simple_lock.h>
46 #include <kern/sched_prim.h> /* for thread_wakeup() */
47 #include <kern/thread_call.h>
48 #include <kern/task.h>
49 #include <machine/atomic.h>
50
51 extern struct arm_saved_state *find_kern_regs(thread_t);
52
53 extern dtrace_id_t dtrace_probeid_error; /* special ERROR probe */
54 typedef arm_saved_state_t savearea_t;
55
56 #if XNU_MONITOR
57 extern void * pmap_stacks_start;
58 extern void * pmap_stacks_end;
59 #endif
60
61 struct frame {
62 struct frame *backchain;
63 uintptr_t retaddr;
64 };
65
66 /*
67 * Atomicity and synchronization
68 */
69 inline void
dtrace_membar_producer(void)70 dtrace_membar_producer(void)
71 {
72 __builtin_arm_dmb(DMB_ISH);
73 }
74
75 inline void
dtrace_membar_consumer(void)76 dtrace_membar_consumer(void)
77 {
78 __builtin_arm_dmb(DMB_ISH);
79 }
80
81 /*
82 * Interrupt manipulation
83 * XXX dtrace_getipl() can be called from probe context.
84 */
85 int
dtrace_getipl(void)86 dtrace_getipl(void)
87 {
88 /*
89 * XXX Drat, get_interrupt_level is MACH_KERNEL_PRIVATE
90 * in osfmk/kern/cpu_data.h
91 */
92 /* return get_interrupt_level(); */
93 return ml_at_interrupt_context() ? 1 : 0;
94 }
95
96 /*
97 * MP coordination
98 */
99
100 static LCK_MTX_DECLARE_ATTR(dt_xc_lock, &dtrace_lck_grp, &dtrace_lck_attr);
101 static uint32_t dt_xc_sync;
102
103 typedef struct xcArg {
104 processorid_t cpu;
105 dtrace_xcall_t f;
106 void *arg;
107 } xcArg_t;
108
109 static void
xcRemote(void * foo)110 xcRemote(void *foo)
111 {
112 xcArg_t *pArg = (xcArg_t *) foo;
113
114 if (pArg->cpu == CPU->cpu_id || pArg->cpu == DTRACE_CPUALL) {
115 (pArg->f)(pArg->arg);
116 }
117
118 if (os_atomic_dec(&dt_xc_sync, relaxed) == 0) {
119 thread_wakeup((event_t) &dt_xc_sync);
120 }
121 }
122
123 /*
124 * dtrace_xcall() is not called from probe context.
125 */
126 void
dtrace_xcall(processorid_t cpu,dtrace_xcall_t f,void * arg)127 dtrace_xcall(processorid_t cpu, dtrace_xcall_t f, void *arg)
128 {
129 /* Only one dtrace_xcall in flight allowed */
130 lck_mtx_lock(&dt_xc_lock);
131
132 xcArg_t xcArg;
133
134 xcArg.cpu = cpu;
135 xcArg.f = f;
136 xcArg.arg = arg;
137
138 cpu_broadcast_xcall(&dt_xc_sync, TRUE, xcRemote, (void*) &xcArg);
139
140 lck_mtx_unlock(&dt_xc_lock);
141 return;
142 }
143
144
145 /**
146 * Register definitions
147 */
148 #define ARM64_FP 29
149 #define ARM64_LR 30
150 #define ARM64_SP 31
151 #define ARM64_PC 32
152 #define ARM64_CPSR 33
153
154 /*
155 * Runtime and ABI
156 */
157 uint64_t
dtrace_getreg(struct regs * savearea,uint_t reg)158 dtrace_getreg(struct regs * savearea, uint_t reg)
159 {
160 struct arm_saved_state *regs = (struct arm_saved_state *) savearea;
161
162 if (regs == NULL) {
163 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
164 return 0;
165 }
166
167 if (!check_saved_state_reglimit(regs, reg)) {
168 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
169 return 0;
170 }
171
172 return (uint64_t)get_saved_state_reg(regs, reg);
173 }
174
175 uint64_t
dtrace_getvmreg(uint_t ndx)176 dtrace_getvmreg(uint_t ndx)
177 {
178 #pragma unused(ndx)
179 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
180 return 0;
181 }
182
183 void
dtrace_livedump(char * filename,size_t len)184 dtrace_livedump(char *filename, size_t len)
185 {
186 #pragma unused(filename)
187 #pragma unused(len)
188 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
189 }
190
191 #define RETURN_OFFSET64 8
192
193 static int
dtrace_getustack_common(uint64_t * pcstack,int pcstack_limit,user_addr_t pc,user_addr_t sp)194 dtrace_getustack_common(uint64_t * pcstack, int pcstack_limit, user_addr_t pc,
195 user_addr_t sp)
196 {
197 volatile uint16_t *flags = (volatile uint16_t *) &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
198 int ret = 0;
199
200 ASSERT(pcstack == NULL || pcstack_limit > 0);
201
202 while (pc != 0) {
203 ret++;
204 if (pcstack != NULL) {
205 *pcstack++ = (uint64_t) pc;
206 pcstack_limit--;
207 if (pcstack_limit <= 0) {
208 break;
209 }
210 }
211
212 if (sp == 0) {
213 break;
214 }
215
216 pc = dtrace_fuword64((sp + RETURN_OFFSET64));
217 sp = dtrace_fuword64(sp);
218
219 /* Truncate ustack if the iterator causes fault. */
220 if (*flags & CPU_DTRACE_FAULT) {
221 *flags &= ~CPU_DTRACE_FAULT;
222 break;
223 }
224 }
225
226 return ret;
227 }
228
229 void
dtrace_getupcstack(uint64_t * pcstack,int pcstack_limit)230 dtrace_getupcstack(uint64_t * pcstack, int pcstack_limit)
231 {
232 thread_t thread = current_thread();
233 savearea_t *regs;
234 user_addr_t pc, sp, fp;
235 volatile uint16_t *flags = (volatile uint16_t *) &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
236 int n;
237
238 if (*flags & CPU_DTRACE_FAULT) {
239 return;
240 }
241
242 if (pcstack_limit <= 0) {
243 return;
244 }
245
246 /*
247 * If there's no user context we still need to zero the stack.
248 */
249 if (thread == NULL) {
250 goto zero;
251 }
252
253 regs = (savearea_t *) find_user_regs(thread);
254 if (regs == NULL) {
255 goto zero;
256 }
257
258 *pcstack++ = (uint64_t)dtrace_proc_selfpid();
259 pcstack_limit--;
260
261 if (pcstack_limit <= 0) {
262 return;
263 }
264
265 pc = get_saved_state_pc(regs);
266 sp = get_saved_state_sp(regs);
267
268 {
269 fp = get_saved_state_fp(regs);
270 }
271
272 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
273 *pcstack++ = (uint64_t) pc;
274 pcstack_limit--;
275 if (pcstack_limit <= 0) {
276 return;
277 }
278
279 pc = get_saved_state_lr(regs);
280 }
281
282 n = dtrace_getustack_common(pcstack, pcstack_limit, pc, fp);
283
284 ASSERT(n >= 0);
285 ASSERT(n <= pcstack_limit);
286
287 pcstack += n;
288 pcstack_limit -= n;
289
290 zero:
291 while (pcstack_limit-- > 0) {
292 *pcstack++ = 0ULL;
293 }
294 }
295
296 int
dtrace_getustackdepth(void)297 dtrace_getustackdepth(void)
298 {
299 thread_t thread = current_thread();
300 savearea_t *regs;
301 user_addr_t pc, sp, fp;
302 int n = 0;
303
304 if (thread == NULL) {
305 return 0;
306 }
307
308 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) {
309 return -1;
310 }
311
312 regs = (savearea_t *) find_user_regs(thread);
313 if (regs == NULL) {
314 return 0;
315 }
316
317 pc = get_saved_state_pc(regs);
318 sp = get_saved_state_sp(regs);
319 fp = get_saved_state_fp(regs);
320
321 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
322 n++;
323 pc = get_saved_state_lr(regs);
324 }
325
326 /*
327 * Note that unlike ppc, the arm code does not use
328 * CPU_DTRACE_USTACK_FP. This is because arm always
329 * traces from the sp, even in syscall/profile/fbt
330 * providers.
331 */
332
333 n += dtrace_getustack_common(NULL, 0, pc, fp);
334
335 return n;
336 }
337
338 void
dtrace_getufpstack(uint64_t * pcstack,uint64_t * fpstack,int pcstack_limit)339 dtrace_getufpstack(uint64_t * pcstack, uint64_t * fpstack, int pcstack_limit)
340 {
341 thread_t thread = current_thread();
342 boolean_t is64bit = proc_is64bit_data(current_proc());
343 savearea_t *regs;
344 user_addr_t pc, sp;
345 volatile uint16_t *flags = (volatile uint16_t *) &cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
346
347
348 if (*flags & CPU_DTRACE_FAULT) {
349 return;
350 }
351
352 if (pcstack_limit <= 0) {
353 return;
354 }
355
356 /*
357 * If there's no user context we still need to zero the stack.
358 */
359 if (thread == NULL) {
360 goto zero;
361 }
362
363 regs = (savearea_t *) find_user_regs(thread);
364 if (regs == NULL) {
365 goto zero;
366 }
367
368 *pcstack++ = (uint64_t)dtrace_proc_selfpid();
369 pcstack_limit--;
370
371 if (pcstack_limit <= 0) {
372 return;
373 }
374
375 pc = get_saved_state_pc(regs);
376 sp = get_saved_state_lr(regs);
377
378 #if 0 /* XXX signal stack crawl */
379 oldcontext = lwp->lwp_oldcontext;
380
381 if (p->p_model == DATAMODEL_NATIVE) {
382 s1 = sizeof(struct frame) + 2 * sizeof(long);
383 s2 = s1 + sizeof(siginfo_t);
384 } else {
385 s1 = sizeof(struct frame32) + 3 * sizeof(int);
386 s2 = s1 + sizeof(siginfo32_t);
387 }
388 #endif
389
390 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
391 *pcstack++ = (uint64_t) pc;
392 *fpstack++ = 0;
393 pcstack_limit--;
394 if (pcstack_limit <= 0) {
395 return;
396 }
397
398 if (is64bit) {
399 pc = dtrace_fuword64(sp);
400 } else {
401 pc = dtrace_fuword32(sp);
402 }
403 }
404 while (pc != 0 && sp != 0) {
405 *pcstack++ = (uint64_t) pc;
406 *fpstack++ = sp;
407 pcstack_limit--;
408 if (pcstack_limit <= 0) {
409 break;
410 }
411
412 #if 0 /* XXX signal stack crawl */
413 if (oldcontext == sp + s1 || oldcontext == sp + s2) {
414 if (p->p_model == DATAMODEL_NATIVE) {
415 ucontext_t *ucp = (ucontext_t *) oldcontext;
416 greg_t *gregs = ucp->uc_mcontext.gregs;
417
418 sp = dtrace_fulword(&gregs[REG_FP]);
419 pc = dtrace_fulword(&gregs[REG_PC]);
420
421 oldcontext = dtrace_fulword(&ucp->uc_link);
422 } else {
423 ucontext_t *ucp = (ucontext_t *) oldcontext;
424 greg_t *gregs = ucp->uc_mcontext.gregs;
425
426 sp = dtrace_fuword32(&gregs[EBP]);
427 pc = dtrace_fuword32(&gregs[EIP]);
428
429 oldcontext = dtrace_fuword32(&ucp->uc_link);
430 }
431 } else
432 #endif
433 {
434 pc = dtrace_fuword64((sp + RETURN_OFFSET64));
435 sp = dtrace_fuword64(sp);
436 }
437
438 /* Truncate ustack if the iterator causes fault. */
439 if (*flags & CPU_DTRACE_FAULT) {
440 *flags &= ~CPU_DTRACE_FAULT;
441 break;
442 }
443 }
444
445 zero:
446 while (pcstack_limit-- > 0) {
447 *pcstack++ = 0ULL;
448 }
449 }
450
451 #if XNU_MONITOR
452 static inline boolean_t
dtrace_frame_in_ppl_stack(struct frame * fp)453 dtrace_frame_in_ppl_stack(struct frame * fp)
454 {
455 return ((void *)fp >= pmap_stacks_start) &&
456 ((void *)fp < pmap_stacks_end);
457 }
458 #endif
459
460 void
dtrace_getpcstack(pc_t * pcstack,int pcstack_limit,int aframes,uint32_t * intrpc)461 dtrace_getpcstack(pc_t * pcstack, int pcstack_limit, int aframes,
462 uint32_t * intrpc)
463 {
464 struct frame *fp = (struct frame *) __builtin_frame_address(0);
465 struct frame *nextfp, *minfp, *stacktop;
466 int depth = 0;
467 int on_intr;
468 #if XNU_MONITOR
469 int on_ppl_stack;
470 #endif
471 int last = 0;
472 uintptr_t pc;
473 uintptr_t caller = CPU->cpu_dtrace_caller;
474
475 if ((on_intr = CPU_ON_INTR(CPU)) != 0) {
476 stacktop = (struct frame *) dtrace_get_cpu_int_stack_top();
477 }
478 #if XNU_MONITOR
479 else if ((on_ppl_stack = dtrace_frame_in_ppl_stack(fp))) {
480 stacktop = (struct frame *) pmap_stacks_end;
481 }
482 #endif
483 else {
484 stacktop = (struct frame *) (dtrace_get_kernel_stack(current_thread()) + kernel_stack_size);
485 }
486
487 minfp = fp;
488
489 aframes++;
490
491 if (intrpc != NULL && depth < pcstack_limit) {
492 pcstack[depth++] = (pc_t) intrpc;
493 }
494
495 while (depth < pcstack_limit) {
496 nextfp = *(struct frame **) fp;
497 pc = *(uintptr_t *) (((uintptr_t) fp) + RETURN_OFFSET64);
498
499 if (nextfp <= minfp || nextfp >= stacktop) {
500 if (on_intr) {
501 /*
502 * Hop from interrupt stack to thread stack.
503 */
504 arm_saved_state_t *arm_kern_regs = (arm_saved_state_t *) find_kern_regs(current_thread());
505 if (arm_kern_regs) {
506 nextfp = (struct frame *)(saved_state64(arm_kern_regs)->fp);
507
508 #if XNU_MONITOR
509 on_ppl_stack = dtrace_frame_in_ppl_stack(nextfp);
510
511 if (on_ppl_stack) {
512 minfp = pmap_stacks_start;
513 stacktop = pmap_stacks_end;
514 } else
515 #endif
516 {
517 vm_offset_t kstack_base = dtrace_get_kernel_stack(current_thread());
518
519 minfp = (struct frame *)kstack_base;
520 stacktop = (struct frame *)(kstack_base + kernel_stack_size);
521 }
522
523 on_intr = 0;
524
525 if (nextfp <= minfp || nextfp >= stacktop) {
526 last = 1;
527 }
528 } else {
529 /*
530 * If this thread was on the interrupt stack, but did not
531 * take an interrupt (i.e, the idle thread), there is no
532 * explicit saved state for us to use.
533 */
534 last = 1;
535 }
536 } else {
537 #if XNU_MONITOR
538 if ((!on_ppl_stack) && dtrace_frame_in_ppl_stack(nextfp)) {
539 /*
540 * We are switching from the kernel stack
541 * to the PPL stack.
542 */
543 on_ppl_stack = 1;
544 minfp = pmap_stacks_start;
545 stacktop = pmap_stacks_end;
546 } else if (on_ppl_stack) {
547 /*
548 * We could be going from the PPL stack
549 * to the kernel stack.
550 */
551 vm_offset_t kstack_base = dtrace_get_kernel_stack(current_thread());
552
553 minfp = (struct frame *)kstack_base;
554 stacktop = (struct frame *)(kstack_base + kernel_stack_size);
555
556 if (nextfp <= minfp || nextfp >= stacktop) {
557 last = 1;
558 }
559 } else
560 #endif
561 {
562 /*
563 * This is the last frame we can process; indicate
564 * that we should return after processing this frame.
565 */
566 last = 1;
567 }
568 }
569 }
570 if (aframes > 0) {
571 if (--aframes == 0 && caller != (uintptr_t)NULL) {
572 /*
573 * We've just run out of artificial frames,
574 * and we have a valid caller -- fill it in
575 * now.
576 */
577 ASSERT(depth < pcstack_limit);
578 pcstack[depth++] = (pc_t) caller;
579 caller = (uintptr_t)NULL;
580 }
581 } else {
582 if (depth < pcstack_limit) {
583 pcstack[depth++] = (pc_t) pc;
584 }
585 }
586
587 if (last) {
588 while (depth < pcstack_limit) {
589 pcstack[depth++] = (pc_t) NULL;
590 }
591 return;
592 }
593 fp = nextfp;
594 minfp = fp;
595 }
596 }
597
598 uint64_t
dtrace_getarg(int arg,int aframes,dtrace_mstate_t * mstate,dtrace_vstate_t * vstate)599 dtrace_getarg(int arg, int aframes, dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
600 {
601 #pragma unused(arg, aframes)
602 uint64_t val = 0;
603 struct frame *fp = (struct frame *)__builtin_frame_address(0);
604 uintptr_t *stack;
605 uintptr_t pc;
606 int i;
607
608 /*
609 * A total of 8 arguments are passed via registers; any argument with
610 * index of 7 or lower is therefore in a register.
611 */
612 int inreg = 7;
613
614 for (i = 1; i <= aframes; ++i) {
615 fp = fp->backchain;
616 #if __has_feature(ptrauth_returns)
617 pc = (uintptr_t)ptrauth_strip((void*)fp->retaddr, ptrauth_key_return_address);
618 #else
619 pc = fp->retaddr;
620 #endif
621
622 if (dtrace_invop_callsite_pre != NULL
623 && pc > (uintptr_t) dtrace_invop_callsite_pre
624 && pc <= (uintptr_t) dtrace_invop_callsite_post) {
625 /* fp points to frame of dtrace_invop() activation */
626 fp = fp->backchain; /* to fbt_perfCallback activation */
627 fp = fp->backchain; /* to sleh_synchronous activation */
628 fp = fp->backchain; /* to fleh_synchronous activation */
629
630 arm_saved_state_t *tagged_regs = (arm_saved_state_t*) ((void*) &fp[1]);
631 arm_saved_state64_t *saved_state = saved_state64(tagged_regs);
632
633 if (arg <= inreg) {
634 /* the argument will be found in a register */
635 stack = (uintptr_t*) &saved_state->x[0];
636 } else {
637 /* the argument will be found in the stack */
638 fp = (struct frame*) saved_state->sp;
639 stack = (uintptr_t*) &fp[1];
640 arg -= (inreg + 1);
641 }
642
643 goto load;
644 }
645 }
646
647 /*
648 * We know that we did not come through a trap to get into
649 * dtrace_probe() -- We arrive here when the provider has
650 * called dtrace_probe() directly.
651 * The probe ID is the first argument to dtrace_probe().
652 * We must advance beyond that to get the argX.
653 */
654 arg++; /* Advance past probeID */
655
656 if (arg <= inreg) {
657 /*
658 * This shouldn't happen. If the argument is passed in a
659 * register then it should have been, well, passed in a
660 * register...
661 */
662 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
663 return 0;
664 }
665
666 arg -= (inreg + 1);
667 stack = (uintptr_t*) &fp[1]; /* Find marshalled arguments */
668
669 load:
670 if (dtrace_canload((uint64_t)(stack + arg), sizeof(uint64_t),
671 mstate, vstate)) {
672 /* dtrace_probe arguments arg0 ... arg4 are 64bits wide */
673 val = dtrace_load64((uint64_t)(stack + arg));
674 }
675
676 return val;
677 }
678
679 void
dtrace_probe_error(dtrace_state_t * state,dtrace_epid_t epid,int which,int fltoffs,int fault,uint64_t illval)680 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
681 int fltoffs, int fault, uint64_t illval)
682 {
683 /* XXX ARMTODO */
684 /*
685 * For the case of the error probe firing lets
686 * stash away "illval" here, and special-case retrieving it in DIF_VARIABLE_ARG.
687 */
688 state->dts_arg_error_illval = illval;
689 dtrace_probe( dtrace_probeid_error, (uint64_t)(uintptr_t)state, epid, which, fltoffs, fault );
690 }
691
692 void
dtrace_toxic_ranges(void (* func)(uintptr_t base,uintptr_t limit))693 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
694 {
695 /* XXX ARMTODO check copied from ppc/x86*/
696 /*
697 * "base" is the smallest toxic address in the range, "limit" is the first
698 * VALID address greater than "base".
699 */
700 func(0x0, VM_MIN_KERNEL_ADDRESS);
701 if (VM_MAX_KERNEL_ADDRESS < ~(uintptr_t)0) {
702 func(VM_MAX_KERNEL_ADDRESS + 1, ~(uintptr_t)0);
703 }
704 }
705
706 void
dtrace_flush_caches(void)707 dtrace_flush_caches(void)
708 {
709 /* TODO There were some problems with flushing just the cache line that had been modified.
710 * For now, we'll flush the entire cache, until we figure out how to flush just the patched block.
711 */
712 FlushPoU_Dcache();
713 InvalidatePoU_Icache();
714 }
715