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 ppc_saved_state
*ppc_last_saved_statep
;
69 struct ppc_saved_state 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 int db_user_to_kernel_address(
123 extern void db_write_bytes_user_space(
128 extern int db_search_null(
134 extern int kdb_enter(int);
135 extern void kdb_leave(void);
136 extern void lock_kdb(void);
137 extern void unlock_kdb(void);
139 #if DB_MACHINE_COMMANDS
140 struct db_command ppc_db_commands
[] = {
141 { "lt", db_low_trace
, CS_MORE
|CS_SET_DOT
, 0 },
142 { (char *)0, 0, 0, 0 }
144 #endif /* DB_MACHINE_COMMANDS */
147 void kdp_register_send_receive(void) {}
150 extern jmp_buf_t
*db_recover
;
151 spl_t saved_ipl
[NCPUS
]; /* just to know what IPL was before trap */
152 struct ppc_saved_state
*saved_state
[NCPUS
];
155 * kdb_trap - field a TRACE or BPT trap
160 struct ppc_saved_state
*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 = %x\n",
190 kdbprinttrap(type
, code
, (int *)®s
->srr0
, regs
->r1
);
193 saved_state
[cpu_number()] = 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
->srr0
,
217 db_target_space(current_act(),
220 regs
->srr0
+= BKPT_SIZE
;
223 saved_state
[cpu_number()] = 0;
224 switch_to_old_console(previous_console_device
);
241 if (type
> TRAP_TYPES
)
242 db_printf("type %d", type
);
244 db_printf("%s", trap_type
[type
]);
245 db_printf(" trap, code=%x pc@%x = %x sp=%x\n",
246 code
, pc
, *(int *)pc
, sp
);
247 db_run_mode
= STEP_CONTINUE
;
253 vm_offset_t
db_vtophys(
257 register mapping
*mp
;
258 register vm_offset_t pa
;
260 pa
= (vm_offset_t
)LRA(pmap
->space
,(void *)va
);
265 mp
= hw_lock_phys_vir(pmap
->space
, va
);
266 if((unsigned int)mp
&1) {
270 if(!mp
) { /* If it was not a normal page */
271 pa
= hw_cvp_blk(pmap
, va
); /* Try to convert odd-sized page (returns 0 if not found) */
272 return pa
; /* Return physical address */
275 mp
= hw_cpv(mp
); /* Convert to virtual address */
278 pa
= (vm_offset_t
)((mp
->PTEr
& -PAGE_SIZE
) | ((unsigned int)va
& (PAGE_SIZE
-1)));
280 pa
= (vm_offset_t
)((mp
->physent
->pte1
& -PAGE_SIZE
) | ((unsigned int)va
& (PAGE_SIZE
-1)));
281 hw_unlock_bit((unsigned int *)&mp
->physent
->phys_link
, PHYS_LOCK
);
288 db_user_to_kernel_address(
294 unsigned int sr_val
, raddr
;
296 raddr
= (unsigned int)db_vtophys(task
->map
->pmap
, trunc_page(addr
)); /* Get the real address */
300 db_printf("\nno memory is assigned to address %08x\n", addr
);
306 sr_val
= SEG_REG_PROT
| task
->map
->pmap
->space
307 | ((addr
>> 8) & 0x00F00000);
309 mtsr(SR_COPYIN_NUM
, sr_val
);
311 *kaddr
= (addr
& 0x0fffffff) | (SR_COPYIN_NUM
<< 28);
316 * Read bytes from task address space for debugger.
332 pmap
= task
->map
->pmap
;
336 phys_src
= (unsigned int)db_vtophys(pmap
, trunc_page(addr
));
338 db_printf("\nno memory is assigned to src address %08x\n",
343 phys_src
= phys_src
| (addr
& page_mask
);
345 phys_dst
= (unsigned int)db_vtophys(kernel_pmap
, trunc_page(data
));
347 db_printf("\nno memory is assigned to dst address %08x\n",
353 phys_dst
= phys_dst
| (((vm_offset_t
) data
) & page_mask
);
355 /* don't over-run any page boundaries - check src range */
356 max
= ppc_round_page(phys_src
) - phys_src
;
359 /* Check destination won't run over boundary either */
360 n
= ppc_round_page(phys_dst
) - phys_dst
;
365 phys_copy(phys_src
, phys_dst
, max
);
367 /* resync I+D caches */
368 sync_cache(phys_dst
, max
);
376 * Write bytes to task address space for debugger.
392 phys_src
= (unsigned int)db_vtophys(kernel_pmap
, trunc_page(data
));
394 db_printf("\nno memory is assigned to src address %08x\n",
400 phys_src
= phys_src
| (((vm_offset_t
) data
) & page_mask
);
402 /* space stays as kernel space unless in another task */
403 if (task
== NULL
) pmap
= kernel_pmap
;
404 else pmap
= task
->map
->pmap
;
406 phys_dst
= (unsigned int)db_vtophys(pmap
, trunc_page(addr
));
408 db_printf("\nno memory is assigned to dst address %08x\n",
413 phys_dst
= phys_dst
| (addr
& page_mask
);
415 /* don't over-run any page boundaries - check src range */
416 max
= ppc_round_page(phys_src
) - phys_src
;
419 /* Check destination won't run over boundary either */
420 n
= ppc_round_page(phys_dst
) - phys_dst
;
425 phys_copy(phys_src
, phys_dst
, max
);
427 /* resync I+D caches */
428 sync_cache(phys_dst
, max
);
442 unsigned int kern_addr
;
444 if (task
== kernel_task
|| task
== TASK_NULL
) {
445 if (kernel_task
== TASK_NULL
)
448 } else if (task
== TASK_NULL
) {
449 if (current_act() == THR_ACT_NULL
)
451 task
= current_act()->task
;
454 if (db_user_to_kernel_address(task
, addr
, &kern_addr
, 0) < 0)
456 n
= ppc_trunc_page(addr
+PPC_PGBYTES
) - addr
;
472 vm_offset_t physa
, physb
;
474 if ((addr1
& (PPC_PGBYTES
-1)) != (addr2
& (PPC_PGBYTES
-1))) /* Is byte displacement the same? */
477 if (task1
== TASK_NULL
) { /* See if there is a task active */
478 if (current_act() == THR_ACT_NULL
) /* See if there is a current task */
480 task1
= current_act()->task
; /* If so, use that one */
483 if(!(physa
= db_vtophys(task1
->map
->pmap
, trunc_page(addr1
)))) return FALSE
; /* Get real address of the first */
484 if(!(physb
= db_vtophys(task2
->map
->pmap
, trunc_page(addr2
)))) return FALSE
; /* Get real address of the second */
486 return (physa
== physb
); /* Check if they are equal, then return... */
489 #define DB_USER_STACK_ADDR (0xc0000000)
490 #define DB_NAME_SEARCH_LIMIT (DB_USER_STACK_ADDR-(PPC_PGBYTES*3))
500 register unsigned vaddr
;
501 register unsigned *kaddr
;
503 kaddr
= (unsigned *)*skaddr
;
504 for (vaddr
= *svaddr
; vaddr
> evaddr
; ) {
505 if (vaddr
% PPC_PGBYTES
== 0) {
506 vaddr
-= sizeof(unsigned);
507 if (db_user_to_kernel_address(task
, vaddr
, skaddr
, 0) < 0)
509 kaddr
= (unsigned *)*skaddr
;
511 vaddr
-= sizeof(unsigned);
514 if ((*kaddr
== 0) ^ (flag
== 0)) {
516 *skaddr
= (unsigned)kaddr
;
529 unsigned int vaddr
, kaddr
;
531 vaddr
= DB_USER_STACK_ADDR
;
535 * skip nulls at the end
537 if (db_search_null(task
, &vaddr
, DB_NAME_SEARCH_LIMIT
, &kaddr
, 0) < 0) {
538 db_printf(DB_NULL_TASK_NAME
);
542 * search start of args
544 if (db_search_null(task
, &vaddr
, DB_NAME_SEARCH_LIMIT
, &kaddr
, 1) < 0) {
545 db_printf(DB_NULL_TASK_NAME
);
549 n
= DB_TASK_NAME_LEN
-1;
550 p
= (char *)kaddr
+ sizeof(unsigned);
551 for (vaddr
+= sizeof(int); vaddr
< DB_USER_STACK_ADDR
&& n
> 0;
553 if (vaddr
% PPC_PGBYTES
== 0) {
554 if (db_user_to_kernel_address(task
, vaddr
, &kaddr
, 0) <0)
558 db_printf("%c", (*p
< ' ' || *p
> '~')? ' ': *p
);
560 while (n
-- >= 0) /* compare with >= 0 for one more space */
565 db_machdep_init(void) {
566 #define KDB_READY 0x1
569 kdb_flag
|= KDB_READY
;
574 #define KDB_SAVE(type, name) extern type name; type name##_save = name
575 #define KDB_RESTORE(name) name = name##_save
577 #define KDB_SAVE(type, name) extern type name; type name/**/_save = name
578 #define KDB_RESTORE(name) name = name/**/_save
579 #endif /* __STDC__ */
581 #define KDB_SAVE_CTXT() \
582 KDB_SAVE(int, db_run_mode); \
583 KDB_SAVE(boolean_t, db_sstep_print); \
584 KDB_SAVE(int, db_loop_count); \
585 KDB_SAVE(int, db_call_depth); \
586 KDB_SAVE(int, db_inst_count); \
587 KDB_SAVE(int, db_last_inst_count); \
588 KDB_SAVE(int, db_load_count); \
589 KDB_SAVE(int, db_store_count); \
590 KDB_SAVE(boolean_t, db_cmd_loop_done); \
591 KDB_SAVE(jmp_buf_t *, db_recover); \
592 KDB_SAVE(db_addr_t, db_dot); \
593 KDB_SAVE(db_addr_t, db_last_addr); \
594 KDB_SAVE(db_addr_t, db_prev); \
595 KDB_SAVE(db_addr_t, db_next); \
596 KDB_SAVE(db_regs_t, ddb_regs);
598 #define KDB_RESTORE_CTXT() \
599 KDB_RESTORE(db_run_mode); \
600 KDB_RESTORE(db_sstep_print); \
601 KDB_RESTORE(db_loop_count); \
602 KDB_RESTORE(db_call_depth); \
603 KDB_RESTORE(db_inst_count); \
604 KDB_RESTORE(db_last_inst_count); \
605 KDB_RESTORE(db_load_count); \
606 KDB_RESTORE(db_store_count); \
607 KDB_RESTORE(db_cmd_loop_done); \
608 KDB_RESTORE(db_recover); \
609 KDB_RESTORE(db_dot); \
610 KDB_RESTORE(db_last_addr); \
611 KDB_RESTORE(db_prev); \
612 KDB_RESTORE(db_next); \
613 KDB_RESTORE(ddb_regs);
616 * switch to another cpu
623 if (cpu
< 0 || cpu
>= NCPUS
|| !debugger_active
[cpu
])
625 db_set_breakpoints();
626 db_set_watchpoints();
630 db_clear_breakpoints();
631 db_clear_watchpoints();
633 if (debugger_cpu
== -1) {/* someone continued */
634 debugger_cpu
= cpu_number();
635 db_continue_cmd(0, 0, 0, "");
648 boolean_t reboot
= TRUE
;
652 while ((c
= *cp
++) != 0) {
653 if (c
== 'r') /* reboot */
655 if (c
== 'h') /* halt */
658 halt_all_cpus(reboot
);
668 extern unsigned int switch_debugger
;