From nobody Fri Nov 14 23:29:40 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=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1759996734; cv=none; d=zohomail.com; s=zohoarc; b=mS2fY5xAlcY2yImFLYNZqPs6pNBhULjlVnVk3ueiOavwOShTwF2Y66DMa2nJRc8XPCAe+FFFHip2lKbeQuHhVvj+8IbIXUynSKrTF49ySMulGC9qPVSzSjLFhGlQXWvoKEmL5g7hLXyHgbheaBsRNJp8tmfCXsa+1aGhFNh2QOU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1759996734; 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=+8HYpUdO2P7nAh/MHMViByHQJ/N9zXJCXtwAC6CreTc=; b=FOLzx69dg76M9IqB8GK/vqot30eqm72DdvgoWgnxv1bA8rJbJ3IBm1WpJzp7t7VJf8yzCr0d/UzJQuOg8u1v7qhwBKQzk5wDZfx9KKF0WcnNMyd3GZ7QWoBhjh0t4NdhtLEnkKa++jq2GJIxc7eh3+KjV+ZemYjTPI/9JkZbe+o= 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=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1759996734879990.6066383624826; Thu, 9 Oct 2025 00:58:54 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v6lQP-0008DF-Jy; Thu, 09 Oct 2025 03:50:57 -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 1v6lQN-0008Br-46 for qemu-devel@nongnu.org; Thu, 09 Oct 2025 03:50:55 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v6lQH-0004Mp-Cs for qemu-devel@nongnu.org; Thu, 09 Oct 2025 03:50:54 -0400 Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-408-brftt72HOnW11E6qbFwZlw-1; Thu, 09 Oct 2025 03:50:47 -0400 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-3f93db57449so436874f8f.2 for ; Thu, 09 Oct 2025 00:50:46 -0700 (PDT) Received: from [192.168.10.48] ([151.95.36.217]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4255d8ab960sm33305506f8f.13.2025.10.09.00.50.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Oct 2025 00:50:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1759996248; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+8HYpUdO2P7nAh/MHMViByHQJ/N9zXJCXtwAC6CreTc=; b=Zc17l/c1JIlz+OdbgRcQyYgpVIrgPtHbu0xSQfNnW83yDiPK5htVl2JYr8/+DGXVAW9oKC ssScmEw9A9wsjwYOIZ57qdL8PNgqAFtBzPPWLCZhe5MEt4mvCpb3Woz24vBonMZgne5p3l 5a1S2Jj8HFlunWO7sthG8V1gnqOoEhM= X-MC-Unique: brftt72HOnW11E6qbFwZlw-1 X-Mimecast-MFC-AGG-ID: brftt72HOnW11E6qbFwZlw_1759996246 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759996245; x=1760601045; 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=+8HYpUdO2P7nAh/MHMViByHQJ/N9zXJCXtwAC6CreTc=; b=hMwp8ATFH4FQe2Gh5Gl5zqf1t54UsxXNBdfy3/mOvDmZkzvq9AhWo/6B3XHsowATW4 Z8Ghgt5zxNhgMVmrW05bdzkrtCNVutXnktjZGmhzmhoEP2tqO/aGhXmELL9wOZXvpyuI hIjWA8O/bSnZ+zKxFe8W4EFOTz7GFpWL4R9G8Zz5eOjfytZ7YgXfrMUSij8PQ7ZS0V7y DwGsyQQ38rMVAM3cDGysiHndxKgL+HHAEd++/wiKppbdzI1jd9u/AHJH9dozrrE1B218 zBhzzJksQoZr3etMad9qnht6UXh+vLSbSPJl+fwh6mhEobrOtQBB/IVgD4cXnFKwaRt9 4DVg== X-Gm-Message-State: AOJu0YykTHQG4+DkUi7BYSnMkAqoHSRGYBQIQuk2ByxF3es4qSZpU1zu Oy7/GDpuBGJVfHnlFULagt3YUYlA80KmNWu1RyT/j8RUJPoj5WbgSgctzWcFiVYtzyMvyoKNfJD Jll3Wp3edf6UIm0QJMUxVclBZ5UWDM9xang7eF/23I8fF1uKo2I6LtPtY+ICEIPrx3NV8tIjbVX 7TsHnDiKdWuzI396n00nceu6J6wyX4rtzkSdEg2gHOf6U= X-Gm-Gg: ASbGncv9D6q+eY0HO/zT6MTgWMEqRp+NBh7hErhWQrJXWOxNNkOS5zyQmP06/Uymr1f HWJDczOZ/Rzxqz/KzNhs8N7Nqecw9jclfyVVkm68KKQQ+bnIn6RYJxjcASP1IhZZwpJnDZJ61Lb hJ0sPZpUYQ2icaeF7IBCUshY4dhf/QJXAZBmsZQrHoy+3Lwv9ewC8dnbZIayxZI0BFixyOFcp4y k08V42gWo5FG4yBE+4l+52MdM4p+l47/vLrpyXUM7d63wS/QExCVFr8EjcZKWj6L+K52Z1lWhc4 vmtMG7c1T7FlP6tMsjkYLCwjH146eRbKxENF0r9AyUx4oxXY5zad35GGhgpvzkg61GvEIsz1eBO 9K8NeCXk6f0PMIiPT4JYlCdtgmTrI+LNgyCPA4uPGVn4yLXoN X-Received: by 2002:a05:6000:26ca:b0:400:1bbb:d279 with SMTP id ffacd0b85a97d-4266e7d44f1mr4180462f8f.39.1759996244648; Thu, 09 Oct 2025 00:50:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEAKpfSXghO/EWp8z5+s6JW65nAgBB9/RMLk5Xkr+m0p+hGQr11MJJ74krAwxsR/EmUt77D8A== X-Received: by 2002:a05:6000:26ca:b0:400:1bbb:d279 with SMTP id ffacd0b85a97d-4266e7d44f1mr4180441f8f.39.1759996244193; Thu, 09 Oct 2025 00:50:44 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Magnus Kulke Subject: [PULL 08/35] target/i386/emulate: Allow instruction decoding from stream Date: Thu, 9 Oct 2025 09:49:58 +0200 Message-ID: <20251009075026.505715-9-pbonzini@redhat.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251009075026.505715-1-pbonzini@redhat.com> References: <20251009075026.505715-1-pbonzini@redhat.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=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.442, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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 @redhat.com) X-ZM-MESSAGEID: 1759996740440116600 Content-Type: text/plain; charset="utf-8" From: Magnus Kulke 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. Added "fetch_instruction()" op to x86_emul_ops() to improve traceability. Signed-off-by: Magnus Kulke Link: https://lore.kernel.org/r/20250916164847.77883-3-magnuskulke@linux.mi= crosoft.com Signed-off-by: Paolo Bonzini --- target/i386/emulate/x86_decode.h | 9 +++++++++ target/i386/emulate/x86_emu.h | 2 ++ target/i386/emulate/x86_decode.c | 27 +++++++++++++++++++++++---- target/i386/emulate/x86_emu.c | 3 ++- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/target/i386/emulate/x86_decode.h b/target/i386/emulate/x86_dec= ode.h index 927645af1a3..1cadf3694f0 100644 --- a/target/i386/emulate/x86_decode.h +++ b/target/i386/emulate/x86_decode.h @@ -272,6 +272,11 @@ typedef struct x86_decode_op { }; } 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]; @@ -298,11 +303,15 @@ 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); +uint32_t decode_instruction_stream(CPUX86State *env, struct x86_decode *de= code, + struct x86_insn_stream *stream); =20 void *get_reg_ref(CPUX86State *env, int reg, int rex_present, int is_extended, int size); diff --git a/target/i386/emulate/x86_emu.h b/target/i386/emulate/x86_emu.h index a1a961284b2..05686b162f6 100644 --- a/target/i386/emulate/x86_emu.h +++ b/target/i386/emulate/x86_emu.h @@ -24,6 +24,8 @@ #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, diff --git a/target/i386/emulate/x86_decode.c b/target/i386/emulate/x86_dec= ode.c index 2eca39802e3..97bd6f1a3be 100644 --- a/target/i386/emulate/x86_decode.c +++ b/target/i386/emulate/x86_decode.c @@ -71,10 +71,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 { + target_ulong va =3D linear_rip(env_cpu(env), env->eip) + decode->l= en; + emul_ops->fetch_instruction(env_cpu(env), &val, va, size); + } decode->len +=3D size; - =20 + return val; } =20 @@ -2076,9 +2082,10 @@ static void decode_opcodes(CPUX86State *env, struct = x86_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 +2095,18 @@ uint32_t decode_instruction(CPUX86State *env, struct= x86_decode *decode) return decode->len; } =20 +uint32_t decode_instruction(CPUX86State *env, struct x86_decode *decode) +{ + return decode_opcode(env, decode); +} + +uint32_t decode_instruction_stream(CPUX86State *env, struct x86_decode *de= code, + struct x86_insn_stream *stream) +{ + decode->stream =3D stream; + return decode_opcode(env, decode); +} + void init_decoder(void) { int i; diff --git a/target/i386/emulate/x86_emu.c b/target/i386/emulate/x86_emu.c index db7a7f7437d..4409f7bc134 100644 --- a/target/i386/emulate/x86_emu.c +++ b/target/i386/emulate/x86_emu.c @@ -1246,7 +1246,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; --=20 2.51.0