kernel: add kernel.early-console and kernel.console boot args

kernel.early-console is used to specify which output device the
kernel boot log should be written to. the first thing the kernel
does on boot after initialising the bootstrap processor is initialise
the early console, making it useful for debugging problems that
occur early in the boot process. this arg accepts a list of hard-coded
values for output devices, such as tty0 for the display or ttyS0
for the serial port. the exact values supported will depend on the
platform.

once all drivers are loaded, the kernel switches to the device specified
by kernel.console for output. unlike kernel.early-console, this arg
specifies the name of a tty device in /dev/tty. this means that, not
only are more devices supported (any device provided by a tty driver),
but the kernel can also get input from the user using this console too
(not used by the kernel itself, but will be used by the user to interact
with userspace programs, like the shell).
This commit is contained in:
2023-12-30 09:09:18 +00:00
parent fc56f906d3
commit b0c021d4e9
6 changed files with 91 additions and 18 deletions

View File

@@ -9,10 +9,13 @@ static struct char_device_ops tty_ops = {
.write = tty_write,
};
static spin_lock_t foreground_lock;
static spin_lock_t foreground_lock = SPIN_LOCK_INIT;
static struct device *foreground = NULL;
static struct device *foreground_input = NULL;
static spin_lock_t kernel_console_tty_lock = SPIN_LOCK_INIT;
static struct device *kernel_console_tty = NULL;
static void tty_input_hook_callback(struct device *dev, struct input_event *ev, enum input_event_hook_flags *flags, void *arg)
{
struct device *fg_tty = foreground;
@@ -31,9 +34,9 @@ static struct input_event_hook foreground_input_hook = {
static void tty_console_write(struct console *con, const char *s, unsigned int len)
{
if (foreground) {
if (kernel_console_tty) {
size_t nr_written;
tty_write(foreground, s, 0, len, &nr_written, 0);
tty_write(kernel_console_tty, s, 0, len, &nr_written, 0);
}
}
@@ -42,6 +45,26 @@ static struct console tty_console = {
.c_write = tty_console_write,
};
void register_tty_console(void)
{
console_register(&tty_console);
}
void redirect_printk_to_tty(struct device *dest)
{
unsigned long flags;
spin_lock_irqsave(&kernel_console_tty_lock, &flags);
if (kernel_console_tty) {
device_deref(kernel_console_tty);
kernel_console_tty = NULL;
}
kernel_console_tty = device_ref(dest);
spin_unlock_irqrestore(&kernel_console_tty_lock, flags);
}
struct device *tty_device_create(void)
{
struct char_device *cdev = char_device_create();
@@ -94,17 +117,17 @@ kern_status_t tty_device_register(struct device *dev, struct tty_driver *owner,
void tty_set_foreground(struct device *tty)
{
bool console_init = false;
unsigned long flags;
spin_lock_irqsave(&foreground_lock, &flags);
if (foreground) {
console_init = true;
device_deref(foreground);
foreground = NULL;
}
foreground = device_ref(tty);
if (!console_init) {
console_register(&tty_console);
}
spin_unlock_irqrestore(&foreground_lock, flags);
}
kern_status_t tty_connect_foreground_input_device(struct device *input)