From f9f16ac135cd085e6527b508339e27826a8b4849 Mon Sep 17 00:00:00 2001 From: Krisztian Szilvasi <34309983+kr-t@users.noreply.github.com> Date: Mon, 1 Sep 2025 14:00:22 +0200 Subject: [PATCH] Improve Linux shutdown, memory handling, and messaging integration - Fixed shutdown behavior on Linux systems to ensure proper termination - Placed WAMR heap in external RAM when enabled for better memory management - Added validity checks before accessing module parameters to prevent crashes - Included `ocre_common.h` when messaging is defined to support conditional compilation Co-authored-by: Dan Kouba Signed-off-by: Krisztian Szilvasi <34309983+kr-t@users.noreply.github.com> --- boards/b_u585i_iot02a.conf | 18 ++- boards/b_u585i_iot02a.overlay | 113 ------------------ .../container_supervisor/cs_sm_impl.c | 68 +++++++---- src/shared/platform/posix/core_internal.h | 1 + 4 files changed, 52 insertions(+), 148 deletions(-) diff --git a/boards/b_u585i_iot02a.conf b/boards/b_u585i_iot02a.conf index 9144e257..92d3ea9e 100644 --- a/boards/b_u585i_iot02a.conf +++ b/boards/b_u585i_iot02a.conf @@ -3,6 +3,13 @@ CONFIG_ARM_MPU=y CONFIG_MAIN_STACK_SIZE=8192 CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=-1 +# PSRAM +CONFIG_MEMC=y + +# Container defaults +CONFIG_MAX_CONTAINERS=5 +CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE=8388608 + # Bus interfaces CONFIG_GPIO=y CONFIG_I2C=y @@ -36,19 +43,8 @@ CONFIG_OCRE_GPIO_MAX_PORTS=8 CONFIG_OCRE_GPIO_PINS_PER_PORT=16 CONFIG_OCRE_GPIO_MAX_PINS=256 -CONFIG_MEMC=y -CONFIG_SPI_NOR=y -CONFIG_FLASH=y -CONFIG_FLASH_MAP=y - - CONFIG_CODE_DATA_RELOCATION=n -CONFIG_LINKER_LAST_SECTION_ID=y - -CONFIG_MM_DRV=y -CONFIG_MM_DRV_PAGE_SIZE=4096 - # Networking options CONFIG_NET_TCP=y CONFIG_NET_MAX_CONN=10 diff --git a/boards/b_u585i_iot02a.overlay b/boards/b_u585i_iot02a.overlay index 88c82162..c838f7ba 100644 --- a/boards/b_u585i_iot02a.overlay +++ b/boards/b_u585i_iot02a.overlay @@ -2,7 +2,6 @@ / { aliases { - psram0 = &psram; rng0 = &rng_device; led0 = &green_led_1; led1 = &red_led_1; @@ -40,118 +39,6 @@ status = "okay"; device_list = <&rng_device &ism330dhcx &lps22hh &hts221 &iis2mdc &veml6030>; }; - - reserved-memory { - #address-cells = <1>; - #size-cells = <1>; - ranges; - - psram0: psram@90000000 { - compatible = "zephyr,memory-region"; - reg = <0x90000000 0x00800000>; - zephyr,memory-region = "PSRAM"; - }; - }; - - chosen { - zephyr,memory-region = &psram0; - }; -}; - -&octospi1 { - status = "okay"; - pinctrl-0 = <&octospi1_clk &octospi1_ncs &octospi1_dqs - &octospi1_io0 &octospi1_io1 &octospi1_io2 &octospi1_io3 - &octospi1_io4 &octospi1_io5 &octospi1_io6 &octospi1_io7>; - pinctrl-names = "default"; - - psram: psram@0 { - compatible = "st,stm32-ospi-psram"; - reg = <0x0 0x00800000>; - st,ospi-mode = <3>; // Octal mode - st,ospi-clk-frequency = <104>; // MHz - st,ospi-chip-select = <0>; // CS0 - }; -}; - -/* OCTOSPI1 pinmux setup */ -&pinctrl { - octospi1_clk: octospi1_clk { - pinmux = ; - bias-disable; - drive-push-pull; - slew-rate = "very-high-speed"; - }; - - octospi1_ncs: octospi1_ncs { - pinmux = ; - bias-disable; - drive-push-pull; - slew-rate = "very-high-speed"; - }; - - octospi1_dqs: octospi1_dqs { - pinmux = ; - bias-disable; - drive-push-pull; - slew-rate = "very-high-speed"; - }; - - octospi1_io0: octospi1_io0 { - pinmux = ; - bias-disable; - drive-push-pull; - slew-rate = "very-high-speed"; - }; - - octospi1_io1: octospi1_io1 { - pinmux = ; - bias-disable; - drive-push-pull; - slew-rate = "very-high-speed"; - }; - - octospi1_io2: octospi1_io2 { - pinmux = ; - bias-disable; - drive-push-pull; - slew-rate = "very-high-speed"; - }; - - octospi1_io3: octospi1_io3 { - pinmux = ; - bias-disable; - drive-push-pull; - slew-rate = "very-high-speed"; - }; - - octospi1_io4: octospi1_io4 { - pinmux = ; - bias-disable; - drive-push-pull; - slew-rate = "very-high-speed"; - }; - - octospi1_io5: octospi1_io5 { - pinmux = ; - bias-disable; - drive-push-pull; - slew-rate = "very-high-speed"; - }; - - octospi1_io6: octospi1_io6 { - pinmux = ; - bias-disable; - drive-push-pull; - slew-rate = "very-high-speed"; - }; - - octospi1_io7: octospi1_io7 { - pinmux = ; - bias-disable; - drive-push-pull; - slew-rate = "very-high-speed"; - }; }; // Add labels for sensors defined in the board .dtsi file, and any other user configuration diff --git a/src/ocre/components/container_supervisor/cs_sm_impl.c b/src/ocre/components/container_supervisor/cs_sm_impl.c index d603332d..31936185 100644 --- a/src/ocre/components/container_supervisor/cs_sm_impl.c +++ b/src/ocre/components/container_supervisor/cs_sm_impl.c @@ -16,7 +16,8 @@ #ifdef CONFIG_OCRE_CONTAINER_MESSAGING #include "ocre_messaging/ocre_messaging.h" #endif -#if defined(CONFIG_OCRE_TIMER) || defined(CONFIG_OCRE_GPIO) || defined(CONFIG_OCRE_SENSORS) +#if defined(CONFIG_OCRE_TIMER) || defined(CONFIG_OCRE_GPIO) || defined(CONFIG_OCRE_SENSORS) || \ + defined(CONFIG_OCRE_CONTAINER_MESSAGING) #include "api/ocre_common.h" #endif @@ -33,6 +34,16 @@ LOG_MODULE_DECLARE(ocre_cs_component, OCRE_LOG_LEVEL); #include "cs_sm.h" #include "cs_sm_impl.h" +// External RAM support for WAMR heap on boards that have it +#if defined(CONFIG_MEMC) + #if defined(CONFIG_BOARD_ARDUINO_PORTENTA_H7) + __attribute__((section("SDRAM1"), aligned(32))) + #elif defined(CONFIG_BOARD_B_U585I_IOT02A) + __attribute__((section(".stm32_psram"), aligned(32))) + #elif defined(CONFIG_BOARD_MIMXRT1064_EVK) + __attribute__((section("SDRAM"), aligned(32))) + #endif // defined () +#endif // defined(CONFIG_MEMC) static char wamr_heap_buf[CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE] = {0}; // Thread pool for container execution @@ -329,29 +340,6 @@ ocre_container_status_t CS_run_container(ocre_container_t *container) { return CONTAINER_STATUS_RUNNING; } -#ifdef CONFIG_OCRE_NETWORKING -#define ADDRESS_POOL_SIZE 1 - const char *addr_pool[ADDRESS_POOL_SIZE] = { - "0.0.0.0/0", - }; - wasm_runtime_set_wasi_addr_pool(curr_container_arguments->module, addr_pool, ADDRESS_POOL_SIZE); -#endif - -#ifdef CONFIG_OCRE_CONTAINER_FILESYSTEM -// Simple for now: map CONTAINER_FS_PATH to / -// TODO: eventually every container should probably have its own root folder, -// however wasm_runtime_set_wasi_args expects constant values. -#define DIR_LIST_SIZE 1 - static const char *dir_map_list[DIR_LIST_SIZE] = { - "/::" CONTAINER_FS_PATH - }; - wasm_runtime_set_wasi_args(curr_container_arguments->module, - NULL, 0, - dir_map_list, DIR_LIST_SIZE, - NULL, 0, NULL, 0); -#endif - - if (container->container_runtime_status != CONTAINER_STATUS_CREATED && container->container_runtime_status != CONTAINER_STATUS_STOPPED) { LOG_ERR("Container (ID: %d), is not in a valid state to run", curr_container_ID); @@ -359,6 +347,28 @@ ocre_container_status_t CS_run_container(ocre_container_t *container) { return CONTAINER_STATUS_ERROR; } + #ifdef CONFIG_OCRE_NETWORKING + #define ADDRESS_POOL_SIZE 1 + const char *addr_pool[ADDRESS_POOL_SIZE] = { + "0.0.0.0/0", + }; + wasm_runtime_set_wasi_addr_pool(curr_container_arguments->module, addr_pool, ADDRESS_POOL_SIZE); + #endif + + #ifdef CONFIG_OCRE_CONTAINER_FILESYSTEM + // Simple for now: map CONTAINER_FS_PATH to / + // TODO: eventually every container should probably have its own root folder, + // however wasm_runtime_set_wasi_args expects constant values. + #define DIR_LIST_SIZE 1 + static const char *dir_map_list[DIR_LIST_SIZE] = { + "/::" CONTAINER_FS_PATH + }; + wasm_runtime_set_wasi_args(curr_container_arguments->module, + NULL, 0, + dir_map_list, DIR_LIST_SIZE, + NULL, 0, NULL, 0); + #endif + if (curr_container_arguments->module_inst) { LOG_INF("WASM runtime already instantiated for container:%d", curr_container_ID); } else { @@ -446,7 +456,17 @@ ocre_container_status_t CS_stop_container(ocre_container_t *container, ocre_cont for (int i = 0; i < CONTAINER_THREAD_POOL_SIZE; i++) { if (container_thread_active[i] && container_threads[i].user_options == curr_container_ID) { +#if defined(CONFIG_OCRE_CONTAINER_WAMR_TERMINATION) +/** + * wasm_runtime_terminate uses POSIX signals to terminate the thread from the outside; calling core_thread_destroy + * would try to destroy again the thread, and pthread_join() will never return, while freeing the stack would cause + * segfault. This separation is needed to distinguish platform supported by wamr with this features, + * from those which aren't. Since this function exists on those platforms, but stubbed, config parameter is used. + */ + wasm_runtime_terminate(curr_container_arguments->module_inst); +#else core_thread_destroy(&container_threads[i]); +#endif container_thread_active[i] = false; } } diff --git a/src/shared/platform/posix/core_internal.h b/src/shared/platform/posix/core_internal.h index a76e7a95..9f26c80e 100644 --- a/src/shared/platform/posix/core_internal.h +++ b/src/shared/platform/posix/core_internal.h @@ -20,6 +20,7 @@ //#define CONFIG_OCRE_CONTAINER_MESSAGING /*!< Enable container messaging support */ #define CONFIG_OCRE_NETWORKING /*!< Enable networking support */ #define CONFIG_OCRE_CONTAINER_FILESYSTEM +#define CONFIG_OCRE_CONTAINER_WAMR_TERMINATION // Base paths for the application #define OCRE_BASE_PATH "./ocre" /*!< Base directory for Ocre resources */