Adding semihosting support for bmsp

This commit is contained in:
gabriel 2024-04-08 14:36:22 +02:00
parent 39ee91af7d
commit e83f7996bc
14 changed files with 175 additions and 7 deletions

View File

@ -27,6 +27,7 @@ SET(LIBWRAP_SRCS
${LIBWRAP_DIR}/sys/_exit.c
${LIBWRAP_DIR}/misc/write_hex.c
${LIBWRAP_DIR}/sys/printf.c
${LIBWRAP_DIR}/sys/puts.c
)
IF(${SEMIHOSTING})
SET(LIBWRAP_SRCS ${LIBWRAP_SRCS} ${LIBWRAP_DIR}/semihosting/semihosting.c ${LIBWRAP_DIR}/semihosting/trap.c)
@ -49,6 +50,6 @@ FOREACH(SYM ${LIBWRAP_SYMS})
LIST(APPEND WRAP_LDFLAGS "-Wl,--wrap=${SYM}")
ENDFOREACH()
SET(LIBWRAP_TGC_LDFLAGS ${WRAP_LDFLAGS} "-Wl,--start-group" "-Wl,--end-group")
SET(LIBWRAP_TGC_LDFLAGS ${WRAP_LDFLAGS} "-Wl,--start-group" "-Wl,--end-group" "-L. -lLIBWRAP_TGC")
ENDIF(NOT DEFINED _MK_LIBWRAP)

View File

@ -1,6 +1,7 @@
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include "semihosting.h"
@ -25,12 +26,31 @@
#define SEMIHOSTING_SYS_GET_CMDLINE 0x15
#define SEMIHOSTING_SYS_HEAPINFO 0x16
#define SEMIHOSTING_EnterSVC 0x17
#define SEMIHOSTING_ReportException 0x18
#define SEMIHOSTING_SYS_EXIT 0x18
#define SEMIHOSTING_SYS_EXIT_EXTENDED 0x20
#define SEMIHOSTING_SYS_ELAPSED 0x30
#define SEMIHOSTING_SYS_TICKFREQ 0x31
#define RISCV_SEMIHOSTING_CALL_NUMBER 7
/*typedef struct {
char* str;
int mode;
size_t length;
} OpenVector;
typedef struct {
char* old;
int old_len;
char* new;
int new_len;
} RenameVector;
typedef struct {
char* path;
int len;
} RemoveVector;
*/
static inline int __attribute__ ((always_inline)) call_host(int reason, void* arg) {
#if 1
@ -66,6 +86,71 @@ static inline int __attribute__ ((always_inline)) call_host(int reason, void* ar
#endif
}
/*
int sh_errno(void) {
return call_host(SEMIHOSTING_SYS_ERRNO, (void*)NULL);
}
int sh_time(void) {
return call_host(SEMIHOSTING_SYS_TIME, (void*)NULL);
}
void sh_seek(int file_handle, int pos) {
int vec[2] = {file_handle, pos};
call_host(SEMIHOSTING_SYS_SEEK, &vec);
return;
}
void sh_write(char* str, int file_handle) {
size_t length = strlen(str);
OpenVector vec = {str, file_handle, length};
call_host(SEMIHOSTING_SYS_WRITE, &vec);
return;
}
int sh_close(int file_handle) {
return call_host(SEMIHOSTING_SYS_CLOSE, file_handle);
}
void sh_exit(void) {
call_host(SEMIHOSTING_SYS_EXIT, (void*)NULL);
return;
}
void sh_exit_extended(void) {
call_host(SEMIHOSTING_SYS_EXIT_EXTENDED, (void*)NULL);
return;
}
int sh_flen(int file_handle) {
return call_host(SEMIHOSTING_SYS_FLEN, file_handle);
}
int sh_iserror(int num) {
return call_host(SEMIHOSTING_SYS_ISERROR, num);
}
int sh_istty(int file_handle) {
return call_host(SEMIHOSTING_SYS_ISTTY, file_handle);
}
int sh_remove(char* path) {
int len = strlen(path);
RemoveVector vec = {path, len};
return call_host(SEMIHOSTING_SYS_REMOVE, &vec);
}
void sh_rename(char* old, char* new) {
int old_len = strlen(old);
int new_len = strlen(new);
RenameVector vec = {old, old_len, new, new_len};
call_host(SEMIHOSTING_SYS_RENAME, &vec);
return;
}
void sh_write0(const char* buf)
{
// Print zero-terminated string
@ -82,9 +167,26 @@ char sh_readc(void)
{
// Read character from keyboard. (Blocking operation!)
char c = call_host(SEMIHOSTING_SYS_READC, (void*)NULL);
return c;
}
if (sh_missing_host)
return 0;
else
return c;
int sh_open(char* str, int mode) {
//mode = 0;
//int length = 44;
size_t length = strlen(str);
OpenVector vec = {str, mode, length};
int i = call_host(SEMIHOSTING_SYS_OPEN, &vec);
return i;
}
void sh_read(char* buf, int file_handle, size_t length) {
OpenVector vec = {buf, file_handle, length};
int i = call_host(SEMIHOSTING_SYS_READ, &vec);
return i;
} */
int sh_clock(void)
{
int clock = call_host(SEMIHOSTING_SYS_CLOCK, (void*)NULL);
return clock;
}

View File

@ -6,6 +6,20 @@
void sh_write0(const char* buf);
void sh_writec(char c);
char sh_readc(void);
int sh_clock(void);
void sh_read(char*, int, size_t);
void sh_write(char*, int);
int sh_open(char*, int);
void sh_rename(char*, char*);
int sh_remove(char*);
int sh_istty(int);
int sh_iserror(int);
int sh_flen(int);
void sh_exit(void);
void sh_exit_extended(void);
int sh_close(int);
int sh_time(void);
int sh_errno(void);
int getchar(void);

View File

@ -13,7 +13,7 @@
int sh_missing_host = 0;
void trap()
{
{ //ToDo: Check why macro CSR_MEPC and others are not resolved
uint32_t mepc = read_csr(0x341); // Address of trap
uint32_t mtval = read_csr(0x343); // Instruction value of trap
uint32_t mcause = read_csr(0x342); // Reason for the trap

View File

@ -3,6 +3,7 @@
#include <unistd.h>
#include "platform.h"
#include "weak_under_alias.h"
#include "semihosting.h"
#if defined(BOARD_hifive1)
static volatile uint32_t tohost;
@ -16,6 +17,10 @@ void write_hex(int fd, uint32_t hex);
void __wrap_exit(int code)
{
#if defined(SEMIHOSTING)
sh_exit();
return;
#endif
//volatile uint32_t* leds = (uint32_t*) (GPIO_BASE_ADDR + GPIO_OUT_OFFSET);
const char message[] = "\nProgam has exited with code:";
//*leds = (~(code));

View File

@ -3,9 +3,14 @@
#include <errno.h>
#include "stub.h"
#include "weak_under_alias.h"
#include "semihosting.h"
int __wrap_close(int fd)
{
#if defined(SEMIHOTING)
int i = sh_close(fd);
return i;
#endif
return _stub(EBADF);
}

View File

@ -2,9 +2,14 @@
#include <unistd.h>
#include "weak_under_alias.h"
#include "semihosting.h"
int __wrap_isatty(int fd)
{
#if defined(SEMIHOSTING)
int i = sh_istty(fd);
return i;
#endif
if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
return 1;

View File

@ -5,9 +5,16 @@
#include <sys/types.h>
#include "stub.h"
#include "weak_under_alias.h"
#include "semihosting.h"
off_t __wrap_lseek(int fd, off_t ptr, int dir)
{
#if defined(SEMIHOSTING)
if(sh_istty(fd))
return 0;
sh_seek();
return ptr;
#endif
if (isatty(fd))
return 0;

View File

@ -3,9 +3,14 @@
#include <errno.h>
#include "stub.h"
#include "weak_under_alias.h"
#include "semihosting.h"
int __wrap_open(const char* name, int flags, int mode)
{
#if defined(SEMIHOSTING)
int fd = sh_open(name, mode);
return fd;
#endif
return _stub(ENOENT);
}
weak_under_alias(open);

View File

@ -7,6 +7,7 @@
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include "semihosting.h"
#undef putchar
int putchar(int ch)
@ -114,7 +115,11 @@ static void vprintfmt(void (*putch)(int, void**), void **putdat, const char *fmt
if (ch == '\0')
return;
fmt++;
#if defined(SEMIHOSTING)
sh_writec(ch);
#else
putch(ch, putdat);
#endif
}
fmt++;

View File

@ -8,9 +8,14 @@
#include "platform.h"
#include "stub.h"
#include "weak_under_alias.h"
#include "semihosting.h"
int __wrap_puts(const char *s)
{
#if defined(SEMIHOSTING)
sh_write0(s);
return 0;
#endif
while (*s != '\0') {
#if defined(BOARD_ehrenberg)
while (uart_get_tx_free(uart)==0) ;

View File

@ -10,10 +10,15 @@
#include "platform.h"
#include "stub.h"
#include "weak_under_alias.h"
#include "semihosting.h"
ssize_t __wrap_read(int fd, void* ptr, size_t len)
{
uint8_t * current = (uint8_t *)ptr;
#if defined(SEMIHOSTING)
sh_read(current, fd, len);
return;
#endif
#if defined(BOARD_hifive1)
volatile uint32_t * uart_rx = (uint32_t *)(UART0_CTRL_ADDR + UART_REG_RXFIFO);
volatile uint8_t * uart_rx_cnt = (uint8_t *)(UART0_CTRL_ADDR + UART_REG_RXCTRL + 2);

View File

@ -3,9 +3,13 @@
#include <errno.h>
#include "stub.h"
#include "weak_under_alias.h"
#include "semihosting.h"
int __wrap_unlink(const char* name)
{
#if defined(SEMIHOSTING)
return sh_remove(name);
#endif
return _stub(ENOENT);
}
weak_under_alias(unlink);

View File

@ -8,10 +8,15 @@
#include "platform.h"
#include "stub.h"
#include "weak_under_alias.h"
#include "semihosting.h"
ssize_t __wrap_write(int fd, const void* ptr, size_t len)
{
const uint8_t * current = (const uint8_t *)ptr;
#if defined(SEMIHOSTING)
sh_write(current, fd);
return len;
#endif
if (isatty(fd)) {
for (size_t jj = 0; jj < len; jj++) {
#if defined(BOARD_ehrenberg)