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.
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/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/serial_core.h>
26 #define MAX_CONFIG_LEN 40
28 static struct kgdb_io kgdboc_io_ops;
30 /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
31 static int configured = -1;
32 static DEFINE_MUTEX(config_mutex);
34 static char config[MAX_CONFIG_LEN];
35 static struct kparam_string kps = {
37 .maxlen = MAX_CONFIG_LEN,
40 static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
41 static struct tty_driver *kgdb_tty_driver;
42 static int kgdb_tty_line;
44 static struct platform_device *kgdboc_pdev;
46 #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
47 static struct kgdb_io kgdboc_earlycon_io_ops;
48 static int (*earlycon_orig_exit)(struct console *con);
49 #endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
51 #ifdef CONFIG_KDB_KEYBOARD
52 static int kgdboc_reset_connect(struct input_handler *handler,
53 struct input_dev *dev,
54 const struct input_device_id *id)
56 input_reset_device(dev);
58 /* Return an error - we do not want to bind, just to reset */
62 static void kgdboc_reset_disconnect(struct input_handle *handle)
64 /* We do not expect anyone to actually bind to us */
68 static const struct input_device_id kgdboc_reset_ids[] = {
70 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
71 .evbit = { BIT_MASK(EV_KEY) },
76 static struct input_handler kgdboc_reset_handler = {
77 .connect = kgdboc_reset_connect,
78 .disconnect = kgdboc_reset_disconnect,
79 .name = "kgdboc_reset",
80 .id_table = kgdboc_reset_ids,
83 static DEFINE_MUTEX(kgdboc_reset_mutex);
85 static void kgdboc_restore_input_helper(struct work_struct *dummy)
88 * We need to take a mutex to prevent several instances of
89 * this work running on different CPUs so they don't try
90 * to register again already registered handler.
92 mutex_lock(&kgdboc_reset_mutex);
94 if (input_register_handler(&kgdboc_reset_handler) == 0)
95 input_unregister_handler(&kgdboc_reset_handler);
97 mutex_unlock(&kgdboc_reset_mutex);
100 static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
102 static void kgdboc_restore_input(void)
104 if (likely(system_state == SYSTEM_RUNNING))
105 schedule_work(&kgdboc_restore_input_work);
108 static int kgdboc_register_kbd(char **cptr)
110 if (strncmp(*cptr, "kbd", 3) == 0 ||
111 strncmp(*cptr, "kdb", 3) == 0) {
112 if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
113 kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
115 if (cptr[0][3] == ',')
124 static void kgdboc_unregister_kbd(void)
128 for (i = 0; i < kdb_poll_idx; i++) {
129 if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
131 kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
132 kdb_poll_funcs[kdb_poll_idx] = NULL;
136 flush_work(&kgdboc_restore_input_work);
138 #else /* ! CONFIG_KDB_KEYBOARD */
139 #define kgdboc_register_kbd(x) 0
140 #define kgdboc_unregister_kbd()
141 #define kgdboc_restore_input()
142 #endif /* ! CONFIG_KDB_KEYBOARD */
144 #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
145 static void cleanup_earlycon(void)
147 if (kgdboc_earlycon_io_ops.cons)
148 kgdb_unregister_io_module(&kgdboc_earlycon_io_ops);
150 #else /* !IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
151 static inline void cleanup_earlycon(void) { }
152 #endif /* !IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
154 static void cleanup_kgdboc(void)
161 if (kgdb_unregister_nmi_console())
163 kgdboc_unregister_kbd();
164 kgdb_unregister_io_module(&kgdboc_io_ops);
167 static int configure_kgdboc(void)
169 struct tty_driver *p;
173 struct console *cons;
176 if (!strlen(config) || isspace(config[0])) {
181 kgdboc_io_ops.cons = NULL;
182 kgdb_tty_driver = NULL;
185 if (strncmp(cptr, "kms,", 4) == 0) {
190 if (kgdboc_register_kbd(&cptr))
193 p = tty_find_polling_driver(cptr, &tty_line);
198 * Take console_lock to serialize device() callback with
199 * other console operations. For example, fg_console is
200 * modified under console_lock when switching vt.
204 cookie = console_srcu_read_lock();
205 for_each_console_srcu(cons) {
207 if (cons->device && cons->device(cons, &idx) == p &&
209 kgdboc_io_ops.cons = cons;
213 console_srcu_read_unlock(cookie);
218 kgdb_tty_line = tty_line;
221 err = kgdb_register_io_module(&kgdboc_io_ops);
225 err = kgdb_register_nmi_console();
234 kgdb_unregister_io_module(&kgdboc_io_ops);
236 kgdboc_unregister_kbd();
242 static int kgdboc_probe(struct platform_device *pdev)
246 mutex_lock(&config_mutex);
247 if (configured != 1) {
248 ret = configure_kgdboc();
250 /* Convert "no device" to "defer" so we'll keep trying */
254 mutex_unlock(&config_mutex);
259 static struct platform_driver kgdboc_platform_driver = {
260 .probe = kgdboc_probe,
263 .suppress_bind_attrs = true,
267 static int __init init_kgdboc(void)
272 * kgdboc is a little bit of an odd "platform_driver". It can be
273 * up and running long before the platform_driver object is
274 * created and thus doesn't actually store anything in it. There's
275 * only one instance of kgdb so anything is stored as global state.
276 * The platform_driver is only created so that we can leverage the
277 * kernel's mechanisms (like -EPROBE_DEFER) to call us when our
278 * underlying tty is ready. Here we init our platform driver and
279 * then create the single kgdboc instance.
281 ret = platform_driver_register(&kgdboc_platform_driver);
285 kgdboc_pdev = platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE);
288 goto err_did_register;
291 ret = platform_device_add(kgdboc_pdev);
295 platform_device_put(kgdboc_pdev);
298 platform_driver_unregister(&kgdboc_platform_driver);
302 static void exit_kgdboc(void)
304 mutex_lock(&config_mutex);
306 mutex_unlock(&config_mutex);
308 platform_device_unregister(kgdboc_pdev);
309 platform_driver_unregister(&kgdboc_platform_driver);
312 static int kgdboc_get_char(void)
314 if (!kgdb_tty_driver)
316 return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
320 static void kgdboc_put_char(u8 chr)
322 if (!kgdb_tty_driver)
324 kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
328 static int param_set_kgdboc_var(const char *kmessage,
329 const struct kernel_param *kp)
331 size_t len = strlen(kmessage);
334 if (len >= MAX_CONFIG_LEN) {
335 pr_err("config string too long\n");
339 if (kgdb_connected) {
340 pr_err("Cannot reconfigure while KGDB is connected.\n");
344 mutex_lock(&config_mutex);
346 strcpy(config, kmessage);
347 /* Chop out \n char as a result of echo */
348 if (len && config[len - 1] == '\n')
349 config[len - 1] = '\0';
355 * Configure with the new params as long as init already ran.
356 * Note that we can get called before init if someone loads us
357 * with "modprobe kgdboc kgdboc=..." or if they happen to use
358 * the odd syntax of "kgdboc.kgdboc=..." on the kernel command.
361 ret = configure_kgdboc();
364 * If we couldn't configure then clear out the config. Note that
365 * specifying an invalid config on the kernel command line vs.
366 * through sysfs have slightly different behaviors. If we fail
367 * to configure what was specified on the kernel command line
368 * we'll leave it in the 'config' and return -EPROBE_DEFER from
369 * our probe. When specified through sysfs userspace is
370 * responsible for loading the tty driver before setting up.
375 mutex_unlock(&config_mutex);
380 static int dbg_restore_graphics;
382 static void kgdboc_pre_exp_handler(void)
384 if (!dbg_restore_graphics && kgdboc_use_kms) {
385 dbg_restore_graphics = 1;
386 con_debug_enter(vc_cons[fg_console].d);
388 /* Increment the module count when the debugger is active */
390 try_module_get(THIS_MODULE);
393 static void kgdboc_post_exp_handler(void)
395 /* decrement the module count when the debugger detaches */
397 module_put(THIS_MODULE);
398 if (kgdboc_use_kms && dbg_restore_graphics) {
399 dbg_restore_graphics = 0;
402 kgdboc_restore_input();
405 static struct kgdb_io kgdboc_io_ops = {
407 .read_char = kgdboc_get_char,
408 .write_char = kgdboc_put_char,
409 .pre_exception = kgdboc_pre_exp_handler,
410 .post_exception = kgdboc_post_exp_handler,
413 #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
414 static int kgdboc_option_setup(char *opt)
417 pr_err("config string not provided\n");
421 if (strlen(opt) >= MAX_CONFIG_LEN) {
422 pr_err("config string too long\n");
430 __setup("kgdboc=", kgdboc_option_setup);
433 /* This is only available if kgdboc is a built in for early debugging */
434 static int __init kgdboc_early_init(char *opt)
436 kgdboc_option_setup(opt);
441 early_param("ekgdboc", kgdboc_early_init);
443 static int kgdboc_earlycon_get_char(void)
447 if (!kgdboc_earlycon_io_ops.cons->read(kgdboc_earlycon_io_ops.cons,
454 static void kgdboc_earlycon_put_char(u8 chr)
456 kgdboc_earlycon_io_ops.cons->write(kgdboc_earlycon_io_ops.cons, &chr,
460 static void kgdboc_earlycon_pre_exp_handler(void)
463 static bool already_warned;
470 * When the first normal console comes up the kernel will take all
471 * the boot consoles out of the list. Really, we should stop using
472 * the boot console when it does that but until a TTY is registered
473 * we have no other choice so we keep using it. Since not all
474 * serial drivers might be OK with this, print a warning once per
475 * boot if we detect this case.
477 cookie = console_srcu_read_lock();
478 for_each_console_srcu(con) {
479 if (con == kgdboc_earlycon_io_ops.cons)
482 console_srcu_read_unlock(cookie);
486 already_warned = true;
487 pr_warn("kgdboc_earlycon is still using bootconsole\n");
490 static int kgdboc_earlycon_deferred_exit(struct console *con)
493 * If we get here it means the boot console is going away but we
494 * don't yet have a suitable replacement. Don't pass through to
495 * the original exit routine. We'll call it later in our deinit()
496 * function. For now, restore the original exit() function pointer
497 * as a sentinal that we've hit this point.
499 con->exit = earlycon_orig_exit;
504 static void kgdboc_earlycon_deinit(void)
506 if (!kgdboc_earlycon_io_ops.cons)
509 if (kgdboc_earlycon_io_ops.cons->exit == kgdboc_earlycon_deferred_exit)
511 * kgdboc_earlycon is exiting but original boot console exit
512 * was never called (AKA kgdboc_earlycon_deferred_exit()
513 * didn't ever run). Undo our trap.
515 kgdboc_earlycon_io_ops.cons->exit = earlycon_orig_exit;
516 else if (kgdboc_earlycon_io_ops.cons->exit)
518 * We skipped calling the exit() routine so we could try to
519 * keep using the boot console even after it went away. We're
520 * finally done so call the function now.
522 kgdboc_earlycon_io_ops.cons->exit(kgdboc_earlycon_io_ops.cons);
524 kgdboc_earlycon_io_ops.cons = NULL;
527 static struct kgdb_io kgdboc_earlycon_io_ops = {
528 .name = "kgdboc_earlycon",
529 .read_char = kgdboc_earlycon_get_char,
530 .write_char = kgdboc_earlycon_put_char,
531 .pre_exception = kgdboc_earlycon_pre_exp_handler,
532 .deinit = kgdboc_earlycon_deinit,
535 #define MAX_CONSOLE_NAME_LEN (sizeof((struct console *) 0)->name)
536 static char kgdboc_earlycon_param[MAX_CONSOLE_NAME_LEN] __initdata;
537 static bool kgdboc_earlycon_late_enable __initdata;
539 static int __init kgdboc_earlycon_init(char *opt)
543 kdb_init(KDB_INIT_EARLY);
546 * Look for a matching console, or if the name was left blank just
547 * pick the first one we find.
551 * Hold the console_list_lock to guarantee that no consoles are
552 * unregistered until the kgdboc_earlycon setup is complete.
553 * Trapping the exit() callback relies on exit() not being
554 * called until the trap is setup. This also allows safe
555 * traversal of the console list and race-free reading of @flags.
558 for_each_console(con) {
559 if (con->write && con->read &&
560 (con->flags & (CON_BOOT | CON_ENABLED)) &&
561 (!opt || !opt[0] || strcmp(con->name, opt) == 0))
567 * Both earlycon and kgdboc_earlycon are initialized during
568 * early parameter parsing. We cannot guarantee earlycon gets
569 * in first and, in any case, on ACPI systems earlycon may
570 * defer its own initialization (usually to somewhere within
571 * setup_arch() ). To cope with either of these situations
572 * we can defer our own initialization to a little later in
575 if (!kgdboc_earlycon_late_enable) {
576 pr_info("No suitable earlycon yet, will try later\n");
578 strscpy(kgdboc_earlycon_param, opt,
579 sizeof(kgdboc_earlycon_param));
580 kgdboc_earlycon_late_enable = true;
582 pr_info("Couldn't find kgdb earlycon\n");
587 kgdboc_earlycon_io_ops.cons = con;
588 pr_info("Going to register kgdb with earlycon '%s'\n", con->name);
589 if (kgdb_register_io_module(&kgdboc_earlycon_io_ops) != 0) {
590 kgdboc_earlycon_io_ops.cons = NULL;
591 pr_info("Failed to register kgdb with earlycon\n");
593 /* Trap exit so we can keep earlycon longer if needed. */
594 earlycon_orig_exit = con->exit;
595 con->exit = kgdboc_earlycon_deferred_exit;
599 console_list_unlock();
601 /* Non-zero means malformed option so we always return zero */
605 early_param("kgdboc_earlycon", kgdboc_earlycon_init);
608 * This is only intended for the late adoption of an early console.
610 * It is not a reliable way to adopt regular consoles because we can not
611 * control what order console initcalls are made and, in any case, many
612 * regular consoles are registered much later in the boot process than
613 * the console initcalls!
615 static int __init kgdboc_earlycon_late_init(void)
617 if (kgdboc_earlycon_late_enable)
618 kgdboc_earlycon_init(kgdboc_earlycon_param);
621 console_initcall(kgdboc_earlycon_late_init);
623 #endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
625 module_init(init_kgdboc);
626 module_exit(exit_kgdboc);
627 module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
628 MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
629 MODULE_DESCRIPTION("KGDB Console TTY Driver");
630 MODULE_LICENSE("GPL");