From 5d8217cd487fc404305d16e20bce7fa284e922fc Mon Sep 17 00:00:00 2001 From: Germain Haugou Date: Wed, 17 Jul 2024 16:09:02 +0200 Subject: [PATCH] sw: Fixed wrong return value when using ocd semihosting (#165) Co-authored-by: Germain Haugou --- sw/snRuntime/src/openocd.h | 10 ++++++++++ sw/snRuntime/src/start.c | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/sw/snRuntime/src/openocd.h b/sw/snRuntime/src/openocd.h index 699ace9ca..272e92581 100644 --- a/sw/snRuntime/src/openocd.h +++ b/sw/snRuntime/src/openocd.h @@ -5,6 +5,11 @@ #include +#pragma once + +#define SEMIHOST_EXIT_SUCCESS 0x20026 +#define SEMIHOST_EXIT_ERROR 0x20023 + /* riscv semihosting standard: * IN: a0 holds syscall number * IN: a1 holds pointer to arg struct @@ -32,3 +37,8 @@ static inline int __ocd_semihost_write(int fd, uint8_t *buffer, int len) { __asm__ __volatile__("" : : : "memory"); return __ocd_semihost(0x05, (long)args); } + +static inline void __ocd_semihost_exit(int status) { + __ocd_semihost(0x18, + status == 0 ? SEMIHOST_EXIT_SUCCESS : SEMIHOST_EXIT_ERROR); +} \ No newline at end of file diff --git a/sw/snRuntime/src/start.c b/sw/snRuntime/src/start.c index 779dfcf2f..7f117c206 100644 --- a/sw/snRuntime/src/start.c +++ b/sw/snRuntime/src/start.c @@ -2,6 +2,10 @@ // Licensed under the Apache License, Version 2.0, see LICENSE for details. // SPDX-License-Identifier: Apache-2.0 +#ifdef OPENOCD_SEMIHOSTING +#include "openocd.h" +#endif + #ifdef SNRT_INIT_CLS static inline uint32_t snrt_cls_base_addr() { extern volatile uint32_t __cdata_start, __cdata_end; @@ -100,7 +104,7 @@ static inline void snrt_init_libs() { snrt_alloc_init(); } static inline void snrt_exit_default(int exit_code) { exit_code = snrt_global_all_to_all_reduction(exit_code); #ifdef OPENOCD_SEMIHOSTING - if (snrt_global_core_idx() == 0) __ocd_semihost(0x18, (long)exit_code); + if (snrt_global_core_idx() == 0) __ocd_semihost_exit(exit_code); #else if (snrt_global_core_idx() == 0) *(snrt_exit_code_destination()) = (exit_code << 1) | 1;