blob: b9bd9d8de665ada43b98864108ac4412b78a3f8c [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>
Jason Wessel3751d3e2012-03-23 09:35:05 -050046#include <linux/uaccess.h>
47#include <linux/memory.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020048
K.Prasad62edab92009-06-01 23:47:06 +053049#include <asm/debugreg.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020050#include <asm/apicdef.h>
51#include <asm/system.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010052#include <asm/apic.h>
Don Zickus166d7512011-01-06 16:18:49 -050053#include <asm/nmi.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020054
Jason Wessel12bfa3d2010-08-05 09:22:20 -050055struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
Ingo Molnar82da3ff2008-04-17 20:05:37 +020056{
Ingo Molnar82da3ff2008-04-17 20:05:37 +020057#ifdef CONFIG_X86_32
Jason Wessel12bfa3d2010-08-05 09:22:20 -050058 { "ax", 4, offsetof(struct pt_regs, ax) },
59 { "cx", 4, offsetof(struct pt_regs, cx) },
60 { "dx", 4, offsetof(struct pt_regs, dx) },
61 { "bx", 4, offsetof(struct pt_regs, bx) },
62 { "sp", 4, offsetof(struct pt_regs, sp) },
63 { "bp", 4, offsetof(struct pt_regs, bp) },
64 { "si", 4, offsetof(struct pt_regs, si) },
65 { "di", 4, offsetof(struct pt_regs, di) },
66 { "ip", 4, offsetof(struct pt_regs, ip) },
67 { "flags", 4, offsetof(struct pt_regs, flags) },
68 { "cs", 4, offsetof(struct pt_regs, cs) },
69 { "ss", 4, offsetof(struct pt_regs, ss) },
70 { "ds", 4, offsetof(struct pt_regs, ds) },
71 { "es", 4, offsetof(struct pt_regs, es) },
Ingo Molnar82da3ff2008-04-17 20:05:37 +020072#else
Jason Wessel12bfa3d2010-08-05 09:22:20 -050073 { "ax", 8, offsetof(struct pt_regs, ax) },
74 { "bx", 8, offsetof(struct pt_regs, bx) },
75 { "cx", 8, offsetof(struct pt_regs, cx) },
76 { "dx", 8, offsetof(struct pt_regs, dx) },
77 { "si", 8, offsetof(struct pt_regs, dx) },
78 { "di", 8, offsetof(struct pt_regs, di) },
79 { "bp", 8, offsetof(struct pt_regs, bp) },
80 { "sp", 8, offsetof(struct pt_regs, sp) },
81 { "r8", 8, offsetof(struct pt_regs, r8) },
82 { "r9", 8, offsetof(struct pt_regs, r9) },
83 { "r10", 8, offsetof(struct pt_regs, r10) },
84 { "r11", 8, offsetof(struct pt_regs, r11) },
85 { "r12", 8, offsetof(struct pt_regs, r12) },
86 { "r13", 8, offsetof(struct pt_regs, r13) },
87 { "r14", 8, offsetof(struct pt_regs, r14) },
88 { "r15", 8, offsetof(struct pt_regs, r15) },
89 { "ip", 8, offsetof(struct pt_regs, ip) },
90 { "flags", 4, offsetof(struct pt_regs, flags) },
91 { "cs", 4, offsetof(struct pt_regs, cs) },
92 { "ss", 4, offsetof(struct pt_regs, ss) },
Jan Kiszka639077f2012-03-19 15:16:48 -050093 { "ds", 4, -1 },
94 { "es", 4, -1 },
Jason Wesselcf6f1962009-12-11 08:43:16 -060095#endif
Jan Kiszka639077f2012-03-19 15:16:48 -050096 { "fs", 4, -1 },
97 { "gs", 4, -1 },
Jason Wessel12bfa3d2010-08-05 09:22:20 -050098};
99
100int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
101{
102 if (
103#ifdef CONFIG_X86_32
104 regno == GDB_SS || regno == GDB_FS || regno == GDB_GS ||
105#endif
106 regno == GDB_SP || regno == GDB_ORIG_AX)
107 return 0;
108
109 if (dbg_reg_def[regno].offset != -1)
110 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
111 dbg_reg_def[regno].size);
112 return 0;
113}
114
115char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
116{
117 if (regno == GDB_ORIG_AX) {
118 memcpy(mem, &regs->orig_ax, sizeof(regs->orig_ax));
119 return "orig_ax";
120 }
121 if (regno >= DBG_MAX_REG_NUM || regno < 0)
122 return NULL;
123
124 if (dbg_reg_def[regno].offset != -1)
125 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
126 dbg_reg_def[regno].size);
127
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500128#ifdef CONFIG_X86_32
Jason Wessel21431c22010-03-15 07:28:00 -0500129 switch (regno) {
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500130 case GDB_SS:
131 if (!user_mode_vm(regs))
132 *(unsigned long *)mem = __KERNEL_DS;
133 break;
134 case GDB_SP:
135 if (!user_mode_vm(regs))
136 *(unsigned long *)mem = kernel_stack_pointer(regs);
137 break;
138 case GDB_GS:
139 case GDB_FS:
140 *(unsigned long *)mem = 0xFFFF;
141 break;
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500142 }
Jason Wessel21431c22010-03-15 07:28:00 -0500143#endif
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500144 return dbg_reg_def[regno].name;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200145}
146
147/**
148 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
149 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
150 * @p: The &struct task_struct of the desired process.
151 *
152 * Convert the register values of the sleeping process in @p to
153 * the format that GDB expects.
154 * This function is called when kgdb does not have access to the
155 * &struct pt_regs and therefore it should fill the gdb registers
156 * @gdb_regs with what has been saved in &struct thread_struct
157 * thread field during switch_to.
158 */
159void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
160{
Jason Wessel703a1ed2008-09-26 10:36:42 -0500161#ifndef CONFIG_X86_32
162 u32 *gdb_regs32 = (u32 *)gdb_regs;
163#endif
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200164 gdb_regs[GDB_AX] = 0;
165 gdb_regs[GDB_BX] = 0;
166 gdb_regs[GDB_CX] = 0;
167 gdb_regs[GDB_DX] = 0;
168 gdb_regs[GDB_SI] = 0;
169 gdb_regs[GDB_DI] = 0;
170 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
171#ifdef CONFIG_X86_32
172 gdb_regs[GDB_DS] = __KERNEL_DS;
173 gdb_regs[GDB_ES] = __KERNEL_DS;
174 gdb_regs[GDB_PS] = 0;
175 gdb_regs[GDB_CS] = __KERNEL_CS;
176 gdb_regs[GDB_PC] = p->thread.ip;
177 gdb_regs[GDB_SS] = __KERNEL_DS;
178 gdb_regs[GDB_FS] = 0xFFFF;
179 gdb_regs[GDB_GS] = 0xFFFF;
180#else
Jason Wessel703a1ed2008-09-26 10:36:42 -0500181 gdb_regs32[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
182 gdb_regs32[GDB_CS] = __KERNEL_CS;
183 gdb_regs32[GDB_SS] = __KERNEL_DS;
Alexey Dobriyan0c235902009-05-04 03:30:15 +0400184 gdb_regs[GDB_PC] = 0;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200185 gdb_regs[GDB_R8] = 0;
186 gdb_regs[GDB_R9] = 0;
187 gdb_regs[GDB_R10] = 0;
188 gdb_regs[GDB_R11] = 0;
189 gdb_regs[GDB_R12] = 0;
190 gdb_regs[GDB_R13] = 0;
191 gdb_regs[GDB_R14] = 0;
192 gdb_regs[GDB_R15] = 0;
193#endif
194 gdb_regs[GDB_SP] = p->thread.sp;
195}
196
Jason Wessel64e9ee32008-02-15 14:55:56 -0600197static struct hw_breakpoint {
198 unsigned enabled;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600199 unsigned long addr;
Jason Wesselcc096742010-01-28 17:04:42 -0600200 int len;
201 int type;
Namhyung Kim8c8aefc2010-08-07 11:00:59 -0700202 struct perf_event * __percpu *pev;
Dongdong Dengdf493932010-08-05 09:22:25 -0500203} breakinfo[HBP_NUM];
Jason Wessel64e9ee32008-02-15 14:55:56 -0600204
Jason Wessel031acd82010-05-20 21:04:30 -0500205static unsigned long early_dr7;
206
Jason Wessel64e9ee32008-02-15 14:55:56 -0600207static void kgdb_correct_hw_break(void)
208{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600209 int breakno;
210
Dongdong Dengdf493932010-08-05 09:22:25 -0500211 for (breakno = 0; breakno < HBP_NUM; breakno++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600212 struct perf_event *bp;
213 struct arch_hw_breakpoint *info;
214 int val;
215 int cpu = raw_smp_processor_id();
216 if (!breakinfo[breakno].enabled)
217 continue;
Jason Wessel031acd82010-05-20 21:04:30 -0500218 if (dbg_is_early) {
219 set_debugreg(breakinfo[breakno].addr, breakno);
220 early_dr7 |= encode_dr7(breakno,
221 breakinfo[breakno].len,
222 breakinfo[breakno].type);
223 set_debugreg(early_dr7, 7);
224 continue;
225 }
Jason Wesselcc096742010-01-28 17:04:42 -0600226 bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
227 info = counter_arch_bp(bp);
228 if (bp->attr.disabled != 1)
229 continue;
230 bp->attr.bp_addr = breakinfo[breakno].addr;
231 bp->attr.bp_len = breakinfo[breakno].len;
232 bp->attr.bp_type = breakinfo[breakno].type;
233 info->address = breakinfo[breakno].addr;
234 info->len = breakinfo[breakno].len;
235 info->type = breakinfo[breakno].type;
236 val = arch_install_hw_breakpoint(bp);
237 if (!val)
238 bp->attr.disabled = 0;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600239 }
Jason Wessel031acd82010-05-20 21:04:30 -0500240 if (!dbg_is_early)
241 hw_breakpoint_restore();
Jason Wessel64e9ee32008-02-15 14:55:56 -0600242}
243
Jason Wessel5352ae62010-01-28 17:04:43 -0600244static int hw_break_reserve_slot(int breakno)
245{
246 int cpu;
247 int cnt = 0;
248 struct perf_event **pevent;
249
Jason Wessel031acd82010-05-20 21:04:30 -0500250 if (dbg_is_early)
251 return 0;
252
Jason Wessel5352ae62010-01-28 17:04:43 -0600253 for_each_online_cpu(cpu) {
254 cnt++;
255 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
256 if (dbg_reserve_bp_slot(*pevent))
257 goto fail;
258 }
259
260 return 0;
261
262fail:
263 for_each_online_cpu(cpu) {
264 cnt--;
265 if (!cnt)
266 break;
267 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
268 dbg_release_bp_slot(*pevent);
269 }
270 return -1;
271}
272
273static int hw_break_release_slot(int breakno)
274{
275 struct perf_event **pevent;
276 int cpu;
277
Jason Wessel031acd82010-05-20 21:04:30 -0500278 if (dbg_is_early)
279 return 0;
280
Jason Wessel5352ae62010-01-28 17:04:43 -0600281 for_each_online_cpu(cpu) {
282 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
283 if (dbg_release_bp_slot(*pevent))
284 /*
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300285 * The debugger is responsible for handing the retry on
Jason Wessel5352ae62010-01-28 17:04:43 -0600286 * remove failure.
287 */
288 return -1;
289 }
290 return 0;
291}
292
Jason Wessel64e9ee32008-02-15 14:55:56 -0600293static int
294kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
295{
296 int i;
297
Dongdong Dengdf493932010-08-05 09:22:25 -0500298 for (i = 0; i < HBP_NUM; i++)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600299 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
300 break;
Dongdong Dengdf493932010-08-05 09:22:25 -0500301 if (i == HBP_NUM)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600302 return -1;
303
Jason Wessel5352ae62010-01-28 17:04:43 -0600304 if (hw_break_release_slot(i)) {
305 printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
306 return -1;
307 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600308 breakinfo[i].enabled = 0;
309
310 return 0;
311}
312
313static void kgdb_remove_all_hw_break(void)
314{
315 int i;
Jason Wesselcc096742010-01-28 17:04:42 -0600316 int cpu = raw_smp_processor_id();
317 struct perf_event *bp;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600318
Dongdong Dengdf493932010-08-05 09:22:25 -0500319 for (i = 0; i < HBP_NUM; i++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600320 if (!breakinfo[i].enabled)
321 continue;
322 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
Jason Wessel10a6e672010-11-15 08:07:35 -0600323 if (!bp->attr.disabled) {
324 arch_uninstall_hw_breakpoint(bp);
325 bp->attr.disabled = 1;
Jason Wesselcc096742010-01-28 17:04:42 -0600326 continue;
Jason Wessel10a6e672010-11-15 08:07:35 -0600327 }
Jason Wessel031acd82010-05-20 21:04:30 -0500328 if (dbg_is_early)
329 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
330 breakinfo[i].type);
Jason Wessel10a6e672010-11-15 08:07:35 -0600331 else if (hw_break_release_slot(i))
332 printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n",
333 breakinfo[i].addr);
334 breakinfo[i].enabled = 0;
Jason Wesselcc096742010-01-28 17:04:42 -0600335 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600336}
337
338static int
339kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
340{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600341 int i;
342
Dongdong Dengdf493932010-08-05 09:22:25 -0500343 for (i = 0; i < HBP_NUM; i++)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600344 if (!breakinfo[i].enabled)
345 break;
Dongdong Dengdf493932010-08-05 09:22:25 -0500346 if (i == HBP_NUM)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600347 return -1;
348
349 switch (bptype) {
350 case BP_HARDWARE_BREAKPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600351 len = 1;
352 breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600353 break;
354 case BP_WRITE_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600355 breakinfo[i].type = X86_BREAKPOINT_WRITE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600356 break;
357 case BP_ACCESS_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600358 breakinfo[i].type = X86_BREAKPOINT_RW;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600359 break;
360 default:
361 return -1;
362 }
Jason Wesselcc096742010-01-28 17:04:42 -0600363 switch (len) {
364 case 1:
365 breakinfo[i].len = X86_BREAKPOINT_LEN_1;
366 break;
367 case 2:
368 breakinfo[i].len = X86_BREAKPOINT_LEN_2;
369 break;
370 case 4:
371 breakinfo[i].len = X86_BREAKPOINT_LEN_4;
372 break;
373#ifdef CONFIG_X86_64
374 case 8:
375 breakinfo[i].len = X86_BREAKPOINT_LEN_8;
376 break;
377#endif
378 default:
Jason Wessel64e9ee32008-02-15 14:55:56 -0600379 return -1;
Jason Wesselcc096742010-01-28 17:04:42 -0600380 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600381 breakinfo[i].addr = addr;
Jason Wessel5352ae62010-01-28 17:04:43 -0600382 if (hw_break_reserve_slot(i)) {
383 breakinfo[i].addr = 0;
384 return -1;
385 }
Jason Wesselcc096742010-01-28 17:04:42 -0600386 breakinfo[i].enabled = 1;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600387
388 return 0;
389}
390
391/**
392 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
393 * @regs: Current &struct pt_regs.
394 *
395 * This function will be called if the particular architecture must
396 * disable hardware debugging while it is processing gdb packets or
397 * handling exception.
398 */
Dongdong Dengd7ba979d2010-08-18 06:02:00 -0500399static void kgdb_disable_hw_debug(struct pt_regs *regs)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600400{
Jason Wesselcc096742010-01-28 17:04:42 -0600401 int i;
402 int cpu = raw_smp_processor_id();
403 struct perf_event *bp;
404
Jason Wessel64e9ee32008-02-15 14:55:56 -0600405 /* Disable hardware debugging while we are in kgdb: */
406 set_debugreg(0UL, 7);
Dongdong Dengdf493932010-08-05 09:22:25 -0500407 for (i = 0; i < HBP_NUM; i++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600408 if (!breakinfo[i].enabled)
409 continue;
Jason Wessel031acd82010-05-20 21:04:30 -0500410 if (dbg_is_early) {
411 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
412 breakinfo[i].type);
413 continue;
414 }
Jason Wesselcc096742010-01-28 17:04:42 -0600415 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
416 if (bp->attr.disabled == 1)
417 continue;
418 arch_uninstall_hw_breakpoint(bp);
419 bp->attr.disabled = 1;
420 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600421}
422
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200423#ifdef CONFIG_SMP
424/**
425 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
426 * @flags: Current IRQ state
427 *
428 * On SMP systems, we need to get the attention of the other CPUs
429 * and get them be in a known state. This should do what is needed
430 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
431 * the NMI approach is not used for rounding up all the CPUs. For example,
432 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
433 * this case, we have to make sure that interrupts are enabled before
434 * calling smp_call_function(). The argument to this function is
435 * the flags that will be used when restoring the interrupts. There is
436 * local_irq_save() call before kgdb_roundup_cpus().
437 *
438 * On non-SMP systems, this is not called.
439 */
440void kgdb_roundup_cpus(unsigned long flags)
441{
Ingo Molnardac5f412009-01-28 15:42:24 +0100442 apic->send_IPI_allbutself(APIC_DM_NMI);
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200443}
444#endif
445
446/**
447 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
448 * @vector: The error vector of the exception that happened.
449 * @signo: The signal number of the exception that happened.
450 * @err_code: The error code of the exception that happened.
451 * @remcom_in_buffer: The buffer of the packet we have read.
452 * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
453 * @regs: The &struct pt_regs of the current process.
454 *
455 * This function MUST handle the 'c' and 's' command packets,
456 * as well packets to set / remove a hardware breakpoint, if used.
457 * If there are additional packets which the hardware needs to handle,
458 * they are handled here. The code should return -1 if it wants to
459 * process more packets, and a %0 or %1 if it wants to exit from the
460 * kgdb callback.
461 */
462int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
463 char *remcomInBuffer, char *remcomOutBuffer,
464 struct pt_regs *linux_regs)
465{
466 unsigned long addr;
467 char *ptr;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200468
469 switch (remcomInBuffer[0]) {
470 case 'c':
471 case 's':
472 /* try to read optional parameter, pc unchanged if no parm */
473 ptr = &remcomInBuffer[1];
474 if (kgdb_hex2long(&ptr, &addr))
475 linux_regs->ip = addr;
Jason Wessel737a4602008-03-07 16:34:16 -0600476 case 'D':
477 case 'k':
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200478 /* clear the trace bit */
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700479 linux_regs->flags &= ~X86_EFLAGS_TF;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200480 atomic_set(&kgdb_cpu_doing_single_step, -1);
481
482 /* set the trace bit if we're stepping */
483 if (remcomInBuffer[0] == 's') {
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700484 linux_regs->flags |= X86_EFLAGS_TF;
Jason Wesseld7161a62008-09-26 10:36:41 -0500485 atomic_set(&kgdb_cpu_doing_single_step,
486 raw_smp_processor_id());
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200487 }
488
489 return 0;
490 }
491
492 /* this means that we do not want to exit from the handler: */
493 return -1;
494}
495
496static inline int
497single_step_cont(struct pt_regs *regs, struct die_args *args)
498{
499 /*
500 * Single step exception from kernel space to user space so
501 * eat the exception and continue the process:
502 */
503 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
504 "resuming...\n");
505 kgdb_arch_handle_exception(args->trapnr, args->signr,
506 args->err, "c", "", regs);
K.Prasad62edab92009-06-01 23:47:06 +0530507 /*
508 * Reset the BS bit in dr6 (pointed by args->err) to
509 * denote completion of processing
510 */
511 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200512
513 return NOTIFY_STOP;
514}
515
Jason Wesseld3597522008-02-15 14:55:53 -0600516static int was_in_debug_nmi[NR_CPUS];
517
Don Zickus9c48f1c2011-09-30 15:06:21 -0400518static int kgdb_nmi_handler(unsigned int cmd, struct pt_regs *regs)
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200519{
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200520 switch (cmd) {
Don Zickus9c48f1c2011-09-30 15:06:21 -0400521 case NMI_LOCAL:
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200522 if (atomic_read(&kgdb_active) != -1) {
523 /* KGDB CPU roundup */
524 kgdb_nmicallback(raw_smp_processor_id(), regs);
Jason Wesseld3597522008-02-15 14:55:53 -0600525 was_in_debug_nmi[raw_smp_processor_id()] = 1;
526 touch_nmi_watchdog();
Don Zickus9c48f1c2011-09-30 15:06:21 -0400527 return NMI_HANDLED;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200528 }
Don Zickus9c48f1c2011-09-30 15:06:21 -0400529 break;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200530
Don Zickus9c48f1c2011-09-30 15:06:21 -0400531 case NMI_UNKNOWN:
Jason Wesseld3597522008-02-15 14:55:53 -0600532 if (was_in_debug_nmi[raw_smp_processor_id()]) {
533 was_in_debug_nmi[raw_smp_processor_id()] = 0;
Don Zickus9c48f1c2011-09-30 15:06:21 -0400534 return NMI_HANDLED;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200535 }
Don Zickus9c48f1c2011-09-30 15:06:21 -0400536 break;
537 default:
538 /* do nothing */
539 break;
540 }
541 return NMI_DONE;
542}
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200543
Don Zickus9c48f1c2011-09-30 15:06:21 -0400544static int __kgdb_notify(struct die_args *args, unsigned long cmd)
545{
546 struct pt_regs *regs = args->regs;
547
548 switch (cmd) {
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200549 case DIE_DEBUG:
Jason Wesselcc096742010-01-28 17:04:42 -0600550 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
Jason Wesseld7161a62008-09-26 10:36:41 -0500551 if (user_mode(regs))
552 return single_step_cont(regs, args);
553 break;
554 } else if (test_thread_flag(TIF_SINGLESTEP))
555 /* This means a user thread is single stepping
556 * a system call which should be ignored
557 */
558 return NOTIFY_DONE;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200559 /* fall through */
560 default:
561 if (user_mode(regs))
562 return NOTIFY_DONE;
563 }
564
Jason Wesself503b5a2010-05-20 21:04:25 -0500565 if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200566 return NOTIFY_DONE;
567
Jason Wessel737a4602008-03-07 16:34:16 -0600568 /* Must touch watchdog before return to normal operation */
569 touch_nmi_watchdog();
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200570 return NOTIFY_STOP;
571}
572
Jason Wesself503b5a2010-05-20 21:04:25 -0500573int kgdb_ll_trap(int cmd, const char *str,
574 struct pt_regs *regs, long err, int trap, int sig)
575{
576 struct die_args args = {
577 .regs = regs,
578 .str = str,
579 .err = err,
580 .trapnr = trap,
581 .signr = sig,
582
583 };
584
585 if (!kgdb_io_module_registered)
586 return NOTIFY_DONE;
587
588 return __kgdb_notify(&args, cmd);
589}
Jason Wesself503b5a2010-05-20 21:04:25 -0500590
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200591static int
592kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
593{
594 unsigned long flags;
595 int ret;
596
597 local_irq_save(flags);
598 ret = __kgdb_notify(ptr, cmd);
599 local_irq_restore(flags);
600
601 return ret;
602}
603
604static struct notifier_block kgdb_notifier = {
605 .notifier_call = kgdb_notify,
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200606};
607
608/**
609 * kgdb_arch_init - Perform any architecture specific initalization.
610 *
611 * This function will handle the initalization of any architecture
612 * specific callbacks.
613 */
614int kgdb_arch_init(void)
615{
Don Zickus9c48f1c2011-09-30 15:06:21 -0400616 int retval;
617
618 retval = register_die_notifier(&kgdb_notifier);
619 if (retval)
620 goto out;
621
622 retval = register_nmi_handler(NMI_LOCAL, kgdb_nmi_handler,
623 0, "kgdb");
624 if (retval)
625 goto out1;
626
627 retval = register_nmi_handler(NMI_UNKNOWN, kgdb_nmi_handler,
628 0, "kgdb");
629
630 if (retval)
631 goto out2;
632
633 return retval;
634
635out2:
636 unregister_nmi_handler(NMI_LOCAL, "kgdb");
637out1:
638 unregister_die_notifier(&kgdb_notifier);
639out:
640 return retval;
Jason Wessel0b4b3822010-05-20 21:04:29 -0500641}
642
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200643static void kgdb_hw_overflow_handler(struct perf_event *event,
Jason Wesselba773f72010-07-28 19:10:30 -0500644 struct perf_sample_data *data, struct pt_regs *regs)
645{
Jason Wesselfad99fa2010-10-20 08:20:00 -0500646 struct task_struct *tsk = current;
647 int i;
648
649 for (i = 0; i < 4; i++)
650 if (breakinfo[i].enabled)
651 tsk->thread.debugreg6 |= (DR_TRAP0 << i);
Jason Wesselba773f72010-07-28 19:10:30 -0500652}
653
Jason Wessel0b4b3822010-05-20 21:04:29 -0500654void kgdb_arch_late(void)
655{
Jason Wesselcc096742010-01-28 17:04:42 -0600656 int i, cpu;
Jason Wesselcc096742010-01-28 17:04:42 -0600657 struct perf_event_attr attr;
658 struct perf_event **pevent;
659
Jason Wesselcc096742010-01-28 17:04:42 -0600660 /*
661 * Pre-allocate the hw breakpoint structions in the non-atomic
662 * portion of kgdb because this operation requires mutexs to
663 * complete.
664 */
Jason Wesselab310b52010-03-30 14:05:07 -0500665 hw_breakpoint_init(&attr);
Jason Wesselcc096742010-01-28 17:04:42 -0600666 attr.bp_addr = (unsigned long)kgdb_arch_init;
Jason Wesselcc096742010-01-28 17:04:42 -0600667 attr.bp_len = HW_BREAKPOINT_LEN_1;
668 attr.bp_type = HW_BREAKPOINT_W;
669 attr.disabled = 1;
Dongdong Dengdf493932010-08-05 09:22:25 -0500670 for (i = 0; i < HBP_NUM; i++) {
Jason Wessel0b4b3822010-05-20 21:04:29 -0500671 if (breakinfo[i].pev)
672 continue;
Avi Kivity4dc0da82011-06-29 18:42:35 +0300673 breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
Jason Wessel91b152a2010-08-23 09:20:14 -0500674 if (IS_ERR((void * __force)breakinfo[i].pev)) {
Jason Wessel0b4b3822010-05-20 21:04:29 -0500675 printk(KERN_ERR "kgdb: Could not allocate hw"
676 "breakpoints\nDisabling the kernel debugger\n");
Jason Wesselcc096742010-01-28 17:04:42 -0600677 breakinfo[i].pev = NULL;
678 kgdb_arch_exit();
Jason Wessel0b4b3822010-05-20 21:04:29 -0500679 return;
Jason Wesselcc096742010-01-28 17:04:42 -0600680 }
681 for_each_online_cpu(cpu) {
682 pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
683 pevent[0]->hw.sample_period = 1;
Jason Wesselba773f72010-07-28 19:10:30 -0500684 pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
Jason Wesselcc096742010-01-28 17:04:42 -0600685 if (pevent[0]->destroy != NULL) {
686 pevent[0]->destroy = NULL;
687 release_bp_slot(*pevent);
688 }
689 }
690 }
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200691}
692
693/**
694 * kgdb_arch_exit - Perform any architecture specific uninitalization.
695 *
696 * This function will handle the uninitalization of any architecture
697 * specific callbacks, for dynamic registration and unregistration.
698 */
699void kgdb_arch_exit(void)
700{
Jason Wesselcc096742010-01-28 17:04:42 -0600701 int i;
702 for (i = 0; i < 4; i++) {
703 if (breakinfo[i].pev) {
704 unregister_wide_hw_breakpoint(breakinfo[i].pev);
705 breakinfo[i].pev = NULL;
706 }
707 }
Don Zickus9c48f1c2011-09-30 15:06:21 -0400708 unregister_nmi_handler(NMI_UNKNOWN, "kgdb");
709 unregister_nmi_handler(NMI_LOCAL, "kgdb");
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200710 unregister_die_notifier(&kgdb_notifier);
711}
712
713/**
714 *
715 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
716 * @exception: Exception vector number
717 * @regs: Current &struct pt_regs.
718 *
719 * On some architectures we need to skip a breakpoint exception when
720 * it occurs after a breakpoint has been removed.
721 *
722 * Skip an int3 exception when it occurs after a breakpoint has been
723 * removed. Backtrack eip by 1 since the int3 would have caused it to
724 * increment by 1.
725 */
726int kgdb_skipexception(int exception, struct pt_regs *regs)
727{
728 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
729 regs->ip -= 1;
730 return 1;
731 }
732 return 0;
733}
734
735unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
736{
737 if (exception == 3)
738 return instruction_pointer(regs) - 1;
739 return instruction_pointer(regs);
740}
741
Jason Wesseldcc78712010-05-20 21:04:21 -0500742void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
743{
744 regs->ip = ip;
745}
746
Jason Wessel3751d3e2012-03-23 09:35:05 -0500747int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
748{
749 int err;
750 char opc[BREAK_INSTR_SIZE];
751
752 bpt->type = BP_BREAKPOINT;
753 err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
754 BREAK_INSTR_SIZE);
755 if (err)
756 return err;
757 err = probe_kernel_write((char *)bpt->bpt_addr,
758 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
759#ifdef CONFIG_DEBUG_RODATA
760 if (!err)
761 return err;
762 /*
763 * It is safe to call text_poke() because normal kernel execution
764 * is stopped on all cores, so long as the text_mutex is not locked.
765 */
766 if (mutex_is_locked(&text_mutex))
767 return -EBUSY;
768 text_poke((void *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr,
769 BREAK_INSTR_SIZE);
770 err = probe_kernel_read(opc, (char *)bpt->bpt_addr, BREAK_INSTR_SIZE);
771 if (err)
772 return err;
773 if (memcmp(opc, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE))
774 return -EINVAL;
775 bpt->type = BP_POKE_BREAKPOINT;
776#endif /* CONFIG_DEBUG_RODATA */
777 return err;
778}
779
780int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
781{
782#ifdef CONFIG_DEBUG_RODATA
783 int err;
784 char opc[BREAK_INSTR_SIZE];
785
786 if (bpt->type != BP_POKE_BREAKPOINT)
787 goto knl_write;
788 /*
789 * It is safe to call text_poke() because normal kernel execution
790 * is stopped on all cores, so long as the text_mutex is not locked.
791 */
792 if (mutex_is_locked(&text_mutex))
793 goto knl_write;
794 text_poke((void *)bpt->bpt_addr, bpt->saved_instr, BREAK_INSTR_SIZE);
795 err = probe_kernel_read(opc, (char *)bpt->bpt_addr, BREAK_INSTR_SIZE);
796 if (err || memcmp(opc, bpt->saved_instr, BREAK_INSTR_SIZE))
797 goto knl_write;
798 return err;
799knl_write:
800#endif /* CONFIG_DEBUG_RODATA */
801 return probe_kernel_write((char *)bpt->bpt_addr,
802 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
803}
804
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200805struct kgdb_arch arch_kgdb_ops = {
806 /* Breakpoint instruction: */
807 .gdb_bpt_instr = { 0xcc },
Jason Wessel64e9ee32008-02-15 14:55:56 -0600808 .flags = KGDB_HW_BREAKPOINT,
809 .set_hw_breakpoint = kgdb_set_hw_break,
810 .remove_hw_breakpoint = kgdb_remove_hw_break,
Dongdong Dengd7ba979d2010-08-18 06:02:00 -0500811 .disable_hw_break = kgdb_disable_hw_debug,
Jason Wessel64e9ee32008-02-15 14:55:56 -0600812 .remove_all_hw_break = kgdb_remove_all_hw_break,
813 .correct_hw_break = kgdb_correct_hw_break,
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200814};