Skip to content

Commit

Permalink
sw: Runtime Fixes (#166)
Browse files Browse the repository at this point in the history
* sw: Fix problems with linker garbage collection

* sw: Statically allocate printf buffers

* sw: Thread-safe `putchar()` function

---------

Co-authored-by: Luca Colagrande <[email protected]>
  • Loading branch information
Xeratec and colluca authored Jul 19, 2024
1 parent 56cdf7b commit 8ad8ff8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sw/snRuntime/base.ld
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ SECTIONS
.cbss :
{
__cbss_start = .;
*(.cbss .cbss.*)
KEEP(*(.cbss .cbss.*))
__cbss_end = .;
} >L3

Expand Down
19 changes: 13 additions & 6 deletions target/snitch_cluster/sw/runtime/rtl/src/putchar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 8ad8ff8

Please sign in to comment.