hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/kernel/debug/debug_core.c
....@@ -55,6 +55,8 @@
5555 #include <linux/mm.h>
5656 #include <linux/vmacache.h>
5757 #include <linux/rcupdate.h>
58
+#include <linux/irq.h>
59
+#include <linux/security.h>
5860
5961 #include <asm/cacheflush.h>
6062 #include <asm/byteorder.h>
....@@ -66,9 +68,7 @@
6668
6769 struct debuggerinfo_struct kgdb_info[NR_CPUS];
6870
69
-/**
70
- * kgdb_connected - Is a host GDB connected to us?
71
- */
71
+/* kgdb_connected - Is a host GDB connected to us? */
7272 int kgdb_connected;
7373 EXPORT_SYMBOL_GPL(kgdb_connected);
7474
....@@ -81,7 +81,7 @@
8181 struct kgdb_io *dbg_io_ops;
8282 static DEFINE_SPINLOCK(kgdb_registration_lock);
8383
84
-/* Action for the reboot notifiter, a global allow kdb to change it */
84
+/* Action for the reboot notifier, a global allow kdb to change it */
8585 static int kgdbreboot;
8686 /* kgdb console driver is loaded */
8787 static int kgdb_con_registered;
....@@ -156,31 +156,37 @@
156156
157157 /*
158158 * Weak aliases for breakpoint management,
159
- * can be overriden by architectures when needed:
159
+ * can be overridden by architectures when needed:
160160 */
161161 int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
162162 {
163163 int err;
164164
165
- err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
165
+ err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
166166 BREAK_INSTR_SIZE);
167167 if (err)
168168 return err;
169
- err = probe_kernel_write((char *)bpt->bpt_addr,
169
+ err = copy_to_kernel_nofault((char *)bpt->bpt_addr,
170170 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
171171 return err;
172172 }
173
+NOKPROBE_SYMBOL(kgdb_arch_set_breakpoint);
173174
174175 int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
175176 {
176
- return probe_kernel_write((char *)bpt->bpt_addr,
177
+ return copy_to_kernel_nofault((char *)bpt->bpt_addr,
177178 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
178179 }
180
+NOKPROBE_SYMBOL(kgdb_arch_remove_breakpoint);
179181
180182 int __weak kgdb_validate_break_address(unsigned long addr)
181183 {
182184 struct kgdb_bkpt tmp;
183185 int err;
186
+
187
+ if (kgdb_within_blocklist(addr))
188
+ return -EINVAL;
189
+
184190 /* Validate setting the breakpoint and then removing it. If the
185191 * remove fails, the kernel needs to emit a bad message because we
186192 * are deep trouble not being able to put things back the way we
....@@ -201,6 +207,7 @@
201207 {
202208 return instruction_pointer(regs);
203209 }
210
+NOKPROBE_SYMBOL(kgdb_arch_pc);
204211
205212 int __weak kgdb_arch_init(void)
206213 {
....@@ -211,6 +218,65 @@
211218 {
212219 return 0;
213220 }
221
+NOKPROBE_SYMBOL(kgdb_skipexception);
222
+
223
+#ifdef CONFIG_SMP
224
+
225
+/*
226
+ * Default (weak) implementation for kgdb_roundup_cpus
227
+ */
228
+
229
+static DEFINE_PER_CPU(call_single_data_t, kgdb_roundup_csd);
230
+
231
+void __weak kgdb_call_nmi_hook(void *ignored)
232
+{
233
+ /*
234
+ * NOTE: get_irq_regs() is supposed to get the registers from
235
+ * before the IPI interrupt happened and so is supposed to
236
+ * show where the processor was. In some situations it's
237
+ * possible we might be called without an IPI, so it might be
238
+ * safer to figure out how to make kgdb_breakpoint() work
239
+ * properly here.
240
+ */
241
+ kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
242
+}
243
+NOKPROBE_SYMBOL(kgdb_call_nmi_hook);
244
+
245
+void __weak kgdb_roundup_cpus(void)
246
+{
247
+ call_single_data_t *csd;
248
+ int this_cpu = raw_smp_processor_id();
249
+ int cpu;
250
+ int ret;
251
+
252
+ for_each_online_cpu(cpu) {
253
+ /* No need to roundup ourselves */
254
+ if (cpu == this_cpu)
255
+ continue;
256
+
257
+ csd = &per_cpu(kgdb_roundup_csd, cpu);
258
+
259
+ /*
260
+ * If it didn't round up last time, don't try again
261
+ * since smp_call_function_single_async() will block.
262
+ *
263
+ * If rounding_up is false then we know that the
264
+ * previous call must have at least started and that
265
+ * means smp_call_function_single_async() won't block.
266
+ */
267
+ if (kgdb_info[cpu].rounding_up)
268
+ continue;
269
+ kgdb_info[cpu].rounding_up = true;
270
+
271
+ csd->func = kgdb_call_nmi_hook;
272
+ ret = smp_call_function_single_async(cpu, csd);
273
+ if (ret)
274
+ kgdb_info[cpu].rounding_up = false;
275
+ }
276
+}
277
+NOKPROBE_SYMBOL(kgdb_roundup_cpus);
278
+
279
+#endif
214280
215281 /*
216282 * Some architectures need cache flushes when we set/clear a
....@@ -235,6 +301,7 @@
235301 /* Force flush instruction cache if it was outside the mm */
236302 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
237303 }
304
+NOKPROBE_SYMBOL(kgdb_flush_swbreak_addr);
238305
239306 /*
240307 * SW breakpoint management:
....@@ -262,6 +329,7 @@
262329 }
263330 return ret;
264331 }
332
+NOKPROBE_SYMBOL(dbg_activate_sw_breakpoints);
265333
266334 int dbg_set_sw_break(unsigned long addr)
267335 {
....@@ -325,6 +393,7 @@
325393 }
326394 return ret;
327395 }
396
+NOKPROBE_SYMBOL(dbg_deactivate_sw_breakpoints);
328397
329398 int dbg_remove_sw_break(unsigned long addr)
330399 {
....@@ -347,6 +416,18 @@
347416 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
348417 if ((kgdb_break[i].state == BP_REMOVED) &&
349418 (kgdb_break[i].bpt_addr == addr))
419
+ return 1;
420
+ }
421
+ return 0;
422
+}
423
+
424
+int kgdb_has_hit_break(unsigned long addr)
425
+{
426
+ int i;
427
+
428
+ for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
429
+ if (kgdb_break[i].state == BP_ACTIVE &&
430
+ kgdb_break[i].bpt_addr == addr)
350431 return 1;
351432 }
352433 return 0;
....@@ -376,6 +457,48 @@
376457 return 0;
377458 }
378459
460
+void kgdb_free_init_mem(void)
461
+{
462
+ int i;
463
+
464
+ /* Clear init memory breakpoints. */
465
+ for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
466
+ if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))
467
+ kgdb_break[i].state = BP_UNDEFINED;
468
+ }
469
+}
470
+
471
+#ifdef CONFIG_KGDB_KDB
472
+void kdb_dump_stack_on_cpu(int cpu)
473
+{
474
+ if (cpu == raw_smp_processor_id() || !IS_ENABLED(CONFIG_SMP)) {
475
+ dump_stack();
476
+ return;
477
+ }
478
+
479
+ if (!(kgdb_info[cpu].exception_state & DCPU_IS_SLAVE)) {
480
+ kdb_printf("ERROR: Task on cpu %d didn't stop in the debugger\n",
481
+ cpu);
482
+ return;
483
+ }
484
+
485
+ /*
486
+ * In general, architectures don't support dumping the stack of a
487
+ * "running" process that's not the current one. From the point of
488
+ * view of the Linux, kernel processes that are looping in the kgdb
489
+ * slave loop are still "running". There's also no API (that actually
490
+ * works across all architectures) that can do a stack crawl based
491
+ * on registers passed as a parameter.
492
+ *
493
+ * Solve this conundrum by asking slave CPUs to do the backtrace
494
+ * themselves.
495
+ */
496
+ kgdb_info[cpu].exception_state |= DCPU_WANT_BT;
497
+ while (kgdb_info[cpu].exception_state & DCPU_WANT_BT)
498
+ cpu_relax();
499
+}
500
+#endif
501
+
379502 /*
380503 * Return true if there is a valid kgdb I/O module. Also if no
381504 * debugger is attached a message can be printed to the console about
....@@ -403,6 +526,7 @@
403526 }
404527 return 1;
405528 }
529
+NOKPROBE_SYMBOL(kgdb_io_ready);
406530
407531 static int kgdb_reenter_check(struct kgdb_state *ks)
408532 {
....@@ -450,6 +574,7 @@
450574
451575 return 1;
452576 }
577
+NOKPROBE_SYMBOL(kgdb_reenter_check);
453578
454579 static void dbg_touch_watchdogs(void)
455580 {
....@@ -457,6 +582,7 @@
457582 clocksource_touch_watchdog();
458583 rcu_cpu_stall_reset();
459584 }
585
+NOKPROBE_SYMBOL(dbg_touch_watchdogs);
460586
461587 static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
462588 int exception_state)
....@@ -517,6 +643,9 @@
517643 atomic_xchg(&kgdb_active, cpu);
518644 break;
519645 }
646
+ } else if (kgdb_info[cpu].exception_state & DCPU_WANT_BT) {
647
+ dump_stack();
648
+ kgdb_info[cpu].exception_state &= ~DCPU_WANT_BT;
520649 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
521650 if (!raw_spin_is_locked(&dbg_slave_lock))
522651 goto return_normal;
....@@ -593,7 +722,7 @@
593722
594723 /* Signal the other CPUs to enter kgdb_wait() */
595724 else if ((!kgdb_single_step) && kgdb_do_roundup)
596
- kgdb_roundup_cpus(flags);
725
+ kgdb_roundup_cpus();
597726 #endif
598727
599728 /*
....@@ -628,6 +757,29 @@
628757 continue;
629758 kgdb_connected = 0;
630759 } else {
760
+ /*
761
+ * This is a brutal way to interfere with the debugger
762
+ * and prevent gdb being used to poke at kernel memory.
763
+ * This could cause trouble if lockdown is applied when
764
+ * there is already an active gdb session. For now the
765
+ * answer is simply "don't do that". Typically lockdown
766
+ * *will* be applied before the debug core gets started
767
+ * so only developers using kgdb for fairly advanced
768
+ * early kernel debug can be biten by this. Hopefully
769
+ * they are sophisticated enough to take care of
770
+ * themselves, especially with help from the lockdown
771
+ * message printed on the console!
772
+ */
773
+ if (security_locked_down(LOCKDOWN_DBG_WRITE_KERNEL)) {
774
+ if (IS_ENABLED(CONFIG_KGDB_KDB)) {
775
+ /* Switch back to kdb if possible... */
776
+ dbg_kdb_mode = 1;
777
+ continue;
778
+ } else {
779
+ /* ... otherwise just bail */
780
+ break;
781
+ }
782
+ }
631783 error = gdb_serial_stub(ks);
632784 }
633785
....@@ -642,6 +794,8 @@
642794 break;
643795 }
644796 }
797
+
798
+ dbg_activate_sw_breakpoints();
645799
646800 /* Call the I/O driver's post_exception routine */
647801 if (dbg_io_ops->post_exception)
....@@ -685,6 +839,7 @@
685839
686840 return kgdb_info[cpu].ret_state;
687841 }
842
+NOKPROBE_SYMBOL(kgdb_cpu_enter);
688843
689844 /*
690845 * kgdb_handle_exception() - main entry point from a kernel exception
....@@ -729,13 +884,11 @@
729884 arch_kgdb_ops.enable_nmi(1);
730885 return ret;
731886 }
887
+NOKPROBE_SYMBOL(kgdb_handle_exception);
732888
733889 /*
734
- * GDB places a breakpoint at this function to know dynamically
735
- * loaded objects. It's not defined static so that only one instance with this
736
- * name exists in the kernel.
890
+ * GDB places a breakpoint at this function to know dynamically loaded objects.
737891 */
738
-
739892 static int module_event(struct notifier_block *self, unsigned long val,
740893 void *data)
741894 {
....@@ -752,6 +905,8 @@
752905 struct kgdb_state kgdb_var;
753906 struct kgdb_state *ks = &kgdb_var;
754907
908
+ kgdb_info[cpu].rounding_up = false;
909
+
755910 memset(ks, 0, sizeof(struct kgdb_state));
756911 ks->cpu = cpu;
757912 ks->linux_regs = regs;
....@@ -764,6 +919,7 @@
764919 #endif
765920 return 1;
766921 }
922
+NOKPROBE_SYMBOL(kgdb_nmicallback);
767923
768924 int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
769925 atomic_t *send_ready)
....@@ -789,6 +945,7 @@
789945 #endif
790946 return 1;
791947 }
948
+NOKPROBE_SYMBOL(kgdb_nmicallin);
792949
793950 static void kgdb_console_write(struct console *co, const char *s,
794951 unsigned count)
....@@ -845,36 +1002,39 @@
8451002 kgdb_breakpoint();
8461003 }
8471004
848
-static struct sysrq_key_op sysrq_dbg_op = {
1005
+static const struct sysrq_key_op sysrq_dbg_op = {
8491006 .handler = sysrq_handle_dbg,
8501007 .help_msg = "debug(g)",
8511008 .action_msg = "DEBUG",
8521009 };
8531010 #endif
8541011
855
-static int kgdb_panic_event(struct notifier_block *self,
856
- unsigned long val,
857
- void *data)
1012
+void kgdb_panic(const char *msg)
8581013 {
1014
+ if (!kgdb_io_module_registered)
1015
+ return;
1016
+
8591017 /*
860
- * Avoid entering the debugger if we were triggered due to a panic
861
- * We don't want to get stuck waiting for input from user in such case.
862
- * panic_timeout indicates the system should automatically
1018
+ * We don't want to get stuck waiting for input from user if
1019
+ * "panic_timeout" indicates the system should automatically
8631020 * reboot on panic.
8641021 */
8651022 if (panic_timeout)
866
- return NOTIFY_DONE;
1023
+ return;
8671024
8681025 if (dbg_kdb_mode)
869
- kdb_printf("PANIC: %s\n", (char *)data);
1026
+ kdb_printf("PANIC: %s\n", msg);
1027
+
8701028 kgdb_breakpoint();
871
- return NOTIFY_DONE;
8721029 }
8731030
874
-static struct notifier_block kgdb_panic_event_nb = {
875
- .notifier_call = kgdb_panic_event,
876
- .priority = INT_MAX,
877
-};
1031
+static void kgdb_initial_breakpoint(void)
1032
+{
1033
+ kgdb_break_asap = 0;
1034
+
1035
+ pr_crit("Waiting for connection from remote gdb...\n");
1036
+ kgdb_breakpoint();
1037
+}
8781038
8791039 void __weak kgdb_arch_late(void)
8801040 {
....@@ -886,6 +1046,9 @@
8861046 if (kgdb_io_module_registered)
8871047 kgdb_arch_late();
8881048 kdb_init(KDB_INIT_FULL);
1049
+
1050
+ if (kgdb_io_module_registered && kgdb_break_asap)
1051
+ kgdb_initial_breakpoint();
8891052 }
8901053
8911054 static int
....@@ -924,8 +1087,6 @@
9241087 kgdb_arch_late();
9251088 register_module_notifier(&dbg_module_load_nb);
9261089 register_reboot_notifier(&dbg_reboot_notifier);
927
- atomic_notifier_chain_register(&panic_notifier_list,
928
- &kgdb_panic_event_nb);
9291090 #ifdef CONFIG_MAGIC_SYSRQ
9301091 register_sysrq_key('g', &sysrq_dbg_op);
9311092 #endif
....@@ -939,16 +1100,14 @@
9391100 static void kgdb_unregister_callbacks(void)
9401101 {
9411102 /*
942
- * When this routine is called KGDB should unregister from the
943
- * panic handler and clean up, making sure it is not handling any
1103
+ * When this routine is called KGDB should unregister from
1104
+ * handlers and clean up, making sure it is not handling any
9441105 * break exceptions at the time.
9451106 */
9461107 if (kgdb_io_module_registered) {
9471108 kgdb_io_module_registered = 0;
9481109 unregister_reboot_notifier(&dbg_reboot_notifier);
9491110 unregister_module_notifier(&dbg_module_load_nb);
950
- atomic_notifier_chain_unregister(&panic_notifier_list,
951
- &kgdb_panic_event_nb);
9521111 kgdb_arch_exit();
9531112 #ifdef CONFIG_MAGIC_SYSRQ
9541113 unregister_sysrq_key('g', &sysrq_dbg_op);
....@@ -972,7 +1131,7 @@
9721131 atomic_set(&kgdb_break_tasklet_var, 0);
9731132 }
9741133
975
-static DECLARE_TASKLET(kgdb_tasklet_breakpoint, kgdb_tasklet_bpt, 0);
1134
+static DECLARE_TASKLET_OLD(kgdb_tasklet_breakpoint, kgdb_tasklet_bpt);
9761135
9771136 void kgdb_schedule_breakpoint(void)
9781137 {
....@@ -985,14 +1144,6 @@
9851144 }
9861145 EXPORT_SYMBOL_GPL(kgdb_schedule_breakpoint);
9871146
988
-static void kgdb_initial_breakpoint(void)
989
-{
990
- kgdb_break_asap = 0;
991
-
992
- pr_crit("Waiting for connection from remote gdb...\n");
993
- kgdb_breakpoint();
994
-}
995
-
9961147 /**
9971148 * kgdb_register_io_module - register KGDB IO module
9981149 * @new_dbg_io_ops: the io ops vector
....@@ -1001,15 +1152,22 @@
10011152 */
10021153 int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
10031154 {
1155
+ struct kgdb_io *old_dbg_io_ops;
10041156 int err;
10051157
10061158 spin_lock(&kgdb_registration_lock);
10071159
1008
- if (dbg_io_ops) {
1009
- spin_unlock(&kgdb_registration_lock);
1160
+ old_dbg_io_ops = dbg_io_ops;
1161
+ if (old_dbg_io_ops) {
1162
+ if (!old_dbg_io_ops->deinit) {
1163
+ spin_unlock(&kgdb_registration_lock);
10101164
1011
- pr_err("Another I/O driver is already registered with KGDB\n");
1012
- return -EBUSY;
1165
+ pr_err("KGDB I/O driver %s can't replace %s.\n",
1166
+ new_dbg_io_ops->name, old_dbg_io_ops->name);
1167
+ return -EBUSY;
1168
+ }
1169
+ pr_info("Replacing I/O driver %s with %s\n",
1170
+ old_dbg_io_ops->name, new_dbg_io_ops->name);
10131171 }
10141172
10151173 if (new_dbg_io_ops->init) {
....@@ -1024,12 +1182,18 @@
10241182
10251183 spin_unlock(&kgdb_registration_lock);
10261184
1185
+ if (old_dbg_io_ops) {
1186
+ old_dbg_io_ops->deinit();
1187
+ return 0;
1188
+ }
1189
+
10271190 pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name);
10281191
10291192 /* Arm KGDB now. */
10301193 kgdb_register_callbacks();
10311194
1032
- if (kgdb_break_asap)
1195
+ if (kgdb_break_asap &&
1196
+ (!dbg_is_early || IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG)))
10331197 kgdb_initial_breakpoint();
10341198
10351199 return 0;
....@@ -1058,6 +1222,9 @@
10581222 dbg_io_ops = NULL;
10591223
10601224 spin_unlock(&kgdb_registration_lock);
1225
+
1226
+ if (old_dbg_io_ops->deinit)
1227
+ old_dbg_io_ops->deinit();
10611228
10621229 pr_info("Unregistered I/O driver %s, debugger disabled\n",
10631230 old_dbg_io_ops->name);
....@@ -1099,7 +1266,8 @@
10991266 kgdb_break_asap = 1;
11001267
11011268 kdb_init(KDB_INIT_EARLY);
1102
- if (kgdb_io_module_registered)
1269
+ if (kgdb_io_module_registered &&
1270
+ IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG))
11031271 kgdb_initial_breakpoint();
11041272
11051273 return 0;