x86: kgdb support
[linux-2.6/zen-sources.git] / arch / x86 / kernel / kgdb.c
blob37194d6374d829204ef4769e49da1b0e6d154fd9
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.
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>
45 #include <asm/apicdef.h>
46 #include <asm/system.h>
48 #ifdef CONFIG_X86_32
49 # include <mach_ipi.h>
50 #else
51 # include <asm/mach_apic.h>
52 #endif
55 * Put the error code here just in case the user cares:
57 static int gdb_x86errcode;
60 * Likewise, the vector number here (since GDB only gets the signal
61 * number through the usual means, and that's not very specific):
63 static int gdb_x86vector = -1;
65 /**
66 * pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs
67 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
68 * @regs: The &struct pt_regs of the current process.
70 * Convert the pt_regs in @regs into the format for registers that
71 * GDB expects, stored in @gdb_regs.
73 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *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_PS] = regs->flags;
83 gdb_regs[GDB_PC] = regs->ip;
84 #ifdef CONFIG_X86_32
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 #else
92 gdb_regs[GDB_R8] = regs->r8;
93 gdb_regs[GDB_R9] = regs->r9;
94 gdb_regs[GDB_R10] = regs->r10;
95 gdb_regs[GDB_R11] = regs->r11;
96 gdb_regs[GDB_R12] = regs->r12;
97 gdb_regs[GDB_R13] = regs->r13;
98 gdb_regs[GDB_R14] = regs->r14;
99 gdb_regs[GDB_R15] = regs->r15;
100 #endif
101 gdb_regs[GDB_SP] = regs->sp;
105 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
106 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
107 * @p: The &struct task_struct of the desired process.
109 * Convert the register values of the sleeping process in @p to
110 * the format that GDB expects.
111 * This function is called when kgdb does not have access to the
112 * &struct pt_regs and therefore it should fill the gdb registers
113 * @gdb_regs with what has been saved in &struct thread_struct
114 * thread field during switch_to.
116 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
118 gdb_regs[GDB_AX] = 0;
119 gdb_regs[GDB_BX] = 0;
120 gdb_regs[GDB_CX] = 0;
121 gdb_regs[GDB_DX] = 0;
122 gdb_regs[GDB_SI] = 0;
123 gdb_regs[GDB_DI] = 0;
124 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
125 #ifdef CONFIG_X86_32
126 gdb_regs[GDB_DS] = __KERNEL_DS;
127 gdb_regs[GDB_ES] = __KERNEL_DS;
128 gdb_regs[GDB_PS] = 0;
129 gdb_regs[GDB_CS] = __KERNEL_CS;
130 gdb_regs[GDB_PC] = p->thread.ip;
131 gdb_regs[GDB_SS] = __KERNEL_DS;
132 gdb_regs[GDB_FS] = 0xFFFF;
133 gdb_regs[GDB_GS] = 0xFFFF;
134 #else
135 gdb_regs[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
136 gdb_regs[GDB_PC] = 0;
137 gdb_regs[GDB_R8] = 0;
138 gdb_regs[GDB_R9] = 0;
139 gdb_regs[GDB_R10] = 0;
140 gdb_regs[GDB_R11] = 0;
141 gdb_regs[GDB_R12] = 0;
142 gdb_regs[GDB_R13] = 0;
143 gdb_regs[GDB_R14] = 0;
144 gdb_regs[GDB_R15] = 0;
145 #endif
146 gdb_regs[GDB_SP] = p->thread.sp;
150 * gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs.
151 * @gdb_regs: A pointer to hold the registers we've received from GDB.
152 * @regs: A pointer to a &struct pt_regs to hold these values in.
154 * Convert the GDB regs in @gdb_regs into the pt_regs, and store them
155 * in @regs.
157 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
159 regs->ax = gdb_regs[GDB_AX];
160 regs->bx = gdb_regs[GDB_BX];
161 regs->cx = gdb_regs[GDB_CX];
162 regs->dx = gdb_regs[GDB_DX];
163 regs->si = gdb_regs[GDB_SI];
164 regs->di = gdb_regs[GDB_DI];
165 regs->bp = gdb_regs[GDB_BP];
166 regs->flags = gdb_regs[GDB_PS];
167 regs->ip = gdb_regs[GDB_PC];
168 #ifdef CONFIG_X86_32
169 regs->ds = gdb_regs[GDB_DS];
170 regs->es = gdb_regs[GDB_ES];
171 regs->cs = gdb_regs[GDB_CS];
172 #else
173 regs->r8 = gdb_regs[GDB_R8];
174 regs->r9 = gdb_regs[GDB_R9];
175 regs->r10 = gdb_regs[GDB_R10];
176 regs->r11 = gdb_regs[GDB_R11];
177 regs->r12 = gdb_regs[GDB_R12];
178 regs->r13 = gdb_regs[GDB_R13];
179 regs->r14 = gdb_regs[GDB_R14];
180 regs->r15 = gdb_regs[GDB_R15];
181 #endif
185 * kgdb_post_primary_code - Save error vector/code numbers.
186 * @regs: Original pt_regs.
187 * @e_vector: Original error vector.
188 * @err_code: Original error code.
190 * This is needed on architectures which support SMP and KGDB.
191 * This function is called after all the slave cpus have been put
192 * to a know spin state and the primary CPU has control over KGDB.
194 void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
196 /* primary processor is completely in the debugger */
197 gdb_x86vector = e_vector;
198 gdb_x86errcode = err_code;
201 #ifdef CONFIG_SMP
203 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
204 * @flags: Current IRQ state
206 * On SMP systems, we need to get the attention of the other CPUs
207 * and get them be in a known state. This should do what is needed
208 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
209 * the NMI approach is not used for rounding up all the CPUs. For example,
210 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
211 * this case, we have to make sure that interrupts are enabled before
212 * calling smp_call_function(). The argument to this function is
213 * the flags that will be used when restoring the interrupts. There is
214 * local_irq_save() call before kgdb_roundup_cpus().
216 * On non-SMP systems, this is not called.
218 void kgdb_roundup_cpus(unsigned long flags)
220 send_IPI_allbutself(APIC_DM_NMI);
222 #endif
225 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
226 * @vector: The error vector of the exception that happened.
227 * @signo: The signal number of the exception that happened.
228 * @err_code: The error code of the exception that happened.
229 * @remcom_in_buffer: The buffer of the packet we have read.
230 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
231 * @regs: The &struct pt_regs of the current process.
233 * This function MUST handle the 'c' and 's' command packets,
234 * as well packets to set / remove a hardware breakpoint, if used.
235 * If there are additional packets which the hardware needs to handle,
236 * they are handled here. The code should return -1 if it wants to
237 * process more packets, and a %0 or %1 if it wants to exit from the
238 * kgdb callback.
240 int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
241 char *remcomInBuffer, char *remcomOutBuffer,
242 struct pt_regs *linux_regs)
244 unsigned long addr;
245 char *ptr;
246 int newPC;
248 switch (remcomInBuffer[0]) {
249 case 'c':
250 case 's':
251 /* try to read optional parameter, pc unchanged if no parm */
252 ptr = &remcomInBuffer[1];
253 if (kgdb_hex2long(&ptr, &addr))
254 linux_regs->ip = addr;
255 newPC = linux_regs->ip;
257 /* clear the trace bit */
258 linux_regs->flags &= ~TF_MASK;
259 atomic_set(&kgdb_cpu_doing_single_step, -1);
261 /* set the trace bit if we're stepping */
262 if (remcomInBuffer[0] == 's') {
263 linux_regs->flags |= TF_MASK;
264 kgdb_single_step = 1;
265 if (kgdb_contthread) {
266 atomic_set(&kgdb_cpu_doing_single_step,
267 raw_smp_processor_id());
271 return 0;
274 /* this means that we do not want to exit from the handler: */
275 return -1;
278 static inline int
279 single_step_cont(struct pt_regs *regs, struct die_args *args)
282 * Single step exception from kernel space to user space so
283 * eat the exception and continue the process:
285 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
286 "resuming...\n");
287 kgdb_arch_handle_exception(args->trapnr, args->signr,
288 args->err, "c", "", regs);
290 return NOTIFY_STOP;
293 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
295 struct pt_regs *regs = args->regs;
297 switch (cmd) {
298 case DIE_NMI:
299 if (atomic_read(&kgdb_active) != -1) {
300 /* KGDB CPU roundup */
301 kgdb_nmicallback(raw_smp_processor_id(), regs);
302 return NOTIFY_STOP;
304 return NOTIFY_DONE;
306 case DIE_NMI_IPI:
307 if (atomic_read(&kgdb_active) != -1) {
308 /* KGDB CPU roundup: */
309 if (kgdb_nmicallback(raw_smp_processor_id(), regs))
310 return NOTIFY_DONE;
311 return NOTIFY_STOP;
313 return NOTIFY_DONE;
315 case DIE_NMIWATCHDOG:
316 if (atomic_read(&kgdb_active) != -1) {
317 /* KGDB CPU roundup: */
318 kgdb_nmicallback(raw_smp_processor_id(), regs);
319 return NOTIFY_STOP;
321 /* Enter debugger: */
322 break;
324 case DIE_DEBUG:
325 if (atomic_read(&kgdb_cpu_doing_single_step) ==
326 raw_smp_processor_id() &&
327 user_mode(regs))
328 return single_step_cont(regs, args);
329 /* fall through */
330 default:
331 if (user_mode(regs))
332 return NOTIFY_DONE;
335 if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs))
336 return NOTIFY_DONE;
338 return NOTIFY_STOP;
341 static int
342 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
344 unsigned long flags;
345 int ret;
347 local_irq_save(flags);
348 ret = __kgdb_notify(ptr, cmd);
349 local_irq_restore(flags);
351 return ret;
354 static struct notifier_block kgdb_notifier = {
355 .notifier_call = kgdb_notify,
358 * Lowest-prio notifier priority, we want to be notified last:
360 .priority = -INT_MAX,
364 * kgdb_arch_init - Perform any architecture specific initalization.
366 * This function will handle the initalization of any architecture
367 * specific callbacks.
369 int kgdb_arch_init(void)
371 return register_die_notifier(&kgdb_notifier);
375 * kgdb_arch_exit - Perform any architecture specific uninitalization.
377 * This function will handle the uninitalization of any architecture
378 * specific callbacks, for dynamic registration and unregistration.
380 void kgdb_arch_exit(void)
382 unregister_die_notifier(&kgdb_notifier);
387 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
388 * @exception: Exception vector number
389 * @regs: Current &struct pt_regs.
391 * On some architectures we need to skip a breakpoint exception when
392 * it occurs after a breakpoint has been removed.
394 * Skip an int3 exception when it occurs after a breakpoint has been
395 * removed. Backtrack eip by 1 since the int3 would have caused it to
396 * increment by 1.
398 int kgdb_skipexception(int exception, struct pt_regs *regs)
400 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
401 regs->ip -= 1;
402 return 1;
404 return 0;
407 unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
409 if (exception == 3)
410 return instruction_pointer(regs) - 1;
411 return instruction_pointer(regs);
414 struct kgdb_arch arch_kgdb_ops = {
415 /* Breakpoint instruction: */
416 .gdb_bpt_instr = { 0xcc },