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>
90 #include <kern/clock.h>
91 #include <kern/debug.h>
92 #include <machine/trap.h>
94 #include <pexpert/pexpert.h>
97 #include <IOKit/IOPlatformExpert.h>
99 #include <mach/vm_prot.h>
101 #include <mach/time_value.h>
102 #include <machine/machparam.h> /* for btop */
105 #include <ddb/db_aout.h>
106 #include <ddb/db_output.h>
107 #include <ddb/db_command.h>
108 #include <machine/db_machdep.h>
110 extern struct db_command ppc_db_commands
[];
111 #endif /* MACH_KDB */
113 char kernel_args_buf
[256] = "/mach_kernel";
114 char boot_args_buf
[256] = "/mach_servers/bootstrap";
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
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
131 hw_lock_data_t debugger_lock
; /* debugger lock */
132 hw_lock_data_t pbtlock
; /* backtrace print lock */
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 */
144 extern volatile int panicwait
;
145 volatile unsigned int pbtcnt
= 0;
146 volatile unsigned int pbtcpu
= -1;
148 unsigned int lastTrace
; /* Value of low-level exception trace controls */
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
);
159 boolean_t db_breakpoints_inserted
= TRUE
;
160 jmp_buf_t
*db_recover
= 0;
164 #include <ddb/db_run.h>
166 extern boolean_t db_breakpoints_inserted
;
167 extern jmp_buf_t
*db_recover
;
168 #define KDB_READY 0x1
173 #define KDP_READY 0x1
176 boolean_t db_im_stepping
= 0xFFFFFFFF; /* Remember if we were stepping */
179 char *failNames
[] = {
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 */
191 char *invxcption
= "Unknown code";
193 extern const char version
[];
194 extern char *trap_type
[];
195 extern vm_offset_t mem_actual
;
198 void kdb_trap(int type
, struct savearea
*regs
);
199 void kdb_trap(int type
, struct savearea
*regs
) {
205 void kdp_trap(int type
, struct savearea
*regs
);
206 void kdp_trap(int type
, struct savearea
*regs
) {
212 machine_startup(boot_args
*args
)
216 if (PE_parse_boot_arg("cpus", &wncpu
)) {
217 if (!((wncpu
> 0) && (wncpu
< NCPUS
)))
222 if( PE_get_hotkey( kPEControlKey
))
223 halt_in_debugger
= halt_in_debugger
? 0 : 1;
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
;
233 hw_lock_init(&debugger_lock
); /* initialize debugger lock */
234 hw_lock_init(&pbtlock
); /* initialize print backtrace lock */
240 #if DB_MACHINE_COMMANDS
241 db_machine_commands_install(ppc_db_commands
);
242 #endif /* DB_MACHINE_COMMANDS */
245 if (boot_arg
& DB_KDB
)
246 current_debugger
= KDB_CUR_DB
;
249 * Cause a breakpoint trap to the debugger before proceeding
250 * any further if the proper option bit was specified in
253 if (halt_in_debugger
&& (current_debugger
== KDB_CUR_DB
)) {
254 Debugger("inline call to debugger(machine_startup)");
255 halt_in_debugger
= 0;
258 #endif /* MACH_KDB */
259 if (PE_parse_boot_arg("preempt", &boot_arg
)) {
260 extern int default_preemption_rate
;
262 default_preemption_rate
= boot_arg
;
264 if (PE_parse_boot_arg("unsafe", &boot_arg
)) {
265 extern int max_unsafe_quanta
;
267 max_unsafe_quanta
= boot_arg
;
269 if (PE_parse_boot_arg("poll", &boot_arg
)) {
270 extern int max_poll_quanta
;
272 max_poll_quanta
= boot_arg
;
274 if (PE_parse_boot_arg("yield", &boot_arg
)) {
275 extern int sched_poll_yield_shift
;
277 sched_poll_yield_shift
= boot_arg
;
282 ml_thrm_init(); /* Start thermal monitoring on this processor */
289 /* Should never return */
297 return(PE_boot_args());
303 machine_info
.max_cpus
= NCPUS
;
304 machine_info
.avail_cpus
= 1;
305 machine_info
.memory_size
= mem_size
;
314 void slave_machine_init(void)
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 */
322 halt_all_cpus(boolean_t reboot
)
326 printf("MACH Reboot\n");
327 PEHaltRestart(kPERestartCPU
);
331 printf("CPU halted\n");
332 PEHaltRestart(kPEHaltCPU
);
340 halt_all_cpus(FALSE
);
345 * Machine-dependent routine to fill in an array with up to callstack_max
346 * levels of return pc information.
348 void machine_callstack(
350 vm_size_t callstack_max
)
353 #endif /* MACH_ASSERT */
357 print_backtrace(struct savearea
*ssp
)
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];
364 savearea
*sv
, *svssp
;
368 * We need this lock to make sure we don't hang up when we double panic on an MP.
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 */
378 svssp
= (savearea
*)ssp
; /* Make this easier */
380 if(current_thread()) sv
= (savearea
*)current_act()->mact
.pcb
; /* Find most current savearea if system has started */
382 fence
= 0xFFFFFFFF; /* Show we go all the way */
383 if(sv
) fence
= sv
->save_r1
; /* Stop at previous exception point */
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 */
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 */
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 */
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 */
414 kdb_printf("Proceeding back via exception chain:\n");
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
);
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");
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 */
433 dump_savearea(sv
, fence
); /* Dump this savearea */
436 sv
= sv
->save_hdr
.save_prev
; /* Back chain */
439 kdb_printf("\nKernel version:\n%s\n",version
); /* Print kernel version */
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 */
445 while(pbtcnt
); /* Wait for completion */
450 void dump_savearea(savearea
*sv
, unsigned int fence
) {
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 */
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
);
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 */
470 #define DUMPFRAMES 32
473 void dump_backtrace(unsigned int stackptr
, unsigned int fence
) {
475 unsigned int bframes
[DUMPFRAMES
];
476 unsigned int sframe
[8], raddr
, dumbo
;
479 kdb_printf(" Backtrace:\n");
480 for(i
= 0; i
< DUMPFRAMES
; i
++) { /* Dump up to max frames */
482 if(!stackptr
|| (stackptr
== fence
)) break; /* Hit stop point or end... */
484 if(stackptr
& 0x0000000f) { /* Is stack pointer valid? */
485 kdb_printf("\n backtrace terminated - unaligned frame address: 0x%08X\n", stackptr
); /* No, tell 'em */
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 */
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 */
500 ReadReal(raddr
, &sframe
[0]); /* Fetch the stack frame */
502 bframes
[i
] = sframe
[LRindex
]; /* Save the link register */
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 */
508 stackptr
= sframe
[0]; /* Chain back */
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 */
519 Debugger(const char *message
) {
522 unsigned int store
[8];
523 unsigned long pi_size
= 0;
526 spl
= splhigh(); /* No interruptions from here on */
529 * backtrace for Debugger() call from panic() if no current debugger
530 * backtrace and return for double panic() call
532 if ((panicstr
!= (char *)0) &&
533 (((nestedpanic
!= 0) && (current_debugger
== 1)) || (active_debugger
== 0))) {
534 print_backtrace(NULL
);
535 if (nestedpanic
!= 0) {
537 return; /* Yeah, don't enter again... */
541 if (debug_mode
&& debugger_active
[cpu_number()]) { /* Are we already on debugger on this processor? */
543 return; /* Yeah, don't do it again... */
548 * The above stuff catches the double panic case so we shouldn't have to worry about that here.
550 if ( panicstr
!= (char *)0 )
552 /* diable kernel preemptions */
553 disable_preemption();
555 /* everything should be printed now so copy to NVRAM
557 if( debug_buf_size
> 0)
558 pi_size
= PESavePanicInfo( debug_buf
, debug_buf_ptr
- debug_buf
);
560 if( !panicDebugging
&& (pi_size
!= 0) ) {
561 int my_cpu
, debugger_cpu
;
564 my_cpu
= cpu_number();
565 debugger_cpu
= my_cpu
;
567 hw_atomic_add(&debug_mode
, 1);
568 debugger_active
[my_cpu
]++;
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);
576 (void)hw_cpu_sync(&debugger_sync
, LockTimeOut
);
582 if( !panicDebugging
&& (pi_size
!= 0))
583 PEHaltRestart( kPEHangCPU
);
589 if ((current_debugger
!= NO_CUR_DB
)) { /* If there is a debugger configured, enter it */
590 printf("Debugger(%s)\n", message
);
593 return; /* Done debugging for a while */
596 printf("\nNo debugger configured - dumping debug information\n");
597 printf("MSR=%08X\n",mfmsr());
598 print_backtrace(NULL
);
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
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.
611 * save_r3 contains the failure reason code.
614 void SysChoked(int type
, savearea
*sv
) { /* The system is bad dead */
616 unsigned int failcode
;
618 mp_disable_preemption();
619 disableDebugOuput
= FALSE
;
622 failcode
= sv
->save_r3
; /* Get the failure code */
623 if(failcode
> failUnknown
) failcode
= failUnknown
; /* Set unknown code code */
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
]);
628 print_backtrace(sv
); /* Attempt to print backtrace */
629 Call_DebuggerC(type
, sv
); /* Attempt to get into debugger */
631 if ((current_debugger
!= NO_CUR_DB
)) Call_DebuggerC(type
, sv
); /* Attempt to get into debugger */
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
644 struct savearea
*saved_state
)
646 int directcall
, wait
;
647 vm_offset_t instr_ptr
;
651 my_cpu
= cpu_number(); /* Get our CPU */
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... */
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 */
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 */
671 if (debugger_debug
) {
673 kprintf("Call_DebuggerC(%d): %08X %08X, debact = %d\n", my_cpu
, type
, saved_state
, debug_mode
); /* (TEST/DEBUG) */
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
);
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 */
685 if (debugger_debug
) kprintf("Call_DebuggerC(%d): instr_ptr = %08X, instr = %08X\n", my_cpu
, instr_ptr
, instr
); /* (TEST/DEBUG) */
688 if (db_breakpoints_inserted
) cpus_holding_bkpts
++; /* Bump up the holding count */
689 if (debugger_cpu
== -1 && !debugger_is_slave
[my_cpu
]) {
691 if (debugger_debug
) kprintf("Call_DebuggerC(%d): lasttrace = %08X\n", my_cpu
, lastTrace
); /* (TEST/DEBUG) */
693 debugger_cpu
= my_cpu
; /* Show that we are debugger */
694 lastTrace
= LLTraceSet(0); /* Disable low-level tracing */
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 */
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 */
704 else if (debugger_cpu
!= my_cpu
) goto debugger_exit
; /* We are not debugger, don't continue... */
707 if (instr
== TRAP_DIRECT_INST
) {
708 disableDebugOuput
= FALSE
;
709 print_backtrace(saved_state
);
712 switch_debugger
= 0; /* Make sure switch request is off */
713 directcall
= 1; /* Assume direct call */
715 if (saved_state
->save_srr1
& MASK(SRR1_PRG_TRAP
)) { /* Trap instruction? */
717 directcall
= 0; /* We had a trap not a direct call */
719 switch (instr
) { /* Select trap type */
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 */
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 */
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 */
745 default: /* Unknown/bogus trap type */
750 while(1) { /* We are here to handle debugger switches */
752 if(!directcall
) { /* Was this a direct call? */
753 if(!switch_debugger
) break; /* No, then leave if no switch requested... */
756 * Note: we can only switch to a debugger we have. Ignore bogus switch requests.
759 if (debugger_debug
) kprintf("Call_DebuggerC(%d): switching debuggers\n", my_cpu
); /* (TEST/DEBUG) */
762 if(current_debugger
== KDP_CUR_DB
) current_debugger
= KDB_CUR_DB
; /* Switch to KDB */
768 if(current_debugger
== KDB_CUR_DB
) current_debugger
= KDP_CUR_DB
; /* Switch to KDP */
772 switch_debugger
= 0; /* Clear request */
773 directcall
= 0; /* Clear first-time direct call indication */
775 switch (current_debugger
) { /* Enter correct debugger */
777 case KDP_CUR_DB
: /* Enter KDP */
778 kdp_trap(type
, saved_state
);
781 case KDB_CUR_DB
: /* Enter KDB */
782 kdb_trap(type
, saved_state
);
785 default: /* No debugger installed */
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) */
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 */
799 if(debugger_cpu
== my_cpu
) LLTraceSet(lastTrace
); /* Enable tracing on the way out if we are debugger */
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 */
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 */
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 */
818 printf("Call_Debugger: exit - cpu %d, debugger_cpu %d, run_mode %d holds %d\n",
819 my_cpu
, debugger_cpu
, db_run_mode
,
822 unlock_debugger(); /* Release the lock */
823 debugger_active
[my_cpu
]--; /* Say we aren't active anymore */
825 if (wait
) while(cpus_holding_bkpts
); /* Wait for breakpoints to clear */
827 hw_atomic_sub(&debug_mode
, 1); /* Set out of debug now */
829 return(1); /* Exit debugger normally */
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... */
838 void lock_debugger(void) {
842 my_cpu
= cpu_number(); /* Get our CPU number */
844 while(1) { /* Check until we get it */
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 */
854 void unlock_debugger(void) {
856 hw_lock_unlock(&debugger_lock
);