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@
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 * Interface to new debugger.
56 #include <platforms.h>
57 #include <time_stamp.h>
58 #include <mach_mp_debug.h>
59 #include <mach_ldebug.h>
61 #include <kern/cpu_number.h>
62 #include <kern/kern_types.h>
63 #include <kern/misc_protos.h>
66 #include <i386/thread.h>
67 #include <i386/db_machdep.h>
69 #include <i386/trap.h>
70 #include <i386/setjmp.h>
71 #include <i386/pmap.h>
72 #include <i386/misc_protos.h>
74 #include <mach/vm_param.h>
75 #include <vm/vm_map.h>
76 #include <kern/thread.h>
77 #include <kern/task.h>
79 #include <ddb/db_command.h>
80 #include <ddb/db_task_thread.h>
81 #include <ddb/db_run.h>
82 #include <ddb/db_trap.h>
83 #include <ddb/db_output.h>
84 #include <ddb/db_access.h>
85 #include <ddb/db_sym.h>
86 #include <ddb/db_break.h>
87 #include <ddb/db_watch.h>
90 struct i386_saved_state
*i386_last_saved_statep
;
91 struct i386_saved_state i386_nested_saved_state
;
92 unsigned i386_last_kdb_sp
;
94 extern thread_t db_default_act
;
95 extern pt_entry_t
*DMAP1
;
96 extern caddr_t DADDR1
;
99 extern int masked_state_cnt
[];
100 #endif /* MACH_MP_DEBUG */
103 * Enter KDB through a keyboard trap.
104 * We show the registers as of the keyboard interrupt
105 * instead of those at its call to KDB.
114 struct i386_interrupt_state
*is
;
117 extern char * trap_type
[];
118 extern int TRAP_TYPES
;
122 extern void kdbprinttrap(
127 extern void kdb_kentry(
128 struct int_regs
*int_regs
);
129 extern int db_user_to_kernel_address(
134 extern void db_write_bytes_user_space(
139 extern int db_search_null(
145 extern int kdb_enter(int);
146 extern void kdb_leave(void);
147 extern void lock_kdb(void);
148 extern void unlock_kdb(void);
151 * kdb_trap - field a TRACE or BPT trap
155 extern jmp_buf_t
*db_recover
;
158 * Translate the state saved in a task state segment into an
159 * exception frame. Since we "know" we always want the state
160 * in a ktss, we hard-wire that in, rather than indexing the gdt
161 * with tss_sel to derive a pointer to the desired tss.
166 struct i386_saved_state
*regs
)
168 extern struct i386_tss ktss
;
169 int mycpu
= cpu_number();
170 struct i386_tss
*tss
;
172 tss
= cpu_datap(mycpu
)->cpu_desc_index
.cdi_ktss
; /* XXX */
175 * ddb will overwrite whatever's in esp, so put esp0 elsewhere, too.
177 regs
->esp
= tss
->esp0
;
178 regs
->efl
= tss
->eflags
;
179 regs
->eip
= tss
->eip
;
180 regs
->trapno
= tss
->ss0
; /* XXX */
181 regs
->err
= tss
->esp0
; /* XXX */
182 regs
->eax
= tss
->eax
;
183 regs
->ecx
= tss
->ecx
;
184 regs
->edx
= tss
->edx
;
185 regs
->ebx
= tss
->ebx
;
186 regs
->uesp
= tss
->esp
;
187 regs
->ebp
= tss
->ebp
;
188 regs
->esi
= tss
->esi
;
189 regs
->edi
= tss
->edi
;
199 * Compose a call to the debugger from the saved state in regs. (No
200 * reason not to do this in C.)
204 struct i386_saved_state
*regs
)
211 return (kdb_trap(type
, code
, regs
));
218 struct i386_saved_state
*regs
)
221 boolean_t trap_from_user
;
225 case T_DEBUG
: /* single_step */
227 extern int dr_addr
[];
231 if (status
& 0xf) { /* hmm hdw break */
232 addr
= status
& 0x8 ? dr_addr
[3] :
233 status
& 0x4 ? dr_addr
[2] :
234 status
& 0x2 ? dr_addr
[1] :
237 db_single_step_cmd(addr
, 0, 1, "p");
240 case T_INT3
: /* breakpoint */
241 case T_WATCHPOINT
: /* watchpoint */
242 case -1: /* keyboard interrupt */
247 i386_nested_saved_state
= *regs
;
248 db_printf("Caught ");
249 if (type
< 0 || type
> TRAP_TYPES
)
250 db_printf("type %d", type
);
252 db_printf("%s", trap_type
[type
]);
253 db_printf(" trap, code = %x, pc = %x\n",
259 kdbprinttrap(type
, code
, (int *)®s
->eip
, regs
->uesp
);
262 disable_preemption();
264 current_cpu_datap()->cpu_kdb_saved_ipl
= s
;
265 current_cpu_datap()->cpu_kdb_saved_state
= regs
;
267 i386_last_saved_statep
= regs
;
268 i386_last_kdb_sp
= (unsigned) &type
;
270 if (!kdb_enter(regs
->eip
))
273 /* Should switch to kdb's own stack here. */
275 if (!IS_USER_TRAP(regs
, &etext
)) {
276 bzero((char *)&ddb_regs
, sizeof (ddb_regs
));
277 *(struct i386_saved_state_from_kernel
*)&ddb_regs
=
278 *(struct i386_saved_state_from_kernel
*)regs
;
279 trap_from_user
= FALSE
;
283 trap_from_user
= TRUE
;
285 if (!trap_from_user
) {
287 * Kernel mode - esp and ss not saved
289 ddb_regs
.uesp
= (int)®s
->uesp
; /* kernel stack pointer */
290 ddb_regs
.ss
= KERNEL_DS
;
294 db_task_trap(type
, code
, trap_from_user
);
297 regs
->eip
= ddb_regs
.eip
;
298 regs
->efl
= ddb_regs
.efl
;
299 regs
->eax
= ddb_regs
.eax
;
300 regs
->ecx
= ddb_regs
.ecx
;
301 regs
->edx
= ddb_regs
.edx
;
302 regs
->ebx
= ddb_regs
.ebx
;
303 if (trap_from_user
) {
305 * user mode - saved esp and ss valid
307 regs
->uesp
= ddb_regs
.uesp
; /* user stack pointer */
308 regs
->ss
= ddb_regs
.ss
& 0xffff; /* user stack segment */
310 regs
->ebp
= ddb_regs
.ebp
;
311 regs
->esi
= ddb_regs
.esi
;
312 regs
->edi
= ddb_regs
.edi
;
313 regs
->es
= ddb_regs
.es
& 0xffff;
314 regs
->cs
= ddb_regs
.cs
& 0xffff;
315 regs
->ds
= ddb_regs
.ds
& 0xffff;
316 regs
->fs
= ddb_regs
.fs
& 0xffff;
317 regs
->gs
= ddb_regs
.gs
& 0xffff;
319 if ((type
== T_INT3
) &&
320 (db_get_task_value(regs
->eip
,
323 db_target_space(current_thread(),
326 regs
->eip
+= BKPT_SIZE
;
331 current_cpu_datap()->cpu_kdb_saved_state
= 0;
334 current_cpu_datap()->cpu_masked_state_cnt
= 0;
335 #endif /* MACH_MP_DEBUG */
341 /* Allow continue to upper layers of exception handling if
342 * trap was not a debugging trap.
345 if (trap_from_user
&& type
!= T_DEBUG
&& type
!= T_INT3
346 && type
!= T_WATCHPOINT
)
353 * Enter KDB through a keyboard trap.
354 * We show the registers as of the keyboard interrupt
355 * instead of those at its call to KDB.
362 struct int_regs
*int_regs
)
365 boolean_t trap_from_user
;
366 struct i386_interrupt_state
*is
= int_regs
->is
;
367 struct i386_saved_state regs
;
373 if (IS_USER_TRAP(is
, &etext
))
375 regs
.uesp
= ((int *)(is
+1))[0];
376 regs
.ss
= ((int *)(is
+1))[1];
380 regs
.uesp
= (int)(is
+1);
388 regs
.ebx
= int_regs
->ebx
;
389 regs
.ebp
= int_regs
->ebp
;
390 regs
.esi
= int_regs
->esi
;
391 regs
.edi
= int_regs
->edi
;
394 regs
.fs
= int_regs
->fs
;
395 regs
.gs
= int_regs
->gs
;
397 disable_preemption();
399 current_cpu_datap()->cpu_kdb_saved_state
= ®s
;
401 if (!kdb_enter(regs
.eip
))
404 bcopy((char *)®s
, (char *)&ddb_regs
, sizeof (ddb_regs
));
405 trap_from_user
= IS_USER_TRAP(&ddb_regs
, &etext
);
408 db_task_trap(-1, 0, trap_from_user
);
411 if (trap_from_user
) {
412 ((int *)(is
+1))[0] = ddb_regs
.uesp
;
413 ((int *)(is
+1))[1] = ddb_regs
.ss
& 0xffff;
415 is
->efl
= ddb_regs
.efl
;
416 is
->cs
= ddb_regs
.cs
& 0xffff;
417 is
->eip
= ddb_regs
.eip
;
418 is
->eax
= ddb_regs
.eax
;
419 is
->ecx
= ddb_regs
.ecx
;
420 is
->edx
= ddb_regs
.edx
;
421 int_regs
->ebx
= ddb_regs
.ebx
;
422 int_regs
->ebp
= ddb_regs
.ebp
;
423 int_regs
->esi
= ddb_regs
.esi
;
424 int_regs
->edi
= ddb_regs
.edi
;
425 is
->ds
= ddb_regs
.ds
& 0xffff;
426 is
->es
= ddb_regs
.es
& 0xffff;
427 int_regs
->fs
= ddb_regs
.fs
& 0xffff;
428 int_regs
->gs
= ddb_regs
.gs
& 0xffff;
432 current_cpu_datap()->cpu_kdb_saved_state
= 0;
451 if (type
< 0 || type
> TRAP_TYPES
)
452 db_printf("type %d", type
);
454 db_printf("%s", trap_type
[type
]);
455 db_printf(" trap, code=%x eip@%x = %x esp=%x\n",
456 code
, pc
, *(int *)pc
, sp
);
457 db_run_mode
= STEP_CONTINUE
;
461 db_user_to_kernel_address(
467 register pt_entry_t
*ptp
;
469 ptp
= pmap_pte(task
->map
->pmap
, addr
);
470 if (ptp
== PT_ENTRY_NULL
|| (*ptp
& INTEL_PTE_VALID
) == 0) {
472 db_printf("\nno memory is assigned to address %08x\n", addr
);
479 src
= (vm_offset_t
)pte_to_pa(*ptp
);
480 *(int *) DMAP1
= INTEL_PTE_VALID
| INTEL_PTE_RW
| (src
& PG_FRAME
) |
481 INTEL_PTE_REF
| INTEL_PTE_MOD
;
482 #if defined(I386_CPU)
483 if (cpu_class
== CPUCLASS_386
) {
488 invlpg((u_int
)DADDR1
);
491 *kaddr
= (unsigned)DADDR1
+ (addr
& PAGE_MASK
);
497 * Read bytes from kernel address space for debugger.
512 if (task
== kernel_task
|| task
== TASK_NULL
) {
513 while (--size
>= 0) {
514 if (addr
++ > VM_MAX_KERNEL_ADDRESS
) {
515 db_printf("\nbad address %x\n", addr
);
524 if (db_user_to_kernel_address(task
, addr
, &kern_addr
, 1) < 0)
526 src
= (char *)kern_addr
;
527 n
= intel_trunc_page(addr
+INTEL_PGBYTES
) - addr
;
538 * Write bytes to kernel address space for debugger.
550 register pt_entry_t
*ptep0
= 0;
551 pt_entry_t oldmap0
= 0;
553 register pt_entry_t
*ptep1
= 0;
554 pt_entry_t oldmap1
= 0;
557 if (task
&& task
!= kernel_task
) {
558 db_write_bytes_user_space(addr
, size
, data
, task
);
563 if (addr
>= VM_MIN_KERNEL_LOADED_ADDRESS
) {
564 db_write_bytes_user_space(addr
, size
, data
, kernel_task
);
568 if (addr
>= VM_MIN_KERNEL_ADDRESS
&&
569 addr
<= (vm_offset_t
)&etext
)
571 ptep0
= pmap_pte(kernel_pmap
, addr
);
573 *ptep0
|= INTEL_PTE_WRITE
;
575 addr1
= i386_trunc_page(addr
+ size
- 1);
576 if (i386_trunc_page(addr
) != addr1
) {
577 /* data crosses a page boundary */
579 ptep1
= pmap_pte(kernel_pmap
, addr1
);
581 *ptep1
|= INTEL_PTE_WRITE
;
588 while (--size
>= 0) {
589 if (addr
++ > VM_MAX_KERNEL_ADDRESS
) {
590 db_printf("\nbad address %x\n", addr
);
607 db_write_bytes_user_space(
618 if (db_user_to_kernel_address(task
, addr
, &kern_addr
, 1) < 0)
620 dst
= (char *)kern_addr
;
621 n
= intel_trunc_page(addr
+INTEL_PGBYTES
) - addr
;
640 if (task
== kernel_task
|| task
== TASK_NULL
) {
641 if (kernel_task
== TASK_NULL
)
644 } else if (task
== TASK_NULL
) {
645 if (current_thread() == THREAD_NULL
)
647 task
= current_thread()->task
;
650 if (db_user_to_kernel_address(task
, addr
, &kern_addr
, 0) < 0)
652 n
= intel_trunc_page(addr
+INTEL_PGBYTES
) - addr
;
668 unsigned kern_addr1
, kern_addr2
;
670 if ((addr1
& (INTEL_PGBYTES
-1)) != (addr2
& (INTEL_PGBYTES
-1)))
672 if (task1
== TASK_NULL
) {
673 if (current_thread() == THREAD_NULL
)
675 task1
= current_thread()->task
;
677 if (db_user_to_kernel_address(task1
, addr1
, &kern_addr1
, 0) < 0 ||
678 db_user_to_kernel_address(task2
, addr2
, &kern_addr2
, 0) < 0)
680 return(kern_addr1
== kern_addr2
);
683 #define DB_USER_STACK_ADDR (VM_MIN_KERNEL_ADDRESS)
684 #define DB_NAME_SEARCH_LIMIT (DB_USER_STACK_ADDR-(INTEL_PGBYTES*3))
694 register unsigned vaddr
;
695 register unsigned *kaddr
;
697 kaddr
= (unsigned *)*skaddr
;
698 for (vaddr
= *svaddr
; vaddr
> evaddr
; vaddr
-= sizeof(unsigned)) {
699 if (vaddr
% INTEL_PGBYTES
== 0) {
700 vaddr
-= sizeof(unsigned);
701 if (db_user_to_kernel_address(task
, vaddr
, skaddr
, 0) < 0)
703 kaddr
= (unsigned *)*skaddr
;
705 vaddr
-= sizeof(unsigned);
708 if ((*kaddr
== 0) ^ (flag
== 0)) {
710 *skaddr
= (unsigned)kaddr
;
723 unsigned vaddr
, kaddr
;
725 vaddr
= DB_USER_STACK_ADDR
;
729 * skip nulls at the end
731 if (db_search_null(task
, &vaddr
, DB_NAME_SEARCH_LIMIT
, &kaddr
, 0) < 0) {
732 db_printf(DB_NULL_TASK_NAME
);
736 * search start of args
738 if (db_search_null(task
, &vaddr
, DB_NAME_SEARCH_LIMIT
, &kaddr
, 1) < 0) {
739 db_printf(DB_NULL_TASK_NAME
);
743 n
= DB_TASK_NAME_LEN
-1;
744 p
= (char *)kaddr
+ sizeof(unsigned);
745 for (vaddr
+= sizeof(int); vaddr
< DB_USER_STACK_ADDR
&& n
> 0;
747 if (vaddr
% INTEL_PGBYTES
== 0) {
748 (void)db_user_to_kernel_address(task
, vaddr
, &kaddr
, 0);
751 db_printf("%c", (*p
< ' ' || *p
> '~')? ' ': *p
);
753 while (n
-- >= 0) /* compare with >= 0 for one more space */
758 * Code used to synchronize kdb among all cpus, one active at a time, switch
759 * from on to another using kdb_on! #cpu or cpu #cpu
762 decl_simple_lock_data(, kdb_lock
) /* kdb lock */
764 #define db_simple_lock_init(l, e) hw_lock_init(&((l)->interlock))
765 #define db_simple_lock_try(l) hw_lock_try(&((l)->interlock))
766 #define db_simple_unlock(l) hw_lock_unlock(&((l)->interlock))
768 int kdb_cpu
= -1; /* current cpu running kdb */
770 volatile unsigned int cpus_holding_bkpts
; /* counter for number of cpus holding
771 breakpoints (ie: cpus that did not
772 insert back breakpoints) */
773 extern boolean_t db_breakpoints_inserted
;
776 db_machdep_init(void)
780 db_simple_lock_init(&kdb_lock
, 0);
781 for (c
= 0; c
< real_ncpus
; ++c
) {
782 db_stacks
[c
] = (vm_offset_t
) (db_stack_store
+
783 (INTSTACK_SIZE
* (c
+ 1)) - sizeof (natural_t
));
784 if (c
== master_cpu
) {
785 dbtss
.esp0
= (int)(db_task_stack_store
+
786 (INTSTACK_SIZE
* (c
+ 1)) - sizeof (natural_t
));
787 dbtss
.esp
= dbtss
.esp0
;
788 dbtss
.eip
= (int)&db_task_start
;
790 * The TSS for the debugging task on each slave CPU
791 * is set up in mp_desc_init().
798 * Called when entering kdb:
799 * Takes kdb lock. If if we were called remotely (slave state) we just
800 * wait for kdb_cpu to be equal to cpu_number(). Otherwise enter kdb if
801 * not active on another cpu.
802 * If db_pass_thru[cpu_number()] > 0, then kdb can't stop now.
811 disable_preemption();
813 mycpu
= cpu_number();
815 if (current_cpu_datap()->cpu_db_pass_thru
) {
820 current_cpu_datap()->cpu_kdb_active
++;
824 db_printf("kdb_enter: cpu %d, is_slave %d, kdb_cpu %d, run mode %d pc %x (%x) holds %d\n",
825 my_cpu
, current_cpu_datap()->cpu_kdb_is_slave
, kdb_cpu
,
826 db_run_mode
, pc
, *(int *)pc
, cpus_holding_bkpts
);
827 if (db_breakpoints_inserted
)
828 cpus_holding_bkpts
++;
829 if (kdb_cpu
== -1 && !current_cpu_datap()->cpu_kdb_is_slave
) {
831 remote_kdb(); /* stop other cpus */
833 } else if (kdb_cpu
== my_cpu
)
848 boolean_t wait
= FALSE
;
850 disable_preemption();
852 my_cpu
= cpu_number();
854 if (db_run_mode
== STEP_CONTINUE
) {
858 if (db_breakpoints_inserted
)
859 cpus_holding_bkpts
--;
860 if (current_cpu_datap()->cpu_kdb_is_slave
)
861 current_cpu_datap()->cpu_kdb_is_slave
--;
863 db_printf("kdb_leave: cpu %d, kdb_cpu %d, run_mode %d pc %x (%x) holds %d\n",
864 my_cpu
, kdb_cpu
, db_run_mode
,
865 ddb_regs
.eip
, *(int *)ddb_regs
.eip
,
869 current_cpu_datap()->cpu_kdb_active
--;
874 while(cpus_holding_bkpts
);
883 extern void kdb_console(void);
885 disable_preemption();
887 my_cpu
= cpu_number();
891 if (kdb_cpu
!= -1 && kdb_cpu
!= my_cpu
) {
894 if (db_simple_lock_try(&kdb_lock
)) {
895 if (kdb_cpu
== -1 || kdb_cpu
== my_cpu
)
897 db_simple_unlock(&kdb_lock
);
905 extern unsigned old_time_stamp
;
906 #endif /* TIME_STAMP */
911 db_simple_unlock(&kdb_lock
);
914 #endif /* TIME_STAMP */
919 #define KDB_SAVE(type, name) extern type name; type name##_save = name
920 #define KDB_RESTORE(name) name = name##_save
922 #define KDB_SAVE(type, name) extern type name; type name/**/_save = name
923 #define KDB_RESTORE(name) name = name/**/_save
924 #endif /* __STDC__ */
926 #define KDB_SAVE_CTXT() \
927 KDB_SAVE(int, db_run_mode); \
928 KDB_SAVE(boolean_t, db_sstep_print); \
929 KDB_SAVE(int, db_loop_count); \
930 KDB_SAVE(int, db_call_depth); \
931 KDB_SAVE(int, db_inst_count); \
932 KDB_SAVE(int, db_last_inst_count); \
933 KDB_SAVE(int, db_load_count); \
934 KDB_SAVE(int, db_store_count); \
935 KDB_SAVE(boolean_t, db_cmd_loop_done); \
936 KDB_SAVE(jmp_buf_t *, db_recover); \
937 KDB_SAVE(db_addr_t, db_dot); \
938 KDB_SAVE(db_addr_t, db_last_addr); \
939 KDB_SAVE(db_addr_t, db_prev); \
940 KDB_SAVE(db_addr_t, db_next); \
941 KDB_SAVE(db_regs_t, ddb_regs);
943 #define KDB_RESTORE_CTXT() \
944 KDB_RESTORE(db_run_mode); \
945 KDB_RESTORE(db_sstep_print); \
946 KDB_RESTORE(db_loop_count); \
947 KDB_RESTORE(db_call_depth); \
948 KDB_RESTORE(db_inst_count); \
949 KDB_RESTORE(db_last_inst_count); \
950 KDB_RESTORE(db_load_count); \
951 KDB_RESTORE(db_store_count); \
952 KDB_RESTORE(db_cmd_loop_done); \
953 KDB_RESTORE(db_recover); \
954 KDB_RESTORE(db_dot); \
955 KDB_RESTORE(db_last_addr); \
956 KDB_RESTORE(db_prev); \
957 KDB_RESTORE(db_next); \
958 KDB_RESTORE(ddb_regs);
961 * switch to another cpu
969 if (cpu
< 0 || cpu
>= real_ncpus
|| !cpu_datap(cpu
)->cpu_kdb_active
)
971 db_set_breakpoints();
972 db_set_watchpoints();
976 db_clear_breakpoints();
977 db_clear_watchpoints();
979 if (kdb_cpu
== -1) {/* someone continued */
980 kdb_cpu
= cpu_number();
981 db_continue_cmd(0, 0, 0, "");
991 boolean_t reboot
= TRUE
;
995 while ((c
= *cp
++) != 0) {
996 if (c
== 'r') /* reboot */
998 if (c
== 'h') /* halt */
1001 halt_all_cpus(reboot
);