]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/model_dep.c
xnu-517.9.4.tar.gz
[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 * 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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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 #include <ppc/hw_perfmon.h>
87
88 #include <kern/clock.h>
89 #include <kern/debug.h>
90 #include <machine/trap.h>
91 #include <kern/spl.h>
92 #include <pexpert/pexpert.h>
93 #include <ppc/mp.h>
94
95 #include <IOKit/IOPlatformExpert.h>
96
97 #include <mach/vm_prot.h>
98 #include <vm/pmap.h>
99 #include <mach/time_value.h>
100 #include <machine/machparam.h> /* for btop */
101
102 #if MACH_KDB
103 #include <ddb/db_aout.h>
104 #include <ddb/db_output.h>
105 #include <ddb/db_command.h>
106 #include <machine/db_machdep.h>
107
108 extern struct db_command ppc_db_commands[];
109 #endif /* MACH_KDB */
110
111 char kernel_args_buf[256] = "/mach_kernel";
112 char boot_args_buf[256] = "/mach_servers/bootstrap";
113 char env_buf[256];
114
115 #define TRAP_DEBUGGER __asm__ volatile("tw 4,r3,r3");
116 #define TRAP_DEBUGGER_INST 0x7c831808
117 #define TRAP_DIRECT __asm__ volatile("tw 4,r4,r4");
118 #define TRAP_DIRECT_INST 0x7c842008
119 #define TRAP_INST_SIZE 4
120 #define BREAK_TO_KDP0 0x7fe00008
121 #define BREAK_TO_KDP1 0x7c800008
122 #define BREAK_TO_KDB0 0x7c810808
123
124 /*
125 * Code used to synchronize debuggers among all cpus, one active at a time, switch
126 * from on to another using kdb_on! #cpu or cpu #cpu
127 */
128
129 hw_lock_data_t debugger_lock; /* debugger lock */
130 hw_lock_data_t pbtlock; /* backtrace print lock */
131
132 int debugger_cpu = -1; /* current cpu running debugger */
133 int debugger_debug = 0; /* Debug debugger */
134 int debugger_is_slave[NCPUS]; /* Show that we were entered via sigp */
135 int debugger_active[NCPUS]; /* Debugger active on CPU */
136 int debugger_pending[NCPUS]; /* Debugger entry pending on CPU (this is a HACK) */
137 int debugger_holdoff[NCPUS]; /* Holdoff debugger entry on this CPU (this is a HACK) */
138 int db_run_mode; /* Debugger run mode */
139 unsigned int debugger_sync = 0; /* Cross processor debugger entry sync */
140 extern unsigned int NMIss; /* NMI debounce switch */
141
142 extern volatile int panicwait;
143 volatile unsigned int pbtcnt = 0;
144 volatile unsigned int pbtcpu = -1;
145
146 unsigned int lastTrace; /* Value of low-level exception trace controls */
147
148 volatile unsigned int cpus_holding_bkpts; /* counter for number of cpus holding
149 breakpoints (ie: cpus that did not
150 insert back breakpoints) */
151 void unlock_debugger(void);
152 void lock_debugger(void);
153 void dump_backtrace(unsigned int stackptr, unsigned int fence);
154 void dump_savearea(savearea *sv, unsigned int fence);
155
156 int packAsc (unsigned char *inbuf, unsigned int length);
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 "Unaligned stack", /* failUnalignedStk */
189 "Unknown failure code" /* Unknown failure code - must always be last */
190 };
191
192 char *invxcption = "Unknown code";
193
194 extern const char version[];
195 extern char *trap_type[];
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 if (PE_parse_boot_arg("refunn", &boot_arg)) {
280 extern int refunnel_hint_enabled;
281
282 refunnel_hint_enabled = boot_arg;
283 }
284
285 machine_conf();
286
287 ml_thrm_init(); /* Start thermal monitoring on this processor */
288
289 /*
290 * Start the system.
291 */
292 setup_main();
293
294 /* Should never return */
295 }
296
297 char *
298 machine_boot_info(
299 char *buf,
300 vm_size_t size)
301 {
302 return(PE_boot_args());
303 }
304
305 void
306 machine_conf(void)
307 {
308 machine_info.max_cpus = NCPUS;
309 machine_info.avail_cpus = 1;
310 machine_info.memory_size = mem_size; /* Note that this will be 2 GB for >= 2 GB machines */
311 }
312
313 void
314 machine_init(void)
315 {
316 clock_config();
317 perfmon_init();
318 }
319
320 void slave_machine_init(void)
321 {
322 (void) ml_set_interrupts_enabled(FALSE); /* Make sure we are disabled */
323 clock_init(); /* Init the clock */
324 cpu_machine_init(); /* Initialize the processor */
325 }
326
327 void
328 halt_all_cpus(boolean_t reboot)
329 {
330 if(reboot)
331 {
332 printf("MACH Reboot\n");
333 PEHaltRestart(kPERestartCPU);
334 }
335 else
336 {
337 printf("CPU halted\n");
338 PEHaltRestart(kPEHaltCPU);
339 }
340 while(1);
341 }
342
343 void
344 halt_cpu(void)
345 {
346 halt_all_cpus(FALSE);
347 }
348
349 #if MACH_ASSERT
350 /*
351 * Machine-dependent routine to fill in an array with up to callstack_max
352 * levels of return pc information.
353 */
354 void machine_callstack(
355 natural_t *buf,
356 vm_size_t callstack_max)
357 {
358 }
359 #endif /* MACH_ASSERT */
360
361
362 void
363 print_backtrace(struct savearea *ssp)
364 {
365 unsigned int stackptr, *raddr, *rstack, trans, fence;
366 int i, frames_cnt, skip_top_frames, frames_max;
367 unsigned int store[8]; /* Buffer for real storage reads */
368 vm_offset_t backtrace_entries[32];
369 thread_act_t *act;
370 savearea *sv, *svssp;
371 int cpu;
372 savearea *psv;
373
374 /*
375 * We need this lock to make sure we don't hang up when we double panic on an MP.
376 */
377
378 cpu = cpu_number(); /* Just who are we anyways? */
379 if(pbtcpu != cpu) { /* Allow recursion */
380 hw_atomic_add((uint32_t *)&pbtcnt, 1); /* Remember we are trying */
381 while(!hw_lock_try(&pbtlock)); /* Spin here until we can get in. If we never do, well, we're crashing anyhow... */
382 pbtcpu = cpu; /* Mark it as us */
383 }
384
385 svssp = (savearea *)ssp; /* Make this easier */
386 sv = 0;
387 if(current_thread()) sv = (savearea *)current_act()->mact.pcb; /* Find most current savearea if system has started */
388
389 fence = 0xFFFFFFFF; /* Show we go all the way */
390 if(sv) fence = (unsigned int)sv->save_r1; /* Stop at previous exception point */
391
392 if(!svssp) { /* Should we start from stack? */
393 kdb_printf("Latest stack backtrace for cpu %d:\n", cpu_number());
394 __asm__ volatile("mr %0,r1" : "=r" (stackptr)); /* Get current stack */
395 dump_backtrace(stackptr, fence); /* Dump the backtrace */
396 if(!sv) { /* Leave if no saveareas */
397 kdb_printf("\nKernel version:\n%s\n",version); /* Print kernel version */
398 hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */
399 return;
400 }
401 }
402 else { /* Were we passed an exception? */
403 fence = 0xFFFFFFFF; /* Show we go all the way */
404 if(svssp->save_hdr.save_prev) {
405 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? */
406 psv = (savearea *)((unsigned int)svssp->save_hdr.save_prev); /* Get the 64-bit back chain converted to a regualr pointer */
407 fence = (unsigned int)psv->save_r1; /* Stop at previous exception point */
408 }
409 }
410
411 kdb_printf("Latest crash info for cpu %d:\n", cpu_number());
412 kdb_printf(" Exception state (sv=0x%08X)\n", sv);
413 dump_savearea(svssp, fence); /* Dump this savearea */
414 }
415
416 if(!sv) { /* Leave if no saveareas */
417 kdb_printf("\nKernel version:\n%s\n",version); /* Print kernel version */
418 hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */
419 return;
420 }
421
422 kdb_printf("Proceeding back via exception chain:\n");
423
424 while(sv) { /* Do them all... */
425 if(!(((addr64_t)((uintptr_t)sv) <= vm_last_addr) &&
426 (unsigned int)pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)sv)))) { /* Valid address? */
427 kdb_printf(" Exception state (sv=0x%08X) Not mapped or invalid. stopping...\n", sv);
428 break;
429 }
430
431 kdb_printf(" Exception state (sv=0x%08X)\n", sv);
432 if(sv == svssp) { /* Did we dump it already? */
433 kdb_printf(" previously dumped as \"Latest\" state. skipping...\n");
434 }
435 else {
436 fence = 0xFFFFFFFF; /* Show we go all the way */
437 if(sv->save_hdr.save_prev) {
438 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? */
439 psv = (savearea *)((unsigned int)sv->save_hdr.save_prev); /* Get the 64-bit back chain converted to a regualr pointer */
440 fence = (unsigned int)psv->save_r1; /* Stop at previous exception point */
441 }
442 }
443 dump_savearea(sv, fence); /* Dump this savearea */
444 }
445
446 sv = CAST_DOWN(savearea *, sv->save_hdr.save_prev); /* Back chain */
447 }
448
449 kdb_printf("\nKernel version:\n%s\n",version); /* Print kernel version */
450
451 pbtcpu = -1; /* Mark as unowned */
452 hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */
453 hw_atomic_sub((uint32_t *) &pbtcnt, 1); /* Show we are done */
454
455 while(pbtcnt); /* Wait for completion */
456
457 return;
458 }
459
460 void dump_savearea(savearea *sv, unsigned int fence) {
461
462 char *xcode;
463
464 if(sv->save_exception > T_MAX) xcode = invxcption; /* Too big for table */
465 else xcode = trap_type[sv->save_exception / 4]; /* Point to the type */
466
467 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",
468 (unsigned int)sv->save_srr0, (unsigned int)sv->save_srr1, (unsigned int)sv->save_dar, sv->save_dsisr,
469 (unsigned int)sv->save_lr, (unsigned int)sv->save_r1, sv->save_exception, xcode);
470
471 if(!(sv->save_srr1 & MASK(MSR_PR))) { /* Are we in the kernel? */
472 dump_backtrace((unsigned int)sv->save_r1, fence); /* Dump the stack back trace from here if not user state */
473 }
474
475 return;
476 }
477
478
479
480 #define DUMPFRAMES 32
481 #define LRindex 2
482
483 void dump_backtrace(unsigned int stackptr, unsigned int fence) {
484
485 unsigned int bframes[DUMPFRAMES];
486 unsigned int sframe[8], raddr, dumbo;
487 int i;
488
489 kdb_printf(" Backtrace:\n");
490 for(i = 0; i < DUMPFRAMES; i++) { /* Dump up to max frames */
491
492 if(!stackptr || (stackptr == fence)) break; /* Hit stop point or end... */
493
494 if(stackptr & 0x0000000F) { /* Is stack pointer valid? */
495 kdb_printf("\n backtrace terminated - unaligned frame address: 0x%08X\n", stackptr); /* No, tell 'em */
496 break;
497 }
498
499 raddr = (unsigned int)pmap_find_phys(kernel_pmap, (addr64_t)stackptr); /* Get physical frame address */
500 if(!raddr || (stackptr > vm_last_addr)) { /* Is it mapped? */
501 kdb_printf("\n backtrace terminated - frame not mapped or invalid: 0x%08X\n", stackptr); /* No, tell 'em */
502 break;
503 }
504
505 if(!mapping_phys_lookup(raddr, &dumbo)) { /* Is it within physical RAM? */
506 kdb_printf("\n backtrace terminated - frame outside of RAM: v=0x%08X, p=%08X\n", stackptr, raddr); /* No, tell 'em */
507 break;
508 }
509
510 ReadReal((addr64_t)((raddr << 12) | (stackptr & 4095)), &sframe[0]); /* Fetch the stack frame */
511
512 bframes[i] = sframe[LRindex]; /* Save the link register */
513
514 if(!i) kdb_printf(" "); /* Indent first time */
515 else if(!(i & 7)) kdb_printf("\n "); /* Skip to new line every 8 */
516 kdb_printf("0x%08X ", bframes[i]); /* Dump the link register */
517
518 stackptr = sframe[0]; /* Chain back */
519 }
520 kdb_printf("\n");
521 if(i >= DUMPFRAMES) kdb_printf(" backtrace continues...\n"); /* Say we terminated early */
522 if(i) kmod_dump((vm_offset_t *)&bframes[0], i); /* Show what kmods are in trace */
523
524 }
525
526
527
528 void
529 Debugger(const char *message) {
530
531 int i;
532 unsigned int store[8];
533 unsigned long pi_size = 0;
534 spl_t spl;
535
536 spl = splhigh(); /* No interruptions from here on */
537
538 /*
539 * backtrace for Debugger() call from panic() if no current debugger
540 * backtrace and return for double panic() call
541 */
542 if ((panicstr != (char *)0) &&
543 (((nestedpanic != 0) && (current_debugger == 1)) || (active_debugger == 0))) {
544 print_backtrace(NULL);
545 if (nestedpanic != 0) {
546 splx(spl);
547 return; /* Yeah, don't enter again... */
548 }
549 }
550
551 if (debug_mode && debugger_active[cpu_number()]) { /* Are we already on debugger on this processor? */
552 splx(spl);
553 return; /* Yeah, don't do it again... */
554 }
555
556
557 /*
558 * The above stuff catches the double panic case so we shouldn't have to worry about that here.
559 */
560 if ( panicstr != (char *)0 )
561 {
562 /* diable kernel preemptions */
563 disable_preemption();
564
565 /* everything should be printed now so copy to NVRAM
566 */
567 if( debug_buf_size > 0)
568
569 {
570 /* Do not compress the panic log unless kernel debugging
571 * is disabled - the panic log isn't synced to NVRAM if
572 * debugging is enabled, and the panic log is valuable
573 * whilst debugging
574 */
575 if (!panicDebugging)
576 {
577 unsigned int bufpos;
578
579 /* Now call the compressor */
580 bufpos = packAsc (debug_buf, (unsigned int) (debug_buf_ptr - debug_buf) );
581 /* If compression was successful, use the compressed length */
582 if (bufpos)
583 {
584 debug_buf_ptr = debug_buf + bufpos;
585 }
586 }
587 /* Truncate if the buffer is larger than a certain magic
588 * size - this really ought to be some appropriate fraction
589 * of the NVRAM image buffer, and is best done in the
590 * savePanicInfo() or PESavePanicInfo() calls
591 */
592 pi_size = debug_buf_ptr - debug_buf;
593 pi_size = PESavePanicInfo( debug_buf, ((pi_size > 2040) ? 2040 : pi_size));
594 }
595
596 if( !panicDebugging && (pi_size != 0) ) {
597 int my_cpu, debugger_cpu;
598 int tcpu;
599
600 my_cpu = cpu_number();
601 debugger_cpu = my_cpu;
602
603 hw_atomic_add(&debug_mode, 1);
604 debugger_active[my_cpu]++;
605 lock_debugger();
606
607 for(tcpu = 0; tcpu < NCPUS; tcpu++) {
608 if(tcpu == my_cpu) continue;
609 hw_atomic_add(&debugger_sync, 1);
610 (void)cpu_signal(tcpu, SIGPdebug, 0 ,0);
611 }
612 (void)hw_cpu_sync(&debugger_sync, LockTimeOut);
613 debugger_sync = 0;
614 }
615
616 draw_panic_dialog();
617
618 if( !panicDebugging && (pi_size != 0))
619 PEHaltRestart( kPEHangCPU );
620
621 enable_preemption();
622 }
623
624
625 if ((current_debugger != NO_CUR_DB)) { /* If there is a debugger configured, enter it */
626 printf("Debugger(%s)\n", message);
627 TRAP_DEBUGGER;
628 splx(spl);
629 return; /* Done debugging for a while */
630 }
631
632 printf("\nNo debugger configured - dumping debug information\n");
633 printf("MSR=%08X\n",mfmsr());
634 print_backtrace(NULL);
635 splx(spl);
636 return;
637 }
638
639 /*
640 * Here's where we attempt to get some diagnostic information dumped out
641 * when the system is really confused. We will try to get into the
642 * debugger as well.
643 *
644 * We are here with interrupts disabled and on the debug stack. The savearea
645 * that was passed in is NOT chained to the activation.
646 *
647 * save_r3 contains the failure reason code.
648 */
649
650 void SysChoked(int type, savearea *sv) { /* The system is bad dead */
651
652 unsigned int failcode;
653
654 mp_disable_preemption();
655 disableDebugOuput = FALSE;
656 debug_mode = TRUE;
657
658 failcode = (unsigned int)sv->save_r3; /* Get the failure code */
659 if(failcode > failUnknown) failcode = failUnknown; /* Set unknown code code */
660
661 kprintf("System Failure: cpu=%d; code=%08X (%s)\n", cpu_number(), (unsigned int)sv->save_r3, failNames[failcode]);
662 kdb_printf("System Failure: cpu=%d; code=%08X (%s)\n", cpu_number(), (unsigned int)sv->save_r3, failNames[failcode]);
663
664 print_backtrace(sv); /* Attempt to print backtrace */
665 Call_DebuggerC(type, sv); /* Attempt to get into debugger */
666
667 if ((current_debugger != NO_CUR_DB)) Call_DebuggerC(type, sv); /* Attempt to get into debugger */
668
669 }
670
671
672
673 /*
674 * When we get here, interruptions are disabled and we are on the debugger stack
675 * Never, ever, ever, ever enable interruptions from here on
676 */
677
678 int Call_DebuggerC(
679 int type,
680 struct savearea *saved_state)
681 {
682 int directcall, wait;
683 addr64_t instr_ptr;
684 ppnum_t instr_pp;
685 unsigned int instr;
686 int my_cpu, tcpu;
687
688 my_cpu = cpu_number(); /* Get our CPU */
689
690 #if MACH_KDB
691 if((debugger_cpu == my_cpu) && /* Do we already own debugger? */
692 debugger_active[my_cpu] && /* and are we really active? */
693 db_recover && /* and have we set up recovery? */
694 (current_debugger == KDB_CUR_DB)) { /* and are we in KDB (only it handles recovery) */
695 kdb_trap(type, saved_state); /* Then reenter it... */
696 }
697 #endif
698
699 hw_atomic_add(&debug_mode, 1); /* Indicate we are in debugger */
700 debugger_active[my_cpu]++; /* Show active on our CPU */
701 lock_debugger(); /* Insure that only one CPU is in debugger */
702
703 if(db_im_stepping == my_cpu) { /* Are we just back from a step? */
704 enable_preemption_no_check(); /* Enable preemption now */
705 db_im_stepping = 0xFFFFFFFF; /* Nobody stepping right now */
706 }
707
708 if (debugger_debug) {
709 #if 0
710 kprintf("Call_DebuggerC(%d): %08X %08X, debact = %d\n", my_cpu, type, saved_state, debug_mode); /* (TEST/DEBUG) */
711 #endif
712 printf("Call_Debugger: enter - cpu %d, is_slave %d, debugger_cpu %d, pc %08X\n",
713 my_cpu, debugger_is_slave[my_cpu], debugger_cpu, saved_state->save_srr0);
714 }
715
716 instr_pp = (vm_offset_t)pmap_find_phys(kernel_pmap, (addr64_t)(saved_state->save_srr0));
717
718 if (instr_pp) {
719 instr_ptr = (addr64_t)(((addr64_t)instr_pp << 12) | (saved_state->save_srr0 & 0xFFF)); /* Make physical address */
720 instr = ml_phys_read_64(instr_ptr); /* Get the trap that caused entry */
721 }
722 else instr = 0;
723
724 #if 0
725 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) */
726 #endif
727
728 if (db_breakpoints_inserted) cpus_holding_bkpts++; /* Bump up the holding count */
729 if (debugger_cpu == -1 && !debugger_is_slave[my_cpu]) {
730 #if 0
731 if (debugger_debug) kprintf("Call_DebuggerC(%d): lasttrace = %08X\n", my_cpu, lastTrace); /* (TEST/DEBUG) */
732 #endif
733 debugger_cpu = my_cpu; /* Show that we are debugger */
734 lastTrace = LLTraceSet(0); /* Disable low-level tracing */
735
736 for(tcpu = 0; tcpu < NCPUS; tcpu++) { /* Stop all the other guys */
737 if(tcpu == my_cpu) continue; /* Don't diddle ourselves */
738 hw_atomic_add(&debugger_sync, 1); /* Count signal sent */
739 (void)cpu_signal(tcpu, SIGPdebug, 0 ,0); /* Tell 'em to enter debugger */
740 }
741 (void)hw_cpu_sync(&debugger_sync, LockTimeOut); /* Wait for the other processors to enter debug */
742 debugger_sync = 0; /* We're done with it */
743 }
744 else if (debugger_cpu != my_cpu) goto debugger_exit; /* We are not debugger, don't continue... */
745
746
747 if (instr == TRAP_DIRECT_INST) {
748 disableDebugOuput = FALSE;
749 print_backtrace(saved_state);
750 }
751
752 switch_debugger = 0; /* Make sure switch request is off */
753 directcall = 1; /* Assume direct call */
754
755 if (saved_state->save_srr1 & MASK(SRR1_PRG_TRAP)) { /* Trap instruction? */
756
757 directcall = 0; /* We had a trap not a direct call */
758
759 switch (instr) { /* Select trap type */
760
761 #if MACH_KDP
762 case BREAK_TO_KDP0: /* Breakpoint into KDP? */
763 case BREAK_TO_KDP1: /* Breakpoint into KDP? */
764 current_debugger = KDP_CUR_DB; /* Yes, set KDP */
765 kdp_trap(type, saved_state); /* Enter it */
766 break;
767 #endif
768
769 #if MACH_KDB
770 case BREAK_TO_KDB0: /* Breakpoint to KDB (the "good" debugger)? */
771 current_debugger = KDB_CUR_DB; /* Yes, set it */
772 kdb_trap(type, saved_state); /* Enter it */
773 break;
774 #endif
775
776 case TRAP_DEBUGGER_INST: /* Should we enter the current debugger? */
777 case TRAP_DIRECT_INST: /* Should we enter the current debugger? */
778 if (current_debugger == KDP_CUR_DB) /* Is current KDP? */
779 kdp_trap(type, saved_state); /* Yes, enter it */
780 else if (current_debugger == KDB_CUR_DB) /* Is this KDB? */
781 kdb_trap(type, saved_state); /* Yes, go ahead and enter */
782 else goto debugger_error; /* No debugger active */
783 break;
784
785 default: /* Unknown/bogus trap type */
786 goto debugger_error;
787 }
788 }
789
790 while(1) { /* We are here to handle debugger switches */
791
792 if(!directcall) { /* Was this a direct call? */
793 if(!switch_debugger) break; /* No, then leave if no switch requested... */
794
795 /*
796 * Note: we can only switch to a debugger we have. Ignore bogus switch requests.
797 */
798 #if 0
799 if (debugger_debug) kprintf("Call_DebuggerC(%d): switching debuggers\n", my_cpu); /* (TEST/DEBUG) */
800 #endif
801 #if MACH_KDB
802 if(current_debugger == KDP_CUR_DB) current_debugger = KDB_CUR_DB; /* Switch to KDB */
803 #if MACH_KDP
804 else
805 #endif
806 #endif
807 #if MACH_KDP
808 if(current_debugger == KDB_CUR_DB) current_debugger = KDP_CUR_DB; /* Switch to KDP */
809 #endif
810 }
811
812 switch_debugger = 0; /* Clear request */
813 directcall = 0; /* Clear first-time direct call indication */
814
815 switch (current_debugger) { /* Enter correct debugger */
816
817 case KDP_CUR_DB: /* Enter KDP */
818 kdp_trap(type, saved_state);
819 break;
820
821 case KDB_CUR_DB: /* Enter KDB */
822 kdb_trap(type, saved_state);
823 break;
824
825 default: /* No debugger installed */
826 goto debugger_error;
827 break;
828 }
829 }
830
831 debugger_exit:
832 #if 0
833 if (debugger_debug) kprintf("Call_DebuggerC(%d): exit - inst = %08X, cpu=%d(%d), run=%d\n", my_cpu,
834 instr, my_cpu, debugger_cpu, db_run_mode); /* (TEST/DEBUG) */
835 #endif
836 if ((instr == TRAP_DEBUGGER_INST) || /* Did we trap to enter debugger? */
837 (instr == TRAP_DIRECT_INST)) saved_state->save_srr0 += TRAP_INST_SIZE; /* Yes, point past trap */
838
839 if(debugger_cpu == my_cpu) LLTraceSet(lastTrace); /* Enable tracing on the way out if we are debugger */
840
841 wait = FALSE; /* Assume we are not going to wait */
842 if (db_run_mode == STEP_CONTINUE) { /* Are we going to run? */
843 wait = TRUE; /* Yeah, remember to wait for breakpoints to clear */
844 debugger_cpu = -1; /* Release other processor's debuggers */
845 debugger_pending[0] = 0; /* Release request (this is a HACK) */
846 debugger_pending[1] = 0; /* Release request (this is a HACK) */
847 NMIss = 0; /* Let NMI bounce */
848 }
849
850 if(db_run_mode == STEP_ONCE) { /* Are we about to step? */
851 disable_preemption(); /* Disable preemption for the step */
852 db_im_stepping = my_cpu; /* Remember that I am about to step */
853 }
854
855 if (db_breakpoints_inserted) cpus_holding_bkpts--; /* If any breakpoints, back off count */
856 if (debugger_is_slave[my_cpu]) debugger_is_slave[my_cpu]--; /* If we were a slove, uncount us */
857 if (debugger_debug)
858 printf("Call_Debugger: exit - cpu %d, debugger_cpu %d, run_mode %d holds %d\n",
859 my_cpu, debugger_cpu, db_run_mode,
860 cpus_holding_bkpts);
861
862 unlock_debugger(); /* Release the lock */
863 debugger_active[my_cpu]--; /* Say we aren't active anymore */
864
865 if (wait) while(cpus_holding_bkpts); /* Wait for breakpoints to clear */
866
867 hw_atomic_sub(&debug_mode, 1); /* Set out of debug now */
868
869 return(1); /* Exit debugger normally */
870
871 debugger_error:
872 if(db_run_mode != STEP_ONCE) enable_preemption_no_check(); /* Enable preemption, but don't preempt here */
873 hw_atomic_sub(&debug_mode, 1); /* Set out of debug now */
874 return(0); /* Return in shame... */
875
876 }
877
878 void lock_debugger(void) {
879 int my_cpu;
880 register int i;
881
882 my_cpu = cpu_number(); /* Get our CPU number */
883
884 while(1) { /* Check until we get it */
885
886 if (debugger_cpu != -1 && debugger_cpu != my_cpu) continue; /* Someone, not us, is debugger... */
887 if (hw_lock_try(&debugger_lock)) { /* Get the debug lock */
888 if (debugger_cpu == -1 || debugger_cpu == my_cpu) break; /* Is it us? */
889 hw_lock_unlock(&debugger_lock); /* Not us, release lock */
890 }
891 }
892 }
893
894 void unlock_debugger(void) {
895
896 hw_lock_unlock(&debugger_lock);
897
898 }
899
900 struct pasc {
901 unsigned a: 7;
902 unsigned b: 7;
903 unsigned c: 7;
904 unsigned d: 7;
905 unsigned e: 7;
906 unsigned f: 7;
907 unsigned g: 7;
908 unsigned h: 7;
909 } __attribute__((packed));
910
911 typedef struct pasc pasc_t;
912
913 int packAsc (unsigned char *inbuf, unsigned int length)
914 {
915 unsigned int i, j = 0;
916 pasc_t pack;
917
918 for (i = 0; i < length; i+=8)
919 {
920 pack.a = inbuf[i];
921 pack.b = inbuf[i+1];
922 pack.c = inbuf[i+2];
923 pack.d = inbuf[i+3];
924 pack.e = inbuf[i+4];
925 pack.f = inbuf[i+5];
926 pack.g = inbuf[i+6];
927 pack.h = inbuf[i+7];
928 bcopy ((char *) &pack, inbuf + j, 7);
929 j += 7;
930 }
931 if (0 != (i - length))
932 inbuf[j - (i - length)] &= 0xFF << (8-(i - length));
933 return j-(((i-length) == 7) ? 6 : (i - length));
934 }