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/debugreg.h>
47 #include <asm/apicdef.h>
48 #include <asm/system.h>
53 * Put the error code here just in case the user cares:
55 static int gdb_x86errcode
;
58 * Likewise, the vector number here (since GDB only gets the signal
59 * number through the usual means, and that's not very specific):
61 static int gdb_x86vector
= -1;
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.
68 * Convert the pt_regs in @regs into the format for registers that
69 * GDB expects, stored in @gdb_regs.
71 void pt_regs_to_gdb_regs(unsigned long *gdb_regs
, struct pt_regs
*regs
)
74 u32
*gdb_regs32
= (u32
*)gdb_regs
;
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
;
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_FS
] = 0xFFFF;
90 gdb_regs
[GDB_GS
] = 0xFFFF;
91 if (user_mode_vm(regs
)) {
92 gdb_regs
[GDB_SS
] = regs
->ss
;
93 gdb_regs
[GDB_SP
] = regs
->sp
;
95 gdb_regs
[GDB_SS
] = __KERNEL_DS
;
96 gdb_regs
[GDB_SP
] = kernel_stack_pointer(regs
);
99 gdb_regs
[GDB_R8
] = regs
->r8
;
100 gdb_regs
[GDB_R9
] = regs
->r9
;
101 gdb_regs
[GDB_R10
] = regs
->r10
;
102 gdb_regs
[GDB_R11
] = regs
->r11
;
103 gdb_regs
[GDB_R12
] = regs
->r12
;
104 gdb_regs
[GDB_R13
] = regs
->r13
;
105 gdb_regs
[GDB_R14
] = regs
->r14
;
106 gdb_regs
[GDB_R15
] = regs
->r15
;
107 gdb_regs32
[GDB_PS
] = regs
->flags
;
108 gdb_regs32
[GDB_CS
] = regs
->cs
;
109 gdb_regs32
[GDB_SS
] = regs
->ss
;
110 gdb_regs
[GDB_SP
] = kernel_stack_pointer(regs
);
115 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
116 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
117 * @p: The &struct task_struct of the desired process.
119 * Convert the register values of the sleeping process in @p to
120 * the format that GDB expects.
121 * This function is called when kgdb does not have access to the
122 * &struct pt_regs and therefore it should fill the gdb registers
123 * @gdb_regs with what has been saved in &struct thread_struct
124 * thread field during switch_to.
126 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs
, struct task_struct
*p
)
128 #ifndef CONFIG_X86_32
129 u32
*gdb_regs32
= (u32
*)gdb_regs
;
131 gdb_regs
[GDB_AX
] = 0;
132 gdb_regs
[GDB_BX
] = 0;
133 gdb_regs
[GDB_CX
] = 0;
134 gdb_regs
[GDB_DX
] = 0;
135 gdb_regs
[GDB_SI
] = 0;
136 gdb_regs
[GDB_DI
] = 0;
137 gdb_regs
[GDB_BP
] = *(unsigned long *)p
->thread
.sp
;
139 gdb_regs
[GDB_DS
] = __KERNEL_DS
;
140 gdb_regs
[GDB_ES
] = __KERNEL_DS
;
141 gdb_regs
[GDB_PS
] = 0;
142 gdb_regs
[GDB_CS
] = __KERNEL_CS
;
143 gdb_regs
[GDB_PC
] = p
->thread
.ip
;
144 gdb_regs
[GDB_SS
] = __KERNEL_DS
;
145 gdb_regs
[GDB_FS
] = 0xFFFF;
146 gdb_regs
[GDB_GS
] = 0xFFFF;
148 gdb_regs32
[GDB_PS
] = *(unsigned long *)(p
->thread
.sp
+ 8);
149 gdb_regs32
[GDB_CS
] = __KERNEL_CS
;
150 gdb_regs32
[GDB_SS
] = __KERNEL_DS
;
151 gdb_regs
[GDB_PC
] = 0;
152 gdb_regs
[GDB_R8
] = 0;
153 gdb_regs
[GDB_R9
] = 0;
154 gdb_regs
[GDB_R10
] = 0;
155 gdb_regs
[GDB_R11
] = 0;
156 gdb_regs
[GDB_R12
] = 0;
157 gdb_regs
[GDB_R13
] = 0;
158 gdb_regs
[GDB_R14
] = 0;
159 gdb_regs
[GDB_R15
] = 0;
161 gdb_regs
[GDB_SP
] = p
->thread
.sp
;
165 * gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs.
166 * @gdb_regs: A pointer to hold the registers we've received from GDB.
167 * @regs: A pointer to a &struct pt_regs to hold these values in.
169 * Convert the GDB regs in @gdb_regs into the pt_regs, and store them
172 void gdb_regs_to_pt_regs(unsigned long *gdb_regs
, struct pt_regs
*regs
)
174 #ifndef CONFIG_X86_32
175 u32
*gdb_regs32
= (u32
*)gdb_regs
;
177 regs
->ax
= gdb_regs
[GDB_AX
];
178 regs
->bx
= gdb_regs
[GDB_BX
];
179 regs
->cx
= gdb_regs
[GDB_CX
];
180 regs
->dx
= gdb_regs
[GDB_DX
];
181 regs
->si
= gdb_regs
[GDB_SI
];
182 regs
->di
= gdb_regs
[GDB_DI
];
183 regs
->bp
= gdb_regs
[GDB_BP
];
184 regs
->ip
= gdb_regs
[GDB_PC
];
186 regs
->flags
= gdb_regs
[GDB_PS
];
187 regs
->ds
= gdb_regs
[GDB_DS
];
188 regs
->es
= gdb_regs
[GDB_ES
];
189 regs
->cs
= gdb_regs
[GDB_CS
];
191 regs
->r8
= gdb_regs
[GDB_R8
];
192 regs
->r9
= gdb_regs
[GDB_R9
];
193 regs
->r10
= gdb_regs
[GDB_R10
];
194 regs
->r11
= gdb_regs
[GDB_R11
];
195 regs
->r12
= gdb_regs
[GDB_R12
];
196 regs
->r13
= gdb_regs
[GDB_R13
];
197 regs
->r14
= gdb_regs
[GDB_R14
];
198 regs
->r15
= gdb_regs
[GDB_R15
];
199 regs
->flags
= gdb_regs32
[GDB_PS
];
200 regs
->cs
= gdb_regs32
[GDB_CS
];
201 regs
->ss
= gdb_regs32
[GDB_SS
];
205 static struct hw_breakpoint
{
212 static void kgdb_correct_hw_break(void)
219 get_debugreg(dr7
, 7);
220 for (breakno
= 0; breakno
< 4; breakno
++) {
221 breakbit
= 2 << (breakno
<< 1);
222 if (!(dr7
& breakbit
) && breakinfo
[breakno
].enabled
) {
225 dr7
&= ~(0xf0000 << (breakno
<< 2));
226 dr7
|= ((breakinfo
[breakno
].len
<< 2) |
227 breakinfo
[breakno
].type
) <<
228 ((breakno
<< 2) + 16);
229 set_debugreg(breakinfo
[breakno
].addr
, breakno
);
232 if ((dr7
& breakbit
) && !breakinfo
[breakno
].enabled
) {
235 dr7
&= ~(0xf0000 << (breakno
<< 2));
240 set_debugreg(dr7
, 7);
244 kgdb_remove_hw_break(unsigned long addr
, int len
, enum kgdb_bptype bptype
)
248 for (i
= 0; i
< 4; i
++)
249 if (breakinfo
[i
].addr
== addr
&& breakinfo
[i
].enabled
)
254 breakinfo
[i
].enabled
= 0;
259 static void kgdb_remove_all_hw_break(void)
263 for (i
= 0; i
< 4; i
++)
264 memset(&breakinfo
[i
], 0, sizeof(struct hw_breakpoint
));
268 kgdb_set_hw_break(unsigned long addr
, int len
, enum kgdb_bptype bptype
)
273 for (i
= 0; i
< 4; i
++)
274 if (!breakinfo
[i
].enabled
)
280 case BP_HARDWARE_BREAKPOINT
:
284 case BP_WRITE_WATCHPOINT
:
287 case BP_ACCESS_WATCHPOINT
:
294 if (len
== 1 || len
== 2 || len
== 4)
295 breakinfo
[i
].len
= len
- 1;
299 breakinfo
[i
].enabled
= 1;
300 breakinfo
[i
].addr
= addr
;
301 breakinfo
[i
].type
= type
;
307 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
308 * @regs: Current &struct pt_regs.
310 * This function will be called if the particular architecture must
311 * disable hardware debugging while it is processing gdb packets or
312 * handling exception.
314 void kgdb_disable_hw_debug(struct pt_regs
*regs
)
316 /* Disable hardware debugging while we are in kgdb: */
317 set_debugreg(0UL, 7);
321 * kgdb_post_primary_code - Save error vector/code numbers.
322 * @regs: Original pt_regs.
323 * @e_vector: Original error vector.
324 * @err_code: Original error code.
326 * This is needed on architectures which support SMP and KGDB.
327 * This function is called after all the slave cpus have been put
328 * to a know spin state and the primary CPU has control over KGDB.
330 void kgdb_post_primary_code(struct pt_regs
*regs
, int e_vector
, int err_code
)
332 /* primary processor is completely in the debugger */
333 gdb_x86vector
= e_vector
;
334 gdb_x86errcode
= err_code
;
339 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
340 * @flags: Current IRQ state
342 * On SMP systems, we need to get the attention of the other CPUs
343 * and get them be in a known state. This should do what is needed
344 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
345 * the NMI approach is not used for rounding up all the CPUs. For example,
346 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
347 * this case, we have to make sure that interrupts are enabled before
348 * calling smp_call_function(). The argument to this function is
349 * the flags that will be used when restoring the interrupts. There is
350 * local_irq_save() call before kgdb_roundup_cpus().
352 * On non-SMP systems, this is not called.
354 void kgdb_roundup_cpus(unsigned long flags
)
356 apic
->send_IPI_allbutself(APIC_DM_NMI
);
361 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
362 * @vector: The error vector of the exception that happened.
363 * @signo: The signal number of the exception that happened.
364 * @err_code: The error code of the exception that happened.
365 * @remcom_in_buffer: The buffer of the packet we have read.
366 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
367 * @regs: The &struct pt_regs of the current process.
369 * This function MUST handle the 'c' and 's' command packets,
370 * as well packets to set / remove a hardware breakpoint, if used.
371 * If there are additional packets which the hardware needs to handle,
372 * they are handled here. The code should return -1 if it wants to
373 * process more packets, and a %0 or %1 if it wants to exit from the
376 int kgdb_arch_handle_exception(int e_vector
, int signo
, int err_code
,
377 char *remcomInBuffer
, char *remcomOutBuffer
,
378 struct pt_regs
*linux_regs
)
385 switch (remcomInBuffer
[0]) {
388 /* try to read optional parameter, pc unchanged if no parm */
389 ptr
= &remcomInBuffer
[1];
390 if (kgdb_hex2long(&ptr
, &addr
))
391 linux_regs
->ip
= addr
;
394 newPC
= linux_regs
->ip
;
396 /* clear the trace bit */
397 linux_regs
->flags
&= ~X86_EFLAGS_TF
;
398 atomic_set(&kgdb_cpu_doing_single_step
, -1);
400 /* set the trace bit if we're stepping */
401 if (remcomInBuffer
[0] == 's') {
402 linux_regs
->flags
|= X86_EFLAGS_TF
;
403 kgdb_single_step
= 1;
404 atomic_set(&kgdb_cpu_doing_single_step
,
405 raw_smp_processor_id());
408 get_debugreg(dr6
, 6);
409 if (!(dr6
& 0x4000)) {
412 for (breakno
= 0; breakno
< 4; breakno
++) {
413 if (dr6
& (1 << breakno
) &&
414 breakinfo
[breakno
].type
== 0) {
415 /* Set restore flag: */
416 linux_regs
->flags
|= X86_EFLAGS_RF
;
421 set_debugreg(0UL, 6);
422 kgdb_correct_hw_break();
427 /* this means that we do not want to exit from the handler: */
432 single_step_cont(struct pt_regs
*regs
, struct die_args
*args
)
435 * Single step exception from kernel space to user space so
436 * eat the exception and continue the process:
438 printk(KERN_ERR
"KGDB: trap/step from kernel to user space, "
440 kgdb_arch_handle_exception(args
->trapnr
, args
->signr
,
441 args
->err
, "c", "", regs
);
443 * Reset the BS bit in dr6 (pointed by args->err) to
444 * denote completion of processing
446 (*(unsigned long *)ERR_PTR(args
->err
)) &= ~DR_STEP
;
451 static int was_in_debug_nmi
[NR_CPUS
];
453 static int __kgdb_notify(struct die_args
*args
, unsigned long cmd
)
455 struct pt_regs
*regs
= args
->regs
;
459 if (atomic_read(&kgdb_active
) != -1) {
460 /* KGDB CPU roundup */
461 kgdb_nmicallback(raw_smp_processor_id(), regs
);
462 was_in_debug_nmi
[raw_smp_processor_id()] = 1;
463 touch_nmi_watchdog();
469 /* Just ignore, we will handle the roundup on DIE_NMI. */
473 if (was_in_debug_nmi
[raw_smp_processor_id()]) {
474 was_in_debug_nmi
[raw_smp_processor_id()] = 0;
479 case DIE_NMIWATCHDOG
:
480 if (atomic_read(&kgdb_active
) != -1) {
481 /* KGDB CPU roundup: */
482 kgdb_nmicallback(raw_smp_processor_id(), regs
);
485 /* Enter debugger: */
489 if (atomic_read(&kgdb_cpu_doing_single_step
) ==
490 raw_smp_processor_id()) {
492 return single_step_cont(regs
, args
);
494 } else if (test_thread_flag(TIF_SINGLESTEP
))
495 /* This means a user thread is single stepping
496 * a system call which should be ignored
505 if (kgdb_handle_exception(args
->trapnr
, args
->signr
, args
->err
, regs
))
508 /* Must touch watchdog before return to normal operation */
509 touch_nmi_watchdog();
514 kgdb_notify(struct notifier_block
*self
, unsigned long cmd
, void *ptr
)
519 local_irq_save(flags
);
520 ret
= __kgdb_notify(ptr
, cmd
);
521 local_irq_restore(flags
);
526 static struct notifier_block kgdb_notifier
= {
527 .notifier_call
= kgdb_notify
,
530 * Lowest-prio notifier priority, we want to be notified last:
532 .priority
= -INT_MAX
,
536 * kgdb_arch_init - Perform any architecture specific initalization.
538 * This function will handle the initalization of any architecture
539 * specific callbacks.
541 int kgdb_arch_init(void)
543 return register_die_notifier(&kgdb_notifier
);
547 * kgdb_arch_exit - Perform any architecture specific uninitalization.
549 * This function will handle the uninitalization of any architecture
550 * specific callbacks, for dynamic registration and unregistration.
552 void kgdb_arch_exit(void)
554 unregister_die_notifier(&kgdb_notifier
);
559 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
560 * @exception: Exception vector number
561 * @regs: Current &struct pt_regs.
563 * On some architectures we need to skip a breakpoint exception when
564 * it occurs after a breakpoint has been removed.
566 * Skip an int3 exception when it occurs after a breakpoint has been
567 * removed. Backtrack eip by 1 since the int3 would have caused it to
570 int kgdb_skipexception(int exception
, struct pt_regs
*regs
)
572 if (exception
== 3 && kgdb_isremovedbreak(regs
->ip
- 1)) {
579 unsigned long kgdb_arch_pc(int exception
, struct pt_regs
*regs
)
582 return instruction_pointer(regs
) - 1;
583 return instruction_pointer(regs
);
586 struct kgdb_arch arch_kgdb_ops
= {
587 /* Breakpoint instruction: */
588 .gdb_bpt_instr
= { 0xcc },
589 .flags
= KGDB_HW_BREAKPOINT
,
590 .set_hw_breakpoint
= kgdb_set_hw_break
,
591 .remove_hw_breakpoint
= kgdb_remove_hw_break
,
592 .remove_all_hw_break
= kgdb_remove_all_hw_break
,
593 .correct_hw_break
= kgdb_correct_hw_break
,