Skip to content

Commit 5181deb

Browse files
kr-tdkouba-atym
andauthored
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 Signed-off-by: Krisztian Szilvasi <34309983+kr-t@users.noreply.github.com> Co-authored-by: Dan Kouba <dan@atym.io>
1 parent 8ab65d6 commit 5181deb

4 files changed

Lines changed: 52 additions & 148 deletions

File tree

boards/b_u585i_iot02a.conf

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ CONFIG_ARM_MPU=y
33
CONFIG_MAIN_STACK_SIZE=8192
44
CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=-1
55

6+
# PSRAM
7+
CONFIG_MEMC=y
8+
9+
# Container defaults
10+
CONFIG_MAX_CONTAINERS=5
11+
CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE=8388608
12+
613
# Bus interfaces
714
CONFIG_GPIO=y
815
CONFIG_I2C=y
@@ -36,19 +43,8 @@ CONFIG_OCRE_GPIO_MAX_PORTS=8
3643
CONFIG_OCRE_GPIO_PINS_PER_PORT=16
3744
CONFIG_OCRE_GPIO_MAX_PINS=256
3845

39-
CONFIG_MEMC=y
40-
CONFIG_SPI_NOR=y
41-
CONFIG_FLASH=y
42-
CONFIG_FLASH_MAP=y
43-
44-
4546
CONFIG_CODE_DATA_RELOCATION=n
4647

47-
CONFIG_LINKER_LAST_SECTION_ID=y
48-
49-
CONFIG_MM_DRV=y
50-
CONFIG_MM_DRV_PAGE_SIZE=4096
51-
5248
# Networking options
5349
CONFIG_NET_TCP=y
5450
CONFIG_NET_MAX_CONN=10

boards/b_u585i_iot02a.overlay

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
/ {
44
aliases {
5-
psram0 = &psram;
65
rng0 = &rng_device;
76
led0 = &green_led_1;
87
led1 = &red_led_1;
@@ -40,118 +39,6 @@
4039
status = "okay";
4140
device_list = <&rng_device &ism330dhcx &lps22hh &hts221 &iis2mdc &veml6030>;
4241
};
43-
44-
reserved-memory {
45-
#address-cells = <1>;
46-
#size-cells = <1>;
47-
ranges;
48-
49-
psram0: psram@90000000 {
50-
compatible = "zephyr,memory-region";
51-
reg = <0x90000000 0x00800000>;
52-
zephyr,memory-region = "PSRAM";
53-
};
54-
};
55-
56-
chosen {
57-
zephyr,memory-region = &psram0;
58-
};
59-
};
60-
61-
&octospi1 {
62-
status = "okay";
63-
pinctrl-0 = <&octospi1_clk &octospi1_ncs &octospi1_dqs
64-
&octospi1_io0 &octospi1_io1 &octospi1_io2 &octospi1_io3
65-
&octospi1_io4 &octospi1_io5 &octospi1_io6 &octospi1_io7>;
66-
pinctrl-names = "default";
67-
68-
psram: psram@0 {
69-
compatible = "st,stm32-ospi-psram";
70-
reg = <0x0 0x00800000>;
71-
st,ospi-mode = <3>; // Octal mode
72-
st,ospi-clk-frequency = <104>; // MHz
73-
st,ospi-chip-select = <0>; // CS0
74-
};
75-
};
76-
77-
/* OCTOSPI1 pinmux setup */
78-
&pinctrl {
79-
octospi1_clk: octospi1_clk {
80-
pinmux = <STM32_PINMUX('E', 10, AF10)>;
81-
bias-disable;
82-
drive-push-pull;
83-
slew-rate = "very-high-speed";
84-
};
85-
86-
octospi1_ncs: octospi1_ncs {
87-
pinmux = <STM32_PINMUX('E', 11, AF10)>;
88-
bias-disable;
89-
drive-push-pull;
90-
slew-rate = "very-high-speed";
91-
};
92-
93-
octospi1_dqs: octospi1_dqs {
94-
pinmux = <STM32_PINMUX('E', 12, AF10)>;
95-
bias-disable;
96-
drive-push-pull;
97-
slew-rate = "very-high-speed";
98-
};
99-
100-
octospi1_io0: octospi1_io0 {
101-
pinmux = <STM32_PINMUX('E', 7, AF10)>;
102-
bias-disable;
103-
drive-push-pull;
104-
slew-rate = "very-high-speed";
105-
};
106-
107-
octospi1_io1: octospi1_io1 {
108-
pinmux = <STM32_PINMUX('E', 8, AF10)>;
109-
bias-disable;
110-
drive-push-pull;
111-
slew-rate = "very-high-speed";
112-
};
113-
114-
octospi1_io2: octospi1_io2 {
115-
pinmux = <STM32_PINMUX('E', 9, AF10)>;
116-
bias-disable;
117-
drive-push-pull;
118-
slew-rate = "very-high-speed";
119-
};
120-
121-
octospi1_io3: octospi1_io3 {
122-
pinmux = <STM32_PINMUX('E', 13, AF10)>;
123-
bias-disable;
124-
drive-push-pull;
125-
slew-rate = "very-high-speed";
126-
};
127-
128-
octospi1_io4: octospi1_io4 {
129-
pinmux = <STM32_PINMUX('E', 14, AF10)>;
130-
bias-disable;
131-
drive-push-pull;
132-
slew-rate = "very-high-speed";
133-
};
134-
135-
octospi1_io5: octospi1_io5 {
136-
pinmux = <STM32_PINMUX('E', 15, AF10)>;
137-
bias-disable;
138-
drive-push-pull;
139-
slew-rate = "very-high-speed";
140-
};
141-
142-
octospi1_io6: octospi1_io6 {
143-
pinmux = <STM32_PINMUX('E', 2, AF10)>;
144-
bias-disable;
145-
drive-push-pull;
146-
slew-rate = "very-high-speed";
147-
};
148-
149-
octospi1_io7: octospi1_io7 {
150-
pinmux = <STM32_PINMUX('D', 7, AF10)>;
151-
bias-disable;
152-
drive-push-pull;
153-
slew-rate = "very-high-speed";
154-
};
15542
};
15643

15744
// Add labels for sensors defined in the board .dtsi file, and any other user configuration

src/ocre/components/container_supervisor/cs_sm_impl.c

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
#ifdef CONFIG_OCRE_CONTAINER_MESSAGING
1717
#include "ocre_messaging/ocre_messaging.h"
1818
#endif
19-
#if defined(CONFIG_OCRE_TIMER) || defined(CONFIG_OCRE_GPIO) || defined(CONFIG_OCRE_SENSORS)
19+
#if defined(CONFIG_OCRE_TIMER) || defined(CONFIG_OCRE_GPIO) || defined(CONFIG_OCRE_SENSORS) || \
20+
defined(CONFIG_OCRE_CONTAINER_MESSAGING)
2021
#include "api/ocre_common.h"
2122
#endif
2223

@@ -33,6 +34,16 @@ LOG_MODULE_DECLARE(ocre_cs_component, OCRE_LOG_LEVEL);
3334
#include "cs_sm.h"
3435
#include "cs_sm_impl.h"
3536

37+
// External RAM support for WAMR heap on boards that have it
38+
#if defined(CONFIG_MEMC)
39+
#if defined(CONFIG_BOARD_ARDUINO_PORTENTA_H7)
40+
__attribute__((section("SDRAM1"), aligned(32)))
41+
#elif defined(CONFIG_BOARD_B_U585I_IOT02A)
42+
__attribute__((section(".stm32_psram"), aligned(32)))
43+
#elif defined(CONFIG_BOARD_MIMXRT1064_EVK)
44+
__attribute__((section("SDRAM"), aligned(32)))
45+
#endif // defined (<board>)
46+
#endif // defined(CONFIG_MEMC)
3647
static char wamr_heap_buf[CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE] = {0};
3748

3849
// Thread pool for container execution
@@ -329,36 +340,35 @@ ocre_container_status_t CS_run_container(ocre_container_t *container) {
329340
return CONTAINER_STATUS_RUNNING;
330341
}
331342

332-
#ifdef CONFIG_OCRE_NETWORKING
333-
#define ADDRESS_POOL_SIZE 1
334-
const char *addr_pool[ADDRESS_POOL_SIZE] = {
335-
"0.0.0.0/0",
336-
};
337-
wasm_runtime_set_wasi_addr_pool(curr_container_arguments->module, addr_pool, ADDRESS_POOL_SIZE);
338-
#endif
339-
340-
#ifdef CONFIG_OCRE_CONTAINER_FILESYSTEM
341-
// Simple for now: map CONTAINER_FS_PATH to /
342-
// TODO: eventually every container should probably have its own root folder,
343-
// however wasm_runtime_set_wasi_args expects constant values.
344-
#define DIR_LIST_SIZE 1
345-
static const char *dir_map_list[DIR_LIST_SIZE] = {
346-
"/::" CONTAINER_FS_PATH
347-
};
348-
wasm_runtime_set_wasi_args(curr_container_arguments->module,
349-
NULL, 0,
350-
dir_map_list, DIR_LIST_SIZE,
351-
NULL, 0, NULL, 0);
352-
#endif
353-
354-
355343
if (container->container_runtime_status != CONTAINER_STATUS_CREATED &&
356344
container->container_runtime_status != CONTAINER_STATUS_STOPPED) {
357345
LOG_ERR("Container (ID: %d), is not in a valid state to run", curr_container_ID);
358346
container->container_runtime_status = CONTAINER_STATUS_ERROR;
359347
return CONTAINER_STATUS_ERROR;
360348
}
361349

350+
#ifdef CONFIG_OCRE_NETWORKING
351+
#define ADDRESS_POOL_SIZE 1
352+
const char *addr_pool[ADDRESS_POOL_SIZE] = {
353+
"0.0.0.0/0",
354+
};
355+
wasm_runtime_set_wasi_addr_pool(curr_container_arguments->module, addr_pool, ADDRESS_POOL_SIZE);
356+
#endif
357+
358+
#ifdef CONFIG_OCRE_CONTAINER_FILESYSTEM
359+
// Simple for now: map CONTAINER_FS_PATH to /
360+
// TODO: eventually every container should probably have its own root folder,
361+
// however wasm_runtime_set_wasi_args expects constant values.
362+
#define DIR_LIST_SIZE 1
363+
static const char *dir_map_list[DIR_LIST_SIZE] = {
364+
"/::" CONTAINER_FS_PATH
365+
};
366+
wasm_runtime_set_wasi_args(curr_container_arguments->module,
367+
NULL, 0,
368+
dir_map_list, DIR_LIST_SIZE,
369+
NULL, 0, NULL, 0);
370+
#endif
371+
362372
if (curr_container_arguments->module_inst) {
363373
LOG_INF("WASM runtime already instantiated for container:%d", curr_container_ID);
364374
} else {
@@ -446,7 +456,17 @@ ocre_container_status_t CS_stop_container(ocre_container_t *container, ocre_cont
446456

447457
for (int i = 0; i < CONTAINER_THREAD_POOL_SIZE; i++) {
448458
if (container_thread_active[i] && container_threads[i].user_options == curr_container_ID) {
459+
#if defined(CONFIG_OCRE_CONTAINER_WAMR_TERMINATION)
460+
/**
461+
* wasm_runtime_terminate uses POSIX signals to terminate the thread from the outside; calling core_thread_destroy
462+
* would try to destroy again the thread, and pthread_join() will never return, while freeing the stack would cause
463+
* segfault. This separation is needed to distinguish platform supported by wamr with this features,
464+
* from those which aren't. Since this function exists on those platforms, but stubbed, config parameter is used.
465+
*/
466+
wasm_runtime_terminate(curr_container_arguments->module_inst);
467+
#else
449468
core_thread_destroy(&container_threads[i]);
469+
#endif
450470
container_thread_active[i] = false;
451471
}
452472
}

src/shared/platform/posix/core_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
//#define CONFIG_OCRE_CONTAINER_MESSAGING /*!< Enable container messaging support */
2121
#define CONFIG_OCRE_NETWORKING /*!< Enable networking support */
2222
#define CONFIG_OCRE_CONTAINER_FILESYSTEM
23+
#define CONFIG_OCRE_CONTAINER_WAMR_TERMINATION
2324

2425
// Base paths for the application
2526
#define OCRE_BASE_PATH "./ocre" /*!< Base directory for Ocre resources */

0 commit comments

Comments
 (0)