From 011f0ab2d6fcc8aa8b30c1111f6eae6cdd319e74 Mon Sep 17 00:00:00 2001 From: Alberto Escolar Piedras Date: Wed, 5 Jul 2023 16:45:38 +0200 Subject: [PATCH 1/2] native_simulator: Align with upstream latest with more trampolines Upstream SHA: be3eac6931b49291e51da5d5aa1a99ab58f81541 Signed-off-by: Alberto Escolar Piedras --- scripts/native_simulator/Makefile | 2 + .../common/src/include/nsi_host_trampolines.h | 10 ++++ .../common/src/nsi_host_trampolines.c | 53 +++++++++++++++++++ .../native_simulator/common/src/nsi_tasks.h | 2 +- 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/scripts/native_simulator/Makefile b/scripts/native_simulator/Makefile index 5dc9b409e1f0da..6684c938945464 100644 --- a/scripts/native_simulator/Makefile +++ b/scripts/native_simulator/Makefile @@ -10,6 +10,8 @@ NSI_CONFIG_FILE?=nsi_config -include ${NSI_CONFIG_FILE} +#If the file does not exist, we don't use it as a build dependency +NSI_CONFIG_FILE:=$(wildcard ${NSI_CONFIG_FILE}) NSI_PATH?=./ NSI_BUILD_PATH?=$(abspath _build/) diff --git a/scripts/native_simulator/common/src/include/nsi_host_trampolines.h b/scripts/native_simulator/common/src/include/nsi_host_trampolines.h index 53eff33bc99ffb..f7f7dce877df89 100644 --- a/scripts/native_simulator/common/src/include/nsi_host_trampolines.h +++ b/scripts/native_simulator/common/src/include/nsi_host_trampolines.h @@ -22,10 +22,20 @@ extern "C" { #endif +void *nsi_host_calloc(unsigned long nmemb, unsigned long size); +int nsi_host_close(int fd); /* void nsi_host_exit (int status); Use nsi_exit() instead */ +void nsi_host_free(void *ptr); +char *nsi_host_getcwd(char *buf, unsigned long size); +int nsi_host_isatty(int fd); +void *nsi_host_malloc(unsigned long size); +int nsi_host_open(const char *pathname, int flags); /* int nsi_host_printf (const char *fmt, ...); Use the nsi_tracing.h equivalents */ long nsi_host_random(void); +long nsi_host_read(int fd, void *buffer, unsigned long size); void nsi_host_srandom(unsigned int seed); +char *nsi_host_strdup(const char *s); +long nsi_host_write(int fd, void *buffer, unsigned long size); #ifdef __cplusplus } diff --git a/scripts/native_simulator/common/src/nsi_host_trampolines.c b/scripts/native_simulator/common/src/nsi_host_trampolines.c index d35dce40759064..3feb8482c913d5 100644 --- a/scripts/native_simulator/common/src/nsi_host_trampolines.c +++ b/scripts/native_simulator/common/src/nsi_host_trampolines.c @@ -7,13 +7,66 @@ */ #include +#include +#include +#include + +void *nsi_host_calloc(unsigned long nmemb, unsigned long size) +{ + return calloc(nmemb, size); +} + +int nsi_host_close(int fd) +{ + return close(fd); +} + +void nsi_host_free(void *ptr) +{ + free(ptr); +} + +char *nsi_host_getcwd(char *buf, unsigned long size) +{ + return getcwd(buf, size); +} + +int nsi_host_isatty(int fd) +{ + return isatty(fd); +} + +void *nsi_host_malloc(unsigned long size) +{ + return malloc(size); +} + +int nsi_host_open(const char *pathname, int flags) +{ + return open(pathname, flags); +} long nsi_host_random(void) { return random(); } +long nsi_host_read(int fd, void *buffer, unsigned long size) +{ + return read(fd, buffer, size); +} + void nsi_host_srandom(unsigned int seed) { srandom(seed); } + +char *nsi_host_strdup(const char *s) +{ + return strdup(s); +} + +long nsi_host_write(int fd, void *buffer, unsigned long size) +{ + return write(fd, buffer, size); +} diff --git a/scripts/native_simulator/common/src/nsi_tasks.h b/scripts/native_simulator/common/src/nsi_tasks.h index ee98ba5ddc108e..f3d4ec08ae22be 100644 --- a/scripts/native_simulator/common/src/nsi_tasks.h +++ b/scripts/native_simulator/common/src/nsi_tasks.h @@ -42,7 +42,7 @@ extern "C" { * The function must take no parameters and return nothing. */ #define NSI_TASK(fn, level, prio) \ - static void (* const NSI_CONCAT(__nsi_task_, fn))() \ + static void (* const NSI_CONCAT(__nsi_task_, fn))(void) \ __attribute__((__used__)) \ __attribute__((__section__(".nsi_" #level NSI_STRINGIFY(prio) "_task")))\ = fn From 4cfe6567c7dd8585948e119b444f46dcebfbbfc6 Mon Sep 17 00:00:00 2001 From: Alberto Escolar Piedras Date: Thu, 6 Jul 2023 14:05:46 +0200 Subject: [PATCH 2/2] native boards: Make native simulator host trampolines avaliable to all To ease writing common drivers, let's make the host trampolines from the native simulator avaliable to all posix based boards. Signed-off-by: Alberto Escolar Piedras --- arch/posix/core/CMakeLists.txt | 1 + .../core/nsi_compat/nsi_host_trampolines.h | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 arch/posix/core/nsi_compat/nsi_host_trampolines.h diff --git a/arch/posix/core/CMakeLists.txt b/arch/posix/core/CMakeLists.txt index 23802715c1dd67..4fbfb617949dd4 100644 --- a/arch/posix/core/CMakeLists.txt +++ b/arch/posix/core/CMakeLists.txt @@ -18,6 +18,7 @@ if(CONFIG_NATIVE_APPLICATION) posix_core.c nsi_compat/nsi_compat.c ${ZEPHYR_BASE}/scripts/native_simulator/common/src/nce.c + ${ZEPHYR_BASE}/scripts/native_simulator/common/src/nsi_host_trampolines.c ) else() zephyr_library_sources( diff --git a/arch/posix/core/nsi_compat/nsi_host_trampolines.h b/arch/posix/core/nsi_compat/nsi_host_trampolines.h new file mode 100644 index 00000000000000..f0a2e06c1caae0 --- /dev/null +++ b/arch/posix/core/nsi_compat/nsi_host_trampolines.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + * + * Note: This is a provisional header which exists to allow + * old POSIX arch based boards (i.e. native_posix) to provide access + * to the host C library as if the native simulator trampolines + * existed. + * + * Boards based on the native simulator do NOT use this file + */ + +#ifndef ARCH_POSIX_CORE_NSI_COMPAT_NSI_HOST_TRAMPOLINES_H +#define ARCH_POSIX_CORE_NSI_COMPAT_NSI_HOST_TRAMPOLINES_H + +#include "../scripts/native_simulator/common/src/include/nsi_host_trampolines.h" + +#endif /* ARCH_POSIX_CORE_NSI_COMPAT_NSI_HOST_TRAMPOLINES_H */