From 8ad8ff881d8c6d032fcfb7f281dd0e86f3bac804 Mon Sep 17 00:00:00 2001 From: Philip Wiese Date: Fri, 19 Jul 2024 12:54:18 +0200 Subject: [PATCH] sw: Runtime Fixes (#166) * sw: Fix problems with linker garbage collection * sw: Statically allocate printf buffers * sw: Thread-safe `putchar()` function --------- Co-authored-by: Luca Colagrande --- sw/snRuntime/base.ld | 2 +- .../sw/runtime/rtl/src/putchar.c | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/sw/snRuntime/base.ld b/sw/snRuntime/base.ld index d0979b719..171921d5e 100644 --- a/sw/snRuntime/base.ld +++ b/sw/snRuntime/base.ld @@ -66,7 +66,7 @@ SECTIONS .cbss : { __cbss_start = .; - *(.cbss .cbss.*) + KEEP(*(.cbss .cbss.*)) __cbss_end = .; } >L3 diff --git a/target/snitch_cluster/sw/runtime/rtl/src/putchar.c b/target/snitch_cluster/sw/runtime/rtl/src/putchar.c index ea5841888..e9412473e 100644 --- a/target/snitch_cluster/sw/runtime/rtl/src/putchar.c +++ b/target/snitch_cluster/sw/runtime/rtl/src/putchar.c @@ -25,16 +25,21 @@ void _putchar(char character) { extern uintptr_t volatile tohost, fromhost; // Rudimentary string buffer for putc calls. -extern uint32_t _edram; #define PUTC_BUFFER_LEN (1024 - sizeof(size_t)) -struct putc_buffer_header { + +typedef struct { size_t size; uint64_t syscall_mem[8]; -}; -static volatile struct putc_buffer { - struct putc_buffer_header hdr; +} putc_buffer_header_t; + +typedef struct putc_buffer { + putc_buffer_header_t hdr; char data[PUTC_BUFFER_LEN]; -} *const putc_buffer = (void *)&_edram; +} putc_buffer_t; + +static volatile putc_buffer_t + putc_buffer[SNRT_CLUSTER_NUM * SNRT_CLUSTER_CORE_NUM] + __attribute__((section(".dram"))); // Provide an implementation for putchar. void _putchar(char character) { @@ -46,10 +51,12 @@ void _putchar(char character) { buf->hdr.syscall_mem[2] = (uintptr_t)&buf->data; // buffer buf->hdr.syscall_mem[3] = buf->hdr.size; // length + snrt_mutex_acquire(snrt_mutex()); tohost = (uintptr_t)buf->hdr.syscall_mem; while (fromhost == 0) ; fromhost = 0; + snrt_mutex_release(snrt_mutex()); buf->hdr.size = 0; }