Skip to content
Open
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
429 changes: 429 additions & 0 deletions modules/wasm-micro-runtime/CMakeLists.txt

Large diffs are not rendered by default.

109 changes: 109 additions & 0 deletions modules/wasm-micro-runtime/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Configuration for the wasm-micro-runtime (WAMR) module

# Copyright (c) 2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

config ZEPHYR_WAMR_MODULE
bool

menuconfig WAMR
bool "WebAssembly Micro Runtime support"
help
This option enables the WebAssembly Micro Runtime library.

if WAMR

config WAMR_BUILD_TARGET
string "WAMR build target architecture"
help
Set the target CPU architecture. Current supported targets are:
X86_64, X86_32, AARCH64, ARM, THUMB, XTENSA, ARC,
RISCV64, RSCV32 and MIPS.
If it is not set, system will try to set it according to the
board name and other config info.
For ARM and THUMB, the format is <arch>[<sub-arch>][_VFP], where
<sub-arch> is the ARM sub-architecture and the "_VFP" suffix
means using VFP coprocessor registers s0-s15 (d0-d7) for passing
arguments or returning results in standard procedure-call. Both
<sub-arch> and "_VFP" are optional, e.g. ARMV7, ARMV7_VFP,
THUMBV7, THUMBV7_VFP and so on.
For AARCH64, the format is <arch>[<sub-arch>], VFP is enabled by
default. <sub-arch> is optional, e.g. AARCH64, AARCH64V8,
AARCH64V8.1 and so on.
For RISCV64, the format is <arch>[_abi], where "_abi" is
optional, currently the supported formats are RISCV64,
RISCV64_LP64D and RISCV64_LP64. RISCV64 and RISCV64_LP64D are
identical, using LP64D as abi (LP64 with hardware floating-point
calling convention for FLEN=64). And RISCV64_LP64 uses LP64 as
abi (Integer calling-convention only, and hardware floating-point
calling convention is not used).
For RISCV32, the format is <arch>[_abi], where "_abi" is
optional, currently the supported formats are RISCV32,
RISCV32_ILP32D and RISCV32_ILP32. RISCV32 and RISCV32_ILP32D are
identical, using ILP32D as abi (ILP32 with hardware
floating-point calling convention for FLEN=64). And RISCV32_ILP32
uses ILP32 as abi (Integer calling-convention only, and hardware
floating-point calling convention is not used).

config WAMR_INTERP
bool "WebAssembly interpreter support"
default y
help
Enable WebAssembly interpreter

config WAMR_FAST_INTERP
bool "WebAssembly fast interpreter support"
depends on WAMR_INTERP
default y
help
Enable WebAssembly fast interpreter, if not, the classic
interpreter will be enabled

config WAMR_AOT
bool "WebAssembly Ahead of Time support"
default y
help
Enable WebAssembly Ahead of Time

config WAMR_LIBC_BUILTIN
bool "WAMR built-in libc native library support"
default y
help
Enable WAMR built-in libc native library

config WAMR_LIB_PTHREAD
bool "WAMR pthread native native library support"
help
Enable WAMR built-in libc native library

config WAMR_SIMD
bool "WAMR 128-bit SIMD support"
depends on WAMR_AOT
default y
help
Enable WAMR 128-bit SIMD feature

config WAMR_REF_TYPES
bool "WAMR reference types support"
help
Enable WAMR reference types feature

config WAMR_MPU_STACK_COUNT
int "WAMR MPU stack count"
default 4
help
The count of MPU stack: when multi-thread and Zephyr MPU are
enabled, the native thread's stack must be previously defined
by Macro provided by Zephyr, this setting configures how many
mpu stacks are enabled, or, how many threads can be created.

config WAMR_MPU_STACK_SIZE
int "WAMR MPU stack size"
default 4096
help
The size of each MPU stack: when multi-thread and Zephyr MPU are
enabled, the native thread's stack must be previously defined
by Macro provided by Zephyr, this setting configures the size
of each MPU stack.

endif # WAMR
9 changes: 9 additions & 0 deletions samples/modules/wasm_micro_runtime/wasm_blinky/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

project(wasm_hello_world)

target_sources(app PRIVATE src/main.c)
103 changes: 103 additions & 0 deletions samples/modules/wasm_micro_runtime/wasm_blinky/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
.. _wasm-micro-runtime-wasm_hello_world:

WebAssembly Micro Runtime Hello World
#####################################

Overview
********
The sample project illustrates how to run a WebAssembly (WASM) application with
WebAssembly Micro Runtime (WAMR). The WASM application binary is pre-converted into
a byte array defined in ``test_wasm.h``, which is statically linked with the sample.
The bundled WASM program is built with a :abbr:`WIT (WebAssembly Interface Types)`
definition describing its interface to Zephyr. The program configures the
:dtcompatible:`gpio-leds` device referenced by the ``led0`` devicetree alias and
blinks the LED ten times by calling the WIT-described host functions exported from
Zephyr. The WAMR runtime then loads the binary and executes the LED blink loop from
WebAssembly.

Building and Running
********************

This example can be built in the standard way. The target board must provide a
``led0`` devicetree alias that points to a controllable GPIO LED. For example with
the :zephyr:board:`qemu_x86_nommu` target:

.. zephyr-app-commands::
:zephyr-app: samples/modules/wasm_micro_runtime/wasm_hello_world/
:board: qemu_x86_nommu
:goals: build

For QEMU target, we can launch with following command.

.. code-block:: console

west build -t run

Sample Output
=============

When the application runs, the LED connected to ``led0`` toggles at 250 ms
intervals for ten cycles. The console shows the elapsed time reported by the host
application once execution completes:

.. code-block:: console

elpase: 60

Build WASM application
**********************

Install WASI-SDK
================
Download WASI-SDK from https://github.com/CraneStation/wasi-sdk/releases and extract the archive to default path "/opt/wasi-sdk"

Generate WIT bindings and build WASM application with WASI-SDK
=============================================================

The WebAssembly application consumes a WIT world defined in ``zephyr-led.wit``.
Regenerate the bindings with ``wit-bindgen`` and rebuild the WASM binary whenever
the interface or guest source changes. The provided ``Makefile`` automates the
binding generation and compilation steps:

.. code-block:: console

cd wasm-app
cargo install wit-bindgen-cli # if wit-bindgen is not already installed
make

The build rules default to the WASI SDK installation at ``/opt/wasi-sdk`` and
rebuild the bindings when ``zephyr-led.wit`` changes. Override the ``WASI_SDK``
environment variable if your SDK is installed elsewhere.

Dump WASM binary file into byte array file
==========================================

.. code-block:: console

cd wasm-app
make embed

The ``embed`` target depends only on standard POSIX tools (``od``, ``tr``, ``sed``
and ``awk``) to regenerate ``../src/test_wasm.h``. The following one-liner mirrors
the rule when the conversion needs to be run manually:

.. code-block:: console

od -An -v -tx1 test.wasm | tr -s ' ' '\n' | sed '/^$/d' | \
awk 'BEGIN {print "unsigned char __aligned(4) wasm_test_file[] = {"} \
{bytes[NR]=$1} \
END {for (i=1; i<=NR; ++i) { \
if ((i-1)%12==0) printf(" "); \
printf("0x%s", bytes[i]); \
if (i!=NR) { \
if (i%12==0) printf(",\\n"); \
else printf(", "); \
} else { \
printf("\\n"); \
} \
} print("};");}' > ../src/test_wasm.h

References
**********

- WAMR sample: https://github.com/bytecodealliance/wasm-micro-runtime/tree/main/product-mini/platforms/zephyr/simple
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_ARM_MPU=y
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONFIG_ARM_MMU=n
#CONFIG_TEST_RANDOM_GENERATOR=y
#CONFIG_TIMER_RANDOM_GENERATOR=y
21 changes: 21 additions & 0 deletions samples/modules/wasm_micro_runtime/wasm_blinky/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CONFIG_STACK_SENTINEL=y
CONFIG_PRINTK=y
CONFIG_LOG=y
CONFIG_WAMR=y
CONFIG_WAMR_INTERP=y
CONFIG_WAMR_AOT=y
#CONFIG_WAMR_LIBC_WASI=y
#CONFIG_HEAP_MEM_POOL_SIZE=81292
#CONFIG_TEST_RANDOM_GENERATOR=y
#CONFIG_NETWORKING=y
#CONFIG_FILE_SYSTEM=y
#CONFIG_POSIX_API=y


CONFIG_WAMR_LIB_PTHREAD=y
CONFIG_WAMR_SIMD=y
CONFIG_WAMR_REF_TYPES=y
#CONFIG_WAMR_MEMORY_PROFILING=y
#CONFIG_WAMR_PERF_PROFILING=y
#CONFIG_WAMR_DUMP_CALL_STACK=y

Loading
Loading