]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/arm/dtrace_isa.c
xnu-4903.241.1.tar.gz
[apple/xnu.git] / bsd / dev / arm / dtrace_isa.c
1 /*
2 * Copyright (c) 2005-2008 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 #define MACH__POSIX_C_SOURCE_PRIVATE 1 /* pulls in suitable savearea from
30 * mach/ppc/thread_status.h */
31 #include <arm/caches_internal.h>
32 #include <arm/proc_reg.h>
33
34 #include <kern/thread.h>
35 #include <mach/thread_status.h>
36
37 #include <stdarg.h>
38 #include <string.h>
39 #include <sys/malloc.h>
40 #include <sys/time.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/proc_internal.h>
44 #include <sys/kauth.h>
45 #include <sys/dtrace.h>
46 #include <sys/dtrace_impl.h>
47 #include <libkern/OSAtomic.h>
48 #include <kern/simple_lock.h>
49 #include <kern/sched_prim.h> /* for thread_wakeup() */
50 #include <kern/thread_call.h>
51 #include <kern/task.h>
52 #include <miscfs/devfs/devfs.h>
53 #include <mach/vm_param.h>
54
55 extern struct arm_saved_state *find_kern_regs(thread_t);
56
57 extern dtrace_id_t dtrace_probeid_error; /* special ERROR probe */
58 typedef arm_saved_state_t savearea_t;
59
60 extern lck_attr_t *dtrace_lck_attr;
61 extern lck_grp_t *dtrace_lck_grp;
62
63 int dtrace_arm_condition_true(int condition, int cpsr);
64
65 /*
66 * Atomicity and synchronization
67 */
68 inline void
69 dtrace_membar_producer(void)
70 {
71 #if __ARM_SMP__
72 __asm__ volatile("dmb ish" : : : "memory");
73 #else
74 __asm__ volatile("nop" : : : "memory");
75 #endif
76 }
77
78 inline void
79 dtrace_membar_consumer(void)
80 {
81 #if __ARM_SMP__
82 __asm__ volatile("dmb ish" : : : "memory");
83 #else
84 __asm__ volatile("nop" : : : "memory");
85 #endif
86 }
87
88 /*
89 * Interrupt manipulation
90 * XXX dtrace_getipl() can be called from probe context.
91 */
92 int
93 dtrace_getipl(void)
94 {
95 /*
96 * XXX Drat, get_interrupt_level is MACH_KERNEL_PRIVATE
97 * in osfmk/kern/cpu_data.h
98 */
99 /* return get_interrupt_level(); */
100 return (ml_at_interrupt_context() ? 1 : 0);
101 }
102
103 #if __ARM_SMP__
104 /*
105 * MP coordination
106 */
107
108 decl_lck_mtx_data(static, dt_xc_lock);
109 static uint32_t dt_xc_sync;
110
111 typedef struct xcArg {
112 processorid_t cpu;
113 dtrace_xcall_t f;
114 void *arg;
115 } xcArg_t;
116
117 static void
118 xcRemote(void *foo)
119 {
120 xcArg_t *pArg = (xcArg_t *) foo;
121
122 if (pArg->cpu == CPU->cpu_id || pArg->cpu == DTRACE_CPUALL)
123 (pArg->f) (pArg->arg);
124
125 if (hw_atomic_sub(&dt_xc_sync, 1) == 0)
126 thread_wakeup((event_t) &dt_xc_sync);
127 }
128 #endif
129
130 /*
131 * dtrace_xcall() is not called from probe context.
132 */
133 void
134 dtrace_xcall(processorid_t cpu, dtrace_xcall_t f, void *arg)
135 {
136 #if __ARM_SMP__
137 /* Only one dtrace_xcall in flight allowed */
138 lck_mtx_lock(&dt_xc_lock);
139
140 xcArg_t xcArg;
141
142 xcArg.cpu = cpu;
143 xcArg.f = f;
144 xcArg.arg = arg;
145
146 cpu_broadcast_xcall(&dt_xc_sync, TRUE, xcRemote, (void*) &xcArg);
147
148 lck_mtx_unlock(&dt_xc_lock);
149 return;
150 #else
151 #pragma unused(cpu)
152 /* On uniprocessor systems, the cpu should always be either ourselves or all */
153 ASSERT(cpu == CPU->cpu_id || cpu == DTRACE_CPUALL);
154
155 (*f)(arg);
156 return;
157 #endif
158 }
159
160 /*
161 * Initialization
162 */
163 void
164 dtrace_isa_init(void)
165 {
166 #if __ARM_SMP__
167 lck_mtx_init(&dt_xc_lock, dtrace_lck_grp, dtrace_lck_attr);
168 #endif
169 return;
170 }
171
172 /*
173 * Runtime and ABI
174 */
175 uint64_t
176 dtrace_getreg(struct regs * savearea, uint_t reg)
177 {
178 struct arm_saved_state *regs = (struct arm_saved_state *) savearea;
179 if (regs == NULL) {
180 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
181 return (0);
182 }
183 /* beyond register limit? */
184 if (reg > ARM_SAVED_STATE32_COUNT - 1) {
185 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
186 return (0);
187 }
188
189 return (uint64_t) ((unsigned int *) (&(regs->r)))[reg];
190 }
191
192 #define RETURN_OFFSET 4
193
194 static int
195 dtrace_getustack_common(uint64_t * pcstack, int pcstack_limit, user_addr_t pc,
196 user_addr_t sp)
197 {
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 if (sp == 0)
212 break;
213
214 pc = dtrace_fuword32((sp + RETURN_OFFSET));
215 sp = dtrace_fuword32(sp);
216 }
217
218 return (ret);
219 }
220
221 void
222 dtrace_getupcstack(uint64_t * pcstack, int pcstack_limit)
223 {
224 thread_t thread = current_thread();
225 savearea_t *regs;
226 user_addr_t pc, sp;
227 volatile uint16_t *flags = (volatile uint16_t *) & cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
228 int n;
229
230 if (*flags & CPU_DTRACE_FAULT)
231 return;
232
233 if (pcstack_limit <= 0)
234 return;
235
236 /*
237 * If there's no user context we still need to zero the stack.
238 */
239 if (thread == NULL)
240 goto zero;
241
242 regs = (savearea_t *) find_user_regs(thread);
243 if (regs == NULL)
244 goto zero;
245
246 *pcstack++ = (uint64_t)dtrace_proc_selfpid();
247 pcstack_limit--;
248
249 if (pcstack_limit <= 0)
250 return;
251
252 pc = regs->pc;
253 sp = regs->sp;
254
255 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
256 *pcstack++ = (uint64_t) pc;
257 pcstack_limit--;
258 if (pcstack_limit <= 0)
259 return;
260
261 pc = regs->lr;
262 }
263
264 n = dtrace_getustack_common(pcstack, pcstack_limit, pc, regs->r[7]);
265
266 ASSERT(n >= 0);
267 ASSERT(n <= pcstack_limit);
268
269 pcstack += n;
270 pcstack_limit -= n;
271
272 zero:
273 while (pcstack_limit-- > 0)
274 *pcstack++ = 0ULL;
275 }
276
277 int
278 dtrace_getustackdepth(void)
279 {
280 thread_t thread = current_thread();
281 savearea_t *regs;
282 user_addr_t pc, sp;
283 int n = 0;
284
285 if (thread == NULL)
286 return 0;
287
288 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
289 return (-1);
290
291 regs = (savearea_t *) find_user_regs(thread);
292 if (regs == NULL)
293 return 0;
294
295 pc = regs->pc;
296 sp = regs->sp;
297
298 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
299 n++;
300 pc = regs->lr;
301 }
302
303 /*
304 * Note that unlike ppc, the arm code does not use
305 * CPU_DTRACE_USTACK_FP. This is because arm always
306 * traces from the sp, even in syscall/profile/fbt
307 * providers.
308 */
309
310 n += dtrace_getustack_common(NULL, 0, pc, regs->r[7]);
311
312 return (n);
313 }
314
315 void
316 dtrace_getufpstack(uint64_t * pcstack, uint64_t * fpstack, int pcstack_limit)
317 {
318 /* XXX ARMTODO 64vs32 */
319 thread_t thread = current_thread();
320 savearea_t *regs;
321 user_addr_t pc, sp;
322
323 volatile uint16_t *flags = (volatile uint16_t *) & cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
324
325 #if 0
326 uintptr_t oldcontext;
327 size_t s1, s2;
328 #endif
329
330 if (*flags & CPU_DTRACE_FAULT)
331 return;
332
333 if (pcstack_limit <= 0)
334 return;
335
336 /*
337 * If there's no user context we still need to zero the stack.
338 */
339 if (thread == NULL)
340 goto zero;
341
342 regs = (savearea_t *) find_user_regs(thread);
343 if (regs == NULL)
344 goto zero;
345
346 *pcstack++ = (uint64_t)dtrace_proc_selfpid();
347 pcstack_limit--;
348
349 if (pcstack_limit <= 0)
350 return;
351
352 pc = regs->pc;
353 sp = regs->sp;
354
355 #if 0 /* XXX signal stack crawl */
356 oldcontext = lwp->lwp_oldcontext;
357
358 if (p->p_model == DATAMODEL_NATIVE) {
359 s1 = sizeof(struct frame) + 2 * sizeof(long);
360 s2 = s1 + sizeof(siginfo_t);
361 } else {
362 s1 = sizeof(struct frame32) + 3 * sizeof(int);
363 s2 = s1 + sizeof(siginfo32_t);
364 }
365 #endif
366
367 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
368 *pcstack++ = (uint64_t) pc;
369 *fpstack++ = 0;
370 pcstack_limit--;
371 if (pcstack_limit <= 0)
372 return;
373
374 pc = dtrace_fuword32(sp);
375 }
376 while (pc != 0 && sp != 0) {
377 *pcstack++ = (uint64_t) pc;
378 *fpstack++ = sp;
379 pcstack_limit--;
380 if (pcstack_limit <= 0)
381 break;
382
383 #if 0 /* XXX signal stack crawl */
384 if (oldcontext == sp + s1 || oldcontext == sp + s2) {
385 if (p->p_model == DATAMODEL_NATIVE) {
386 ucontext_t *ucp = (ucontext_t *) oldcontext;
387 greg_t *gregs = ucp->uc_mcontext.gregs;
388
389 sp = dtrace_fulword(&gregs[REG_FP]);
390 pc = dtrace_fulword(&gregs[REG_PC]);
391
392 oldcontext = dtrace_fulword(&ucp->uc_link);
393 } else {
394 ucontext_t *ucp = (ucontext_t *) oldcontext;
395 greg_t *gregs = ucp->uc_mcontext.gregs;
396
397 sp = dtrace_fuword32(&gregs[EBP]);
398 pc = dtrace_fuword32(&gregs[EIP]);
399
400 oldcontext = dtrace_fuword32(&ucp->uc_link);
401 }
402 } else
403 #endif
404 {
405 pc = dtrace_fuword32((sp + RETURN_OFFSET));
406 sp = dtrace_fuword32(sp);
407 }
408
409 #if 0
410 /* XXX ARMTODO*/
411 /*
412 * This is totally bogus: if we faulted, we're going to clear
413 * the fault and break. This is to deal with the apparently
414 * broken Java stacks on x86.
415 */
416 if (*flags & CPU_DTRACE_FAULT) {
417 *flags &= ~CPU_DTRACE_FAULT;
418 break;
419 }
420 #endif
421 }
422
423 zero:
424 while (pcstack_limit-- > 0)
425 *pcstack++ = 0ULL;
426 }
427
428 void
429 dtrace_getpcstack(pc_t * pcstack, int pcstack_limit, int aframes,
430 uint32_t * intrpc)
431 {
432 struct frame *fp = (struct frame *) __builtin_frame_address(0);
433 struct frame *nextfp, *minfp, *stacktop;
434 int depth = 0;
435 int on_intr;
436 int last = 0;
437 uintptr_t pc;
438 uintptr_t caller = CPU->cpu_dtrace_caller;
439
440 if ((on_intr = CPU_ON_INTR(CPU)) != 0)
441 stacktop = (struct frame *) dtrace_get_cpu_int_stack_top();
442 else
443 stacktop = (struct frame *) (dtrace_get_kernel_stack(current_thread()) + kernel_stack_size);
444
445 minfp = fp;
446
447 aframes++;
448
449 if (intrpc != NULL && depth < pcstack_limit)
450 pcstack[depth++] = (pc_t) intrpc;
451
452 while (depth < pcstack_limit) {
453 nextfp = *(struct frame **) fp;
454 pc = *(uintptr_t *) (((uint32_t) fp) + RETURN_OFFSET);
455
456 if (nextfp <= minfp || nextfp >= stacktop) {
457 if (on_intr) {
458 /*
459 * Hop from interrupt stack to thread stack.
460 */
461 arm_saved_state_t *arm_kern_regs = (arm_saved_state_t *) find_kern_regs(current_thread());
462 if (arm_kern_regs) {
463 nextfp = (struct frame *)arm_kern_regs->r[7];
464
465 vm_offset_t kstack_base = dtrace_get_kernel_stack(current_thread());
466
467 minfp = (struct frame *)kstack_base;
468 stacktop = (struct frame *)(kstack_base + kernel_stack_size);
469
470 on_intr = 0;
471
472 if (nextfp <= minfp || nextfp >= stacktop) {
473 last = 1;
474 }
475 } else {
476 /*
477 * If this thread was on the interrupt stack, but did not
478 * take an interrupt (i.e, the idle thread), there is no
479 * explicit saved state for us to use.
480 */
481 last = 1;
482 }
483 } else {
484 /*
485 * This is the last frame we can process; indicate
486 * that we should return after processing this frame.
487 */
488 last = 1;
489 }
490 }
491 if (aframes > 0) {
492 if (--aframes == 0 && caller != (uintptr_t)NULL) {
493 /*
494 * We've just run out of artificial frames,
495 * and we have a valid caller -- fill it in
496 * now.
497 */
498 ASSERT(depth < pcstack_limit);
499 pcstack[depth++] = (pc_t) caller;
500 caller = (uintptr_t)NULL;
501 }
502 } else {
503 if (depth < pcstack_limit)
504 pcstack[depth++] = (pc_t) pc;
505 }
506
507 if (last) {
508 while (depth < pcstack_limit)
509 pcstack[depth++] = (pc_t) NULL;
510 return;
511 }
512 fp = nextfp;
513 minfp = fp;
514 }
515 }
516
517 int
518 dtrace_instr_size(uint32_t instr, int thumb_mode)
519 {
520 if (thumb_mode) {
521 uint16_t instr16 = *(uint16_t*) &instr;
522 if (((instr16 >> 11) & 0x1F) > 0x1C)
523 return 4;
524 else
525 return 2;
526 } else {
527 return 4;
528 }
529 }
530
531 uint64_t
532 dtrace_getarg(int arg, int aframes, dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
533 {
534 #pragma unused(arg, aframes, mstate, vstate)
535 #if 0
536 /* XXX ARMTODO */
537 uint64_t val;
538 uintptr_t *fp = (uintptr_t *)__builtin_frame_address(0);
539 uintptr_t *stack;
540 uintptr_t pc;
541 int i;
542
543 for (i = 1; i <= aframes; i++) {
544 fp = fp[0];
545 pc = fp[1];
546
547 if (dtrace_invop_callsite_pre != NULL
548 && pc > (uintptr_t)dtrace_invop_callsite_pre
549 && pc <= (uintptr_t)dtrace_invop_callsite_post) {
550 /*
551 * If we pass through the invalid op handler, we will
552 * use the pointer that it passed to the stack as the
553 * second argument to dtrace_invop() as the pointer to
554 * the frame we're hunting for.
555 */
556
557 stack = (uintptr_t *)&fp[1]; /* Find marshalled arguments */
558 fp = (struct frame *)stack[1]; /* Grab *second* argument */
559 stack = (uintptr_t *)&fp[1]; /* Find marshalled arguments */
560 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
561 val = (uint64_t)(stack[arg]);
562 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
563 return val;
564 }
565 }
566
567 /*
568 * Arrive here when provider has called dtrace_probe directly.
569 */
570 stack = (uintptr_t *)&fp[1]; /* Find marshalled arguments */
571 stack++; /* Advance past probeID */
572
573 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
574 val = *(((uint64_t *)stack) + arg); /* dtrace_probe arguments arg0 .. arg4 are 64bits wide */
575 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
576 return (val);
577 #endif
578 return 0xfeedfacedeafbeadLL;
579 }
580
581 void
582 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
583 int fltoffs, int fault, uint64_t illval)
584 {
585 /* XXX ARMTODO */
586 /*
587 * For the case of the error probe firing lets
588 * stash away "illval" here, and special-case retrieving it in DIF_VARIABLE_ARG.
589 */
590 state->dts_arg_error_illval = illval;
591 dtrace_probe( dtrace_probeid_error, (uint64_t)(uintptr_t)state, epid, which, fltoffs, fault );
592 }
593
594 void
595 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
596 {
597 /* XXX ARMTODO check copied from ppc/x86*/
598 /*
599 * "base" is the smallest toxic address in the range, "limit" is the first
600 * VALID address greater than "base".
601 */
602 func(0x0, VM_MIN_KERNEL_ADDRESS);
603 if (VM_MAX_KERNEL_ADDRESS < ~(uintptr_t)0)
604 func(VM_MAX_KERNEL_ADDRESS + 1, ~(uintptr_t)0);
605 }
606
607 int
608 dtrace_arm_condition_true(int cond, int cpsr)
609 {
610 int taken = 0;
611 int zf = (cpsr & PSR_ZF) ? 1 : 0,
612 nf = (cpsr & PSR_NF) ? 1 : 0,
613 cf = (cpsr & PSR_CF) ? 1 : 0,
614 vf = (cpsr & PSR_VF) ? 1 : 0;
615
616 switch(cond) {
617 case 0: taken = zf; break;
618 case 1: taken = !zf; break;
619 case 2: taken = cf; break;
620 case 3: taken = !cf; break;
621 case 4: taken = nf; break;
622 case 5: taken = !nf; break;
623 case 6: taken = vf; break;
624 case 7: taken = !vf; break;
625 case 8: taken = (cf && !zf); break;
626 case 9: taken = (!cf || zf); break;
627 case 10: taken = (nf == vf); break;
628 case 11: taken = (nf != vf); break;
629 case 12: taken = (!zf && (nf == vf)); break;
630 case 13: taken = (zf || (nf != vf)); break;
631 case 14: taken = 1; break;
632 case 15: taken = 1; break; /* always "true" for ARM, unpredictable for THUMB. */
633 }
634
635 return taken;
636 }
637
638 void dtrace_flush_caches(void)
639 {
640 /* TODO There were some problems with flushing just the cache line that had been modified.
641 * For now, we'll flush the entire cache, until we figure out how to flush just the patched block.
642 */
643 FlushPoU_Dcache();
644 InvalidatePoU_Icache();
645 }