lockdown: also lock down previous kgdb use
[linux-2.6-block.git] / kernel / debug / debug_core.c
CommitLineData
f8416aa2 1// SPDX-License-Identifier: GPL-2.0-only
dc7d5527 2/*
53197fc4 3 * Kernel Debug Core
dc7d5527
JW
4 *
5 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
6 *
7 * Copyright (C) 2000-2001 VERITAS Software Corporation.
8 * Copyright (C) 2002-2004 Timesys Corporation
9 * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
a2531293 10 * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz>
dc7d5527
JW
11 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
12 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
53197fc4 13 * Copyright (C) 2005-2009 Wind River Systems, Inc.
dc7d5527
JW
14 * Copyright (C) 2007 MontaVista Software, Inc.
15 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
16 *
17 * Contributors at various stages not listed above:
18 * Jason Wessel ( jason.wessel@windriver.com )
19 * George Anzinger <george@mvista.com>
20 * Anurekh Saxena (anurekh.saxena@timesys.com)
21 * Lake Stevens Instrument Division (Glenn Engel)
22 * Jim Kingdon, Cygnus Support.
23 *
24 * Original KGDB stub: David Grothe <dave@gcom.com>,
25 * Tigran Aivazian <tigran@sco.com>
dc7d5527 26 */
0f16996c
FF
27
28#define pr_fmt(fmt) "KGDB: " fmt
29
dc7d5527 30#include <linux/pid_namespace.h>
7c3078b6 31#include <linux/clocksource.h>
16559ae4 32#include <linux/serial_core.h>
dc7d5527
JW
33#include <linux/interrupt.h>
34#include <linux/spinlock.h>
35#include <linux/console.h>
36#include <linux/threads.h>
37#include <linux/uaccess.h>
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/ptrace.h>
dc7d5527
JW
41#include <linux/string.h>
42#include <linux/delay.h>
43#include <linux/sched.h>
44#include <linux/sysrq.h>
2366e047 45#include <linux/reboot.h>
dc7d5527
JW
46#include <linux/init.h>
47#include <linux/kgdb.h>
dcc78711 48#include <linux/kdb.h>
38b8d208 49#include <linux/nmi.h>
dc7d5527
JW
50#include <linux/pid.h>
51#include <linux/smp.h>
52#include <linux/mm.h>
615d6e87 53#include <linux/vmacache.h>
fb70b588 54#include <linux/rcupdate.h>
3cd99ac3 55#include <linux/irq.h>
eadb2f47 56#include <linux/security.h>
dc7d5527
JW
57
58#include <asm/cacheflush.h>
59#include <asm/byteorder.h>
60063497 60#include <linux/atomic.h>
dc7d5527 61
53197fc4 62#include "debug_core.h"
dc7d5527 63
53197fc4 64static int kgdb_break_asap;
62fae312 65
53197fc4 66struct debuggerinfo_struct kgdb_info[NR_CPUS];
dc7d5527 67
a1350207 68/* kgdb_connected - Is a host GDB connected to us? */
dc7d5527
JW
69int kgdb_connected;
70EXPORT_SYMBOL_GPL(kgdb_connected);
71
72/* All the KGDB handlers are installed */
f503b5ae 73int kgdb_io_module_registered;
dc7d5527
JW
74
75/* Guard for recursive entry */
76static int exception_level;
77
53197fc4 78struct kgdb_io *dbg_io_ops;
dc7d5527
JW
79static DEFINE_SPINLOCK(kgdb_registration_lock);
80
e16c33e2 81/* Action for the reboot notifier, a global allow kdb to change it */
bec4d62e 82static int kgdbreboot;
dc7d5527
JW
83/* kgdb console driver is loaded */
84static int kgdb_con_registered;
85/* determine if kgdb console output should be used */
86static int kgdb_use_con;
0b4b3827
JW
87/* Flag for alternate operations for early debugging */
88bool dbg_is_early = true;
dcc78711
JW
89/* Next cpu to become the master debug core */
90int dbg_switch_cpu;
91
92/* Use kdb or gdbserver mode */
a0de055c 93int dbg_kdb_mode = 1;
dc7d5527 94
dc7d5527 95module_param(kgdb_use_con, int, 0644);
bec4d62e 96module_param(kgdbreboot, int, 0644);
dc7d5527
JW
97
98/*
99 * Holds information about breakpoints in a kernel. These breakpoints are
100 * added and removed by gdb.
101 */
102static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
103 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
104};
105
106/*
107 * The CPU# of the active CPU, or -1 if none:
108 */
109atomic_t kgdb_active = ATOMIC_INIT(-1);
dcc78711 110EXPORT_SYMBOL_GPL(kgdb_active);
dfee3a7b
JW
111static DEFINE_RAW_SPINLOCK(dbg_master_lock);
112static DEFINE_RAW_SPINLOCK(dbg_slave_lock);
dc7d5527
JW
113
114/*
115 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
116 * bootup code (which might not have percpu set up yet):
117 */
dfee3a7b
JW
118static atomic_t masters_in_kgdb;
119static atomic_t slaves_in_kgdb;
dc7d5527
JW
120atomic_t kgdb_setting_breakpoint;
121
122struct task_struct *kgdb_usethread;
123struct task_struct *kgdb_contthread;
124
125int kgdb_single_step;
53197fc4 126static pid_t kgdb_sstep_pid;
dc7d5527
JW
127
128/* to keep track of the CPU which is doing the single stepping*/
129atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
130
131/*
132 * If you are debugging a problem where roundup (the collection of
133 * all other CPUs) is a problem [this should be extremely rare],
134 * then use the nokgdbroundup option to avoid roundup. In that case
135 * the other CPUs might interfere with your debugging context, so
136 * use this with care:
137 */
688b744d 138static int kgdb_do_roundup = 1;
dc7d5527
JW
139
140static int __init opt_nokgdbroundup(char *str)
141{
142 kgdb_do_roundup = 0;
143
144 return 0;
145}
146
147early_param("nokgdbroundup", opt_nokgdbroundup);
148
149/*
150 * Finally, some KGDB code :-)
151 */
152
153/*
154 * Weak aliases for breakpoint management,
e16c33e2 155 * can be overridden by architectures when needed:
dc7d5527 156 */
98b54aa1 157int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
dc7d5527
JW
158{
159 int err;
160
fe557319 161 err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
98b54aa1 162 BREAK_INSTR_SIZE);
dc7d5527
JW
163 if (err)
164 return err;
fe557319 165 err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
98b54aa1
JW
166 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
167 return err;
dc7d5527 168}
4c4197ed 169NOKPROBE_SYMBOL(kgdb_arch_set_breakpoint);
dc7d5527 170
98b54aa1 171int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
dc7d5527 172{
fe557319 173 return copy_to_kernel_nofault((char *)bpt->bpt_addr,
98b54aa1 174 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
dc7d5527 175}
4c4197ed 176NOKPROBE_SYMBOL(kgdb_arch_remove_breakpoint);
dc7d5527 177
a9b60bf4
JW
178int __weak kgdb_validate_break_address(unsigned long addr)
179{
98b54aa1 180 struct kgdb_bkpt tmp;
a9b60bf4 181 int err;
f2d10ff4
DT
182
183 if (kgdb_within_blocklist(addr))
184 return -EINVAL;
185
98b54aa1 186 /* Validate setting the breakpoint and then removing it. If the
a9b60bf4
JW
187 * remove fails, the kernel needs to emit a bad message because we
188 * are deep trouble not being able to put things back the way we
189 * found them.
190 */
98b54aa1
JW
191 tmp.bpt_addr = addr;
192 err = kgdb_arch_set_breakpoint(&tmp);
a9b60bf4
JW
193 if (err)
194 return err;
98b54aa1 195 err = kgdb_arch_remove_breakpoint(&tmp);
a9b60bf4 196 if (err)
0f16996c
FF
197 pr_err("Critical breakpoint error, kernel memory destroyed at: %lx\n",
198 addr);
a9b60bf4
JW
199 return err;
200}
201
dc7d5527
JW
202unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
203{
204 return instruction_pointer(regs);
205}
4c4197ed 206NOKPROBE_SYMBOL(kgdb_arch_pc);
dc7d5527
JW
207
208int __weak kgdb_arch_init(void)
209{
210 return 0;
211}
212
b4b8ac52
JW
213int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
214{
215 return 0;
216}
4c4197ed 217NOKPROBE_SYMBOL(kgdb_skipexception);
b4b8ac52 218
3cd99ac3
DA
219#ifdef CONFIG_SMP
220
221/*
222 * Default (weak) implementation for kgdb_roundup_cpus
223 */
224
3cd99ac3
DA
225void __weak kgdb_call_nmi_hook(void *ignored)
226{
227 /*
228 * NOTE: get_irq_regs() is supposed to get the registers from
229 * before the IPI interrupt happened and so is supposed to
230 * show where the processor was. In some situations it's
231 * possible we might be called without an IPI, so it might be
232 * safer to figure out how to make kgdb_breakpoint() work
233 * properly here.
234 */
235 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
236}
4c4197ed 237NOKPROBE_SYMBOL(kgdb_call_nmi_hook);
3cd99ac3 238
545b8c8d
PZ
239static DEFINE_PER_CPU(call_single_data_t, kgdb_roundup_csd) =
240 CSD_INIT(kgdb_call_nmi_hook, NULL);
241
3cd99ac3
DA
242void __weak kgdb_roundup_cpus(void)
243{
244 call_single_data_t *csd;
245 int this_cpu = raw_smp_processor_id();
246 int cpu;
87b09592 247 int ret;
3cd99ac3
DA
248
249 for_each_online_cpu(cpu) {
250 /* No need to roundup ourselves */
251 if (cpu == this_cpu)
252 continue;
253
254 csd = &per_cpu(kgdb_roundup_csd, cpu);
87b09592
DA
255
256 /*
257 * If it didn't round up last time, don't try again
258 * since smp_call_function_single_async() will block.
259 *
260 * If rounding_up is false then we know that the
261 * previous call must have at least started and that
262 * means smp_call_function_single_async() won't block.
263 */
264 if (kgdb_info[cpu].rounding_up)
265 continue;
266 kgdb_info[cpu].rounding_up = true;
267
87b09592
DA
268 ret = smp_call_function_single_async(cpu, csd);
269 if (ret)
270 kgdb_info[cpu].rounding_up = false;
3cd99ac3
DA
271 }
272}
4c4197ed 273NOKPROBE_SYMBOL(kgdb_roundup_cpus);
3cd99ac3
DA
274
275#endif
276
dc7d5527
JW
277/*
278 * Some architectures need cache flushes when we set/clear a
279 * breakpoint:
280 */
281static void kgdb_flush_swbreak_addr(unsigned long addr)
282{
283 if (!CACHE_FLUSH_IS_SAFE)
284 return;
285
615d6e87
DB
286 if (current->mm) {
287 int i;
288
289 for (i = 0; i < VMACACHE_SIZE; i++) {
314ff785 290 if (!current->vmacache.vmas[i])
615d6e87 291 continue;
314ff785 292 flush_cache_range(current->vmacache.vmas[i],
615d6e87
DB
293 addr, addr + BREAK_INSTR_SIZE);
294 }
dc7d5527 295 }
615d6e87 296
1a9a3e76
JW
297 /* Force flush instruction cache if it was outside the mm */
298 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
dc7d5527 299}
4c4197ed 300NOKPROBE_SYMBOL(kgdb_flush_swbreak_addr);
dc7d5527
JW
301
302/*
303 * SW breakpoint management:
304 */
53197fc4 305int dbg_activate_sw_breakpoints(void)
dc7d5527 306{
7f8b7ed6
JW
307 int error;
308 int ret = 0;
dc7d5527
JW
309 int i;
310
311 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
312 if (kgdb_break[i].state != BP_SET)
313 continue;
314
98b54aa1 315 error = kgdb_arch_set_breakpoint(&kgdb_break[i]);
7f8b7ed6
JW
316 if (error) {
317 ret = error;
0f16996c
FF
318 pr_info("BP install failed: %lx\n",
319 kgdb_break[i].bpt_addr);
7f8b7ed6
JW
320 continue;
321 }
dc7d5527 322
98b54aa1 323 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
dc7d5527
JW
324 kgdb_break[i].state = BP_ACTIVE;
325 }
7f8b7ed6 326 return ret;
dc7d5527 327}
4c4197ed 328NOKPROBE_SYMBOL(dbg_activate_sw_breakpoints);
dc7d5527 329
53197fc4 330int dbg_set_sw_break(unsigned long addr)
dc7d5527
JW
331{
332 int err = kgdb_validate_break_address(addr);
333 int breakno = -1;
334 int i;
335
336 if (err)
337 return err;
338
339 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
340 if ((kgdb_break[i].state == BP_SET) &&
341 (kgdb_break[i].bpt_addr == addr))
342 return -EEXIST;
343 }
344 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
345 if (kgdb_break[i].state == BP_REMOVED &&
346 kgdb_break[i].bpt_addr == addr) {
347 breakno = i;
348 break;
349 }
350 }
351
352 if (breakno == -1) {
353 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
354 if (kgdb_break[i].state == BP_UNDEFINED) {
355 breakno = i;
356 break;
357 }
358 }
359 }
360
361 if (breakno == -1)
362 return -E2BIG;
363
364 kgdb_break[breakno].state = BP_SET;
365 kgdb_break[breakno].type = BP_BREAKPOINT;
366 kgdb_break[breakno].bpt_addr = addr;
367
368 return 0;
369}
370
dcc78711 371int dbg_deactivate_sw_breakpoints(void)
dc7d5527 372{
7f8b7ed6
JW
373 int error;
374 int ret = 0;
dc7d5527
JW
375 int i;
376
377 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
378 if (kgdb_break[i].state != BP_ACTIVE)
379 continue;
98b54aa1 380 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
7f8b7ed6 381 if (error) {
0f16996c
FF
382 pr_info("BP remove failed: %lx\n",
383 kgdb_break[i].bpt_addr);
7f8b7ed6
JW
384 ret = error;
385 }
dc7d5527 386
98b54aa1 387 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
dc7d5527
JW
388 kgdb_break[i].state = BP_SET;
389 }
7f8b7ed6 390 return ret;
dc7d5527 391}
4c4197ed 392NOKPROBE_SYMBOL(dbg_deactivate_sw_breakpoints);
dc7d5527 393
53197fc4 394int dbg_remove_sw_break(unsigned long addr)
dc7d5527
JW
395{
396 int i;
397
398 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
399 if ((kgdb_break[i].state == BP_SET) &&
400 (kgdb_break[i].bpt_addr == addr)) {
401 kgdb_break[i].state = BP_REMOVED;
402 return 0;
403 }
404 }
405 return -ENOENT;
406}
407
408int kgdb_isremovedbreak(unsigned long addr)
409{
410 int i;
411
412 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
413 if ((kgdb_break[i].state == BP_REMOVED) &&
414 (kgdb_break[i].bpt_addr == addr))
415 return 1;
416 }
417 return 0;
418}
419
f83b04d3
VC
420int kgdb_has_hit_break(unsigned long addr)
421{
422 int i;
423
424 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
425 if (kgdb_break[i].state == BP_ACTIVE &&
426 kgdb_break[i].bpt_addr == addr)
427 return 1;
428 }
429 return 0;
430}
431
53197fc4 432int dbg_remove_all_break(void)
dc7d5527 433{
dc7d5527
JW
434 int error;
435 int i;
436
437 /* Clear memory breakpoints. */
438 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
737a460f
JW
439 if (kgdb_break[i].state != BP_ACTIVE)
440 goto setundefined;
98b54aa1 441 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
dc7d5527 442 if (error)
0f16996c 443 pr_err("breakpoint remove failed: %lx\n",
98b54aa1 444 kgdb_break[i].bpt_addr);
737a460f
JW
445setundefined:
446 kgdb_break[i].state = BP_UNDEFINED;
dc7d5527
JW
447 }
448
449 /* Clear hardware breakpoints. */
450 if (arch_kgdb_ops.remove_all_hw_break)
451 arch_kgdb_ops.remove_all_hw_break();
452
453 return 0;
454}
455
d54ce615
SG
456void kgdb_free_init_mem(void)
457{
458 int i;
459
460 /* Clear init memory breakpoints. */
461 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
462 if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))
463 kgdb_break[i].state = BP_UNDEFINED;
464 }
465}
466
2277b492
DA
467#ifdef CONFIG_KGDB_KDB
468void kdb_dump_stack_on_cpu(int cpu)
469{
d07ce4e3 470 if (cpu == raw_smp_processor_id() || !IS_ENABLED(CONFIG_SMP)) {
2277b492
DA
471 dump_stack();
472 return;
473 }
474
475 if (!(kgdb_info[cpu].exception_state & DCPU_IS_SLAVE)) {
476 kdb_printf("ERROR: Task on cpu %d didn't stop in the debugger\n",
477 cpu);
478 return;
479 }
480
481 /*
482 * In general, architectures don't support dumping the stack of a
483 * "running" process that's not the current one. From the point of
484 * view of the Linux, kernel processes that are looping in the kgdb
485 * slave loop are still "running". There's also no API (that actually
486 * works across all architectures) that can do a stack crawl based
487 * on registers passed as a parameter.
488 *
489 * Solve this conundrum by asking slave CPUs to do the backtrace
490 * themselves.
491 */
492 kgdb_info[cpu].exception_state |= DCPU_WANT_BT;
493 while (kgdb_info[cpu].exception_state & DCPU_WANT_BT)
494 cpu_relax();
495}
496#endif
497
dc7d5527
JW
498/*
499 * Return true if there is a valid kgdb I/O module. Also if no
500 * debugger is attached a message can be printed to the console about
501 * waiting for the debugger to attach.
502 *
503 * The print_wait argument is only to be true when called from inside
504 * the core kgdb_handle_exception, because it will wait for the
505 * debugger to attach.
506 */
507static int kgdb_io_ready(int print_wait)
508{
53197fc4 509 if (!dbg_io_ops)
dc7d5527
JW
510 return 0;
511 if (kgdb_connected)
512 return 1;
513 if (atomic_read(&kgdb_setting_breakpoint))
514 return 1;
dcc78711
JW
515 if (print_wait) {
516#ifdef CONFIG_KGDB_KDB
517 if (!dbg_kdb_mode)
0f16996c 518 pr_crit("waiting... or $3#33 for KDB\n");
dcc78711 519#else
0f16996c 520 pr_crit("Waiting for remote debugger\n");
dcc78711
JW
521#endif
522 }
dc7d5527
JW
523 return 1;
524}
4c4197ed 525NOKPROBE_SYMBOL(kgdb_io_ready);
dc7d5527 526
dc7d5527
JW
527static int kgdb_reenter_check(struct kgdb_state *ks)
528{
529 unsigned long addr;
530
531 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
532 return 0;
533
534 /* Panic on recursive debugger calls: */
535 exception_level++;
536 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
dcc78711 537 dbg_deactivate_sw_breakpoints();
dc7d5527
JW
538
539 /*
540 * If the break point removed ok at the place exception
541 * occurred, try to recover and print a warning to the end
542 * user because the user planted a breakpoint in a place that
543 * KGDB needs in order to function.
544 */
53197fc4 545 if (dbg_remove_sw_break(addr) == 0) {
dc7d5527
JW
546 exception_level = 0;
547 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
53197fc4 548 dbg_activate_sw_breakpoints();
0f16996c 549 pr_crit("re-enter error: breakpoint removed %lx\n", addr);
dc7d5527
JW
550 WARN_ON_ONCE(1);
551
552 return 1;
553 }
53197fc4 554 dbg_remove_all_break();
dc7d5527
JW
555 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
556
557 if (exception_level > 1) {
558 dump_stack();
3ca676e4 559 kgdb_io_module_registered = false;
dc7d5527
JW
560 panic("Recursive entry to debugger");
561 }
562
0f16996c 563 pr_crit("re-enter exception: ALL breakpoints killed\n");
6d906340
JW
564#ifdef CONFIG_KGDB_KDB
565 /* Allow kdb to debug itself one level */
566 return 0;
567#endif
dc7d5527
JW
568 dump_stack();
569 panic("Recursive entry to debugger");
570
571 return 1;
572}
4c4197ed 573NOKPROBE_SYMBOL(kgdb_reenter_check);
dc7d5527 574
16cdc628
JW
575static void dbg_touch_watchdogs(void)
576{
577 touch_softlockup_watchdog_sync();
578 clocksource_touch_watchdog();
fb70b588 579 rcu_cpu_stall_reset();
16cdc628 580}
4c4197ed 581NOKPROBE_SYMBOL(dbg_touch_watchdogs);
16cdc628 582
dfee3a7b
JW
583static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
584 int exception_state)
dc7d5527 585{
dc7d5527 586 unsigned long flags;
028e7b17 587 int sstep_tries = 100;
dcc78711 588 int error;
dfee3a7b 589 int cpu;
4da75b9c 590 int trace_on = 0;
dfee3a7b 591 int online_cpus = num_online_cpus();
a1465d2f 592 u64 time_left;
c1bb9a9c 593
dfee3a7b
JW
594 kgdb_info[ks->cpu].enter_kgdb++;
595 kgdb_info[ks->cpu].exception_state |= exception_state;
596
597 if (exception_state == DCPU_WANT_MASTER)
598 atomic_inc(&masters_in_kgdb);
599 else
600 atomic_inc(&slaves_in_kgdb);
d7ba979d
DD
601
602 if (arch_kgdb_ops.disable_hw_break)
603 arch_kgdb_ops.disable_hw_break(regs);
c1bb9a9c 604
dc7d5527 605acquirelock:
440ab9e1 606 rcu_read_lock();
dc7d5527
JW
607 /*
608 * Interrupts will be restored by the 'trap return' code, except when
609 * single stepping.
610 */
611 local_irq_save(flags);
612
62fae312
JW
613 cpu = ks->cpu;
614 kgdb_info[cpu].debuggerinfo = regs;
615 kgdb_info[cpu].task = current;
dcc78711
JW
616 kgdb_info[cpu].ret_state = 0;
617 kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
dc7d5527 618
dfee3a7b
JW
619 /* Make sure the above info reaches the primary CPU */
620 smp_mb();
621
622 if (exception_level == 1) {
623 if (raw_spin_trylock(&dbg_master_lock))
624 atomic_xchg(&kgdb_active, cpu);
6d906340 625 goto cpu_master_loop;
dfee3a7b 626 }
6d906340 627
dc7d5527 628 /*
62fae312
JW
629 * CPU will loop if it is a slave or request to become a kgdb
630 * master cpu and acquire the kgdb_active lock:
dc7d5527 631 */
62fae312 632 while (1) {
dcc78711
JW
633cpu_loop:
634 if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) {
635 kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER;
636 goto cpu_master_loop;
637 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
dfee3a7b
JW
638 if (raw_spin_trylock(&dbg_master_lock)) {
639 atomic_xchg(&kgdb_active, cpu);
62fae312 640 break;
dfee3a7b 641 }
2277b492
DA
642 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_BT) {
643 dump_stack();
644 kgdb_info[cpu].exception_state &= ~DCPU_WANT_BT;
62fae312 645 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
dfee3a7b 646 if (!raw_spin_is_locked(&dbg_slave_lock))
62fae312
JW
647 goto return_normal;
648 } else {
649return_normal:
650 /* Return to normal operation by executing any
651 * hw breakpoint fixup.
652 */
653 if (arch_kgdb_ops.correct_hw_break)
654 arch_kgdb_ops.correct_hw_break();
4da75b9c
JW
655 if (trace_on)
656 tracing_on();
162bc7f5
DA
657 kgdb_info[cpu].debuggerinfo = NULL;
658 kgdb_info[cpu].task = NULL;
dfee3a7b
JW
659 kgdb_info[cpu].exception_state &=
660 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
661 kgdb_info[cpu].enter_kgdb--;
4e857c58 662 smp_mb__before_atomic();
dfee3a7b 663 atomic_dec(&slaves_in_kgdb);
16cdc628 664 dbg_touch_watchdogs();
62fae312 665 local_irq_restore(flags);
440ab9e1 666 rcu_read_unlock();
62fae312
JW
667 return 0;
668 }
dc7d5527 669 cpu_relax();
62fae312 670 }
dc7d5527
JW
671
672 /*
028e7b17 673 * For single stepping, try to only enter on the processor
25985edc 674 * that was single stepping. To guard against a deadlock, the
028e7b17
JW
675 * kernel will only try for the value of sstep_tries before
676 * giving up and continuing on.
dc7d5527
JW
677 */
678 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
028e7b17
JW
679 (kgdb_info[cpu].task &&
680 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
dc7d5527 681 atomic_set(&kgdb_active, -1);
dfee3a7b 682 raw_spin_unlock(&dbg_master_lock);
16cdc628 683 dbg_touch_watchdogs();
dc7d5527 684 local_irq_restore(flags);
440ab9e1 685 rcu_read_unlock();
dc7d5527
JW
686
687 goto acquirelock;
688 }
689
690 if (!kgdb_io_ready(1)) {
dcc78711 691 kgdb_info[cpu].ret_state = 1;
53197fc4 692 goto kgdb_restore; /* No I/O connection, resume the system */
dc7d5527
JW
693 }
694
695 /*
696 * Don't enter if we have hit a removed breakpoint.
697 */
698 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
699 goto kgdb_restore;
700
202164fb
DA
701 atomic_inc(&ignore_console_lock_warning);
702
dc7d5527 703 /* Call the I/O driver's pre_exception routine */
53197fc4
JW
704 if (dbg_io_ops->pre_exception)
705 dbg_io_ops->pre_exception();
dc7d5527 706
dc7d5527
JW
707 /*
708 * Get the passive CPU lock which will hold all the non-primary
709 * CPU in a spin state while the debugger is active
710 */
dfee3a7b
JW
711 if (!kgdb_single_step)
712 raw_spin_lock(&dbg_slave_lock);
dc7d5527 713
56fb7093 714#ifdef CONFIG_SMP
8daaa5f8
MT
715 /* If send_ready set, slaves are already waiting */
716 if (ks->send_ready)
717 atomic_set(ks->send_ready, 1);
718
56fb7093 719 /* Signal the other CPUs to enter kgdb_wait() */
8daaa5f8 720 else if ((!kgdb_single_step) && kgdb_do_roundup)
9ef7fa50 721 kgdb_roundup_cpus();
56fb7093
JW
722#endif
723
dc7d5527
JW
724 /*
725 * Wait for the other CPUs to be notified and be waiting for us:
726 */
2d13bb64 727 time_left = MSEC_PER_SEC;
a1465d2f
DT
728 while (kgdb_do_roundup && --time_left &&
729 (atomic_read(&masters_in_kgdb) + atomic_read(&slaves_in_kgdb)) !=
730 online_cpus)
2d13bb64 731 udelay(1000);
a1465d2f 732 if (!time_left)
df0036d1 733 pr_crit("Timed out waiting for secondary CPUs.\n");
dc7d5527
JW
734
735 /*
736 * At this point the primary processor is completely
737 * in the debugger and all secondary CPUs are quiescent
738 */
dcc78711 739 dbg_deactivate_sw_breakpoints();
dc7d5527 740 kgdb_single_step = 0;
d7161a65 741 kgdb_contthread = current;
dc7d5527 742 exception_level = 0;
4da75b9c
JW
743 trace_on = tracing_is_on();
744 if (trace_on)
745 tracing_off();
dc7d5527 746
dcc78711
JW
747 while (1) {
748cpu_master_loop:
749 if (dbg_kdb_mode) {
750 kgdb_connected = 1;
751 error = kdb_stub(ks);
3fa43aba
JW
752 if (error == -1)
753 continue;
b0679c63 754 kgdb_connected = 0;
dcc78711 755 } else {
eadb2f47
DT
756 /*
757 * This is a brutal way to interfere with the debugger
758 * and prevent gdb being used to poke at kernel memory.
759 * This could cause trouble if lockdown is applied when
760 * there is already an active gdb session. For now the
761 * answer is simply "don't do that". Typically lockdown
762 * *will* be applied before the debug core gets started
763 * so only developers using kgdb for fairly advanced
764 * early kernel debug can be biten by this. Hopefully
765 * they are sophisticated enough to take care of
766 * themselves, especially with help from the lockdown
767 * message printed on the console!
768 */
769 if (security_locked_down(LOCKDOWN_DBG_WRITE_KERNEL)) {
770 if (IS_ENABLED(CONFIG_KGDB_KDB)) {
771 /* Switch back to kdb if possible... */
772 dbg_kdb_mode = 1;
773 continue;
774 } else {
775 /* ... otherwise just bail */
776 break;
777 }
778 }
dcc78711
JW
779 error = gdb_serial_stub(ks);
780 }
781
782 if (error == DBG_PASS_EVENT) {
783 dbg_kdb_mode = !dbg_kdb_mode;
dcc78711 784 } else if (error == DBG_SWITCH_CPU_EVENT) {
495363d3
JW
785 kgdb_info[dbg_switch_cpu].exception_state |=
786 DCPU_NEXT_MASTER;
dcc78711
JW
787 goto cpu_loop;
788 } else {
789 kgdb_info[cpu].ret_state = error;
790 break;
791 }
792 }
dc7d5527 793
771910f7
DT
794 dbg_activate_sw_breakpoints();
795
dc7d5527 796 /* Call the I/O driver's post_exception routine */
53197fc4
JW
797 if (dbg_io_ops->post_exception)
798 dbg_io_ops->post_exception();
dc7d5527 799
202164fb
DA
800 atomic_dec(&ignore_console_lock_warning);
801
d7161a65 802 if (!kgdb_single_step) {
dfee3a7b
JW
803 raw_spin_unlock(&dbg_slave_lock);
804 /* Wait till all the CPUs have quit from the debugger. */
805 while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb))
806 cpu_relax();
dc7d5527
JW
807 }
808
809kgdb_restore:
028e7b17
JW
810 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
811 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
812 if (kgdb_info[sstep_cpu].task)
813 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
814 else
815 kgdb_sstep_pid = 0;
816 }
c1bb9a9c
DD
817 if (arch_kgdb_ops.correct_hw_break)
818 arch_kgdb_ops.correct_hw_break();
4da75b9c
JW
819 if (trace_on)
820 tracing_on();
dfee3a7b 821
162bc7f5
DA
822 kgdb_info[cpu].debuggerinfo = NULL;
823 kgdb_info[cpu].task = NULL;
dfee3a7b
JW
824 kgdb_info[cpu].exception_state &=
825 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
826 kgdb_info[cpu].enter_kgdb--;
4e857c58 827 smp_mb__before_atomic();
dfee3a7b 828 atomic_dec(&masters_in_kgdb);
dc7d5527
JW
829 /* Free kgdb_active */
830 atomic_set(&kgdb_active, -1);
dfee3a7b 831 raw_spin_unlock(&dbg_master_lock);
16cdc628 832 dbg_touch_watchdogs();
dc7d5527 833 local_irq_restore(flags);
440ab9e1 834 rcu_read_unlock();
dc7d5527 835
dcc78711 836 return kgdb_info[cpu].ret_state;
dc7d5527 837}
4c4197ed 838NOKPROBE_SYMBOL(kgdb_cpu_enter);
dc7d5527 839
62fae312
JW
840/*
841 * kgdb_handle_exception() - main entry point from a kernel exception
842 *
843 * Locking hierarchy:
844 * interface locks, if any (begin_session)
845 * kgdb lock (kgdb_active)
846 */
847int
848kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
849{
850 struct kgdb_state kgdb_var;
851 struct kgdb_state *ks = &kgdb_var;
5a14fead
AV
852 int ret = 0;
853
854 if (arch_kgdb_ops.enable_nmi)
855 arch_kgdb_ops.enable_nmi(0);
5516fd7b
CC
856 /*
857 * Avoid entering the debugger if we were triggered due to an oops
858 * but panic_timeout indicates the system should automatically
859 * reboot on panic. We don't want to get stuck waiting for input
860 * on such systems, especially if its "just" an oops.
861 */
862 if (signo != SIGTRAP && panic_timeout)
863 return 1;
62fae312 864
8daaa5f8 865 memset(ks, 0, sizeof(struct kgdb_state));
62fae312
JW
866 ks->cpu = raw_smp_processor_id();
867 ks->ex_vector = evector;
868 ks->signo = signo;
62fae312 869 ks->err_code = ecode;
62fae312
JW
870 ks->linux_regs = regs;
871
872 if (kgdb_reenter_check(ks))
5a14fead 873 goto out; /* Ouch, double exception ! */
dfee3a7b 874 if (kgdb_info[ks->cpu].enter_kgdb != 0)
5a14fead 875 goto out;
dfee3a7b 876
5a14fead
AV
877 ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
878out:
879 if (arch_kgdb_ops.enable_nmi)
880 arch_kgdb_ops.enable_nmi(1);
881 return ret;
62fae312 882}
4c4197ed 883NOKPROBE_SYMBOL(kgdb_handle_exception);
62fae312 884
f30fed10 885/*
d8a050f5 886 * GDB places a breakpoint at this function to know dynamically loaded objects.
f30fed10 887 */
f30fed10
JW
888static int module_event(struct notifier_block *self, unsigned long val,
889 void *data)
890{
891 return 0;
892}
893
894static struct notifier_block dbg_module_load_nb = {
895 .notifier_call = module_event,
896};
897
dc7d5527
JW
898int kgdb_nmicallback(int cpu, void *regs)
899{
900#ifdef CONFIG_SMP
62fae312
JW
901 struct kgdb_state kgdb_var;
902 struct kgdb_state *ks = &kgdb_var;
903
87b09592
DA
904 kgdb_info[cpu].rounding_up = false;
905
62fae312
JW
906 memset(ks, 0, sizeof(struct kgdb_state));
907 ks->cpu = cpu;
908 ks->linux_regs = regs;
909
dfee3a7b
JW
910 if (kgdb_info[ks->cpu].enter_kgdb == 0 &&
911 raw_spin_is_locked(&dbg_master_lock)) {
912 kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE);
dc7d5527
JW
913 return 0;
914 }
915#endif
916 return 1;
917}
4c4197ed 918NOKPROBE_SYMBOL(kgdb_nmicallback);
dc7d5527 919
fc8b1374
MT
920int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
921 atomic_t *send_ready)
8daaa5f8
MT
922{
923#ifdef CONFIG_SMP
924 if (!kgdb_io_ready(0) || !send_ready)
925 return 1;
926
927 if (kgdb_info[cpu].enter_kgdb == 0) {
928 struct kgdb_state kgdb_var;
929 struct kgdb_state *ks = &kgdb_var;
930
931 memset(ks, 0, sizeof(struct kgdb_state));
932 ks->cpu = cpu;
933 ks->ex_vector = trapnr;
934 ks->signo = SIGTRAP;
fc8b1374 935 ks->err_code = err_code;
8daaa5f8
MT
936 ks->linux_regs = regs;
937 ks->send_ready = send_ready;
938 kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
939 return 0;
940 }
941#endif
942 return 1;
943}
4c4197ed 944NOKPROBE_SYMBOL(kgdb_nmicallin);
8daaa5f8 945
aabdc3b8
JW
946static void kgdb_console_write(struct console *co, const char *s,
947 unsigned count)
dc7d5527
JW
948{
949 unsigned long flags;
950
951 /* If we're debugging, or KGDB has not connected, don't try
952 * and print. */
dcc78711 953 if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode)
dc7d5527
JW
954 return;
955
956 local_irq_save(flags);
53197fc4 957 gdbstub_msg_write(s, count);
dc7d5527
JW
958 local_irq_restore(flags);
959}
960
961static struct console kgdbcons = {
962 .name = "kgdb",
963 .write = kgdb_console_write,
964 .flags = CON_PRINTBUFFER | CON_ENABLED,
965 .index = -1,
966};
967
b18b099e
DA
968static int __init opt_kgdb_con(char *str)
969{
970 kgdb_use_con = 1;
971
972 if (kgdb_io_module_registered && !kgdb_con_registered) {
973 register_console(&kgdbcons);
974 kgdb_con_registered = 1;
975 }
976
977 return 0;
978}
979
980early_param("kgdbcon", opt_kgdb_con);
981
dc7d5527 982#ifdef CONFIG_MAGIC_SYSRQ
1495cc9d 983static void sysrq_handle_dbg(int key)
dc7d5527 984{
53197fc4 985 if (!dbg_io_ops) {
0f16996c 986 pr_crit("ERROR: No KGDB I/O module available\n");
dc7d5527
JW
987 return;
988 }
dcc78711
JW
989 if (!kgdb_connected) {
990#ifdef CONFIG_KGDB_KDB
991 if (!dbg_kdb_mode)
0f16996c 992 pr_crit("KGDB or $3#33 for KDB\n");
dcc78711 993#else
0f16996c 994 pr_crit("Entering KGDB\n");
dcc78711
JW
995#endif
996 }
dc7d5527
JW
997
998 kgdb_breakpoint();
999}
1000
c69b470e 1001static const struct sysrq_key_op sysrq_dbg_op = {
53197fc4 1002 .handler = sysrq_handle_dbg,
f3456509 1003 .help_msg = "debug(g)",
364b5b7b 1004 .action_msg = "DEBUG",
dc7d5527
JW
1005};
1006#endif
1007
7d92bda2 1008void kgdb_panic(const char *msg)
4402c153 1009{
7d92bda2
DA
1010 if (!kgdb_io_module_registered)
1011 return;
1012
5516fd7b 1013 /*
7d92bda2
DA
1014 * We don't want to get stuck waiting for input from user if
1015 * "panic_timeout" indicates the system should automatically
5516fd7b
CC
1016 * reboot on panic.
1017 */
1018 if (panic_timeout)
7d92bda2 1019 return;
5516fd7b 1020
4402c153 1021 if (dbg_kdb_mode)
7d92bda2
DA
1022 kdb_printf("PANIC: %s\n", msg);
1023
4402c153 1024 kgdb_breakpoint();
4402c153
JW
1025}
1026
b1a57bbf
DA
1027static void kgdb_initial_breakpoint(void)
1028{
1029 kgdb_break_asap = 0;
1030
1031 pr_crit("Waiting for connection from remote gdb...\n");
1032 kgdb_breakpoint();
1033}
1034
0b4b3827
JW
1035void __weak kgdb_arch_late(void)
1036{
1037}
1038
1039void __init dbg_late_init(void)
1040{
1041 dbg_is_early = false;
1042 if (kgdb_io_module_registered)
1043 kgdb_arch_late();
1044 kdb_init(KDB_INIT_FULL);
b1a57bbf
DA
1045
1046 if (kgdb_io_module_registered && kgdb_break_asap)
1047 kgdb_initial_breakpoint();
0b4b3827
JW
1048}
1049
2366e047
JW
1050static int
1051dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
1052{
bec4d62e
JW
1053 /*
1054 * Take the following action on reboot notify depending on value:
1055 * 1 == Enter debugger
220a31b0 1056 * 0 == [the default] detach debug client
bec4d62e
JW
1057 * -1 == Do nothing... and use this until the board resets
1058 */
1059 switch (kgdbreboot) {
1060 case 1:
1061 kgdb_breakpoint();
c8daba46 1062 goto done;
bec4d62e
JW
1063 case -1:
1064 goto done;
1065 }
2366e047
JW
1066 if (!dbg_kdb_mode)
1067 gdbstub_exit(code);
bec4d62e 1068done:
2366e047
JW
1069 return NOTIFY_DONE;
1070}
1071
1072static struct notifier_block dbg_reboot_notifier = {
1073 .notifier_call = dbg_notify_reboot,
1074 .next = NULL,
1075 .priority = INT_MAX,
1076};
1077
dc7d5527
JW
1078static void kgdb_register_callbacks(void)
1079{
1080 if (!kgdb_io_module_registered) {
1081 kgdb_io_module_registered = 1;
1082 kgdb_arch_init();
0b4b3827
JW
1083 if (!dbg_is_early)
1084 kgdb_arch_late();
f30fed10 1085 register_module_notifier(&dbg_module_load_nb);
2366e047 1086 register_reboot_notifier(&dbg_reboot_notifier);
dc7d5527 1087#ifdef CONFIG_MAGIC_SYSRQ
53197fc4 1088 register_sysrq_key('g', &sysrq_dbg_op);
dc7d5527
JW
1089#endif
1090 if (kgdb_use_con && !kgdb_con_registered) {
1091 register_console(&kgdbcons);
1092 kgdb_con_registered = 1;
1093 }
1094 }
1095}
1096
1097static void kgdb_unregister_callbacks(void)
1098{
1099 /*
7d92bda2
DA
1100 * When this routine is called KGDB should unregister from
1101 * handlers and clean up, making sure it is not handling any
dc7d5527
JW
1102 * break exceptions at the time.
1103 */
1104 if (kgdb_io_module_registered) {
1105 kgdb_io_module_registered = 0;
2366e047 1106 unregister_reboot_notifier(&dbg_reboot_notifier);
f30fed10 1107 unregister_module_notifier(&dbg_module_load_nb);
dc7d5527
JW
1108 kgdb_arch_exit();
1109#ifdef CONFIG_MAGIC_SYSRQ
53197fc4 1110 unregister_sysrq_key('g', &sysrq_dbg_op);
dc7d5527
JW
1111#endif
1112 if (kgdb_con_registered) {
1113 unregister_console(&kgdbcons);
1114 kgdb_con_registered = 0;
1115 }
1116 }
1117}
1118
dc7d5527 1119/**
737a460f 1120 * kgdb_register_io_module - register KGDB IO module
53197fc4 1121 * @new_dbg_io_ops: the io ops vector
dc7d5527
JW
1122 *
1123 * Register it with the KGDB core.
1124 */
53197fc4 1125int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
dc7d5527 1126{
22099562 1127 struct kgdb_io *old_dbg_io_ops;
dc7d5527
JW
1128 int err;
1129
1130 spin_lock(&kgdb_registration_lock);
1131
22099562
DA
1132 old_dbg_io_ops = dbg_io_ops;
1133 if (old_dbg_io_ops) {
1134 if (!old_dbg_io_ops->deinit) {
1135 spin_unlock(&kgdb_registration_lock);
dc7d5527 1136
22099562
DA
1137 pr_err("KGDB I/O driver %s can't replace %s.\n",
1138 new_dbg_io_ops->name, old_dbg_io_ops->name);
1139 return -EBUSY;
1140 }
1141 pr_info("Replacing I/O driver %s with %s\n",
1142 old_dbg_io_ops->name, new_dbg_io_ops->name);
dc7d5527
JW
1143 }
1144
53197fc4
JW
1145 if (new_dbg_io_ops->init) {
1146 err = new_dbg_io_ops->init();
dc7d5527
JW
1147 if (err) {
1148 spin_unlock(&kgdb_registration_lock);
1149 return err;
1150 }
1151 }
1152
53197fc4 1153 dbg_io_ops = new_dbg_io_ops;
dc7d5527
JW
1154
1155 spin_unlock(&kgdb_registration_lock);
1156
b1350132
DA
1157 if (old_dbg_io_ops) {
1158 old_dbg_io_ops->deinit();
22099562 1159 return 0;
b1350132 1160 }
22099562 1161
0f16996c 1162 pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name);
dc7d5527
JW
1163
1164 /* Arm KGDB now. */
1165 kgdb_register_callbacks();
1166
b1a57bbf
DA
1167 if (kgdb_break_asap &&
1168 (!dbg_is_early || IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG)))
dc7d5527
JW
1169 kgdb_initial_breakpoint();
1170
1171 return 0;
1172}
1173EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1174
1175/**
2da2687b 1176 * kgdb_unregister_io_module - unregister KGDB IO module
53197fc4 1177 * @old_dbg_io_ops: the io ops vector
dc7d5527
JW
1178 *
1179 * Unregister it with the KGDB core.
1180 */
53197fc4 1181void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
dc7d5527
JW
1182{
1183 BUG_ON(kgdb_connected);
1184
1185 /*
1186 * KGDB is no longer able to communicate out, so
1187 * unregister our callbacks and reset state.
1188 */
1189 kgdb_unregister_callbacks();
1190
1191 spin_lock(&kgdb_registration_lock);
1192
53197fc4
JW
1193 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
1194 dbg_io_ops = NULL;
dc7d5527
JW
1195
1196 spin_unlock(&kgdb_registration_lock);
1197
22099562
DA
1198 if (old_dbg_io_ops->deinit)
1199 old_dbg_io_ops->deinit();
1200
0f16996c 1201 pr_info("Unregistered I/O driver %s, debugger disabled\n",
53197fc4 1202 old_dbg_io_ops->name);
dc7d5527
JW
1203}
1204EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1205
dcc78711
JW
1206int dbg_io_get_char(void)
1207{
1208 int ret = dbg_io_ops->read_char();
f5316b4a
JW
1209 if (ret == NO_POLL_CHAR)
1210 return -1;
dcc78711
JW
1211 if (!dbg_kdb_mode)
1212 return ret;
1213 if (ret == 127)
1214 return 8;
1215 return ret;
1216}
1217
dc7d5527
JW
1218/**
1219 * kgdb_breakpoint - generate breakpoint exception
1220 *
1221 * This function will generate a breakpoint exception. It is used at the
1222 * beginning of a program to sync up with a debugger and can be used
1223 * otherwise as a quick means to stop program execution and "break" into
1224 * the debugger.
1225 */
d498d4b4 1226noinline void kgdb_breakpoint(void)
dc7d5527 1227{
ae6bf53e 1228 atomic_inc(&kgdb_setting_breakpoint);
dc7d5527
JW
1229 wmb(); /* Sync point before breakpoint */
1230 arch_kgdb_breakpoint();
1231 wmb(); /* Sync point after breakpoint */
ae6bf53e 1232 atomic_dec(&kgdb_setting_breakpoint);
dc7d5527
JW
1233}
1234EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1235
1236static int __init opt_kgdb_wait(char *str)
1237{
1238 kgdb_break_asap = 1;
1239
dcc78711 1240 kdb_init(KDB_INIT_EARLY);
b1a57bbf
DA
1241 if (kgdb_io_module_registered &&
1242 IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG))
dc7d5527
JW
1243 kgdb_initial_breakpoint();
1244
1245 return 0;
1246}
1247
1248early_param("kgdbwait", opt_kgdb_wait);