2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 #include <platforms.h>
33 #include <time_stamp.h>
34 #include <mach_mp_debug.h>
35 #include <mach_ldebug.h>
36 #include <db_machine_commands.h>
39 #include <kern/cpu_number.h>
40 #include <kern/kern_types.h>
41 #include <kern/misc_protos.h>
45 #include <ppc/db_machdep.h>
47 #include <ppc/setjmp.h>
49 #include <ppc/misc_protos.h>
50 #include <ppc/cpu_internal.h>
51 #include <ppc/exception.h>
52 #include <ppc/db_machdep.h>
53 #include <ppc/mappings.h>
54 #include <ppc/Firmware.h>
55 #include <ppc/serial_io.h> /* for switch_to_serial_console */
57 #include <mach/vm_param.h>
58 #include <mach/machine/vm_types.h>
59 #include <vm/vm_map.h>
60 #include <kern/thread.h>
61 #include <kern/task.h>
62 #include <kern/debug.h>
63 #include <kern/machine.h> /* for halt_all_cpus() */
64 #include <pexpert/pexpert.h>
65 #include <IOKit/IOPlatformExpert.h>
67 #include <ddb/db_command.h>
68 #include <ddb/db_task_thread.h>
69 #include <ddb/db_run.h>
70 #include <ddb/db_trap.h>
71 #include <ddb/db_output.h>
72 #include <ddb/db_access.h>
73 #include <ddb/db_sym.h>
74 #include <ddb/db_break.h>
75 #include <ddb/db_watch.h>
77 struct savearea
*ppc_last_saved_statep
;
78 struct savearea ppc_nested_saved_state
;
79 unsigned ppc_last_kdb_sp
;
81 extern int debugger_cpu
; /* Current cpu running debugger */
83 int db_all_set_up
= 0;
87 void kdp_register_send_receive(void);
91 * Enter KDB through a keyboard trap.
92 * We show the registers as of the keyboard interrupt
93 * instead of those at its call to KDB.
96 /* XXX more registers ? */
97 struct ppc_interrupt_state
*is
;
100 extern int TRAP_TYPES
;
103 * Code used to synchronize kdb among all cpus, one active at a time, switch
104 * from on to another using kdb_on! #cpu or cpu #cpu
107 decl_simple_lock_data(, kdb_lock
) /* kdb lock */
109 #define db_simple_lock_init(l, e) hw_lock_init(&((l)->interlock))
110 #define db_simple_lock_try(l) hw_lock_try(&((l)->interlock))
111 #define db_simple_unlock(l) hw_lock_unlock(&((l)->interlock))
113 extern volatile unsigned int cpus_holding_bkpts
; /* counter for number of cpus holding
114 breakpoints (ie: cpus that did not
115 insert back breakpoints) */
116 extern boolean_t db_breakpoints_inserted
;
120 extern void kdbprinttrap(
125 extern void db_write_bytes_user_space(
130 extern int db_search_null(
136 extern int kdb_enter(int);
137 extern void kdb_leave(void);
138 extern void lock_kdb(void);
139 extern void unlock_kdb(void);
141 #if DB_MACHINE_COMMANDS
142 struct db_command ppc_db_commands
[] = {
143 { "lt", db_low_trace
, CS_MORE
|CS_SET_DOT
, 0 },
144 { (char *)0, 0, 0, 0 }
146 #endif /* DB_MACHINE_COMMANDS */
149 void kdp_register_send_receive(void) {}
152 extern jmp_buf_t
*db_recover
;
155 * kdb_trap - field a TRACE or BPT trap
160 struct savearea
*regs
)
162 boolean_t trap_from_user
;
163 int previous_console_device
;
166 previous_console_device
=switch_to_serial_console();
169 case T_TRACE
: /* single_step */
170 case T_PROGRAM
: /* breakpoint */
172 case T_WATCHPOINT
: /* watchpoint */
174 case -1: /* keyboard interrupt */
179 ppc_nested_saved_state
= *regs
;
180 db_printf("Caught ");
181 if (type
> TRAP_TYPES
)
182 db_printf("type %d", type
);
184 db_printf("%s", trap_type
[type
]);
185 db_printf(" trap, pc = %llx\n",
190 kdbprinttrap(type
, code
, (int *)®s
->save_srr0
, regs
->save_r1
);
193 getPerProc()->db_saved_state
= regs
;
195 ppc_last_saved_statep
= regs
;
196 ppc_last_kdb_sp
= (unsigned) &type
;
198 if (!IS_USER_TRAP(regs
)) {
199 bzero((char *)&ddb_regs
, sizeof (ddb_regs
));
201 trap_from_user
= FALSE
;
206 trap_from_user
= TRUE
;
209 db_task_trap(type
, code
, trap_from_user
);
213 if ((type
== T_PROGRAM
) &&
214 (db_get_task_value(regs
->save_srr0
,
217 db_target_space(current_thread(),
220 regs
->save_srr0
+= BKPT_SIZE
;
222 getPerProc()->db_saved_state
= 0;
223 switch_to_old_console(previous_console_device
);
240 if (type
> TRAP_TYPES
)
241 db_printf("type %d", type
);
243 db_printf("%s", trap_type
[type
]);
244 db_printf(" trap, code=%x pc@%x = %x sp=%x\n",
245 code
, pc
, *(int *)pc
, sp
);
246 db_run_mode
= STEP_CONTINUE
;
253 db_vtophys(pmap_t pmap
, vm_offset_t va
)
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
);
389 if (task
== kernel_task
|| task
== TASK_NULL
) {
390 if (kernel_task
== TASK_NULL
) return(TRUE
);
392 } else if (task
== TASK_NULL
) {
393 if (current_thread() == THR_ACT_NULL
) return(FALSE
);
394 task
= current_thread()->task
;
398 if(!pmap_find_phys(task
->map
->pmap
, (addr64_t
)addr
)) return (FALSE
); /* Fail if page not mapped */
399 n
= trunc_page_32(addr
+PPC_PGBYTES
) - addr
;
415 addr64_t physa
, physb
;
417 if ((addr1
& (PPC_PGBYTES
-1)) != (addr2
& (PPC_PGBYTES
-1))) /* Is byte displacement the same? */
420 if (task1
== TASK_NULL
) { /* See if there is a task active */
421 if (current_thread() == THR_ACT_NULL
) /* See if there is a current task */
423 task1
= current_thread()->task
; /* If so, use that one */
426 if(!(physa
= db_vtophys(task1
->map
->pmap
, (vm_offset_t
)trunc_page_32(addr1
)))) return FALSE
; /* Get real address of the first */
427 if(!(physb
= db_vtophys(task2
->map
->pmap
, (vm_offset_t
)trunc_page_32(addr2
)))) return FALSE
; /* Get real address of the second */
429 return (physa
== physb
); /* Check if they are equal, then return... */
432 #define DB_USER_STACK_ADDR (0xc0000000)
433 #define DB_NAME_SEARCH_LIMIT (DB_USER_STACK_ADDR-(PPC_PGBYTES*3))
436 db_phys_cmp(__unused vm_offset_t a1
, __unused vm_offset_t a2
,
437 __unused vm_size_t s1
)
439 db_printf("db_phys_cmp: not implemented\n");
445 db_search_null(__unused task_t task
, __unused
unsigned *svaddr
,
446 __unused
unsigned evaddr
, __unused
unsigned *skaddr
,
449 db_printf("db_search_null: not implemented\n");
454 unsigned char *getProcName(struct proc
*proc
);
460 register unsigned char *p
;
461 unsigned char tname
[33];
467 if(task
->bsd_info
) p
= getProcName((struct proc
*)(task
->bsd_info
)); /* Point to task name */
470 for(i
= 0; i
< 32; i
++) { /* Move no more than 32 bytes */
475 db_printf("%s", tname
);
477 else db_printf("no name");
482 db_machdep_init(void)
484 #define KDB_READY 0x1
485 kdb_flag
|= KDB_READY
;
490 //#define KDB_SAVE(type, name) extern type name; type name##_save = name
491 #define KDB_SAVE(type, name) type name##_save = name
492 #define KDB_RESTORE(name) name = name##_save
494 #define KDB_SAVE(type, name) type name/**/_save = name
495 //#define KDB_SAVE(type, name) extern type name; type name/**/_save = name
496 #define KDB_RESTORE(name) name = name/**/_save
497 #endif /* __STDC__ */
499 #define KDB_SAVE_CTXT() \
500 KDB_SAVE(int, db_run_mode); \
501 KDB_SAVE(boolean_t, db_sstep_print); \
502 KDB_SAVE(int, db_loop_count); \
503 KDB_SAVE(int, db_call_depth); \
504 KDB_SAVE(int, db_inst_count); \
505 KDB_SAVE(int, db_last_inst_count); \
506 KDB_SAVE(int, db_load_count); \
507 KDB_SAVE(int, db_store_count); \
508 KDB_SAVE(boolean_t, db_cmd_loop_done); \
509 KDB_SAVE(jmp_buf_t *, db_recover); \
510 KDB_SAVE(db_addr_t, db_dot); \
511 KDB_SAVE(db_addr_t, db_last_addr); \
512 KDB_SAVE(db_addr_t, db_prev); \
513 KDB_SAVE(db_addr_t, db_next); \
514 KDB_SAVE(db_regs_t, ddb_regs);
516 #define KDB_RESTORE_CTXT() \
517 KDB_RESTORE(db_run_mode); \
518 KDB_RESTORE(db_sstep_print); \
519 KDB_RESTORE(db_loop_count); \
520 KDB_RESTORE(db_call_depth); \
521 KDB_RESTORE(db_inst_count); \
522 KDB_RESTORE(db_last_inst_count); \
523 KDB_RESTORE(db_load_count); \
524 KDB_RESTORE(db_store_count); \
525 KDB_RESTORE(db_cmd_loop_done); \
526 KDB_RESTORE(db_recover); \
527 KDB_RESTORE(db_dot); \
528 KDB_RESTORE(db_last_addr); \
529 KDB_RESTORE(db_prev); \
530 KDB_RESTORE(db_next); \
531 KDB_RESTORE(ddb_regs);
533 extern boolean_t db_sstep_print
;
534 extern int db_loop_count
;
535 extern int db_call_depth
;
536 extern int db_inst_count
;
537 extern int db_last_inst_count
;
538 extern int db_load_count
;
539 extern int db_store_count
;
540 extern boolean_t db_cmd_loop_done
;
541 extern void unlock_debugger(void);
542 extern void lock_debugger(void);
544 * switch to another cpu
551 if (cpu
< 0 || cpu
>= (int)real_ncpus
|| !PerProcTable
[cpu
].ppe_vaddr
->debugger_active
)
553 db_set_breakpoints();
554 db_set_watchpoints();
558 db_clear_breakpoints();
559 db_clear_watchpoints();
561 if (debugger_cpu
== -1) {/* someone continued */
562 debugger_cpu
= cpu_number();
563 db_continue_cmd(0, 0, 0, NULL
);
572 db_reboot(__unused db_expr_t addr
, __unused boolean_t have_addr
,
573 __unused db_expr_t count
, char *modif
)
575 boolean_t reboot
= TRUE
;
579 while ((c
= *cp
++) != 0) {
580 if (c
== 'r') /* reboot */
582 if (c
== 'h') /* halt */
585 if(!reboot
) halt_all_cpus(FALSE
); /* If no reboot, try to be clean about it */
588 (*PE_halt_restart
)(kPERestartCPU
);
589 db_printf("Sorry, system can't reboot automatically yet... You need to do it by hand...\n");