]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - kernel/debug/debug_core.c
UBUNTU: SAUCE: debug: Lock down kgdb
[mirror_ubuntu-jammy-kernel.git] / kernel / debug / debug_core.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Kernel Debug Core
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>
10 * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz>
11 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
12 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
13 * Copyright (C) 2005-2009 Wind River Systems, Inc.
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>
26 */
27
28 #define pr_fmt(fmt) "KGDB: " fmt
29
30 #include <linux/pid_namespace.h>
31 #include <linux/clocksource.h>
32 #include <linux/serial_core.h>
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>
41 #include <linux/string.h>
42 #include <linux/delay.h>
43 #include <linux/sched.h>
44 #include <linux/sysrq.h>
45 #include <linux/reboot.h>
46 #include <linux/init.h>
47 #include <linux/kgdb.h>
48 #include <linux/kdb.h>
49 #include <linux/nmi.h>
50 #include <linux/pid.h>
51 #include <linux/smp.h>
52 #include <linux/mm.h>
53 #include <linux/vmacache.h>
54 #include <linux/rcupdate.h>
55 #include <linux/irq.h>
56 #include <linux/security.h>
57
58 #include <asm/cacheflush.h>
59 #include <asm/byteorder.h>
60 #include <linux/atomic.h>
61
62 #include "debug_core.h"
63
64 static int kgdb_break_asap;
65
66 struct debuggerinfo_struct kgdb_info[NR_CPUS];
67
68 /* kgdb_connected - Is a host GDB connected to us? */
69 int kgdb_connected;
70 EXPORT_SYMBOL_GPL(kgdb_connected);
71
72 /* All the KGDB handlers are installed */
73 int kgdb_io_module_registered;
74
75 /* Guard for recursive entry */
76 static int exception_level;
77
78 struct kgdb_io *dbg_io_ops;
79 static DEFINE_SPINLOCK(kgdb_registration_lock);
80
81 /* Action for the reboot notifier, a global allow kdb to change it */
82 static int kgdbreboot;
83 /* kgdb console driver is loaded */
84 static int kgdb_con_registered;
85 /* determine if kgdb console output should be used */
86 static int kgdb_use_con;
87 /* Flag for alternate operations for early debugging */
88 bool dbg_is_early = true;
89 /* Next cpu to become the master debug core */
90 int dbg_switch_cpu;
91
92 /* Use kdb or gdbserver mode */
93 int dbg_kdb_mode = 1;
94
95 module_param(kgdb_use_con, int, 0644);
96 module_param(kgdbreboot, int, 0644);
97
98 /*
99 * Holds information about breakpoints in a kernel. These breakpoints are
100 * added and removed by gdb.
101 */
102 static 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 */
109 atomic_t kgdb_active = ATOMIC_INIT(-1);
110 EXPORT_SYMBOL_GPL(kgdb_active);
111 static DEFINE_RAW_SPINLOCK(dbg_master_lock);
112 static DEFINE_RAW_SPINLOCK(dbg_slave_lock);
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 */
118 static atomic_t masters_in_kgdb;
119 static atomic_t slaves_in_kgdb;
120 atomic_t kgdb_setting_breakpoint;
121
122 struct task_struct *kgdb_usethread;
123 struct task_struct *kgdb_contthread;
124
125 int kgdb_single_step;
126 static pid_t kgdb_sstep_pid;
127
128 /* to keep track of the CPU which is doing the single stepping*/
129 atomic_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 */
138 static int kgdb_do_roundup = 1;
139
140 static int __init opt_nokgdbroundup(char *str)
141 {
142 kgdb_do_roundup = 0;
143
144 return 0;
145 }
146
147 early_param("nokgdbroundup", opt_nokgdbroundup);
148
149 /*
150 * Finally, some KGDB code :-)
151 */
152
153 /*
154 * Weak aliases for breakpoint management,
155 * can be overridden by architectures when needed:
156 */
157 int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
158 {
159 int err;
160
161 err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
162 BREAK_INSTR_SIZE);
163 if (err)
164 return err;
165 err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
166 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
167 return err;
168 }
169 NOKPROBE_SYMBOL(kgdb_arch_set_breakpoint);
170
171 int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
172 {
173 return copy_to_kernel_nofault((char *)bpt->bpt_addr,
174 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
175 }
176 NOKPROBE_SYMBOL(kgdb_arch_remove_breakpoint);
177
178 int __weak kgdb_validate_break_address(unsigned long addr)
179 {
180 struct kgdb_bkpt tmp;
181 int err;
182
183 if (kgdb_within_blocklist(addr))
184 return -EINVAL;
185
186 /* Validate setting the breakpoint and then removing it. If the
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 */
191 tmp.bpt_addr = addr;
192 err = kgdb_arch_set_breakpoint(&tmp);
193 if (err)
194 return err;
195 err = kgdb_arch_remove_breakpoint(&tmp);
196 if (err)
197 pr_err("Critical breakpoint error, kernel memory destroyed at: %lx\n",
198 addr);
199 return err;
200 }
201
202 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
203 {
204 return instruction_pointer(regs);
205 }
206 NOKPROBE_SYMBOL(kgdb_arch_pc);
207
208 int __weak kgdb_arch_init(void)
209 {
210 return 0;
211 }
212
213 int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
214 {
215 return 0;
216 }
217 NOKPROBE_SYMBOL(kgdb_skipexception);
218
219 #ifdef CONFIG_SMP
220
221 /*
222 * Default (weak) implementation for kgdb_roundup_cpus
223 */
224
225 void __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 }
237 NOKPROBE_SYMBOL(kgdb_call_nmi_hook);
238
239 static DEFINE_PER_CPU(call_single_data_t, kgdb_roundup_csd) =
240 CSD_INIT(kgdb_call_nmi_hook, NULL);
241
242 void __weak kgdb_roundup_cpus(void)
243 {
244 call_single_data_t *csd;
245 int this_cpu = raw_smp_processor_id();
246 int cpu;
247 int ret;
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);
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
268 ret = smp_call_function_single_async(cpu, csd);
269 if (ret)
270 kgdb_info[cpu].rounding_up = false;
271 }
272 }
273 NOKPROBE_SYMBOL(kgdb_roundup_cpus);
274
275 #endif
276
277 /*
278 * Some architectures need cache flushes when we set/clear a
279 * breakpoint:
280 */
281 static void kgdb_flush_swbreak_addr(unsigned long addr)
282 {
283 if (!CACHE_FLUSH_IS_SAFE)
284 return;
285
286 if (current->mm) {
287 int i;
288
289 for (i = 0; i < VMACACHE_SIZE; i++) {
290 if (!current->vmacache.vmas[i])
291 continue;
292 flush_cache_range(current->vmacache.vmas[i],
293 addr, addr + BREAK_INSTR_SIZE);
294 }
295 }
296
297 /* Force flush instruction cache if it was outside the mm */
298 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
299 }
300 NOKPROBE_SYMBOL(kgdb_flush_swbreak_addr);
301
302 /*
303 * SW breakpoint management:
304 */
305 int dbg_activate_sw_breakpoints(void)
306 {
307 int error;
308 int ret = 0;
309 int i;
310
311 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
312 if (kgdb_break[i].state != BP_SET)
313 continue;
314
315 error = kgdb_arch_set_breakpoint(&kgdb_break[i]);
316 if (error) {
317 ret = error;
318 pr_info("BP install failed: %lx\n",
319 kgdb_break[i].bpt_addr);
320 continue;
321 }
322
323 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
324 kgdb_break[i].state = BP_ACTIVE;
325 }
326 return ret;
327 }
328 NOKPROBE_SYMBOL(dbg_activate_sw_breakpoints);
329
330 int dbg_set_sw_break(unsigned long addr)
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
371 int dbg_deactivate_sw_breakpoints(void)
372 {
373 int error;
374 int ret = 0;
375 int i;
376
377 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
378 if (kgdb_break[i].state != BP_ACTIVE)
379 continue;
380 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
381 if (error) {
382 pr_info("BP remove failed: %lx\n",
383 kgdb_break[i].bpt_addr);
384 ret = error;
385 }
386
387 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
388 kgdb_break[i].state = BP_SET;
389 }
390 return ret;
391 }
392 NOKPROBE_SYMBOL(dbg_deactivate_sw_breakpoints);
393
394 int dbg_remove_sw_break(unsigned long addr)
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
408 int 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
420 int 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
432 int dbg_remove_all_break(void)
433 {
434 int error;
435 int i;
436
437 /* Clear memory breakpoints. */
438 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
439 if (kgdb_break[i].state != BP_ACTIVE)
440 goto setundefined;
441 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
442 if (error)
443 pr_err("breakpoint remove failed: %lx\n",
444 kgdb_break[i].bpt_addr);
445 setundefined:
446 kgdb_break[i].state = BP_UNDEFINED;
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
456 void 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
467 #ifdef CONFIG_KGDB_KDB
468 void kdb_dump_stack_on_cpu(int cpu)
469 {
470 if (cpu == raw_smp_processor_id() || !IS_ENABLED(CONFIG_SMP)) {
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
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 */
507 static int kgdb_io_ready(int print_wait)
508 {
509 if (!dbg_io_ops)
510 return 0;
511 if (kgdb_connected)
512 return 1;
513 if (atomic_read(&kgdb_setting_breakpoint))
514 return 1;
515 if (print_wait) {
516 #ifdef CONFIG_KGDB_KDB
517 if (!dbg_kdb_mode)
518 pr_crit("waiting... or $3#33 for KDB\n");
519 #else
520 pr_crit("Waiting for remote debugger\n");
521 #endif
522 }
523 return 1;
524 }
525 NOKPROBE_SYMBOL(kgdb_io_ready);
526
527 static 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);
537 dbg_deactivate_sw_breakpoints();
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 */
545 if (dbg_remove_sw_break(addr) == 0) {
546 exception_level = 0;
547 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
548 dbg_activate_sw_breakpoints();
549 pr_crit("re-enter error: breakpoint removed %lx\n", addr);
550 WARN_ON_ONCE(1);
551
552 return 1;
553 }
554 dbg_remove_all_break();
555 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
556
557 if (exception_level > 1) {
558 dump_stack();
559 kgdb_io_module_registered = false;
560 panic("Recursive entry to debugger");
561 }
562
563 pr_crit("re-enter exception: ALL breakpoints killed\n");
564 #ifdef CONFIG_KGDB_KDB
565 /* Allow kdb to debug itself one level */
566 return 0;
567 #endif
568 dump_stack();
569 panic("Recursive entry to debugger");
570
571 return 1;
572 }
573 NOKPROBE_SYMBOL(kgdb_reenter_check);
574
575 static void dbg_touch_watchdogs(void)
576 {
577 touch_softlockup_watchdog_sync();
578 clocksource_touch_watchdog();
579 rcu_cpu_stall_reset();
580 }
581 NOKPROBE_SYMBOL(dbg_touch_watchdogs);
582
583 static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
584 int exception_state)
585 {
586 unsigned long flags;
587 int sstep_tries = 100;
588 int error;
589 int cpu;
590 int trace_on = 0;
591 int online_cpus = num_online_cpus();
592 u64 time_left;
593
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);
601
602 if (arch_kgdb_ops.disable_hw_break)
603 arch_kgdb_ops.disable_hw_break(regs);
604
605 acquirelock:
606 rcu_read_lock();
607 /*
608 * Interrupts will be restored by the 'trap return' code, except when
609 * single stepping.
610 */
611 local_irq_save(flags);
612
613 cpu = ks->cpu;
614 kgdb_info[cpu].debuggerinfo = regs;
615 kgdb_info[cpu].task = current;
616 kgdb_info[cpu].ret_state = 0;
617 kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
618
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);
625 goto cpu_master_loop;
626 }
627
628 /*
629 * CPU will loop if it is a slave or request to become a kgdb
630 * master cpu and acquire the kgdb_active lock:
631 */
632 while (1) {
633 cpu_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) {
638 if (raw_spin_trylock(&dbg_master_lock)) {
639 atomic_xchg(&kgdb_active, cpu);
640 break;
641 }
642 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_BT) {
643 dump_stack();
644 kgdb_info[cpu].exception_state &= ~DCPU_WANT_BT;
645 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
646 if (!raw_spin_is_locked(&dbg_slave_lock))
647 goto return_normal;
648 } else {
649 return_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();
655 if (trace_on)
656 tracing_on();
657 kgdb_info[cpu].debuggerinfo = NULL;
658 kgdb_info[cpu].task = NULL;
659 kgdb_info[cpu].exception_state &=
660 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
661 kgdb_info[cpu].enter_kgdb--;
662 smp_mb__before_atomic();
663 atomic_dec(&slaves_in_kgdb);
664 dbg_touch_watchdogs();
665 local_irq_restore(flags);
666 rcu_read_unlock();
667 return 0;
668 }
669 cpu_relax();
670 }
671
672 /*
673 * For single stepping, try to only enter on the processor
674 * that was single stepping. To guard against a deadlock, the
675 * kernel will only try for the value of sstep_tries before
676 * giving up and continuing on.
677 */
678 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
679 (kgdb_info[cpu].task &&
680 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
681 atomic_set(&kgdb_active, -1);
682 raw_spin_unlock(&dbg_master_lock);
683 dbg_touch_watchdogs();
684 local_irq_restore(flags);
685 rcu_read_unlock();
686
687 goto acquirelock;
688 }
689
690 if (!kgdb_io_ready(1)) {
691 kgdb_info[cpu].ret_state = 1;
692 goto kgdb_restore; /* No I/O connection, resume the system */
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
701 atomic_inc(&ignore_console_lock_warning);
702
703 /* Call the I/O driver's pre_exception routine */
704 if (dbg_io_ops->pre_exception)
705 dbg_io_ops->pre_exception();
706
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 */
711 if (!kgdb_single_step)
712 raw_spin_lock(&dbg_slave_lock);
713
714 #ifdef CONFIG_SMP
715 /* If send_ready set, slaves are already waiting */
716 if (ks->send_ready)
717 atomic_set(ks->send_ready, 1);
718
719 /* Signal the other CPUs to enter kgdb_wait() */
720 else if ((!kgdb_single_step) && kgdb_do_roundup)
721 kgdb_roundup_cpus();
722 #endif
723
724 /*
725 * Wait for the other CPUs to be notified and be waiting for us:
726 */
727 time_left = MSEC_PER_SEC;
728 while (kgdb_do_roundup && --time_left &&
729 (atomic_read(&masters_in_kgdb) + atomic_read(&slaves_in_kgdb)) !=
730 online_cpus)
731 udelay(1000);
732 if (!time_left)
733 pr_crit("Timed out waiting for secondary CPUs.\n");
734
735 /*
736 * At this point the primary processor is completely
737 * in the debugger and all secondary CPUs are quiescent
738 */
739 dbg_deactivate_sw_breakpoints();
740 kgdb_single_step = 0;
741 kgdb_contthread = current;
742 exception_level = 0;
743 trace_on = tracing_is_on();
744 if (trace_on)
745 tracing_off();
746
747 while (1) {
748 cpu_master_loop:
749 if (security_locked_down(LOCKDOWN_KGDB))
750 break;
751 if (dbg_kdb_mode) {
752 kgdb_connected = 1;
753 error = kdb_stub(ks);
754 if (error == -1)
755 continue;
756 kgdb_connected = 0;
757 } else {
758 error = gdb_serial_stub(ks);
759 }
760
761 if (error == DBG_PASS_EVENT) {
762 dbg_kdb_mode = !dbg_kdb_mode;
763 } else if (error == DBG_SWITCH_CPU_EVENT) {
764 kgdb_info[dbg_switch_cpu].exception_state |=
765 DCPU_NEXT_MASTER;
766 goto cpu_loop;
767 } else {
768 kgdb_info[cpu].ret_state = error;
769 break;
770 }
771 }
772
773 dbg_activate_sw_breakpoints();
774
775 /* Call the I/O driver's post_exception routine */
776 if (dbg_io_ops->post_exception)
777 dbg_io_ops->post_exception();
778
779 atomic_dec(&ignore_console_lock_warning);
780
781 if (!kgdb_single_step) {
782 raw_spin_unlock(&dbg_slave_lock);
783 /* Wait till all the CPUs have quit from the debugger. */
784 while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb))
785 cpu_relax();
786 }
787
788 kgdb_restore:
789 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
790 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
791 if (kgdb_info[sstep_cpu].task)
792 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
793 else
794 kgdb_sstep_pid = 0;
795 }
796 if (arch_kgdb_ops.correct_hw_break)
797 arch_kgdb_ops.correct_hw_break();
798 if (trace_on)
799 tracing_on();
800
801 kgdb_info[cpu].debuggerinfo = NULL;
802 kgdb_info[cpu].task = NULL;
803 kgdb_info[cpu].exception_state &=
804 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
805 kgdb_info[cpu].enter_kgdb--;
806 smp_mb__before_atomic();
807 atomic_dec(&masters_in_kgdb);
808 /* Free kgdb_active */
809 atomic_set(&kgdb_active, -1);
810 raw_spin_unlock(&dbg_master_lock);
811 dbg_touch_watchdogs();
812 local_irq_restore(flags);
813 rcu_read_unlock();
814
815 return kgdb_info[cpu].ret_state;
816 }
817 NOKPROBE_SYMBOL(kgdb_cpu_enter);
818
819 /*
820 * kgdb_handle_exception() - main entry point from a kernel exception
821 *
822 * Locking hierarchy:
823 * interface locks, if any (begin_session)
824 * kgdb lock (kgdb_active)
825 */
826 int
827 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
828 {
829 struct kgdb_state kgdb_var;
830 struct kgdb_state *ks = &kgdb_var;
831 int ret = 0;
832
833 if (arch_kgdb_ops.enable_nmi)
834 arch_kgdb_ops.enable_nmi(0);
835 /*
836 * Avoid entering the debugger if we were triggered due to an oops
837 * but panic_timeout indicates the system should automatically
838 * reboot on panic. We don't want to get stuck waiting for input
839 * on such systems, especially if its "just" an oops.
840 */
841 if (signo != SIGTRAP && panic_timeout)
842 return 1;
843
844 memset(ks, 0, sizeof(struct kgdb_state));
845 ks->cpu = raw_smp_processor_id();
846 ks->ex_vector = evector;
847 ks->signo = signo;
848 ks->err_code = ecode;
849 ks->linux_regs = regs;
850
851 if (kgdb_reenter_check(ks))
852 goto out; /* Ouch, double exception ! */
853 if (kgdb_info[ks->cpu].enter_kgdb != 0)
854 goto out;
855
856 ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
857 out:
858 if (arch_kgdb_ops.enable_nmi)
859 arch_kgdb_ops.enable_nmi(1);
860 return ret;
861 }
862 NOKPROBE_SYMBOL(kgdb_handle_exception);
863
864 /*
865 * GDB places a breakpoint at this function to know dynamically loaded objects.
866 */
867 static int module_event(struct notifier_block *self, unsigned long val,
868 void *data)
869 {
870 return 0;
871 }
872
873 static struct notifier_block dbg_module_load_nb = {
874 .notifier_call = module_event,
875 };
876
877 int kgdb_nmicallback(int cpu, void *regs)
878 {
879 #ifdef CONFIG_SMP
880 struct kgdb_state kgdb_var;
881 struct kgdb_state *ks = &kgdb_var;
882
883 kgdb_info[cpu].rounding_up = false;
884
885 memset(ks, 0, sizeof(struct kgdb_state));
886 ks->cpu = cpu;
887 ks->linux_regs = regs;
888
889 if (kgdb_info[ks->cpu].enter_kgdb == 0 &&
890 raw_spin_is_locked(&dbg_master_lock)) {
891 kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE);
892 return 0;
893 }
894 #endif
895 return 1;
896 }
897 NOKPROBE_SYMBOL(kgdb_nmicallback);
898
899 int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
900 atomic_t *send_ready)
901 {
902 #ifdef CONFIG_SMP
903 if (!kgdb_io_ready(0) || !send_ready)
904 return 1;
905
906 if (kgdb_info[cpu].enter_kgdb == 0) {
907 struct kgdb_state kgdb_var;
908 struct kgdb_state *ks = &kgdb_var;
909
910 memset(ks, 0, sizeof(struct kgdb_state));
911 ks->cpu = cpu;
912 ks->ex_vector = trapnr;
913 ks->signo = SIGTRAP;
914 ks->err_code = err_code;
915 ks->linux_regs = regs;
916 ks->send_ready = send_ready;
917 kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
918 return 0;
919 }
920 #endif
921 return 1;
922 }
923 NOKPROBE_SYMBOL(kgdb_nmicallin);
924
925 static void kgdb_console_write(struct console *co, const char *s,
926 unsigned count)
927 {
928 unsigned long flags;
929
930 /* If we're debugging, or KGDB has not connected, don't try
931 * and print. */
932 if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode)
933 return;
934
935 local_irq_save(flags);
936 gdbstub_msg_write(s, count);
937 local_irq_restore(flags);
938 }
939
940 static struct console kgdbcons = {
941 .name = "kgdb",
942 .write = kgdb_console_write,
943 .flags = CON_PRINTBUFFER | CON_ENABLED,
944 .index = -1,
945 };
946
947 static int __init opt_kgdb_con(char *str)
948 {
949 kgdb_use_con = 1;
950
951 if (kgdb_io_module_registered && !kgdb_con_registered) {
952 register_console(&kgdbcons);
953 kgdb_con_registered = 1;
954 }
955
956 return 0;
957 }
958
959 early_param("kgdbcon", opt_kgdb_con);
960
961 #ifdef CONFIG_MAGIC_SYSRQ
962 static void sysrq_handle_dbg(int key)
963 {
964 if (!dbg_io_ops) {
965 pr_crit("ERROR: No KGDB I/O module available\n");
966 return;
967 }
968 if (!kgdb_connected) {
969 #ifdef CONFIG_KGDB_KDB
970 if (!dbg_kdb_mode)
971 pr_crit("KGDB or $3#33 for KDB\n");
972 #else
973 pr_crit("Entering KGDB\n");
974 #endif
975 }
976
977 kgdb_breakpoint();
978 }
979
980 static const struct sysrq_key_op sysrq_dbg_op = {
981 .handler = sysrq_handle_dbg,
982 .help_msg = "debug(g)",
983 .action_msg = "DEBUG",
984 };
985 #endif
986
987 void kgdb_panic(const char *msg)
988 {
989 if (!kgdb_io_module_registered)
990 return;
991
992 /*
993 * We don't want to get stuck waiting for input from user if
994 * "panic_timeout" indicates the system should automatically
995 * reboot on panic.
996 */
997 if (panic_timeout)
998 return;
999
1000 if (dbg_kdb_mode)
1001 kdb_printf("PANIC: %s\n", msg);
1002
1003 kgdb_breakpoint();
1004 }
1005
1006 static void kgdb_initial_breakpoint(void)
1007 {
1008 kgdb_break_asap = 0;
1009
1010 pr_crit("Waiting for connection from remote gdb...\n");
1011 kgdb_breakpoint();
1012 }
1013
1014 void __weak kgdb_arch_late(void)
1015 {
1016 }
1017
1018 void __init dbg_late_init(void)
1019 {
1020 dbg_is_early = false;
1021 if (kgdb_io_module_registered)
1022 kgdb_arch_late();
1023 kdb_init(KDB_INIT_FULL);
1024
1025 if (kgdb_io_module_registered && kgdb_break_asap)
1026 kgdb_initial_breakpoint();
1027 }
1028
1029 static int
1030 dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
1031 {
1032 /*
1033 * Take the following action on reboot notify depending on value:
1034 * 1 == Enter debugger
1035 * 0 == [the default] detach debug client
1036 * -1 == Do nothing... and use this until the board resets
1037 */
1038 switch (kgdbreboot) {
1039 case 1:
1040 kgdb_breakpoint();
1041 goto done;
1042 case -1:
1043 goto done;
1044 }
1045 if (!dbg_kdb_mode)
1046 gdbstub_exit(code);
1047 done:
1048 return NOTIFY_DONE;
1049 }
1050
1051 static struct notifier_block dbg_reboot_notifier = {
1052 .notifier_call = dbg_notify_reboot,
1053 .next = NULL,
1054 .priority = INT_MAX,
1055 };
1056
1057 static void kgdb_register_callbacks(void)
1058 {
1059 if (!kgdb_io_module_registered) {
1060 kgdb_io_module_registered = 1;
1061 kgdb_arch_init();
1062 if (!dbg_is_early)
1063 kgdb_arch_late();
1064 register_module_notifier(&dbg_module_load_nb);
1065 register_reboot_notifier(&dbg_reboot_notifier);
1066 #ifdef CONFIG_MAGIC_SYSRQ
1067 register_sysrq_key('g', &sysrq_dbg_op);
1068 #endif
1069 if (kgdb_use_con && !kgdb_con_registered) {
1070 register_console(&kgdbcons);
1071 kgdb_con_registered = 1;
1072 }
1073 }
1074 }
1075
1076 static void kgdb_unregister_callbacks(void)
1077 {
1078 /*
1079 * When this routine is called KGDB should unregister from
1080 * handlers and clean up, making sure it is not handling any
1081 * break exceptions at the time.
1082 */
1083 if (kgdb_io_module_registered) {
1084 kgdb_io_module_registered = 0;
1085 unregister_reboot_notifier(&dbg_reboot_notifier);
1086 unregister_module_notifier(&dbg_module_load_nb);
1087 kgdb_arch_exit();
1088 #ifdef CONFIG_MAGIC_SYSRQ
1089 unregister_sysrq_key('g', &sysrq_dbg_op);
1090 #endif
1091 if (kgdb_con_registered) {
1092 unregister_console(&kgdbcons);
1093 kgdb_con_registered = 0;
1094 }
1095 }
1096 }
1097
1098 /**
1099 * kgdb_register_io_module - register KGDB IO module
1100 * @new_dbg_io_ops: the io ops vector
1101 *
1102 * Register it with the KGDB core.
1103 */
1104 int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
1105 {
1106 struct kgdb_io *old_dbg_io_ops;
1107 int err;
1108
1109 spin_lock(&kgdb_registration_lock);
1110
1111 old_dbg_io_ops = dbg_io_ops;
1112 if (old_dbg_io_ops) {
1113 if (!old_dbg_io_ops->deinit) {
1114 spin_unlock(&kgdb_registration_lock);
1115
1116 pr_err("KGDB I/O driver %s can't replace %s.\n",
1117 new_dbg_io_ops->name, old_dbg_io_ops->name);
1118 return -EBUSY;
1119 }
1120 pr_info("Replacing I/O driver %s with %s\n",
1121 old_dbg_io_ops->name, new_dbg_io_ops->name);
1122 }
1123
1124 if (new_dbg_io_ops->init) {
1125 err = new_dbg_io_ops->init();
1126 if (err) {
1127 spin_unlock(&kgdb_registration_lock);
1128 return err;
1129 }
1130 }
1131
1132 dbg_io_ops = new_dbg_io_ops;
1133
1134 spin_unlock(&kgdb_registration_lock);
1135
1136 if (old_dbg_io_ops) {
1137 old_dbg_io_ops->deinit();
1138 return 0;
1139 }
1140
1141 pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name);
1142
1143 /* Arm KGDB now. */
1144 kgdb_register_callbacks();
1145
1146 if (kgdb_break_asap &&
1147 (!dbg_is_early || IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG)))
1148 kgdb_initial_breakpoint();
1149
1150 return 0;
1151 }
1152 EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1153
1154 /**
1155 * kgdb_unregister_io_module - unregister KGDB IO module
1156 * @old_dbg_io_ops: the io ops vector
1157 *
1158 * Unregister it with the KGDB core.
1159 */
1160 void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
1161 {
1162 BUG_ON(kgdb_connected);
1163
1164 /*
1165 * KGDB is no longer able to communicate out, so
1166 * unregister our callbacks and reset state.
1167 */
1168 kgdb_unregister_callbacks();
1169
1170 spin_lock(&kgdb_registration_lock);
1171
1172 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
1173 dbg_io_ops = NULL;
1174
1175 spin_unlock(&kgdb_registration_lock);
1176
1177 if (old_dbg_io_ops->deinit)
1178 old_dbg_io_ops->deinit();
1179
1180 pr_info("Unregistered I/O driver %s, debugger disabled\n",
1181 old_dbg_io_ops->name);
1182 }
1183 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1184
1185 int dbg_io_get_char(void)
1186 {
1187 int ret = dbg_io_ops->read_char();
1188 if (ret == NO_POLL_CHAR)
1189 return -1;
1190 if (!dbg_kdb_mode)
1191 return ret;
1192 if (ret == 127)
1193 return 8;
1194 return ret;
1195 }
1196
1197 /**
1198 * kgdb_breakpoint - generate breakpoint exception
1199 *
1200 * This function will generate a breakpoint exception. It is used at the
1201 * beginning of a program to sync up with a debugger and can be used
1202 * otherwise as a quick means to stop program execution and "break" into
1203 * the debugger.
1204 */
1205 noinline void kgdb_breakpoint(void)
1206 {
1207 atomic_inc(&kgdb_setting_breakpoint);
1208 wmb(); /* Sync point before breakpoint */
1209 arch_kgdb_breakpoint();
1210 wmb(); /* Sync point after breakpoint */
1211 atomic_dec(&kgdb_setting_breakpoint);
1212 }
1213 EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1214
1215 static int __init opt_kgdb_wait(char *str)
1216 {
1217 kgdb_break_asap = 1;
1218
1219 kdb_init(KDB_INIT_EARLY);
1220 if (kgdb_io_module_registered &&
1221 IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG))
1222 kgdb_initial_breakpoint();
1223
1224 return 0;
1225 }
1226
1227 early_param("kgdbwait", opt_kgdb_wait);