xref: /linux-6.15/kernel/debug/debug_core.c (revision c8daba46)
1c4338209SJason Wessel /*
253197fc4SJason Wessel  * Kernel Debug Core
3c4338209SJason Wessel  *
4c4338209SJason Wessel  * Maintainer: Jason Wessel <[email protected]>
5c4338209SJason Wessel  *
6c4338209SJason Wessel  * Copyright (C) 2000-2001 VERITAS Software Corporation.
7c4338209SJason Wessel  * Copyright (C) 2002-2004 Timesys Corporation
8c4338209SJason Wessel  * Copyright (C) 2003-2004 Amit S. Kale <[email protected]>
9a2531293SPavel Machek  * Copyright (C) 2004 Pavel Machek <[email protected]>
10c4338209SJason Wessel  * Copyright (C) 2004-2006 Tom Rini <[email protected]>
11c4338209SJason Wessel  * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
1253197fc4SJason Wessel  * Copyright (C) 2005-2009 Wind River Systems, Inc.
13c4338209SJason Wessel  * Copyright (C) 2007 MontaVista Software, Inc.
14c4338209SJason Wessel  * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <[email protected]>
15c4338209SJason Wessel  *
16c4338209SJason Wessel  * Contributors at various stages not listed above:
17c4338209SJason Wessel  *  Jason Wessel ( [email protected] )
18c4338209SJason Wessel  *  George Anzinger <[email protected]>
19c4338209SJason Wessel  *  Anurekh Saxena ([email protected])
20c4338209SJason Wessel  *  Lake Stevens Instrument Division (Glenn Engel)
21c4338209SJason Wessel  *  Jim Kingdon, Cygnus Support.
22c4338209SJason Wessel  *
23c4338209SJason Wessel  * Original KGDB stub: David Grothe <[email protected]>,
24c4338209SJason Wessel  * Tigran Aivazian <[email protected]>
25c4338209SJason Wessel  *
26c4338209SJason Wessel  * This file is licensed under the terms of the GNU General Public License
27c4338209SJason Wessel  * version 2. This program is licensed "as is" without any warranty of any
28c4338209SJason Wessel  * kind, whether express or implied.
29c4338209SJason Wessel  */
300f16996cSFabian Frederick 
310f16996cSFabian Frederick #define pr_fmt(fmt) "KGDB: " fmt
320f16996cSFabian Frederick 
33c4338209SJason Wessel #include <linux/pid_namespace.h>
34c4338209SJason Wessel #include <linux/clocksource.h>
3516559ae4SGreg Kroah-Hartman #include <linux/serial_core.h>
36c4338209SJason Wessel #include <linux/interrupt.h>
37c4338209SJason Wessel #include <linux/spinlock.h>
38c4338209SJason Wessel #include <linux/console.h>
39c4338209SJason Wessel #include <linux/threads.h>
40c4338209SJason Wessel #include <linux/uaccess.h>
41c4338209SJason Wessel #include <linux/kernel.h>
42c4338209SJason Wessel #include <linux/module.h>
43c4338209SJason Wessel #include <linux/ptrace.h>
44c4338209SJason Wessel #include <linux/string.h>
45c4338209SJason Wessel #include <linux/delay.h>
46c4338209SJason Wessel #include <linux/sched.h>
47c4338209SJason Wessel #include <linux/sysrq.h>
482366e047SJason Wessel #include <linux/reboot.h>
49c4338209SJason Wessel #include <linux/init.h>
50c4338209SJason Wessel #include <linux/kgdb.h>
51dcc78711SJason Wessel #include <linux/kdb.h>
5238b8d208SIngo Molnar #include <linux/nmi.h>
53c4338209SJason Wessel #include <linux/pid.h>
54c4338209SJason Wessel #include <linux/smp.h>
55c4338209SJason Wessel #include <linux/mm.h>
56615d6e87SDavidlohr Bueso #include <linux/vmacache.h>
57fb70b588SJason Wessel #include <linux/rcupdate.h>
583cd99ac3SDouglas Anderson #include <linux/irq.h>
59c4338209SJason Wessel 
60c4338209SJason Wessel #include <asm/cacheflush.h>
61c4338209SJason Wessel #include <asm/byteorder.h>
6260063497SArun Sharma #include <linux/atomic.h>
6353197fc4SJason Wessel 
6453197fc4SJason Wessel #include "debug_core.h"
65c4338209SJason Wessel 
66c4338209SJason Wessel static int kgdb_break_asap;
67c4338209SJason Wessel 
6853197fc4SJason Wessel struct debuggerinfo_struct kgdb_info[NR_CPUS];
69c4338209SJason Wessel 
70a1350207SAndy Shevchenko /* kgdb_connected - Is a host GDB connected to us? */
71c4338209SJason Wessel int				kgdb_connected;
72c4338209SJason Wessel EXPORT_SYMBOL_GPL(kgdb_connected);
73c4338209SJason Wessel 
74c4338209SJason Wessel /* All the KGDB handlers are installed */
75f503b5aeSJason Wessel int			kgdb_io_module_registered;
76c4338209SJason Wessel 
77c4338209SJason Wessel /* Guard for recursive entry */
78c4338209SJason Wessel static int			exception_level;
79c4338209SJason Wessel 
8053197fc4SJason Wessel struct kgdb_io		*dbg_io_ops;
81c4338209SJason Wessel static DEFINE_SPINLOCK(kgdb_registration_lock);
82c4338209SJason Wessel 
83e16c33e2SYouling Tang /* Action for the reboot notifier, a global allow kdb to change it */
84bec4d62eSJason Wessel static int kgdbreboot;
85c4338209SJason Wessel /* kgdb console driver is loaded */
86c4338209SJason Wessel static int kgdb_con_registered;
87c4338209SJason Wessel /* determine if kgdb console output should be used */
88c4338209SJason Wessel static int kgdb_use_con;
890b4b3827SJason Wessel /* Flag for alternate operations for early debugging */
900b4b3827SJason Wessel bool dbg_is_early = true;
91dcc78711SJason Wessel /* Next cpu to become the master debug core */
92dcc78711SJason Wessel int dbg_switch_cpu;
93dcc78711SJason Wessel 
94dcc78711SJason Wessel /* Use kdb or gdbserver mode */
95a0de055cSJason Wessel int dbg_kdb_mode = 1;
96c4338209SJason Wessel 
97c4338209SJason Wessel module_param(kgdb_use_con, int, 0644);
98bec4d62eSJason Wessel module_param(kgdbreboot, int, 0644);
99c4338209SJason Wessel 
100c4338209SJason Wessel /*
101c4338209SJason Wessel  * Holds information about breakpoints in a kernel. These breakpoints are
102c4338209SJason Wessel  * added and removed by gdb.
103c4338209SJason Wessel  */
104c4338209SJason Wessel static struct kgdb_bkpt		kgdb_break[KGDB_MAX_BREAKPOINTS] = {
105c4338209SJason Wessel 	[0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
106c4338209SJason Wessel };
107c4338209SJason Wessel 
108c4338209SJason Wessel /*
109c4338209SJason Wessel  * The CPU# of the active CPU, or -1 if none:
110c4338209SJason Wessel  */
111c4338209SJason Wessel atomic_t			kgdb_active = ATOMIC_INIT(-1);
112dcc78711SJason Wessel EXPORT_SYMBOL_GPL(kgdb_active);
113dfee3a7bSJason Wessel static DEFINE_RAW_SPINLOCK(dbg_master_lock);
114dfee3a7bSJason Wessel static DEFINE_RAW_SPINLOCK(dbg_slave_lock);
115c4338209SJason Wessel 
116c4338209SJason Wessel /*
117c4338209SJason Wessel  * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
118c4338209SJason Wessel  * bootup code (which might not have percpu set up yet):
119c4338209SJason Wessel  */
120dfee3a7bSJason Wessel static atomic_t			masters_in_kgdb;
121dfee3a7bSJason Wessel static atomic_t			slaves_in_kgdb;
122c4338209SJason Wessel atomic_t			kgdb_setting_breakpoint;
123c4338209SJason Wessel 
124c4338209SJason Wessel struct task_struct		*kgdb_usethread;
125c4338209SJason Wessel struct task_struct		*kgdb_contthread;
126c4338209SJason Wessel 
127c4338209SJason Wessel int				kgdb_single_step;
12853197fc4SJason Wessel static pid_t			kgdb_sstep_pid;
129c4338209SJason Wessel 
130c4338209SJason Wessel /* to keep track of the CPU which is doing the single stepping*/
131c4338209SJason Wessel atomic_t			kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
132c4338209SJason Wessel 
133c4338209SJason Wessel /*
134c4338209SJason Wessel  * If you are debugging a problem where roundup (the collection of
135c4338209SJason Wessel  * all other CPUs) is a problem [this should be extremely rare],
136c4338209SJason Wessel  * then use the nokgdbroundup option to avoid roundup. In that case
137c4338209SJason Wessel  * the other CPUs might interfere with your debugging context, so
138c4338209SJason Wessel  * use this with care:
139c4338209SJason Wessel  */
140c4338209SJason Wessel static int kgdb_do_roundup = 1;
141c4338209SJason Wessel 
142c4338209SJason Wessel static int __init opt_nokgdbroundup(char *str)
143c4338209SJason Wessel {
144c4338209SJason Wessel 	kgdb_do_roundup = 0;
145c4338209SJason Wessel 
146c4338209SJason Wessel 	return 0;
147c4338209SJason Wessel }
148c4338209SJason Wessel 
149c4338209SJason Wessel early_param("nokgdbroundup", opt_nokgdbroundup);
150c4338209SJason Wessel 
151c4338209SJason Wessel /*
152c4338209SJason Wessel  * Finally, some KGDB code :-)
153c4338209SJason Wessel  */
154c4338209SJason Wessel 
155c4338209SJason Wessel /*
156c4338209SJason Wessel  * Weak aliases for breakpoint management,
157e16c33e2SYouling Tang  * can be overridden by architectures when needed:
158c4338209SJason Wessel  */
15998b54aa1SJason Wessel int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
160c4338209SJason Wessel {
161c4338209SJason Wessel 	int err;
162c4338209SJason Wessel 
163fe557319SChristoph Hellwig 	err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
16498b54aa1SJason Wessel 				BREAK_INSTR_SIZE);
165c4338209SJason Wessel 	if (err)
166c4338209SJason Wessel 		return err;
167fe557319SChristoph Hellwig 	err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
16898b54aa1SJason Wessel 				 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
16998b54aa1SJason Wessel 	return err;
170c4338209SJason Wessel }
1714c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_arch_set_breakpoint);
172c4338209SJason Wessel 
17398b54aa1SJason Wessel int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
174c4338209SJason Wessel {
175fe557319SChristoph Hellwig 	return copy_to_kernel_nofault((char *)bpt->bpt_addr,
17698b54aa1SJason Wessel 				  (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
177c4338209SJason Wessel }
1784c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_arch_remove_breakpoint);
179c4338209SJason Wessel 
180c4338209SJason Wessel int __weak kgdb_validate_break_address(unsigned long addr)
181c4338209SJason Wessel {
18298b54aa1SJason Wessel 	struct kgdb_bkpt tmp;
183c4338209SJason Wessel 	int err;
184f2d10ff4SDaniel Thompson 
185f2d10ff4SDaniel Thompson 	if (kgdb_within_blocklist(addr))
186f2d10ff4SDaniel Thompson 		return -EINVAL;
187f2d10ff4SDaniel Thompson 
18898b54aa1SJason Wessel 	/* Validate setting the breakpoint and then removing it.  If the
189c4338209SJason Wessel 	 * remove fails, the kernel needs to emit a bad message because we
190c4338209SJason Wessel 	 * are deep trouble not being able to put things back the way we
191c4338209SJason Wessel 	 * found them.
192c4338209SJason Wessel 	 */
19398b54aa1SJason Wessel 	tmp.bpt_addr = addr;
19498b54aa1SJason Wessel 	err = kgdb_arch_set_breakpoint(&tmp);
195c4338209SJason Wessel 	if (err)
196c4338209SJason Wessel 		return err;
19798b54aa1SJason Wessel 	err = kgdb_arch_remove_breakpoint(&tmp);
198c4338209SJason Wessel 	if (err)
1990f16996cSFabian Frederick 		pr_err("Critical breakpoint error, kernel memory destroyed at: %lx\n",
2000f16996cSFabian Frederick 		       addr);
201c4338209SJason Wessel 	return err;
202c4338209SJason Wessel }
203c4338209SJason Wessel 
204c4338209SJason Wessel unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
205c4338209SJason Wessel {
206c4338209SJason Wessel 	return instruction_pointer(regs);
207c4338209SJason Wessel }
2084c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_arch_pc);
209c4338209SJason Wessel 
210c4338209SJason Wessel int __weak kgdb_arch_init(void)
211c4338209SJason Wessel {
212c4338209SJason Wessel 	return 0;
213c4338209SJason Wessel }
214c4338209SJason Wessel 
215c4338209SJason Wessel int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
216c4338209SJason Wessel {
217c4338209SJason Wessel 	return 0;
218c4338209SJason Wessel }
2194c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_skipexception);
220c4338209SJason Wessel 
2213cd99ac3SDouglas Anderson #ifdef CONFIG_SMP
2223cd99ac3SDouglas Anderson 
2233cd99ac3SDouglas Anderson /*
2243cd99ac3SDouglas Anderson  * Default (weak) implementation for kgdb_roundup_cpus
2253cd99ac3SDouglas Anderson  */
2263cd99ac3SDouglas Anderson 
2273cd99ac3SDouglas Anderson void __weak kgdb_call_nmi_hook(void *ignored)
2283cd99ac3SDouglas Anderson {
2293cd99ac3SDouglas Anderson 	/*
2303cd99ac3SDouglas Anderson 	 * NOTE: get_irq_regs() is supposed to get the registers from
2313cd99ac3SDouglas Anderson 	 * before the IPI interrupt happened and so is supposed to
2323cd99ac3SDouglas Anderson 	 * show where the processor was.  In some situations it's
2333cd99ac3SDouglas Anderson 	 * possible we might be called without an IPI, so it might be
2343cd99ac3SDouglas Anderson 	 * safer to figure out how to make kgdb_breakpoint() work
2353cd99ac3SDouglas Anderson 	 * properly here.
2363cd99ac3SDouglas Anderson 	 */
2373cd99ac3SDouglas Anderson 	kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
2383cd99ac3SDouglas Anderson }
2394c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_call_nmi_hook);
2403cd99ac3SDouglas Anderson 
241545b8c8dSPeter Zijlstra static DEFINE_PER_CPU(call_single_data_t, kgdb_roundup_csd) =
242545b8c8dSPeter Zijlstra 	CSD_INIT(kgdb_call_nmi_hook, NULL);
243545b8c8dSPeter Zijlstra 
2443cd99ac3SDouglas Anderson void __weak kgdb_roundup_cpus(void)
2453cd99ac3SDouglas Anderson {
2463cd99ac3SDouglas Anderson 	call_single_data_t *csd;
2473cd99ac3SDouglas Anderson 	int this_cpu = raw_smp_processor_id();
2483cd99ac3SDouglas Anderson 	int cpu;
24987b09592SDouglas Anderson 	int ret;
2503cd99ac3SDouglas Anderson 
2513cd99ac3SDouglas Anderson 	for_each_online_cpu(cpu) {
2523cd99ac3SDouglas Anderson 		/* No need to roundup ourselves */
2533cd99ac3SDouglas Anderson 		if (cpu == this_cpu)
2543cd99ac3SDouglas Anderson 			continue;
2553cd99ac3SDouglas Anderson 
2563cd99ac3SDouglas Anderson 		csd = &per_cpu(kgdb_roundup_csd, cpu);
25787b09592SDouglas Anderson 
25887b09592SDouglas Anderson 		/*
25987b09592SDouglas Anderson 		 * If it didn't round up last time, don't try again
26087b09592SDouglas Anderson 		 * since smp_call_function_single_async() will block.
26187b09592SDouglas Anderson 		 *
26287b09592SDouglas Anderson 		 * If rounding_up is false then we know that the
26387b09592SDouglas Anderson 		 * previous call must have at least started and that
26487b09592SDouglas Anderson 		 * means smp_call_function_single_async() won't block.
26587b09592SDouglas Anderson 		 */
26687b09592SDouglas Anderson 		if (kgdb_info[cpu].rounding_up)
26787b09592SDouglas Anderson 			continue;
26887b09592SDouglas Anderson 		kgdb_info[cpu].rounding_up = true;
26987b09592SDouglas Anderson 
27087b09592SDouglas Anderson 		ret = smp_call_function_single_async(cpu, csd);
27187b09592SDouglas Anderson 		if (ret)
27287b09592SDouglas Anderson 			kgdb_info[cpu].rounding_up = false;
2733cd99ac3SDouglas Anderson 	}
2743cd99ac3SDouglas Anderson }
2754c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_roundup_cpus);
2763cd99ac3SDouglas Anderson 
2773cd99ac3SDouglas Anderson #endif
2783cd99ac3SDouglas Anderson 
279c4338209SJason Wessel /*
280c4338209SJason Wessel  * Some architectures need cache flushes when we set/clear a
281c4338209SJason Wessel  * breakpoint:
282c4338209SJason Wessel  */
283c4338209SJason Wessel static void kgdb_flush_swbreak_addr(unsigned long addr)
284c4338209SJason Wessel {
285c4338209SJason Wessel 	if (!CACHE_FLUSH_IS_SAFE)
286c4338209SJason Wessel 		return;
287c4338209SJason Wessel 
288615d6e87SDavidlohr Bueso 	if (current->mm) {
289615d6e87SDavidlohr Bueso 		int i;
290615d6e87SDavidlohr Bueso 
291615d6e87SDavidlohr Bueso 		for (i = 0; i < VMACACHE_SIZE; i++) {
292314ff785SIngo Molnar 			if (!current->vmacache.vmas[i])
293615d6e87SDavidlohr Bueso 				continue;
294314ff785SIngo Molnar 			flush_cache_range(current->vmacache.vmas[i],
295c4338209SJason Wessel 					  addr, addr + BREAK_INSTR_SIZE);
296c4338209SJason Wessel 		}
297615d6e87SDavidlohr Bueso 	}
298615d6e87SDavidlohr Bueso 
299c4338209SJason Wessel 	/* Force flush instruction cache if it was outside the mm */
300c4338209SJason Wessel 	flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
301c4338209SJason Wessel }
3024c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_flush_swbreak_addr);
303c4338209SJason Wessel 
304c4338209SJason Wessel /*
305c4338209SJason Wessel  * SW breakpoint management:
306c4338209SJason Wessel  */
30753197fc4SJason Wessel int dbg_activate_sw_breakpoints(void)
308c4338209SJason Wessel {
309c4338209SJason Wessel 	int error;
310c4338209SJason Wessel 	int ret = 0;
311c4338209SJason Wessel 	int i;
312c4338209SJason Wessel 
313c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
314c4338209SJason Wessel 		if (kgdb_break[i].state != BP_SET)
315c4338209SJason Wessel 			continue;
316c4338209SJason Wessel 
31798b54aa1SJason Wessel 		error = kgdb_arch_set_breakpoint(&kgdb_break[i]);
318c4338209SJason Wessel 		if (error) {
319c4338209SJason Wessel 			ret = error;
3200f16996cSFabian Frederick 			pr_info("BP install failed: %lx\n",
32198b54aa1SJason Wessel 				kgdb_break[i].bpt_addr);
322c4338209SJason Wessel 			continue;
323c4338209SJason Wessel 		}
324c4338209SJason Wessel 
32598b54aa1SJason Wessel 		kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
326c4338209SJason Wessel 		kgdb_break[i].state = BP_ACTIVE;
327c4338209SJason Wessel 	}
328c4338209SJason Wessel 	return ret;
329c4338209SJason Wessel }
3304c4197edSDaniel Thompson NOKPROBE_SYMBOL(dbg_activate_sw_breakpoints);
331c4338209SJason Wessel 
33253197fc4SJason Wessel int dbg_set_sw_break(unsigned long addr)
333c4338209SJason Wessel {
334c4338209SJason Wessel 	int err = kgdb_validate_break_address(addr);
335c4338209SJason Wessel 	int breakno = -1;
336c4338209SJason Wessel 	int i;
337c4338209SJason Wessel 
338c4338209SJason Wessel 	if (err)
339c4338209SJason Wessel 		return err;
340c4338209SJason Wessel 
341c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
342c4338209SJason Wessel 		if ((kgdb_break[i].state == BP_SET) &&
343c4338209SJason Wessel 					(kgdb_break[i].bpt_addr == addr))
344c4338209SJason Wessel 			return -EEXIST;
345c4338209SJason Wessel 	}
346c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
347c4338209SJason Wessel 		if (kgdb_break[i].state == BP_REMOVED &&
348c4338209SJason Wessel 					kgdb_break[i].bpt_addr == addr) {
349c4338209SJason Wessel 			breakno = i;
350c4338209SJason Wessel 			break;
351c4338209SJason Wessel 		}
352c4338209SJason Wessel 	}
353c4338209SJason Wessel 
354c4338209SJason Wessel 	if (breakno == -1) {
355c4338209SJason Wessel 		for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
356c4338209SJason Wessel 			if (kgdb_break[i].state == BP_UNDEFINED) {
357c4338209SJason Wessel 				breakno = i;
358c4338209SJason Wessel 				break;
359c4338209SJason Wessel 			}
360c4338209SJason Wessel 		}
361c4338209SJason Wessel 	}
362c4338209SJason Wessel 
363c4338209SJason Wessel 	if (breakno == -1)
364c4338209SJason Wessel 		return -E2BIG;
365c4338209SJason Wessel 
366c4338209SJason Wessel 	kgdb_break[breakno].state = BP_SET;
367c4338209SJason Wessel 	kgdb_break[breakno].type = BP_BREAKPOINT;
368c4338209SJason Wessel 	kgdb_break[breakno].bpt_addr = addr;
369c4338209SJason Wessel 
370c4338209SJason Wessel 	return 0;
371c4338209SJason Wessel }
372c4338209SJason Wessel 
373dcc78711SJason Wessel int dbg_deactivate_sw_breakpoints(void)
374c4338209SJason Wessel {
375c4338209SJason Wessel 	int error;
376c4338209SJason Wessel 	int ret = 0;
377c4338209SJason Wessel 	int i;
378c4338209SJason Wessel 
379c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
380c4338209SJason Wessel 		if (kgdb_break[i].state != BP_ACTIVE)
381c4338209SJason Wessel 			continue;
38298b54aa1SJason Wessel 		error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
383c4338209SJason Wessel 		if (error) {
3840f16996cSFabian Frederick 			pr_info("BP remove failed: %lx\n",
38598b54aa1SJason Wessel 				kgdb_break[i].bpt_addr);
386c4338209SJason Wessel 			ret = error;
387c4338209SJason Wessel 		}
388c4338209SJason Wessel 
38998b54aa1SJason Wessel 		kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
390c4338209SJason Wessel 		kgdb_break[i].state = BP_SET;
391c4338209SJason Wessel 	}
392c4338209SJason Wessel 	return ret;
393c4338209SJason Wessel }
3944c4197edSDaniel Thompson NOKPROBE_SYMBOL(dbg_deactivate_sw_breakpoints);
395c4338209SJason Wessel 
39653197fc4SJason Wessel int dbg_remove_sw_break(unsigned long addr)
397c4338209SJason Wessel {
398c4338209SJason Wessel 	int i;
399c4338209SJason Wessel 
400c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
401c4338209SJason Wessel 		if ((kgdb_break[i].state == BP_SET) &&
402c4338209SJason Wessel 				(kgdb_break[i].bpt_addr == addr)) {
403c4338209SJason Wessel 			kgdb_break[i].state = BP_REMOVED;
404c4338209SJason Wessel 			return 0;
405c4338209SJason Wessel 		}
406c4338209SJason Wessel 	}
407c4338209SJason Wessel 	return -ENOENT;
408c4338209SJason Wessel }
409c4338209SJason Wessel 
410c4338209SJason Wessel int kgdb_isremovedbreak(unsigned long addr)
411c4338209SJason Wessel {
412c4338209SJason Wessel 	int i;
413c4338209SJason Wessel 
414c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
415c4338209SJason Wessel 		if ((kgdb_break[i].state == BP_REMOVED) &&
416c4338209SJason Wessel 					(kgdb_break[i].bpt_addr == addr))
417c4338209SJason Wessel 			return 1;
418c4338209SJason Wessel 	}
419c4338209SJason Wessel 	return 0;
420c4338209SJason Wessel }
421c4338209SJason Wessel 
422f83b04d3SVincent Chen int kgdb_has_hit_break(unsigned long addr)
423f83b04d3SVincent Chen {
424f83b04d3SVincent Chen 	int i;
425f83b04d3SVincent Chen 
426f83b04d3SVincent Chen 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
427f83b04d3SVincent Chen 		if (kgdb_break[i].state == BP_ACTIVE &&
428f83b04d3SVincent Chen 		    kgdb_break[i].bpt_addr == addr)
429f83b04d3SVincent Chen 			return 1;
430f83b04d3SVincent Chen 	}
431f83b04d3SVincent Chen 	return 0;
432f83b04d3SVincent Chen }
433f83b04d3SVincent Chen 
43453197fc4SJason Wessel int dbg_remove_all_break(void)
435c4338209SJason Wessel {
436c4338209SJason Wessel 	int error;
437c4338209SJason Wessel 	int i;
438c4338209SJason Wessel 
439c4338209SJason Wessel 	/* Clear memory breakpoints. */
440c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
441c4338209SJason Wessel 		if (kgdb_break[i].state != BP_ACTIVE)
442c4338209SJason Wessel 			goto setundefined;
44398b54aa1SJason Wessel 		error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
444c4338209SJason Wessel 		if (error)
4450f16996cSFabian Frederick 			pr_err("breakpoint remove failed: %lx\n",
44698b54aa1SJason Wessel 			       kgdb_break[i].bpt_addr);
447c4338209SJason Wessel setundefined:
448c4338209SJason Wessel 		kgdb_break[i].state = BP_UNDEFINED;
449c4338209SJason Wessel 	}
450c4338209SJason Wessel 
451c4338209SJason Wessel 	/* Clear hardware breakpoints. */
452c4338209SJason Wessel 	if (arch_kgdb_ops.remove_all_hw_break)
453c4338209SJason Wessel 		arch_kgdb_ops.remove_all_hw_break();
454c4338209SJason Wessel 
455c4338209SJason Wessel 	return 0;
456c4338209SJason Wessel }
457c4338209SJason Wessel 
458d54ce615SSumit Garg void kgdb_free_init_mem(void)
459d54ce615SSumit Garg {
460d54ce615SSumit Garg 	int i;
461d54ce615SSumit Garg 
462d54ce615SSumit Garg 	/* Clear init memory breakpoints. */
463d54ce615SSumit Garg 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
464d54ce615SSumit Garg 		if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))
465d54ce615SSumit Garg 			kgdb_break[i].state = BP_UNDEFINED;
466d54ce615SSumit Garg 	}
467d54ce615SSumit Garg }
468d54ce615SSumit Garg 
4692277b492SDouglas Anderson #ifdef CONFIG_KGDB_KDB
4702277b492SDouglas Anderson void kdb_dump_stack_on_cpu(int cpu)
4712277b492SDouglas Anderson {
472d07ce4e3SDaniel Thompson 	if (cpu == raw_smp_processor_id() || !IS_ENABLED(CONFIG_SMP)) {
4732277b492SDouglas Anderson 		dump_stack();
4742277b492SDouglas Anderson 		return;
4752277b492SDouglas Anderson 	}
4762277b492SDouglas Anderson 
4772277b492SDouglas Anderson 	if (!(kgdb_info[cpu].exception_state & DCPU_IS_SLAVE)) {
4782277b492SDouglas Anderson 		kdb_printf("ERROR: Task on cpu %d didn't stop in the debugger\n",
4792277b492SDouglas Anderson 			   cpu);
4802277b492SDouglas Anderson 		return;
4812277b492SDouglas Anderson 	}
4822277b492SDouglas Anderson 
4832277b492SDouglas Anderson 	/*
4842277b492SDouglas Anderson 	 * In general, architectures don't support dumping the stack of a
4852277b492SDouglas Anderson 	 * "running" process that's not the current one.  From the point of
4862277b492SDouglas Anderson 	 * view of the Linux, kernel processes that are looping in the kgdb
4872277b492SDouglas Anderson 	 * slave loop are still "running".  There's also no API (that actually
4882277b492SDouglas Anderson 	 * works across all architectures) that can do a stack crawl based
4892277b492SDouglas Anderson 	 * on registers passed as a parameter.
4902277b492SDouglas Anderson 	 *
4912277b492SDouglas Anderson 	 * Solve this conundrum by asking slave CPUs to do the backtrace
4922277b492SDouglas Anderson 	 * themselves.
4932277b492SDouglas Anderson 	 */
4942277b492SDouglas Anderson 	kgdb_info[cpu].exception_state |= DCPU_WANT_BT;
4952277b492SDouglas Anderson 	while (kgdb_info[cpu].exception_state & DCPU_WANT_BT)
4962277b492SDouglas Anderson 		cpu_relax();
4972277b492SDouglas Anderson }
4982277b492SDouglas Anderson #endif
4992277b492SDouglas Anderson 
500c4338209SJason Wessel /*
501c4338209SJason Wessel  * Return true if there is a valid kgdb I/O module.  Also if no
502c4338209SJason Wessel  * debugger is attached a message can be printed to the console about
503c4338209SJason Wessel  * waiting for the debugger to attach.
504c4338209SJason Wessel  *
505c4338209SJason Wessel  * The print_wait argument is only to be true when called from inside
506c4338209SJason Wessel  * the core kgdb_handle_exception, because it will wait for the
507c4338209SJason Wessel  * debugger to attach.
508c4338209SJason Wessel  */
509c4338209SJason Wessel static int kgdb_io_ready(int print_wait)
510c4338209SJason Wessel {
51153197fc4SJason Wessel 	if (!dbg_io_ops)
512c4338209SJason Wessel 		return 0;
513c4338209SJason Wessel 	if (kgdb_connected)
514c4338209SJason Wessel 		return 1;
515c4338209SJason Wessel 	if (atomic_read(&kgdb_setting_breakpoint))
516c4338209SJason Wessel 		return 1;
517dcc78711SJason Wessel 	if (print_wait) {
518dcc78711SJason Wessel #ifdef CONFIG_KGDB_KDB
519dcc78711SJason Wessel 		if (!dbg_kdb_mode)
5200f16996cSFabian Frederick 			pr_crit("waiting... or $3#33 for KDB\n");
521dcc78711SJason Wessel #else
5220f16996cSFabian Frederick 		pr_crit("Waiting for remote debugger\n");
523dcc78711SJason Wessel #endif
524dcc78711SJason Wessel 	}
525c4338209SJason Wessel 	return 1;
526c4338209SJason Wessel }
5274c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_io_ready);
528c4338209SJason Wessel 
529c4338209SJason Wessel static int kgdb_reenter_check(struct kgdb_state *ks)
530c4338209SJason Wessel {
531c4338209SJason Wessel 	unsigned long addr;
532c4338209SJason Wessel 
533c4338209SJason Wessel 	if (atomic_read(&kgdb_active) != raw_smp_processor_id())
534c4338209SJason Wessel 		return 0;
535c4338209SJason Wessel 
536c4338209SJason Wessel 	/* Panic on recursive debugger calls: */
537c4338209SJason Wessel 	exception_level++;
538c4338209SJason Wessel 	addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
539dcc78711SJason Wessel 	dbg_deactivate_sw_breakpoints();
540c4338209SJason Wessel 
541c4338209SJason Wessel 	/*
542c4338209SJason Wessel 	 * If the break point removed ok at the place exception
543c4338209SJason Wessel 	 * occurred, try to recover and print a warning to the end
544c4338209SJason Wessel 	 * user because the user planted a breakpoint in a place that
545c4338209SJason Wessel 	 * KGDB needs in order to function.
546c4338209SJason Wessel 	 */
54753197fc4SJason Wessel 	if (dbg_remove_sw_break(addr) == 0) {
548c4338209SJason Wessel 		exception_level = 0;
549c4338209SJason Wessel 		kgdb_skipexception(ks->ex_vector, ks->linux_regs);
55053197fc4SJason Wessel 		dbg_activate_sw_breakpoints();
5510f16996cSFabian Frederick 		pr_crit("re-enter error: breakpoint removed %lx\n", addr);
552c4338209SJason Wessel 		WARN_ON_ONCE(1);
553c4338209SJason Wessel 
554c4338209SJason Wessel 		return 1;
555c4338209SJason Wessel 	}
55653197fc4SJason Wessel 	dbg_remove_all_break();
557c4338209SJason Wessel 	kgdb_skipexception(ks->ex_vector, ks->linux_regs);
558c4338209SJason Wessel 
559c4338209SJason Wessel 	if (exception_level > 1) {
560c4338209SJason Wessel 		dump_stack();
5613ca676e4SDouglas Anderson 		kgdb_io_module_registered = false;
562c4338209SJason Wessel 		panic("Recursive entry to debugger");
563c4338209SJason Wessel 	}
564c4338209SJason Wessel 
5650f16996cSFabian Frederick 	pr_crit("re-enter exception: ALL breakpoints killed\n");
5666d906340SJason Wessel #ifdef CONFIG_KGDB_KDB
5676d906340SJason Wessel 	/* Allow kdb to debug itself one level */
5686d906340SJason Wessel 	return 0;
5696d906340SJason Wessel #endif
570c4338209SJason Wessel 	dump_stack();
571c4338209SJason Wessel 	panic("Recursive entry to debugger");
572c4338209SJason Wessel 
573c4338209SJason Wessel 	return 1;
574c4338209SJason Wessel }
5754c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_reenter_check);
576c4338209SJason Wessel 
57716cdc628SJason Wessel static void dbg_touch_watchdogs(void)
57816cdc628SJason Wessel {
57916cdc628SJason Wessel 	touch_softlockup_watchdog_sync();
58016cdc628SJason Wessel 	clocksource_touch_watchdog();
581fb70b588SJason Wessel 	rcu_cpu_stall_reset();
58216cdc628SJason Wessel }
5834c4197edSDaniel Thompson NOKPROBE_SYMBOL(dbg_touch_watchdogs);
58416cdc628SJason Wessel 
585dfee3a7bSJason Wessel static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
586dfee3a7bSJason Wessel 		int exception_state)
587c4338209SJason Wessel {
588c4338209SJason Wessel 	unsigned long flags;
589c4338209SJason Wessel 	int sstep_tries = 100;
590dcc78711SJason Wessel 	int error;
591dfee3a7bSJason Wessel 	int cpu;
592c4338209SJason Wessel 	int trace_on = 0;
593dfee3a7bSJason Wessel 	int online_cpus = num_online_cpus();
594a1465d2fSDaniel Thompson 	u64 time_left;
595c1bb9a9cSDongdong Deng 
596dfee3a7bSJason Wessel 	kgdb_info[ks->cpu].enter_kgdb++;
597dfee3a7bSJason Wessel 	kgdb_info[ks->cpu].exception_state |= exception_state;
598dfee3a7bSJason Wessel 
599dfee3a7bSJason Wessel 	if (exception_state == DCPU_WANT_MASTER)
600dfee3a7bSJason Wessel 		atomic_inc(&masters_in_kgdb);
601dfee3a7bSJason Wessel 	else
602dfee3a7bSJason Wessel 		atomic_inc(&slaves_in_kgdb);
603d7ba979dSDongdong Deng 
604d7ba979dSDongdong Deng 	if (arch_kgdb_ops.disable_hw_break)
605d7ba979dSDongdong Deng 		arch_kgdb_ops.disable_hw_break(regs);
606c1bb9a9cSDongdong Deng 
607c4338209SJason Wessel acquirelock:
608440ab9e1SDouglas Anderson 	rcu_read_lock();
609c4338209SJason Wessel 	/*
610c4338209SJason Wessel 	 * Interrupts will be restored by the 'trap return' code, except when
611c4338209SJason Wessel 	 * single stepping.
612c4338209SJason Wessel 	 */
613c4338209SJason Wessel 	local_irq_save(flags);
614c4338209SJason Wessel 
615c4338209SJason Wessel 	cpu = ks->cpu;
616c4338209SJason Wessel 	kgdb_info[cpu].debuggerinfo = regs;
617c4338209SJason Wessel 	kgdb_info[cpu].task = current;
618dcc78711SJason Wessel 	kgdb_info[cpu].ret_state = 0;
619dcc78711SJason Wessel 	kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
620c4338209SJason Wessel 
621dfee3a7bSJason Wessel 	/* Make sure the above info reaches the primary CPU */
622dfee3a7bSJason Wessel 	smp_mb();
623dfee3a7bSJason Wessel 
624dfee3a7bSJason Wessel 	if (exception_level == 1) {
625dfee3a7bSJason Wessel 		if (raw_spin_trylock(&dbg_master_lock))
626dfee3a7bSJason Wessel 			atomic_xchg(&kgdb_active, cpu);
6276d906340SJason Wessel 		goto cpu_master_loop;
628dfee3a7bSJason Wessel 	}
6296d906340SJason Wessel 
630c4338209SJason Wessel 	/*
631c4338209SJason Wessel 	 * CPU will loop if it is a slave or request to become a kgdb
632c4338209SJason Wessel 	 * master cpu and acquire the kgdb_active lock:
633c4338209SJason Wessel 	 */
634c4338209SJason Wessel 	while (1) {
635dcc78711SJason Wessel cpu_loop:
636dcc78711SJason Wessel 		if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) {
637dcc78711SJason Wessel 			kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER;
638dcc78711SJason Wessel 			goto cpu_master_loop;
639dcc78711SJason Wessel 		} else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
640dfee3a7bSJason Wessel 			if (raw_spin_trylock(&dbg_master_lock)) {
641dfee3a7bSJason Wessel 				atomic_xchg(&kgdb_active, cpu);
642c4338209SJason Wessel 				break;
643dfee3a7bSJason Wessel 			}
6442277b492SDouglas Anderson 		} else if (kgdb_info[cpu].exception_state & DCPU_WANT_BT) {
6452277b492SDouglas Anderson 			dump_stack();
6462277b492SDouglas Anderson 			kgdb_info[cpu].exception_state &= ~DCPU_WANT_BT;
647c4338209SJason Wessel 		} else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
648dfee3a7bSJason Wessel 			if (!raw_spin_is_locked(&dbg_slave_lock))
649c4338209SJason Wessel 				goto return_normal;
650c4338209SJason Wessel 		} else {
651c4338209SJason Wessel return_normal:
652c4338209SJason Wessel 			/* Return to normal operation by executing any
653c4338209SJason Wessel 			 * hw breakpoint fixup.
654c4338209SJason Wessel 			 */
655c4338209SJason Wessel 			if (arch_kgdb_ops.correct_hw_break)
656c4338209SJason Wessel 				arch_kgdb_ops.correct_hw_break();
657c4338209SJason Wessel 			if (trace_on)
658c4338209SJason Wessel 				tracing_on();
659162bc7f5SDouglas Anderson 			kgdb_info[cpu].debuggerinfo = NULL;
660162bc7f5SDouglas Anderson 			kgdb_info[cpu].task = NULL;
661dfee3a7bSJason Wessel 			kgdb_info[cpu].exception_state &=
662dfee3a7bSJason Wessel 				~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
663dfee3a7bSJason Wessel 			kgdb_info[cpu].enter_kgdb--;
6644e857c58SPeter Zijlstra 			smp_mb__before_atomic();
665dfee3a7bSJason Wessel 			atomic_dec(&slaves_in_kgdb);
66616cdc628SJason Wessel 			dbg_touch_watchdogs();
667c4338209SJason Wessel 			local_irq_restore(flags);
668440ab9e1SDouglas Anderson 			rcu_read_unlock();
669c4338209SJason Wessel 			return 0;
670c4338209SJason Wessel 		}
671c4338209SJason Wessel 		cpu_relax();
672c4338209SJason Wessel 	}
673c4338209SJason Wessel 
674c4338209SJason Wessel 	/*
675c4338209SJason Wessel 	 * For single stepping, try to only enter on the processor
67625985edcSLucas De Marchi 	 * that was single stepping.  To guard against a deadlock, the
677c4338209SJason Wessel 	 * kernel will only try for the value of sstep_tries before
678c4338209SJason Wessel 	 * giving up and continuing on.
679c4338209SJason Wessel 	 */
680c4338209SJason Wessel 	if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
681c4338209SJason Wessel 	    (kgdb_info[cpu].task &&
682c4338209SJason Wessel 	     kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
683c4338209SJason Wessel 		atomic_set(&kgdb_active, -1);
684dfee3a7bSJason Wessel 		raw_spin_unlock(&dbg_master_lock);
68516cdc628SJason Wessel 		dbg_touch_watchdogs();
686c4338209SJason Wessel 		local_irq_restore(flags);
687440ab9e1SDouglas Anderson 		rcu_read_unlock();
688c4338209SJason Wessel 
689c4338209SJason Wessel 		goto acquirelock;
690c4338209SJason Wessel 	}
691c4338209SJason Wessel 
692c4338209SJason Wessel 	if (!kgdb_io_ready(1)) {
693dcc78711SJason Wessel 		kgdb_info[cpu].ret_state = 1;
69453197fc4SJason Wessel 		goto kgdb_restore; /* No I/O connection, resume the system */
695c4338209SJason Wessel 	}
696c4338209SJason Wessel 
697c4338209SJason Wessel 	/*
698c4338209SJason Wessel 	 * Don't enter if we have hit a removed breakpoint.
699c4338209SJason Wessel 	 */
700c4338209SJason Wessel 	if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
701c4338209SJason Wessel 		goto kgdb_restore;
702c4338209SJason Wessel 
703202164fbSDouglas Anderson 	atomic_inc(&ignore_console_lock_warning);
704202164fbSDouglas Anderson 
705c4338209SJason Wessel 	/* Call the I/O driver's pre_exception routine */
70653197fc4SJason Wessel 	if (dbg_io_ops->pre_exception)
70753197fc4SJason Wessel 		dbg_io_ops->pre_exception();
708c4338209SJason Wessel 
709c4338209SJason Wessel 	/*
710c4338209SJason Wessel 	 * Get the passive CPU lock which will hold all the non-primary
711c4338209SJason Wessel 	 * CPU in a spin state while the debugger is active
712c4338209SJason Wessel 	 */
713dfee3a7bSJason Wessel 	if (!kgdb_single_step)
714dfee3a7bSJason Wessel 		raw_spin_lock(&dbg_slave_lock);
715c4338209SJason Wessel 
716c4338209SJason Wessel #ifdef CONFIG_SMP
7178daaa5f8SMike Travis 	/* If send_ready set, slaves are already waiting */
7188daaa5f8SMike Travis 	if (ks->send_ready)
7198daaa5f8SMike Travis 		atomic_set(ks->send_ready, 1);
7208daaa5f8SMike Travis 
721c4338209SJason Wessel 	/* Signal the other CPUs to enter kgdb_wait() */
7228daaa5f8SMike Travis 	else if ((!kgdb_single_step) && kgdb_do_roundup)
7239ef7fa50SDouglas Anderson 		kgdb_roundup_cpus();
724c4338209SJason Wessel #endif
725c4338209SJason Wessel 
726c4338209SJason Wessel 	/*
727c4338209SJason Wessel 	 * Wait for the other CPUs to be notified and be waiting for us:
728c4338209SJason Wessel 	 */
7292d13bb64SDouglas Anderson 	time_left = MSEC_PER_SEC;
730a1465d2fSDaniel Thompson 	while (kgdb_do_roundup && --time_left &&
731a1465d2fSDaniel Thompson 	       (atomic_read(&masters_in_kgdb) + atomic_read(&slaves_in_kgdb)) !=
732a1465d2fSDaniel Thompson 		   online_cpus)
7332d13bb64SDouglas Anderson 		udelay(1000);
734a1465d2fSDaniel Thompson 	if (!time_left)
735df0036d1SJason Wessel 		pr_crit("Timed out waiting for secondary CPUs.\n");
736c4338209SJason Wessel 
737c4338209SJason Wessel 	/*
738c4338209SJason Wessel 	 * At this point the primary processor is completely
739c4338209SJason Wessel 	 * in the debugger and all secondary CPUs are quiescent
740c4338209SJason Wessel 	 */
741dcc78711SJason Wessel 	dbg_deactivate_sw_breakpoints();
742c4338209SJason Wessel 	kgdb_single_step = 0;
743c4338209SJason Wessel 	kgdb_contthread = current;
744c4338209SJason Wessel 	exception_level = 0;
745c4338209SJason Wessel 	trace_on = tracing_is_on();
746c4338209SJason Wessel 	if (trace_on)
747c4338209SJason Wessel 		tracing_off();
748c4338209SJason Wessel 
749dcc78711SJason Wessel 	while (1) {
750dcc78711SJason Wessel cpu_master_loop:
751dcc78711SJason Wessel 		if (dbg_kdb_mode) {
752dcc78711SJason Wessel 			kgdb_connected = 1;
753dcc78711SJason Wessel 			error = kdb_stub(ks);
7543fa43abaSJason Wessel 			if (error == -1)
7553fa43abaSJason Wessel 				continue;
756b0679c63SJason Wessel 			kgdb_connected = 0;
757dcc78711SJason Wessel 		} else {
758c4338209SJason Wessel 			error = gdb_serial_stub(ks);
759dcc78711SJason Wessel 		}
760dcc78711SJason Wessel 
761dcc78711SJason Wessel 		if (error == DBG_PASS_EVENT) {
762dcc78711SJason Wessel 			dbg_kdb_mode = !dbg_kdb_mode;
763dcc78711SJason Wessel 		} else if (error == DBG_SWITCH_CPU_EVENT) {
764495363d3SJason Wessel 			kgdb_info[dbg_switch_cpu].exception_state |=
765495363d3SJason Wessel 				DCPU_NEXT_MASTER;
766dcc78711SJason Wessel 			goto cpu_loop;
767dcc78711SJason Wessel 		} else {
768dcc78711SJason Wessel 			kgdb_info[cpu].ret_state = error;
769dcc78711SJason Wessel 			break;
770dcc78711SJason Wessel 		}
771dcc78711SJason Wessel 	}
772c4338209SJason Wessel 
773771910f7SDaniel Thompson 	dbg_activate_sw_breakpoints();
774771910f7SDaniel Thompson 
775c4338209SJason Wessel 	/* Call the I/O driver's post_exception routine */
77653197fc4SJason Wessel 	if (dbg_io_ops->post_exception)
77753197fc4SJason Wessel 		dbg_io_ops->post_exception();
778c4338209SJason Wessel 
779202164fbSDouglas Anderson 	atomic_dec(&ignore_console_lock_warning);
780202164fbSDouglas Anderson 
781c4338209SJason Wessel 	if (!kgdb_single_step) {
782dfee3a7bSJason Wessel 		raw_spin_unlock(&dbg_slave_lock);
783dfee3a7bSJason Wessel 		/* Wait till all the CPUs have quit from the debugger. */
784dfee3a7bSJason Wessel 		while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb))
785c4338209SJason Wessel 			cpu_relax();
786c4338209SJason Wessel 	}
787c4338209SJason Wessel 
788c4338209SJason Wessel kgdb_restore:
789c4338209SJason Wessel 	if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
790c4338209SJason Wessel 		int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
791c4338209SJason Wessel 		if (kgdb_info[sstep_cpu].task)
792c4338209SJason Wessel 			kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
793c4338209SJason Wessel 		else
794c4338209SJason Wessel 			kgdb_sstep_pid = 0;
795c4338209SJason Wessel 	}
796c1bb9a9cSDongdong Deng 	if (arch_kgdb_ops.correct_hw_break)
797c1bb9a9cSDongdong Deng 		arch_kgdb_ops.correct_hw_break();
798c4338209SJason Wessel 	if (trace_on)
799c4338209SJason Wessel 		tracing_on();
800dfee3a7bSJason Wessel 
801162bc7f5SDouglas Anderson 	kgdb_info[cpu].debuggerinfo = NULL;
802162bc7f5SDouglas Anderson 	kgdb_info[cpu].task = NULL;
803dfee3a7bSJason Wessel 	kgdb_info[cpu].exception_state &=
804dfee3a7bSJason Wessel 		~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
805dfee3a7bSJason Wessel 	kgdb_info[cpu].enter_kgdb--;
8064e857c58SPeter Zijlstra 	smp_mb__before_atomic();
807dfee3a7bSJason Wessel 	atomic_dec(&masters_in_kgdb);
808c4338209SJason Wessel 	/* Free kgdb_active */
809c4338209SJason Wessel 	atomic_set(&kgdb_active, -1);
810dfee3a7bSJason Wessel 	raw_spin_unlock(&dbg_master_lock);
81116cdc628SJason Wessel 	dbg_touch_watchdogs();
812c4338209SJason Wessel 	local_irq_restore(flags);
813440ab9e1SDouglas Anderson 	rcu_read_unlock();
814c4338209SJason Wessel 
815dcc78711SJason Wessel 	return kgdb_info[cpu].ret_state;
816c4338209SJason Wessel }
8174c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_cpu_enter);
818c4338209SJason Wessel 
819c4338209SJason Wessel /*
820c4338209SJason Wessel  * kgdb_handle_exception() - main entry point from a kernel exception
821c4338209SJason Wessel  *
822c4338209SJason Wessel  * Locking hierarchy:
823c4338209SJason Wessel  *	interface locks, if any (begin_session)
824c4338209SJason Wessel  *	kgdb lock (kgdb_active)
825c4338209SJason Wessel  */
826c4338209SJason Wessel int
827c4338209SJason Wessel kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
828c4338209SJason Wessel {
829c4338209SJason Wessel 	struct kgdb_state kgdb_var;
830c4338209SJason Wessel 	struct kgdb_state *ks = &kgdb_var;
8315a14feadSAnton Vorontsov 	int ret = 0;
8325a14feadSAnton Vorontsov 
8335a14feadSAnton Vorontsov 	if (arch_kgdb_ops.enable_nmi)
8345a14feadSAnton Vorontsov 		arch_kgdb_ops.enable_nmi(0);
8355516fd7bSColin Cross 	/*
8365516fd7bSColin Cross 	 * Avoid entering the debugger if we were triggered due to an oops
8375516fd7bSColin Cross 	 * but panic_timeout indicates the system should automatically
8385516fd7bSColin Cross 	 * reboot on panic. We don't want to get stuck waiting for input
8395516fd7bSColin Cross 	 * on such systems, especially if its "just" an oops.
8405516fd7bSColin Cross 	 */
8415516fd7bSColin Cross 	if (signo != SIGTRAP && panic_timeout)
8425516fd7bSColin Cross 		return 1;
843c4338209SJason Wessel 
8448daaa5f8SMike Travis 	memset(ks, 0, sizeof(struct kgdb_state));
845c4338209SJason Wessel 	ks->cpu			= raw_smp_processor_id();
846c4338209SJason Wessel 	ks->ex_vector		= evector;
847c4338209SJason Wessel 	ks->signo		= signo;
848c4338209SJason Wessel 	ks->err_code		= ecode;
849c4338209SJason Wessel 	ks->linux_regs		= regs;
850c4338209SJason Wessel 
851c4338209SJason Wessel 	if (kgdb_reenter_check(ks))
8525a14feadSAnton Vorontsov 		goto out; /* Ouch, double exception ! */
853dfee3a7bSJason Wessel 	if (kgdb_info[ks->cpu].enter_kgdb != 0)
8545a14feadSAnton Vorontsov 		goto out;
855dfee3a7bSJason Wessel 
8565a14feadSAnton Vorontsov 	ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
8575a14feadSAnton Vorontsov out:
8585a14feadSAnton Vorontsov 	if (arch_kgdb_ops.enable_nmi)
8595a14feadSAnton Vorontsov 		arch_kgdb_ops.enable_nmi(1);
8605a14feadSAnton Vorontsov 	return ret;
861c4338209SJason Wessel }
8624c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_handle_exception);
863c4338209SJason Wessel 
864f30fed10SJason Wessel /*
865d8a050f5SNadav Amit  * GDB places a breakpoint at this function to know dynamically loaded objects.
866f30fed10SJason Wessel  */
867f30fed10SJason Wessel static int module_event(struct notifier_block *self, unsigned long val,
868f30fed10SJason Wessel 	void *data)
869f30fed10SJason Wessel {
870f30fed10SJason Wessel 	return 0;
871f30fed10SJason Wessel }
872f30fed10SJason Wessel 
873f30fed10SJason Wessel static struct notifier_block dbg_module_load_nb = {
874f30fed10SJason Wessel 	.notifier_call	= module_event,
875f30fed10SJason Wessel };
876f30fed10SJason Wessel 
877c4338209SJason Wessel int kgdb_nmicallback(int cpu, void *regs)
878c4338209SJason Wessel {
879c4338209SJason Wessel #ifdef CONFIG_SMP
880c4338209SJason Wessel 	struct kgdb_state kgdb_var;
881c4338209SJason Wessel 	struct kgdb_state *ks = &kgdb_var;
882c4338209SJason Wessel 
88387b09592SDouglas Anderson 	kgdb_info[cpu].rounding_up = false;
88487b09592SDouglas Anderson 
885c4338209SJason Wessel 	memset(ks, 0, sizeof(struct kgdb_state));
886c4338209SJason Wessel 	ks->cpu			= cpu;
887c4338209SJason Wessel 	ks->linux_regs		= regs;
888c4338209SJason Wessel 
889dfee3a7bSJason Wessel 	if (kgdb_info[ks->cpu].enter_kgdb == 0 &&
890dfee3a7bSJason Wessel 			raw_spin_is_locked(&dbg_master_lock)) {
891dfee3a7bSJason Wessel 		kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE);
892c4338209SJason Wessel 		return 0;
893c4338209SJason Wessel 	}
894c4338209SJason Wessel #endif
895c4338209SJason Wessel 	return 1;
896c4338209SJason Wessel }
8974c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_nmicallback);
898c4338209SJason Wessel 
899fc8b1374SMike Travis int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
900fc8b1374SMike Travis 							atomic_t *send_ready)
9018daaa5f8SMike Travis {
9028daaa5f8SMike Travis #ifdef CONFIG_SMP
9038daaa5f8SMike Travis 	if (!kgdb_io_ready(0) || !send_ready)
9048daaa5f8SMike Travis 		return 1;
9058daaa5f8SMike Travis 
9068daaa5f8SMike Travis 	if (kgdb_info[cpu].enter_kgdb == 0) {
9078daaa5f8SMike Travis 		struct kgdb_state kgdb_var;
9088daaa5f8SMike Travis 		struct kgdb_state *ks = &kgdb_var;
9098daaa5f8SMike Travis 
9108daaa5f8SMike Travis 		memset(ks, 0, sizeof(struct kgdb_state));
9118daaa5f8SMike Travis 		ks->cpu			= cpu;
9128daaa5f8SMike Travis 		ks->ex_vector		= trapnr;
9138daaa5f8SMike Travis 		ks->signo		= SIGTRAP;
914fc8b1374SMike Travis 		ks->err_code		= err_code;
9158daaa5f8SMike Travis 		ks->linux_regs		= regs;
9168daaa5f8SMike Travis 		ks->send_ready		= send_ready;
9178daaa5f8SMike Travis 		kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
9188daaa5f8SMike Travis 		return 0;
9198daaa5f8SMike Travis 	}
9208daaa5f8SMike Travis #endif
9218daaa5f8SMike Travis 	return 1;
9228daaa5f8SMike Travis }
9234c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_nmicallin);
9248daaa5f8SMike Travis 
925c4338209SJason Wessel static void kgdb_console_write(struct console *co, const char *s,
926c4338209SJason Wessel    unsigned count)
927c4338209SJason Wessel {
928c4338209SJason Wessel 	unsigned long flags;
929c4338209SJason Wessel 
930c4338209SJason Wessel 	/* If we're debugging, or KGDB has not connected, don't try
931c4338209SJason Wessel 	 * and print. */
932dcc78711SJason Wessel 	if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode)
933c4338209SJason Wessel 		return;
934c4338209SJason Wessel 
935c4338209SJason Wessel 	local_irq_save(flags);
93653197fc4SJason Wessel 	gdbstub_msg_write(s, count);
937c4338209SJason Wessel 	local_irq_restore(flags);
938c4338209SJason Wessel }
939c4338209SJason Wessel 
940c4338209SJason Wessel static struct console kgdbcons = {
941c4338209SJason Wessel 	.name		= "kgdb",
942c4338209SJason Wessel 	.write		= kgdb_console_write,
943c4338209SJason Wessel 	.flags		= CON_PRINTBUFFER | CON_ENABLED,
944c4338209SJason Wessel 	.index		= -1,
945c4338209SJason Wessel };
946c4338209SJason Wessel 
947b18b099eSDouglas Anderson static int __init opt_kgdb_con(char *str)
948b18b099eSDouglas Anderson {
949b18b099eSDouglas Anderson 	kgdb_use_con = 1;
950b18b099eSDouglas Anderson 
951b18b099eSDouglas Anderson 	if (kgdb_io_module_registered && !kgdb_con_registered) {
952b18b099eSDouglas Anderson 		register_console(&kgdbcons);
953b18b099eSDouglas Anderson 		kgdb_con_registered = 1;
954b18b099eSDouglas Anderson 	}
955b18b099eSDouglas Anderson 
956b18b099eSDouglas Anderson 	return 0;
957b18b099eSDouglas Anderson }
958b18b099eSDouglas Anderson 
959b18b099eSDouglas Anderson early_param("kgdbcon", opt_kgdb_con);
960b18b099eSDouglas Anderson 
961c4338209SJason Wessel #ifdef CONFIG_MAGIC_SYSRQ
9621495cc9dSDmitry Torokhov static void sysrq_handle_dbg(int key)
963c4338209SJason Wessel {
96453197fc4SJason Wessel 	if (!dbg_io_ops) {
9650f16996cSFabian Frederick 		pr_crit("ERROR: No KGDB I/O module available\n");
966c4338209SJason Wessel 		return;
967c4338209SJason Wessel 	}
968dcc78711SJason Wessel 	if (!kgdb_connected) {
969dcc78711SJason Wessel #ifdef CONFIG_KGDB_KDB
970dcc78711SJason Wessel 		if (!dbg_kdb_mode)
9710f16996cSFabian Frederick 			pr_crit("KGDB or $3#33 for KDB\n");
972dcc78711SJason Wessel #else
9730f16996cSFabian Frederick 		pr_crit("Entering KGDB\n");
974dcc78711SJason Wessel #endif
975dcc78711SJason Wessel 	}
976c4338209SJason Wessel 
977c4338209SJason Wessel 	kgdb_breakpoint();
978c4338209SJason Wessel }
979c4338209SJason Wessel 
980c69b470eSEmil Velikov static const struct sysrq_key_op sysrq_dbg_op = {
98153197fc4SJason Wessel 	.handler	= sysrq_handle_dbg,
982f3456509Szhangwei(Jovi) 	.help_msg	= "debug(g)",
983c4338209SJason Wessel 	.action_msg	= "DEBUG",
984c4338209SJason Wessel };
985c4338209SJason Wessel #endif
986c4338209SJason Wessel 
9877d92bda2SDouglas Anderson void kgdb_panic(const char *msg)
9884402c153SJason Wessel {
9897d92bda2SDouglas Anderson 	if (!kgdb_io_module_registered)
9907d92bda2SDouglas Anderson 		return;
9917d92bda2SDouglas Anderson 
9925516fd7bSColin Cross 	/*
9937d92bda2SDouglas Anderson 	 * We don't want to get stuck waiting for input from user if
9947d92bda2SDouglas Anderson 	 * "panic_timeout" indicates the system should automatically
9955516fd7bSColin Cross 	 * reboot on panic.
9965516fd7bSColin Cross 	 */
9975516fd7bSColin Cross 	if (panic_timeout)
9987d92bda2SDouglas Anderson 		return;
9995516fd7bSColin Cross 
10004402c153SJason Wessel 	if (dbg_kdb_mode)
10017d92bda2SDouglas Anderson 		kdb_printf("PANIC: %s\n", msg);
10024402c153SJason Wessel 
10037d92bda2SDouglas Anderson 	kgdb_breakpoint();
10047d92bda2SDouglas Anderson }
10054402c153SJason Wessel 
1006b1a57bbfSDouglas Anderson static void kgdb_initial_breakpoint(void)
1007b1a57bbfSDouglas Anderson {
1008b1a57bbfSDouglas Anderson 	kgdb_break_asap = 0;
1009b1a57bbfSDouglas Anderson 
1010b1a57bbfSDouglas Anderson 	pr_crit("Waiting for connection from remote gdb...\n");
1011b1a57bbfSDouglas Anderson 	kgdb_breakpoint();
1012b1a57bbfSDouglas Anderson }
1013b1a57bbfSDouglas Anderson 
10140b4b3827SJason Wessel void __weak kgdb_arch_late(void)
10150b4b3827SJason Wessel {
10160b4b3827SJason Wessel }
10170b4b3827SJason Wessel 
10180b4b3827SJason Wessel void __init dbg_late_init(void)
10190b4b3827SJason Wessel {
10200b4b3827SJason Wessel 	dbg_is_early = false;
10210b4b3827SJason Wessel 	if (kgdb_io_module_registered)
10220b4b3827SJason Wessel 		kgdb_arch_late();
10230b4b3827SJason Wessel 	kdb_init(KDB_INIT_FULL);
1024b1a57bbfSDouglas Anderson 
1025b1a57bbfSDouglas Anderson 	if (kgdb_io_module_registered && kgdb_break_asap)
1026b1a57bbfSDouglas Anderson 		kgdb_initial_breakpoint();
10270b4b3827SJason Wessel }
10280b4b3827SJason Wessel 
10292366e047SJason Wessel static int
10302366e047SJason Wessel dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
10312366e047SJason Wessel {
1032bec4d62eSJason Wessel 	/*
1033bec4d62eSJason Wessel 	 * Take the following action on reboot notify depending on value:
1034bec4d62eSJason Wessel 	 *    1 == Enter debugger
1035220a31b0SZhen Lei 	 *    0 == [the default] detach debug client
1036bec4d62eSJason Wessel 	 *   -1 == Do nothing... and use this until the board resets
1037bec4d62eSJason Wessel 	 */
1038bec4d62eSJason Wessel 	switch (kgdbreboot) {
1039bec4d62eSJason Wessel 	case 1:
1040bec4d62eSJason Wessel 		kgdb_breakpoint();
1041*c8daba46SGustavo A. R. Silva 		goto done;
1042bec4d62eSJason Wessel 	case -1:
1043bec4d62eSJason Wessel 		goto done;
1044bec4d62eSJason Wessel 	}
10452366e047SJason Wessel 	if (!dbg_kdb_mode)
10462366e047SJason Wessel 		gdbstub_exit(code);
1047bec4d62eSJason Wessel done:
10482366e047SJason Wessel 	return NOTIFY_DONE;
10492366e047SJason Wessel }
10502366e047SJason Wessel 
10512366e047SJason Wessel static struct notifier_block dbg_reboot_notifier = {
10522366e047SJason Wessel 	.notifier_call		= dbg_notify_reboot,
10532366e047SJason Wessel 	.next			= NULL,
10542366e047SJason Wessel 	.priority		= INT_MAX,
10552366e047SJason Wessel };
10562366e047SJason Wessel 
1057c4338209SJason Wessel static void kgdb_register_callbacks(void)
1058c4338209SJason Wessel {
1059c4338209SJason Wessel 	if (!kgdb_io_module_registered) {
1060c4338209SJason Wessel 		kgdb_io_module_registered = 1;
1061c4338209SJason Wessel 		kgdb_arch_init();
10620b4b3827SJason Wessel 		if (!dbg_is_early)
10630b4b3827SJason Wessel 			kgdb_arch_late();
1064f30fed10SJason Wessel 		register_module_notifier(&dbg_module_load_nb);
10652366e047SJason Wessel 		register_reboot_notifier(&dbg_reboot_notifier);
1066c4338209SJason Wessel #ifdef CONFIG_MAGIC_SYSRQ
106753197fc4SJason Wessel 		register_sysrq_key('g', &sysrq_dbg_op);
1068c4338209SJason Wessel #endif
1069c4338209SJason Wessel 		if (kgdb_use_con && !kgdb_con_registered) {
1070c4338209SJason Wessel 			register_console(&kgdbcons);
1071c4338209SJason Wessel 			kgdb_con_registered = 1;
1072c4338209SJason Wessel 		}
1073c4338209SJason Wessel 	}
1074c4338209SJason Wessel }
1075c4338209SJason Wessel 
1076c4338209SJason Wessel static void kgdb_unregister_callbacks(void)
1077c4338209SJason Wessel {
1078c4338209SJason Wessel 	/*
10797d92bda2SDouglas Anderson 	 * When this routine is called KGDB should unregister from
10807d92bda2SDouglas Anderson 	 * handlers and clean up, making sure it is not handling any
1081c4338209SJason Wessel 	 * break exceptions at the time.
1082c4338209SJason Wessel 	 */
1083c4338209SJason Wessel 	if (kgdb_io_module_registered) {
1084c4338209SJason Wessel 		kgdb_io_module_registered = 0;
10852366e047SJason Wessel 		unregister_reboot_notifier(&dbg_reboot_notifier);
1086f30fed10SJason Wessel 		unregister_module_notifier(&dbg_module_load_nb);
1087c4338209SJason Wessel 		kgdb_arch_exit();
1088c4338209SJason Wessel #ifdef CONFIG_MAGIC_SYSRQ
108953197fc4SJason Wessel 		unregister_sysrq_key('g', &sysrq_dbg_op);
1090c4338209SJason Wessel #endif
1091c4338209SJason Wessel 		if (kgdb_con_registered) {
1092c4338209SJason Wessel 			unregister_console(&kgdbcons);
1093c4338209SJason Wessel 			kgdb_con_registered = 0;
1094c4338209SJason Wessel 		}
1095c4338209SJason Wessel 	}
1096c4338209SJason Wessel }
1097c4338209SJason Wessel 
1098c4338209SJason Wessel /**
1099c4338209SJason Wessel  *	kgdb_register_io_module - register KGDB IO module
110053197fc4SJason Wessel  *	@new_dbg_io_ops: the io ops vector
1101c4338209SJason Wessel  *
1102c4338209SJason Wessel  *	Register it with the KGDB core.
1103c4338209SJason Wessel  */
110453197fc4SJason Wessel int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
1105c4338209SJason Wessel {
110622099562SDouglas Anderson 	struct kgdb_io *old_dbg_io_ops;
1107c4338209SJason Wessel 	int err;
1108c4338209SJason Wessel 
1109c4338209SJason Wessel 	spin_lock(&kgdb_registration_lock);
1110c4338209SJason Wessel 
111122099562SDouglas Anderson 	old_dbg_io_ops = dbg_io_ops;
111222099562SDouglas Anderson 	if (old_dbg_io_ops) {
111322099562SDouglas Anderson 		if (!old_dbg_io_ops->deinit) {
1114c4338209SJason Wessel 			spin_unlock(&kgdb_registration_lock);
1115c4338209SJason Wessel 
111622099562SDouglas Anderson 			pr_err("KGDB I/O driver %s can't replace %s.\n",
111722099562SDouglas Anderson 				new_dbg_io_ops->name, old_dbg_io_ops->name);
1118c4338209SJason Wessel 			return -EBUSY;
1119c4338209SJason Wessel 		}
112022099562SDouglas Anderson 		pr_info("Replacing I/O driver %s with %s\n",
112122099562SDouglas Anderson 			old_dbg_io_ops->name, new_dbg_io_ops->name);
112222099562SDouglas Anderson 	}
1123c4338209SJason Wessel 
112453197fc4SJason Wessel 	if (new_dbg_io_ops->init) {
112553197fc4SJason Wessel 		err = new_dbg_io_ops->init();
1126c4338209SJason Wessel 		if (err) {
1127c4338209SJason Wessel 			spin_unlock(&kgdb_registration_lock);
1128c4338209SJason Wessel 			return err;
1129c4338209SJason Wessel 		}
1130c4338209SJason Wessel 	}
1131c4338209SJason Wessel 
113253197fc4SJason Wessel 	dbg_io_ops = new_dbg_io_ops;
1133c4338209SJason Wessel 
1134c4338209SJason Wessel 	spin_unlock(&kgdb_registration_lock);
1135c4338209SJason Wessel 
1136b1350132SDouglas Anderson 	if (old_dbg_io_ops) {
1137b1350132SDouglas Anderson 		old_dbg_io_ops->deinit();
113822099562SDouglas Anderson 		return 0;
1139b1350132SDouglas Anderson 	}
114022099562SDouglas Anderson 
11410f16996cSFabian Frederick 	pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name);
1142c4338209SJason Wessel 
1143c4338209SJason Wessel 	/* Arm KGDB now. */
1144c4338209SJason Wessel 	kgdb_register_callbacks();
1145c4338209SJason Wessel 
1146b1a57bbfSDouglas Anderson 	if (kgdb_break_asap &&
1147b1a57bbfSDouglas Anderson 	    (!dbg_is_early || IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG)))
1148c4338209SJason Wessel 		kgdb_initial_breakpoint();
1149c4338209SJason Wessel 
1150c4338209SJason Wessel 	return 0;
1151c4338209SJason Wessel }
1152c4338209SJason Wessel EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1153c4338209SJason Wessel 
1154c4338209SJason Wessel /**
11552da2687bSLukas Bulwahn  *	kgdb_unregister_io_module - unregister KGDB IO module
115653197fc4SJason Wessel  *	@old_dbg_io_ops: the io ops vector
1157c4338209SJason Wessel  *
1158c4338209SJason Wessel  *	Unregister it with the KGDB core.
1159c4338209SJason Wessel  */
116053197fc4SJason Wessel void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
1161c4338209SJason Wessel {
1162c4338209SJason Wessel 	BUG_ON(kgdb_connected);
1163c4338209SJason Wessel 
1164c4338209SJason Wessel 	/*
1165c4338209SJason Wessel 	 * KGDB is no longer able to communicate out, so
1166c4338209SJason Wessel 	 * unregister our callbacks and reset state.
1167c4338209SJason Wessel 	 */
1168c4338209SJason Wessel 	kgdb_unregister_callbacks();
1169c4338209SJason Wessel 
1170c4338209SJason Wessel 	spin_lock(&kgdb_registration_lock);
1171c4338209SJason Wessel 
117253197fc4SJason Wessel 	WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
117353197fc4SJason Wessel 	dbg_io_ops = NULL;
1174c4338209SJason Wessel 
1175c4338209SJason Wessel 	spin_unlock(&kgdb_registration_lock);
1176c4338209SJason Wessel 
117722099562SDouglas Anderson 	if (old_dbg_io_ops->deinit)
117822099562SDouglas Anderson 		old_dbg_io_ops->deinit();
117922099562SDouglas Anderson 
11800f16996cSFabian Frederick 	pr_info("Unregistered I/O driver %s, debugger disabled\n",
118153197fc4SJason Wessel 		old_dbg_io_ops->name);
1182c4338209SJason Wessel }
1183c4338209SJason Wessel EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1184c4338209SJason Wessel 
1185dcc78711SJason Wessel int dbg_io_get_char(void)
1186dcc78711SJason Wessel {
1187dcc78711SJason Wessel 	int ret = dbg_io_ops->read_char();
1188f5316b4aSJason Wessel 	if (ret == NO_POLL_CHAR)
1189f5316b4aSJason Wessel 		return -1;
1190dcc78711SJason Wessel 	if (!dbg_kdb_mode)
1191dcc78711SJason Wessel 		return ret;
1192dcc78711SJason Wessel 	if (ret == 127)
1193dcc78711SJason Wessel 		return 8;
1194dcc78711SJason Wessel 	return ret;
1195dcc78711SJason Wessel }
1196dcc78711SJason Wessel 
1197c4338209SJason Wessel /**
1198c4338209SJason Wessel  * kgdb_breakpoint - generate breakpoint exception
1199c4338209SJason Wessel  *
1200c4338209SJason Wessel  * This function will generate a breakpoint exception.  It is used at the
1201c4338209SJason Wessel  * beginning of a program to sync up with a debugger and can be used
1202c4338209SJason Wessel  * otherwise as a quick means to stop program execution and "break" into
1203c4338209SJason Wessel  * the debugger.
1204c4338209SJason Wessel  */
1205d498d4b4SVijaya Kumar K noinline void kgdb_breakpoint(void)
1206c4338209SJason Wessel {
1207c4338209SJason Wessel 	atomic_inc(&kgdb_setting_breakpoint);
1208c4338209SJason Wessel 	wmb(); /* Sync point before breakpoint */
1209c4338209SJason Wessel 	arch_kgdb_breakpoint();
1210c4338209SJason Wessel 	wmb(); /* Sync point after breakpoint */
1211c4338209SJason Wessel 	atomic_dec(&kgdb_setting_breakpoint);
1212c4338209SJason Wessel }
1213c4338209SJason Wessel EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1214c4338209SJason Wessel 
1215c4338209SJason Wessel static int __init opt_kgdb_wait(char *str)
1216c4338209SJason Wessel {
1217c4338209SJason Wessel 	kgdb_break_asap = 1;
1218c4338209SJason Wessel 
1219dcc78711SJason Wessel 	kdb_init(KDB_INIT_EARLY);
1220b1a57bbfSDouglas Anderson 	if (kgdb_io_module_registered &&
1221b1a57bbfSDouglas Anderson 	    IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG))
1222c4338209SJason Wessel 		kgdb_initial_breakpoint();
1223c4338209SJason Wessel 
1224c4338209SJason Wessel 	return 0;
1225c4338209SJason Wessel }
1226c4338209SJason Wessel 
1227c4338209SJason Wessel early_param("kgdbwait", opt_kgdb_wait);
1228