From nobody Mon Feb 9 01:50:59 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1769412094; cv=none; d=zohomail.com; s=zohoarc; b=ZlRj9KJUHruAjyVl8uJAa4AZa8zYtvna2JYFRnIr97FmR8vovxDpFiJ0S12esOc7b1Y6oUa3cLTLcOGwwMfUZaWr7Wlq1A0d8a+N3zuN8L7ahVorZflR3hPvM3B10oIQEapkIjejJpX/3VaVb1865sdzgldpl61xihsnSVMWk+8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1769412094; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QBiuZHC2ujyOFipmh/6NFwPTzHzb15N2Yq98z1rMyKY=; b=Ri6wjN7n0hNrM2bJKGZ7vsIKqLnc3A7s2gE+9oBm9BZJJ4lgBLitR/EbCww/y/d+x0dnkGMTnUyrN2rscwUnUoAAye0HSqru+cU5ghT/2Ym48BPELg8fEe00sZt7hsHGxHJ/je9LUFUqWnoivnILzOML6C1LNE5RXSz2iScJZKY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1769412094738167.86906766671814; Sun, 25 Jan 2026 23:21:34 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vkGtv-0006Ba-FJ; Mon, 26 Jan 2026 02:20:46 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vkGg4-00039d-CQ for qemu-devel@nongnu.org; Mon, 26 Jan 2026 02:06:33 -0500 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vkGfu-0004R3-Bl for qemu-devel@nongnu.org; Mon, 26 Jan 2026 02:06:20 -0500 Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-c636487ccaeso843629a12.1 for ; Sun, 25 Jan 2026 23:05:56 -0800 (PST) Received: from brahms.. (fp93c00990.tkyc601.ap.nuro.jp. [147.192.9.144]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-823187716ebsm8661487b3a.66.2026.01.25.23.05.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jan 2026 23:05:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769411155; x=1770015955; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QBiuZHC2ujyOFipmh/6NFwPTzHzb15N2Yq98z1rMyKY=; b=GiIJEj9B7HMoskYSCrhOFVzSDCmqGrUD1s0hQJ4QxJ5CkldaUFG0Qsky5LP7o0giya Lwv9jsX/nC72hdN8EmFI7FC9t5E4lb/YTPNf5eD9GmPHS60/FtPFLlS/uV9OuMxkFlQ/ xzvu5THNfd2ukZt5Bz+GzWJEUZTHJWdvwE4mLgIjFF/iEgW0g1pCY7wsEf4fQrBUNlWa nnzu289V9aQoCnjaNypv+zu4SZr9XeH4oh8EyAPEdEeJa2YtV4O6B2PLEQTaZ+UqBqMH /7EDYIfK1ub7V/WK9nD33th8zl4mZXtS0+dogvp9kqu7eaQsZnxsboUIlyIcZBDo59Wx wo0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769411155; x=1770015955; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=QBiuZHC2ujyOFipmh/6NFwPTzHzb15N2Yq98z1rMyKY=; b=mWsx0q2hCqb684Ej4no2v3XYnfNK1v1Y/KbZ7dLtDd6+WAcKesX7opk5O4gKt1UVmZ Ih4IIzYIW56LfUXOy9h7EyvoHiVmM8CgToBWNxC5f2Fhn1g9ZQGC0ynMAmlqzBXNN634 Ik84USTcbuKc38ecQqtg2oZ2pBbQZ0FOs7PFZ7RTFL+Xp4DI5NaRv0ZEGya3YYdibgTt n14XS1tEZrELXFxjEGiOa2bw75NLSOQR0geXnuK7C1e2tAcGEXsvwPGQdFAUqAykkdj/ POkeAYoXoOi2kRm06FvU1OQSvfsNruEA9OndSXbZTLvuxrdjgEcbm7EuSyJOmb0WYnEV oLYA== X-Gm-Message-State: AOJu0YwG18Lk+06J1fWuYgiCCHV1bxytehIYZbwY4ustxqaHkuiKwK+x Z9/ra1utp6tXsLXZfD3XptTlA46bzfyiCo+cdOZIE+ZQlqFb6Z6+SBV9NNor0eUo X-Gm-Gg: AZuq6aITd7Vl3nCPt/4KaTxqMNaNtenAzfwfLWZkrDl5+i9ZI8kdntSkGKuqPuhQ2/x cjo87FY/NizTkaDZ76BQXTCpOP56zNea7vV3ixzq9xyL3WoPMQT10rtHZhp+qlrsldZfaX0Zo+a mScZb4MvSFrf4eavKqeDo80teS08HXTL/JEKYLHnQLXC9KLAtIG9ppEQoITiGSAM4W1JeMNVV9O XJYDP9xB5IJpLTUOSwj5quRIafqMDrzU/pAT4p16cFOHkrP9pPLlNwt8kflfN4URSqzMlkKB8/h 4rAzN8b+NZobDk8Y+ABPn8F3Fm5cJFtWuymtETge0H0q+V1962SAq+XTXsd/AF6BeAahgSx3SCF F3LZJIpTZgt2t1TBmznsiSLrhDeFjsdgXHZARYc0PSDTG8m3TnPAAo0We7b41Z2X6EhJf6V/ugD eSopeRnYBCz0JaTG59ohYgq9Gm5vrUmx0QMZo2+3zpEA== X-Received: by 2002:a17:90b:6c3:b0:34a:9d9a:3f67 with SMTP id 98e67ed59e1d1-353c41a69e3mr3084073a91.33.1769411155173; Sun, 25 Jan 2026 23:05:55 -0800 (PST) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Paolo Bonzini , Richard Henderson , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , WANG Xuerui , Aurelien Jarno , Huacai Chen , Jiaxun Yang , Aleksandar Rikalo , Palmer Dabbelt , Alistair Francis , Stefan Weil , Kohei Tokunaga , qemu-arm@nongnu.org, qemu-riscv@nongnu.org, Stefan Hajnoczi , Pierrick Bouvier Subject: [PATCH v4 24/33] tcg/wasm64: Implement instantiation of Wasm binary Date: Mon, 26 Jan 2026 07:03:37 +0000 Message-ID: <00b8d87b3e0acf00b140a3f60f91c84eeae95b3b.1769407033.git.ktokunaga.mail@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::535; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pg1-x535.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1769412110230158500 Content-Type: text/plain; charset="utf-8" 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 wasm64/tcg-target.c.inc, QEMU's memory is imported into the module as "env.memory", and helper functions are imported as "helper.". 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/Inter= acting-with-code.html#calling-javascript-functions-as-function-pointers-fro= m-c [2] https://webassembly.github.io/memory64/js-api/#limits [3] https://bugzilla.mozilla.org/show_bug.cgi?id=3D1965217 [4] https://github.com/ktock/qemu-wasm/pull/25 Signed-off-by: Kohei Tokunaga --- tcg/wasm64.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tcg/wasm64.c b/tcg/wasm64.c index 895ae062ee..41898ff867 100644 --- a/tcg/wasm64.c +++ b/tcg/wasm64.c @@ -23,6 +23,42 @@ #include "tcg/tcg-ldst.h" #include "tcg/helper-info.h" #include +#include + +#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 =3D new DataView(HEAP8.buffer); + const wasm =3D HEAP8.subarray(DEC_PTR(wasm_begin), + DEC_PTR(wasm_begin) + wasm_size); + var helper =3D {}; + for (var i =3D 0; i < import_vec_size / 8; i++) { + const idx =3D memory_v.getBigInt64( + DEC_PTR(import_vec_begin) + i * 8, true); + helper[i] =3D wasmTable.get(ENC_WASM_TABLE_IDX(idx)); + } + const mod =3D new WebAssembly.Module(new Uint8Array(wasm)); + const inst =3D new WebAssembly.Instance(mod, { + "env" : { + "memory" : wasmMemory, + }, + "helper" : helper, + }); + + return ENC_PTR(addFunction(inst.exports.start, 'ii')); +}); =20 __thread uintptr_t tci_tb_ptr; =20 --=20 2.43.0