]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/model_dep.c
xnu-201.5.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
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
107 extern struct db_command ppc_db_commands[];
108 #endif /* MACH_KDB */
109
110 char kernel_args_buf[256] = "/mach_kernel";
111 char boot_args_buf[256] = "/mach_servers/bootstrap";
112 char 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
128 decl_simple_lock_data(, debugger_lock) /* debugger lock */
129 decl_simple_lock_data(, pbtlock) /* backtrace print lock */
130
131 int debugger_cpu = -1; /* current cpu running debugger */
132 int debugger_debug = 0; /* Debug debugger */
133 int debugger_is_slave[NCPUS]; /* Show that we were entered via sigp */
134 int debugger_active[NCPUS]; /* Debugger active on CPU */
135 int debugger_pending[NCPUS]; /* Debugger entry pending on CPU (this is a HACK) */
136 int debugger_holdoff[NCPUS]; /* Holdoff debugger entry on this CPU (this is a HACK) */
137 int db_run_mode; /* Debugger run mode */
138 unsigned int debugger_sync = 0; /* Cross processor debugger entry sync */
139 extern unsigned int NMIss; /* NMI debounce switch */
140
141 extern volatile int panicwait;
142 volatile unsigned int pbtcnt = 0;
143 volatile unsigned int pbtcpu = -1;
144
145 unsigned int lastTrace; /* Value of low-level exception trace controls */
146
147 volatile unsigned int cpus_holding_bkpts; /* counter for number of cpus holding
148 breakpoints (ie: cpus that did not
149 insert back breakpoints) */
150 void unlock_debugger(void);
151 void lock_debugger(void);
152 void dump_backtrace(unsigned int stackptr, unsigned int fence);
153 void dump_savearea(savearea *sv, unsigned int fence);
154
155 #if !MACH_KDB
156 boolean_t db_breakpoints_inserted = TRUE;
157 jmp_buf_t *db_recover = 0;
158 #endif
159
160 #if MACH_KDB
161 #include <ddb/db_run.h>
162 int kdb_flag=0;
163 extern boolean_t db_breakpoints_inserted;
164 extern jmp_buf_t *db_recover;
165 #define KDB_READY 0x1
166 #endif
167
168 #if MACH_KDP
169 extern int kdp_flag;
170 #define KDP_READY 0x1
171 #endif
172
173 boolean_t db_im_stepping = 0xFFFFFFFF; /* Remember if we were stepping */
174
175
176 char *failNames[] = {
177
178 "Debugging trap", /* failDebug */
179 "Corrupt stack", /* failStack */
180 "Corrupt mapping tables", /* failMapping */
181 "Corrupt context", /* failContext */
182 "Unknown failure code" /* Unknown failure code - must always be last */
183 };
184
185 char *invxcption = "Unknown code";
186
187 extern const char version[];
188 extern char *trap_type[];
189 extern vm_offset_t mem_actual;
190
191 #if !MACH_KDB
192 void kdb_trap(int type, struct ppc_saved_state *regs);
193 void kdb_trap(int type, struct ppc_saved_state *regs) {
194 return;
195 }
196 #endif
197
198 #if !MACH_KDP
199 void kdp_trap(int type, struct ppc_saved_state *regs);
200 void kdp_trap(int type, struct ppc_saved_state *regs) {
201 return;
202 }
203 #endif
204
205 void
206 machine_startup(boot_args *args)
207 {
208 int boot_arg;
209
210 if (PE_parse_boot_arg("cpus", &wncpu)) {
211 if (!((wncpu > 0) && (wncpu < NCPUS)))
212 wncpu = NCPUS;
213 } else
214 wncpu = NCPUS;
215
216 if( PE_get_hotkey( kPEControlKey ))
217 halt_in_debugger = halt_in_debugger ? 0 : 1;
218
219 if (PE_parse_boot_arg("debug", &boot_arg)) {
220 if (boot_arg & DB_HALT) halt_in_debugger=1;
221 if (boot_arg & DB_PRT) disableDebugOuput=FALSE;
222 if (boot_arg & DB_SLOG) systemLogDiags=TRUE;
223 }
224
225 hw_lock_init(&debugger_lock); /* initialize debugger lock */
226 hw_lock_init(&pbtlock); /* initialize print backtrace lock */
227
228 #if MACH_KDB
229 /*
230 * Initialize KDB
231 */
232 #if DB_MACHINE_COMMANDS
233 db_machine_commands_install(ppc_db_commands);
234 #endif /* DB_MACHINE_COMMANDS */
235 ddb_init();
236
237 if (boot_arg & DB_KDB)
238 current_debugger = KDB_CUR_DB;
239
240 /*
241 * Cause a breakpoint trap to the debugger before proceeding
242 * any further if the proper option bit was specified in
243 * the boot flags.
244 */
245 if (halt_in_debugger && (current_debugger == KDB_CUR_DB)) {
246 Debugger("inline call to debugger(machine_startup)");
247 halt_in_debugger = 0;
248 active_debugger =1;
249 }
250 #endif /* MACH_KDB */
251 if (PE_parse_boot_arg("preempt", &boot_arg)) {
252 extern int default_preemption_rate;
253
254 default_preemption_rate = boot_arg;
255 }
256 if (PE_parse_boot_arg("kpreempt", &boot_arg)) {
257 extern int kernel_preemption_mode;
258 extern boolean_t zone_gc_allowed;
259
260 kernel_preemption_mode = boot_arg;
261 zone_gc_allowed = FALSE; /* XXX: TO BE REMOVED */
262 }
263 if (PE_parse_boot_arg("unsafe", &boot_arg)) {
264 extern int max_unsafe_quanta;
265
266 max_unsafe_quanta = boot_arg;
267 }
268 if (PE_parse_boot_arg("poll", &boot_arg)) {
269 extern int max_poll_quanta;
270
271 max_poll_quanta = boot_arg;
272 }
273 if (PE_parse_boot_arg("yield", &boot_arg)) {
274 extern int sched_poll_yield_shift;
275
276 sched_poll_yield_shift = boot_arg;
277 }
278
279 machine_conf();
280
281 ml_thrm_init(); /* Start thermal monitoring on this processor */
282
283 /*
284 * Start the system.
285 */
286 setup_main();
287
288 /* Should never return */
289 }
290
291 char *
292 machine_boot_info(
293 char *buf,
294 vm_size_t size)
295 {
296 return(PE_boot_args());
297 }
298
299 void
300 machine_conf(void)
301 {
302 machine_info.max_cpus = NCPUS;
303 machine_info.avail_cpus = 1;
304 machine_info.memory_size = mem_size;
305 }
306
307 void
308 machine_init(void)
309 {
310 clock_config();
311 }
312
313 void slave_machine_init(void)
314 {
315 (void) ml_set_interrupts_enabled(FALSE); /* Make sure we are disabled */
316 clock_init(); /* Init the clock */
317 cpu_machine_init(); /* Initialize the processor */
318 }
319
320 void
321 halt_all_cpus(boolean_t reboot)
322 {
323 if(reboot)
324 {
325 printf("MACH Reboot\n");
326 PEHaltRestart(kPERestartCPU);
327 }
328 else
329 {
330 printf("CPU halted\n");
331 PEHaltRestart(kPEHaltCPU);
332 }
333 while(1);
334 }
335
336 void
337 halt_cpu(void)
338 {
339 halt_all_cpus(FALSE);
340 }
341
342 #if MACH_ASSERT
343 /*
344 * Machine-dependent routine to fill in an array with up to callstack_max
345 * levels of return pc information.
346 */
347 void machine_callstack(
348 natural_t *buf,
349 vm_size_t callstack_max)
350 {
351 }
352 #endif /* MACH_ASSERT */
353
354
355 void
356 print_backtrace(struct ppc_saved_state *ssp)
357 {
358 unsigned int stackptr, *raddr, *rstack, trans, fence;
359 int i, frames_cnt, skip_top_frames, frames_max;
360 unsigned int store[8]; /* Buffer for real storage reads */
361 vm_offset_t backtrace_entries[32];
362 thread_act_t *act;
363 savearea *sv, *svssp;
364 int cpu;
365
366 /*
367 * We need this lock to make sure we don't hang up when we double panic on an MP.
368 */
369
370 cpu = cpu_number(); /* Just who are we anyways? */
371 if(pbtcpu != cpu) { /* Allow recursion */
372 hw_atomic_add(&pbtcnt, 1); /* Remember we are trying */
373 while(!hw_lock_try(&pbtlock)); /* Spin here until we can get in. If we never do, well, we're crashing anyhow... */
374 pbtcpu = cpu; /* Mark it as us */
375 }
376
377 svssp = (savearea *)ssp; /* Make this easier */
378 sv = 0;
379 if(current_thread()) sv = (savearea *)current_act()->mact.pcb; /* Find most current savearea if system has started */
380
381 fence = 0xFFFFFFFF; /* Show we go all the way */
382 if(sv) fence = sv->save_r1; /* Stop at previous exception point */
383
384 if(!svssp) { /* Should we start from stack? */
385 printf("Latest stack backtrace for cpu %d:\n", cpu_number());
386 __asm__ volatile("mr %0,r1" : "=r" (stackptr)); /* Get current stack */
387 dump_backtrace(stackptr, fence); /* Dump the backtrace */
388 if(!sv) { /* Leave if no saveareas */
389 printf("\nKernel version:\n%s\n",version); /* Print kernel version */
390 hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */
391 return;
392 }
393 }
394 else { /* Were we passed an exception? */
395 fence = 0xFFFFFFFF; /* Show we go all the way */
396 if(svssp->save_prev) fence = svssp->save_prev->save_r1; /* Stop at previous exception point */
397
398 printf("Latest crash info for cpu %d:\n", cpu_number());
399 printf(" Exception state (sv=0x%08x)\n", sv);
400 dump_savearea(svssp, fence); /* Dump this savearea */
401 }
402
403 if(!sv) { /* Leave if no saveareas */
404 printf("\nKernel version:\n%s\n",version); /* Print kernel version */
405 hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */
406 return;
407 }
408
409 printf("Proceeding back via exception chain:\n");
410
411 while(sv) { /* Do them all... */
412 printf(" Exception state (sv=0x%08x)\n", sv);
413 if(sv == svssp) { /* Did we dump it already? */
414 printf(" previously dumped as \"Latest\" state. skipping...\n");
415 }
416 else {
417 fence = 0xFFFFFFFF; /* Show we go all the way */
418 if(sv->save_prev) fence = sv->save_prev->save_r1; /* Stop at previous exception point */
419 dump_savearea(sv, fence); /* Dump this savearea */
420 }
421
422 sv = sv->save_prev; /* Back chain */
423 }
424
425 printf("\nKernel version:\n%s\n",version); /* Print kernel version */
426
427 pbtcpu = -1; /* Mark as unowned */
428 hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */
429 hw_atomic_sub(&pbtcnt, 1); /* Show we are done */
430
431 while(pbtcnt); /* Wait for completion */
432
433 return;
434 }
435
436 void dump_savearea(savearea *sv, unsigned int fence) {
437
438 char *xcode;
439
440 if(sv->save_exception > T_MAX) xcode = invxcption; /* Too big for table */
441 else xcode = trap_type[sv->save_exception / 4]; /* Point to the type */
442
443 printf(" PC=0x%08X; MSR=0x%08x; DAR=0x%08x; DSISR=0x%08x; LR=0x%08x; R1=0x%08x; XCP=0x%08x (%s)\n",
444 sv->save_srr0, sv->save_srr1, sv->save_dar, sv->save_dsisr,
445 sv->save_lr, sv->save_r1, sv->save_exception, xcode);
446
447 if(!(sv->save_srr1 & MASK(MSR_PR))) { /* Are we in the kernel? */
448 dump_backtrace(sv->save_r1, fence); /* Dump the stack back trace from here if not user state */
449 }
450
451 return;
452 }
453
454
455
456 #define DUMPFRAMES 32
457 #define LRindex 2
458
459 void dump_backtrace(unsigned int stackptr, unsigned int fence) {
460
461 unsigned int bframes[DUMPFRAMES];
462 unsigned int sframe[8], raddr, dumbo;
463 int i;
464
465 printf(" Backtrace:\n");
466 for(i = 0; i < DUMPFRAMES; i++) { /* Dump up to max frames */
467
468 if(!stackptr || (stackptr == fence)) break; /* Hit stop point or end... */
469
470 if(stackptr & 0x0000000f) { /* Is stack pointer valid? */
471 printf("\n backtrace terminated - unaligned frame address: 0x%08x\n", stackptr); /* No, tell 'em */
472 break;
473 }
474
475 raddr = (unsigned int)LRA(PPC_SID_KERNEL, (void *)stackptr); /* Get physical frame address */
476 if(!raddr) { /* Is it mapped? */
477 printf("\n backtrace terminated - frame not mapped: 0x%08x\n", stackptr); /* No, tell 'em */
478 break;
479 }
480
481 if(raddr >= mem_actual) { /* Is it within physical RAM? */
482 printf("\n backtrace terminated - frame outside of RAM: v=0x%08x, p=%08X\n", stackptr, raddr); /* No, tell 'em */
483 break;
484 }
485
486 ReadReal(raddr, &sframe[0]); /* Fetch the stack frame */
487
488 bframes[i] = sframe[LRindex]; /* Save the link register */
489
490 if(!i) printf(" "); /* Indent first time */
491 else if(!(i & 7)) printf("\n "); /* Skip to new line every 8 */
492 printf("0x%08x ", bframes[i]); /* Dump the link register */
493
494 stackptr = sframe[0]; /* Chain back */
495 }
496 printf("\n");
497 if(i >= DUMPFRAMES) printf(" backtrace continues...\n"); /* Say we terminated early */
498 if(i) kmod_dump((vm_offset_t *)&bframes[0], i); /* Show what kmods are in trace */
499
500 }
501
502
503
504 void
505 Debugger(const char *message) {
506
507 int i;
508 unsigned int store[8];
509 spl_t spl;
510
511 spl = splhigh(); /* No interruptions from here on */
512
513 /*
514 * backtrace for Debugger() call from panic() if no current debugger
515 * backtrace and return for double panic() call
516 */
517 if ((panicstr != (char *)0) &&
518 (((nestedpanic != 0) && (current_debugger == 1)) || (active_debugger == 0))) {
519 print_backtrace(NULL);
520 if (nestedpanic != 0) {
521 splx(spl);
522 return; /* Yeah, don't enter again... */
523 }
524 }
525
526 if (debug_mode && debugger_active[cpu_number()]) { /* Are we already on debugger on this processor? */
527 splx(spl);
528 return; /* Yeah, don't do it again... */
529 }
530
531 if ((current_debugger != NO_CUR_DB)) { /* If there is a debugger configured, enter it */
532 printf("Debugger(%s)\n", message);
533 TRAP_DEBUGGER;
534 splx(spl);
535 return; /* Done debugging for a while */
536 }
537
538 printf("\nNo debugger configured - dumping debug information\n");
539 mfdbatu(store[0],0);
540 mfdbatl(store[1],0);
541 mfdbatu(store[2],1);
542 mfdbatl(store[3],1);
543 mfdbatu(store[4],2);
544 mfdbatl(store[5],2);
545 mfdbatu(store[6],3);
546 mfdbatl(store[7],3);
547 printf("DBAT0: %08X %08X\n", store[0], store[1]);
548 printf("DBAT1: %08X %08X\n", store[2], store[3]);
549 printf("DBAT2: %08X %08X\n", store[4], store[5]);
550 printf("DBAT3: %08X %08X\n", store[6], store[7]);
551 printf("MSR=%08X\n",mfmsr());
552 print_backtrace(NULL);
553 splx(spl);
554 return;
555 }
556
557 /*
558 * Here's where we attempt to get some diagnostic information dumped out
559 * when the system is really confused. We will try to get into the
560 * debugger as well.
561 *
562 * We are here with interrupts disabled and on the debug stack. The savearea
563 * that was passed in is NOT chained to the activation.
564 *
565 * save_r3 contains the failure reason code.
566 */
567
568 void SysChoked(int type, savearea *sv) { /* The system is bad dead */
569
570 unsigned int failcode;
571
572 mp_disable_preemption();
573 disableDebugOuput = FALSE;
574 debug_mode = TRUE;
575
576 failcode = sv->save_r3; /* Get the failure code */
577 if(failcode > failUnknown) failcode = failUnknown; /* Set unknown code code */
578
579 kprintf("System Failure: cpu=%d; code=%08X (%s)\n", cpu_number(), sv->save_r3, failNames[failcode]);
580 printf("System Failure: cpu=%d; code=%08X (%s)\n", cpu_number(), sv->save_r3, failNames[failcode]);
581
582 print_backtrace((struct ppc_saved_state *)sv); /* Attempt to print backtrace */
583 Call_DebuggerC(type, sv); /* Attempt to get into debugger */
584
585 if ((current_debugger != NO_CUR_DB)) Call_DebuggerC(type, sv); /* Attempt to get into debugger */
586
587 }
588
589
590
591 /*
592 * When we get here, interruptions are disabled and we are on the debugger stack
593 * Never, ever, ever, ever enable interruptions from here on
594 */
595
596 int Call_DebuggerC(
597 int type,
598 struct ppc_saved_state *saved_state)
599 {
600 int directcall, wait;
601 vm_offset_t instr_ptr;
602 unsigned int instr;
603 int my_cpu, tcpu;
604
605 my_cpu = cpu_number(); /* Get our CPU */
606
607 #if MACH_KDB
608 if((debugger_cpu == my_cpu) && /* Do we already own debugger? */
609 debugger_active[my_cpu] && /* and are we really active? */
610 db_recover && /* and have we set up recovery? */
611 (current_debugger == KDB_CUR_DB)) { /* and are we in KDB (only it handles recovery) */
612 kdb_trap(type, saved_state); /* Then reenter it... */
613 }
614 #endif
615
616 hw_atomic_add(&debug_mode, 1); /* Indicate we are in debugger */
617 debugger_active[my_cpu]++; /* Show active on our CPU */
618 lock_debugger(); /* Insure that only one CPU is in debugger */
619
620 if(db_im_stepping == my_cpu) { /* Are we just back from a step? */
621 enable_preemption_no_check(); /* Enable preemption now */
622 db_im_stepping = 0xFFFFFFFF; /* Nobody stepping right now */
623 }
624
625 if (debugger_debug) {
626 #if 0
627 kprintf("Call_DebuggerC(%d): %08X %08X, debact = %d\n", my_cpu, type, saved_state, debug_mode); /* (TEST/DEBUG) */
628 #endif
629 printf("Call_Debugger: enter - cpu %d, is_slave %d, debugger_cpu %d, pc %08X\n",
630 my_cpu, debugger_is_slave[my_cpu], debugger_cpu, saved_state->srr0);
631 }
632
633 if (instr_ptr = (vm_offset_t)LRA(PPC_SID_KERNEL, (void *)(saved_state->srr0))) {
634 instr = ml_phys_read(instr_ptr); /* Get the trap that caused entry */
635 }
636 else instr = 0;
637
638 #if 0
639 if (debugger_debug) kprintf("Call_DebuggerC(%d): instr_ptr = %08X, instr = %08X\n", my_cpu, instr_ptr, instr); /* (TEST/DEBUG) */
640 #endif
641
642 if (db_breakpoints_inserted) cpus_holding_bkpts++; /* Bump up the holding count */
643 if (debugger_cpu == -1 && !debugger_is_slave[my_cpu]) {
644 #if 0
645 if (debugger_debug) kprintf("Call_DebuggerC(%d): lasttrace = %08X\n", my_cpu, lastTrace); /* (TEST/DEBUG) */
646 #endif
647 debugger_cpu = my_cpu; /* Show that we are debugger */
648 lastTrace = LLTraceSet(0); /* Disable low-level tracing */
649
650 for(tcpu = 0; tcpu < NCPUS; tcpu++) { /* Stop all the other guys */
651 if(tcpu == my_cpu) continue; /* Don't diddle ourselves */
652 hw_atomic_add(&debugger_sync, 1); /* Count signal sent */
653 (void)cpu_signal(tcpu, SIGPdebug, 0 ,0); /* Tell 'em to enter debugger */
654 }
655 (void)hw_cpu_sync(&debugger_sync, LockTimeOut); /* Wait for the other processors to enter debug */
656 debugger_sync = 0; /* We're done with it */
657 }
658 else if (debugger_cpu != my_cpu) goto debugger_exit; /* We are not debugger, don't continue... */
659
660
661 if (instr == TRAP_DIRECT_INST) {
662 disableDebugOuput = FALSE;
663 print_backtrace(saved_state);
664 }
665
666 switch_debugger = 0; /* Make sure switch request is off */
667 directcall = 1; /* Assume direct call */
668
669 if (saved_state->srr1 & MASK(SRR1_PRG_TRAP)) { /* Trap instruction? */
670
671 directcall = 0; /* We had a trap not a direct call */
672
673 switch (instr) { /* Select trap type */
674
675 #if MACH_KDP
676 case BREAK_TO_KDP0: /* Breakpoint into KDP? */
677 case BREAK_TO_KDP1: /* Breakpoint into KDP? */
678 current_debugger = KDP_CUR_DB; /* Yes, set KDP */
679 kdp_trap(type, saved_state); /* Enter it */
680 break;
681 #endif
682
683 #if MACH_KDB
684 case BREAK_TO_KDB0: /* Breakpoint to KDB (the "good" debugger)? */
685 current_debugger = KDB_CUR_DB; /* Yes, set it */
686 kdb_trap(type, saved_state); /* Enter it */
687 break;
688 #endif
689
690 case TRAP_DEBUGGER_INST: /* Should we enter the current debugger? */
691 case TRAP_DIRECT_INST: /* Should we enter the current debugger? */
692 if (current_debugger == KDP_CUR_DB) /* Is current KDP? */
693 kdp_trap(type, saved_state); /* Yes, enter it */
694 else if (current_debugger == KDB_CUR_DB) /* Is this KDB? */
695 kdb_trap(type, saved_state); /* Yes, go ahead and enter */
696 else goto debugger_error; /* No debugger active */
697 break;
698
699 default: /* Unknown/bogus trap type */
700 goto debugger_error;
701 }
702 }
703
704 while(1) { /* We are here to handle debugger switches */
705
706 if(!directcall) { /* Was this a direct call? */
707 if(!switch_debugger) break; /* No, then leave if no switch requested... */
708
709 /*
710 * Note: we can only switch to a debugger we have. Ignore bogus switch requests.
711 */
712 #if 0
713 if (debugger_debug) kprintf("Call_DebuggerC(%d): switching debuggers\n", my_cpu); /* (TEST/DEBUG) */
714 #endif
715 #if MACH_KDB
716 if(current_debugger == KDP_CUR_DB) current_debugger = KDB_CUR_DB; /* Switch to KDB */
717 #if MACH_KDP
718 else
719 #endif
720 #endif
721 #if MACH_KDP
722 if(current_debugger == KDB_CUR_DB) current_debugger = KDP_CUR_DB; /* Switch to KDP */
723 #endif
724 }
725
726 switch_debugger = 0; /* Clear request */
727 directcall = 0; /* Clear first-time direct call indication */
728
729 switch (current_debugger) { /* Enter correct debugger */
730
731 case KDP_CUR_DB: /* Enter KDP */
732 kdp_trap(type, saved_state);
733 break;
734
735 case KDB_CUR_DB: /* Enter KDB */
736 kdb_trap(type, saved_state);
737 break;
738
739 default: /* No debugger installed */
740 goto debugger_error;
741 break;
742 }
743 }
744
745 debugger_exit:
746 #if 0
747 if (debugger_debug) kprintf("Call_DebuggerC(%d): exit - inst = %08X, cpu=%d(%d), run=%d\n", my_cpu,
748 instr, my_cpu, debugger_cpu, db_run_mode); /* (TEST/DEBUG) */
749 #endif
750 if ((instr == TRAP_DEBUGGER_INST) || /* Did we trap to enter debugger? */
751 (instr == TRAP_DIRECT_INST)) saved_state->srr0 += TRAP_INST_SIZE; /* Yes, point past trap */
752
753 if(debugger_cpu == my_cpu) LLTraceSet(lastTrace); /* Enable tracing on the way out if we are debugger */
754
755 wait = FALSE; /* Assume we are not going to wait */
756 if (db_run_mode == STEP_CONTINUE) { /* Are we going to run? */
757 wait = TRUE; /* Yeah, remember to wait for breakpoints to clear */
758 debugger_cpu = -1; /* Release other processor's debuggers */
759 debugger_pending[0] = 0; /* Release request (this is a HACK) */
760 debugger_pending[1] = 0; /* Release request (this is a HACK) */
761 NMIss = 0; /* Let NMI bounce */
762 }
763
764 if(db_run_mode == STEP_ONCE) { /* Are we about to step? */
765 disable_preemption(); /* Disable preemption for the step */
766 db_im_stepping = my_cpu; /* Remember that I am about to step */
767 }
768
769 if (db_breakpoints_inserted) cpus_holding_bkpts--; /* If any breakpoints, back off count */
770 if (debugger_is_slave[my_cpu]) debugger_is_slave[my_cpu]--; /* If we were a slove, uncount us */
771 if (debugger_debug)
772 printf("Call_Debugger: exit - cpu %d, debugger_cpu %d, run_mode %d holds %d\n",
773 my_cpu, debugger_cpu, db_run_mode,
774 cpus_holding_bkpts);
775
776 unlock_debugger(); /* Release the lock */
777 debugger_active[my_cpu]--; /* Say we aren't active anymore */
778
779 if (wait) while(cpus_holding_bkpts); /* Wait for breakpoints to clear */
780
781 hw_atomic_sub(&debug_mode, 1); /* Set out of debug now */
782
783 return(1); /* Exit debugger normally */
784
785 debugger_error:
786 if(db_run_mode != STEP_ONCE) enable_preemption_no_check(); /* Enable preemption, but don't preempt here */
787 hw_atomic_sub(&debug_mode, 1); /* Set out of debug now */
788 return(0); /* Return in shame... */
789
790 }
791
792 void lock_debugger(void) {
793 int my_cpu;
794 register int i;
795
796 my_cpu = cpu_number(); /* Get our CPU number */
797
798 while(1) { /* Check until we get it */
799
800 if (debugger_cpu != -1 && debugger_cpu != my_cpu) continue; /* Someone, not us, is debugger... */
801 if (hw_lock_try(&debugger_lock)) { /* Get the debug lock */
802 if (debugger_cpu == -1 || debugger_cpu == my_cpu) break; /* Is it us? */
803 hw_lock_unlock(&debugger_lock); /* Not us, release lock */
804 }
805 }
806 }
807
808 void unlock_debugger(void) {
809
810 hw_lock_unlock(&debugger_lock);
811
812 }
813
814