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.
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.
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
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>
46 #include <asm/apicdef.h>
47 #include <asm/system.h>
52 * Put the error code here just in case the user cares:
54 static int gdb_x86errcode;
57 * Likewise, the vector number here (since GDB only gets the signal
58 * number through the usual means, and that's not very specific):
60 static int gdb_x86vector = -1;
63 * pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs
64 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
65 * @regs: The &struct pt_regs of the current process.
67 * Convert the pt_regs in @regs into the format for registers that
68 * GDB expects, stored in @gdb_regs.
70 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
73 u32 *gdb_regs32 = (u32 *)gdb_regs;
75 gdb_regs[GDB_AX] = regs->ax;
76 gdb_regs[GDB_BX] = regs->bx;
77 gdb_regs[GDB_CX] = regs->cx;
78 gdb_regs[GDB_DX] = regs->dx;
79 gdb_regs[GDB_SI] = regs->si;
80 gdb_regs[GDB_DI] = regs->di;
81 gdb_regs[GDB_BP] = regs->bp;
82 gdb_regs[GDB_PC] = regs->ip;
84 gdb_regs[GDB_PS] = regs->flags;
85 gdb_regs[GDB_DS] = regs->ds;
86 gdb_regs[GDB_ES] = regs->es;
87 gdb_regs[GDB_CS] = regs->cs;
88 gdb_regs[GDB_SS] = __KERNEL_DS;
89 gdb_regs[GDB_FS] = 0xFFFF;
90 gdb_regs[GDB_GS] = 0xFFFF;
91 gdb_regs[GDB_SP] = (int)®s->sp;
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 gdb_regs[GDB_SP] = regs->sp;
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.
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.
120 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
122 #ifndef CONFIG_X86_32
123 u32 *gdb_regs32 = (u32 *)gdb_regs;
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;
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;
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] = p->thread.ip;
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;
155 gdb_regs[GDB_SP] = p->thread.sp;
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.
163 * Convert the GDB regs in @gdb_regs into the pt_regs, and store them
166 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
168 #ifndef CONFIG_X86_32
169 u32 *gdb_regs32 = (u32 *)gdb_regs;
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];
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];
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];
199 static struct hw_breakpoint {
206 static void kgdb_correct_hw_break(void)
213 get_debugreg(dr7, 7);
214 for (breakno = 0; breakno < 4; breakno++) {
215 breakbit = 2 << (breakno << 1);
216 if (!(dr7 & breakbit) && breakinfo[breakno].enabled) {
219 dr7 &= ~(0xf0000 << (breakno << 2));
220 dr7 |= ((breakinfo[breakno].len << 2) |
221 breakinfo[breakno].type) <<
222 ((breakno << 2) + 16);
223 if (breakno >= 0 && breakno <= 3)
224 set_debugreg(breakinfo[breakno].addr, breakno);
227 if ((dr7 & breakbit) && !breakinfo[breakno].enabled) {
230 dr7 &= ~(0xf0000 << (breakno << 2));
235 set_debugreg(dr7, 7);
239 kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
243 for (i = 0; i < 4; i++)
244 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
249 breakinfo[i].enabled = 0;
254 static void kgdb_remove_all_hw_break(void)
258 for (i = 0; i < 4; i++)
259 memset(&breakinfo[i], 0, sizeof(struct hw_breakpoint));
263 kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
268 for (i = 0; i < 4; i++)
269 if (!breakinfo[i].enabled)
275 case BP_HARDWARE_BREAKPOINT:
279 case BP_WRITE_WATCHPOINT:
282 case BP_ACCESS_WATCHPOINT:
289 if (len == 1 || len == 2 || len == 4)
290 breakinfo[i].len = len - 1;
294 breakinfo[i].enabled = 1;
295 breakinfo[i].addr = addr;
296 breakinfo[i].type = type;
302 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
303 * @regs: Current &struct pt_regs.
305 * This function will be called if the particular architecture must
306 * disable hardware debugging while it is processing gdb packets or
307 * handling exception.
309 void kgdb_disable_hw_debug(struct pt_regs *regs)
311 /* Disable hardware debugging while we are in kgdb: */
312 set_debugreg(0UL, 7);
316 * kgdb_post_primary_code - Save error vector/code numbers.
317 * @regs: Original pt_regs.
318 * @e_vector: Original error vector.
319 * @err_code: Original error code.
321 * This is needed on architectures which support SMP and KGDB.
322 * This function is called after all the slave cpus have been put
323 * to a know spin state and the primary CPU has control over KGDB.
325 void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
327 /* primary processor is completely in the debugger */
328 gdb_x86vector = e_vector;
329 gdb_x86errcode = err_code;
334 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
335 * @flags: Current IRQ state
337 * On SMP systems, we need to get the attention of the other CPUs
338 * and get them be in a known state. This should do what is needed
339 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
340 * the NMI approach is not used for rounding up all the CPUs. For example,
341 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
342 * this case, we have to make sure that interrupts are enabled before
343 * calling smp_call_function(). The argument to this function is
344 * the flags that will be used when restoring the interrupts. There is
345 * local_irq_save() call before kgdb_roundup_cpus().
347 * On non-SMP systems, this is not called.
349 void kgdb_roundup_cpus(unsigned long flags)
351 apic->send_IPI_allbutself(APIC_DM_NMI);
356 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
357 * @vector: The error vector of the exception that happened.
358 * @signo: The signal number of the exception that happened.
359 * @err_code: The error code of the exception that happened.
360 * @remcom_in_buffer: The buffer of the packet we have read.
361 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
362 * @regs: The &struct pt_regs of the current process.
364 * This function MUST handle the 'c' and 's' command packets,
365 * as well packets to set / remove a hardware breakpoint, if used.
366 * If there are additional packets which the hardware needs to handle,
367 * they are handled here. The code should return -1 if it wants to
368 * process more packets, and a %0 or %1 if it wants to exit from the
371 int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
372 char *remcomInBuffer, char *remcomOutBuffer,
373 struct pt_regs *linux_regs)
380 switch (remcomInBuffer[0]) {
383 /* try to read optional parameter, pc unchanged if no parm */
384 ptr = &remcomInBuffer[1];
385 if (kgdb_hex2long(&ptr, &addr))
386 linux_regs->ip = addr;
389 newPC = linux_regs->ip;
391 /* clear the trace bit */
392 linux_regs->flags &= ~X86_EFLAGS_TF;
393 atomic_set(&kgdb_cpu_doing_single_step, -1);
395 /* set the trace bit if we're stepping */
396 if (remcomInBuffer[0] == 's') {
397 linux_regs->flags |= X86_EFLAGS_TF;
398 kgdb_single_step = 1;
399 atomic_set(&kgdb_cpu_doing_single_step,
400 raw_smp_processor_id());
403 get_debugreg(dr6, 6);
404 if (!(dr6 & 0x4000)) {
407 for (breakno = 0; breakno < 4; breakno++) {
408 if (dr6 & (1 << breakno) &&
409 breakinfo[breakno].type == 0) {
410 /* Set restore flag: */
411 linux_regs->flags |= X86_EFLAGS_RF;
416 set_debugreg(0UL, 6);
417 kgdb_correct_hw_break();
422 /* this means that we do not want to exit from the handler: */
427 single_step_cont(struct pt_regs *regs, struct die_args *args)
430 * Single step exception from kernel space to user space so
431 * eat the exception and continue the process:
433 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
435 kgdb_arch_handle_exception(args->trapnr, args->signr,
436 args->err, "c", "", regs);
441 static int was_in_debug_nmi[NR_CPUS];
443 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
445 struct pt_regs *regs = args->regs;
449 if (atomic_read(&kgdb_active) != -1) {
450 /* KGDB CPU roundup */
451 kgdb_nmicallback(raw_smp_processor_id(), regs);
452 was_in_debug_nmi[raw_smp_processor_id()] = 1;
453 touch_nmi_watchdog();
459 /* Just ignore, we will handle the roundup on DIE_NMI. */
463 if (was_in_debug_nmi[raw_smp_processor_id()]) {
464 was_in_debug_nmi[raw_smp_processor_id()] = 0;
469 case DIE_NMIWATCHDOG:
470 if (atomic_read(&kgdb_active) != -1) {
471 /* KGDB CPU roundup: */
472 kgdb_nmicallback(raw_smp_processor_id(), regs);
475 /* Enter debugger: */
479 if (atomic_read(&kgdb_cpu_doing_single_step) ==
480 raw_smp_processor_id()) {
482 return single_step_cont(regs, args);
484 } else if (test_thread_flag(TIF_SINGLESTEP))
485 /* This means a user thread is single stepping
486 * a system call which should be ignored
495 if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs))
498 /* Must touch watchdog before return to normal operation */
499 touch_nmi_watchdog();
504 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
509 local_irq_save(flags);
510 ret = __kgdb_notify(ptr, cmd);
511 local_irq_restore(flags);
516 static struct notifier_block kgdb_notifier = {
517 .notifier_call = kgdb_notify,
520 * Lowest-prio notifier priority, we want to be notified last:
522 .priority = -INT_MAX,
526 * kgdb_arch_init - Perform any architecture specific initalization.
528 * This function will handle the initalization of any architecture
529 * specific callbacks.
531 int kgdb_arch_init(void)
533 return register_die_notifier(&kgdb_notifier);
537 * kgdb_arch_exit - Perform any architecture specific uninitalization.
539 * This function will handle the uninitalization of any architecture
540 * specific callbacks, for dynamic registration and unregistration.
542 void kgdb_arch_exit(void)
544 unregister_die_notifier(&kgdb_notifier);
549 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
550 * @exception: Exception vector number
551 * @regs: Current &struct pt_regs.
553 * On some architectures we need to skip a breakpoint exception when
554 * it occurs after a breakpoint has been removed.
556 * Skip an int3 exception when it occurs after a breakpoint has been
557 * removed. Backtrack eip by 1 since the int3 would have caused it to
560 int kgdb_skipexception(int exception, struct pt_regs *regs)
562 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
569 unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
572 return instruction_pointer(regs) - 1;
573 return instruction_pointer(regs);
576 struct kgdb_arch arch_kgdb_ops = {
577 /* Breakpoint instruction: */
578 .gdb_bpt_instr = { 0xcc },
579 .flags = KGDB_HW_BREAKPOINT,
580 .set_hw_breakpoint = kgdb_set_hw_break,
581 .remove_hw_breakpoint = kgdb_remove_hw_break,
582 .remove_all_hw_break = kgdb_remove_all_hw_break,
583 .correct_hw_break = kgdb_correct_hw_break,