Skip to content

tcg: Add WebAssembly backend

Kohei Tokunaga qemu-arm qemu-devel qemu-riscv 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest33 patches 2 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/33tcg: Fork TCI for wasm32 backendMAINTAINERS include/accel/tcg/getpc.h include/tcg/helper-info.h +13 more+2604 -170 replies
2/33tcg/wasm32: Do not use TCI disassembler in Wasm backendtcg/wasm32.c+1 -2420 replies
3/33meson: Enable to build wasm backendmeson.build+0 -40 replies
4/33tcg/wasm32: Set TCG_TARGET_INSN_UNIT_SIZE to 1tcg/wasm32/tcg-target.c.inc tcg/wasm32/tcg-target.h+23 -190 replies
5/33tcg/wasm32: Add and/or/xor instructionstcg/wasm32/tcg-target.c.inc+136 -10 replies
6/33tcg/wasm32: Add add/sub/mul instructionstcg/wasm32/tcg-target.c.inc+18 -00 replies
7/33tcg/wasm32: Add shl/shr/sar instructionstcg/wasm32/tcg-target.c.inc+107 -00 replies
8/33tcg/wasm32: Add setcond/negsetcond/movcond instructionstcg/wasm32/tcg-target-has.h tcg/wasm32/tcg-target.c.inc+133 -50 replies
9/33tcg/wasm32: Add deposit/sextract/extract instrcutionstcg/wasm32/tcg-target.c.inc+109 -130 replies
10/33tcg/wasm32: Add load and store instructionstcg/wasm32/tcg-target.c.inc+242 -00 replies
11/33tcg/wasm32: Add mov/movi instructionstcg/wasm32/tcg-target.c.inc+60 -250 replies
12/33tcg/wasm32: Add ext instructionstcg/wasm32/tcg-target.c.inc+78 -10 replies
13/33tcg/wasm32: Add bswap instructionstcg/wasm32/tcg-target.c.inc+136 -00 replies
14/33tcg/wasm32: Add rem/div instructionstcg/wasm32/tcg-target.c.inc+120 -00 replies
15/33tcg/wasm32: Add andc/orc/eqv/nand/nor instructionstcg/wasm32/tcg-target.c.inc+55 -00 replies
16/33tcg/wasm32: Add neg/not/ctpop instructionstcg/wasm32/tcg-target.c.inc+37 -20 replies
17/33tcg/wasm32: Add rot/clz/ctz instructionstcg/wasm32/tcg-target.c.inc+155 -00 replies
18/33tcg/wasm32: Add addc/subb instructionstcg/wasm32/tcg-target.c.inc+151 -00 replies
19/33tcg/wasm32: Add br/brcond instructionstcg/aarch64/tcg-target.c.inc tcg/arm/tcg-target.c.inc tcg/i386/tcg-target.c.inc +9 more+297 -00 replies
20/33tcg/wasm32: Add exit_tb/goto_tb/goto_ptr instructionsMAINTAINERS tcg/wasm32.h tcg/wasm32/tcg-target.c.inc+129 -00 replies
21/33tcg/wasm32: Add call instructiontcg/wasm32.h tcg/wasm32/tcg-target.c.inc+180 -00 replies
22/33tcg/wasm32: Add qemu_ld/qemu_st instructionstcg/wasm32/tcg-target.c.inc+108 -00 replies
23/33include/exec: Allow using 64bit guest addresses on emscriptenaccel/tcg/cputlb.c include/exec/helper-head.h.inc include/exec/tlb-common.h +4 more+44 -112 replies
24/33tcg/wasm32: Set TCG_TARGET_REG_BITS to 64tcg/wasm32.c tcg/wasm32/tcg-target-reg-bits.h tcg/wasm32/tcg-target.c.inc+9 -730 replies
25/33tcg/wasm32: Set mulu2/muls2 as unimplementedtcg/wasm32/tcg-target.c.inc+2 -360 replies
26/33tcg/wasm32: Add initialization of fundamental registerstcg/wasm32.h tcg/wasm32/tcg-target.c.inc+27 -00 replies
27/33tcg/wasm32: Write wasm binary to TBtcg/wasm32.h tcg/wasm32/tcg-target.c.inc+416 -00 replies
28/33tcg/wasm32: Implement instantiation of Wasm binarytcg/wasm32.c+24 -00 replies
29/33tcg/wasm32: Allow Asyncify unwinding from TBtcg/wasm32.c tcg/wasm32.h tcg/wasm32/tcg-target.c.inc+68 -20 replies
30/33tcg/wasm32: Enable instantiation of TBs executed many timestcg/wasm32.c tcg/wasm32.h tcg/wasm32/tcg-target.c.inc+299 -40 replies
31/33tcg/wasm32: Enable TLB lookuptcg/wasm32/tcg-target.c.inc+221 -20 replies
32/33meson: Propagate optimization flag for linking on Emscriptenmeson.build+4 -00 replies
33/33.gitlab-ci.d: build wasm backend in CI.gitlab-ci.d/buildtest.yml+1 -10 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 00/33] tcg: Add WebAssembly backend

This patch series is split from the original "Enable QEMU to run on browsers" series, focusing solely on introducing a TCG backend for WebAssembly. This implemention is based on the latest master which already includes the essential changes required to compile QEMU (in 32bit TCI mode) using Emscripten.

# New TCG Backend for Browsers

A new TCG backend translates IR instructions into Wasm instructions and runs them using the browser's WebAssembly APIs (WebAssembly.Module and WebAssembly.instantiate). To minimize compilation overhead and avoid hitting the browser's limitation of the number of instances, this backend integrates a forked TCI. TBs run on TCI by default, with frequently executed TBs compiled into WebAssembly.

# Workaround for Running 64bit Guests

The current implementation uses Wasm's 32bit memory model. This series explores supporting TCG 64bit instructions while relying on SoftMMU for address translation. To enable 64bit guest support in Wasm today, it was necessary to partially revert recent changes that removed support for 64bit guests on 32bit hosts (e.g. commits a70af12addd9060fdf8f3dbd42b42e3072c3914f and bf455ec50b6fea15b4d2493059365bf94c706273) when compiling with Emscripten. The reverting is partial and addresses only pointer width differences between hosts and guests since the Wasm backend supports 64bit word operations. While this serves as a temporary workaround, a long-term solution could involve migrating to Wasm's 64bit memory model once it gains broader support, as it is currently not widely adopted (e.g. unsupported by Safari and libffi).

# Overview of build process

This section provides an overview of the build process for compiling QEMU using Emscripten. Full instructions are available in the sample repository[1].

To compile QEMU with Emscripten, the following dependencies are required. The emsdk-wasm32-cross.docker environment includes all necessary components and can be used as the build environment:

- Emscripten SDK (emsdk) v3.1.50
- Libraries cross-compiled with Emscripten (refer to emsdk-wasm32-cross.docker for build steps)
- GLib v2.84.0
- zlib v1.3.1
- libffi v3.4.7
- Pixman v0.44.2

QEMU can be compiled using Emscripten's emconfigure and emmake, which automatically set environment variables such as CC for targeting Emscripten.

emconfigure configure --static --disable-tools --target-list=x86_64-softmmu emmake make -j$(nproc)

This process generates the following files:

- qemu-system-x86_64.js
- qemu-system-x86_64.wasm
- qemu-system-x86_64.worker.js

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', '-accel', 'tcg,tb-size=500',
    '-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[1] provides a complete setup, including an HTML file that implements a terminal UI.

[1] https://github.com/ktock/qemu-wasm-sample

# Additional references

- A talk at FOSDEM 2025:
https://fosdem.org/2025/schedule/event/fosdem-2025-6290-running-qemu-inside-browser/
- Demo page on GitHub Pages: https://ktock.github.io/qemu-wasm-demo/

Kohei Tokunaga (33):
tcg: Fork TCI for wasm32 backend
tcg/wasm32: Do not use TCI disassembler in Wasm backend
meson: Enable to build wasm backend
tcg/wasm32: Set TCG_TARGET_INSN_UNIT_SIZE to 1 tcg/wasm32: Add and/or/xor instructions
tcg/wasm32: Add add/sub/mul instructions tcg/wasm32: Add shl/shr/sar instructions tcg/wasm32: Add setcond/negsetcond/movcond instructions tcg/wasm32: Add deposit/sextract/extract instrcutions tcg/wasm32: Add load and store instructions tcg/wasm32: Add mov/movi instructions
tcg/wasm32: Add ext instructions
tcg/wasm32: Add bswap instructions
tcg/wasm32: Add rem/div instructions
tcg/wasm32: Add andc/orc/eqv/nand/nor instructions tcg/wasm32: Add neg/not/ctpop instructions tcg/wasm32: Add rot/clz/ctz instructions tcg/wasm32: Add addc/subb instructions
tcg/wasm32: Add br/brcond instructions
tcg/wasm32: Add exit_tb/goto_tb/goto_ptr instructions tcg/wasm32: Add call instruction
tcg/wasm32: Add qemu_ld/qemu_st instructions include/exec: Allow using 64bit guest addresses on emscripten tcg/wasm32: Set TCG_TARGET_REG_BITS to 64 tcg/wasm32: Set mulu2/muls2 as unimplemented tcg/wasm32: Add initialization of fundamental registers tcg/wasm32: Write wasm binary to TB
tcg/wasm32: Implement instantiation of Wasm binary tcg/wasm32: Allow Asyncify unwinding from TB tcg/wasm32: Enable instantiation of TBs executed many times tcg/wasm32: Enable TLB lookup
meson: Propagate optimization flag for linking on Emscripten .gitlab-ci.d: build wasm backend in CI

 .gitlab-ci.d/buildtest.yml       |    2 +-
 MAINTAINERS                      |    7 +
 accel/tcg/cputlb.c               |    8 +-
 include/accel/tcg/getpc.h        |    2 +-
 include/exec/helper-head.h.inc   |    6 +
 include/exec/tlb-common.h        |   14 +-
 include/exec/vaddr.h             |   11 +
 include/qemu/atomic.h            |    4 +
 include/tcg/helper-info.h        |    4 +-
 include/tcg/tcg.h                |    6 +-
 meson.build                      |   16 +-
 tcg/aarch64/tcg-target.c.inc     |   11 +
 tcg/arm/tcg-target.c.inc         |   11 +
 tcg/i386/tcg-target.c.inc        |   11 +
 tcg/loongarch64/tcg-target.c.inc |   11 +
 tcg/meson.build                  |    5 +
 tcg/mips/tcg-target.c.inc        |   11 +
 tcg/ppc/tcg-target.c.inc         |   11 +
 tcg/region.c                     |   10 +-
 tcg/riscv/tcg-target.c.inc       |   11 +
 tcg/s390x/tcg-target.c.inc       |   11 +
 tcg/sparc64/tcg-target.c.inc     |   11 +
 tcg/tcg.c                        |   23 +-
 tcg/tci/tcg-target.c.inc         |   11 +
 tcg/wasm32.c                     | 1096 ++++++++
 tcg/wasm32.h                     |  119 +
 tcg/wasm32/tcg-target-con-set.h  |   21 +
 tcg/wasm32/tcg-target-con-str.h  |   11 +
 tcg/wasm32/tcg-target-has.h      |   22 +
 tcg/wasm32/tcg-target-mo.h       |   17 +
 tcg/wasm32/tcg-target-opc.h.inc  |   15 +
 tcg/wasm32/tcg-target-reg-bits.h |   12 +
 tcg/wasm32/tcg-target.c.inc      | 3985 ++++++++++++++++++++++++++++++
 tcg/wasm32/tcg-target.h          |   76 +
 34 files changed, 5569 insertions(+), 33 deletions(-)
 create mode 100644 tcg/wasm32.c
 create mode 100644 tcg/wasm32.h
 create mode 100644 tcg/wasm32/tcg-target-con-set.h
 create mode 100644 tcg/wasm32/tcg-target-con-str.h
 create mode 100644 tcg/wasm32/tcg-target-has.h
 create mode 100644 tcg/wasm32/tcg-target-mo.h
 create mode 100644 tcg/wasm32/tcg-target-opc.h.inc
 create mode 100644 tcg/wasm32/tcg-target-reg-bits.h
 create mode 100644 tcg/wasm32/tcg-target.c.inc
 create mode 100644 tcg/wasm32/tcg-target.h
Signature · 1 line
2.43.0