diff --git a/core/app-mgr/app-manager/module_wasm_app.c b/core/app-mgr/app-manager/module_wasm_app.c index 72e1f5e6f..a3e7a2a76 100644 --- a/core/app-mgr/app-manager/module_wasm_app.c +++ b/core/app-mgr/app-manager/module_wasm_app.c @@ -13,7 +13,9 @@ #include "event.h" #include "watchdog.h" #include "runtime_lib.h" +#if WASM_ENABLE_INTERP != 0 #include "wasm.h" +#endif #if WASM_ENABLE_AOT != 0 #include "aot_export.h" #endif @@ -163,6 +165,15 @@ module_interface wasm_app_module_interface = { wasm_app_module_on_install_request_byte_arrive }; +#if WASM_ENABLE_INTERP == 0 +static unsigned +align_uint(unsigned v, unsigned b) +{ + unsigned m = b - 1; + return (v + m) & ~m; +} +#endif + static void exchange_uint32(uint8 *p_data) { @@ -577,7 +588,7 @@ wasm_app_module_install(request_t * msg) char m_name[APP_NAME_MAX_LEN] = { 0 }; char timeout_str[MAX_INT_STR_LEN] = { 0 }; char heap_size_str[MAX_INT_STR_LEN] = { 0 }; - char timers_str[MAX_INT_STR_LEN] = { 0 }, err[256]; + char timers_str[MAX_INT_STR_LEN] = { 0 }, err[128], err_resp[256]; #if WASM_ENABLE_LIBC_WASI != 0 char wasi_dir_buf[PATH_MAX] = { 0 }; const char *wasi_dir_list[] = { wasi_dir_buf }; @@ -651,8 +662,9 @@ wasm_app_module_install(request_t * msg) module = wasm_runtime_load_from_sections(aot_file->sections, true, err, err_size); if (!module) { - SEND_ERR_RESPONSE(msg->mid, - "Install WASM app failed: load WASM file failed."); + snprintf(err_resp, sizeof(err_resp), + "Install WASM app failed: %s", err); + SEND_ERR_RESPONSE(msg->mid, err_resp); goto fail; } /* Destroy useless sections from list after load */ @@ -677,8 +689,9 @@ wasm_app_module_install(request_t * msg) /* Instantiate the AOT module */ inst = wasm_runtime_instantiate(module, 0, heap_size, err, err_size); if (!inst) { - SEND_ERR_RESPONSE(msg->mid, - "Install WASM app failed: instantiate wasm runtime failed."); + snprintf(err_resp, sizeof(err_resp), + "Install WASM app failed: %s", err); + SEND_ERR_RESPONSE(msg->mid, err); goto fail; } break; @@ -715,8 +728,9 @@ wasm_app_module_install(request_t * msg) module = wasm_runtime_load_from_sections(bytecode_file->sections, false, err, err_size); if (!module) { - SEND_ERR_RESPONSE(msg->mid, - "Install WASM app failed: load WASM file failed."); + snprintf(err_resp, sizeof(err_resp), + "Install WASM app failed: %s", err); + SEND_ERR_RESPONSE(msg->mid, err_resp); goto fail; } @@ -742,8 +756,9 @@ wasm_app_module_install(request_t * msg) /* Instantiate the wasm module */ inst = wasm_runtime_instantiate(module, 0, heap_size, err, err_size); if (!inst) { - SEND_ERR_RESPONSE(msg->mid, - "Install WASM app failed: instantiate wasm runtime failed."); + snprintf(err_resp, sizeof(err_resp), + "Install WASM app failed: %s", err); + SEND_ERR_RESPONSE(msg->mid, err_resp); goto fail; } diff --git a/core/shared/platform/zephyr/zephyr_thread.c b/core/shared/platform/zephyr/zephyr_thread.c index 932255195..55cadb47c 100644 --- a/core/shared/platform/zephyr/zephyr_thread.c +++ b/core/shared/platform/zephyr/zephyr_thread.c @@ -14,6 +14,55 @@ } \ } while (0) +#if defined(CONFIG_ARM_MPU) || defined(CONFIG_ARC_MPU) \ + || KERNEL_VERSION_NUMBER > 0x020300 /* version 2.3.0 */ +#define BH_ENABLE_ZEPHYR_MPU_STACK 1 +#elif !defined(BH_ENABLE_ZEPHYR_MPU_STACK) +#define BH_ENABLE_ZEPHYR_MPU_STACK 0 +#endif +#if !defined(BH_ZEPHYR_MPU_STACK_SIZE) +#define BH_ZEPHYR_MPU_STACK_SIZE APP_THREAD_STACK_SIZE_MIN +#endif +#if !defined(BH_ZEPHYR_MPU_STACK_COUNT) +#define BH_ZEPHYR_MPU_STACK_COUNT 4 +#endif + +#if BH_ENABLE_ZEPHYR_MPU_STACK != 0 +static K_THREAD_STACK_ARRAY_DEFINE(mpu_stacks, + BH_ZEPHYR_MPU_STACK_COUNT, + BH_ZEPHYR_MPU_STACK_SIZE); +static bool mpu_stack_allocated[BH_ZEPHYR_MPU_STACK_COUNT]; +static struct k_mutex mpu_stack_lock; + +static char *mpu_stack_alloc() +{ + int i; + + k_mutex_lock(&mpu_stack_lock, K_FOREVER); + for (i = 0; i < BH_ZEPHYR_MPU_STACK_COUNT; i++) { + if (!mpu_stack_allocated[i]) { + mpu_stack_allocated[i] = true; + k_mutex_unlock(&mpu_stack_lock); + return (char*)mpu_stacks[i]; + } + } + k_mutex_unlock(&mpu_stack_lock); + return NULL; +} + +static void mpu_stack_free(char *stack) +{ + int i; + + k_mutex_lock(&mpu_stack_lock, K_FOREVER); + for (i = 0; i < BH_ZEPHYR_MPU_STACK_COUNT; i++) { + if ((char *)mpu_stacks[i] == stack) + mpu_stack_allocated[i] = false; + } + k_mutex_unlock(&mpu_stack_lock); +} +#endif + typedef struct os_thread_wait_node { struct k_sem sem; os_thread_wait_list next; @@ -32,8 +81,12 @@ typedef struct os_thread_data { os_thread_wait_list thread_wait_list; /* Thread stack size */ unsigned stack_size; +#if BH_ENABLE_ZEPHYR_MPU_STACK == 0 /* Thread stack */ char stack[1]; +#else + char *stack; +#endif } os_thread_data; typedef struct os_thread_obj { @@ -164,6 +217,9 @@ int os_thread_sys_init() if (is_thread_sys_inited) return BHT_OK; +#if BH_ENABLE_ZEPHYR_MPU_STACK != 0 + k_mutex_init(&mpu_stack_lock); +#endif k_mutex_init(&thread_data_lock); k_mutex_init(&thread_obj_lock); @@ -214,6 +270,9 @@ static void os_thread_cleanup(void) /* Set flag to true for the next thread creating to free the thread object */ ((os_thread_obj*) thread_data->tid)->to_be_freed = true; +#if BH_ENABLE_ZEPHYR_MPU_STACK != 0 + mpu_stack_free(thread_data->stack); +#endif BH_FREE(thread_data); } @@ -253,14 +312,17 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, memset(tid, 0, sizeof(os_thread_obj)); + /* Create and initialize thread data */ +#if BH_ENABLE_ZEPHYR_MPU_STACK == 0 if (stack_size < APP_THREAD_STACK_SIZE_MIN) stack_size = APP_THREAD_STACK_SIZE_MIN; - - /* Create and initialize thread data */ thread_data_size = offsetof(os_thread_data, stack) + stack_size; +#else + stack_size = BH_ZEPHYR_MPU_STACK_SIZE; + thread_data_size = sizeof(os_thread_data); +#endif if (!(thread_data = BH_MALLOC(thread_data_size))) { - BH_FREE(tid); - return BHT_ERROR; + goto fail1; } memset(thread_data, 0, thread_data_size); @@ -268,22 +330,36 @@ int os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, thread_data->stack_size = stack_size; thread_data->tid = tid; +#if BH_ENABLE_ZEPHYR_MPU_STACK != 0 + if (!(thread_data->stack = mpu_stack_alloc())) { + goto fail2; + } +#endif + /* Create the thread */ if (!((tid = k_thread_create(tid, (k_thread_stack_t *)thread_data->stack, stack_size, os_thread_wrapper, start, arg, thread_data, prio, 0, K_NO_WAIT)))) { - BH_FREE(tid); - BH_FREE(thread_data); - return BHT_ERROR; + goto fail3; } bh_assert(tid == thread_data->tid); /* Set thread custom data */ thread_data_list_add(thread_data); - thread_obj_list_add((os_thread_obj*) tid); + thread_obj_list_add((os_thread_obj*)tid); *p_tid = tid; return BHT_OK; + +fail3: +#if BH_ENABLE_ZEPHYR_MPU_STACK != 0 + mpu_stack_free(thread_data->stack); +fail2: +#endif + BH_FREE(thread_data); +fail1: + BH_FREE(tid); + return BHT_ERROR; } korp_tid os_self_thread() diff --git a/samples/gui/README.md b/samples/gui/README.md index 2d3d062cb..d79453c3e 100644 --- a/samples/gui/README.md +++ b/samples/gui/README.md @@ -77,7 +77,7 @@ Refer to [Zephyr getting started](https://docs.zephyrproject.org/latest/getting b. copy samples ```bash cd zephyr/samples - cp -a samples/gui/wasm-runtime-wgl wasm-runtime-wgl + cp -a /samples/gui/wasm-runtime-wgl wasm-runtime-wgl cd wasm-runtime-wgl/zephyr_build ``` c. create a link to wamr root dir @@ -123,7 +123,8 @@ Refer to [Zephyr getting started](https://docs.zephyrproject.org/latest/getting - Install WASM application to Zephyr using host_tool First, connect PC and STM32 with UART. Then install to use host_tool. ```bash - ./host_tool -D /dev/ttyUSBXXX -i inc -f ui_increase.wasm + sudo ./host_tool -D /dev/ttyUSBXXX -i inc -f ui_increase.wasm + # /dev/ttyUSBXXX is the UART device, e.g. /dev/ttyUSB0 ``` - Install AOT version WASM application diff --git a/samples/littlevgl/README.md b/samples/littlevgl/README.md index 0b6bbe9fb..87fd2bd42 100644 --- a/samples/littlevgl/README.md +++ b/samples/littlevgl/README.md @@ -103,7 +103,7 @@ We can use a STM32 NUCLEO_F767ZI board with ILI9341 display and XPT2046 touch s b. copy samples ```bash cd zephyr/samples/ - cp -a samples/littlevgl/vgl-wasm-runtime vgl-wasm-runtime + cp -a /samples/littlevgl/vgl-wasm-runtime vgl-wasm-runtime cd vgl-wasm-runtime/zephyr_build ``` c. create a link to wamr root dir @@ -161,7 +161,8 @@ d. build source code - Install WASM application to Zephyr using host_tool First, connect PC and STM32 with UART. Then install to use host_tool. ```bash - ./host_tool -D /dev/ttyUSBXXX -i ui_app -f ui_app_builtin_libc.wasm + sudo ./host_tool -D /dev/ttyUSBXXX -i ui_app -f ui_app_builtin_libc.wasm + # /dev/ttyUSBXXX is the UART device, e.g. /dev/ttyUSB0 ``` **Note**: WASI is unavailable on zephyr currently, so you have to use the ui_app_builtin_libc.wasm which doesn't depend on WASI. diff --git a/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/display_ili9340.c b/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/display_ili9340.c index eb4295094..4a0276787 100644 --- a/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/display_ili9340.c +++ b/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/display_ili9340.c @@ -26,7 +26,7 @@ struct ili9340_data { struct device *spi_dev; struct spi_config spi_config; #ifdef DT_ILITEK_ILI9340_0_CS_GPIO_CONTROLLER -struct spi_cs_control cs_ctrl; + struct spi_cs_control cs_ctrl; #endif }; diff --git a/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/iwasm_main.c b/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/iwasm_main.c index e2f4a5c9e..6ec3bd49e 100644 --- a/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/iwasm_main.c +++ b/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/iwasm_main.c @@ -76,7 +76,7 @@ host_interface interface = { timer_ctx_t timer_ctx; -static char global_heap_buf[368 * 1024] = { 0 }; +static char global_heap_buf[350 * 1024] = { 0 }; static NativeSymbol native_symbols[] = { EXPORT_WASM_API_WITH_SIG(display_input_read, "(*)i"), diff --git a/samples/littlevgl/vgl-wasm-runtime/zephyr-build/prj.conf b/samples/littlevgl/vgl-wasm-runtime/zephyr-build/prj.conf index f9f13f9a7..6ca7f4426 100644 --- a/samples/littlevgl/vgl-wasm-runtime/zephyr-build/prj.conf +++ b/samples/littlevgl/vgl-wasm-runtime/zephyr-build/prj.conf @@ -1,6 +1,5 @@ CONFIG_SPI=y CONFIG_SPI_STM32=y -CONFIG_SPI_1=y CONFIG_PRINTK=y CONFIG_LOG=y #CONFIG_UART_2=y diff --git a/test-tools/host-tool/src/main.c b/test-tools/host-tool/src/main.c index 230977b4c..305abc7ad 100644 --- a/test-tools/host-tool/src/main.c +++ b/test-tools/host-tool/src/main.c @@ -750,7 +750,7 @@ static void output_event(request_t *obj) int main(int argc, char *argv[]) { - int ret; + int ret = -1; imrt_link_recv_context_t recv_ctx = { 0 }; char buffer[BUF_SIZE] = { 0 }; uint32_t last_check = 0, total_elpased_ms = 0;