summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-05-28 23:31:46 -0400
committerIan Moffett <ian@osmora.org>2025-05-28 23:36:34 -0400
commit51ca9c82c3e8fb323d238e1f58a085f4cb306110 (patch)
treec48309afbff0dccb21f4dde2a43203d5148fd60b
parentcb807eb795e1380513db4c7dbce0452645746abf (diff)
kernel: syslog: Handle userspace kmsg logging
- Add USER_KMSG option to configure if kernel messages should be logged in user contexts - Add syslog_silence() to silence kernel messages in user contexts if USER_KMSG is "yes" Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r--sys/arch/aarch64/conf/GENERIC1
-rw-r--r--sys/arch/amd64/conf/GENERIC1
-rw-r--r--sys/include/sys/syslog.h2
-rw-r--r--sys/kern/init_main.c2
-rw-r--r--sys/kern/kern_syslog.c29
5 files changed, 35 insertions, 0 deletions
diff --git a/sys/arch/aarch64/conf/GENERIC b/sys/arch/aarch64/conf/GENERIC
index 3271cb7..a716ac2 100644
--- a/sys/arch/aarch64/conf/GENERIC
+++ b/sys/arch/aarch64/conf/GENERIC
@@ -1,5 +1,6 @@
// Kernel options
option SERIAL_DEBUG yes // Enable kmsg serial logging
+option USER_KMSG yes // Show kmsg in user consoles
// Kernel constants
setval SCHED_NQUEUE 4 // Number of scheduler queues (for MLFQ)
diff --git a/sys/arch/amd64/conf/GENERIC b/sys/arch/amd64/conf/GENERIC
index 8e97d6e..7a28038 100644
--- a/sys/arch/amd64/conf/GENERIC
+++ b/sys/arch/amd64/conf/GENERIC
@@ -7,6 +7,7 @@
//
option SPECTRE_IBRS no // Enable the IBRS CPU feature
option SERIAL_DEBUG yes // Enable kmsg serial logging
+option USER_KMSG no // Show kmsg in user consoles
// Kernel constants
setval SCHED_NQUEUE 4 // Number of scheduler queues (for MLFQ)
diff --git a/sys/include/sys/syslog.h b/sys/include/sys/syslog.h
index defb341..b9d34ab 100644
--- a/sys/include/sys/syslog.h
+++ b/sys/include/sys/syslog.h
@@ -31,11 +31,13 @@
#define _SYS_SYSLOG_H_
#include <stdarg.h>
+#include <stdbool.h>
#if defined(_KERNEL)
#define OMIT_TIMESTAMP "\x01"
+void syslog_silence(bool option);
void kprintf(const char *fmt, ...);
void serial_init(void);
void serial_putc(char c);
diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c
index 76fa6fd..665bade 100644
--- a/sys/kern/init_main.c
+++ b/sys/kern/init_main.c
@@ -130,6 +130,8 @@ main(void)
begin_install();
#endif
+ syslog_silence(true);
+
/* Bootstrap APs and here we go! */
mp_bootstrap_aps(&g_bsp_ci);
sched_enter();
diff --git a/sys/kern/kern_syslog.c b/sys/kern/kern_syslog.c
index 656362e..db50a3c 100644
--- a/sys/kern/kern_syslog.c
+++ b/sys/kern/kern_syslog.c
@@ -40,8 +40,15 @@
#define SERIAL_DEBUG 0
#endif
+#if defined(__USER_KMSG)
+#define USER_KMSG __USER_KMSG
+#else
+#define USER_KMSG 0
+#endif
+
/* Global logger lock */
static struct spinlock lock = {0};
+static bool no_cons_log = false;
static void
syslog_write(const char *s, size_t len)
@@ -58,6 +65,15 @@ syslog_write(const char *s, size_t len)
}
}
+ /*
+ * If the USER_KMSG option is disabled in kconf,
+ * do not log to the console if everything else
+ * has already started.
+ */
+ if (!USER_KMSG && no_cons_log) {
+ return;
+ }
+
cons_putstr(&g_root_scr, s, len);
}
@@ -116,3 +132,16 @@ kprintf(const char *fmt, ...)
va_end(ap);
spinlock_release(&lock);
}
+
+/*
+ * Silence kernel messages in if the system
+ * is already operating in a user context.
+ *
+ * XXX: This is ignored if the kconf USER_KMSG
+ * option is set to "no"
+ */
+void
+syslog_silence(bool option)
+{
+ no_cons_log = option;
+}