blob: 62bea7307eaad9ba3b6d4774e0af7fd6fa92b547 [file] [log] [blame]
Ingo Molnar82da3ff2008-04-17 20:05:37 +02001/*
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.
6 *
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.
11 *
12 */
13
14/*
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.
21 */
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
33 */
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>
Jason Wesseld3597522008-02-15 14:55:53 -060044#include <linux/nmi.h>
Jason Wesselcc096742010-01-28 17:04:42 -060045#include <linux/hw_breakpoint.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020046
K.Prasad62edab92009-06-01 23:47:06 +053047#include <asm/debugreg.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020048#include <asm/apicdef.h>
49#include <asm/system.h>
50
Ingo Molnar7b6aa332009-02-17 13:58:15 +010051#include <asm/apic.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020052
53/*
54 * Put the error code here just in case the user cares:
55 */
56static int gdb_x86errcode;
57
58/*
59 * Likewise, the vector number here (since GDB only gets the signal
60 * number through the usual means, and that's not very specific):
61 */
62static int gdb_x86vector = -1;
63
64/**
65 * pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs
66 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
67 * @regs: The &struct pt_regs of the current process.
68 *
69 * Convert the pt_regs in @regs into the format for registers that
70 * GDB expects, stored in @gdb_regs.
71 */
72void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
73{
Jason Wessel703a1ed2008-09-26 10:36:42 -050074#ifndef CONFIG_X86_32
75 u32 *gdb_regs32 = (u32 *)gdb_regs;
76#endif
Ingo Molnar82da3ff2008-04-17 20:05:37 +020077 gdb_regs[GDB_AX] = regs->ax;
78 gdb_regs[GDB_BX] = regs->bx;
79 gdb_regs[GDB_CX] = regs->cx;
80 gdb_regs[GDB_DX] = regs->dx;
81 gdb_regs[GDB_SI] = regs->si;
82 gdb_regs[GDB_DI] = regs->di;
83 gdb_regs[GDB_BP] = regs->bp;
Ingo Molnar82da3ff2008-04-17 20:05:37 +020084 gdb_regs[GDB_PC] = regs->ip;
85#ifdef CONFIG_X86_32
Jason Wessel703a1ed2008-09-26 10:36:42 -050086 gdb_regs[GDB_PS] = regs->flags;
Ingo Molnar82da3ff2008-04-17 20:05:37 +020087 gdb_regs[GDB_DS] = regs->ds;
88 gdb_regs[GDB_ES] = regs->es;
89 gdb_regs[GDB_CS] = regs->cs;
Ingo Molnar82da3ff2008-04-17 20:05:37 +020090 gdb_regs[GDB_FS] = 0xFFFF;
91 gdb_regs[GDB_GS] = 0xFFFF;
Jason Wesselcf6f1962009-12-11 08:43:16 -060092 if (user_mode_vm(regs)) {
93 gdb_regs[GDB_SS] = regs->ss;
94 gdb_regs[GDB_SP] = regs->sp;
95 } else {
96 gdb_regs[GDB_SS] = __KERNEL_DS;
97 gdb_regs[GDB_SP] = kernel_stack_pointer(regs);
98 }
Ingo Molnar82da3ff2008-04-17 20:05:37 +020099#else
100 gdb_regs[GDB_R8] = regs->r8;
101 gdb_regs[GDB_R9] = regs->r9;
102 gdb_regs[GDB_R10] = regs->r10;
103 gdb_regs[GDB_R11] = regs->r11;
104 gdb_regs[GDB_R12] = regs->r12;
105 gdb_regs[GDB_R13] = regs->r13;
106 gdb_regs[GDB_R14] = regs->r14;
107 gdb_regs[GDB_R15] = regs->r15;
Jason Wessel703a1ed2008-09-26 10:36:42 -0500108 gdb_regs32[GDB_PS] = regs->flags;
109 gdb_regs32[GDB_CS] = regs->cs;
110 gdb_regs32[GDB_SS] = regs->ss;
H. Peter Anvin5ca6c0c2009-10-12 14:12:18 -0700111 gdb_regs[GDB_SP] = kernel_stack_pointer(regs);
Jason Wesselcf6f1962009-12-11 08:43:16 -0600112#endif
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200113}
114
115/**
116 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
117 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
118 * @p: The &struct task_struct of the desired process.
119 *
120 * Convert the register values of the sleeping process in @p to
121 * the format that GDB expects.
122 * This function is called when kgdb does not have access to the
123 * &struct pt_regs and therefore it should fill the gdb registers
124 * @gdb_regs with what has been saved in &struct thread_struct
125 * thread field during switch_to.
126 */
127void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
128{
Jason Wessel703a1ed2008-09-26 10:36:42 -0500129#ifndef CONFIG_X86_32
130 u32 *gdb_regs32 = (u32 *)gdb_regs;
131#endif
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200132 gdb_regs[GDB_AX] = 0;
133 gdb_regs[GDB_BX] = 0;
134 gdb_regs[GDB_CX] = 0;
135 gdb_regs[GDB_DX] = 0;
136 gdb_regs[GDB_SI] = 0;
137 gdb_regs[GDB_DI] = 0;
138 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
139#ifdef CONFIG_X86_32
140 gdb_regs[GDB_DS] = __KERNEL_DS;
141 gdb_regs[GDB_ES] = __KERNEL_DS;
142 gdb_regs[GDB_PS] = 0;
143 gdb_regs[GDB_CS] = __KERNEL_CS;
144 gdb_regs[GDB_PC] = p->thread.ip;
145 gdb_regs[GDB_SS] = __KERNEL_DS;
146 gdb_regs[GDB_FS] = 0xFFFF;
147 gdb_regs[GDB_GS] = 0xFFFF;
148#else
Jason Wessel703a1ed2008-09-26 10:36:42 -0500149 gdb_regs32[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
150 gdb_regs32[GDB_CS] = __KERNEL_CS;
151 gdb_regs32[GDB_SS] = __KERNEL_DS;
Alexey Dobriyan0c235902009-05-04 03:30:15 +0400152 gdb_regs[GDB_PC] = 0;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200153 gdb_regs[GDB_R8] = 0;
154 gdb_regs[GDB_R9] = 0;
155 gdb_regs[GDB_R10] = 0;
156 gdb_regs[GDB_R11] = 0;
157 gdb_regs[GDB_R12] = 0;
158 gdb_regs[GDB_R13] = 0;
159 gdb_regs[GDB_R14] = 0;
160 gdb_regs[GDB_R15] = 0;
161#endif
162 gdb_regs[GDB_SP] = p->thread.sp;
163}
164
165/**
166 * gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs.
167 * @gdb_regs: A pointer to hold the registers we've received from GDB.
168 * @regs: A pointer to a &struct pt_regs to hold these values in.
169 *
170 * Convert the GDB regs in @gdb_regs into the pt_regs, and store them
171 * in @regs.
172 */
173void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
174{
Jason Wessel703a1ed2008-09-26 10:36:42 -0500175#ifndef CONFIG_X86_32
176 u32 *gdb_regs32 = (u32 *)gdb_regs;
177#endif
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200178 regs->ax = gdb_regs[GDB_AX];
179 regs->bx = gdb_regs[GDB_BX];
180 regs->cx = gdb_regs[GDB_CX];
181 regs->dx = gdb_regs[GDB_DX];
182 regs->si = gdb_regs[GDB_SI];
183 regs->di = gdb_regs[GDB_DI];
184 regs->bp = gdb_regs[GDB_BP];
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200185 regs->ip = gdb_regs[GDB_PC];
186#ifdef CONFIG_X86_32
Jason Wessel703a1ed2008-09-26 10:36:42 -0500187 regs->flags = gdb_regs[GDB_PS];
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200188 regs->ds = gdb_regs[GDB_DS];
189 regs->es = gdb_regs[GDB_ES];
190 regs->cs = gdb_regs[GDB_CS];
191#else
192 regs->r8 = gdb_regs[GDB_R8];
193 regs->r9 = gdb_regs[GDB_R9];
194 regs->r10 = gdb_regs[GDB_R10];
195 regs->r11 = gdb_regs[GDB_R11];
196 regs->r12 = gdb_regs[GDB_R12];
197 regs->r13 = gdb_regs[GDB_R13];
198 regs->r14 = gdb_regs[GDB_R14];
199 regs->r15 = gdb_regs[GDB_R15];
Jason Wessel703a1ed2008-09-26 10:36:42 -0500200 regs->flags = gdb_regs32[GDB_PS];
201 regs->cs = gdb_regs32[GDB_CS];
202 regs->ss = gdb_regs32[GDB_SS];
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200203#endif
204}
205
Jason Wessel64e9ee32008-02-15 14:55:56 -0600206static struct hw_breakpoint {
207 unsigned enabled;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600208 unsigned long addr;
Jason Wesselcc096742010-01-28 17:04:42 -0600209 int len;
210 int type;
211 struct perf_event **pev;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600212} breakinfo[4];
213
214static void kgdb_correct_hw_break(void)
215{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600216 int breakno;
217
Jason Wessel64e9ee32008-02-15 14:55:56 -0600218 for (breakno = 0; breakno < 4; breakno++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600219 struct perf_event *bp;
220 struct arch_hw_breakpoint *info;
221 int val;
222 int cpu = raw_smp_processor_id();
223 if (!breakinfo[breakno].enabled)
224 continue;
225 bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
226 info = counter_arch_bp(bp);
227 if (bp->attr.disabled != 1)
228 continue;
229 bp->attr.bp_addr = breakinfo[breakno].addr;
230 bp->attr.bp_len = breakinfo[breakno].len;
231 bp->attr.bp_type = breakinfo[breakno].type;
232 info->address = breakinfo[breakno].addr;
233 info->len = breakinfo[breakno].len;
234 info->type = breakinfo[breakno].type;
235 val = arch_install_hw_breakpoint(bp);
236 if (!val)
237 bp->attr.disabled = 0;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600238 }
Jason Wesselcc096742010-01-28 17:04:42 -0600239 hw_breakpoint_restore();
Jason Wessel64e9ee32008-02-15 14:55:56 -0600240}
241
242static int
243kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
244{
245 int i;
246
247 for (i = 0; i < 4; i++)
248 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
249 break;
250 if (i == 4)
251 return -1;
252
253 breakinfo[i].enabled = 0;
254
255 return 0;
256}
257
258static void kgdb_remove_all_hw_break(void)
259{
260 int i;
Jason Wesselcc096742010-01-28 17:04:42 -0600261 int cpu = raw_smp_processor_id();
262 struct perf_event *bp;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600263
Jason Wesselcc096742010-01-28 17:04:42 -0600264 for (i = 0; i < 4; i++) {
265 if (!breakinfo[i].enabled)
266 continue;
267 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
268 if (bp->attr.disabled == 1)
269 continue;
270 arch_uninstall_hw_breakpoint(bp);
271 bp->attr.disabled = 1;
272 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600273}
274
275static int
276kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
277{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600278 int i;
279
280 for (i = 0; i < 4; i++)
281 if (!breakinfo[i].enabled)
282 break;
283 if (i == 4)
284 return -1;
285
286 switch (bptype) {
287 case BP_HARDWARE_BREAKPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600288 len = 1;
289 breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600290 break;
291 case BP_WRITE_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600292 breakinfo[i].type = X86_BREAKPOINT_WRITE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600293 break;
294 case BP_ACCESS_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600295 breakinfo[i].type = X86_BREAKPOINT_RW;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600296 break;
297 default:
298 return -1;
299 }
Jason Wesselcc096742010-01-28 17:04:42 -0600300 switch (len) {
301 case 1:
302 breakinfo[i].len = X86_BREAKPOINT_LEN_1;
303 break;
304 case 2:
305 breakinfo[i].len = X86_BREAKPOINT_LEN_2;
306 break;
307 case 4:
308 breakinfo[i].len = X86_BREAKPOINT_LEN_4;
309 break;
310#ifdef CONFIG_X86_64
311 case 8:
312 breakinfo[i].len = X86_BREAKPOINT_LEN_8;
313 break;
314#endif
315 default:
Jason Wessel64e9ee32008-02-15 14:55:56 -0600316 return -1;
Jason Wesselcc096742010-01-28 17:04:42 -0600317 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600318 breakinfo[i].addr = addr;
Jason Wesselcc096742010-01-28 17:04:42 -0600319 breakinfo[i].enabled = 1;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600320
321 return 0;
322}
323
324/**
325 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
326 * @regs: Current &struct pt_regs.
327 *
328 * This function will be called if the particular architecture must
329 * disable hardware debugging while it is processing gdb packets or
330 * handling exception.
331 */
332void kgdb_disable_hw_debug(struct pt_regs *regs)
333{
Jason Wesselcc096742010-01-28 17:04:42 -0600334 int i;
335 int cpu = raw_smp_processor_id();
336 struct perf_event *bp;
337
Jason Wessel64e9ee32008-02-15 14:55:56 -0600338 /* Disable hardware debugging while we are in kgdb: */
339 set_debugreg(0UL, 7);
Jason Wesselcc096742010-01-28 17:04:42 -0600340 for (i = 0; i < 4; i++) {
341 if (!breakinfo[i].enabled)
342 continue;
343 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
344 if (bp->attr.disabled == 1)
345 continue;
346 arch_uninstall_hw_breakpoint(bp);
347 bp->attr.disabled = 1;
348 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600349}
350
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200351/**
352 * kgdb_post_primary_code - Save error vector/code numbers.
353 * @regs: Original pt_regs.
354 * @e_vector: Original error vector.
355 * @err_code: Original error code.
356 *
357 * This is needed on architectures which support SMP and KGDB.
358 * This function is called after all the slave cpus have been put
359 * to a know spin state and the primary CPU has control over KGDB.
360 */
361void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
362{
363 /* primary processor is completely in the debugger */
364 gdb_x86vector = e_vector;
365 gdb_x86errcode = err_code;
366}
367
368#ifdef CONFIG_SMP
369/**
370 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
371 * @flags: Current IRQ state
372 *
373 * On SMP systems, we need to get the attention of the other CPUs
374 * and get them be in a known state. This should do what is needed
375 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
376 * the NMI approach is not used for rounding up all the CPUs. For example,
377 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
378 * this case, we have to make sure that interrupts are enabled before
379 * calling smp_call_function(). The argument to this function is
380 * the flags that will be used when restoring the interrupts. There is
381 * local_irq_save() call before kgdb_roundup_cpus().
382 *
383 * On non-SMP systems, this is not called.
384 */
385void kgdb_roundup_cpus(unsigned long flags)
386{
Ingo Molnardac5f412009-01-28 15:42:24 +0100387 apic->send_IPI_allbutself(APIC_DM_NMI);
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200388}
389#endif
390
391/**
392 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
393 * @vector: The error vector of the exception that happened.
394 * @signo: The signal number of the exception that happened.
395 * @err_code: The error code of the exception that happened.
396 * @remcom_in_buffer: The buffer of the packet we have read.
397 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
398 * @regs: The &struct pt_regs of the current process.
399 *
400 * This function MUST handle the 'c' and 's' command packets,
401 * as well packets to set / remove a hardware breakpoint, if used.
402 * If there are additional packets which the hardware needs to handle,
403 * they are handled here. The code should return -1 if it wants to
404 * process more packets, and a %0 or %1 if it wants to exit from the
405 * kgdb callback.
406 */
407int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
408 char *remcomInBuffer, char *remcomOutBuffer,
409 struct pt_regs *linux_regs)
410{
411 unsigned long addr;
412 char *ptr;
413 int newPC;
414
415 switch (remcomInBuffer[0]) {
416 case 'c':
417 case 's':
418 /* try to read optional parameter, pc unchanged if no parm */
419 ptr = &remcomInBuffer[1];
420 if (kgdb_hex2long(&ptr, &addr))
421 linux_regs->ip = addr;
Jason Wessel737a4602008-03-07 16:34:16 -0600422 case 'D':
423 case 'k':
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200424 newPC = linux_regs->ip;
425
426 /* clear the trace bit */
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700427 linux_regs->flags &= ~X86_EFLAGS_TF;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200428 atomic_set(&kgdb_cpu_doing_single_step, -1);
429
430 /* set the trace bit if we're stepping */
431 if (remcomInBuffer[0] == 's') {
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700432 linux_regs->flags |= X86_EFLAGS_TF;
Jason Wesseld7161a62008-09-26 10:36:41 -0500433 atomic_set(&kgdb_cpu_doing_single_step,
434 raw_smp_processor_id());
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200435 }
436
Jason Wessel64e9ee32008-02-15 14:55:56 -0600437 kgdb_correct_hw_break();
438
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200439 return 0;
440 }
441
442 /* this means that we do not want to exit from the handler: */
443 return -1;
444}
445
446static inline int
447single_step_cont(struct pt_regs *regs, struct die_args *args)
448{
449 /*
450 * Single step exception from kernel space to user space so
451 * eat the exception and continue the process:
452 */
453 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
454 "resuming...\n");
455 kgdb_arch_handle_exception(args->trapnr, args->signr,
456 args->err, "c", "", regs);
K.Prasad62edab92009-06-01 23:47:06 +0530457 /*
458 * Reset the BS bit in dr6 (pointed by args->err) to
459 * denote completion of processing
460 */
461 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200462
463 return NOTIFY_STOP;
464}
465
Jason Wesseld3597522008-02-15 14:55:53 -0600466static int was_in_debug_nmi[NR_CPUS];
467
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200468static int __kgdb_notify(struct die_args *args, unsigned long cmd)
469{
470 struct pt_regs *regs = args->regs;
471
472 switch (cmd) {
473 case DIE_NMI:
474 if (atomic_read(&kgdb_active) != -1) {
475 /* KGDB CPU roundup */
476 kgdb_nmicallback(raw_smp_processor_id(), regs);
Jason Wesseld3597522008-02-15 14:55:53 -0600477 was_in_debug_nmi[raw_smp_processor_id()] = 1;
478 touch_nmi_watchdog();
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200479 return NOTIFY_STOP;
480 }
481 return NOTIFY_DONE;
482
483 case DIE_NMI_IPI:
Jan Kiszkae85ceae2008-10-06 13:50:59 -0500484 /* Just ignore, we will handle the roundup on DIE_NMI. */
Jason Wesseld3597522008-02-15 14:55:53 -0600485 return NOTIFY_DONE;
486
487 case DIE_NMIUNKNOWN:
488 if (was_in_debug_nmi[raw_smp_processor_id()]) {
489 was_in_debug_nmi[raw_smp_processor_id()] = 0;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200490 return NOTIFY_STOP;
491 }
492 return NOTIFY_DONE;
493
494 case DIE_NMIWATCHDOG:
495 if (atomic_read(&kgdb_active) != -1) {
496 /* KGDB CPU roundup: */
497 kgdb_nmicallback(raw_smp_processor_id(), regs);
498 return NOTIFY_STOP;
499 }
500 /* Enter debugger: */
501 break;
502
503 case DIE_DEBUG:
Jason Wesselcc096742010-01-28 17:04:42 -0600504 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
Jason Wesseld7161a62008-09-26 10:36:41 -0500505 if (user_mode(regs))
506 return single_step_cont(regs, args);
507 break;
508 } else if (test_thread_flag(TIF_SINGLESTEP))
509 /* This means a user thread is single stepping
510 * a system call which should be ignored
511 */
512 return NOTIFY_DONE;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200513 /* fall through */
514 default:
515 if (user_mode(regs))
516 return NOTIFY_DONE;
517 }
518
519 if (kgdb_handle_exception(args->trapnr, args->signr, args->err, regs))
520 return NOTIFY_DONE;
521
Jason Wessel737a4602008-03-07 16:34:16 -0600522 /* Must touch watchdog before return to normal operation */
523 touch_nmi_watchdog();
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200524 return NOTIFY_STOP;
525}
526
527static int
528kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
529{
530 unsigned long flags;
531 int ret;
532
533 local_irq_save(flags);
534 ret = __kgdb_notify(ptr, cmd);
535 local_irq_restore(flags);
536
537 return ret;
538}
539
540static struct notifier_block kgdb_notifier = {
541 .notifier_call = kgdb_notify,
542
543 /*
544 * Lowest-prio notifier priority, we want to be notified last:
545 */
546 .priority = -INT_MAX,
547};
548
549/**
550 * kgdb_arch_init - Perform any architecture specific initalization.
551 *
552 * This function will handle the initalization of any architecture
553 * specific callbacks.
554 */
555int kgdb_arch_init(void)
556{
Jason Wesselcc096742010-01-28 17:04:42 -0600557 int i, cpu;
558 int ret;
559 struct perf_event_attr attr;
560 struct perf_event **pevent;
561
562 ret = register_die_notifier(&kgdb_notifier);
563 if (ret != 0)
564 return ret;
565 /*
566 * Pre-allocate the hw breakpoint structions in the non-atomic
567 * portion of kgdb because this operation requires mutexs to
568 * complete.
569 */
570 attr.bp_addr = (unsigned long)kgdb_arch_init;
571 attr.type = PERF_TYPE_BREAKPOINT;
572 attr.bp_len = HW_BREAKPOINT_LEN_1;
573 attr.bp_type = HW_BREAKPOINT_W;
574 attr.disabled = 1;
575 for (i = 0; i < 4; i++) {
576 breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL);
577 if (IS_ERR(breakinfo[i].pev)) {
578 printk(KERN_ERR "kgdb: Could not allocate hw breakpoints\n");
579 breakinfo[i].pev = NULL;
580 kgdb_arch_exit();
581 return -1;
582 }
583 for_each_online_cpu(cpu) {
584 pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
585 pevent[0]->hw.sample_period = 1;
586 if (pevent[0]->destroy != NULL) {
587 pevent[0]->destroy = NULL;
588 release_bp_slot(*pevent);
589 }
590 }
591 }
592 return ret;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200593}
594
595/**
596 * kgdb_arch_exit - Perform any architecture specific uninitalization.
597 *
598 * This function will handle the uninitalization of any architecture
599 * specific callbacks, for dynamic registration and unregistration.
600 */
601void kgdb_arch_exit(void)
602{
Jason Wesselcc096742010-01-28 17:04:42 -0600603 int i;
604 for (i = 0; i < 4; i++) {
605 if (breakinfo[i].pev) {
606 unregister_wide_hw_breakpoint(breakinfo[i].pev);
607 breakinfo[i].pev = NULL;
608 }
609 }
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200610 unregister_die_notifier(&kgdb_notifier);
611}
612
613/**
614 *
615 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
616 * @exception: Exception vector number
617 * @regs: Current &struct pt_regs.
618 *
619 * On some architectures we need to skip a breakpoint exception when
620 * it occurs after a breakpoint has been removed.
621 *
622 * Skip an int3 exception when it occurs after a breakpoint has been
623 * removed. Backtrack eip by 1 since the int3 would have caused it to
624 * increment by 1.
625 */
626int kgdb_skipexception(int exception, struct pt_regs *regs)
627{
628 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
629 regs->ip -= 1;
630 return 1;
631 }
632 return 0;
633}
634
635unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
636{
637 if (exception == 3)
638 return instruction_pointer(regs) - 1;
639 return instruction_pointer(regs);
640}
641
642struct kgdb_arch arch_kgdb_ops = {
643 /* Breakpoint instruction: */
644 .gdb_bpt_instr = { 0xcc },
Jason Wessel64e9ee32008-02-15 14:55:56 -0600645 .flags = KGDB_HW_BREAKPOINT,
646 .set_hw_breakpoint = kgdb_set_hw_break,
647 .remove_hw_breakpoint = kgdb_remove_hw_break,
648 .remove_all_hw_break = kgdb_remove_all_hw_break,
649 .correct_hw_break = kgdb_correct_hw_break,
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200650};