2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
29 * @APPLE_FREE_COPYRIGHT@
32 * (c) Copyright 1988 HEWLETT-PACKARD COMPANY
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.
46 * Copyright (c) 1990,1991,1992,1994 The University of Utah and
47 * the Computer Systems Laboratory (CSL). All rights reserved.
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.
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.
56 * Utah $Hdr: model_dep.c 1.34 94/12/14$
62 #include <db_machine_commands.h>
65 #include <kern/thread.h>
66 #include <machine/pmap.h>
67 #include <machine/mach_param.h>
68 #include <device/device_types.h>
70 #include <mach/vm_param.h>
71 #include <mach/clock_types.h>
72 #include <mach/machine.h>
73 #include <mach/kmod.h>
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>
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 #include <ppc/hw_perfmon.h>
91 #include <kern/clock.h>
92 #include <kern/debug.h>
93 #include <machine/trap.h>
95 #include <pexpert/pexpert.h>
98 #include <IOKit/IOPlatformExpert.h>
100 #include <mach/vm_prot.h>
102 #include <mach/time_value.h>
103 #include <machine/machparam.h> /* for btop */
106 #include <ddb/db_aout.h>
107 #include <ddb/db_output.h>
108 #include <ddb/db_command.h>
109 #include <machine/db_machdep.h>
111 extern struct db_command ppc_db_commands
[];
112 #endif /* MACH_KDB */
114 char kernel_args_buf
[256] = "/mach_kernel";
115 char boot_args_buf
[256] = "/mach_servers/bootstrap";
118 #define TRAP_DEBUGGER __asm__ volatile("tw 4,r3,r3");
119 #define TRAP_DEBUGGER_INST 0x7c831808
120 #define TRAP_DIRECT __asm__ volatile("tw 4,r4,r4");
121 #define TRAP_DIRECT_INST 0x7c842008
122 #define TRAP_INST_SIZE 4
123 #define BREAK_TO_KDP0 0x7fe00008
124 #define BREAK_TO_KDP1 0x7c800008
125 #define BREAK_TO_KDB0 0x7c810808
128 * Code used to synchronize debuggers among all cpus, one active at a time, switch
129 * from on to another using kdb_on! #cpu or cpu #cpu
132 hw_lock_data_t debugger_lock
; /* debugger lock */
133 hw_lock_data_t pbtlock
; /* backtrace print lock */
135 int debugger_cpu
= -1; /* current cpu running debugger */
136 int debugger_debug
= 0; /* Debug debugger */
137 int debugger_is_slave
[NCPUS
]; /* Show that we were entered via sigp */
138 int debugger_active
[NCPUS
]; /* Debugger active on CPU */
139 int debugger_pending
[NCPUS
]; /* Debugger entry pending on CPU (this is a HACK) */
140 int debugger_holdoff
[NCPUS
]; /* Holdoff debugger entry on this CPU (this is a HACK) */
141 int db_run_mode
; /* Debugger run mode */
142 unsigned int debugger_sync
= 0; /* Cross processor debugger entry sync */
143 extern unsigned int NMIss
; /* NMI debounce switch */
145 extern volatile int panicwait
;
146 volatile unsigned int pbtcnt
= 0;
147 volatile unsigned int pbtcpu
= -1;
149 unsigned int lastTrace
; /* Value of low-level exception trace controls */
151 volatile unsigned int cpus_holding_bkpts
; /* counter for number of cpus holding
152 breakpoints (ie: cpus that did not
153 insert back breakpoints) */
154 void unlock_debugger(void);
155 void lock_debugger(void);
156 void dump_backtrace(unsigned int stackptr
, unsigned int fence
);
157 void dump_savearea(savearea
*sv
, unsigned int fence
);
160 boolean_t db_breakpoints_inserted
= TRUE
;
161 jmp_buf_t
*db_recover
= 0;
165 #include <ddb/db_run.h>
167 extern boolean_t db_breakpoints_inserted
;
168 extern jmp_buf_t
*db_recover
;
169 #define KDB_READY 0x1
174 #define KDP_READY 0x1
177 boolean_t db_im_stepping
= 0xFFFFFFFF; /* Remember if we were stepping */
180 char *failNames
[] = {
182 "Debugging trap", /* failDebug */
183 "Corrupt stack", /* failStack */
184 "Corrupt mapping tables", /* failMapping */
185 "Corrupt context", /* failContext */
186 "No saveareas", /* failNoSavearea */
187 "Savearea corruption", /* failSaveareaCorr */
188 "Invalid live context", /* failBadLiveContext */
189 "Unaligned stack", /* failUnalignedStk */
190 "Unknown failure code" /* Unknown failure code - must always be last */
193 char *invxcption
= "Unknown code";
195 extern const char version
[];
196 extern char *trap_type
[];
199 void kdb_trap(int type
, struct savearea
*regs
);
200 void kdb_trap(int type
, struct savearea
*regs
) {
206 void kdp_trap(int type
, struct savearea
*regs
);
207 void kdp_trap(int type
, struct savearea
*regs
) {
213 machine_startup(boot_args
*args
)
217 if (PE_parse_boot_arg("cpus", &wncpu
)) {
218 if (!((wncpu
> 0) && (wncpu
< NCPUS
)))
223 if( PE_get_hotkey( kPEControlKey
))
224 halt_in_debugger
= halt_in_debugger
? 0 : 1;
226 if (PE_parse_boot_arg("debug", &boot_arg
)) {
227 if (boot_arg
& DB_HALT
) halt_in_debugger
=1;
228 if (boot_arg
& DB_PRT
) disableDebugOuput
=FALSE
;
229 if (boot_arg
& DB_SLOG
) systemLogDiags
=TRUE
;
230 if (boot_arg
& DB_NMI
) panicDebugging
=TRUE
;
231 if (boot_arg
& DB_LOG_PI_SCRN
) logPanicDataToScreen
=TRUE
;
234 hw_lock_init(&debugger_lock
); /* initialize debugger lock */
235 hw_lock_init(&pbtlock
); /* initialize print backtrace lock */
241 #if DB_MACHINE_COMMANDS
242 db_machine_commands_install(ppc_db_commands
);
243 #endif /* DB_MACHINE_COMMANDS */
246 if (boot_arg
& DB_KDB
)
247 current_debugger
= KDB_CUR_DB
;
250 * Cause a breakpoint trap to the debugger before proceeding
251 * any further if the proper option bit was specified in
254 if (halt_in_debugger
&& (current_debugger
== KDB_CUR_DB
)) {
255 Debugger("inline call to debugger(machine_startup)");
256 halt_in_debugger
= 0;
259 #endif /* MACH_KDB */
260 if (PE_parse_boot_arg("preempt", &boot_arg
)) {
261 extern int default_preemption_rate
;
263 default_preemption_rate
= boot_arg
;
265 if (PE_parse_boot_arg("unsafe", &boot_arg
)) {
266 extern int max_unsafe_quanta
;
268 max_unsafe_quanta
= boot_arg
;
270 if (PE_parse_boot_arg("poll", &boot_arg
)) {
271 extern int max_poll_quanta
;
273 max_poll_quanta
= boot_arg
;
275 if (PE_parse_boot_arg("yield", &boot_arg
)) {
276 extern int sched_poll_yield_shift
;
278 sched_poll_yield_shift
= boot_arg
;
283 ml_thrm_init(); /* Start thermal monitoring on this processor */
290 /* Should never return */
298 return(PE_boot_args());
304 machine_info
.max_cpus
= NCPUS
;
305 machine_info
.avail_cpus
= 1;
306 machine_info
.memory_size
= mem_size
; /* Note that this will be 2 GB for >= 2 GB machines */
316 void slave_machine_init(void)
318 (void) ml_set_interrupts_enabled(FALSE
); /* Make sure we are disabled */
319 clock_init(); /* Init the clock */
320 cpu_machine_init(); /* Initialize the processor */
324 halt_all_cpus(boolean_t reboot
)
328 printf("MACH Reboot\n");
329 PEHaltRestart(kPERestartCPU
);
333 printf("CPU halted\n");
334 PEHaltRestart(kPEHaltCPU
);
342 halt_all_cpus(FALSE
);
347 * Machine-dependent routine to fill in an array with up to callstack_max
348 * levels of return pc information.
350 void machine_callstack(
352 vm_size_t callstack_max
)
355 #endif /* MACH_ASSERT */
359 print_backtrace(struct savearea
*ssp
)
361 unsigned int stackptr
, *raddr
, *rstack
, trans
, fence
;
362 int i
, frames_cnt
, skip_top_frames
, frames_max
;
363 unsigned int store
[8]; /* Buffer for real storage reads */
364 vm_offset_t backtrace_entries
[32];
366 savearea
*sv
, *svssp
;
371 * We need this lock to make sure we don't hang up when we double panic on an MP.
374 cpu
= cpu_number(); /* Just who are we anyways? */
375 if(pbtcpu
!= cpu
) { /* Allow recursion */
376 hw_atomic_add(&pbtcnt
, 1); /* Remember we are trying */
377 while(!hw_lock_try(&pbtlock
)); /* Spin here until we can get in. If we never do, well, we're crashing anyhow... */
378 pbtcpu
= cpu
; /* Mark it as us */
381 svssp
= (savearea
*)ssp
; /* Make this easier */
383 if(current_thread()) sv
= (savearea
*)current_act()->mact
.pcb
; /* Find most current savearea if system has started */
385 fence
= 0xFFFFFFFF; /* Show we go all the way */
386 if(sv
) fence
= (unsigned int)sv
->save_r1
; /* Stop at previous exception point */
388 if(!svssp
) { /* Should we start from stack? */
389 kdb_printf("Latest stack backtrace for cpu %d:\n", cpu_number());
390 __asm__
volatile("mr %0,r1" : "=r" (stackptr
)); /* Get current stack */
391 dump_backtrace(stackptr
, fence
); /* Dump the backtrace */
392 if(!sv
) { /* Leave if no saveareas */
393 kdb_printf("\nKernel version:\n%s\n",version
); /* Print kernel version */
394 hw_lock_unlock(&pbtlock
); /* Allow another back trace to happen */
398 else { /* Were we passed an exception? */
399 fence
= 0xFFFFFFFF; /* Show we go all the way */
400 if(svssp
->save_hdr
.save_prev
) {
401 if((svssp
->save_hdr
.save_prev
<= vm_last_addr
) && ((unsigned int)pmap_find_phys(kernel_pmap
, (addr64_t
)svssp
->save_hdr
.save_prev
))) { /* Valid address? */
402 psv
= (savearea
*)((unsigned int)svssp
->save_hdr
.save_prev
); /* Get the 64-bit back chain converted to a regualr pointer */
403 fence
= (unsigned int)psv
->save_r1
; /* Stop at previous exception point */
407 kdb_printf("Latest crash info for cpu %d:\n", cpu_number());
408 kdb_printf(" Exception state (sv=0x%08X)\n", sv
);
409 dump_savearea(svssp
, fence
); /* Dump this savearea */
412 if(!sv
) { /* Leave if no saveareas */
413 kdb_printf("\nKernel version:\n%s\n",version
); /* Print kernel version */
414 hw_lock_unlock(&pbtlock
); /* Allow another back trace to happen */
418 kdb_printf("Proceeding back via exception chain:\n");
420 while(sv
) { /* Do them all... */
421 if(!((sv
<= vm_last_addr
) && (unsigned int)pmap_find_phys(kernel_pmap
, (addr64_t
)sv
))) { /* Valid address? */
422 kdb_printf(" Exception state (sv=0x%08X) Not mapped or invalid. stopping...\n", sv
);
426 kdb_printf(" Exception state (sv=0x%08X)\n", sv
);
427 if(sv
== svssp
) { /* Did we dump it already? */
428 kdb_printf(" previously dumped as \"Latest\" state. skipping...\n");
431 fence
= 0xFFFFFFFF; /* Show we go all the way */
432 if(sv
->save_hdr
.save_prev
) {
433 if((sv
->save_hdr
.save_prev
<= vm_last_addr
) && ((unsigned int)pmap_find_phys(kernel_pmap
, (addr64_t
)sv
->save_hdr
.save_prev
))) { /* Valid address? */
434 psv
= (savearea
*)((unsigned int)sv
->save_hdr
.save_prev
); /* Get the 64-bit back chain converted to a regualr pointer */
435 fence
= (unsigned int)psv
->save_r1
; /* Stop at previous exception point */
438 dump_savearea(sv
, fence
); /* Dump this savearea */
441 sv
= (savearea
*)sv
->save_hdr
.save_prev
; /* Back chain */
444 kdb_printf("\nKernel version:\n%s\n",version
); /* Print kernel version */
446 pbtcpu
= -1; /* Mark as unowned */
447 hw_lock_unlock(&pbtlock
); /* Allow another back trace to happen */
448 hw_atomic_sub(&pbtcnt
, 1); /* Show we are done */
450 while(pbtcnt
); /* Wait for completion */
455 void dump_savearea(savearea
*sv
, unsigned int fence
) {
459 if(sv
->save_exception
> T_MAX
) xcode
= invxcption
; /* Too big for table */
460 else xcode
= trap_type
[sv
->save_exception
/ 4]; /* Point to the type */
462 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",
463 (unsigned int)sv
->save_srr0
, (unsigned int)sv
->save_srr1
, (unsigned int)sv
->save_dar
, sv
->save_dsisr
,
464 (unsigned int)sv
->save_lr
, (unsigned int)sv
->save_r1
, sv
->save_exception
, xcode
);
466 if(!(sv
->save_srr1
& MASK(MSR_PR
))) { /* Are we in the kernel? */
467 dump_backtrace((unsigned int)sv
->save_r1
, fence
); /* Dump the stack back trace from here if not user state */
475 #define DUMPFRAMES 32
478 void dump_backtrace(unsigned int stackptr
, unsigned int fence
) {
480 unsigned int bframes
[DUMPFRAMES
];
481 unsigned int sframe
[8], raddr
, dumbo
;
484 kdb_printf(" Backtrace:\n");
485 for(i
= 0; i
< DUMPFRAMES
; i
++) { /* Dump up to max frames */
487 if(!stackptr
|| (stackptr
== fence
)) break; /* Hit stop point or end... */
489 if(stackptr
& 0x0000000F) { /* Is stack pointer valid? */
490 kdb_printf("\n backtrace terminated - unaligned frame address: 0x%08X\n", stackptr
); /* No, tell 'em */
494 raddr
= (unsigned int)pmap_find_phys(kernel_pmap
, (addr64_t
)stackptr
); /* Get physical frame address */
495 if(!raddr
|| (stackptr
> vm_last_addr
)) { /* Is it mapped? */
496 kdb_printf("\n backtrace terminated - frame not mapped or invalid: 0x%08X\n", stackptr
); /* No, tell 'em */
500 if(!mapping_phys_lookup(raddr
, &dumbo
)) { /* Is it within physical RAM? */
501 kdb_printf("\n backtrace terminated - frame outside of RAM: v=0x%08X, p=%08X\n", stackptr
, raddr
); /* No, tell 'em */
505 ReadReal((addr64_t
)((raddr
<< 12) | (stackptr
& 4095)), &sframe
[0]); /* Fetch the stack frame */
507 bframes
[i
] = sframe
[LRindex
]; /* Save the link register */
509 if(!i
) kdb_printf(" "); /* Indent first time */
510 else if(!(i
& 7)) kdb_printf("\n "); /* Skip to new line every 8 */
511 kdb_printf("0x%08X ", bframes
[i
]); /* Dump the link register */
513 stackptr
= sframe
[0]; /* Chain back */
516 if(i
>= DUMPFRAMES
) kdb_printf(" backtrace continues...\n"); /* Say we terminated early */
517 if(i
) kmod_dump((vm_offset_t
*)&bframes
[0], i
); /* Show what kmods are in trace */
524 Debugger(const char *message
) {
527 unsigned int store
[8];
528 unsigned long pi_size
= 0;
531 spl
= splhigh(); /* No interruptions from here on */
534 * backtrace for Debugger() call from panic() if no current debugger
535 * backtrace and return for double panic() call
537 if ((panicstr
!= (char *)0) &&
538 (((nestedpanic
!= 0) && (current_debugger
== 1)) || (active_debugger
== 0))) {
539 print_backtrace(NULL
);
540 if (nestedpanic
!= 0) {
542 return; /* Yeah, don't enter again... */
546 if (debug_mode
&& debugger_active
[cpu_number()]) { /* Are we already on debugger on this processor? */
548 return; /* Yeah, don't do it again... */
553 * The above stuff catches the double panic case so we shouldn't have to worry about that here.
555 if ( panicstr
!= (char *)0 )
557 /* diable kernel preemptions */
558 disable_preemption();
560 /* everything should be printed now so copy to NVRAM
562 if( debug_buf_size
> 0)
563 pi_size
= PESavePanicInfo( debug_buf
, debug_buf_ptr
- debug_buf
);
565 if( !panicDebugging
&& (pi_size
!= 0) ) {
566 int my_cpu
, debugger_cpu
;
569 my_cpu
= cpu_number();
570 debugger_cpu
= my_cpu
;
572 hw_atomic_add(&debug_mode
, 1);
573 debugger_active
[my_cpu
]++;
576 for(tcpu
= 0; tcpu
< NCPUS
; tcpu
++) {
577 if(tcpu
== my_cpu
) continue;
578 hw_atomic_add(&debugger_sync
, 1);
579 (void)cpu_signal(tcpu
, SIGPdebug
, 0 ,0);
581 (void)hw_cpu_sync(&debugger_sync
, LockTimeOut
);
587 if( !panicDebugging
&& (pi_size
!= 0))
588 PEHaltRestart( kPEHangCPU
);
594 if ((current_debugger
!= NO_CUR_DB
)) { /* If there is a debugger configured, enter it */
595 printf("Debugger(%s)\n", message
);
598 return; /* Done debugging for a while */
601 printf("\nNo debugger configured - dumping debug information\n");
602 printf("MSR=%08X\n",mfmsr());
603 print_backtrace(NULL
);
609 * Here's where we attempt to get some diagnostic information dumped out
610 * when the system is really confused. We will try to get into the
613 * We are here with interrupts disabled and on the debug stack. The savearea
614 * that was passed in is NOT chained to the activation.
616 * save_r3 contains the failure reason code.
619 void SysChoked(int type
, savearea
*sv
) { /* The system is bad dead */
621 unsigned int failcode
;
623 mp_disable_preemption();
624 disableDebugOuput
= FALSE
;
627 failcode
= (unsigned int)sv
->save_r3
; /* Get the failure code */
628 if(failcode
> failUnknown
) failcode
= failUnknown
; /* Set unknown code code */
630 kprintf("System Failure: cpu=%d; code=%08X (%s)\n", cpu_number(), (unsigned int)sv
->save_r3
, failNames
[failcode
]);
631 kdb_printf("System Failure: cpu=%d; code=%08X (%s)\n", cpu_number(), (unsigned int)sv
->save_r3
, failNames
[failcode
]);
633 print_backtrace(sv
); /* Attempt to print backtrace */
634 Call_DebuggerC(type
, sv
); /* Attempt to get into debugger */
636 if ((current_debugger
!= NO_CUR_DB
)) Call_DebuggerC(type
, sv
); /* Attempt to get into debugger */
643 * When we get here, interruptions are disabled and we are on the debugger stack
644 * Never, ever, ever, ever enable interruptions from here on
649 struct savearea
*saved_state
)
651 int directcall
, wait
;
657 my_cpu
= cpu_number(); /* Get our CPU */
660 if((debugger_cpu
== my_cpu
) && /* Do we already own debugger? */
661 debugger_active
[my_cpu
] && /* and are we really active? */
662 db_recover
&& /* and have we set up recovery? */
663 (current_debugger
== KDB_CUR_DB
)) { /* and are we in KDB (only it handles recovery) */
664 kdb_trap(type
, saved_state
); /* Then reenter it... */
668 hw_atomic_add(&debug_mode
, 1); /* Indicate we are in debugger */
669 debugger_active
[my_cpu
]++; /* Show active on our CPU */
670 lock_debugger(); /* Insure that only one CPU is in debugger */
672 if(db_im_stepping
== my_cpu
) { /* Are we just back from a step? */
673 enable_preemption_no_check(); /* Enable preemption now */
674 db_im_stepping
= 0xFFFFFFFF; /* Nobody stepping right now */
677 if (debugger_debug
) {
679 kprintf("Call_DebuggerC(%d): %08X %08X, debact = %d\n", my_cpu
, type
, saved_state
, debug_mode
); /* (TEST/DEBUG) */
681 printf("Call_Debugger: enter - cpu %d, is_slave %d, debugger_cpu %d, pc %08X\n",
682 my_cpu
, debugger_is_slave
[my_cpu
], debugger_cpu
, saved_state
->save_srr0
);
685 instr_pp
= (vm_offset_t
)pmap_find_phys(kernel_pmap
, (addr64_t
)(saved_state
->save_srr0
));
688 instr_ptr
= (addr64_t
)(((addr64_t
)instr_pp
<< 12) | (saved_state
->save_srr0
& 0xFFF)); /* Make physical address */
689 instr
= ml_phys_read_64(instr_ptr
); /* Get the trap that caused entry */
694 if (debugger_debug
) kprintf("Call_DebuggerC(%d): instr_pp = %08X, instr_ptr = %016llX, instr = %08X\n", my_cpu
, instr_pp
, instr_ptr
, instr
); /* (TEST/DEBUG) */
697 if (db_breakpoints_inserted
) cpus_holding_bkpts
++; /* Bump up the holding count */
698 if (debugger_cpu
== -1 && !debugger_is_slave
[my_cpu
]) {
700 if (debugger_debug
) kprintf("Call_DebuggerC(%d): lasttrace = %08X\n", my_cpu
, lastTrace
); /* (TEST/DEBUG) */
702 debugger_cpu
= my_cpu
; /* Show that we are debugger */
703 lastTrace
= LLTraceSet(0); /* Disable low-level tracing */
705 for(tcpu
= 0; tcpu
< NCPUS
; tcpu
++) { /* Stop all the other guys */
706 if(tcpu
== my_cpu
) continue; /* Don't diddle ourselves */
707 hw_atomic_add(&debugger_sync
, 1); /* Count signal sent */
708 (void)cpu_signal(tcpu
, SIGPdebug
, 0 ,0); /* Tell 'em to enter debugger */
710 (void)hw_cpu_sync(&debugger_sync
, LockTimeOut
); /* Wait for the other processors to enter debug */
711 debugger_sync
= 0; /* We're done with it */
713 else if (debugger_cpu
!= my_cpu
) goto debugger_exit
; /* We are not debugger, don't continue... */
716 if (instr
== TRAP_DIRECT_INST
) {
717 disableDebugOuput
= FALSE
;
718 print_backtrace(saved_state
);
721 switch_debugger
= 0; /* Make sure switch request is off */
722 directcall
= 1; /* Assume direct call */
724 if (saved_state
->save_srr1
& MASK(SRR1_PRG_TRAP
)) { /* Trap instruction? */
726 directcall
= 0; /* We had a trap not a direct call */
728 switch (instr
) { /* Select trap type */
731 case BREAK_TO_KDP0
: /* Breakpoint into KDP? */
732 case BREAK_TO_KDP1
: /* Breakpoint into KDP? */
733 current_debugger
= KDP_CUR_DB
; /* Yes, set KDP */
734 kdp_trap(type
, saved_state
); /* Enter it */
739 case BREAK_TO_KDB0
: /* Breakpoint to KDB (the "good" debugger)? */
740 current_debugger
= KDB_CUR_DB
; /* Yes, set it */
741 kdb_trap(type
, saved_state
); /* Enter it */
745 case TRAP_DEBUGGER_INST
: /* Should we enter the current debugger? */
746 case TRAP_DIRECT_INST
: /* Should we enter the current debugger? */
747 if (current_debugger
== KDP_CUR_DB
) /* Is current KDP? */
748 kdp_trap(type
, saved_state
); /* Yes, enter it */
749 else if (current_debugger
== KDB_CUR_DB
) /* Is this KDB? */
750 kdb_trap(type
, saved_state
); /* Yes, go ahead and enter */
751 else goto debugger_error
; /* No debugger active */
754 default: /* Unknown/bogus trap type */
759 while(1) { /* We are here to handle debugger switches */
761 if(!directcall
) { /* Was this a direct call? */
762 if(!switch_debugger
) break; /* No, then leave if no switch requested... */
765 * Note: we can only switch to a debugger we have. Ignore bogus switch requests.
768 if (debugger_debug
) kprintf("Call_DebuggerC(%d): switching debuggers\n", my_cpu
); /* (TEST/DEBUG) */
771 if(current_debugger
== KDP_CUR_DB
) current_debugger
= KDB_CUR_DB
; /* Switch to KDB */
777 if(current_debugger
== KDB_CUR_DB
) current_debugger
= KDP_CUR_DB
; /* Switch to KDP */
781 switch_debugger
= 0; /* Clear request */
782 directcall
= 0; /* Clear first-time direct call indication */
784 switch (current_debugger
) { /* Enter correct debugger */
786 case KDP_CUR_DB
: /* Enter KDP */
787 kdp_trap(type
, saved_state
);
790 case KDB_CUR_DB
: /* Enter KDB */
791 kdb_trap(type
, saved_state
);
794 default: /* No debugger installed */
802 if (debugger_debug
) kprintf("Call_DebuggerC(%d): exit - inst = %08X, cpu=%d(%d), run=%d\n", my_cpu
,
803 instr
, my_cpu
, debugger_cpu
, db_run_mode
); /* (TEST/DEBUG) */
805 if ((instr
== TRAP_DEBUGGER_INST
) || /* Did we trap to enter debugger? */
806 (instr
== TRAP_DIRECT_INST
)) saved_state
->save_srr0
+= TRAP_INST_SIZE
; /* Yes, point past trap */
808 if(debugger_cpu
== my_cpu
) LLTraceSet(lastTrace
); /* Enable tracing on the way out if we are debugger */
810 wait
= FALSE
; /* Assume we are not going to wait */
811 if (db_run_mode
== STEP_CONTINUE
) { /* Are we going to run? */
812 wait
= TRUE
; /* Yeah, remember to wait for breakpoints to clear */
813 debugger_cpu
= -1; /* Release other processor's debuggers */
814 debugger_pending
[0] = 0; /* Release request (this is a HACK) */
815 debugger_pending
[1] = 0; /* Release request (this is a HACK) */
816 NMIss
= 0; /* Let NMI bounce */
819 if(db_run_mode
== STEP_ONCE
) { /* Are we about to step? */
820 disable_preemption(); /* Disable preemption for the step */
821 db_im_stepping
= my_cpu
; /* Remember that I am about to step */
824 if (db_breakpoints_inserted
) cpus_holding_bkpts
--; /* If any breakpoints, back off count */
825 if (debugger_is_slave
[my_cpu
]) debugger_is_slave
[my_cpu
]--; /* If we were a slove, uncount us */
827 printf("Call_Debugger: exit - cpu %d, debugger_cpu %d, run_mode %d holds %d\n",
828 my_cpu
, debugger_cpu
, db_run_mode
,
831 unlock_debugger(); /* Release the lock */
832 debugger_active
[my_cpu
]--; /* Say we aren't active anymore */
834 if (wait
) while(cpus_holding_bkpts
); /* Wait for breakpoints to clear */
836 hw_atomic_sub(&debug_mode
, 1); /* Set out of debug now */
838 return(1); /* Exit debugger normally */
841 if(db_run_mode
!= STEP_ONCE
) enable_preemption_no_check(); /* Enable preemption, but don't preempt here */
842 hw_atomic_sub(&debug_mode
, 1); /* Set out of debug now */
843 return(0); /* Return in shame... */
847 void lock_debugger(void) {
851 my_cpu
= cpu_number(); /* Get our CPU number */
853 while(1) { /* Check until we get it */
855 if (debugger_cpu
!= -1 && debugger_cpu
!= my_cpu
) continue; /* Someone, not us, is debugger... */
856 if (hw_lock_try(&debugger_lock
)) { /* Get the debug lock */
857 if (debugger_cpu
== -1 || debugger_cpu
== my_cpu
) break; /* Is it us? */
858 hw_lock_unlock(&debugger_lock
); /* Not us, release lock */
863 void unlock_debugger(void) {
865 hw_lock_unlock(&debugger_lock
);