Skip to contentSkip to conversation

Enable QEMU TCI to run 32bit guests on browsers

PATCH Superseded Kohei Tokunaga 7 participants 54 messages Apr 16, 2025 – Apr 21, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Enable QEMU TCI to run 32bit guests on browsers

This patch series is split from the original "Enable QEMU to run on browsers" series, focusing solely on the essential code changes needed to compile QEMU TCI with Emscripten. It also inclues fixes based on feedback received in the original series, thank you for the comments.

# Supported features in this series

This patch series enables TCI interpreter mode with 32bit guest support. While the original series included the non-TCI backend and 9pfs patches, those components are not included here. They will be reintroduced in follow-up series after the foundational patches have been merged.

# 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. As a result, this patch series doesn't support components like hw/9pfs, which rely on that behavior.

# 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=arm-softmmu --enable-tcg-interpreter

emmake make -j$(nproc)

This process generates the following files:

- qemu-system-arm.js
- qemu-system-arm.wasm
- qemu-system-arm.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-arm.data --preload pack > load.js

This process generates the following files:

- qemu-system-arm.data
- load.js

Emscripten allows passing arguments to the QEMU command via the Module object in JavaScript:

Module['arguments'] = [

    '-nographic', '-m', '512M', '-machine', 'virt',
    '-L', 'pack/',
    '-global', 'virtio-mmio.force-legacy=false',
    '-device', 'virtio-blk-device,drive=d0',
    '-drive', 'file=pack/rootfs.bin,if=none,format=raw,id=d0',
    '-kernel', 'pack/kernel.img',
    '-append', 'console=ttyAMA0 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/tree/tcidev

# Additional references

- Original patch series "Enable QEMU to run on browsers":
https://patchew.org/QEMU/[email protected]/
- A talk at FOSDEM 2025:
https://fosdem.org/2025/schedule/event/fosdem-2025-6290-running-qemu-inside-browser/

Kohei Tokunaga (19):
hw/core/loader.c: Fix type conflict of GLib function pointers qom/object.c: Fix type conflict of GLib function pointers system/vl.c: Fix type conflict of GLib function pointers target/arm/helper.c: Fix type conflict of GLib function pointers target/i386/cpu.c: Fix type conflict of GLib function pointers contrib/plugins: Fix type conflict of GLib function pointers hw/net/can: Fix type conflict of GLib function pointers target/ppc: Fix type conflict of GLib function pointers target/s390x: Fix type conflict of GLib function pointers include/glib-compat.h: Poison g_list_sort and g_slist_sort util/cacheflush.c: Update cache flushing mechanism for Emscripten
block: Update block to compile with Emscripten include/qemu/osdep.h: Add Emscripten-specific OS dependencies Disable options unsupported 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
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                                   |  10 ++
 block/file-posix.c                            |   6 +
 configs/meson/emscripten.txt                  |   8 +
 configure                                     |   7 +
 contrib/plugins/cache.c                       |  12 +-
 contrib/plugins/cflow.c                       |  10 +-
 contrib/plugins/hotblocks.c                   |   4 +-
 contrib/plugins/hotpages.c                    |   4 +-
 contrib/plugins/howvec.c                      |   4 +-
 contrib/plugins/hwprofile.c                   |   8 +-
 hw/core/loader.c                              |   4 +-
 hw/net/can/xlnx-versal-canfd.c                |   4 +-
 include/glib-compat.h                         |   6 +
 include/qemu/cacheflush.h                     |   3 +-
 include/qemu/osdep.h                          |   8 +-
 include/system/os-posix.h                     |   3 +-
 include/system/os-wasm.h                      | 105 +++++++++++++
 meson.build                                   |  25 ++-
 meson_options.txt                             |   2 +-
 os-wasm.c                                     | 119 ++++++++++++++
 qemu-options.hx                               |   4 +-
 qom/object.c                                  |   7 +-
 scripts/meson-buildoptions.sh                 |   2 +-
 stubs/emscripten.c                            |  13 ++
 stubs/meson.build                             |   4 +
 system/vl.c                                   |   8 +-
 target/arm/helper.c                           |   4 +-
 target/i386/cpu.c                             |  11 +-
 target/ppc/cpu_init.c                         |   4 +-
 target/s390x/cpu_models.c                     |   4 +-
 .../dockerfiles/emsdk-wasm32-cross.docker     | 145 ++++++++++++++++++
 tests/tcg/plugins/mem.c                       |   4 +-
 tests/tcg/plugins/syscall.c                   |   4 +-
 util/cacheflush.c                             |   3 +-
 util/coroutine-wasm.c                         | 127 +++++++++++++++
 util/mmap-alloc.c                             |  18 +++
 39 files changed, 697 insertions(+), 58 deletions(-)
 create mode 100644 configs/meson/emscripten.txt
 create mode 100644 include/system/os-wasm.h
 create mode 100644 os-wasm.c
 create mode 100644 stubs/emscripten.c
 create mode 100644 tests/docker/dockerfiles/emsdk-wasm32-cross.docker
 create mode 100644 util/coroutine-wasm.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/19 hw/core/loader.c: 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 using g_list_sort_with_data.

---
 hw/core/loader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/core/loader.c +2 -2
index 2e35f0aa90..93a8b45d28 100644
@@ -1410,7 +1410,7 @@ typedef struct RomSec {
  * work, but this way saves a little work later by avoiding
  * dealing with "gaps" of 0 length.
  */
-static gint sort_secs(gconstpointer a, gconstpointer b)
+static gint sort_secs(gconstpointer a, gconstpointer b, gpointer d)
 {
     RomSec *ra = (RomSec *) a;
     RomSec *rb = (RomSec *) b;
@@ -1463,7 +1463,7 @@ RomGap rom_find_largest_gap_between(hwaddr base, size_t size)
     /* sentinel */
     secs = add_romsec_to_list(secs, base + size, 1);
 
-    secs = g_list_sort(secs, sort_secs);
+    secs = g_list_sort_with_data(secs, sort_secs, NULL);
 
     for (it = g_list_first(secs); it; it = g_list_next(it)) {
         cand = (RomSec *) it->data;
Signature · 1 line
2.25.1
#10 lore raw

PATCH 2/19 qom/object.c: Fix type conflict of GLib function pointers

In reply to 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 using g_slist_sort_with_data.

---
 qom/object.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
qom/object.c +4 -3
index 01618d06bd..87f84bac41 100644
@@ -1191,7 +1191,7 @@ 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 d)
 {
     return strcasecmp(object_class_get_name((ObjectClass *)a),
                       object_class_get_name((ObjectClass *)b));
@@ -1200,8 +1200,9 @@ static gint object_class_cmp(gconstpointer a, gconstpointer b)
 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);
+    return g_slist_sort_with_data(
+        object_class_get_list(implements_type, include_abstract),
+        object_class_cmp, NULL);
 }
 
 Object *object_ref(void *objptr)
Signature · 1 line
2.25.1
#11 lore raw

PATCH 3/19 system/vl.c: Fix type conflict of GLib function pointers

In reply to 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 using g_slist_sort_with_data.

---
 system/vl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
system/vl.c +2 -2
index ec93988a03..8d89394b45 100644
@@ -1523,7 +1523,7 @@ static bool debugcon_parse(const char *devname, Error **errp)
     return true;
 }
 
-static gint machine_class_cmp(gconstpointer a, gconstpointer b)
+static gint machine_class_cmp(gconstpointer a, gconstpointer b, gpointer d)
 {
     const MachineClass *mc1 = a, *mc2 = b;
     int res;
@@ -1573,7 +1573,7 @@ static void machine_help_func(const QDict *qdict)
     }
 
     printf("Supported machines are:\n");
-    machines = g_slist_sort(machines, machine_class_cmp);
+    machines = g_slist_sort_with_data(machines, machine_class_cmp, NULL);
     for (el = machines; el; el = el->next) {
         MachineClass *mc = el->data;
         if (mc->alias) {
Signature · 1 line
2.25.1
#12 lore raw

PATCH 4/19 target/arm/helper.c: Fix type conflict of GLib function pointers

In reply to 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 using g_list_sort_with_data.

---
 target/arm/helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/helper.c +2 -2
index bb445e30cd..05793a6c97 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_with_data(keys, cpreg_key_compare, NULL);
 
     cpu->cpreg_array_len = 0;
 
Signature · 1 line
2.25.1
#13 lore raw

PATCH 5/19 target/i386/cpu.c: Fix type conflict of GLib function pointers

In reply to 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 using g_list_sort_with_data and g_slist_sort_with_data.

---
 target/i386/cpu.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
target/i386/cpu.c +8 -3
index 1b64ceaaba..2c494e4b0b 100644
@@ -6226,7 +6226,7 @@ static void listflags(GList *features)
 }
 
 /* Sort alphabetically by type name, respecting X86CPUClass::ordering. */
-static gint x86_cpu_list_compare(gconstpointer a, gconstpointer b)
+static gint x86_cpu_list_compare(gconstpointer a, gconstpointer b, gpointer d)
 {
     ObjectClass *class_a = (ObjectClass *)a;
     ObjectClass *class_b = (ObjectClass *)b;
@@ -6247,7 +6247,7 @@ static gint x86_cpu_list_compare(gconstpointer a, gconstpointer b)
 static GSList *get_sorted_cpu_model_list(void)
 {
     GSList *list = object_class_get_list(TYPE_X86_CPU, false);
-    list = g_slist_sort(list, x86_cpu_list_compare);
+    list = g_slist_sort_with_data(list, x86_cpu_list_compare, NULL);
     return list;
 }
 
@@ -6304,6 +6304,11 @@ static void x86_cpu_list_entry(gpointer data, gpointer user_data)
     qemu_printf("  %-20s  %s\n", name, desc);
 }
 
+static gint strcmp_wrap(gconstpointer a, gconstpointer b, gpointer d)
+{
+    return strcmp(a, b);
+}
+
 /* list available CPU models and flags */
 void x86_cpu_list(void)
 {
@@ -6326,7 +6331,7 @@ void x86_cpu_list(void)
         }
     }
 
-    names = g_list_sort(names, (GCompareFunc)strcmp);
+    names = g_list_sort_with_data(names, strcmp_wrap, NULL);
 
     qemu_printf("\nRecognized CPUID flags:\n");
     listflags(names);
Signature · 1 line
2.25.1
#14 lore raw

PATCH 6/19 contrib/plugins: Fix type conflict of GLib function pointers

In reply to 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 using g_list_sort_with_data.

---
 contrib/plugins/cache.c     | 12 ++++++------
 contrib/plugins/cflow.c     | 10 +++++-----
 contrib/plugins/hotblocks.c |  4 ++--
 contrib/plugins/hotpages.c  |  4 ++--
 contrib/plugins/howvec.c    |  4 ++--
 contrib/plugins/hwprofile.c |  8 ++++----
 tests/tcg/plugins/mem.c     |  4 ++--
 tests/tcg/plugins/syscall.c |  4 ++--
 8 files changed, 25 insertions(+), 25 deletions(-)
contrib/plugins/cache.c +6 -6
index 7cfd3df249..56508587d3 100644
@@ -576,7 +576,7 @@ static void sum_stats(void)
     }
 }
 
-static int dcmp(gconstpointer a, gconstpointer b)
+static int dcmp(gconstpointer a, gconstpointer b, gpointer d)
 {
     InsnData *insn_a = (InsnData *) a;
     InsnData *insn_b = (InsnData *) b;
@@ -584,7 +584,7 @@ static int dcmp(gconstpointer a, gconstpointer b)
     return insn_a->l1_dmisses < insn_b->l1_dmisses ? 1 : -1;
 }
 
-static int icmp(gconstpointer a, gconstpointer b)
+static int icmp(gconstpointer a, gconstpointer b, gpointer d)
 {
     InsnData *insn_a = (InsnData *) a;
     InsnData *insn_b = (InsnData *) b;
@@ -592,7 +592,7 @@ static int icmp(gconstpointer a, gconstpointer b)
     return insn_a->l1_imisses < insn_b->l1_imisses ? 1 : -1;
 }
 
-static int l2_cmp(gconstpointer a, gconstpointer b)
+static int l2_cmp(gconstpointer a, gconstpointer b, gpointer d)
 {
     InsnData *insn_a = (InsnData *) a;
     InsnData *insn_b = (InsnData *) b;
@@ -645,7 +645,7 @@ static void log_top_insns(void)
     InsnData *insn;
 
     miss_insns = g_hash_table_get_values(miss_ht);
-    miss_insns = g_list_sort(miss_insns, dcmp);
+    miss_insns = g_list_sort_with_data(miss_insns, dcmp, NULL);
     g_autoptr(GString) rep = g_string_new("");
     g_string_append_printf(rep, "%s", "address, data misses, instruction\n");
 
@@ -659,7 +659,7 @@ static void log_top_insns(void)
                                insn->l1_dmisses, insn->disas_str);
     }
 
-    miss_insns = g_list_sort(miss_insns, icmp);
+    miss_insns = g_list_sort_with_data(miss_insns, icmp, NULL);
     g_string_append_printf(rep, "%s", "\naddress, fetch misses, instruction\n");
 
     for (curr = miss_insns, i = 0; curr && i < limit; i++, curr = curr->next) {
@@ -676,7 +676,7 @@ static void log_top_insns(void)
         goto finish;
     }
 
-    miss_insns = g_list_sort(miss_insns, l2_cmp);
+    miss_insns = g_list_sort_with_data(miss_insns, l2_cmp, NULL);
     g_string_append_printf(rep, "%s", "\naddress, L2 misses, instruction\n");
 
     for (curr = miss_insns, i = 0; curr && i < limit; i++, curr = curr->next) {
contrib/plugins/cflow.c +5 -5
index 930ecb46fc..b5e33f25f9 100644
@@ -98,7 +98,7 @@ static GHashTable *nodes;
 struct qemu_plugin_scoreboard *state;
 
 /* SORT_HOTTEST */
-static gint hottest(gconstpointer a, gconstpointer b)
+static gint hottest(gconstpointer a, gconstpointer b, gpointer d)
 {
     NodeData *na = (NodeData *) a;
     NodeData *nb = (NodeData *) b;
@@ -107,7 +107,7 @@ static gint hottest(gconstpointer a, gconstpointer b)
         na->dest_count == nb->dest_count ? 0 : 1;
 }
 
-static gint exception(gconstpointer a, gconstpointer b)
+static gint exception(gconstpointer a, gconstpointer b, gpointer d)
 {
     NodeData *na = (NodeData *) a;
     NodeData *nb = (NodeData *) b;
@@ -116,7 +116,7 @@ static gint exception(gconstpointer a, gconstpointer b)
         na->early_exit == nb->early_exit ? 0 : 1;
 }
 
-static gint popular(gconstpointer a, gconstpointer b)
+static gint popular(gconstpointer a, gconstpointer b, gpointer d)
 {
     NodeData *na = (NodeData *) a;
     NodeData *nb = (NodeData *) b;
@@ -138,7 +138,7 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
 {
     g_autoptr(GString) result = g_string_new("collected ");
     GList *data;
-    GCompareFunc sort = &hottest;
+    GCompareDataFunc sort = &hottest;
     int i = 0;
 
     g_mutex_lock(&node_lock);
@@ -162,7 +162,7 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
         break;
     }
 
-    data = g_list_sort(data, sort);
+    data = g_list_sort_with_data(data, sort, NULL);
 
     for (GList *l = data;
          l != NULL && i < topn;
contrib/plugins/hotblocks.c +2 -2
index f12bfb7a26..98404b6885 100644
@@ -39,7 +39,7 @@ typedef struct {
     unsigned long insns;
 } ExecCount;
 
-static gint cmp_exec_count(gconstpointer a, gconstpointer b)
+static gint cmp_exec_count(gconstpointer a, gconstpointer b, gpointer d)
 {
     ExecCount *ea = (ExecCount *) a;
     ExecCount *eb = (ExecCount *) b;
@@ -79,7 +79,7 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
     g_string_append_printf(report, "%d entries in the hash table\n",
                            g_hash_table_size(hotblocks));
     counts = g_hash_table_get_values(hotblocks);
-    it = g_list_sort(counts, cmp_exec_count);
+    it = g_list_sort_with_data(counts, cmp_exec_count, NULL);
 
     if (it) {
         g_string_append_printf(report, "pc, tcount, icount, ecount\n");
contrib/plugins/hotpages.c +2 -2
index c6e6493719..9d48ac969e 100644
@@ -48,7 +48,7 @@ typedef struct {
 static GMutex lock;
 static GHashTable *pages;
 
-static gint cmp_access_count(gconstpointer a, gconstpointer b)
+static gint cmp_access_count(gconstpointer a, gconstpointer b, gpointer d)
 {
     PageCounters *ea = (PageCounters *) a;
     PageCounters *eb = (PageCounters *) b;
@@ -83,7 +83,7 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
     if (counts && g_list_next(counts)) {
         GList *it;
 
-        it = g_list_sort(counts, cmp_access_count);
+        it = g_list_sort_with_data(counts, cmp_access_count, NULL);
 
         for (i = 0; i < limit && it->next; i++, it = it->next) {
             PageCounters *rec = (PageCounters *) it->data;
contrib/plugins/howvec.c +2 -2
index 2aa9029c3f..42bddb6566 100644
@@ -155,7 +155,7 @@ static ClassSelector class_tables[] = {
 static InsnClassExecCount *class_table;
 static int class_table_sz;
 
-static gint cmp_exec_count(gconstpointer a, gconstpointer b)
+static gint cmp_exec_count(gconstpointer a, gconstpointer b, gpointer d)
 {
     InsnExecCount *ea = (InsnExecCount *) a;
     InsnExecCount *eb = (InsnExecCount *) b;
@@ -208,7 +208,7 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
     counts = g_hash_table_get_values(insns);
     if (counts && g_list_next(counts)) {
         g_string_append_printf(report, "Individual Instructions:\n");
-        counts = g_list_sort(counts, cmp_exec_count);
+        counts = g_list_sort_with_data(counts, cmp_exec_count, NULL);
 
         for (i = 0; i < limit && g_list_next(counts);
              i++, counts = g_list_next(counts)) {
contrib/plugins/hwprofile.c +4 -4
index 2a4cbc47d4..a9838ccc87 100644
@@ -71,7 +71,7 @@ static void plugin_init(void)
     devices = g_hash_table_new(NULL, NULL);
 }
 
-static gint sort_cmp(gconstpointer a, gconstpointer b)
+static gint sort_cmp(gconstpointer a, gconstpointer b, gpointer d)
 {
     DeviceCounts *ea = (DeviceCounts *) a;
     DeviceCounts *eb = (DeviceCounts *) b;
@@ -79,7 +79,7 @@ static gint sort_cmp(gconstpointer a, gconstpointer b)
            eb->totals.reads + eb->totals.writes ? -1 : 1;
 }
 
-static gint sort_loc(gconstpointer a, gconstpointer b)
+static gint sort_loc(gconstpointer a, gconstpointer b, gpointer d)
 {
     IOLocationCounts *ea = (IOLocationCounts *) a;
     IOLocationCounts *eb = (IOLocationCounts *) b;
@@ -126,13 +126,13 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
     if (counts && g_list_next(counts)) {
         GList *it;
 
-        it = g_list_sort(counts, sort_cmp);
+        it = g_list_sort_with_data(counts, sort_cmp, NULL);
 
         while (it) {
             DeviceCounts *rec = (DeviceCounts *) it->data;
             if (rec->detail) {
                 GList *accesses = g_hash_table_get_values(rec->detail);
-                GList *io_it = g_list_sort(accesses, sort_loc);
+                GList *io_it = g_list_sort_with_data(accesses, sort_loc, NULL);
                 const char *prefix = pattern ? "off" : "pc";
                 g_string_append_printf(report, "%s @ 0x%"PRIx64"\n",
                                        rec->name, rec->base);
tests/tcg/plugins/mem.c +2 -2
index d87d6628e0..ca4e8883dd 100644
@@ -67,7 +67,7 @@ static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
 static GMutex lock;
 static GHashTable *regions;
 
-static gint addr_order(gconstpointer a, gconstpointer b)
+static gint addr_order(gconstpointer a, gconstpointer b, gpointer d)
 {
     RegionInfo *na = (RegionInfo *) a;
     RegionInfo *nb = (RegionInfo *) b;
@@ -94,7 +94,7 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
     if (do_region_summary) {
         GList *counts = g_hash_table_get_values(regions);
 
-        counts = g_list_sort(counts, addr_order);
+        counts = g_list_sort_with_data(counts, addr_order, NULL);
 
         g_string_printf(out, "Region Base, Reads, Writes, Seen all\n");
 
tests/tcg/plugins/syscall.c +2 -2
index 47aad55fc1..42801f5c86 100644
@@ -180,7 +180,7 @@ static void print_entry(gpointer val, gpointer user_data)
     qemu_plugin_outs(out);
 }
 
-static gint comp_func(gconstpointer ea, gconstpointer eb)
+static gint comp_func(gconstpointer ea, gconstpointer eb, gpointer d)
 {
     SyscallStats *ent_a = (SyscallStats *) ea;
     SyscallStats *ent_b = (SyscallStats *) eb;
@@ -197,7 +197,7 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
 
     g_mutex_lock(&lock);
     GList *entries = g_hash_table_get_values(statistics);
-    entries = g_list_sort(entries, comp_func);
+    entries = g_list_sort_with_data(entries, comp_func, NULL);
     qemu_plugin_outs("syscall no.  calls  errors\n");
 
     g_list_foreach(entries, print_entry, NULL);
Signature · 1 line
2.25.1
#15 lore raw

PATCH 7/19 hw/net/can: Fix type conflict of GLib function pointers

In reply to 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 using g_slist_sort_with_data.

---
 hw/net/can/xlnx-versal-canfd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/net/can/xlnx-versal-canfd.c +2 -2
index dc242e9215..013ebc10dc 100644
@@ -1278,7 +1278,7 @@ static void tx_fifo_stamp(XlnxVersalCANFDState *s, uint32_t tb0_regid)
     }
 }
 
-static gint g_cmp_ids(gconstpointer data1, gconstpointer data2)
+static gint g_cmp_ids(gconstpointer data1, gconstpointer data2, gpointer d)
 {
     tx_ready_reg_info *tx_reg_1 = (tx_ready_reg_info *) data1;
     tx_ready_reg_info *tx_reg_2 = (tx_ready_reg_info *) data2;
@@ -1318,7 +1318,7 @@ static GSList *prepare_tx_data(XlnxVersalCANFDState *s)
             temp->can_id = s->regs[reg_num];
             temp->reg_num = reg_num;
             list = g_slist_prepend(list, temp);
-            list = g_slist_sort(list, g_cmp_ids);
+            list = g_slist_sort_with_data(list, g_cmp_ids, NULL);
         }
 
         reg_ready >>= 1;
Signature · 1 line
2.25.1
#16 lore raw

PATCH 8/19 target/ppc: Fix type conflict of GLib function pointers

In reply to 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 using g_slist_sort_with_data.

---
 target/ppc/cpu_init.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/ppc/cpu_init.c +2 -2
index f81cb680fc..f03e48ba31 100644
@@ -7115,7 +7115,7 @@ PowerPCCPUClass *ppc_cpu_get_family_class(PowerPCCPUClass *pcc)
 }
 
 /* Sort by PVR, ordering special case "host" last. */
-static gint ppc_cpu_list_compare(gconstpointer a, gconstpointer b)
+static gint ppc_cpu_list_compare(gconstpointer a, gconstpointer b, gpointer d)
 {
     ObjectClass *oc_a = (ObjectClass *)a;
     ObjectClass *oc_b = (ObjectClass *)b;
@@ -7183,7 +7183,7 @@ void ppc_cpu_list(void)
 
     qemu_printf("Available CPUs:\n");
     list = object_class_get_list(TYPE_POWERPC_CPU, false);
-    list = g_slist_sort(list, ppc_cpu_list_compare);
+    list = g_slist_sort_with_data(list, ppc_cpu_list_compare, NULL);
     g_slist_foreach(list, ppc_cpu_list_entry, NULL);
     g_slist_free(list);
 
Signature · 1 line
2.25.1
#17 lore raw

PATCH 9/19 target/s390x: Fix type conflict of GLib function pointers

In reply to 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 using g_slist_sort_with_data.

---
 target/s390x/cpu_models.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/s390x/cpu_models.c +2 -2
index 93a05e43d7..48cdef285d 100644
@@ -373,7 +373,7 @@ static void s390_print_cpu_model_list_entry(gpointer data, gpointer user_data)
     g_free(name);
 }
 
-static gint s390_cpu_list_compare(gconstpointer a, gconstpointer b)
+static gint s390_cpu_list_compare(gconstpointer a, gconstpointer b, gpointer d)
 {
     const S390CPUClass *cc_a = S390_CPU_CLASS((ObjectClass *)a);
     const S390CPUClass *cc_b = S390_CPU_CLASS((ObjectClass *)b);
@@ -415,7 +415,7 @@ void s390_cpu_list(void)
 
     qemu_printf("Available CPUs:\n");
     list = object_class_get_list(TYPE_S390_CPU, false);
-    list = g_slist_sort(list, s390_cpu_list_compare);
+    list = g_slist_sort_with_data(list, s390_cpu_list_compare, NULL);
     g_slist_foreach(list, s390_print_cpu_model_list_entry, NULL);
     g_slist_free(list);
 
Signature · 1 line
2.25.1
#18 lore raw

PATCH 10/19 include/glib-compat.h: Poison g_list_sort and g_slist_sort

In reply to Kohei Tokunaga

On emscripten, function pointer casts can cause function call failure. g_list_sort and g_slist_sort performs this internally so can't be used on Emscripten. Instead, g_list_sort_with_data and g_slist_sort_with_data should be used.

---
 include/glib-compat.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/glib-compat.h +6 -0
index 86be439ba0..e441b396ef 100644
@@ -36,6 +36,12 @@
 #include <pwd.h>
 #endif
 
+/* These functions perform function pointer casts which can cause function call
+ * failure on Emscripten. Use g_slist_sort_with_data and g_list_sort_with_data
+ * insted of these functions.
+ */
+#pragma GCC poison g_slist_sort g_list_sort
+
 /*
  * Note that because of the GLIB_VERSION_MAX_ALLOWED constant above, allowing
  * use of functions from newer GLib via this compat header needs a little
Signature · 1 line
2.25.1
#21 lore raw

PATCH 11/19 util/cacheflush.c: Update cache flushing mechanism for Emscripten

In reply to Kohei Tokunaga
---
 include/qemu/cacheflush.h | 3 ++-
 util/cacheflush.c         | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
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)
 {
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
#25 lore raw

PATCH 12/19 block: Update block to compile with Emscripten

In reply to Kohei Tokunaga

emscripten exposes copy_file_range declaration but doesn't provide the implementation in the final link. Define the emscripten-specific stub function to avoid type conflict with the emscripten's header.

---
 block/file-posix.c |  6 ++++++
 stubs/emscripten.c | 13 +++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 stubs/emscripten.c
block/file-posix.c +6 -0
index 56d1972d15..22e0ed5069 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
@@ -2010,6 +2014,7 @@ static int handle_aiocb_write_zeroes_unmap(void *opaque)
     return handle_aiocb_write_zeroes(aiocb);
 }
 
+#ifndef EMSCRIPTEN
 #ifndef HAVE_COPY_FILE_RANGE
 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 +2028,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
stubs/emscripten.c new file +13 -0
new file mode 100644
index 0000000000..2157d6349b
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include "qemu/osdep.h"
+/*
+ * 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;
+}
Signature · 1 line
2.25.1
#34 lore raw

PATCH 13/19 include/qemu/osdep.h: Add Emscripten-specific OS dependencies

In reply to Kohei Tokunaga

On emscripten, some implementations in os-posix.c can't be used such as daemonizing and changing user. This commit introduces os-wasm.c and os-wasm.h which are forked from os-posix.c and os-posix.h and patched for targetting Emscripten.

---
 include/qemu/osdep.h      |   8 ++-
 include/system/os-posix.h |   3 +-
 include/system/os-wasm.h  | 105 +++++++++++++++++++++++++++++++++
 os-wasm.c                 | 119 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 232 insertions(+), 3 deletions(-)
 create mode 100644 include/system/os-wasm.h
 create mode 100644 os-wasm.c
include/qemu/osdep.h +6 -2
index 4397a90680..96fe51bc39 100644
@@ -8,7 +8,7 @@
  * To avoid getting into possible circular include dependencies, this
  * file should not include any other QEMU headers, with the exceptions
  * of config-host.h, config-target.h, qemu/compiler.h,
- * system/os-posix.h, system/os-win32.h, glib-compat.h and
+ * system/os-posix.h, system/os-win32.h, system/os-wasm.h, glib-compat.h and
  * qemu/typedefs.h, all of which are doing a similar job to this file
  * and are under similar constraints.
  *
@@ -164,10 +164,14 @@ QEMU_EXTERN_C int daemon(int, int);
 #include "system/os-win32.h"
 #endif
 
-#ifdef CONFIG_POSIX
+#if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)
 #include "system/os-posix.h"
 #endif
 
+#if defined(EMSCRIPTEN)
+#include "system/os-wasm.h"
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
include/system/os-posix.h +2 -1
index ce5b3bccf8..e87cc36a3a 100644
@@ -1,5 +1,6 @@
 /*
- * posix specific declarations
+ * posix specific declarations forked from os-posix.c, removing functions not
+ * working on Emscripten
  *
  * Copyright (c) 2003-2008 Fabrice Bellard
  * Copyright (c) 2010 Jes Sorensen <[email protected]>
include/system/os-wasm.h new file +105 -0

105 lines not on this page · raw message

os-wasm.c new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
2.25.1
#37 lore raw

PATCH 14/19 Disable options unsupported on Emscripten

In reply to Kohei Tokunaga

Daemonizing and run-with aren't supported on Emscripten so disable these flags.

---
 qemu-options.hx | 4 +++-
 system/vl.c     | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)
qemu-options.hx +3 -1
index dc694a99a3..20ed22afe2 100644
@@ -4863,9 +4863,11 @@ SRST
 ERST
 
 #ifndef _WIN32
+#ifndef EMSCRIPTEN
 DEF("daemonize", 0, QEMU_OPTION_daemonize, \
     "-daemonize      daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
 #endif
+#endif
 SRST
 ``-daemonize``
     Daemonize the QEMU process after initialization. QEMU will not
@@ -5249,7 +5251,7 @@ HXCOMM Internal use
 DEF("qtest", HAS_ARG, QEMU_OPTION_qtest, "", QEMU_ARCH_ALL)
 DEF("qtest-log", HAS_ARG, QEMU_OPTION_qtest_log, "", QEMU_ARCH_ALL)
 
-#ifdef CONFIG_POSIX
+#if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)
 DEF("run-with", HAS_ARG, QEMU_OPTION_run_with,
     "-run-with [async-teardown=on|off][,chroot=dir][user=username|uid:gid]\n"
     "                Set miscellaneous QEMU process lifecycle options:\n"
system/vl.c +2 -2
index 8d89394b45..255ea3be6b 100644
@@ -766,7 +766,7 @@ static QemuOptsList qemu_smp_opts = {
     },
 };
 
-#if defined(CONFIG_POSIX)
+#if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)
 static QemuOptsList qemu_run_with_opts = {
     .name = "run-with",
     .head = QTAILQ_HEAD_INITIALIZER(qemu_run_with_opts.head),
@@ -3677,7 +3677,7 @@ void qemu_init(int argc, char **argv)
             case QEMU_OPTION_nouserconfig:
                 /* Nothing to be parsed here. Especially, do not error out below. */
                 break;
-#if defined(CONFIG_POSIX)
+#if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)
             case QEMU_OPTION_daemonize:
                 os_set_daemonize(true);
                 break;
Signature · 1 line
2.25.1
#40 lore raw

PATCH 15/19 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
#44 lore raw

PATCH 16/19 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-wasm.c | 127 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 127 insertions(+)
 create mode 100644 util/coroutine-wasm.c
util/coroutine-wasm.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
#45 lore raw

PATCH 17/19 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. Tests are disabled on emscripten because they rely on host features that aren't supported by emscripten (e.g. fork and unix socket).

---
 configs/meson/emscripten.txt  |  8 ++++++++
 configure                     |  7 +++++++
 meson.build                   | 25 ++++++++++++++++++++-----
 meson_options.txt             |  2 +-
 scripts/meson-buildoptions.sh |  2 +-
 stubs/meson.build             |  4 ++++
 6 files changed, 41 insertions(+), 7 deletions(-)
 create mode 100644 configs/meson/emscripten.txt
configs/meson/emscripten.txt new file +8 -0
new file mode 100644
index 0000000000..4230e88005
@@ -0,0 +1,8 @@
+[built-in options]
+c_args = ['-pthread']
+cpp_args = ['-pthread']
+objc_args = ['-pthread']
+# -sPROXY_TO_PTHREAD link time flag always requires -pthread even during
+# configuration so explicitly add the flag here.
+c_link_args = ['-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 = ['-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 +20 -5
index 41f68d3806..c9aa5016d6 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
@@ -470,7 +472,10 @@ endif
 # instead, we can't add -no-pie because it overrides -shared: the linker then
 # tries to build an executable instead of a shared library and fails.  So
 # don't add -no-pie anywhere and cross fingers. :(
-if not get_option('b_pie')
+#
+# Emscripten doesn't support -no-pie but meson can't catch the compiler
+# warning. So explicitly omit the flag for Emscripten.
+if not get_option('b_pie') and host_os != 'emscripten'
   qemu_common_flags += cc.get_supported_arguments('-fno-pie', '-no-pie')
 endif
 
@@ -514,6 +519,8 @@ ucontext_probe = '''
 supported_backends = []
 if host_os == 'windows'
   supported_backends += ['windows']
+elif host_os == 'emscripten'
+  supported_backends += ['wasm']
 else
   if host_os != 'darwin' and cc.links(ucontext_probe)
     supported_backends += ['ucontext']
@@ -2962,7 +2969,9 @@ config_host_data.set('CONFIG_ATOMIC64', cc.links('''
     return 0;
   }''', args: qemu_isa_flags))
 
-has_int128_type = cc.compiles('''
+# has_int128_type is set to false on Emscripten to avoid errors by libffi
+# during runtime.
+has_int128_type = host_os != 'emscripten' and cc.compiles('''
   __int128_t a;
   __uint128_t b;
   int main(void) { b = a; }''')
@@ -3774,6 +3783,8 @@ if have_block
   # os-win32.c does not
   if host_os == 'windows'
     system_ss.add(files('os-win32.c'))
+  elif host_os == 'emscripten'
+    blockdev_ss.add(files('os-wasm.c'))
   else
     blockdev_ss.add(files('os-posix.c'))
   endif
@@ -4456,7 +4467,11 @@ subdir('scripts')
 subdir('tools')
 subdir('pc-bios')
 subdir('docs')
-subdir('tests')
+# Tests are disabled on emscripten because they rely on host features that aren't
+# supported by emscripten (e.g. fork and unix socket).
+if host_os != 'emscripten'
+  subdir('tests')
+endif
 if gtk.found()
   subdir('po')
 endif
meson_options.txt +1 -1
index 59d973bca0..45772484cc 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', 'wasm', 'auto'],
        value: 'auto', description: 'coroutine backend to use')
 
 # Everything else can be set via --enable/--disable-* option
scripts/meson-buildoptions.sh +1 -1
index 3e8e00852b..0568385f00 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/sigaltstack/ucontext/windows/wasm)'
   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'
stubs/meson.build +4 -0
index 63392f5e78..4fd4d362f9 100644
@@ -89,3 +89,7 @@ if have_system or have_user
   stub_ss.add(files('hotplug-stubs.c'))
   stub_ss.add(files('sysbus.c'))
 endif
+
+if host_os == 'emscripten'
+  stub_ss.add(files('emscripten.c'))
+endif
\ No newline at end of file
Signature · 1 line
2.25.1
#48 lore raw

PATCH 18/19 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..0f92d5313a 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 --enable-tcg-interpreter
.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
#51 lore raw

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

In reply to Kohei Tokunaga
---
 MAINTAINERS | 10 ++++++++++
 1 file changed, 10 insertions(+)
MAINTAINERS +10 -0
index d54b5578f8..74a102c594 100644
@@ -3903,6 +3903,16 @@ 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: tests/docker/dockerfiles/emsdk-wasm32-cross.docker
+F: util/coroutine-wasm.c
+F: os-wasm.c
+F: include/system/os-wasm.h
+F: stubs/emscripten.c
+
 Block drivers
 -------------
 VMDK
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 01/19] hw/core/loader.c: Fix type conflict of GLib function pointers

In reply to Kohei Tokunaga
Quoted 23 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 using g_list_sort_with_data.

---
 hw/core/loader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/core/loader.c +1 -1
index 2e35f0aa90..93a8b45d28 100644
@@ -1410,7 +1410,7 @@ typedef struct RomSec {
  * work, but this way saves a little work later by avoiding
  * dealing with "gaps" of 0 length.
  */
-static gint sort_secs(gconstpointer a, gconstpointer b)
+static gint sort_secs(gconstpointer a, gconstpointer b, gpointer d)
 {
     RomSec *ra = (RomSec *) a;
     RomSec *rb = (RomSec *) b;

hw/core/loader.c RomGap rom_find_largest_gap_between(hwaddr base, size_t size) · line 1466

Quoted 5 lines
hw/core/loader.c +1 -1
@@ -1463,7 +1463,7 @@ RomGap rom_find_largest_gap_between(hwaddr base, size_t size)
     /* sentinel */
     secs = add_romsec_to_list(secs, base + size, 1);
 
-    secs = g_list_sort(secs, sort_secs);
+    secs = g_list_sort_with_data(secs, sort_secs, NULL);

I don't see what the problem is with the original code.

The commit message says we have a bad function cast, but the original method decl is

  GList *g_list_sort(GList*list, GCompareFunc compare_func);

where the callback is

typedef gint (*GCompareFunc)(gconstpointer a, gconstpointer b);

Our code complies with that GCompareFunc signature.

For comparison the new code uses:

  GList *g_list_sort_with_data(GList *list, GCompareDataFunc  compare_func, gpointer user_data);

where the callback is

typedef gint (*GCompareDataFunc)(gconstpointer a, gconstpointer b, gpointer user_data);

which the new code complies with, but it is undesirable since we have no use for the data parameter.

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 :|
#26 lore raw

Re: [PATCH 12/19] block: Update block to compile with Emscripten

In reply to Kohei Tokunaga
Quoted 51 lines · Kohei Tokunaga

emscripten exposes copy_file_range declaration but doesn't provide the implementation in the final link. Define the emscripten-specific stub function to avoid type conflict with the emscripten's header.

---
  block/file-posix.c |  6 ++++++
  stubs/emscripten.c | 13 +++++++++++++
  2 files changed, 19 insertions(+)

create mode 100644 stubs/emscripten.c

block/file-posix.c +6 -0
index 56d1972d15..22e0ed5069 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
@@ -2010,6 +2014,7 @@ static int handle_aiocb_write_zeroes_unmap(void *opaque)
      return handle_aiocb_write_zeroes(aiocb);
  }
 
+#ifndef EMSCRIPTEN
  #ifndef HAVE_COPY_FILE_RANGE
  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 +2028,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
stubs/emscripten.c new file +3 -0
new file mode 100644
index 0000000000..2157d6349b
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include "qemu/osdep.h"
+/*

stubs/emscripten.c line 13

Quoted 10 lines
stubs/emscripten.c new file +10 -0
+ * 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;
+}

I'd include in this patch this hunk from patch 17:

-- >8 --

stubs/meson.build +4 -0
index 63392f5e78..4fd4d362f9 100644
@@ -89,3 +89,7 @@ if have_system or have_user
   stub_ss.add(files('hotplug-stubs.c'))
   stub_ss.add(files('sysbus.c'))
 endif
+
+if host_os == 'emscripten'
+  stub_ss.add(files('emscripten.c'))
+endif

---

#3 lore raw

Re: [PATCH 01/19] hw/core/loader.c: Fix type conflict of GLib function pointers

In reply to Daniel P. Berrangé

Il mer 16 apr 2025, 10:29 Daniel P. Berrangé <[email protected]> ha scritto:

Quoted 8 lines

hw/core/loader.c RomGap rom_find_largest_gap_between(hwaddr base, size_t size) · line 1466

Quoted 2 lines
hw/core/loader.c +1 -1
-    secs = g_list_sort(secs, sort_secs);
+    secs = g_list_sort_with_data(secs, sort_secs, NULL);

I don't see what the problem is with the original code.

The commit message says we have a bad function cast, but the original method decl is

Quoted 8 lines
  GList *g_list_sort(GList*list, GCompareFunc compare_func);

where the callback is

   typedef gint (*GCompareFunc)(gconstpointer a, gconstpointer b);

Our code complies with that GCompareFunc signature.

The cast is inside glib; g_list_sort casts the function pointer and calls g_list_sort_with_data.

I suggested this solution to Kohei because it's easy to check that we're converting all users and not introducing new ones in the future (see poisoning in patch 10).

Paolo

For comparison the new code uses:

Quoted 23 lines

GList *g_list_sort_with_data(GList *list, GCompareDataFunc
compare_func, gpointer user_data);

where the callback is

typedef gint (*GCompareDataFunc)(gconstpointer a, gconstpointer b,
gpointer user_data);

which the new code complies with, but it is undesirable since we have no use for the data parameter.

With regards,
Daniel
--
|: 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 :|

#38 lore raw

Re: [PATCH 14/19] Disable options unsupported on Emscripten

In reply to Kohei Tokunaga
Quoted 14 lines · Kohei Tokunaga

Daemonizing and run-with aren't supported on Emscripten so disable these flags.

---
  qemu-options.hx | 4 +++-
  system/vl.c     | 4 ++--
  2 files changed, 5 insertions(+), 3 deletions(-)
qemu-options.hx +0 -0
index dc694a99a3..20ed22afe2 100644

qemu-options.hx SRST · line 4866

Quoted 4 lines
qemu-options.hx +1 -0
@@ -4863,9 +4863,11 @@ SRST
  ERST
 
  #ifndef _WIN32
+#ifndef EMSCRIPTEN

#if !defined(_WIN32) && !defined(EMSCRIPTEN)

?

Quoted 16 lines
qemu-options.hx +2 -1
  DEF("daemonize", 0, QEMU_OPTION_daemonize, \
      "-daemonize      daemonize QEMU after initializing\n", QEMU_ARCH_ALL)
  #endif
+#endif
  SRST
  ``-daemonize``
      Daemonize the QEMU process after initialization. QEMU will not
@@ -5249,7 +5251,7 @@ HXCOMM Internal use
  DEF("qtest", HAS_ARG, QEMU_OPTION_qtest, "", QEMU_ARCH_ALL)
  DEF("qtest-log", HAS_ARG, QEMU_OPTION_qtest_log, "", QEMU_ARCH_ALL)
 
-#ifdef CONFIG_POSIX
+#if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)
  DEF("run-with", HAS_ARG, QEMU_OPTION_run_with,
      "-run-with [async-teardown=on|off][,chroot=dir][user=username|uid:gid]\n"
      "                Set miscellaneous QEMU process lifecycle options:\n"
#4 lore raw

Re: [PATCH 01/19] hw/core/loader.c: Fix type conflict of GLib function pointers

In reply to Paolo Bonzini
Quoted 15 lines · Paolo Bonzini

Il mer 16 apr 2025, 10:29 Daniel P. Berrangé <[email protected]> ha scritto:

Quoted 12 lines

hw/core/loader.c RomGap rom_find_largest_gap_between(hwaddr base, size_t size) · line 1466

Quoted 2 lines
hw/core/loader.c +1 -1
-    secs = g_list_sort(secs, sort_secs);
+    secs = g_list_sort_with_data(secs, sort_secs, NULL);

I don't see what the problem is with the original code.

The commit message says we have a bad function cast, but the original method decl is

  GList *g_list_sort(GList*list, GCompareFunc compare_func);

where the callback is

Quoted 7 lines
Quoted 4 lines
   typedef gint (*GCompareFunc)(gconstpointer a, gconstpointer b);

Our code complies with that GCompareFunc signature.

The cast is inside glib; g_list_sort casts the function pointer and calls g_list_sort_with_data.

Sigh, the commit message should have said that, as it reads like the problem is in QEMU code, not glib code.

Quoted 3 lines

I suggested this solution to Kohei because it's easy to check that we're converting all users and not introducing new ones in the future (see poisoning in patch 10).

It is easy to check this /one/ example, but this pattern of bad casts is typical in glib with likely many more examples, so avoidance in this way does not feel like a sustainable long term strategy to me.

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 :|
#7 lore raw

Re: [PATCH 01/19] hw/core/loader.c: Fix type conflict of GLib function pointers

In reply to Paolo Bonzini
Quoted 14 lines · Paolo Bonzini

Il mer 16 apr 2025, 10:29 Daniel P. Berrangé <[email protected] <mailto:[email protected]>> ha scritto:

> -    secs = g_list_sort(secs, sort_secs);
> +    secs = g_list_sort_with_data(secs, sort_secs, NULL);

I don't see what the problem is with the original code.

The commit message says we have a bad function cast, but the original method decl is

  GList *g_list_sort(GList*list, GCompareFunc compare_func);

Quoted 8 lines

where the callback is

   typedef gint (*GCompareFunc)(gconstpointer a, gconstpointer b);

Our code complies with that GCompareFunc signature.

The cast is inside glib; g_list_sort casts the function pointer and calls g_list_sort_with_data.

Did anybody already report this to the glib people? I guess it should get fixed there in the long run...?

Thomas

#41 lore raw

Re: [PATCH 15/19] util/mmap-alloc: Add qemu_ram_mmap implementation for emscripten

In reply to Kohei Tokunaga
Quoted 48 lines · Kohei Tokunaga
---
  util/mmap-alloc.c | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
util/mmap-alloc.c +17 -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,

util/mmap-alloc.c void *qemu_ram_mmap(int fd, · line 316

Quoted 7 lines
util/mmap-alloc.c +1 -0
@@ -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)
  {

Can we keep this code generic? I.e. with something in the lines of (only build-tested):

-- >8 --

util/mmap-alloc.c +22 -12
index ed14f9c64de..0e52cce5b29 100644
@@ -238,3 +238,10 @@ static inline size_t mmap_guard_pagesize(int fd)
 {
-#if defined(__powerpc64__) && defined(__linux__)
+#if defined(EMSCRIPTEN)
+    /*
+     * emscripten doesn't support non-zero first argument for mmap so we
+     * don't use any guard, returning 0 to mmap a larger region without the
+     * hint and return an aligned pointer in qemu_ram_mmap().
+     */
+    return 0;
+#elif defined(__powerpc64__) && defined(__linux__)
     /* Mappings in the same segment must share the same page size */
@@ -246,2 +253,3 @@ static inline size_t mmap_guard_pagesize(int fd)
 
+
 void *qemu_ram_mmap(int fd,
@@ -253,4 +261,8 @@ void *qemu_ram_mmap(int fd,
     const size_t guard_pagesize = mmap_guard_pagesize(fd);
-    size_t offset, total;
-    void *ptr, *guardptr;
+    size_t offset = 0, total;
+    void *ptr, *guardptr = NULL;
+
+    assert(is_power_of_2(align));
+    /* Always align to host page size */
+    assert(align >= guard_pagesize);
 
@@ -262,13 +274,11 @@ void *qemu_ram_mmap(int fd,
 
-    guardptr = mmap_reserve(total, fd);
-    if (guardptr == MAP_FAILED) {
-        return MAP_FAILED;
+    if (guard_pagesize) {
+        guardptr = mmap_reserve(total, fd);
+        if (guardptr == MAP_FAILED) {
+            return MAP_FAILED;
+        }
+
+        offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
     }
 
-    assert(is_power_of_2(align));
-    /* Always align to host page size */
-    assert(align >= guard_pagesize);
-
-    offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
-
     ptr = mmap_activate(guardptr + offset, size, fd, qemu_map_flags,

---

#46 lore raw

Re: [PATCH 17/19] meson: Add wasm build in build scripts

In reply to Kohei Tokunaga

Hi Kohei,

Quoted 101 lines · Kohei Tokunaga

has_int128_type is set to false on emscripten as of now to avoid errors by libffi. Tests are disabled on emscripten because they rely on host features that aren't supported by emscripten (e.g. fork and unix socket).

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

create mode 100644 configs/meson/emscripten.txt

meson.build +20 -5
index 41f68d3806..c9aa5016d6 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
@@ -470,7 +472,10 @@ endif
  # instead, we can't add -no-pie because it overrides -shared: the linker then
  # tries to build an executable instead of a shared library and fails.  So
  # don't add -no-pie anywhere and cross fingers. :(
-if not get_option('b_pie')
+#
+# Emscripten doesn't support -no-pie but meson can't catch the compiler
+# warning. So explicitly omit the flag for Emscripten.
+if not get_option('b_pie') and host_os != 'emscripten'
    qemu_common_flags += cc.get_supported_arguments('-fno-pie', '-no-pie')
  endif
 
@@ -514,6 +519,8 @@ ucontext_probe = '''
  supported_backends = []
  if host_os == 'windows'
    supported_backends += ['windows']
+elif host_os == 'emscripten'
+  supported_backends += ['wasm']
  else
    if host_os != 'darwin' and cc.links(ucontext_probe)
      supported_backends += ['ucontext']
@@ -2962,7 +2969,9 @@ config_host_data.set('CONFIG_ATOMIC64', cc.links('''
      return 0;
    }''', args: qemu_isa_flags))
 
-has_int128_type = cc.compiles('''
+# has_int128_type is set to false on Emscripten to avoid errors by libffi
+# during runtime.
+has_int128_type = host_os != 'emscripten' and cc.compiles('''
    __int128_t a;
    __uint128_t b;
    int main(void) { b = a; }''')
@@ -3774,6 +3783,8 @@ if have_block
    # os-win32.c does not
    if host_os == 'windows'
      system_ss.add(files('os-win32.c'))
+  elif host_os == 'emscripten'
+    blockdev_ss.add(files('os-wasm.c'))
    else
      blockdev_ss.add(files('os-posix.c'))
    endif
@@ -4456,7 +4467,11 @@ subdir('scripts')
  subdir('tools')
  subdir('pc-bios')
  subdir('docs')
-subdir('tests')
+# Tests are disabled on emscripten because they rely on host features that aren't
+# supported by emscripten (e.g. fork and unix socket).
+if host_os != 'emscripten'
+  subdir('tests')
+endif
  if gtk.found()
    subdir('po')
  endif
meson_options.txt +0 -0
index 59d973bca0..45772484cc 100644

meson_options.txt option('fuzzing_engine', type : 'string', value : '', · line 38

Quoted 6 lines
meson_options.txt +1 -1
@@ -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', 'wasm', 'auto'],
         value: 'auto', description: 'coroutine backend to use')

Does wasm depend on TCI at this point (no TCG backend)? If so, should we check TCI is enabled? (I wonder if configuring with the --disable-tcg-interpreter option succeed).

#49 lore raw

Re: [PATCH 18/19] gitlab: Enable CI for wasm build

In reply to Kohei Tokunaga
Quoted 4 lines · 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.

Quoted 8 lines
---
  .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

Probably nitpicking, I'd add the Dockerfile first, then the GitLab job in another patch.

#5 lore raw

Re: [PATCH 01/19] hw/core/loader.c: Fix type conflict of GLib function pointers

In reply to Daniel P. Berrangé

Il mer 16 apr 2025, 11:12 Daniel P. Berrangé <[email protected]> ha scritto:

Quoted 8 lines
Quoted 3 lines

I suggested this solution to Kohei because it's easy to check that we're converting all users and not introducing new ones in the future (see poisoning in patch 10).

It is easy to check this /one/ example, but this pattern of bad casts is typical in glib with likely many more examples, so avoidance in this way does not feel like a sustainable long term strategy to me.

If you refer to the general case of function casting then yes, I agree that there could be other problems in the future but that would have to be solved in glib.

However QEMU's use of glib is overall relatively limited and for now this seems to be the only case we hit. What this patch is doing is both effective at solving the immediate issue, and future proof.

Paolo

Quoted 12 lines

With regards,
Daniel
--
|: 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 :|

#27 lore raw

Re: [PATCH 12/19] block: Update block to compile with Emscripten

In reply to Philippe Mathieu-Daudé
Quoted 72 lines · Philippe Mathieu-Daudé
Quoted 58 lines · Kohei Tokunaga

emscripten exposes copy_file_range declaration but doesn't provide the implementation in the final link. Define the emscripten-specific stub function to avoid type conflict with the emscripten's header.

---
  block/file-posix.c |  6 ++++++
  stubs/emscripten.c | 13 +++++++++++++

  2 files changed, 19 insertions(+)

  create mode 100644 stubs/emscripten.c

block/file-posix.c +6 -0
index 56d1972d15..22e0ed5069 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
@@ -2010,6 +2014,7 @@ static int handle_aiocb_write_zeroes_unmap(void 
 *opaque)
       return handle_aiocb_write_zeroes(aiocb);
   }
+#ifndef EMSCRIPTEN
   #ifndef HAVE_COPY_FILE_RANGE
   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 +2028,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
stubs/emscripten.c new file +4 -0
new file mode 100644
index 0000000000..2157d6349b
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include "qemu/osdep.h"
+/*
+ * 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)

stubs/emscripten.c

Quoted 4 lines
stubs/emscripten.c new file +4 -0
+{
+    errno = ENOSYS;
+    return -1;
+}

I'd include in this patch this hunk from patch 17:

-- >8 --

stubs/meson.build +0 -0
index 63392f5e78..4fd4d362f9 100644

stubs/meson.build if have_system or have_user

Quoted 8 lines
stubs/meson.build +4 -1
@@ -89,3 +89,7 @@ if have_system or have_user
    stub_ss.add(files('hotplug-stubs.c'))
    stub_ss.add(files('sysbus.c'))
  endif
+
+if host_os == 'emscripten'
+  stub_ss.add(files('emscripten.c'))
+endif
---

Actually what about checking the symbol presence in meson? Something like (untested):

-- >8 --

meson.build +3 -1
index b18c46d16a2..33185fdf315 100644
@@ -2654,3 +2654,2 @@ config_host_data.set('CONFIG_TIMERFD', cc.has_function('timerfd_create'))
 config_host_data.set('CONFIG_GETLOADAVG', cc.has_function('getloadavg'))
-config_host_data.set('HAVE_COPY_FILE_RANGE', cc.has_function('copy_file_range'))
 config_host_data.set('HAVE_GETIFADDRS', cc.has_function('getifaddrs'))
@@ -2756,2 +2755,6 @@ config_host_data.set('HAVE_UTMPX',
 
+config_host_data.set('HAVE_COPY_FILE_RANGE', cc.links(gnu_source_prefix + '''
+  #include <unistd.h>
+  int main(void) { return copy_file_range(-1, NULL, -1, NULL, 0, 0); }'''))
 config_host_data.set('CONFIG_EVENTFD', cc.links('''

---

#52 lore raw

Re: [PATCH 19/19] MAINTAINERS: Update MAINTAINERS file for wasm-related files

In reply to Kohei Tokunaga
Quoted 10 lines · Kohei Tokunaga
---
  MAINTAINERS | 10 ++++++++++
  1 file changed, 10 insertions(+)
MAINTAINERS +0 -0
index d54b5578f8..74a102c594 100644

MAINTAINERS F: tcg/tci/ · line 3906

Quoted 4 lines
MAINTAINERS +1 -0
@@ -3903,6 +3903,16 @@ F: tcg/tci/
  F: tcg/tci.c
  F: disas/tci.c
 
+WebAssembly TCG target

Just "WebAssembly target"? (I'd add the future tcg/wasm/ files in another MAINTAINERS section).

Maybe better to squash with patch 17? Or start adding the section when you add a new file, then update for each new file added?

Quoted 12 lines
MAINTAINERS +9 -0
+M: Kohei Tokunaga <[email protected]>
+S: Maintained
+F: configs/meson/emscripten.txt
+F: tests/docker/dockerfiles/emsdk-wasm32-cross.docker
+F: util/coroutine-wasm.c
+F: os-wasm.c
+F: include/system/os-wasm.h
+F: stubs/emscripten.c
+
  Block drivers
  -------------
  VMDK
#8 lore raw

Re: [PATCH 01/19] hw/core/loader.c: Fix type conflict of GLib function pointers

In reply to Daniel P. Berrangé
Quoted 27 lines · Daniel P. Berrangé
Quoted 26 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 using g_list_sort_with_data.

---
  hw/core/loader.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/core/loader.c +1 -1
index 2e35f0aa90..93a8b45d28 100644
@@ -1410,7 +1410,7 @@ typedef struct RomSec {
   * work, but this way saves a little work later by avoiding
   * dealing with "gaps" of 0 length.
   */
-static gint sort_secs(gconstpointer a, gconstpointer b)
+static gint sort_secs(gconstpointer a, gconstpointer b, gpointer d)
  {
      RomSec *ra = (RomSec *) a;
      RomSec *rb = (RomSec *) b;
@@ -1463,7 +1463,7 @@ RomGap rom_find_largest_gap_between(hwaddr base, size_t size)
      /* sentinel */
      secs = add_romsec_to_list(secs, base + size, 1);
 
Quoted 4 lines

hw/core/loader.c RomGap rom_find_largest_gap_between(hwaddr base, size_t size) · line 1466

Quoted 2 lines
hw/core/loader.c +1 -1
-    secs = g_list_sort(secs, sort_secs);
+    secs = g_list_sort_with_data(secs, sort_secs, NULL);

I don't see what the problem is with the original code.

IIUC the rationale is in patch #10 where Kohei poisons g_list_sort():

On emscripten, function pointer casts can cause function call failure. g_list_sort and g_slist_sort performs this internally so can't be used on Emscripten. Instead, g_list_sort_with_data and g_slist_sort_with_data should be used.

Quoted 25 lines

The commit message says we have a bad function cast, but the original method decl is

   GList *g_list_sort(GList*list, GCompareFunc compare_func);

where the callback is

    typedef gint (*GCompareFunc)(gconstpointer a, gconstpointer b);

Our code complies with that GCompareFunc signature.

For comparison the new code uses:

   GList *g_list_sort_with_data(GList *list, GCompareDataFunc  compare_func, gpointer user_data);

where the callback is

   typedef gint (*GCompareDataFunc)(gconstpointer a, gconstpointer b, gpointer user_data);

which the new code complies with, but it is undesirable since we have no use for the data parameter.

With regards,
Daniel

#22 lore raw

Re: [PATCH 11/19] util/cacheflush.c: Update cache flushing mechanism for Emscripten

In reply to Kohei Tokunaga

The "why?" isn't clearly described.

Quoted 34 lines · Kohei Tokunaga
---
  include/qemu/cacheflush.h | 3 ++-
  util/cacheflush.c         | 3 ++-
  2 files changed, 4 insertions(+), 2 deletions(-)
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)
  {
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. */
 
#19 lore raw

Re: [PATCH 10/19] include/glib-compat.h: Poison g_list_sort and g_slist_sort

In reply to Kohei Tokunaga
Quoted 15 lines · Kohei Tokunaga

On emscripten, function pointer casts can cause function call failure. g_list_sort and g_slist_sort performs this internally so can't be used on Emscripten. Instead, g_list_sort_with_data and g_slist_sort_with_data should be used.

---
include/glib-compat.h | 6 ++++++

1 file changed, 6 insertions(+)
include/glib-compat.h +0 -0
index 86be439ba0..e441b396ef 100644

include/glib-compat.h line 41

Quoted 6 lines
include/glib-compat.h +3 -0
@@ -36,6 +36,12 @@
 #include <pwd.h>
 #endif
 
+/* These functions perform function pointer casts which can cause function call
+ * failure on Emscripten. Use g_slist_sort_with_data and g_list_sort_with_data
+ * insted of these functions.

Typo: instead

Regards,
BALATON Zoltan

Quoted 7 lines
include/glib-compat.h +3 -0
+ */
+#pragma GCC poison g_slist_sort g_list_sort
+
 /*
 * Note that because of the GLIB_VERSION_MAX_ALLOWED constant above, allowing
 * use of functions from newer GLib via this compat header needs a little
#28 lore raw

Re: [PATCH 12/19] block: Update block to compile with Emscripten

In reply to Philippe Mathieu-Daudé
Quoted 92 lines · Philippe Mathieu-Daudé
Quoted 71 lines · Philippe Mathieu-Daudé
Quoted 48 lines · Kohei Tokunaga

emscripten exposes copy_file_range declaration but doesn't provide the implementation in the final link. Define the emscripten-specific stub function to avoid type conflict with the emscripten's header.

---
  block/file-posix.c |  6 ++++++
  stubs/emscripten.c | 13 +++++++++++++

  2 files changed, 19 insertions(+)

  create mode 100644 stubs/emscripten.c

block/file-posix.c +6 -0
index 56d1972d15..22e0ed5069 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
@@ -2010,6 +2014,7 @@ static int handle_aiocb_write_zeroes_unmap(void *opaque)
       return handle_aiocb_write_zeroes(aiocb);
   }
+#ifndef EMSCRIPTEN
   #ifndef HAVE_COPY_FILE_RANGE
   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 +2028,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
stubs/emscripten.c new file +3 -0
new file mode 100644
index 0000000000..2157d6349b
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include "qemu/osdep.h"
+/*

stubs/emscripten.c line 13

Quoted 10 lines
stubs/emscripten.c new file +10 -0
+ * 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;
+}

I'd include in this patch this hunk from patch 17:

-- >8 --

stubs/meson.build +0 -0
index 63392f5e78..4fd4d362f9 100644
@@ -89,3 +89,7 @@ if have_system or have_user
     stub_ss.add(files('hotplug-stubs.c'))
     stub_ss.add(files('sysbus.c'))

  endif

stubs/meson.build

Quoted 5 lines
stubs/meson.build +4 -1
+
+if host_os == 'emscripten'
+  stub_ss.add(files('emscripten.c'))
+endif
---

Actually what about checking the symbol presence in meson? Something like (untested):

-- >8 --

meson.build +0 -1
index b18c46d16a2..33185fdf315 100644
@@ -2654,3 +2654,2 @@ config_host_data.set('CONFIG_TIMERFD', 
 cc.has_function('timerfd_create'))
  config_host_data.set('CONFIG_GETLOADAVG', cc.has_function('getloadavg'))
-config_host_data.set('HAVE_COPY_FILE_RANGE', cc.has_function('copy_file_range'))

 config_host_data.set('HAVE_GETIFADDRS', cc.has_function('getifaddrs'))

meson.build config_host_data.set('HAVE_UTMPX',

Quoted 6 lines
meson.build +3 -1
@@ -2756,2 +2755,6 @@ config_host_data.set('HAVE_UTMPX',
 
+config_host_data.set('HAVE_COPY_FILE_RANGE', cc.links(gnu_source_prefix + '''
+  #include <unistd.h>
+  int main(void) { return copy_file_range(-1, NULL, -1, NULL, 0, 0); }'''))
  config_host_data.set('CONFIG_EVENTFD', cc.links('''
---

Yes indeed. We should be making sure HAVE_COPY_FILE_RANGE is unset, not providing stubs.

r~

#9 lore raw

Re: [PATCH 01/19] hw/core/loader.c: Fix type conflict of GLib function pointers

In reply to Philippe Mathieu-Daudé

Hi Daniel and Philippe,

Quoted 9 lines
Quoted 9 lines
Quoted 9 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 using g_list_sort_with_data.

---
  hw/core/loader.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
hw/core/loader.c +0 -0
index 2e35f0aa90..93a8b45d28 100644
@@ -1410,7 +1410,7 @@ typedef struct RomSec {
   * work, but this way saves a little work later by avoiding
Quoted 8 lines

hw/core/loader.c RomGap rom_find_largest_gap_between(hwaddr base,

Quoted 8 lines
hw/core/loader.c +1 -1
   * dealing with "gaps" of 0 length.
   */
-static gint sort_secs(gconstpointer a, gconstpointer b)
+static gint sort_secs(gconstpointer a, gconstpointer b, gpointer d)
  {
      RomSec *ra = (RomSec *) a;
      RomSec *rb = (RomSec *) b;
@@ -1463,7 +1463,7 @@ RomGap rom_find_largest_gap_between(hwaddr base,

size_t size)

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
hw/core/loader.c +1 -1
      /* sentinel */
      secs = add_romsec_to_list(secs, base + size, 1);
 
-    secs = g_list_sort(secs, sort_secs);
+    secs = g_list_sort_with_data(secs, sort_secs, NULL);
Quoted 8 lines
Quoted 1 line

I don't see what the problem is with the original code.

IIUC the rationale is in patch #10 where Kohei poisons g_list_sort():

On emscripten, function pointer casts can cause function call failure. g_list_sort and g_slist_sort performs this internally so can't be used on Emscripten. Instead, g_list_sort_with_data and g_slist_sort_with_data should be used.

Thank you for the feedback. As Philippe pointed out, the rationale is explained in the patch #10. I'll make sure to include the explanation in the commit message of this patch as well.

#6 lore raw

Re: [PATCH 01/19] hw/core/loader.c: Fix type conflict of GLib function pointers

In reply to Paolo Bonzini

Hi Daniel, Paolo and Thomas,

Quoted 1 line
Quoted 1 line
Quoted 1 line

I suggested this solution to Kohei because it's easy to check that

we're

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

converting all users and not introducing new ones in the future (see poisoning in patch 10).

It is easy to check this /one/ example, but this pattern of bad casts is typical in glib with likely many more examples, so avoidance in this way does not feel like a sustainable long term strategy to me.

If you refer to the general case of function casting then yes, I agree

that there could be other problems in the future but that would have to be solved in glib.

Quoted 2 lines

However QEMU's use of glib is overall relatively limited and for now this

seems to be the only case we hit. What this patch is doing is both effective at solving the immediate issue, and future proof.

The current patch addresses the cases we've encountered so far, and the poisoning will help catch the usage of them in future development.

That said, it's worth continuing to explore a more robust solution. I've submitted an issue to the GLib repository to investigate a long-term fix[1].

[1] https://gitlab.gnome.org/GNOME/glib/-/issues/3670

#20 lore raw

Re: [PATCH 10/19] include/glib-compat.h: Poison g_list_sort and g_slist_sort

In reply to BALATON Zoltan

Hi Balaton,

Quoted 1 line

Typo: instead

Thank you for the feedback. I'll fix this in the next version of the series.

#23 lore raw

Re: [PATCH 11/19] util/cacheflush.c: Update cache flushing mechanism for Emscripten

In reply to Philippe Mathieu-Daudé

Hi Philippe,

Quoted 1 line

The "why?" isn't clearly described.

Although __builtin___clear_cache is used to flush the instruction cache for a specified memory region[1], this operation doesn't apply to wasm, as its memory isn't executable. Moreover, Emscripten does not support this builtin and fails to compile it with the following error.

Quoted 1 line

fatal error: error in backend: llvm.clear_cache is not supported on wasm

To resolve this, I've removed the call to __builtin___clear_cache.

I'll update this patch to include an explicit "#elif" branch with an explanation, like the following:

+#elif defined(EMSCRIPTEN)
+
+/* Wasm does not have an executable memory region. */ +

Please let me know if I'm missing something or if there is a preferred way to handle this case.

[1]
https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005f_005f_005fclear_005fcache

#29 lore raw

Re: [PATCH 12/19] block: Update block to compile with Emscripten

In reply to Philippe Mathieu-Daudé

Hi Philippe and Richard, thank you for the feedback.

Quoted 15 lines

Actually what about checking the symbol presence in meson? Something like (untested):

-- >8 --

meson.build +0 -1
index b18c46d16a2..33185fdf315 100644
@@ -2654,3 +2654,2 @@ config_host_data.set('CONFIG_TIMERFD',
 cc.has_function('timerfd_create'))
  config_host_data.set('CONFIG_GETLOADAVG', cc.has_function('getloadavg'))
-config_host_data.set('HAVE_COPY_FILE_RANGE',

cc.has_function('copy_file_range'))
config_host_data.set('HAVE_GETIFADDRS', cc.has_function('getifaddrs'))

meson.build config_host_data.set('HAVE_UTMPX', · line 2759

Quoted 5 lines
meson.build +4 -0
@@ -2756,2 +2755,6 @@ config_host_data.set('HAVE_UTMPX',
 
+config_host_data.set('HAVE_COPY_FILE_RANGE', cc.links(gnu_source_prefix
+ '''
+  #include <unistd.h>
+  int main(void) { return copy_file_range(-1, NULL, -1, NULL, 0, 0);

}'''))

meson.build config_host_data.set('HAVE_UTMPX',

Quoted 2 lines
meson.build +0 -1
  config_host_data.set('CONFIG_EVENTFD', cc.links('''
---

Emscripten doesn't provide copy_file_range implementation but it declares this function in its headers. Meson correctly detects the missing implementation and unsets HAVE_COPY_FILE_RANGE. However, the stub defined in file-posix.c causes a type conflict with the declaration from Emscripten during compilation:

Quoted 1 line

../qemu/block/file-posix.c:2019:14: error: static declaration of

'copy_file_range' follows non-static declaration

Quoted 3 lines

2019 | static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
| ^
/emsdk/upstream/emscripten/cache/sysroot/include/unistd.h:207:9: note:

previous declaration is here

Quoted 1 line

207 | ssize_t copy_file_range(int, off_t *, int, off_t *, size_t,

unsigned);

Quoted 2 lines

| ^
1 error generated.

If introducing a new stub isn't preferable, we could update the existing stub in file-posix.c to match the declaration used by Emscripten. The manpage[1] also aligns with this signature.

[1] https://man7.org/linux/man-pages/man2/copy_file_range.2.html

#39 lore raw

Re: [PATCH 14/19] Disable options unsupported on Emscripten

In reply to Philippe Mathieu-Daudé

Hi Philippe,

Quoted 6 lines
Quoted 2 lines

#ifndef _WIN32
+#ifndef EMSCRIPTEN

#if !defined(_WIN32) && !defined(EMSCRIPTEN)

?

Thank you for the sueggestion. I'll apply this in the next version of the series.

#42 lore raw

Re: [PATCH 15/19] util/mmap-alloc: Add qemu_ram_mmap implementation for emscripten

In reply to Philippe Mathieu-Daudé

Hi Philippe,

Quoted 2 lines

Can we keep this code generic? I.e. with something in the lines of (only build-tested):

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

#47 lore raw

Re: [PATCH 17/19] meson: Add wasm build in build scripts

In reply to Philippe Mathieu-Daudé

Hi Philippe,

Quoted 3 lines

Does wasm depend on TCI at this point (no TCG backend)? If so, should we check TCI is enabled? (I wonder if configuring with the --disable-tcg-interpreter option succeed).

Yes, it depends on TCI at this point. Configuring with the --disable-tcg-interpreter succeeds but leads to a build error. I'll update the patch to include a check to ensure TCI is enabled.

#50 lore raw

Re: [PATCH 18/19] gitlab: Enable CI for wasm build

In reply to Philippe Mathieu-Daudé

Hi Philippe,

Quoted 2 lines

Probably nitpicking, I'd add the Dockerfile first, then the GitLab job in another patch.

Thank you for the suggestion. I'll split the patch in the next version.

#53 lore raw

Re: [PATCH 19/19] MAINTAINERS: Update MAINTAINERS file for wasm-related files

In reply to Philippe Mathieu-Daudé

Hi Philippe,

Quoted 6 lines

Just "WebAssembly target"? (I'd add the future tcg/wasm/ files in another MAINTAINERS section).

Maybe better to squash with patch 17? Or start adding the section when you add a new file, then update for each new file added?

Sure, I'll update the patch to follow this approach.

#33 lore raw

Re: [PATCH 12/19] block: Update block to compile with Emscripten

In reply to Philippe Mathieu-Daudé

Hi Philippe,

Quoted 8 lines

I'd include in this patch this hunk from patch 17:

-- >8 --

stubs/meson.build +0 -0
index 63392f5e78..4fd4d362f9 100644

stubs/meson.build if have_system or have_user

Quoted 8 lines
stubs/meson.build +4 -1
@@ -89,3 +89,7 @@ if have_system or have_user
    stub_ss.add(files('hotplug-stubs.c'))
    stub_ss.add(files('sysbus.c'))
  endif
+
+if host_os == 'emscripten'
+  stub_ss.add(files('emscripten.c'))
+endif
---

Sure, I'll move that hunk into this patch.

#24 lore raw

Re: [PATCH 11/19] util/cacheflush.c: Update cache flushing mechanism for Emscripten

In reply to Kohei Tokunaga
Quoted 4 lines · Kohei Tokunaga

Hi Philippe,

Quoted 1 line

The "why?" isn't clearly described.

I'm happy with the following ...

Quoted 8 lines

Although __builtin___clear_cache is used to flush the instruction cache for a specified memory region[1], this operation doesn't apply to wasm, as its memory isn't executable. Moreover, Emscripten does not support this builtin and fails to compile it with the following error.

Quoted 1 line

fatal error: error in backend: llvm.clear_cache is not supported on wasm

To resolve this, I've removed the call to __builtin___clear_cache.

... used as commit description,

Review

Quoted 8 lines

I'll update this patch to include an explicit "#elif" branch with an explanation, like the following:

(quoted diff) +4 -0
+#elif defined(EMSCRIPTEN)
+
+/* Wasm does not have an executable memory region. */
+

and this comment in the code. Thanks!

Quoted 6 lines

Please let me know if I'm missing something or if there is a preferred way to handle this case.

[1] https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Other- Builtins.html#index-_005f_005fbuiltin_005f_005f_005fclear_005fcache <https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Other- Builtins.html#index-_005f_005fbuiltin_005f_005f_005fclear_005fcache>

#30 lore raw

Re: [PATCH 12/19] block: Update block to compile with Emscripten

In reply to Kohei Tokunaga
Quoted 41 lines · Kohei Tokunaga

Hi Philippe and Richard, thank you for the feedback.

meson.build config_host_data.set('CONFIG_TIMERFD', · line 2655

Quoted 11 lines

Actually what about checking the symbol presence in meson? Something like (untested):

-- >8 --

meson.build +0 -0
index b18c46d16a2..33185fdf315 100644
@@ -2654,3 +2654,2 @@ config_host_data.set('CONFIG_TIMERFD',
 cc.has_function('timerfd_create'))
   config_host_data.set('CONFIG_GETLOADAVG', 

cc.has_function('getloadavg'))

meson.build config_host_data.set('HAVE_UTMPX', · line 2759

Quoted 9 lines
meson.build +0 -1
-config_host_data.set('HAVE_COPY_FILE_RANGE',

cc.has_function('copy_file_range')) config_host_data.set('HAVE_GETIFADDRS', cc.has_function('getifaddrs'))

meson.build +4 -0
@@ -2756,2 +2755,6 @@ config_host_data.set('HAVE_UTMPX',
 
+config_host_data.set('HAVE_COPY_FILE_RANGE', cc.links(gnu_source_prefix
+ '''
+  #include <unistd.h>
+  int main(void) { return copy_file_range(-1, NULL, -1, NULL, 0, 

0); }'''))

Quoted 2 lines

  config_host_data.set('CONFIG_EVENTFD', cc.links('''
---

Emscripten doesn't provide copy_file_range implementation but it declares this function in its headers. Meson correctly detects the missing implementation and unsets HAVE_COPY_FILE_RANGE. However, the stub defined in file-posix.c causes a type conflict with the declaration from Emscripten during compilation:

Quoted 1 line

../qemu/block/file-posix.c:2019:14: error: static declaration of

'copy_file_range' follows non-static declaration

Quoted 1 line

 2019 | static off_t copy_file_range(int in_fd, off_t *in_off, int

out_fd,

Quoted 2 lines

      |              ^ /emsdk/upstream/emscripten/cache/sysroot/include/unistd.h:207:9:

note: previous declaration is here

Quoted 8 lines
Quoted 1 line

  207 | ssize_t copy_file_range(int, off_t *, int, off_t *, size_t,

unsigned);

Quoted 2 lines

      |         ^
1 error generated.

If introducing a new stub isn't preferable, we could update the existing stub in file-posix.c to match the declaration used by Emscripten. The manpage[1] also aligns with this signature.

You are correct we should use ssize_t instead of off_t.

If meson fails to link, it won't define HAVE_COPY_FILE_RANGE, so you
shouldn't get "static declaration of 'copy_file_range' follows non-static declaration" right?

Quoted 3 lines

[1] https://man7.org/linux/man-pages/man2/copy_file_range.2.html <https://man7.org/linux/man-pages/man2/copy_file_range.2.html>

#35 lore raw

Re: [PATCH 13/19] include/qemu/osdep.h: Add Emscripten-specific OS dependencies

In reply to Kohei Tokunaga

Hi Kohei,

Quoted 21 lines · Kohei Tokunaga

On emscripten, some implementations in os-posix.c can't be used such as daemonizing and changing user. This commit introduces os-wasm.c and os-wasm.h which are forked from os-posix.c and os-posix.h and patched for targetting Emscripten.

---
  include/qemu/osdep.h      |   8 ++-
  include/system/os-posix.h |   3 +-
  include/system/os-wasm.h  | 105 +++++++++++++++++++++++++++++++++
  os-wasm.c                 | 119 ++++++++++++++++++++++++++++++++++++++
  4 files changed, 232 insertions(+), 3 deletions(-)

create mode 100644 include/system/os-wasm.h
create mode 100644 os-wasm.c

include/system/os-posix.h +0 -0
index ce5b3bccf8..e87cc36a3a 100644

include/system/os-posix.h line 3

Quoted 4 lines
include/system/os-posix.h +2 -1
@@ -1,5 +1,6 @@
  /*
- * posix specific declarations
+ * posix specific declarations forked from os-posix.c, removing functions not
+ * working on Emscripten

You shouldn't have to modify this file, likely a rebase typo.

Quoted 3 lines
include/system/os-posix.h +0 -0
   *
   * Copyright (c) 2003-2008 Fabrice Bellard
   * Copyright (c) 2010 Jes Sorensen <[email protected]>
#31 lore raw

Re: [PATCH 12/19] block: Update block to compile with Emscripten

In reply to Philippe Mathieu-Daudé

Hi Philippe,

Quoted 1 line

If meson fails to link, it won't define HAVE_COPY_FILE_RANGE,

Yes, meson correctly detects the link failure when checking for copy_file_range, as shown in meson-log.txt:

Quoted 1 line

wasm-ld: error: /tmp/emscripten_temp_oqvz296m/testfile_0.o: undefined

symbol: copy_file_range

and reflects this in the configure output:

Quoted 5 lines

Checking for function "copy_file_range" : NO

so you
shouldn't get "static declaration of 'copy_file_range' follows non-static declaration" right?

To fix this error, I needed to update the stub implementation in file-posix.c to exactly match the declaration in Emscripten's headers. Specifically, I changed the return type from off_t to ssize_t and removed the "static" qualifier. With this change, file-posix.c builds correctly under Emscripten, and there is no need to add a new stub in stubs/emscripten.c

The following is the patch updates file-posix.c to solve this error:

#ifndef HAVE_COPY_FILE_RANGE
-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)
+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)
{
#ifdef __NR_copy_file_range
return syscall(__NR_copy_file_range, in_fd, in_off, out_fd,

#36 lore raw

Re: [PATCH 13/19] include/qemu/osdep.h: Add Emscripten-specific OS dependencies

In reply to Philippe Mathieu-Daudé

Hi Philippe,

Quoted 1 line

You shouldn't have to modify this file, likely a rebase typo.

Thank you for pointing this out. I'll fix this in the next version of the series.

#32 lore raw

Re: [PATCH 12/19] block: Update block to compile with Emscripten

In reply to Kohei Tokunaga
Quoted 13 lines · Kohei Tokunaga

The following is the patch updates file-posix.c to solve this error:

 #ifndef HAVE_COPY_FILE_RANGE
-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)
+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)
 {
 #ifdef __NR_copy_file_range
     return syscall(__NR_copy_file_range, in_fd, in_off, out_fd,

Yes, LGTM!

#43 lore raw

Re: [PATCH 15/19] util/mmap-alloc: Add qemu_ram_mmap implementation for emscripten

In reply to Kohei Tokunaga

Hi Philippe,

While working on mmap-alloc.c, I found that Emscripten does not support partial unmapping of memory regions [1]. This limitation prevents correct implementation of qemu_ram_mmap and qemu_ram_munmap, which rely on partial unmap behavior.

[1]
https://github.com/emscripten-core/emscripten/blob/d4a74336f23214bf3304d9eb0d03966786b30a36/system/lib/libc/emscripten_mmap.c#L61

As a workaround, I'm considering excluding mmap-alloc.c from the Emscripten build. Instead, for Emscripten build, we can modify qemu_anon_ram_alloc (in oslib-posix.c) to use qemu_memalign in place of qemu_ram_mmap, and disable memory backends that rely on mmap, such as memory-backend-file and memory-backend-shm.

I plan to include this fix in the next version of the patch series. I'm happy to hear any suggestions if there is a better way to address this limitation.

The change in oslib-posix.c will look like this:

util/oslib-posix.c +1 -0
index a697c602c6..8eb0e53458 100644
@@ -58,6 +58,7 @@
 #include <lwp.h>
 #endif
 
+#include "qemu/memalign.h"
 #include "qemu/mmap-alloc.h"
 
 #define MAX_MEM_PREALLOC_THREAD_COUNT 16
@@ -210,11 +211,21 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t

*alignment, bool shared,

     const uint32_t qemu_map_flags = (shared ? QEMU_MAP_SHARED : 0) |
                                     (noreserve ? QEMU_MAP_NORESERVE : 0);
     size_t align = QEMU_VMALLOC_ALIGN;
+#ifndef EMSCRIPTEN
     void *ptr = qemu_ram_mmap(-1, size, align, qemu_map_flags, 0);

     if (ptr == MAP_FAILED) {
         return NULL;
     }

+#else
+ /*
+ * qemu_ram_mmap is not implemented for Emscripten. Use qemu_memalign
+ * for the anonymous allocation. noreserve is ignored as there is no
swap

+     * space on Emscripten, and shared is ignored as there is no other
+     * processes on Emscripten.
+     */

+ void *ptr = qemu_memalign(align, size);
+#endif

     if (alignment) {
         *alignment = align;
@@ -227,7 +238,16 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t
*alignment, bool shared,
 void qemu_anon_ram_free(void *ptr, size_t size)
 {
     trace_qemu_anon_ram_free(ptr, size);
+#ifndef EMSCRIPTEN
     qemu_ram_munmap(-1, ptr, size);
+#else
+    /*
+     * qemu_ram_munmap is not implemented for Emscripten and qemu_memalign
+     * was used for the allocation. Use the corresponding freeing function
+     * here.
+     */

+ qemu_vfree(ptr);
+#endif
}

void qemu_socket_set_block(int fd)
@@ -588,7 +608,15 @@ bool qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,

 {
     static gsize initialized;
     int ret;
+#ifndef EMSCRIPTEN
     size_t hpagesize = qemu_fd_getpagesize(fd);

+#else
+ /*
+ * mmap-alloc.c is excluded from Emscripten build, so
qemu_fd_getpagesize
+ * is unavailable. Fallback to the lower level implementation.
+ */
+ size_t hpagesize = qemu_real_host_page_size();
+#endif

     size_t numpages = DIV_ROUND_UP(sz, hpagesize);
     bool use_madv_populate_write;
     struct sigaction act;