2 * Copyright (c) 2000-2005 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 <i386/machine_cpu.h>
76 #include <mach/vm_param.h>
77 #include <vm/vm_map.h>
78 #include <kern/thread.h>
79 #include <kern/task.h>
81 #include <ddb/db_command.h>
82 #include <ddb/db_task_thread.h>
83 #include <ddb/db_run.h>
84 #include <ddb/db_trap.h>
85 #include <ddb/db_output.h>
86 #include <ddb/db_access.h>
87 #include <ddb/db_sym.h>
88 #include <ddb/db_break.h>
89 #include <ddb/db_watch.h>
91 #include <i386/cpu_data.h>
94 x86_saved_state32_t
*i386_last_saved_statep
;
95 x86_saved_state32_t i386_nested_saved_state
;
96 unsigned i386_last_kdb_sp
;
98 extern thread_t db_default_act
;
99 extern pt_entry_t
*DMAP1
;
100 extern caddr_t DADDR1
;
103 extern int masked_state_cnt
[];
104 #endif /* MACH_MP_DEBUG */
107 * Enter KDB through a keyboard trap.
108 * We show the registers as of the keyboard interrupt
109 * instead of those at its call to KDB.
118 x86_saved_state32_t
*is
;
121 extern char * trap_type
[];
122 extern int TRAP_TYPES
;
126 extern void kdbprinttrap(
131 extern void kdb_kentry(
132 struct int_regs
*int_regs
);
133 extern int db_user_to_kernel_address(
138 extern void db_write_bytes_user_space(
143 extern int db_search_null(
149 extern int kdb_enter(int);
150 extern void kdb_leave(void);
151 extern void lock_kdb(void);
152 extern void unlock_kdb(void);
155 * kdb_trap - field a TRACE or BPT trap
159 extern jmp_buf_t
*db_recover
;
162 * Translate the state saved in a task state segment into an
163 * exception frame. Since we "know" we always want the state
164 * in a ktss, we hard-wire that in, rather than indexing the gdt
165 * with tss_sel to derive a pointer to the desired tss.
169 * Code used to synchronize kdb among all cpus, one active at a time, switch
170 * from one to another using cpu #cpu
173 decl_simple_lock_data(, kdb_lock
) /* kdb lock */
175 #define db_simple_lock_init(l, e) hw_lock_init(&((l)->interlock))
176 #define db_simple_lock_try(l) hw_lock_try(&((l)->interlock))
177 #define db_simple_unlock(l) hw_lock_unlock(&((l)->interlock))
179 int kdb_cpu
= -1; /* current cpu running kdb */
181 volatile unsigned int cpus_holding_bkpts
; /* counter for number of cpus
182 * holding breakpoints
184 extern boolean_t db_breakpoints_inserted
;
189 x86_saved_state32_t
*regs
)
191 extern struct i386_tss ktss
;
192 int mycpu
= cpu_number();
193 struct i386_tss
*tss
;
195 tss
= cpu_datap(mycpu
)->cpu_desc_index
.cdi_ktss
; /* XXX */
198 * ddb will overwrite whatever's in esp, so put esp0 elsewhere, too.
200 regs
->cr2
= tss
->esp0
;
201 regs
->efl
= tss
->eflags
;
202 regs
->eip
= tss
->eip
;
203 regs
->trapno
= tss
->ss0
; /* XXX */
204 regs
->err
= tss
->esp0
; /* XXX */
205 regs
->eax
= tss
->eax
;
206 regs
->ecx
= tss
->ecx
;
207 regs
->edx
= tss
->edx
;
208 regs
->ebx
= tss
->ebx
;
209 regs
->uesp
= tss
->esp
;
210 regs
->ebp
= tss
->ebp
;
211 regs
->esi
= tss
->esi
;
212 regs
->edi
= tss
->edi
;
222 * Compose a call to the debugger from the saved state in regs. (No
223 * reason not to do this in C.)
227 x86_saved_state32_t
*regs
)
234 return (kdb_trap(type
, code
, regs
));
241 x86_saved_state32_t
*regs
)
244 boolean_t trap_from_user
;
246 int previous_console_device
;
250 previous_console_device
= switch_to_serial_console();
252 db_printf("kdb_trap(): type %d, code %d, regs->eip 0x%x\n", type
, code
, regs
->eip
);
254 case T_DEBUG
: /* single_step */
256 extern int dr_addr
[];
260 if (status
& 0xf) { /* hmm hdw break */
261 addr
= status
& 0x8 ? dr_addr
[3] :
262 status
& 0x4 ? dr_addr
[2] :
263 status
& 0x2 ? dr_addr
[1] :
266 db_single_step_cmd(addr
, 0, 1, "p");
269 case T_INT3
: /* breakpoint */
270 case T_WATCHPOINT
: /* watchpoint */
271 case -1: /* keyboard interrupt */
276 i386_nested_saved_state
= *regs
;
277 db_printf("Caught ");
278 if (type
< 0 || type
> TRAP_TYPES
)
279 db_printf("type %d", type
);
281 db_printf("%s", trap_type
[type
]);
282 db_printf(" trap, code = %x, pc = %x\n",
288 kdbprinttrap(type
, code
, (int *)®s
->eip
, regs
->uesp
);
291 disable_preemption();
293 current_cpu_datap()->cpu_kdb_saved_ipl
= s
;
294 current_cpu_datap()->cpu_kdb_saved_state
= regs
;
296 i386_last_saved_statep
= regs
;
297 i386_last_kdb_sp
= (unsigned) &type
;
299 if (!kdb_enter(regs
->eip
))
302 /* Should switch to kdb's own stack here. */
304 if (!IS_USER_TRAP(regs
, &etext
)) {
305 bzero((char *)&ddb_regs
, sizeof (ddb_regs
));
306 *(struct x86_saved_state32_from_kernel
*)&ddb_regs
=
307 *(struct x86_saved_state32_from_kernel
*)regs
;
308 trap_from_user
= FALSE
;
312 trap_from_user
= TRUE
;
314 if (!trap_from_user
) {
316 * Kernel mode - esp and ss not saved
318 ddb_regs
.uesp
= (int)®s
->uesp
; /* kernel stack pointer */
319 ddb_regs
.ss
= KERNEL_DS
;
323 db_task_trap(type
, code
, trap_from_user
);
326 regs
->eip
= ddb_regs
.eip
;
327 regs
->efl
= ddb_regs
.efl
;
328 regs
->eax
= ddb_regs
.eax
;
329 regs
->ecx
= ddb_regs
.ecx
;
330 regs
->edx
= ddb_regs
.edx
;
331 regs
->ebx
= ddb_regs
.ebx
;
333 if (trap_from_user
) {
335 * user mode - saved esp and ss valid
337 regs
->uesp
= ddb_regs
.uesp
; /* user stack pointer */
338 regs
->ss
= ddb_regs
.ss
& 0xffff; /* user stack segment */
341 regs
->ebp
= ddb_regs
.ebp
;
342 regs
->esi
= ddb_regs
.esi
;
343 regs
->edi
= ddb_regs
.edi
;
344 regs
->es
= ddb_regs
.es
& 0xffff;
345 regs
->cs
= ddb_regs
.cs
& 0xffff;
346 regs
->ds
= ddb_regs
.ds
& 0xffff;
347 regs
->fs
= ddb_regs
.fs
& 0xffff;
348 regs
->gs
= ddb_regs
.gs
& 0xffff;
350 if ((type
== T_INT3
) &&
351 (db_get_task_value(regs
->eip
,
354 db_target_space(current_thread(),
357 regs
->eip
+= BKPT_SIZE
;
359 switch_to_old_console(previous_console_device
);
363 current_cpu_datap()->cpu_kdb_saved_state
= 0;
369 /* Allow continue to upper layers of exception handling if
370 * trap was not a debugging trap.
373 if (trap_from_user
&& type
!= T_DEBUG
&& type
!= T_INT3
374 && type
!= T_WATCHPOINT
)
381 * Enter KDB through a keyboard trap.
382 * We show the registers as of the keyboard interrupt
383 * instead of those at its call to KDB.
390 struct int_regs
*int_regs
)
393 boolean_t trap_from_user
;
394 x86_saved_state32_t
*is
= int_regs
->is
;
395 x86_saved_state32_t regs
;
401 if (IS_USER_TRAP(is
, &etext
))
403 regs
.uesp
= ((int *)(is
+1))[0];
404 regs
.ss
= ((int *)(is
+1))[1];
408 regs
.uesp
= (int)(is
+1);
416 regs
.ebx
= int_regs
->ebx
;
417 regs
.ebp
= int_regs
->ebp
;
418 regs
.esi
= int_regs
->esi
;
419 regs
.edi
= int_regs
->edi
;
422 regs
.fs
= int_regs
->fs
;
423 regs
.gs
= int_regs
->gs
;
425 disable_preemption();
427 current_cpu_datap()->cpu_kdb_saved_state
= ®s
;
429 if (!kdb_enter(regs
.eip
))
432 bcopy((char *)®s
, (char *)&ddb_regs
, sizeof (ddb_regs
));
433 trap_from_user
= IS_USER_TRAP(&ddb_regs
, &etext
);
436 db_task_trap(-1, 0, trap_from_user
);
439 if (trap_from_user
) {
440 ((int *)(is
+1))[0] = ddb_regs
.uesp
;
441 ((int *)(is
+1))[1] = ddb_regs
.ss
& 0xffff;
443 is
->efl
= ddb_regs
.efl
;
444 is
->cs
= ddb_regs
.cs
& 0xffff;
445 is
->eip
= ddb_regs
.eip
;
446 is
->eax
= ddb_regs
.eax
;
447 is
->ecx
= ddb_regs
.ecx
;
448 is
->edx
= ddb_regs
.edx
;
449 int_regs
->ebx
= ddb_regs
.ebx
;
450 int_regs
->ebp
= ddb_regs
.ebp
;
451 int_regs
->esi
= ddb_regs
.esi
;
452 int_regs
->edi
= ddb_regs
.edi
;
453 is
->ds
= ddb_regs
.ds
& 0xffff;
454 is
->es
= ddb_regs
.es
& 0xffff;
455 int_regs
->fs
= ddb_regs
.fs
& 0xffff;
456 int_regs
->gs
= ddb_regs
.gs
& 0xffff;
460 current_cpu_datap()->cpu_kdb_saved_state
= 0;
479 if (type
< 0 || type
> TRAP_TYPES
)
480 db_printf("type %d", type
);
482 db_printf("%s", trap_type
[type
]);
483 db_printf(" trap, code=%x eip@%x = %x esp=%x\n",
484 code
, pc
, *(int *)pc
, sp
);
485 db_run_mode
= STEP_CONTINUE
;
489 db_user_to_kernel_address(
495 register pt_entry_t
*ptp
;
499 * must not pre-empted while using the pte pointer passed
500 * back since it's been mapped through a per-cpu window
502 mp_disable_preemption();
504 ptp
= pmap_pte(task
->map
->pmap
, (vm_map_offset_t
)addr
);
505 if (ptp
== PT_ENTRY_NULL
|| (*ptp
& INTEL_PTE_VALID
) == 0) {
507 db_printf("\nno memory is assigned to address %08x\n", addr
);
511 mp_enable_preemption();
514 src
= (vm_offset_t
)pte_to_pa(*ptp
);
516 mp_enable_preemption();
518 *(int *) DMAP1
= INTEL_PTE_VALID
| INTEL_PTE_RW
| (src
& PG_FRAME
) |
519 INTEL_PTE_REF
| INTEL_PTE_MOD
;
520 #if defined(I386_CPU)
521 if (cpu_class
== CPUCLASS_386
) {
526 invlpg((u_int
)DADDR1
);
529 *kaddr
= (unsigned)DADDR1
+ (addr
& PAGE_MASK
);
535 * Read bytes from kernel address space for debugger.
550 if (task
== kernel_task
|| task
== TASK_NULL
) {
551 while (--size
>= 0) {
552 if (addr
++ > VM_MAX_KERNEL_ADDRESS
) {
553 db_printf("\nbad address %x\n", addr
);
562 if (db_user_to_kernel_address(task
, addr
, &kern_addr
, 1) < 0)
564 src
= (char *)kern_addr
;
565 n
= intel_trunc_page(addr
+INTEL_PGBYTES
) - addr
;
576 * Write bytes to kernel address space for debugger.
588 register pt_entry_t
*ptep0
= 0;
589 pt_entry_t oldmap0
= 0;
591 register pt_entry_t
*ptep1
= 0;
592 pt_entry_t oldmap1
= 0;
595 if (task
&& task
!= kernel_task
) {
596 db_write_bytes_user_space(addr
, size
, data
, task
);
601 if (addr
>= VM_MIN_KERNEL_LOADED_ADDRESS
) {
602 db_write_bytes_user_space(addr
, size
, data
, kernel_task
);
606 if (addr
>= VM_MIN_KERNEL_ADDRESS
&&
607 addr
<= (vm_offset_t
)&etext
)
609 ptep0
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)addr
);
611 *ptep0
|= INTEL_PTE_WRITE
;
613 addr1
= i386_trunc_page(addr
+ size
- 1);
614 if (i386_trunc_page(addr
) != addr1
) {
615 /* data crosses a page boundary */
617 ptep1
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)addr1
);
619 *ptep1
|= INTEL_PTE_WRITE
;
626 while (--size
>= 0) {
627 if (addr
++ > VM_MAX_KERNEL_ADDRESS
) {
628 db_printf("\nbad address %x\n", addr
);
645 db_write_bytes_user_space(
656 if (db_user_to_kernel_address(task
, addr
, &kern_addr
, 1) < 0)
658 dst
= (char *)kern_addr
;
659 n
= intel_trunc_page(addr
+INTEL_PGBYTES
) - addr
;
678 if (task
== kernel_task
|| task
== TASK_NULL
) {
679 if (kernel_task
== TASK_NULL
)
682 } else if (task
== TASK_NULL
) {
683 if (current_thread() == THREAD_NULL
)
685 task
= current_thread()->task
;
688 if (db_user_to_kernel_address(task
, addr
, &kern_addr
, 0) < 0)
690 n
= intel_trunc_page(addr
+INTEL_PGBYTES
) - addr
;
706 unsigned kern_addr1
, kern_addr2
;
708 if ((addr1
& (INTEL_PGBYTES
-1)) != (addr2
& (INTEL_PGBYTES
-1)))
710 if (task1
== TASK_NULL
) {
711 if (current_thread() == THREAD_NULL
)
713 task1
= current_thread()->task
;
715 if (db_user_to_kernel_address(task1
, addr1
, &kern_addr1
, 0) < 0 ||
716 db_user_to_kernel_address(task2
, addr2
, &kern_addr2
, 0) < 0)
718 return(kern_addr1
== kern_addr2
);
721 #define DB_USER_STACK_ADDR (VM_MIN_KERNEL_ADDRESS)
722 #define DB_NAME_SEARCH_LIMIT (DB_USER_STACK_ADDR-(INTEL_PGBYTES*3))
732 register unsigned vaddr
;
733 register unsigned *kaddr
;
735 kaddr
= (unsigned *)*skaddr
;
736 for (vaddr
= *svaddr
; vaddr
> evaddr
; vaddr
-= sizeof(unsigned)) {
737 if (vaddr
% INTEL_PGBYTES
== 0) {
738 vaddr
-= sizeof(unsigned);
739 if (db_user_to_kernel_address(task
, vaddr
, skaddr
, 0) < 0)
741 kaddr
= (unsigned *)*skaddr
;
743 vaddr
-= sizeof(unsigned);
746 if ((*kaddr
== 0) ^ (flag
== 0)) {
748 *skaddr
= (unsigned)kaddr
;
761 unsigned vaddr
, kaddr
;
763 vaddr
= DB_USER_STACK_ADDR
;
767 * skip nulls at the end
769 if (db_search_null(task
, &vaddr
, DB_NAME_SEARCH_LIMIT
, &kaddr
, 0) < 0) {
770 db_printf(DB_NULL_TASK_NAME
);
774 * search start of args
776 if (db_search_null(task
, &vaddr
, DB_NAME_SEARCH_LIMIT
, &kaddr
, 1) < 0) {
777 db_printf(DB_NULL_TASK_NAME
);
781 n
= DB_TASK_NAME_LEN
-1;
782 p
= (char *)kaddr
+ sizeof(unsigned);
783 for (vaddr
+= sizeof(int); vaddr
< DB_USER_STACK_ADDR
&& n
> 0;
785 if (vaddr
% INTEL_PGBYTES
== 0) {
786 (void)db_user_to_kernel_address(task
, vaddr
, &kaddr
, 0);
789 db_printf("%c", (*p
< ' ' || *p
> '~')? ' ': *p
);
791 while (n
-- >= 0) /* compare with >= 0 for one more space */
796 db_machdep_init(void)
800 db_simple_lock_init(&kdb_lock
, 0);
801 for (c
= 0; c
< real_ncpus
; ++c
) {
802 if (c
== master_cpu
) {
803 master_dbtss
.esp0
= (int)(db_task_stack_store
+
804 (INTSTACK_SIZE
* (c
+ 1)) - sizeof (natural_t
));
805 master_dbtss
.esp
= master_dbtss
.esp0
;
806 master_dbtss
.eip
= (int)&db_task_start
;
808 * The TSS for the debugging task on each slave CPU
809 * is set up in cpu_desc_init().
816 * Called when entering kdb:
817 * Takes kdb lock. If if we were called remotely (slave state) we just
818 * wait for kdb_cpu to be equal to cpu_number(). Otherwise enter kdb if
819 * not active on another cpu.
820 * If db_pass_thru[cpu_number()] > 0, then kdb can't stop now.
829 disable_preemption();
831 my_cpu
= cpu_number();
833 if (current_cpu_datap()->cpu_db_pass_thru
) {
838 current_cpu_datap()->cpu_kdb_active
++;
842 db_printf("kdb_enter(): cpu_number %d, kdb_cpu %d\n", my_cpu
, kdb_cpu
);
844 if (db_breakpoints_inserted
)
845 cpus_holding_bkpts
++;
847 if (kdb_cpu
== -1 && !current_cpu_datap()->cpu_kdb_is_slave
) {
849 db_printf("Signaling other processors..\n");
850 remote_kdb(); /* stop other cpus */
852 } else if (kdb_cpu
== my_cpu
)
867 boolean_t wait
= FALSE
;
869 disable_preemption();
871 my_cpu
= cpu_number();
873 if (db_run_mode
== STEP_CONTINUE
) {
877 if (db_breakpoints_inserted
)
878 cpus_holding_bkpts
--;
879 if (current_cpu_datap()->cpu_kdb_is_slave
)
880 current_cpu_datap()->cpu_kdb_is_slave
--;
882 db_printf("kdb_leave: cpu %d, kdb_cpu %d, run_mode %d pc %x (%x) holds %d\n",
883 my_cpu
, kdb_cpu
, db_run_mode
,
884 ddb_regs
.eip
, *(int *)ddb_regs
.eip
,
888 current_cpu_datap()->cpu_kdb_active
--;
895 while(cpus_holding_bkpts
);
905 disable_preemption();
907 my_cpu
= cpu_number();
910 if (kdb_cpu
!= -1 && kdb_cpu
!= my_cpu
) {
913 if (db_simple_lock_try(&kdb_lock
)) {
914 if (kdb_cpu
== -1 || kdb_cpu
== my_cpu
)
916 db_simple_unlock(&kdb_lock
);
924 extern unsigned old_time_stamp
;
925 #endif /* TIME_STAMP */
930 db_simple_unlock(&kdb_lock
);
933 #endif /* TIME_STAMP */
938 #define KDB_SAVE(type, name) extern type name; type name##_save = name
939 #define KDB_RESTORE(name) name = name##_save
941 #define KDB_SAVE(type, name) extern type name; type name/**/_save = name
942 #define KDB_RESTORE(name) name = name/**/_save
943 #endif /* __STDC__ */
945 #define KDB_SAVE_CTXT() \
946 KDB_SAVE(int, db_run_mode); \
947 KDB_SAVE(boolean_t, db_sstep_print); \
948 KDB_SAVE(int, db_loop_count); \
949 KDB_SAVE(int, db_call_depth); \
950 KDB_SAVE(int, db_inst_count); \
951 KDB_SAVE(int, db_last_inst_count); \
952 KDB_SAVE(int, db_load_count); \
953 KDB_SAVE(int, db_store_count); \
954 KDB_SAVE(boolean_t, db_cmd_loop_done); \
955 KDB_SAVE(jmp_buf_t *, db_recover); \
956 KDB_SAVE(db_addr_t, db_dot); \
957 KDB_SAVE(db_addr_t, db_last_addr); \
958 KDB_SAVE(db_addr_t, db_prev); \
959 KDB_SAVE(db_addr_t, db_next); \
960 KDB_SAVE(db_regs_t, ddb_regs);
962 #define KDB_RESTORE_CTXT() \
963 KDB_RESTORE(db_run_mode); \
964 KDB_RESTORE(db_sstep_print); \
965 KDB_RESTORE(db_loop_count); \
966 KDB_RESTORE(db_call_depth); \
967 KDB_RESTORE(db_inst_count); \
968 KDB_RESTORE(db_last_inst_count); \
969 KDB_RESTORE(db_load_count); \
970 KDB_RESTORE(db_store_count); \
971 KDB_RESTORE(db_cmd_loop_done); \
972 KDB_RESTORE(db_recover); \
973 KDB_RESTORE(db_dot); \
974 KDB_RESTORE(db_last_addr); \
975 KDB_RESTORE(db_prev); \
976 KDB_RESTORE(db_next); \
977 KDB_RESTORE(ddb_regs);
980 * switch to another cpu
988 if (cpu
< 0 || cpu
>= real_ncpus
|| !cpu_datap(cpu
)->cpu_kdb_active
)
990 db_set_breakpoints();
991 db_set_watchpoints();
995 db_clear_breakpoints();
996 db_clear_watchpoints();
998 if (kdb_cpu
== -1) {/* someone continued */
999 kdb_cpu
= cpu_number();
1000 db_continue_cmd(0, 0, 0, "");
1008 extern void kdp_reboot(void);
1012 boolean_t have_addr
,