Skip to contentSkip to conversation

Enable QEMU to run on browsers

PATCH In review Kohei Tokunaga 7 participants 46 messages Apr 7, 2025 – Apr 13, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover Enable QEMU to run on browsers

This patch series enables QEMU's system emulator to run in a browser using Emscripten.
It includes implementations and workarounds to address browser environment limitations, as shown in the following.

# 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 64-bit Guests

The current implementation uses Wasm's 32-bit memory model, even though Wasm supports 64-bit variables and instructions. This patch explores implementing TCG 64-bit instructions while leveraging SoftMMU for address translation. To enable 64-bit guest support in Wasm today, it was necessary to partially revert recent changes that removed support for different pointer widths between the host and guest (e.g., commits a70af12addd9060fdf8f3dbd42b42e3072c3914f and bf455ec50b6fea15b4d2493059365bf94c706273) when compiling with Emscripten. While this serves as a temporary workaround, a long-term solution could involve adopting Wasm's 64-bit memory model once it gains broader support, as it is currently not widely adopted (e.g., unsupported by Safari and libffi). Feedback and suggestions on this approach are welcome.

# Emscripten-Based Coroutine Backend

Emscripten does not support couroutine methods currently used by QEMU but provides a coroutine implementation called "fiber". This patch series introduces a coroutine backend using fiber. However, fiber does not support submitting coroutines to other threads. So this patch series modifies hw/9pfs/coth.h to disable this behavior when compiled with Emscripten.

# 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 (10):
various: Fix type conflict of GLib function pointers
various: Define macros for dependencies on emscripten util/mmap-alloc: Add qemu_ram_mmap implementation for emscripten
util: Add coroutine backend for emscripten
meson: Add wasm build in build scripts
include/exec: Allow using 64bit guest addresses on emscripten
tcg: Add a TCG backend for WebAssembly
hw/9pfs: Allow using hw/9pfs with emscripten
gitlab: Enable CI for wasm build
MAINTAINERS: Update MAINTAINERS file for wasm-related files

 .gitlab-ci.d/buildtest-template.yml           |   27 +
 .gitlab-ci.d/buildtest.yml                    |    9 +
 .gitlab-ci.d/container-cross.yml              |    5 +
 MAINTAINERS                                   |   11 +
 accel/tcg/cputlb.c                            |    8 +-
 block/file-posix.c                            |   18 +
 configs/meson/emscripten.txt                  |    6 +
 configure                                     |    7 +
 fsdev/file-op-9p.h                            |    3 +
 fsdev/meson.build                             |    2 +-
 hw/9pfs/9p-util-stub.c                        |   43 +
 hw/9pfs/9p-util.h                             |   18 +
 hw/9pfs/9p.c                                  |    3 +
 hw/9pfs/coth.h                                |   12 +
 hw/9pfs/meson.build                           |    2 +
 hw/riscv/riscv_hart.c                         |    9 +-
 include/accel/tcg/getpc.h                     |    2 +-
 include/exec/tlb-common.h                     |   14 +-
 include/exec/vaddr.h                          |   11 +
 include/qemu/atomic.h                         |    4 +
 include/qemu/cacheflush.h                     |    3 +-
 include/tcg/helper-info.h                     |    4 +-
 include/tcg/tcg.h                             |    2 +-
 meson.build                                   |   30 +-
 meson_options.txt                             |    2 +-
 os-posix.c                                    |    5 +
 qom/object.c                                  |    5 +-
 scripts/meson-buildoptions.sh                 |    2 +-
 target/arm/helper.c                           |    4 +-
 tcg/meson.build                               |    5 +
 tcg/tcg.c                                     |   26 +-
 tcg/wasm32.c                                  | 1260 +++++
 tcg/wasm32.h                                  |   39 +
 tcg/wasm32/tcg-target-con-set.h               |   18 +
 tcg/wasm32/tcg-target-con-str.h               |    8 +
 tcg/wasm32/tcg-target-has.h                   |  102 +
 tcg/wasm32/tcg-target-mo.h                    |   12 +
 tcg/wasm32/tcg-target-opc.h.inc               |    4 +
 tcg/wasm32/tcg-target-reg-bits.h              |   12 +
 tcg/wasm32/tcg-target.c.inc                   | 4484 +++++++++++++++++
 tcg/wasm32/tcg-target.h                       |   65 +
 .../dockerfiles/emsdk-wasm32-cross.docker     |  145 +
 util/cacheflush.c                             |    3 +-
 util/coroutine-fiber.c                        |  127 +
 util/mmap-alloc.c                             |   18 +
 45 files changed, 6561 insertions(+), 38 deletions(-)
 create mode 100644 configs/meson/emscripten.txt
 create mode 100644 hw/9pfs/9p-util-stub.c
 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
 create mode 100644 tests/docker/dockerfiles/emsdk-wasm32-cross.docker
 create mode 100644 util/coroutine-fiber.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/10 various: Fix type conflict of GLib function pointers

On emscripten, function pointer casts can cause function call failure. This commit fixes the function definition to match to the type of the function call.

- qtest_set_command_cb passed to g_once should match to GThreadFunc
- object_class_cmp and cpreg_key_compare are passed to g_list_sort as GCopmareFunc but GLib cast them to GCompareDataFunc.

---
 hw/riscv/riscv_hart.c | 9 ++++++++-
 qom/object.c          | 5 +++--
 target/arm/helper.c   | 4 ++--
 3 files changed, 13 insertions(+), 5 deletions(-)
hw/riscv/riscv_hart.c +8 -1
index a55d156668..e37317dcbd 100644
@@ -102,10 +102,17 @@ static bool csr_qtest_callback(CharBackend *chr, gchar **words)
     return false;
 }
 
+static gpointer g_qtest_set_command_cb(
+    bool (*pc_cb)(CharBackend *chr, gchar **words))
+{
+    qtest_set_command_cb(pc_cb);
+    return NULL;
+}
+
 static void riscv_cpu_register_csr_qtest_callback(void)
 {
     static GOnce once;
-    g_once(&once, (GThreadFunc)qtest_set_command_cb, csr_qtest_callback);
+    g_once(&once, (GThreadFunc)g_qtest_set_command_cb, csr_qtest_callback);
 }
 #endif
 
qom/object.c +3 -2
index 01618d06bd..19698aae4c 100644
@@ -1191,7 +1191,8 @@ GSList *object_class_get_list(const char *implements_type,
     return list;
 }
 
-static gint object_class_cmp(gconstpointer a, gconstpointer b)
+static gint object_class_cmp(gconstpointer a, gconstpointer b,
+                             gpointer user_data)
 {
     return strcasecmp(object_class_get_name((ObjectClass *)a),
                       object_class_get_name((ObjectClass *)b));
@@ -1201,7 +1202,7 @@ GSList *object_class_get_list_sorted(const char *implements_type,
                                      bool include_abstract)
 {
     return g_slist_sort(object_class_get_list(implements_type, include_abstract),
-                        object_class_cmp);
+                        (GCompareFunc)object_class_cmp);
 }
 
 Object *object_ref(void *objptr)
target/arm/helper.c +2 -2
index bb445e30cd..68f81fadfc 100644
@@ -220,7 +220,7 @@ static void count_cpreg(gpointer key, gpointer opaque)
     }
 }
 
-static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
+static gint cpreg_key_compare(gconstpointer a, gconstpointer b, void *d)
 {
     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
@@ -244,7 +244,7 @@ void init_cpreg_list(ARMCPU *cpu)
     int arraylen;
 
     keys = g_hash_table_get_keys(cpu->cp_regs);
-    keys = g_list_sort(keys, cpreg_key_compare);
+    keys = g_list_sort(keys, (GCompareFunc)cpreg_key_compare);
 
     cpu->cpreg_array_len = 0;
 
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 01/10] various: Fix type conflict of GLib function pointers

Hi Kohei,

Quoted 20 lines · Kohei Tokunaga

On emscripten, function pointer casts can cause function call failure. This commit fixes the function definition to match to the type of the function call.

- qtest_set_command_cb passed to g_once should match to GThreadFunc
- object_class_cmp and cpreg_key_compare are passed to g_list_sort as GCopmareFunc but GLib cast them to GCompareDataFunc.

---
  hw/riscv/riscv_hart.c | 9 ++++++++-
  qom/object.c          | 5 +++--
  target/arm/helper.c   | 4 ++--
  3 files changed, 13 insertions(+), 5 deletions(-)
hw/riscv/riscv_hart.c +0 -0
index a55d156668..e37317dcbd 100644

hw/riscv/riscv_hart.c static bool csr_qtest_callback(CharBackend *chr, gchar **words) · line 107

Quoted 6 lines
hw/riscv/riscv_hart.c +3 -0
@@ -102,10 +102,17 @@ static bool csr_qtest_callback(CharBackend *chr, gchar **words)
      return false;
  }
 
+static gpointer g_qtest_set_command_cb(
+    bool (*pc_cb)(CharBackend *chr, gchar **words))
+{

Why not use a GThreadFunc prototype, casting the argument?

hw/riscv/riscv_hart.c static bool csr_qtest_callback(CharBackend *chr, gchar **words) · line 118

Quoted 12 lines
hw/riscv/riscv_hart.c +5 -1
+    qtest_set_command_cb(pc_cb);
+    return NULL;
+}
+
  static void riscv_cpu_register_csr_qtest_callback(void)
  {
      static GOnce once;
-    g_once(&once, (GThreadFunc)qtest_set_command_cb, csr_qtest_callback);
+    g_once(&once, (GThreadFunc)g_qtest_set_command_cb, csr_qtest_callback);
  }
  #endif
 

OK for the rest, but it might help to merge by corresponding maintainers if split in 3 distinct patches.

Quoted 45 lines
qom/object.c +3 -2
index 01618d06bd..19698aae4c 100644
@@ -1191,7 +1191,8 @@ GSList *object_class_get_list(const char *implements_type,
      return list;
  }
 
-static gint object_class_cmp(gconstpointer a, gconstpointer b)
+static gint object_class_cmp(gconstpointer a, gconstpointer b,
+                             gpointer user_data)
  {
      return strcasecmp(object_class_get_name((ObjectClass *)a),
                        object_class_get_name((ObjectClass *)b));
@@ -1201,7 +1202,7 @@ GSList *object_class_get_list_sorted(const char *implements_type,
                                       bool include_abstract)
  {
      return g_slist_sort(object_class_get_list(implements_type, include_abstract),
-                        object_class_cmp);
+                        (GCompareFunc)object_class_cmp);
  }
 
  Object *object_ref(void *objptr)
target/arm/helper.c +2 -2
index bb445e30cd..68f81fadfc 100644
@@ -220,7 +220,7 @@ static void count_cpreg(gpointer key, gpointer opaque)
      }
  }
 
-static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
+static gint cpreg_key_compare(gconstpointer a, gconstpointer b, void *d)
  {
      uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
      uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
@@ -244,7 +244,7 @@ void init_cpreg_list(ARMCPU *cpu)
      int arraylen;
 
      keys = g_hash_table_get_keys(cpu->cp_regs);
-    keys = g_list_sort(keys, cpreg_key_compare);
+    keys = g_list_sort(keys, (GCompareFunc)cpreg_key_compare);
 
      cpu->cpreg_array_len = 0;
 
#3 lore raw

Re: [PATCH 01/10] various: Fix type conflict of GLib function pointers

Hi Philippe, thank you for the comments.

Quoted 5 lines

hw/riscv/riscv_hart.c static bool csr_qtest_callback(CharBackend *chr, gchar **words) · line 107

Quoted 3 lines
hw/riscv/riscv_hart.c +3 -0
+static gpointer g_qtest_set_command_cb(
+    bool (*pc_cb)(CharBackend *chr, gchar **words))
+{

Why not use a GThreadFunc prototype, casting the argument?

Sure, I'll fix this.

Quoted 2 lines

OK for the rest, but it might help to merge by corresponding maintainers if split in 3 distinct patches.

Thank you for the suggestion. I'll split it to distinct patches in the next version of the series.

#4 lore raw

Re: [PATCH 01/10] various: Fix type conflict of GLib function pointers

In reply to Kohei Tokunaga
Quoted 5 lines · Kohei Tokunaga

On emscripten, function pointer casts can cause function call failure. This commit fixes the function definition to match to the type of the function call.

- qtest_set_command_cb passed to g_once should match to GThreadFunc

Sending an alternative patch that doesn't use GOnce, this code runs in the main thread.

Quoted 2 lines

- object_class_cmp and cpreg_key_compare are passed to g_list_sort as GCopmareFunc but GLib cast them to GCompareDataFunc.

Please use g_list_sort_with_data instead, and poison g_slist_sort/g_list_sort in include/glib-compat.h, with a comment explaining that it's done this way because of Emscripten.

Paolo

Quoted 75 lines
---
  hw/riscv/riscv_hart.c | 9 ++++++++-
  qom/object.c          | 5 +++--
  target/arm/helper.c   | 4 ++--
  3 files changed, 13 insertions(+), 5 deletions(-)
hw/riscv/riscv_hart.c +8 -1
index a55d156668..e37317dcbd 100644
@@ -102,10 +102,17 @@ static bool csr_qtest_callback(CharBackend *chr, gchar **words)
      return false;
  }
 
+static gpointer g_qtest_set_command_cb(
+    bool (*pc_cb)(CharBackend *chr, gchar **words))
+{
+    qtest_set_command_cb(pc_cb);
+    return NULL;
+}
+
  static void riscv_cpu_register_csr_qtest_callback(void)
  {
      static GOnce once;
-    g_once(&once, (GThreadFunc)qtest_set_command_cb, csr_qtest_callback);
+    g_once(&once, (GThreadFunc)g_qtest_set_command_cb, csr_qtest_callback);
  }
  #endif
 
qom/object.c +3 -2
index 01618d06bd..19698aae4c 100644
@@ -1191,7 +1191,8 @@ GSList *object_class_get_list(const char *implements_type,
      return list;
  }
 
-static gint object_class_cmp(gconstpointer a, gconstpointer b)
+static gint object_class_cmp(gconstpointer a, gconstpointer b,
+                             gpointer user_data)
  {
      return strcasecmp(object_class_get_name((ObjectClass *)a),
                        object_class_get_name((ObjectClass *)b));
@@ -1201,7 +1202,7 @@ GSList *object_class_get_list_sorted(const char *implements_type,
                                       bool include_abstract)
  {
      return g_slist_sort(object_class_get_list(implements_type, include_abstract),
-                        object_class_cmp);
+                        (GCompareFunc)object_class_cmp);
  }
 
  Object *object_ref(void *objptr)
target/arm/helper.c +2 -2
index bb445e30cd..68f81fadfc 100644
@@ -220,7 +220,7 @@ static void count_cpreg(gpointer key, gpointer opaque)
      }
  }
 
-static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
+static gint cpreg_key_compare(gconstpointer a, gconstpointer b, void *d)
  {
      uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
      uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
@@ -244,7 +244,7 @@ void init_cpreg_list(ARMCPU *cpu)
      int arraylen;
 
      keys = g_hash_table_get_keys(cpu->cp_regs);
-    keys = g_list_sort(keys, cpreg_key_compare);
+    keys = g_list_sort(keys, (GCompareFunc)cpreg_key_compare);
 
      cpu->cpreg_array_len = 0;
 
#5 lore raw

Re: [PATCH 01/10] various: Fix type conflict of GLib function pointers

Hi Paolo,

Quoted 8 lines
Quoted 5 lines

On emscripten, function pointer casts can cause function call failure. This commit fixes the function definition to match to the type of the function call.

- qtest_set_command_cb passed to g_once should match to GThreadFunc

Sending an alternative patch that doesn't use GOnce, this code runs in the main thread.

Thank you for addressing this issue. I've sent a review to that patch.

Quoted 6 lines
Quoted 2 lines

- object_class_cmp and cpreg_key_compare are passed to g_list_sort as GCopmareFunc but GLib cast them to GCompareDataFunc.

Please use g_list_sort_with_data instead, and poison g_slist_sort/g_list_sort in include/glib-compat.h, with a comment explaining that it's done this way because of Emscripten.

Sure, I’ll fix this in the next version of the series.

#6 lore raw

PATCH 2/10 various: Define macros for dependencies on emscripten

In reply to Kohei Tokunaga
---
 block/file-posix.c        | 18 ++++++++++++++++++
 include/qemu/cacheflush.h |  3 ++-
 os-posix.c                |  5 +++++
 util/cacheflush.c         |  3 ++-
 4 files changed, 27 insertions(+), 2 deletions(-)
block/file-posix.c +18 -0
index 56d1972d15..69f54505bd 100644
@@ -110,6 +110,10 @@
 #include <sys/diskslice.h>
 #endif
 
+#ifdef EMSCRIPTEN
+#include <sys/ioctl.h>
+#endif
+
 /* OS X does not have O_DSYNC */
 #ifndef O_DSYNC
 #ifdef O_SYNC
@@ -2011,6 +2015,19 @@ static int handle_aiocb_write_zeroes_unmap(void *opaque)
 }
 
 #ifndef HAVE_COPY_FILE_RANGE
+#ifdef EMSCRIPTEN
+/*
+ * emscripten exposes copy_file_range declaration but doesn't provide the
+ * implementation in the final link. Define the stub here but avoid type
+ * conflict with the emscripten's header.
+ */
+ssize_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
+                             off_t *out_off, size_t len, unsigned int flags)
+{
+    errno = ENOSYS;
+    return -1;
+}
+#else
 static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
                              off_t *out_off, size_t len, unsigned int flags)
 {
@@ -2023,6 +2040,7 @@ static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
 #endif
 }
 #endif
+#endif
 
 /*
  * parse_zone - Fill a zone descriptor
include/qemu/cacheflush.h +2 -1
index ae20bcda73..84969801e3 100644
@@ -19,7 +19,8 @@
  * mappings of the same physical page(s).
  */
 
-#if defined(__i386__) || defined(__x86_64__) || defined(__s390__)
+#if defined(__i386__) || defined(__x86_64__) || defined(__s390__) \
+    || defined(EMSCRIPTEN)
 
 static inline void flush_idcache_range(uintptr_t rx, uintptr_t rw, size_t len)
 {
os-posix.c +5 -0
index 52925c23d3..9a7099e279 100644
@@ -148,11 +148,16 @@ static void change_process_uid(void)
             exit(1);
         }
         if (user_pwd) {
+#ifdef EMSCRIPTEN
+            error_report("initgroups unsupported");
+            exit(1);
+#else
             if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) {
                 error_report("Failed to initgroups(\"%s\", %d)",
                         user_pwd->pw_name, user_pwd->pw_gid);
                 exit(1);
             }
+#endif
         } else {
             if (setgroups(1, &user_gid) < 0) {
                 error_report("Failed to setgroups(1, [%d])",
util/cacheflush.c +2 -1
index 1d12899a39..e5aa256cd8 100644
@@ -225,7 +225,8 @@ static void __attribute__((constructor)) init_cache_info(void)
  * Architecture (+ OS) specific cache flushing mechanisms.
  */
 
-#if defined(__i386__) || defined(__x86_64__) || defined(__s390__)
+#if defined(__i386__) || defined(__x86_64__) || defined(__s390__) || \
+    defined(EMSCRIPTEN)
 
 /* Caches are coherent and do not require flushing; symbol inline. */
 
Signature · 1 line
2.25.1
#7 lore raw

Re: [PATCH 02/10] various: Define macros for dependencies on emscripten

block/file-posix.c static int handle_aiocb_write_zeroes_unmap(void *opaque) · line 2029

Quoted 12 lines · Kohei Tokunaga
block/file-posix.c +12 -0
+#ifdef EMSCRIPTEN
+/*
+ * emscripten exposes copy_file_range declaration but doesn't provide the
+ * implementation in the final link. Define the stub here but avoid type
+ * conflict with the emscripten's header.
+ */
+ssize_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
+                             off_t *out_off, size_t len, unsigned int flags)
+{
+    errno = ENOSYS;
+    return -1;
+}

Please add a file stubs/emscripten.c with this function, and add it to the build in stubs/meson.build.

os-posix.c static void change_process_uid(void) · line 153

Quoted 3 lines
os-posix.c +3 -0
+#ifdef EMSCRIPTEN
+            error_report("initgroups unsupported");
+            exit(1);

I think it's best to add a new function os-wasm.c in addition to os-posix.c and os-win32.c, and disable all the functionality of -run-with and -daemonize in vl.c via

-#if defined(CONFIG_POSIX)
+#if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)

(there are a couple occurrences).

Thanks,

Paolo

#8 lore raw

Re: [PATCH 02/10] various: Define macros for dependencies on emscripten

Hi Paolo,

Quoted 3 lines

block/file-posix.c static int handle_aiocb_write_zeroes_unmap(void *opaque)

Quoted 3 lines
block/file-posix.c +3 -0
+#ifdef EMSCRIPTEN
+/*
+ * emscripten exposes copy_file_range declaration but doesn't provide

the

Quoted 1 line
Quoted 1 line

+ * implementation in the final link. Define the stub here but avoid

type

Quoted 4 lines

Review

Quoted 4 lines

+ * conflict with the emscripten's header.
+ */

(quoted diff) +2 -0
+ssize_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
+                             off_t *out_off, size_t len, unsigned int

flags)

Quoted 19 lines

block/file-posix.c static int handle_aiocb_write_zeroes_unmap(void *opaque) · line 2029

Quoted 4 lines
block/file-posix.c +4 -0
+{
+    errno = ENOSYS;
+    return -1;
+}

Please add a file stubs/emscripten.c with this function, and add it to the build in stubs/meson.build.

os-posix.c static void change_process_uid(void) · line 153

Quoted 3 lines
os-posix.c +3 -0
+#ifdef EMSCRIPTEN
+            error_report("initgroups unsupported");
+            exit(1);

I think it's best to add a new function os-wasm.c in addition to os-posix.c and os-win32.c, and disable all the functionality of

(quoted diff) +1 -2
-run-with and -daemonize in vl.c via
 
-#if defined(CONFIG_POSIX)
+#if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)
Quoted 1 line

(there are a couple occurrences).

Sure, I'll apply these reorganization in the next version of the series.

#9 lore raw

PATCH 3/10 util/mmap-alloc: Add qemu_ram_mmap implementation for emscripten

In reply to Kohei Tokunaga
---
 util/mmap-alloc.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
util/mmap-alloc.c +18 -0
index ed14f9c64d..91f33682e8 100644
@@ -145,6 +145,7 @@ static bool map_noreserve_effective(int fd, uint32_t qemu_map_flags)
     return false;
 }
 
+#ifndef EMSCRIPTEN
 /*
  * Reserve a new memory region of the requested size to be used for mapping
  * from the given fd (if any).
@@ -176,6 +177,7 @@ static void *mmap_reserve(size_t size, int fd)
 
     return mmap(0, size, PROT_NONE, flags, fd, 0);
 }
+#endif
 
 /*
  * Activate memory in a reserved region from the given fd (if any), to make
@@ -244,6 +246,21 @@ static inline size_t mmap_guard_pagesize(int fd)
 #endif
 }
 
+#ifdef EMSCRIPTEN
+void *qemu_ram_mmap(int fd,
+                    size_t size,
+                    size_t align,
+                    uint32_t qemu_map_flags,
+                    off_t map_offset)
+{
+    /*
+     * emscripten doesn't support non-zero first argument for mmap so
+     * mmap a larger region without the hint and return an aligned pointer.
+     */
+    void *ptr = mmap_activate(0, size + align, fd, qemu_map_flags, map_offset);
+    return (void *)QEMU_ALIGN_UP((uintptr_t)ptr, align);
+}
+#else
 void *qemu_ram_mmap(int fd,
                     size_t size,
                     size_t align,
@@ -293,6 +310,7 @@ void *qemu_ram_mmap(int fd,
 
     return ptr;
 }
+#endif /* EMSCRIPTEN */
 
 void qemu_ram_munmap(int fd, void *ptr, size_t size)
 {
Signature · 1 line
2.25.1
#10 lore raw

PATCH 4/10 util: Add coroutine backend for emscripten

In reply to Kohei Tokunaga

Emscripten does not support couroutine methods currently used by QEMU but provides a coroutine implementation called "fiber". This commit introduces a coroutine backend using fiber. Note that fiber does not support submitting coroutines to other threads.

---
 util/coroutine-fiber.c | 127 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 127 insertions(+)
 create mode 100644 util/coroutine-fiber.c
util/coroutine-fiber.c new file +127 -0
new file mode 100644
index 0000000000..cb1ec92509
@@ -0,0 +1,127 @@
+/*
+ * emscripten fiber coroutine initialization code
+ * based on coroutine-ucontext.c
+ *
+ * Copyright (C) 2006  Anthony Liguori <[email protected]>
+ * Copyright (C) 2011  Kevin Wolf <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.0 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/coroutine_int.h"
+#include "qemu/coroutine-tls.h"
+
+#include <emscripten/fiber.h>
+
+typedef struct {
+    Coroutine base;
+    void *stack;
+    size_t stack_size;
+
+    void *asyncify_stack;
+    size_t asyncify_stack_size;
+
+    CoroutineAction action;
+
+    emscripten_fiber_t fiber;
+} CoroutineEmscripten;
+
+/**
+ * Per-thread coroutine bookkeeping
+ */
+QEMU_DEFINE_STATIC_CO_TLS(Coroutine *, current);
+QEMU_DEFINE_STATIC_CO_TLS(CoroutineEmscripten *, leader);
+size_t leader_asyncify_stack_size = COROUTINE_STACK_SIZE;
+
+static void coroutine_trampoline(void *co_)
+{
+    Coroutine *co = co_;
+
+    while (true) {
+        co->entry(co->entry_arg);
+        qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
+    }
+}
+
+Coroutine *qemu_coroutine_new(void)
+{
+    CoroutineEmscripten *co;
+
+    co = g_malloc0(sizeof(*co));
+
+    co->stack_size = COROUTINE_STACK_SIZE;
+    co->stack = qemu_alloc_stack(&co->stack_size);
+
+    co->asyncify_stack_size = COROUTINE_STACK_SIZE;
+    co->asyncify_stack = g_malloc0(co->asyncify_stack_size);
+    emscripten_fiber_init(&co->fiber, coroutine_trampoline, &co->base,
+                          co->stack, co->stack_size, co->asyncify_stack,
+                          co->asyncify_stack_size);
+
+    return &co->base;
+}
+
+void qemu_coroutine_delete(Coroutine *co_)
+{
+    CoroutineEmscripten *co = DO_UPCAST(CoroutineEmscripten, base, co_);
+
+    qemu_free_stack(co->stack, co->stack_size);
+    g_free(co->asyncify_stack);
+    g_free(co);
+}
+
+CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
+                      CoroutineAction action)
+{
+    CoroutineEmscripten *from = DO_UPCAST(CoroutineEmscripten, base, from_);
+    CoroutineEmscripten *to = DO_UPCAST(CoroutineEmscripten, base, to_);
+
+    set_current(to_);
+    to->action = action;
+    emscripten_fiber_swap(&from->fiber, &to->fiber);
+    return from->action;
+}
+
+Coroutine *qemu_coroutine_self(void)
+{
+    Coroutine *self = get_current();
+
+    if (!self) {
+        CoroutineEmscripten *leaderp = get_leader();
+        if (!leaderp) {
+            leaderp = g_malloc0(sizeof(*leaderp));
+            leaderp->asyncify_stack = g_malloc0(leader_asyncify_stack_size);
+            leaderp->asyncify_stack_size = leader_asyncify_stack_size;
+            emscripten_fiber_init_from_current_context(
+                &leaderp->fiber,
+                leaderp->asyncify_stack,
+                leaderp->asyncify_stack_size);
+            leaderp->stack = leaderp->fiber.stack_limit;
+            leaderp->stack_size =
+                leaderp->fiber.stack_base - leaderp->fiber.stack_limit;
+            set_leader(leaderp);
+        }
+        self = &leaderp->base;
+        set_current(self);
+    }
+    return self;
+}
+
+bool qemu_in_coroutine(void)
+{
+    Coroutine *self = get_current();
+
+    return self && self->caller;
+}
Signature · 1 line
2.25.1
#11 lore raw

PATCH 5/10 meson: Add wasm build in build scripts

In reply to Kohei Tokunaga

has_int128_type is set to false on emscripten as of now to avoid errors by libffi. And tests aren't integrated with Wasm execution environment as of now so this commit disables tests.

---
 configs/meson/emscripten.txt  |  6 ++++++
 configure                     |  7 +++++++
 meson.build                   | 14 ++++++++++----
 meson_options.txt             |  2 +-
 scripts/meson-buildoptions.sh |  2 +-
 5 files changed, 25 insertions(+), 6 deletions(-)
 create mode 100644 configs/meson/emscripten.txt
configs/meson/emscripten.txt new file +6 -0
new file mode 100644
index 0000000000..054b263814
@@ -0,0 +1,6 @@
+[built-in options]
+c_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread']
+cpp_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread']
+objc_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread']
+c_link_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread','-sASYNCIFY=1','-sPROXY_TO_PTHREAD=1','-sFORCE_FILESYSTEM','-sALLOW_TABLE_GROWTH','-sTOTAL_MEMORY=2GB','-sWASM_BIGINT','-sEXPORT_ES6=1','-sASYNCIFY_IMPORTS=ffi_call_js','-sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction,TTY,FS']
+cpp_link_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread','-sASYNCIFY=1','-sPROXY_TO_PTHREAD=1','-sFORCE_FILESYSTEM','-sALLOW_TABLE_GROWTH','-sTOTAL_MEMORY=2GB','-sWASM_BIGINT','-sEXPORT_ES6=1','-sASYNCIFY_IMPORTS=ffi_call_js','-sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction,TTY,FS']
configure +7 -0
index 02f1dd2311..a1fe6e11cd 100755
@@ -360,6 +360,10 @@ elif check_define __NetBSD__; then
   host_os=netbsd
 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
   # might be going to just print the --help text, or it might
@@ -526,6 +530,9 @@ case "$cpu" in
     linux_arch=x86
     CPU_CFLAGS="-m64"
     ;;
+  wasm32)
+    CPU_CFLAGS="-m32"
+    ;;
 esac
 
 if test -n "$host_arch" && {
meson.build +10 -4
index 41f68d3806..bcf1e33ddf 100644
@@ -50,9 +50,9 @@ genh = []
 qapi_trace_events = []
 
 bsd_oses = ['gnu/kfreebsd', 'freebsd', 'netbsd', 'openbsd', 'dragonfly', 'darwin']
-supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux']
+supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux', 'emscripten']
 supported_cpus = ['ppc', 'ppc64', 's390x', 'riscv32', 'riscv64', 'x86', 'x86_64',
-  'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc64']
+  'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc64', 'wasm32']
 
 cpu = host_machine.cpu_family()
 
@@ -353,6 +353,8 @@ foreach lang : all_languages
       # endif
       #endif''')
     # ok
+  elif compiler.get_id() == 'emscripten'
+    # ok
   else
     error('You either need GCC v7.4 or Clang v10.0 (or XCode Clang v15.0) to compile QEMU')
   endif
@@ -514,6 +516,8 @@ ucontext_probe = '''
 supported_backends = []
 if host_os == 'windows'
   supported_backends += ['windows']
+elif host_os == 'emscripten'
+  supported_backends += ['fiber']
 else
   if host_os != 'darwin' and cc.links(ucontext_probe)
     supported_backends += ['ucontext']
@@ -2962,7 +2966,7 @@ config_host_data.set('CONFIG_ATOMIC64', cc.links('''
     return 0;
   }''', args: qemu_isa_flags))
 
-has_int128_type = cc.compiles('''
+has_int128_type = host_os != 'emscripten' and cc.compiles('''
   __int128_t a;
   __uint128_t b;
   int main(void) { b = a; }''')
@@ -4456,7 +4460,9 @@ subdir('scripts')
 subdir('tools')
 subdir('pc-bios')
 subdir('docs')
-subdir('tests')
+if host_os != 'emscripten'
+  subdir('tests')
+endif
 if gtk.found()
   subdir('po')
 endif
meson_options.txt +1 -1
index 59d973bca0..6d73aafe91 100644
@@ -34,7 +34,7 @@ option('fuzzing_engine', type : 'string', value : '',
 option('trace_file', type: 'string', value: 'trace',
        description: 'Trace file prefix for simple backend')
 option('coroutine_backend', type: 'combo',
-       choices: ['ucontext', 'sigaltstack', 'windows', 'auto'],
+       choices: ['ucontext', 'sigaltstack', 'windows', 'auto', 'fiber'],
        value: 'auto', description: 'coroutine backend to use')
 
 # Everything else can be set via --enable/--disable-* option
scripts/meson-buildoptions.sh +1 -1
index 3e8e00852b..cbba2f248c 100644
@@ -80,7 +80,7 @@ meson_options_help() {
   printf "%s\n" '  --tls-priority=VALUE     Default TLS protocol/cipher priority string'
   printf "%s\n" '                           [NORMAL]'
   printf "%s\n" '  --with-coroutine=CHOICE  coroutine backend to use (choices:'
-  printf "%s\n" '                           auto/sigaltstack/ucontext/windows)'
+  printf "%s\n" '                           auto/fiber/sigaltstack/ucontext/windows)'
   printf "%s\n" '  --with-pkgversion=VALUE  use specified string as sub-version of the'
   printf "%s\n" '                           package'
   printf "%s\n" '  --with-suffix=VALUE      Suffix for QEMU data/modules/config directories'
Signature · 1 line
2.25.1
#12 lore raw

Re: [PATCH 05/10] meson: Add wasm build in build scripts

Cc'ing Pierrick

Quoted 131 lines · Kohei Tokunaga

has_int128_type is set to false on emscripten as of now to avoid errors by libffi. And tests aren't integrated with Wasm execution environment as of now so this commit disables tests.

---
  configs/meson/emscripten.txt  |  6 ++++++
  configure                     |  7 +++++++
  meson.build                   | 14 ++++++++++----
  meson_options.txt             |  2 +-
  scripts/meson-buildoptions.sh |  2 +-
  5 files changed, 25 insertions(+), 6 deletions(-)

create mode 100644 configs/meson/emscripten.txt

configs/meson/emscripten.txt new file +6 -0
new file mode 100644
index 0000000000..054b263814
@@ -0,0 +1,6 @@
+[built-in options]
+c_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread']
+cpp_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread']
+objc_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread']
+c_link_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread','-sASYNCIFY=1','-sPROXY_TO_PTHREAD=1','-sFORCE_FILESYSTEM','-sALLOW_TABLE_GROWTH','-sTOTAL_MEMORY=2GB','-sWASM_BIGINT','-sEXPORT_ES6=1','-sASYNCIFY_IMPORTS=ffi_call_js','-sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction,TTY,FS']
+cpp_link_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread','-sASYNCIFY=1','-sPROXY_TO_PTHREAD=1','-sFORCE_FILESYSTEM','-sALLOW_TABLE_GROWTH','-sTOTAL_MEMORY=2GB','-sWASM_BIGINT','-sEXPORT_ES6=1','-sASYNCIFY_IMPORTS=ffi_call_js','-sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction,TTY,FS']
configure +7 -0
index 02f1dd2311..a1fe6e11cd 100755
@@ -360,6 +360,10 @@ elif check_define __NetBSD__; then
    host_os=netbsd
  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
    # might be going to just print the --help text, or it might
@@ -526,6 +530,9 @@ case "$cpu" in
      linux_arch=x86
      CPU_CFLAGS="-m64"
      ;;
+  wasm32)
+    CPU_CFLAGS="-m32"
+    ;;
  esac
 
  if test -n "$host_arch" && {
meson.build +10 -4
index 41f68d3806..bcf1e33ddf 100644
@@ -50,9 +50,9 @@ genh = []
  qapi_trace_events = []
 
  bsd_oses = ['gnu/kfreebsd', 'freebsd', 'netbsd', 'openbsd', 'dragonfly', 'darwin']
-supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux']
+supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux', 'emscripten']
  supported_cpus = ['ppc', 'ppc64', 's390x', 'riscv32', 'riscv64', 'x86', 'x86_64',
-  'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc64']
+  'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc64', 'wasm32']
 
  cpu = host_machine.cpu_family()
 
@@ -353,6 +353,8 @@ foreach lang : all_languages
        # endif
        #endif''')
      # ok
+  elif compiler.get_id() == 'emscripten'
+    # ok
    else
      error('You either need GCC v7.4 or Clang v10.0 (or XCode Clang v15.0) to compile QEMU')
    endif
@@ -514,6 +516,8 @@ ucontext_probe = '''
  supported_backends = []
  if host_os == 'windows'
    supported_backends += ['windows']
+elif host_os == 'emscripten'
+  supported_backends += ['fiber']
  else
    if host_os != 'darwin' and cc.links(ucontext_probe)
      supported_backends += ['ucontext']
@@ -2962,7 +2966,7 @@ config_host_data.set('CONFIG_ATOMIC64', cc.links('''
      return 0;
    }''', args: qemu_isa_flags))
 
-has_int128_type = cc.compiles('''
+has_int128_type = host_os != 'emscripten' and cc.compiles('''
    __int128_t a;
    __uint128_t b;
    int main(void) { b = a; }''')
@@ -4456,7 +4460,9 @@ subdir('scripts')
  subdir('tools')
  subdir('pc-bios')
  subdir('docs')
-subdir('tests')
+if host_os != 'emscripten'
+  subdir('tests')
+endif
  if gtk.found()
    subdir('po')
  endif
meson_options.txt +1 -1
index 59d973bca0..6d73aafe91 100644
@@ -34,7 +34,7 @@ option('fuzzing_engine', type : 'string', value : '',
  option('trace_file', type: 'string', value: 'trace',
         description: 'Trace file prefix for simple backend')
  option('coroutine_backend', type: 'combo',
-       choices: ['ucontext', 'sigaltstack', 'windows', 'auto'],
+       choices: ['ucontext', 'sigaltstack', 'windows', 'auto', 'fiber'],
         value: 'auto', description: 'coroutine backend to use')
 
  # Everything else can be set via --enable/--disable-* option
scripts/meson-buildoptions.sh +1 -1
index 3e8e00852b..cbba2f248c 100644
@@ -80,7 +80,7 @@ meson_options_help() {
    printf "%s\n" '  --tls-priority=VALUE     Default TLS protocol/cipher priority string'
    printf "%s\n" '                           [NORMAL]'
    printf "%s\n" '  --with-coroutine=CHOICE  coroutine backend to use (choices:'
-  printf "%s\n" '                           auto/sigaltstack/ucontext/windows)'
+  printf "%s\n" '                           auto/fiber/sigaltstack/ucontext/windows)'
    printf "%s\n" '  --with-pkgversion=VALUE  use specified string as sub-version of the'
    printf "%s\n" '                           package'
    printf "%s\n" '  --with-suffix=VALUE      Suffix for QEMU data/modules/config directories'
#13 lore raw

Re: [PATCH 05/10] meson: Add wasm build in build scripts

Quoted 5 lines · Philippe Mathieu-Daudé

Cc'ing Pierrick

Quoted 2 lines · Kohei Tokunaga

has_int128_type is set to false on emscripten as of now to avoid errors by libffi.

What is the error here? How hard would it be to test for it?

Quoted 2 lines
Quoted 2 lines

And tests aren't integrated with Wasm execution environment as of now so this commit disables tests.

Perhaps it would be enough to add

[binaries]
exe_wrapper = 'node'

to the emscripten.txt file?

Quoted 8 lines
Quoted 8 lines

+[built-in options]

configs/meson/emscripten.txt new file +4 -0
+c_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread']
+cpp_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread']
+objc_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread']
+c_link_args = ['-Wno-unused-command-line-argument','-g','-O3','- 

pthread','-sASYNCIFY=1','-sPROXY_TO_PTHREAD=1','-sFORCE_FILESYSTEM','- sALLOW_TABLE_GROWTH','-sTOTAL_MEMORY=2GB','-sWASM_BIGINT','- sEXPORT_ES6=1','-sASYNCIFY_IMPORTS=ffi_call_js','- sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction,TTY,FS'] +cpp_link_args = ['-Wno-unused-command-line-argument','-g','-O3','-
pthread','-sASYNCIFY=1','-sPROXY_TO_PTHREAD=1','-sFORCE_FILESYSTEM','- sALLOW_TABLE_GROWTH','-sTOTAL_MEMORY=2GB','-sWASM_BIGINT','- sEXPORT_ES6=1','-sASYNCIFY_IMPORTS=ffi_call_js','- sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction,TTY,FS']

At least -g -O3 -pthread should not be necessary.

For -Wno-unused-command-line-argument what are the warnings/errors that you are getting?

Quoted 2 lines
Quoted 2 lines

+elif host_os == 'emscripten'
+  supported_backends += ['fiber']

Can you rename the backend to 'wasm' since the 'windows' backend also uses an API called Fibers?

Otherwise the changes look good.

Paolo

#14 lore raw

Re: [PATCH 05/10] meson: Add wasm build in build scripts

Hi Paolo, thank you for the comments.

Quoted 1 line
Quoted 1 line
Quoted 1 line

has_int128_type is set to false on emscripten as of now to avoid

errors by

Quoted 3 lines
Quoted 1 line
Quoted 1 line

libffi.

What is the error here? How hard would it be to test for it?

When has_int128_type=true, I encountered a runtime error from libffi. To reproduce this, we need to actually execute a libffi call with 128-bit arguments.

Quoted 4 lines

Uncaught TypeError: Cannot convert 1079505232 to a BigInt

    at ffi_call_js (out.js:702:37)
    at qemu-system-x86_64.wasm.ffi_call (qemu-system-x86_64.wasm:0xa37712)
    at qemu-system-x86_64.wasm.tcg_qemu_tb_exec_tci

(qemu-system-x86_64.wasm:0x65f440)

Quoted 1 line
    at qemu-system-x86_64.wasm.tcg_qemu_tb_exec

(qemu-system-x86_64.wasm:0x65edff)

Quoted 1 line
    at qemu-system-x86_64.wasm.cpu_tb_exec

(qemu-system-x86_64.wasm:0x6762c0)

Quoted 1 line
    at qemu-system-x86_64.wasm.cpu_exec_loop

(qemu-system-x86_64.wasm:0x677c84)

Quoted 1 line
    at qemu-system-x86_64.wasm.dynCall_iii

(qemu-system-x86_64.wasm:0xab9014)

Quoted 3 lines
    at ret.<computed> (out.js:6016:24)
    at invoke_iii (out.js:7574:10)
    at qemu-system-x86_64.wasm.cpu_exec_setjmp

(qemu-system-x86_64.wasm:0x676db8)

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

And tests aren't integrated with Wasm execution environment as of now so this commit disables tests.

Perhaps it would be enough to add

[binaries]
exe_wrapper = 'node'

to the emscripten.txt file?

Thank you for the suggestion. I'll explore this approach.

Quoted 4 lines
Quoted 4 lines

configs/meson/emscripten.txt

Quoted 4 lines

+[built-in options]

configs/meson/emscripten.txt new file +3 -0
+c_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread']
+cpp_args = ['-Wno-unused-command-line-argument','-g','-O3','-pthread']
+objc_args =

['-Wno-unused-command-line-argument','-g','-O3','-pthread']

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

+c_link_args = ['-Wno-unused-command-line-argument','-g','-O3','- pthread','-sASYNCIFY=1','-sPROXY_TO_PTHREAD=1','-sFORCE_FILESYSTEM','- sALLOW_TABLE_GROWTH','-sTOTAL_MEMORY=2GB','-sWASM_BIGINT','- sEXPORT_ES6=1','-sASYNCIFY_IMPORTS=ffi_call_js','-

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction,TTY,FS'] +cpp_link_args = ['-Wno-unused-command-line-argument','-g','-O3','- pthread','-sASYNCIFY=1','-sPROXY_TO_PTHREAD=1','-sFORCE_FILESYSTEM','- sALLOW_TABLE_GROWTH','-sTOTAL_MEMORY=2GB','-sWASM_BIGINT','- sEXPORT_ES6=1','-sASYNCIFY_IMPORTS=ffi_call_js','- sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction,TTY,FS']

At least -g -O3 -pthread should not be necessary.

Thank you for the suggestion. -sPROXY_TO_PTHREAD flag used in c_link_args always requires -pthread, even during configuration. Otherwise, emcc returns an error like:

Quoted 1 line

emcc: error: -sPROXY_TO_PTHREAD requires -pthread to work!

So I think -pthread needs to be included in c_link_args at minimum. I'll try to remove other flags in the next version of the series.

Quoted 2 lines

For -Wno-unused-command-line-argument what are the warnings/errors that you are getting?

I encountered the following error when compiling QEMU:

Quoted 1 line

clang: error: argument unused during compilation: '-no-pie'

[-Werror,-Wunused-command-line-argument]

It seems Emscripten doesn't support the -no-pie flag, and this wasn't caught during the configure phase. It seems that removing -Wno-unused-command-line-argument would require the following change in meson.build, but I'm open to better approaches.

Quoted 10 lines
(quoted diff) +1 -1
-if not get_option('b_pie')
+if not get_option('b_pie') and host_os != 'emscripten'
   qemu_common_flags += cc.get_supported_arguments('-fno-pie', '-no-pie')
 endif
Quoted 3 lines

meson.build ucontext_probe = ''' · line 520

Quoted 3 lines
meson.build +2 -0
+elif host_os == 'emscripten'
+  supported_backends += ['fiber']

Can you rename the backend to 'wasm' since the 'windows' backend also uses an API called Fibers?

Sure, I'll rename the coroutine backend in the next version of the series.

#15 lore raw

Re: [PATCH 05/10] meson: Add wasm build in build scripts

Quoted 20 lines · Kohei Tokunaga
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

has_int128_type is set to false on emscripten as of now to avoid errors by libffi.

What is the error here? How hard would it be to test for it?

When has_int128_type=true, I encountered a runtime error from libffi. To reproduce this, we need to actually execute a libffi call with 128-bit arguments.

Quoted 11 lines

Uncaught TypeError: Cannot convert 1079505232 to a BigInt

    at ffi_call_js (out.js:702:37)
    at qemu-system-x86_64.wasm.ffi_call (qemu-system-x86_64.wasm:0xa37712)
    at qemu-system-x86_64.wasm.tcg_qemu_tb_exec_tci (qemu-system-x86_64.wasm:0x65f440)
    at qemu-system-x86_64.wasm.tcg_qemu_tb_exec (qemu-system-x86_64.wasm:0x65edff)
    at qemu-system-x86_64.wasm.cpu_tb_exec (qemu-system-x86_64.wasm:0x6762c0)
    at qemu-system-x86_64.wasm.cpu_exec_loop (qemu-system-x86_64.wasm:0x677c84)
    at qemu-system-x86_64.wasm.dynCall_iii (qemu-system-x86_64.wasm:0xab9014)
    at ret.<computed> (out.js:6016:24)
    at invoke_iii (out.js:7574:10)
    at qemu-system-x86_64.wasm.cpu_exec_setjmp (qemu-system-x86_64.wasm:0x676db8)

Ok, I guess a comment mentioning that it's a libffi limitation is enough.

Quoted 10 lines
Quoted 1 line

At least -g -O3 -pthread should not be necessary.

Thank you for the suggestion. -sPROXY_TO_PTHREAD flag used in c_link_args always requires -pthread, even during configuration. Otherwise, emcc returns an error like:

Quoted 1 line

emcc: error: -sPROXY_TO_PTHREAD requires -pthread to work!

So I think -pthread needs to be included in c_link_args at minimum. I'll try to remove other flags in the next version of the series.

Reading more about -sPROXY_TO_PTHREAD it seems that you need it for all calls to emcc, even when compiling, so it's better to leave it in everywhere.

Quoted 12 lines
Quoted 2 lines

For -Wno-unused-command-line-argument what are the warnings/errors that you are getting?

I encountered the following error when compiling QEMU:

Quoted 1 line

clang: error: argument unused during compilation: '-no-pie' [-Werror,-Wunused-command-line-argument]

It seems Emscripten doesn't support the -no-pie flag, and this wasn't caught during the configure phase. It seems that removing -Wno-unused-command-line-argument would require the following change in meson.build, but I'm open to better approaches.

Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +1 -1
-if not get_option('b_pie')
+if not get_option('b_pie') and host_os != 'emscripten'
   qemu_common_flags += cc.get_supported_arguments('-fno-pie', '-no-pie')
 endif

Meson should have passed the -Werror=unused-command-line-argument flag when doing the above test (CLikeCompiler._has_multi_arguments -> has_arguments -> Compiler.compiles -> _build_wrapper -> build_wrapper_args -> ClangCompiler.get_compiler_check_args). It would be great if you can check what's wrong in this theory so perhaps meson can be fixed, or at least send here a meson-log.txt.

My suggestion is (if possible) to split out the parts of this series that are enough to run QEMU under TCI, and get those in as quickly as possible. The TCG backend can come second.

Thanks,

Paolo

#16 lore raw

Re: [PATCH 05/10] meson: Add wasm build in build scripts

Hi Paolo,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

has_int128_type is set to false on emscripten as of now to avoid

errors by

Quoted 11 lines
Quoted 11 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

libffi.

What is the error here? How hard would it be to test for it?

When has_int128_type=true, I encountered a runtime error from libffi. To reproduce this, we need to actually execute a libffi call with 128-bit arguments.

Quoted 3 lines

Uncaught TypeError: Cannot convert 1079505232 to a BigInt

    at ffi_call_js (out.js:702:37)
    at qemu-system-x86_64.wasm.ffi_call

(qemu-system-x86_64.wasm:0xa37712)

Quoted 1 line
Quoted 1 line
Quoted 1 line
    at qemu-system-x86_64.wasm.tcg_qemu_tb_exec_tci

(qemu-system-x86_64.wasm:0x65f440)

Quoted 1 line
Quoted 1 line
Quoted 1 line
    at qemu-system-x86_64.wasm.tcg_qemu_tb_exec

(qemu-system-x86_64.wasm:0x65edff)

Quoted 1 line
Quoted 1 line
Quoted 1 line
    at qemu-system-x86_64.wasm.cpu_tb_exec

(qemu-system-x86_64.wasm:0x6762c0)

Quoted 1 line
Quoted 1 line
Quoted 1 line
    at qemu-system-x86_64.wasm.cpu_exec_loop

(qemu-system-x86_64.wasm:0x677c84)

Quoted 1 line
Quoted 1 line
Quoted 1 line
    at qemu-system-x86_64.wasm.dynCall_iii

(qemu-system-x86_64.wasm:0xab9014)

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
    at ret.<computed> (out.js:6016:24)
    at invoke_iii (out.js:7574:10)
    at qemu-system-x86_64.wasm.cpu_exec_setjmp

(qemu-system-x86_64.wasm:0x676db8)

Quoted 6 lines

Ok, I guess a comment mentioning that it's a libffi limitation is enough.

Quoted 3 lines
Quoted 1 line

At least -g -O3 -pthread should not be necessary.

Thank you for the suggestion. -sPROXY_TO_PTHREAD flag used in

c_link_args

Quoted 1 line
Quoted 1 line

always requires -pthread, even during configuration. Otherwise, emcc

returns

Quoted 5 lines
Quoted 5 lines

an error like:

Quoted 1 line

emcc: error: -sPROXY_TO_PTHREAD requires -pthread to work!

So I think -pthread needs to be included in c_link_args at minimum.

I'll try

Quoted 7 lines
Quoted 1 line

to remove other flags in the next version of the series.

Reading more about -sPROXY_TO_PTHREAD it seems that you need it for all calls to emcc, even when compiling, so it's better to leave it in everywhere.

Quoted 1 line
Quoted 1 line

For -Wno-unused-command-line-argument what are the warnings/errors

that

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

you are getting?

I encountered the following error when compiling QEMU:

Quoted 1 line

clang: error: argument unused during compilation: '-no-pie'

[-Werror,-Wunused-command-line-argument]

Quoted 2 lines
Quoted 2 lines

It seems Emscripten doesn't support the -no-pie flag, and this wasn't

caught

Quoted 7 lines
Quoted 7 lines

during the configure phase. It seems that removing -Wno-unused-command-line-argument would require the following change in meson.build, but I'm open to better approaches.

Review

Quoted 3 lines
(quoted diff) +1 -1
-if not get_option('b_pie')
+if not get_option('b_pie') and host_os != 'emscripten'
   qemu_common_flags += cc.get_supported_arguments('-fno-pie',

'-no-pie')

Quoted 8 lines
Quoted 1 line
Quoted 1 line

endif

Meson should have passed the -Werror=unused-command-line-argument flag when doing the above test (CLikeCompiler._has_multi_arguments -> has_arguments -> Compiler.compiles -> _build_wrapper -> build_wrapper_args -> ClangCompiler.get_compiler_check_args). It would be great if you can check what's wrong in this theory so perhaps meson can be fixed, or at least send here a meson-log.txt.

According to meson-log.txt as shown below, -Werror=unused-command-line-argument was passed to the compiler, but it didn't catch the warning.

Quoted 7 lines

Working directory: /build/meson-private/tmp4q_5wl_9 Code:
extern int i;
int i;

-----------
Command line: `/emsdk/upstream/emscripten/emcc -m32

/build/meson-private/tmp4q_5wl_9/testfile.c -o /build/meson-private/tmp4q_5wl_9/output.o -c -D_FILE_OFFSET_BITS=64 -O0 -Werror=implicit-function-declaration -Werror=unknown-warning-option -Werror=unused-command-line-argument -fno-pie` -> 0

Quoted 9 lines

Compiler for C supports arguments -fno-pie: YES Running compile:
Working directory: /build/meson-private/tmpl9yy_8gs Code:
extern int i;
int i;

-----------
Command line: `/emsdk/upstream/emscripten/emcc -m32

/build/meson-private/tmpl9yy_8gs/testfile.c -o /build/meson-private/tmpl9yy_8gs/output.o -c -D_FILE_OFFSET_BITS=64 -O0 -Werror=implicit-function-declaration -Werror=unknown-warning-option -Werror=unused-command-line-argument -no-pie` -> 0

Quoted 2 lines

stderr:
clang: warning: argument unused during compilation: '-no-pie'

[-Wunused-command-line-argument]

Quoted 2 lines

-----------
Compiler for C supports arguments -no-pie: YES

It seems there's a related issue thread on the Meson repository [1].

[1] https://github.com/mesonbuild/meson/issues/5355

Quoted 3 lines

My suggestion is (if possible) to split out the parts of this series that are enough to run QEMU under TCI, and get those in as quickly as possible. The TCG backend can come second.

Sure, I'll try to split this patch series.

#17 lore raw

PATCH 6/10 include/exec: Allow using 64bit guest addresses on emscripten

In reply to Kohei Tokunaga

To enable 64-bit guest support in Wasm 32bit memory model today, it was necessary to partially revert recent changes that removed support for different pointer widths between the host and guest (e.g., commits a70af12addd9060fdf8f3dbd42b42e3072c3914f and bf455ec50b6fea15b4d2493059365bf94c706273) when compiling with Emscripten. While this serves as a temporary workaround, a long-term solution could involve adopting Wasm's 64-bit memory model once it gains broader support, as it is currently not widely adopted (e.g., unsupported by Safari and libffi).

---
 accel/tcg/cputlb.c        |  8 ++++----
 include/exec/tlb-common.h | 14 ++++++++++----
 include/exec/vaddr.h      | 11 +++++++++++
 include/qemu/atomic.h     |  4 ++++
 meson.build               |  8 +++++---
 5 files changed, 34 insertions(+), 11 deletions(-)
accel/tcg/cputlb.c +4 -4
index fb22048876..8f8f5c19c4 100644
@@ -104,13 +104,13 @@ static inline uint64_t tlb_read_idx(const CPUTLBEntry *entry,
 {
     /* Do not rearrange the CPUTLBEntry structure members. */
     QEMU_BUILD_BUG_ON(offsetof(CPUTLBEntry, addr_read) !=
-                      MMU_DATA_LOAD * sizeof(uintptr_t));
+                      MMU_DATA_LOAD * sizeof(tlb_addr));
     QEMU_BUILD_BUG_ON(offsetof(CPUTLBEntry, addr_write) !=
-                      MMU_DATA_STORE * sizeof(uintptr_t));
+                      MMU_DATA_STORE * sizeof(tlb_addr));
     QEMU_BUILD_BUG_ON(offsetof(CPUTLBEntry, addr_code) !=
-                      MMU_INST_FETCH * sizeof(uintptr_t));
+                      MMU_INST_FETCH * sizeof(tlb_addr));
 
-    const uintptr_t *ptr = &entry->addr_idx[access_type];
+    const tlb_addr *ptr = &entry->addr_idx[access_type];
     /* ofs might correspond to .addr_write, so use qatomic_read */
     return qatomic_read(ptr);
 }
include/exec/tlb-common.h +10 -4
index 03b5a8ffc7..679054bb44 100644
@@ -19,14 +19,20 @@
 #ifndef EXEC_TLB_COMMON_H
 #define EXEC_TLB_COMMON_H 1
 
+#ifndef EMSCRIPTEN
 #define CPU_TLB_ENTRY_BITS (HOST_LONG_BITS == 32 ? 4 : 5)
+typedef uintptr_t tlb_addr;
+#else
+#define CPU_TLB_ENTRY_BITS 5
+typedef uint64_t tlb_addr;
+#endif
 
 /* Minimalized TLB entry for use by TCG fast path. */
 typedef union CPUTLBEntry {
     struct {
-        uintptr_t addr_read;
-        uintptr_t addr_write;
-        uintptr_t addr_code;
+        tlb_addr addr_read;
+        tlb_addr addr_write;
+        tlb_addr addr_code;
         /*
          * Addend to virtual address to get host address.  IO accesses
          * use the corresponding iotlb value.
@@ -37,7 +43,7 @@ typedef union CPUTLBEntry {
      * Padding to get a power of two size, as well as index
      * access to addr_{read,write,code}.
      */
-    uintptr_t addr_idx[(1 << CPU_TLB_ENTRY_BITS) / sizeof(uintptr_t)];
+    tlb_addr addr_idx[(1 << CPU_TLB_ENTRY_BITS) / sizeof(tlb_addr)];
 } CPUTLBEntry;
 
 QEMU_BUILD_BUG_ON(sizeof(CPUTLBEntry) != (1 << CPU_TLB_ENTRY_BITS));
include/exec/vaddr.h +11 -0
index 28bec632fb..ff57f944dd 100644
@@ -9,6 +9,7 @@
  * We do not support 64-bit guest on 32-host and detect at configure time.
  * Therefore, a host pointer width will always fit a guest pointer.
  */
+#ifndef EMSCRIPTEN
 typedef uintptr_t vaddr;
 #define VADDR_PRId PRIdPTR
 #define VADDR_PRIu PRIuPTR
@@ -16,5 +17,15 @@ typedef uintptr_t vaddr;
 #define VADDR_PRIx PRIxPTR
 #define VADDR_PRIX PRIXPTR
 #define VADDR_MAX UINTPTR_MAX
+#else
+/* Explicitly define this as 64bit on emscripten */
+typedef uint64_t vaddr;
+#define VADDR_PRId PRId64
+#define VADDR_PRIu PRIu64
+#define VADDR_PRIo PRIo64
+#define VADDR_PRIx PRIx64
+#define VADDR_PRIX PRIX64
+#define VADDR_MAX UINT64_MAX
+#endif
 
 #endif
include/qemu/atomic.h +4 -0
index f80cba24cf..76a8fbcd8c 100644
@@ -56,6 +56,7 @@
  */
 #define signal_barrier()    __atomic_signal_fence(__ATOMIC_SEQ_CST)
 
+#ifndef EMSCRIPTEN
 /*
  * Sanity check that the size of an atomic operation isn't "overly large".
  * Despite the fact that e.g. i686 has 64-bit atomic operations, we do not
@@ -63,6 +64,9 @@
  * bit of sanity checking that other 32-bit hosts might build.
  */
 #define ATOMIC_REG_SIZE  sizeof(void *)
+#else
+#define ATOMIC_REG_SIZE  8 /* wasm supports 64bit atomics */
+#endif
 
 /* Weak atomic operations prevent the compiler moving other
  * loads/stores past the atomic operation load/store. However there is
meson.build +5 -3
index bcf1e33ddf..343408636b 100644
@@ -3304,9 +3304,11 @@ foreach target : target_dirs
 
   target_kconfig = []
   foreach sym: accelerators
-    # Disallow 64-bit on 32-bit emulation and virtualization
-    if host_long_bits < config_target['TARGET_LONG_BITS'].to_int()
-      continue
+    if host_arch != 'wasm32'
+      # Disallow 64-bit on 32-bit emulation and virtualization
+      if host_long_bits < config_target['TARGET_LONG_BITS'].to_int()
+        continue
+      endif
     endif
     if sym == 'CONFIG_TCG' or target in accelerator_targets.get(sym, [])
       config_target += { sym: 'y' }
Signature · 1 line
2.25.1
#18 lore raw

PATCH 7/10 tcg: Add a TCG backend for WebAssembly

In reply to Kohei Tokunaga

A TB consists of a wasmTBHeader followed by the data listed below. The wasmTBHeader contains pointers for each element:

- TCI code
- Wasm code
- Array of function indices imported into the Wasm instance
- Counter tracking the number of TB executions
- Pointer to the Wasm instance information

The Wasm backend (tcg/wasm32.c) and Wasm instances running on the same thread share information, such as CPUArchState, through a wasmContext structure. The Wasm backend defines tcg_qemu_tb_exec as a common entry point for TBs, similar to the TCI backend. tcg_qemu_tb_exec runs TBs on a forked TCI interpreter by default, while compiles and executes frequently executed TBs as Wasm.

The code generator (tcg/wasm32) receives TCG IR and generates both Wasm and TCI instructions. Since Wasm cannot directly jump to specific addresses, labels are implemented using Wasm control flow instructions. As shown in the pseudo-code below, a TB wraps instructions in a large loop, where codes are placed within if blocks separated by labels. Branching is handled by breaking from the current block and entering the target block.

loop
if
... code after label1
end
if
... code after label2
end
...
end

Additionally, the Wasm backend differs from other backends in several ways:

- goto_tb and goto_ptr return control to tcg_qemu_tb_exec which runs the target TB
- Helper function pointers are stored in an array in TB and imported into the Wasm instance on execution
- Wasm TBs lack prologue and epilogue. TBs are executed via tcg_qemu_tb_exec

Browsers cause out of memory error if too many Wasm instances are created. To prevent this, the Wasm backend tracks active instances using an array. When instantiating a new instance risks exceeding the limit, the backend removes older instances to avoid browser errors. These removed instances are re-instantiated when needed.

---
 include/accel/tcg/getpc.h        |    2 +-
 include/tcg/helper-info.h        |    4 +-
 include/tcg/tcg.h                |    2 +-
 meson.build                      |    2 +
 tcg/meson.build                  |    5 +
 tcg/tcg.c                        |   26 +-
 tcg/wasm32.c                     | 1260 +++++++++
 tcg/wasm32.h                     |   39 +
 tcg/wasm32/tcg-target-con-set.h  |   18 +
 tcg/wasm32/tcg-target-con-str.h  |    8 +
 tcg/wasm32/tcg-target-has.h      |  102 +
 tcg/wasm32/tcg-target-mo.h       |   12 +
 tcg/wasm32/tcg-target-opc.h.inc  |    4 +
 tcg/wasm32/tcg-target-reg-bits.h |   12 +
 tcg/wasm32/tcg-target.c.inc      | 4484 ++++++++++++++++++++++++++++++
 tcg/wasm32/tcg-target.h          |   65 +
 16 files changed, 6035 insertions(+), 10 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
include/accel/tcg/getpc.h +1 -1
index 8a97ce34e7..78acb4a3cf 100644
@@ -13,7 +13,7 @@
 #endif
 
 /* GETPC is the true target of the return instruction that we'll execute.  */
-#ifdef CONFIG_TCG_INTERPRETER
+#if defined(CONFIG_TCG_INTERPRETER) || defined(EMSCRIPTEN)
 extern __thread uintptr_t tci_tb_ptr;
 # define GETPC() tci_tb_ptr
 #else
include/tcg/helper-info.h +2 -2
index 909fe73afa..9b4e8832a8 100644
@@ -9,7 +9,7 @@
 #ifndef TCG_HELPER_INFO_H
 #define TCG_HELPER_INFO_H
 
-#ifdef CONFIG_TCG_INTERPRETER
+#if defined(CONFIG_TCG_INTERPRETER) || defined(EMSCRIPTEN)
 #include <ffi.h>
 #endif
 #include "tcg-target-reg-bits.h"
@@ -48,7 +48,7 @@ struct TCGHelperInfo {
     const char *name;
 
     /* Used with g_once_init_enter. */
-#ifdef CONFIG_TCG_INTERPRETER
+#if defined(CONFIG_TCG_INTERPRETER) || defined(EMSCRIPTEN)
     ffi_cif *cif;
 #else
     uintptr_t init;
include/tcg/tcg.h +1 -1
index 84d99508b6..c9ab6c838a 100644
@@ -940,7 +940,7 @@ static inline size_t tcg_current_code_size(TCGContext *s)
 #define TB_EXIT_IDXMAX    1
 #define TB_EXIT_REQUESTED 3
 
-#ifdef CONFIG_TCG_INTERPRETER
+#if defined(CONFIG_TCG_INTERPRETER) || defined(EMSCRIPTEN)
 uintptr_t tcg_qemu_tb_exec(CPUArchState *env, const void *tb_ptr);
 #else
 typedef uintptr_t tcg_prologue_fn(CPUArchState *env, const void *tb_ptr);
meson.build +2 -0
index 343408636b..ab84820bc5 100644
@@ -920,6 +920,8 @@ if get_option('tcg').allowed()
     tcg_arch = 'i386'
   elif host_arch == 'ppc64'
     tcg_arch = 'ppc'
+  elif host_arch == 'wasm32'
+    tcg_arch = 'wasm32'
   endif
   add_project_arguments('-iquote', meson.current_source_dir() / 'tcg' / tcg_arch,
                         language: all_languages)
tcg/meson.build +5 -0
index 69ebb4908a..f1a1f9485d 100644
@@ -20,6 +20,11 @@ if get_option('tcg_interpreter')
                       method: 'pkg-config')
   tcg_ss.add(libffi)
   tcg_ss.add(files('tci.c'))
+elif host_os == 'emscripten'
+  libffi = dependency('libffi', version: '>=3.0', required: true,
+                      method: 'pkg-config')
+  specific_ss.add(libffi)
+  specific_ss.add(files('wasm32.c'))
 endif
 
 tcg_ss.add(when: libdw, if_true: files('debuginfo.c'))
tcg/tcg.c +20 -6
index dfd48b8264..154a4dafa7 100644
@@ -136,6 +136,10 @@ static void tcg_out_goto_tb(TCGContext *s, int which);
 static void tcg_out_op(TCGContext *s, TCGOpcode opc, TCGType type,
                        const TCGArg args[TCG_MAX_OP_ARGS],
                        const int const_args[TCG_MAX_OP_ARGS]);
+#if defined(EMSCRIPTEN)
+static void tcg_out_label_cb(TCGContext *s, TCGLabel *l);
+static int tcg_out_tb_end(TCGContext *s);
+#endif
 #if TCG_TARGET_MAYBE_vec
 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
                             TCGReg dst, TCGReg src);
@@ -251,7 +255,7 @@ TCGv_env tcg_env;
 const void *tcg_code_gen_epilogue;
 uintptr_t tcg_splitwx_diff;
 
-#ifndef CONFIG_TCG_INTERPRETER
+#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN)
 tcg_prologue_fn *tcg_qemu_tb_exec;
 #endif
 
@@ -358,6 +362,9 @@ static void tcg_out_label(TCGContext *s, TCGLabel *l)
     tcg_debug_assert(!l->has_value);
     l->has_value = 1;
     l->u.value_ptr = tcg_splitwx_to_rx(s->code_ptr);
+#if defined(EMSCRIPTEN)
+    tcg_out_label_cb(s, l);
+#endif
 }
 
 TCGLabel *gen_new_label(void)
@@ -1139,7 +1146,7 @@ static TCGHelperInfo info_helper_st128_mmu = {
               | dh_typemask(ptr, 5)  /* uintptr_t ra */
 };
 
-#ifdef CONFIG_TCG_INTERPRETER
+#if defined(CONFIG_TCG_INTERPRETER) || defined(EMSCRIPTEN)
 static ffi_type *typecode_to_ffi(int argmask)
 {
     /*
@@ -1593,7 +1600,7 @@ void tcg_prologue_init(void)
     s->code_buf = s->code_gen_ptr;
     s->data_gen_ptr = NULL;
 
-#ifndef CONFIG_TCG_INTERPRETER
+#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN)
     tcg_qemu_tb_exec = (tcg_prologue_fn *)tcg_splitwx_to_rx(s->code_ptr);
 #endif
 
@@ -1649,11 +1656,11 @@ void tcg_prologue_init(void)
         }
     }
 
-#ifndef CONFIG_TCG_INTERPRETER
+#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN)
     /*
      * Assert that goto_ptr is implemented completely, setting an epilogue.
-     * For tci, we use NULL as the signal to return from the interpreter,
-     * so skip this check.
+     * For tci and wasm backend, we use NULL as the signal to return from the
+     * interpreter, so skip this check.
      */
     tcg_debug_assert(tcg_code_gen_epilogue != NULL);
 #endif
@@ -6505,6 +6512,13 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start)
                         tcg_ptr_byte_diff(s->code_ptr, s->code_buf));
 #endif
 
+#if defined(EMSCRIPTEN)
+    i = tcg_out_tb_end(s);
+    if (i < 0) {
+        return i;
+    }
+#endif
+
     return tcg_current_code_size(s);
 }
 
tcg/wasm32.c new file +1260 -0

1,260 lines not on this page · raw message

tcg/wasm32.h new file +39 -0
new file mode 100644
index 0000000000..cbeb281a7d
@@ -0,0 +1,39 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef TCG_WASM32_H
+#define TCG_WASM32_H
+
+struct wasmContext {
+    CPUArchState *env;
+    uint64_t *stack;
+    void *tb_ptr;
+    void *tci_tb_ptr;
+    uint32_t do_init;
+    void *stack128;
+};
+
+#define ENV_OFF 0
+#define STACK_OFF 4
+#define TB_PTR_OFF 8
+#define HELPER_RET_TB_PTR_OFF 12
+#define DO_INIT_OFF 16
+#define STACK128_OFF 20
+
+int get_core_nums(void);
+
+/*
+ * TB of wasm backend starts from a header which stores pointers for each data
+ * stored in the following region of the TB.
+ */
+struct wasmTBHeader {
+    void *tci_ptr;
+    void *wasm_ptr;
+    int wasm_size;
+    void *import_ptr;
+    int import_size;
+    void *counter_ptr;
+    void *info_ptr;
+};
+
+#endif
tcg/wasm32/tcg-target-con-set.h new file +18 -0
new file mode 100644
index 0000000000..093c8e8c3b
@@ -0,0 +1,18 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+/*
+ * C_On_Im(...) defines a constraint set with <n> outputs and <m> inputs.
+ * Each operand should be a sequence of constraint letters as defined by
+ * tcg-target-con-str.h; the constraint combination is inclusive or.
+ */
+C_O0_I1(r)
+C_O0_I2(r, r)
+C_O0_I3(r, r, r)
+C_O0_I4(r, r, r, r)
+C_O1_I1(r, r)
+C_O1_I2(r, r, r)
+C_O1_I4(r, r, r, r, r)
+C_O2_I1(r, r, r)
+C_O2_I2(r, r, r, r)
+C_O2_I4(r, r, r, r, r, r)
tcg/wasm32/tcg-target-con-str.h new file +8 -0
new file mode 100644
index 0000000000..f17f2e850f
@@ -0,0 +1,8 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+/*
+ * Define constraint letters for register sets:
+ * REGS(letter, register_mask)
+ */
+REGS('r', MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS))
tcg/wasm32/tcg-target-has.h new file +102 -0

102 lines not on this page · raw message

tcg/wasm32/tcg-target-mo.h new file +12 -0
new file mode 100644
index 0000000000..0865185c9a
@@ -0,0 +1,12 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+/*
+ * Define target-specific memory model
+ */
+#ifndef TCG_TARGET_MO_H
+#define TCG_TARGET_MO_H
+
+#define TCG_TARGET_DEFAULT_MO  0
+
+#endif
tcg/wasm32/tcg-target-opc.h.inc new file +4 -0
new file mode 100644
index 0000000000..ecc8c4e55e
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: MIT */
+/* These opcodes for use between the tci generator and interpreter. */
+DEF(tci_movi, 1, 0, 1, TCG_OPF_NOT_PRESENT)
+DEF(tci_movl, 1, 0, 1, TCG_OPF_NOT_PRESENT)
tcg/wasm32/tcg-target-reg-bits.h new file +12 -0
new file mode 100644
index 0000000000..4f60ae9166
@@ -0,0 +1,12 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+/*
+ * Define target-specific register size
+ */
+#ifndef TCG_TARGET_REG_BITS_H
+#define TCG_TARGET_REG_BITS_H
+
+#define TCG_TARGET_REG_BITS  64
+
+#endif
tcg/wasm32/tcg-target.c.inc new file +4484 -0

1,424 lines not on this page · raw message

tcg/wasm32/tcg-target.h new file +65 -0

65 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.25.1

Part of the diff is not shown here. View the raw message.

#19 lore raw

Re: [PATCH 07/10] tcg: Add a TCG backend for WebAssembly

Quoted 58 lines · Kohei Tokunaga

A TB consists of a wasmTBHeader followed by the data listed below. The wasmTBHeader contains pointers for each element:

- TCI code
- Wasm code
- Array of function indices imported into the Wasm instance
- Counter tracking the number of TB executions
- Pointer to the Wasm instance information

The Wasm backend (tcg/wasm32.c) and Wasm instances running on the same thread share information, such as CPUArchState, through a wasmContext structure. The Wasm backend defines tcg_qemu_tb_exec as a common entry point for TBs, similar to the TCI backend. tcg_qemu_tb_exec runs TBs on a forked TCI interpreter by default, while compiles and executes frequently executed TBs as Wasm.

The code generator (tcg/wasm32) receives TCG IR and generates both Wasm and TCI instructions. Since Wasm cannot directly jump to specific addresses, labels are implemented using Wasm control flow instructions. As shown in the pseudo-code below, a TB wraps instructions in a large loop, where codes are placed within if blocks separated by labels. Branching is handled by breaking from the current block and entering the target block.

loop
if
... code after label1
end
if
... code after label2
end
...
end

Additionally, the Wasm backend differs from other backends in several ways:

- goto_tb and goto_ptr return control to tcg_qemu_tb_exec which runs the target TB
- Helper function pointers are stored in an array in TB and imported into the Wasm instance on execution
- Wasm TBs lack prologue and epilogue. TBs are executed via tcg_qemu_tb_exec

Browsers cause out of memory error if too many Wasm instances are created. To prevent this, the Wasm backend tracks active instances using an array. When instantiating a new instance risks exceeding the limit, the backend removes older instances to avoid browser errors. These removed instances are re-instantiated when needed.

---
  include/accel/tcg/getpc.h        |    2 +-
  include/tcg/helper-info.h        |    4 +-
  include/tcg/tcg.h                |    2 +-
  meson.build                      |    2 +
  tcg/meson.build                  |    5 +
  tcg/tcg.c                        |   26 +-
  tcg/wasm32.c                     | 1260 +++++++++
  tcg/wasm32.h                     |   39 +
  tcg/wasm32/tcg-target-con-set.h  |   18 +
Quoted 8 lines
  tcg/wasm32/tcg-target-con-str.h  |    8 +
  tcg/wasm32/tcg-target-has.h      |  102 +
  tcg/wasm32/tcg-target-mo.h       |   12 +
  tcg/wasm32/tcg-target-opc.h.inc  |    4 +
  tcg/wasm32/tcg-target-reg-bits.h |   12 +
  tcg/wasm32/tcg-target.c.inc      | 4484 ++++++++++++++++++++++++++++++
  tcg/wasm32/tcg-target.h          |   65 +
  16 files changed, 6035 insertions(+), 10 deletions(-)

Eh TBH this is too much to review as a single patch.

Do you already have an idea how different the wasm64 implementation can be?

#20 lore raw

Re: [PATCH 07/10] tcg: Add a TCG backend for WebAssembly

Quoted 3 lines

Eh TBH this is too much to review as a single patch.

Do you already have an idea how different the wasm64 implementation can

be?

Sorry for the large patch. I'll split it into smaller patches in the next version of the series.

With wasm64, instructions that manipulate pointers (e.g., ld/st, qemu_ld/st) will be updated to handle 64bit pointers. I believe wasm64 will not require large changes to other parts, such as label handling, goto_tb/goto_ptr, or the forked TCI.

#21 lore raw

Re: [PATCH 07/10] tcg: Add a TCG backend for WebAssembly

In reply to Kohei Tokunaga

Hi Kohei,

Quoted 83 lines · Kohei Tokunaga

A TB consists of a wasmTBHeader followed by the data listed below. The wasmTBHeader contains pointers for each element:

- TCI code
- Wasm code
- Array of function indices imported into the Wasm instance
- Counter tracking the number of TB executions
- Pointer to the Wasm instance information

The Wasm backend (tcg/wasm32.c) and Wasm instances running on the same thread share information, such as CPUArchState, through a wasmContext structure. The Wasm backend defines tcg_qemu_tb_exec as a common entry point for TBs, similar to the TCI backend. tcg_qemu_tb_exec runs TBs on a forked TCI interpreter by default, while compiles and executes frequently executed TBs as Wasm.

The code generator (tcg/wasm32) receives TCG IR and generates both Wasm and TCI instructions. Since Wasm cannot directly jump to specific addresses, labels are implemented using Wasm control flow instructions. As shown in the pseudo-code below, a TB wraps instructions in a large loop, where codes are placed within if blocks separated by labels. Branching is handled by breaking from the current block and entering the target block.

loop
if
... code after label1
end
if
... code after label2
end
...
end

Additionally, the Wasm backend differs from other backends in several ways:

- goto_tb and goto_ptr return control to tcg_qemu_tb_exec which runs the target TB
- Helper function pointers are stored in an array in TB and imported into the Wasm instance on execution
- Wasm TBs lack prologue and epilogue. TBs are executed via tcg_qemu_tb_exec

Browsers cause out of memory error if too many Wasm instances are created. To prevent this, the Wasm backend tracks active instances using an array. When instantiating a new instance risks exceeding the limit, the backend removes older instances to avoid browser errors. These removed instances are re-instantiated when needed.

---
  include/accel/tcg/getpc.h        |    2 +-
  include/tcg/helper-info.h        |    4 +-
  include/tcg/tcg.h                |    2 +-
  meson.build                      |    2 +
  tcg/meson.build                  |    5 +
  tcg/tcg.c                        |   26 +-
  tcg/wasm32.c                     | 1260 +++++++++
  tcg/wasm32.h                     |   39 +
  tcg/wasm32/tcg-target-con-set.h  |   18 +
  tcg/wasm32/tcg-target-con-str.h  |    8 +
  tcg/wasm32/tcg-target-has.h      |  102 +
  tcg/wasm32/tcg-target-mo.h       |   12 +
  tcg/wasm32/tcg-target-opc.h.inc  |    4 +
  tcg/wasm32/tcg-target-reg-bits.h |   12 +
  tcg/wasm32/tcg-target.c.inc      | 4484 ++++++++++++++++++++++++++++++
  tcg/wasm32/tcg-target.h          |   65 +
  16 files changed, 6035 insertions(+), 10 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

tcg/tcg.c +0 -0
index dfd48b8264..154a4dafa7 100644

tcg/tcg.c static void tcg_out_goto_tb(TCGContext *s, int which); · line 139

Quoted 4 lines
tcg/tcg.c +1 -0
@@ -136,6 +136,10 @@ static void tcg_out_goto_tb(TCGContext *s, int which);
  static void tcg_out_op(TCGContext *s, TCGOpcode opc, TCGType type,
                         const TCGArg args[TCG_MAX_OP_ARGS],
                         const int const_args[TCG_MAX_OP_ARGS]);
+#if defined(EMSCRIPTEN)

Maybe we can let this independently of EMSCRIPTEN, to reduce #ifdef'ry.

tcg/tcg.c TCGv_env tcg_env; · line 258

Quoted 12 lines
tcg/tcg.c +4 -1
+static void tcg_out_label_cb(TCGContext *s, TCGLabel *l);
+static int tcg_out_tb_end(TCGContext *s);
+#endif
  #if TCG_TARGET_MAYBE_vec
  static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
                              TCGReg dst, TCGReg src);
@@ -251,7 +255,7 @@ TCGv_env tcg_env;
  const void *tcg_code_gen_epilogue;
  uintptr_t tcg_splitwx_diff;
 
-#ifndef CONFIG_TCG_INTERPRETER
+#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN)

s/&&/||/ otherwise breaks TCI? (various cases)

Quoted 49 lines
tcg/tcg.c +8 -5
  tcg_prologue_fn *tcg_qemu_tb_exec;
  #endif
 
@@ -358,6 +362,9 @@ static void tcg_out_label(TCGContext *s, TCGLabel *l)
      tcg_debug_assert(!l->has_value);
      l->has_value = 1;
      l->u.value_ptr = tcg_splitwx_to_rx(s->code_ptr);
+#if defined(EMSCRIPTEN)
+    tcg_out_label_cb(s, l);
+#endif
  }
 
  TCGLabel *gen_new_label(void)
@@ -1139,7 +1146,7 @@ static TCGHelperInfo info_helper_st128_mmu = {
                | dh_typemask(ptr, 5)  /* uintptr_t ra */
  };
 
-#ifdef CONFIG_TCG_INTERPRETER
+#if defined(CONFIG_TCG_INTERPRETER) || defined(EMSCRIPTEN)
  static ffi_type *typecode_to_ffi(int argmask)
  {
      /*
@@ -1593,7 +1600,7 @@ void tcg_prologue_init(void)
      s->code_buf = s->code_gen_ptr;
      s->data_gen_ptr = NULL;
 
-#ifndef CONFIG_TCG_INTERPRETER
+#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN)
      tcg_qemu_tb_exec = (tcg_prologue_fn *)tcg_splitwx_to_rx(s->code_ptr);
  #endif
 
@@ -1649,11 +1656,11 @@ void tcg_prologue_init(void)
          }
      }
 
-#ifndef CONFIG_TCG_INTERPRETER
+#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN)
      /*
       * Assert that goto_ptr is implemented completely, setting an epilogue.
-     * For tci, we use NULL as the signal to return from the interpreter,
-     * so skip this check.
+     * For tci and wasm backend, we use NULL as the signal to return from the
+     * interpreter, so skip this check.
       */
      tcg_debug_assert(tcg_code_gen_epilogue != NULL);
  #endif
@@ -6505,6 +6512,13 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start)
                          tcg_ptr_byte_diff(s->code_ptr, s->code_buf));
  #endif

tcg/tcg.c int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start) · line 6523

Quoted 10 lines
tcg/tcg.c +7 -0
 
+#if defined(EMSCRIPTEN)
+    i = tcg_out_tb_end(s);
+    if (i < 0) {
+        return i;
+    }
+#endif
+
      return tcg_current_code_size(s);
  }

Out of curiosity, have you tried to run a big-endian guest?

#22 lore raw

Re: [PATCH 07/10] tcg: Add a TCG backend for WebAssembly

Hi Philippe, thank you for the feedback.

Quoted 5 lines

tcg/tcg.c static void tcg_out_goto_tb(TCGContext *s, int

Quoted 5 lines
tcg/tcg.c +0 -0
index dfd48b8264..154a4dafa7 100644
@@ -136,6 +136,10 @@ static void tcg_out_goto_tb(TCGContext *s, int

which);

Quoted 6 lines

tcg/tcg.c static void tcg_out_goto_tb(TCGContext *s, int which); · line 139

Quoted 4 lines
tcg/tcg.c +1 -0
  static void tcg_out_op(TCGContext *s, TCGOpcode opc, TCGType type,
                         const TCGArg args[TCG_MAX_OP_ARGS],
                         const int const_args[TCG_MAX_OP_ARGS]);
+#if defined(EMSCRIPTEN)

Maybe we can let this independently of EMSCRIPTEN, to reduce #ifdef'ry.

Sure, I'll make this independent of EMSCRIPTEN in the next version of the series.

Quoted 5 lines

tcg/tcg.c static void tcg_out_goto_tb(TCGContext *s, int which);

Quoted 5 lines
tcg/tcg.c +3 -0
+static void tcg_out_label_cb(TCGContext *s, TCGLabel *l);
+static int tcg_out_tb_end(TCGContext *s);
+#endif
  #if TCG_TARGET_MAYBE_vec
  static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned

vece,

Quoted 9 lines

tcg/tcg.c TCGv_env tcg_env; · line 258

Quoted 7 lines
tcg/tcg.c +1 -1
                              TCGReg dst, TCGReg src);
@@ -251,7 +255,7 @@ TCGv_env tcg_env;
  const void *tcg_code_gen_epilogue;
  uintptr_t tcg_splitwx_diff;
 
-#ifndef CONFIG_TCG_INTERPRETER
+#if !defined(CONFIG_TCG_INTERPRETER) && !defined(EMSCRIPTEN)

s/&&/||/ otherwise breaks TCI? (various cases)

Thank you for pointing this out. Let me break it down with a table to clarify this logic.

                  | !defined(CONFIG_TCG_INTERPRETER) | !defined(EMSCRIPTEN)
| && | || |
------------------+----------------------------------+----------------------+----+----+
non-emcc + TCI    |              F                   |           T
 | F  |  T |
non-emcc + non-TCI|              T                   |           T
 | T  |  T |
emcc + TCI        |              F                   |           F
 | F  |  F |
emcc + wasm       |              T                   |           F
 | F  |  T |

If we use "||", this condition becomes always true for non-emscripten builds, regardless of whether TCI is used. That would break TCI's intended behaviour. On emscripten builds, both of TCI and wasm backends define tcg_qemu_tb_exec on their own so the declaration here is unnecessary. This aligns with the behaviour of "&&", not "||". So unless I'm missing something, "&&" seems to match the intent?

That said, this condition is a bit complex. Parhaps we shoud introduce a clearer flag (e.g., HAS_TCG_QEMU_TB_EXEC) in tcg-target.h?

Quoted 1 line

Out of curiosity, have you tried to run a big-endian guest?

I've just tried a s390x guest by booting Linux. It worked after applying a small bugfix (unrelated to endianness, shown below). I'll include this fix in the next verison of the patch series.

Review static void tcg_wasm_out_sub2_i64(TCGContext *s,

Quoted 1 line
(quoted diff) +0 -0
@@ -1718,6 +1718,7 @@ static void tcg_wasm_out_sub2_i64(TCGContext *s,

TCGReg retl, TCGReg reth,

Review static void tcg_wasm_out_sub2_i64(TCGContext *s,

Quoted 8 lines
(quoted diff) +1 -0
         tcg_wasm_out_op_local_get(s, TMP64_LOCAL_0_IDX);
         tcg_wasm_out_op_global_get_r(s, al);
         tcg_wasm_out_op_i64_gt_u(s);
+        tcg_wasm_out_op_i64_extend_i32_s(s);
 
         tcg_wasm_out_op_local_get(s, TMP64_LOCAL_0_IDX);
         tcg_wasm_out_op_global_set_r(s, retl);
@@ -1727,6 +1728,7 @@ static void tcg_wasm_out_sub2_i64(TCGContext *s,

TCGReg retl, TCGReg reth,

Quoted 5 lines
(quoted diff) +1 -0
         tcg_wasm_out_op_global_get_r(s, retl);
         tcg_wasm_out_op_global_get_r(s, al);
         tcg_wasm_out_op_i64_gt_u(s);
+        tcg_wasm_out_op_i64_extend_i32_s(s);
     }
#23 lore raw

Re: [PATCH 07/10] tcg: Add a TCG backend for WebAssembly

Hi Philippe, let me resend the table as it was corrupted in the last mail.

A: !defined(CONFIG_TCG_INTERPRETER)
B: !defined(EMSCRIPTEN)

                  | A | B | && | || |
------------------+---+---+----+----+
non-emcc + TCI    | F | T | F  | T  |
non-emcc + non-TCI| T | T | T  | T  |
emcc + TCI        | F | F | F  | F  |
emcc + wasm       | T | F | F  | T  |
#24 lore raw

PATCH 8/10 hw/9pfs: Allow using hw/9pfs with emscripten

In reply to Kohei Tokunaga

Emscripten's fiber does not support submitting coroutines to other threads. So this commit modifies hw/9pfs/coth.h to disable this behavior when compiled with Emscripten.

---
 fsdev/file-op-9p.h     |  3 +++
 fsdev/meson.build      |  2 +-
 hw/9pfs/9p-util-stub.c | 43 ++++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/9p-util.h      | 18 ++++++++++++++++++
 hw/9pfs/9p.c           |  3 +++
 hw/9pfs/coth.h         | 12 ++++++++++++
 hw/9pfs/meson.build    |  2 ++
 meson.build            |  6 +++---
 8 files changed, 85 insertions(+), 4 deletions(-)
 create mode 100644 hw/9pfs/9p-util-stub.c
fsdev/file-op-9p.h +3 -0
index 4997677460..b7ca2640ce 100644
@@ -26,6 +26,9 @@
 # include <sys/param.h>
 # include <sys/mount.h>
 #endif
+#ifdef EMSCRIPTEN
+#include <sys/vfs.h>
+#endif
 
 #define SM_LOCAL_MODE_BITS    0600
 #define SM_LOCAL_DIR_MODE_BITS    0700
fsdev/meson.build +1 -1
index c751d8cb62..c3e92a29d7 100644
@@ -5,6 +5,6 @@ fsdev_ss.add(when: ['CONFIG_FSDEV_9P'], if_true: files(
   '9p-marshal.c',
   'qemu-fsdev.c',
 ), if_false: files('qemu-fsdev-dummy.c'))
-if host_os in ['linux', 'darwin']
+if host_os in ['linux', 'darwin', 'emscripten']
   system_ss.add_all(fsdev_ss)
 endif
hw/9pfs/9p-util-stub.c new file +43 -0

43 lines not on this page · raw message

hw/9pfs/9p-util.h +18 -0
index 7bc4ec8e85..8c5006fcdc 100644
@@ -84,6 +84,24 @@ static inline int errno_to_dotl(int err) {
     } else if (err == EOPNOTSUPP) {
         err = 95; /* ==EOPNOTSUPP on Linux */
     }
+#elif defined(EMSCRIPTEN)
+    /*
+     * FIXME: Only most important errnos translated here yet, this should be
+     * extended to as many errnos being translated as possible in future.
+     */
+    if (err == ENAMETOOLONG) {
+        err = 36; /* ==ENAMETOOLONG on Linux */
+    } else if (err == ENOTEMPTY) {
+        err = 39; /* ==ENOTEMPTY on Linux */
+    } else if (err == ELOOP) {
+        err = 40; /* ==ELOOP on Linux */
+    } else if (err == ENODATA) {
+        err = 61; /* ==ENODATA on Linux */
+    } else if (err == ENOTSUP) {
+        err = 95; /* ==EOPNOTSUPP on Linux */
+    } else if (err == EOPNOTSUPP) {
+        err = 95; /* ==EOPNOTSUPP on Linux */
+    }
 #else
 #error Missing errno translation to Linux for this host system
 #endif
hw/9pfs/9p.c +3 -0
index 7cad2bce62..4f45f0edd3 100644
@@ -4013,6 +4013,9 @@ out_nofid:
  * Linux guests.
  */
 #define P9_XATTR_SIZE_MAX 65536
+#elif defined(EMSCRIPTEN)
+/* No support for xattr */
+#define P9_XATTR_SIZE_MAX 0
 #else
 #error Missing definition for P9_XATTR_SIZE_MAX for this host system
 #endif
hw/9pfs/coth.h +12 -0
index 2c54249b35..7b0d05ba1b 100644
@@ -19,6 +19,7 @@
 #include "qemu/coroutine-core.h"
 #include "9p.h"
 
+#ifndef EMSCRIPTEN
 /*
  * we want to use bottom half because we want to make sure the below
  * sequence of events.
@@ -57,6 +58,17 @@
         /* re-enter back to qemu thread */                              \
         qemu_coroutine_yield();                                         \
     } while (0)
+#else
+/*
+ * FIXME: implement this on emscripten but emscripten's coroutine
+ * implementation (fiber) doesn't support submitting a coroutine to other
+ * threads.
+ */
+#define v9fs_co_run_in_worker(code_block)                               \
+    do {                                                                \
+        code_block;                                                     \
+    } while (0)
+#endif
 
 void co_run_in_worker_bh(void *);
 int coroutine_fn v9fs_co_readlink(V9fsPDU *, V9fsPath *, V9fsString *);
hw/9pfs/meson.build +2 -0
index d35d4f44ff..04f85fb9e9 100644
@@ -17,6 +17,8 @@ if host_os == 'darwin'
   fs_ss.add(files('9p-util-darwin.c'))
 elif host_os == 'linux'
   fs_ss.add(files('9p-util-linux.c'))
+elif host_os == 'emscripten'
+  fs_ss.add(files('9p-util-stub.c'))
 endif
 fs_ss.add(when: 'CONFIG_XEN_BUS', if_true: files('xen-9p-backend.c'))
 system_ss.add_all(when: 'CONFIG_FSDEV_9P', if_true: fs_ss)
meson.build +3 -3
index ab84820bc5..a3aadf8b59 100644
@@ -2356,11 +2356,11 @@ dbus_display = get_option('dbus_display') \
   .allowed()
 
 have_virtfs = get_option('virtfs') \
-    .require(host_os == 'linux' or host_os == 'darwin',
+    .require(host_os == 'linux' or host_os == 'darwin' or host_os == 'emscripten',
              error_message: 'virtio-9p (virtfs) requires Linux or macOS') \
-    .require(host_os == 'linux' or cc.has_function('pthread_fchdir_np'),
+    .require(host_os == 'linux' or host_os == 'emscripten' or cc.has_function('pthread_fchdir_np'),
              error_message: 'virtio-9p (virtfs) on macOS requires the presence of pthread_fchdir_np') \
-    .require(host_os == 'darwin' or libattr.found(),
+    .require(host_os == 'darwin' or host_os == 'emscripten' or libattr.found(),
              error_message: 'virtio-9p (virtfs) on Linux requires libattr-devel') \
     .disable_auto_if(not have_tools and not have_system) \
     .allowed()
Signature · 1 line
2.25.1
#25 lore raw

Re: [PATCH 08/10] hw/9pfs: Allow using hw/9pfs with emscripten

Quoted 3 lines · CEST Kohei Tokunaga

Emscripten's fiber does not support submitting coroutines to other threads. So this commit modifies hw/9pfs/coth.h to disable this behavior when compiled with Emscripten.

The lack of being able to dispatch a coroutine to a worker thread is one thing, however it would probably still make sense to use fibers in 9pfs as replacement of its coroutines mechanism.

In 9pfs coroutines are used to dispatch blocking fs I/O syscalls from main thread to worker thread(s):

https://wiki.qemu.org/Documentation/9p#Control_Flow

If you just remove the coroutine code entirely, 9p server might hang for good, and with it QEMU's main thread.

By using fibers instead, it would not hang, as it seems as if I/O syscalls are emulated in Emscripten, right?

Quoted 78 lines
---
 fsdev/file-op-9p.h     |  3 +++
 fsdev/meson.build      |  2 +-
 hw/9pfs/9p-util-stub.c | 43 ++++++++++++++++++++++++++++++++++++++++++
 hw/9pfs/9p-util.h      | 18 ++++++++++++++++++
 hw/9pfs/9p.c           |  3 +++
 hw/9pfs/coth.h         | 12 ++++++++++++
 hw/9pfs/meson.build    |  2 ++
 meson.build            |  6 +++---
 8 files changed, 85 insertions(+), 4 deletions(-)
 create mode 100644 hw/9pfs/9p-util-stub.c
fsdev/file-op-9p.h +3 -0
index 4997677460..b7ca2640ce 100644
@@ -26,6 +26,9 @@
 # include <sys/param.h>
 # include <sys/mount.h>
 #endif
+#ifdef EMSCRIPTEN
+#include <sys/vfs.h>
+#endif
 
 #define SM_LOCAL_MODE_BITS    0600
 #define SM_LOCAL_DIR_MODE_BITS    0700
fsdev/meson.build +1 -1
index c751d8cb62..c3e92a29d7 100644
@@ -5,6 +5,6 @@ fsdev_ss.add(when: ['CONFIG_FSDEV_9P'], if_true: files(
   '9p-marshal.c',
   'qemu-fsdev.c',
 ), if_false: files('qemu-fsdev-dummy.c'))
-if host_os in ['linux', 'darwin']
+if host_os in ['linux', 'darwin', 'emscripten']
   system_ss.add_all(fsdev_ss)
 endif
hw/9pfs/9p-util-stub.c new file +33 -0
new file mode 100644
index 0000000000..57c89902ab
@@ -0,0 +1,43 @@
+/*
+ * 9p utilities stub functions
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "9p-util.h"
+
+ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name,
+                             void *value, size_t size)
+{
+    return -1;
+}
+
+ssize_t flistxattrat_nofollow(int dirfd, const char *filename,
+                              char *list, size_t size)
+{
+    return -1;
+}
+
+ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,
+                                const char *name)
+{
+    return -1;
+}
+
+int fsetxattrat_nofollow(int dirfd, const char *path, const char *name,
+                         void *value, size_t size, int flags)
+{
+    return -1;
+
+}

hw/9pfs/9p-util-stub.c line 43

Quoted 10 lines
hw/9pfs/9p-util-stub.c new file +10 -0
+
+int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
+{
+    return -1;
+}
+
+ssize_t fgetxattr(int fd, const char *name, void *value, size_t size)
+{
+    return -1;
+}

Missing

    errno = ENOTSUP;
Quoted 16 lines
hw/9pfs/9p-util.h +8 -0
index 7bc4ec8e85..8c5006fcdc 100644
@@ -84,6 +84,24 @@ static inline int errno_to_dotl(int err) {
     } else if (err == EOPNOTSUPP) {
         err = 95; /* ==EOPNOTSUPP on Linux */
     }
+#elif defined(EMSCRIPTEN)
+    /*
+     * FIXME: Only most important errnos translated here yet, this should be
+     * extended to as many errnos being translated as possible in future.
+     */
+    if (err == ENAMETOOLONG) {
+        err = 36; /* ==ENAMETOOLONG on Linux */
+    } else if (err == ENOTEMPTY) {

hw/9pfs/9p-util.h static inline int errno_to_dotl(int err) { · line 104

Quoted 10 lines
hw/9pfs/9p-util.h +10 -0
+        err = 39; /* ==ENOTEMPTY on Linux */
+    } else if (err == ELOOP) {
+        err = 40; /* ==ELOOP on Linux */
+    } else if (err == ENODATA) {
+        err = 61; /* ==ENODATA on Linux */
+    } else if (err == ENOTSUP) {
+        err = 95; /* ==EOPNOTSUPP on Linux */
+    } else if (err == EOPNOTSUPP) {
+        err = 95; /* ==EOPNOTSUPP on Linux */
+    }

Looks like you just copied the macOS errno translation code. That probably doesn't make sense.

/Christian

Quoted 80 lines
hw/9pfs/9p-util.h +0 -0
 #else
 #error Missing errno translation to Linux for this host system
 #endif
hw/9pfs/9p.c +3 -0
index 7cad2bce62..4f45f0edd3 100644
@@ -4013,6 +4013,9 @@ out_nofid:
  * Linux guests.
  */
 #define P9_XATTR_SIZE_MAX 65536
+#elif defined(EMSCRIPTEN)
+/* No support for xattr */
+#define P9_XATTR_SIZE_MAX 0
 #else
 #error Missing definition for P9_XATTR_SIZE_MAX for this host system
 #endif
hw/9pfs/coth.h +12 -0
index 2c54249b35..7b0d05ba1b 100644
@@ -19,6 +19,7 @@
 #include "qemu/coroutine-core.h"
 #include "9p.h"
 
+#ifndef EMSCRIPTEN
 /*
  * we want to use bottom half because we want to make sure the below
  * sequence of events.
@@ -57,6 +58,17 @@
         /* re-enter back to qemu thread */                              \
         qemu_coroutine_yield();                                         \
     } while (0)
+#else
+/*
+ * FIXME: implement this on emscripten but emscripten's coroutine
+ * implementation (fiber) doesn't support submitting a coroutine to other
+ * threads.
+ */
+#define v9fs_co_run_in_worker(code_block)                               \
+    do {                                                                \
+        code_block;                                                     \
+    } while (0)
+#endif
 
 void co_run_in_worker_bh(void *);
 int coroutine_fn v9fs_co_readlink(V9fsPDU *, V9fsPath *, V9fsString *);
hw/9pfs/meson.build +2 -0
index d35d4f44ff..04f85fb9e9 100644
@@ -17,6 +17,8 @@ if host_os == 'darwin'
   fs_ss.add(files('9p-util-darwin.c'))
 elif host_os == 'linux'
   fs_ss.add(files('9p-util-linux.c'))
+elif host_os == 'emscripten'
+  fs_ss.add(files('9p-util-stub.c'))
 endif
 fs_ss.add(when: 'CONFIG_XEN_BUS', if_true: files('xen-9p-backend.c'))
 system_ss.add_all(when: 'CONFIG_FSDEV_9P', if_true: fs_ss)
meson.build +3 -3
index ab84820bc5..a3aadf8b59 100644
@@ -2356,11 +2356,11 @@ dbus_display = get_option('dbus_display') \
   .allowed()
 
 have_virtfs = get_option('virtfs') \
-    .require(host_os == 'linux' or host_os == 'darwin',
+    .require(host_os == 'linux' or host_os == 'darwin' or host_os == 'emscripten',
              error_message: 'virtio-9p (virtfs) requires Linux or macOS') \
-    .require(host_os == 'linux' or cc.has_function('pthread_fchdir_np'),
+    .require(host_os == 'linux' or host_os == 'emscripten' or cc.has_function('pthread_fchdir_np'),
              error_message: 'virtio-9p (virtfs) on macOS requires the presence of pthread_fchdir_np') \
-    .require(host_os == 'darwin' or libattr.found(),
+    .require(host_os == 'darwin' or host_os == 'emscripten' or libattr.found(),
              error_message: 'virtio-9p (virtfs) on Linux requires libattr-devel') \
     .disable_auto_if(not have_tools and not have_system) \
     .allowed()
#26 lore raw

Re: [PATCH 08/10] hw/9pfs: Allow using hw/9pfs with emscripten

Hi Christian,

Quoted 8 lines
Quoted 3 lines

Emscripten's fiber does not support submitting coroutines to other threads. So this commit modifies hw/9pfs/coth.h to disable this behavior when compiled with Emscripten.

The lack of being able to dispatch a coroutine to a worker thread is one thing, however it would probably still make sense to use fibers in 9pfs as replacement of its coroutines mechanism.

Quoted 6 lines

In 9pfs coroutines are used to dispatch blocking fs I/O syscalls from main thread to worker thread(s):

https://wiki.qemu.org/Documentation/9p#Control_Flow

If you just remove the coroutine code entirely, 9p server might hang for

good,

Quoted 3 lines

and with it QEMU's main thread.

By using fibers instead, it would not hang, as it seems as if I/O

syscalls are

Quoted 1 line

emulated in Emscripten, right?

Thank you for the feedback. Yes, it would be great if Emscripten's fiber could be used to address this limitation. Since Emscripten's fiber is cooperative, I believe a blocking code_block can still block the 9pfs server unless an explicit yield occurs within it. I'll continue exploring better solutions for this. Please let me know if I'm missing anything.

Quoted 3 lines

Missing

    errno = ENOTSUP;

Sure, I'll fix this in the next version of the series.

Quoted 2 lines

Looks like you just copied the macOS errno translation code. That probably doesn't make sense.

Errno values differ between Emscripten and Linux, so conversion is required here. I've used the same mappings as macOS for now, but I'm happy to add more conversions if needed.

#27 lore raw

Re: [PATCH 08/10] hw/9pfs: Allow using hw/9pfs with emscripten

Quoted 20 lines · CEST Kohei Tokunaga

Hi Christian,

Quoted 8 lines
Quoted 3 lines

Emscripten's fiber does not support submitting coroutines to other threads. So this commit modifies hw/9pfs/coth.h to disable this behavior when compiled with Emscripten.

The lack of being able to dispatch a coroutine to a worker thread is one thing, however it would probably still make sense to use fibers in 9pfs as replacement of its coroutines mechanism.

Quoted 6 lines

In 9pfs coroutines are used to dispatch blocking fs I/O syscalls from main thread to worker thread(s):

https://wiki.qemu.org/Documentation/9p#Control_Flow

If you just remove the coroutine code entirely, 9p server might hang for

good,

Quoted 3 lines

and with it QEMU's main thread.

By using fibers instead, it would not hang, as it seems as if I/O

Quoted 8 lines

syscalls are

Quoted 1 line

emulated in Emscripten, right?

Thank you for the feedback. Yes, it would be great if Emscripten's fiber could be used to address this limitation. Since Emscripten's fiber is cooperative, I believe a blocking code_block can still block the 9pfs server unless an explicit yield occurs within it. I'll continue exploring better solutions for this. Please let me know if I'm missing anything.

As far as I understand it, the I/O syscalls are emulated, and when being called by fibers, blocking syscalls would imply to yield under the hood, without explicit yield by application that is.

If that's true, it would only require little code changes for this to work.

Quoted 4 lines
Quoted 3 lines

Missing

    errno = ENOTSUP;
Quoted 8 lines

Sure, I'll fix this in the next version of the series.

Quoted 2 lines

Looks like you just copied the macOS errno translation code. That probably doesn't make sense.

Errno values differ between Emscripten and Linux, so conversion is required here. I've used the same mappings as macOS for now, but I'm happy to add more conversions if needed.

OK, but why have you chosen macOS errno mapping exactly? Are you testing on a macOS host machine? Are errno values of emscripten machine dependent?

The errno mapping must be correct, otherwise funny things will happen on guest side if 9p2000.L client is used, as it blindly expects errno numbers sent by 9p server to be in native Linux errno number presentation.

Alternatively 9p2000.u protocol variant could be used for Emscripten. Not ideal, as this 9p protocol version is somewhat a legacy protocol from QEMU perspective, reduced performance, less reliable, but it transmits error strings to client which it can map to correct errno values by itself. Linux 9p client uses a hash map for this errno translation of 9p2000.u error strings.

/Christian

#28 lore raw

Re: [PATCH 08/10] hw/9pfs: Allow using hw/9pfs with emscripten

Quoted 49 lines · CEST Christian Schoenebeck
Quoted 20 lines · CEST Kohei Tokunaga

Hi Christian,

Quoted 8 lines
Quoted 3 lines

Emscripten's fiber does not support submitting coroutines to other threads. So this commit modifies hw/9pfs/coth.h to disable this behavior when compiled with Emscripten.

The lack of being able to dispatch a coroutine to a worker thread is one thing, however it would probably still make sense to use fibers in 9pfs as replacement of its coroutines mechanism.

Quoted 6 lines

In 9pfs coroutines are used to dispatch blocking fs I/O syscalls from main thread to worker thread(s):

https://wiki.qemu.org/Documentation/9p#Control_Flow

If you just remove the coroutine code entirely, 9p server might hang for

good,

Quoted 3 lines

and with it QEMU's main thread.

By using fibers instead, it would not hang, as it seems as if I/O

Quoted 8 lines

syscalls are

Quoted 1 line

emulated in Emscripten, right?

Thank you for the feedback. Yes, it would be great if Emscripten's fiber could be used to address this limitation. Since Emscripten's fiber is cooperative, I believe a blocking code_block can still block the 9pfs server unless an explicit yield occurs within it. I'll continue exploring better solutions for this. Please let me know if I'm missing anything.

As far as I understand it, the I/O syscalls are emulated, and when being called by fibers, blocking syscalls would imply to yield under the hood, without explicit yield by application that is.

If that's true, it would only require little code changes for this to work.

Quoted 4 lines
Quoted 3 lines

Missing

    errno = ENOTSUP;
Quoted 8 lines

Sure, I'll fix this in the next version of the series.

Quoted 2 lines

Looks like you just copied the macOS errno translation code. That probably doesn't make sense.

Errno values differ between Emscripten and Linux, so conversion is required here. I've used the same mappings as macOS for now, but I'm happy to add more conversions if needed.

Quoted 6 lines

OK, but why have you chosen macOS errno mapping exactly? Are you testing on a macOS host machine? Are errno values of emscripten machine dependent?

The errno mapping must be correct, otherwise funny things will happen on guest side if 9p2000.L client is used, as it blindly expects errno numbers sent by 9p server to be in native Linux errno number presentation.

Let my answer my own question: I just checked the wasi sources. The errno values are hard coded by the wasi API, consistent over systems. So the current mapping of this patch is wrong. macOS uses a different mapping than the wasi API.

https://github.com/WebAssembly/wasi-libc/blob/main/libc-bottom-half/headers/public/__errno_values.h

https://github.com/emscripten-core/emscripten/blob/4af36cf80647f9a82be617a0ff32f3e56f220e41/system/include/wasi/api.h#L116

So please use a correct mapping as defined in that header file.

/Christian

Quoted 7 lines

Alternatively 9p2000.u protocol variant could be used for Emscripten. Not ideal, as this 9p protocol version is somewhat a legacy protocol from QEMU perspective, reduced performance, less reliable, but it transmits error strings to client which it can map to correct errno values by itself. Linux 9p client uses a hash map for this errno translation of 9p2000.u error strings.

/Christian

#29 lore raw

Re: [PATCH 08/10] hw/9pfs: Allow using hw/9pfs with emscripten

Quoted 2 lines · CEST Christian Schoenebeck
Quoted 1 line · CEST Christian Schoenebeck

On Friday, April 11, 2025 12:47:29 PM CEST Kohei Tokunaga wrote:

[...]

Quoted 5 lines

Let my answer my own question: I just checked the wasi sources. The errno values are hard coded by the wasi API, consistent over systems. So the current mapping of this patch is wrong. macOS uses a different mapping than the wasi API.

Quoted 13 lines

https://github.com/WebAssembly/wasi-libc/blob/main/libc-bottom-half/headers/public/__errno_values.h

https://github.com/emscripten-core/emscripten/blob/4af36cf80647f9a82be617a0ff32f3e56f220e41/system/include/wasi/api.h#L116

So please use a correct mapping as defined in that header file.

/Christian

Quoted 5 lines

Alternatively 9p2000.u protocol variant could be used for Emscripten. Not ideal, as this 9p protocol version is somewhat a legacy protocol from QEMU perspective, reduced performance, less reliable, but it transmits error strings to client which it can map to correct errno values by itself. Linux 9p client uses a hash map for this errno translation of 9p2000.u error strings.

Stupid me. That's host errno -> Linux errno translation. So your values are obviously correct, sorry!

However still worth comparing the Linux vs. wasi header files on this.

And I would avoid duplicating the macOS translation code. Instead I would just do a one-line change:

#elif defined(CONFIG_DARWIN) || defined(EMSCRIPTEN)
...

And probably leave a comment with a link to the wasi API header file there, so in case new errno translations are added for macOS, that people also check whether those macros exist in the wasi header file as well.

/Christian

#30 lore raw

Re: [PATCH 08/10] hw/9pfs: Allow using hw/9pfs with emscripten

Hi Christian,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Emscripten's fiber does not support submitting coroutines to other threads. So this commit modifies hw/9pfs/coth.h to disable this

behavior

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

when compiled with Emscripten.

The lack of being able to dispatch a coroutine to a worker thread is

one

Quoted 1 line
Quoted 1 line
Quoted 1 line

thing, however it would probably still make sense to use fibers in

9pfs as

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

replacement of its coroutines mechanism.

In 9pfs coroutines are used to dispatch blocking fs I/O syscalls from

main

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

thread to worker thread(s):

https://wiki.qemu.org/Documentation/9p#Control_Flow

If you just remove the coroutine code entirely, 9p server might hang

for

Quoted 10 lines
Quoted 10 lines

good,

Quoted 3 lines

and with it QEMU's main thread.

By using fibers instead, it would not hang, as it seems as if I/O

syscalls are

Quoted 1 line

emulated in Emscripten, right?

Thank you for the feedback. Yes, it would be great if Emscripten's fiber could be used to address this limitation. Since Emscripten's fiber is cooperative, I believe a blocking code_block can still block the 9pfs

server

Quoted 1 line
Quoted 1 line

unless an explicit yield occurs within it. I'll continue exploring

better

Quoted 7 lines
Quoted 1 line

solutions for this. Please let me know if I'm missing anything.

As far as I understand it, the I/O syscalls are emulated, and when being called by fibers, blocking syscalls would imply to yield under the hood, without explicit yield by application that is.

If that's true, it would only require little code changes for this to

work.

Thank you for the information. Yes, I/O syscalls are emulated by Emscripten. While I haven't found documentation or implementation details on whether Fibers implicitly yield on blocking syscalls, I'll continue to explore this approach.

Quoted 1 line
Quoted 1 line

Let my answer my own question: I just checked the wasi sources. The

errno

Quoted 1 line
Quoted 1 line

values are hard coded by the wasi API, consistent over systems. So the

current

Quoted 1 line
Quoted 1 line

mapping of this patch is wrong. macOS uses a different mapping than the

wasi

Quoted 3 lines
Quoted 3 lines

API.

https://github.com/WebAssembly/wasi-libc/blob/main/libc-bottom-half/headers/public/__errno_values.h

Quoted 2 lines
Quoted 2 lines

https://github.com/emscripten-core/emscripten/blob/4af36cf80647f9a82be617a0ff32f3e56f220e41/system/include/wasi/api.h#L116

Quoted 6 lines
Quoted 6 lines

So please use a correct mapping as defined in that header file.

/Christian

Quoted 1 line

Alternatively 9p2000.u protocol variant could be used for Emscripten.

Not

Quoted 1 line
Quoted 1 line
Quoted 1 line

ideal, as this 9p protocol version is somewhat a legacy protocol from

QEMU

Quoted 1 line
Quoted 1 line
Quoted 1 line

perspective, reduced performance, less reliable, but it transmits

error

Quoted 1 line
Quoted 1 line
Quoted 1 line

strings to client which it can map to correct errno values by itself.

Linux 9p

Quoted 1 line
Quoted 1 line
Quoted 1 line

client uses a hash map for this errno translation of 9p2000.u error

strings.

Quoted 2 lines

Stupid me. That's host errno -> Linux errno translation. So your values

are

Quoted 5 lines

obviously correct, sorry!

However still worth comparing the Linux vs. wasi header files on this.

And I would avoid duplicating the macOS translation code. Instead I would

just

Quoted 6 lines

do a one-line change:

#elif defined(CONFIG_DARWIN) || defined(EMSCRIPTEN)
...

And probably leave a comment with a link to the wasi API header file

there, so

Quoted 2 lines

in case new errno translations are added for macOS, that people also check whether those macros exist in the wasi header file as well.

Thanks again for the suggestion. I'll apply this change in the next version of the series.

#31 lore raw

Re: [PATCH 08/10] hw/9pfs: Allow using hw/9pfs with emscripten

In reply to Kohei Tokunaga
Quoted 2 lines · Kohei Tokunaga

Emscripten's fiber does not support submitting coroutines to other threads.

Does it work as long as the thread does not rewind?

Quoted 39 lines
hw/9pfs/9p-util-stub.c new file +33 -0
new file mode 100644
index 0000000000..57c89902ab
@@ -0,0 +1,43 @@
+/*
+ * 9p utilities stub functions
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "9p-util.h"
+
+ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name,
+                             void *value, size_t size)
+{
+    return -1;
+}
+
+ssize_t flistxattrat_nofollow(int dirfd, const char *filename,
+                              char *list, size_t size)
+{
+    return -1;
+}
+
+ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,
+                                const char *name)
+{
+    return -1;
+}
+
+int fsetxattrat_nofollow(int dirfd, const char *path, const char *name,
+                         void *value, size_t size, int flags)
+{
+    return -1;
+
+}

hw/9pfs/9p-util-stub.c line 43

Quoted 10 lines
hw/9pfs/9p-util-stub.c new file +10 -0
+
+int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev)
+{
+    return -1;
+}
+
+ssize_t fgetxattr(int fd, const char *name, void *value, size_t size)
+{
+    return -1;
+}

You can add all these to the stubs/emscripten.c file that I suggested elsewhere.

Quoted 60 lines
hw/9pfs/9p-util.h +18 -0
index 7bc4ec8e85..8c5006fcdc 100644
@@ -84,6 +84,24 @@ static inline int errno_to_dotl(int err) {
      } else if (err == EOPNOTSUPP) {
          err = 95; /* ==EOPNOTSUPP on Linux */
      }
+#elif defined(EMSCRIPTEN)
+    /*
+     * FIXME: Only most important errnos translated here yet, this should be
+     * extended to as many errnos being translated as possible in future.
+     */
+    if (err == ENAMETOOLONG) {
+        err = 36; /* ==ENAMETOOLONG on Linux */
+    } else if (err == ENOTEMPTY) {
+        err = 39; /* ==ENOTEMPTY on Linux */
+    } else if (err == ELOOP) {
+        err = 40; /* ==ELOOP on Linux */
+    } else if (err == ENODATA) {
+        err = 61; /* ==ENODATA on Linux */
+    } else if (err == ENOTSUP) {
+        err = 95; /* ==EOPNOTSUPP on Linux */
+    } else if (err == EOPNOTSUPP) {
+        err = 95; /* ==EOPNOTSUPP on Linux */
+    }
  #else
  #error Missing errno translation to Linux for this host system
  #endif
hw/9pfs/9p.c +3 -0
index 7cad2bce62..4f45f0edd3 100644
@@ -4013,6 +4013,9 @@ out_nofid:
   * Linux guests.
   */
  #define P9_XATTR_SIZE_MAX 65536
+#elif defined(EMSCRIPTEN)
+/* No support for xattr */
+#define P9_XATTR_SIZE_MAX 0
  #else
  #error Missing definition for P9_XATTR_SIZE_MAX for this host system
  #endif
hw/9pfs/coth.h +2 -0
index 2c54249b35..7b0d05ba1b 100644
@@ -19,6 +19,7 @@
  #include "qemu/coroutine-core.h"
  #include "9p.h"
 
+#ifndef EMSCRIPTEN
  /*
   * we want to use bottom half because we want to make sure the below
   * sequence of events.
@@ -57,6 +58,17 @@
          /* re-enter back to qemu thread */                              \
          qemu_coroutine_yield();                                         \
      } while (0)
+#else

hw/9pfs/coth.h line 71

Quoted 10 lines
hw/9pfs/coth.h +10 -0
+/*
+ * FIXME: implement this on emscripten but emscripten's coroutine
+ * implementation (fiber) doesn't support submitting a coroutine to other
+ * threads.
+ */
+#define v9fs_co_run_in_worker(code_block)                               \
+    do {                                                                \
+        code_block;                                                     \
+    } while (0)
+#endif

You could extracting v9fs_co_run_in_worker()'s bodies into separate functions. It is tedious but not hard; all you have to do is define structs for the to parameters and return values of v9fs_co_*(), unpack them in the callback functions, and retrieve the return value in v9fs_co_*(). Many functions

The advantage is that, instead of all the bottom half and yielding dance that is done by v9fs_co_run_in_worker() and co_run_in_worker_bh(), you can just use thread_pool_submit_co().

Paolo

Quoted 34 lines
hw/9pfs/coth.h +0 -0
  void co_run_in_worker_bh(void *);
  int coroutine_fn v9fs_co_readlink(V9fsPDU *, V9fsPath *, V9fsString *);
hw/9pfs/meson.build +2 -0
index d35d4f44ff..04f85fb9e9 100644
@@ -17,6 +17,8 @@ if host_os == 'darwin'
    fs_ss.add(files('9p-util-darwin.c'))
  elif host_os == 'linux'
    fs_ss.add(files('9p-util-linux.c'))
+elif host_os == 'emscripten'
+  fs_ss.add(files('9p-util-stub.c'))
  endif
  fs_ss.add(when: 'CONFIG_XEN_BUS', if_true: files('xen-9p-backend.c'))
  system_ss.add_all(when: 'CONFIG_FSDEV_9P', if_true: fs_ss)
meson.build +3 -3
index ab84820bc5..a3aadf8b59 100644
@@ -2356,11 +2356,11 @@ dbus_display = get_option('dbus_display') \
    .allowed()
 
  have_virtfs = get_option('virtfs') \
-    .require(host_os == 'linux' or host_os == 'darwin',
+    .require(host_os == 'linux' or host_os == 'darwin' or host_os == 'emscripten',
               error_message: 'virtio-9p (virtfs) requires Linux or macOS') \
-    .require(host_os == 'linux' or cc.has_function('pthread_fchdir_np'),
+    .require(host_os == 'linux' or host_os == 'emscripten' or cc.has_function('pthread_fchdir_np'),
               error_message: 'virtio-9p (virtfs) on macOS requires the presence of pthread_fchdir_np') \
-    .require(host_os == 'darwin' or libattr.found(),
+    .require(host_os == 'darwin' or host_os == 'emscripten' or libattr.found(),
               error_message: 'virtio-9p (virtfs) on Linux requires libattr-devel') \
      .disable_auto_if(not have_tools and not have_system) \
      .allowed()
#32 lore raw

Re: [PATCH 08/10] hw/9pfs: Allow using hw/9pfs with emscripten

Hi Paolo,

Quoted 4 lines
Quoted 2 lines

Emscripten's fiber does not support submitting coroutines to other threads.

Does it work as long as the thread does not rewind?

The structure used by Fiber includes a thread-specific field related to rewind [1], which prevents it from being shared across threads. The behavior of the remaining fields in multi-threaded contexts is not documented, so further experimentation is needed to determine whether they can be safely shared.

[1]
https://emscripten.org/docs/api_reference/fiber.h.html#c.asyncify_data_t.rewind_id

Quoted 2 lines

You can add all these to the stubs/emscripten.c file that I suggested elsewhere.

Sure, I'll apply this reorganization in the next verison of the series.

Quoted 9 lines

You could extracting v9fs_co_run_in_worker()'s bodies into separate functions. It is tedious but not hard; all you have to do is define structs for the to parameters and return values of v9fs_co_*(), unpack them in the callback functions, and retrieve the return value in v9fs_co_*(). Many functions

The advantage is that, instead of all the bottom half and yielding dance that is done by v9fs_co_run_in_worker() and co_run_in_worker_bh(), you can just use thread_pool_submit_co().

Thank you for the suggestion. I'll explore this approach, though it's still unclear whether thread_pool_submit_co() can be used with Emscripten's Fiber due to the limitations mentioned above.

#33 lore raw

PATCH 9/10 gitlab: Enable CI for wasm build

In reply to Kohei Tokunaga

Add GitLab CI job that builds QEMU using emscripten. The build runs in the added Dockerfile that contains dependencies (glib, libffi, pixman, zlib) compiled by emscripten.

---
 .gitlab-ci.d/buildtest-template.yml           |  27 ++++
 .gitlab-ci.d/buildtest.yml                    |   9 ++
 .gitlab-ci.d/container-cross.yml              |   5 +
 .../dockerfiles/emsdk-wasm32-cross.docker     | 145 ++++++++++++++++++
 4 files changed, 186 insertions(+)
 create mode 100644 tests/docker/dockerfiles/emsdk-wasm32-cross.docker
.gitlab-ci.d/buildtest-template.yml +27 -0
index 39da7698b0..67167d68a5 100644
@@ -126,3 +126,30 @@
     - du -chs ${CI_PROJECT_DIR}/*-cache
   variables:
     QEMU_JOB_AVOCADO: 1
+
+.wasm_build_job_template:
+  extends: .base_job_template
+  stage: build
+  image: $CI_REGISTRY_IMAGE/qemu/$IMAGE:$QEMU_CI_CONTAINER_TAG
+  before_script:
+    - source scripts/ci/gitlab-ci-section
+    - section_start setup "Pre-script setup"
+    - JOBS=$(expr $(nproc) + 1)
+    - section_end setup
+  script:
+    - du -sh .git
+    - mkdir build
+    - cd build
+    - section_start configure "Running configure"
+    - emconfigure ../configure --disable-docs
+          ${TARGETS:+--target-list="$TARGETS"}
+          $CONFIGURE_ARGS ||
+      { cat config.log meson-logs/meson-log.txt && exit 1; }
+    - if test -n "$LD_JOBS";
+      then
+        pyvenv/bin/meson configure . -Dbackend_max_links="$LD_JOBS" ;
+      fi || exit 1;
+    - section_end configure
+    - section_start build "Building QEMU"
+    - emmake make -j"$JOBS"
+    - section_end build
.gitlab-ci.d/buildtest.yml +9 -0
index 00f4bfcd9f..0f4d15021f 100644
@@ -801,3 +801,12 @@ coverity:
       when: never
     # Always manual on forks even if $QEMU_CI == "2"
     - when: manual
+
+build-wasm:
+  extends: .wasm_build_job_template
+  timeout: 2h
+  needs:
+    job: wasm-emsdk-cross-container
+  variables:
+    IMAGE: emsdk-wasm32-cross
+    CONFIGURE_ARGS: --static --disable-tools --enable-debug
.gitlab-ci.d/container-cross.yml +5 -0
index 34c0e729ad..3ea4971950 100644
@@ -94,3 +94,8 @@ win64-fedora-cross-container:
   extends: .container_job_template
   variables:
     NAME: fedora-win64-cross
+
+wasm-emsdk-cross-container:
+  extends: .container_job_template
+  variables:
+    NAME: emsdk-wasm32-cross
tests/docker/dockerfiles/emsdk-wasm32-cross.docker new file +145 -0

145 lines not on this page · raw message

Signature · 1 line
2.25.1
#34 lore raw

PATCH 10/10 MAINTAINERS: Update MAINTAINERS file for wasm-related files

In reply to Kohei Tokunaga
---
 MAINTAINERS | 11 +++++++++++
 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
index d54b5578f8..ea5fde475c 100644
@@ -3903,6 +3903,17 @@ F: tcg/tci/
 F: tcg/tci.c
 F: disas/tci.c
 
+WebAssembly TCG target
+M: Kohei Tokunaga <[email protected]>
+S: Maintained
+F: configs/meson/emscripten.txt
+F: hw/9pfs/9p-util-stub.c
+F: tcg/wasm32/
+F: tcg/wasm32.c
+F: tcg/wasm32.h
+F: tests/docker/dockerfiles/emsdk-wasm32-cross.docker
+F: util/coroutine-fiber.c
+
 Block drivers
 -------------
 VMDK
Signature · 1 line
2.25.1
#35 lore raw
In reply to Kohei Tokunaga
Quoted 4 lines · Kohei Tokunaga

This patch series enables QEMU's system emulator to run in a browser using Emscripten.
It includes implementations and workarounds to address browser environment limitations, as shown in the following.

I think it would be great to merge this even if there are limitations once code review comments have been addressed. Developing WebAssembly support in-tree is likely to allow this effort to develop further than if done in personal repos (and with significant efforts required to rebase the code periodically).

Quoted 24 lines

# 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 64-bit Guests

The current implementation uses Wasm's 32-bit memory model, even though Wasm supports 64-bit variables and instructions. This patch explores implementing TCG 64-bit instructions while leveraging SoftMMU for address translation. To enable 64-bit guest support in Wasm today, it was necessary to partially revert recent changes that removed support for different pointer widths between the host and guest (e.g., commits a70af12addd9060fdf8f3dbd42b42e3072c3914f and bf455ec50b6fea15b4d2493059365bf94c706273) when compiling with Emscripten. While this serves as a temporary workaround, a long-term solution could involve adopting Wasm's 64-bit memory model once it gains broader support, as it is currently not widely adopted (e.g., unsupported by Safari and libffi). Feedback and suggestions on this approach are welcome.

Quoted 7 lines

# Emscripten-Based Coroutine Backend

Emscripten does not support couroutine methods currently used by QEMU but provides a coroutine implementation called "fiber". This patch series introduces a coroutine backend using fiber. However, fiber does not support submitting coroutines to other threads. So this patch series modifies hw/9pfs/coth.h to disable this behavior when compiled with Emscripten.

QEMU's block job coroutines also rely on switching between threads. See how job_co_entry() schedules job_exit(). It's not very likely that users will run jobs in a WebAssembly environment, so maybe this is more of a theoretical problem for the time being.

Quoted 51 lines

# 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',
Quoted 4 lines

];

The sample repository[1] provides a complete setup, including an HTML file that implements a terminal UI.

If I understand correctly the QEMU project is only build the statically linked wasm binary in the CI system and not distributing it (e.g. making it available for download)? I'm asking because if the QEMU project wants to distribute the wasm binary it may be necessary to put together a combined software license to meet the license requirements of glib and other dependencies that are statically linked.

Quoted 85 lines

[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 (10):
various: Fix type conflict of GLib function pointers
various: Define macros for dependencies on emscripten util/mmap-alloc: Add qemu_ram_mmap implementation for emscripten
util: Add coroutine backend for emscripten
meson: Add wasm build in build scripts
include/exec: Allow using 64bit guest addresses on emscripten
tcg: Add a TCG backend for WebAssembly
hw/9pfs: Allow using hw/9pfs with emscripten
gitlab: Enable CI for wasm build
MAINTAINERS: Update MAINTAINERS file for wasm-related files

 .gitlab-ci.d/buildtest-template.yml           |   27 +
 .gitlab-ci.d/buildtest.yml                    |    9 +
 .gitlab-ci.d/container-cross.yml              |    5 +
 MAINTAINERS                                   |   11 +
 accel/tcg/cputlb.c                            |    8 +-
 block/file-posix.c                            |   18 +
 configs/meson/emscripten.txt                  |    6 +
 configure                                     |    7 +
 fsdev/file-op-9p.h                            |    3 +
 fsdev/meson.build                             |    2 +-
 hw/9pfs/9p-util-stub.c                        |   43 +
 hw/9pfs/9p-util.h                             |   18 +
 hw/9pfs/9p.c                                  |    3 +
 hw/9pfs/coth.h                                |   12 +
 hw/9pfs/meson.build                           |    2 +
 hw/riscv/riscv_hart.c                         |    9 +-
 include/accel/tcg/getpc.h                     |    2 +-
 include/exec/tlb-common.h                     |   14 +-
 include/exec/vaddr.h                          |   11 +
 include/qemu/atomic.h                         |    4 +
 include/qemu/cacheflush.h                     |    3 +-
 include/tcg/helper-info.h                     |    4 +-
 include/tcg/tcg.h                             |    2 +-
 meson.build                                   |   30 +-
 meson_options.txt                             |    2 +-
 os-posix.c                                    |    5 +
 qom/object.c                                  |    5 +-
 scripts/meson-buildoptions.sh                 |    2 +-
 target/arm/helper.c                           |    4 +-
 tcg/meson.build                               |    5 +
 tcg/tcg.c                                     |   26 +-
 tcg/wasm32.c                                  | 1260 +++++
 tcg/wasm32.h                                  |   39 +
 tcg/wasm32/tcg-target-con-set.h               |   18 +
 tcg/wasm32/tcg-target-con-str.h               |    8 +
 tcg/wasm32/tcg-target-has.h                   |  102 +
 tcg/wasm32/tcg-target-mo.h                    |   12 +
 tcg/wasm32/tcg-target-opc.h.inc               |    4 +
 tcg/wasm32/tcg-target-reg-bits.h              |   12 +
 tcg/wasm32/tcg-target.c.inc                   | 4484 +++++++++++++++++
 tcg/wasm32/tcg-target.h                       |   65 +
 .../dockerfiles/emsdk-wasm32-cross.docker     |  145 +
 util/cacheflush.c                             |    3 +-
 util/coroutine-fiber.c                        |  127 +
 util/mmap-alloc.c                             |   18 +
 45 files changed, 6561 insertions(+), 38 deletions(-)
 create mode 100644 configs/meson/emscripten.txt
 create mode 100644 hw/9pfs/9p-util-stub.c
 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
 create mode 100644 tests/docker/dockerfiles/emsdk-wasm32-cross.docker
 create mode 100644 util/coroutine-fiber.c
Signature · 1 line
2.25.1
#36 lore raw
Quoted 6 lines · Stefan Hajnoczi
Quoted 4 lines · Kohei Tokunaga

This patch series enables QEMU's system emulator to run in a browser using Emscripten.
It includes implementations and workarounds to address browser environment limitations, as shown in the following.

Quoted 29 lines

I think it would be great to merge this even if there are limitations once code review comments have been addressed. Developing WebAssembly support in-tree is likely to allow this effort to develop further than if done in personal repos (and with significant efforts required to rebase the code periodically).

Quoted 23 lines

# 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 64-bit Guests

The current implementation uses Wasm's 32-bit memory model, even though Wasm supports 64-bit variables and instructions. This patch explores implementing TCG 64-bit instructions while leveraging SoftMMU for address translation. To enable 64-bit guest support in Wasm today, it was necessary to partially revert recent changes that removed support for different pointer widths between the host and guest (e.g., commits a70af12addd9060fdf8f3dbd42b42e3072c3914f and bf455ec50b6fea15b4d2493059365bf94c706273) when compiling with Emscripten. While this serves as a temporary workaround, a long-term solution could involve adopting Wasm's 64-bit memory model once it gains broader support, as it is currently not widely adopted (e.g., unsupported by Safari and libffi). Feedback and suggestions on this approach are welcome.

The biggest problem I'm seeing is we no longer support 64-bit guests on 32-bit hosts, and don't plan to revert that.

#37 lore raw

Hi Stefan,

Quoted 1 line
Quoted 1 line

This patch series enables QEMU's system emulator to run in a browser

using

Quoted 2 lines
Quoted 2 lines

Emscripten.
It includes implementations and workarounds to address browser

environment

Quoted 11 lines
Quoted 1 line

limitations, as shown in the following.

I think it would be great to merge this even if there are limitations once code review comments have been addressed. Developing WebAssembly support in-tree is likely to allow this effort to develop further than if done in personal repos (and with significant efforts required to rebase the code periodically).

Quoted 3 lines

# New TCG Backend for Browsers

A new TCG backend translates IR instructions into Wasm instructions and

runs

Quoted 2 lines
Quoted 2 lines

them using the browser's WebAssembly APIs (WebAssembly.Module and WebAssembly.instantiate). To minimize compilation overhead and avoid

hitting

Quoted 1 line
Quoted 1 line

the browser's limitation of the number of instances, this backend

integrates

Quoted 6 lines
Quoted 6 lines

a forked TCI. TBs run on TCI by default, with frequently executed TBs compiled into WebAssembly.

# Workaround for Running 64-bit Guests

The current implementation uses Wasm's 32-bit memory model, even though

Wasm

Quoted 1 line
Quoted 1 line

supports 64-bit variables and instructions. This patch explores

implementing

Quoted 1 line
Quoted 1 line

TCG 64-bit instructions while leveraging SoftMMU for address

translation. To

Quoted 8 lines
Quoted 8 lines

enable 64-bit guest support in Wasm today, it was necessary to partially revert recent changes that removed support for different pointer widths between the host and guest (e.g., commits a70af12addd9060fdf8f3dbd42b42e3072c3914f and bf455ec50b6fea15b4d2493059365bf94c706273) when compiling with Emscripten. While this serves as a temporary workaround, a long-term solution could involve adopting Wasm's 64-bit memory model once it gains broader support, as it is currently not widely adopted (e.g.,

unsupported by

Quoted 1 line
Quoted 1 line

Safari and libffi). Feedback and suggestions on this approach are

welcome.

Quoted 4 lines
Quoted 4 lines

# Emscripten-Based Coroutine Backend

Emscripten does not support couroutine methods currently used by QEMU

but

Quoted 2 lines
Quoted 2 lines

provides a coroutine implementation called "fiber". This patch series introduces a coroutine backend using fiber. However, fiber does not

support

Quoted 7 lines
Quoted 2 lines

submitting coroutines to other threads. So this patch series modifies hw/9pfs/coth.h to disable this behavior when compiled with Emscripten.

QEMU's block job coroutines also rely on switching between threads. See how job_co_entry() schedules job_exit(). It's not very likely that users will run jobs in a WebAssembly environment, so maybe this is more of a theoretical problem for the time being.

Thank you for the feedback. I'll investigate the block job coroutines further. As you pointed out, I agree that users aren't likely to run block jobs in the WebAssembly environment.

Quoted 6 lines

If I understand correctly the QEMU project is only build the statically linked wasm binary in the CI system and not distributing it (e.g. making it available for download)? I'm asking because if the QEMU project wants to distribute the wasm binary it may be necessary to put together a combined software license to meet the license requirements of glib and other dependencies that are statically linked.

Yes, it doesn't distribute the statically linked wasm binary.

#38 lore raw

Hi Philippe,

Quoted 3 lines · Stefan Hajnoczi
Quoted 2 lines · Kohei Tokunaga
Quoted 1 line

This patch series enables QEMU's system emulator to run in a browser

using

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Emscripten.
It includes implementations and workarounds to address browser

environment

Quoted 11 lines
Quoted 11 lines
Quoted 1 line

limitations, as shown in the following.

I think it would be great to merge this even if there are limitations once code review comments have been addressed. Developing WebAssembly support in-tree is likely to allow this effort to develop further than if done in personal repos (and with significant efforts required to rebase the code periodically).

Quoted 3 lines

# New TCG Backend for Browsers

A new TCG backend translates IR instructions into Wasm instructions

and runs

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

them using the browser's WebAssembly APIs (WebAssembly.Module and WebAssembly.instantiate). To minimize compilation overhead and avoid

hitting

Quoted 1 line
Quoted 1 line
Quoted 1 line

the browser's limitation of the number of instances, this backend

integrates

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

a forked TCI. TBs run on TCI by default, with frequently executed TBs compiled into WebAssembly.

# Workaround for Running 64-bit Guests

The current implementation uses Wasm's 32-bit memory model, even

though Wasm

Quoted 1 line
Quoted 1 line
Quoted 1 line

supports 64-bit variables and instructions. This patch explores

implementing

Quoted 1 line
Quoted 1 line
Quoted 1 line

TCG 64-bit instructions while leveraging SoftMMU for address

translation. To

Quoted 1 line
Quoted 1 line
Quoted 1 line

enable 64-bit guest support in Wasm today, it was necessary to

partially

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

revert recent changes that removed support for different pointer widths between the host and guest (e.g., commits a70af12addd9060fdf8f3dbd42b42e3072c3914f and bf455ec50b6fea15b4d2493059365bf94c706273) when compiling with Emscripten. While this serves as a temporary workaround, a long-term solution could involve adopting Wasm's 64-bit memory model once it

gains

Quoted 1 line
Quoted 1 line
Quoted 1 line

broader support, as it is currently not widely adopted (e.g.,

unsupported by

Quoted 1 line
Quoted 1 line
Quoted 1 line

Safari and libffi). Feedback and suggestions on this approach are

welcome.

Quoted 3 lines

The biggest problem I'm seeing is we no longer support 64-bit guests on 32-bit hosts, and don't plan to revert that.

Yes, so the sixth patch ("[PATCH 06/10] include/exec: Allow using 64bit guest addresses on emscripten") should be considered as a temporary workaround, enabled only for Emsripten builds. It will be removed once wasm64 gains broader support and is adopted in the Wasm backend.

#39 lore raw

Hi Kohei,

first, congrats for this work!
It would be really nice to have this available upstream, starting with a modest TCI port, before having the tcg backend also.

Quoted 11 lines · Kohei Tokunaga

Hi Philippe,

Quoted 3 lines

The biggest problem I'm seeing is we no longer support 64-bit guests on 32-bit hosts, and don't plan to revert that.

Yes, so the sixth patch ("[PATCH 06/10] include/exec: Allow using 64bit guest addresses on emscripten") should be considered as a temporary workaround, enabled only for Emsripten builds. It will be removed once wasm64 gains broader support and is adopted in the Wasm backend.

Do you have recent information about wasm64? It seems to be something that has been discussed for several years, without really seeing any progress.

As Philippe said, we can't really revert that (it's now an assumption for our softmmu implementation, and would break code if we enabled it again now.)

That said, we can always have a first version supporting only 32 bits targets. And later, when wasm64 will be available, we can extend this to all remaining ones.

Regards,
Pierrick

#40 lore raw

Hi Pierrick, thank you for the feedback.

Quoted 3 lines

first, congrats for this work!
It would be really nice to have this available upstream, starting with a modest TCI port, before having the tcg backend also.

Sure, I'll split the patch series, starting with the TCI port.

Quoted 3 lines

Do you have recent information about wasm64? It seems to be something that has been discussed for several years, without really seeing any progress.

According to the adoption status [1], wasm64 is supported by recent versions of Chrome and Firefox. Emscripten also supports wasm64 [2]. However, it is not yet supported by Safari or libffi [3], and I haven't found a roadmap for the adoption.

[1] https://webassembly.org/features/
[2]
https://emscripten.org/docs/tools_reference/settings_reference.html#memory64
[3]
https://github.com/libffi/libffi/blob/6a99edb8082f75e523e0d6ebaba42218b80e10c8/README.md#supported-platforms

Quoted 7 lines

As Philippe said, we can't really revert that (it's now an assumption for our softmmu implementation, and would break code if we enabled it again now.)

That said, we can always have a first version supporting only 32 bits targets. And later, when wasm64 will be available, we can extend this to all remaining ones.

The main challenge I'm seeing is that I couldn't find a clear timeline for wasm64 adoption so it's difficult to predict when it will be widely available.

Regarding the revert, the wasm backend supports 64-bit words, so the revert introduced in patch 06/10 is partial, as also pointed out by Paolo.

#41 lore raw
Quoted 8 lines · Kohei Tokunaga

Hi Pierrick, thank you for the feedback.

Quoted 3 lines

first, congrats for this work!
It would be really nice to have this available upstream, starting with a modest TCI port, before having the tcg backend also.

Sure, I'll split the patch series, starting with the TCI port.

Quoted 9 lines
Quoted 3 lines

Do you have recent information about wasm64? It seems to be something that has been discussed for several years, without really seeing any progress.

According to the adoption status [1], wasm64 is supported by recent versions of Chrome and Firefox. Emscripten also supports wasm64 [2]. However, it is not yet supported by Safari or libffi [3], and I haven't found a roadmap for the adoption.

Great, I didn't notice major browsers were already supporting it.
Even if Safari does not support it, maybe having the two other major ones would be enough to target this.

Regarding libffi, our usage of it seems limited in tci, I wonder if we could not workaround this. Or eventually see if enabling wasm64 is a big work in libffi itself.

Quoted 12 lines

[1] https://webassembly.org/features/ <https://webassembly.org/features/>
[2] https://emscripten.org/docs/tools_reference/ settings_reference.html#memory64 <https://emscripten.org/docs/ tools_reference/settings_reference.html#memory64>
[3] https://github.com/libffi/libffi/ blob/6a99edb8082f75e523e0d6ebaba42218b80e10c8/README.md#supported- platforms <https://github.com/libffi/libffi/ blob/6a99edb8082f75e523e0d6ebaba42218b80e10c8/README.md#supported-platforms>

Quoted 7 lines

As Philippe said, we can't really revert that (it's now an assumption for our softmmu implementation, and would break code if we enabled it again now.)

That said, we can always have a first version supporting only 32 bits targets. And later, when wasm64 will be available, we can extend this to all remaining ones.

Quoted 7 lines

The main challenge I'm seeing is that I couldn't find a clear timeline for wasm64 adoption so it's difficult to predict when it will be widely available.

Regarding the revert, the wasm backend supports 64-bit words, so the revert introduced in patch 06/10 is partial, as also pointed out by Paolo.

Ok, thanks for pointing it, I didn't really read into details of the patch, and simply assumed you wanted to revert this change completely. If we can have a solution only affecting emscripten builds, I think it's ok.

Regards,
Pierrick

#42 lore raw
In reply to Kohei Tokunaga
Quoted 7 lines · Kohei Tokunaga
Quoted 2 lines

The biggest problem I'm seeing is we no longer support 64-bit guests on 32-bit hosts, and don't plan to revert that.

Yes, so the sixth patch ("[PATCH 06/10] include/exec: Allow using 64bit guest addresses on emscripten") should be considered as a temporary workaround, enabled only for Emsripten builds. It will be removed once wasm64 gains broader support and is adopted in the Wasm backend.

Maybe there's a way though. Currently we don't support 64-bit guests on 32-bit hosts, but more precisely we don't support 64-bit guests with 32-bit host word size.

The wasm TCG backend is able to compile with 64-bit words:

+#define TCG_TARGET_HAS_div_i64 1
+#define TCG_TARGET_HAS_rem_i64 1
etc.

and if x32 was a thing it would as well. In fact the changes in patch 6/10 are not a full revert, and the "#ifdef EMSCRIPTEN" could be changed to

#if HOST_LONG_BITS >= TARGET_LONG_BITS
... use uintptr_t
#else
... use uint64_t
#endif

Paolo

#43 lore raw

Hi Paolo,

Quoted 1 line
Quoted 1 line
Quoted 1 line

The biggest problem I'm seeing is we no longer support 64-bit guests

on

Quoted 18 lines
Quoted 6 lines
Quoted 1 line

32-bit hosts, and don't plan to revert that.

Yes, so the sixth patch ("[PATCH 06/10] include/exec: Allow using 64bit guest addresses on emscripten") should be considered as a temporary workaround, enabled only for Emsripten builds. It will be removed once wasm64 gains broader support and is adopted in the Wasm backend.

Maybe there's a way though. Currently we don't support 64-bit guests on 32-bit hosts, but more precisely we don't support 64-bit guests with 32-bit host word size.

The wasm TCG backend is able to compile with 64-bit words:

(quoted diff) +2 -0
+#define TCG_TARGET_HAS_div_i64          1
+#define TCG_TARGET_HAS_rem_i64          1

etc.

Quoted 2 lines

and if x32 was a thing it would as well. In fact the changes in patch 6/10 are not a full revert, and the "#ifdef EMSCRIPTEN" could be changed

to

Quoted 6 lines

#if HOST_LONG_BITS >= TARGET_LONG_BITS
... use uintptr_t
#else
... use uint64_t
#endif

Thank you for the feedback.

Yes, the Wasm backend can be compiled with 64-bit words. The change regarding the macro looks good to me.

#44 lore raw
In reply to Stefan Hajnoczi
Quoted 11 lines · Stefan Hajnoczi
Quoted 4 lines · Kohei Tokunaga

This patch series enables QEMU's system emulator to run in a browser using Emscripten.
It includes implementations and workarounds to address browser environment limitations, as shown in the following.

I think it would be great to merge this even if there are limitations once code review comments have been addressed. Developing WebAssembly support in-tree is likely to allow this effort to develop further than if done in personal repos (and with significant efforts required to rebase the code periodically).

It is certainly impressive & clever but first two critical questions..

Is there a committment to long term (many years) development & maintenance of this, or is it just a short term experiment which will have attention dwindle in a year's time ?

Is there a compelling real world use case for this that will justify carrying it in QEMU, or is it a case of "it exists because it can" ?

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#45 lore raw

Hi Daniel,

Quoted 3 lines · Stefan Hajnoczi
Quoted 2 lines · Kohei Tokunaga
Quoted 1 line

This patch series enables QEMU's system emulator to run in a browser

using

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Emscripten.
It includes implementations and workarounds to address browser

environment

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

limitations, as shown in the following.

I think it would be great to merge this even if there are limitations once code review comments have been addressed. Developing WebAssembly support in-tree is likely to allow this effort to develop further than if done in personal repos (and with significant efforts required to rebase the code periodically).

Quoted 8 lines

It is certainly impressive & clever but first two critical questions..

Is there a committment to long term (many years) development & maintenance of this, or is it just a short term experiment which will have attention dwindle in a year's time ?

Is there a compelling real world use case for this that will justify carrying it in QEMU, or is it a case of "it exists because it can" ?

Thank you for the qeustions.

In recent years, I've observed several use cases for on-browser VM environments.

One potential application is an on-browser development environment, similar to VSCode's on-browser Python execution environment [1], but with QEMU offering enhanced compatibility for existing software

Another use case could be an on-browser playground or demo environment. Several programming languages, such as Ruby [2] and Swift [3], have already leveraged WebAssembly for this purpose. On-browser QEMU would extend this capability, allowing a wider variety of software to run within browsers.

Although the current Wasm backend implementation is still in its early stages and has room for improvement (e.g., the aforementioned workarounds, performance, etc.), I believe the examples above represent promising use cases. Of course, I am committed to maintaining and improving this backend.

[1] https://code.visualstudio.com/docs/python/python-web
[2]
https://github.com/ruby/ruby.wasm/blob/d4a9a03aae6b84327d29783b75edb63549df7e6c/README.md#try-rubywasm-no-installation-needed
[3] https://swiftwasm.org/