]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ppc/model_dep.c
xnu-123.5.tar.gz
[apple/xnu.git] / osfmk / ppc / model_dep.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
107extern struct db_command ppc_db_commands[];
108#endif /* MACH_KDB */
109
110char kernel_args_buf[256] = "/mach_kernel";
111char boot_args_buf[256] = "/mach_servers/bootstrap";
112char env_buf[256];
113
114#define TRAP_DEBUGGER __asm__ volatile("tw 4,r3,r3");
115#define TRAP_DEBUGGER_INST 0x7c831808
116#define TRAP_DIRECT __asm__ volatile("tw 4,r4,r4");
117#define TRAP_DIRECT_INST 0x7c842008
118#define TRAP_INST_SIZE 4
119#define BREAK_TO_KDP0 0x7fe00008
120#define BREAK_TO_KDP1 0x7c800008
121#define BREAK_TO_KDB0 0x7c810808
122
123/*
124 * Code used to synchronize debuggers among all cpus, one active at a time, switch
125 * from on to another using kdb_on! #cpu or cpu #cpu
126 */
127
128decl_simple_lock_data(, debugger_lock) /* debugger lock */
129
130int debugger_cpu = -1; /* current cpu running debugger */
131int debugger_debug = 0; /* Debug debugger */
132int debugger_is_slave[NCPUS]; /* Show that we were entered via sigp */
133int debugger_active[NCPUS]; /* Debugger active on CPU */
134int debugger_pending[NCPUS]; /* Debugger entry pending on CPU (this is a HACK) */
135int debugger_holdoff[NCPUS]; /* Holdoff debugger entry on this CPU (this is a HACK) */
136int db_run_mode; /* Debugger run mode */
137unsigned int debugger_sync = 0; /* Cross processor debugger entry sync */
138extern unsigned int NMIss; /* NMI debounce switch */
139
140unsigned int lastTrace; /* Value of low-level exception trace controls */
141
142volatile unsigned int cpus_holding_bkpts; /* counter for number of cpus holding
143 breakpoints (ie: cpus that did not
144 insert back breakpoints) */
145void unlock_debugger(void);
146void lock_debugger(void);
147
148#if !MACH_KDB
149boolean_t db_breakpoints_inserted = TRUE;
150jmp_buf_t *db_recover = 0;
151#endif
152
153#if MACH_KDB
154#include <ddb/db_run.h>
155int kdb_flag=0;
156extern boolean_t db_breakpoints_inserted;
157extern jmp_buf_t *db_recover;
158#define KDB_READY 0x1
159#endif
160
161#if MACH_KDP
162extern int kdp_flag;
163#define KDP_READY 0x1
164#endif
165
166boolean_t db_im_stepping = 0xFFFFFFFF; /* Remember if we were stepping */
167
168extern const char version[];
169
170#if !MACH_KDB
171void kdb_trap(int type, struct ppc_saved_state *regs);
172void kdb_trap(int type, struct ppc_saved_state *regs) {
173 return;
174}
175#endif
176
177#if !MACH_KDP
178void kdp_trap(int type, struct ppc_saved_state *regs);
179void kdp_trap(int type, struct ppc_saved_state *regs) {
180 return;
181}
182#endif
183
184void
185machine_startup(boot_args *args)
186{
187 int boot_arg;
188
189 if (PE_parse_boot_arg("cpus", &wncpu)) {
190 if (!((wncpu > 0) && (wncpu < NCPUS)))
191 wncpu = NCPUS;
192 } else
193 wncpu = NCPUS;
194
195 if( PE_get_hotkey( kPEControlKey ))
196 halt_in_debugger = halt_in_debugger ? 0 : 1;
197
198 if (PE_parse_boot_arg("debug", &boot_arg)) {
199 if (boot_arg & DB_HALT) halt_in_debugger=1;
200 if (boot_arg & DB_PRT) disableDebugOuput=FALSE;
201 if (boot_arg & DB_SLOG) systemLogDiags=TRUE;
202 }
203
204 hw_lock_init(&debugger_lock); /* initialized debugger lock */
205
206#if MACH_KDB
207 /*
208 * Initialize KDB
209 */
210#if DB_MACHINE_COMMANDS
211 db_machine_commands_install(ppc_db_commands);
212#endif /* DB_MACHINE_COMMANDS */
213 ddb_init();
214
215 if (boot_arg & DB_KDB)
216 current_debugger = KDB_CUR_DB;
217
218 /*
219 * Cause a breakpoint trap to the debugger before proceeding
220 * any further if the proper option bit was specified in
221 * the boot flags.
222 */
223 if (halt_in_debugger && (current_debugger == KDB_CUR_DB)) {
224 Debugger("inline call to debugger(machine_startup)");
225 halt_in_debugger = 0;
226 active_debugger =1;
227 }
228#endif /* MACH_KDB */
229 if (PE_parse_boot_arg("preempt", &boot_arg)) {
230 extern int default_preemption_rate;
231
232 default_preemption_rate = boot_arg;
233 }
234 if (PE_parse_boot_arg("kpreempt", &boot_arg)) {
235 extern int kernel_preemption_mode;
236 extern boolean_t zone_gc_allowed;
237
238 kernel_preemption_mode = boot_arg;
239 zone_gc_allowed = FALSE; /* XXX: TO BE REMOVED */
240 }
241
242 machine_conf();
243
244 ml_thrm_init(); /* Start thermal monitoring on this processor */
245
246 /*
247 * Start the system.
248 */
249 setup_main();
250
251 /* Should never return */
252}
253
254char *
255machine_boot_info(
256 char *buf,
257 vm_size_t size)
258{
259 return(PE_boot_args());
260}
261
262void
263machine_conf(void)
264{
265 machine_info.max_cpus = NCPUS;
266 machine_info.avail_cpus = 1;
267 machine_info.memory_size = mem_size;
268}
269
270void
271machine_init(void)
272{
273 clock_config();
274}
275
276void slave_machine_init(void)
277{
278 (void) ml_set_interrupts_enabled(FALSE); /* Make sure we are disabled */
279 clock_init(); /* Init the clock */
280 cpu_machine_init(); /* Initialize the processor */
281}
282
283void
284halt_all_cpus(boolean_t reboot)
285{
286 if(reboot)
287 {
288 printf("MACH Reboot\n");
289 PEHaltRestart(kPERestartCPU);
290 }
291 else
292 {
293 printf("CPU halted\n");
294 PEHaltRestart(kPEHaltCPU);
295 }
296 while(1);
297}
298
299void
300halt_cpu(void)
301{
302 halt_all_cpus(FALSE);
303}
304
305#if MACH_ASSERT
306/*
307 * Machine-dependent routine to fill in an array with up to callstack_max
308 * levels of return pc information.
309 */
310void machine_callstack(
311 natural_t *buf,
312 vm_size_t callstack_max)
313{
314}
315#endif /* MACH_ASSERT */
316
317
318void
319print_backtrace(struct ppc_saved_state *ssp)
320{
321 unsigned int *stackptr, *raddr, *rstack, trans;
322 int i, frames_cnt, skip_top_frames, frames_max;
323 unsigned int store[8]; /* Buffer for real storage reads */
324 vm_offset_t backtrace_entries[32];
325
326 printf("backtrace: ");
327 frames_cnt =0;
328
329 /* Get our stackpointer for backtrace */
330 if (ssp==NULL) {
331 __asm__ volatile("mr %0, r1" : "=r" (stackptr));
332 skip_top_frames = 1;
333 } else {
334 stackptr = (unsigned int *)(ssp->r1);
335 skip_top_frames = 0;
336 backtrace_entries[frames_cnt] = ssp->srr0;
337 frames_cnt++;
338 printf("0x%08x ", ssp->srr0);
339 }
340
341 frames_max = 32-frames_cnt;
342 for (i = 0; i < frames_max; i++) {
343
344 if(!stackptr) break; /* No more to get... */
345
346 /* Avoid causing page fault */
347 if (!(raddr = LRA(PPC_SID_KERNEL, (void *)((unsigned int)stackptr+FM_LR_SAVE))))
348 break;
349 ReadReal((unsigned int)raddr, &store[0]);
350 if (skip_top_frames)
351 skip_top_frames--;
352 else {
353 backtrace_entries[frames_cnt] = store[0];
354 frames_cnt++;
355 printf("0x%08x ",store[0]);
356 }
357 if (!(raddr = LRA(PPC_SID_KERNEL, (void *)stackptr)))
358 break;
359 ReadReal((unsigned int)raddr, &store[0]);
360 stackptr=(unsigned int *)store[0];
361 }
362 printf("\n");
363
364 if (frames_cnt)
365 kmod_dump((vm_offset_t *)&backtrace_entries[0], frames_cnt);
366}
367
368void
369Debugger(const char *message) {
370
371 int i;
372 unsigned int store[8];
373 spl_t spl;
374
375 spl = splhigh(); /* No interruptions from here on */
376
377/*
378 * backtrace for Debugger() call from panic() if no current debugger
379 * backtrace and return for double panic() call
380 */
381 if ((panicstr != (char *)0) &&
382 (((nestedpanic != 0) && (current_debugger == 1)) || (active_debugger == 0))) {
383 print_backtrace(NULL);
384 if (nestedpanic != 0) {
385 splx(spl);
386 return; /* Yeah, don't enter again... */
387 }
388 }
389
390 if (debug_mode && debugger_active[cpu_number()]) { /* Are we already on debugger on this processor? */
391 splx(spl);
392 return; /* Yeah, don't do it again... */
393 }
394
395 if ((current_debugger != NO_CUR_DB)) { /* If there is a debugger configured, enter it */
396 printf("Debugger(%s)\n", message);
397 TRAP_DEBUGGER;
398 splx(spl);
399 return; /* Done debugging for a while */
400 }
401
402 printf("\nNo debugger configured - dumping debug information\n");
403 printf("\nversion string : %s\n",version);
404 mfdbatu(store[0],0);
405 mfdbatl(store[1],0);
406 mfdbatu(store[2],1);
407 mfdbatl(store[3],1);
408 mfdbatu(store[4],2);
409 mfdbatl(store[5],2);
410 mfdbatu(store[6],3);
411 mfdbatl(store[7],3);
412 printf("DBAT0: %08X %08X\n", store[0], store[1]);
413 printf("DBAT1: %08X %08X\n", store[2], store[3]);
414 printf("DBAT2: %08X %08X\n", store[4], store[5]);
415 printf("DBAT3: %08X %08X\n", store[6], store[7]);
416 printf("MSR=%08X\n",mfmsr());
417 print_backtrace(NULL);
418 splx(spl);
419 return;
420}
421
422/*
423 * When we get here, interruptions are disabled and we are on the debugger stack
424 * Never, ever, ever, ever enable interruptions from here on
425 */
426
427int Call_DebuggerC(
428 int type,
429 struct ppc_saved_state *saved_state)
430{
431 int directcall, wait;
432 vm_offset_t instr_ptr;
433 unsigned int instr;
434 int my_cpu, tcpu;
435
436 my_cpu = cpu_number(); /* Get our CPU */
437
438#if MACH_KDB
439 if((debugger_cpu == my_cpu) && /* Do we already own debugger? */
440 debugger_active[my_cpu] && /* and are we really active? */
441 db_recover && /* and have we set up recovery? */
442 (current_debugger == KDB_CUR_DB)) { /* and are we in KDB (only it handles recovery) */
443 kdb_trap(type, saved_state); /* Then reenter it... */
444 }
445#endif
446
447 hw_atomic_add(&debug_mode, 1); /* Indicate we are in debugger */
448 debugger_active[my_cpu]++; /* Show active on our CPU */
449 lock_debugger(); /* Insure that only one CPU is in debugger */
450
451 if(db_im_stepping == my_cpu) { /* Are we just back from a step? */
452 enable_preemption_no_check(); /* Enable preemption now */
453 db_im_stepping = 0xFFFFFFFF; /* Nobody stepping right now */
454 }
455
456 if (debugger_debug) {
457#if 0
458 kprintf("Call_DebuggerC(%d): %08X %08X, debact = %d\n", my_cpu, type, saved_state, debug_mode); /* (TEST/DEBUG) */
459#endif
460 printf("Call_Debugger: enter - cpu %d, is_slave %d, debugger_cpu %d, pc %08X\n",
461 my_cpu, debugger_is_slave[my_cpu], debugger_cpu, saved_state->srr0);
462 }
463
464 if (instr_ptr = (vm_offset_t)LRA(PPC_SID_KERNEL, (void *)(saved_state->srr0))) {
465 instr = ml_phys_read(instr_ptr); /* Get the trap that caused entry */
466 }
467 else instr = 0;
468
469#if 0
470 if (debugger_debug) kprintf("Call_DebuggerC(%d): instr_ptr = %08X, instr = %08X\n", my_cpu, instr_ptr, instr); /* (TEST/DEBUG) */
471#endif
472
473 if (db_breakpoints_inserted) cpus_holding_bkpts++; /* Bump up the holding count */
474 if (debugger_cpu == -1 && !debugger_is_slave[my_cpu]) {
475#if 0
476 if (debugger_debug) kprintf("Call_DebuggerC(%d): lasttrace = %08X\n", my_cpu, lastTrace); /* (TEST/DEBUG) */
477#endif
478 debugger_cpu = my_cpu; /* Show that we are debugger */
479 lastTrace = LLTraceSet(0); /* Disable low-level tracing */
480
481 for(tcpu = 0; tcpu < NCPUS; tcpu++) { /* Stop all the other guys */
482 if(tcpu == my_cpu) continue; /* Don't diddle ourselves */
483 hw_atomic_add(&debugger_sync, 1); /* Count signal sent */
484 (void)cpu_signal(tcpu, SIGPdebug, 0 ,0); /* Tell 'em to enter debugger */
485 }
486 (void)hw_cpu_sync(&debugger_sync, LockTimeOut); /* Wait for the other processors to enter debug */
487 debugger_sync = 0; /* We're done with it */
488 }
489 else if (debugger_cpu != my_cpu) goto debugger_exit; /* We are not debugger, don't continue... */
490
491
492 if (instr == TRAP_DIRECT_INST) {
493 disableDebugOuput = FALSE;
494 print_backtrace(saved_state);
495 }
496
497 switch_debugger = 0; /* Make sure switch request is off */
498 directcall = 1; /* Assume direct call */
499
500 if (saved_state->srr1 & MASK(SRR1_PRG_TRAP)) { /* Trap instruction? */
501
502 directcall = 0; /* We had a trap not a direct call */
503
504 switch (instr) { /* Select trap type */
505
506#if MACH_KDP
507 case BREAK_TO_KDP0: /* Breakpoint into KDP? */
508 case BREAK_TO_KDP1: /* Breakpoint into KDP? */
509 current_debugger = KDP_CUR_DB; /* Yes, set KDP */
510 kdp_trap(type, saved_state); /* Enter it */
511 break;
512#endif
513
514#if MACH_KDB
515 case BREAK_TO_KDB0: /* Breakpoint to KDB (the "good" debugger)? */
516 current_debugger = KDB_CUR_DB; /* Yes, set it */
517 kdb_trap(type, saved_state); /* Enter it */
518 break;
519#endif
520
521 case TRAP_DEBUGGER_INST: /* Should we enter the current debugger? */
522 case TRAP_DIRECT_INST: /* Should we enter the current debugger? */
523 if (current_debugger == KDP_CUR_DB) /* Is current KDP? */
524 kdp_trap(type, saved_state); /* Yes, enter it */
525 else if (current_debugger == KDB_CUR_DB) /* Is this KDB? */
526 kdb_trap(type, saved_state); /* Yes, go ahead and enter */
527 else goto debugger_error; /* No debugger active */
528 break;
529
530 default: /* Unknown/bogus trap type */
531 goto debugger_error;
532 }
533 }
534
535 while(1) { /* We are here to handle debugger switches */
536
537 if(!directcall) { /* Was this a direct call? */
538 if(!switch_debugger) break; /* No, then leave if no switch requested... */
539
540/*
541 * Note: we can only switch to a debugger we have. Ignore bogus switch requests.
542 */
543#if 0
544 if (debugger_debug) kprintf("Call_DebuggerC(%d): switching debuggers\n", my_cpu); /* (TEST/DEBUG) */
545#endif
546#if MACH_KDB
547 if(current_debugger == KDP_CUR_DB) current_debugger = KDB_CUR_DB; /* Switch to KDB */
548#if MACH_KDP
549 else
550#endif
551#endif
552#if MACH_KDP
553 if(current_debugger == KDB_CUR_DB) current_debugger = KDP_CUR_DB; /* Switch to KDP */
554#endif
555 }
556
557 switch_debugger = 0; /* Clear request */
558 directcall = 0; /* Clear first-time direct call indication */
559
560 switch (current_debugger) { /* Enter correct debugger */
561
562 case KDP_CUR_DB: /* Enter KDP */
563 kdp_trap(type, saved_state);
564 break;
565
566 case KDB_CUR_DB: /* Enter KDB */
567 kdb_trap(type, saved_state);
568 break;
569
570 default: /* No debugger installed */
571 goto debugger_error;
572 break;
573 }
574 }
575
576debugger_exit:
577#if 0
578 if (debugger_debug) kprintf("Call_DebuggerC(%d): exit - inst = %08X, cpu=%d(%d), run=%d\n", my_cpu,
579 instr, my_cpu, debugger_cpu, db_run_mode); /* (TEST/DEBUG) */
580#endif
581 if ((instr == TRAP_DEBUGGER_INST) || /* Did we trap to enter debugger? */
582 (instr == TRAP_DIRECT_INST)) saved_state->srr0 += TRAP_INST_SIZE; /* Yes, point past trap */
583
584 if(debugger_cpu == my_cpu) LLTraceSet(lastTrace); /* Enable tracing on the way out if we are debugger */
585
586 wait = FALSE; /* Assume we are not going to wait */
587 if (db_run_mode == STEP_CONTINUE) { /* Are we going to run? */
588 wait = TRUE; /* Yeah, remember to wait for breakpoints to clear */
589 debugger_cpu = -1; /* Release other processor's debuggers */
590 debugger_pending[0] = 0; /* Release request (this is a HACK) */
591 debugger_pending[1] = 0; /* Release request (this is a HACK) */
592 NMIss = 0; /* Let NMI bounce */
593 }
594
595 if(db_run_mode == STEP_ONCE) { /* Are we about to step? */
596 disable_preemption(); /* Disable preemption for the step */
597 db_im_stepping = my_cpu; /* Remember that I am about to step */
598 }
599
600 if (db_breakpoints_inserted) cpus_holding_bkpts--; /* If any breakpoints, back off count */
601 if (debugger_is_slave[my_cpu]) debugger_is_slave[my_cpu]--; /* If we were a slove, uncount us */
602 if (debugger_debug)
603 printf("Call_Debugger: exit - cpu %d, debugger_cpu %d, run_mode %d holds %d\n",
604 my_cpu, debugger_cpu, db_run_mode,
605 cpus_holding_bkpts);
606
607 unlock_debugger(); /* Release the lock */
608 debugger_active[my_cpu]--; /* Say we aren't active anymore */
609
610 if (wait) while(cpus_holding_bkpts); /* Wait for breakpoints to clear */
611
612 hw_atomic_sub(&debug_mode, 1); /* Set out of debug now */
613
614 return(1); /* Exit debugger normally */
615
616debugger_error:
617 if(db_run_mode != STEP_ONCE) enable_preemption_no_check(); /* Enable preemption, but don't preempt here */
618 hw_atomic_sub(&debug_mode, 1); /* Set out of debug now */
619 return(0); /* Return in shame... */
620
621}
622
623void lock_debugger(void) {
624 int my_cpu;
625 register int i;
626
627 my_cpu = cpu_number(); /* Get our CPU number */
628
629 while(1) { /* Check until we get it */
630
631 if (debugger_cpu != -1 && debugger_cpu != my_cpu) continue; /* Someone, not us, is debugger... */
632 if (hw_lock_try(&debugger_lock)) { /* Get the debug lock */
633 if (debugger_cpu == -1 || debugger_cpu == my_cpu) break; /* Is it us? */
634 hw_lock_unlock(&debugger_lock); /* Not us, release lock */
635 }
636 }
637}
638
639void unlock_debugger(void) {
640
641 hw_lock_unlock(&debugger_lock);
642
643}
644
645