From nobody Sat Nov 15 22:35:32 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=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1747740732; cv=none; d=zohomail.com; s=zohoarc; b=KTWZJ/y5KxlfU1+/cEamqFVr0jinX4r3rrkFJgWg9waajKEVNq7F/ab1tRC8Kk7FwpQwG1jjiBsS74qEgGFOQgqr1P70PGBn6GP130pLGUBGQ+uvdyZ5jC1liAyysi8TY1eJ5in0FSowe5D+9ZfKM39VxhpbBImUfy7QviSl+L4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747740732; 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=w1BvluDl1fz7hFalZU7oqAiLldLQh5M2Touk4/f4rWE=; b=hcHcaeimrSipCZN5SPPl6IrL5Qb4YQn417CxugL/cl9jvLSNBFG37xspeAduSbokK05PHAeyWUmo9VVK1xqdFlxlY5iWyHnqHh5iqQJqZfT3lUf4XCHZqhS85FHTYRwiaQV/Adn9KNfyz767LzQQSe2SuySGpy0f3xgYU0WrXYs= 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 1747740732546478.0861249598156; Tue, 20 May 2025 04:32:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHLBq-000829-Iu; Tue, 20 May 2025 07:31:22 -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 1uHLBl-000818-LB for qemu-devel@nongnu.org; Tue, 20 May 2025 07:31:17 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHLBj-0001AI-GT for qemu-devel@nongnu.org; Tue, 20 May 2025 07:31:17 -0400 Received: from DESKTOP-TUU1E5L.fritz.box (unknown [172.201.77.43]) by linux.microsoft.com (Postfix) with ESMTPSA id 182822067887; Tue, 20 May 2025 04:31:10 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 182822067887 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1747740674; bh=w1BvluDl1fz7hFalZU7oqAiLldLQh5M2Touk4/f4rWE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E9J4tCYN4AzeinItrxYtz++T22ftQ+uukBQth0eX0cH7L9KWptVMDQ3uR5EpD7fQe Of19uQeQrdNT37po59BrMajMclNIVwiDCZ3VDhrkWEVLBiZSjM1s+ehLTDD464Hc6W nVIfcOXNM9myAqfDbFosn0iJklOAFCfh+F165uyE= From: Magnus Kulke To: magnuskulke@microsoft.com, qemu-devel@nongnu.org, liuwe@microsoft.com Cc: Paolo Bonzini , "Michael S. Tsirkin" , Wei Liu , Phil Dennis-Jordan , Roman Bolshakov , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Zhao Liu , Richard Henderson , Cameron Esfahani , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [RFC PATCH 02/25] target/i386/emulate: allow instruction decoding from stream Date: Tue, 20 May 2025 13:29:55 +0200 Message-Id: <20250520113018.49569-3-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250520113018.49569-1-magnuskulke@linux.microsoft.com> References: <20250520113018.49569-1-magnuskulke@linux.microsoft.com> 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=13.77.154.182; envelope-from=magnuskulke@linux.microsoft.com; helo=linux.microsoft.com X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-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 @linux.microsoft.com) X-ZM-MESSAGEID: 1747740734375116600 Content-Type: text/plain; charset="utf-8" Introduce a new helper function to decode x86 instructions from a raw instruction byte stream. MSHV delivers an instruction stream in a buffer of the vm_exit message. It can be used to speed up MMIO emulation, since instructions do not have to be fetched and translated. Signed-off-by: Magnus Kulke --- target/i386/emulate/x86_decode.c | 32 +++++++++++++++++++++++++++----- target/i386/emulate/x86_decode.h | 11 +++++++++++ target/i386/emulate/x86_emu.c | 3 ++- target/i386/emulate/x86_emu.h | 1 + 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/target/i386/emulate/x86_decode.c b/target/i386/emulate/x86_dec= ode.c index 88be9479a8..7a862b976e 100644 --- a/target/i386/emulate/x86_decode.c +++ b/target/i386/emulate/x86_decode.c @@ -60,6 +60,7 @@ static inline uint64_t decode_bytes(CPUX86State *env, str= uct x86_decode *decode, int size) { uint64_t val =3D 0; + target_ulong va; =20 switch (size) { case 1: @@ -71,10 +72,16 @@ static inline uint64_t decode_bytes(CPUX86State *env, s= truct x86_decode *decode, VM_PANIC_EX("%s invalid size %d\n", __func__, size); break; } - target_ulong va =3D linear_rip(env_cpu(env), env->eip) + decode->len; - emul_ops->read_mem(env_cpu(env), &val, va, size); + + /* copy the bytes from the instruction stream, if available */ + if (decode->stream && decode->len + size <=3D decode->stream->len) { + memcpy(&val, decode->stream->bytes + decode->len, size); + } else { + va =3D linear_rip(env_cpu(env), env->eip) + decode->len; + emul_ops->fetch_instruction(env_cpu(env), &val, va, size); + } decode->len +=3D size; - =20 + return val; } =20 @@ -2076,9 +2083,8 @@ static void decode_opcodes(CPUX86State *env, struct x= 86_decode *decode) } } =20 -uint32_t decode_instruction(CPUX86State *env, struct x86_decode *decode) +static uint32_t decode_opcode(CPUX86State *env, struct x86_decode *decode) { - memset(decode, 0, sizeof(*decode)); decode_prefix(env, decode); set_addressing_size(env, decode); set_operand_size(env, decode); @@ -2088,6 +2094,22 @@ uint32_t decode_instruction(CPUX86State *env, struct= x86_decode *decode) return decode->len; } =20 +uint32_t decode_instruction(CPUX86State *env, struct x86_decode *decode) +{ + memset(decode, 0, sizeof(*decode)); + return decode_opcode(env, decode); +} + +uint32_t decode_instruction_stream(CPUX86State *env, struct x86_decode *de= code, + struct x86_insn_stream *stream) +{ + memset(decode, 0, sizeof(*decode)); + if (stream !=3D NULL) { + decode->stream =3D stream; + } + return decode_opcode(env, decode); +} + void init_decoder(void) { int i; diff --git a/target/i386/emulate/x86_decode.h b/target/i386/emulate/x86_dec= ode.h index 87cc728598..9bc7d6cc49 100644 --- a/target/i386/emulate/x86_decode.h +++ b/target/i386/emulate/x86_decode.h @@ -269,6 +269,11 @@ typedef struct x86_decode_op { target_ulong ptr; } x86_decode_op; =20 +typedef struct x86_insn_stream { + const uint8_t *bytes; + size_t len; +} x86_insn_stream; + typedef struct x86_decode { int len; uint8_t opcode[4]; @@ -295,12 +300,18 @@ typedef struct x86_decode { struct x86_modrm modrm; struct x86_decode_op op[4]; bool is_fpu; + + x86_insn_stream *stream; } x86_decode; =20 uint64_t sign(uint64_t val, int size); =20 uint32_t decode_instruction(CPUX86State *env, struct x86_decode *decode); =20 +uint32_t decode_instruction_stream(CPUX86State *env, + struct x86_decode *decode, + struct x86_insn_stream *stream); + target_ulong get_reg_ref(CPUX86State *env, int reg, int rex_present, int is_extended, int size); target_ulong get_reg_val(CPUX86State *env, int reg, int rex_present, diff --git a/target/i386/emulate/x86_emu.c b/target/i386/emulate/x86_emu.c index 7773b51b95..73c9eb41d1 100644 --- a/target/i386/emulate/x86_emu.c +++ b/target/i386/emulate/x86_emu.c @@ -1241,7 +1241,8 @@ static void init_cmd_handler(void) bool exec_instruction(CPUX86State *env, struct x86_decode *ins) { if (!_cmd_handler[ins->cmd].handler) { - printf("Unimplemented handler (" TARGET_FMT_lx ") for %d (%x %x) \= n", env->eip, + printf("Unimplemented handler (" TARGET_FMT_lx ") for %d (%x %x) \= n", + env->eip, ins->cmd, ins->opcode[0], ins->opcode_len > 1 ? ins->opcode[1] : 0); env->eip +=3D ins->len; diff --git a/target/i386/emulate/x86_emu.h b/target/i386/emulate/x86_emu.h index 555b567e2c..761e83fd6b 100644 --- a/target/i386/emulate/x86_emu.h +++ b/target/i386/emulate/x86_emu.h @@ -24,6 +24,7 @@ #include "cpu.h" =20 struct x86_emul_ops { + void (*fetch_instruction)(CPUState *cpu, void *data, target_ulong addr= , int bytes); void (*read_mem)(CPUState *cpu, void *data, target_ulong addr, int byt= es); void (*write_mem)(CPUState *cpu, void *data, target_ulong addr, int by= tes); void (*read_segment_descriptor)(CPUState *cpu, struct x86_segment_desc= riptor *desc, --=20 2.34.1