[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Minios-devel] [UNIKRAFT/APPWAMR_RASPI_DEMO PATCH 1/1] apps/wamr-raspi-demo: Unikraft WAMR baremetal demo running on a Raspberry Pi3 B+
Unikraft WAMR baremetal demo running on a Raspberry Pi3 B+. Signed-off-by: Santiago Pagani <santiagopagani@xxxxxxxxx> --- Config.uk | 11 ++++ Makefile | 10 ++++ Makefile.uk | 5 ++ README.md | 17 +++++- compileWAMR.sh | 8 +++ helloworld_wasm.c | 42 +++++++++++++ main.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 Config.uk create mode 100644 Makefile create mode 100644 Makefile.uk create mode 100755 compileWAMR.sh create mode 100644 helloworld_wasm.c create mode 100644 main.c diff --git a/Config.uk b/Config.uk new file mode 100644 index 0000000..c98b01b --- /dev/null +++ b/Config.uk @@ -0,0 +1,11 @@ +### Invisible option for dependencies +config APPWAMR_RASPI_DEMO_DEPENDENCIES + bool + default y + +### App configuration +config APPWAMR_RASPI_DEMO_PRINTARGS + bool "Print arguments" + default y + help + Prints argument list (argv) to stdout diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d846348 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +UK_ROOT ?= $(PWD)/../../unikraft +UK_LIBS ?= $(PWD)/../../libs +UK_PLAT ?= $(PWD)/../../plats/raspi +LIBS := $(UK_LIBS)/wamr:$(UK_LIBS)/pthread-embedded:$(UK_LIBS)/lwip:$(UK_LIBS)/newlib + +all: + @$(MAKE) -C $(UK_ROOT) A=$(PWD) P=$(UK_PLAT) L=$(LIBS) + +$(MAKECMDGOALS): + @$(MAKE) -C $(UK_ROOT) A=$(PWD) P=$(UK_PLAT) L=$(LIBS) $(MAKECMDGOALS) diff --git a/Makefile.uk b/Makefile.uk new file mode 100644 index 0000000..472657a --- /dev/null +++ b/Makefile.uk @@ -0,0 +1,5 @@ +$(eval $(call addlib,appwamr_raspi_demo)) + +APPWAMR_RASPI_DEMO_CINCLUDES-y += -I$(LIBRASPIPLAT_BASE)/include + +APPWAMR_RASPI_DEMO_SRCS-y += $(APPWAMR_RASPI_DEMO_BASE)/main.c diff --git a/README.md b/README.md index 416e4d1..f61c2af 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,16 @@ -# WAMR Raspi Demo +# wamr-raspi-demo -Unikraft WAMR baremetal demo running on a Raspberry Pi 3B+ \ No newline at end of file +Unikraft WAMR baremetal demo running on a Raspberry Pi 3B+ + +Please select/deselect the following options in the make menuconfig: +- Architecture Selection --> Architecture --> select: Arrmv8 compatible (64 bits) +- Architecture Selection --> Processor Optimization --> select: Generic Armv8 Cortex A53 +- Architecture Selection --> deselect: Workaround for Cortex-A73 erratum +- Platform Configuration --> select: Raspberry Pi 3B+ +- Library Configuration --> ukboot --> deselect: Show Unikraft banner (this is to minimize boot time) +- Library Configuration --> wamr --> deselect: Provide main function +- Build Options --> select: Drop unused functions and data + +Once built, in a blank SD card formated to FAT32, copy the generated kernel8.img Unikraft image (without changing +the name of the image file, as otherwise the Raspberry Pi's bootloader will not recongize it), plus the four files +that are inside the plats/raspi/bootfiles folder (i.e., bootcode.bin, config.txt, fixup.dat, and start.elf). diff --git a/compileWAMR.sh b/compileWAMR.sh new file mode 100755 index 0000000..be822d4 --- /dev/null +++ b/compileWAMR.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Compile WAMR binary +EMCC_ONLY_FORCED_STDLIBS=1 emcc -g0 -O3 -s WASM=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s TOTAL_MEMORY=65536 -s TOTAL_STACK=4096 -s ASSERTIONS=1 -s STACK_OVERFLOW_CHECK=2 -s "EXPORTED_FUNCTIONS=['_main']" -o helloworld.wasm helloworld_wasm.c +#clang-8 --target=wasm32 -O3 -z stack-size=4096 -Wl,--initial-memory=65536 -Wl,--allow-undefined,--export=main -Wl,--strip-all,--no-entry -nostdlib -o helloworld.wasm -I/usr/aarch64-linux-gnu/include helloworld_wasm.c + +xxd -i helloworld.wasm helloworld_wasm.h + diff --git a/helloworld_wasm.c b/helloworld_wasm.c new file mode 100644 index 0000000..f79ecd1 --- /dev/null +++ b/helloworld_wasm.c @@ -0,0 +1,42 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +static inline long get_system_timer(void) +{ + struct timespec time; + if (clock_gettime(CLOCK_MONOTONIC, &time) != 0) { + printf("Error executing clock_gettime\n"); + } + return time.tv_sec; +} + +int main(int argc, char **argv) +{ + char *buf; + long time_start, time_end; + + time_start = get_system_timer(); + + printf("Hello world from inside WAMR!\n"); + + buf = malloc(1024); + if (!buf) { + printf("malloc buf failed\n"); + return -1; + } + + printf("buf ptr: %p\n", buf); + + sprintf(buf, "%s", "1234\n"); + printf("buf: %s", buf); + + free(buf); + + + time_end = get_system_timer(); + printf("WAMR start time: %ld us\n", time_start); + printf("WAMR end time: %ld us\n", time_end); + + return 0; +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..d135a12 --- /dev/null +++ b/main.c @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Authors: Santiago Pagani <santiagopagani@xxxxxxxxx> + * + * Copyright (c) 2020, NEC Laboratories Europe GmbH, NEC Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY. + */ +#include <uk/config.h> +#include <stdio.h> +#include <stdint.h> +#include <unistd.h> +#include <uk/print.h> +#include <uk/essentials.h> +#include <raspi/setup.h> +#include <raspi/time.h> +#include <raspi/raspi_info.h> + + +#include <stdlib.h> +#include <string.h> +#include <bh_platform.h> +#include <bh_queue.h> +#include <wasm_assert.h> +#include <wasm_log.h> +#include <wasm_platform_log.h> +#include <wasm_thread.h> +#include <wasm_export.h> +#include <wasm_memory.h> +#include <bh_memory.h> + +#include "helloworld_wasm.h" + +static int app_argc; +static char **app_argv; + +#define WAMR_HEAP_SIZE (1 * 1024 * 1024) + +bool wasm_application_execute_main(wasm_module_inst_t module_inst, int argc, char *argv[]); + +int bh_platform_init() +{ + return 0; +} + +int main(int argc __unused, char *argv[] __unused) +{ + uint64_t main_entry = get_system_timer(); + + char *global_heap_buf; + wasm_module_t wasm_module = NULL; + wasm_module_inst_t wasm_module_inst = NULL; + char error_buf[128]; + int log_verbose_level = 1; + const char *wasm_exception; + + app_argc = 0; + app_argv = NULL; + + global_heap_buf = malloc(WAMR_HEAP_SIZE); + if (bh_memory_init_with_pool(global_heap_buf, WAMR_HEAP_SIZE) != 0) { + uk_pr_err("WASM: Init memory with global heap buffer failed.\n"); + return -1; + } + + // Initialize runtime environment + if (!wasm_runtime_init()) + goto fail1; + + wasm_log_set_verbose_level(log_verbose_level); + + // Load WASM module + if (!(wasm_module = wasm_runtime_load(helloworld_wasm, helloworld_wasm_len, error_buf, sizeof(error_buf)))) { + uk_pr_err("WASM: %s\n", error_buf); + goto fail3; + } + + // Instantiate the module + if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, + 64 * 1024, // Stack size + 64 * 1024, // Heap size + error_buf, + sizeof(error_buf)))) { + uk_pr_err("WASM: %s\n", error_buf); + goto fail4; + } + + // Execute the WASM application + wasm_application_execute_main(wasm_module_inst, app_argc, app_argv); + if ((wasm_exception = wasm_runtime_get_exception(wasm_module_inst))) { + uk_pr_err("WASM - app_instance_main: %s\n", wasm_exception); + goto fail4; + } + + #if CONFIG_RASPI_PRINTF_SERIAL_CONSOLE + printf("WAMR Helloworld on Raspberry Pi3\n"); + printf("Boot Time (absolute, CPU reset after power on): %lu us\n", _libraspiplat_get_reset_time()); + printf("Main entry (absolute, main entry after power on): %lu us\n", main_entry); + printf("Boot Time (Unikraft difference from CPU reset): %lu us\n", main_entry - _libraspiplat_get_reset_time()); + printf("Text Size: %lu\n", get_unikraft_text_size()); + printf("Data Size: %lu\n", get_unikraft_data_size()); + printf("BSS Size: %lu\n", get_unikraft_bss_size()); + printf("Stack Use: %lu\n", computeUsedStack()); + float testFloat = 3.948f; + printf("Test float: %f\n", testFloat + 1.5f); + #endif + + // Destroy the module instance + wasm_runtime_deinstantiate(wasm_module_inst); + +fail4: + // Unload the module + wasm_runtime_unload(wasm_module); + +fail3: + // destroy runtime environment + wasm_runtime_destroy(); + +fail1: + bh_memory_destroy(); + + return 0; +} -- 2.17.1 _______________________________________________ Minios-devel mailing list Minios-devel@xxxxxxxxxxxxxxxxxxxx https://lists.xenproject.org/mailman/listinfo/minios-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |