wasm: Enable 64bit guests on TCI using wasm64
v5 cover wasm: Enable 64bit guests on TCI using wasm64
V5:
- In emsdk-wasm-cross.docker, removed -sMEMORY64 from the environment variables (e.g. CFLAGS) in the QEMU compilation stage.
- Updated the Dockerfile to build dependencies always with -sMEMORY64=1 for wasm64 target. The final QEMU compilation can be lowered to wasm32 by using configure's --wasm64-32bit-address-limit flag which enables Emscripten's MEMORY64=2.
- Fixed wasm64 tests (build-wasm64-64bit and build-wasm64-32bit) to share the same wasm64 container. The build-wasm64-32bit test passes --wasm64-32bit-address-limit to the configure script so that the output is lowered to wasm32 by Emscripten's -sMEMORY64=2.
V4:
- Rebased on the recent tree.
V3:
- Renamed the build tests on GitLab CI("build-wasm32" -> "build-wasm32-32bit", "build-wasm-wasm64" -> "build-wasm64-64bit", "build-wasm-wasm64l" -> "build-wasm64-32bit"). The same change has also been applied to container-cross.yml.
V2:
- Added a link to the Emscripten documentation about -sMEMORY64 in the configure script.
- Changed --wasm64-memory64 flag to --wasm64-32bit-address-limit to avoid exposing the -sMEMORY64 value directly to the users.
- Fixed GitLab CI to use --wasm64-32bit-address-limit instead of --wasm64-memory64.
V1:
This patch series enables the TCI of the Wasm build to run 64bit guests. Unlike the previous series[1], this patch series is implemented by adding support for WebAssembly's "wasm64" target which enables 64bit pointers.
In the previous discussion[2], the main challenge of using wasm64 was its limited adoption, particularly the lack of support in our dependency (libffi) and some engines such as Safari.
For libffi, I've completed the addition of wasm64 support upstream[3] so it can be used.
To support wasm32 engines, this patch uses Emscripten's compatibility feature, -sMEMORY64=2 flag[4]. This flag still enables 64bit pointers in the C code while Emscripten lowers the output binary to wasm32 and limits the maximum memory size to 4GB. As a result, QEMU can run on wasm32 engiens while still supporting 64bit guests.
# Overview of the build process
To compile QEMU with Emscripten, the following dependencies are required. The emsdk-wasm-cross.docker environment includes all necessary components and can be used as the build environment:
- Emscripten SDK (emsdk) v4.0.10
- Libraries cross-compiled with Emscripten (please see also emsdk-wasm-cross.docker for build steps)
- GLib v2.84.0
- zlib v1.3.1
- libffi v3.5.2
- Pixman v0.44.2
The configure script supports --cpu=wasm64 flag to compile QEMU with 64bit pointer support.
emconfigure ./configure --cpu=wasm64 \
--static --disable-tools \
--target-list=x86_64-softmmu \
--enable-tcg-interpreteremmake make -j$(nproc)
If the output needs to run on wasm32 engines, use --wasm64-32bit-address-limit flag. This flag enables Emscripten's -sMEMORY64=2 flag[4]. (Note: --wasm64-memory64=2 flag in the V1 patch has been renamed to --wasm64-32bit-address-limit in V2)
emconfigure ./configure --cpu=wasm64 --wasm64-32bit-address-limit \
--static --disable-tools \
--target-list=x86_64-softmmu \
--enable-tcg-interpreteremmake make -j$(nproc)
Either of the above commands generates the following files:
- qemu-system-x86_64.js
- qemu-system-x86_64.wasm
Guest images can be packaged using Emscripten's file_packager.py tool. For example, if the images are stored in a directory named "pack", the following command packages them, allowing QEMU to access them through Emscripten's virtual filesystem:
/path/to/file_packager.py qemu-system-x86_64.data --preload pack > load.js
This process generates the following files:
- qemu-system-x86_64.data
- load.js
Emscripten allows passing arguments to the QEMU command via the Module object in JavaScript:
Module['arguments'] = [
'-nographic', '-m', '512M',
'-L', 'pack/',
'-drive', 'if=virtio,format=raw,file=pack/rootfs.bin',
'-kernel', 'pack/bzImage',
'-append', 'earlyprintk=ttyS0 console=ttyS0 root=/dev/vda loglevel=7',];
The sample repository[5] (tcidev64 branch) provides a complete setup, including an HTML file that implements a terminal UI.
[1] https://lists.nongnu.org/archive/html/qemu-devel/2025-05/msg05376.html
[2] https://lists.nongnu.org/archive/html/qemu-devel/2025-04/msg01795.html
[3] https://github.com/libffi/libffi/pull/927
[4] https://emscripten.org/docs/tools_reference/settings_reference.html#memory64
[5] https://github.com/ktock/qemu-wasm-sample/tree/tcidev64
Kohei Tokunaga (4):
meson: Add wasm64 support to the --cpu flag
configure: Enable to propagate -sMEMORY64 flag to Emscripten
dockerfiles: Add support for wasm64 to the wasm Dockerfile .gitlab-ci.d: Add build tests for wasm64
.gitlab-ci.d/buildtest.yml | 24 ++++++++++++++---
.gitlab-ci.d/container-cross.yml | 11 +++++++-
.gitlab-ci.d/container-template.yml | 4 ++-
.gitlab-ci.d/containers.yml | 3 ++-
MAINTAINERS | 2 +-
configure | 16 +++++++++++-
meson.build | 4 +--
...2-cross.docker => emsdk-wasm-cross.docker} | 26 ++++++++++++++-----
8 files changed, 73 insertions(+), 17 deletions(-)
rename tests/docker/dockerfiles/{emsdk-wasm32-cross.docker => emsdk-wasm-cross.docker} (89%)Signature · 1 line
2.43.0
PATCH v5 1/4 meson: Add wasm64 support to the --cpu flag
wasm64 target enables 64bit pointers using Emscripten's -sMEMORY64=1 flag[1]. This enables QEMU to run 64bit guests.
Although the configure script uses "uname -m" as the fallback value when "cpu" is empty, this can't be used for Emscripten which targets to Wasm. So, in wasm build, this commit fixes configure to require --cpu flag to be explicitly specified by the user.
[1] https://emscripten.org/docs/tools_reference/settings_reference.html#memory64
--- configure | 6 +++++- meson.build | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-)
configure +5 -1
@@ -365,7 +365,6 @@ elif check_define __APPLE__; then host_os=darwin elif check_define EMSCRIPTEN ; then host_os=emscripten - cpu=wasm32 cross_compile="yes" else # This is a fatal error, but don't report it yet, because we
@@ -419,6 +418,8 @@ elif check_define __aarch64__ ; then cpu="aarch64" elif check_define __loongarch64 ; then cpu="loongarch64" +elif check_define EMSCRIPTEN ; then + error_exit "wasm32 or wasm64 must be specified to the cpu flag" else # Using uname is really broken, but it is just a fallback for architectures # that are going to use TCI anyway
@@ -519,6 +520,9 @@ case "$cpu" in wasm32) CPU_CFLAGS="-m32" ;; + wasm64) + CPU_CFLAGS="-m64 -sMEMORY64=1" + ;; esac if test -n "$host_arch" && {
meson.build +2 -2
@@ -51,7 +51,7 @@ qapi_trace_events = [] bsd_oses = ['gnu/kfreebsd', 'freebsd', 'netbsd', 'openbsd', 'dragonfly', 'darwin'] supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux', 'emscripten'] supported_cpus = ['ppc', 'ppc64', 's390x', 'riscv32', 'riscv64', 'x86', 'x86_64', - 'arm', 'aarch64', 'loongarch64', 'mips64', 'sparc64', 'wasm32'] + 'arm', 'aarch64', 'loongarch64', 'mips64', 'sparc64', 'wasm32', 'wasm64'] cpu = host_machine.cpu_family()
@@ -914,7 +914,7 @@ if have_tcg if not get_option('tcg_interpreter') error('Unsupported CPU @0@, try --enable-tcg-interpreter'.format(cpu)) endif - elif host_arch == 'wasm32' + elif host_arch == 'wasm32' or host_arch == 'wasm64' if not get_option('tcg_interpreter') error('WebAssembly host requires --enable-tcg-interpreter') endif
Signature · 1 line
2.43.0
Re: [PATCH v5 1/4] meson: Add wasm64 support to the --cpu flag
Quoted 8 lines · Kohei Tokunaga
wasm64 target enables 64bit pointers using Emscripten's -sMEMORY64=1 flag[1]. This enables QEMU to run 64bit guests.
Although the configure script uses "uname -m" as the fallback value when "cpu" is empty, this can't be used for Emscripten which targets to Wasm. So, in wasm build, this commit fixes configure to require --cpu flag to be explicitly specified by the user.
Quoted 8 lines
[1] https://emscripten.org/docs/tools_reference/settings_reference.html#memory64
Signed-off-by Kohei Tokunaga <[email protected]>Reviewed-by Pierrick Bouvier <[email protected]>--- configure | 6 +++++- meson.build | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-)
PATCH v5 2/4 configure: Enable to propagate -sMEMORY64 flag to Emscripten
In reply to Kohei TokunagaCurrently there are some engines that don't support wasm64 (e.g. unsupported on Safari[1]). To mitigate this issue, the configure script allows the user to use Emscripten's compatibility feature, "-sMEMORY64=2" flag[2].
Emscripten's "-sMEMORY64=2" flag still enables 64bit pointers in C code. But this flag lowers the output binary into wasm32, with limiting the maximum memory size to 4GB. So QEMU can run on wasm32 engines.
[1] https://webassembly.org/features/
[2] https://emscripten.org/docs/tools_reference/settings_reference.html#memory64
--- configure | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
configure +11 -1
@@ -182,6 +182,10 @@ EXTRA_CXXFLAGS="" EXTRA_OBJCFLAGS="" EXTRA_LDFLAGS="" +# The value is propagated to Emscripten's "-sMEMORY64" flag. +# https://emscripten.org/docs/tools_reference/settings_reference.html#memory64 +wasm64_memory64=1 + # Default value for a variable defining feature "foo". # * foo="no" feature will only be used if --enable-foo arg is given # * foo="" feature will be searched for, and if found, will be used
@@ -239,6 +243,8 @@ for opt do ;; --without-default-features) default_feature="no" ;; + --wasm64-32bit-address-limit) wasm64_memory64="2" + ;; esac done
@@ -521,7 +527,7 @@ case "$cpu" in CPU_CFLAGS="-m32" ;; wasm64) - CPU_CFLAGS="-m64 -sMEMORY64=1" + CPU_CFLAGS="-m64 -sMEMORY64=$wasm64_memory64" ;; esac
@@ -779,6 +785,8 @@ for opt do ;; --disable-rust) rust=disabled ;; + --wasm64-32bit-address-limit) + ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;;
@@ -904,6 +912,8 @@ Advanced options (experts only): --disable-containers don't use containers for cross-building --container-engine=TYPE which container engine to use [$container_engine] --gdb=GDB-path gdb to use for gdbstub tests [$gdb_bin] + --wasm64-32bit-address-limit Restrict wasm64 address space to 32-bit (default + is to use the whole 64-bit range). EOF meson_options_help cat << EOF
Signature · 1 line
2.43.0
PATCH v5 3/4 dockerfiles: Add support for wasm64 to the wasm Dockerfile
In reply to Kohei TokunagaThis commit fixes Dockerfile of the wasm build to support both of wasm32 and wasm64 build. Dockerfile takes the following build argument and use it for building dependencies.
- TARGET_CPU: target wasm arch (wasm32 or wasm64)
---
MAINTAINERS | 2 +-
...2-cross.docker => emsdk-wasm-cross.docker} | 26 ++++++++++++++-----
2 files changed, 20 insertions(+), 8 deletions(-)
rename tests/docker/dockerfiles/{emsdk-wasm32-cross.docker => emsdk-wasm-cross.docker} (89%)V5:
- Removed -sMEMORY64 from the environment variables (e.g. CFLAGS) in the QEMU compilation stage.
- Updated the Dockerfile to build dependencies always with -sMEMORY64=1 for wasm64 target. The final QEMU compilation can be lowered to wasm32 by using configure's --wasm64-32bit-address-limit flag which enables Emscripten's MEMORY64=2.
MAINTAINERS +1 -1
@@ -671,7 +671,7 @@ F: include/system/os-wasm.h F: os-wasm.c F: util/coroutine-wasm.c F: configs/meson/emscripten.txt -F: tests/docker/dockerfiles/emsdk-wasm32-cross.docker +F: tests/docker/dockerfiles/emsdk-wasm-cross.docker Alpha Machines --------------
tests/docker/dockerfiles/emsdk-wasm-cross.docker renamed from tests/docker/dockerfiles/emsdk-wasm32-cross.docker +19 -7
@@ -1,14 +1,15 @@ # syntax = docker/dockerfile:1.5 -ARG EMSDK_VERSION_QEMU=3.1.50 +ARG EMSDK_VERSION_QEMU=4.0.10 ARG ZLIB_VERSION=1.3.1 ARG GLIB_MINOR_VERSION=2.84 ARG GLIB_VERSION=${GLIB_MINOR_VERSION}.0 ARG PIXMAN_VERSION=0.44.2 -ARG FFI_VERSION=v3.4.7 +ARG FFI_VERSION=v3.5.2 ARG MESON_VERSION=1.5.0 +ARG TARGET_CPU=wasm32 -FROM docker.io/emscripten/emsdk:$EMSDK_VERSION_QEMU AS build-base +FROM docker.io/emscripten/emsdk:$EMSDK_VERSION_QEMU AS build-base-common ARG MESON_VERSION ENV TARGET=/builddeps/target ENV CPATH="$TARGET/include"
@@ -29,12 +30,22 @@ RUN pip3 install meson==${MESON_VERSION} tomli RUN mkdir /build WORKDIR /build RUN mkdir -p $TARGET + +FROM build-base-common AS build-base-wasm32 + +FROM build-base-common AS build-base-wasm64 +ENV CFLAGS="$CFLAGS -sMEMORY64=1" +ENV CXXFLAGS="$CXXFLAGS -sMEMORY64=1" +ENV LDFLAGS="$LDFLAGS -sMEMORY64=1" + +FROM build-base-${TARGET_CPU} AS build-base +ARG TARGET_CPU RUN <<EOF cat <<EOT > /cross.meson [host_machine] system = 'emscripten' -cpu_family = 'wasm32' -cpu = 'wasm32' +cpu_family = '${TARGET_CPU}' +cpu = '${TARGET_CPU}' endian = 'little' [binaries]
@@ -56,13 +67,14 @@ RUN emconfigure ./configure --prefix=$TARGET --static RUN emmake make install -j$(nproc) FROM build-base AS libffi-dev +ARG TARGET_CPU ARG FFI_VERSION RUN mkdir -p /libffi RUN git clone https://github.com/libffi/libffi /libffi WORKDIR /libffi RUN git checkout $FFI_VERSION RUN autoreconf -fiv -RUN emconfigure ./configure --host=wasm32-unknown-linux \ +RUN emconfigure ./configure --host=${TARGET_CPU}-unknown-linux \ --prefix=$TARGET --enable-static \ --disable-shared --disable-dependency-tracking \ --disable-builddir --disable-multi-os-directory \
@@ -140,6 +152,6 @@ RUN sed -i -E "/#define HAVE_POSIX_SPAWN 1/d" ./_build/config.h RUN sed -i -E "/#define HAVE_PTHREAD_GETNAME_NP 1/d" ./_build/config.h RUN meson install -C _build -FROM build-base +FROM build-base-common COPY --from=glib-dev /builddeps/ /builddeps/ COPY --from=pixman-dev /builddeps/ /builddeps/
Signature · 1 line
2.43.0
PATCH v5 4/4 .gitlab-ci.d: Add build tests for wasm64
In reply to Kohei TokunagaThe wasm builds are tested for 3 targets: wasm32, wasm64(-sMEMORY64=1) and wasm64(-sMEMORY64=2). The CI builds the containers using the same Dockerfile (emsdk-wasm-cross.docker) with different build args.
--- .gitlab-ci.d/buildtest.yml | 24 +++++++++++++++++++++--- .gitlab-ci.d/container-cross.yml | 11 ++++++++++- .gitlab-ci.d/container-template.yml | 4 +++- .gitlab-ci.d/containers.yml | 3 ++- 4 files changed, 36 insertions(+), 6 deletions(-)
V5:
- Fixed wasm64 tests (build-wasm64-64bit and build-wasm64-32bit) to share the same wasm64 container. The build-wasm64-32bit test passes --wasm64-32bit-address-limit to the configure script so that the output is lowered to wasm32 by Emscripten's -sMEMORY64=2.
.gitlab-ci.d/buildtest.yml +21 -3
@@ -785,11 +785,29 @@ coverity: # Always manual on forks even if $QEMU_CI == "2" - when: manual -build-wasm: +build-wasm32-32bit: extends: .wasm_build_job_template timeout: 2h needs: - - job: wasm-emsdk-cross-container + - job: wasm32-emsdk-cross-container variables: IMAGE: emsdk-wasm32-cross - CONFIGURE_ARGS: --static --disable-tools --enable-debug --enable-tcg-interpreter + CONFIGURE_ARGS: --static --cpu=wasm32 --disable-tools --enable-debug --enable-tcg-interpreter + +build-wasm64-64bit: + extends: .wasm_build_job_template + timeout: 2h + needs: + - job: wasm64-emsdk-cross-container + variables: + IMAGE: emsdk-wasm64-cross + CONFIGURE_ARGS: --static --cpu=wasm64 --disable-tools --enable-debug --enable-tcg-interpreter + +build-wasm64-32bit: + extends: .wasm_build_job_template + timeout: 2h + needs: + - job: wasm64-emsdk-cross-container + variables: + IMAGE: emsdk-wasm64-cross + CONFIGURE_ARGS: --static --cpu=wasm64 --wasm64-32bit-address-limit --disable-tools --enable-debug --enable-tcg-interpreter
.gitlab-ci.d/container-cross.yml +10 -1
@@ -86,7 +86,16 @@ win64-fedora-cross-container: variables: NAME: fedora-win64-cross -wasm-emsdk-cross-container: +wasm32-emsdk-cross-container: extends: .container_job_template variables: NAME: emsdk-wasm32-cross + BUILD_ARGS: --build-arg TARGET_CPU=wasm32 + DOCKERFILE: emsdk-wasm-cross + +wasm64-emsdk-cross-container: + extends: .container_job_template + variables: + NAME: emsdk-wasm64-cross + BUILD_ARGS: --build-arg TARGET_CPU=wasm64 + DOCKERFILE: emsdk-wasm-cross
.gitlab-ci.d/container-template.yml +3 -1
@@ -10,12 +10,14 @@ - export COMMON_TAG="$CI_REGISTRY/qemu-project/qemu/qemu/$NAME:latest" - docker login $CI_REGISTRY -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" - until docker info; do sleep 1; done + - export DOCKERFILE_NAME=${DOCKERFILE:-$NAME} script: - echo "TAG:$TAG" - echo "COMMON_TAG:$COMMON_TAG" - docker build --tag "$TAG" --cache-from "$TAG" --cache-from "$COMMON_TAG" --build-arg BUILDKIT_INLINE_CACHE=1 - -f "tests/docker/dockerfiles/$NAME.docker" "." + $BUILD_ARGS + -f "tests/docker/dockerfiles/$DOCKERFILE_NAME.docker" "." - docker push "$TAG" after_script: - docker logout
.gitlab-ci.d/containers.yml +2 -1
@@ -58,7 +58,8 @@ weekly-container-builds: - tricore-debian-cross-container - xtensa-debian-cross-container - win64-fedora-cross-container - - wasm-emsdk-cross-container + - wasm32-emsdk-cross-container + - wasm64-emsdk-cross-container # containers - amd64-alpine-container - amd64-debian-container
Signature · 1 line
2.43.0
Re: [PATCH v5 4/4] .gitlab-ci.d: Add build tests for wasm64
Quoted 12 lines · Kohei Tokunaga
The wasm builds are tested for 3 targets: wasm32, wasm64(-sMEMORY64=1) and wasm64(-sMEMORY64=2). The CI builds the containers using the same Dockerfile (emsdk-wasm-cross.docker) with different build args.
Signed-off-by Kohei Tokunaga <[email protected]>--- .gitlab-ci.d/buildtest.yml | 24 +++++++++++++++++++++--- .gitlab-ci.d/container-cross.yml | 11 ++++++++++- .gitlab-ci.d/container-template.yml | 4 +++- .gitlab-ci.d/containers.yml | 3 ++- 4 files changed, 36 insertions(+), 6 deletions(-)
Quoted 6 lines
V5:
- Fixed wasm64 tests (build-wasm64-64bit and build-wasm64-32bit) to share the same wasm64 container. The build-wasm64-32bit test passes --wasm64-32bit-address-limit to the configure script so that the output is lowered to wasm32 by Emscripten's -sMEMORY64=2.
Hi,
Quoted 5 lines · Kohei Tokunaga
Kohei Tokunaga (4):
meson: Add wasm64 support to the --cpu flag
configure: Enable to propagate -sMEMORY64 flag to Emscripten
dockerfiles: Add support for wasm64 to the wasm Dockerfile .gitlab-ci.d: Add build tests for wasm64
Out of curiosity, since the series is fully reviewed, who is going to merge it, Paolo or Richard?
Quoted 4 lines · Philippe Mathieu-Daudé
Hi,
On 13/1/26 14:54, Kohei Tokunaga wrote:
Quoted 9 lines
Quoted 5 lines
Kohei Tokunaga (4):
meson: Add wasm64 support to the --cpu flag
configure: Enable to propagate -sMEMORY64 flag to Emscripten
dockerfiles: Add support for wasm64 to the wasm Dockerfile .gitlab-ci.d: Add build tests for wasm64Out of curiosity, since the series is fully reviewed, who is going to merge it, Paolo or Richard?
I've pulled this into my remove-32 branch, which I'll be re-posting soon. But if someone includes it into a PR first, that's fine too.
r~
Quoted 11 lines · Richard Henderson
Quoted 10 lines · Philippe Mathieu-Daudé
Hi,
Quoted 5 lines · Kohei Tokunaga
Kohei Tokunaga (4):
meson: Add wasm64 support to the --cpu flag
configure: Enable to propagate -sMEMORY64 flag to Emscripten
dockerfiles: Add support for wasm64 to the wasm Dockerfile .gitlab-ci.d: Add build tests for wasm64
Quoted 5 lines
Quoted 3 lines
Out of curiosity, since the series is fully reviewed, who is going to merge it, Paolo or Richard?
I've pulled this into my remove-32 branch, which I'll be re-posting soon. But if someone includes it into a PR first, that's fine too.
Oh OK great, I'll do that so less to carry then!