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