From nobody Sun Jul 26 10:06:44 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=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1783462669; cv=none; d=zohomail.com; s=zohoarc; b=Ws2r3DWgFtPe70CPyXsFRt66I1K0tLv4Vz3Na1oYdQlejDinzYeQMbgTliXlP6XFUf2B5dTrMTUKYGa3nAeSuzb6k4DTnpxEn56z6OwQe+oWie8fVYk6tbn0mPJYeqwY5EHgQ23QgPtU3UWlr2t5IWG9h5ApDLW1zCpL4hN5Ko8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783462669; h=Content-Type: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=VEZEP3aUh8gT4Kp/FDw4WRo8q8NHSoahctbWzyibCdE=; b=e5N7HDeM2H54YhxIhu3BGhdVGwzQCv7E4d0heNY0a0DV88/QUdNXsLa/UN5PaBiRMmcxI+ZBf8EWjzJFngucx8pllcWkAI/is/N5PwWToqzvPSAhKzKkE9h/OF/NuWVjvveyZ45teW+FyEmn/GNg2wa+QF5YF9X4NLItxl26LD8= 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 lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1783462669343317.8212664090121; Tue, 7 Jul 2026 15:17:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whE69-0003s7-6q; Tue, 07 Jul 2026 18:17:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1whE67-0003rr-JV for qemu-devel@nongnu.org; Tue, 07 Jul 2026 18:16:59 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whE65-0000JR-U7 for qemu-devel@nongnu.org; Tue, 07 Jul 2026 18:16:59 -0400 Received: from laptop.localdomain (unknown [86.121.140.206]) by linux.microsoft.com (Postfix) with ESMTPSA id 17DBE20B7167; Tue, 7 Jul 2026 15:16:49 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 17DBE20B7167 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1783462612; bh=VEZEP3aUh8gT4Kp/FDw4WRo8q8NHSoahctbWzyibCdE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F+GMnw7x+AdW4Rq0mvRV0SZ9XXrUT2wC4vBWb0F6/OVh/CQ/4yNPBHYGbkvNRgYBT 20ht4UBWm8u7gDemT0AlR1X4OzXAMlrKlmXUwkCzmbRXYimGtHs85ehmUPPZ4YFTPw RApRqydFh6z/PhmoFJRHyZQM6Hu9LLOIk9SXLSPQ= From: =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Wei Liu , Magnus Kulke , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Wei Liu Subject: [PATCH 1/5] include/hw/hyperv: move hv_vp_register_page struct definition Date: Wed, 8 Jul 2026 01:16:41 +0300 Message-ID: <20260707221645.24557-2-dblanzeanu@linux.microsoft.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260707221645.24557-1-dblanzeanu@linux.microsoft.com> References: <20260707221645.24557-1-dblanzeanu@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=lists1p.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=dblanzeanu@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, 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: 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 @linux.microsoft.com) X-ZM-MESSAGEID: 1783462671080158500 Move the `hv_vp_register_page` structure definition from `hvgdk_mini.h` to `hvhdk.h`. Fixes: 8afb40b7fe Signed-off-by: Doru Bl=C3=A2nzeanu Reviewed-by: Magnus Kulke --- include/hw/hyperv/hvgdk.h | 2 + include/hw/hyperv/hvgdk_mini.h | 103 -------------------------------- include/hw/hyperv/hvhdk.h | 106 +++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 103 deletions(-) diff --git a/include/hw/hyperv/hvgdk.h b/include/hw/hyperv/hvgdk.h index 71161f477c..e4be861716 100644 --- a/include/hw/hyperv/hvgdk.h +++ b/include/hw/hyperv/hvgdk.h @@ -9,6 +9,8 @@ #ifndef HW_HYPERV_HVGDK_H #define HW_HYPERV_HVGDK_H =20 +#include "hvgdk_mini.h" + #define HVGDK_H_VERSION (25125) =20 enum hv_unimplemented_msr_action { diff --git a/include/hw/hyperv/hvgdk_mini.h b/include/hw/hyperv/hvgdk_mini.h index f8838a31bb..c6749a9525 100644 --- a/include/hw/hyperv/hvgdk_mini.h +++ b/include/hw/hyperv/hvgdk_mini.h @@ -504,109 +504,6 @@ struct hv_input_assert_virtual_interrupt { uint16_t rsvd_z1; } QEMU_PACKED; =20 -/* Flags for dirty mask of hv_vp_register_page */ -enum hv_x64_register_class_type { - HV_X64_REGISTER_CLASS_GENERAL =3D 0, - HV_X64_REGISTER_CLASS_IP =3D 1, - HV_X64_REGISTER_CLASS_XMM =3D 2, - HV_X64_REGISTER_CLASS_SEGMENT =3D 3, - HV_X64_REGISTER_CLASS_FLAGS =3D 4, -}; - -#define HV_VP_REGISTER_PAGE_MAX_VECTOR_COUNT 7 - -union hv_vp_register_page_interrupt_vectors { - uint64_t as_uint64; - struct { - uint8_t vector_count; - uint8_t vector[HV_VP_REGISTER_PAGE_MAX_VECTOR_COUNT]; - }; -}; - -struct hv_vp_register_page { - uint16_t version; - uint8_t isvalid; - uint8_t rsvdz; - uint32_t dirty; - - union { - struct { - /* General purpose registers (HV_X64_REGISTER_CLASS_GENERAL) */ - union { - struct { - uint64_t rax; - uint64_t rcx; - uint64_t rdx; - uint64_t rbx; - uint64_t rsp; - uint64_t rbp; - uint64_t rsi; - uint64_t rdi; - uint64_t r8; - uint64_t r9; - uint64_t r10; - uint64_t r11; - uint64_t r12; - uint64_t r13; - uint64_t r14; - uint64_t r15; - } QEMU_PACKED; - - uint64_t gp_registers[16]; - }; - /* Instruction pointer (HV_X64_REGISTER_CLASS_IP) */ - uint64_t rip; - /* Flags (HV_X64_REGISTER_CLASS_FLAGS) */ - uint64_t rflags; - } QEMU_PACKED; - - uint64_t registers[18]; - }; - uint8_t reserved[8]; - /* Volatile XMM registers (HV_X64_REGISTER_CLASS_XMM) */ - union { - struct { - struct hv_u128 xmm0; - struct hv_u128 xmm1; - struct hv_u128 xmm2; - struct hv_u128 xmm3; - struct hv_u128 xmm4; - struct hv_u128 xmm5; - } QEMU_PACKED; - - struct hv_u128 xmm_registers[6]; - }; - /* Segment registers (HV_X64_REGISTER_CLASS_SEGMENT) */ - union { - struct { - struct hv_x64_segment_register es; - struct hv_x64_segment_register cs; - struct hv_x64_segment_register ss; - struct hv_x64_segment_register ds; - struct hv_x64_segment_register fs; - struct hv_x64_segment_register gs; - } QEMU_PACKED; - - struct hv_x64_segment_register segment_registers[6]; - }; - /* Misc. control registers (cannot be set via this interface) */ - uint64_t cr0; - uint64_t cr3; - uint64_t cr4; - uint64_t cr8; - uint64_t efer; - uint64_t dr7; - union hv_x64_pending_interruption_register pending_interruption; - union hv_x64_interrupt_state_register interrupt_state; - uint64_t instruction_emulation_hints; - uint64_t xfem; - - uint8_t reserved1[0x100]; - - /* Interrupts injected as part of HvCallDispatchVp. */ - union hv_vp_register_page_interrupt_vectors interrupt_vectors; -} QEMU_PACKED; - /* /dev/mshv */ #define MSHV_CREATE_PARTITION _IOW(MSHV_IOCTL, 0x00, struct mshv_create_= partition) #define MSHV_CREATE_VP _IOW(MSHV_IOCTL, 0x01, struct mshv_create_= vp) diff --git a/include/hw/hyperv/hvhdk.h b/include/hw/hyperv/hvhdk.h index 9e6dcb22f6..b9b76cf93c 100644 --- a/include/hw/hyperv/hvhdk.h +++ b/include/hw/hyperv/hvhdk.h @@ -9,7 +9,12 @@ #ifndef HW_HYPERV_HVHDK_H #define HW_HYPERV_HVHDK_H =20 +#include "hvgdk.h" +#include "hvhdk_mini.h" + #define HV_PARTITION_SYNTHETIC_PROCESSOR_FEATURES_BANKS 1 +#define HV_VP_REGISTER_PAGE_VERSION_1 1u +#define HV_VP_REGISTER_PAGE_MAX_VECTOR_COUNT 7 =20 struct hv_input_get_partition_property { uint64_t partition_id; @@ -441,4 +446,105 @@ typedef struct hv_input_register_intercept_result { union hv_register_intercept_result_parameters parameters; } QEMU_PACKED hv_input_register_intercept_result; =20 +/* Flags for dirty mask of hv_vp_register_page */ +enum hv_x64_register_class_type { + HV_X64_REGISTER_CLASS_GENERAL =3D 0, + HV_X64_REGISTER_CLASS_IP =3D 1, + HV_X64_REGISTER_CLASS_XMM =3D 2, + HV_X64_REGISTER_CLASS_SEGMENT =3D 3, + HV_X64_REGISTER_CLASS_FLAGS =3D 4, +}; + +union hv_vp_register_page_interrupt_vectors { + uint64_t as_uint64; + struct { + uint8_t vector_count; + uint8_t vector[HV_VP_REGISTER_PAGE_MAX_VECTOR_COUNT]; + }; +}; + +struct hv_vp_register_page { + uint16_t version; + uint8_t isvalid; + uint8_t rsvdz; + uint32_t dirty; + + union { + struct { + /* General purpose registers (HV_X64_REGISTER_CLASS_GENERAL) */ + union { + struct { + uint64_t rax; + uint64_t rcx; + uint64_t rdx; + uint64_t rbx; + uint64_t rsp; + uint64_t rbp; + uint64_t rsi; + uint64_t rdi; + uint64_t r8; + uint64_t r9; + uint64_t r10; + uint64_t r11; + uint64_t r12; + uint64_t r13; + uint64_t r14; + uint64_t r15; + } QEMU_PACKED; + + uint64_t gp_registers[16]; + }; + /* Instruction pointer (HV_X64_REGISTER_CLASS_IP) */ + uint64_t rip; + /* Flags (HV_X64_REGISTER_CLASS_FLAGS) */ + uint64_t rflags; + } QEMU_PACKED; + + uint64_t registers[18]; + }; + uint8_t reserved[8]; + /* Volatile XMM registers (HV_X64_REGISTER_CLASS_XMM) */ + union { + struct { + struct hv_u128 xmm0; + struct hv_u128 xmm1; + struct hv_u128 xmm2; + struct hv_u128 xmm3; + struct hv_u128 xmm4; + struct hv_u128 xmm5; + } QEMU_PACKED; + + struct hv_u128 xmm_registers[6]; + }; + /* Segment registers (HV_X64_REGISTER_CLASS_SEGMENT) */ + union { + struct { + struct hv_x64_segment_register es; + struct hv_x64_segment_register cs; + struct hv_x64_segment_register ss; + struct hv_x64_segment_register ds; + struct hv_x64_segment_register fs; + struct hv_x64_segment_register gs; + } QEMU_PACKED; + + struct hv_x64_segment_register segment_registers[6]; + }; + /* Misc. control registers (cannot be set via this interface) */ + uint64_t cr0; + uint64_t cr3; + uint64_t cr4; + uint64_t cr8; + uint64_t efer; + uint64_t dr7; + union hv_x64_pending_interruption_register pending_interruption; + union hv_x64_interrupt_state_register interrupt_state; + uint64_t instruction_emulation_hints; + uint64_t xfem; + + uint8_t reserved1[0x100]; + + /* Interrupts injected as part of HvCallDispatchVp. */ + union hv_vp_register_page_interrupt_vectors interrupt_vectors; +} QEMU_PACKED; + #endif /* HW_HYPERV_HVHDK_H */ --=20 2.53.0 From nobody Sun Jul 26 10:06:44 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=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1783462668; cv=none; d=zohomail.com; s=zohoarc; b=bxJfdo6m6UfhfTnUDYlRIfs4g0lXzVOW7493zN42Q9UQtIoR2ICfLNfitRvc88Y/tB109+6Q5DJ5k8ycYidx4K7VsLttR41P1UU5PG+bX7B3UV9xvj5GbhAubvyIjBD2fK/AJS0DfYw+LPeei03Pt2dR9H0j8OjUBr834bv7eXo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783462668; h=Content-Type: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=soNbHmugulup9sC/v+wVwnkS2OglUYxoHnlwOg/fARI=; b=Ehn3UuWKIe4DtcQofyCkQB/7swA5SJpDlbRpjMSHKPdjpa7ZDi3SVwaI8k3xDqz17sLl+A1e0LPJySVG9+mUpU5iaqRFjH96joJyQF1r291EJ+Xy1cnC9v9NtZgKfUS8TzdcaFn/dN7CG5ZfMxUX3SHThR1Y2VhJNjt2HjMDag8= 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 lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1783462668935802.3928890867741; Tue, 7 Jul 2026 15:17:48 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whE6C-0003tB-Mz; Tue, 07 Jul 2026 18:17:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1whE6A-0003sa-IJ for qemu-devel@nongnu.org; Tue, 07 Jul 2026 18:17:02 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whE69-0000KB-2f for qemu-devel@nongnu.org; Tue, 07 Jul 2026 18:17:02 -0400 Received: from laptop.localdomain (unknown [86.121.140.206]) by linux.microsoft.com (Postfix) with ESMTPSA id 0CC8820B7166; Tue, 7 Jul 2026 15:16:52 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 0CC8820B7166 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1783462614; bh=soNbHmugulup9sC/v+wVwnkS2OglUYxoHnlwOg/fARI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JMR8EwVS16wWPXClnXOPYpKbh8pj9fFPUINoJ2Sa1FhCjYfJKR5JELyqVX4MZxVCR 2erq/fl0IzkJh4XicQX5PPZ8qUpP+1tsNqC86GssL4qrdngbDSuDNtVR22m1hApqNh nVfpJCnXLayqNGz9udhCE2vbXLFUpHuUedC7zuSw= From: =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Wei Liu , Magnus Kulke , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Wei Liu Subject: [PATCH 2/5] target/i386/mshv: abort when hv_vp_register_page setup fails Date: Wed, 8 Jul 2026 01:16:42 +0300 Message-ID: <20260707221645.24557-3-dblanzeanu@linux.microsoft.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260707221645.24557-1-dblanzeanu@linux.microsoft.com> References: <20260707221645.24557-1-dblanzeanu@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=lists1p.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=dblanzeanu@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, 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: 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 @linux.microsoft.com) X-ZM-MESSAGEID: 1783462671109158500 In case the call to mmap fails, we log an error and abort to signal there is something wrong with the system. Check the register page version and compare with the expected version and abort in case of a mismatch. Fixes: a173f8f170 Signed-off-by: Doru Bl=C3=A2nzeanu Reviewed-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 1c433c408c..c782715497 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -2049,6 +2049,7 @@ void mshv_arch_init_vcpu(CPUState *cpu) CPUX86State *env =3D &x86_cpu->env; AccelCPUState *state =3D cpu->accel; size_t page =3D HV_HYP_PAGE_SIZE, xsave_len; + void *regs_page; void *mem =3D qemu_memalign(page, 2 * page); int ret; X86XSaveHeader *header; @@ -2060,15 +2061,20 @@ void mshv_arch_init_vcpu(CPUState *cpu) > HV_HYP_PAGE_SIZE)); =20 /* mmap the registers page */ - void *rp =3D mmap(NULL, page, PROT_READ | PROT_WRITE, + regs_page =3D mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_SHARED, mshv_vcpufd(cpu), MSHV_VP_MMAP_OFFSET_REGISTERS * page); - if (rp =3D=3D MAP_FAILED) { - warn_report("register page mmap failed, falling back to hypercalls= : %s", - strerror(errno)); - env->regs_page =3D NULL; - } else { - env->regs_page =3D (struct hv_vp_register_page *) rp; + if (regs_page =3D=3D MAP_FAILED) { + /* This shouldn't fail, so we treat it as a fatal error */ + error_report("register page mmap failed: %s", strerror(errno)); + abort(); + } + env->regs_page =3D (struct hv_vp_register_page *) regs_page; + + if (env->regs_page->version !=3D HV_VP_REGISTER_PAGE_VERSION_1) { + error_report("register page version mismatch: got %u, expected %u", + env->regs_page->version, HV_VP_REGISTER_PAGE_VERSION_= 1); + abort(); } =20 state->hvcall_args.base =3D mem; --=20 2.53.0 From nobody Sun Jul 26 10:06:44 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=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1783462700; cv=none; d=zohomail.com; s=zohoarc; b=Gb8UGqoOdiMeGvtnolxQTciKGUVBuFfaenXHpqwSUGh6ro4EehxmfFFbn+fjeRFSUB0yucMchuVkEfAfgC2FfxJKXvL5ino4NzlL6Uvupf3y5pvumZ+m9GSFUnuqC3arotJ6o1cEzonjjVMbHyzyO3XXkU1Jd8DBqRFZ1rheh6I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783462700; h=Content-Type: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=P8sFzwZWvX+wmEgiICVzK3eIuMkwpPeF576/ijgWEzo=; b=d4u8eDFz2I7t8FGv4HI+2vW+Ntmwq3sdlKG/MXKCCUhZdqphhIXE+zoSiY+RuLpYl/irhx6bTwu4+sw8Nf5GE+S+A2TOVwa3SWAdWvCJ9RH45Pc/9gjUpxoBTkZnZzz7jEBf+rdaGkpp5Xk5JumxKJ4YU9DO0gr5rnr71uTaVrw= 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 lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1783462700705799.6923724558707; Tue, 7 Jul 2026 15:18:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whE6E-0003tc-HQ; Tue, 07 Jul 2026 18:17:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1whE6D-0003tU-4r for qemu-devel@nongnu.org; Tue, 07 Jul 2026 18:17:05 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whE6B-0000LN-CU for qemu-devel@nongnu.org; Tue, 07 Jul 2026 18:17:04 -0400 Received: from laptop.localdomain (unknown [86.121.140.206]) by linux.microsoft.com (Postfix) with ESMTPSA id B5DD320B7167; Tue, 7 Jul 2026 15:16:55 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com B5DD320B7167 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1783462617; bh=P8sFzwZWvX+wmEgiICVzK3eIuMkwpPeF576/ijgWEzo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Th0hzQM0C6WPcJlhxhoLuMO986CMxgygD4UNy06pmTxTWTODka8K30EjGew1L8FRB jes0uZGAnKMjZeFdYw8VWDkZ6IwFyLbchVZ174JhfYJ9zTBtguP1zuIC6tk3WvxL8a 2aSstwfmBfVmbeh4ZGrHtKD28HsE6fXLttr0y2OQ= From: =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Wei Liu , Magnus Kulke , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Wei Liu Subject: [PATCH 3/5] target/i386/mshv: remove fallback for register page get registers Date: Wed, 8 Jul 2026 01:16:43 +0300 Message-ID: <20260707221645.24557-4-dblanzeanu@linux.microsoft.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260707221645.24557-1-dblanzeanu@linux.microsoft.com> References: <20260707221645.24557-1-dblanzeanu@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=lists1p.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=dblanzeanu@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, 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: 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 @linux.microsoft.com) X-ZM-MESSAGEID: 1783462703113158500 Change `load_regs` to use the register page when it is mmapped and is valid. Eliminate the hypercall based logic and fail in case the register page is found in an unexpected state. When retrieving the special registers, there are some registers that are not present in the register page: TR, LDTR, GDTR, IDTR, CR2, APIC_BASE. As this registers are not likely to be used in an MMIO/PIO operation, and to avoid a hypercall overhead we do not retrieve them. Local testing showed no regression when using this logic. To properly retrieve all the necessary registers for each decoded operation implies having a mechanism that tracks the state of each register, which is beyond the scope of this patch series. Fixes: 40072a7391 Signed-off-by: Doru Bl=C3=A2nzeanu Reviewed-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 107 +++++++++++------------------------- 1 file changed, 31 insertions(+), 76 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index c782715497..9ec5c19c67 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -270,15 +270,6 @@ static int get_xc_reg(CPUState *cpu) return 0; } =20 -static enum hv_register_name NON_VP_PAGE_REGISTER_NAMES[6] =3D { - HV_X64_REGISTER_TR, - HV_X64_REGISTER_LDTR, - HV_X64_REGISTER_GDTR, - HV_X64_REGISTER_IDTR, - HV_X64_REGISTER_CR2, - HV_X64_REGISTER_APIC_BASE, -}; - static int translate_gva(const CPUState *cpu, uint64_t gva, uint64_t *gpa, uint64_t flags) { @@ -615,7 +606,7 @@ static void populate_special_regs(const hv_register_ass= oc *assocs, cpu_set_apic_base(x86cpu->apic_state, assocs[16].value.reg64); } =20 -static void mshv_get_standard_regs_vp_page(CPUState *cpu) +static void get_standard_regs_vp_page(CPUState *cpu) { X86CPU *x86cpu =3D X86_CPU(cpu); CPUX86State *env =3D &x86cpu->env; @@ -643,13 +634,19 @@ static void mshv_get_standard_regs_vp_page(CPUState *= cpu) rflags_to_lflags(env); } =20 -static int mshv_get_special_regs_vp_page(CPUState *cpu) +/* + * This function synchronizes the special registers present in the + * register vp page, which are not all the special registers. + * The rest of the special registers (LD, TR, GDT, IDT, CR2, APIC_BASE) + * are not synchronized to avoid the overhead of a hypercall. + * + * These special registers are not normally used by the guest, + * and are only used in some specific cases. + */ +static void get_special_regs_vp_page(CPUState *cpu) { X86CPU *x86cpu =3D X86_CPU(cpu); CPUX86State *env =3D &x86cpu->env; - struct hv_register_assoc assocs[ARRAY_SIZE(NON_VP_PAGE_REGISTER_NAMES)= ]; - int ret; - size_t n_regs =3D ARRAY_SIZE(NON_VP_PAGE_REGISTER_NAMES); hv_x64_segment_register seg; =20 /* Populate special registers that are in the VP register page */ @@ -672,46 +669,15 @@ static int mshv_get_special_regs_vp_page(CPUState *cp= u) populate_segment_reg(&seg, &env->segs[R_FS]); memcpy(&seg, &env->regs_page->gs, sizeof(hv_x64_segment_register)); populate_segment_reg(&seg, &env->segs[R_GS]); - - /* The rest of the special registers that are not in the VP register p= age */ - for (size_t i =3D 0; i < n_regs; i++) { - assocs[i].name =3D NON_VP_PAGE_REGISTER_NAMES[i]; - } - - ret =3D mshv_get_generic_regs(cpu, assocs, n_regs); - if (ret < 0) { - error_report("failed to get non-vp-page special registers"); - return -1; - } - - /* Non-VP page registers - TR, LDTR, GDTR, IDTR, CR2, APIC_BASE */ - populate_segment_reg(&assocs[0].value.segment, &env->tr); - populate_segment_reg(&assocs[1].value.segment, &env->ldt); - - populate_table_reg(&assocs[2].value.table, &env->gdt); - populate_table_reg(&assocs[3].value.table, &env->idt); - env->cr[2] =3D assocs[4].value.reg64; - - cpu_set_apic_base(x86cpu->apic_state, assocs[5].value.reg64); - - return ret; } =20 -static int mshv_get_registers_vp_page(CPUState *cpu) +static void get_registers_vp_page(CPUState *cpu) { - int ret; - /* General Purpose Registers */ - mshv_get_standard_regs_vp_page(cpu); + get_standard_regs_vp_page(cpu); =20 - /* Special Registers - makes a hypercall */ - ret =3D mshv_get_special_regs_vp_page(cpu); - if (ret < 0) { - error_report("failed to get special registers for vp page"); - return -1; - } - - return 0; + /* Special Registers */ + get_special_regs_vp_page(cpu); } =20 =20 @@ -735,29 +701,26 @@ static int get_special_regs(CPUState *cpu) return 0; } =20 -static int load_regs(CPUState *cpu) +static void load_regs(CPUState *cpu) { X86CPU *x86_cpu =3D X86_CPU(cpu); CPUX86State *env =3D &x86_cpu->env; - int ret; - - /* Use register vp page to optimize registers access */ - if (env->regs_page && env->regs_page->isvalid !=3D 0) { - ret =3D mshv_get_registers_vp_page(cpu); - return ret; - } =20 - ret =3D get_standard_regs(cpu); - if (ret < 0) { - return ret; + /* Check register page pointer and abort if in unexpected state */ + if (!env->regs_page) { + error_report( + "load regs: register page not set for vcpu %d", + cpu->cpu_index); + abort(); } - - ret =3D get_special_regs(cpu); - if (ret < 0) { - return ret; + if (env->regs_page->isvalid =3D=3D 0) { + error_report( + "load regs: register page invalid for vcpu %d", + cpu->cpu_index); + abort(); } =20 - return 0; + get_registers_vp_page(cpu); } =20 static int get_vcpu_events(CPUState *cpu) @@ -1565,11 +1528,7 @@ static int emulate_instruction(CPUState *cpu, int ret; x86_insn_stream stream =3D { .bytes =3D insn_bytes, .len =3D insn_len = }; =20 - ret =3D load_regs(cpu); - if (ret < 0) { - error_report("Failed to load registers"); - return -1; - } + load_regs(cpu); =20 decode_instruction_stream(env, &decode, &stream); exec_instruction(env, &decode); @@ -1872,11 +1831,7 @@ static int handle_pio_str(CPUState *cpu, hv_x64_io_p= ort_intercept_message *info) X86CPU *x86_cpu =3D X86_CPU(cpu); CPUX86State *env =3D &x86_cpu->env; =20 - ret =3D load_regs(cpu); - if (ret < 0) { - error_report("Failed to load registers"); - return -1; - } + load_regs(cpu); =20 direction_flag =3D (env->eflags & DESC_E_MASK) !=3D 0; =20 @@ -2013,7 +1968,7 @@ static void read_segment_descriptor(CPUState *cpu, =20 /* * SegmentCache stores the hypervisor-provided value verbatim (populat= ed by - * mshv_load_regs). We need to convert it to format expected by the + * load_regs). We need to convert it to format expected by the * instruction emulator. We can have a limit value > 0xfffff with * granularity of 0 (byte granularity), which is not representable * in real x86_segment_descriptor. In this case we set granularity to 1 --=20 2.53.0 From nobody Sun Jul 26 10:06:44 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=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1783462669; cv=none; d=zohomail.com; s=zohoarc; b=E0Qgvb5IxJX6LHPfizOa4IyyxgFhvU8KaDbXbON9EGcvM0LA/Jlw/ZZK9tOG5yNkQQno3qaEuzudGIOY3IXDllTAjyL1e99O4+uF2phE4IMJQnhj6RXom40k9tiT76kLo5GSzknx0mZwOKhZVtjFmWDrpZSKP5rDxM9pPUxOS8o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783462669; h=Content-Type: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=CMGlnLFkWL8URMY81GJqwDmjtZXhLunIy+myzRaU5/w=; b=hizXscUseOW+cPLMfcC1d9ClyN+sCsWEuW7J1UtTkdaFF8V0ZXXOb5UtnZo0tSmt9bjHwdAmZ8zW7PWk0y9Sxb4PS2UEluISNQUXOWFrC0FdLsQIvb5ZlrJeux2KXyzyU2sJ8iMuKCzgMPy5Tq+553DXC2/BO1ZCFYQxW0e9QHk= 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 lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1783462669063772.3799062580053; Tue, 7 Jul 2026 15:17:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whE6H-0003uR-5h; Tue, 07 Jul 2026 18:17:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1whE6F-0003tv-Ps for qemu-devel@nongnu.org; Tue, 07 Jul 2026 18:17:07 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whE6E-0000Ld-65 for qemu-devel@nongnu.org; Tue, 07 Jul 2026 18:17:07 -0400 Received: from laptop.localdomain (unknown [86.121.140.206]) by linux.microsoft.com (Postfix) with ESMTPSA id 7AE3F20B7166; Tue, 7 Jul 2026 15:16:58 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 7AE3F20B7166 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1783462620; bh=CMGlnLFkWL8URMY81GJqwDmjtZXhLunIy+myzRaU5/w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JRb3eGT3uk6MYx+6g29cnK/m7KwfasLZFxMXp+KP1lh4EUbSWhE7fu7Hf7eeugS2p DXqAe9THE8ixIXXP2Zc1uXa4mL7tcWfD21G5mAmKOnQO5EPsIYn2nPUph9ETUFMenL fgcSDwfUL2tmS0EZVa9vJhLhNE5+gDAWuaTEXndA= From: =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Wei Liu , Magnus Kulke , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Wei Liu Subject: [PATCH 4/5] target/i386/mshv: remove fallback for register page set registers Date: Wed, 8 Jul 2026 01:16:44 +0300 Message-ID: <20260707221645.24557-5-dblanzeanu@linux.microsoft.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260707221645.24557-1-dblanzeanu@linux.microsoft.com> References: <20260707221645.24557-1-dblanzeanu@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=lists1p.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=dblanzeanu@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, 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: 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 @linux.microsoft.com) X-ZM-MESSAGEID: 1783462671169158500 Update `store_regs` to use the register page when it is mmapped and valid to set registers. Remove the ioctl based register retrieval and fail in case the register page is not correctly set or valid. Fixes: 80c7f8e9cd Signed-off-by: Doru Bl=C3=A2nzeanu Reviewed-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 48 +++++++++++++------------------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 9ec5c19c67..8a59a0b40e 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -448,7 +448,7 @@ static int set_standard_regs(const CPUState *cpu) return 0; } =20 -static void mshv_set_standard_regs_vp_page(CPUState *cpu) +static void set_standard_regs_vp_page(CPUState *cpu) { X86CPU *x86cpu =3D X86_CPU(cpu); CPUX86State *env =3D &x86cpu->env; @@ -478,29 +478,26 @@ static void mshv_set_standard_regs_vp_page(CPUState *= cpu) | (1u << HV_X64_REGISTER_CLASS_FLAGS); } =20 -static int store_regs(CPUState *cpu) +static void store_regs(CPUState *cpu) { X86CPU *x86cpu =3D X86_CPU(cpu); CPUX86State *env =3D &x86cpu->env; - int ret; =20 - /* Use register vp page to optimize registers access */ - if (env->regs_page && env->regs_page->isvalid !=3D 0) { - mshv_set_standard_regs_vp_page(cpu); - } else { - ret =3D set_standard_regs(cpu); - if (ret < 0) { - return ret; - } + /* Check register page pointer and abort if in unexpected state */ + if (!env->regs_page) { + error_report( + "store regs: register page not set for vcpu %d", + cpu->cpu_index); + abort(); } - - ret =3D set_special_regs(cpu); - if (ret < 0) { - error_report("Failed to store speical registers"); - return ret; + if (env->regs_page->isvalid =3D=3D 0) { + error_report( + "store regs: register page invalid for vcpu %d", + cpu->cpu_index); + abort(); } =20 - return 0; + set_standard_regs_vp_page(cpu); } =20 static void populate_standard_regs(const hv_register_assoc *assocs, @@ -1518,14 +1515,13 @@ static int set_memory_info(const struct hyperv_mess= age *msg, return 0; } =20 -static int emulate_instruction(CPUState *cpu, +static void emulate_instruction(CPUState *cpu, const uint8_t *insn_bytes, size_t insn_len, uint64_t gva, uint64_t gpa) { X86CPU *x86_cpu =3D X86_CPU(cpu); CPUX86State *env =3D &x86_cpu->env; struct x86_decode decode =3D { 0 }; - int ret; x86_insn_stream stream =3D { .bytes =3D insn_bytes, .len =3D insn_len = }; =20 load_regs(cpu); @@ -1533,13 +1529,7 @@ static int emulate_instruction(CPUState *cpu, decode_instruction_stream(env, &decode, &stream); exec_instruction(env, &decode); =20 - ret =3D store_regs(cpu); - if (ret < 0) { - error_report("failed to store registers"); - return -1; - } - - return 0; + store_regs(cpu); } =20 static int handle_mmio(CPUState *cpu, const struct hyperv_message *msg, @@ -1575,13 +1565,9 @@ static int handle_mmio(CPUState *cpu, const struct h= yperv_message *msg, =20 instruction_bytes =3D info.instruction_bytes; =20 - ret =3D emulate_instruction(cpu, instruction_bytes, insn_len, + emulate_instruction(cpu, instruction_bytes, insn_len, info.guest_virtual_address, info.guest_physical_address); - if (ret < 0) { - error_report("failed to emulate mmio"); - return -1; - } =20 *exit_reason =3D MshvVmExitIgnore; =20 --=20 2.53.0 From nobody Sun Jul 26 10:06:44 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=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1783462685; cv=none; d=zohomail.com; s=zohoarc; b=KmMwI+Zi4UkA7ZGY1mTe3vK0FreneQRmUwCvA8qLo84s4nc2+RzzxxmL/lxVMZ0TowyxBAjQLNZFC5uf6pSnqLZsVUipAL1GABId8rbiHlDWSISST7af9TVlGfd+ktO1YaHqUDCZrCyzv5SBQ0EYRiR6GLi0ou2jmkTpY1PE7no= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783462685; h=Content-Type: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=s3tJ+KAIDTg3lGQufzfBhUJka+0u7JQQUAk3SRLSGD8=; b=I0YtHGH84WBlSi0UQC4dijsGtf5zLJ2plQgZmw1m8kRIg6ac10LOd0hl+PiBbGbb9DyQL0OVGadQDLm2ZyFpWQ5yZPF+UpVzG7GoQnikip2v1bvqiB4Nn1JaJKOHR75sNlM6xrq3V2FKqq3DlPbIg2XBVMda3h4kcV39/yqSBfI= 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 lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1783462685120996.9560597902591; Tue, 7 Jul 2026 15:18:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whE6L-0003uk-8T; Tue, 07 Jul 2026 18:17:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1whE6I-0003uX-R5 for qemu-devel@nongnu.org; Tue, 07 Jul 2026 18:17:10 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1whE6H-0000M8-4N for qemu-devel@nongnu.org; Tue, 07 Jul 2026 18:17:10 -0400 Received: from laptop.localdomain (unknown [86.121.140.206]) by linux.microsoft.com (Postfix) with ESMTPSA id 2F0DB20B7167; Tue, 7 Jul 2026 15:17:01 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 2F0DB20B7167 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1783462623; bh=s3tJ+KAIDTg3lGQufzfBhUJka+0u7JQQUAk3SRLSGD8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qNj4PjEVLAXQk2+QfjXbvvmLOisw+w1LDmULqedldQY5AS9vsHji36XZbRovRbpLP OkwjP8fD7kWdZwhSpYhaRSVg9ecTj6jac3Wqhg6r6017iUlZVFZPYX1PlAahd13O0+ micupI2KeUy8SEG6LU7DIbpxPJNlnJk++mI4by50= From: =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Wei Liu , Magnus Kulke , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Wei Liu Subject: [PATCH 5/5] target/i386/mshv: fix pio handlers clobbering device-modified registers Date: Wed, 8 Jul 2026 01:16:45 +0300 Message-ID: <20260707221645.24557-6-dblanzeanu@linux.microsoft.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260707221645.24557-1-dblanzeanu@linux.microsoft.com> References: <20260707221645.24557-1-dblanzeanu@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=lists1p.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=dblanzeanu@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, 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: 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 @linux.microsoft.com) X-ZM-MESSAGEID: 1783462687112158500 When a device handler (e.g. vmport) calls cpu_synchronize_state() during I/O port dispatch, it sets cpu->accel->dirty =3D true and may modify registers directly in env. The old PIO code ignored this: it unconditionally wrote the stale info->rax from the VM-exit intercept message back to the hypervisor and then cleared dirty, discarding any register changes made by the device. Bifurcate both handlers on cpu->accel->dirty: handle_pio_non_str: - dirty path: update env->eip directly. For reads (IN), merge the I/O result into env->regs[R_EAX] (which may have been modified by the device) rather than info->rax. For writes (OUT), leave RAX untouched. Flush all registers via mshv_store_regs() and clear dirty. - non-dirty path: write RIP and RAX via set_x64_registers hypercall as before. handle_pio_str: - dirty path: update env->eip and the appropriate index register (RSI for OUTS, RDI for INS) directly. Flush via mshv_store_regs() and clear dirty. - non-dirty path: write the index register and RIP via set_x64_registers. Drop the RAX assignment that was here before; string I/O does not modify RAX, and set_x64_registers is hardcoded to write only 2 registers so the third slot was silently ignored anyway. Remove the unconditional "cpu->accel->dirty =3D false" at the end of both handlers. In the non-dirty fast path it was redundant (already false). In the dirty path it was actively harmful: it told the vcpu run loop that env was clean when it was not, losing the device's modifications. Signed-off-by: Doru Bl=C3=A2nzeanu Reviewed-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 72 +++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 8a59a0b40e..5e680351cd 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -1646,10 +1646,12 @@ static int handle_pio_non_str(CPUState *cpu, uint32_t val, eax; const uint32_t eax_mask =3D 0xffffffffu >> (32 - len * 8); size_t insn_len; - uint64_t rip, rax; + uint64_t rip; uint32_t reg_names[2]; uint64_t reg_values[2]; uint16_t port =3D info->port_number; + X86CPU *x86_cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86_cpu->env; =20 if (access_type =3D=3D HV_X64_INTERCEPT_ACCESS_TYPE_WRITE) { union { @@ -1680,21 +1682,36 @@ static int handle_pio_non_str(CPUState *cpu, =20 /* Advance RIP and update RAX */ rip =3D info->header.rip + insn_len; - rax =3D info->rax; =20 - reg_names[0] =3D HV_X64_REGISTER_RIP; - reg_values[0] =3D rip; - reg_names[1] =3D HV_X64_REGISTER_RAX; - reg_values[1] =3D rax; + if (cpu->vcpu_dirty) { + env->eip =3D rip; + if (access_type !=3D HV_X64_INTERCEPT_ACCESS_TYPE_WRITE) { + /* + * For reads, merge the I/O result into the current RAX. + * Use env->regs[R_EAX] as the base since a device handler + * (e.g. vmport) may have called cpu_synchronize_state() + * and modified registers. + */ + eax =3D (((uint32_t)env->regs[R_EAX]) & ~eax_mask) + | (val & eax_mask); + env->regs[R_EAX] =3D (uint64_t)eax; + } + /* Sync modified standard registers back and clear dirty. */ + store_regs(cpu); + cpu->vcpu_dirty =3D false; + } else { + reg_names[0] =3D HV_X64_REGISTER_RIP; + reg_values[0] =3D rip; + reg_names[1] =3D HV_X64_REGISTER_RAX; + reg_values[1] =3D info->rax; =20 - ret =3D set_x64_registers(cpu, reg_names, reg_values); - if (ret < 0) { - error_report("Failed to set x64 registers"); - return -1; + ret =3D set_x64_registers(cpu, reg_names, reg_values); + if (ret < 0) { + error_report("Failed to set x64 registers"); + return -1; + } } =20 - cpu->vcpu_dirty =3D false; - return 0; } =20 @@ -1810,6 +1827,7 @@ static int handle_pio_str(CPUState *cpu, hv_x64_io_po= rt_intercept_message *info) bool repop =3D info->access_info.rep_prefix =3D=3D 1; size_t repeat =3D repop ? info->rcx : 1; size_t insn_len =3D info->header.instruction_length; + uint64_t rip; bool direction_flag; uint32_t reg_names[3]; uint64_t reg_values[3]; @@ -1839,18 +1857,28 @@ static int handle_pio_str(CPUState *cpu, hv_x64_io_= port_intercept_message *info) reg_values[0] =3D info->rdi; } =20 - reg_names[1] =3D HV_X64_REGISTER_RIP; - reg_values[1] =3D info->header.rip + insn_len; - reg_names[2] =3D HV_X64_REGISTER_RAX; - reg_values[2] =3D info->rax; + rip =3D info->header.rip + insn_len; =20 - ret =3D set_x64_registers(cpu, reg_names, reg_values); - if (ret < 0) { - error_report("Failed to set RIP and RAX registers"); - return -1; - } + if (cpu->vcpu_dirty) { + env->eip =3D rip; + if (access_type =3D=3D HV_X64_INTERCEPT_ACCESS_TYPE_WRITE) { + env->regs[R_ESI] =3D info->rsi; + } else { + env->regs[R_EDI] =3D info->rdi; + } + /* Sync modified standard registers back and clear dirty. */ + store_regs(cpu); + cpu->vcpu_dirty =3D false; + } else { + reg_names[1] =3D HV_X64_REGISTER_RIP; + reg_values[1] =3D rip; =20 - cpu->vcpu_dirty =3D false; + ret =3D set_x64_registers(cpu, reg_names, reg_values); + if (ret < 0) { + error_report("Failed to set x64 registers"); + return -1; + } + } =20 return 0; } --=20 2.53.0