blob: cd21b654dec6c70382b68ab29f4364ab60ff4e88 [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>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010050#include <asm/apic.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) },
69 { "fs", 4, -1 },
70 { "gs", 4, -1 },
Ingo Molnar82da3ff2008-04-17 20:05:37 +020071#else
Jason Wessel12bfa3d2010-08-05 09:22:20 -050072 { "ax", 8, offsetof(struct pt_regs, ax) },
73 { "bx", 8, offsetof(struct pt_regs, bx) },
74 { "cx", 8, offsetof(struct pt_regs, cx) },
75 { "dx", 8, offsetof(struct pt_regs, dx) },
76 { "si", 8, offsetof(struct pt_regs, dx) },
77 { "di", 8, offsetof(struct pt_regs, di) },
78 { "bp", 8, offsetof(struct pt_regs, bp) },
79 { "sp", 8, offsetof(struct pt_regs, sp) },
80 { "r8", 8, offsetof(struct pt_regs, r8) },
81 { "r9", 8, offsetof(struct pt_regs, r9) },
82 { "r10", 8, offsetof(struct pt_regs, r10) },
83 { "r11", 8, offsetof(struct pt_regs, r11) },
84 { "r12", 8, offsetof(struct pt_regs, r12) },
85 { "r13", 8, offsetof(struct pt_regs, r13) },
86 { "r14", 8, offsetof(struct pt_regs, r14) },
87 { "r15", 8, offsetof(struct pt_regs, r15) },
88 { "ip", 8, offsetof(struct pt_regs, ip) },
89 { "flags", 4, offsetof(struct pt_regs, flags) },
90 { "cs", 4, offsetof(struct pt_regs, cs) },
91 { "ss", 4, offsetof(struct pt_regs, ss) },
Jason Wesselcf6f1962009-12-11 08:43:16 -060092#endif
Jason Wessel12bfa3d2010-08-05 09:22:20 -050093};
94
95int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
96{
97 if (
98#ifdef CONFIG_X86_32
99 regno == GDB_SS || regno == GDB_FS || regno == GDB_GS ||
100#endif
101 regno == GDB_SP || regno == GDB_ORIG_AX)
102 return 0;
103
104 if (dbg_reg_def[regno].offset != -1)
105 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
106 dbg_reg_def[regno].size);
107 return 0;
108}
109
110char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
111{
112 if (regno == GDB_ORIG_AX) {
113 memcpy(mem, &regs->orig_ax, sizeof(regs->orig_ax));
114 return "orig_ax";
115 }
116 if (regno >= DBG_MAX_REG_NUM || regno < 0)
117 return NULL;
118
119 if (dbg_reg_def[regno].offset != -1)
120 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
121 dbg_reg_def[regno].size);
122
123 switch (regno) {
124#ifdef CONFIG_X86_32
125 case GDB_SS:
126 if (!user_mode_vm(regs))
127 *(unsigned long *)mem = __KERNEL_DS;
128 break;
129 case GDB_SP:
130 if (!user_mode_vm(regs))
131 *(unsigned long *)mem = kernel_stack_pointer(regs);
132 break;
133 case GDB_GS:
134 case GDB_FS:
135 *(unsigned long *)mem = 0xFFFF;
136 break;
137#endif
138 }
139 return dbg_reg_def[regno].name;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200140}
141
142/**
143 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
144 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
145 * @p: The &struct task_struct of the desired process.
146 *
147 * Convert the register values of the sleeping process in @p to
148 * the format that GDB expects.
149 * This function is called when kgdb does not have access to the
150 * &struct pt_regs and therefore it should fill the gdb registers
151 * @gdb_regs with what has been saved in &struct thread_struct
152 * thread field during switch_to.
153 */
154void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
155{
Jason Wessel703a1ed2008-09-26 10:36:42 -0500156#ifndef CONFIG_X86_32
157 u32 *gdb_regs32 = (u32 *)gdb_regs;
158#endif
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200159 gdb_regs[GDB_AX] = 0;
160 gdb_regs[GDB_BX] = 0;
161 gdb_regs[GDB_CX] = 0;
162 gdb_regs[GDB_DX] = 0;
163 gdb_regs[GDB_SI] = 0;
164 gdb_regs[GDB_DI] = 0;
165 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
166#ifdef CONFIG_X86_32
167 gdb_regs[GDB_DS] = __KERNEL_DS;
168 gdb_regs[GDB_ES] = __KERNEL_DS;
169 gdb_regs[GDB_PS] = 0;
170 gdb_regs[GDB_CS] = __KERNEL_CS;
171 gdb_regs[GDB_PC] = p->thread.ip;
172 gdb_regs[GDB_SS] = __KERNEL_DS;
173 gdb_regs[GDB_FS] = 0xFFFF;
174 gdb_regs[GDB_GS] = 0xFFFF;
175#else
Jason Wessel703a1ed2008-09-26 10:36:42 -0500176 gdb_regs32[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
177 gdb_regs32[GDB_CS] = __KERNEL_CS;
178 gdb_regs32[GDB_SS] = __KERNEL_DS;
Alexey Dobriyan0c235902009-05-04 03:30:15 +0400179 gdb_regs[GDB_PC] = 0;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200180 gdb_regs[GDB_R8] = 0;
181 gdb_regs[GDB_R9] = 0;
182 gdb_regs[GDB_R10] = 0;
183 gdb_regs[GDB_R11] = 0;
184 gdb_regs[GDB_R12] = 0;
185 gdb_regs[GDB_R13] = 0;
186 gdb_regs[GDB_R14] = 0;
187 gdb_regs[GDB_R15] = 0;
188#endif
189 gdb_regs[GDB_SP] = p->thread.sp;
190}
191
Jason Wessel64e9ee32008-02-15 14:55:56 -0600192static struct hw_breakpoint {
193 unsigned enabled;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600194 unsigned long addr;
Jason Wesselcc096742010-01-28 17:04:42 -0600195 int len;
196 int type;
Namhyung Kim8c8aefc2010-08-07 11:00:59 -0700197 struct perf_event * __percpu *pev;
Dongdong Dengdf493932010-08-05 09:22:25 -0500198} breakinfo[HBP_NUM];
Jason Wessel64e9ee32008-02-15 14:55:56 -0600199
Jason Wessel031acd82010-05-20 21:04:30 -0500200static unsigned long early_dr7;
201
Jason Wessel64e9ee32008-02-15 14:55:56 -0600202static void kgdb_correct_hw_break(void)
203{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600204 int breakno;
205
Dongdong Dengdf493932010-08-05 09:22:25 -0500206 for (breakno = 0; breakno < HBP_NUM; breakno++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600207 struct perf_event *bp;
208 struct arch_hw_breakpoint *info;
209 int val;
210 int cpu = raw_smp_processor_id();
211 if (!breakinfo[breakno].enabled)
212 continue;
Jason Wessel031acd82010-05-20 21:04:30 -0500213 if (dbg_is_early) {
214 set_debugreg(breakinfo[breakno].addr, breakno);
215 early_dr7 |= encode_dr7(breakno,
216 breakinfo[breakno].len,
217 breakinfo[breakno].type);
218 set_debugreg(early_dr7, 7);
219 continue;
220 }
Jason Wesselcc096742010-01-28 17:04:42 -0600221 bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
222 info = counter_arch_bp(bp);
223 if (bp->attr.disabled != 1)
224 continue;
225 bp->attr.bp_addr = breakinfo[breakno].addr;
226 bp->attr.bp_len = breakinfo[breakno].len;
227 bp->attr.bp_type = breakinfo[breakno].type;
228 info->address = breakinfo[breakno].addr;
229 info->len = breakinfo[breakno].len;
230 info->type = breakinfo[breakno].type;
231 val = arch_install_hw_breakpoint(bp);
232 if (!val)
233 bp->attr.disabled = 0;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600234 }
Jason Wessel031acd82010-05-20 21:04:30 -0500235 if (!dbg_is_early)
236 hw_breakpoint_restore();
Jason Wessel64e9ee32008-02-15 14:55:56 -0600237}
238
Jason Wessel5352ae62010-01-28 17:04:43 -0600239static int hw_break_reserve_slot(int breakno)
240{
241 int cpu;
242 int cnt = 0;
243 struct perf_event **pevent;
244
Jason Wessel031acd82010-05-20 21:04:30 -0500245 if (dbg_is_early)
246 return 0;
247
Jason Wessel5352ae62010-01-28 17:04:43 -0600248 for_each_online_cpu(cpu) {
249 cnt++;
250 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
251 if (dbg_reserve_bp_slot(*pevent))
252 goto fail;
253 }
254
255 return 0;
256
257fail:
258 for_each_online_cpu(cpu) {
259 cnt--;
260 if (!cnt)
261 break;
262 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
263 dbg_release_bp_slot(*pevent);
264 }
265 return -1;
266}
267
268static int hw_break_release_slot(int breakno)
269{
270 struct perf_event **pevent;
271 int cpu;
272
Jason Wessel031acd82010-05-20 21:04:30 -0500273 if (dbg_is_early)
274 return 0;
275
Jason Wessel5352ae62010-01-28 17:04:43 -0600276 for_each_online_cpu(cpu) {
277 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
278 if (dbg_release_bp_slot(*pevent))
279 /*
280 * The debugger is responisble for handing the retry on
281 * remove failure.
282 */
283 return -1;
284 }
285 return 0;
286}
287
Jason Wessel64e9ee32008-02-15 14:55:56 -0600288static int
289kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
290{
291 int i;
292
Dongdong Dengdf493932010-08-05 09:22:25 -0500293 for (i = 0; i < HBP_NUM; i++)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600294 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
295 break;
Dongdong Dengdf493932010-08-05 09:22:25 -0500296 if (i == HBP_NUM)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600297 return -1;
298
Jason Wessel5352ae62010-01-28 17:04:43 -0600299 if (hw_break_release_slot(i)) {
300 printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
301 return -1;
302 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600303 breakinfo[i].enabled = 0;
304
305 return 0;
306}
307
308static void kgdb_remove_all_hw_break(void)
309{
310 int i;
Jason Wesselcc096742010-01-28 17:04:42 -0600311 int cpu = raw_smp_processor_id();
312 struct perf_event *bp;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600313
Dongdong Dengdf493932010-08-05 09:22:25 -0500314 for (i = 0; i < HBP_NUM; i++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600315 if (!breakinfo[i].enabled)
316 continue;
317 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
Jason Wessel10a6e672010-11-15 08:07:35 -0600318 if (!bp->attr.disabled) {
319 arch_uninstall_hw_breakpoint(bp);
320 bp->attr.disabled = 1;
Jason Wesselcc096742010-01-28 17:04:42 -0600321 continue;
Jason Wessel10a6e672010-11-15 08:07:35 -0600322 }
Jason Wessel031acd82010-05-20 21:04:30 -0500323 if (dbg_is_early)
324 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
325 breakinfo[i].type);
Jason Wessel10a6e672010-11-15 08:07:35 -0600326 else if (hw_break_release_slot(i))
327 printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n",
328 breakinfo[i].addr);
329 breakinfo[i].enabled = 0;
Jason Wesselcc096742010-01-28 17:04:42 -0600330 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600331}
332
333static int
334kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
335{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600336 int i;
337
Dongdong Dengdf493932010-08-05 09:22:25 -0500338 for (i = 0; i < HBP_NUM; i++)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600339 if (!breakinfo[i].enabled)
340 break;
Dongdong Dengdf493932010-08-05 09:22:25 -0500341 if (i == HBP_NUM)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600342 return -1;
343
344 switch (bptype) {
345 case BP_HARDWARE_BREAKPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600346 len = 1;
347 breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600348 break;
349 case BP_WRITE_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600350 breakinfo[i].type = X86_BREAKPOINT_WRITE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600351 break;
352 case BP_ACCESS_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600353 breakinfo[i].type = X86_BREAKPOINT_RW;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600354 break;
355 default:
356 return -1;
357 }
Jason Wesselcc096742010-01-28 17:04:42 -0600358 switch (len) {
359 case 1:
360 breakinfo[i].len = X86_BREAKPOINT_LEN_1;
361 break;
362 case 2:
363 breakinfo[i].len = X86_BREAKPOINT_LEN_2;
364 break;
365 case 4:
366 breakinfo[i].len = X86_BREAKPOINT_LEN_4;
367 break;
368#ifdef CONFIG_X86_64
369 case 8:
370 breakinfo[i].len = X86_BREAKPOINT_LEN_8;
371 break;
372#endif
373 default:
Jason Wessel64e9ee32008-02-15 14:55:56 -0600374 return -1;
Jason Wesselcc096742010-01-28 17:04:42 -0600375 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600376 breakinfo[i].addr = addr;
Jason Wessel5352ae62010-01-28 17:04:43 -0600377 if (hw_break_reserve_slot(i)) {
378 breakinfo[i].addr = 0;
379 return -1;
380 }
Jason Wesselcc096742010-01-28 17:04:42 -0600381 breakinfo[i].enabled = 1;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600382
383 return 0;
384}
385
386/**
387 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
388 * @regs: Current &struct pt_regs.
389 *
390 * This function will be called if the particular architecture must
391 * disable hardware debugging while it is processing gdb packets or
392 * handling exception.
393 */
Dongdong Dengd7ba979d2010-08-18 06:02:00 -0500394static void kgdb_disable_hw_debug(struct pt_regs *regs)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600395{
Jason Wesselcc096742010-01-28 17:04:42 -0600396 int i;
397 int cpu = raw_smp_processor_id();
398 struct perf_event *bp;
399
Jason Wessel64e9ee32008-02-15 14:55:56 -0600400 /* Disable hardware debugging while we are in kgdb: */
401 set_debugreg(0UL, 7);
Dongdong Dengdf493932010-08-05 09:22:25 -0500402 for (i = 0; i < HBP_NUM; i++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600403 if (!breakinfo[i].enabled)
404 continue;
Jason Wessel031acd82010-05-20 21:04:30 -0500405 if (dbg_is_early) {
406 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
407 breakinfo[i].type);
408 continue;
409 }
Jason Wesselcc096742010-01-28 17:04:42 -0600410 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
411 if (bp->attr.disabled == 1)
412 continue;
413 arch_uninstall_hw_breakpoint(bp);
414 bp->attr.disabled = 1;
415 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600416}
417
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200418#ifdef CONFIG_SMP
419/**
420 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
421 * @flags: Current IRQ state
422 *
423 * On SMP systems, we need to get the attention of the other CPUs
424 * and get them be in a known state. This should do what is needed
425 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
426 * the NMI approach is not used for rounding up all the CPUs. For example,
427 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
428 * this case, we have to make sure that interrupts are enabled before
429 * calling smp_call_function(). The argument to this function is
430 * the flags that will be used when restoring the interrupts. There is
431 * local_irq_save() call before kgdb_roundup_cpus().
432 *
433 * On non-SMP systems, this is not called.
434 */
435void kgdb_roundup_cpus(unsigned long flags)
436{
Ingo Molnardac5f412009-01-28 15:42:24 +0100437 apic->send_IPI_allbutself(APIC_DM_NMI);
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200438}
439#endif
440
441/**
442 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
443 * @vector: The error vector of the exception that happened.
444 * @signo: The signal number of the exception that happened.
445 * @err_code: The error code of the exception that happened.
446 * @remcom_in_buffer: The buffer of the packet we have read.
447 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
448 * @regs: The &struct pt_regs of the current process.
449 *
450 * This function MUST handle the 'c' and 's' command packets,
451 * as well packets to set / remove a hardware breakpoint, if used.
452 * If there are additional packets which the hardware needs to handle,
453 * they are handled here. The code should return -1 if it wants to
454 * process more packets, and a %0 or %1 if it wants to exit from the
455 * kgdb callback.
456 */
457int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
458 char *remcomInBuffer, char *remcomOutBuffer,
459 struct pt_regs *linux_regs)
460{
461 unsigned long addr;
462 char *ptr;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200463
464 switch (remcomInBuffer[0]) {
465 case 'c':
466 case 's':
467 /* try to read optional parameter, pc unchanged if no parm */
468 ptr = &remcomInBuffer[1];
469 if (kgdb_hex2long(&ptr, &addr))
470 linux_regs->ip = addr;
Jason Wessel737a4602008-03-07 16:34:16 -0600471 case 'D':
472 case 'k':
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200473 /* clear the trace bit */
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700474 linux_regs->flags &= ~X86_EFLAGS_TF;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200475 atomic_set(&kgdb_cpu_doing_single_step, -1);
476
477 /* set the trace bit if we're stepping */
478 if (remcomInBuffer[0] == 's') {
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700479 linux_regs->flags |= X86_EFLAGS_TF;
Jason Wesseld7161a62008-09-26 10:36:41 -0500480 atomic_set(&kgdb_cpu_doing_single_step,
481 raw_smp_processor_id());
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200482 }
483
484 return 0;
485 }
486
487 /* this means that we do not want to exit from the handler: */
488 return -1;
489}
490
491static inline int
492single_step_cont(struct pt_regs *regs, struct die_args *args)
493{
494 /*
495 * Single step exception from kernel space to user space so
496 * eat the exception and continue the process:
497 */
498 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
499 "resuming...\n");
500 kgdb_arch_handle_exception(args->trapnr, args->signr,
501 args->err, "c", "", regs);
K.Prasad62edab92009-06-01 23:47:06 +0530502 /*
503 * Reset the BS bit in dr6 (pointed by args->err) to
504 * denote completion of processing
505 */
506 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200507
508 return NOTIFY_STOP;
509}
510
Jason Wesseld3597522008-02-15 14:55:53 -0600511static int was_in_debug_nmi[NR_CPUS];
512
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200513static int __kgdb_notify(struct die_args *args, unsigned long cmd)
514{
515 struct pt_regs *regs = args->regs;
516
517 switch (cmd) {
518 case DIE_NMI:
519 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();
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200524 return NOTIFY_STOP;
525 }
526 return NOTIFY_DONE;
527
528 case DIE_NMI_IPI:
Jan Kiszkae85ceae2008-10-06 13:50:59 -0500529 /* Just ignore, we will handle the roundup on DIE_NMI. */
Jason Wesseld3597522008-02-15 14:55:53 -0600530 return NOTIFY_DONE;
531
532 case DIE_NMIUNKNOWN:
533 if (was_in_debug_nmi[raw_smp_processor_id()]) {
534 was_in_debug_nmi[raw_smp_processor_id()] = 0;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200535 return NOTIFY_STOP;
536 }
537 return NOTIFY_DONE;
538
539 case DIE_NMIWATCHDOG:
540 if (atomic_read(&kgdb_active) != -1) {
541 /* KGDB CPU roundup: */
542 kgdb_nmicallback(raw_smp_processor_id(), regs);
543 return NOTIFY_STOP;
544 }
545 /* Enter debugger: */
546 break;
547
548 case DIE_DEBUG:
Jason Wesselcc096742010-01-28 17:04:42 -0600549 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
Jason Wesseld7161a62008-09-26 10:36:41 -0500550 if (user_mode(regs))
551 return single_step_cont(regs, args);
552 break;
553 } else if (test_thread_flag(TIF_SINGLESTEP))
554 /* This means a user thread is single stepping
555 * a system call which should be ignored
556 */
557 return NOTIFY_DONE;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200558 /* fall through */
559 default:
560 if (user_mode(regs))
561 return NOTIFY_DONE;
562 }
563
Jason Wesself503b5a2010-05-20 21:04:25 -0500564 if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200565 return NOTIFY_DONE;
566
Jason Wessel737a4602008-03-07 16:34:16 -0600567 /* Must touch watchdog before return to normal operation */
568 touch_nmi_watchdog();
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200569 return NOTIFY_STOP;
570}
571
Jason Wesself503b5a2010-05-20 21:04:25 -0500572int kgdb_ll_trap(int cmd, const char *str,
573 struct pt_regs *regs, long err, int trap, int sig)
574{
575 struct die_args args = {
576 .regs = regs,
577 .str = str,
578 .err = err,
579 .trapnr = trap,
580 .signr = sig,
581
582 };
583
584 if (!kgdb_io_module_registered)
585 return NOTIFY_DONE;
586
587 return __kgdb_notify(&args, cmd);
588}
Jason Wesself503b5a2010-05-20 21:04:25 -0500589
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200590static int
591kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
592{
593 unsigned long flags;
594 int ret;
595
596 local_irq_save(flags);
597 ret = __kgdb_notify(ptr, cmd);
598 local_irq_restore(flags);
599
600 return ret;
601}
602
603static struct notifier_block kgdb_notifier = {
604 .notifier_call = kgdb_notify,
605
606 /*
607 * Lowest-prio notifier priority, we want to be notified last:
608 */
609 .priority = -INT_MAX,
610};
611
612/**
613 * kgdb_arch_init - Perform any architecture specific initalization.
614 *
615 * This function will handle the initalization of any architecture
616 * specific callbacks.
617 */
618int kgdb_arch_init(void)
619{
Jason Wessel0b4b3822010-05-20 21:04:29 -0500620 return register_die_notifier(&kgdb_notifier);
621}
622
Jason Wesselba773f72010-07-28 19:10:30 -0500623static void kgdb_hw_overflow_handler(struct perf_event *event, int nmi,
624 struct perf_sample_data *data, struct pt_regs *regs)
625{
Jason Wesselfad99fa2010-10-20 08:20:00 -0500626 struct task_struct *tsk = current;
627 int i;
628
629 for (i = 0; i < 4; i++)
630 if (breakinfo[i].enabled)
631 tsk->thread.debugreg6 |= (DR_TRAP0 << i);
Jason Wesselba773f72010-07-28 19:10:30 -0500632}
633
Jason Wessel0b4b3822010-05-20 21:04:29 -0500634void kgdb_arch_late(void)
635{
Jason Wesselcc096742010-01-28 17:04:42 -0600636 int i, cpu;
Jason Wesselcc096742010-01-28 17:04:42 -0600637 struct perf_event_attr attr;
638 struct perf_event **pevent;
639
Jason Wesselcc096742010-01-28 17:04:42 -0600640 /*
641 * Pre-allocate the hw breakpoint structions in the non-atomic
642 * portion of kgdb because this operation requires mutexs to
643 * complete.
644 */
Jason Wesselab310b52010-03-30 14:05:07 -0500645 hw_breakpoint_init(&attr);
Jason Wesselcc096742010-01-28 17:04:42 -0600646 attr.bp_addr = (unsigned long)kgdb_arch_init;
Jason Wesselcc096742010-01-28 17:04:42 -0600647 attr.bp_len = HW_BREAKPOINT_LEN_1;
648 attr.bp_type = HW_BREAKPOINT_W;
649 attr.disabled = 1;
Dongdong Dengdf493932010-08-05 09:22:25 -0500650 for (i = 0; i < HBP_NUM; i++) {
Jason Wessel0b4b3822010-05-20 21:04:29 -0500651 if (breakinfo[i].pev)
652 continue;
Jason Wesselcc096742010-01-28 17:04:42 -0600653 breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL);
Jason Wessel91b152a2010-08-23 09:20:14 -0500654 if (IS_ERR((void * __force)breakinfo[i].pev)) {
Jason Wessel0b4b3822010-05-20 21:04:29 -0500655 printk(KERN_ERR "kgdb: Could not allocate hw"
656 "breakpoints\nDisabling the kernel debugger\n");
Jason Wesselcc096742010-01-28 17:04:42 -0600657 breakinfo[i].pev = NULL;
658 kgdb_arch_exit();
Jason Wessel0b4b3822010-05-20 21:04:29 -0500659 return;
Jason Wesselcc096742010-01-28 17:04:42 -0600660 }
661 for_each_online_cpu(cpu) {
662 pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
663 pevent[0]->hw.sample_period = 1;
Jason Wesselba773f72010-07-28 19:10:30 -0500664 pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
Jason Wesselcc096742010-01-28 17:04:42 -0600665 if (pevent[0]->destroy != NULL) {
666 pevent[0]->destroy = NULL;
667 release_bp_slot(*pevent);
668 }
669 }
670 }
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200671}
672
673/**
674 * kgdb_arch_exit - Perform any architecture specific uninitalization.
675 *
676 * This function will handle the uninitalization of any architecture
677 * specific callbacks, for dynamic registration and unregistration.
678 */
679void kgdb_arch_exit(void)
680{
Jason Wesselcc096742010-01-28 17:04:42 -0600681 int i;
682 for (i = 0; i < 4; i++) {
683 if (breakinfo[i].pev) {
684 unregister_wide_hw_breakpoint(breakinfo[i].pev);
685 breakinfo[i].pev = NULL;
686 }
687 }
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200688 unregister_die_notifier(&kgdb_notifier);
689}
690
691/**
692 *
693 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
694 * @exception: Exception vector number
695 * @regs: Current &struct pt_regs.
696 *
697 * On some architectures we need to skip a breakpoint exception when
698 * it occurs after a breakpoint has been removed.
699 *
700 * Skip an int3 exception when it occurs after a breakpoint has been
701 * removed. Backtrack eip by 1 since the int3 would have caused it to
702 * increment by 1.
703 */
704int kgdb_skipexception(int exception, struct pt_regs *regs)
705{
706 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
707 regs->ip -= 1;
708 return 1;
709 }
710 return 0;
711}
712
713unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
714{
715 if (exception == 3)
716 return instruction_pointer(regs) - 1;
717 return instruction_pointer(regs);
718}
719
Jason Wesseldcc78712010-05-20 21:04:21 -0500720void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
721{
722 regs->ip = ip;
723}
724
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200725struct kgdb_arch arch_kgdb_ops = {
726 /* Breakpoint instruction: */
727 .gdb_bpt_instr = { 0xcc },
Jason Wessel64e9ee32008-02-15 14:55:56 -0600728 .flags = KGDB_HW_BREAKPOINT,
729 .set_hw_breakpoint = kgdb_set_hw_break,
730 .remove_hw_breakpoint = kgdb_remove_hw_break,
Dongdong Dengd7ba979d2010-08-18 06:02:00 -0500731 .disable_hw_break = kgdb_disable_hw_debug,
Jason Wessel64e9ee32008-02-15 14:55:56 -0600732 .remove_all_hw_break = kgdb_remove_all_hw_break,
733 .correct_hw_break = kgdb_correct_hw_break,
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200734};