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
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.
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.
22 /****************************************************************************
23 * Contributor: Lake Stevens Instrument Division$
24 * Written by: Glenn Engel $
28 * Modified for 386 by Jim Kingdon, Cygnus Support.
29 * Origianl kgdb, compatibility with 2.1.xx kernel by
32 * X86_64 changes from Andi Kleen's patch merged by Jim Houston
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/smp.h>
43 #include <linux/nmi.h>
44 #include <linux/hw_breakpoint.h>
45 #include <linux/uaccess.h>
46 #include <linux/memory.h>
48 #include <asm/debugreg.h>
49 #include <asm/apicdef.h>
53 struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
56 { "ax", 4, offsetof(struct pt_regs, ax) },
57 { "cx", 4, offsetof(struct pt_regs, cx) },
58 { "dx", 4, offsetof(struct pt_regs, dx) },
59 { "bx", 4, offsetof(struct pt_regs, bx) },
60 { "sp", 4, offsetof(struct pt_regs, sp) },
61 { "bp", 4, offsetof(struct pt_regs, bp) },
62 { "si", 4, offsetof(struct pt_regs, si) },
63 { "di", 4, offsetof(struct pt_regs, di) },
64 { "ip", 4, offsetof(struct pt_regs, ip) },
65 { "flags", 4, offsetof(struct pt_regs, flags) },
66 { "cs", 4, offsetof(struct pt_regs, cs) },
67 { "ss", 4, offsetof(struct pt_regs, ss) },
68 { "ds", 4, offsetof(struct pt_regs, ds) },
69 { "es", 4, offsetof(struct pt_regs, es) },
71 { "ax", 8, offsetof(struct pt_regs, ax) },
72 { "bx", 8, offsetof(struct pt_regs, bx) },
73 { "cx", 8, offsetof(struct pt_regs, cx) },
74 { "dx", 8, offsetof(struct pt_regs, dx) },
75 { "si", 8, offsetof(struct pt_regs, si) },
76 { "di", 8, offsetof(struct pt_regs, di) },
77 { "bp", 8, offsetof(struct pt_regs, bp) },
78 { "sp", 8, offsetof(struct pt_regs, sp) },
79 { "r8", 8, offsetof(struct pt_regs, r8) },
80 { "r9", 8, offsetof(struct pt_regs, r9) },
81 { "r10", 8, offsetof(struct pt_regs, r10) },
82 { "r11", 8, offsetof(struct pt_regs, r11) },
83 { "r12", 8, offsetof(struct pt_regs, r12) },
84 { "r13", 8, offsetof(struct pt_regs, r13) },
85 { "r14", 8, offsetof(struct pt_regs, r14) },
86 { "r15", 8, offsetof(struct pt_regs, r15) },
87 { "ip", 8, offsetof(struct pt_regs, ip) },
88 { "flags", 4, offsetof(struct pt_regs, flags) },
89 { "cs", 4, offsetof(struct pt_regs, cs) },
90 { "ss", 4, offsetof(struct pt_regs, ss) },
98 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
102 regno == GDB_SS || regno == GDB_FS || regno == GDB_GS ||
104 regno == GDB_SP || regno == GDB_ORIG_AX)
107 if (dbg_reg_def[regno].offset != -1)
108 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
109 dbg_reg_def[regno].size);
113 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
115 if (regno == GDB_ORIG_AX) {
116 memcpy(mem, ®s->orig_ax, sizeof(regs->orig_ax));
119 if (regno >= DBG_MAX_REG_NUM || regno < 0)
122 if (dbg_reg_def[regno].offset != -1)
123 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
124 dbg_reg_def[regno].size);
129 if (!user_mode(regs))
130 *(unsigned long *)mem = __KERNEL_DS;
133 if (!user_mode(regs))
134 *(unsigned long *)mem = kernel_stack_pointer(regs);
138 *(unsigned long *)mem = 0xFFFF;
142 return dbg_reg_def[regno].name;
146 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
147 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
148 * @p: The &struct task_struct of the desired process.
150 * Convert the register values of the sleeping process in @p to
151 * the format that GDB expects.
152 * This function is called when kgdb does not have access to the
153 * &struct pt_regs and therefore it should fill the gdb registers
154 * @gdb_regs with what has been saved in &struct thread_struct
155 * thread field during switch_to.
157 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
159 #ifndef CONFIG_X86_32
160 u32 *gdb_regs32 = (u32 *)gdb_regs;
162 gdb_regs[GDB_AX] = 0;
163 gdb_regs[GDB_BX] = 0;
164 gdb_regs[GDB_CX] = 0;
165 gdb_regs[GDB_DX] = 0;
166 gdb_regs[GDB_SI] = 0;
167 gdb_regs[GDB_DI] = 0;
168 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
170 gdb_regs[GDB_DS] = __KERNEL_DS;
171 gdb_regs[GDB_ES] = __KERNEL_DS;
172 gdb_regs[GDB_PS] = 0;
173 gdb_regs[GDB_CS] = __KERNEL_CS;
174 gdb_regs[GDB_PC] = p->thread.ip;
175 gdb_regs[GDB_SS] = __KERNEL_DS;
176 gdb_regs[GDB_FS] = 0xFFFF;
177 gdb_regs[GDB_GS] = 0xFFFF;
179 gdb_regs32[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
180 gdb_regs32[GDB_CS] = __KERNEL_CS;
181 gdb_regs32[GDB_SS] = __KERNEL_DS;
182 gdb_regs[GDB_PC] = 0;
183 gdb_regs[GDB_R8] = 0;
184 gdb_regs[GDB_R9] = 0;
185 gdb_regs[GDB_R10] = 0;
186 gdb_regs[GDB_R11] = 0;
187 gdb_regs[GDB_R12] = 0;
188 gdb_regs[GDB_R13] = 0;
189 gdb_regs[GDB_R14] = 0;
190 gdb_regs[GDB_R15] = 0;
192 gdb_regs[GDB_SP] = p->thread.sp;
195 static struct hw_breakpoint {
200 struct perf_event * __percpu *pev;
201 } breakinfo[HBP_NUM];
203 static unsigned long early_dr7;
205 static void kgdb_correct_hw_break(void)
209 for (breakno = 0; breakno < HBP_NUM; breakno++) {
210 struct perf_event *bp;
211 struct arch_hw_breakpoint *info;
213 int cpu = raw_smp_processor_id();
214 if (!breakinfo[breakno].enabled)
217 set_debugreg(breakinfo[breakno].addr, breakno);
218 early_dr7 |= encode_dr7(breakno,
219 breakinfo[breakno].len,
220 breakinfo[breakno].type);
221 set_debugreg(early_dr7, 7);
224 bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
225 info = counter_arch_bp(bp);
226 if (bp->attr.disabled != 1)
228 bp->attr.bp_addr = breakinfo[breakno].addr;
229 bp->attr.bp_len = breakinfo[breakno].len;
230 bp->attr.bp_type = breakinfo[breakno].type;
231 info->address = breakinfo[breakno].addr;
232 info->len = breakinfo[breakno].len;
233 info->type = breakinfo[breakno].type;
234 val = arch_install_hw_breakpoint(bp);
236 bp->attr.disabled = 0;
239 hw_breakpoint_restore();
242 static int hw_break_reserve_slot(int breakno)
246 struct perf_event **pevent;
251 for_each_online_cpu(cpu) {
253 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
254 if (dbg_reserve_bp_slot(*pevent))
261 for_each_online_cpu(cpu) {
265 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
266 dbg_release_bp_slot(*pevent);
271 static int hw_break_release_slot(int breakno)
273 struct perf_event **pevent;
279 for_each_online_cpu(cpu) {
280 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
281 if (dbg_release_bp_slot(*pevent))
283 * The debugger is responsible for handing the retry on
292 kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
296 for (i = 0; i < HBP_NUM; i++)
297 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
302 if (hw_break_release_slot(i)) {
303 printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
306 breakinfo[i].enabled = 0;
311 static void kgdb_remove_all_hw_break(void)
314 int cpu = raw_smp_processor_id();
315 struct perf_event *bp;
317 for (i = 0; i < HBP_NUM; i++) {
318 if (!breakinfo[i].enabled)
320 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
321 if (!bp->attr.disabled) {
322 arch_uninstall_hw_breakpoint(bp);
323 bp->attr.disabled = 1;
327 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
329 else if (hw_break_release_slot(i))
330 printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n",
332 breakinfo[i].enabled = 0;
337 kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
341 for (i = 0; i < HBP_NUM; i++)
342 if (!breakinfo[i].enabled)
348 case BP_HARDWARE_BREAKPOINT:
350 breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
352 case BP_WRITE_WATCHPOINT:
353 breakinfo[i].type = X86_BREAKPOINT_WRITE;
355 case BP_ACCESS_WATCHPOINT:
356 breakinfo[i].type = X86_BREAKPOINT_RW;
363 breakinfo[i].len = X86_BREAKPOINT_LEN_1;
366 breakinfo[i].len = X86_BREAKPOINT_LEN_2;
369 breakinfo[i].len = X86_BREAKPOINT_LEN_4;
373 breakinfo[i].len = X86_BREAKPOINT_LEN_8;
379 breakinfo[i].addr = addr;
380 if (hw_break_reserve_slot(i)) {
381 breakinfo[i].addr = 0;
384 breakinfo[i].enabled = 1;
390 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
391 * @regs: Current &struct pt_regs.
393 * This function will be called if the particular architecture must
394 * disable hardware debugging while it is processing gdb packets or
395 * handling exception.
397 static void kgdb_disable_hw_debug(struct pt_regs *regs)
400 int cpu = raw_smp_processor_id();
401 struct perf_event *bp;
403 /* Disable hardware debugging while we are in kgdb: */
404 set_debugreg(0UL, 7);
405 for (i = 0; i < HBP_NUM; i++) {
406 if (!breakinfo[i].enabled)
409 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
413 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
414 if (bp->attr.disabled == 1)
416 arch_uninstall_hw_breakpoint(bp);
417 bp->attr.disabled = 1;
423 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
424 * @flags: Current IRQ state
426 * On SMP systems, we need to get the attention of the other CPUs
427 * and get them be in a known state. This should do what is needed
428 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
429 * the NMI approach is not used for rounding up all the CPUs. For example,
430 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
431 * this case, we have to make sure that interrupts are enabled before
432 * calling smp_call_function(). The argument to this function is
433 * the flags that will be used when restoring the interrupts. There is
434 * local_irq_save() call before kgdb_roundup_cpus().
436 * On non-SMP systems, this is not called.
438 void kgdb_roundup_cpus(unsigned long flags)
440 apic->send_IPI_allbutself(APIC_DM_NMI);
445 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
446 * @e_vector: The error vector of the exception that happened.
447 * @signo: The signal number of the exception that happened.
448 * @err_code: The error code of the exception that happened.
449 * @remcomInBuffer: The buffer of the packet we have read.
450 * @remcomOutBuffer: The buffer of %BUFMAX bytes to write a packet into.
451 * @linux_regs: The &struct pt_regs of the current process.
453 * This function MUST handle the 'c' and 's' command packets,
454 * as well packets to set / remove a hardware breakpoint, if used.
455 * If there are additional packets which the hardware needs to handle,
456 * they are handled here. The code should return -1 if it wants to
457 * process more packets, and a %0 or %1 if it wants to exit from the
460 int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
461 char *remcomInBuffer, char *remcomOutBuffer,
462 struct pt_regs *linux_regs)
467 switch (remcomInBuffer[0]) {
470 /* try to read optional parameter, pc unchanged if no parm */
471 ptr = &remcomInBuffer[1];
472 if (kgdb_hex2long(&ptr, &addr))
473 linux_regs->ip = addr;
476 /* clear the trace bit */
477 linux_regs->flags &= ~X86_EFLAGS_TF;
478 atomic_set(&kgdb_cpu_doing_single_step, -1);
480 /* set the trace bit if we're stepping */
481 if (remcomInBuffer[0] == 's') {
482 linux_regs->flags |= X86_EFLAGS_TF;
483 atomic_set(&kgdb_cpu_doing_single_step,
484 raw_smp_processor_id());
490 /* this means that we do not want to exit from the handler: */
495 single_step_cont(struct pt_regs *regs, struct die_args *args)
498 * Single step exception from kernel space to user space so
499 * eat the exception and continue the process:
501 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
503 kgdb_arch_handle_exception(args->trapnr, args->signr,
504 args->err, "c", "", regs);
506 * Reset the BS bit in dr6 (pointed by args->err) to
507 * denote completion of processing
509 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
514 static int was_in_debug_nmi[NR_CPUS];
516 static int kgdb_nmi_handler(unsigned int cmd, struct pt_regs *regs)
520 if (atomic_read(&kgdb_active) != -1) {
521 /* KGDB CPU roundup */
522 kgdb_nmicallback(raw_smp_processor_id(), regs);
523 was_in_debug_nmi[raw_smp_processor_id()] = 1;
524 touch_nmi_watchdog();
530 if (was_in_debug_nmi[raw_smp_processor_id()]) {
531 was_in_debug_nmi[raw_smp_processor_id()] = 0;
542 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
544 struct pt_regs *regs = args->regs;
548 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
550 return single_step_cont(regs, args);
552 } else if (test_thread_flag(TIF_SINGLESTEP))
553 /* This means a user thread is single stepping
554 * a system call which should be ignored
563 if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
566 /* Must touch watchdog before return to normal operation */
567 touch_nmi_watchdog();
571 int kgdb_ll_trap(int cmd, const char *str,
572 struct pt_regs *regs, long err, int trap, int sig)
574 struct die_args args = {
583 if (!kgdb_io_module_registered)
586 return __kgdb_notify(&args, cmd);
590 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
595 local_irq_save(flags);
596 ret = __kgdb_notify(ptr, cmd);
597 local_irq_restore(flags);
602 static struct notifier_block kgdb_notifier = {
603 .notifier_call = kgdb_notify,
607 * kgdb_arch_init - Perform any architecture specific initalization.
609 * This function will handle the initalization of any architecture
610 * specific callbacks.
612 int kgdb_arch_init(void)
616 retval = register_die_notifier(&kgdb_notifier);
620 retval = register_nmi_handler(NMI_LOCAL, kgdb_nmi_handler,
625 retval = register_nmi_handler(NMI_UNKNOWN, kgdb_nmi_handler,
634 unregister_nmi_handler(NMI_LOCAL, "kgdb");
636 unregister_die_notifier(&kgdb_notifier);
641 static void kgdb_hw_overflow_handler(struct perf_event *event,
642 struct perf_sample_data *data, struct pt_regs *regs)
644 struct task_struct *tsk = current;
647 for (i = 0; i < 4; i++)
648 if (breakinfo[i].enabled)
649 tsk->thread.debugreg6 |= (DR_TRAP0 << i);
652 void kgdb_arch_late(void)
655 struct perf_event_attr attr;
656 struct perf_event **pevent;
659 * Pre-allocate the hw breakpoint structions in the non-atomic
660 * portion of kgdb because this operation requires mutexs to
663 hw_breakpoint_init(&attr);
664 attr.bp_addr = (unsigned long)kgdb_arch_init;
665 attr.bp_len = HW_BREAKPOINT_LEN_1;
666 attr.bp_type = HW_BREAKPOINT_W;
668 for (i = 0; i < HBP_NUM; i++) {
669 if (breakinfo[i].pev)
671 breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
672 if (IS_ERR((void * __force)breakinfo[i].pev)) {
673 printk(KERN_ERR "kgdb: Could not allocate hw"
674 "breakpoints\nDisabling the kernel debugger\n");
675 breakinfo[i].pev = NULL;
679 for_each_online_cpu(cpu) {
680 pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
681 pevent[0]->hw.sample_period = 1;
682 pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
683 if (pevent[0]->destroy != NULL) {
684 pevent[0]->destroy = NULL;
685 release_bp_slot(*pevent);
692 * kgdb_arch_exit - Perform any architecture specific uninitalization.
694 * This function will handle the uninitalization of any architecture
695 * specific callbacks, for dynamic registration and unregistration.
697 void kgdb_arch_exit(void)
700 for (i = 0; i < 4; i++) {
701 if (breakinfo[i].pev) {
702 unregister_wide_hw_breakpoint(breakinfo[i].pev);
703 breakinfo[i].pev = NULL;
706 unregister_nmi_handler(NMI_UNKNOWN, "kgdb");
707 unregister_nmi_handler(NMI_LOCAL, "kgdb");
708 unregister_die_notifier(&kgdb_notifier);
713 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
714 * @exception: Exception vector number
715 * @regs: Current &struct pt_regs.
717 * On some architectures we need to skip a breakpoint exception when
718 * it occurs after a breakpoint has been removed.
720 * Skip an int3 exception when it occurs after a breakpoint has been
721 * removed. Backtrack eip by 1 since the int3 would have caused it to
724 int kgdb_skipexception(int exception, struct pt_regs *regs)
726 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
733 unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
736 return instruction_pointer(regs) - 1;
737 return instruction_pointer(regs);
740 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
745 int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
748 #ifdef CONFIG_DEBUG_RODATA
749 char opc[BREAK_INSTR_SIZE];
750 #endif /* CONFIG_DEBUG_RODATA */
752 bpt->type = BP_BREAKPOINT;
753 err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
757 err = probe_kernel_write((char *)bpt->bpt_addr,
758 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
759 #ifdef CONFIG_DEBUG_RODATA
763 * It is safe to call text_poke() because normal kernel execution
764 * is stopped on all cores, so long as the text_mutex is not locked.
766 if (mutex_is_locked(&text_mutex))
768 text_poke((void *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr,
770 err = probe_kernel_read(opc, (char *)bpt->bpt_addr, BREAK_INSTR_SIZE);
773 if (memcmp(opc, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE))
775 bpt->type = BP_POKE_BREAKPOINT;
776 #endif /* CONFIG_DEBUG_RODATA */
780 int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
782 #ifdef CONFIG_DEBUG_RODATA
784 char opc[BREAK_INSTR_SIZE];
786 if (bpt->type != BP_POKE_BREAKPOINT)
789 * It is safe to call text_poke() because normal kernel execution
790 * is stopped on all cores, so long as the text_mutex is not locked.
792 if (mutex_is_locked(&text_mutex))
794 text_poke((void *)bpt->bpt_addr, bpt->saved_instr, BREAK_INSTR_SIZE);
795 err = probe_kernel_read(opc, (char *)bpt->bpt_addr, BREAK_INSTR_SIZE);
796 if (err || memcmp(opc, bpt->saved_instr, BREAK_INSTR_SIZE))
800 #endif /* CONFIG_DEBUG_RODATA */
801 return probe_kernel_write((char *)bpt->bpt_addr,
802 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
805 struct kgdb_arch arch_kgdb_ops = {
806 /* Breakpoint instruction: */
807 .gdb_bpt_instr = { 0xcc },
808 .flags = KGDB_HW_BREAKPOINT,
809 .set_hw_breakpoint = kgdb_set_hw_break,
810 .remove_hw_breakpoint = kgdb_remove_hw_break,
811 .disable_hw_break = kgdb_disable_hw_debug,
812 .remove_all_hw_break = kgdb_remove_all_hw_break,
813 .correct_hw_break = kgdb_correct_hw_break,