blob: 7ec1d5f8d28339bce0b74191a1d458c6c8e5d5df [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>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020042#include <linux/smp.h>
Jason Wesseld3597522008-02-15 14:55:53 -060043#include <linux/nmi.h>
Jason Wesselcc096742010-01-28 17:04:42 -060044#include <linux/hw_breakpoint.h>
Jason Wessel3751d3e2012-03-23 09:35:05 -050045#include <linux/uaccess.h>
46#include <linux/memory.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020047
K.Prasad62edab92009-06-01 23:47:06 +053048#include <asm/debugreg.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020049#include <asm/apicdef.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010050#include <asm/apic.h>
Don Zickus166d7512011-01-06 16:18:49 -050051#include <asm/nmi.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020052
Jason Wessel12bfa3d2010-08-05 09:22:20 -050053struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
Ingo Molnar82da3ff2008-04-17 20:05:37 +020054{
Ingo Molnar82da3ff2008-04-17 20:05:37 +020055#ifdef CONFIG_X86_32
Jason Wessel12bfa3d2010-08-05 09:22:20 -050056 { "ax", 4, offsetof(struct pt_regs, ax) },
57 { "cx", 4, offsetof(struct pt_regs, cx) },
58 { "dx", 4, offsetof(struct pt_regs, dx) },
59 { "bx", 4, offsetof(struct pt_regs, bx) },
60 { "sp", 4, offsetof(struct pt_regs, sp) },
61 { "bp", 4, offsetof(struct pt_regs, bp) },
62 { "si", 4, offsetof(struct pt_regs, si) },
63 { "di", 4, offsetof(struct pt_regs, di) },
64 { "ip", 4, offsetof(struct pt_regs, ip) },
65 { "flags", 4, offsetof(struct pt_regs, flags) },
66 { "cs", 4, offsetof(struct pt_regs, cs) },
67 { "ss", 4, offsetof(struct pt_regs, ss) },
68 { "ds", 4, offsetof(struct pt_regs, ds) },
69 { "es", 4, offsetof(struct pt_regs, es) },
Ingo Molnar82da3ff2008-04-17 20:05:37 +020070#else
Jason Wessel12bfa3d2010-08-05 09:22:20 -050071 { "ax", 8, offsetof(struct pt_regs, ax) },
72 { "bx", 8, offsetof(struct pt_regs, bx) },
73 { "cx", 8, offsetof(struct pt_regs, cx) },
74 { "dx", 8, offsetof(struct pt_regs, dx) },
75 { "si", 8, offsetof(struct pt_regs, dx) },
76 { "di", 8, offsetof(struct pt_regs, di) },
77 { "bp", 8, offsetof(struct pt_regs, bp) },
78 { "sp", 8, offsetof(struct pt_regs, sp) },
79 { "r8", 8, offsetof(struct pt_regs, r8) },
80 { "r9", 8, offsetof(struct pt_regs, r9) },
81 { "r10", 8, offsetof(struct pt_regs, r10) },
82 { "r11", 8, offsetof(struct pt_regs, r11) },
83 { "r12", 8, offsetof(struct pt_regs, r12) },
84 { "r13", 8, offsetof(struct pt_regs, r13) },
85 { "r14", 8, offsetof(struct pt_regs, r14) },
86 { "r15", 8, offsetof(struct pt_regs, r15) },
87 { "ip", 8, offsetof(struct pt_regs, ip) },
88 { "flags", 4, offsetof(struct pt_regs, flags) },
89 { "cs", 4, offsetof(struct pt_regs, cs) },
90 { "ss", 4, offsetof(struct pt_regs, ss) },
Jan Kiszka639077f2012-03-19 15:16:48 -050091 { "ds", 4, -1 },
92 { "es", 4, -1 },
Jason Wesselcf6f1962009-12-11 08:43:16 -060093#endif
Jan Kiszka639077f2012-03-19 15:16:48 -050094 { "fs", 4, -1 },
95 { "gs", 4, -1 },
Jason Wessel12bfa3d2010-08-05 09:22:20 -050096};
97
98int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
99{
100 if (
101#ifdef CONFIG_X86_32
102 regno == GDB_SS || regno == GDB_FS || regno == GDB_GS ||
103#endif
104 regno == GDB_SP || regno == GDB_ORIG_AX)
105 return 0;
106
107 if (dbg_reg_def[regno].offset != -1)
108 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
109 dbg_reg_def[regno].size);
110 return 0;
111}
112
113char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
114{
115 if (regno == GDB_ORIG_AX) {
116 memcpy(mem, &regs->orig_ax, sizeof(regs->orig_ax));
117 return "orig_ax";
118 }
119 if (regno >= DBG_MAX_REG_NUM || regno < 0)
120 return NULL;
121
122 if (dbg_reg_def[regno].offset != -1)
123 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
124 dbg_reg_def[regno].size);
125
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500126#ifdef CONFIG_X86_32
Jason Wessel21431c22010-03-15 07:28:00 -0500127 switch (regno) {
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500128 case GDB_SS:
129 if (!user_mode_vm(regs))
130 *(unsigned long *)mem = __KERNEL_DS;
131 break;
132 case GDB_SP:
133 if (!user_mode_vm(regs))
134 *(unsigned long *)mem = kernel_stack_pointer(regs);
135 break;
136 case GDB_GS:
137 case GDB_FS:
138 *(unsigned long *)mem = 0xFFFF;
139 break;
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500140 }
Jason Wessel21431c22010-03-15 07:28:00 -0500141#endif
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500142 return dbg_reg_def[regno].name;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200143}
144
145/**
146 * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
147 * @gdb_regs: A pointer to hold the registers in the order GDB wants.
148 * @p: The &struct task_struct of the desired process.
149 *
150 * Convert the register values of the sleeping process in @p to
151 * the format that GDB expects.
152 * This function is called when kgdb does not have access to the
153 * &struct pt_regs and therefore it should fill the gdb registers
154 * @gdb_regs with what has been saved in &struct thread_struct
155 * thread field during switch_to.
156 */
157void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
158{
Jason Wessel703a1ed2008-09-26 10:36:42 -0500159#ifndef CONFIG_X86_32
160 u32 *gdb_regs32 = (u32 *)gdb_regs;
161#endif
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200162 gdb_regs[GDB_AX] = 0;
163 gdb_regs[GDB_BX] = 0;
164 gdb_regs[GDB_CX] = 0;
165 gdb_regs[GDB_DX] = 0;
166 gdb_regs[GDB_SI] = 0;
167 gdb_regs[GDB_DI] = 0;
168 gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp;
169#ifdef CONFIG_X86_32
170 gdb_regs[GDB_DS] = __KERNEL_DS;
171 gdb_regs[GDB_ES] = __KERNEL_DS;
172 gdb_regs[GDB_PS] = 0;
173 gdb_regs[GDB_CS] = __KERNEL_CS;
174 gdb_regs[GDB_PC] = p->thread.ip;
175 gdb_regs[GDB_SS] = __KERNEL_DS;
176 gdb_regs[GDB_FS] = 0xFFFF;
177 gdb_regs[GDB_GS] = 0xFFFF;
178#else
Jason Wessel703a1ed2008-09-26 10:36:42 -0500179 gdb_regs32[GDB_PS] = *(unsigned long *)(p->thread.sp + 8);
180 gdb_regs32[GDB_CS] = __KERNEL_CS;
181 gdb_regs32[GDB_SS] = __KERNEL_DS;
Alexey Dobriyan0c235902009-05-04 03:30:15 +0400182 gdb_regs[GDB_PC] = 0;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200183 gdb_regs[GDB_R8] = 0;
184 gdb_regs[GDB_R9] = 0;
185 gdb_regs[GDB_R10] = 0;
186 gdb_regs[GDB_R11] = 0;
187 gdb_regs[GDB_R12] = 0;
188 gdb_regs[GDB_R13] = 0;
189 gdb_regs[GDB_R14] = 0;
190 gdb_regs[GDB_R15] = 0;
191#endif
192 gdb_regs[GDB_SP] = p->thread.sp;
193}
194
Jason Wessel64e9ee32008-02-15 14:55:56 -0600195static struct hw_breakpoint {
196 unsigned enabled;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600197 unsigned long addr;
Jason Wesselcc096742010-01-28 17:04:42 -0600198 int len;
199 int type;
Namhyung Kim8c8aefc2010-08-07 11:00:59 -0700200 struct perf_event * __percpu *pev;
Dongdong Dengdf493932010-08-05 09:22:25 -0500201} breakinfo[HBP_NUM];
Jason Wessel64e9ee32008-02-15 14:55:56 -0600202
Jason Wessel031acd82010-05-20 21:04:30 -0500203static unsigned long early_dr7;
204
Jason Wessel64e9ee32008-02-15 14:55:56 -0600205static void kgdb_correct_hw_break(void)
206{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600207 int breakno;
208
Dongdong Dengdf493932010-08-05 09:22:25 -0500209 for (breakno = 0; breakno < HBP_NUM; breakno++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600210 struct perf_event *bp;
211 struct arch_hw_breakpoint *info;
212 int val;
213 int cpu = raw_smp_processor_id();
214 if (!breakinfo[breakno].enabled)
215 continue;
Jason Wessel031acd82010-05-20 21:04:30 -0500216 if (dbg_is_early) {
217 set_debugreg(breakinfo[breakno].addr, breakno);
218 early_dr7 |= encode_dr7(breakno,
219 breakinfo[breakno].len,
220 breakinfo[breakno].type);
221 set_debugreg(early_dr7, 7);
222 continue;
223 }
Jason Wesselcc096742010-01-28 17:04:42 -0600224 bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
225 info = counter_arch_bp(bp);
226 if (bp->attr.disabled != 1)
227 continue;
228 bp->attr.bp_addr = breakinfo[breakno].addr;
229 bp->attr.bp_len = breakinfo[breakno].len;
230 bp->attr.bp_type = breakinfo[breakno].type;
231 info->address = breakinfo[breakno].addr;
232 info->len = breakinfo[breakno].len;
233 info->type = breakinfo[breakno].type;
234 val = arch_install_hw_breakpoint(bp);
235 if (!val)
236 bp->attr.disabled = 0;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600237 }
Jason Wessel031acd82010-05-20 21:04:30 -0500238 if (!dbg_is_early)
239 hw_breakpoint_restore();
Jason Wessel64e9ee32008-02-15 14:55:56 -0600240}
241
Jason Wessel5352ae62010-01-28 17:04:43 -0600242static int hw_break_reserve_slot(int breakno)
243{
244 int cpu;
245 int cnt = 0;
246 struct perf_event **pevent;
247
Jason Wessel031acd82010-05-20 21:04:30 -0500248 if (dbg_is_early)
249 return 0;
250
Jason Wessel5352ae62010-01-28 17:04:43 -0600251 for_each_online_cpu(cpu) {
252 cnt++;
253 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
254 if (dbg_reserve_bp_slot(*pevent))
255 goto fail;
256 }
257
258 return 0;
259
260fail:
261 for_each_online_cpu(cpu) {
262 cnt--;
263 if (!cnt)
264 break;
265 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
266 dbg_release_bp_slot(*pevent);
267 }
268 return -1;
269}
270
271static int hw_break_release_slot(int breakno)
272{
273 struct perf_event **pevent;
274 int cpu;
275
Jason Wessel031acd82010-05-20 21:04:30 -0500276 if (dbg_is_early)
277 return 0;
278
Jason Wessel5352ae62010-01-28 17:04:43 -0600279 for_each_online_cpu(cpu) {
280 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
281 if (dbg_release_bp_slot(*pevent))
282 /*
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300283 * The debugger is responsible for handing the retry on
Jason Wessel5352ae62010-01-28 17:04:43 -0600284 * remove failure.
285 */
286 return -1;
287 }
288 return 0;
289}
290
Jason Wessel64e9ee32008-02-15 14:55:56 -0600291static int
292kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
293{
294 int i;
295
Dongdong Dengdf493932010-08-05 09:22:25 -0500296 for (i = 0; i < HBP_NUM; i++)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600297 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
298 break;
Dongdong Dengdf493932010-08-05 09:22:25 -0500299 if (i == HBP_NUM)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600300 return -1;
301
Jason Wessel5352ae62010-01-28 17:04:43 -0600302 if (hw_break_release_slot(i)) {
303 printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
304 return -1;
305 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600306 breakinfo[i].enabled = 0;
307
308 return 0;
309}
310
311static void kgdb_remove_all_hw_break(void)
312{
313 int i;
Jason Wesselcc096742010-01-28 17:04:42 -0600314 int cpu = raw_smp_processor_id();
315 struct perf_event *bp;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600316
Dongdong Dengdf493932010-08-05 09:22:25 -0500317 for (i = 0; i < HBP_NUM; i++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600318 if (!breakinfo[i].enabled)
319 continue;
320 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
Jason Wessel10a6e672010-11-15 08:07:35 -0600321 if (!bp->attr.disabled) {
322 arch_uninstall_hw_breakpoint(bp);
323 bp->attr.disabled = 1;
Jason Wesselcc096742010-01-28 17:04:42 -0600324 continue;
Jason Wessel10a6e672010-11-15 08:07:35 -0600325 }
Jason Wessel031acd82010-05-20 21:04:30 -0500326 if (dbg_is_early)
327 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
328 breakinfo[i].type);
Jason Wessel10a6e672010-11-15 08:07:35 -0600329 else if (hw_break_release_slot(i))
330 printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n",
331 breakinfo[i].addr);
332 breakinfo[i].enabled = 0;
Jason Wesselcc096742010-01-28 17:04:42 -0600333 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600334}
335
336static int
337kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
338{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600339 int i;
340
Dongdong Dengdf493932010-08-05 09:22:25 -0500341 for (i = 0; i < HBP_NUM; i++)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600342 if (!breakinfo[i].enabled)
343 break;
Dongdong Dengdf493932010-08-05 09:22:25 -0500344 if (i == HBP_NUM)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600345 return -1;
346
347 switch (bptype) {
348 case BP_HARDWARE_BREAKPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600349 len = 1;
350 breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600351 break;
352 case BP_WRITE_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600353 breakinfo[i].type = X86_BREAKPOINT_WRITE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600354 break;
355 case BP_ACCESS_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600356 breakinfo[i].type = X86_BREAKPOINT_RW;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600357 break;
358 default:
359 return -1;
360 }
Jason Wesselcc096742010-01-28 17:04:42 -0600361 switch (len) {
362 case 1:
363 breakinfo[i].len = X86_BREAKPOINT_LEN_1;
364 break;
365 case 2:
366 breakinfo[i].len = X86_BREAKPOINT_LEN_2;
367 break;
368 case 4:
369 breakinfo[i].len = X86_BREAKPOINT_LEN_4;
370 break;
371#ifdef CONFIG_X86_64
372 case 8:
373 breakinfo[i].len = X86_BREAKPOINT_LEN_8;
374 break;
375#endif
376 default:
Jason Wessel64e9ee32008-02-15 14:55:56 -0600377 return -1;
Jason Wesselcc096742010-01-28 17:04:42 -0600378 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600379 breakinfo[i].addr = addr;
Jason Wessel5352ae62010-01-28 17:04:43 -0600380 if (hw_break_reserve_slot(i)) {
381 breakinfo[i].addr = 0;
382 return -1;
383 }
Jason Wesselcc096742010-01-28 17:04:42 -0600384 breakinfo[i].enabled = 1;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600385
386 return 0;
387}
388
389/**
390 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
391 * @regs: Current &struct pt_regs.
392 *
393 * This function will be called if the particular architecture must
394 * disable hardware debugging while it is processing gdb packets or
395 * handling exception.
396 */
Dongdong Dengd7ba979d2010-08-18 06:02:00 -0500397static void kgdb_disable_hw_debug(struct pt_regs *regs)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600398{
Jason Wesselcc096742010-01-28 17:04:42 -0600399 int i;
400 int cpu = raw_smp_processor_id();
401 struct perf_event *bp;
402
Jason Wessel64e9ee32008-02-15 14:55:56 -0600403 /* Disable hardware debugging while we are in kgdb: */
404 set_debugreg(0UL, 7);
Dongdong Dengdf493932010-08-05 09:22:25 -0500405 for (i = 0; i < HBP_NUM; i++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600406 if (!breakinfo[i].enabled)
407 continue;
Jason Wessel031acd82010-05-20 21:04:30 -0500408 if (dbg_is_early) {
409 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
410 breakinfo[i].type);
411 continue;
412 }
Jason Wesselcc096742010-01-28 17:04:42 -0600413 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
414 if (bp->attr.disabled == 1)
415 continue;
416 arch_uninstall_hw_breakpoint(bp);
417 bp->attr.disabled = 1;
418 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600419}
420
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200421#ifdef CONFIG_SMP
422/**
423 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
424 * @flags: Current IRQ state
425 *
426 * On SMP systems, we need to get the attention of the other CPUs
427 * and get them be in a known state. This should do what is needed
428 * to get the other CPUs to call kgdb_wait(). Note that on some arches,
429 * the NMI approach is not used for rounding up all the CPUs. For example,
430 * in case of MIPS, smp_call_function() is used to roundup CPUs. In
431 * this case, we have to make sure that interrupts are enabled before
432 * calling smp_call_function(). The argument to this function is
433 * the flags that will be used when restoring the interrupts. There is
434 * local_irq_save() call before kgdb_roundup_cpus().
435 *
436 * On non-SMP systems, this is not called.
437 */
438void kgdb_roundup_cpus(unsigned long flags)
439{
Ingo Molnardac5f412009-01-28 15:42:24 +0100440 apic->send_IPI_allbutself(APIC_DM_NMI);
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200441}
442#endif
443
444/**
445 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
Wanpeng Lic15acff2012-06-12 12:53:21 +0800446 * @e_vector: The error vector of the exception that happened.
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200447 * @signo: The signal number of the exception that happened.
448 * @err_code: The error code of the exception that happened.
Wanpeng Lic15acff2012-06-12 12:53:21 +0800449 * @remcomInBuffer: The buffer of the packet we have read.
450 * @remcomOutBuffer: The buffer of %BUFMAX bytes to write a packet into.
451 * @linux_regs: The &struct pt_regs of the current process.
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200452 *
453 * This function MUST handle the 'c' and 's' command packets,
454 * as well packets to set / remove a hardware breakpoint, if used.
455 * If there are additional packets which the hardware needs to handle,
456 * they are handled here. The code should return -1 if it wants to
457 * process more packets, and a %0 or %1 if it wants to exit from the
458 * kgdb callback.
459 */
460int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
461 char *remcomInBuffer, char *remcomOutBuffer,
462 struct pt_regs *linux_regs)
463{
464 unsigned long addr;
465 char *ptr;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200466
467 switch (remcomInBuffer[0]) {
468 case 'c':
469 case 's':
470 /* try to read optional parameter, pc unchanged if no parm */
471 ptr = &remcomInBuffer[1];
472 if (kgdb_hex2long(&ptr, &addr))
473 linux_regs->ip = addr;
Jason Wessel737a4602008-03-07 16:34:16 -0600474 case 'D':
475 case 'k':
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200476 /* clear the trace bit */
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700477 linux_regs->flags &= ~X86_EFLAGS_TF;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200478 atomic_set(&kgdb_cpu_doing_single_step, -1);
479
480 /* set the trace bit if we're stepping */
481 if (remcomInBuffer[0] == 's') {
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700482 linux_regs->flags |= X86_EFLAGS_TF;
Jason Wesseld7161a62008-09-26 10:36:41 -0500483 atomic_set(&kgdb_cpu_doing_single_step,
484 raw_smp_processor_id());
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200485 }
486
487 return 0;
488 }
489
490 /* this means that we do not want to exit from the handler: */
491 return -1;
492}
493
494static inline int
495single_step_cont(struct pt_regs *regs, struct die_args *args)
496{
497 /*
498 * Single step exception from kernel space to user space so
499 * eat the exception and continue the process:
500 */
501 printk(KERN_ERR "KGDB: trap/step from kernel to user space, "
502 "resuming...\n");
503 kgdb_arch_handle_exception(args->trapnr, args->signr,
504 args->err, "c", "", regs);
K.Prasad62edab92009-06-01 23:47:06 +0530505 /*
506 * Reset the BS bit in dr6 (pointed by args->err) to
507 * denote completion of processing
508 */
509 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200510
511 return NOTIFY_STOP;
512}
513
Jason Wesseld3597522008-02-15 14:55:53 -0600514static int was_in_debug_nmi[NR_CPUS];
515
Don Zickus9c48f1c2011-09-30 15:06:21 -0400516static int kgdb_nmi_handler(unsigned int cmd, struct pt_regs *regs)
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200517{
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200518 switch (cmd) {
Don Zickus9c48f1c2011-09-30 15:06:21 -0400519 case NMI_LOCAL:
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200520 if (atomic_read(&kgdb_active) != -1) {
521 /* KGDB CPU roundup */
522 kgdb_nmicallback(raw_smp_processor_id(), regs);
Jason Wesseld3597522008-02-15 14:55:53 -0600523 was_in_debug_nmi[raw_smp_processor_id()] = 1;
524 touch_nmi_watchdog();
Don Zickus9c48f1c2011-09-30 15:06:21 -0400525 return NMI_HANDLED;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200526 }
Don Zickus9c48f1c2011-09-30 15:06:21 -0400527 break;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200528
Don Zickus9c48f1c2011-09-30 15:06:21 -0400529 case NMI_UNKNOWN:
Jason Wesseld3597522008-02-15 14:55:53 -0600530 if (was_in_debug_nmi[raw_smp_processor_id()]) {
531 was_in_debug_nmi[raw_smp_processor_id()] = 0;
Don Zickus9c48f1c2011-09-30 15:06:21 -0400532 return NMI_HANDLED;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200533 }
Don Zickus9c48f1c2011-09-30 15:06:21 -0400534 break;
535 default:
536 /* do nothing */
537 break;
538 }
539 return NMI_DONE;
540}
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200541
Don Zickus9c48f1c2011-09-30 15:06:21 -0400542static int __kgdb_notify(struct die_args *args, unsigned long cmd)
543{
544 struct pt_regs *regs = args->regs;
545
546 switch (cmd) {
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200547 case DIE_DEBUG:
Jason Wesselcc096742010-01-28 17:04:42 -0600548 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
Jason Wesseld7161a62008-09-26 10:36:41 -0500549 if (user_mode(regs))
550 return single_step_cont(regs, args);
551 break;
552 } else if (test_thread_flag(TIF_SINGLESTEP))
553 /* This means a user thread is single stepping
554 * a system call which should be ignored
555 */
556 return NOTIFY_DONE;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200557 /* fall through */
558 default:
559 if (user_mode(regs))
560 return NOTIFY_DONE;
561 }
562
Jason Wesself503b5a2010-05-20 21:04:25 -0500563 if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200564 return NOTIFY_DONE;
565
Jason Wessel737a4602008-03-07 16:34:16 -0600566 /* Must touch watchdog before return to normal operation */
567 touch_nmi_watchdog();
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200568 return NOTIFY_STOP;
569}
570
Jason Wesself503b5a2010-05-20 21:04:25 -0500571int kgdb_ll_trap(int cmd, const char *str,
572 struct pt_regs *regs, long err, int trap, int sig)
573{
574 struct die_args args = {
575 .regs = regs,
576 .str = str,
577 .err = err,
578 .trapnr = trap,
579 .signr = sig,
580
581 };
582
583 if (!kgdb_io_module_registered)
584 return NOTIFY_DONE;
585
586 return __kgdb_notify(&args, cmd);
587}
Jason Wesself503b5a2010-05-20 21:04:25 -0500588
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200589static int
590kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
591{
592 unsigned long flags;
593 int ret;
594
595 local_irq_save(flags);
596 ret = __kgdb_notify(ptr, cmd);
597 local_irq_restore(flags);
598
599 return ret;
600}
601
602static struct notifier_block kgdb_notifier = {
603 .notifier_call = kgdb_notify,
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200604};
605
606/**
607 * kgdb_arch_init - Perform any architecture specific initalization.
608 *
609 * This function will handle the initalization of any architecture
610 * specific callbacks.
611 */
612int kgdb_arch_init(void)
613{
Don Zickus9c48f1c2011-09-30 15:06:21 -0400614 int retval;
615
616 retval = register_die_notifier(&kgdb_notifier);
617 if (retval)
618 goto out;
619
620 retval = register_nmi_handler(NMI_LOCAL, kgdb_nmi_handler,
621 0, "kgdb");
622 if (retval)
623 goto out1;
624
625 retval = register_nmi_handler(NMI_UNKNOWN, kgdb_nmi_handler,
626 0, "kgdb");
627
628 if (retval)
629 goto out2;
630
631 return retval;
632
633out2:
634 unregister_nmi_handler(NMI_LOCAL, "kgdb");
635out1:
636 unregister_die_notifier(&kgdb_notifier);
637out:
638 return retval;
Jason Wessel0b4b3822010-05-20 21:04:29 -0500639}
640
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200641static void kgdb_hw_overflow_handler(struct perf_event *event,
Jason Wesselba773f72010-07-28 19:10:30 -0500642 struct perf_sample_data *data, struct pt_regs *regs)
643{
Jason Wesselfad99fa2010-10-20 08:20:00 -0500644 struct task_struct *tsk = current;
645 int i;
646
647 for (i = 0; i < 4; i++)
648 if (breakinfo[i].enabled)
649 tsk->thread.debugreg6 |= (DR_TRAP0 << i);
Jason Wesselba773f72010-07-28 19:10:30 -0500650}
651
Jason Wessel0b4b3822010-05-20 21:04:29 -0500652void kgdb_arch_late(void)
653{
Jason Wesselcc096742010-01-28 17:04:42 -0600654 int i, cpu;
Jason Wesselcc096742010-01-28 17:04:42 -0600655 struct perf_event_attr attr;
656 struct perf_event **pevent;
657
Jason Wesselcc096742010-01-28 17:04:42 -0600658 /*
659 * Pre-allocate the hw breakpoint structions in the non-atomic
660 * portion of kgdb because this operation requires mutexs to
661 * complete.
662 */
Jason Wesselab310b52010-03-30 14:05:07 -0500663 hw_breakpoint_init(&attr);
Jason Wesselcc096742010-01-28 17:04:42 -0600664 attr.bp_addr = (unsigned long)kgdb_arch_init;
Jason Wesselcc096742010-01-28 17:04:42 -0600665 attr.bp_len = HW_BREAKPOINT_LEN_1;
666 attr.bp_type = HW_BREAKPOINT_W;
667 attr.disabled = 1;
Dongdong Dengdf493932010-08-05 09:22:25 -0500668 for (i = 0; i < HBP_NUM; i++) {
Jason Wessel0b4b3822010-05-20 21:04:29 -0500669 if (breakinfo[i].pev)
670 continue;
Avi Kivity4dc0da82011-06-29 18:42:35 +0300671 breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
Jason Wessel91b152a2010-08-23 09:20:14 -0500672 if (IS_ERR((void * __force)breakinfo[i].pev)) {
Jason Wessel0b4b3822010-05-20 21:04:29 -0500673 printk(KERN_ERR "kgdb: Could not allocate hw"
674 "breakpoints\nDisabling the kernel debugger\n");
Jason Wesselcc096742010-01-28 17:04:42 -0600675 breakinfo[i].pev = NULL;
676 kgdb_arch_exit();
Jason Wessel0b4b3822010-05-20 21:04:29 -0500677 return;
Jason Wesselcc096742010-01-28 17:04:42 -0600678 }
679 for_each_online_cpu(cpu) {
680 pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
681 pevent[0]->hw.sample_period = 1;
Jason Wesselba773f72010-07-28 19:10:30 -0500682 pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
Jason Wesselcc096742010-01-28 17:04:42 -0600683 if (pevent[0]->destroy != NULL) {
684 pevent[0]->destroy = NULL;
685 release_bp_slot(*pevent);
686 }
687 }
688 }
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200689}
690
691/**
692 * kgdb_arch_exit - Perform any architecture specific uninitalization.
693 *
694 * This function will handle the uninitalization of any architecture
695 * specific callbacks, for dynamic registration and unregistration.
696 */
697void kgdb_arch_exit(void)
698{
Jason Wesselcc096742010-01-28 17:04:42 -0600699 int i;
700 for (i = 0; i < 4; i++) {
701 if (breakinfo[i].pev) {
702 unregister_wide_hw_breakpoint(breakinfo[i].pev);
703 breakinfo[i].pev = NULL;
704 }
705 }
Don Zickus9c48f1c2011-09-30 15:06:21 -0400706 unregister_nmi_handler(NMI_UNKNOWN, "kgdb");
707 unregister_nmi_handler(NMI_LOCAL, "kgdb");
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200708 unregister_die_notifier(&kgdb_notifier);
709}
710
711/**
712 *
713 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
714 * @exception: Exception vector number
715 * @regs: Current &struct pt_regs.
716 *
717 * On some architectures we need to skip a breakpoint exception when
718 * it occurs after a breakpoint has been removed.
719 *
720 * Skip an int3 exception when it occurs after a breakpoint has been
721 * removed. Backtrack eip by 1 since the int3 would have caused it to
722 * increment by 1.
723 */
724int kgdb_skipexception(int exception, struct pt_regs *regs)
725{
726 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
727 regs->ip -= 1;
728 return 1;
729 }
730 return 0;
731}
732
733unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
734{
735 if (exception == 3)
736 return instruction_pointer(regs) - 1;
737 return instruction_pointer(regs);
738}
739
Jason Wesseldcc78712010-05-20 21:04:21 -0500740void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
741{
742 regs->ip = ip;
743}
744
Jason Wessel3751d3e2012-03-23 09:35:05 -0500745int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
746{
747 int err;
Jason Wessel42c12212012-08-08 21:12:19 -0500748#ifdef CONFIG_DEBUG_RODATA
Jason Wessel3751d3e2012-03-23 09:35:05 -0500749 char opc[BREAK_INSTR_SIZE];
Jason Wessel42c12212012-08-08 21:12:19 -0500750#endif /* CONFIG_DEBUG_RODATA */
Jason Wessel3751d3e2012-03-23 09:35:05 -0500751
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};