Skip to content

Commit

Permalink
sw: Fixed wrong return value when using ocd semihosting (#165)
Browse files Browse the repository at this point in the history
Co-authored-by: Germain Haugou <[email protected]>
  • Loading branch information
haugoug and Germain Haugou authored Jul 17, 2024
1 parent 70ff02f commit 5d8217c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions sw/snRuntime/src/openocd.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

#include <stdint.h>

#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
Expand Down Expand Up @@ -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);
}
6 changes: 5 additions & 1 deletion sw/snRuntime/src/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5d8217c

Please sign in to comment.