2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 #include <platforms.h>
27 #include <time_stamp.h>
28 #include <mach_mp_debug.h>
29 #include <mach_ldebug.h>
30 #include <db_machine_commands.h>
33 #include <kern/cpu_number.h>
34 #include <kern/kern_types.h>
35 #include <kern/misc_protos.h>
39 #include <ppc/db_machdep.h>
41 #include <ppc/setjmp.h>
43 #include <ppc/misc_protos.h>
44 #include <ppc/cpu_internal.h>
45 #include <ppc/exception.h>
46 #include <ppc/db_machdep.h>
47 #include <ppc/mappings.h>
48 #include <ppc/Firmware.h>
50 #include <mach/vm_param.h>
51 #include <mach/machine/vm_types.h>
52 #include <vm/vm_map.h>
53 #include <kern/thread.h>
54 #include <kern/task.h>
55 #include <kern/debug.h>
56 #include <pexpert/pexpert.h>
57 #include <IOKit/IOPlatformExpert.h>
59 #include <ddb/db_command.h>
60 #include <ddb/db_task_thread.h>
61 #include <ddb/db_run.h>
62 #include <ddb/db_trap.h>
63 #include <ddb/db_output.h>
64 #include <ddb/db_access.h>
65 #include <ddb/db_sym.h>
66 #include <ddb/db_break.h>
67 #include <ddb/db_watch.h>
69 struct savearea
*ppc_last_saved_statep
;
70 struct savearea ppc_nested_saved_state
;
71 unsigned ppc_last_kdb_sp
;
73 extern int debugger_cpu
; /* Current cpu running debugger */
75 int db_all_set_up
= 0;
79 void kdp_register_send_receive(void);
83 * Enter KDB through a keyboard trap.
84 * We show the registers as of the keyboard interrupt
85 * instead of those at its call to KDB.
88 /* XXX more registers ? */
89 struct ppc_interrupt_state
*is
;
92 extern char * trap_type
[];
93 extern int TRAP_TYPES
;
96 * Code used to synchronize kdb among all cpus, one active at a time, switch
97 * from on to another using kdb_on! #cpu or cpu #cpu
100 decl_simple_lock_data(, kdb_lock
) /* kdb lock */
102 #define db_simple_lock_init(l, e) hw_lock_init(&((l)->interlock))
103 #define db_simple_lock_try(l) hw_lock_try(&((l)->interlock))
104 #define db_simple_unlock(l) hw_lock_unlock(&((l)->interlock))
106 extern volatile unsigned int cpus_holding_bkpts
; /* counter for number of cpus holding
107 breakpoints (ie: cpus that did not
108 insert back breakpoints) */
109 extern boolean_t db_breakpoints_inserted
;
113 extern void kdbprinttrap(
118 extern void db_write_bytes_user_space(
123 extern int db_search_null(
129 extern int kdb_enter(int);
130 extern void kdb_leave(void);
131 extern void lock_kdb(void);
132 extern void unlock_kdb(void);
134 #if DB_MACHINE_COMMANDS
135 struct db_command ppc_db_commands
[] = {
136 { "lt", db_low_trace
, CS_MORE
|CS_SET_DOT
, 0 },
137 { (char *)0, 0, 0, 0 }
139 #endif /* DB_MACHINE_COMMANDS */
142 void kdp_register_send_receive(void) {}
145 extern jmp_buf_t
*db_recover
;
148 * kdb_trap - field a TRACE or BPT trap
153 struct savearea
*regs
)
155 boolean_t trap_from_user
;
156 int previous_console_device
;
159 previous_console_device
=switch_to_serial_console();
162 case T_TRACE
: /* single_step */
163 case T_PROGRAM
: /* breakpoint */
165 case T_WATCHPOINT
: /* watchpoint */
167 case -1: /* keyboard interrupt */
172 ppc_nested_saved_state
= *regs
;
173 db_printf("Caught ");
174 if (type
> TRAP_TYPES
)
175 db_printf("type %d", type
);
177 db_printf("%s", trap_type
[type
]);
178 db_printf(" trap, pc = %llx\n",
183 kdbprinttrap(type
, code
, (int *)®s
->save_srr0
, regs
->save_r1
);
186 getPerProc()->db_saved_state
= regs
;
188 ppc_last_saved_statep
= regs
;
189 ppc_last_kdb_sp
= (unsigned) &type
;
191 if (!IS_USER_TRAP(regs
)) {
192 bzero((char *)&ddb_regs
, sizeof (ddb_regs
));
194 trap_from_user
= FALSE
;
199 trap_from_user
= TRUE
;
202 db_task_trap(type
, code
, trap_from_user
);
206 if ((type
== T_PROGRAM
) &&
207 (db_get_task_value(regs
->save_srr0
,
210 db_target_space(current_thread(),
213 regs
->save_srr0
+= BKPT_SIZE
;
216 getPerProc()->db_saved_state
= 0;
217 switch_to_old_console(previous_console_device
);
234 if (type
> TRAP_TYPES
)
235 db_printf("type %d", type
);
237 db_printf("%s", trap_type
[type
]);
238 db_printf(" trap, code=%x pc@%x = %x sp=%x\n",
239 code
, pc
, *(int *)pc
, sp
);
240 db_run_mode
= STEP_CONTINUE
;
253 pp
= pmap_find_phys(pmap
, (addr64_t
)va
);
255 if (pp
== 0) return(0); /* Couldn't find it */
257 pa
= ((addr64_t
)pp
<< 12) | (addr64_t
)(va
& 0xFFF); /* Get physical address */
263 * Read bytes from task address space for debugger.
279 pmap
= task
->map
->pmap
;
283 phys_src
= db_vtophys(pmap
, (vm_offset_t
)addr
);
285 db_printf("\nno memory is assigned to src address %08x\n",
291 phys_dst
= db_vtophys(kernel_pmap
, (vm_offset_t
)data
);
293 db_printf("\nno memory is assigned to dst address %08x\n",
299 /* don't over-run any page boundaries - check src range */
300 max
= round_page_64(phys_src
+ 1) - phys_src
;
303 /* Check destination won't run over boundary either */
304 n
= round_page_64(phys_dst
+ 1) - phys_dst
;
306 if (n
< max
) max
= n
;
309 phys_copy(phys_src
, phys_dst
, max
);
311 /* resync I+D caches */
312 sync_cache64(phys_dst
, max
);
320 * Write bytes to task address space for debugger.
336 phys_src
= db_vtophys(kernel_pmap
, (vm_offset_t
)data
);
338 db_printf("\nno memory is assigned to src address %08x\n",
344 /* space stays as kernel space unless in another task */
345 if (task
== NULL
) pmap
= kernel_pmap
;
346 else pmap
= task
->map
->pmap
;
348 phys_dst
= db_vtophys(pmap
, (vm_offset_t
)addr
);
350 db_printf("\nno memory is assigned to dst address %08x\n",
356 /* don't over-run any page boundaries - check src range */
357 max
= round_page_64(phys_src
+ 1) - phys_src
;
360 /* Check destination won't run over boundary either */
361 n
= round_page_64(phys_dst
+ 1) - phys_dst
;
366 phys_copy(phys_src
, phys_dst
, max
);
368 /* resync I+D caches */
369 sync_cache64(phys_dst
, max
);
383 unsigned int kern_addr
;
385 if (task
== kernel_task
|| task
== TASK_NULL
) {
386 if (kernel_task
== TASK_NULL
) return(TRUE
);
388 } else if (task
== TASK_NULL
) {
389 if (current_thread() == THR_ACT_NULL
) return(FALSE
);
390 task
= current_thread()->task
;
394 if(!pmap_find_phys(task
->map
->pmap
, (addr64_t
)addr
)) return (FALSE
); /* Fail if page not mapped */
395 n
= trunc_page_32(addr
+PPC_PGBYTES
) - addr
;
411 addr64_t physa
, physb
;
413 if ((addr1
& (PPC_PGBYTES
-1)) != (addr2
& (PPC_PGBYTES
-1))) /* Is byte displacement the same? */
416 if (task1
== TASK_NULL
) { /* See if there is a task active */
417 if (current_thread() == THR_ACT_NULL
) /* See if there is a current task */
419 task1
= current_thread()->task
; /* If so, use that one */
422 if(!(physa
= db_vtophys(task1
->map
->pmap
, (vm_offset_t
)trunc_page_32(addr1
)))) return FALSE
; /* Get real address of the first */
423 if(!(physb
= db_vtophys(task2
->map
->pmap
, (vm_offset_t
)trunc_page_32(addr2
)))) return FALSE
; /* Get real address of the second */
425 return (physa
== physb
); /* Check if they are equal, then return... */
428 #define DB_USER_STACK_ADDR (0xc0000000)
429 #define DB_NAME_SEARCH_LIMIT (DB_USER_STACK_ADDR-(PPC_PGBYTES*3))
431 boolean_t
db_phys_cmp(
436 db_printf("db_phys_cmp: not implemented\n");
449 register unsigned vaddr
;
450 register unsigned *kaddr
;
452 db_printf("db_search_null: not implemented\n");
457 unsigned char *getProcName(struct proc
*proc
);
463 register unsigned char *p
;
465 unsigned int vaddr
, kaddr
;
466 unsigned char tname
[33];
472 if(task
->bsd_info
) p
= getProcName((struct proc
*)(task
->bsd_info
)); /* Point to task name */
475 for(i
= 0; i
< 32; i
++) { /* Move no more than 32 bytes */
480 db_printf("%s", tname
);
482 else db_printf("no name");
486 db_machdep_init(void) {
487 #define KDB_READY 0x1
490 kdb_flag
|= KDB_READY
;
495 #define KDB_SAVE(type, name) extern type name; type name##_save = name
496 #define KDB_RESTORE(name) name = name##_save
498 #define KDB_SAVE(type, name) extern type name; type name/**/_save = name
499 #define KDB_RESTORE(name) name = name/**/_save
500 #endif /* __STDC__ */
502 #define KDB_SAVE_CTXT() \
503 KDB_SAVE(int, db_run_mode); \
504 KDB_SAVE(boolean_t, db_sstep_print); \
505 KDB_SAVE(int, db_loop_count); \
506 KDB_SAVE(int, db_call_depth); \
507 KDB_SAVE(int, db_inst_count); \
508 KDB_SAVE(int, db_last_inst_count); \
509 KDB_SAVE(int, db_load_count); \
510 KDB_SAVE(int, db_store_count); \
511 KDB_SAVE(boolean_t, db_cmd_loop_done); \
512 KDB_SAVE(jmp_buf_t *, db_recover); \
513 KDB_SAVE(db_addr_t, db_dot); \
514 KDB_SAVE(db_addr_t, db_last_addr); \
515 KDB_SAVE(db_addr_t, db_prev); \
516 KDB_SAVE(db_addr_t, db_next); \
517 KDB_SAVE(db_regs_t, ddb_regs);
519 #define KDB_RESTORE_CTXT() \
520 KDB_RESTORE(db_run_mode); \
521 KDB_RESTORE(db_sstep_print); \
522 KDB_RESTORE(db_loop_count); \
523 KDB_RESTORE(db_call_depth); \
524 KDB_RESTORE(db_inst_count); \
525 KDB_RESTORE(db_last_inst_count); \
526 KDB_RESTORE(db_load_count); \
527 KDB_RESTORE(db_store_count); \
528 KDB_RESTORE(db_cmd_loop_done); \
529 KDB_RESTORE(db_recover); \
530 KDB_RESTORE(db_dot); \
531 KDB_RESTORE(db_last_addr); \
532 KDB_RESTORE(db_prev); \
533 KDB_RESTORE(db_next); \
534 KDB_RESTORE(ddb_regs);
537 * switch to another cpu
544 if (cpu
< 0 || cpu
>= real_ncpus
|| !PerProcTable
[cpu
].ppe_vaddr
->debugger_active
)
546 db_set_breakpoints();
547 db_set_watchpoints();
551 db_clear_breakpoints();
552 db_clear_watchpoints();
554 if (debugger_cpu
== -1) {/* someone continued */
555 debugger_cpu
= cpu_number();
556 db_continue_cmd(0, 0, 0, "");
564 extern int (*PE_halt_restart
)(unsigned int type
);
572 boolean_t reboot
= TRUE
;
576 while ((c
= *cp
++) != 0) {
577 if (c
== 'r') /* reboot */
579 if (c
== 'h') /* halt */
582 if(!reboot
) halt_all_cpus(FALSE
); /* If no reboot, try to be clean about it */
584 if (PE_halt_restart
) return (*PE_halt_restart
)(kPERestartCPU
);
585 db_printf("Sorry, system can't reboot automatically yet... You need to do it by hand...\n");
596 extern unsigned int switch_debugger
;