1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
57#include <asm/cacheflush.h>
58#include <asm/byteorder.h>
59#include <linux/atomic.h>
60
61#include "debug_core.h"
62
63static int kgdb_break_asap;
64
65struct debuggerinfo_struct kgdb_info[NR_CPUS];
66
67
68int kgdb_connected;
69EXPORT_SYMBOL_GPL(kgdb_connected);
70
71
72int kgdb_io_module_registered;
73
74
75static int exception_level;
76
77struct kgdb_io *dbg_io_ops;
78static DEFINE_SPINLOCK(kgdb_registration_lock);
79
80
81static int kgdbreboot;
82
83static int kgdb_con_registered;
84
85static int kgdb_use_con;
86
87bool dbg_is_early = true;
88
89int dbg_switch_cpu;
90
91
92int dbg_kdb_mode = 1;
93
94module_param(kgdb_use_con, int, 0644);
95module_param(kgdbreboot, int, 0644);
96
97
98
99
100
101static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
102 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
103};
104
105
106
107
108atomic_t kgdb_active = ATOMIC_INIT(-1);
109EXPORT_SYMBOL_GPL(kgdb_active);
110static DEFINE_RAW_SPINLOCK(dbg_master_lock);
111static DEFINE_RAW_SPINLOCK(dbg_slave_lock);
112
113
114
115
116
117static atomic_t masters_in_kgdb;
118static atomic_t slaves_in_kgdb;
119atomic_t kgdb_setting_breakpoint;
120
121struct task_struct *kgdb_usethread;
122struct task_struct *kgdb_contthread;
123
124int kgdb_single_step;
125static pid_t kgdb_sstep_pid;
126
127
128atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
129
130
131
132
133
134
135
136
137static int kgdb_do_roundup = 1;
138
139static int __init opt_nokgdbroundup(char *str)
140{
141 kgdb_do_roundup = 0;
142
143 return 0;
144}
145
146early_param("nokgdbroundup", opt_nokgdbroundup);
147
148
149
150
151
152
153
154
155
156int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
157{
158 int err;
159
160 err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
161 BREAK_INSTR_SIZE);
162 if (err)
163 return err;
164 err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
165 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
166 return err;
167}
168NOKPROBE_SYMBOL(kgdb_arch_set_breakpoint);
169
170int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
171{
172 return copy_to_kernel_nofault((char *)bpt->bpt_addr,
173 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
174}
175NOKPROBE_SYMBOL(kgdb_arch_remove_breakpoint);
176
177int __weak kgdb_validate_break_address(unsigned long addr)
178{
179 struct kgdb_bkpt tmp;
180 int err;
181
182 if (kgdb_within_blocklist(addr))
183 return -EINVAL;
184
185
186
187
188
189
190 tmp.bpt_addr = addr;
191 err = kgdb_arch_set_breakpoint(&tmp);
192 if (err)
193 return err;
194 err = kgdb_arch_remove_breakpoint(&tmp);
195 if (err)
196 pr_err("Critical breakpoint error, kernel memory destroyed at: %lx\n",
197 addr);
198 return err;
199}
200
201unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
202{
203 return instruction_pointer(regs);
204}
205NOKPROBE_SYMBOL(kgdb_arch_pc);
206
207int __weak kgdb_arch_init(void)
208{
209 return 0;
210}
211
212int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
213{
214 return 0;
215}
216NOKPROBE_SYMBOL(kgdb_skipexception);
217
218#ifdef CONFIG_SMP
219
220
221
222
223
224void __weak kgdb_call_nmi_hook(void *ignored)
225{
226
227
228
229
230
231
232
233
234 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
235}
236NOKPROBE_SYMBOL(kgdb_call_nmi_hook);
237
238static DEFINE_PER_CPU(call_single_data_t, kgdb_roundup_csd) =
239 CSD_INIT(kgdb_call_nmi_hook, NULL);
240
241void __weak kgdb_roundup_cpus(void)
242{
243 call_single_data_t *csd;
244 int this_cpu = raw_smp_processor_id();
245 int cpu;
246 int ret;
247
248 for_each_online_cpu(cpu) {
249
250 if (cpu == this_cpu)
251 continue;
252
253 csd = &per_cpu(kgdb_roundup_csd, cpu);
254
255
256
257
258
259
260
261
262
263 if (kgdb_info[cpu].rounding_up)
264 continue;
265 kgdb_info[cpu].rounding_up = true;
266
267 ret = smp_call_function_single_async(cpu, csd);
268 if (ret)
269 kgdb_info[cpu].rounding_up = false;
270 }
271}
272NOKPROBE_SYMBOL(kgdb_roundup_cpus);
273
274#endif
275
276
277
278
279
280static void kgdb_flush_swbreak_addr(unsigned long addr)
281{
282 if (!CACHE_FLUSH_IS_SAFE)
283 return;
284
285 if (current->mm) {
286 int i;
287
288 for (i = 0; i < VMACACHE_SIZE; i++) {
289 if (!current->vmacache.vmas[i])
290 continue;
291 flush_cache_range(current->vmacache.vmas[i],
292 addr, addr + BREAK_INSTR_SIZE);
293 }
294 }
295
296
297 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
298}
299NOKPROBE_SYMBOL(kgdb_flush_swbreak_addr);
300
301
302
303
304int dbg_activate_sw_breakpoints(void)
305{
306 int error;
307 int ret = 0;
308 int i;
309
310 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
311 if (kgdb_break[i].state != BP_SET)
312 continue;
313
314 error = kgdb_arch_set_breakpoint(&kgdb_break[i]);
315 if (error) {
316 ret = error;
317 pr_info("BP install failed: %lx\n",
318 kgdb_break[i].bpt_addr);
319 continue;
320 }
321
322 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
323 kgdb_break[i].state = BP_ACTIVE;
324 }
325 return ret;
326}
327NOKPROBE_SYMBOL(dbg_activate_sw_breakpoints);
328
329int dbg_set_sw_break(unsigned long addr)
330{
331 int err = kgdb_validate_break_address(addr);
332 int breakno = -1;
333 int i;
334
335 if (err)
336 return err;
337
338 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
339 if ((kgdb_break[i].state == BP_SET) &&
340 (kgdb_break[i].bpt_addr == addr))
341 return -EEXIST;
342 }
343 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
344 if (kgdb_break[i].state == BP_REMOVED &&
345 kgdb_break[i].bpt_addr == addr) {
346 breakno = i;
347 break;
348 }
349 }
350
351 if (breakno == -1) {
352 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
353 if (kgdb_break[i].state == BP_UNDEFINED) {
354 breakno = i;
355 break;
356 }
357 }
358 }
359
360 if (breakno == -1)
361 return -E2BIG;
362
363 kgdb_break[breakno].state = BP_SET;
364 kgdb_break[breakno].type = BP_BREAKPOINT;
365 kgdb_break[breakno].bpt_addr = addr;
366
367 return 0;
368}
369
370int dbg_deactivate_sw_breakpoints(void)
371{
372 int error;
373 int ret = 0;
374 int i;
375
376 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
377 if (kgdb_break[i].state != BP_ACTIVE)
378 continue;
379 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
380 if (error) {
381 pr_info("BP remove failed: %lx\n",
382 kgdb_break[i].bpt_addr);
383 ret = error;
384 }
385
386 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
387 kgdb_break[i].state = BP_SET;
388 }
389 return ret;
390}
391NOKPROBE_SYMBOL(dbg_deactivate_sw_breakpoints);
392
393int dbg_remove_sw_break(unsigned long addr)
394{
395 int i;
396
397 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
398 if ((kgdb_break[i].state == BP_SET) &&
399 (kgdb_break[i].bpt_addr == addr)) {
400 kgdb_break[i].state = BP_REMOVED;
401 return 0;
402 }
403 }
404 return -ENOENT;
405}
406
407int kgdb_isremovedbreak(unsigned long addr)
408{
409 int i;
410
411 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
412 if ((kgdb_break[i].state == BP_REMOVED) &&
413 (kgdb_break[i].bpt_addr == addr))
414 return 1;
415 }
416 return 0;
417}
418
419int kgdb_has_hit_break(unsigned long addr)
420{
421 int i;
422
423 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
424 if (kgdb_break[i].state == BP_ACTIVE &&
425 kgdb_break[i].bpt_addr == addr)
426 return 1;
427 }
428 return 0;
429}
430
431int dbg_remove_all_break(void)
432{
433 int error;
434 int i;
435
436
437 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
438 if (kgdb_break[i].state != BP_ACTIVE)
439 goto setundefined;
440 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
441 if (error)
442 pr_err("breakpoint remove failed: %lx\n",
443 kgdb_break[i].bpt_addr);
444setundefined:
445 kgdb_break[i].state = BP_UNDEFINED;
446 }
447
448
449 if (arch_kgdb_ops.remove_all_hw_break)
450 arch_kgdb_ops.remove_all_hw_break();
451
452 return 0;
453}
454
455void kgdb_free_init_mem(void)
456{
457 int i;
458
459
460 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
461 if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))
462 kgdb_break[i].state = BP_UNDEFINED;
463 }
464}
465
466#ifdef CONFIG_KGDB_KDB
467void kdb_dump_stack_on_cpu(int cpu)
468{
469 if (cpu == raw_smp_processor_id() || !IS_ENABLED(CONFIG_SMP)) {
470 dump_stack();
471 return;
472 }
473
474 if (!(kgdb_info[cpu].exception_state & DCPU_IS_SLAVE)) {
475 kdb_printf("ERROR: Task on cpu %d didn't stop in the debugger\n",
476 cpu);
477 return;
478 }
479
480
481
482
483
484
485
486
487
488
489
490
491 kgdb_info[cpu].exception_state |= DCPU_WANT_BT;
492 while (kgdb_info[cpu].exception_state & DCPU_WANT_BT)
493 cpu_relax();
494}
495#endif
496
497
498
499
500
501
502
503
504
505
506static int kgdb_io_ready(int print_wait)
507{
508 if (!dbg_io_ops)
509 return 0;
510 if (kgdb_connected)
511 return 1;
512 if (atomic_read(&kgdb_setting_breakpoint))
513 return 1;
514 if (print_wait) {
515#ifdef CONFIG_KGDB_KDB
516 if (!dbg_kdb_mode)
517 pr_crit("waiting... or $3#33 for KDB\n");
518#else
519 pr_crit("Waiting for remote debugger\n");
520#endif
521 }
522 return 1;
523}
524NOKPROBE_SYMBOL(kgdb_io_ready);
525
526static int kgdb_reenter_check(struct kgdb_state *ks)
527{
528 unsigned long addr;
529
530 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
531 return 0;
532
533
534 exception_level++;
535 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
536 dbg_deactivate_sw_breakpoints();
537
538
539
540
541
542
543
544 if (dbg_remove_sw_break(addr) == 0) {
545 exception_level = 0;
546 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
547 dbg_activate_sw_breakpoints();
548 pr_crit("re-enter error: breakpoint removed %lx\n", addr);
549 WARN_ON_ONCE(1);
550
551 return 1;
552 }
553 dbg_remove_all_break();
554 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
555
556 if (exception_level > 1) {
557 dump_stack();
558 kgdb_io_module_registered = false;
559 panic("Recursive entry to debugger");
560 }
561
562 pr_crit("re-enter exception: ALL breakpoints killed\n");
563#ifdef CONFIG_KGDB_KDB
564
565 return 0;
566#endif
567 dump_stack();
568 panic("Recursive entry to debugger");
569
570 return 1;
571}
572NOKPROBE_SYMBOL(kgdb_reenter_check);
573
574static void dbg_touch_watchdogs(void)
575{
576 touch_softlockup_watchdog_sync();
577 clocksource_touch_watchdog();
578 rcu_cpu_stall_reset();
579}
580NOKPROBE_SYMBOL(dbg_touch_watchdogs);
581
582static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
583 int exception_state)
584{
585 unsigned long flags;
586 int sstep_tries = 100;
587 int error;
588 int cpu;
589 int trace_on = 0;
590 int online_cpus = num_online_cpus();
591 u64 time_left;
592
593 kgdb_info[ks->cpu].enter_kgdb++;
594 kgdb_info[ks->cpu].exception_state |= exception_state;
595
596 if (exception_state == DCPU_WANT_MASTER)
597 atomic_inc(&masters_in_kgdb);
598 else
599 atomic_inc(&slaves_in_kgdb);
600
601 if (arch_kgdb_ops.disable_hw_break)
602 arch_kgdb_ops.disable_hw_break(regs);
603
604acquirelock:
605 rcu_read_lock();
606
607
608
609
610 local_irq_save(flags);
611
612 cpu = ks->cpu;
613 kgdb_info[cpu].debuggerinfo = regs;
614 kgdb_info[cpu].task = current;
615 kgdb_info[cpu].ret_state = 0;
616 kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
617
618
619 smp_mb();
620
621 if (exception_level == 1) {
622 if (raw_spin_trylock(&dbg_master_lock))
623 atomic_xchg(&kgdb_active, cpu);
624 goto cpu_master_loop;
625 }
626
627
628
629
630
631 while (1) {
632cpu_loop:
633 if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) {
634 kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER;
635 goto cpu_master_loop;
636 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
637 if (raw_spin_trylock(&dbg_master_lock)) {
638 atomic_xchg(&kgdb_active, cpu);
639 break;
640 }
641 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_BT) {
642 dump_stack();
643 kgdb_info[cpu].exception_state &= ~DCPU_WANT_BT;
644 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
645 if (!raw_spin_is_locked(&dbg_slave_lock))
646 goto return_normal;
647 } else {
648return_normal:
649
650
651
652 if (arch_kgdb_ops.correct_hw_break)
653 arch_kgdb_ops.correct_hw_break();
654 if (trace_on)
655 tracing_on();
656 kgdb_info[cpu].debuggerinfo = NULL;
657 kgdb_info[cpu].task = NULL;
658 kgdb_info[cpu].exception_state &=
659 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
660 kgdb_info[cpu].enter_kgdb--;
661 smp_mb__before_atomic();
662 atomic_dec(&slaves_in_kgdb);
663 dbg_touch_watchdogs();
664 local_irq_restore(flags);
665 rcu_read_unlock();
666 return 0;
667 }
668 cpu_relax();
669 }
670
671
672
673
674
675
676
677 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
678 (kgdb_info[cpu].task &&
679 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
680 atomic_set(&kgdb_active, -1);
681 raw_spin_unlock(&dbg_master_lock);
682 dbg_touch_watchdogs();
683 local_irq_restore(flags);
684 rcu_read_unlock();
685
686 goto acquirelock;
687 }
688
689 if (!kgdb_io_ready(1)) {
690 kgdb_info[cpu].ret_state = 1;
691 goto kgdb_restore;
692 }
693
694
695
696
697 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
698 goto kgdb_restore;
699
700 atomic_inc(&ignore_console_lock_warning);
701
702
703 if (dbg_io_ops->pre_exception)
704 dbg_io_ops->pre_exception();
705
706
707
708
709
710 if (!kgdb_single_step)
711 raw_spin_lock(&dbg_slave_lock);
712
713#ifdef CONFIG_SMP
714
715 if (ks->send_ready)
716 atomic_set(ks->send_ready, 1);
717
718
719 else if ((!kgdb_single_step) && kgdb_do_roundup)
720 kgdb_roundup_cpus();
721#endif
722
723
724
725
726 time_left = MSEC_PER_SEC;
727 while (kgdb_do_roundup && --time_left &&
728 (atomic_read(&masters_in_kgdb) + atomic_read(&slaves_in_kgdb)) !=
729 online_cpus)
730 udelay(1000);
731 if (!time_left)
732 pr_crit("Timed out waiting for secondary CPUs.\n");
733
734
735
736
737
738 dbg_deactivate_sw_breakpoints();
739 kgdb_single_step = 0;
740 kgdb_contthread = current;
741 exception_level = 0;
742 trace_on = tracing_is_on();
743 if (trace_on)
744 tracing_off();
745
746 while (1) {
747cpu_master_loop:
748 if (dbg_kdb_mode) {
749 kgdb_connected = 1;
750 error = kdb_stub(ks);
751 if (error == -1)
752 continue;
753 kgdb_connected = 0;
754 } else {
755 error = gdb_serial_stub(ks);
756 }
757
758 if (error == DBG_PASS_EVENT) {
759 dbg_kdb_mode = !dbg_kdb_mode;
760 } else if (error == DBG_SWITCH_CPU_EVENT) {
761 kgdb_info[dbg_switch_cpu].exception_state |=
762 DCPU_NEXT_MASTER;
763 goto cpu_loop;
764 } else {
765 kgdb_info[cpu].ret_state = error;
766 break;
767 }
768 }
769
770 dbg_activate_sw_breakpoints();
771
772
773 if (dbg_io_ops->post_exception)
774 dbg_io_ops->post_exception();
775
776 atomic_dec(&ignore_console_lock_warning);
777
778 if (!kgdb_single_step) {
779 raw_spin_unlock(&dbg_slave_lock);
780
781 while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb))
782 cpu_relax();
783 }
784
785kgdb_restore:
786 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
787 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
788 if (kgdb_info[sstep_cpu].task)
789 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
790 else
791 kgdb_sstep_pid = 0;
792 }
793 if (arch_kgdb_ops.correct_hw_break)
794 arch_kgdb_ops.correct_hw_break();
795 if (trace_on)
796 tracing_on();
797
798 kgdb_info[cpu].debuggerinfo = NULL;
799 kgdb_info[cpu].task = NULL;
800 kgdb_info[cpu].exception_state &=
801 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
802 kgdb_info[cpu].enter_kgdb--;
803 smp_mb__before_atomic();
804 atomic_dec(&masters_in_kgdb);
805
806 atomic_set(&kgdb_active, -1);
807 raw_spin_unlock(&dbg_master_lock);
808 dbg_touch_watchdogs();
809 local_irq_restore(flags);
810 rcu_read_unlock();
811
812 return kgdb_info[cpu].ret_state;
813}
814NOKPROBE_SYMBOL(kgdb_cpu_enter);
815
816
817
818
819
820
821
822
823int
824kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
825{
826 struct kgdb_state kgdb_var;
827 struct kgdb_state *ks = &kgdb_var;
828 int ret = 0;
829
830 if (arch_kgdb_ops.enable_nmi)
831 arch_kgdb_ops.enable_nmi(0);
832
833
834
835
836
837
838 if (signo != SIGTRAP && panic_timeout)
839 return 1;
840
841 memset(ks, 0, sizeof(struct kgdb_state));
842 ks->cpu = raw_smp_processor_id();
843 ks->ex_vector = evector;
844 ks->signo = signo;
845 ks->err_code = ecode;
846 ks->linux_regs = regs;
847
848 if (kgdb_reenter_check(ks))
849 goto out;
850 if (kgdb_info[ks->cpu].enter_kgdb != 0)
851 goto out;
852
853 ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
854out:
855 if (arch_kgdb_ops.enable_nmi)
856 arch_kgdb_ops.enable_nmi(1);
857 return ret;
858}
859NOKPROBE_SYMBOL(kgdb_handle_exception);
860
861
862
863
864static int module_event(struct notifier_block *self, unsigned long val,
865 void *data)
866{
867 return 0;
868}
869
870static struct notifier_block dbg_module_load_nb = {
871 .notifier_call = module_event,
872};
873
874int kgdb_nmicallback(int cpu, void *regs)
875{
876#ifdef CONFIG_SMP
877 struct kgdb_state kgdb_var;
878 struct kgdb_state *ks = &kgdb_var;
879
880 kgdb_info[cpu].rounding_up = false;
881
882 memset(ks, 0, sizeof(struct kgdb_state));
883 ks->cpu = cpu;
884 ks->linux_regs = regs;
885
886 if (kgdb_info[ks->cpu].enter_kgdb == 0 &&
887 raw_spin_is_locked(&dbg_master_lock)) {
888 kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE);
889 return 0;
890 }
891#endif
892 return 1;
893}
894NOKPROBE_SYMBOL(kgdb_nmicallback);
895
896int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
897 atomic_t *send_ready)
898{
899#ifdef CONFIG_SMP
900 if (!kgdb_io_ready(0) || !send_ready)
901 return 1;
902
903 if (kgdb_info[cpu].enter_kgdb == 0) {
904 struct kgdb_state kgdb_var;
905 struct kgdb_state *ks = &kgdb_var;
906
907 memset(ks, 0, sizeof(struct kgdb_state));
908 ks->cpu = cpu;
909 ks->ex_vector = trapnr;
910 ks->signo = SIGTRAP;
911 ks->err_code = err_code;
912 ks->linux_regs = regs;
913 ks->send_ready = send_ready;
914 kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
915 return 0;
916 }
917#endif
918 return 1;
919}
920NOKPROBE_SYMBOL(kgdb_nmicallin);
921
922static void kgdb_console_write(struct console *co, const char *s,
923 unsigned count)
924{
925 unsigned long flags;
926
927
928
929 if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode)
930 return;
931
932 local_irq_save(flags);
933 gdbstub_msg_write(s, count);
934 local_irq_restore(flags);
935}
936
937static struct console kgdbcons = {
938 .name = "kgdb",
939 .write = kgdb_console_write,
940 .flags = CON_PRINTBUFFER | CON_ENABLED,
941 .index = -1,
942};
943
944static int __init opt_kgdb_con(char *str)
945{
946 kgdb_use_con = 1;
947
948 if (kgdb_io_module_registered && !kgdb_con_registered) {
949 register_console(&kgdbcons);
950 kgdb_con_registered = 1;
951 }
952
953 return 0;
954}
955
956early_param("kgdbcon", opt_kgdb_con);
957
958#ifdef CONFIG_MAGIC_SYSRQ
959static void sysrq_handle_dbg(int key)
960{
961 if (!dbg_io_ops) {
962 pr_crit("ERROR: No KGDB I/O module available\n");
963 return;
964 }
965 if (!kgdb_connected) {
966#ifdef CONFIG_KGDB_KDB
967 if (!dbg_kdb_mode)
968 pr_crit("KGDB or $3#33 for KDB\n");
969#else
970 pr_crit("Entering KGDB\n");
971#endif
972 }
973
974 kgdb_breakpoint();
975}
976
977static const struct sysrq_key_op sysrq_dbg_op = {
978 .handler = sysrq_handle_dbg,
979 .help_msg = "debug(g)",
980 .action_msg = "DEBUG",
981};
982#endif
983
984void kgdb_panic(const char *msg)
985{
986 if (!kgdb_io_module_registered)
987 return;
988
989
990
991
992
993
994 if (panic_timeout)
995 return;
996
997 if (dbg_kdb_mode)
998 kdb_printf("PANIC: %s\n", msg);
999
1000 kgdb_breakpoint();
1001}
1002
1003static void kgdb_initial_breakpoint(void)
1004{
1005 kgdb_break_asap = 0;
1006
1007 pr_crit("Waiting for connection from remote gdb...\n");
1008 kgdb_breakpoint();
1009}
1010
1011void __weak kgdb_arch_late(void)
1012{
1013}
1014
1015void __init dbg_late_init(void)
1016{
1017 dbg_is_early = false;
1018 if (kgdb_io_module_registered)
1019 kgdb_arch_late();
1020 kdb_init(KDB_INIT_FULL);
1021
1022 if (kgdb_io_module_registered && kgdb_break_asap)
1023 kgdb_initial_breakpoint();
1024}
1025
1026static int
1027dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
1028{
1029
1030
1031
1032
1033
1034
1035 switch (kgdbreboot) {
1036 case 1:
1037 kgdb_breakpoint();
1038 goto done;
1039 case -1:
1040 goto done;
1041 }
1042 if (!dbg_kdb_mode)
1043 gdbstub_exit(code);
1044done:
1045 return NOTIFY_DONE;
1046}
1047
1048static struct notifier_block dbg_reboot_notifier = {
1049 .notifier_call = dbg_notify_reboot,
1050 .next = NULL,
1051 .priority = INT_MAX,
1052};
1053
1054static void kgdb_register_callbacks(void)
1055{
1056 if (!kgdb_io_module_registered) {
1057 kgdb_io_module_registered = 1;
1058 kgdb_arch_init();
1059 if (!dbg_is_early)
1060 kgdb_arch_late();
1061 register_module_notifier(&dbg_module_load_nb);
1062 register_reboot_notifier(&dbg_reboot_notifier);
1063#ifdef CONFIG_MAGIC_SYSRQ
1064 register_sysrq_key('g', &sysrq_dbg_op);
1065#endif
1066 if (kgdb_use_con && !kgdb_con_registered) {
1067 register_console(&kgdbcons);
1068 kgdb_con_registered = 1;
1069 }
1070 }
1071}
1072
1073static void kgdb_unregister_callbacks(void)
1074{
1075
1076
1077
1078
1079
1080 if (kgdb_io_module_registered) {
1081 kgdb_io_module_registered = 0;
1082 unregister_reboot_notifier(&dbg_reboot_notifier);
1083 unregister_module_notifier(&dbg_module_load_nb);
1084 kgdb_arch_exit();
1085#ifdef CONFIG_MAGIC_SYSRQ
1086 unregister_sysrq_key('g', &sysrq_dbg_op);
1087#endif
1088 if (kgdb_con_registered) {
1089 unregister_console(&kgdbcons);
1090 kgdb_con_registered = 0;
1091 }
1092 }
1093}
1094
1095
1096
1097
1098
1099
1100
1101int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
1102{
1103 struct kgdb_io *old_dbg_io_ops;
1104 int err;
1105
1106 spin_lock(&kgdb_registration_lock);
1107
1108 old_dbg_io_ops = dbg_io_ops;
1109 if (old_dbg_io_ops) {
1110 if (!old_dbg_io_ops->deinit) {
1111 spin_unlock(&kgdb_registration_lock);
1112
1113 pr_err("KGDB I/O driver %s can't replace %s.\n",
1114 new_dbg_io_ops->name, old_dbg_io_ops->name);
1115 return -EBUSY;
1116 }
1117 pr_info("Replacing I/O driver %s with %s\n",
1118 old_dbg_io_ops->name, new_dbg_io_ops->name);
1119 }
1120
1121 if (new_dbg_io_ops->init) {
1122 err = new_dbg_io_ops->init();
1123 if (err) {
1124 spin_unlock(&kgdb_registration_lock);
1125 return err;
1126 }
1127 }
1128
1129 dbg_io_ops = new_dbg_io_ops;
1130
1131 spin_unlock(&kgdb_registration_lock);
1132
1133 if (old_dbg_io_ops) {
1134 old_dbg_io_ops->deinit();
1135 return 0;
1136 }
1137
1138 pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name);
1139
1140
1141 kgdb_register_callbacks();
1142
1143 if (kgdb_break_asap &&
1144 (!dbg_is_early || IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG)))
1145 kgdb_initial_breakpoint();
1146
1147 return 0;
1148}
1149EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1150
1151
1152
1153
1154
1155
1156
1157void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
1158{
1159 BUG_ON(kgdb_connected);
1160
1161
1162
1163
1164
1165 kgdb_unregister_callbacks();
1166
1167 spin_lock(&kgdb_registration_lock);
1168
1169 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
1170 dbg_io_ops = NULL;
1171
1172 spin_unlock(&kgdb_registration_lock);
1173
1174 if (old_dbg_io_ops->deinit)
1175 old_dbg_io_ops->deinit();
1176
1177 pr_info("Unregistered I/O driver %s, debugger disabled\n",
1178 old_dbg_io_ops->name);
1179}
1180EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1181
1182int dbg_io_get_char(void)
1183{
1184 int ret = dbg_io_ops->read_char();
1185 if (ret == NO_POLL_CHAR)
1186 return -1;
1187 if (!dbg_kdb_mode)
1188 return ret;
1189 if (ret == 127)
1190 return 8;
1191 return ret;
1192}
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202noinline void kgdb_breakpoint(void)
1203{
1204 atomic_inc(&kgdb_setting_breakpoint);
1205 wmb();
1206 arch_kgdb_breakpoint();
1207 wmb();
1208 atomic_dec(&kgdb_setting_breakpoint);
1209}
1210EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1211
1212static int __init opt_kgdb_wait(char *str)
1213{
1214 kgdb_break_asap = 1;
1215
1216 kdb_init(KDB_INIT_EARLY);
1217 if (kgdb_io_module_registered &&
1218 IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG))
1219 kgdb_initial_breakpoint();
1220
1221 return 0;
1222}
1223
1224early_param("kgdbwait", opt_kgdb_wait);
1225