2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
30 #include <platforms.h>
31 #include <time_stamp.h>
32 #include <mach_mp_debug.h>
33 #include <mach_ldebug.h>
34 #include <db_machine_commands.h>
37 #include <kern/cpu_number.h>
38 #include <kern/kern_types.h>
39 #include <kern/misc_protos.h>
43 #include <ppc/thread.h>
44 #include <ppc/db_machdep.h>
46 #include <ppc/setjmp.h>
48 #include <ppc/misc_protos.h>
49 #include <ppc/exception.h>
50 #include <ppc/db_machdep.h>
51 #include <ppc/mappings.h>
52 #include <ppc/Firmware.h>
54 #include <mach/vm_param.h>
55 #include <mach/machine/vm_types.h>
56 #include <vm/vm_map.h>
57 #include <kern/thread.h>
58 #include <kern/task.h>
59 #include <kern/debug.h>
61 #include <ddb/db_command.h>
62 #include <ddb/db_task_thread.h>
63 #include <ddb/db_run.h>
64 #include <ddb/db_trap.h>
65 #include <ddb/db_output.h>
66 #include <ddb/db_access.h>
67 #include <ddb/db_sym.h>
68 #include <ddb/db_break.h>
69 #include <ddb/db_watch.h>
71 struct savearea
*ppc_last_saved_statep
;
72 struct savearea ppc_nested_saved_state
;
73 unsigned ppc_last_kdb_sp
;
75 extern int debugger_active
[NCPUS
]; /* Debugger active on CPU */
76 extern int debugger_cpu
; /* Current cpu running debugger */
78 int db_all_set_up
= 0;
82 void kdp_register_send_receive(void);
86 * Enter KDB through a keyboard trap.
87 * We show the registers as of the keyboard interrupt
88 * instead of those at its call to KDB.
91 /* XXX more registers ? */
92 struct ppc_interrupt_state
*is
;
95 extern char * trap_type
[];
96 extern int TRAP_TYPES
;
99 * Code used to synchronize kdb among all cpus, one active at a time, switch
100 * from on to another using kdb_on! #cpu or cpu #cpu
103 decl_simple_lock_data(, kdb_lock
) /* kdb lock */
105 #define db_simple_lock_init(l, e) hw_lock_init(&((l)->interlock))
106 #define db_simple_lock_try(l) hw_lock_try(&((l)->interlock))
107 #define db_simple_unlock(l) hw_lock_unlock(&((l)->interlock))
109 extern volatile unsigned int cpus_holding_bkpts
; /* counter for number of cpus holding
110 breakpoints (ie: cpus that did not
111 insert back breakpoints) */
112 extern boolean_t db_breakpoints_inserted
;
116 extern void kdbprinttrap(
121 extern void db_write_bytes_user_space(
126 extern int db_search_null(
132 extern int kdb_enter(int);
133 extern void kdb_leave(void);
134 extern void lock_kdb(void);
135 extern void unlock_kdb(void);
137 #if DB_MACHINE_COMMANDS
138 struct db_command ppc_db_commands
[] = {
139 { "lt", db_low_trace
, CS_MORE
|CS_SET_DOT
, 0 },
140 { (char *)0, 0, 0, 0 }
142 #endif /* DB_MACHINE_COMMANDS */
145 void kdp_register_send_receive(void) {}
148 extern jmp_buf_t
*db_recover
;
149 spl_t saved_ipl
[NCPUS
]; /* just to know what IPL was before trap */
150 struct savearea
*saved_state
[NCPUS
];
153 * kdb_trap - field a TRACE or BPT trap
158 struct savearea
*regs
)
160 boolean_t trap_from_user
;
161 int previous_console_device
;
164 previous_console_device
=switch_to_serial_console();
167 case T_TRACE
: /* single_step */
168 case T_PROGRAM
: /* breakpoint */
170 case T_WATCHPOINT
: /* watchpoint */
172 case -1: /* keyboard interrupt */
177 ppc_nested_saved_state
= *regs
;
178 db_printf("Caught ");
179 if (type
> TRAP_TYPES
)
180 db_printf("type %d", type
);
182 db_printf("%s", trap_type
[type
]);
183 db_printf(" trap, pc = %x\n",
188 kdbprinttrap(type
, code
, (int *)®s
->save_srr0
, regs
->save_r1
);
191 saved_state
[cpu_number()] = regs
;
193 ppc_last_saved_statep
= regs
;
194 ppc_last_kdb_sp
= (unsigned) &type
;
196 if (!IS_USER_TRAP(regs
)) {
197 bzero((char *)&ddb_regs
, sizeof (ddb_regs
));
199 trap_from_user
= FALSE
;
204 trap_from_user
= TRUE
;
207 db_task_trap(type
, code
, trap_from_user
);
211 if ((type
== T_PROGRAM
) &&
212 (db_get_task_value(regs
->save_srr0
,
215 db_target_space(current_act(),
218 regs
->save_srr0
+= BKPT_SIZE
;
221 saved_state
[cpu_number()] = 0;
222 switch_to_old_console(previous_console_device
);
239 if (type
> TRAP_TYPES
)
240 db_printf("type %d", type
);
242 db_printf("%s", trap_type
[type
]);
243 db_printf(" trap, code=%x pc@%x = %x sp=%x\n",
244 code
, pc
, *(int *)pc
, sp
);
245 db_run_mode
= STEP_CONTINUE
;
258 pp
= pmap_find_phys(pmap
, (addr64_t
)va
);
260 if (pp
== 0) return(0); /* Couldn't find it */
262 pa
= ((addr64_t
)pp
<< 12) | (addr64_t
)(va
& 0xFFF); /* Get physical address */
268 * Read bytes from task address space for debugger.
284 pmap
= task
->map
->pmap
;
288 phys_src
= db_vtophys(pmap
, (vm_offset_t
)addr
);
290 db_printf("\nno memory is assigned to src address %08x\n",
296 phys_dst
= db_vtophys(kernel_pmap
, (vm_offset_t
)data
);
298 db_printf("\nno memory is assigned to dst address %08x\n",
304 /* don't over-run any page boundaries - check src range */
305 max
= round_page_64(phys_src
+ 1) - phys_src
;
308 /* Check destination won't run over boundary either */
309 n
= round_page_64(phys_dst
+ 1) - phys_dst
;
311 if (n
< max
) max
= n
;
314 phys_copy(phys_src
, phys_dst
, max
);
316 /* resync I+D caches */
317 sync_cache64(phys_dst
, max
);
325 * Write bytes to task address space for debugger.
341 phys_src
= db_vtophys(kernel_pmap
, (vm_offset_t
)data
);
343 db_printf("\nno memory is assigned to src address %08x\n",
349 /* space stays as kernel space unless in another task */
350 if (task
== NULL
) pmap
= kernel_pmap
;
351 else pmap
= task
->map
->pmap
;
353 phys_dst
= db_vtophys(pmap
, (vm_offset_t
)addr
);
355 db_printf("\nno memory is assigned to dst address %08x\n",
361 /* don't over-run any page boundaries - check src range */
362 max
= round_page_64(phys_src
+ 1) - phys_src
;
365 /* Check destination won't run over boundary either */
366 n
= round_page_64(phys_dst
+ 1) - phys_dst
;
371 phys_copy(phys_src
, phys_dst
, max
);
373 /* resync I+D caches */
374 sync_cache64(phys_dst
, max
);
388 unsigned int kern_addr
;
390 if (task
== kernel_task
|| task
== TASK_NULL
) {
391 if (kernel_task
== TASK_NULL
) return(TRUE
);
393 } else if (task
== TASK_NULL
) {
394 if (current_act() == THR_ACT_NULL
) return(FALSE
);
395 task
= current_act()->task
;
399 if(!pmap_find_phys(task
->map
->pmap
, (addr64_t
)addr
)) return (FALSE
); /* Fail if page not mapped */
400 n
= trunc_page_32(addr
+PPC_PGBYTES
) - addr
;
416 addr64_t physa
, physb
;
418 if ((addr1
& (PPC_PGBYTES
-1)) != (addr2
& (PPC_PGBYTES
-1))) /* Is byte displacement the same? */
421 if (task1
== TASK_NULL
) { /* See if there is a task active */
422 if (current_act() == THR_ACT_NULL
) /* See if there is a current task */
424 task1
= current_act()->task
; /* If so, use that one */
427 if(!(physa
= db_vtophys(task1
->map
->pmap
, (vm_offset_t
)trunc_page_32(addr1
)))) return FALSE
; /* Get real address of the first */
428 if(!(physb
= db_vtophys(task2
->map
->pmap
, (vm_offset_t
)trunc_page_32(addr2
)))) return FALSE
; /* Get real address of the second */
430 return (physa
== physb
); /* Check if they are equal, then return... */
433 #define DB_USER_STACK_ADDR (0xc0000000)
434 #define DB_NAME_SEARCH_LIMIT (DB_USER_STACK_ADDR-(PPC_PGBYTES*3))
436 boolean_t
db_phys_cmp(
441 db_printf("db_phys_cmp: not implemented\n");
454 register unsigned vaddr
;
455 register unsigned *kaddr
;
457 db_printf("db_search_null: not implemented\n");
462 unsigned char *getProcName(struct proc
*proc
);
468 register unsigned char *p
;
470 unsigned int vaddr
, kaddr
;
471 unsigned char tname
[33];
477 if(task
->bsd_info
) p
= getProcName((struct proc
*)(task
->bsd_info
)); /* Point to task name */
480 for(i
= 0; i
< 32; i
++) { /* Move no more than 32 bytes */
485 db_printf("%s", tname
);
487 else db_printf("no name");
491 db_machdep_init(void) {
492 #define KDB_READY 0x1
495 kdb_flag
|= KDB_READY
;
500 #define KDB_SAVE(type, name) extern type name; type name##_save = name
501 #define KDB_RESTORE(name) name = name##_save
503 #define KDB_SAVE(type, name) extern type name; type name/**/_save = name
504 #define KDB_RESTORE(name) name = name/**/_save
505 #endif /* __STDC__ */
507 #define KDB_SAVE_CTXT() \
508 KDB_SAVE(int, db_run_mode); \
509 KDB_SAVE(boolean_t, db_sstep_print); \
510 KDB_SAVE(int, db_loop_count); \
511 KDB_SAVE(int, db_call_depth); \
512 KDB_SAVE(int, db_inst_count); \
513 KDB_SAVE(int, db_last_inst_count); \
514 KDB_SAVE(int, db_load_count); \
515 KDB_SAVE(int, db_store_count); \
516 KDB_SAVE(boolean_t, db_cmd_loop_done); \
517 KDB_SAVE(jmp_buf_t *, db_recover); \
518 KDB_SAVE(db_addr_t, db_dot); \
519 KDB_SAVE(db_addr_t, db_last_addr); \
520 KDB_SAVE(db_addr_t, db_prev); \
521 KDB_SAVE(db_addr_t, db_next); \
522 KDB_SAVE(db_regs_t, ddb_regs);
524 #define KDB_RESTORE_CTXT() \
525 KDB_RESTORE(db_run_mode); \
526 KDB_RESTORE(db_sstep_print); \
527 KDB_RESTORE(db_loop_count); \
528 KDB_RESTORE(db_call_depth); \
529 KDB_RESTORE(db_inst_count); \
530 KDB_RESTORE(db_last_inst_count); \
531 KDB_RESTORE(db_load_count); \
532 KDB_RESTORE(db_store_count); \
533 KDB_RESTORE(db_cmd_loop_done); \
534 KDB_RESTORE(db_recover); \
535 KDB_RESTORE(db_dot); \
536 KDB_RESTORE(db_last_addr); \
537 KDB_RESTORE(db_prev); \
538 KDB_RESTORE(db_next); \
539 KDB_RESTORE(ddb_regs);
542 * switch to another cpu
549 if (cpu
< 0 || cpu
>= NCPUS
|| !debugger_active
[cpu
])
551 db_set_breakpoints();
552 db_set_watchpoints();
556 db_clear_breakpoints();
557 db_clear_watchpoints();
559 if (debugger_cpu
== -1) {/* someone continued */
560 debugger_cpu
= cpu_number();
561 db_continue_cmd(0, 0, 0, "");
574 boolean_t reboot
= TRUE
;
578 while ((c
= *cp
++) != 0) {
579 if (c
== 'r') /* reboot */
581 if (c
== 'h') /* halt */
584 halt_all_cpus(reboot
);
594 extern unsigned int switch_debugger
;