]>
Commit | Line | Data |
---|---|---|
3e0a4e85 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
82da3ff8 | 2 | /* |
82da3ff8 IM |
3 | */ |
4 | ||
5 | /* | |
6 | * Copyright (C) 2004 Amit S. Kale <amitkale@linsyssoft.com> | |
7 | * Copyright (C) 2000-2001 VERITAS Software Corporation. | |
8 | * Copyright (C) 2002 Andi Kleen, SuSE Labs | |
9 | * Copyright (C) 2004 LinSysSoft Technologies Pvt. Ltd. | |
10 | * Copyright (C) 2007 MontaVista Software, Inc. | |
11 | * Copyright (C) 2007-2008 Jason Wessel, Wind River Systems, Inc. | |
12 | */ | |
13 | /**************************************************************************** | |
14 | * Contributor: Lake Stevens Instrument Division$ | |
15 | * Written by: Glenn Engel $ | |
16 | * Updated by: Amit Kale<akale@veritas.com> | |
17 | * Updated by: Tom Rini <trini@kernel.crashing.org> | |
18 | * Updated by: Jason Wessel <jason.wessel@windriver.com> | |
19 | * Modified for 386 by Jim Kingdon, Cygnus Support. | |
d9f6e12f | 20 | * Original kgdb, compatibility with 2.1.xx kernel by |
82da3ff8 IM |
21 | * David Grothe <dave@gcom.com> |
22 | * Integrated into 2.2.5 kernel by Tigran Aivazian <tigran@sco.com> | |
23 | * X86_64 changes from Andi Kleen's patch merged by Jim Houston | |
24 | */ | |
25 | #include <linux/spinlock.h> | |
26 | #include <linux/kdebug.h> | |
27 | #include <linux/string.h> | |
28 | #include <linux/kernel.h> | |
29 | #include <linux/ptrace.h> | |
30 | #include <linux/sched.h> | |
31 | #include <linux/delay.h> | |
32 | #include <linux/kgdb.h> | |
82da3ff8 | 33 | #include <linux/smp.h> |
d3597524 | 34 | #include <linux/nmi.h> |
cc096749 | 35 | #include <linux/hw_breakpoint.h> |
3751d3e8 JW |
36 | #include <linux/uaccess.h> |
37 | #include <linux/memory.h> | |
82da3ff8 | 38 | |
35de5b06 | 39 | #include <asm/text-patching.h> |
62edab90 | 40 | #include <asm/debugreg.h> |
82da3ff8 | 41 | #include <asm/apicdef.h> |
7b6aa335 | 42 | #include <asm/apic.h> |
166d7514 | 43 | #include <asm/nmi.h> |
7b32aead | 44 | #include <asm/switch_to.h> |
82da3ff8 | 45 | |
12bfa3de | 46 | struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = |
82da3ff8 | 47 | { |
12bfa3de JW |
48 | #ifdef CONFIG_X86_32 |
49 | { "ax", 4, offsetof(struct pt_regs, ax) }, | |
50 | { "cx", 4, offsetof(struct pt_regs, cx) }, | |
51 | { "dx", 4, offsetof(struct pt_regs, dx) }, | |
52 | { "bx", 4, offsetof(struct pt_regs, bx) }, | |
53 | { "sp", 4, offsetof(struct pt_regs, sp) }, | |
54 | { "bp", 4, offsetof(struct pt_regs, bp) }, | |
55 | { "si", 4, offsetof(struct pt_regs, si) }, | |
56 | { "di", 4, offsetof(struct pt_regs, di) }, | |
57 | { "ip", 4, offsetof(struct pt_regs, ip) }, | |
58 | { "flags", 4, offsetof(struct pt_regs, flags) }, | |
59 | { "cs", 4, offsetof(struct pt_regs, cs) }, | |
60 | { "ss", 4, offsetof(struct pt_regs, ss) }, | |
61 | { "ds", 4, offsetof(struct pt_regs, ds) }, | |
62 | { "es", 4, offsetof(struct pt_regs, es) }, | |
12bfa3de JW |
63 | #else |
64 | { "ax", 8, offsetof(struct pt_regs, ax) }, | |
65 | { "bx", 8, offsetof(struct pt_regs, bx) }, | |
66 | { "cx", 8, offsetof(struct pt_regs, cx) }, | |
67 | { "dx", 8, offsetof(struct pt_regs, dx) }, | |
f59df35f | 68 | { "si", 8, offsetof(struct pt_regs, si) }, |
12bfa3de JW |
69 | { "di", 8, offsetof(struct pt_regs, di) }, |
70 | { "bp", 8, offsetof(struct pt_regs, bp) }, | |
71 | { "sp", 8, offsetof(struct pt_regs, sp) }, | |
72 | { "r8", 8, offsetof(struct pt_regs, r8) }, | |
73 | { "r9", 8, offsetof(struct pt_regs, r9) }, | |
74 | { "r10", 8, offsetof(struct pt_regs, r10) }, | |
75 | { "r11", 8, offsetof(struct pt_regs, r11) }, | |
76 | { "r12", 8, offsetof(struct pt_regs, r12) }, | |
77 | { "r13", 8, offsetof(struct pt_regs, r13) }, | |
78 | { "r14", 8, offsetof(struct pt_regs, r14) }, | |
79 | { "r15", 8, offsetof(struct pt_regs, r15) }, | |
80 | { "ip", 8, offsetof(struct pt_regs, ip) }, | |
81 | { "flags", 4, offsetof(struct pt_regs, flags) }, | |
82 | { "cs", 4, offsetof(struct pt_regs, cs) }, | |
83 | { "ss", 4, offsetof(struct pt_regs, ss) }, | |
639077fb JK |
84 | { "ds", 4, -1 }, |
85 | { "es", 4, -1 }, | |
703a1edc | 86 | #endif |
639077fb JK |
87 | { "fs", 4, -1 }, |
88 | { "gs", 4, -1 }, | |
12bfa3de JW |
89 | }; |
90 | ||
91 | int dbg_set_reg(int regno, void *mem, struct pt_regs *regs) | |
92 | { | |
93 | if ( | |
82da3ff8 | 94 | #ifdef CONFIG_X86_32 |
12bfa3de JW |
95 | regno == GDB_SS || regno == GDB_FS || regno == GDB_GS || |
96 | #endif | |
97 | regno == GDB_SP || regno == GDB_ORIG_AX) | |
98 | return 0; | |
99 | ||
100 | if (dbg_reg_def[regno].offset != -1) | |
101 | memcpy((void *)regs + dbg_reg_def[regno].offset, mem, | |
102 | dbg_reg_def[regno].size); | |
103 | return 0; | |
104 | } | |
105 | ||
106 | char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs) | |
107 | { | |
108 | if (regno == GDB_ORIG_AX) { | |
109 | memcpy(mem, ®s->orig_ax, sizeof(regs->orig_ax)); | |
110 | return "orig_ax"; | |
cf6f196d | 111 | } |
12bfa3de JW |
112 | if (regno >= DBG_MAX_REG_NUM || regno < 0) |
113 | return NULL; | |
114 | ||
115 | if (dbg_reg_def[regno].offset != -1) | |
116 | memcpy(mem, (void *)regs + dbg_reg_def[regno].offset, | |
117 | dbg_reg_def[regno].size); | |
118 | ||
12bfa3de | 119 | #ifdef CONFIG_X86_32 |
21431c29 | 120 | switch (regno) { |
12bfa3de JW |
121 | case GDB_GS: |
122 | case GDB_FS: | |
123 | *(unsigned long *)mem = 0xFFFF; | |
124 | break; | |
12bfa3de | 125 | } |
21431c29 | 126 | #endif |
12bfa3de | 127 | return dbg_reg_def[regno].name; |
82da3ff8 IM |
128 | } |
129 | ||
130 | /** | |
131 | * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs | |
132 | * @gdb_regs: A pointer to hold the registers in the order GDB wants. | |
133 | * @p: The &struct task_struct of the desired process. | |
134 | * | |
135 | * Convert the register values of the sleeping process in @p to | |
136 | * the format that GDB expects. | |
137 | * This function is called when kgdb does not have access to the | |
138 | * &struct pt_regs and therefore it should fill the gdb registers | |
139 | * @gdb_regs with what has been saved in &struct thread_struct | |
140 | * thread field during switch_to. | |
141 | */ | |
142 | void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p) | |
143 | { | |
703a1edc JW |
144 | #ifndef CONFIG_X86_32 |
145 | u32 *gdb_regs32 = (u32 *)gdb_regs; | |
146 | #endif | |
82da3ff8 IM |
147 | gdb_regs[GDB_AX] = 0; |
148 | gdb_regs[GDB_BX] = 0; | |
149 | gdb_regs[GDB_CX] = 0; | |
150 | gdb_regs[GDB_DX] = 0; | |
151 | gdb_regs[GDB_SI] = 0; | |
152 | gdb_regs[GDB_DI] = 0; | |
7b32aead | 153 | gdb_regs[GDB_BP] = ((struct inactive_task_frame *)p->thread.sp)->bp; |
82da3ff8 IM |
154 | #ifdef CONFIG_X86_32 |
155 | gdb_regs[GDB_DS] = __KERNEL_DS; | |
156 | gdb_regs[GDB_ES] = __KERNEL_DS; | |
157 | gdb_regs[GDB_PS] = 0; | |
158 | gdb_regs[GDB_CS] = __KERNEL_CS; | |
82da3ff8 IM |
159 | gdb_regs[GDB_SS] = __KERNEL_DS; |
160 | gdb_regs[GDB_FS] = 0xFFFF; | |
161 | gdb_regs[GDB_GS] = 0xFFFF; | |
162 | #else | |
16363019 | 163 | gdb_regs32[GDB_PS] = 0; |
703a1edc JW |
164 | gdb_regs32[GDB_CS] = __KERNEL_CS; |
165 | gdb_regs32[GDB_SS] = __KERNEL_DS; | |
82da3ff8 IM |
166 | gdb_regs[GDB_R8] = 0; |
167 | gdb_regs[GDB_R9] = 0; | |
168 | gdb_regs[GDB_R10] = 0; | |
169 | gdb_regs[GDB_R11] = 0; | |
170 | gdb_regs[GDB_R12] = 0; | |
171 | gdb_regs[GDB_R13] = 0; | |
172 | gdb_regs[GDB_R14] = 0; | |
173 | gdb_regs[GDB_R15] = 0; | |
174 | #endif | |
4e047aa7 | 175 | gdb_regs[GDB_PC] = 0; |
82da3ff8 IM |
176 | gdb_regs[GDB_SP] = p->thread.sp; |
177 | } | |
178 | ||
64e9ee30 JW |
179 | static struct hw_breakpoint { |
180 | unsigned enabled; | |
64e9ee30 | 181 | unsigned long addr; |
cc096749 JW |
182 | int len; |
183 | int type; | |
8c8aefce | 184 | struct perf_event * __percpu *pev; |
df493935 | 185 | } breakinfo[HBP_NUM]; |
64e9ee30 | 186 | |
031acd8c JW |
187 | static unsigned long early_dr7; |
188 | ||
64e9ee30 JW |
189 | static void kgdb_correct_hw_break(void) |
190 | { | |
64e9ee30 JW |
191 | int breakno; |
192 | ||
df493935 | 193 | for (breakno = 0; breakno < HBP_NUM; breakno++) { |
cc096749 JW |
194 | struct perf_event *bp; |
195 | struct arch_hw_breakpoint *info; | |
196 | int val; | |
197 | int cpu = raw_smp_processor_id(); | |
198 | if (!breakinfo[breakno].enabled) | |
199 | continue; | |
031acd8c JW |
200 | if (dbg_is_early) { |
201 | set_debugreg(breakinfo[breakno].addr, breakno); | |
202 | early_dr7 |= encode_dr7(breakno, | |
203 | breakinfo[breakno].len, | |
204 | breakinfo[breakno].type); | |
205 | set_debugreg(early_dr7, 7); | |
206 | continue; | |
207 | } | |
cc096749 JW |
208 | bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu); |
209 | info = counter_arch_bp(bp); | |
210 | if (bp->attr.disabled != 1) | |
211 | continue; | |
212 | bp->attr.bp_addr = breakinfo[breakno].addr; | |
213 | bp->attr.bp_len = breakinfo[breakno].len; | |
214 | bp->attr.bp_type = breakinfo[breakno].type; | |
215 | info->address = breakinfo[breakno].addr; | |
216 | info->len = breakinfo[breakno].len; | |
217 | info->type = breakinfo[breakno].type; | |
218 | val = arch_install_hw_breakpoint(bp); | |
219 | if (!val) | |
220 | bp->attr.disabled = 0; | |
64e9ee30 | 221 | } |
031acd8c JW |
222 | if (!dbg_is_early) |
223 | hw_breakpoint_restore(); | |
64e9ee30 JW |
224 | } |
225 | ||
5352ae63 JW |
226 | static int hw_break_reserve_slot(int breakno) |
227 | { | |
228 | int cpu; | |
229 | int cnt = 0; | |
230 | struct perf_event **pevent; | |
231 | ||
031acd8c JW |
232 | if (dbg_is_early) |
233 | return 0; | |
234 | ||
5352ae63 JW |
235 | for_each_online_cpu(cpu) { |
236 | cnt++; | |
237 | pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu); | |
238 | if (dbg_reserve_bp_slot(*pevent)) | |
239 | goto fail; | |
240 | } | |
241 | ||
242 | return 0; | |
243 | ||
244 | fail: | |
245 | for_each_online_cpu(cpu) { | |
246 | cnt--; | |
247 | if (!cnt) | |
248 | break; | |
249 | pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu); | |
250 | dbg_release_bp_slot(*pevent); | |
251 | } | |
252 | return -1; | |
253 | } | |
254 | ||
255 | static int hw_break_release_slot(int breakno) | |
256 | { | |
257 | struct perf_event **pevent; | |
258 | int cpu; | |
259 | ||
031acd8c JW |
260 | if (dbg_is_early) |
261 | return 0; | |
262 | ||
5352ae63 JW |
263 | for_each_online_cpu(cpu) { |
264 | pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu); | |
265 | if (dbg_release_bp_slot(*pevent)) | |
266 | /* | |
0d2eb44f | 267 | * The debugger is responsible for handing the retry on |
5352ae63 JW |
268 | * remove failure. |
269 | */ | |
270 | return -1; | |
271 | } | |
272 | return 0; | |
273 | } | |
274 | ||
64e9ee30 JW |
275 | static int |
276 | kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype) | |
277 | { | |
278 | int i; | |
279 | ||
df493935 | 280 | for (i = 0; i < HBP_NUM; i++) |
64e9ee30 JW |
281 | if (breakinfo[i].addr == addr && breakinfo[i].enabled) |
282 | break; | |
df493935 | 283 | if (i == HBP_NUM) |
64e9ee30 JW |
284 | return -1; |
285 | ||
5352ae63 JW |
286 | if (hw_break_release_slot(i)) { |
287 | printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr); | |
288 | return -1; | |
289 | } | |
64e9ee30 JW |
290 | breakinfo[i].enabled = 0; |
291 | ||
292 | return 0; | |
293 | } | |
294 | ||
295 | static void kgdb_remove_all_hw_break(void) | |
296 | { | |
297 | int i; | |
cc096749 JW |
298 | int cpu = raw_smp_processor_id(); |
299 | struct perf_event *bp; | |
64e9ee30 | 300 | |
df493935 | 301 | for (i = 0; i < HBP_NUM; i++) { |
cc096749 JW |
302 | if (!breakinfo[i].enabled) |
303 | continue; | |
304 | bp = *per_cpu_ptr(breakinfo[i].pev, cpu); | |
10a6e676 JW |
305 | if (!bp->attr.disabled) { |
306 | arch_uninstall_hw_breakpoint(bp); | |
307 | bp->attr.disabled = 1; | |
cc096749 | 308 | continue; |
10a6e676 | 309 | } |
031acd8c JW |
310 | if (dbg_is_early) |
311 | early_dr7 &= ~encode_dr7(i, breakinfo[i].len, | |
312 | breakinfo[i].type); | |
10a6e676 JW |
313 | else if (hw_break_release_slot(i)) |
314 | printk(KERN_ERR "KGDB: hw bpt remove failed %lx\n", | |
315 | breakinfo[i].addr); | |
316 | breakinfo[i].enabled = 0; | |
cc096749 | 317 | } |
64e9ee30 JW |
318 | } |
319 | ||
320 | static int | |
321 | kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype) | |
322 | { | |
64e9ee30 JW |
323 | int i; |
324 | ||
df493935 | 325 | for (i = 0; i < HBP_NUM; i++) |
64e9ee30 JW |
326 | if (!breakinfo[i].enabled) |
327 | break; | |
df493935 | 328 | if (i == HBP_NUM) |
64e9ee30 JW |
329 | return -1; |
330 | ||
331 | switch (bptype) { | |
332 | case BP_HARDWARE_BREAKPOINT: | |
cc096749 JW |
333 | len = 1; |
334 | breakinfo[i].type = X86_BREAKPOINT_EXECUTE; | |
64e9ee30 JW |
335 | break; |
336 | case BP_WRITE_WATCHPOINT: | |
cc096749 | 337 | breakinfo[i].type = X86_BREAKPOINT_WRITE; |
64e9ee30 JW |
338 | break; |
339 | case BP_ACCESS_WATCHPOINT: | |
cc096749 | 340 | breakinfo[i].type = X86_BREAKPOINT_RW; |
64e9ee30 JW |
341 | break; |
342 | default: | |
343 | return -1; | |
344 | } | |
cc096749 JW |
345 | switch (len) { |
346 | case 1: | |
347 | breakinfo[i].len = X86_BREAKPOINT_LEN_1; | |
348 | break; | |
349 | case 2: | |
350 | breakinfo[i].len = X86_BREAKPOINT_LEN_2; | |
351 | break; | |
352 | case 4: | |
353 | breakinfo[i].len = X86_BREAKPOINT_LEN_4; | |
354 | break; | |
355 | #ifdef CONFIG_X86_64 | |
356 | case 8: | |
357 | breakinfo[i].len = X86_BREAKPOINT_LEN_8; | |
358 | break; | |
359 | #endif | |
360 | default: | |
64e9ee30 | 361 | return -1; |
cc096749 | 362 | } |
64e9ee30 | 363 | breakinfo[i].addr = addr; |
5352ae63 JW |
364 | if (hw_break_reserve_slot(i)) { |
365 | breakinfo[i].addr = 0; | |
366 | return -1; | |
367 | } | |
cc096749 | 368 | breakinfo[i].enabled = 1; |
64e9ee30 JW |
369 | |
370 | return 0; | |
371 | } | |
372 | ||
373 | /** | |
374 | * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb. | |
375 | * @regs: Current &struct pt_regs. | |
376 | * | |
377 | * This function will be called if the particular architecture must | |
378 | * disable hardware debugging while it is processing gdb packets or | |
379 | * handling exception. | |
380 | */ | |
d7ba979d | 381 | static void kgdb_disable_hw_debug(struct pt_regs *regs) |
64e9ee30 | 382 | { |
cc096749 JW |
383 | int i; |
384 | int cpu = raw_smp_processor_id(); | |
385 | struct perf_event *bp; | |
386 | ||
64e9ee30 JW |
387 | /* Disable hardware debugging while we are in kgdb: */ |
388 | set_debugreg(0UL, 7); | |
df493935 | 389 | for (i = 0; i < HBP_NUM; i++) { |
cc096749 JW |
390 | if (!breakinfo[i].enabled) |
391 | continue; | |
031acd8c JW |
392 | if (dbg_is_early) { |
393 | early_dr7 &= ~encode_dr7(i, breakinfo[i].len, | |
394 | breakinfo[i].type); | |
395 | continue; | |
396 | } | |
cc096749 JW |
397 | bp = *per_cpu_ptr(breakinfo[i].pev, cpu); |
398 | if (bp->attr.disabled == 1) | |
399 | continue; | |
400 | arch_uninstall_hw_breakpoint(bp); | |
401 | bp->attr.disabled = 1; | |
402 | } | |
64e9ee30 JW |
403 | } |
404 | ||
82da3ff8 IM |
405 | #ifdef CONFIG_SMP |
406 | /** | |
407 | * kgdb_roundup_cpus - Get other CPUs into a holding pattern | |
82da3ff8 IM |
408 | * |
409 | * On SMP systems, we need to get the attention of the other CPUs | |
410 | * and get them be in a known state. This should do what is needed | |
411 | * to get the other CPUs to call kgdb_wait(). Note that on some arches, | |
412 | * the NMI approach is not used for rounding up all the CPUs. For example, | |
9ef7fa50 | 413 | * in case of MIPS, smp_call_function() is used to roundup CPUs. |
82da3ff8 IM |
414 | * |
415 | * On non-SMP systems, this is not called. | |
416 | */ | |
9ef7fa50 | 417 | void kgdb_roundup_cpus(void) |
82da3ff8 | 418 | { |
22ca7ee9 | 419 | apic_send_IPI_allbutself(NMI_VECTOR); |
82da3ff8 IM |
420 | } |
421 | #endif | |
422 | ||
423 | /** | |
424 | * kgdb_arch_handle_exception - Handle architecture specific GDB packets. | |
c15acff3 | 425 | * @e_vector: The error vector of the exception that happened. |
82da3ff8 IM |
426 | * @signo: The signal number of the exception that happened. |
427 | * @err_code: The error code of the exception that happened. | |
c15acff3 WL |
428 | * @remcomInBuffer: The buffer of the packet we have read. |
429 | * @remcomOutBuffer: The buffer of %BUFMAX bytes to write a packet into. | |
430 | * @linux_regs: The &struct pt_regs of the current process. | |
82da3ff8 IM |
431 | * |
432 | * This function MUST handle the 'c' and 's' command packets, | |
433 | * as well packets to set / remove a hardware breakpoint, if used. | |
434 | * If there are additional packets which the hardware needs to handle, | |
435 | * they are handled here. The code should return -1 if it wants to | |
436 | * process more packets, and a %0 or %1 if it wants to exit from the | |
437 | * kgdb callback. | |
438 | */ | |
439 | int kgdb_arch_handle_exception(int e_vector, int signo, int err_code, | |
440 | char *remcomInBuffer, char *remcomOutBuffer, | |
441 | struct pt_regs *linux_regs) | |
442 | { | |
443 | unsigned long addr; | |
444 | char *ptr; | |
82da3ff8 IM |
445 | |
446 | switch (remcomInBuffer[0]) { | |
447 | case 'c': | |
448 | case 's': | |
449 | /* try to read optional parameter, pc unchanged if no parm */ | |
450 | ptr = &remcomInBuffer[1]; | |
451 | if (kgdb_hex2long(&ptr, &addr)) | |
452 | linux_regs->ip = addr; | |
df561f66 | 453 | fallthrough; |
737a460f JW |
454 | case 'D': |
455 | case 'k': | |
82da3ff8 | 456 | /* clear the trace bit */ |
fda31d7d | 457 | linux_regs->flags &= ~X86_EFLAGS_TF; |
82da3ff8 IM |
458 | atomic_set(&kgdb_cpu_doing_single_step, -1); |
459 | ||
460 | /* set the trace bit if we're stepping */ | |
461 | if (remcomInBuffer[0] == 's') { | |
fda31d7d | 462 | linux_regs->flags |= X86_EFLAGS_TF; |
d7161a65 JW |
463 | atomic_set(&kgdb_cpu_doing_single_step, |
464 | raw_smp_processor_id()); | |
82da3ff8 IM |
465 | } |
466 | ||
467 | return 0; | |
468 | } | |
469 | ||
470 | /* this means that we do not want to exit from the handler: */ | |
471 | return -1; | |
472 | } | |
473 | ||
474 | static inline int | |
475 | single_step_cont(struct pt_regs *regs, struct die_args *args) | |
476 | { | |
477 | /* | |
478 | * Single step exception from kernel space to user space so | |
479 | * eat the exception and continue the process: | |
480 | */ | |
481 | printk(KERN_ERR "KGDB: trap/step from kernel to user space, " | |
482 | "resuming...\n"); | |
483 | kgdb_arch_handle_exception(args->trapnr, args->signr, | |
484 | args->err, "c", "", regs); | |
62edab90 P |
485 | /* |
486 | * Reset the BS bit in dr6 (pointed by args->err) to | |
487 | * denote completion of processing | |
488 | */ | |
489 | (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP; | |
82da3ff8 IM |
490 | |
491 | return NOTIFY_STOP; | |
492 | } | |
493 | ||
0d44975d | 494 | static DECLARE_BITMAP(was_in_debug_nmi, NR_CPUS); |
d3597524 | 495 | |
9c48f1c6 | 496 | static int kgdb_nmi_handler(unsigned int cmd, struct pt_regs *regs) |
82da3ff8 | 497 | { |
0d44975d DV |
498 | int cpu; |
499 | ||
82da3ff8 | 500 | switch (cmd) { |
9c48f1c6 | 501 | case NMI_LOCAL: |
82da3ff8 IM |
502 | if (atomic_read(&kgdb_active) != -1) { |
503 | /* KGDB CPU roundup */ | |
0d44975d DV |
504 | cpu = raw_smp_processor_id(); |
505 | kgdb_nmicallback(cpu, regs); | |
506 | set_bit(cpu, was_in_debug_nmi); | |
d3597524 | 507 | touch_nmi_watchdog(); |
0d44975d | 508 | |
9c48f1c6 | 509 | return NMI_HANDLED; |
82da3ff8 | 510 | } |
9c48f1c6 | 511 | break; |
82da3ff8 | 512 | |
9c48f1c6 | 513 | case NMI_UNKNOWN: |
0d44975d DV |
514 | cpu = raw_smp_processor_id(); |
515 | ||
516 | if (__test_and_clear_bit(cpu, was_in_debug_nmi)) | |
9c48f1c6 | 517 | return NMI_HANDLED; |
0d44975d | 518 | |
9c48f1c6 DZ |
519 | break; |
520 | default: | |
521 | /* do nothing */ | |
522 | break; | |
523 | } | |
524 | return NMI_DONE; | |
525 | } | |
526 | ||
527 | static int __kgdb_notify(struct die_args *args, unsigned long cmd) | |
528 | { | |
529 | struct pt_regs *regs = args->regs; | |
82da3ff8 | 530 | |
9c48f1c6 | 531 | switch (cmd) { |
82da3ff8 | 532 | case DIE_DEBUG: |
cc096749 | 533 | if (atomic_read(&kgdb_cpu_doing_single_step) != -1) { |
d7161a65 JW |
534 | if (user_mode(regs)) |
535 | return single_step_cont(regs, args); | |
536 | break; | |
537 | } else if (test_thread_flag(TIF_SINGLESTEP)) | |
538 | /* This means a user thread is single stepping | |
539 | * a system call which should be ignored | |
540 | */ | |
541 | return NOTIFY_DONE; | |
df561f66 | 542 | fallthrough; |
82da3ff8 IM |
543 | default: |
544 | if (user_mode(regs)) | |
545 | return NOTIFY_DONE; | |
546 | } | |
547 | ||
f503b5ae | 548 | if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs)) |
82da3ff8 IM |
549 | return NOTIFY_DONE; |
550 | ||
737a460f JW |
551 | /* Must touch watchdog before return to normal operation */ |
552 | touch_nmi_watchdog(); | |
82da3ff8 IM |
553 | return NOTIFY_STOP; |
554 | } | |
555 | ||
f503b5ae JW |
556 | int kgdb_ll_trap(int cmd, const char *str, |
557 | struct pt_regs *regs, long err, int trap, int sig) | |
558 | { | |
559 | struct die_args args = { | |
560 | .regs = regs, | |
561 | .str = str, | |
562 | .err = err, | |
563 | .trapnr = trap, | |
564 | .signr = sig, | |
565 | ||
566 | }; | |
567 | ||
568 | if (!kgdb_io_module_registered) | |
569 | return NOTIFY_DONE; | |
570 | ||
571 | return __kgdb_notify(&args, cmd); | |
572 | } | |
f503b5ae | 573 | |
82da3ff8 IM |
574 | static int |
575 | kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr) | |
576 | { | |
577 | unsigned long flags; | |
578 | int ret; | |
579 | ||
580 | local_irq_save(flags); | |
581 | ret = __kgdb_notify(ptr, cmd); | |
582 | local_irq_restore(flags); | |
583 | ||
584 | return ret; | |
585 | } | |
586 | ||
587 | static struct notifier_block kgdb_notifier = { | |
588 | .notifier_call = kgdb_notify, | |
82da3ff8 IM |
589 | }; |
590 | ||
591 | /** | |
6a6256f9 | 592 | * kgdb_arch_init - Perform any architecture specific initialization. |
82da3ff8 | 593 | * |
6a6256f9 | 594 | * This function will handle the initialization of any architecture |
82da3ff8 IM |
595 | * specific callbacks. |
596 | */ | |
597 | int kgdb_arch_init(void) | |
0b4b3827 | 598 | { |
9c48f1c6 DZ |
599 | int retval; |
600 | ||
601 | retval = register_die_notifier(&kgdb_notifier); | |
602 | if (retval) | |
603 | goto out; | |
604 | ||
605 | retval = register_nmi_handler(NMI_LOCAL, kgdb_nmi_handler, | |
606 | 0, "kgdb"); | |
607 | if (retval) | |
608 | goto out1; | |
609 | ||
610 | retval = register_nmi_handler(NMI_UNKNOWN, kgdb_nmi_handler, | |
611 | 0, "kgdb"); | |
612 | ||
613 | if (retval) | |
614 | goto out2; | |
615 | ||
616 | return retval; | |
617 | ||
618 | out2: | |
619 | unregister_nmi_handler(NMI_LOCAL, "kgdb"); | |
620 | out1: | |
621 | unregister_die_notifier(&kgdb_notifier); | |
622 | out: | |
623 | return retval; | |
0b4b3827 JW |
624 | } |
625 | ||
a8b0ca17 | 626 | static void kgdb_hw_overflow_handler(struct perf_event *event, |
ba773f7c JW |
627 | struct perf_sample_data *data, struct pt_regs *regs) |
628 | { | |
fad99fac JW |
629 | struct task_struct *tsk = current; |
630 | int i; | |
631 | ||
d53d9bc0 | 632 | for (i = 0; i < 4; i++) { |
fad99fac | 633 | if (breakinfo[i].enabled) |
d53d9bc0 PZ |
634 | tsk->thread.virtual_dr6 |= (DR_TRAP0 << i); |
635 | } | |
ba773f7c JW |
636 | } |
637 | ||
0b4b3827 | 638 | void kgdb_arch_late(void) |
82da3ff8 | 639 | { |
cc096749 | 640 | int i, cpu; |
cc096749 JW |
641 | struct perf_event_attr attr; |
642 | struct perf_event **pevent; | |
643 | ||
cc096749 | 644 | /* |
d9f6e12f | 645 | * Pre-allocate the hw breakpoint instructions in the non-atomic |
cc096749 JW |
646 | * portion of kgdb because this operation requires mutexs to |
647 | * complete. | |
648 | */ | |
ab310b5e | 649 | hw_breakpoint_init(&attr); |
cc096749 | 650 | attr.bp_addr = (unsigned long)kgdb_arch_init; |
cc096749 JW |
651 | attr.bp_len = HW_BREAKPOINT_LEN_1; |
652 | attr.bp_type = HW_BREAKPOINT_W; | |
653 | attr.disabled = 1; | |
df493935 | 654 | for (i = 0; i < HBP_NUM; i++) { |
0b4b3827 JW |
655 | if (breakinfo[i].pev) |
656 | continue; | |
4dc0da86 | 657 | breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL); |
91b152aa | 658 | if (IS_ERR((void * __force)breakinfo[i].pev)) { |
0b4b3827 JW |
659 | printk(KERN_ERR "kgdb: Could not allocate hw" |
660 | "breakpoints\nDisabling the kernel debugger\n"); | |
cc096749 JW |
661 | breakinfo[i].pev = NULL; |
662 | kgdb_arch_exit(); | |
0b4b3827 | 663 | return; |
cc096749 JW |
664 | } |
665 | for_each_online_cpu(cpu) { | |
666 | pevent = per_cpu_ptr(breakinfo[i].pev, cpu); | |
667 | pevent[0]->hw.sample_period = 1; | |
ba773f7c | 668 | pevent[0]->overflow_handler = kgdb_hw_overflow_handler; |
cc096749 JW |
669 | if (pevent[0]->destroy != NULL) { |
670 | pevent[0]->destroy = NULL; | |
671 | release_bp_slot(*pevent); | |
672 | } | |
673 | } | |
674 | } | |
82da3ff8 IM |
675 | } |
676 | ||
677 | /** | |
678 | * kgdb_arch_exit - Perform any architecture specific uninitalization. | |
679 | * | |
680 | * This function will handle the uninitalization of any architecture | |
681 | * specific callbacks, for dynamic registration and unregistration. | |
682 | */ | |
683 | void kgdb_arch_exit(void) | |
684 | { | |
cc096749 JW |
685 | int i; |
686 | for (i = 0; i < 4; i++) { | |
687 | if (breakinfo[i].pev) { | |
688 | unregister_wide_hw_breakpoint(breakinfo[i].pev); | |
689 | breakinfo[i].pev = NULL; | |
690 | } | |
691 | } | |
9c48f1c6 DZ |
692 | unregister_nmi_handler(NMI_UNKNOWN, "kgdb"); |
693 | unregister_nmi_handler(NMI_LOCAL, "kgdb"); | |
82da3ff8 IM |
694 | unregister_die_notifier(&kgdb_notifier); |
695 | } | |
696 | ||
697 | /** | |
698 | * | |
699 | * kgdb_skipexception - Bail out of KGDB when we've been triggered. | |
700 | * @exception: Exception vector number | |
701 | * @regs: Current &struct pt_regs. | |
702 | * | |
703 | * On some architectures we need to skip a breakpoint exception when | |
704 | * it occurs after a breakpoint has been removed. | |
705 | * | |
706 | * Skip an int3 exception when it occurs after a breakpoint has been | |
707 | * removed. Backtrack eip by 1 since the int3 would have caused it to | |
708 | * increment by 1. | |
709 | */ | |
710 | int kgdb_skipexception(int exception, struct pt_regs *regs) | |
711 | { | |
712 | if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) { | |
713 | regs->ip -= 1; | |
714 | return 1; | |
715 | } | |
716 | return 0; | |
717 | } | |
718 | ||
719 | unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs) | |
720 | { | |
721 | if (exception == 3) | |
722 | return instruction_pointer(regs) - 1; | |
723 | return instruction_pointer(regs); | |
724 | } | |
725 | ||
dcc78711 JW |
726 | void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip) |
727 | { | |
728 | regs->ip = ip; | |
729 | } | |
730 | ||
3751d3e8 JW |
731 | int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt) |
732 | { | |
733 | int err; | |
3751d3e8 JW |
734 | |
735 | bpt->type = BP_BREAKPOINT; | |
fe557319 | 736 | err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr, |
3751d3e8 JW |
737 | BREAK_INSTR_SIZE); |
738 | if (err) | |
739 | return err; | |
fe557319 | 740 | err = copy_to_kernel_nofault((char *)bpt->bpt_addr, |
3751d3e8 | 741 | arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE); |
3751d3e8 JW |
742 | if (!err) |
743 | return err; | |
744 | /* | |
e836673c | 745 | * It is safe to call text_poke_kgdb() because normal kernel execution |
3751d3e8 JW |
746 | * is stopped on all cores, so long as the text_mutex is not locked. |
747 | */ | |
748 | if (mutex_is_locked(&text_mutex)) | |
749 | return -EBUSY; | |
e836673c NA |
750 | text_poke_kgdb((void *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr, |
751 | BREAK_INSTR_SIZE); | |
3751d3e8 | 752 | bpt->type = BP_POKE_BREAKPOINT; |
9ccaf77c | 753 | |
71ab8323 | 754 | return 0; |
3751d3e8 JW |
755 | } |
756 | ||
757 | int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt) | |
758 | { | |
3751d3e8 JW |
759 | if (bpt->type != BP_POKE_BREAKPOINT) |
760 | goto knl_write; | |
761 | /* | |
e836673c | 762 | * It is safe to call text_poke_kgdb() because normal kernel execution |
3751d3e8 JW |
763 | * is stopped on all cores, so long as the text_mutex is not locked. |
764 | */ | |
765 | if (mutex_is_locked(&text_mutex)) | |
766 | goto knl_write; | |
e836673c NA |
767 | text_poke_kgdb((void *)bpt->bpt_addr, bpt->saved_instr, |
768 | BREAK_INSTR_SIZE); | |
86a22057 | 769 | return 0; |
9ccaf77c | 770 | |
3751d3e8 | 771 | knl_write: |
fe557319 | 772 | return copy_to_kernel_nofault((char *)bpt->bpt_addr, |
3751d3e8 JW |
773 | (char *)bpt->saved_instr, BREAK_INSTR_SIZE); |
774 | } | |
775 | ||
cc028297 | 776 | const struct kgdb_arch arch_kgdb_ops = { |
82da3ff8 IM |
777 | /* Breakpoint instruction: */ |
778 | .gdb_bpt_instr = { 0xcc }, | |
64e9ee30 JW |
779 | .flags = KGDB_HW_BREAKPOINT, |
780 | .set_hw_breakpoint = kgdb_set_hw_break, | |
781 | .remove_hw_breakpoint = kgdb_remove_hw_break, | |
d7ba979d | 782 | .disable_hw_break = kgdb_disable_hw_debug, |
64e9ee30 JW |
783 | .remove_all_hw_break = kgdb_remove_all_hw_break, |
784 | .correct_hw_break = kgdb_correct_hw_break, | |
82da3ff8 | 785 | }; |