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>
50 # include <mach_ipi.h>
52 # include <asm/mach_apic.h>
56 * Put the error code here just in case the user cares:
58 static int gdb_x86errcode;
61 * Likewise, the vector number here (since GDB only gets the signal
62 * number through the usual means, and that's not very specific):
64 static int gdb_x86vector = -1;
67 * pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs
68 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
69 * @regs: The &struct pt_regs of the current process.
71 * Convert the pt_regs in @regs into the format for registers that
72 * GDB expects, stored in @gdb_regs.
74 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *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_PS] = regs->flags;
84 gdb_regs[GDB_PC] = regs->ip;
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;
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;
102 gdb_regs[GDB_SP] = regs->sp;
106 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
107 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
108 * @p: The &struct task_struct of the desired process.
110 * Convert the register values of the sleeping process in @p to
111 * the format that GDB expects.
112 * This function is called when kgdb does not have access to the
113 * &struct pt_regs and therefore it should fill the gdb registers
114 * @gdb_regs with what has been saved in &struct thread_struct
115 * thread field during switch_to.
117 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
119 gdb_regs[GDB_AX] = 0;
120 gdb_regs[GDB_BX] = 0;
121 gdb_regs[GDB_CX] = 0;
122 gdb_regs[GDB_DX] = 0;
123 gdb_regs[GDB_SI] = 0;
124 gdb_regs[GDB_DI] = 0;
125 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
127 gdb_regs[GDB_DS] = __KERNEL_DS;
128 gdb_regs[GDB_ES] = __KERNEL_DS;
129 gdb_regs[GDB_PS] = 0;
130 gdb_regs[GDB_CS] = __KERNEL_CS;
131 gdb_regs[GDB_PC] = p->thread.ip;
132 gdb_regs[GDB_SS] = __KERNEL_DS;
133 gdb_regs[GDB_FS] = 0xFFFF;
134 gdb_regs[GDB_GS] = 0xFFFF;
136 gdb_regs[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
137 gdb_regs[GDB_PC] = 0;
138 gdb_regs[GDB_R8] = 0;
139 gdb_regs[GDB_R9] = 0;
140 gdb_regs[GDB_R10] = 0;
141 gdb_regs[GDB_R11] = 0;
142 gdb_regs[GDB_R12] = 0;
143 gdb_regs[GDB_R13] = 0;
144 gdb_regs[GDB_R14] = 0;
145 gdb_regs[GDB_R15] = 0;
147 gdb_regs[GDB_SP] = p->thread.sp;
151 * gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs.
152 * @gdb_regs: A pointer to hold the registers we've received from GDB.
153 * @regs: A pointer to a &struct pt_regs to hold these values in.
155 * Convert the GDB regs in @gdb_regs into the pt_regs, and store them
158 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
160 regs->ax = gdb_regs[GDB_AX];
161 regs->bx = gdb_regs[GDB_BX];
162 regs->cx = gdb_regs[GDB_CX];
163 regs->dx = gdb_regs[GDB_DX];
164 regs->si = gdb_regs[GDB_SI];
165 regs->di = gdb_regs[GDB_DI];
166 regs->bp = gdb_regs[GDB_BP];
167 regs->flags = gdb_regs[GDB_PS];
168 regs->ip = gdb_regs[GDB_PC];
170 regs->ds = gdb_regs[GDB_DS];
171 regs->es = gdb_regs[GDB_ES];
172 regs->cs = gdb_regs[GDB_CS];
174 regs->r8 = gdb_regs[GDB_R8];
175 regs->r9 = gdb_regs[GDB_R9];
176 regs->r10 = gdb_regs[GDB_R10];
177 regs->r11 = gdb_regs[GDB_R11];
178 regs->r12 = gdb_regs[GDB_R12];
179 regs->r13 = gdb_regs[GDB_R13];
180 regs->r14 = gdb_regs[GDB_R14];
181 regs->r15 = gdb_regs[GDB_R15];
186 * kgdb_post_primary_code - Save error vector/code numbers.
187 * @regs: Original pt_regs.
188 * @e_vector: Original error vector.
189 * @err_code: Original error code.
191 * This is needed on architectures which support SMP and KGDB.
192 * This function is called after all the slave cpus have been put
193 * to a know spin state and the primary CPU has control over KGDB.
195 void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
197 /* primary processor is completely in the debugger */
198 gdb_x86vector = e_vector;
199 gdb_x86errcode = err_code;
204 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
205 * @flags: Current IRQ state
207 * On SMP systems, we need to get the attention of the other CPUs
208 * and get them be in a known state. This should do what is needed
209 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
210 * the NMI approach is not used for rounding up all the CPUs. For example,
211 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
212 * this case, we have to make sure that interrupts are enabled before
213 * calling smp_call_function(). The argument to this function is
214 * the flags that will be used when restoring the interrupts. There is
215 * local_irq_save() call before kgdb_roundup_cpus().
217 * On non-SMP systems, this is not called.
219 void kgdb_roundup_cpus(unsigned long flags)
221 send_IPI_allbutself(APIC_DM_NMI);
226 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
227 * @vector: The error vector of the exception that happened.
228 * @signo: The signal number of the exception that happened.
229 * @err_code: The error code of the exception that happened.
230 * @remcom_in_buffer: The buffer of the packet we have read.
231 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
232 * @regs: The &struct pt_regs of the current process.
234 * This function MUST handle the 'c' and 's' command packets,
235 * as well packets to set / remove a hardware breakpoint, if used.
236 * If there are additional packets which the hardware needs to handle,
237 * they are handled here. The code should return -1 if it wants to
238 * process more packets, and a %0 or %1 if it wants to exit from the
241 int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
242 char *remcomInBuffer, char *remcomOutBuffer,
243 struct pt_regs *linux_regs)
249 switch (remcomInBuffer[0]) {
252 /* try to read optional parameter, pc unchanged if no parm */
253 ptr = &remcomInBuffer[1];
254 if (kgdb_hex2long(&ptr, &addr))
255 linux_regs->ip = addr;
256 newPC = linux_regs->ip;
258 /* clear the trace bit */
259 linux_regs->flags &= ~TF_MASK;
260 atomic_set(&kgdb_cpu_doing_single_step, -1);
262 /* set the trace bit if we're stepping */
263 if (remcomInBuffer[0] == 's') {
264 linux_regs->flags |= TF_MASK;
265 kgdb_single_step = 1;
266 if (kgdb_contthread) {
267 atomic_set(&kgdb_cpu_doing_single_step,
268 raw_smp_processor_id());
275 /* this means that we do not want to exit from the handler: */
280 single_step_cont(struct pt_regs *regs, struct die_args *args)
283 * Single step exception from kernel space to user space so
284 * eat the exception and continue the process:
286 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
288 kgdb_arch_handle_exception(args->trapnr, args->signr,
289 args->err, "c", "", regs);
294 static int was_in_debug_nmi[NR_CPUS];
296 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
298 struct pt_regs *regs = args->regs;
302 if (atomic_read(&kgdb_active) != -1) {
303 /* KGDB CPU roundup */
304 kgdb_nmicallback(raw_smp_processor_id(), regs);
305 was_in_debug_nmi[raw_smp_processor_id()] = 1;
306 touch_nmi_watchdog();
312 if (atomic_read(&kgdb_active) != -1) {
313 /* KGDB CPU roundup */
314 kgdb_nmicallback(raw_smp_processor_id(), regs);
315 was_in_debug_nmi[raw_smp_processor_id()] = 1;
316 touch_nmi_watchdog();
321 if (was_in_debug_nmi[raw_smp_processor_id()]) {
322 was_in_debug_nmi[raw_smp_processor_id()] = 0;
327 case DIE_NMIWATCHDOG:
328 if (atomic_read(&kgdb_active) != -1) {
329 /* KGDB CPU roundup: */
330 kgdb_nmicallback(raw_smp_processor_id(), regs);
333 /* Enter debugger: */
337 if (atomic_read(&kgdb_cpu_doing_single_step) ==
338 raw_smp_processor_id() &&
340 return single_step_cont(regs, args);
347 if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs))
354 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
359 local_irq_save(flags);
360 ret = __kgdb_notify(ptr, cmd);
361 local_irq_restore(flags);
366 static struct notifier_block kgdb_notifier = {
367 .notifier_call = kgdb_notify,
370 * Lowest-prio notifier priority, we want to be notified last:
372 .priority = -INT_MAX,
376 * kgdb_arch_init - Perform any architecture specific initalization.
378 * This function will handle the initalization of any architecture
379 * specific callbacks.
381 int kgdb_arch_init(void)
383 return register_die_notifier(&kgdb_notifier);
387 * kgdb_arch_exit - Perform any architecture specific uninitalization.
389 * This function will handle the uninitalization of any architecture
390 * specific callbacks, for dynamic registration and unregistration.
392 void kgdb_arch_exit(void)
394 unregister_die_notifier(&kgdb_notifier);
399 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
400 * @exception: Exception vector number
401 * @regs: Current &struct pt_regs.
403 * On some architectures we need to skip a breakpoint exception when
404 * it occurs after a breakpoint has been removed.
406 * Skip an int3 exception when it occurs after a breakpoint has been
407 * removed. Backtrack eip by 1 since the int3 would have caused it to
410 int kgdb_skipexception(int exception, struct pt_regs *regs)
412 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
419 unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
422 return instruction_pointer(regs) - 1;
423 return instruction_pointer(regs);
426 struct kgdb_arch arch_kgdb_ops = {
427 /* Breakpoint instruction: */
428 .gdb_bpt_instr = { 0xcc },