2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
14 * 3. Neither the name of The DragonFly Project nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific, prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Mach Operating System
34 * Copyright (c) 1991,1990 Carnegie Mellon University
35 * All Rights Reserved.
37 * Permission to use, copy, modify and distribute this software and its
38 * documentation is hereby granted, provided that both the copyright
39 * notice and this permission notice appear in all copies of the
40 * software, derivative works or modified versions, and any portions
41 * thereof, and that both notices appear in supporting documentation.
43 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
44 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
45 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 * Carnegie Mellon requests users of this software to return to
49 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
50 * School of Computer Science
51 * Carnegie Mellon University
52 * Pittsburgh PA 15213-3890
54 * any improvements or extensions that they make and grant Carnegie the
55 * rights to redistribute these changes.
57 * $FreeBSD: src/sys/i386/i386/db_interface.c,v 1.48.2.1 2000/07/07 00:38:46 obrien Exp $
61 * Interface to new debugger.
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/reboot.h>
67 #include <sys/thread.h>
69 #include <machine/cpu.h>
70 #include <machine/smp.h>
71 #include <machine/globaldata.h>
72 #include <machine/md_var.h>
73 #include <machine/setjmp.h>
80 #include <sys/thread2.h>
82 static jmp_buf *db_nofault = NULL;
83 extern jmp_buf db_jmpbuf;
85 extern void gdb_handle_exception (db_regs_t *, int, int);
90 static jmp_buf db_global_jmpbuf;
91 static int db_global_jmpbuf_valid;
94 #define rss() ({u_short ss; __asm __volatile("mov %%ss,%0" : "=r" (ss)); ss;})
98 * kdb_trap - field a TRACE or BPT trap
101 kdb_trap(int type, int code, struct x86_64_saved_state *regs)
103 volatile int ddb_mode = !(boothowto & RB_GDB);
106 * XXX try to do nothing if the console is in graphics mode.
107 * Handle trace traps (and hardware breakpoints...) by ignoring
108 * them except for forgetting about them. Return 0 for other
109 * traps to say that we haven't done anything. The trap handler
110 * will usually panic. We should handle breakpoint traps for
111 * our breakpoints by disarming our breakpoints and fixing up
114 if (cons_unavail && ddb_mode) {
115 if (type == T_TRCTRAP) {
116 regs->tf_rflags &= ~PSL_T;
123 case T_BPTFLT: /* breakpoint */
124 case T_TRCTRAP: /* debug exception */
129 * XXX this is almost useless now. In most cases,
130 * trap_fatal() has already printed a much more verbose
131 * message. However, it is dangerous to print things in
132 * trap_fatal() - kprintf() might be reentered and trap.
133 * The debugger should be given control first.
136 db_printf("kernel: type %d trap, code=%x\n", type, code);
139 jmp_buf *no_fault = db_nofault;
141 longjmp(*no_fault, 1);
146 * This handles unexpected traps in ddb commands, including calls to
147 * non-ddb functions. db_nofault only applies to memory accesses by
148 * internal ddb commands.
150 if (db_global_jmpbuf_valid)
151 longjmp(db_global_jmpbuf, 1);
154 * XXX We really should switch to a local stack here.
159 db_printf("\nCPU%d stopping CPUs: 0x%016jx\n",
160 mycpu->gd_cpuid, (uintmax_t)CPUMASK_LOWMASK(mycpu->gd_other_cpus));
162 /* We stop all CPUs except ourselves (obviously) */
163 stop_cpus(mycpu->gd_other_cpus);
165 db_printf(" stopped\n");
167 setjmp(db_global_jmpbuf);
168 db_global_jmpbuf_valid = TRUE;
176 gdb_handle_exception(&ddb_regs, type, code);
179 db_global_jmpbuf_valid = FALSE;
181 db_printf("\nCPU%d restarting CPUs: 0x%016jx\n",
182 mycpu->gd_cpuid, (uintmax_t)CPUMASK_LOWMASK(stopped_cpus));
184 /* Restart all the CPUs we previously stopped */
185 if (CPUMASK_CMPMASKNEQ(stopped_cpus, mycpu->gd_other_cpus)) {
186 db_printf("whoa, other_cpus: 0x%016jx, "
187 "stopped_cpus: 0x%016jx\n",
188 (uintmax_t)CPUMASK_LOWMASK(mycpu->gd_other_cpus),
189 (uintmax_t)CPUMASK_LOWMASK(stopped_cpus));
190 panic("stop_cpus() failed");
192 restart_cpus(stopped_cpus);
194 db_printf(" restarted\n");
197 regs->tf_rip = ddb_regs.tf_rip;
198 regs->tf_rflags = ddb_regs.tf_rflags;
199 regs->tf_rax = ddb_regs.tf_rax;
200 regs->tf_rcx = ddb_regs.tf_rcx;
201 regs->tf_rdx = ddb_regs.tf_rdx;
202 regs->tf_rbx = ddb_regs.tf_rbx;
204 regs->tf_rsp = ddb_regs.tf_rsp;
205 regs->tf_ss = ddb_regs.tf_ss & 0xffff;
207 regs->tf_rbp = ddb_regs.tf_rbp;
208 regs->tf_rsi = ddb_regs.tf_rsi;
209 regs->tf_rdi = ddb_regs.tf_rdi;
211 regs->tf_r8 = ddb_regs.tf_r8;
212 regs->tf_r9 = ddb_regs.tf_r9;
213 regs->tf_r10 = ddb_regs.tf_r10;
214 regs->tf_r11 = ddb_regs.tf_r11;
215 regs->tf_r12 = ddb_regs.tf_r12;
216 regs->tf_r13 = ddb_regs.tf_r13;
217 regs->tf_r14 = ddb_regs.tf_r14;
218 regs->tf_r15 = ddb_regs.tf_r15;
220 /* regs->tf_es = ddb_regs.tf_es & 0xffff; */
221 /* regs->tf_fs = ddb_regs.tf_fs & 0xffff; */
222 /* regs->tf_gs = ddb_regs.tf_gs & 0xffff; */
223 regs->tf_cs = ddb_regs.tf_cs & 0xffff;
224 /* regs->tf_ds = ddb_regs.tf_ds & 0xffff; */
229 * Read bytes from kernel address space for debugger.
232 db_read_bytes(vm_offset_t addr, size_t size, char *data)
236 db_nofault = &db_jmpbuf;
246 * Write bytes to kernel address space for debugger.
249 db_write_bytes(vm_offset_t addr, size_t size, char *data)
253 vpte_t *ptep0 = NULL;
256 vpte_t *ptep1 = NULL;
260 db_nofault = &db_jmpbuf;
262 if (addr > trunc_page((vm_offset_t)btext) - size &&
263 addr < round_page((vm_offset_t)etext)) {
265 ptep0 = pmap_kpte(addr);
269 /* Map another page if the data crosses a page boundary. */
270 if ((*ptep0 & PG_PS) == 0) {
271 addr1 = trunc_page(addr + size - 1);
272 if (trunc_page(addr) != addr1) {
273 ptep1 = pmap_kpte(addr1);
278 addr1 = trunc_4mpage(addr + size - 1);
279 if (trunc_4mpage(addr) != addr1) {
280 ptep1 = pmap_kpte(addr1);
310 * The debugger sometimes needs to know the actual KVM address represented
311 * by the instruction pointer, stack pointer, or base pointer. Normally
312 * the actual KVM address is simply the contents of the register. However,
313 * if the debugger is entered from the BIOS or VM86 we need to figure out
314 * the offset from the segment register.
317 PC_REGS(db_regs_t *regs)
319 return(regs->tf_rip);
323 SP_REGS(db_regs_t *regs)
325 return(regs->tf_rsp);
329 BP_REGS(db_regs_t *regs)
331 return(regs->tf_rbp);
336 * Move this to machdep.c and allow it to be called if any debugger is
340 Debugger(const char *msg)
342 static volatile u_char in_Debugger;
346 * Do nothing if the console is in graphics mode. This is
347 * OK if the call is for the debugger hotkey but not if the call
348 * is a weak form of panicing.
350 if (cons_unavail && !(boothowto & RB_GDB))
355 db_printf("Debugger(\"%s\")\n", msg);