From e1e8f1813080958ed3547e0cd03f940859f20a60 Mon Sep 17 00:00:00 2001 From: Damien Le Moal Date: Fri, 21 Dec 2018 18:12:58 +0900 Subject: [PATCH] console: protect output with a spinlock Avoid getting messages from multiple harts mingled into garbage text with a spinlock serializing calls to sbi_puts() and sbi_printf(). Signed-off-by: Damien Le Moal --- lib/sbi_console.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/sbi_console.c b/lib/sbi_console.c index 3d17340a..6923b9a7 100644 --- a/lib/sbi_console.c +++ b/lib/sbi_console.c @@ -9,8 +9,10 @@ #include #include +#include static struct sbi_platform *console_plat = NULL; +static spinlock_t console_out_lock = SPIN_LOCK_INITIALIZER; bool sbi_isprintable(char c) { @@ -38,28 +40,24 @@ void sbi_putc(char ch) void sbi_puts(const char *str) { + spin_lock(&console_out_lock); while (*str) { sbi_putc(*str); str++; } + spin_unlock(&console_out_lock); } void sbi_gets(char *s, int maxwidth, char endchar) { - char *retval; - char ch; - retval = s; - ch = sbi_getc(); - while (ch != endchar && maxwidth > 0) { + char ch, *retval = s; + + while ((ch = sbi_getc()) != endchar && maxwidth > 1) { *retval = ch; retval++; maxwidth--; - if (maxwidth == 0) - break; - ch = sbi_getc(); } *retval = '\0'; - return; } #define PAD_RIGHT 1 @@ -360,9 +358,13 @@ int sbi_printf(const char *format, ...) { va_list args; int retval; + + spin_lock(&console_out_lock); va_start(args, format); retval = print(NULL, NULL, format, args); va_end(args); + spin_unlock(&console_out_lock); + return retval; }