]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/ppc/dtrace_isa.c
21b49bdc4cddca61ec0aa41ed961f9b27d3f1a8c
[apple/xnu.git] / bsd / dev / ppc / dtrace_isa.c
1 /*
2 * Copyright (c) 2005-2006 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 mach/ppc/thread_status.h */
30 #include <kern/thread.h>
31 #include <mach/thread_status.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <sys/malloc.h>
35 #include <sys/time.h>
36 #include <sys/systm.h>
37 #include <sys/proc.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>
49
50 extern dtrace_id_t dtrace_probeid_error; /* special ERROR probe */
51
52 void
53 dtrace_probe_error(dtrace_state_t *state, dtrace_epid_t epid, int which,
54 int fltoffs, int fault, uint64_t illval)
55 {
56 /*
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.
59 */
60 state->dts_arg_error_illval = illval;
61 dtrace_probe( dtrace_probeid_error, (uint64_t)(uintptr_t)state, epid, which, fltoffs, fault );
62 }
63
64 /*
65 * Atomicity and synchronization
66 */
67 void
68 dtrace_membar_producer(void)
69 {
70 __asm__ volatile("sync");
71 }
72
73 void
74 dtrace_membar_consumer(void)
75 {
76 __asm__ volatile("isync");
77 }
78
79 /*
80 * Interrupt manipulation
81 * XXX dtrace_getipl() can be called from probe context.
82 */
83 int
84 dtrace_getipl(void)
85 {
86 return (ml_at_interrupt_context() ? 1: 0);
87 }
88
89 /*
90 * MP coordination
91 */
92 typedef void (*broadcastFunc) (uint32_t);
93
94 int32_t cpu_broadcast(uint32_t *, broadcastFunc, uint32_t); /* osfmk/ppc/machine_cpu.h */
95
96 typedef struct xcArg {
97 processorid_t cpu;
98 dtrace_xcall_t f;
99 void *arg;
100 uint32_t waitVar;
101 } xcArg_t;
102
103 static void
104 xcRemote( uint32_t foo )
105 {
106 xcArg_t *pArg = (xcArg_t *)foo;
107
108 if ( pArg->cpu == CPU->cpu_id || pArg->cpu == DTRACE_CPUALL ) {
109 (pArg->f)(pArg->arg);
110 }
111
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 */
114 }
115 }
116
117 /*
118 * dtrace_xcall() is not called from probe context.
119 */
120 void
121 dtrace_xcall(processorid_t cpu, dtrace_xcall_t f, void *arg)
122 {
123 xcArg_t xcArg;
124
125 /* Talking to ourselves, are we? */
126 if ( cpu == CPU->cpu_id ) {
127 (*f)(arg);
128 return;
129 }
130
131 if ( cpu == DTRACE_CPUALL ) {
132 (*f)(arg);
133 }
134
135 xcArg.cpu = cpu;
136 xcArg.f = f;
137 xcArg.arg = arg;
138 xcArg.waitVar = 0;
139
140 (void)cpu_broadcast(&(xcArg.waitVar), xcRemote, (uint32_t)&xcArg);
141 }
142
143 /*
144 * Runtime and ABI
145 */
146 uint64_t
147 dtrace_getreg(struct regs *savearea, uint_t reg)
148 {
149 ppc_saved_state_t *regs = (ppc_saved_state_t *)savearea;
150 uint64_t mask = (_cpu_capabilities & k64Bit) ? 0xffffffffffffffffULL : 0x00000000ffffffffULL;
151
152 /* See osfmk/ppc/savearea.h */
153 if (reg > 68) { /* beyond mmcr2 */
154 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
155 return (0);
156 }
157
158 switch (reg) {
159 /* First 38 registers are saved to 64 bits r0-r31, srr0, srr1, xer, lr, ctr, dar. */
160 default:
161 return (((uint64_t *)(&(regs->save_r0)))[reg]) & mask;
162
163 /* Handle the 32-bit registers */
164 case 38: case 39: case 40: case 41: /* cr, dsisr, exception, vrsave */
165 case 42: case 43: case 44: case 45: /* vscr[4] */
166 case 46: case 47: case 48: case 49: /* fpscrpad, fpscr, save_1d8[2] */
167 case 50: case 51: case 52: case 53: /* save_1E0[8] */
168 case 54: case 55: case 56: case 57:
169 case 58: case 59: case 60: case 61: /* save_pmc[8] */
170 case 62: case 63: case 64: case 65:
171 return (uint64_t)(((unsigned int *)(&(regs->save_cr)))[reg - 38]);
172
173 case 66:
174 return regs->save_mmcr0 & mask;
175 case 67:
176 return regs->save_mmcr1 & mask;
177 case 68:
178 return regs->save_mmcr2 & mask;
179 }
180 }
181
182 #define RETURN_OFFSET 8
183 #define RETURN_OFFSET64 16
184 #define REGPC save_srr0
185 #define REGSP save_r1
186
187 /*
188 * XXX dtrace_getustack_common() can be called from probe context.
189 */
190 static int
191 dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, user_addr_t pc,
192 user_addr_t sp)
193 {
194 #if 0
195 volatile uint16_t *flags =
196 (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
197
198 uintptr_t oldcontext = lwp->lwp_oldcontext; /* XXX signal stack crawl*/
199 size_t s1, s2;
200 #endif
201 int ret = 0;
202 boolean_t is64Bit = proc_is64bit(current_proc());
203
204 ASSERT(pcstack == NULL || pcstack_limit > 0);
205
206 #if 0 /* XXX signal stack crawl*/
207 if (p->p_model == DATAMODEL_NATIVE) {
208 s1 = sizeof (struct frame) + 2 * sizeof (long);
209 s2 = s1 + sizeof (siginfo_t);
210 } else {
211 s1 = sizeof (struct frame32) + 3 * sizeof (int);
212 s2 = s1 + sizeof (siginfo32_t);
213 }
214 #endif
215
216 while (pc != 0) {
217 ret++;
218 if (pcstack != NULL) {
219 *pcstack++ = (uint64_t)pc;
220 pcstack_limit--;
221 if (pcstack_limit <= 0)
222 break;
223 }
224
225 if (sp == 0)
226 break;
227
228 #if 0 /* XXX signal stack crawl*/
229 if (oldcontext == sp + s1 || oldcontext == sp + s2) {
230 if (p->p_model == DATAMODEL_NATIVE) {
231 ucontext_t *ucp = (ucontext_t *)oldcontext;
232 greg_t *gregs = ucp->uc_mcontext.gregs;
233
234 sp = dtrace_fulword(&gregs[REG_FP]);
235 pc = dtrace_fulword(&gregs[REG_PC]);
236
237 oldcontext = dtrace_fulword(&ucp->uc_link);
238 } else {
239 ucontext32_t *ucp = (ucontext32_t *)oldcontext;
240 greg32_t *gregs = ucp->uc_mcontext.gregs;
241
242 sp = dtrace_fuword32(&gregs[EBP]);
243 pc = dtrace_fuword32(&gregs[EIP]);
244
245 oldcontext = dtrace_fuword32(&ucp->uc_link);
246 }
247 }
248 else
249 #endif
250 {
251 if (is64Bit) {
252 pc = dtrace_fuword64((sp + RETURN_OFFSET64));
253 sp = dtrace_fuword64(sp);
254 } else {
255 pc = dtrace_fuword32((sp + RETURN_OFFSET));
256 sp = dtrace_fuword32(sp);
257 }
258 }
259 }
260
261 return (ret);
262 }
263
264 void
265 dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
266 {
267 thread_t thread = current_thread();
268 ppc_saved_state_t *regs;
269 user_addr_t pc, sp;
270 volatile uint16_t *flags =
271 (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
272 int n;
273 boolean_t is64Bit = proc_is64bit(current_proc());
274
275 if (*flags & CPU_DTRACE_FAULT)
276 return;
277
278 if (pcstack_limit <= 0)
279 return;
280
281 /*
282 * If there's no user context we still need to zero the stack.
283 */
284 if (thread == NULL)
285 goto zero;
286
287 regs = (ppc_saved_state_t *)find_user_regs(thread);
288 if (regs == NULL)
289 goto zero;
290
291 *pcstack++ = (uint64_t)proc_selfpid();
292 pcstack_limit--;
293
294 if (pcstack_limit <= 0)
295 return;
296
297 pc = regs->REGPC;
298 sp = regs->REGSP;
299
300 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
301 *pcstack++ = (uint64_t)pc;
302 pcstack_limit--;
303 if (pcstack_limit <= 0)
304 return;
305
306 pc = regs->save_lr;
307 }
308
309 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_USTACK_FP)) {
310 /*
311 * If the ustack fp flag is set, the stack frame from sp to
312 * fp contains no valid call information. Start with the fp.
313 */
314 if (is64Bit)
315 sp = dtrace_fuword64(sp);
316 else
317 sp = (user_addr_t)dtrace_fuword32(sp);
318 }
319
320 n = dtrace_getustack_common(pcstack, pcstack_limit, pc, sp);
321 ASSERT(n >= 0);
322 ASSERT(n <= pcstack_limit);
323
324 pcstack += n;
325 pcstack_limit -= n;
326
327 zero:
328 while (pcstack_limit-- > 0)
329 *pcstack++ = 0;
330 }
331
332 int
333 dtrace_getustackdepth(void)
334 {
335 thread_t thread = current_thread();
336 ppc_saved_state_t *regs;
337 user_addr_t pc, sp;
338 int n = 0;
339 boolean_t is64Bit = proc_is64bit(current_proc());
340
341 if (thread == NULL)
342 return 0;
343
344 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
345 return (-1);
346
347 regs = (ppc_saved_state_t *)find_user_regs(thread);
348 if (regs == NULL)
349 return 0;
350
351 pc = regs->REGPC;
352 sp = regs->REGSP;
353
354 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
355 n++;
356 pc = regs->save_lr;
357 }
358
359 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_USTACK_FP)) {
360 /*
361 * If the ustack fp flag is set, the stack frame from sp to
362 * fp contains no valid call information. Start with the fp.
363 */
364 if (is64Bit)
365 sp = dtrace_fuword64(sp);
366 else
367 sp = (user_addr_t)dtrace_fuword32(sp);
368 }
369
370 n += dtrace_getustack_common(NULL, 0, pc, sp);
371
372 return (n);
373 }
374
375 void
376 dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
377 {
378 thread_t thread = current_thread();
379 ppc_saved_state_t *regs;
380 user_addr_t pc, sp;
381 volatile uint16_t *flags =
382 (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
383 #if 0
384 uintptr_t oldcontext;
385 size_t s1, s2;
386 #endif
387 boolean_t is64Bit = proc_is64bit(current_proc());
388
389 if (*flags & CPU_DTRACE_FAULT)
390 return;
391
392 if (pcstack_limit <= 0)
393 return;
394
395 /*
396 * If there's no user context we still need to zero the stack.
397 */
398 if (thread == NULL)
399 goto zero;
400
401 regs = (ppc_saved_state_t *)find_user_regs(thread);
402 if (regs == NULL)
403 goto zero;
404
405 *pcstack++ = (uint64_t)proc_selfpid();
406 pcstack_limit--;
407
408 if (pcstack_limit <= 0)
409 return;
410
411 pc = regs->REGPC;
412 sp = regs->REGSP;
413
414 #if 0 /* XXX signal stack crawl*/
415 oldcontext = lwp->lwp_oldcontext;
416
417 if (p->p_model == DATAMODEL_NATIVE) {
418 s1 = sizeof (struct frame) + 2 * sizeof (long);
419 s2 = s1 + sizeof (siginfo_t);
420 } else {
421 s1 = sizeof (struct frame32) + 3 * sizeof (int);
422 s2 = s1 + sizeof (siginfo32_t);
423 }
424 #endif
425
426 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
427 *pcstack++ = (uint64_t)pc;
428 *fpstack++ = 0;
429 pcstack_limit--;
430 if (pcstack_limit <= 0)
431 return;
432
433 /*
434 * XXX This is wrong, but we do not yet support stack helpers.
435 */
436 if (is64Bit)
437 pc = dtrace_fuword64(sp);
438 else
439 pc = dtrace_fuword32(sp);
440 }
441
442 while (pc != 0) {
443 *pcstack++ = (uint64_t)pc;
444 *fpstack++ = sp;
445 pcstack_limit--;
446 if (pcstack_limit <= 0)
447 break;
448
449 if (sp == 0)
450 break;
451
452 #if 0 /* XXX signal stack crawl*/
453 if (oldcontext == sp + s1 || oldcontext == sp + s2) {
454 if (p->p_model == DATAMODEL_NATIVE) {
455 ucontext_t *ucp = (ucontext_t *)oldcontext;
456 greg_t *gregs = ucp->uc_mcontext.gregs;
457
458 sp = dtrace_fulword(&gregs[REG_FP]);
459 pc = dtrace_fulword(&gregs[REG_PC]);
460
461 oldcontext = dtrace_fulword(&ucp->uc_link);
462 } else {
463 ucontext_t *ucp = (ucontext_t *)oldcontext;
464 greg_t *gregs = ucp->uc_mcontext.gregs;
465
466 sp = dtrace_fuword32(&gregs[EBP]);
467 pc = dtrace_fuword32(&gregs[EIP]);
468
469 oldcontext = dtrace_fuword32(&ucp->uc_link);
470 }
471 }
472 else
473 #endif
474 {
475 if (is64Bit) {
476 pc = dtrace_fuword64((sp + RETURN_OFFSET64));
477 sp = dtrace_fuword64(sp);
478 } else {
479 pc = dtrace_fuword32((sp + RETURN_OFFSET));
480 sp = dtrace_fuword32(sp);
481 }
482 }
483 }
484
485 zero:
486 while (pcstack_limit-- > 0)
487 *pcstack++ = 0;
488 }
489
490 void
491 dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes,
492 uint32_t *intrpc)
493 {
494 struct frame *fp = (struct frame *)__builtin_frame_address(0);
495 struct frame *nextfp, *minfp, *stacktop;
496 int depth = 0;
497 int last = 0;
498 uintptr_t pc;
499 uintptr_t caller = CPU->cpu_dtrace_caller;
500 int on_intr;
501
502 if ((on_intr = CPU_ON_INTR(CPU)) != 0)
503 stacktop = (struct frame *)dtrace_get_cpu_int_stack_top();
504 else
505 stacktop = (struct frame *)(dtrace_get_kernel_stack(current_thread()) + kernel_stack_size);
506
507 minfp = fp;
508
509 aframes++;
510
511 if (intrpc != NULL && depth < pcstack_limit)
512 pcstack[depth++] = (pc_t)intrpc;
513
514 while (depth < pcstack_limit) {
515 nextfp = *(struct frame **)fp;
516 pc = *(uintptr_t *)(((uintptr_t)fp) + RETURN_OFFSET);
517
518 if (nextfp <= minfp || nextfp >= stacktop) {
519 if (on_intr) {
520 /*
521 * Hop from interrupt stack to thread stack.
522 */
523 vm_offset_t kstack_base = dtrace_get_kernel_stack(current_thread());
524
525 minfp = (struct frame *)kstack_base;
526 stacktop = (struct frame *)(kstack_base + kernel_stack_size);
527
528 on_intr = 0;
529 continue;
530 }
531 /*
532 * This is the last frame we can process; indicate
533 * that we should return after processing this frame.
534 */
535 last = 1;
536 }
537
538 if (aframes > 0) {
539 if (--aframes == 0 && caller != 0) {
540 /*
541 * We've just run out of artificial frames,
542 * and we have a valid caller -- fill it in
543 * now.
544 */
545 ASSERT(depth < pcstack_limit);
546 pcstack[depth++] = (pc_t)caller;
547 caller = 0;
548 }
549 } else {
550 if (depth < pcstack_limit)
551 pcstack[depth++] = (pc_t)pc;
552 }
553
554 if (last) {
555 while (depth < pcstack_limit)
556 pcstack[depth++] = 0;
557 return;
558 }
559
560 fp = nextfp;
561 minfp = fp;
562 }
563 }
564
565 uint64_t
566 dtrace_getarg(int arg, int aframes)
567 {
568 #pragma unused(arg,aframes)
569 return 0xfeedfacedeafbeadLL; /* XXX Only called for arg >= 5 */
570 }
571
572 /*
573 * Load/Store Safety
574 */
575
576 void
577 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
578 {
579 /*
580 * "base" is the smallest toxic address in the range, "limit" is the first
581 * VALID address greater than "base".
582 */
583 func(0x0, VM_MIN_KERNEL_ADDRESS);
584 if (VM_MAX_KERNEL_ADDRESS < ~(uintptr_t)0)
585 func(VM_MAX_KERNEL_ADDRESS + 1, ~(uintptr_t)0);
586 }
587
588 extern void *mapping_phys_lookup(ppnum_t, unsigned int *);
589