Skip to content

Commit d11743b

Browse files
authored
Enable mpu stack for latest zephyr to enable app manager (#673)
And output detail info when install wasm app failed, update document and fix some compile warnings and errors. Signed-off-by: Wenyong Huang <[email protected]>
1 parent 5867527 commit d11743b

File tree

8 files changed

+117
-25
lines changed

8 files changed

+117
-25
lines changed

core/app-mgr/app-manager/module_wasm_app.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#include "event.h"
1414
#include "watchdog.h"
1515
#include "runtime_lib.h"
16+
#if WASM_ENABLE_INTERP != 0
1617
#include "wasm.h"
18+
#endif
1719
#if WASM_ENABLE_AOT != 0
1820
#include "aot_export.h"
1921
#endif
@@ -163,6 +165,15 @@ module_interface wasm_app_module_interface = {
163165
wasm_app_module_on_install_request_byte_arrive
164166
};
165167

168+
#if WASM_ENABLE_INTERP == 0
169+
static unsigned
170+
align_uint(unsigned v, unsigned b)
171+
{
172+
unsigned m = b - 1;
173+
return (v + m) & ~m;
174+
}
175+
#endif
176+
166177
static void
167178
exchange_uint32(uint8 *p_data)
168179
{
@@ -577,7 +588,7 @@ wasm_app_module_install(request_t * msg)
577588
char m_name[APP_NAME_MAX_LEN] = { 0 };
578589
char timeout_str[MAX_INT_STR_LEN] = { 0 };
579590
char heap_size_str[MAX_INT_STR_LEN] = { 0 };
580-
char timers_str[MAX_INT_STR_LEN] = { 0 }, err[256];
591+
char timers_str[MAX_INT_STR_LEN] = { 0 }, err[128], err_resp[256];
581592
#if WASM_ENABLE_LIBC_WASI != 0
582593
char wasi_dir_buf[PATH_MAX] = { 0 };
583594
const char *wasi_dir_list[] = { wasi_dir_buf };
@@ -651,8 +662,9 @@ wasm_app_module_install(request_t * msg)
651662
module = wasm_runtime_load_from_sections(aot_file->sections, true,
652663
err, err_size);
653664
if (!module) {
654-
SEND_ERR_RESPONSE(msg->mid,
655-
"Install WASM app failed: load WASM file failed.");
665+
snprintf(err_resp, sizeof(err_resp),
666+
"Install WASM app failed: %s", err);
667+
SEND_ERR_RESPONSE(msg->mid, err_resp);
656668
goto fail;
657669
}
658670
/* Destroy useless sections from list after load */
@@ -677,8 +689,9 @@ wasm_app_module_install(request_t * msg)
677689
/* Instantiate the AOT module */
678690
inst = wasm_runtime_instantiate(module, 0, heap_size, err, err_size);
679691
if (!inst) {
680-
SEND_ERR_RESPONSE(msg->mid,
681-
"Install WASM app failed: instantiate wasm runtime failed.");
692+
snprintf(err_resp, sizeof(err_resp),
693+
"Install WASM app failed: %s", err);
694+
SEND_ERR_RESPONSE(msg->mid, err);
682695
goto fail;
683696
}
684697
break;
@@ -715,8 +728,9 @@ wasm_app_module_install(request_t * msg)
715728
module = wasm_runtime_load_from_sections(bytecode_file->sections, false,
716729
err, err_size);
717730
if (!module) {
718-
SEND_ERR_RESPONSE(msg->mid,
719-
"Install WASM app failed: load WASM file failed.");
731+
snprintf(err_resp, sizeof(err_resp),
732+
"Install WASM app failed: %s", err);
733+
SEND_ERR_RESPONSE(msg->mid, err_resp);
720734
goto fail;
721735
}
722736

@@ -742,8 +756,9 @@ wasm_app_module_install(request_t * msg)
742756
/* Instantiate the wasm module */
743757
inst = wasm_runtime_instantiate(module, 0, heap_size, err, err_size);
744758
if (!inst) {
745-
SEND_ERR_RESPONSE(msg->mid,
746-
"Install WASM app failed: instantiate wasm runtime failed.");
759+
snprintf(err_resp, sizeof(err_resp),
760+
"Install WASM app failed: %s", err);
761+
SEND_ERR_RESPONSE(msg->mid, err_resp);
747762
goto fail;
748763
}
749764

core/shared/platform/zephyr/zephyr_thread.c

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,55 @@
1414
} \
1515
} while (0)
1616

17+
#if defined(CONFIG_ARM_MPU) || defined(CONFIG_ARC_MPU) \
18+
|| KERNEL_VERSION_NUMBER > 0x020300 /* version 2.3.0 */
19+
#define BH_ENABLE_ZEPHYR_MPU_STACK 1
20+
#elif !defined(BH_ENABLE_ZEPHYR_MPU_STACK)
21+
#define BH_ENABLE_ZEPHYR_MPU_STACK 0
22+
#endif
23+
#if !defined(BH_ZEPHYR_MPU_STACK_SIZE)
24+
#define BH_ZEPHYR_MPU_STACK_SIZE APP_THREAD_STACK_SIZE_MIN
25+
#endif
26+
#if !defined(BH_ZEPHYR_MPU_STACK_COUNT)
27+
#define BH_ZEPHYR_MPU_STACK_COUNT 4
28+
#endif
29+
30+
#if BH_ENABLE_ZEPHYR_MPU_STACK != 0
31+
static K_THREAD_STACK_ARRAY_DEFINE(mpu_stacks,
32+
BH_ZEPHYR_MPU_STACK_COUNT,
33+
BH_ZEPHYR_MPU_STACK_SIZE);
34+
static bool mpu_stack_allocated[BH_ZEPHYR_MPU_STACK_COUNT];
35+
static struct k_mutex mpu_stack_lock;
36+
37+
static char *mpu_stack_alloc()
38+
{
39+
int i;
40+
41+
k_mutex_lock(&mpu_stack_lock, K_FOREVER);
42+
for (i = 0; i < BH_ZEPHYR_MPU_STACK_COUNT; i++) {
43+
if (!mpu_stack_allocated[i]) {
44+
mpu_stack_allocated[i] = true;
45+
k_mutex_unlock(&mpu_stack_lock);
46+
return (char*)mpu_stacks[i];
47+
}
48+
}
49+
k_mutex_unlock(&mpu_stack_lock);
50+
return NULL;
51+
}
52+
53+
static void mpu_stack_free(char *stack)
54+
{
55+
int i;
56+
57+
k_mutex_lock(&mpu_stack_lock, K_FOREVER);
58+
for (i = 0; i < BH_ZEPHYR_MPU_STACK_COUNT; i++) {
59+
if ((char *)mpu_stacks[i] == stack)
60+
mpu_stack_allocated[i] = false;
61+
}
62+
k_mutex_unlock(&mpu_stack_lock);
63+
}
64+
#endif
65+
1766
typedef struct os_thread_wait_node {
1867
struct k_sem sem;
1968
os_thread_wait_list next;
@@ -32,8 +81,12 @@ typedef struct os_thread_data {
3281
os_thread_wait_list thread_wait_list;
3382
/* Thread stack size */
3483
unsigned stack_size;
84+
#if BH_ENABLE_ZEPHYR_MPU_STACK == 0
3585
/* Thread stack */
3686
char stack[1];
87+
#else
88+
char *stack;
89+
#endif
3790
} os_thread_data;
3891

3992
typedef struct os_thread_obj {
@@ -164,6 +217,9 @@ int os_thread_sys_init()
164217
if (is_thread_sys_inited)
165218
return BHT_OK;
166219

220+
#if BH_ENABLE_ZEPHYR_MPU_STACK != 0
221+
k_mutex_init(&mpu_stack_lock);
222+
#endif
167223
k_mutex_init(&thread_data_lock);
168224
k_mutex_init(&thread_obj_lock);
169225

@@ -214,6 +270,9 @@ static void os_thread_cleanup(void)
214270
/* Set flag to true for the next thread creating to
215271
free the thread object */
216272
((os_thread_obj*) thread_data->tid)->to_be_freed = true;
273+
#if BH_ENABLE_ZEPHYR_MPU_STACK != 0
274+
mpu_stack_free(thread_data->stack);
275+
#endif
217276
BH_FREE(thread_data);
218277
}
219278

@@ -253,37 +312,54 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
253312

254313
memset(tid, 0, sizeof(os_thread_obj));
255314

315+
/* Create and initialize thread data */
316+
#if BH_ENABLE_ZEPHYR_MPU_STACK == 0
256317
if (stack_size < APP_THREAD_STACK_SIZE_MIN)
257318
stack_size = APP_THREAD_STACK_SIZE_MIN;
258-
259-
/* Create and initialize thread data */
260319
thread_data_size = offsetof(os_thread_data, stack) + stack_size;
320+
#else
321+
stack_size = BH_ZEPHYR_MPU_STACK_SIZE;
322+
thread_data_size = sizeof(os_thread_data);
323+
#endif
261324
if (!(thread_data = BH_MALLOC(thread_data_size))) {
262-
BH_FREE(tid);
263-
return BHT_ERROR;
325+
goto fail1;
264326
}
265327

266328
memset(thread_data, 0, thread_data_size);
267329
k_mutex_init(&thread_data->wait_list_lock);
268330
thread_data->stack_size = stack_size;
269331
thread_data->tid = tid;
270332

333+
#if BH_ENABLE_ZEPHYR_MPU_STACK != 0
334+
if (!(thread_data->stack = mpu_stack_alloc())) {
335+
goto fail2;
336+
}
337+
#endif
338+
271339
/* Create the thread */
272340
if (!((tid = k_thread_create(tid, (k_thread_stack_t *)thread_data->stack,
273341
stack_size, os_thread_wrapper, start, arg,
274342
thread_data, prio, 0, K_NO_WAIT)))) {
275-
BH_FREE(tid);
276-
BH_FREE(thread_data);
277-
return BHT_ERROR;
343+
goto fail3;
278344
}
279345

280346
bh_assert(tid == thread_data->tid);
281347

282348
/* Set thread custom data */
283349
thread_data_list_add(thread_data);
284-
thread_obj_list_add((os_thread_obj*) tid);
350+
thread_obj_list_add((os_thread_obj*)tid);
285351
*p_tid = tid;
286352
return BHT_OK;
353+
354+
fail3:
355+
#if BH_ENABLE_ZEPHYR_MPU_STACK != 0
356+
mpu_stack_free(thread_data->stack);
357+
fail2:
358+
#endif
359+
BH_FREE(thread_data);
360+
fail1:
361+
BH_FREE(tid);
362+
return BHT_ERROR;
287363
}
288364

289365
korp_tid os_self_thread()

samples/gui/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Refer to [Zephyr getting started](https://docs.zephyrproject.org/latest/getting
7777
b. copy samples
7878
```bash
7979
cd zephyr/samples
80-
cp -a <wamr_root>samples/gui/wasm-runtime-wgl wasm-runtime-wgl
80+
cp -a <wamr_root>/samples/gui/wasm-runtime-wgl wasm-runtime-wgl
8181
cd wasm-runtime-wgl/zephyr_build
8282
```
8383
c. create a link to wamr root dir
@@ -123,7 +123,8 @@ Refer to [Zephyr getting started](https://docs.zephyrproject.org/latest/getting
123123
- Install WASM application to Zephyr using host_tool
124124
First, connect PC and STM32 with UART. Then install to use host_tool.
125125
```bash
126-
./host_tool -D /dev/ttyUSBXXX -i inc -f ui_increase.wasm
126+
sudo ./host_tool -D /dev/ttyUSBXXX -i inc -f ui_increase.wasm
127+
# /dev/ttyUSBXXX is the UART device, e.g. /dev/ttyUSB0
127128
```
128129

129130
- Install AOT version WASM application

samples/littlevgl/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ We can use a STM32 NUCLEO_F767ZI board with ILI9341 display and XPT2046 touch s
103103
b. copy samples
104104
```bash
105105
cd zephyr/samples/
106-
cp -a <wamr_root>samples/littlevgl/vgl-wasm-runtime vgl-wasm-runtime
106+
cp -a <wamr_root>/samples/littlevgl/vgl-wasm-runtime vgl-wasm-runtime
107107
cd vgl-wasm-runtime/zephyr_build
108108
```
109109
c. create a link to wamr root dir
@@ -161,7 +161,8 @@ d. build source code
161161
- Install WASM application to Zephyr using host_tool
162162
First, connect PC and STM32 with UART. Then install to use host_tool.
163163
```bash
164-
./host_tool -D /dev/ttyUSBXXX -i ui_app -f ui_app_builtin_libc.wasm
164+
sudo ./host_tool -D /dev/ttyUSBXXX -i ui_app -f ui_app_builtin_libc.wasm
165+
# /dev/ttyUSBXXX is the UART device, e.g. /dev/ttyUSB0
165166
```
166167
**Note**: WASI is unavailable on zephyr currently, so you have to use the ui_app_builtin_libc.wasm which doesn't depend on WASI.
167168

samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/display_ili9340.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct ili9340_data {
2626
struct device *spi_dev;
2727
struct spi_config spi_config;
2828
#ifdef DT_ILITEK_ILI9340_0_CS_GPIO_CONTROLLER
29-
struct spi_cs_control cs_ctrl;
29+
struct spi_cs_control cs_ctrl;
3030
#endif
3131
};
3232

samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/iwasm_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ host_interface interface = {
7676

7777
timer_ctx_t timer_ctx;
7878

79-
static char global_heap_buf[368 * 1024] = { 0 };
79+
static char global_heap_buf[350 * 1024] = { 0 };
8080

8181
static NativeSymbol native_symbols[] = {
8282
EXPORT_WASM_API_WITH_SIG(display_input_read, "(*)i"),

samples/littlevgl/vgl-wasm-runtime/zephyr-build/prj.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CONFIG_SPI=y
22
CONFIG_SPI_STM32=y
3-
CONFIG_SPI_1=y
43
CONFIG_PRINTK=y
54
CONFIG_LOG=y
65
#CONFIG_UART_2=y

test-tools/host-tool/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ static void output_event(request_t *obj)
750750

751751
int main(int argc, char *argv[])
752752
{
753-
int ret;
753+
int ret = -1;
754754
imrt_link_recv_context_t recv_ctx = { 0 };
755755
char buffer[BUF_SIZE] = { 0 };
756756
uint32_t last_check = 0, total_elpased_ms = 0;

0 commit comments

Comments
 (0)