From f13efe57a16d030d893dcaa702c4fd2a551a4856 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 1 Dec 2022 17:31:38 +0100 Subject: [PATCH 01/10] cpu/native: use async read for stdio_read() The real_read() function will block the thread but won't preempt it. That means all other thereads on the same (or higher) priority level are blocked as RIOT still consideres the thread that called stdio_read() as running. Use async_read/isrpipe to properly block the thread when reading from stdin. --- cpu/native/Makefile.dep | 4 ++++ cpu/native/startup.c | 5 +++-- cpu/native/stdio_native/stdio_native.c | 24 +++++++++++++++++++++++- sys/Kconfig.stdio | 1 + 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/cpu/native/Makefile.dep b/cpu/native/Makefile.dep index 7fb477b19e61..384b75042a97 100644 --- a/cpu/native/Makefile.dep +++ b/cpu/native/Makefile.dep @@ -17,6 +17,10 @@ ifeq (,$(filter stdio_%,$(USEMODULE))) USEMODULE += stdio_native endif +ifneq (,$(filter stdio_native,$(USEMODULE))) + USEMODULE += isrpipe +endif + ifneq (,$(filter periph_rtc,$(USEMODULE))) USEMODULE += ztimer USEMODULE += ztimer_msec diff --git a/cpu/native/startup.c b/cpu/native/startup.c index 5014e0c70141..6f0532444471 100644 --- a/cpu/native/startup.c +++ b/cpu/native/startup.c @@ -465,8 +465,6 @@ static void _reset_handler(void) __attribute__((constructor)) static void startup(int argc, char **argv, char **envp) { _native_init_syscalls(); - /* initialize stdio as early as possible */ - early_init(); _native_argv = argv; _progname = argv[0]; @@ -682,6 +680,9 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e register_interrupt(SIGUSR1, _reset_handler); + /* initialize stdio after signal setup */ + early_init(); + puts("RIOT native hardware initialization complete.\n"); irq_enable(); kernel_init(); diff --git a/cpu/native/stdio_native/stdio_native.c b/cpu/native/stdio_native/stdio_native.c index e79bec4eba0a..907c68a67785 100644 --- a/cpu/native/stdio_native/stdio_native.c +++ b/cpu/native/stdio_native/stdio_native.c @@ -13,18 +13,40 @@ * @author Martine S. Lenders */ +#include "async_read.h" #include "kernel_defines.h" #include "native_internal.h" +#include "isrpipe.h" #include "stdio_base.h" +#ifndef STDIO_NATIVE_RX_BUFSIZE +#define STDIO_NATIVE_RX_BUFSIZE (64) +#endif + +static uint8_t _rx_buf_mem[STDIO_NATIVE_RX_BUFSIZE]; +static isrpipe_t stdio_isrpipe = ISRPIPE_INIT(_rx_buf_mem); + +static void _async_read_wrapper(int fd, void *arg) { + uint8_t buf[1]; + + int res = real_read(fd, &buf, sizeof(buf)); + if (res > 0) { + isrpipe_write(arg, buf, res); + } + + native_async_read_continue(fd); +} + void stdio_init(void) { + native_async_read_setup(); + native_async_read_add_int_handler(STDIN_FILENO, &stdio_isrpipe, _async_read_wrapper); } ssize_t stdio_read(void* buffer, size_t max_len) { - return real_read(STDIN_FILENO, buffer, max_len); + return (ssize_t)isrpipe_read(&stdio_isrpipe, buffer, max_len); } ssize_t stdio_write(const void* buffer, size_t len) diff --git a/sys/Kconfig.stdio b/sys/Kconfig.stdio index 15e5f2859720..1cce0d12a995 100644 --- a/sys/Kconfig.stdio +++ b/sys/Kconfig.stdio @@ -59,6 +59,7 @@ config MODULE_STDIO_UART_ONLCR config MODULE_STDIO_NATIVE bool "Native" depends on CPU_ARCH_NATIVE + select MODULE_ISRPIPE config MODULE_STDIO_ETHOS bool "ETHOS" From bef1d377c90ce859677677a1e21b8f844a682586 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 1 Dec 2022 18:03:55 +0100 Subject: [PATCH 02/10] cpu/native: bump ASYNC_READ_NUMOF --- cpu/native/include/async_read.h | 2 +- examples/gnrc_border_router/Makefile.native.conf | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/cpu/native/include/async_read.h b/cpu/native/include/async_read.h index 1c2edd9a5639..2317d7c4c1f0 100644 --- a/cpu/native/include/async_read.h +++ b/cpu/native/include/async_read.h @@ -29,7 +29,7 @@ extern "C" { * @brief Maximum number of file descriptors */ #ifndef ASYNC_READ_NUMOF -#define ASYNC_READ_NUMOF 2 +#define ASYNC_READ_NUMOF 8 #endif /** diff --git a/examples/gnrc_border_router/Makefile.native.conf b/examples/gnrc_border_router/Makefile.native.conf index 4c15e517c27e..cf12c4399a2b 100644 --- a/examples/gnrc_border_router/Makefile.native.conf +++ b/examples/gnrc_border_router/Makefile.native.conf @@ -4,7 +4,6 @@ ZEP_PORT_BASE ?= 17754 ZEP_PORT_MAX := $(shell expr $(ZEP_PORT_BASE) + $(ZEP_DEVICES) - 1) CFLAGS += -DSOCKET_ZEP_MAX=$(ZEP_DEVICES) -CFLAGS += -DASYNC_READ_NUMOF=$(shell expr $(ZEP_DEVICES) + 1) CFLAGS += -DCONFIG_DHCPV6_CLIENT_PFX_LEASE_MAX=$(ZEP_DEVICES) # Set CFLAGS if not being set via Kconfig From 6927ec282f068d81eb10c76a2651d378b4dc81d5 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 2 Dec 2022 16:04:18 +0100 Subject: [PATCH 03/10] cpu/native: async_read: make sure not to close stdin on reboot --- cpu/native/async_read.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cpu/native/async_read.c b/cpu/native/async_read.c index 570553540651..b4f953b1c8cf 100644 --- a/cpu/native/async_read.c +++ b/cpu/native/async_read.c @@ -52,7 +52,10 @@ void native_async_read_cleanup(void) { unregister_interrupt(SIGIO); for (int i = 0; i < _next_index; i++) { - real_close(_fds[i].fd); + /* don't close stdin */ + if (_fds[i].fd != STDIN_FILENO) { + real_close(_fds[i].fd); + } if (pollers[i].child_pid) { kill(pollers[i].child_pid, SIGKILL); } From d932184e77b89991593e54189152c5b5efe42a4f Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 2 Dec 2022 16:05:06 +0100 Subject: [PATCH 04/10] tests/shell: drop test that only ever worked on native It's no longer working on native, but native behaves more like a real board now. --- tests/sys/shell/tests/01-run.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/sys/shell/tests/01-run.py b/tests/sys/shell/tests/01-run.py index fc9c081d0b3f..039a2949b86e 100755 --- a/tests/sys/shell/tests/01-run.py +++ b/tests/sys/shell/tests/01-run.py @@ -182,16 +182,6 @@ def check_line_canceling(child): assert garbage_expected == garbage_received -def check_erase_long_line(child, longline): - # FIXME: this only works on native, due to #10634 combined with socat - # insisting in line-buffering the terminal. - - if BOARD in ['native', 'native64']: - longline_erased = longline + "\b"*len(longline) + "echo" - child.sendline(longline_erased) - child.expect_exact('"echo"') - - def check_control_d(child): # The current shell instance was initiated by shell_run_once(). The shell will exit. child.sendline(CONTROL_D) @@ -220,8 +210,6 @@ def testfunc(child): else: print("skipping check_line_canceling()") - check_erase_long_line(child, longline) - check_control_d(child) # loop other defined commands and expected output From a9b8edf027337f1661329733b53d4a32f6027fb9 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 2 Feb 2023 19:52:44 +0100 Subject: [PATCH 05/10] tests/pkg_libfixmath_unittests: disable interactive sync on native --- tests/pkg/libfixmath_unittests/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/pkg/libfixmath_unittests/Makefile b/tests/pkg/libfixmath_unittests/Makefile index 6e6576f1f37a..5d8b6c2482f3 100644 --- a/tests/pkg/libfixmath_unittests/Makefile +++ b/tests/pkg/libfixmath_unittests/Makefile @@ -3,6 +3,7 @@ include ../Makefile.pkg_common USEMODULE += libfixmath-unittests ifneq (,$(filter native native64,$(BOARD))) + DISABLE_MODULE += test_utils_interactive_sync LINKFLAGS += -lm endif From 3fbf29cc579eaafc070ef1a95f789e70b82fc8b8 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 2 Feb 2023 19:53:30 +0100 Subject: [PATCH 06/10] tests/pkg_wolfssl: disable interactive sync on native --- tests/pkg/wolfssl/Makefile | 2 ++ tests/pkg/wolfssl/main.c | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/pkg/wolfssl/Makefile b/tests/pkg/wolfssl/Makefile index 671ea24b4bdb..e27c16056bcd 100644 --- a/tests/pkg/wolfssl/Makefile +++ b/tests/pkg/wolfssl/Makefile @@ -21,6 +21,8 @@ USEMODULE += xtimer ifeq (,$(filter native native64,$(BOARD))) CFLAGS += -DBENCH_EMBEDDED +else + DISABLE_MODULE += test_utils_interactive_sync endif TEST_ON_CI_WHITELIST += native native64 diff --git a/tests/pkg/wolfssl/main.c b/tests/pkg/wolfssl/main.c index 017418de701f..5813358353f6 100644 --- a/tests/pkg/wolfssl/main.c +++ b/tests/pkg/wolfssl/main.c @@ -33,10 +33,6 @@ int main(void) { LOG_INFO("wolfSSL Crypto Test!\n"); - /* Wait to work around a failing tests - * on platforms that don't have RTC synchronized - */ - xtimer_sleep(1); wolfcrypt_test(NULL); #ifdef MODULE_WOLFCRYPT_BENCHMARK LOG_INFO("wolfSSL Benchmark!\n"); From b61ec2dbeb2dfb7e6342ef6197c190c9bd066102 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Mon, 12 Jun 2023 16:24:36 +0200 Subject: [PATCH 07/10] tests/pkg/ubasic: don't use floating point math --- ...002-avoid-use-of-floating-point-math.patch | 37 +++++++++++++++++++ tests/pkg/ubasic/tests/01-run.py | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 pkg/ubasic/patches/0002-avoid-use-of-floating-point-math.patch diff --git a/pkg/ubasic/patches/0002-avoid-use-of-floating-point-math.patch b/pkg/ubasic/patches/0002-avoid-use-of-floating-point-math.patch new file mode 100644 index 000000000000..0cd2f28c8ec4 --- /dev/null +++ b/pkg/ubasic/patches/0002-avoid-use-of-floating-point-math.patch @@ -0,0 +1,37 @@ +From 5a492cf6ba56d2d16cfeaa18b6d1f0cb21429640 Mon Sep 17 00:00:00 2001 +From: Benjamin Valentin +Date: Mon, 12 Jun 2023 16:23:01 +0200 +Subject: [PATCH] avoid use of floating point math + +--- + tests.c | 6 +++--- + 1 file changed, 3 insertions(+), 3 deletions(-) + +diff --git a/tests.c b/tests.c +index a7bab89..77be22f 100644 +--- a/tests.c ++++ b/tests.c +@@ -93,7 +93,7 @@ void run(const char program[]) { + fflush(stdout); + + clock_t start_t, end_t; +- double delta_t; ++ unsigned delta_t; + + start_t = clock(); + +@@ -104,9 +104,9 @@ void run(const char program[]) { + } while(!ubasic_finished()); + + end_t = clock(); +- delta_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; ++ delta_t = (1000UL * (end_t - start_t)) / CLOCKS_PER_SEC; + +- printf("done. Run time: %.3f s\n", delta_t); ++ printf("done. Run time: %u ms\n", delta_t); + } + + /*---------------------------------------------------------------------------*/ +-- +2.39.2 + diff --git a/tests/pkg/ubasic/tests/01-run.py b/tests/pkg/ubasic/tests/01-run.py index 998a60392b4c..b05eb1f85879 100755 --- a/tests/pkg/ubasic/tests/01-run.py +++ b/tests/pkg/ubasic/tests/01-run.py @@ -17,7 +17,7 @@ def testfunc(child): for i in range(1, 6): - child.expect(r"Running test #%d... done. Run time: \d+.\d{3} s" % i, + child.expect(r"Running test #%d... done. Run time: \d+ ms" % i, timeout=TIMEOUT) From a7cfbb6953da5eb973f34a9f6d07e3ab7b32ac6c Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 8 Feb 2024 21:51:00 +0100 Subject: [PATCH 08/10] cpu/native: port stdio to new interface --- cpu/native/Makefile.dep | 4 ---- cpu/native/stdio_native/stdio_native.c | 25 +++++++++---------------- cpu/native/syscalls.c | 2 +- makefiles/stdio.inc.mk | 1 - sys/Kconfig.stdio | 1 - 5 files changed, 10 insertions(+), 23 deletions(-) diff --git a/cpu/native/Makefile.dep b/cpu/native/Makefile.dep index 384b75042a97..7fb477b19e61 100644 --- a/cpu/native/Makefile.dep +++ b/cpu/native/Makefile.dep @@ -17,10 +17,6 @@ ifeq (,$(filter stdio_%,$(USEMODULE))) USEMODULE += stdio_native endif -ifneq (,$(filter stdio_native,$(USEMODULE))) - USEMODULE += isrpipe -endif - ifneq (,$(filter periph_rtc,$(USEMODULE))) USEMODULE += ztimer USEMODULE += ztimer_msec diff --git a/cpu/native/stdio_native/stdio_native.c b/cpu/native/stdio_native/stdio_native.c index 907c68a67785..44c9457fa0d2 100644 --- a/cpu/native/stdio_native/stdio_native.c +++ b/cpu/native/stdio_native/stdio_native.c @@ -11,22 +11,15 @@ * * @file * @author Martine S. Lenders + * @author Benjamin Valentin */ #include "async_read.h" #include "kernel_defines.h" #include "native_internal.h" -#include "isrpipe.h" #include "stdio_base.h" -#ifndef STDIO_NATIVE_RX_BUFSIZE -#define STDIO_NATIVE_RX_BUFSIZE (64) -#endif - -static uint8_t _rx_buf_mem[STDIO_NATIVE_RX_BUFSIZE]; -static isrpipe_t stdio_isrpipe = ISRPIPE_INIT(_rx_buf_mem); - static void _async_read_wrapper(int fd, void *arg) { uint8_t buf[1]; @@ -38,20 +31,20 @@ static void _async_read_wrapper(int fd, void *arg) { native_async_read_continue(fd); } -void stdio_init(void) +static void _init(void) { native_async_read_setup(); - native_async_read_add_int_handler(STDIN_FILENO, &stdio_isrpipe, _async_read_wrapper); -} - -ssize_t stdio_read(void* buffer, size_t max_len) -{ - return (ssize_t)isrpipe_read(&stdio_isrpipe, buffer, max_len); + if (IS_USED(MODULE_STDIN)) { + native_async_read_add_int_handler(STDIN_FILENO, &stdin_isrpipe, + _async_read_wrapper); + } } -ssize_t stdio_write(const void* buffer, size_t len) +static ssize_t _write(const void* buffer, size_t len) { return real_write(STDOUT_FILENO, buffer, len); } +STDIO_PROVIDER(STDIO_NATIVE, _init, NULL, _write) + /** @} */ diff --git a/cpu/native/syscalls.c b/cpu/native/syscalls.c index 382183d23aa7..d2ce5a22caf8 100644 --- a/cpu/native/syscalls.c +++ b/cpu/native/syscalls.c @@ -229,7 +229,7 @@ ssize_t _native_read(int fd, void *buf, size_t count) { ssize_t r; - if (fd == STDIN_FILENO) { + if (fd == STDIN_FILENO && IS_USED(MODULE_STDIN)) { return stdio_read(buf, count); } diff --git a/makefiles/stdio.inc.mk b/makefiles/stdio.inc.mk index bfbb9feb708b..43dbcf7bef6f 100644 --- a/makefiles/stdio.inc.mk +++ b/makefiles/stdio.inc.mk @@ -17,7 +17,6 @@ STDIO_MODULES = \ STDIO_LEGACY_MODULES = \ ethos_stdio \ stdio_ethos \ - stdio_native # requires #19002 \ # # select stdio_uart if no other stdio module is slected diff --git a/sys/Kconfig.stdio b/sys/Kconfig.stdio index 1cce0d12a995..fe939cf0dadd 100644 --- a/sys/Kconfig.stdio +++ b/sys/Kconfig.stdio @@ -40,7 +40,6 @@ config MODULE_SLIPDEV_STDIO bool "SLIP network device" depends on MODULE_SLIPDEV select USE_STDOUT_BUFFERED - select MODULE_ISRPIPE config MODULE_STDIO_NULL bool "Null" From c65dd56eb65b9a300d5597d56db899af17ed3a11 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 8 Feb 2024 21:55:00 +0100 Subject: [PATCH 09/10] examples/telnet_server: enable alternative stdio method --- examples/telnet_server/Makefile | 7 +++++++ examples/telnet_server/main.c | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/examples/telnet_server/Makefile b/examples/telnet_server/Makefile index a865188ddf8d..43a1c4615378 100644 --- a/examples/telnet_server/Makefile +++ b/examples/telnet_server/Makefile @@ -35,6 +35,13 @@ USEMODULE += ps # Include the telnet server USEMODULE += stdio_telnet +# select a 2nd stdio method +ifeq (,$(filter native%,$(BOARD))) + USEMODULE += stdio_uart +else + USEMODULE += stdio_native +endif + # Enable faster re-connects CFLAGS += -DCONFIG_GNRC_TCP_EXPERIMENTAL_DYN_MSL_EN=1 diff --git a/examples/telnet_server/main.c b/examples/telnet_server/main.c index aed0a26fb36e..9e36799b9bd8 100644 --- a/examples/telnet_server/main.c +++ b/examples/telnet_server/main.c @@ -79,7 +79,6 @@ int main(void) /* start shell */ printf("All up, awaiting connection on port %u\n", CONFIG_TELNET_PORT); - puts("Local shell disabled"); char line_buf[SHELL_DEFAULT_BUFSIZE]; shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE); From 41ce809cfe43eadd0a2259edfee91c9b00e770e0 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 10 Feb 2024 15:31:07 +0100 Subject: [PATCH 10/10] sys/net/telnet: align API with stdio API --- sys/include/net/telnet.h | 2 +- sys/net/application_layer/telnet/telnet_server.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sys/include/net/telnet.h b/sys/include/net/telnet.h index 544a35872a84..6823206fed3e 100644 --- a/sys/include/net/telnet.h +++ b/sys/include/net/telnet.h @@ -72,7 +72,7 @@ int telnet_server_start(void); * * @return 0 on success, error otherwise */ -int telnet_server_write(const void* buffer, size_t len); +ssize_t telnet_server_write(const void* buffer, size_t len); /** * @brief Read data from the telnet client, will block until data is available. diff --git a/sys/net/application_layer/telnet/telnet_server.c b/sys/net/application_layer/telnet/telnet_server.c index 6512c8b2931a..5ff418cf6371 100644 --- a/sys/net/application_layer/telnet/telnet_server.c +++ b/sys/net/application_layer/telnet/telnet_server.c @@ -293,7 +293,7 @@ static void *telnet_thread(void *arg) return NULL; } -int telnet_server_write(const void* buffer, size_t len) +ssize_t telnet_server_write(const void* buffer, size_t len) { if (connected) { int res = _write_buffer(buffer, len);