]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/model_dep.c
90d8a05d914b91f00a4f92a25a882fecf97959fe
[apple/xnu.git] / osfmk / ppc / model_dep.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * @APPLE_FREE_COPYRIGHT@
30 */
31 /*
32 * (c) Copyright 1988 HEWLETT-PACKARD COMPANY
33 *
34 * To anyone who acknowledges that this file is provided "AS IS"
35 * without any express or implied warranty:
36 * permission to use, copy, modify, and distribute this file
37 * for any purpose is hereby granted without fee, provided that
38 * the above copyright notice and this notice appears in all
39 * copies, and that the name of Hewlett-Packard Company not be
40 * used in advertising or publicity pertaining to distribution
41 * of the software without specific, written prior permission.
42 * Hewlett-Packard Company makes no representations about the
43 * suitability of this software for any purpose.
44 */
45 /*
46 * Copyright (c) 1990,1991,1992,1994 The University of Utah and
47 * the Computer Systems Laboratory (CSL). All rights reserved.
48 *
49 * THE UNIVERSITY OF UTAH AND CSL PROVIDE THIS SOFTWARE IN ITS "AS IS"
50 * CONDITION, AND DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
51 * WHATSOEVER RESULTING FROM ITS USE.
52 *
53 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
54 * improvements that they make and grant CSL redistribution rights.
55 *
56 * Utah $Hdr: model_dep.c 1.34 94/12/14$
57 */
58
59 #include <debug.h>
60 #include <mach_kdb.h>
61 #include <mach_kdp.h>
62 #include <db_machine_commands.h>
63 #include <cpus.h>
64
65 #include <kern/thread.h>
66 #include <machine/pmap.h>
67 #include <machine/mach_param.h>
68 #include <device/device_types.h>
69
70 #include <mach/vm_param.h>
71 #include <mach/clock_types.h>
72 #include <mach/machine.h>
73 #include <mach/kmod.h>
74 #include <ppc/boot.h>
75
76 #include <kern/misc_protos.h>
77 #include <kern/startup.h>
78 #include <ppc/misc_protos.h>
79 #include <ppc/proc_reg.h>
80 #include <ppc/thread.h>
81 #include <ppc/asm.h>
82 #include <ppc/mem.h>
83 #include <ppc/Firmware.h>
84 #include <ppc/low_trace.h>
85 #include <ppc/mappings.h>
86 #include <ppc/FirmwareCalls.h>
87 #include <ppc/setjmp.h>
88 #include <ppc/exception.h>
89
90 #include <kern/clock.h>
91 #include <kern/debug.h>
92 #include <machine/trap.h>
93 #include <kern/spl.h>
94 #include <pexpert/pexpert.h>
95 #include <ppc/mp.h>
96
97 #include <IOKit/IOPlatformExpert.h>
98
99 #include <mach/vm_prot.h>
100 #include <vm/pmap.h>
101 #include <mach/time_value.h>
102 #include <machine/machparam.h> /* for btop */
103
104 #if MACH_KDB
105 #include <ddb/db_aout.h>
106 #include <ddb/db_output.h>
107 #include <ddb/db_command.h>
108 #include <machine/db_machdep.h>
109
110 extern struct db_command ppc_db_commands[];
111 #endif /* MACH_KDB */
112
113 char kernel_args_buf[256] = "/mach_kernel";
114 char boot_args_buf[256] = "/mach_servers/bootstrap";
115 char env_buf[256];
116
117 #define TRAP_DEBUGGER __asm__ volatile("tw 4,r3,r3");
118 #define TRAP_DEBUGGER_INST 0x7c831808
119 #define TRAP_DIRECT __asm__ volatile("tw 4,r4,r4");
120 #define TRAP_DIRECT_INST 0x7c842008
121 #define TRAP_INST_SIZE 4
122 #define BREAK_TO_KDP0 0x7fe00008
123 #define BREAK_TO_KDP1 0x7c800008
124 #define BREAK_TO_KDB0 0x7c810808
125
126 /*
127 * Code used to synchronize debuggers among all cpus, one active at a time, switch
128 * from on to another using kdb_on! #cpu or cpu #cpu
129 */
130
131 hw_lock_data_t debugger_lock; /* debugger lock */
132 hw_lock_data_t pbtlock; /* backtrace print lock */
133
134 int debugger_cpu = -1; /* current cpu running debugger */
135 int debugger_debug = 0; /* Debug debugger */
136 int debugger_is_slave[NCPUS]; /* Show that we were entered via sigp */
137 int debugger_active[NCPUS]; /* Debugger active on CPU */
138 int debugger_pending[NCPUS]; /* Debugger entry pending on CPU (this is a HACK) */
139 int debugger_holdoff[NCPUS]; /* Holdoff debugger entry on this CPU (this is a HACK) */
140 int db_run_mode; /* Debugger run mode */
141 unsigned int debugger_sync = 0; /* Cross processor debugger entry sync */
142 extern unsigned int NMIss; /* NMI debounce switch */
143
144 extern volatile int panicwait;
145 volatile unsigned int pbtcnt = 0;
146 volatile unsigned int pbtcpu = -1;
147
148 unsigned int lastTrace; /* Value of low-level exception trace controls */
149
150 volatile unsigned int cpus_holding_bkpts; /* counter for number of cpus holding
151 breakpoints (ie: cpus that did not
152 insert back breakpoints) */
153 void unlock_debugger(void);
154 void lock_debugger(void);
155 void dump_backtrace(unsigned int stackptr, unsigned int fence);
156 void dump_savearea(savearea *sv, unsigned int fence);
157
158 #if !MACH_KDB
159 boolean_t db_breakpoints_inserted = TRUE;
160 jmp_buf_t *db_recover = 0;
161 #endif
162
163 #if MACH_KDB
164 #include <ddb/db_run.h>
165 int kdb_flag=0;
166 extern boolean_t db_breakpoints_inserted;
167 extern jmp_buf_t *db_recover;
168 #define KDB_READY 0x1
169 #endif
170
171 #if MACH_KDP
172 extern int kdp_flag;
173 #define KDP_READY 0x1
174 #endif
175
176 boolean_t db_im_stepping = 0xFFFFFFFF; /* Remember if we were stepping */
177
178
179 char *failNames[] = {
180
181 "Debugging trap", /* failDebug */
182 "Corrupt stack", /* failStack */
183 "Corrupt mapping tables", /* failMapping */
184 "Corrupt context", /* failContext */
185 "No saveareas", /* failNoSavearea */
186 "Savearea corruption", /* failSaveareaCorr */
187 "Invalid live context", /* failBadLiveContext */
188 "Unknown failure code" /* Unknown failure code - must always be last */
189 };
190
191 char *invxcption = "Unknown code";
192
193 extern const char version[];
194 extern char *trap_type[];
195 extern vm_offset_t mem_actual;
196
197 #if !MACH_KDB
198 void kdb_trap(int type, struct savearea *regs);
199 void kdb_trap(int type, struct savearea *regs) {
200 return;
201 }
202 #endif
203
204 #if !MACH_KDP
205 void kdp_trap(int type, struct savearea *regs);
206 void kdp_trap(int type, struct savearea *regs) {
207 return;
208 }
209 #endif
210
211 void
212 machine_startup(boot_args *args)
213 {
214 int boot_arg;
215
216 if (PE_parse_boot_arg("cpus", &wncpu)) {
217 if (!((wncpu > 0) && (wncpu < NCPUS)))
218 wncpu = NCPUS;
219 } else
220 wncpu = NCPUS;
221
222 if( PE_get_hotkey( kPEControlKey ))
223 halt_in_debugger = halt_in_debugger ? 0 : 1;
224
225 if (PE_parse_boot_arg("debug", &boot_arg)) {
226 if (boot_arg & DB_HALT) halt_in_debugger=1;
227 if (boot_arg & DB_PRT) disableDebugOuput=FALSE;
228 if (boot_arg & DB_SLOG) systemLogDiags=TRUE;
229 if (boot_arg & DB_NMI) panicDebugging=TRUE;
230 if (boot_arg & DB_LOG_PI_SCRN) logPanicDataToScreen=TRUE;
231 }
232
233 hw_lock_init(&debugger_lock); /* initialize debugger lock */
234 hw_lock_init(&pbtlock); /* initialize print backtrace lock */
235
236 #if MACH_KDB
237 /*
238 * Initialize KDB
239 */
240 #if DB_MACHINE_COMMANDS
241 db_machine_commands_install(ppc_db_commands);
242 #endif /* DB_MACHINE_COMMANDS */
243 ddb_init();
244
245 if (boot_arg & DB_KDB)
246 current_debugger = KDB_CUR_DB;
247
248 /*
249 * Cause a breakpoint trap to the debugger before proceeding
250 * any further if the proper option bit was specified in
251 * the boot flags.
252 */
253 if (halt_in_debugger && (current_debugger == KDB_CUR_DB)) {
254 Debugger("inline call to debugger(machine_startup)");
255 halt_in_debugger = 0;
256 active_debugger =1;
257 }
258 #endif /* MACH_KDB */
259 if (PE_parse_boot_arg("preempt", &boot_arg)) {
260 extern int default_preemption_rate;
261
262 default_preemption_rate = boot_arg;
263 }
264 if (PE_parse_boot_arg("unsafe", &boot_arg)) {
265 extern int max_unsafe_quanta;
266
267 max_unsafe_quanta = boot_arg;
268 }
269 if (PE_parse_boot_arg("poll", &boot_arg)) {
270 extern int max_poll_quanta;
271
272 max_poll_quanta = boot_arg;
273 }
274 if (PE_parse_boot_arg("yield", &boot_arg)) {
275 extern int sched_poll_yield_shift;
276
277 sched_poll_yield_shift = boot_arg;
278 }
279
280 machine_conf();
281
282 ml_thrm_init(); /* Start thermal monitoring on this processor */
283
284 /*
285 * Start the system.
286 */
287 setup_main();
288
289 /* Should never return */
290 }
291
292 char *
293 machine_boot_info(
294 char *buf,
295 vm_size_t size)
296 {
297 return(PE_boot_args());
298 }
299
300 void
301 machine_conf(void)
302 {
303 machine_info.max_cpus = NCPUS;
304 machine_info.avail_cpus = 1;
305 machine_info.memory_size = mem_size;
306 }
307
308 void
309 machine_init(void)
310 {
311 clock_config();
312 }
313
314 void slave_machine_init(void)
315 {
316 (void) ml_set_interrupts_enabled(FALSE); /* Make sure we are disabled */
317 clock_init(); /* Init the clock */
318 cpu_machine_init(); /* Initialize the processor */
319 }
320
321 void
322 halt_all_cpus(boolean_t reboot)
323 {
324 if(reboot)
325 {
326 printf("MACH Reboot\n");
327 PEHaltRestart(kPERestartCPU);
328 }
329 else
330 {
331 printf("CPU halted\n");
332 PEHaltRestart(kPEHaltCPU);
333 }
334 while(1);
335 }
336
337 void
338 halt_cpu(void)
339 {
340 halt_all_cpus(FALSE);
341 }
342
343 #if MACH_ASSERT
344 /*
345 * Machine-dependent routine to fill in an array with up to callstack_max
346 * levels of return pc information.
347 */
348 void machine_callstack(
349 natural_t *buf,
350 vm_size_t callstack_max)
351 {
352 }
353 #endif /* MACH_ASSERT */
354
355
356 void
357 print_backtrace(struct savearea *ssp)
358 {
359 unsigned int stackptr, *raddr, *rstack, trans, fence;
360 int i, frames_cnt, skip_top_frames, frames_max;
361 unsigned int store[8]; /* Buffer for real storage reads */
362 vm_offset_t backtrace_entries[32];
363 thread_act_t *act;
364 savearea *sv, *svssp;
365 int cpu;
366
367 /*
368 * We need this lock to make sure we don't hang up when we double panic on an MP.
369 */
370
371 cpu = cpu_number(); /* Just who are we anyways? */
372 if(pbtcpu != cpu) { /* Allow recursion */
373 hw_atomic_add(&pbtcnt, 1); /* Remember we are trying */
374 while(!hw_lock_try(&pbtlock)); /* Spin here until we can get in. If we never do, well, we're crashing anyhow... */
375 pbtcpu = cpu; /* Mark it as us */
376 }
377
378 svssp = (savearea *)ssp; /* Make this easier */
379 sv = 0;
380 if(current_thread()) sv = (savearea *)current_act()->mact.pcb; /* Find most current savearea if system has started */
381
382 fence = 0xFFFFFFFF; /* Show we go all the way */
383 if(sv) fence = sv->save_r1; /* Stop at previous exception point */
384
385 if(!svssp) { /* Should we start from stack? */
386 kdb_printf("Latest stack backtrace for cpu %d:\n", cpu_number());
387 __asm__ volatile("mr %0,r1" : "=r" (stackptr)); /* Get current stack */
388 dump_backtrace(stackptr, fence); /* Dump the backtrace */
389 if(!sv) { /* Leave if no saveareas */
390 kdb_printf("\nKernel version:\n%s\n",version); /* Print kernel version */
391 hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */
392 return;
393 }
394 }
395 else { /* Were we passed an exception? */
396 fence = 0xFFFFFFFF; /* Show we go all the way */
397 if(svssp->save_hdr.save_prev) {
398 if((svssp->save_hdr.save_prev <= VM_MAX_KERNEL_ADDRESS) && ((unsigned int)LRA(PPC_SID_KERNEL, (void *)svssp->save_hdr.save_prev))) { /* Valid address? */
399 fence = svssp->save_hdr.save_prev->save_r1; /* Stop at previous exception point */
400 }
401 }
402
403 kdb_printf("Latest crash info for cpu %d:\n", cpu_number());
404 kdb_printf(" Exception state (sv=0x%08X)\n", sv);
405 dump_savearea(svssp, fence); /* Dump this savearea */
406 }
407
408 if(!sv) { /* Leave if no saveareas */
409 kdb_printf("\nKernel version:\n%s\n",version); /* Print kernel version */
410 hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */
411 return;
412 }
413
414 kdb_printf("Proceeding back via exception chain:\n");
415
416 while(sv) { /* Do them all... */
417 if(!((sv <= VM_MAX_KERNEL_ADDRESS) && (unsigned int)LRA(PPC_SID_KERNEL, (void *)sv))) { /* Valid address? */
418 kdb_printf(" Exception state (sv=0x%08X) Not mapped or invalid. stopping...\n", sv);
419 break;
420 }
421
422 kdb_printf(" Exception state (sv=0x%08X)\n", sv);
423 if(sv == svssp) { /* Did we dump it already? */
424 kdb_printf(" previously dumped as \"Latest\" state. skipping...\n");
425 }
426 else {
427 fence = 0xFFFFFFFF; /* Show we go all the way */
428 if(sv->save_hdr.save_prev) {
429 if((sv->save_hdr.save_prev <= VM_MAX_KERNEL_ADDRESS) && ((unsigned int)LRA(PPC_SID_KERNEL, (void *)sv->save_hdr.save_prev))) { /* Valid address? */
430 fence = sv->save_hdr.save_prev->save_r1; /* Stop at previous exception point */
431 }
432 }
433 dump_savearea(sv, fence); /* Dump this savearea */
434 }
435
436 sv = sv->save_hdr.save_prev; /* Back chain */
437 }
438
439 kdb_printf("\nKernel version:\n%s\n",version); /* Print kernel version */
440
441 pbtcpu = -1; /* Mark as unowned */
442 hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */
443 hw_atomic_sub(&pbtcnt, 1); /* Show we are done */
444
445 while(pbtcnt); /* Wait for completion */
446
447 return;
448 }
449
450 void dump_savearea(savearea *sv, unsigned int fence) {
451
452 char *xcode;
453
454 if(sv->save_exception > T_MAX) xcode = invxcption; /* Too big for table */
455 else xcode = trap_type[sv->save_exception / 4]; /* Point to the type */
456
457 kdb_printf(" PC=0x%08X; MSR=0x%08X; DAR=0x%08X; DSISR=0x%08X; LR=0x%08X; R1=0x%08X; XCP=0x%08X (%s)\n",
458 sv->save_srr0, sv->save_srr1, sv->save_dar, sv->save_dsisr,
459 sv->save_lr, sv->save_r1, sv->save_exception, xcode);
460
461 if(!(sv->save_srr1 & MASK(MSR_PR))) { /* Are we in the kernel? */
462 dump_backtrace(sv->save_r1, fence); /* Dump the stack back trace from here if not user state */
463 }
464
465 return;
466 }
467
468
469
470 #define DUMPFRAMES 32
471 #define LRindex 2
472
473 void dump_backtrace(unsigned int stackptr, unsigned int fence) {
474
475 unsigned int bframes[DUMPFRAMES];
476 unsigned int sframe[8], raddr, dumbo;
477 int i;
478
479 kdb_printf(" Backtrace:\n");
480 for(i = 0; i < DUMPFRAMES; i++) { /* Dump up to max frames */
481
482 if(!stackptr || (stackptr == fence)) break; /* Hit stop point or end... */
483
484 if(stackptr & 0x0000000f) { /* Is stack pointer valid? */
485 kdb_printf("\n backtrace terminated - unaligned frame address: 0x%08X\n", stackptr); /* No, tell 'em */
486 break;
487 }
488
489 raddr = (unsigned int)LRA(PPC_SID_KERNEL, (void *)stackptr); /* Get physical frame address */
490 if(!raddr || (stackptr > VM_MAX_KERNEL_ADDRESS)) { /* Is it mapped? */
491 kdb_printf("\n backtrace terminated - frame not mapped or invalid: 0x%08X\n", stackptr); /* No, tell 'em */
492 break;
493 }
494
495 if(raddr >= mem_actual) { /* Is it within physical RAM? */
496 kdb_printf("\n backtrace terminated - frame outside of RAM: v=0x%08X, p=%08X\n", stackptr, raddr); /* No, tell 'em */
497 break;
498 }
499
500 ReadReal(raddr, &sframe[0]); /* Fetch the stack frame */
501
502 bframes[i] = sframe[LRindex]; /* Save the link register */
503
504 if(!i) kdb_printf(" "); /* Indent first time */
505 else if(!(i & 7)) kdb_printf("\n "); /* Skip to new line every 8 */
506 kdb_printf("0x%08X ", bframes[i]); /* Dump the link register */
507
508 stackptr = sframe[0]; /* Chain back */
509 }
510 kdb_printf("\n");
511 if(i >= DUMPFRAMES) kdb_printf(" backtrace continues...\n"); /* Say we terminated early */
512 if(i) kmod_dump((vm_offset_t *)&bframes[0], i); /* Show what kmods are in trace */
513
514 }
515
516
517
518 void
519 Debugger(const char *message) {
520
521 int i;
522 unsigned int store[8];
523 unsigned long pi_size = 0;
524 spl_t spl;
525
526 spl = splhigh(); /* No interruptions from here on */
527
528 /*
529 * backtrace for Debugger() call from panic() if no current debugger
530 * backtrace and return for double panic() call
531 */
532 if ((panicstr != (char *)0) &&
533 (((nestedpanic != 0) && (current_debugger == 1)) || (active_debugger == 0))) {
534 print_backtrace(NULL);
535 if (nestedpanic != 0) {
536 splx(spl);
537 return; /* Yeah, don't enter again... */
538 }
539 }
540
541 if (debug_mode && debugger_active[cpu_number()]) { /* Are we already on debugger on this processor? */
542 splx(spl);
543 return; /* Yeah, don't do it again... */
544 }
545
546
547 /*
548 * The above stuff catches the double panic case so we shouldn't have to worry about that here.
549 */
550 if ( panicstr != (char *)0 )
551 {
552 /* diable kernel preemptions */
553 disable_preemption();
554
555 /* everything should be printed now so copy to NVRAM
556 */
557 if( debug_buf_size > 0)
558 pi_size = PESavePanicInfo( debug_buf, debug_buf_ptr - debug_buf);
559
560 if( !panicDebugging && (pi_size != 0) ) {
561 int my_cpu, debugger_cpu;
562 int tcpu;
563
564 my_cpu = cpu_number();
565 debugger_cpu = my_cpu;
566
567 hw_atomic_add(&debug_mode, 1);
568 debugger_active[my_cpu]++;
569 lock_debugger();
570
571 for(tcpu = 0; tcpu < NCPUS; tcpu++) {
572 if(tcpu == my_cpu) continue;
573 hw_atomic_add(&debugger_sync, 1);
574 (void)cpu_signal(tcpu, SIGPdebug, 0 ,0);
575 }
576 (void)hw_cpu_sync(&debugger_sync, LockTimeOut);
577 debugger_sync = 0;
578 }
579
580 draw_panic_dialog();
581
582 if( !panicDebugging && (pi_size != 0))
583 PEHaltRestart( kPEHangCPU );
584
585 enable_preemption();
586 }
587
588
589 if ((current_debugger != NO_CUR_DB)) { /* If there is a debugger configured, enter it */
590 printf("Debugger(%s)\n", message);
591 TRAP_DEBUGGER;
592 splx(spl);
593 return; /* Done debugging for a while */
594 }
595
596 printf("\nNo debugger configured - dumping debug information\n");
597 printf("MSR=%08X\n",mfmsr());
598 print_backtrace(NULL);
599 splx(spl);
600 return;
601 }
602
603 /*
604 * Here's where we attempt to get some diagnostic information dumped out
605 * when the system is really confused. We will try to get into the
606 * debugger as well.
607 *
608 * We are here with interrupts disabled and on the debug stack. The savearea
609 * that was passed in is NOT chained to the activation.
610 *
611 * save_r3 contains the failure reason code.
612 */
613
614 void SysChoked(int type, savearea *sv) { /* The system is bad dead */
615
616 unsigned int failcode;
617
618 mp_disable_preemption();
619 disableDebugOuput = FALSE;
620 debug_mode = TRUE;
621
622 failcode = sv->save_r3; /* Get the failure code */
623 if(failcode > failUnknown) failcode = failUnknown; /* Set unknown code code */
624
625 kprintf("System Failure: cpu=%d; code=%08X (%s)\n", cpu_number(), sv->save_r3, failNames[failcode]);
626 kdb_printf("System Failure: cpu=%d; code=%08X (%s)\n", cpu_number(), sv->save_r3, failNames[failcode]);
627
628 print_backtrace(sv); /* Attempt to print backtrace */
629 Call_DebuggerC(type, sv); /* Attempt to get into debugger */
630
631 if ((current_debugger != NO_CUR_DB)) Call_DebuggerC(type, sv); /* Attempt to get into debugger */
632
633 }
634
635
636
637 /*
638 * When we get here, interruptions are disabled and we are on the debugger stack
639 * Never, ever, ever, ever enable interruptions from here on
640 */
641
642 int Call_DebuggerC(
643 int type,
644 struct savearea *saved_state)
645 {
646 int directcall, wait;
647 vm_offset_t instr_ptr;
648 unsigned int instr;
649 int my_cpu, tcpu;
650
651 my_cpu = cpu_number(); /* Get our CPU */
652
653 #if MACH_KDB
654 if((debugger_cpu == my_cpu) && /* Do we already own debugger? */
655 debugger_active[my_cpu] && /* and are we really active? */
656 db_recover && /* and have we set up recovery? */
657 (current_debugger == KDB_CUR_DB)) { /* and are we in KDB (only it handles recovery) */
658 kdb_trap(type, saved_state); /* Then reenter it... */
659 }
660 #endif
661
662 hw_atomic_add(&debug_mode, 1); /* Indicate we are in debugger */
663 debugger_active[my_cpu]++; /* Show active on our CPU */
664 lock_debugger(); /* Insure that only one CPU is in debugger */
665
666 if(db_im_stepping == my_cpu) { /* Are we just back from a step? */
667 enable_preemption_no_check(); /* Enable preemption now */
668 db_im_stepping = 0xFFFFFFFF; /* Nobody stepping right now */
669 }
670
671 if (debugger_debug) {
672 #if 0
673 kprintf("Call_DebuggerC(%d): %08X %08X, debact = %d\n", my_cpu, type, saved_state, debug_mode); /* (TEST/DEBUG) */
674 #endif
675 printf("Call_Debugger: enter - cpu %d, is_slave %d, debugger_cpu %d, pc %08X\n",
676 my_cpu, debugger_is_slave[my_cpu], debugger_cpu, saved_state->save_srr0);
677 }
678
679 if (instr_ptr = (vm_offset_t)LRA(PPC_SID_KERNEL, (void *)(saved_state->save_srr0))) {
680 instr = ml_phys_read(instr_ptr); /* Get the trap that caused entry */
681 }
682 else instr = 0;
683
684 #if 0
685 if (debugger_debug) kprintf("Call_DebuggerC(%d): instr_ptr = %08X, instr = %08X\n", my_cpu, instr_ptr, instr); /* (TEST/DEBUG) */
686 #endif
687
688 if (db_breakpoints_inserted) cpus_holding_bkpts++; /* Bump up the holding count */
689 if (debugger_cpu == -1 && !debugger_is_slave[my_cpu]) {
690 #if 0
691 if (debugger_debug) kprintf("Call_DebuggerC(%d): lasttrace = %08X\n", my_cpu, lastTrace); /* (TEST/DEBUG) */
692 #endif
693 debugger_cpu = my_cpu; /* Show that we are debugger */
694 lastTrace = LLTraceSet(0); /* Disable low-level tracing */
695
696 for(tcpu = 0; tcpu < NCPUS; tcpu++) { /* Stop all the other guys */
697 if(tcpu == my_cpu) continue; /* Don't diddle ourselves */
698 hw_atomic_add(&debugger_sync, 1); /* Count signal sent */
699 (void)cpu_signal(tcpu, SIGPdebug, 0 ,0); /* Tell 'em to enter debugger */
700 }
701 (void)hw_cpu_sync(&debugger_sync, LockTimeOut); /* Wait for the other processors to enter debug */
702 debugger_sync = 0; /* We're done with it */
703 }
704 else if (debugger_cpu != my_cpu) goto debugger_exit; /* We are not debugger, don't continue... */
705
706
707 if (instr == TRAP_DIRECT_INST) {
708 disableDebugOuput = FALSE;
709 print_backtrace(saved_state);
710 }
711
712 switch_debugger = 0; /* Make sure switch request is off */
713 directcall = 1; /* Assume direct call */
714
715 if (saved_state->save_srr1 & MASK(SRR1_PRG_TRAP)) { /* Trap instruction? */
716
717 directcall = 0; /* We had a trap not a direct call */
718
719 switch (instr) { /* Select trap type */
720
721 #if MACH_KDP
722 case BREAK_TO_KDP0: /* Breakpoint into KDP? */
723 case BREAK_TO_KDP1: /* Breakpoint into KDP? */
724 current_debugger = KDP_CUR_DB; /* Yes, set KDP */
725 kdp_trap(type, saved_state); /* Enter it */
726 break;
727 #endif
728
729 #if MACH_KDB
730 case BREAK_TO_KDB0: /* Breakpoint to KDB (the "good" debugger)? */
731 current_debugger = KDB_CUR_DB; /* Yes, set it */
732 kdb_trap(type, saved_state); /* Enter it */
733 break;
734 #endif
735
736 case TRAP_DEBUGGER_INST: /* Should we enter the current debugger? */
737 case TRAP_DIRECT_INST: /* Should we enter the current debugger? */
738 if (current_debugger == KDP_CUR_DB) /* Is current KDP? */
739 kdp_trap(type, saved_state); /* Yes, enter it */
740 else if (current_debugger == KDB_CUR_DB) /* Is this KDB? */
741 kdb_trap(type, saved_state); /* Yes, go ahead and enter */
742 else goto debugger_error; /* No debugger active */
743 break;
744
745 default: /* Unknown/bogus trap type */
746 goto debugger_error;
747 }
748 }
749
750 while(1) { /* We are here to handle debugger switches */
751
752 if(!directcall) { /* Was this a direct call? */
753 if(!switch_debugger) break; /* No, then leave if no switch requested... */
754
755 /*
756 * Note: we can only switch to a debugger we have. Ignore bogus switch requests.
757 */
758 #if 0
759 if (debugger_debug) kprintf("Call_DebuggerC(%d): switching debuggers\n", my_cpu); /* (TEST/DEBUG) */
760 #endif
761 #if MACH_KDB
762 if(current_debugger == KDP_CUR_DB) current_debugger = KDB_CUR_DB; /* Switch to KDB */
763 #if MACH_KDP
764 else
765 #endif
766 #endif
767 #if MACH_KDP
768 if(current_debugger == KDB_CUR_DB) current_debugger = KDP_CUR_DB; /* Switch to KDP */
769 #endif
770 }
771
772 switch_debugger = 0; /* Clear request */
773 directcall = 0; /* Clear first-time direct call indication */
774
775 switch (current_debugger) { /* Enter correct debugger */
776
777 case KDP_CUR_DB: /* Enter KDP */
778 kdp_trap(type, saved_state);
779 break;
780
781 case KDB_CUR_DB: /* Enter KDB */
782 kdb_trap(type, saved_state);
783 break;
784
785 default: /* No debugger installed */
786 goto debugger_error;
787 break;
788 }
789 }
790
791 debugger_exit:
792 #if 0
793 if (debugger_debug) kprintf("Call_DebuggerC(%d): exit - inst = %08X, cpu=%d(%d), run=%d\n", my_cpu,
794 instr, my_cpu, debugger_cpu, db_run_mode); /* (TEST/DEBUG) */
795 #endif
796 if ((instr == TRAP_DEBUGGER_INST) || /* Did we trap to enter debugger? */
797 (instr == TRAP_DIRECT_INST)) saved_state->save_srr0 += TRAP_INST_SIZE; /* Yes, point past trap */
798
799 if(debugger_cpu == my_cpu) LLTraceSet(lastTrace); /* Enable tracing on the way out if we are debugger */
800
801 wait = FALSE; /* Assume we are not going to wait */
802 if (db_run_mode == STEP_CONTINUE) { /* Are we going to run? */
803 wait = TRUE; /* Yeah, remember to wait for breakpoints to clear */
804 debugger_cpu = -1; /* Release other processor's debuggers */
805 debugger_pending[0] = 0; /* Release request (this is a HACK) */
806 debugger_pending[1] = 0; /* Release request (this is a HACK) */
807 NMIss = 0; /* Let NMI bounce */
808 }
809
810 if(db_run_mode == STEP_ONCE) { /* Are we about to step? */
811 disable_preemption(); /* Disable preemption for the step */
812 db_im_stepping = my_cpu; /* Remember that I am about to step */
813 }
814
815 if (db_breakpoints_inserted) cpus_holding_bkpts--; /* If any breakpoints, back off count */
816 if (debugger_is_slave[my_cpu]) debugger_is_slave[my_cpu]--; /* If we were a slove, uncount us */
817 if (debugger_debug)
818 printf("Call_Debugger: exit - cpu %d, debugger_cpu %d, run_mode %d holds %d\n",
819 my_cpu, debugger_cpu, db_run_mode,
820 cpus_holding_bkpts);
821
822 unlock_debugger(); /* Release the lock */
823 debugger_active[my_cpu]--; /* Say we aren't active anymore */
824
825 if (wait) while(cpus_holding_bkpts); /* Wait for breakpoints to clear */
826
827 hw_atomic_sub(&debug_mode, 1); /* Set out of debug now */
828
829 return(1); /* Exit debugger normally */
830
831 debugger_error:
832 if(db_run_mode != STEP_ONCE) enable_preemption_no_check(); /* Enable preemption, but don't preempt here */
833 hw_atomic_sub(&debug_mode, 1); /* Set out of debug now */
834 return(0); /* Return in shame... */
835
836 }
837
838 void lock_debugger(void) {
839 int my_cpu;
840 register int i;
841
842 my_cpu = cpu_number(); /* Get our CPU number */
843
844 while(1) { /* Check until we get it */
845
846 if (debugger_cpu != -1 && debugger_cpu != my_cpu) continue; /* Someone, not us, is debugger... */
847 if (hw_lock_try(&debugger_lock)) { /* Get the debug lock */
848 if (debugger_cpu == -1 || debugger_cpu == my_cpu) break; /* Is it us? */
849 hw_lock_unlock(&debugger_lock); /* Not us, release lock */
850 }
851 }
852 }
853
854 void unlock_debugger(void) {
855
856 hw_lock_unlock(&debugger_lock);
857
858 }
859
860