From nobody Tue Sep 16 03:17:50 2025 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=1756225108; cv=none; d=zohomail.com; s=zohoarc; b=N7sjNQ8oboRB5UHY3N1Wgx0rqTgHMnFxjhtAg6R7+pbqHw2vyEBZP5aKPdjF6WfBlLgCa8bpr5vxwdAn3MORoaZAytjAVOdjyr8Z4nFR3IRG7sDQil32i/rsuwtvUO+c5bSs4vcrtHYDWz/z3IS1OCw3gO6Sgz3pQU1Gx0ka7q8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756225108; 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=NkF/SrWBRt7mpVRvpwQdPdICO84Pyhp/wixwRcKtvEU=; b=WPRqE5hBVjQv0Wiz4M0cZJ3QLiZ+hWzZFH+cyF97XJqvmvEsI94papOfdA2jWQ2E3c/7Hy4wCD7RqYe2MkP4VboAL1w8OTJbNNU83wHvkU63BLa5MOSB6yD/2xBUz//tEaZ9s9eFqAx885hajzGvCj1YzVku8d4Q7yJ3V+c9B/Y= 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 1756225108672627.487822812648; Tue, 26 Aug 2025 09:18:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uqwJz-0000Og-5q; Tue, 26 Aug 2025 12:14:56 -0400 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 1uqwJF-0006ie-PS; Tue, 26 Aug 2025 12:14:10 -0400 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uqwJ7-0008Bn-Qq; Tue, 26 Aug 2025 12:14:08 -0400 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-24458272c00so67996305ad.3; Tue, 26 Aug 2025 09:13:55 -0700 (PDT) Received: from ktock.. ([240d:1a:3b6:8b00:561f:1400:11f5:714b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-32750bc341asm1749747a91.19.2025.08.26.09.13.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Aug 2025 09:13:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756224833; x=1756829633; 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=NkF/SrWBRt7mpVRvpwQdPdICO84Pyhp/wixwRcKtvEU=; b=koVE/+Xe6nKc+jv6vnl9iZspQUDPrl+iRxEPqHXXEnqtZcRbYmOCqU1f1BW6wY8ppd VdzQYRNf7GgVcp5miVlqL+N3jBJprk4xbTtrUus6fg0BGViKN3FHI5S2fQ875fNoNYU5 fFP1lXQQ8v2XMuh+lrd26jka9DeRMQC2t0iM9nq0wSXH/K+wT5eNQMYvvxjbTdScbzhW 7cJe8/b3GeEr+ClUNDpYXmTPFhyjyh1u9sBi5g6n2mO81UR9tqZgMb5/lRbD9pYwErig XQFz79bqNlAbXD/x9SRP69PaZU70CzCDqknPXu1m58si8Av+8N6VTwF7/1XJ2/jCMuYN rM8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756224833; x=1756829633; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NkF/SrWBRt7mpVRvpwQdPdICO84Pyhp/wixwRcKtvEU=; b=fSHj3K2njpuTSr1YvwAEST75DLPo7NnMXOGNXSSS2fAqaNDWcGqcm1h5S67Vqmc6Xj tUzuAblVNstWXqUnCmGqA0qwdBTS73qCdkDES1cDw6auE1f9BrTEcvA/8Qx50Ndyw7sL 1w/Zo365XrAOqVzWawIRiNQl4rlbQAZrLBWffobLVIKx53sT1L4C3mhEJ7lBU641251z KiWnJ7HlPtkXCL4hV3N9jc7KSH64NM44m7C+/UHua00vDLXFgVMWKorXK9HVN7svijj5 9SFOp6ZtrqOBygovhVPyqUAb+z7Yql+JaIHtIEfgWsF3FEXTH71NihzuZcRLB/ALz39D d7BQ== X-Forwarded-Encrypted: i=1; AJvYcCUI4yRhqWyhjVobi4Oy3eAhjaIApkLQ2fGdZlTl3gfR2c4Q8uwfIZYie4IKDEdVDNXDfo4H+PzDfQ==@nongnu.org, AJvYcCV2pwY1Mp/wCf6dHEfKP8KAfBVuj4Dkej68aNmalUZsYJNcNrbc+92WsZczcnAeVLSrCPmXSMNpKt+WMw==@nongnu.org X-Gm-Message-State: AOJu0Yw/4hUNEc5Pe1sLDAZYI+edxegypeEvouFmkRL3tObbyBtuUpeo paBHOvk/GH+gkgvw9O8XOItVJ9AEsAdYnq9ImhylxQalza+qvbOhQk5Ok7T18w== X-Gm-Gg: ASbGnctOvFegxKg6s9IcvtHW0V2J0Pd1f1TadRt2fY1u28/rILYbqFiYido6Nw4U8wF F4m6TYM+/rsVXcWZ/kUC4hxjCAIzV6+5DXPYU0W0E2jSKduirEE8lKD2iTUWulqtkqHDdc1RtZc 96xpW+aqL+hAvpBn3cBHvs5KODyeXFfai2bp1RtrmE/yy0kLlfDwbj4NoPrSqTOuzt4wId7SZ7h cNTXPeUbSYP1n1bEj5EmKWa8yy4Jn6KmbmIV0FdHa3KWGOn1srWv7r1daGTc3bJiL9LB9lRW8SO 3GxpbCe/otVmx3vc9FgTgC249hFW1wjG2EWYApG/kMpnVRKE0JcK8cRqwt2cDo+YW492ODuL13/ 1iDmQC3jqbk9YxcyMzeAmtw== X-Google-Smtp-Source: AGHT+IEFvgv334bH701ne0mKaBmxUq/pjTqrgeDMEvrImaiNqmHHp0Z+joyZhm835L4NeY5p5xazNA== X-Received: by 2002:a17:902:db0c:b0:240:6406:c471 with SMTP id d9443c01a7336-2462ee0b897mr216696975ad.10.1756224833007; Tue, 26 Aug 2025 09:13:53 -0700 (PDT) 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 , qemu-arm@nongnu.org, qemu-riscv@nongnu.org, Stefan Hajnoczi , Pierrick Bouvier , ktokunaga.mail@gmail.com Subject: [PATCH v2 28/35] tcg/wasm: Implement instantiation of Wasm binary Date: Wed, 27 Aug 2025 01:10:33 +0900 Message-ID: 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::631; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x631.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=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: 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: 1756225109894116600 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 wasm/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/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 +#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 {}; + const entsize =3D TCG_TARGET_REG_BITS / 8; + for (var i =3D 0; i < import_vec_size / entsize; i++) { + const idx =3D memory_v.getBigInt64( + DEC_PTR(import_vec_begin) + i * entsize, 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