1 // SPDX-License-Identifier: GPL-2.0
3 * Based on the same principle as kgdboe using the NETPOLL api, this
4 * driver uses a console polling api to implement a gdb serial inteface
5 * which is multiplexed on a console port.
7 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
9 * 2007-2008 (c) Jason Wessel - Wind River Systems, Inc.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/ctype.h>
16 #include <linux/kgdb.h>
17 #include <linux/kdb.h>
18 #include <linux/tty.h>
19 #include <linux/console.h>
20 #include <linux/vt_kern.h>
21 #include <linux/input.h>
22 #include <linux/irq_work.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/serial_core.h>
27 #define MAX_CONFIG_LEN 40
29 static struct kgdb_io kgdboc_io_ops;
31 /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
32 static int configured = -1;
33 static DEFINE_MUTEX(config_mutex);
35 static char config[MAX_CONFIG_LEN];
36 static struct kparam_string kps = {
38 .maxlen = MAX_CONFIG_LEN,
41 static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
42 static struct tty_driver *kgdb_tty_driver;
43 static int kgdb_tty_line;
45 static struct platform_device *kgdboc_pdev;
47 #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
48 static struct kgdb_io kgdboc_earlycon_io_ops;
49 static int (*earlycon_orig_exit)(struct console *con);
50 #endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
53 * When we leave the debug trap handler we need to reset the keyboard status
54 * (since the original keyboard state gets partially clobbered by kdb use of
57 * The path to deliver the reset is somewhat circuitous.
59 * To deliver the reset we register an input handler, reset the keyboard and
60 * then deregister the input handler. However, to get this done right, we do
61 * have to carefully manage the calling context because we can only register
62 * input handlers from task context.
64 * In particular we need to trigger the action from the debug trap handler with
65 * all its NMI and/or NMI-like oddities. To solve this the kgdboc trap exit code
66 * (the "post_exception" callback) uses irq_work_queue(), which is NMI-safe, to
67 * schedule a callback from a hardirq context. From there we have to defer the
68 * work again, this time using schedule_work(), to get a callback using the
69 * system workqueue, which runs in task context.
71 #ifdef CONFIG_KDB_KEYBOARD
72 static int kgdboc_reset_connect(struct input_handler *handler,
73 struct input_dev *dev,
74 const struct input_device_id *id)
76 input_reset_device(dev);
78 /* Return an error - we do not want to bind, just to reset */
82 static void kgdboc_reset_disconnect(struct input_handle *handle)
84 /* We do not expect anyone to actually bind to us */
88 static const struct input_device_id kgdboc_reset_ids[] = {
90 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
91 .evbit = { BIT_MASK(EV_KEY) },
96 static struct input_handler kgdboc_reset_handler = {
97 .connect = kgdboc_reset_connect,
98 .disconnect = kgdboc_reset_disconnect,
99 .name = "kgdboc_reset",
100 .id_table = kgdboc_reset_ids,
103 static DEFINE_MUTEX(kgdboc_reset_mutex);
105 static void kgdboc_restore_input_helper(struct work_struct *dummy)
108 * We need to take a mutex to prevent several instances of
109 * this work running on different CPUs so they don't try
110 * to register again already registered handler.
112 mutex_lock(&kgdboc_reset_mutex);
114 if (input_register_handler(&kgdboc_reset_handler) == 0)
115 input_unregister_handler(&kgdboc_reset_handler);
117 mutex_unlock(&kgdboc_reset_mutex);
120 static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
122 static void kgdboc_queue_restore_input_helper(struct irq_work *unused)
124 schedule_work(&kgdboc_restore_input_work);
127 static DEFINE_IRQ_WORK(kgdboc_restore_input_irq_work, kgdboc_queue_restore_input_helper);
129 static void kgdboc_restore_input(void)
131 if (likely(system_state == SYSTEM_RUNNING))
132 irq_work_queue(&kgdboc_restore_input_irq_work);
135 static int kgdboc_register_kbd(char **cptr)
137 if (strncmp(*cptr, "kbd", 3) == 0 ||
138 strncmp(*cptr, "kdb", 3) == 0) {
139 if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
140 kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
142 if (cptr[0][3] == ',')
151 static void kgdboc_unregister_kbd(void)
155 for (i = 0; i < kdb_poll_idx; i++) {
156 if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
158 kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
159 kdb_poll_funcs[kdb_poll_idx] = NULL;
163 irq_work_sync(&kgdboc_restore_input_irq_work);
164 flush_work(&kgdboc_restore_input_work);
166 #else /* ! CONFIG_KDB_KEYBOARD */
167 #define kgdboc_register_kbd(x) 0
168 #define kgdboc_unregister_kbd()
169 #define kgdboc_restore_input()
170 #endif /* ! CONFIG_KDB_KEYBOARD */
172 #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
173 static void cleanup_earlycon(void)
175 if (kgdboc_earlycon_io_ops.cons)
176 kgdb_unregister_io_module(&kgdboc_earlycon_io_ops);
178 #else /* !IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
179 static inline void cleanup_earlycon(void) { }
180 #endif /* !IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
182 static void cleanup_kgdboc(void)
189 if (kgdb_unregister_nmi_console())
191 kgdboc_unregister_kbd();
192 kgdb_unregister_io_module(&kgdboc_io_ops);
195 static int configure_kgdboc(void)
197 struct tty_driver *p;
201 struct console *cons;
203 if (!strlen(config) || isspace(config[0])) {
208 kgdboc_io_ops.cons = NULL;
209 kgdb_tty_driver = NULL;
212 if (strncmp(cptr, "kms,", 4) == 0) {
217 if (kgdboc_register_kbd(&cptr))
220 p = tty_find_polling_driver(cptr, &tty_line);
224 for_each_console(cons) {
226 if (cons->device && cons->device(cons, &idx) == p &&
228 kgdboc_io_ops.cons = cons;
234 kgdb_tty_line = tty_line;
237 err = kgdb_register_io_module(&kgdboc_io_ops);
241 err = kgdb_register_nmi_console();
250 kgdb_unregister_io_module(&kgdboc_io_ops);
252 kgdboc_unregister_kbd();
258 static int kgdboc_probe(struct platform_device *pdev)
262 mutex_lock(&config_mutex);
263 if (configured != 1) {
264 ret = configure_kgdboc();
266 /* Convert "no device" to "defer" so we'll keep trying */
270 mutex_unlock(&config_mutex);
275 static struct platform_driver kgdboc_platform_driver = {
276 .probe = kgdboc_probe,
279 .suppress_bind_attrs = true,
283 static int __init init_kgdboc(void)
288 * kgdboc is a little bit of an odd "platform_driver". It can be
289 * up and running long before the platform_driver object is
290 * created and thus doesn't actually store anything in it. There's
291 * only one instance of kgdb so anything is stored as global state.
292 * The platform_driver is only created so that we can leverage the
293 * kernel's mechanisms (like -EPROBE_DEFER) to call us when our
294 * underlying tty is ready. Here we init our platform driver and
295 * then create the single kgdboc instance.
297 ret = platform_driver_register(&kgdboc_platform_driver);
301 kgdboc_pdev = platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE);
304 goto err_did_register;
307 ret = platform_device_add(kgdboc_pdev);
311 platform_device_put(kgdboc_pdev);
314 platform_driver_unregister(&kgdboc_platform_driver);
318 static void exit_kgdboc(void)
320 mutex_lock(&config_mutex);
322 mutex_unlock(&config_mutex);
324 platform_device_unregister(kgdboc_pdev);
325 platform_driver_unregister(&kgdboc_platform_driver);
328 static int kgdboc_get_char(void)
330 if (!kgdb_tty_driver)
332 return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
336 static void kgdboc_put_char(u8 chr)
338 if (!kgdb_tty_driver)
340 kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
344 static int param_set_kgdboc_var(const char *kmessage,
345 const struct kernel_param *kp)
347 size_t len = strlen(kmessage);
350 if (len >= MAX_CONFIG_LEN) {
351 pr_err("config string too long\n");
355 if (kgdb_connected) {
356 pr_err("Cannot reconfigure while KGDB is connected.\n");
360 mutex_lock(&config_mutex);
362 strcpy(config, kmessage);
363 /* Chop out \n char as a result of echo */
364 if (len && config[len - 1] == '\n')
365 config[len - 1] = '\0';
371 * Configure with the new params as long as init already ran.
372 * Note that we can get called before init if someone loads us
373 * with "modprobe kgdboc kgdboc=..." or if they happen to use the
374 * the odd syntax of "kgdboc.kgdboc=..." on the kernel command.
377 ret = configure_kgdboc();
380 * If we couldn't configure then clear out the config. Note that
381 * specifying an invalid config on the kernel command line vs.
382 * through sysfs have slightly different behaviors. If we fail
383 * to configure what was specified on the kernel command line
384 * we'll leave it in the 'config' and return -EPROBE_DEFER from
385 * our probe. When specified through sysfs userspace is
386 * responsible for loading the tty driver before setting up.
391 mutex_unlock(&config_mutex);
396 static int dbg_restore_graphics;
398 static void kgdboc_pre_exp_handler(void)
400 if (!dbg_restore_graphics && kgdboc_use_kms) {
401 dbg_restore_graphics = 1;
402 con_debug_enter(vc_cons[fg_console].d);
404 /* Increment the module count when the debugger is active */
406 try_module_get(THIS_MODULE);
409 static void kgdboc_post_exp_handler(void)
411 /* decrement the module count when the debugger detaches */
413 module_put(THIS_MODULE);
414 if (kgdboc_use_kms && dbg_restore_graphics) {
415 dbg_restore_graphics = 0;
418 kgdboc_restore_input();
421 static struct kgdb_io kgdboc_io_ops = {
423 .read_char = kgdboc_get_char,
424 .write_char = kgdboc_put_char,
425 .pre_exception = kgdboc_pre_exp_handler,
426 .post_exception = kgdboc_post_exp_handler,
429 #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
430 static int kgdboc_option_setup(char *opt)
433 pr_err("config string not provided\n");
437 if (strlen(opt) >= MAX_CONFIG_LEN) {
438 pr_err("config string too long\n");
446 __setup("kgdboc=", kgdboc_option_setup);
449 /* This is only available if kgdboc is a built in for early debugging */
450 static int __init kgdboc_early_init(char *opt)
452 kgdboc_option_setup(opt);
457 early_param("ekgdboc", kgdboc_early_init);
459 static int kgdboc_earlycon_get_char(void)
463 if (!kgdboc_earlycon_io_ops.cons->read(kgdboc_earlycon_io_ops.cons,
470 static void kgdboc_earlycon_put_char(u8 chr)
472 kgdboc_earlycon_io_ops.cons->write(kgdboc_earlycon_io_ops.cons, &chr,
476 static void kgdboc_earlycon_pre_exp_handler(void)
479 static bool already_warned;
485 * When the first normal console comes up the kernel will take all
486 * the boot consoles out of the list. Really, we should stop using
487 * the boot console when it does that but until a TTY is registered
488 * we have no other choice so we keep using it. Since not all
489 * serial drivers might be OK with this, print a warning once per
490 * boot if we detect this case.
492 for_each_console(con)
493 if (con == kgdboc_earlycon_io_ops.cons)
496 already_warned = true;
497 pr_warn("kgdboc_earlycon is still using bootconsole\n");
500 static int kgdboc_earlycon_deferred_exit(struct console *con)
503 * If we get here it means the boot console is going away but we
504 * don't yet have a suitable replacement. Don't pass through to
505 * the original exit routine. We'll call it later in our deinit()
506 * function. For now, restore the original exit() function pointer
507 * as a sentinal that we've hit this point.
509 con->exit = earlycon_orig_exit;
514 static void kgdboc_earlycon_deinit(void)
516 if (!kgdboc_earlycon_io_ops.cons)
519 if (kgdboc_earlycon_io_ops.cons->exit == kgdboc_earlycon_deferred_exit)
521 * kgdboc_earlycon is exiting but original boot console exit
522 * was never called (AKA kgdboc_earlycon_deferred_exit()
523 * didn't ever run). Undo our trap.
525 kgdboc_earlycon_io_ops.cons->exit = earlycon_orig_exit;
526 else if (kgdboc_earlycon_io_ops.cons->exit)
528 * We skipped calling the exit() routine so we could try to
529 * keep using the boot console even after it went away. We're
530 * finally done so call the function now.
532 kgdboc_earlycon_io_ops.cons->exit(kgdboc_earlycon_io_ops.cons);
534 kgdboc_earlycon_io_ops.cons = NULL;
537 static struct kgdb_io kgdboc_earlycon_io_ops = {
538 .name = "kgdboc_earlycon",
539 .read_char = kgdboc_earlycon_get_char,
540 .write_char = kgdboc_earlycon_put_char,
541 .pre_exception = kgdboc_earlycon_pre_exp_handler,
542 .deinit = kgdboc_earlycon_deinit,
545 #define MAX_CONSOLE_NAME_LEN (sizeof((struct console *) 0)->name)
546 static char kgdboc_earlycon_param[MAX_CONSOLE_NAME_LEN] __initdata;
547 static bool kgdboc_earlycon_late_enable __initdata;
549 static int __init kgdboc_earlycon_init(char *opt)
553 kdb_init(KDB_INIT_EARLY);
556 * Look for a matching console, or if the name was left blank just
557 * pick the first one we find.
560 for_each_console(con) {
561 if (con->write && con->read &&
562 (con->flags & (CON_BOOT | CON_ENABLED)) &&
563 (!opt || !opt[0] || strcmp(con->name, opt) == 0))
569 * Both earlycon and kgdboc_earlycon are initialized during
570 * early parameter parsing. We cannot guarantee earlycon gets
571 * in first and, in any case, on ACPI systems earlycon may
572 * defer its own initialization (usually to somewhere within
573 * setup_arch() ). To cope with either of these situations
574 * we can defer our own initialization to a little later in
577 if (!kgdboc_earlycon_late_enable) {
578 pr_info("No suitable earlycon yet, will try later\n");
580 strscpy(kgdboc_earlycon_param, opt,
581 sizeof(kgdboc_earlycon_param));
582 kgdboc_earlycon_late_enable = true;
584 pr_info("Couldn't find kgdb earlycon\n");
589 kgdboc_earlycon_io_ops.cons = con;
590 pr_info("Going to register kgdb with earlycon '%s'\n", con->name);
591 if (kgdb_register_io_module(&kgdboc_earlycon_io_ops) != 0) {
592 kgdboc_earlycon_io_ops.cons = NULL;
593 pr_info("Failed to register kgdb with earlycon\n");
595 /* Trap exit so we can keep earlycon longer if needed. */
596 earlycon_orig_exit = con->exit;
597 con->exit = kgdboc_earlycon_deferred_exit;
603 /* Non-zero means malformed option so we always return zero */
607 early_param("kgdboc_earlycon", kgdboc_earlycon_init);
610 * This is only intended for the late adoption of an early console.
612 * It is not a reliable way to adopt regular consoles because we can not
613 * control what order console initcalls are made and, in any case, many
614 * regular consoles are registered much later in the boot process than
615 * the console initcalls!
617 static int __init kgdboc_earlycon_late_init(void)
619 if (kgdboc_earlycon_late_enable)
620 kgdboc_earlycon_init(kgdboc_earlycon_param);
623 console_initcall(kgdboc_earlycon_late_init);
625 #endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
627 module_init(init_kgdboc);
628 module_exit(exit_kgdboc);
629 module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
630 MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
631 MODULE_DESCRIPTION("KGDB Console TTY Driver");
632 MODULE_LICENSE("GPL");