2 * Copyright (c) 2000 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@
27 #include <platforms.h>
28 #include <time_stamp.h>
29 #include <mach_mp_debug.h>
30 #include <mach_ldebug.h>
31 #include <db_machine_commands.h>
34 #include <kern/cpu_number.h>
35 #include <kern/kern_types.h>
36 #include <kern/misc_protos.h>
40 #include <ppc/thread.h>
41 #include <ppc/db_machdep.h>
43 #include <ppc/setjmp.h>
45 #include <ppc/misc_protos.h>
46 #include <ppc/exception.h>
47 #include <ppc/db_machdep.h>
48 #include <ppc/mappings.h>
49 #include <ppc/Firmware.h>
51 #include <mach/vm_param.h>
52 #include <mach/machine/vm_types.h>
53 #include <vm/vm_map.h>
54 #include <kern/thread.h>
55 #include <kern/task.h>
56 #include <kern/debug.h>
58 #include <ddb/db_command.h>
59 #include <ddb/db_task_thread.h>
60 #include <ddb/db_run.h>
61 #include <ddb/db_trap.h>
62 #include <ddb/db_output.h>
63 #include <ddb/db_access.h>
64 #include <ddb/db_sym.h>
65 #include <ddb/db_break.h>
66 #include <ddb/db_watch.h>
68 struct savearea
*ppc_last_saved_statep
;
69 struct savearea ppc_nested_saved_state
;
70 unsigned ppc_last_kdb_sp
;
72 extern int debugger_active
[NCPUS
]; /* Debugger active on CPU */
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
;
146 spl_t saved_ipl
[NCPUS
]; /* just to know what IPL was before trap */
147 struct savearea
*saved_state
[NCPUS
];
150 * kdb_trap - field a TRACE or BPT trap
155 struct savearea
*regs
)
157 boolean_t trap_from_user
;
158 int previous_console_device
;
161 previous_console_device
=switch_to_serial_console();
164 case T_TRACE
: /* single_step */
165 case T_PROGRAM
: /* breakpoint */
167 case T_WATCHPOINT
: /* watchpoint */
169 case -1: /* keyboard interrupt */
174 ppc_nested_saved_state
= *regs
;
175 db_printf("Caught ");
176 if (type
> TRAP_TYPES
)
177 db_printf("type %d", type
);
179 db_printf("%s", trap_type
[type
]);
180 db_printf(" trap, pc = %x\n",
185 kdbprinttrap(type
, code
, (int *)®s
->save_srr0
, regs
->save_r1
);
188 saved_state
[cpu_number()] = regs
;
190 ppc_last_saved_statep
= regs
;
191 ppc_last_kdb_sp
= (unsigned) &type
;
193 if (!IS_USER_TRAP(regs
)) {
194 bzero((char *)&ddb_regs
, sizeof (ddb_regs
));
196 trap_from_user
= FALSE
;
201 trap_from_user
= TRUE
;
204 db_task_trap(type
, code
, trap_from_user
);
208 if ((type
== T_PROGRAM
) &&
209 (db_get_task_value(regs
->save_srr0
,
212 db_target_space(current_act(),
215 regs
->save_srr0
+= BKPT_SIZE
;
218 saved_state
[cpu_number()] = 0;
219 switch_to_old_console(previous_console_device
);
236 if (type
> TRAP_TYPES
)
237 db_printf("type %d", type
);
239 db_printf("%s", trap_type
[type
]);
240 db_printf(" trap, code=%x pc@%x = %x sp=%x\n",
241 code
, pc
, *(int *)pc
, sp
);
242 db_run_mode
= STEP_CONTINUE
;
255 pp
= pmap_find_phys(pmap
, (addr64_t
)va
);
257 if (pp
== 0) return(0); /* Couldn't find it */
259 pa
= ((addr64_t
)pp
<< 12) | (addr64_t
)(va
& 0xFFF); /* Get physical address */
265 * Read bytes from task address space for debugger.
281 pmap
= task
->map
->pmap
;
285 phys_src
= db_vtophys(pmap
, (vm_offset_t
)addr
);
287 db_printf("\nno memory is assigned to src address %08x\n",
293 phys_dst
= db_vtophys(kernel_pmap
, (vm_offset_t
)data
);
295 db_printf("\nno memory is assigned to dst address %08x\n",
301 /* don't over-run any page boundaries - check src range */
302 max
= round_page_64(phys_src
+ 1) - phys_src
;
305 /* Check destination won't run over boundary either */
306 n
= round_page_64(phys_dst
+ 1) - phys_dst
;
308 if (n
< max
) max
= n
;
311 phys_copy(phys_src
, phys_dst
, max
);
313 /* resync I+D caches */
314 sync_cache64(phys_dst
, max
);
322 * Write bytes to task address space for debugger.
338 phys_src
= db_vtophys(kernel_pmap
, (vm_offset_t
)data
);
340 db_printf("\nno memory is assigned to src address %08x\n",
346 /* space stays as kernel space unless in another task */
347 if (task
== NULL
) pmap
= kernel_pmap
;
348 else pmap
= task
->map
->pmap
;
350 phys_dst
= db_vtophys(pmap
, (vm_offset_t
)addr
);
352 db_printf("\nno memory is assigned to dst address %08x\n",
358 /* don't over-run any page boundaries - check src range */
359 max
= round_page_64(phys_src
+ 1) - phys_src
;
362 /* Check destination won't run over boundary either */
363 n
= round_page_64(phys_dst
+ 1) - phys_dst
;
368 phys_copy(phys_src
, phys_dst
, max
);
370 /* resync I+D caches */
371 sync_cache64(phys_dst
, max
);
385 unsigned int kern_addr
;
387 if (task
== kernel_task
|| task
== TASK_NULL
) {
388 if (kernel_task
== TASK_NULL
) return(TRUE
);
390 } else if (task
== TASK_NULL
) {
391 if (current_act() == THR_ACT_NULL
) return(FALSE
);
392 task
= current_act()->task
;
396 if(!pmap_find_phys(task
->map
->pmap
, (addr64_t
)addr
)) return (FALSE
); /* Fail if page not mapped */
397 n
= trunc_page_32(addr
+PPC_PGBYTES
) - addr
;
413 addr64_t physa
, physb
;
415 if ((addr1
& (PPC_PGBYTES
-1)) != (addr2
& (PPC_PGBYTES
-1))) /* Is byte displacement the same? */
418 if (task1
== TASK_NULL
) { /* See if there is a task active */
419 if (current_act() == THR_ACT_NULL
) /* See if there is a current task */
421 task1
= current_act()->task
; /* If so, use that one */
424 if(!(physa
= db_vtophys(task1
->map
->pmap
, (vm_offset_t
)trunc_page_32(addr1
)))) return FALSE
; /* Get real address of the first */
425 if(!(physb
= db_vtophys(task2
->map
->pmap
, (vm_offset_t
)trunc_page_32(addr2
)))) return FALSE
; /* Get real address of the second */
427 return (physa
== physb
); /* Check if they are equal, then return... */
430 #define DB_USER_STACK_ADDR (0xc0000000)
431 #define DB_NAME_SEARCH_LIMIT (DB_USER_STACK_ADDR-(PPC_PGBYTES*3))
433 boolean_t
db_phys_cmp(
438 db_printf("db_phys_cmp: not implemented\n");
451 register unsigned vaddr
;
452 register unsigned *kaddr
;
454 db_printf("db_search_null: not implemented\n");
459 unsigned char *getProcName(struct proc
*proc
);
465 register unsigned char *p
;
467 unsigned int vaddr
, kaddr
;
468 unsigned char tname
[33];
474 if(task
->bsd_info
) p
= getProcName((struct proc
*)(task
->bsd_info
)); /* Point to task name */
477 for(i
= 0; i
< 32; i
++) { /* Move no more than 32 bytes */
482 db_printf("%s", tname
);
484 else db_printf("no name");
488 db_machdep_init(void) {
489 #define KDB_READY 0x1
492 kdb_flag
|= KDB_READY
;
497 #define KDB_SAVE(type, name) extern type name; type name##_save = name
498 #define KDB_RESTORE(name) name = name##_save
500 #define KDB_SAVE(type, name) extern type name; type name/**/_save = name
501 #define KDB_RESTORE(name) name = name/**/_save
502 #endif /* __STDC__ */
504 #define KDB_SAVE_CTXT() \
505 KDB_SAVE(int, db_run_mode); \
506 KDB_SAVE(boolean_t, db_sstep_print); \
507 KDB_SAVE(int, db_loop_count); \
508 KDB_SAVE(int, db_call_depth); \
509 KDB_SAVE(int, db_inst_count); \
510 KDB_SAVE(int, db_last_inst_count); \
511 KDB_SAVE(int, db_load_count); \
512 KDB_SAVE(int, db_store_count); \
513 KDB_SAVE(boolean_t, db_cmd_loop_done); \
514 KDB_SAVE(jmp_buf_t *, db_recover); \
515 KDB_SAVE(db_addr_t, db_dot); \
516 KDB_SAVE(db_addr_t, db_last_addr); \
517 KDB_SAVE(db_addr_t, db_prev); \
518 KDB_SAVE(db_addr_t, db_next); \
519 KDB_SAVE(db_regs_t, ddb_regs);
521 #define KDB_RESTORE_CTXT() \
522 KDB_RESTORE(db_run_mode); \
523 KDB_RESTORE(db_sstep_print); \
524 KDB_RESTORE(db_loop_count); \
525 KDB_RESTORE(db_call_depth); \
526 KDB_RESTORE(db_inst_count); \
527 KDB_RESTORE(db_last_inst_count); \
528 KDB_RESTORE(db_load_count); \
529 KDB_RESTORE(db_store_count); \
530 KDB_RESTORE(db_cmd_loop_done); \
531 KDB_RESTORE(db_recover); \
532 KDB_RESTORE(db_dot); \
533 KDB_RESTORE(db_last_addr); \
534 KDB_RESTORE(db_prev); \
535 KDB_RESTORE(db_next); \
536 KDB_RESTORE(ddb_regs);
539 * switch to another cpu
546 if (cpu
< 0 || cpu
>= NCPUS
|| !debugger_active
[cpu
])
548 db_set_breakpoints();
549 db_set_watchpoints();
553 db_clear_breakpoints();
554 db_clear_watchpoints();
556 if (debugger_cpu
== -1) {/* someone continued */
557 debugger_cpu
= cpu_number();
558 db_continue_cmd(0, 0, 0, "");
571 boolean_t reboot
= TRUE
;
575 while ((c
= *cp
++) != 0) {
576 if (c
== 'r') /* reboot */
578 if (c
== 'h') /* halt */
581 halt_all_cpus(reboot
);
591 extern unsigned int switch_debugger
;