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