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 <damien.lemoal@wdc.com>
This commit is contained in:
Damien Le Moal
2018-12-21 18:12:58 +09:00
parent 07ee5f2e32
commit e1e8f18130

View File

@@ -9,8 +9,10 @@
#include <sbi/sbi_platform.h> #include <sbi/sbi_platform.h>
#include <sbi/sbi_console.h> #include <sbi/sbi_console.h>
#include <sbi/riscv_locks.h>
static struct sbi_platform *console_plat = NULL; static struct sbi_platform *console_plat = NULL;
static spinlock_t console_out_lock = SPIN_LOCK_INITIALIZER;
bool sbi_isprintable(char c) bool sbi_isprintable(char c)
{ {
@@ -38,28 +40,24 @@ void sbi_putc(char ch)
void sbi_puts(const char *str) void sbi_puts(const char *str)
{ {
spin_lock(&console_out_lock);
while (*str) { while (*str) {
sbi_putc(*str); sbi_putc(*str);
str++; str++;
} }
spin_unlock(&console_out_lock);
} }
void sbi_gets(char *s, int maxwidth, char endchar) void sbi_gets(char *s, int maxwidth, char endchar)
{ {
char *retval; char ch, *retval = s;
char ch;
retval = s; while ((ch = sbi_getc()) != endchar && maxwidth > 1) {
ch = sbi_getc();
while (ch != endchar && maxwidth > 0) {
*retval = ch; *retval = ch;
retval++; retval++;
maxwidth--; maxwidth--;
if (maxwidth == 0)
break;
ch = sbi_getc();
} }
*retval = '\0'; *retval = '\0';
return;
} }
#define PAD_RIGHT 1 #define PAD_RIGHT 1
@@ -360,9 +358,13 @@ int sbi_printf(const char *format, ...)
{ {
va_list args; va_list args;
int retval; int retval;
spin_lock(&console_out_lock);
va_start(args, format); va_start(args, format);
retval = print(NULL, NULL, format, args); retval = print(NULL, NULL, format, args);
va_end(args); va_end(args);
spin_unlock(&console_out_lock);
return retval; return retval;
} }