1 /* 2 * Copyright (c) 2008 The DragonFly Project. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 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 13 * distribution. 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. 17 * 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 29 * SUCH DAMAGE. 30 * 31 * -- 32 * 33 * Mach Operating System 34 * Copyright (c) 1991,1990 Carnegie Mellon University 35 * All Rights Reserved. 36 * 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. 42 * 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. 46 * 47 * Carnegie Mellon requests users of this software to return to 48 * 49 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 50 * School of Computer Science 51 * Carnegie Mellon University 52 * Pittsburgh PA 15213-3890 53 * 54 * any improvements or extensions that they make and grant Carnegie the 55 * rights to redistribute these changes. 56 * 57 * $FreeBSD: src/sys/i386/i386/db_interface.c,v 1.48.2.1 2000/07/07 00:38:46 obrien Exp $ 58 */ 59 60 /* 61 * Interface to new debugger. 62 */ 63 #include <sys/param.h> 64 #include <sys/systm.h> 65 #include <sys/reboot.h> 66 #include <sys/cons.h> 67 #include <sys/thread.h> 68 #include <sys/kerneldump.h> 69 70 #include <machine/cpu.h> 71 #include <machine/smp.h> 72 #include <machine/globaldata.h> 73 #include <machine/md_var.h> 74 75 #include <vm/vm.h> 76 #include <vm/pmap.h> 77 78 #include <ddb/ddb.h> 79 80 #include <sys/thread2.h> 81 82 #include <machine/setjmp.h> 83 84 static jmp_buf *db_nofault = NULL; 85 extern jmp_buf db_jmpbuf; 86 87 extern void gdb_handle_exception (db_regs_t *, int, int); 88 89 int db_active; 90 db_regs_t ddb_regs; 91 92 static jmp_buf db_global_jmpbuf; 93 static int db_global_jmpbuf_valid; 94 95 #ifdef __GNUC__ 96 #define rss() ({u_short ss; __asm __volatile("mov %%ss,%0" : "=r" (ss)); ss;}) 97 #endif 98 99 /* 100 * kdb_trap - field a TRACE or BPT trap 101 */ 102 int 103 kdb_trap(int type, int code, struct x86_64_saved_state *regs) 104 { 105 volatile int ddb_mode = !(boothowto & RB_GDB); 106 107 /* 108 * XXX try to do nothing if the console is in graphics mode. 109 * Handle trace traps (and hardware breakpoints...) by ignoring 110 * them except for forgetting about them. Return 0 for other 111 * traps to say that we haven't done anything. The trap handler 112 * will usually panic. We should handle breakpoint traps for 113 * our breakpoints by disarming our breakpoints and fixing up 114 * %eip. 115 */ 116 if (cons_unavail && ddb_mode) { 117 if (type == T_TRCTRAP) { 118 regs->tf_rflags &= ~PSL_T; 119 return (1); 120 } 121 return (0); 122 } 123 124 switch (type) { 125 case T_BPTFLT: /* breakpoint */ 126 case T_TRCTRAP: /* debug exception */ 127 break; 128 129 default: 130 /* 131 * XXX this is almost useless now. In most cases, 132 * trap_fatal() has already printed a much more verbose 133 * message. However, it is dangerous to print things in 134 * trap_fatal() - kprintf() might be reentered and trap. 135 * The debugger should be given control first. 136 */ 137 if (ddb_mode) 138 db_printf("kernel: type %d trap, code=%x\n", type, code); 139 140 if (db_nofault) { 141 jmp_buf *no_fault = db_nofault; 142 db_nofault = NULL; 143 longjmp(*no_fault, 1); 144 } 145 } 146 147 /* 148 * This handles unexpected traps in ddb commands, including calls to 149 * non-ddb functions. db_nofault only applies to memory accesses by 150 * internal ddb commands. 151 */ 152 if (db_global_jmpbuf_valid) 153 longjmp(db_global_jmpbuf, 1); 154 155 /* 156 * XXX We really should switch to a local stack here. 157 */ 158 ddb_regs = *regs; 159 160 crit_enter(); 161 db_printf("\nCPU%d stopping CPUs: 0x%08jx\n", 162 mycpu->gd_cpuid, 163 (uintmax_t)CPUMASK_LOWMASK(mycpu->gd_other_cpus)); 164 165 /* We stop all CPUs except ourselves (obviously) */ 166 stop_cpus(mycpu->gd_other_cpus); 167 168 db_printf(" stopped\n"); 169 170 setjmp(db_global_jmpbuf); 171 db_global_jmpbuf_valid = TRUE; 172 db_active++; 173 /* vcons_set_mode(1); */ 174 if (ddb_mode) { 175 cndbctl(TRUE); 176 db_trap(type, code); 177 cndbctl(FALSE); 178 } else 179 gdb_handle_exception(&ddb_regs, type, code); 180 db_active--; 181 /* vcons_set_mode(0); */ 182 db_global_jmpbuf_valid = FALSE; 183 184 if (panicstr == NULL) { 185 db_printf("\nCPU%d restarting CPUs: 0x%016jx\n", 186 mycpu->gd_cpuid, 187 (uintmax_t)CPUMASK_LOWMASK(stopped_cpus)); 188 189 /* Restart all the CPUs we previously stopped */ 190 if (CPUMASK_CMPMASKNEQ(stopped_cpus, mycpu->gd_other_cpus)) { 191 db_printf("whoa, other_cpus: 0x%016jx, " 192 "stopped_cpus: 0x%016jx\n", 193 (uintmax_t)CPUMASK_LOWMASK(mycpu->gd_other_cpus), 194 (uintmax_t)CPUMASK_LOWMASK(stopped_cpus)); 195 panic("stop_cpus() failed"); 196 } 197 restart_cpus(stopped_cpus); 198 } 199 200 db_printf(" restarted\n"); 201 crit_exit(); 202 203 regs->tf_rip = ddb_regs.tf_rip; 204 regs->tf_rflags = ddb_regs.tf_rflags; 205 regs->tf_rax = ddb_regs.tf_rax; 206 regs->tf_rcx = ddb_regs.tf_rcx; 207 regs->tf_rdx = ddb_regs.tf_rdx; 208 regs->tf_rbx = ddb_regs.tf_rbx; 209 210 regs->tf_rsp = ddb_regs.tf_rsp; 211 regs->tf_ss = ddb_regs.tf_ss & 0xffff; 212 213 regs->tf_rbp = ddb_regs.tf_rbp; 214 regs->tf_rsi = ddb_regs.tf_rsi; 215 regs->tf_rdi = ddb_regs.tf_rdi; 216 217 regs->tf_r8 = ddb_regs.tf_r8; 218 regs->tf_r9 = ddb_regs.tf_r9; 219 regs->tf_r10 = ddb_regs.tf_r10; 220 regs->tf_r11 = ddb_regs.tf_r11; 221 regs->tf_r12 = ddb_regs.tf_r12; 222 regs->tf_r13 = ddb_regs.tf_r13; 223 regs->tf_r14 = ddb_regs.tf_r14; 224 regs->tf_r15 = ddb_regs.tf_r15; 225 226 /* regs->tf_es = ddb_regs.tf_es & 0xffff; */ 227 /* regs->tf_fs = ddb_regs.tf_fs & 0xffff; */ 228 /* regs->tf_gs = ddb_regs.tf_gs & 0xffff; */ 229 regs->tf_cs = ddb_regs.tf_cs & 0xffff; 230 /* regs->tf_ds = ddb_regs.tf_ds & 0xffff; */ 231 return (1); 232 } 233 234 /* 235 * Read bytes from kernel address space for debugger. 236 */ 237 void 238 db_read_bytes(vm_offset_t addr, size_t size, char *data) 239 { 240 char *src; 241 242 db_nofault = &db_jmpbuf; 243 244 src = (char *)addr; 245 while (size-- > 0) 246 *data++ = *src++; 247 248 db_nofault = NULL; 249 } 250 251 /* 252 * Write bytes to kernel address space for debugger. 253 */ 254 void 255 db_write_bytes(vm_offset_t addr, size_t size, char *data) 256 { 257 char *dst; 258 #if 0 259 vpte_t *ptep0 = NULL; 260 vpte_t oldmap0 = 0; 261 vm_offset_t addr1; 262 vpte_t *ptep1 = NULL; 263 vpte_t oldmap1 = 0; 264 #endif 265 266 db_nofault = &db_jmpbuf; 267 #if 0 268 if (addr > trunc_page((vm_offset_t)btext) - size && 269 addr < round_page((vm_offset_t)etext)) { 270 271 ptep0 = pmap_kpte(addr); 272 oldmap0 = *ptep0; 273 *ptep0 |= VPTE_RW; 274 275 /* Map another page if the data crosses a page boundary. */ 276 if ((*ptep0 & PG_PS) == 0) { 277 addr1 = trunc_page(addr + size - 1); 278 if (trunc_page(addr) != addr1) { 279 ptep1 = pmap_kpte(addr1); 280 oldmap1 = *ptep1; 281 *ptep1 |= VPTE_RW; 282 } 283 } else { 284 addr1 = trunc_4mpage(addr + size - 1); 285 if (trunc_4mpage(addr) != addr1) { 286 ptep1 = pmap_kpte(addr1); 287 oldmap1 = *ptep1; 288 *ptep1 |= VPTE_RW; 289 } 290 } 291 292 cpu_invltlb(); 293 } 294 #endif 295 296 dst = (char *)addr; 297 298 while (size-- > 0) 299 *dst++ = *data++; 300 301 db_nofault = NULL; 302 303 #if 0 304 if (ptep0) { 305 *ptep0 = oldmap0; 306 307 if (ptep1) 308 *ptep1 = oldmap1; 309 310 cpu_invltlb(); 311 } 312 #endif 313 } 314 315 /* 316 * The debugger sometimes needs to know the actual KVM address represented 317 * by the instruction pointer, stack pointer, or base pointer. Normally 318 * the actual KVM address is simply the contents of the register. However, 319 * if the debugger is entered from the BIOS or VM86 we need to figure out 320 * the offset from the segment register. 321 */ 322 db_addr_t 323 PC_REGS(db_regs_t *regs) 324 { 325 return(regs->tf_rip); 326 } 327 328 db_addr_t 329 SP_REGS(db_regs_t *regs) 330 { 331 return(regs->tf_rsp); 332 } 333 334 db_addr_t 335 BP_REGS(db_regs_t *regs) 336 { 337 return(regs->tf_rbp); 338 } 339 340 /* 341 * XXX 342 * Move this to machdep.c and allow it to be called if any debugger is 343 * installed. 344 */ 345 void 346 Debugger(const char *msg) 347 { 348 static volatile u_char in_Debugger; 349 350 /* 351 * XXX 352 * Do nothing if the console is in graphics mode. This is 353 * OK if the call is for the debugger hotkey but not if the call 354 * is a weak form of panicing. 355 */ 356 if (cons_unavail && !(boothowto & RB_GDB)) { 357 /* 358 * If we are panicing, panic() expects cpus to be stopped 359 * when Debugger() returns. 360 */ 361 if (panicstr != NULL) 362 stop_cpus(mycpu->gd_other_cpus); 363 return; 364 } 365 366 if (!in_Debugger) { 367 in_Debugger = 1; 368 db_printf("Debugger(\"%s\")\n", msg); 369 370 /* 371 * Save the pcb just in case the sysop entered the debugger 372 * manually and called dumpsys, 373 */ 374 if (dumpthread == NULL) { 375 savectx(&dumppcb); 376 dumpthread = curthread; 377 } 378 379 breakpoint(); 380 in_Debugger = 0; 381 382 /* 383 * Clear before returning from the debugger so a later panic 384 * saves the correct context. 385 */ 386 dumpthread = NULL; 387 } 388 } 389