Commit | Line | Data |
---|---|---|
82da3ff8 IM |
1 | /* |
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> | |
d3597524 | 44 | #include <linux/nmi.h> |
cc096749 | 45 | #include <linux/hw_breakpoint.h> |
82da3ff8 | 46 | |
62edab90 | 47 | #include <asm/debugreg.h> |
82da3ff8 IM |
48 | #include <asm/apicdef.h> |
49 | #include <asm/system.h> | |
7b6aa335 | 50 | #include <asm/apic.h> |
82da3ff8 | 51 | |
82da3ff8 IM |
52 | /** |
53 | * pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs | |
54 | * @gdb_regs: A pointer to hold the registers in the order GDB wants. | |
55 | * @regs: The &struct pt_regs of the current process. | |
56 | * | |
57 | * Convert the pt_regs in @regs into the format for registers that | |
58 | * GDB expects, stored in @gdb_regs. | |
59 | */ | |
60 | void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs) | |
61 | { | |
703a1edc JW |
62 | #ifndef CONFIG_X86_32 |
63 | u32 *gdb_regs32 = (u32 *)gdb_regs; | |
64 | #endif | |
82da3ff8 IM |
65 | gdb_regs[GDB_AX] = regs->ax; |
66 | gdb_regs[GDB_BX] = regs->bx; | |
67 | gdb_regs[GDB_CX] = regs->cx; | |
68 | gdb_regs[GDB_DX] = regs->dx; | |
69 | gdb_regs[GDB_SI] = regs->si; | |
70 | gdb_regs[GDB_DI] = regs->di; | |
71 | gdb_regs[GDB_BP] = regs->bp; | |
82da3ff8 IM |
72 | gdb_regs[GDB_PC] = regs->ip; |
73 | #ifdef CONFIG_X86_32 | |
703a1edc | 74 | gdb_regs[GDB_PS] = regs->flags; |
82da3ff8 IM |
75 | gdb_regs[GDB_DS] = regs->ds; |
76 | gdb_regs[GDB_ES] = regs->es; | |
77 | gdb_regs[GDB_CS] = regs->cs; | |
82da3ff8 IM |
78 | gdb_regs[GDB_FS] = 0xFFFF; |
79 | gdb_regs[GDB_GS] = 0xFFFF; | |
cf6f196d JW |
80 | if (user_mode_vm(regs)) { |
81 | gdb_regs[GDB_SS] = regs->ss; | |
82 | gdb_regs[GDB_SP] = regs->sp; | |
83 | } else { | |
84 | gdb_regs[GDB_SS] = __KERNEL_DS; | |
85 | gdb_regs[GDB_SP] = kernel_stack_pointer(regs); | |
86 | } | |
82da3ff8 IM |
87 | #else |
88 | gdb_regs[GDB_R8] = regs->r8; | |
89 | gdb_regs[GDB_R9] = regs->r9; | |
90 | gdb_regs[GDB_R10] = regs->r10; | |
91 | gdb_regs[GDB_R11] = regs->r11; | |
92 | gdb_regs[GDB_R12] = regs->r12; | |
93 | gdb_regs[GDB_R13] = regs->r13; | |
94 | gdb_regs[GDB_R14] = regs->r14; | |
95 | gdb_regs[GDB_R15] = regs->r15; | |
703a1edc JW |
96 | gdb_regs32[GDB_PS] = regs->flags; |
97 | gdb_regs32[GDB_CS] = regs->cs; | |
98 | gdb_regs32[GDB_SS] = regs->ss; | |
5ca6c0ca | 99 | gdb_regs[GDB_SP] = kernel_stack_pointer(regs); |
cf6f196d | 100 | #endif |
82da3ff8 IM |
101 | } |
102 | ||
103 | /** | |
104 | * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs | |
105 | * @gdb_regs: A pointer to hold the registers in the order GDB wants. | |
106 | * @p: The &struct task_struct of the desired process. | |
107 | * | |
108 | * Convert the register values of the sleeping process in @p to | |
109 | * the format that GDB expects. | |
110 | * This function is called when kgdb does not have access to the | |
111 | * &struct pt_regs and therefore it should fill the gdb registers | |
112 | * @gdb_regs with what has been saved in &struct thread_struct | |
113 | * thread field during switch_to. | |
114 | */ | |
115 | void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p) | |
116 | { | |
703a1edc JW |
117 | #ifndef CONFIG_X86_32 |
118 | u32 *gdb_regs32 = (u32 *)gdb_regs; | |
119 | #endif | |
82da3ff8 IM |
120 | gdb_regs[GDB_AX] = 0; |
121 | gdb_regs[GDB_BX] = 0; | |
122 | gdb_regs[GDB_CX] = 0; | |
123 | gdb_regs[GDB_DX] = 0; | |
124 | gdb_regs[GDB_SI] = 0; | |
125 | gdb_regs[GDB_DI] = 0; | |
126 | gdb_regs[GDB_BP] = *(unsigned long *)p->thread.sp; | |
127 | #ifdef CONFIG_X86_32 | |
128 | gdb_regs[GDB_DS] = __KERNEL_DS; | |
129 | gdb_regs[GDB_ES] = __KERNEL_DS; | |
130 | gdb_regs[GDB_PS] = 0; | |
131 | gdb_regs[GDB_CS] = __KERNEL_CS; | |
132 | gdb_regs[GDB_PC] = p->thread.ip; | |
133 | gdb_regs[GDB_SS] = __KERNEL_DS; | |
134 | gdb_regs[GDB_FS] = 0xFFFF; | |
135 | gdb_regs[GDB_GS] = 0xFFFF; | |
136 | #else | |
703a1edc JW |
137 | gdb_regs32[GDB_PS] = *(unsigned long *)(p->thread.sp + 8); |
138 | gdb_regs32[GDB_CS] = __KERNEL_CS; | |
139 | gdb_regs32[GDB_SS] = __KERNEL_DS; | |
0c23590f | 140 | gdb_regs[GDB_PC] = 0; |
82da3ff8 IM |
141 | gdb_regs[GDB_R8] = 0; |
142 | gdb_regs[GDB_R9] = 0; | |
143 | gdb_regs[GDB_R10] = 0; | |
144 | gdb_regs[GDB_R11] = 0; | |
145 | gdb_regs[GDB_R12] = 0; | |
146 | gdb_regs[GDB_R13] = 0; | |
147 | gdb_regs[GDB_R14] = 0; | |
148 | gdb_regs[GDB_R15] = 0; | |
149 | #endif | |
150 | gdb_regs[GDB_SP] = p->thread.sp; | |
151 | } | |
152 | ||
153 | /** | |
154 | * gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs. | |
155 | * @gdb_regs: A pointer to hold the registers we've received from GDB. | |
156 | * @regs: A pointer to a &struct pt_regs to hold these values in. | |
157 | * | |
158 | * Convert the GDB regs in @gdb_regs into the pt_regs, and store them | |
159 | * in @regs. | |
160 | */ | |
161 | void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs) | |
162 | { | |
703a1edc JW |
163 | #ifndef CONFIG_X86_32 |
164 | u32 *gdb_regs32 = (u32 *)gdb_regs; | |
165 | #endif | |
82da3ff8 IM |
166 | regs->ax = gdb_regs[GDB_AX]; |
167 | regs->bx = gdb_regs[GDB_BX]; | |
168 | regs->cx = gdb_regs[GDB_CX]; | |
169 | regs->dx = gdb_regs[GDB_DX]; | |
170 | regs->si = gdb_regs[GDB_SI]; | |
171 | regs->di = gdb_regs[GDB_DI]; | |
172 | regs->bp = gdb_regs[GDB_BP]; | |
82da3ff8 IM |
173 | regs->ip = gdb_regs[GDB_PC]; |
174 | #ifdef CONFIG_X86_32 | |
703a1edc | 175 | regs->flags = gdb_regs[GDB_PS]; |
82da3ff8 IM |
176 | regs->ds = gdb_regs[GDB_DS]; |
177 | regs->es = gdb_regs[GDB_ES]; | |
178 | regs->cs = gdb_regs[GDB_CS]; | |
179 | #else | |
180 | regs->r8 = gdb_regs[GDB_R8]; | |
181 | regs->r9 = gdb_regs[GDB_R9]; | |
182 | regs->r10 = gdb_regs[GDB_R10]; | |
183 | regs->r11 = gdb_regs[GDB_R11]; | |
184 | regs->r12 = gdb_regs[GDB_R12]; | |
185 | regs->r13 = gdb_regs[GDB_R13]; | |
186 | regs->r14 = gdb_regs[GDB_R14]; | |
187 | regs->r15 = gdb_regs[GDB_R15]; | |
703a1edc JW |
188 | regs->flags = gdb_regs32[GDB_PS]; |
189 | regs->cs = gdb_regs32[GDB_CS]; | |
190 | regs->ss = gdb_regs32[GDB_SS]; | |
82da3ff8 IM |
191 | #endif |
192 | } | |
193 | ||
64e9ee30 JW |
194 | static struct hw_breakpoint { |
195 | unsigned enabled; | |
64e9ee30 | 196 | unsigned long addr; |
cc096749 JW |
197 | int len; |
198 | int type; | |
199 | struct perf_event **pev; | |
64e9ee30 JW |
200 | } breakinfo[4]; |
201 | ||
202 | static void kgdb_correct_hw_break(void) | |
203 | { | |
64e9ee30 JW |
204 | int breakno; |
205 | ||
64e9ee30 | 206 | for (breakno = 0; breakno < 4; breakno++) { |
cc096749 JW |
207 | 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; | |
213 | bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu); | |
214 | info = counter_arch_bp(bp); | |
215 | if (bp->attr.disabled != 1) | |
216 | continue; | |
217 | bp->attr.bp_addr = breakinfo[breakno].addr; | |
218 | bp->attr.bp_len = breakinfo[breakno].len; | |
219 | bp->attr.bp_type = breakinfo[breakno].type; | |
220 | info->address = breakinfo[breakno].addr; | |
221 | info->len = breakinfo[breakno].len; | |
222 | info->type = breakinfo[breakno].type; | |
223 | val = arch_install_hw_breakpoint(bp); | |
224 | if (!val) | |
225 | bp->attr.disabled = 0; | |
64e9ee30 | 226 | } |
cc096749 | 227 | hw_breakpoint_restore(); |
64e9ee30 JW |
228 | } |
229 | ||
5352ae63 JW |
230 | static int hw_break_reserve_slot(int breakno) |
231 | { | |
232 | int cpu; | |
233 | int cnt = 0; | |
234 | struct perf_event **pevent; | |
235 | ||
236 | for_each_online_cpu(cpu) { | |
237 | cnt++; | |
238 | pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu); | |
239 | if (dbg_reserve_bp_slot(*pevent)) | |
240 | goto fail; | |
241 | } | |
242 | ||
243 | return 0; | |
244 | ||
245 | fail: | |
246 | for_each_online_cpu(cpu) { | |
247 | cnt--; | |
248 | if (!cnt) | |
249 | break; | |
250 | pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu); | |
251 | dbg_release_bp_slot(*pevent); | |
252 | } | |
253 | return -1; | |
254 | } | |
255 | ||
256 | static int hw_break_release_slot(int breakno) | |
257 | { | |
258 | struct perf_event **pevent; | |
259 | int cpu; | |
260 | ||
261 | for_each_online_cpu(cpu) { | |
262 | pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu); | |
263 | if (dbg_release_bp_slot(*pevent)) | |
264 | /* | |
265 | * The debugger is responisble for handing the retry on | |
266 | * remove failure. | |
267 | */ | |
268 | return -1; | |
269 | } | |
270 | return 0; | |
271 | } | |
272 | ||
64e9ee30 JW |
273 | static int |
274 | kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype) | |
275 | { | |
276 | int i; | |
277 | ||
278 | for (i = 0; i < 4; i++) | |
279 | if (breakinfo[i].addr == addr && breakinfo[i].enabled) | |
280 | break; | |
281 | if (i == 4) | |
282 | return -1; | |
283 | ||
5352ae63 JW |
284 | if (hw_break_release_slot(i)) { |
285 | printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr); | |
286 | return -1; | |
287 | } | |
64e9ee30 JW |
288 | breakinfo[i].enabled = 0; |
289 | ||
290 | return 0; | |
291 | } | |
292 | ||
293 | static void kgdb_remove_all_hw_break(void) | |
294 | { | |
295 | int i; | |
cc096749 JW |
296 | int cpu = raw_smp_processor_id(); |
297 | struct perf_event *bp; | |
64e9ee30 | 298 | |
cc096749 JW |
299 | for (i = 0; i < 4; i++) { |
300 | if (!breakinfo[i].enabled) | |
301 | continue; | |
302 | bp = *per_cpu_ptr(breakinfo[i].pev, cpu); | |
303 | if (bp->attr.disabled == 1) | |
304 | continue; | |
305 | arch_uninstall_hw_breakpoint(bp); | |
306 | bp->attr.disabled = 1; | |
307 | } | |
64e9ee30 JW |
308 | } |
309 | ||
310 | static int | |
311 | kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype) | |
312 | { | |
64e9ee30 JW |
313 | int i; |
314 | ||
315 | for (i = 0; i < 4; i++) | |
316 | if (!breakinfo[i].enabled) | |
317 | break; | |
318 | if (i == 4) | |
319 | return -1; | |
320 | ||
321 | switch (bptype) { | |
322 | case BP_HARDWARE_BREAKPOINT: | |
cc096749 JW |
323 | len = 1; |
324 | breakinfo[i].type = X86_BREAKPOINT_EXECUTE; | |
64e9ee30 JW |
325 | break; |
326 | case BP_WRITE_WATCHPOINT: | |
cc096749 | 327 | breakinfo[i].type = X86_BREAKPOINT_WRITE; |
64e9ee30 JW |
328 | break; |
329 | case BP_ACCESS_WATCHPOINT: | |
cc096749 | 330 | breakinfo[i].type = X86_BREAKPOINT_RW; |
64e9ee30 JW |
331 | break; |
332 | default: | |
333 | return -1; | |
334 | } | |
cc096749 JW |
335 | switch (len) { |
336 | case 1: | |
337 | breakinfo[i].len = X86_BREAKPOINT_LEN_1; | |
338 | break; | |
339 | case 2: | |
340 | breakinfo[i].len = X86_BREAKPOINT_LEN_2; | |
341 | break; | |
342 | case 4: | |
343 | breakinfo[i].len = X86_BREAKPOINT_LEN_4; | |
344 | break; | |
345 | #ifdef CONFIG_X86_64 | |
346 | case 8: | |
347 | breakinfo[i].len = X86_BREAKPOINT_LEN_8; | |
348 | break; | |
349 | #endif | |
350 | default: | |
64e9ee30 | 351 | return -1; |
cc096749 | 352 | } |
64e9ee30 | 353 | breakinfo[i].addr = addr; |
5352ae63 JW |
354 | if (hw_break_reserve_slot(i)) { |
355 | breakinfo[i].addr = 0; | |
356 | return -1; | |
357 | } | |
cc096749 | 358 | breakinfo[i].enabled = 1; |
64e9ee30 JW |
359 | |
360 | return 0; | |
361 | } | |
362 | ||
363 | /** | |
364 | * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb. | |
365 | * @regs: Current &struct pt_regs. | |
366 | * | |
367 | * This function will be called if the particular architecture must | |
368 | * disable hardware debugging while it is processing gdb packets or | |
369 | * handling exception. | |
370 | */ | |
371 | void kgdb_disable_hw_debug(struct pt_regs *regs) | |
372 | { | |
cc096749 JW |
373 | int i; |
374 | int cpu = raw_smp_processor_id(); | |
375 | struct perf_event *bp; | |
376 | ||
64e9ee30 JW |
377 | /* Disable hardware debugging while we are in kgdb: */ |
378 | set_debugreg(0UL, 7); | |
cc096749 JW |
379 | for (i = 0; i < 4; i++) { |
380 | if (!breakinfo[i].enabled) | |
381 | continue; | |
382 | bp = *per_cpu_ptr(breakinfo[i].pev, cpu); | |
383 | if (bp->attr.disabled == 1) | |
384 | continue; | |
385 | arch_uninstall_hw_breakpoint(bp); | |
386 | bp->attr.disabled = 1; | |
387 | } | |
64e9ee30 JW |
388 | } |
389 | ||
82da3ff8 IM |
390 | #ifdef CONFIG_SMP |
391 | /** | |
392 | * kgdb_roundup_cpus - Get other CPUs into a holding pattern | |
393 | * @flags: Current IRQ state | |
394 | * | |
395 | * On SMP systems, we need to get the attention of the other CPUs | |
396 | * and get them be in a known state. This should do what is needed | |
397 | * to get the other CPUs to call kgdb_wait(). Note that on some arches, | |
398 | * the NMI approach is not used for rounding up all the CPUs. For example, | |
399 | * in case of MIPS, smp_call_function() is used to roundup CPUs. In | |
400 | * this case, we have to make sure that interrupts are enabled before | |
401 | * calling smp_call_function(). The argument to this function is | |
402 | * the flags that will be used when restoring the interrupts. There is | |
403 | * local_irq_save() call before kgdb_roundup_cpus(). | |
404 | * | |
405 | * On non-SMP systems, this is not called. | |
406 | */ | |
407 | void kgdb_roundup_cpus(unsigned long flags) | |
408 | { | |
dac5f412 | 409 | apic->send_IPI_allbutself(APIC_DM_NMI); |
82da3ff8 IM |
410 | } |
411 | #endif | |
412 | ||
413 | /** | |
414 | * kgdb_arch_handle_exception - Handle architecture specific GDB packets. | |
415 | * @vector: The error vector of the exception that happened. | |
416 | * @signo: The signal number of the exception that happened. | |
417 | * @err_code: The error code of the exception that happened. | |
418 | * @remcom_in_buffer: The buffer of the packet we have read. | |
419 | * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into. | |
420 | * @regs: The &struct pt_regs of the current process. | |
421 | * | |
422 | * This function MUST handle the 'c' and 's' command packets, | |
423 | * as well packets to set / remove a hardware breakpoint, if used. | |
424 | * If there are additional packets which the hardware needs to handle, | |
425 | * they are handled here. The code should return -1 if it wants to | |
426 | * process more packets, and a %0 or %1 if it wants to exit from the | |
427 | * kgdb callback. | |
428 | */ | |
429 | int kgdb_arch_handle_exception(int e_vector, int signo, int err_code, | |
430 | char *remcomInBuffer, char *remcomOutBuffer, | |
431 | struct pt_regs *linux_regs) | |
432 | { | |
433 | unsigned long addr; | |
434 | char *ptr; | |
435 | int newPC; | |
436 | ||
437 | switch (remcomInBuffer[0]) { | |
438 | case 'c': | |
439 | case 's': | |
440 | /* try to read optional parameter, pc unchanged if no parm */ | |
441 | ptr = &remcomInBuffer[1]; | |
442 | if (kgdb_hex2long(&ptr, &addr)) | |
443 | linux_regs->ip = addr; | |
737a460f JW |
444 | case 'D': |
445 | case 'k': | |
82da3ff8 IM |
446 | newPC = linux_regs->ip; |
447 | ||
448 | /* clear the trace bit */ | |
fda31d7d | 449 | linux_regs->flags &= ~X86_EFLAGS_TF; |
82da3ff8 IM |
450 | atomic_set(&kgdb_cpu_doing_single_step, -1); |
451 | ||
452 | /* set the trace bit if we're stepping */ | |
453 | if (remcomInBuffer[0] == 's') { | |
fda31d7d | 454 | linux_regs->flags |= X86_EFLAGS_TF; |
d7161a65 JW |
455 | atomic_set(&kgdb_cpu_doing_single_step, |
456 | raw_smp_processor_id()); | |
82da3ff8 IM |
457 | } |
458 | ||
64e9ee30 JW |
459 | kgdb_correct_hw_break(); |
460 | ||
82da3ff8 IM |
461 | return 0; |
462 | } | |
463 | ||
464 | /* this means that we do not want to exit from the handler: */ | |
465 | return -1; | |
466 | } | |
467 | ||
468 | static inline int | |
469 | single_step_cont(struct pt_regs *regs, struct die_args *args) | |
470 | { | |
471 | /* | |
472 | * Single step exception from kernel space to user space so | |
473 | * eat the exception and continue the process: | |
474 | */ | |
475 | printk(KERN_ERR "KGDB: trap/step from kernel to user space, " | |
476 | "resuming...\n"); | |
477 | kgdb_arch_handle_exception(args->trapnr, args->signr, | |
478 | args->err, "c", "", regs); | |
62edab90 P |
479 | /* |
480 | * Reset the BS bit in dr6 (pointed by args->err) to | |
481 | * denote completion of processing | |
482 | */ | |
483 | (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP; | |
82da3ff8 IM |
484 | |
485 | return NOTIFY_STOP; | |
486 | } | |
487 | ||
d3597524 JW |
488 | static int was_in_debug_nmi[NR_CPUS]; |
489 | ||
82da3ff8 IM |
490 | static int __kgdb_notify(struct die_args *args, unsigned long cmd) |
491 | { | |
492 | struct pt_regs *regs = args->regs; | |
493 | ||
494 | switch (cmd) { | |
495 | case DIE_NMI: | |
496 | if (atomic_read(&kgdb_active) != -1) { | |
497 | /* KGDB CPU roundup */ | |
498 | kgdb_nmicallback(raw_smp_processor_id(), regs); | |
d3597524 JW |
499 | was_in_debug_nmi[raw_smp_processor_id()] = 1; |
500 | touch_nmi_watchdog(); | |
82da3ff8 IM |
501 | return NOTIFY_STOP; |
502 | } | |
503 | return NOTIFY_DONE; | |
504 | ||
505 | case DIE_NMI_IPI: | |
e85ceae9 | 506 | /* Just ignore, we will handle the roundup on DIE_NMI. */ |
d3597524 JW |
507 | return NOTIFY_DONE; |
508 | ||
509 | case DIE_NMIUNKNOWN: | |
510 | if (was_in_debug_nmi[raw_smp_processor_id()]) { | |
511 | was_in_debug_nmi[raw_smp_processor_id()] = 0; | |
82da3ff8 IM |
512 | return NOTIFY_STOP; |
513 | } | |
514 | return NOTIFY_DONE; | |
515 | ||
516 | case DIE_NMIWATCHDOG: | |
517 | if (atomic_read(&kgdb_active) != -1) { | |
518 | /* KGDB CPU roundup: */ | |
519 | kgdb_nmicallback(raw_smp_processor_id(), regs); | |
520 | return NOTIFY_STOP; | |
521 | } | |
522 | /* Enter debugger: */ | |
523 | break; | |
524 | ||
525 | case DIE_DEBUG: | |
cc096749 | 526 | if (atomic_read(&kgdb_cpu_doing_single_step) != -1) { |
d7161a65 JW |
527 | if (user_mode(regs)) |
528 | return single_step_cont(regs, args); | |
529 | break; | |
530 | } else if (test_thread_flag(TIF_SINGLESTEP)) | |
531 | /* This means a user thread is single stepping | |
532 | * a system call which should be ignored | |
533 | */ | |
534 | return NOTIFY_DONE; | |
82da3ff8 IM |
535 | /* fall through */ |
536 | default: | |
537 | if (user_mode(regs)) | |
538 | return NOTIFY_DONE; | |
539 | } | |
540 | ||
f503b5ae | 541 | if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs)) |
82da3ff8 IM |
542 | return NOTIFY_DONE; |
543 | ||
737a460f JW |
544 | /* Must touch watchdog before return to normal operation */ |
545 | touch_nmi_watchdog(); | |
82da3ff8 IM |
546 | return NOTIFY_STOP; |
547 | } | |
548 | ||
f503b5ae JW |
549 | #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP |
550 | int kgdb_ll_trap(int cmd, const char *str, | |
551 | struct pt_regs *regs, long err, int trap, int sig) | |
552 | { | |
553 | struct die_args args = { | |
554 | .regs = regs, | |
555 | .str = str, | |
556 | .err = err, | |
557 | .trapnr = trap, | |
558 | .signr = sig, | |
559 | ||
560 | }; | |
561 | ||
562 | if (!kgdb_io_module_registered) | |
563 | return NOTIFY_DONE; | |
564 | ||
565 | return __kgdb_notify(&args, cmd); | |
566 | } | |
567 | #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */ | |
568 | ||
82da3ff8 IM |
569 | static int |
570 | kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr) | |
571 | { | |
572 | unsigned long flags; | |
573 | int ret; | |
574 | ||
575 | local_irq_save(flags); | |
576 | ret = __kgdb_notify(ptr, cmd); | |
577 | local_irq_restore(flags); | |
578 | ||
579 | return ret; | |
580 | } | |
581 | ||
582 | static struct notifier_block kgdb_notifier = { | |
583 | .notifier_call = kgdb_notify, | |
584 | ||
585 | /* | |
586 | * Lowest-prio notifier priority, we want to be notified last: | |
587 | */ | |
588 | .priority = -INT_MAX, | |
589 | }; | |
590 | ||
591 | /** | |
592 | * kgdb_arch_init - Perform any architecture specific initalization. | |
593 | * | |
594 | * This function will handle the initalization of any architecture | |
595 | * specific callbacks. | |
596 | */ | |
597 | int kgdb_arch_init(void) | |
598 | { | |
cc096749 JW |
599 | int i, cpu; |
600 | int ret; | |
601 | struct perf_event_attr attr; | |
602 | struct perf_event **pevent; | |
603 | ||
604 | ret = register_die_notifier(&kgdb_notifier); | |
605 | if (ret != 0) | |
606 | return ret; | |
607 | /* | |
608 | * Pre-allocate the hw breakpoint structions in the non-atomic | |
609 | * portion of kgdb because this operation requires mutexs to | |
610 | * complete. | |
611 | */ | |
ab310b5e | 612 | hw_breakpoint_init(&attr); |
cc096749 | 613 | attr.bp_addr = (unsigned long)kgdb_arch_init; |
cc096749 JW |
614 | attr.bp_len = HW_BREAKPOINT_LEN_1; |
615 | attr.bp_type = HW_BREAKPOINT_W; | |
616 | attr.disabled = 1; | |
617 | for (i = 0; i < 4; i++) { | |
618 | breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL); | |
619 | if (IS_ERR(breakinfo[i].pev)) { | |
620 | printk(KERN_ERR "kgdb: Could not allocate hw breakpoints\n"); | |
621 | breakinfo[i].pev = NULL; | |
622 | kgdb_arch_exit(); | |
623 | return -1; | |
624 | } | |
625 | for_each_online_cpu(cpu) { | |
626 | pevent = per_cpu_ptr(breakinfo[i].pev, cpu); | |
627 | pevent[0]->hw.sample_period = 1; | |
628 | if (pevent[0]->destroy != NULL) { | |
629 | pevent[0]->destroy = NULL; | |
630 | release_bp_slot(*pevent); | |
631 | } | |
632 | } | |
633 | } | |
634 | return ret; | |
82da3ff8 IM |
635 | } |
636 | ||
637 | /** | |
638 | * kgdb_arch_exit - Perform any architecture specific uninitalization. | |
639 | * | |
640 | * This function will handle the uninitalization of any architecture | |
641 | * specific callbacks, for dynamic registration and unregistration. | |
642 | */ | |
643 | void kgdb_arch_exit(void) | |
644 | { | |
cc096749 JW |
645 | int i; |
646 | for (i = 0; i < 4; i++) { | |
647 | if (breakinfo[i].pev) { | |
648 | unregister_wide_hw_breakpoint(breakinfo[i].pev); | |
649 | breakinfo[i].pev = NULL; | |
650 | } | |
651 | } | |
82da3ff8 IM |
652 | unregister_die_notifier(&kgdb_notifier); |
653 | } | |
654 | ||
655 | /** | |
656 | * | |
657 | * kgdb_skipexception - Bail out of KGDB when we've been triggered. | |
658 | * @exception: Exception vector number | |
659 | * @regs: Current &struct pt_regs. | |
660 | * | |
661 | * On some architectures we need to skip a breakpoint exception when | |
662 | * it occurs after a breakpoint has been removed. | |
663 | * | |
664 | * Skip an int3 exception when it occurs after a breakpoint has been | |
665 | * removed. Backtrack eip by 1 since the int3 would have caused it to | |
666 | * increment by 1. | |
667 | */ | |
668 | int kgdb_skipexception(int exception, struct pt_regs *regs) | |
669 | { | |
670 | if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) { | |
671 | regs->ip -= 1; | |
672 | return 1; | |
673 | } | |
674 | return 0; | |
675 | } | |
676 | ||
677 | unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs) | |
678 | { | |
679 | if (exception == 3) | |
680 | return instruction_pointer(regs) - 1; | |
681 | return instruction_pointer(regs); | |
682 | } | |
683 | ||
dcc78711 JW |
684 | void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip) |
685 | { | |
686 | regs->ip = ip; | |
687 | } | |
688 | ||
82da3ff8 IM |
689 | struct kgdb_arch arch_kgdb_ops = { |
690 | /* Breakpoint instruction: */ | |
691 | .gdb_bpt_instr = { 0xcc }, | |
64e9ee30 JW |
692 | .flags = KGDB_HW_BREAKPOINT, |
693 | .set_hw_breakpoint = kgdb_set_hw_break, | |
694 | .remove_hw_breakpoint = kgdb_remove_hw_break, | |
695 | .remove_all_hw_break = kgdb_remove_all_hw_break, | |
696 | .correct_hw_break = kgdb_correct_hw_break, | |
82da3ff8 | 697 | }; |