From a13a23d5fa90359128f93c8bd454c6f024f1466f Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Wed, 15 Sep 2021 23:18:46 +0900 Subject: [PATCH] iodev: Add a spinlock and allow console for secondaries Signed-off-by: Hector Martin --- src/iodev.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/iodev.c b/src/iodev.c index 8125be26..ebff0b4e 100644 --- a/src/iodev.c +++ b/src/iodev.c @@ -3,6 +3,7 @@ //#define DEBUG_IODEV #include "iodev.h" +#include "memory.h" #include "string.h" #ifdef DEBUG_IODEV @@ -80,14 +81,31 @@ void iodev_flush(iodev_id_t id) int in_iodev = 0; +static DECLARE_SPINLOCK(console_lock); + void iodev_console_write(const void *buf, size_t length) { - if (in_iodev || !is_primary_core()) { + bool do_lock = mmu_active(); + + if (!do_lock && !is_primary_core()) { if (length) { iodev_write(IODEV_UART, "*", 1); iodev_write(IODEV_UART, buf, length); - return; } + return; + } + + if (do_lock) + spin_lock(&console_lock); + + if (in_iodev) { + if (length) { + iodev_write(IODEV_UART, "*", 1); + iodev_write(IODEV_UART, buf, length); + } + if (do_lock) + spin_unlock(&console_lock); + return; } in_iodev++; @@ -159,6 +177,8 @@ void iodev_console_write(const void *buf, size_t length) } in_iodev--; + if (do_lock) + spin_unlock(&console_lock); } void iodev_handle_events(iodev_id_t id)