summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDamien Le Moal <[email protected]>2018-12-21 18:12:58 +0900
committerDamien Le Moal <[email protected]>2018-12-21 18:12:58 +0900
commite1e8f1813080958ed3547e0cd03f940859f20a60 (patch)
tree6751b9e9b36ce51e82b8245503303441a485aa33 /lib
parent07ee5f2e328b7ee3ed57b51f98f2765ae1701a99 (diff)
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 <[email protected]>
Diffstat (limited to 'lib')
-rw-r--r--lib/sbi_console.c20
1 files 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 <sbi/sbi_platform.h>
#include <sbi/sbi_console.h>
+#include <sbi/riscv_locks.h>
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;
}