qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kohei Tokunaga <ktokunaga.mail@gmail.com>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Thomas Huth" <thuth@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Daniel P . Berrangé" <berrange@redhat.com>,
	"WANG Xuerui" <git@xen0n.name>,
	"Aurelien Jarno" <aurelien@aurel32.net>,
	"Huacai Chen" <chenhuacai@kernel.org>,
	"Jiaxun Yang" <jiaxun.yang@flygoat.com>,
	"Aleksandar Rikalo" <arikalo@gmail.com>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Alistair Francis" <Alistair.Francis@wdc.com>,
	"Stefan Weil" <sw@weilnetz.de>,
	qemu-arm@nongnu.org, qemu-riscv@nongnu.org,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Pierrick Bouvier" <pierrick.bouvier@linaro.org>,
	ktokunaga.mail@gmail.com
Subject: [PATCH v3 28/35] tcg/wasm: Implement instantiation of Wasm binary
Date: Mon,  1 Sep 2025 20:44:30 +0900	[thread overview]
Message-ID: <c0d05186cd431515eb3f97a7c860ec5b6deac0a5.1756724464.git.ktokunaga.mail@gmail.com> (raw)
In-Reply-To: <cover.1756724464.git.ktokunaga.mail@gmail.com>

instantiate_wasm is a function that instantiates a TB's Wasm binary,
importing the functions as specified by its arguments. Following the header
definition in wasm/tcg-target.c.inc, QEMU's memory is imported into the
module as "env.memory", and helper functions are imported as "helper.<idx>".

The instantiated Wasm module is imported to QEMU using Emscripten's
"addFunction" feature[1] which returns a function pointer. This allows QEMU
to call this module directly from C code via that pointer.

Since the subarray() method doesn't accept a BigInt value which is used for
the 64bit pointer value, it is converted to a Number (i53) using
bigintToI53Checked method of Emscripten. Although this conversion (64bit to
53bit) drops higher bits, the maximum memory size of the engine
implementations is currently limited to 16GiB[2] so we can assume that the
pointers are within the Number's range.

Note that since FireFox 138, WebAssembly.Module no longer accepts a
SharedArrayBuffer as input [3] as reported by Nicolas Vandeginste in my
fork[4]. This commit ensures that WebAssembly.Module() is passed a
Uint8Array created from the binary data on a SharedArrayBuffer.

[1] https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#calling-javascript-functions-as-function-pointers-from-c
[2] https://webassembly.github.io/memory64/js-api/#limits
[3] https://bugzilla.mozilla.org/show_bug.cgi?id=1965217
[4] https://github.com/ktock/qemu-wasm/pull/25

Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
---
 tcg/wasm.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/tcg/wasm.c b/tcg/wasm.c
index 1cc2e45e77..15db1f9a8a 100644
--- a/tcg/wasm.c
+++ b/tcg/wasm.c
@@ -23,6 +23,43 @@
 #include "tcg/tcg-ldst.h"
 #include "tcg/helper-info.h"
 #include <ffi.h>
+#include <emscripten.h>
+
+#define EM_JS_PRE(ret, name, args, body...) EM_JS(ret, name, args, body)
+
+#define DEC_PTR(p) bigintToI53Checked(p)
+#define ENC_PTR(p) BigInt(p)
+#if defined(WASM64_MEMORY64_2)
+#define ENC_WASM_TABLE_IDX(i) Number(i)
+#else
+#define ENC_WASM_TABLE_IDX(i) i
+#endif
+
+EM_JS_PRE(void*, instantiate_wasm, (void *wasm_begin,
+                                    int wasm_size,
+                                    void *import_vec_begin,
+                                    int import_vec_size),
+{
+    const memory_v = new DataView(HEAP8.buffer);
+    const wasm = HEAP8.subarray(DEC_PTR(wasm_begin),
+                                DEC_PTR(wasm_begin) + wasm_size);
+    var helper = {};
+    const entsize = TCG_TARGET_REG_BITS / 8;
+    for (var i = 0; i < import_vec_size / entsize; i++) {
+        const idx = memory_v.getBigInt64(
+            DEC_PTR(import_vec_begin) + i * entsize, true);
+        helper[i] = wasmTable.get(ENC_WASM_TABLE_IDX(idx));
+    }
+    const mod = new WebAssembly.Module(new Uint8Array(wasm));
+    const inst = new WebAssembly.Instance(mod, {
+            "env" : {
+                "memory" : wasmMemory,
+            },
+            "helper" : helper,
+    });
+
+    return ENC_PTR(addFunction(inst.exports.start, 'ii'));
+});
 
 __thread uintptr_t tci_tb_ptr;
 
-- 
2.43.0



  parent reply	other threads:[~2025-09-01 11:53 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-01 11:44 [PATCH v3 00/35] wasm: Add Wasm TCG backend based on wasm64 Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 01/35] meson: Add wasm64 support to the --cpu flag Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 02/35] configure: Enable to propagate -sMEMORY64 flag to Emscripten Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 03/35] dockerfiles: Add support for wasm64 to the wasm Dockerfile Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 04/35] .gitlab-ci.d: Add build tests for wasm64 Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 05/35] tcg/wasm: Add tcg-target.h and tcg-target-reg-bits.h Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 06/35] tcg/wasm: Add register-related definitions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 07/35] tcg/wasm: Add constraint definitions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 08/35] tcg/wasm: Add relocation callbacks Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 09/35] tcg/wasm: Add and/or/xor instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 10/35] tcg/wasm: Add add/sub/mul instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 11/35] tcg/wasm: Add shl/shr/sar instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 12/35] tcg/wasm: Add setcond/negsetcond/movcond instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 13/35] tcg/wasm: Add sextract instruction Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 14/35] tcg/wasm: Add load and store instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 15/35] tcg/wasm: Add mov/movi instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 16/35] tcg/wasm: Add ext instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 17/35] tcg/wasm: Add div/rem instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 18/35] tcg/wasm: Add neg/ctpop instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 19/35] tcg/wasm: Add rot/clz/ctz instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 20/35] tcg/wasm: Add br/brcond instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 21/35] tcg/wasm: Add exit_tb/goto_tb/goto_ptr instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 22/35] tcg/wasm: Add call instruction Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 23/35] tcg/wasm: Add qemu_ld/qemu_st instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 24/35] tcg/wasm: Add mb instruction Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 25/35] tcg/wasm: Mark unimplemented instructions Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 26/35] tcg/wasm: Add initialization of fundamental registers Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 27/35] tcg/wasm: Write wasm binary to TB Kohei Tokunaga
2025-09-01 11:44 ` Kohei Tokunaga [this message]
2025-09-01 11:44 ` [PATCH v3 29/35] tcg/wasm: Allow switching coroutine from a helper Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 30/35] tcg/wasm: Enable instantiation of TBs executed many times Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 31/35] tcg/wasm: Enable TLB lookup Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 32/35] tcg/wasm: Add tcg_target_init function Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 33/35] meson.build: enable to build Wasm backend Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 34/35] meson.build: Propagate optimization flag for linking on Emscripten Kohei Tokunaga
2025-09-01 11:44 ` [PATCH v3 35/35] .gitlab-ci.d: build wasm backend in CI Kohei Tokunaga
2025-09-29 22:20 ` [PATCH v3 00/35] wasm: Add Wasm TCG backend based on wasm64 Pierrick Bouvier
2025-09-30  8:26   ` Kohei Tokunaga
2025-10-01 23:08     ` Pierrick Bouvier
2025-10-02  1:33   ` Pierrick Bouvier
2025-10-02 15:59     ` Kohei Tokunaga

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c0d05186cd431515eb3f97a7c860ec5b6deac0a5.1756724464.git.ktokunaga.mail@gmail.com \
    --to=ktokunaga.mail@gmail.com \
    --cc=Alistair.Francis@wdc.com \
    --cc=alex.bennee@linaro.org \
    --cc=arikalo@gmail.com \
    --cc=aurelien@aurel32.net \
    --cc=berrange@redhat.com \
    --cc=chenhuacai@kernel.org \
    --cc=git@xen0n.name \
    --cc=jiaxun.yang@flygoat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=palmer@dabbelt.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@linaro.org \
    --cc=pierrick.bouvier@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=stefanha@redhat.com \
    --cc=sw@weilnetz.de \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).