Skip to content

Commit 2209956

Browse files
diandersDaniel Thompson
authored andcommitted
kgdboc: Add kgdboc_earlycon to support early kgdb using boot consoles
We want to enable kgdb to debug the early parts of the kernel. Unfortunately kgdb normally is a client of the tty API in the kernel and serial drivers don't register to the tty layer until fairly late in the boot process. Serial drivers do, however, commonly register a boot console. Let's enable the kgdboc driver to work with boot consoles to provide early debugging. This change co-opts the existing read() function pointer that's part of "struct console". It's assumed that if a boot console (with the flag CON_BOOT) has implemented read() that both the read() and write() function are polling functions. That means they work without interrupts and read() will return immediately (with 0 bytes read) if there's nothing to read. This should be a safe assumption since it appears that no current boot consoles implement read() right now and there seems no reason to do so unless they wanted to support "kgdboc_earlycon". The normal/expected way to make all this work is to use "kgdboc_earlycon" and "kgdboc" together. You should point them both to the same physical serial connection. At boot time, as the system transitions from the boot console to the normal console (and registers a tty), kgdb will switch over. One awkward part of all this, though, is that there can be a window where the boot console goes away and we can't quite transtion over to the main kgdboc that uses the tty layer. There are two main problems: 1. The act of registering the tty doesn't cause any call into kgdboc so there is a window of time when the tty is there but kgdboc's init code hasn't been called so we can't transition to it. 2. On some serial drivers the normal console inits (and replaces the boot console) quite early in the system. Presumably these drivers were coded up before earlycon worked as well as it does today and probably they don't need to do this anymore, but it causes us problems nontheless. Problem #1 is not too big of a deal somewhat due to the luck of probe ordering. kgdboc is last in the tty/serial/Makefile so its probe gets right after all other tty devices. It's not fun to rely on this, but it does work for the most part. Problem #2 is a big deal, but only for some serial drivers. Other serial drivers end up registering the console (which gets rid of the boot console) and tty at nearly the same time. The way we'll deal with the window when the system has stopped using the boot console and the time when we're setup using the tty is to keep using the boot console. This may sound surprising, but it has been found to work well in practice. If it doesn't work, it shouldn't be too hard for a given serial driver to make it keep working. Specifically, it's expected that the read()/write() function provided in the boot console should be the same (or nearly the same) as the normal kgdb polling functions. That means continuing to use them should work just fine. To make things even more likely to work work we'll also trap the recently added exit() function in the boot console we're using and delay any calls to it until we're all done with the boot console. NOTE: there could be ways to use all this in weird / unexpected ways. If you do something like this, it's a bit of a buyer beware situation. Specifically: - If you specify only "kgdboc_earlycon" but not "kgdboc" then (depending on your serial driver) things will probably work OK, but you'll get a warning printed the first time you use kgdb after the boot console is gone. You'd only be able to do this, of course, if the serial driver you're running atop provided an early boot console. - If your "kgdboc_earlycon" and "kgdboc" devices are not the same device things should work OK, but it'll be your job to switch over which device you're monitoring (including figuring out how to switch over gdb in-flight if you're using it). When trying to enable "kgdboc_earlycon" it should be noted that the names that are registered through the boot console layer and the tty layer are not the same for the same port. For example when debugging on one board I'd need to pass "kgdboc_earlycon=qcom_geni kgdboc=ttyMSM0" to enable things properly. Since digging up the boot console name is a pain and there will rarely be more than one boot console enabled, you can provide the "kgdboc_earlycon" parameter without specifying the name of the boot console. In this case we'll just pick the first boot that implements read() that we find. This new "kgdboc_earlycon" parameter should be contrasted to the existing "ekgdboc" parameter. While both provide a way to debug very early, the usage and mechanisms are quite different. Specifically "kgdboc_earlycon" is meant to be used in tandem with "kgdboc" and there is a transition from one to the other. The "ekgdboc" parameter, on the other hand, replaces the "kgdboc" parameter. It runs the same logic as the "kgdboc" parameter but just relies on your TTY driver being present super early. The only known usage of the old "ekgdboc" parameter is documented as "ekgdboc=kbd earlyprintk=vga". It should be noted that "kbd" has special treatment allowing it to init early as a tty device. Signed-off-by: Douglas Anderson <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Tested-by: Sumit Garg <[email protected]> Link: https://lore.kernel.org/r/20200507130644.v4.8.I8fba5961bf452ab92350654aa61957f23ecf0100@changeid Signed-off-by: Daniel Thompson <[email protected]>
1 parent eae3e19 commit 2209956

File tree

3 files changed

+158
-4
lines changed

3 files changed

+158
-4
lines changed

drivers/tty/serial/kgdboc.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/input.h>
2222
#include <linux/module.h>
2323
#include <linux/platform_device.h>
24+
#include <linux/serial_core.h>
2425

2526
#define MAX_CONFIG_LEN 40
2627

@@ -42,6 +43,10 @@ static int kgdb_tty_line;
4243

4344
static struct platform_device *kgdboc_pdev;
4445

46+
static struct kgdb_io kgdboc_earlycon_io_ops;
47+
static struct console *earlycon;
48+
static int (*earlycon_orig_exit)(struct console *con);
49+
4550
#ifdef CONFIG_KDB_KEYBOARD
4651
static int kgdboc_reset_connect(struct input_handler *handler,
4752
struct input_dev *dev,
@@ -137,6 +142,9 @@ static void kgdboc_unregister_kbd(void)
137142

138143
static void cleanup_kgdboc(void)
139144
{
145+
if (earlycon)
146+
kgdb_unregister_io_module(&kgdboc_earlycon_io_ops);
147+
140148
if (configured != 1)
141149
return;
142150

@@ -409,6 +417,134 @@ static int __init kgdboc_early_init(char *opt)
409417

410418
early_param("ekgdboc", kgdboc_early_init);
411419

420+
static int kgdboc_earlycon_get_char(void)
421+
{
422+
char c;
423+
424+
if (!earlycon->read(earlycon, &c, 1))
425+
return NO_POLL_CHAR;
426+
427+
return c;
428+
}
429+
430+
static void kgdboc_earlycon_put_char(u8 chr)
431+
{
432+
earlycon->write(earlycon, &chr, 1);
433+
}
434+
435+
static void kgdboc_earlycon_pre_exp_handler(void)
436+
{
437+
struct console *con;
438+
static bool already_warned;
439+
440+
if (already_warned)
441+
return;
442+
443+
/*
444+
* When the first normal console comes up the kernel will take all
445+
* the boot consoles out of the list. Really, we should stop using
446+
* the boot console when it does that but until a TTY is registered
447+
* we have no other choice so we keep using it. Since not all
448+
* serial drivers might be OK with this, print a warning once per
449+
* boot if we detect this case.
450+
*/
451+
for_each_console(con)
452+
if (con == earlycon)
453+
return;
454+
455+
already_warned = true;
456+
pr_warn("kgdboc_earlycon is still using bootconsole\n");
457+
}
458+
459+
static int kgdboc_earlycon_deferred_exit(struct console *con)
460+
{
461+
/*
462+
* If we get here it means the boot console is going away but we
463+
* don't yet have a suitable replacement. Don't pass through to
464+
* the original exit routine. We'll call it later in our deinit()
465+
* function. For now, restore the original exit() function pointer
466+
* as a sentinal that we've hit this point.
467+
*/
468+
con->exit = earlycon_orig_exit;
469+
470+
return 0;
471+
}
472+
473+
static void kgdboc_earlycon_deinit(void)
474+
{
475+
if (!earlycon)
476+
return;
477+
478+
if (earlycon->exit == kgdboc_earlycon_deferred_exit)
479+
/*
480+
* kgdboc_earlycon is exiting but original boot console exit
481+
* was never called (AKA kgdboc_earlycon_deferred_exit()
482+
* didn't ever run). Undo our trap.
483+
*/
484+
earlycon->exit = earlycon_orig_exit;
485+
else if (earlycon->exit)
486+
/*
487+
* We skipped calling the exit() routine so we could try to
488+
* keep using the boot console even after it went away. We're
489+
* finally done so call the function now.
490+
*/
491+
earlycon->exit(earlycon);
492+
493+
earlycon = NULL;
494+
}
495+
496+
static struct kgdb_io kgdboc_earlycon_io_ops = {
497+
.name = "kgdboc_earlycon",
498+
.read_char = kgdboc_earlycon_get_char,
499+
.write_char = kgdboc_earlycon_put_char,
500+
.pre_exception = kgdboc_earlycon_pre_exp_handler,
501+
.deinit = kgdboc_earlycon_deinit,
502+
.is_console = true,
503+
};
504+
505+
static int __init kgdboc_earlycon_init(char *opt)
506+
{
507+
struct console *con;
508+
509+
kdb_init(KDB_INIT_EARLY);
510+
511+
/*
512+
* Look for a matching console, or if the name was left blank just
513+
* pick the first one we find.
514+
*/
515+
console_lock();
516+
for_each_console(con) {
517+
if (con->write && con->read &&
518+
(con->flags & (CON_BOOT | CON_ENABLED)) &&
519+
(!opt || !opt[0] || strcmp(con->name, opt) == 0))
520+
break;
521+
}
522+
523+
if (!con) {
524+
pr_info("Couldn't find kgdb earlycon\n");
525+
goto unlock;
526+
}
527+
528+
earlycon = con;
529+
pr_info("Going to register kgdb with earlycon '%s'\n", con->name);
530+
if (kgdb_register_io_module(&kgdboc_earlycon_io_ops) != 0) {
531+
earlycon = NULL;
532+
pr_info("Failed to register kgdb with earlycon\n");
533+
} else {
534+
/* Trap exit so we can keep earlycon longer if needed. */
535+
earlycon_orig_exit = con->exit;
536+
con->exit = kgdboc_earlycon_deferred_exit;
537+
}
538+
539+
unlock:
540+
console_unlock();
541+
542+
/* Non-zero means malformed option so we always return zero */
543+
return 0;
544+
}
545+
546+
early_param("kgdboc_earlycon", kgdboc_earlycon_init);
547+
412548
module_init(init_kgdboc);
413549
module_exit(exit_kgdboc);
414550
module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);

include/linux/kgdb.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ struct kgdb_arch {
269269
* @write_char: Pointer to a function that will write one char.
270270
* @flush: Pointer to a function that will flush any pending writes.
271271
* @init: Pointer to a function that will initialize the device.
272+
* @deinit: Pointer to a function that will deinit the device. Implies that
273+
* this I/O driver is temporary and expects to be replaced. Called when
274+
* an I/O driver is replaced or explicitly unregistered.
272275
* @pre_exception: Pointer to a function that will do any prep work for
273276
* the I/O driver.
274277
* @post_exception: Pointer to a function that will do any cleanup work
@@ -282,6 +285,7 @@ struct kgdb_io {
282285
void (*write_char) (u8);
283286
void (*flush) (void);
284287
int (*init) (void);
288+
void (*deinit) (void);
285289
void (*pre_exception) (void);
286290
void (*post_exception) (void);
287291
int is_console;

kernel/debug/debug_core.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,15 +1073,23 @@ EXPORT_SYMBOL_GPL(kgdb_schedule_breakpoint);
10731073
*/
10741074
int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
10751075
{
1076+
struct kgdb_io *old_dbg_io_ops;
10761077
int err;
10771078

10781079
spin_lock(&kgdb_registration_lock);
10791080

1080-
if (dbg_io_ops) {
1081-
spin_unlock(&kgdb_registration_lock);
1081+
old_dbg_io_ops = dbg_io_ops;
1082+
if (old_dbg_io_ops) {
1083+
if (!old_dbg_io_ops->deinit) {
1084+
spin_unlock(&kgdb_registration_lock);
10821085

1083-
pr_err("Another I/O driver is already registered with KGDB\n");
1084-
return -EBUSY;
1086+
pr_err("KGDB I/O driver %s can't replace %s.\n",
1087+
new_dbg_io_ops->name, old_dbg_io_ops->name);
1088+
return -EBUSY;
1089+
}
1090+
pr_info("Replacing I/O driver %s with %s\n",
1091+
old_dbg_io_ops->name, new_dbg_io_ops->name);
1092+
old_dbg_io_ops->deinit();
10851093
}
10861094

10871095
if (new_dbg_io_ops->init) {
@@ -1096,6 +1104,9 @@ int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
10961104

10971105
spin_unlock(&kgdb_registration_lock);
10981106

1107+
if (old_dbg_io_ops)
1108+
return 0;
1109+
10991110
pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name);
11001111

11011112
/* Arm KGDB now. */
@@ -1132,6 +1143,9 @@ void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
11321143

11331144
spin_unlock(&kgdb_registration_lock);
11341145

1146+
if (old_dbg_io_ops->deinit)
1147+
old_dbg_io_ops->deinit();
1148+
11351149
pr_info("Unregistered I/O driver %s, debugger disabled\n",
11361150
old_dbg_io_ops->name);
11371151
}

0 commit comments

Comments
 (0)