Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions core/app-mgr/app-manager/module_wasm_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
92 changes: 84 additions & 8 deletions core/shared/platform/zephyr/zephyr_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -253,37 +312,54 @@ 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);
k_mutex_init(&thread_data->wait_list_lock);
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()
Expand Down
5 changes: 3 additions & 2 deletions samples/gui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Refer to [Zephyr getting started](https://docs.zephyrproject.org/latest/getting
b. copy samples
```bash
cd zephyr/samples
cp -a <wamr_root>samples/gui/wasm-runtime-wgl wasm-runtime-wgl
cp -a <wamr_root>/samples/gui/wasm-runtime-wgl wasm-runtime-wgl
cd wasm-runtime-wgl/zephyr_build
```
c. create a link to wamr root dir
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions samples/littlevgl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <wamr_root>samples/littlevgl/vgl-wasm-runtime vgl-wasm-runtime
cp -a <wamr_root>/samples/littlevgl/vgl-wasm-runtime vgl-wasm-runtime
cd vgl-wasm-runtime/zephyr_build
```
c. create a link to wamr root dir
Expand Down Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
1 change: 0 additions & 1 deletion samples/littlevgl/vgl-wasm-runtime/zephyr-build/prj.conf
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion test-tools/host-tool/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down