xref: /linux-6.15/kernel/debug/debug_core.c (revision f8416aa2)
1*f8416aa2SCai Huoqing // SPDX-License-Identifier: GPL-2.0-only
2c4338209SJason Wessel /*
353197fc4SJason Wessel  * Kernel Debug Core
4c4338209SJason Wessel  *
5c4338209SJason Wessel  * Maintainer: Jason Wessel <[email protected]>
6c4338209SJason Wessel  *
7c4338209SJason Wessel  * Copyright (C) 2000-2001 VERITAS Software Corporation.
8c4338209SJason Wessel  * Copyright (C) 2002-2004 Timesys Corporation
9c4338209SJason Wessel  * Copyright (C) 2003-2004 Amit S. Kale <[email protected]>
10a2531293SPavel Machek  * Copyright (C) 2004 Pavel Machek <[email protected]>
11c4338209SJason Wessel  * Copyright (C) 2004-2006 Tom Rini <[email protected]>
12c4338209SJason Wessel  * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
1353197fc4SJason Wessel  * Copyright (C) 2005-2009 Wind River Systems, Inc.
14c4338209SJason Wessel  * Copyright (C) 2007 MontaVista Software, Inc.
15c4338209SJason Wessel  * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <[email protected]>
16c4338209SJason Wessel  *
17c4338209SJason Wessel  * Contributors at various stages not listed above:
18c4338209SJason Wessel  *  Jason Wessel ( [email protected] )
19c4338209SJason Wessel  *  George Anzinger <[email protected]>
20c4338209SJason Wessel  *  Anurekh Saxena ([email protected])
21c4338209SJason Wessel  *  Lake Stevens Instrument Division (Glenn Engel)
22c4338209SJason Wessel  *  Jim Kingdon, Cygnus Support.
23c4338209SJason Wessel  *
24c4338209SJason Wessel  * Original KGDB stub: David Grothe <[email protected]>,
25c4338209SJason Wessel  * Tigran Aivazian <[email protected]>
26c4338209SJason Wessel  */
270f16996cSFabian Frederick 
280f16996cSFabian Frederick #define pr_fmt(fmt) "KGDB: " fmt
290f16996cSFabian Frederick 
30c4338209SJason Wessel #include <linux/pid_namespace.h>
31c4338209SJason Wessel #include <linux/clocksource.h>
3216559ae4SGreg Kroah-Hartman #include <linux/serial_core.h>
33c4338209SJason Wessel #include <linux/interrupt.h>
34c4338209SJason Wessel #include <linux/spinlock.h>
35c4338209SJason Wessel #include <linux/console.h>
36c4338209SJason Wessel #include <linux/threads.h>
37c4338209SJason Wessel #include <linux/uaccess.h>
38c4338209SJason Wessel #include <linux/kernel.h>
39c4338209SJason Wessel #include <linux/module.h>
40c4338209SJason Wessel #include <linux/ptrace.h>
41c4338209SJason Wessel #include <linux/string.h>
42c4338209SJason Wessel #include <linux/delay.h>
43c4338209SJason Wessel #include <linux/sched.h>
44c4338209SJason Wessel #include <linux/sysrq.h>
452366e047SJason Wessel #include <linux/reboot.h>
46c4338209SJason Wessel #include <linux/init.h>
47c4338209SJason Wessel #include <linux/kgdb.h>
48dcc78711SJason Wessel #include <linux/kdb.h>
4938b8d208SIngo Molnar #include <linux/nmi.h>
50c4338209SJason Wessel #include <linux/pid.h>
51c4338209SJason Wessel #include <linux/smp.h>
52c4338209SJason Wessel #include <linux/mm.h>
53615d6e87SDavidlohr Bueso #include <linux/vmacache.h>
54fb70b588SJason Wessel #include <linux/rcupdate.h>
553cd99ac3SDouglas Anderson #include <linux/irq.h>
56c4338209SJason Wessel 
57c4338209SJason Wessel #include <asm/cacheflush.h>
58c4338209SJason Wessel #include <asm/byteorder.h>
5960063497SArun Sharma #include <linux/atomic.h>
6053197fc4SJason Wessel 
6153197fc4SJason Wessel #include "debug_core.h"
62c4338209SJason Wessel 
63c4338209SJason Wessel static int kgdb_break_asap;
64c4338209SJason Wessel 
6553197fc4SJason Wessel struct debuggerinfo_struct kgdb_info[NR_CPUS];
66c4338209SJason Wessel 
67a1350207SAndy Shevchenko /* kgdb_connected - Is a host GDB connected to us? */
68c4338209SJason Wessel int				kgdb_connected;
69c4338209SJason Wessel EXPORT_SYMBOL_GPL(kgdb_connected);
70c4338209SJason Wessel 
71c4338209SJason Wessel /* All the KGDB handlers are installed */
72f503b5aeSJason Wessel int			kgdb_io_module_registered;
73c4338209SJason Wessel 
74c4338209SJason Wessel /* Guard for recursive entry */
75c4338209SJason Wessel static int			exception_level;
76c4338209SJason Wessel 
7753197fc4SJason Wessel struct kgdb_io		*dbg_io_ops;
78c4338209SJason Wessel static DEFINE_SPINLOCK(kgdb_registration_lock);
79c4338209SJason Wessel 
80e16c33e2SYouling Tang /* Action for the reboot notifier, a global allow kdb to change it */
81bec4d62eSJason Wessel static int kgdbreboot;
82c4338209SJason Wessel /* kgdb console driver is loaded */
83c4338209SJason Wessel static int kgdb_con_registered;
84c4338209SJason Wessel /* determine if kgdb console output should be used */
85c4338209SJason Wessel static int kgdb_use_con;
860b4b3827SJason Wessel /* Flag for alternate operations for early debugging */
870b4b3827SJason Wessel bool dbg_is_early = true;
88dcc78711SJason Wessel /* Next cpu to become the master debug core */
89dcc78711SJason Wessel int dbg_switch_cpu;
90dcc78711SJason Wessel 
91dcc78711SJason Wessel /* Use kdb or gdbserver mode */
92a0de055cSJason Wessel int dbg_kdb_mode = 1;
93c4338209SJason Wessel 
94c4338209SJason Wessel module_param(kgdb_use_con, int, 0644);
95bec4d62eSJason Wessel module_param(kgdbreboot, int, 0644);
96c4338209SJason Wessel 
97c4338209SJason Wessel /*
98c4338209SJason Wessel  * Holds information about breakpoints in a kernel. These breakpoints are
99c4338209SJason Wessel  * added and removed by gdb.
100c4338209SJason Wessel  */
101c4338209SJason Wessel static struct kgdb_bkpt		kgdb_break[KGDB_MAX_BREAKPOINTS] = {
102c4338209SJason Wessel 	[0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
103c4338209SJason Wessel };
104c4338209SJason Wessel 
105c4338209SJason Wessel /*
106c4338209SJason Wessel  * The CPU# of the active CPU, or -1 if none:
107c4338209SJason Wessel  */
108c4338209SJason Wessel atomic_t			kgdb_active = ATOMIC_INIT(-1);
109dcc78711SJason Wessel EXPORT_SYMBOL_GPL(kgdb_active);
110dfee3a7bSJason Wessel static DEFINE_RAW_SPINLOCK(dbg_master_lock);
111dfee3a7bSJason Wessel static DEFINE_RAW_SPINLOCK(dbg_slave_lock);
112c4338209SJason Wessel 
113c4338209SJason Wessel /*
114c4338209SJason Wessel  * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
115c4338209SJason Wessel  * bootup code (which might not have percpu set up yet):
116c4338209SJason Wessel  */
117dfee3a7bSJason Wessel static atomic_t			masters_in_kgdb;
118dfee3a7bSJason Wessel static atomic_t			slaves_in_kgdb;
119c4338209SJason Wessel atomic_t			kgdb_setting_breakpoint;
120c4338209SJason Wessel 
121c4338209SJason Wessel struct task_struct		*kgdb_usethread;
122c4338209SJason Wessel struct task_struct		*kgdb_contthread;
123c4338209SJason Wessel 
124c4338209SJason Wessel int				kgdb_single_step;
12553197fc4SJason Wessel static pid_t			kgdb_sstep_pid;
126c4338209SJason Wessel 
127c4338209SJason Wessel /* to keep track of the CPU which is doing the single stepping*/
128c4338209SJason Wessel atomic_t			kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
129c4338209SJason Wessel 
130c4338209SJason Wessel /*
131c4338209SJason Wessel  * If you are debugging a problem where roundup (the collection of
132c4338209SJason Wessel  * all other CPUs) is a problem [this should be extremely rare],
133c4338209SJason Wessel  * then use the nokgdbroundup option to avoid roundup. In that case
134c4338209SJason Wessel  * the other CPUs might interfere with your debugging context, so
135c4338209SJason Wessel  * use this with care:
136c4338209SJason Wessel  */
137c4338209SJason Wessel static int kgdb_do_roundup = 1;
138c4338209SJason Wessel 
139c4338209SJason Wessel static int __init opt_nokgdbroundup(char *str)
140c4338209SJason Wessel {
141c4338209SJason Wessel 	kgdb_do_roundup = 0;
142c4338209SJason Wessel 
143c4338209SJason Wessel 	return 0;
144c4338209SJason Wessel }
145c4338209SJason Wessel 
146c4338209SJason Wessel early_param("nokgdbroundup", opt_nokgdbroundup);
147c4338209SJason Wessel 
148c4338209SJason Wessel /*
149c4338209SJason Wessel  * Finally, some KGDB code :-)
150c4338209SJason Wessel  */
151c4338209SJason Wessel 
152c4338209SJason Wessel /*
153c4338209SJason Wessel  * Weak aliases for breakpoint management,
154e16c33e2SYouling Tang  * can be overridden by architectures when needed:
155c4338209SJason Wessel  */
15698b54aa1SJason Wessel int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
157c4338209SJason Wessel {
158c4338209SJason Wessel 	int err;
159c4338209SJason Wessel 
160fe557319SChristoph Hellwig 	err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
16198b54aa1SJason Wessel 				BREAK_INSTR_SIZE);
162c4338209SJason Wessel 	if (err)
163c4338209SJason Wessel 		return err;
164fe557319SChristoph Hellwig 	err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
16598b54aa1SJason Wessel 				 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
16698b54aa1SJason Wessel 	return err;
167c4338209SJason Wessel }
1684c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_arch_set_breakpoint);
169c4338209SJason Wessel 
17098b54aa1SJason Wessel int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
171c4338209SJason Wessel {
172fe557319SChristoph Hellwig 	return copy_to_kernel_nofault((char *)bpt->bpt_addr,
17398b54aa1SJason Wessel 				  (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
174c4338209SJason Wessel }
1754c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_arch_remove_breakpoint);
176c4338209SJason Wessel 
177c4338209SJason Wessel int __weak kgdb_validate_break_address(unsigned long addr)
178c4338209SJason Wessel {
17998b54aa1SJason Wessel 	struct kgdb_bkpt tmp;
180c4338209SJason Wessel 	int err;
181f2d10ff4SDaniel Thompson 
182f2d10ff4SDaniel Thompson 	if (kgdb_within_blocklist(addr))
183f2d10ff4SDaniel Thompson 		return -EINVAL;
184f2d10ff4SDaniel Thompson 
18598b54aa1SJason Wessel 	/* Validate setting the breakpoint and then removing it.  If the
186c4338209SJason Wessel 	 * remove fails, the kernel needs to emit a bad message because we
187c4338209SJason Wessel 	 * are deep trouble not being able to put things back the way we
188c4338209SJason Wessel 	 * found them.
189c4338209SJason Wessel 	 */
19098b54aa1SJason Wessel 	tmp.bpt_addr = addr;
19198b54aa1SJason Wessel 	err = kgdb_arch_set_breakpoint(&tmp);
192c4338209SJason Wessel 	if (err)
193c4338209SJason Wessel 		return err;
19498b54aa1SJason Wessel 	err = kgdb_arch_remove_breakpoint(&tmp);
195c4338209SJason Wessel 	if (err)
1960f16996cSFabian Frederick 		pr_err("Critical breakpoint error, kernel memory destroyed at: %lx\n",
1970f16996cSFabian Frederick 		       addr);
198c4338209SJason Wessel 	return err;
199c4338209SJason Wessel }
200c4338209SJason Wessel 
201c4338209SJason Wessel unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
202c4338209SJason Wessel {
203c4338209SJason Wessel 	return instruction_pointer(regs);
204c4338209SJason Wessel }
2054c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_arch_pc);
206c4338209SJason Wessel 
207c4338209SJason Wessel int __weak kgdb_arch_init(void)
208c4338209SJason Wessel {
209c4338209SJason Wessel 	return 0;
210c4338209SJason Wessel }
211c4338209SJason Wessel 
212c4338209SJason Wessel int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
213c4338209SJason Wessel {
214c4338209SJason Wessel 	return 0;
215c4338209SJason Wessel }
2164c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_skipexception);
217c4338209SJason Wessel 
2183cd99ac3SDouglas Anderson #ifdef CONFIG_SMP
2193cd99ac3SDouglas Anderson 
2203cd99ac3SDouglas Anderson /*
2213cd99ac3SDouglas Anderson  * Default (weak) implementation for kgdb_roundup_cpus
2223cd99ac3SDouglas Anderson  */
2233cd99ac3SDouglas Anderson 
2243cd99ac3SDouglas Anderson void __weak kgdb_call_nmi_hook(void *ignored)
2253cd99ac3SDouglas Anderson {
2263cd99ac3SDouglas Anderson 	/*
2273cd99ac3SDouglas Anderson 	 * NOTE: get_irq_regs() is supposed to get the registers from
2283cd99ac3SDouglas Anderson 	 * before the IPI interrupt happened and so is supposed to
2293cd99ac3SDouglas Anderson 	 * show where the processor was.  In some situations it's
2303cd99ac3SDouglas Anderson 	 * possible we might be called without an IPI, so it might be
2313cd99ac3SDouglas Anderson 	 * safer to figure out how to make kgdb_breakpoint() work
2323cd99ac3SDouglas Anderson 	 * properly here.
2333cd99ac3SDouglas Anderson 	 */
2343cd99ac3SDouglas Anderson 	kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
2353cd99ac3SDouglas Anderson }
2364c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_call_nmi_hook);
2373cd99ac3SDouglas Anderson 
238545b8c8dSPeter Zijlstra static DEFINE_PER_CPU(call_single_data_t, kgdb_roundup_csd) =
239545b8c8dSPeter Zijlstra 	CSD_INIT(kgdb_call_nmi_hook, NULL);
240545b8c8dSPeter Zijlstra 
2413cd99ac3SDouglas Anderson void __weak kgdb_roundup_cpus(void)
2423cd99ac3SDouglas Anderson {
2433cd99ac3SDouglas Anderson 	call_single_data_t *csd;
2443cd99ac3SDouglas Anderson 	int this_cpu = raw_smp_processor_id();
2453cd99ac3SDouglas Anderson 	int cpu;
24687b09592SDouglas Anderson 	int ret;
2473cd99ac3SDouglas Anderson 
2483cd99ac3SDouglas Anderson 	for_each_online_cpu(cpu) {
2493cd99ac3SDouglas Anderson 		/* No need to roundup ourselves */
2503cd99ac3SDouglas Anderson 		if (cpu == this_cpu)
2513cd99ac3SDouglas Anderson 			continue;
2523cd99ac3SDouglas Anderson 
2533cd99ac3SDouglas Anderson 		csd = &per_cpu(kgdb_roundup_csd, cpu);
25487b09592SDouglas Anderson 
25587b09592SDouglas Anderson 		/*
25687b09592SDouglas Anderson 		 * If it didn't round up last time, don't try again
25787b09592SDouglas Anderson 		 * since smp_call_function_single_async() will block.
25887b09592SDouglas Anderson 		 *
25987b09592SDouglas Anderson 		 * If rounding_up is false then we know that the
26087b09592SDouglas Anderson 		 * previous call must have at least started and that
26187b09592SDouglas Anderson 		 * means smp_call_function_single_async() won't block.
26287b09592SDouglas Anderson 		 */
26387b09592SDouglas Anderson 		if (kgdb_info[cpu].rounding_up)
26487b09592SDouglas Anderson 			continue;
26587b09592SDouglas Anderson 		kgdb_info[cpu].rounding_up = true;
26687b09592SDouglas Anderson 
26787b09592SDouglas Anderson 		ret = smp_call_function_single_async(cpu, csd);
26887b09592SDouglas Anderson 		if (ret)
26987b09592SDouglas Anderson 			kgdb_info[cpu].rounding_up = false;
2703cd99ac3SDouglas Anderson 	}
2713cd99ac3SDouglas Anderson }
2724c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_roundup_cpus);
2733cd99ac3SDouglas Anderson 
2743cd99ac3SDouglas Anderson #endif
2753cd99ac3SDouglas Anderson 
276c4338209SJason Wessel /*
277c4338209SJason Wessel  * Some architectures need cache flushes when we set/clear a
278c4338209SJason Wessel  * breakpoint:
279c4338209SJason Wessel  */
280c4338209SJason Wessel static void kgdb_flush_swbreak_addr(unsigned long addr)
281c4338209SJason Wessel {
282c4338209SJason Wessel 	if (!CACHE_FLUSH_IS_SAFE)
283c4338209SJason Wessel 		return;
284c4338209SJason Wessel 
285615d6e87SDavidlohr Bueso 	if (current->mm) {
286615d6e87SDavidlohr Bueso 		int i;
287615d6e87SDavidlohr Bueso 
288615d6e87SDavidlohr Bueso 		for (i = 0; i < VMACACHE_SIZE; i++) {
289314ff785SIngo Molnar 			if (!current->vmacache.vmas[i])
290615d6e87SDavidlohr Bueso 				continue;
291314ff785SIngo Molnar 			flush_cache_range(current->vmacache.vmas[i],
292c4338209SJason Wessel 					  addr, addr + BREAK_INSTR_SIZE);
293c4338209SJason Wessel 		}
294615d6e87SDavidlohr Bueso 	}
295615d6e87SDavidlohr Bueso 
296c4338209SJason Wessel 	/* Force flush instruction cache if it was outside the mm */
297c4338209SJason Wessel 	flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
298c4338209SJason Wessel }
2994c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_flush_swbreak_addr);
300c4338209SJason Wessel 
301c4338209SJason Wessel /*
302c4338209SJason Wessel  * SW breakpoint management:
303c4338209SJason Wessel  */
30453197fc4SJason Wessel int dbg_activate_sw_breakpoints(void)
305c4338209SJason Wessel {
306c4338209SJason Wessel 	int error;
307c4338209SJason Wessel 	int ret = 0;
308c4338209SJason Wessel 	int i;
309c4338209SJason Wessel 
310c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
311c4338209SJason Wessel 		if (kgdb_break[i].state != BP_SET)
312c4338209SJason Wessel 			continue;
313c4338209SJason Wessel 
31498b54aa1SJason Wessel 		error = kgdb_arch_set_breakpoint(&kgdb_break[i]);
315c4338209SJason Wessel 		if (error) {
316c4338209SJason Wessel 			ret = error;
3170f16996cSFabian Frederick 			pr_info("BP install failed: %lx\n",
31898b54aa1SJason Wessel 				kgdb_break[i].bpt_addr);
319c4338209SJason Wessel 			continue;
320c4338209SJason Wessel 		}
321c4338209SJason Wessel 
32298b54aa1SJason Wessel 		kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
323c4338209SJason Wessel 		kgdb_break[i].state = BP_ACTIVE;
324c4338209SJason Wessel 	}
325c4338209SJason Wessel 	return ret;
326c4338209SJason Wessel }
3274c4197edSDaniel Thompson NOKPROBE_SYMBOL(dbg_activate_sw_breakpoints);
328c4338209SJason Wessel 
32953197fc4SJason Wessel int dbg_set_sw_break(unsigned long addr)
330c4338209SJason Wessel {
331c4338209SJason Wessel 	int err = kgdb_validate_break_address(addr);
332c4338209SJason Wessel 	int breakno = -1;
333c4338209SJason Wessel 	int i;
334c4338209SJason Wessel 
335c4338209SJason Wessel 	if (err)
336c4338209SJason Wessel 		return err;
337c4338209SJason Wessel 
338c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
339c4338209SJason Wessel 		if ((kgdb_break[i].state == BP_SET) &&
340c4338209SJason Wessel 					(kgdb_break[i].bpt_addr == addr))
341c4338209SJason Wessel 			return -EEXIST;
342c4338209SJason Wessel 	}
343c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
344c4338209SJason Wessel 		if (kgdb_break[i].state == BP_REMOVED &&
345c4338209SJason Wessel 					kgdb_break[i].bpt_addr == addr) {
346c4338209SJason Wessel 			breakno = i;
347c4338209SJason Wessel 			break;
348c4338209SJason Wessel 		}
349c4338209SJason Wessel 	}
350c4338209SJason Wessel 
351c4338209SJason Wessel 	if (breakno == -1) {
352c4338209SJason Wessel 		for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
353c4338209SJason Wessel 			if (kgdb_break[i].state == BP_UNDEFINED) {
354c4338209SJason Wessel 				breakno = i;
355c4338209SJason Wessel 				break;
356c4338209SJason Wessel 			}
357c4338209SJason Wessel 		}
358c4338209SJason Wessel 	}
359c4338209SJason Wessel 
360c4338209SJason Wessel 	if (breakno == -1)
361c4338209SJason Wessel 		return -E2BIG;
362c4338209SJason Wessel 
363c4338209SJason Wessel 	kgdb_break[breakno].state = BP_SET;
364c4338209SJason Wessel 	kgdb_break[breakno].type = BP_BREAKPOINT;
365c4338209SJason Wessel 	kgdb_break[breakno].bpt_addr = addr;
366c4338209SJason Wessel 
367c4338209SJason Wessel 	return 0;
368c4338209SJason Wessel }
369c4338209SJason Wessel 
370dcc78711SJason Wessel int dbg_deactivate_sw_breakpoints(void)
371c4338209SJason Wessel {
372c4338209SJason Wessel 	int error;
373c4338209SJason Wessel 	int ret = 0;
374c4338209SJason Wessel 	int i;
375c4338209SJason Wessel 
376c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
377c4338209SJason Wessel 		if (kgdb_break[i].state != BP_ACTIVE)
378c4338209SJason Wessel 			continue;
37998b54aa1SJason Wessel 		error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
380c4338209SJason Wessel 		if (error) {
3810f16996cSFabian Frederick 			pr_info("BP remove failed: %lx\n",
38298b54aa1SJason Wessel 				kgdb_break[i].bpt_addr);
383c4338209SJason Wessel 			ret = error;
384c4338209SJason Wessel 		}
385c4338209SJason Wessel 
38698b54aa1SJason Wessel 		kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
387c4338209SJason Wessel 		kgdb_break[i].state = BP_SET;
388c4338209SJason Wessel 	}
389c4338209SJason Wessel 	return ret;
390c4338209SJason Wessel }
3914c4197edSDaniel Thompson NOKPROBE_SYMBOL(dbg_deactivate_sw_breakpoints);
392c4338209SJason Wessel 
39353197fc4SJason Wessel int dbg_remove_sw_break(unsigned long addr)
394c4338209SJason Wessel {
395c4338209SJason Wessel 	int i;
396c4338209SJason Wessel 
397c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
398c4338209SJason Wessel 		if ((kgdb_break[i].state == BP_SET) &&
399c4338209SJason Wessel 				(kgdb_break[i].bpt_addr == addr)) {
400c4338209SJason Wessel 			kgdb_break[i].state = BP_REMOVED;
401c4338209SJason Wessel 			return 0;
402c4338209SJason Wessel 		}
403c4338209SJason Wessel 	}
404c4338209SJason Wessel 	return -ENOENT;
405c4338209SJason Wessel }
406c4338209SJason Wessel 
407c4338209SJason Wessel int kgdb_isremovedbreak(unsigned long addr)
408c4338209SJason Wessel {
409c4338209SJason Wessel 	int i;
410c4338209SJason Wessel 
411c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
412c4338209SJason Wessel 		if ((kgdb_break[i].state == BP_REMOVED) &&
413c4338209SJason Wessel 					(kgdb_break[i].bpt_addr == addr))
414c4338209SJason Wessel 			return 1;
415c4338209SJason Wessel 	}
416c4338209SJason Wessel 	return 0;
417c4338209SJason Wessel }
418c4338209SJason Wessel 
419f83b04d3SVincent Chen int kgdb_has_hit_break(unsigned long addr)
420f83b04d3SVincent Chen {
421f83b04d3SVincent Chen 	int i;
422f83b04d3SVincent Chen 
423f83b04d3SVincent Chen 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
424f83b04d3SVincent Chen 		if (kgdb_break[i].state == BP_ACTIVE &&
425f83b04d3SVincent Chen 		    kgdb_break[i].bpt_addr == addr)
426f83b04d3SVincent Chen 			return 1;
427f83b04d3SVincent Chen 	}
428f83b04d3SVincent Chen 	return 0;
429f83b04d3SVincent Chen }
430f83b04d3SVincent Chen 
43153197fc4SJason Wessel int dbg_remove_all_break(void)
432c4338209SJason Wessel {
433c4338209SJason Wessel 	int error;
434c4338209SJason Wessel 	int i;
435c4338209SJason Wessel 
436c4338209SJason Wessel 	/* Clear memory breakpoints. */
437c4338209SJason Wessel 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
438c4338209SJason Wessel 		if (kgdb_break[i].state != BP_ACTIVE)
439c4338209SJason Wessel 			goto setundefined;
44098b54aa1SJason Wessel 		error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
441c4338209SJason Wessel 		if (error)
4420f16996cSFabian Frederick 			pr_err("breakpoint remove failed: %lx\n",
44398b54aa1SJason Wessel 			       kgdb_break[i].bpt_addr);
444c4338209SJason Wessel setundefined:
445c4338209SJason Wessel 		kgdb_break[i].state = BP_UNDEFINED;
446c4338209SJason Wessel 	}
447c4338209SJason Wessel 
448c4338209SJason Wessel 	/* Clear hardware breakpoints. */
449c4338209SJason Wessel 	if (arch_kgdb_ops.remove_all_hw_break)
450c4338209SJason Wessel 		arch_kgdb_ops.remove_all_hw_break();
451c4338209SJason Wessel 
452c4338209SJason Wessel 	return 0;
453c4338209SJason Wessel }
454c4338209SJason Wessel 
455d54ce615SSumit Garg void kgdb_free_init_mem(void)
456d54ce615SSumit Garg {
457d54ce615SSumit Garg 	int i;
458d54ce615SSumit Garg 
459d54ce615SSumit Garg 	/* Clear init memory breakpoints. */
460d54ce615SSumit Garg 	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
461d54ce615SSumit Garg 		if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))
462d54ce615SSumit Garg 			kgdb_break[i].state = BP_UNDEFINED;
463d54ce615SSumit Garg 	}
464d54ce615SSumit Garg }
465d54ce615SSumit Garg 
4662277b492SDouglas Anderson #ifdef CONFIG_KGDB_KDB
4672277b492SDouglas Anderson void kdb_dump_stack_on_cpu(int cpu)
4682277b492SDouglas Anderson {
469d07ce4e3SDaniel Thompson 	if (cpu == raw_smp_processor_id() || !IS_ENABLED(CONFIG_SMP)) {
4702277b492SDouglas Anderson 		dump_stack();
4712277b492SDouglas Anderson 		return;
4722277b492SDouglas Anderson 	}
4732277b492SDouglas Anderson 
4742277b492SDouglas Anderson 	if (!(kgdb_info[cpu].exception_state & DCPU_IS_SLAVE)) {
4752277b492SDouglas Anderson 		kdb_printf("ERROR: Task on cpu %d didn't stop in the debugger\n",
4762277b492SDouglas Anderson 			   cpu);
4772277b492SDouglas Anderson 		return;
4782277b492SDouglas Anderson 	}
4792277b492SDouglas Anderson 
4802277b492SDouglas Anderson 	/*
4812277b492SDouglas Anderson 	 * In general, architectures don't support dumping the stack of a
4822277b492SDouglas Anderson 	 * "running" process that's not the current one.  From the point of
4832277b492SDouglas Anderson 	 * view of the Linux, kernel processes that are looping in the kgdb
4842277b492SDouglas Anderson 	 * slave loop are still "running".  There's also no API (that actually
4852277b492SDouglas Anderson 	 * works across all architectures) that can do a stack crawl based
4862277b492SDouglas Anderson 	 * on registers passed as a parameter.
4872277b492SDouglas Anderson 	 *
4882277b492SDouglas Anderson 	 * Solve this conundrum by asking slave CPUs to do the backtrace
4892277b492SDouglas Anderson 	 * themselves.
4902277b492SDouglas Anderson 	 */
4912277b492SDouglas Anderson 	kgdb_info[cpu].exception_state |= DCPU_WANT_BT;
4922277b492SDouglas Anderson 	while (kgdb_info[cpu].exception_state & DCPU_WANT_BT)
4932277b492SDouglas Anderson 		cpu_relax();
4942277b492SDouglas Anderson }
4952277b492SDouglas Anderson #endif
4962277b492SDouglas Anderson 
497c4338209SJason Wessel /*
498c4338209SJason Wessel  * Return true if there is a valid kgdb I/O module.  Also if no
499c4338209SJason Wessel  * debugger is attached a message can be printed to the console about
500c4338209SJason Wessel  * waiting for the debugger to attach.
501c4338209SJason Wessel  *
502c4338209SJason Wessel  * The print_wait argument is only to be true when called from inside
503c4338209SJason Wessel  * the core kgdb_handle_exception, because it will wait for the
504c4338209SJason Wessel  * debugger to attach.
505c4338209SJason Wessel  */
506c4338209SJason Wessel static int kgdb_io_ready(int print_wait)
507c4338209SJason Wessel {
50853197fc4SJason Wessel 	if (!dbg_io_ops)
509c4338209SJason Wessel 		return 0;
510c4338209SJason Wessel 	if (kgdb_connected)
511c4338209SJason Wessel 		return 1;
512c4338209SJason Wessel 	if (atomic_read(&kgdb_setting_breakpoint))
513c4338209SJason Wessel 		return 1;
514dcc78711SJason Wessel 	if (print_wait) {
515dcc78711SJason Wessel #ifdef CONFIG_KGDB_KDB
516dcc78711SJason Wessel 		if (!dbg_kdb_mode)
5170f16996cSFabian Frederick 			pr_crit("waiting... or $3#33 for KDB\n");
518dcc78711SJason Wessel #else
5190f16996cSFabian Frederick 		pr_crit("Waiting for remote debugger\n");
520dcc78711SJason Wessel #endif
521dcc78711SJason Wessel 	}
522c4338209SJason Wessel 	return 1;
523c4338209SJason Wessel }
5244c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_io_ready);
525c4338209SJason Wessel 
526c4338209SJason Wessel static int kgdb_reenter_check(struct kgdb_state *ks)
527c4338209SJason Wessel {
528c4338209SJason Wessel 	unsigned long addr;
529c4338209SJason Wessel 
530c4338209SJason Wessel 	if (atomic_read(&kgdb_active) != raw_smp_processor_id())
531c4338209SJason Wessel 		return 0;
532c4338209SJason Wessel 
533c4338209SJason Wessel 	/* Panic on recursive debugger calls: */
534c4338209SJason Wessel 	exception_level++;
535c4338209SJason Wessel 	addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
536dcc78711SJason Wessel 	dbg_deactivate_sw_breakpoints();
537c4338209SJason Wessel 
538c4338209SJason Wessel 	/*
539c4338209SJason Wessel 	 * If the break point removed ok at the place exception
540c4338209SJason Wessel 	 * occurred, try to recover and print a warning to the end
541c4338209SJason Wessel 	 * user because the user planted a breakpoint in a place that
542c4338209SJason Wessel 	 * KGDB needs in order to function.
543c4338209SJason Wessel 	 */
54453197fc4SJason Wessel 	if (dbg_remove_sw_break(addr) == 0) {
545c4338209SJason Wessel 		exception_level = 0;
546c4338209SJason Wessel 		kgdb_skipexception(ks->ex_vector, ks->linux_regs);
54753197fc4SJason Wessel 		dbg_activate_sw_breakpoints();
5480f16996cSFabian Frederick 		pr_crit("re-enter error: breakpoint removed %lx\n", addr);
549c4338209SJason Wessel 		WARN_ON_ONCE(1);
550c4338209SJason Wessel 
551c4338209SJason Wessel 		return 1;
552c4338209SJason Wessel 	}
55353197fc4SJason Wessel 	dbg_remove_all_break();
554c4338209SJason Wessel 	kgdb_skipexception(ks->ex_vector, ks->linux_regs);
555c4338209SJason Wessel 
556c4338209SJason Wessel 	if (exception_level > 1) {
557c4338209SJason Wessel 		dump_stack();
5583ca676e4SDouglas Anderson 		kgdb_io_module_registered = false;
559c4338209SJason Wessel 		panic("Recursive entry to debugger");
560c4338209SJason Wessel 	}
561c4338209SJason Wessel 
5620f16996cSFabian Frederick 	pr_crit("re-enter exception: ALL breakpoints killed\n");
5636d906340SJason Wessel #ifdef CONFIG_KGDB_KDB
5646d906340SJason Wessel 	/* Allow kdb to debug itself one level */
5656d906340SJason Wessel 	return 0;
5666d906340SJason Wessel #endif
567c4338209SJason Wessel 	dump_stack();
568c4338209SJason Wessel 	panic("Recursive entry to debugger");
569c4338209SJason Wessel 
570c4338209SJason Wessel 	return 1;
571c4338209SJason Wessel }
5724c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_reenter_check);
573c4338209SJason Wessel 
57416cdc628SJason Wessel static void dbg_touch_watchdogs(void)
57516cdc628SJason Wessel {
57616cdc628SJason Wessel 	touch_softlockup_watchdog_sync();
57716cdc628SJason Wessel 	clocksource_touch_watchdog();
578fb70b588SJason Wessel 	rcu_cpu_stall_reset();
57916cdc628SJason Wessel }
5804c4197edSDaniel Thompson NOKPROBE_SYMBOL(dbg_touch_watchdogs);
58116cdc628SJason Wessel 
582dfee3a7bSJason Wessel static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
583dfee3a7bSJason Wessel 		int exception_state)
584c4338209SJason Wessel {
585c4338209SJason Wessel 	unsigned long flags;
586c4338209SJason Wessel 	int sstep_tries = 100;
587dcc78711SJason Wessel 	int error;
588dfee3a7bSJason Wessel 	int cpu;
589c4338209SJason Wessel 	int trace_on = 0;
590dfee3a7bSJason Wessel 	int online_cpus = num_online_cpus();
591a1465d2fSDaniel Thompson 	u64 time_left;
592c1bb9a9cSDongdong Deng 
593dfee3a7bSJason Wessel 	kgdb_info[ks->cpu].enter_kgdb++;
594dfee3a7bSJason Wessel 	kgdb_info[ks->cpu].exception_state |= exception_state;
595dfee3a7bSJason Wessel 
596dfee3a7bSJason Wessel 	if (exception_state == DCPU_WANT_MASTER)
597dfee3a7bSJason Wessel 		atomic_inc(&masters_in_kgdb);
598dfee3a7bSJason Wessel 	else
599dfee3a7bSJason Wessel 		atomic_inc(&slaves_in_kgdb);
600d7ba979dSDongdong Deng 
601d7ba979dSDongdong Deng 	if (arch_kgdb_ops.disable_hw_break)
602d7ba979dSDongdong Deng 		arch_kgdb_ops.disable_hw_break(regs);
603c1bb9a9cSDongdong Deng 
604c4338209SJason Wessel acquirelock:
605440ab9e1SDouglas Anderson 	rcu_read_lock();
606c4338209SJason Wessel 	/*
607c4338209SJason Wessel 	 * Interrupts will be restored by the 'trap return' code, except when
608c4338209SJason Wessel 	 * single stepping.
609c4338209SJason Wessel 	 */
610c4338209SJason Wessel 	local_irq_save(flags);
611c4338209SJason Wessel 
612c4338209SJason Wessel 	cpu = ks->cpu;
613c4338209SJason Wessel 	kgdb_info[cpu].debuggerinfo = regs;
614c4338209SJason Wessel 	kgdb_info[cpu].task = current;
615dcc78711SJason Wessel 	kgdb_info[cpu].ret_state = 0;
616dcc78711SJason Wessel 	kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
617c4338209SJason Wessel 
618dfee3a7bSJason Wessel 	/* Make sure the above info reaches the primary CPU */
619dfee3a7bSJason Wessel 	smp_mb();
620dfee3a7bSJason Wessel 
621dfee3a7bSJason Wessel 	if (exception_level == 1) {
622dfee3a7bSJason Wessel 		if (raw_spin_trylock(&dbg_master_lock))
623dfee3a7bSJason Wessel 			atomic_xchg(&kgdb_active, cpu);
6246d906340SJason Wessel 		goto cpu_master_loop;
625dfee3a7bSJason Wessel 	}
6266d906340SJason Wessel 
627c4338209SJason Wessel 	/*
628c4338209SJason Wessel 	 * CPU will loop if it is a slave or request to become a kgdb
629c4338209SJason Wessel 	 * master cpu and acquire the kgdb_active lock:
630c4338209SJason Wessel 	 */
631c4338209SJason Wessel 	while (1) {
632dcc78711SJason Wessel cpu_loop:
633dcc78711SJason Wessel 		if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) {
634dcc78711SJason Wessel 			kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER;
635dcc78711SJason Wessel 			goto cpu_master_loop;
636dcc78711SJason Wessel 		} else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
637dfee3a7bSJason Wessel 			if (raw_spin_trylock(&dbg_master_lock)) {
638dfee3a7bSJason Wessel 				atomic_xchg(&kgdb_active, cpu);
639c4338209SJason Wessel 				break;
640dfee3a7bSJason Wessel 			}
6412277b492SDouglas Anderson 		} else if (kgdb_info[cpu].exception_state & DCPU_WANT_BT) {
6422277b492SDouglas Anderson 			dump_stack();
6432277b492SDouglas Anderson 			kgdb_info[cpu].exception_state &= ~DCPU_WANT_BT;
644c4338209SJason Wessel 		} else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
645dfee3a7bSJason Wessel 			if (!raw_spin_is_locked(&dbg_slave_lock))
646c4338209SJason Wessel 				goto return_normal;
647c4338209SJason Wessel 		} else {
648c4338209SJason Wessel return_normal:
649c4338209SJason Wessel 			/* Return to normal operation by executing any
650c4338209SJason Wessel 			 * hw breakpoint fixup.
651c4338209SJason Wessel 			 */
652c4338209SJason Wessel 			if (arch_kgdb_ops.correct_hw_break)
653c4338209SJason Wessel 				arch_kgdb_ops.correct_hw_break();
654c4338209SJason Wessel 			if (trace_on)
655c4338209SJason Wessel 				tracing_on();
656162bc7f5SDouglas Anderson 			kgdb_info[cpu].debuggerinfo = NULL;
657162bc7f5SDouglas Anderson 			kgdb_info[cpu].task = NULL;
658dfee3a7bSJason Wessel 			kgdb_info[cpu].exception_state &=
659dfee3a7bSJason Wessel 				~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
660dfee3a7bSJason Wessel 			kgdb_info[cpu].enter_kgdb--;
6614e857c58SPeter Zijlstra 			smp_mb__before_atomic();
662dfee3a7bSJason Wessel 			atomic_dec(&slaves_in_kgdb);
66316cdc628SJason Wessel 			dbg_touch_watchdogs();
664c4338209SJason Wessel 			local_irq_restore(flags);
665440ab9e1SDouglas Anderson 			rcu_read_unlock();
666c4338209SJason Wessel 			return 0;
667c4338209SJason Wessel 		}
668c4338209SJason Wessel 		cpu_relax();
669c4338209SJason Wessel 	}
670c4338209SJason Wessel 
671c4338209SJason Wessel 	/*
672c4338209SJason Wessel 	 * For single stepping, try to only enter on the processor
67325985edcSLucas De Marchi 	 * that was single stepping.  To guard against a deadlock, the
674c4338209SJason Wessel 	 * kernel will only try for the value of sstep_tries before
675c4338209SJason Wessel 	 * giving up and continuing on.
676c4338209SJason Wessel 	 */
677c4338209SJason Wessel 	if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
678c4338209SJason Wessel 	    (kgdb_info[cpu].task &&
679c4338209SJason Wessel 	     kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
680c4338209SJason Wessel 		atomic_set(&kgdb_active, -1);
681dfee3a7bSJason Wessel 		raw_spin_unlock(&dbg_master_lock);
68216cdc628SJason Wessel 		dbg_touch_watchdogs();
683c4338209SJason Wessel 		local_irq_restore(flags);
684440ab9e1SDouglas Anderson 		rcu_read_unlock();
685c4338209SJason Wessel 
686c4338209SJason Wessel 		goto acquirelock;
687c4338209SJason Wessel 	}
688c4338209SJason Wessel 
689c4338209SJason Wessel 	if (!kgdb_io_ready(1)) {
690dcc78711SJason Wessel 		kgdb_info[cpu].ret_state = 1;
69153197fc4SJason Wessel 		goto kgdb_restore; /* No I/O connection, resume the system */
692c4338209SJason Wessel 	}
693c4338209SJason Wessel 
694c4338209SJason Wessel 	/*
695c4338209SJason Wessel 	 * Don't enter if we have hit a removed breakpoint.
696c4338209SJason Wessel 	 */
697c4338209SJason Wessel 	if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
698c4338209SJason Wessel 		goto kgdb_restore;
699c4338209SJason Wessel 
700202164fbSDouglas Anderson 	atomic_inc(&ignore_console_lock_warning);
701202164fbSDouglas Anderson 
702c4338209SJason Wessel 	/* Call the I/O driver's pre_exception routine */
70353197fc4SJason Wessel 	if (dbg_io_ops->pre_exception)
70453197fc4SJason Wessel 		dbg_io_ops->pre_exception();
705c4338209SJason Wessel 
706c4338209SJason Wessel 	/*
707c4338209SJason Wessel 	 * Get the passive CPU lock which will hold all the non-primary
708c4338209SJason Wessel 	 * CPU in a spin state while the debugger is active
709c4338209SJason Wessel 	 */
710dfee3a7bSJason Wessel 	if (!kgdb_single_step)
711dfee3a7bSJason Wessel 		raw_spin_lock(&dbg_slave_lock);
712c4338209SJason Wessel 
713c4338209SJason Wessel #ifdef CONFIG_SMP
7148daaa5f8SMike Travis 	/* If send_ready set, slaves are already waiting */
7158daaa5f8SMike Travis 	if (ks->send_ready)
7168daaa5f8SMike Travis 		atomic_set(ks->send_ready, 1);
7178daaa5f8SMike Travis 
718c4338209SJason Wessel 	/* Signal the other CPUs to enter kgdb_wait() */
7198daaa5f8SMike Travis 	else if ((!kgdb_single_step) && kgdb_do_roundup)
7209ef7fa50SDouglas Anderson 		kgdb_roundup_cpus();
721c4338209SJason Wessel #endif
722c4338209SJason Wessel 
723c4338209SJason Wessel 	/*
724c4338209SJason Wessel 	 * Wait for the other CPUs to be notified and be waiting for us:
725c4338209SJason Wessel 	 */
7262d13bb64SDouglas Anderson 	time_left = MSEC_PER_SEC;
727a1465d2fSDaniel Thompson 	while (kgdb_do_roundup && --time_left &&
728a1465d2fSDaniel Thompson 	       (atomic_read(&masters_in_kgdb) + atomic_read(&slaves_in_kgdb)) !=
729a1465d2fSDaniel Thompson 		   online_cpus)
7302d13bb64SDouglas Anderson 		udelay(1000);
731a1465d2fSDaniel Thompson 	if (!time_left)
732df0036d1SJason Wessel 		pr_crit("Timed out waiting for secondary CPUs.\n");
733c4338209SJason Wessel 
734c4338209SJason Wessel 	/*
735c4338209SJason Wessel 	 * At this point the primary processor is completely
736c4338209SJason Wessel 	 * in the debugger and all secondary CPUs are quiescent
737c4338209SJason Wessel 	 */
738dcc78711SJason Wessel 	dbg_deactivate_sw_breakpoints();
739c4338209SJason Wessel 	kgdb_single_step = 0;
740c4338209SJason Wessel 	kgdb_contthread = current;
741c4338209SJason Wessel 	exception_level = 0;
742c4338209SJason Wessel 	trace_on = tracing_is_on();
743c4338209SJason Wessel 	if (trace_on)
744c4338209SJason Wessel 		tracing_off();
745c4338209SJason Wessel 
746dcc78711SJason Wessel 	while (1) {
747dcc78711SJason Wessel cpu_master_loop:
748dcc78711SJason Wessel 		if (dbg_kdb_mode) {
749dcc78711SJason Wessel 			kgdb_connected = 1;
750dcc78711SJason Wessel 			error = kdb_stub(ks);
7513fa43abaSJason Wessel 			if (error == -1)
7523fa43abaSJason Wessel 				continue;
753b0679c63SJason Wessel 			kgdb_connected = 0;
754dcc78711SJason Wessel 		} else {
755c4338209SJason Wessel 			error = gdb_serial_stub(ks);
756dcc78711SJason Wessel 		}
757dcc78711SJason Wessel 
758dcc78711SJason Wessel 		if (error == DBG_PASS_EVENT) {
759dcc78711SJason Wessel 			dbg_kdb_mode = !dbg_kdb_mode;
760dcc78711SJason Wessel 		} else if (error == DBG_SWITCH_CPU_EVENT) {
761495363d3SJason Wessel 			kgdb_info[dbg_switch_cpu].exception_state |=
762495363d3SJason Wessel 				DCPU_NEXT_MASTER;
763dcc78711SJason Wessel 			goto cpu_loop;
764dcc78711SJason Wessel 		} else {
765dcc78711SJason Wessel 			kgdb_info[cpu].ret_state = error;
766dcc78711SJason Wessel 			break;
767dcc78711SJason Wessel 		}
768dcc78711SJason Wessel 	}
769c4338209SJason Wessel 
770771910f7SDaniel Thompson 	dbg_activate_sw_breakpoints();
771771910f7SDaniel Thompson 
772c4338209SJason Wessel 	/* Call the I/O driver's post_exception routine */
77353197fc4SJason Wessel 	if (dbg_io_ops->post_exception)
77453197fc4SJason Wessel 		dbg_io_ops->post_exception();
775c4338209SJason Wessel 
776202164fbSDouglas Anderson 	atomic_dec(&ignore_console_lock_warning);
777202164fbSDouglas Anderson 
778c4338209SJason Wessel 	if (!kgdb_single_step) {
779dfee3a7bSJason Wessel 		raw_spin_unlock(&dbg_slave_lock);
780dfee3a7bSJason Wessel 		/* Wait till all the CPUs have quit from the debugger. */
781dfee3a7bSJason Wessel 		while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb))
782c4338209SJason Wessel 			cpu_relax();
783c4338209SJason Wessel 	}
784c4338209SJason Wessel 
785c4338209SJason Wessel kgdb_restore:
786c4338209SJason Wessel 	if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
787c4338209SJason Wessel 		int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
788c4338209SJason Wessel 		if (kgdb_info[sstep_cpu].task)
789c4338209SJason Wessel 			kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
790c4338209SJason Wessel 		else
791c4338209SJason Wessel 			kgdb_sstep_pid = 0;
792c4338209SJason Wessel 	}
793c1bb9a9cSDongdong Deng 	if (arch_kgdb_ops.correct_hw_break)
794c1bb9a9cSDongdong Deng 		arch_kgdb_ops.correct_hw_break();
795c4338209SJason Wessel 	if (trace_on)
796c4338209SJason Wessel 		tracing_on();
797dfee3a7bSJason Wessel 
798162bc7f5SDouglas Anderson 	kgdb_info[cpu].debuggerinfo = NULL;
799162bc7f5SDouglas Anderson 	kgdb_info[cpu].task = NULL;
800dfee3a7bSJason Wessel 	kgdb_info[cpu].exception_state &=
801dfee3a7bSJason Wessel 		~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
802dfee3a7bSJason Wessel 	kgdb_info[cpu].enter_kgdb--;
8034e857c58SPeter Zijlstra 	smp_mb__before_atomic();
804dfee3a7bSJason Wessel 	atomic_dec(&masters_in_kgdb);
805c4338209SJason Wessel 	/* Free kgdb_active */
806c4338209SJason Wessel 	atomic_set(&kgdb_active, -1);
807dfee3a7bSJason Wessel 	raw_spin_unlock(&dbg_master_lock);
80816cdc628SJason Wessel 	dbg_touch_watchdogs();
809c4338209SJason Wessel 	local_irq_restore(flags);
810440ab9e1SDouglas Anderson 	rcu_read_unlock();
811c4338209SJason Wessel 
812dcc78711SJason Wessel 	return kgdb_info[cpu].ret_state;
813c4338209SJason Wessel }
8144c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_cpu_enter);
815c4338209SJason Wessel 
816c4338209SJason Wessel /*
817c4338209SJason Wessel  * kgdb_handle_exception() - main entry point from a kernel exception
818c4338209SJason Wessel  *
819c4338209SJason Wessel  * Locking hierarchy:
820c4338209SJason Wessel  *	interface locks, if any (begin_session)
821c4338209SJason Wessel  *	kgdb lock (kgdb_active)
822c4338209SJason Wessel  */
823c4338209SJason Wessel int
824c4338209SJason Wessel kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
825c4338209SJason Wessel {
826c4338209SJason Wessel 	struct kgdb_state kgdb_var;
827c4338209SJason Wessel 	struct kgdb_state *ks = &kgdb_var;
8285a14feadSAnton Vorontsov 	int ret = 0;
8295a14feadSAnton Vorontsov 
8305a14feadSAnton Vorontsov 	if (arch_kgdb_ops.enable_nmi)
8315a14feadSAnton Vorontsov 		arch_kgdb_ops.enable_nmi(0);
8325516fd7bSColin Cross 	/*
8335516fd7bSColin Cross 	 * Avoid entering the debugger if we were triggered due to an oops
8345516fd7bSColin Cross 	 * but panic_timeout indicates the system should automatically
8355516fd7bSColin Cross 	 * reboot on panic. We don't want to get stuck waiting for input
8365516fd7bSColin Cross 	 * on such systems, especially if its "just" an oops.
8375516fd7bSColin Cross 	 */
8385516fd7bSColin Cross 	if (signo != SIGTRAP && panic_timeout)
8395516fd7bSColin Cross 		return 1;
840c4338209SJason Wessel 
8418daaa5f8SMike Travis 	memset(ks, 0, sizeof(struct kgdb_state));
842c4338209SJason Wessel 	ks->cpu			= raw_smp_processor_id();
843c4338209SJason Wessel 	ks->ex_vector		= evector;
844c4338209SJason Wessel 	ks->signo		= signo;
845c4338209SJason Wessel 	ks->err_code		= ecode;
846c4338209SJason Wessel 	ks->linux_regs		= regs;
847c4338209SJason Wessel 
848c4338209SJason Wessel 	if (kgdb_reenter_check(ks))
8495a14feadSAnton Vorontsov 		goto out; /* Ouch, double exception ! */
850dfee3a7bSJason Wessel 	if (kgdb_info[ks->cpu].enter_kgdb != 0)
8515a14feadSAnton Vorontsov 		goto out;
852dfee3a7bSJason Wessel 
8535a14feadSAnton Vorontsov 	ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
8545a14feadSAnton Vorontsov out:
8555a14feadSAnton Vorontsov 	if (arch_kgdb_ops.enable_nmi)
8565a14feadSAnton Vorontsov 		arch_kgdb_ops.enable_nmi(1);
8575a14feadSAnton Vorontsov 	return ret;
858c4338209SJason Wessel }
8594c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_handle_exception);
860c4338209SJason Wessel 
861f30fed10SJason Wessel /*
862d8a050f5SNadav Amit  * GDB places a breakpoint at this function to know dynamically loaded objects.
863f30fed10SJason Wessel  */
864f30fed10SJason Wessel static int module_event(struct notifier_block *self, unsigned long val,
865f30fed10SJason Wessel 	void *data)
866f30fed10SJason Wessel {
867f30fed10SJason Wessel 	return 0;
868f30fed10SJason Wessel }
869f30fed10SJason Wessel 
870f30fed10SJason Wessel static struct notifier_block dbg_module_load_nb = {
871f30fed10SJason Wessel 	.notifier_call	= module_event,
872f30fed10SJason Wessel };
873f30fed10SJason Wessel 
874c4338209SJason Wessel int kgdb_nmicallback(int cpu, void *regs)
875c4338209SJason Wessel {
876c4338209SJason Wessel #ifdef CONFIG_SMP
877c4338209SJason Wessel 	struct kgdb_state kgdb_var;
878c4338209SJason Wessel 	struct kgdb_state *ks = &kgdb_var;
879c4338209SJason Wessel 
88087b09592SDouglas Anderson 	kgdb_info[cpu].rounding_up = false;
88187b09592SDouglas Anderson 
882c4338209SJason Wessel 	memset(ks, 0, sizeof(struct kgdb_state));
883c4338209SJason Wessel 	ks->cpu			= cpu;
884c4338209SJason Wessel 	ks->linux_regs		= regs;
885c4338209SJason Wessel 
886dfee3a7bSJason Wessel 	if (kgdb_info[ks->cpu].enter_kgdb == 0 &&
887dfee3a7bSJason Wessel 			raw_spin_is_locked(&dbg_master_lock)) {
888dfee3a7bSJason Wessel 		kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE);
889c4338209SJason Wessel 		return 0;
890c4338209SJason Wessel 	}
891c4338209SJason Wessel #endif
892c4338209SJason Wessel 	return 1;
893c4338209SJason Wessel }
8944c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_nmicallback);
895c4338209SJason Wessel 
896fc8b1374SMike Travis int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
897fc8b1374SMike Travis 							atomic_t *send_ready)
8988daaa5f8SMike Travis {
8998daaa5f8SMike Travis #ifdef CONFIG_SMP
9008daaa5f8SMike Travis 	if (!kgdb_io_ready(0) || !send_ready)
9018daaa5f8SMike Travis 		return 1;
9028daaa5f8SMike Travis 
9038daaa5f8SMike Travis 	if (kgdb_info[cpu].enter_kgdb == 0) {
9048daaa5f8SMike Travis 		struct kgdb_state kgdb_var;
9058daaa5f8SMike Travis 		struct kgdb_state *ks = &kgdb_var;
9068daaa5f8SMike Travis 
9078daaa5f8SMike Travis 		memset(ks, 0, sizeof(struct kgdb_state));
9088daaa5f8SMike Travis 		ks->cpu			= cpu;
9098daaa5f8SMike Travis 		ks->ex_vector		= trapnr;
9108daaa5f8SMike Travis 		ks->signo		= SIGTRAP;
911fc8b1374SMike Travis 		ks->err_code		= err_code;
9128daaa5f8SMike Travis 		ks->linux_regs		= regs;
9138daaa5f8SMike Travis 		ks->send_ready		= send_ready;
9148daaa5f8SMike Travis 		kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
9158daaa5f8SMike Travis 		return 0;
9168daaa5f8SMike Travis 	}
9178daaa5f8SMike Travis #endif
9188daaa5f8SMike Travis 	return 1;
9198daaa5f8SMike Travis }
9204c4197edSDaniel Thompson NOKPROBE_SYMBOL(kgdb_nmicallin);
9218daaa5f8SMike Travis 
922c4338209SJason Wessel static void kgdb_console_write(struct console *co, const char *s,
923c4338209SJason Wessel    unsigned count)
924c4338209SJason Wessel {
925c4338209SJason Wessel 	unsigned long flags;
926c4338209SJason Wessel 
927c4338209SJason Wessel 	/* If we're debugging, or KGDB has not connected, don't try
928c4338209SJason Wessel 	 * and print. */
929dcc78711SJason Wessel 	if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode)
930c4338209SJason Wessel 		return;
931c4338209SJason Wessel 
932c4338209SJason Wessel 	local_irq_save(flags);
93353197fc4SJason Wessel 	gdbstub_msg_write(s, count);
934c4338209SJason Wessel 	local_irq_restore(flags);
935c4338209SJason Wessel }
936c4338209SJason Wessel 
937c4338209SJason Wessel static struct console kgdbcons = {
938c4338209SJason Wessel 	.name		= "kgdb",
939c4338209SJason Wessel 	.write		= kgdb_console_write,
940c4338209SJason Wessel 	.flags		= CON_PRINTBUFFER | CON_ENABLED,
941c4338209SJason Wessel 	.index		= -1,
942c4338209SJason Wessel };
943c4338209SJason Wessel 
944b18b099eSDouglas Anderson static int __init opt_kgdb_con(char *str)
945b18b099eSDouglas Anderson {
946b18b099eSDouglas Anderson 	kgdb_use_con = 1;
947b18b099eSDouglas Anderson 
948b18b099eSDouglas Anderson 	if (kgdb_io_module_registered && !kgdb_con_registered) {
949b18b099eSDouglas Anderson 		register_console(&kgdbcons);
950b18b099eSDouglas Anderson 		kgdb_con_registered = 1;
951b18b099eSDouglas Anderson 	}
952b18b099eSDouglas Anderson 
953b18b099eSDouglas Anderson 	return 0;
954b18b099eSDouglas Anderson }
955b18b099eSDouglas Anderson 
956b18b099eSDouglas Anderson early_param("kgdbcon", opt_kgdb_con);
957b18b099eSDouglas Anderson 
958c4338209SJason Wessel #ifdef CONFIG_MAGIC_SYSRQ
9591495cc9dSDmitry Torokhov static void sysrq_handle_dbg(int key)
960c4338209SJason Wessel {
96153197fc4SJason Wessel 	if (!dbg_io_ops) {
9620f16996cSFabian Frederick 		pr_crit("ERROR: No KGDB I/O module available\n");
963c4338209SJason Wessel 		return;
964c4338209SJason Wessel 	}
965dcc78711SJason Wessel 	if (!kgdb_connected) {
966dcc78711SJason Wessel #ifdef CONFIG_KGDB_KDB
967dcc78711SJason Wessel 		if (!dbg_kdb_mode)
9680f16996cSFabian Frederick 			pr_crit("KGDB or $3#33 for KDB\n");
969dcc78711SJason Wessel #else
9700f16996cSFabian Frederick 		pr_crit("Entering KGDB\n");
971dcc78711SJason Wessel #endif
972dcc78711SJason Wessel 	}
973c4338209SJason Wessel 
974c4338209SJason Wessel 	kgdb_breakpoint();
975c4338209SJason Wessel }
976c4338209SJason Wessel 
977c69b470eSEmil Velikov static const struct sysrq_key_op sysrq_dbg_op = {
97853197fc4SJason Wessel 	.handler	= sysrq_handle_dbg,
979f3456509Szhangwei(Jovi) 	.help_msg	= "debug(g)",
980c4338209SJason Wessel 	.action_msg	= "DEBUG",
981c4338209SJason Wessel };
982c4338209SJason Wessel #endif
983c4338209SJason Wessel 
9847d92bda2SDouglas Anderson void kgdb_panic(const char *msg)
9854402c153SJason Wessel {
9867d92bda2SDouglas Anderson 	if (!kgdb_io_module_registered)
9877d92bda2SDouglas Anderson 		return;
9887d92bda2SDouglas Anderson 
9895516fd7bSColin Cross 	/*
9907d92bda2SDouglas Anderson 	 * We don't want to get stuck waiting for input from user if
9917d92bda2SDouglas Anderson 	 * "panic_timeout" indicates the system should automatically
9925516fd7bSColin Cross 	 * reboot on panic.
9935516fd7bSColin Cross 	 */
9945516fd7bSColin Cross 	if (panic_timeout)
9957d92bda2SDouglas Anderson 		return;
9965516fd7bSColin Cross 
9974402c153SJason Wessel 	if (dbg_kdb_mode)
9987d92bda2SDouglas Anderson 		kdb_printf("PANIC: %s\n", msg);
9994402c153SJason Wessel 
10007d92bda2SDouglas Anderson 	kgdb_breakpoint();
10017d92bda2SDouglas Anderson }
10024402c153SJason Wessel 
1003b1a57bbfSDouglas Anderson static void kgdb_initial_breakpoint(void)
1004b1a57bbfSDouglas Anderson {
1005b1a57bbfSDouglas Anderson 	kgdb_break_asap = 0;
1006b1a57bbfSDouglas Anderson 
1007b1a57bbfSDouglas Anderson 	pr_crit("Waiting for connection from remote gdb...\n");
1008b1a57bbfSDouglas Anderson 	kgdb_breakpoint();
1009b1a57bbfSDouglas Anderson }
1010b1a57bbfSDouglas Anderson 
10110b4b3827SJason Wessel void __weak kgdb_arch_late(void)
10120b4b3827SJason Wessel {
10130b4b3827SJason Wessel }
10140b4b3827SJason Wessel 
10150b4b3827SJason Wessel void __init dbg_late_init(void)
10160b4b3827SJason Wessel {
10170b4b3827SJason Wessel 	dbg_is_early = false;
10180b4b3827SJason Wessel 	if (kgdb_io_module_registered)
10190b4b3827SJason Wessel 		kgdb_arch_late();
10200b4b3827SJason Wessel 	kdb_init(KDB_INIT_FULL);
1021b1a57bbfSDouglas Anderson 
1022b1a57bbfSDouglas Anderson 	if (kgdb_io_module_registered && kgdb_break_asap)
1023b1a57bbfSDouglas Anderson 		kgdb_initial_breakpoint();
10240b4b3827SJason Wessel }
10250b4b3827SJason Wessel 
10262366e047SJason Wessel static int
10272366e047SJason Wessel dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
10282366e047SJason Wessel {
1029bec4d62eSJason Wessel 	/*
1030bec4d62eSJason Wessel 	 * Take the following action on reboot notify depending on value:
1031bec4d62eSJason Wessel 	 *    1 == Enter debugger
1032220a31b0SZhen Lei 	 *    0 == [the default] detach debug client
1033bec4d62eSJason Wessel 	 *   -1 == Do nothing... and use this until the board resets
1034bec4d62eSJason Wessel 	 */
1035bec4d62eSJason Wessel 	switch (kgdbreboot) {
1036bec4d62eSJason Wessel 	case 1:
1037bec4d62eSJason Wessel 		kgdb_breakpoint();
1038c8daba46SGustavo A. R. Silva 		goto done;
1039bec4d62eSJason Wessel 	case -1:
1040bec4d62eSJason Wessel 		goto done;
1041bec4d62eSJason Wessel 	}
10422366e047SJason Wessel 	if (!dbg_kdb_mode)
10432366e047SJason Wessel 		gdbstub_exit(code);
1044bec4d62eSJason Wessel done:
10452366e047SJason Wessel 	return NOTIFY_DONE;
10462366e047SJason Wessel }
10472366e047SJason Wessel 
10482366e047SJason Wessel static struct notifier_block dbg_reboot_notifier = {
10492366e047SJason Wessel 	.notifier_call		= dbg_notify_reboot,
10502366e047SJason Wessel 	.next			= NULL,
10512366e047SJason Wessel 	.priority		= INT_MAX,
10522366e047SJason Wessel };
10532366e047SJason Wessel 
1054c4338209SJason Wessel static void kgdb_register_callbacks(void)
1055c4338209SJason Wessel {
1056c4338209SJason Wessel 	if (!kgdb_io_module_registered) {
1057c4338209SJason Wessel 		kgdb_io_module_registered = 1;
1058c4338209SJason Wessel 		kgdb_arch_init();
10590b4b3827SJason Wessel 		if (!dbg_is_early)
10600b4b3827SJason Wessel 			kgdb_arch_late();
1061f30fed10SJason Wessel 		register_module_notifier(&dbg_module_load_nb);
10622366e047SJason Wessel 		register_reboot_notifier(&dbg_reboot_notifier);
1063c4338209SJason Wessel #ifdef CONFIG_MAGIC_SYSRQ
106453197fc4SJason Wessel 		register_sysrq_key('g', &sysrq_dbg_op);
1065c4338209SJason Wessel #endif
1066c4338209SJason Wessel 		if (kgdb_use_con && !kgdb_con_registered) {
1067c4338209SJason Wessel 			register_console(&kgdbcons);
1068c4338209SJason Wessel 			kgdb_con_registered = 1;
1069c4338209SJason Wessel 		}
1070c4338209SJason Wessel 	}
1071c4338209SJason Wessel }
1072c4338209SJason Wessel 
1073c4338209SJason Wessel static void kgdb_unregister_callbacks(void)
1074c4338209SJason Wessel {
1075c4338209SJason Wessel 	/*
10767d92bda2SDouglas Anderson 	 * When this routine is called KGDB should unregister from
10777d92bda2SDouglas Anderson 	 * handlers and clean up, making sure it is not handling any
1078c4338209SJason Wessel 	 * break exceptions at the time.
1079c4338209SJason Wessel 	 */
1080c4338209SJason Wessel 	if (kgdb_io_module_registered) {
1081c4338209SJason Wessel 		kgdb_io_module_registered = 0;
10822366e047SJason Wessel 		unregister_reboot_notifier(&dbg_reboot_notifier);
1083f30fed10SJason Wessel 		unregister_module_notifier(&dbg_module_load_nb);
1084c4338209SJason Wessel 		kgdb_arch_exit();
1085c4338209SJason Wessel #ifdef CONFIG_MAGIC_SYSRQ
108653197fc4SJason Wessel 		unregister_sysrq_key('g', &sysrq_dbg_op);
1087c4338209SJason Wessel #endif
1088c4338209SJason Wessel 		if (kgdb_con_registered) {
1089c4338209SJason Wessel 			unregister_console(&kgdbcons);
1090c4338209SJason Wessel 			kgdb_con_registered = 0;
1091c4338209SJason Wessel 		}
1092c4338209SJason Wessel 	}
1093c4338209SJason Wessel }
1094c4338209SJason Wessel 
1095c4338209SJason Wessel /**
1096c4338209SJason Wessel  *	kgdb_register_io_module - register KGDB IO module
109753197fc4SJason Wessel  *	@new_dbg_io_ops: the io ops vector
1098c4338209SJason Wessel  *
1099c4338209SJason Wessel  *	Register it with the KGDB core.
1100c4338209SJason Wessel  */
110153197fc4SJason Wessel int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
1102c4338209SJason Wessel {
110322099562SDouglas Anderson 	struct kgdb_io *old_dbg_io_ops;
1104c4338209SJason Wessel 	int err;
1105c4338209SJason Wessel 
1106c4338209SJason Wessel 	spin_lock(&kgdb_registration_lock);
1107c4338209SJason Wessel 
110822099562SDouglas Anderson 	old_dbg_io_ops = dbg_io_ops;
110922099562SDouglas Anderson 	if (old_dbg_io_ops) {
111022099562SDouglas Anderson 		if (!old_dbg_io_ops->deinit) {
1111c4338209SJason Wessel 			spin_unlock(&kgdb_registration_lock);
1112c4338209SJason Wessel 
111322099562SDouglas Anderson 			pr_err("KGDB I/O driver %s can't replace %s.\n",
111422099562SDouglas Anderson 				new_dbg_io_ops->name, old_dbg_io_ops->name);
1115c4338209SJason Wessel 			return -EBUSY;
1116c4338209SJason Wessel 		}
111722099562SDouglas Anderson 		pr_info("Replacing I/O driver %s with %s\n",
111822099562SDouglas Anderson 			old_dbg_io_ops->name, new_dbg_io_ops->name);
111922099562SDouglas Anderson 	}
1120c4338209SJason Wessel 
112153197fc4SJason Wessel 	if (new_dbg_io_ops->init) {
112253197fc4SJason Wessel 		err = new_dbg_io_ops->init();
1123c4338209SJason Wessel 		if (err) {
1124c4338209SJason Wessel 			spin_unlock(&kgdb_registration_lock);
1125c4338209SJason Wessel 			return err;
1126c4338209SJason Wessel 		}
1127c4338209SJason Wessel 	}
1128c4338209SJason Wessel 
112953197fc4SJason Wessel 	dbg_io_ops = new_dbg_io_ops;
1130c4338209SJason Wessel 
1131c4338209SJason Wessel 	spin_unlock(&kgdb_registration_lock);
1132c4338209SJason Wessel 
1133b1350132SDouglas Anderson 	if (old_dbg_io_ops) {
1134b1350132SDouglas Anderson 		old_dbg_io_ops->deinit();
113522099562SDouglas Anderson 		return 0;
1136b1350132SDouglas Anderson 	}
113722099562SDouglas Anderson 
11380f16996cSFabian Frederick 	pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name);
1139c4338209SJason Wessel 
1140c4338209SJason Wessel 	/* Arm KGDB now. */
1141c4338209SJason Wessel 	kgdb_register_callbacks();
1142c4338209SJason Wessel 
1143b1a57bbfSDouglas Anderson 	if (kgdb_break_asap &&
1144b1a57bbfSDouglas Anderson 	    (!dbg_is_early || IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG)))
1145c4338209SJason Wessel 		kgdb_initial_breakpoint();
1146c4338209SJason Wessel 
1147c4338209SJason Wessel 	return 0;
1148c4338209SJason Wessel }
1149c4338209SJason Wessel EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1150c4338209SJason Wessel 
1151c4338209SJason Wessel /**
11522da2687bSLukas Bulwahn  *	kgdb_unregister_io_module - unregister KGDB IO module
115353197fc4SJason Wessel  *	@old_dbg_io_ops: the io ops vector
1154c4338209SJason Wessel  *
1155c4338209SJason Wessel  *	Unregister it with the KGDB core.
1156c4338209SJason Wessel  */
115753197fc4SJason Wessel void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
1158c4338209SJason Wessel {
1159c4338209SJason Wessel 	BUG_ON(kgdb_connected);
1160c4338209SJason Wessel 
1161c4338209SJason Wessel 	/*
1162c4338209SJason Wessel 	 * KGDB is no longer able to communicate out, so
1163c4338209SJason Wessel 	 * unregister our callbacks and reset state.
1164c4338209SJason Wessel 	 */
1165c4338209SJason Wessel 	kgdb_unregister_callbacks();
1166c4338209SJason Wessel 
1167c4338209SJason Wessel 	spin_lock(&kgdb_registration_lock);
1168c4338209SJason Wessel 
116953197fc4SJason Wessel 	WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
117053197fc4SJason Wessel 	dbg_io_ops = NULL;
1171c4338209SJason Wessel 
1172c4338209SJason Wessel 	spin_unlock(&kgdb_registration_lock);
1173c4338209SJason Wessel 
117422099562SDouglas Anderson 	if (old_dbg_io_ops->deinit)
117522099562SDouglas Anderson 		old_dbg_io_ops->deinit();
117622099562SDouglas Anderson 
11770f16996cSFabian Frederick 	pr_info("Unregistered I/O driver %s, debugger disabled\n",
117853197fc4SJason Wessel 		old_dbg_io_ops->name);
1179c4338209SJason Wessel }
1180c4338209SJason Wessel EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1181c4338209SJason Wessel 
1182dcc78711SJason Wessel int dbg_io_get_char(void)
1183dcc78711SJason Wessel {
1184dcc78711SJason Wessel 	int ret = dbg_io_ops->read_char();
1185f5316b4aSJason Wessel 	if (ret == NO_POLL_CHAR)
1186f5316b4aSJason Wessel 		return -1;
1187dcc78711SJason Wessel 	if (!dbg_kdb_mode)
1188dcc78711SJason Wessel 		return ret;
1189dcc78711SJason Wessel 	if (ret == 127)
1190dcc78711SJason Wessel 		return 8;
1191dcc78711SJason Wessel 	return ret;
1192dcc78711SJason Wessel }
1193dcc78711SJason Wessel 
1194c4338209SJason Wessel /**
1195c4338209SJason Wessel  * kgdb_breakpoint - generate breakpoint exception
1196c4338209SJason Wessel  *
1197c4338209SJason Wessel  * This function will generate a breakpoint exception.  It is used at the
1198c4338209SJason Wessel  * beginning of a program to sync up with a debugger and can be used
1199c4338209SJason Wessel  * otherwise as a quick means to stop program execution and "break" into
1200c4338209SJason Wessel  * the debugger.
1201c4338209SJason Wessel  */
1202d498d4b4SVijaya Kumar K noinline void kgdb_breakpoint(void)
1203c4338209SJason Wessel {
1204c4338209SJason Wessel 	atomic_inc(&kgdb_setting_breakpoint);
1205c4338209SJason Wessel 	wmb(); /* Sync point before breakpoint */
1206c4338209SJason Wessel 	arch_kgdb_breakpoint();
1207c4338209SJason Wessel 	wmb(); /* Sync point after breakpoint */
1208c4338209SJason Wessel 	atomic_dec(&kgdb_setting_breakpoint);
1209c4338209SJason Wessel }
1210c4338209SJason Wessel EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1211c4338209SJason Wessel 
1212c4338209SJason Wessel static int __init opt_kgdb_wait(char *str)
1213c4338209SJason Wessel {
1214c4338209SJason Wessel 	kgdb_break_asap = 1;
1215c4338209SJason Wessel 
1216dcc78711SJason Wessel 	kdb_init(KDB_INIT_EARLY);
1217b1a57bbfSDouglas Anderson 	if (kgdb_io_module_registered &&
1218b1a57bbfSDouglas Anderson 	    IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG))
1219c4338209SJason Wessel 		kgdb_initial_breakpoint();
1220c4338209SJason Wessel 
1221c4338209SJason Wessel 	return 0;
1222c4338209SJason Wessel }
1223c4338209SJason Wessel 
1224c4338209SJason Wessel early_param("kgdbwait", opt_kgdb_wait);
1225