blob: db6720edfdd04f6d6a5e57da090c36db7a28eb31 [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>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010049#include <asm/apic.h>
Don Zickus166d7512011-01-06 16:18:49 -050050#include <asm/nmi.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020051
Jason Wessel12bfa3d2010-08-05 09:22:20 -050052struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
Ingo Molnar82da3ff2008-04-17 20:05:37 +020053{
Ingo Molnar82da3ff2008-04-17 20:05:37 +020054#ifdef CONFIG_X86_32
Jason Wessel12bfa3d2010-08-05 09:22:20 -050055 { "ax", 4, offsetof(struct pt_regs, ax) },
56 { "cx", 4, offsetof(struct pt_regs, cx) },
57 { "dx", 4, offsetof(struct pt_regs, dx) },
58 { "bx", 4, offsetof(struct pt_regs, bx) },
59 { "sp", 4, offsetof(struct pt_regs, sp) },
60 { "bp", 4, offsetof(struct pt_regs, bp) },
61 { "si", 4, offsetof(struct pt_regs, si) },
62 { "di", 4, offsetof(struct pt_regs, di) },
63 { "ip", 4, offsetof(struct pt_regs, ip) },
64 { "flags", 4, offsetof(struct pt_regs, flags) },
65 { "cs", 4, offsetof(struct pt_regs, cs) },
66 { "ss", 4, offsetof(struct pt_regs, ss) },
67 { "ds", 4, offsetof(struct pt_regs, ds) },
68 { "es", 4, offsetof(struct pt_regs, es) },
Ingo Molnar82da3ff2008-04-17 20:05:37 +020069#else
Jason Wessel12bfa3d2010-08-05 09:22:20 -050070 { "ax", 8, offsetof(struct pt_regs, ax) },
71 { "bx", 8, offsetof(struct pt_regs, bx) },
72 { "cx", 8, offsetof(struct pt_regs, cx) },
73 { "dx", 8, offsetof(struct pt_regs, dx) },
74 { "si", 8, offsetof(struct pt_regs, dx) },
75 { "di", 8, offsetof(struct pt_regs, di) },
76 { "bp", 8, offsetof(struct pt_regs, bp) },
77 { "sp", 8, offsetof(struct pt_regs, sp) },
78 { "r8", 8, offsetof(struct pt_regs, r8) },
79 { "r9", 8, offsetof(struct pt_regs, r9) },
80 { "r10", 8, offsetof(struct pt_regs, r10) },
81 { "r11", 8, offsetof(struct pt_regs, r11) },
82 { "r12", 8, offsetof(struct pt_regs, r12) },
83 { "r13", 8, offsetof(struct pt_regs, r13) },
84 { "r14", 8, offsetof(struct pt_regs, r14) },
85 { "r15", 8, offsetof(struct pt_regs, r15) },
86 { "ip", 8, offsetof(struct pt_regs, ip) },
87 { "flags", 4, offsetof(struct pt_regs, flags) },
88 { "cs", 4, offsetof(struct pt_regs, cs) },
89 { "ss", 4, offsetof(struct pt_regs, ss) },
Jan Kiszka639077f2012-03-19 15:16:48 -050090 { "ds", 4, -1 },
91 { "es", 4, -1 },
Jason Wesselcf6f1962009-12-11 08:43:16 -060092#endif
Jan Kiszka639077f2012-03-19 15:16:48 -050093 { "fs", 4, -1 },
94 { "gs", 4, -1 },
Jason Wessel12bfa3d2010-08-05 09:22:20 -050095};
96
97int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
98{
99 if (
100#ifdef CONFIG_X86_32
101 regno == GDB_SS || regno == GDB_FS || regno == GDB_GS ||
102#endif
103 regno == GDB_SP || regno == GDB_ORIG_AX)
104 return 0;
105
106 if (dbg_reg_def[regno].offset != -1)
107 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
108 dbg_reg_def[regno].size);
109 return 0;
110}
111
112char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
113{
114 if (regno == GDB_ORIG_AX) {
115 memcpy(mem, &regs->orig_ax, sizeof(regs->orig_ax));
116 return "orig_ax";
117 }
118 if (regno >= DBG_MAX_REG_NUM || regno < 0)
119 return NULL;
120
121 if (dbg_reg_def[regno].offset != -1)
122 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
123 dbg_reg_def[regno].size);
124
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500125#ifdef CONFIG_X86_32
Jason Wessel21431c22010-03-15 07:28:00 -0500126 switch (regno) {
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500127 case GDB_SS:
128 if (!user_mode_vm(regs))
129 *(unsigned long *)mem = __KERNEL_DS;
130 break;
131 case GDB_SP:
132 if (!user_mode_vm(regs))
133 *(unsigned long *)mem = kernel_stack_pointer(regs);
134 break;
135 case GDB_GS:
136 case GDB_FS:
137 *(unsigned long *)mem = 0xFFFF;
138 break;
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500139 }
Jason Wessel21431c22010-03-15 07:28:00 -0500140#endif
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500141 return dbg_reg_def[regno].name;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200142}
143
144/**
145 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
146 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
147 * @p: The &struct task_struct of the desired process.
148 *
149 * Convert the register values of the sleeping process in @p to
150 * the format that GDB expects.
151 * This function is called when kgdb does not have access to the
152 * &struct pt_regs and therefore it should fill the gdb registers
153 * @gdb_regs with what has been saved in &struct thread_struct
154 * thread field during switch_to.
155 */
156void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
157{
Jason Wessel703a1ed2008-09-26 10:36:42 -0500158#ifndef CONFIG_X86_32
159 u32 *gdb_regs32 = (u32 *)gdb_regs;
160#endif
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200161 gdb_regs[GDB_AX] = 0;
162 gdb_regs[GDB_BX] = 0;
163 gdb_regs[GDB_CX] = 0;
164 gdb_regs[GDB_DX] = 0;
165 gdb_regs[GDB_SI] = 0;
166 gdb_regs[GDB_DI] = 0;
167 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
168#ifdef CONFIG_X86_32
169 gdb_regs[GDB_DS] = __KERNEL_DS;
170 gdb_regs[GDB_ES] = __KERNEL_DS;
171 gdb_regs[GDB_PS] = 0;
172 gdb_regs[GDB_CS] = __KERNEL_CS;
173 gdb_regs[GDB_PC] = p->thread.ip;
174 gdb_regs[GDB_SS] = __KERNEL_DS;
175 gdb_regs[GDB_FS] = 0xFFFF;
176 gdb_regs[GDB_GS] = 0xFFFF;
177#else
Jason Wessel703a1ed2008-09-26 10:36:42 -0500178 gdb_regs32[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
179 gdb_regs32[GDB_CS] = __KERNEL_CS;
180 gdb_regs32[GDB_SS] = __KERNEL_DS;
Alexey Dobriyan0c235902009-05-04 03:30:15 +0400181 gdb_regs[GDB_PC] = 0;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200182 gdb_regs[GDB_R8] = 0;
183 gdb_regs[GDB_R9] = 0;
184 gdb_regs[GDB_R10] = 0;
185 gdb_regs[GDB_R11] = 0;
186 gdb_regs[GDB_R12] = 0;
187 gdb_regs[GDB_R13] = 0;
188 gdb_regs[GDB_R14] = 0;
189 gdb_regs[GDB_R15] = 0;
190#endif
191 gdb_regs[GDB_SP] = p->thread.sp;
192}
193
Jason Wessel64e9ee32008-02-15 14:55:56 -0600194static struct hw_breakpoint {
195 unsigned enabled;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600196 unsigned long addr;
Jason Wesselcc096742010-01-28 17:04:42 -0600197 int len;
198 int type;
Namhyung Kim8c8aefc2010-08-07 11:00:59 -0700199 struct perf_event * __percpu *pev;
Dongdong Dengdf493932010-08-05 09:22:25 -0500200} breakinfo[HBP_NUM];
Jason Wessel64e9ee32008-02-15 14:55:56 -0600201
Jason Wessel031acd82010-05-20 21:04:30 -0500202static unsigned long early_dr7;
203
Jason Wessel64e9ee32008-02-15 14:55:56 -0600204static void kgdb_correct_hw_break(void)
205{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600206 int breakno;
207
Dongdong Dengdf493932010-08-05 09:22:25 -0500208 for (breakno = 0; breakno < HBP_NUM; breakno++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600209 struct perf_event *bp;
210 struct arch_hw_breakpoint *info;
211 int val;
212 int cpu = raw_smp_processor_id();
213 if (!breakinfo[breakno].enabled)
214 continue;
Jason Wessel031acd82010-05-20 21:04:30 -0500215 if (dbg_is_early) {
216 set_debugreg(breakinfo[breakno].addr, breakno);
217 early_dr7 |= encode_dr7(breakno,
218 breakinfo[breakno].len,
219 breakinfo[breakno].type);
220 set_debugreg(early_dr7, 7);
221 continue;
222 }
Jason Wesselcc096742010-01-28 17:04:42 -0600223 bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
224 info = counter_arch_bp(bp);
225 if (bp->attr.disabled != 1)
226 continue;
227 bp->attr.bp_addr = breakinfo[breakno].addr;
228 bp->attr.bp_len = breakinfo[breakno].len;
229 bp->attr.bp_type = breakinfo[breakno].type;
230 info->address = breakinfo[breakno].addr;
231 info->len = breakinfo[breakno].len;
232 info->type = breakinfo[breakno].type;
233 val = arch_install_hw_breakpoint(bp);
234 if (!val)
235 bp->attr.disabled = 0;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600236 }
Jason Wessel031acd82010-05-20 21:04:30 -0500237 if (!dbg_is_early)
238 hw_breakpoint_restore();
Jason Wessel64e9ee32008-02-15 14:55:56 -0600239}
240
Jason Wessel5352ae62010-01-28 17:04:43 -0600241static int hw_break_reserve_slot(int breakno)
242{
243 int cpu;
244 int cnt = 0;
245 struct perf_event **pevent;
246
Jason Wessel031acd82010-05-20 21:04:30 -0500247 if (dbg_is_early)
248 return 0;
249
Jason Wessel5352ae62010-01-28 17:04:43 -0600250 for_each_online_cpu(cpu) {
251 cnt++;
252 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
253 if (dbg_reserve_bp_slot(*pevent))
254 goto fail;
255 }
256
257 return 0;
258
259fail:
260 for_each_online_cpu(cpu) {
261 cnt--;
262 if (!cnt)
263 break;
264 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
265 dbg_release_bp_slot(*pevent);
266 }
267 return -1;
268}
269
270static int hw_break_release_slot(int breakno)
271{
272 struct perf_event **pevent;
273 int cpu;
274
Jason Wessel031acd82010-05-20 21:04:30 -0500275 if (dbg_is_early)
276 return 0;
277
Jason Wessel5352ae62010-01-28 17:04:43 -0600278 for_each_online_cpu(cpu) {
279 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
280 if (dbg_release_bp_slot(*pevent))
281 /*
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300282 * The debugger is responsible for handing the retry on
Jason Wessel5352ae62010-01-28 17:04:43 -0600283 * remove failure.
284 */
285 return -1;
286 }
287 return 0;
288}
289
Jason Wessel64e9ee32008-02-15 14:55:56 -0600290static int
291kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
292{
293 int i;
294
Dongdong Dengdf493932010-08-05 09:22:25 -0500295 for (i = 0; i < HBP_NUM; i++)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600296 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
297 break;
Dongdong Dengdf493932010-08-05 09:22:25 -0500298 if (i == HBP_NUM)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600299 return -1;
300
Jason Wessel5352ae62010-01-28 17:04:43 -0600301 if (hw_break_release_slot(i)) {
302 printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
303 return -1;
304 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600305 breakinfo[i].enabled = 0;
306
307 return 0;
308}
309
310static void kgdb_remove_all_hw_break(void)
311{
312 int i;
Jason Wesselcc096742010-01-28 17:04:42 -0600313 int cpu = raw_smp_processor_id();
314 struct perf_event *bp;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600315
Dongdong Dengdf493932010-08-05 09:22:25 -0500316 for (i = 0; i < HBP_NUM; i++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600317 if (!breakinfo[i].enabled)
318 continue;
319 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
Jason Wessel10a6e672010-11-15 08:07:35 -0600320 if (!bp->attr.disabled) {
321 arch_uninstall_hw_breakpoint(bp);
322 bp->attr.disabled = 1;
Jason Wesselcc096742010-01-28 17:04:42 -0600323 continue;
Jason Wessel10a6e672010-11-15 08:07:35 -0600324 }
Jason Wessel031acd82010-05-20 21:04:30 -0500325 if (dbg_is_early)
326 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
327 breakinfo[i].type);
Jason Wessel10a6e672010-11-15 08:07:35 -0600328 else if (hw_break_release_slot(i))
329 printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n",
330 breakinfo[i].addr);
331 breakinfo[i].enabled = 0;
Jason Wesselcc096742010-01-28 17:04:42 -0600332 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600333}
334
335static int
336kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
337{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600338 int i;
339
Dongdong Dengdf493932010-08-05 09:22:25 -0500340 for (i = 0; i < HBP_NUM; i++)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600341 if (!breakinfo[i].enabled)
342 break;
Dongdong Dengdf493932010-08-05 09:22:25 -0500343 if (i == HBP_NUM)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600344 return -1;
345
346 switch (bptype) {
347 case BP_HARDWARE_BREAKPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600348 len = 1;
349 breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600350 break;
351 case BP_WRITE_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600352 breakinfo[i].type = X86_BREAKPOINT_WRITE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600353 break;
354 case BP_ACCESS_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600355 breakinfo[i].type = X86_BREAKPOINT_RW;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600356 break;
357 default:
358 return -1;
359 }
Jason Wesselcc096742010-01-28 17:04:42 -0600360 switch (len) {
361 case 1:
362 breakinfo[i].len = X86_BREAKPOINT_LEN_1;
363 break;
364 case 2:
365 breakinfo[i].len = X86_BREAKPOINT_LEN_2;
366 break;
367 case 4:
368 breakinfo[i].len = X86_BREAKPOINT_LEN_4;
369 break;
370#ifdef CONFIG_X86_64
371 case 8:
372 breakinfo[i].len = X86_BREAKPOINT_LEN_8;
373 break;
374#endif
375 default:
Jason Wessel64e9ee32008-02-15 14:55:56 -0600376 return -1;
Jason Wesselcc096742010-01-28 17:04:42 -0600377 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600378 breakinfo[i].addr = addr;
Jason Wessel5352ae62010-01-28 17:04:43 -0600379 if (hw_break_reserve_slot(i)) {
380 breakinfo[i].addr = 0;
381 return -1;
382 }
Jason Wesselcc096742010-01-28 17:04:42 -0600383 breakinfo[i].enabled = 1;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600384
385 return 0;
386}
387
388/**
389 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
390 * @regs: Current &struct pt_regs.
391 *
392 * This function will be called if the particular architecture must
393 * disable hardware debugging while it is processing gdb packets or
394 * handling exception.
395 */
Dongdong Dengd7ba979d2010-08-18 06:02:00 -0500396static void kgdb_disable_hw_debug(struct pt_regs *regs)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600397{
Jason Wesselcc096742010-01-28 17:04:42 -0600398 int i;
399 int cpu = raw_smp_processor_id();
400 struct perf_event *bp;
401
Jason Wessel64e9ee32008-02-15 14:55:56 -0600402 /* Disable hardware debugging while we are in kgdb: */
403 set_debugreg(0UL, 7);
Dongdong Dengdf493932010-08-05 09:22:25 -0500404 for (i = 0; i < HBP_NUM; i++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600405 if (!breakinfo[i].enabled)
406 continue;
Jason Wessel031acd82010-05-20 21:04:30 -0500407 if (dbg_is_early) {
408 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
409 breakinfo[i].type);
410 continue;
411 }
Jason Wesselcc096742010-01-28 17:04:42 -0600412 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
413 if (bp->attr.disabled == 1)
414 continue;
415 arch_uninstall_hw_breakpoint(bp);
416 bp->attr.disabled = 1;
417 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600418}
419
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200420#ifdef CONFIG_SMP
421/**
422 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
423 * @flags: Current IRQ state
424 *
425 * On SMP systems, we need to get the attention of the other CPUs
426 * and get them be in a known state. This should do what is needed
427 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
428 * the NMI approach is not used for rounding up all the CPUs. For example,
429 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
430 * this case, we have to make sure that interrupts are enabled before
431 * calling smp_call_function(). The argument to this function is
432 * the flags that will be used when restoring the interrupts. There is
433 * local_irq_save() call before kgdb_roundup_cpus().
434 *
435 * On non-SMP systems, this is not called.
436 */
437void kgdb_roundup_cpus(unsigned long flags)
438{
Ingo Molnardac5f412009-01-28 15:42:24 +0100439 apic->send_IPI_allbutself(APIC_DM_NMI);
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200440}
441#endif
442
443/**
444 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
445 * @vector: The error vector of the exception that happened.
446 * @signo: The signal number of the exception that happened.
447 * @err_code: The error code of the exception that happened.
448 * @remcom_in_buffer: The buffer of the packet we have read.
449 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
450 * @regs: The &struct pt_regs of the current process.
451 *
452 * This function MUST handle the 'c' and 's' command packets,
453 * as well packets to set / remove a hardware breakpoint, if used.
454 * If there are additional packets which the hardware needs to handle,
455 * they are handled here. The code should return -1 if it wants to
456 * process more packets, and a %0 or %1 if it wants to exit from the
457 * kgdb callback.
458 */
459int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
460 char *remcomInBuffer, char *remcomOutBuffer,
461 struct pt_regs *linux_regs)
462{
463 unsigned long addr;
464 char *ptr;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200465
466 switch (remcomInBuffer[0]) {
467 case 'c':
468 case 's':
469 /* try to read optional parameter, pc unchanged if no parm */
470 ptr = &remcomInBuffer[1];
471 if (kgdb_hex2long(&ptr, &addr))
472 linux_regs->ip = addr;
Jason Wessel737a4602008-03-07 16:34:16 -0600473 case 'D':
474 case 'k':
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200475 /* clear the trace bit */
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700476 linux_regs->flags &= ~X86_EFLAGS_TF;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200477 atomic_set(&kgdb_cpu_doing_single_step, -1);
478
479 /* set the trace bit if we're stepping */
480 if (remcomInBuffer[0] == 's') {
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700481 linux_regs->flags |= X86_EFLAGS_TF;
Jason Wesseld7161a62008-09-26 10:36:41 -0500482 atomic_set(&kgdb_cpu_doing_single_step,
483 raw_smp_processor_id());
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200484 }
485
486 return 0;
487 }
488
489 /* this means that we do not want to exit from the handler: */
490 return -1;
491}
492
493static inline int
494single_step_cont(struct pt_regs *regs, struct die_args *args)
495{
496 /*
497 * Single step exception from kernel space to user space so
498 * eat the exception and continue the process:
499 */
500 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
501 "resuming...\n");
502 kgdb_arch_handle_exception(args->trapnr, args->signr,
503 args->err, "c", "", regs);
K.Prasad62edab92009-06-01 23:47:06 +0530504 /*
505 * Reset the BS bit in dr6 (pointed by args->err) to
506 * denote completion of processing
507 */
508 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200509
510 return NOTIFY_STOP;
511}
512
Jason Wesseld3597522008-02-15 14:55:53 -0600513static int was_in_debug_nmi[NR_CPUS];
514
Don Zickus9c48f1c2011-09-30 15:06:21 -0400515static int kgdb_nmi_handler(unsigned int cmd, struct pt_regs *regs)
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200516{
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200517 switch (cmd) {
Don Zickus9c48f1c2011-09-30 15:06:21 -0400518 case NMI_LOCAL:
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200519 if (atomic_read(&kgdb_active) != -1) {
520 /* KGDB CPU roundup */
521 kgdb_nmicallback(raw_smp_processor_id(), regs);
Jason Wesseld3597522008-02-15 14:55:53 -0600522 was_in_debug_nmi[raw_smp_processor_id()] = 1;
523 touch_nmi_watchdog();
Don Zickus9c48f1c2011-09-30 15:06:21 -0400524 return NMI_HANDLED;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200525 }
Don Zickus9c48f1c2011-09-30 15:06:21 -0400526 break;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200527
Don Zickus9c48f1c2011-09-30 15:06:21 -0400528 case NMI_UNKNOWN:
Jason Wesseld3597522008-02-15 14:55:53 -0600529 if (was_in_debug_nmi[raw_smp_processor_id()]) {
530 was_in_debug_nmi[raw_smp_processor_id()] = 0;
Don Zickus9c48f1c2011-09-30 15:06:21 -0400531 return NMI_HANDLED;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200532 }
Don Zickus9c48f1c2011-09-30 15:06:21 -0400533 break;
534 default:
535 /* do nothing */
536 break;
537 }
538 return NMI_DONE;
539}
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200540
Don Zickus9c48f1c2011-09-30 15:06:21 -0400541static int __kgdb_notify(struct die_args *args, unsigned long cmd)
542{
543 struct pt_regs *regs = args->regs;
544
545 switch (cmd) {
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200546 case DIE_DEBUG:
Jason Wesselcc096742010-01-28 17:04:42 -0600547 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
Jason Wesseld7161a62008-09-26 10:36:41 -0500548 if (user_mode(regs))
549 return single_step_cont(regs, args);
550 break;
551 } else if (test_thread_flag(TIF_SINGLESTEP))
552 /* This means a user thread is single stepping
553 * a system call which should be ignored
554 */
555 return NOTIFY_DONE;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200556 /* fall through */
557 default:
558 if (user_mode(regs))
559 return NOTIFY_DONE;
560 }
561
Jason Wesself503b5a2010-05-20 21:04:25 -0500562 if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200563 return NOTIFY_DONE;
564
Jason Wessel737a4602008-03-07 16:34:16 -0600565 /* Must touch watchdog before return to normal operation */
566 touch_nmi_watchdog();
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200567 return NOTIFY_STOP;
568}
569
Jason Wesself503b5a2010-05-20 21:04:25 -0500570int kgdb_ll_trap(int cmd, const char *str,
571 struct pt_regs *regs, long err, int trap, int sig)
572{
573 struct die_args args = {
574 .regs = regs,
575 .str = str,
576 .err = err,
577 .trapnr = trap,
578 .signr = sig,
579
580 };
581
582 if (!kgdb_io_module_registered)
583 return NOTIFY_DONE;
584
585 return __kgdb_notify(&args, cmd);
586}
Jason Wesself503b5a2010-05-20 21:04:25 -0500587
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200588static int
589kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
590{
591 unsigned long flags;
592 int ret;
593
594 local_irq_save(flags);
595 ret = __kgdb_notify(ptr, cmd);
596 local_irq_restore(flags);
597
598 return ret;
599}
600
601static struct notifier_block kgdb_notifier = {
602 .notifier_call = kgdb_notify,
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200603};
604
605/**
606 * kgdb_arch_init - Perform any architecture specific initalization.
607 *
608 * This function will handle the initalization of any architecture
609 * specific callbacks.
610 */
611int kgdb_arch_init(void)
612{
Don Zickus9c48f1c2011-09-30 15:06:21 -0400613 int retval;
614
615 retval = register_die_notifier(&kgdb_notifier);
616 if (retval)
617 goto out;
618
619 retval = register_nmi_handler(NMI_LOCAL, kgdb_nmi_handler,
620 0, "kgdb");
621 if (retval)
622 goto out1;
623
624 retval = register_nmi_handler(NMI_UNKNOWN, kgdb_nmi_handler,
625 0, "kgdb");
626
627 if (retval)
628 goto out2;
629
630 return retval;
631
632out2:
633 unregister_nmi_handler(NMI_LOCAL, "kgdb");
634out1:
635 unregister_die_notifier(&kgdb_notifier);
636out:
637 return retval;
Jason Wessel0b4b3822010-05-20 21:04:29 -0500638}
639
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200640static void kgdb_hw_overflow_handler(struct perf_event *event,
Jason Wesselba773f72010-07-28 19:10:30 -0500641 struct perf_sample_data *data, struct pt_regs *regs)
642{
Jason Wesselfad99fa2010-10-20 08:20:00 -0500643 struct task_struct *tsk = current;
644 int i;
645
646 for (i = 0; i < 4; i++)
647 if (breakinfo[i].enabled)
648 tsk->thread.debugreg6 |= (DR_TRAP0 << i);
Jason Wesselba773f72010-07-28 19:10:30 -0500649}
650
Jason Wessel0b4b3822010-05-20 21:04:29 -0500651void kgdb_arch_late(void)
652{
Jason Wesselcc096742010-01-28 17:04:42 -0600653 int i, cpu;
Jason Wesselcc096742010-01-28 17:04:42 -0600654 struct perf_event_attr attr;
655 struct perf_event **pevent;
656
Jason Wesselcc096742010-01-28 17:04:42 -0600657 /*
658 * Pre-allocate the hw breakpoint structions in the non-atomic
659 * portion of kgdb because this operation requires mutexs to
660 * complete.
661 */
Jason Wesselab310b52010-03-30 14:05:07 -0500662 hw_breakpoint_init(&attr);
Jason Wesselcc096742010-01-28 17:04:42 -0600663 attr.bp_addr = (unsigned long)kgdb_arch_init;
Jason Wesselcc096742010-01-28 17:04:42 -0600664 attr.bp_len = HW_BREAKPOINT_LEN_1;
665 attr.bp_type = HW_BREAKPOINT_W;
666 attr.disabled = 1;
Dongdong Dengdf493932010-08-05 09:22:25 -0500667 for (i = 0; i < HBP_NUM; i++) {
Jason Wessel0b4b3822010-05-20 21:04:29 -0500668 if (breakinfo[i].pev)
669 continue;
Avi Kivity4dc0da82011-06-29 18:42:35 +0300670 breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
Jason Wessel91b152a2010-08-23 09:20:14 -0500671 if (IS_ERR((void * __force)breakinfo[i].pev)) {
Jason Wessel0b4b3822010-05-20 21:04:29 -0500672 printk(KERN_ERR "kgdb: Could not allocate hw"
673 "breakpoints\nDisabling the kernel debugger\n");
Jason Wesselcc096742010-01-28 17:04:42 -0600674 breakinfo[i].pev = NULL;
675 kgdb_arch_exit();
Jason Wessel0b4b3822010-05-20 21:04:29 -0500676 return;
Jason Wesselcc096742010-01-28 17:04:42 -0600677 }
678 for_each_online_cpu(cpu) {
679 pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
680 pevent[0]->hw.sample_period = 1;
Jason Wesselba773f72010-07-28 19:10:30 -0500681 pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
Jason Wesselcc096742010-01-28 17:04:42 -0600682 if (pevent[0]->destroy != NULL) {
683 pevent[0]->destroy = NULL;
684 release_bp_slot(*pevent);
685 }
686 }
687 }
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200688}
689
690/**
691 * kgdb_arch_exit - Perform any architecture specific uninitalization.
692 *
693 * This function will handle the uninitalization of any architecture
694 * specific callbacks, for dynamic registration and unregistration.
695 */
696void kgdb_arch_exit(void)
697{
Jason Wesselcc096742010-01-28 17:04:42 -0600698 int i;
699 for (i = 0; i < 4; i++) {
700 if (breakinfo[i].pev) {
701 unregister_wide_hw_breakpoint(breakinfo[i].pev);
702 breakinfo[i].pev = NULL;
703 }
704 }
Don Zickus9c48f1c2011-09-30 15:06:21 -0400705 unregister_nmi_handler(NMI_UNKNOWN, "kgdb");
706 unregister_nmi_handler(NMI_LOCAL, "kgdb");
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200707 unregister_die_notifier(&kgdb_notifier);
708}
709
710/**
711 *
712 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
713 * @exception: Exception vector number
714 * @regs: Current &struct pt_regs.
715 *
716 * On some architectures we need to skip a breakpoint exception when
717 * it occurs after a breakpoint has been removed.
718 *
719 * Skip an int3 exception when it occurs after a breakpoint has been
720 * removed. Backtrack eip by 1 since the int3 would have caused it to
721 * increment by 1.
722 */
723int kgdb_skipexception(int exception, struct pt_regs *regs)
724{
725 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
726 regs->ip -= 1;
727 return 1;
728 }
729 return 0;
730}
731
732unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
733{
734 if (exception == 3)
735 return instruction_pointer(regs) - 1;
736 return instruction_pointer(regs);
737}
738
Jason Wesseldcc78712010-05-20 21:04:21 -0500739void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
740{
741 regs->ip = ip;
742}
743
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200744struct kgdb_arch arch_kgdb_ops = {
745 /* Breakpoint instruction: */
746 .gdb_bpt_instr = { 0xcc },
Jason Wessel64e9ee32008-02-15 14:55:56 -0600747 .flags = KGDB_HW_BREAKPOINT,
748 .set_hw_breakpoint = kgdb_set_hw_break,
749 .remove_hw_breakpoint = kgdb_remove_hw_break,
Dongdong Dengd7ba979d2010-08-18 06:02:00 -0500750 .disable_hw_break = kgdb_disable_hw_debug,
Jason Wessel64e9ee32008-02-15 14:55:56 -0600751 .remove_all_hw_break = kgdb_remove_all_hw_break,
752 .correct_hw_break = kgdb_correct_hw_break,
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200753};