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