blob: 68acd30c6b878bacb65244c0edb1e3ddbdde52c0 [file] [log] [blame]
Thomas Gleixner3e0a4e82019-05-23 11:14:55 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Ingo Molnar82da3ff2008-04-17 20:05:37 +02002/*
Ingo Molnar82da3ff2008-04-17 20:05:37 +02003 */
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.
20 * Origianl kgdb, compatibility with 2.1.xx kernel by
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>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020033#include <linux/smp.h>
Jason Wesseld3597522008-02-15 14:55:53 -060034#include <linux/nmi.h>
Jason Wesselcc096742010-01-28 17:04:42 -060035#include <linux/hw_breakpoint.h>
Jason Wessel3751d3e2012-03-23 09:35:05 -050036#include <linux/uaccess.h>
37#include <linux/memory.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020038
Andy Lutomirski35de5b02016-04-26 12:23:24 -070039#include <asm/text-patching.h>
K.Prasad62edab92009-06-01 23:47:06 +053040#include <asm/debugreg.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020041#include <asm/apicdef.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010042#include <asm/apic.h>
Don Zickus166d7512011-01-06 16:18:49 -050043#include <asm/nmi.h>
Brian Gerst7b32aea2016-08-13 12:38:18 -040044#include <asm/switch_to.h>
Ingo Molnar82da3ff2008-04-17 20:05:37 +020045
Jason Wessel12bfa3d2010-08-05 09:22:20 -050046struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
Ingo Molnar82da3ff2008-04-17 20:05:37 +020047{
Ingo Molnar82da3ff2008-04-17 20:05:37 +020048#ifdef CONFIG_X86_32
Jason Wessel12bfa3d2010-08-05 09:22:20 -050049 { "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) },
Ingo Molnar82da3ff2008-04-17 20:05:37 +020063#else
Jason Wessel12bfa3d2010-08-05 09:22:20 -050064 { "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) },
Steffen Liebergeldf59df352015-04-02 11:01:59 +020068 { "si", 8, offsetof(struct pt_regs, si) },
Jason Wessel12bfa3d2010-08-05 09:22:20 -050069 { "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) },
Jan Kiszka639077f2012-03-19 15:16:48 -050084 { "ds", 4, -1 },
85 { "es", 4, -1 },
Jason Wesselcf6f1962009-12-11 08:43:16 -060086#endif
Jan Kiszka639077f2012-03-19 15:16:48 -050087 { "fs", 4, -1 },
88 { "gs", 4, -1 },
Jason Wessel12bfa3d2010-08-05 09:22:20 -050089};
90
91int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
92{
93 if (
94#ifdef CONFIG_X86_32
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
106char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
107{
108 if (regno == GDB_ORIG_AX) {
109 memcpy(mem, &regs->orig_ax, sizeof(regs->orig_ax));
110 return "orig_ax";
111 }
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
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500119#ifdef CONFIG_X86_32
Jason Wessel21431c22010-03-15 07:28:00 -0500120 switch (regno) {
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500121 case GDB_GS:
122 case GDB_FS:
123 *(unsigned long *)mem = 0xFFFF;
124 break;
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500125 }
Jason Wessel21431c22010-03-15 07:28:00 -0500126#endif
Jason Wessel12bfa3d2010-08-05 09:22:20 -0500127 return dbg_reg_def[regno].name;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200128}
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 */
142void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
143{
Jason Wessel703a1ed2008-09-26 10:36:42 -0500144#ifndef CONFIG_X86_32
145 u32 *gdb_regs32 = (u32 *)gdb_regs;
146#endif
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200147 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;
Brian Gerst7b32aea2016-08-13 12:38:18 -0400153 gdb_regs[GDB_BP] = ((struct inactive_task_frame *)p->thread.sp)->bp;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200154#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;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200159 gdb_regs[GDB_SS] = __KERNEL_DS;
160 gdb_regs[GDB_FS] = 0xFFFF;
161 gdb_regs[GDB_GS] = 0xFFFF;
162#else
Brian Gerst16363012016-08-13 12:38:17 -0400163 gdb_regs32[GDB_PS] = 0;
Jason Wessel703a1ed2008-09-26 10:36:42 -0500164 gdb_regs32[GDB_CS] = __KERNEL_CS;
165 gdb_regs32[GDB_SS] = __KERNEL_DS;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200166 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
Brian Gerst4e047aa2016-08-13 12:38:16 -0400175 gdb_regs[GDB_PC] = 0;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200176 gdb_regs[GDB_SP] = p->thread.sp;
177}
178
Jason Wessel64e9ee32008-02-15 14:55:56 -0600179static struct hw_breakpoint {
180 unsigned enabled;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600181 unsigned long addr;
Jason Wesselcc096742010-01-28 17:04:42 -0600182 int len;
183 int type;
Namhyung Kim8c8aefc2010-08-07 11:00:59 -0700184 struct perf_event * __percpu *pev;
Dongdong Dengdf493932010-08-05 09:22:25 -0500185} breakinfo[HBP_NUM];
Jason Wessel64e9ee32008-02-15 14:55:56 -0600186
Jason Wessel031acd82010-05-20 21:04:30 -0500187static unsigned long early_dr7;
188
Jason Wessel64e9ee32008-02-15 14:55:56 -0600189static void kgdb_correct_hw_break(void)
190{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600191 int breakno;
192
Dongdong Dengdf493932010-08-05 09:22:25 -0500193 for (breakno = 0; breakno < HBP_NUM; breakno++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600194 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;
Jason Wessel031acd82010-05-20 21:04:30 -0500200 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 }
Jason Wesselcc096742010-01-28 17:04:42 -0600208 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;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600221 }
Jason Wessel031acd82010-05-20 21:04:30 -0500222 if (!dbg_is_early)
223 hw_breakpoint_restore();
Jason Wessel64e9ee32008-02-15 14:55:56 -0600224}
225
Jason Wessel5352ae632010-01-28 17:04:43 -0600226static int hw_break_reserve_slot(int breakno)
227{
228 int cpu;
229 int cnt = 0;
230 struct perf_event **pevent;
231
Jason Wessel031acd82010-05-20 21:04:30 -0500232 if (dbg_is_early)
233 return 0;
234
Jason Wessel5352ae632010-01-28 17:04:43 -0600235 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
244fail:
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
255static int hw_break_release_slot(int breakno)
256{
257 struct perf_event **pevent;
258 int cpu;
259
Jason Wessel031acd82010-05-20 21:04:30 -0500260 if (dbg_is_early)
261 return 0;
262
Jason Wessel5352ae632010-01-28 17:04:43 -0600263 for_each_online_cpu(cpu) {
264 pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
265 if (dbg_release_bp_slot(*pevent))
266 /*
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300267 * The debugger is responsible for handing the retry on
Jason Wessel5352ae632010-01-28 17:04:43 -0600268 * remove failure.
269 */
270 return -1;
271 }
272 return 0;
273}
274
Jason Wessel64e9ee32008-02-15 14:55:56 -0600275static int
276kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
277{
278 int i;
279
Dongdong Dengdf493932010-08-05 09:22:25 -0500280 for (i = 0; i < HBP_NUM; i++)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600281 if (breakinfo[i].addr == addr && breakinfo[i].enabled)
282 break;
Dongdong Dengdf493932010-08-05 09:22:25 -0500283 if (i == HBP_NUM)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600284 return -1;
285
Jason Wessel5352ae632010-01-28 17:04:43 -0600286 if (hw_break_release_slot(i)) {
287 printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
288 return -1;
289 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600290 breakinfo[i].enabled = 0;
291
292 return 0;
293}
294
295static void kgdb_remove_all_hw_break(void)
296{
297 int i;
Jason Wesselcc096742010-01-28 17:04:42 -0600298 int cpu = raw_smp_processor_id();
299 struct perf_event *bp;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600300
Dongdong Dengdf493932010-08-05 09:22:25 -0500301 for (i = 0; i < HBP_NUM; i++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600302 if (!breakinfo[i].enabled)
303 continue;
304 bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
Jason Wessel10a6e672010-11-15 08:07:35 -0600305 if (!bp->attr.disabled) {
306 arch_uninstall_hw_breakpoint(bp);
307 bp->attr.disabled = 1;
Jason Wesselcc096742010-01-28 17:04:42 -0600308 continue;
Jason Wessel10a6e672010-11-15 08:07:35 -0600309 }
Jason Wessel031acd82010-05-20 21:04:30 -0500310 if (dbg_is_early)
311 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
312 breakinfo[i].type);
Jason Wessel10a6e672010-11-15 08:07:35 -0600313 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;
Jason Wesselcc096742010-01-28 17:04:42 -0600317 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600318}
319
320static int
321kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
322{
Jason Wessel64e9ee32008-02-15 14:55:56 -0600323 int i;
324
Dongdong Dengdf493932010-08-05 09:22:25 -0500325 for (i = 0; i < HBP_NUM; i++)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600326 if (!breakinfo[i].enabled)
327 break;
Dongdong Dengdf493932010-08-05 09:22:25 -0500328 if (i == HBP_NUM)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600329 return -1;
330
331 switch (bptype) {
332 case BP_HARDWARE_BREAKPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600333 len = 1;
334 breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600335 break;
336 case BP_WRITE_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600337 breakinfo[i].type = X86_BREAKPOINT_WRITE;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600338 break;
339 case BP_ACCESS_WATCHPOINT:
Jason Wesselcc096742010-01-28 17:04:42 -0600340 breakinfo[i].type = X86_BREAKPOINT_RW;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600341 break;
342 default:
343 return -1;
344 }
Jason Wesselcc096742010-01-28 17:04:42 -0600345 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:
Jason Wessel64e9ee32008-02-15 14:55:56 -0600361 return -1;
Jason Wesselcc096742010-01-28 17:04:42 -0600362 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600363 breakinfo[i].addr = addr;
Jason Wessel5352ae632010-01-28 17:04:43 -0600364 if (hw_break_reserve_slot(i)) {
365 breakinfo[i].addr = 0;
366 return -1;
367 }
Jason Wesselcc096742010-01-28 17:04:42 -0600368 breakinfo[i].enabled = 1;
Jason Wessel64e9ee32008-02-15 14:55:56 -0600369
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 */
Dongdong Dengd7ba979d2010-08-18 06:02:00 -0500381static void kgdb_disable_hw_debug(struct pt_regs *regs)
Jason Wessel64e9ee32008-02-15 14:55:56 -0600382{
Jason Wesselcc096742010-01-28 17:04:42 -0600383 int i;
384 int cpu = raw_smp_processor_id();
385 struct perf_event *bp;
386
Jason Wessel64e9ee32008-02-15 14:55:56 -0600387 /* Disable hardware debugging while we are in kgdb: */
388 set_debugreg(0UL, 7);
Dongdong Dengdf493932010-08-05 09:22:25 -0500389 for (i = 0; i < HBP_NUM; i++) {
Jason Wesselcc096742010-01-28 17:04:42 -0600390 if (!breakinfo[i].enabled)
391 continue;
Jason Wessel031acd82010-05-20 21:04:30 -0500392 if (dbg_is_early) {
393 early_dr7 &= ~encode_dr7(i, breakinfo[i].len,
394 breakinfo[i].type);
395 continue;
396 }
Jason Wesselcc096742010-01-28 17:04:42 -0600397 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 }
Jason Wessel64e9ee32008-02-15 14:55:56 -0600403}
404
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200405#ifdef CONFIG_SMP
406/**
407 * kgdb_roundup_cpus - Get other CPUs into a holding pattern
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200408 *
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,
Douglas Anderson9ef7fa52018-12-04 19:38:25 -0800413 * in case of MIPS, smp_call_function() is used to roundup CPUs.
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200414 *
415 * On non-SMP systems, this is not called.
416 */
Douglas Anderson9ef7fa52018-12-04 19:38:25 -0800417void kgdb_roundup_cpus(void)
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200418{
Thomas Gleixner22ca7ee2019-07-22 20:47:23 +0200419 apic_send_IPI_allbutself(NMI_VECTOR);
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200420}
421#endif
422
423/**
424 * kgdb_arch_handle_exception - Handle architecture specific GDB packets.
Wanpeng Lic15acff2012-06-12 12:53:21 +0800425 * @e_vector: The error vector of the exception that happened.
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200426 * @signo: The signal number of the exception that happened.
427 * @err_code: The error code of the exception that happened.
Wanpeng Lic15acff2012-06-12 12:53:21 +0800428 * @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.
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200431 *
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 */
439int 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;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200445
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;
Gustavo A. R. Silva6fcebf12019-01-25 12:39:03 -0600453 /* fall through */
Jason Wessel737a4602008-03-07 16:34:16 -0600454 case 'D':
455 case 'k':
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200456 /* clear the trace bit */
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700457 linux_regs->flags &= ~X86_EFLAGS_TF;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200458 atomic_set(&kgdb_cpu_doing_single_step, -1);
459
460 /* set the trace bit if we're stepping */
461 if (remcomInBuffer[0] == 's') {
Harvey Harrisonfda31d72008-04-18 09:54:38 -0700462 linux_regs->flags |= X86_EFLAGS_TF;
Jason Wesseld7161a62008-09-26 10:36:41 -0500463 atomic_set(&kgdb_cpu_doing_single_step,
464 raw_smp_processor_id());
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200465 }
466
467 return 0;
468 }
469
470 /* this means that we do not want to exit from the handler: */
471 return -1;
472}
473
474static inline int
475single_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);
K.Prasad62edab92009-06-01 23:47:06 +0530485 /*
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;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200490
491 return NOTIFY_STOP;
492}
493
Denys Vlasenko0d449752015-09-26 14:47:17 +0200494static DECLARE_BITMAP(was_in_debug_nmi, NR_CPUS);
Jason Wesseld3597522008-02-15 14:55:53 -0600495
Don Zickus9c48f1c2011-09-30 15:06:21 -0400496static int kgdb_nmi_handler(unsigned int cmd, struct pt_regs *regs)
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200497{
Denys Vlasenko0d449752015-09-26 14:47:17 +0200498 int cpu;
499
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200500 switch (cmd) {
Don Zickus9c48f1c2011-09-30 15:06:21 -0400501 case NMI_LOCAL:
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200502 if (atomic_read(&kgdb_active) != -1) {
503 /* KGDB CPU roundup */
Denys Vlasenko0d449752015-09-26 14:47:17 +0200504 cpu = raw_smp_processor_id();
505 kgdb_nmicallback(cpu, regs);
506 set_bit(cpu, was_in_debug_nmi);
Jason Wesseld3597522008-02-15 14:55:53 -0600507 touch_nmi_watchdog();
Denys Vlasenko0d449752015-09-26 14:47:17 +0200508
Don Zickus9c48f1c2011-09-30 15:06:21 -0400509 return NMI_HANDLED;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200510 }
Don Zickus9c48f1c2011-09-30 15:06:21 -0400511 break;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200512
Don Zickus9c48f1c2011-09-30 15:06:21 -0400513 case NMI_UNKNOWN:
Denys Vlasenko0d449752015-09-26 14:47:17 +0200514 cpu = raw_smp_processor_id();
515
516 if (__test_and_clear_bit(cpu, was_in_debug_nmi))
Don Zickus9c48f1c2011-09-30 15:06:21 -0400517 return NMI_HANDLED;
Denys Vlasenko0d449752015-09-26 14:47:17 +0200518
Don Zickus9c48f1c2011-09-30 15:06:21 -0400519 break;
520 default:
521 /* do nothing */
522 break;
523 }
524 return NMI_DONE;
525}
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200526
Don Zickus9c48f1c2011-09-30 15:06:21 -0400527static int __kgdb_notify(struct die_args *args, unsigned long cmd)
528{
529 struct pt_regs *regs = args->regs;
530
531 switch (cmd) {
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200532 case DIE_DEBUG:
Jason Wesselcc096742010-01-28 17:04:42 -0600533 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
Jason Wesseld7161a62008-09-26 10:36:41 -0500534 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;
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200542 /* fall through */
543 default:
544 if (user_mode(regs))
545 return NOTIFY_DONE;
546 }
547
Jason Wesself503b5a2010-05-20 21:04:25 -0500548 if (kgdb_handle_exception(args->trapnr, args->signr, cmd, regs))
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200549 return NOTIFY_DONE;
550
Jason Wessel737a4602008-03-07 16:34:16 -0600551 /* Must touch watchdog before return to normal operation */
552 touch_nmi_watchdog();
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200553 return NOTIFY_STOP;
554}
555
Jason Wesself503b5a2010-05-20 21:04:25 -0500556int 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}
Jason Wesself503b5a2010-05-20 21:04:25 -0500573
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200574static int
575kgdb_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
587static struct notifier_block kgdb_notifier = {
588 .notifier_call = kgdb_notify,
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200589};
590
591/**
Adam Buchbinder6a6256f2016-02-23 15:34:30 -0800592 * kgdb_arch_init - Perform any architecture specific initialization.
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200593 *
Adam Buchbinder6a6256f2016-02-23 15:34:30 -0800594 * This function will handle the initialization of any architecture
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200595 * specific callbacks.
596 */
597int kgdb_arch_init(void)
598{
Don Zickus9c48f1c2011-09-30 15:06:21 -0400599 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
618out2:
619 unregister_nmi_handler(NMI_LOCAL, "kgdb");
620out1:
621 unregister_die_notifier(&kgdb_notifier);
622out:
623 return retval;
Jason Wessel0b4b3822010-05-20 21:04:29 -0500624}
625
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200626static void kgdb_hw_overflow_handler(struct perf_event *event,
Jason Wesselba773f72010-07-28 19:10:30 -0500627 struct perf_sample_data *data, struct pt_regs *regs)
628{
Jason Wesselfad99fa2010-10-20 08:20:00 -0500629 struct task_struct *tsk = current;
630 int i;
631
632 for (i = 0; i < 4; i++)
633 if (breakinfo[i].enabled)
634 tsk->thread.debugreg6 |= (DR_TRAP0 << i);
Jason Wesselba773f72010-07-28 19:10:30 -0500635}
636
Jason Wessel0b4b3822010-05-20 21:04:29 -0500637void kgdb_arch_late(void)
638{
Jason Wesselcc096742010-01-28 17:04:42 -0600639 int i, cpu;
Jason Wesselcc096742010-01-28 17:04:42 -0600640 struct perf_event_attr attr;
641 struct perf_event **pevent;
642
Jason Wesselcc096742010-01-28 17:04:42 -0600643 /*
644 * Pre-allocate the hw breakpoint structions in the non-atomic
645 * portion of kgdb because this operation requires mutexs to
646 * complete.
647 */
Jason Wesselab310b52010-03-30 14:05:07 -0500648 hw_breakpoint_init(&attr);
Jason Wesselcc096742010-01-28 17:04:42 -0600649 attr.bp_addr = (unsigned long)kgdb_arch_init;
Jason Wesselcc096742010-01-28 17:04:42 -0600650 attr.bp_len = HW_BREAKPOINT_LEN_1;
651 attr.bp_type = HW_BREAKPOINT_W;
652 attr.disabled = 1;
Dongdong Dengdf493932010-08-05 09:22:25 -0500653 for (i = 0; i < HBP_NUM; i++) {
Jason Wessel0b4b3822010-05-20 21:04:29 -0500654 if (breakinfo[i].pev)
655 continue;
Avi Kivity4dc0da82011-06-29 18:42:35 +0300656 breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL, NULL);
Jason Wessel91b152a2010-08-23 09:20:14 -0500657 if (IS_ERR((void * __force)breakinfo[i].pev)) {
Jason Wessel0b4b3822010-05-20 21:04:29 -0500658 printk(KERN_ERR "kgdb: Could not allocate hw"
659 "breakpoints\nDisabling the kernel debugger\n");
Jason Wesselcc096742010-01-28 17:04:42 -0600660 breakinfo[i].pev = NULL;
661 kgdb_arch_exit();
Jason Wessel0b4b3822010-05-20 21:04:29 -0500662 return;
Jason Wesselcc096742010-01-28 17:04:42 -0600663 }
664 for_each_online_cpu(cpu) {
665 pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
666 pevent[0]->hw.sample_period = 1;
Jason Wesselba773f72010-07-28 19:10:30 -0500667 pevent[0]->overflow_handler = kgdb_hw_overflow_handler;
Jason Wesselcc096742010-01-28 17:04:42 -0600668 if (pevent[0]->destroy != NULL) {
669 pevent[0]->destroy = NULL;
670 release_bp_slot(*pevent);
671 }
672 }
673 }
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200674}
675
676/**
677 * kgdb_arch_exit - Perform any architecture specific uninitalization.
678 *
679 * This function will handle the uninitalization of any architecture
680 * specific callbacks, for dynamic registration and unregistration.
681 */
682void kgdb_arch_exit(void)
683{
Jason Wesselcc096742010-01-28 17:04:42 -0600684 int i;
685 for (i = 0; i < 4; i++) {
686 if (breakinfo[i].pev) {
687 unregister_wide_hw_breakpoint(breakinfo[i].pev);
688 breakinfo[i].pev = NULL;
689 }
690 }
Don Zickus9c48f1c2011-09-30 15:06:21 -0400691 unregister_nmi_handler(NMI_UNKNOWN, "kgdb");
692 unregister_nmi_handler(NMI_LOCAL, "kgdb");
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200693 unregister_die_notifier(&kgdb_notifier);
694}
695
696/**
697 *
698 * kgdb_skipexception - Bail out of KGDB when we've been triggered.
699 * @exception: Exception vector number
700 * @regs: Current &struct pt_regs.
701 *
702 * On some architectures we need to skip a breakpoint exception when
703 * it occurs after a breakpoint has been removed.
704 *
705 * Skip an int3 exception when it occurs after a breakpoint has been
706 * removed. Backtrack eip by 1 since the int3 would have caused it to
707 * increment by 1.
708 */
709int kgdb_skipexception(int exception, struct pt_regs *regs)
710{
711 if (exception == 3 && kgdb_isremovedbreak(regs->ip - 1)) {
712 regs->ip -= 1;
713 return 1;
714 }
715 return 0;
716}
717
718unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
719{
720 if (exception == 3)
721 return instruction_pointer(regs) - 1;
722 return instruction_pointer(regs);
723}
724
Jason Wesseldcc78712010-05-20 21:04:21 -0500725void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
726{
727 regs->ip = ip;
728}
729
Jason Wessel3751d3e2012-03-23 09:35:05 -0500730int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
731{
732 int err;
Jason Wessel3751d3e2012-03-23 09:35:05 -0500733
734 bpt->type = BP_BREAKPOINT;
Christoph Hellwigfe557312020-06-17 09:37:53 +0200735 err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
Jason Wessel3751d3e2012-03-23 09:35:05 -0500736 BREAK_INSTR_SIZE);
737 if (err)
738 return err;
Christoph Hellwigfe557312020-06-17 09:37:53 +0200739 err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
Jason Wessel3751d3e2012-03-23 09:35:05 -0500740 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
Jason Wessel3751d3e2012-03-23 09:35:05 -0500741 if (!err)
742 return err;
743 /*
Nadav Amite8366732019-04-25 17:11:21 -0700744 * It is safe to call text_poke_kgdb() because normal kernel execution
Jason Wessel3751d3e2012-03-23 09:35:05 -0500745 * is stopped on all cores, so long as the text_mutex is not locked.
746 */
747 if (mutex_is_locked(&text_mutex))
748 return -EBUSY;
Nadav Amite8366732019-04-25 17:11:21 -0700749 text_poke_kgdb((void *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr,
750 BREAK_INSTR_SIZE);
Jason Wessel3751d3e2012-03-23 09:35:05 -0500751 bpt->type = BP_POKE_BREAKPOINT;
Kees Cook9ccaf772016-02-17 14:41:14 -0800752
Matt Mullins71ab8322019-05-31 12:47:54 -0700753 return 0;
Jason Wessel3751d3e2012-03-23 09:35:05 -0500754}
755
756int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
757{
Jason Wessel3751d3e2012-03-23 09:35:05 -0500758 if (bpt->type != BP_POKE_BREAKPOINT)
759 goto knl_write;
760 /*
Nadav Amite8366732019-04-25 17:11:21 -0700761 * It is safe to call text_poke_kgdb() because normal kernel execution
Jason Wessel3751d3e2012-03-23 09:35:05 -0500762 * is stopped on all cores, so long as the text_mutex is not locked.
763 */
764 if (mutex_is_locked(&text_mutex))
765 goto knl_write;
Nadav Amite8366732019-04-25 17:11:21 -0700766 text_poke_kgdb((void *)bpt->bpt_addr, bpt->saved_instr,
767 BREAK_INSTR_SIZE);
Nadav Amit86a22052019-04-25 17:11:28 -0700768 return 0;
Kees Cook9ccaf772016-02-17 14:41:14 -0800769
Jason Wessel3751d3e2012-03-23 09:35:05 -0500770knl_write:
Christoph Hellwigfe557312020-06-17 09:37:53 +0200771 return copy_to_kernel_nofault((char *)bpt->bpt_addr,
Jason Wessel3751d3e2012-03-23 09:35:05 -0500772 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
773}
774
Christophe Leroycc028292018-12-06 20:07:40 +0000775const struct kgdb_arch arch_kgdb_ops = {
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200776 /* Breakpoint instruction: */
777 .gdb_bpt_instr = { 0xcc },
Jason Wessel64e9ee32008-02-15 14:55:56 -0600778 .flags = KGDB_HW_BREAKPOINT,
779 .set_hw_breakpoint = kgdb_set_hw_break,
780 .remove_hw_breakpoint = kgdb_remove_hw_break,
Dongdong Dengd7ba979d2010-08-18 06:02:00 -0500781 .disable_hw_break = kgdb_disable_hw_debug,
Jason Wessel64e9ee32008-02-15 14:55:56 -0600782 .remove_all_hw_break = kgdb_remove_all_hw_break,
783 .correct_hw_break = kgdb_correct_hw_break,
Ingo Molnar82da3ff2008-04-17 20:05:37 +0200784};