[PATCH 4/4] bsps/riscv: HTIF - Use 32-bit HTIF syscalls on RV32

Hesham Almatary Hesham.Almatary at cl.cam.ac.uk
Sun Mar 31 14:33:31 UTC 2019


HTIF devices are not supported on RV32, so proxy a write system call

This syncs HTIF Console with riscv-pk (proxy kernel) to use 32-bit
HTIF syscalls on RV32 instead of 64-bit commands that are not atomic.
---
 bsps/riscv/riscv/console/htif.c | 56 +++++++++++++++++++++++++++++----
 1 file changed, 50 insertions(+), 6 deletions(-)

diff --git a/bsps/riscv/riscv/console/htif.c b/bsps/riscv/riscv/console/htif.c
index bcfe6a5db5..d124d285e1 100644
--- a/bsps/riscv/riscv/console/htif.c
+++ b/bsps/riscv/riscv/console/htif.c
@@ -32,6 +32,7 @@
 #if RISCV_ENABLE_HTIF_SUPPORT != 0
 
 #include <dev/serial/htif.h>
+#include <rtems/score/isrlock.h>
 
 #include <assert.h>
 
@@ -43,12 +44,17 @@
 #define FROMHOST_CMD(fromhost_value) ((uint64_t)(fromhost_value) << 8 >> 56)
 #define FROMHOST_DATA(fromhost_value) ((uint64_t)(fromhost_value) << 16 >> 16)
 
+#define SYS_write 64
+
 volatile uint64_t tohost __attribute__((section(".htif")));
 volatile uint64_t fromhost __attribute__((section(".htif")));
 volatile uint64_t riscv_fill_up_htif_section[510] __attribute__((section(".htif")));
 volatile int htif_console_buf;
 
-static void __check_fromhost(void)
+static ISR_lock_Control htif_lock;
+static ISR_lock_Context htif_lock_context;
+
+static void __check_fromhost()
 {
   uint64_t fh = fromhost;
   if (!fh) {
@@ -77,23 +83,60 @@ static void __set_tohost(uintptr_t dev, uintptr_t cmd, uintptr_t data)
   tohost = TOHOST_CMD(dev, cmd, data);
 }
 
+static void do_tohost_fromhost(uintptr_t dev, uintptr_t cmd, uintptr_t data)
+{
+  _ISR_lock_ISR_disable_and_acquire( &htif_lock, &htif_lock_context );
+    __set_tohost(dev, cmd, data);
+
+    while (1) {
+      uint64_t fh = fromhost;
+      if (fh) {
+        if (FROMHOST_DEV(fh) == dev && FROMHOST_CMD(fh) == cmd) {
+          fromhost = 0;
+          break;
+        }
+        __check_fromhost();
+      }
+    }
+  _ISR_lock_Release_and_ISR_enable( &htif_lock, &htif_lock_context );
+}
+
+void htif_syscall(uintptr_t arg)
+{
+  do_tohost_fromhost(0, 0, arg);
+}
+
+void htif_console_putchar(rtems_termios_device_context *base, char ch)
+{
+#if __riscv_xlen == 32
+  // HTIF devices are not supported on RV32, so proxy a write system call
+  volatile uint64_t magic_mem[8];
+  magic_mem[0] = SYS_write;
+  magic_mem[1] = 1;
+  magic_mem[2] = (uintptr_t)&ch;
+  magic_mem[3] = 1;
+  do_tohost_fromhost(0, 0, (uintptr_t)magic_mem);
+#else
+  _ISR_lock_ISR_disable_and_acquire( &htif_lock, &htif_lock_context );
+    __set_tohost(1, 1, ch);
+  _ISR_lock_Release_and_ISR_enable( &htif_lock, &htif_lock_context );
+#endif
+}
+
 int htif_console_getchar(rtems_termios_device_context *base)
 {
+  _ISR_lock_ISR_disable_and_acquire( &htif_lock, &htif_lock_context );
   __check_fromhost();
   int ch = htif_console_buf;
   if (ch >= 0) {
     htif_console_buf = -1;
     __set_tohost(1, 0, 0);
   }
+  _ISR_lock_Release_and_ISR_enable( &htif_lock, &htif_lock_context );
 
   return ch - 1;
 }
 
-void htif_console_putchar(rtems_termios_device_context *base, char c)
-{
-  __set_tohost(1, 1, c);
-}
-
 static void htif_console_write_polled(
   rtems_termios_device_context *base,
   const char *buf,
@@ -120,6 +163,7 @@ void htif_console_context_init(
   int device_tree_node
 )
 {
+  _ISR_lock_Initialize( &htif_lock, "htif_lock" );
   rtems_termios_device_context_initialize(base, "HTIF");
 }
 
-- 
2.17.1




More information about the devel mailing list