2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
26 * $FreeBSD: src/sys/i386/i386/db_interface.c,v 1.48.2.1 2000/07/07 00:38:46 obrien Exp $
27 * $DragonFly: src/sys/platform/vkernel/i386/db_interface.c,v 1.3 2007/01/09 00:20:17 dillon Exp $
31 * Interface to new debugger.
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/reboot.h>
37 #include <sys/vkernel.h>
38 #include <sys/thread.h>
40 #include <machine/cpu.h>
41 #include <machine/smp.h>
42 #include <machine/globaldata.h>
43 #include <machine/md_var.h>
48 #include <sys/thread2.h>
54 static jmp_buf *db_nofault = 0;
55 extern jmp_buf db_jmpbuf;
57 extern void gdb_handle_exception (db_regs_t *, int, int);
62 static jmp_buf db_global_jmpbuf;
63 static int db_global_jmpbuf_valid;
66 #define rss() ({u_short ss; __asm __volatile("mov %%ss,%0" : "=r" (ss)); ss;})
70 * kdb_trap - field a TRACE or BPT trap
73 kdb_trap(int type, int code, struct i386_saved_state *regs)
75 volatile int ddb_mode = !(boothowto & RB_GDB);
78 * XXX try to do nothing if the console is in graphics mode.
79 * Handle trace traps (and hardware breakpoints...) by ignoring
80 * them except for forgetting about them. Return 0 for other
81 * traps to say that we haven't done anything. The trap handler
82 * will usually panic. We should handle breakpoint traps for
83 * our breakpoints by disarming our breakpoints and fixing up
86 if (cons_unavail && ddb_mode) {
87 if (type == T_TRCTRAP) {
88 regs->tf_eflags &= ~PSL_T;
95 case T_BPTFLT: /* breakpoint */
96 case T_TRCTRAP: /* debug exception */
101 * XXX this is almost useless now. In most cases,
102 * trap_fatal() has already printed a much more verbose
103 * message. However, it is dangerous to print things in
104 * trap_fatal() - kprintf() might be reentered and trap.
105 * The debugger should be given control first.
108 db_printf("kernel: type %d trap, code=%x\n", type, code);
111 jmp_buf *no_fault = db_nofault;
113 longjmp(*no_fault, 1);
118 * This handles unexpected traps in ddb commands, including calls to
119 * non-ddb functions. db_nofault only applies to memory accesses by
120 * internal ddb commands.
122 if (db_global_jmpbuf_valid)
123 longjmp(db_global_jmpbuf, 1);
126 * XXX We really should switch to a local stack here.
131 * If in kernel mode, esp and ss are not saved, so dummy them up.
133 if (ISPL(regs->tf_cs) == 0) {
134 ddb_regs.tf_esp = (int)®s->tf_esp;
135 ddb_regs.tf_ss = rss();
140 db_printf("\nCPU%d stopping CPUs: 0x%08x\n",
141 mycpu->gd_cpuid, mycpu->gd_other_cpus);
143 /* We stop all CPUs except ourselves (obviously) */
144 stop_cpus(mycpu->gd_other_cpus);
146 db_printf(" stopped\n");
149 setjmp(db_global_jmpbuf);
150 db_global_jmpbuf_valid = TRUE;
158 gdb_handle_exception(&ddb_regs, type, code);
161 db_global_jmpbuf_valid = FALSE;
164 db_printf("\nCPU%d restarting CPUs: 0x%08x\n",
165 mycpu->gd_cpuid, stopped_cpus);
167 /* Restart all the CPUs we previously stopped */
168 if (stopped_cpus != mycpu->gd_other_cpus) {
169 db_printf("whoa, other_cpus: 0x%08x, stopped_cpus: 0x%08x\n",
170 mycpu->gd_other_cpus, stopped_cpus);
171 panic("stop_cpus() failed");
173 restart_cpus(stopped_cpus);
175 db_printf(" restarted\n");
179 regs->tf_eip = ddb_regs.tf_eip;
180 regs->tf_eflags = ddb_regs.tf_eflags;
181 regs->tf_eax = ddb_regs.tf_eax;
182 regs->tf_ecx = ddb_regs.tf_ecx;
183 regs->tf_edx = ddb_regs.tf_edx;
184 regs->tf_ebx = ddb_regs.tf_ebx;
187 * If in user mode, the saved ESP and SS were valid, restore them.
189 if (ISPL(regs->tf_cs)) {
190 regs->tf_esp = ddb_regs.tf_esp;
191 regs->tf_ss = ddb_regs.tf_ss & 0xffff;
194 regs->tf_ebp = ddb_regs.tf_ebp;
195 regs->tf_esi = ddb_regs.tf_esi;
196 regs->tf_edi = ddb_regs.tf_edi;
197 regs->tf_es = ddb_regs.tf_es & 0xffff;
198 regs->tf_fs = ddb_regs.tf_fs & 0xffff;
199 regs->tf_gs = ddb_regs.tf_gs & 0xffff;
200 regs->tf_cs = ddb_regs.tf_cs & 0xffff;
201 regs->tf_ds = ddb_regs.tf_ds & 0xffff;
206 * Read bytes from kernel address space for debugger.
209 db_read_bytes(vm_offset_t addr, size_t size, char *data)
213 db_nofault = &db_jmpbuf;
223 * Write bytes to kernel address space for debugger.
226 db_write_bytes(vm_offset_t addr, size_t size, char *data)
230 vpte_t *ptep0 = NULL;
233 vpte_t *ptep1 = NULL;
237 db_nofault = &db_jmpbuf;
239 if (addr > trunc_page((vm_offset_t)btext) - size &&
240 addr < round_page((vm_offset_t)etext)) {
242 ptep0 = pmap_kpte(addr);
246 /* Map another page if the data crosses a page boundary. */
247 if ((*ptep0 & PG_PS) == 0) {
248 addr1 = trunc_page(addr + size - 1);
249 if (trunc_page(addr) != addr1) {
250 ptep1 = pmap_kpte(addr1);
255 addr1 = trunc_4mpage(addr + size - 1);
256 if (trunc_4mpage(addr) != addr1) {
257 ptep1 = pmap_kpte(addr1);
287 * The debugger sometimes needs to know the actual KVM address represented
288 * by the instruction pointer, stack pointer, or base pointer. Normally
289 * the actual KVM address is simply the contents of the register. However,
290 * if the debugger is entered from the BIOS or VM86 we need to figure out
291 * the offset from the segment register.
294 PC_REGS(db_regs_t *regs)
296 return(regs->tf_eip);
300 SP_REGS(db_regs_t *regs)
302 return(regs->tf_esp);
306 BP_REGS(db_regs_t *regs)
308 return(regs->tf_ebp);
313 * Move this to machdep.c and allow it to be called if any debugger is
317 Debugger(const char *msg)
319 static volatile u_char in_Debugger;
323 * Do nothing if the console is in graphics mode. This is
324 * OK if the call is for the debugger hotkey but not if the call
325 * is a weak form of panicing.
327 if (cons_unavail && !(boothowto & RB_GDB))
332 db_printf("Debugger(\"%s\")\n", msg);