kgdb.c revision a5d09d68335bb8422d5e7050c9f03f99ba6cfebd
1/* 2 * This program is free software; you can redistribute it and/or modify it 3 * under the terms of the GNU General Public License as published by the 4 * Free Software Foundation; either version 2, or (at your option) any 5 * later version. 6 * 7 * This program is distributed in the hope that it will be useful, but 8 * WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * General Public License for more details. 11 * 12 */ 13 14/* 15 * Copyright (C) 2004 Amit S. Kale <amitkale@linsyssoft.com> 16 * Copyright (C) 2000-2001 VERITAS Software Corporation. 17 * Copyright (C) 2002 Andi Kleen, SuSE Labs 18 * Copyright (C) 2004 LinSysSoft Technologies Pvt. Ltd. 19 * Copyright (C) 2007 MontaVista Software, Inc. 20 * Copyright (C) 2007-2008 Jason Wessel, Wind River Systems, Inc. 21 */ 22/**************************************************************************** 23 * Contributor: Lake Stevens Instrument Division$ 24 * Written by: Glenn Engel $ 25 * Updated by: Amit Kale<akale@veritas.com> 26 * Updated by: Tom Rini <trini@kernel.crashing.org> 27 * Updated by: Jason Wessel <jason.wessel@windriver.com> 28 * Modified for 386 by Jim Kingdon, Cygnus Support. 29 * Origianl kgdb, compatibility with 2.1.xx kernel by 30 * David Grothe <dave@gcom.com> 31 * Integrated into 2.2.5 kernel by Tigran Aivazian <tigran@sco.com> 32 * X86_64 changes from Andi Kleen's patch merged by Jim Houston 33 */ 34#include <linux/spinlock.h> 35#include <linux/kdebug.h> 36#include <linux/string.h> 37#include <linux/kernel.h> 38#include <linux/ptrace.h> 39#include <linux/sched.h> 40#include <linux/delay.h> 41#include <linux/kgdb.h> 42#include <linux/init.h> 43#include <linux/smp.h> 44#include <linux/nmi.h> 45 46#include <asm/debugreg.h> 47#include <asm/apicdef.h> 48#include <asm/system.h> 49 50#include <asm/apic.h> 51 52/* 53 * Put the error code here just in case the user cares: 54 */ 55static int gdb_x86errcode; 56 57/* 58 * Likewise, the vector number here (since GDB only gets the signal 59 * number through the usual means, and that's not very specific): 60 */ 61static int gdb_x86vector = -1; 62 63/** 64 * pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs 65 * @gdb_regs: A pointer to hold the registers in the order GDB wants. 66 * @regs: The &struct pt_regs of the current process. 67 * 68 * Convert the pt_regs in @regs into the format for registers that 69 * GDB expects, stored in @gdb_regs. 70 */ 71void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs) 72{ 73#ifndef CONFIG_X86_32 74 u32 *gdb_regs32 = (u32 *)gdb_regs; 75#endif 76 gdb_regs[GDB_AX] = regs->ax; 77 gdb_regs[GDB_BX] = regs->bx; 78 gdb_regs[GDB_CX] = regs->cx; 79 gdb_regs[GDB_DX] = regs->dx; 80 gdb_regs[GDB_SI] = regs->si; 81 gdb_regs[GDB_DI] = regs->di; 82 gdb_regs[GDB_BP] = regs->bp; 83 gdb_regs[GDB_PC] = regs->ip; 84#ifdef CONFIG_X86_32 85 gdb_regs[GDB_PS] = regs->flags; 86 gdb_regs[GDB_DS] = regs->ds; 87 gdb_regs[GDB_ES] = regs->es; 88 gdb_regs[GDB_CS] = regs->cs; 89 gdb_regs[GDB_SS] = __KERNEL_DS; 90 gdb_regs[GDB_FS] = 0xFFFF; 91 gdb_regs[GDB_GS] = 0xFFFF; 92#else 93 gdb_regs[GDB_R8] = regs->r8; 94 gdb_regs[GDB_R9] = regs->r9; 95 gdb_regs[GDB_R10] = regs->r10; 96 gdb_regs[GDB_R11] = regs->r11; 97 gdb_regs[GDB_R12] = regs->r12; 98 gdb_regs[GDB_R13] = regs->r13; 99 gdb_regs[GDB_R14] = regs->r14; 100 gdb_regs[GDB_R15] = regs->r15; 101 gdb_regs32[GDB_PS] = regs->flags; 102 gdb_regs32[GDB_CS] = regs->cs; 103 gdb_regs32[GDB_SS] = regs->ss; 104#endif 105 gdb_regs[GDB_SP] = kernel_stack_pointer(regs); 106} 107 108/** 109 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs 110 * @gdb_regs: A pointer to hold the registers in the order GDB wants. 111 * @p: The &struct task_struct of the desired process. 112 * 113 * Convert the register values of the sleeping process in @p to 114 * the format that GDB expects. 115 * This function is called when kgdb does not have access to the 116 * &struct pt_regs and therefore it should fill the gdb registers 117 * @gdb_regs with what has been saved in &struct thread_struct 118 * thread field during switch_to. 119 */ 120void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p) 121{ 122#ifndef CONFIG_X86_32 123 u32 *gdb_regs32 = (u32 *)gdb_regs; 124#endif 125 gdb_regs[GDB_AX] = 0; 126 gdb_regs[GDB_BX] = 0; 127 gdb_regs[GDB_CX] = 0; 128 gdb_regs[GDB_DX] = 0; 129 gdb_regs[GDB_SI] = 0; 130 gdb_regs[GDB_DI] = 0; 131 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp; 132#ifdef CONFIG_X86_32 133 gdb_regs[GDB_DS] = __KERNEL_DS; 134 gdb_regs[GDB_ES] = __KERNEL_DS; 135 gdb_regs[GDB_PS] = 0; 136 gdb_regs[GDB_CS] = __KERNEL_CS; 137 gdb_regs[GDB_PC] = p->thread.ip; 138 gdb_regs[GDB_SS] = __KERNEL_DS; 139 gdb_regs[GDB_FS] = 0xFFFF; 140 gdb_regs[GDB_GS] = 0xFFFF; 141#else 142 gdb_regs32[GDB_PS] = *(unsigned long *)(p->thread.sp + 8); 143 gdb_regs32[GDB_CS] = __KERNEL_CS; 144 gdb_regs32[GDB_SS] = __KERNEL_DS; 145 gdb_regs[GDB_PC] = 0; 146 gdb_regs[GDB_R8] = 0; 147 gdb_regs[GDB_R9] = 0; 148 gdb_regs[GDB_R10] = 0; 149 gdb_regs[GDB_R11] = 0; 150 gdb_regs[GDB_R12] = 0; 151 gdb_regs[GDB_R13] = 0; 152 gdb_regs[GDB_R14] = 0; 153 gdb_regs[GDB_R15] = 0; 154#endif 155 gdb_regs[GDB_SP] = p->thread.sp; 156} 157 158/** 159 * gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs. 160 * @gdb_regs: A pointer to hold the registers we've received from GDB. 161 * @regs: A pointer to a &struct pt_regs to hold these values in. 162 * 163 * Convert the GDB regs in @gdb_regs into the pt_regs, and store them 164 * in @regs. 165 */ 166void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs) 167{ 168#ifndef CONFIG_X86_32 169 u32 *gdb_regs32 = (u32 *)gdb_regs; 170#endif 171 regs->ax = gdb_regs[GDB_AX]; 172 regs->bx = gdb_regs[GDB_BX]; 173 regs->cx = gdb_regs[GDB_CX]; 174 regs->dx = gdb_regs[GDB_DX]; 175 regs->si = gdb_regs[GDB_SI]; 176 regs->di = gdb_regs[GDB_DI]; 177 regs->bp = gdb_regs[GDB_BP]; 178 regs->ip = gdb_regs[GDB_PC]; 179#ifdef CONFIG_X86_32 180 regs->flags = gdb_regs[GDB_PS]; 181 regs->ds = gdb_regs[GDB_DS]; 182 regs->es = gdb_regs[GDB_ES]; 183 regs->cs = gdb_regs[GDB_CS]; 184#else 185 regs->r8 = gdb_regs[GDB_R8]; 186 regs->r9 = gdb_regs[GDB_R9]; 187 regs->r10 = gdb_regs[GDB_R10]; 188 regs->r11 = gdb_regs[GDB_R11]; 189 regs->r12 = gdb_regs[GDB_R12]; 190 regs->r13 = gdb_regs[GDB_R13]; 191 regs->r14 = gdb_regs[GDB_R14]; 192 regs->r15 = gdb_regs[GDB_R15]; 193 regs->flags = gdb_regs32[GDB_PS]; 194 regs->cs = gdb_regs32[GDB_CS]; 195 regs->ss = gdb_regs32[GDB_SS]; 196#endif 197} 198 199static struct hw_breakpoint { 200 unsigned enabled; 201 unsigned type; 202 unsigned len; 203 unsigned long addr; 204} breakinfo[4]; 205 206static void kgdb_correct_hw_break(void) 207{ 208 unsigned long dr7; 209 int correctit = 0; 210 int breakbit; 211 int breakno; 212 213 get_debugreg(dr7, 7); 214 for (breakno = 0; breakno < 4; breakno++) { 215 breakbit = 2 << (breakno << 1); 216 if (!(dr7 & breakbit) && breakinfo[breakno].enabled) { 217 correctit = 1; 218 dr7 |= breakbit; 219 dr7 &= ~(0xf0000 << (breakno << 2)); 220 dr7 |= ((breakinfo[breakno].len << 2) | 221 breakinfo[breakno].type) << 222 ((breakno << 2) + 16); 223 set_debugreg(breakinfo[breakno].addr, breakno); 224 225 } else { 226 if ((dr7 & breakbit) && !breakinfo[breakno].enabled) { 227 correctit = 1; 228 dr7 &= ~breakbit; 229 dr7 &= ~(0xf0000 << (breakno << 2)); 230 } 231 } 232 } 233 if (correctit) 234 set_debugreg(dr7, 7); 235} 236 237static int 238kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype) 239{ 240 int i; 241 242 for (i = 0; i < 4; i++) 243 if (breakinfo[i].addr == addr && breakinfo[i].enabled) 244 break; 245 if (i == 4) 246 return -1; 247 248 breakinfo[i].enabled = 0; 249 250 return 0; 251} 252 253static void kgdb_remove_all_hw_break(void) 254{ 255 int i; 256 257 for (i = 0; i < 4; i++) 258 memset(&breakinfo[i], 0, sizeof(struct hw_breakpoint)); 259} 260 261static int 262kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype) 263{ 264 unsigned type; 265 int i; 266 267 for (i = 0; i < 4; i++) 268 if (!breakinfo[i].enabled) 269 break; 270 if (i == 4) 271 return -1; 272 273 switch (bptype) { 274 case BP_HARDWARE_BREAKPOINT: 275 type = 0; 276 len = 1; 277 break; 278 case BP_WRITE_WATCHPOINT: 279 type = 1; 280 break; 281 case BP_ACCESS_WATCHPOINT: 282 type = 3; 283 break; 284 default: 285 return -1; 286 } 287 288 if (len == 1 || len == 2 || len == 4) 289 breakinfo[i].len = len - 1; 290 else 291 return -1; 292 293 breakinfo[i].enabled = 1; 294 breakinfo[i].addr = addr; 295 breakinfo[i].type = type; 296 297 return 0; 298} 299 300/** 301 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb. 302 * @regs: Current &struct pt_regs. 303 * 304 * This function will be called if the particular architecture must 305 * disable hardware debugging while it is processing gdb packets or 306 * handling exception. 307 */ 308void kgdb_disable_hw_debug(struct pt_regs *regs) 309{ 310 /* Disable hardware debugging while we are in kgdb: */ 311 set_debugreg(0UL, 7); 312} 313 314/** 315 * kgdb_post_primary_code - Save error vector/code numbers. 316 * @regs: Original pt_regs. 317 * @e_vector: Original error vector. 318 * @err_code: Original error code. 319 * 320 * This is needed on architectures which support SMP and KGDB. 321 * This function is called after all the slave cpus have been put 322 * to a know spin state and the primary CPU has control over KGDB. 323 */ 324void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code) 325{ 326 /* primary processor is completely in the debugger */ 327 gdb_x86vector = e_vector; 328 gdb_x86errcode = err_code; 329} 330 331#ifdef CONFIG_SMP 332/** 333 * kgdb_roundup_cpus - Get other CPUs into a holding pattern 334 * @flags: Current IRQ state 335 * 336 * On SMP systems, we need to get the attention of the other CPUs 337 * and get them be in a known state. This should do what is needed 338 * to get the other CPUs to call kgdb_wait(). Note that on some arches, 339 * the NMI approach is not used for rounding up all the CPUs. For example, 340 * in case of MIPS, smp_call_function() is used to roundup CPUs. In 341 * this case, we have to make sure that interrupts are enabled before 342 * calling smp_call_function(). The argument to this function is 343 * the flags that will be used when restoring the interrupts. There is 344 * local_irq_save() call before kgdb_roundup_cpus(). 345 * 346 * On non-SMP systems, this is not called. 347 */ 348void kgdb_roundup_cpus(unsigned long flags) 349{ 350 apic->send_IPI_allbutself(APIC_DM_NMI); 351} 352#endif 353 354/** 355 * kgdb_arch_handle_exception - Handle architecture specific GDB packets. 356 * @vector: The error vector of the exception that happened. 357 * @signo: The signal number of the exception that happened. 358 * @err_code: The error code of the exception that happened. 359 * @remcom_in_buffer: The buffer of the packet we have read. 360 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into. 361 * @regs: The &struct pt_regs of the current process. 362 * 363 * This function MUST handle the 'c' and 's' command packets, 364 * as well packets to set / remove a hardware breakpoint, if used. 365 * If there are additional packets which the hardware needs to handle, 366 * they are handled here. The code should return -1 if it wants to 367 * process more packets, and a %0 or %1 if it wants to exit from the 368 * kgdb callback. 369 */ 370int kgdb_arch_handle_exception(int e_vector, int signo, int err_code, 371 char *remcomInBuffer, char *remcomOutBuffer, 372 struct pt_regs *linux_regs) 373{ 374 unsigned long addr; 375 unsigned long dr6; 376 char *ptr; 377 int newPC; 378 379 switch (remcomInBuffer[0]) { 380 case 'c': 381 case 's': 382 /* try to read optional parameter, pc unchanged if no parm */ 383 ptr = &remcomInBuffer[1]; 384 if (kgdb_hex2long(&ptr, &addr)) 385 linux_regs->ip = addr; 386 case 'D': 387 case 'k': 388 newPC = linux_regs->ip; 389 390 /* clear the trace bit */ 391 linux_regs->flags &= ~X86_EFLAGS_TF; 392 atomic_set(&kgdb_cpu_doing_single_step, -1); 393 394 /* set the trace bit if we're stepping */ 395 if (remcomInBuffer[0] == 's') { 396 linux_regs->flags |= X86_EFLAGS_TF; 397 kgdb_single_step = 1; 398 atomic_set(&kgdb_cpu_doing_single_step, 399 raw_smp_processor_id()); 400 } 401 402 get_debugreg(dr6, 6); 403 if (!(dr6 & 0x4000)) { 404 int breakno; 405 406 for (breakno = 0; breakno < 4; breakno++) { 407 if (dr6 & (1 << breakno) && 408 breakinfo[breakno].type == 0) { 409 /* Set restore flag: */ 410 linux_regs->flags |= X86_EFLAGS_RF; 411 break; 412 } 413 } 414 } 415 set_debugreg(0UL, 6); 416 kgdb_correct_hw_break(); 417 418 return 0; 419 } 420 421 /* this means that we do not want to exit from the handler: */ 422 return -1; 423} 424 425static inline int 426single_step_cont(struct pt_regs *regs, struct die_args *args) 427{ 428 /* 429 * Single step exception from kernel space to user space so 430 * eat the exception and continue the process: 431 */ 432 printk(KERN_ERR "KGDB: trap/step from kernel to user space, " 433 "resuming...\n"); 434 kgdb_arch_handle_exception(args->trapnr, args->signr, 435 args->err, "c", "", regs); 436 /* 437 * Reset the BS bit in dr6 (pointed by args->err) to 438 * denote completion of processing 439 */ 440 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP; 441 442 return NOTIFY_STOP; 443} 444 445static int was_in_debug_nmi[NR_CPUS]; 446 447static int __kgdb_notify(struct die_args *args, unsigned long cmd) 448{ 449 struct pt_regs *regs = args->regs; 450 451 switch (cmd) { 452 case DIE_NMI: 453 if (atomic_read(&kgdb_active) != -1) { 454 /* KGDB CPU roundup */ 455 kgdb_nmicallback(raw_smp_processor_id(), regs); 456 was_in_debug_nmi[raw_smp_processor_id()] = 1; 457 touch_nmi_watchdog(); 458 return NOTIFY_STOP; 459 } 460 return NOTIFY_DONE; 461 462 case DIE_NMI_IPI: 463 /* Just ignore, we will handle the roundup on DIE_NMI. */ 464 return NOTIFY_DONE; 465 466 case DIE_NMIUNKNOWN: 467 if (was_in_debug_nmi[raw_smp_processor_id()]) { 468 was_in_debug_nmi[raw_smp_processor_id()] = 0; 469 return NOTIFY_STOP; 470 } 471 return NOTIFY_DONE; 472 473 case DIE_NMIWATCHDOG: 474 if (atomic_read(&kgdb_active) != -1) { 475 /* KGDB CPU roundup: */ 476 kgdb_nmicallback(raw_smp_processor_id(), regs); 477 return NOTIFY_STOP; 478 } 479 /* Enter debugger: */ 480 break; 481 482 case DIE_DEBUG: 483 if (atomic_read(&kgdb_cpu_doing_single_step) == 484 raw_smp_processor_id()) { 485 if (user_mode(regs)) 486 return single_step_cont(regs, args); 487 break; 488 } else if (test_thread_flag(TIF_SINGLESTEP)) 489 /* This means a user thread is single stepping 490 * a system call which should be ignored 491 */ 492 return NOTIFY_DONE; 493 /* fall through */ 494 default: 495 if (user_mode(regs)) 496 return NOTIFY_DONE; 497 } 498 499 if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs)) 500 return NOTIFY_DONE; 501 502 /* Must touch watchdog before return to normal operation */ 503 touch_nmi_watchdog(); 504 return NOTIFY_STOP; 505} 506 507static int 508kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr) 509{ 510 unsigned long flags; 511 int ret; 512 513 local_irq_save(flags); 514 ret = __kgdb_notify(ptr, cmd); 515 local_irq_restore(flags); 516 517 return ret; 518} 519 520static struct notifier_block kgdb_notifier = { 521 .notifier_call = kgdb_notify, 522 523 /* 524 * Lowest-prio notifier priority, we want to be notified last: 525 */ 526 .priority = -INT_MAX, 527}; 528 529/** 530 * kgdb_arch_init - Perform any architecture specific initalization. 531 * 532 * This function will handle the initalization of any architecture 533 * specific callbacks. 534 */ 535int kgdb_arch_init(void) 536{ 537 return register_die_notifier(&kgdb_notifier); 538} 539 540/** 541 * kgdb_arch_exit - Perform any architecture specific uninitalization. 542 * 543 * This function will handle the uninitalization of any architecture 544 * specific callbacks, for dynamic registration and unregistration. 545 */ 546void kgdb_arch_exit(void) 547{ 548 unregister_die_notifier(&kgdb_notifier); 549} 550 551/** 552 * 553 * kgdb_skipexception - Bail out of KGDB when we've been triggered. 554 * @exception: Exception vector number 555 * @regs: Current &struct pt_regs. 556 * 557 * On some architectures we need to skip a breakpoint exception when 558 * it occurs after a breakpoint has been removed. 559 * 560 * Skip an int3 exception when it occurs after a breakpoint has been 561 * removed. Backtrack eip by 1 since the int3 would have caused it to 562 * increment by 1. 563 */ 564int kgdb_skipexception(int exception, struct pt_regs *regs) 565{ 566 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) { 567 regs->ip -= 1; 568 return 1; 569 } 570 return 0; 571} 572 573unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs) 574{ 575 if (exception == 3) 576 return instruction_pointer(regs) - 1; 577 return instruction_pointer(regs); 578} 579 580struct kgdb_arch arch_kgdb_ops = { 581 /* Breakpoint instruction: */ 582 .gdb_bpt_instr = { 0xcc }, 583 .flags = KGDB_HW_BREAKPOINT, 584 .set_hw_breakpoint = kgdb_set_hw_break, 585 .remove_hw_breakpoint = kgdb_remove_hw_break, 586 .remove_all_hw_break = kgdb_remove_all_hw_break, 587 .correct_hw_break = kgdb_correct_hw_break, 588}; 589