From nobody Mon Nov 23 05:00:15 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=1791362528; cv=none; d=zohomail.com; s=zohoarc; b=Eu/OoSoSBrfWjDKj/f5hdtuipbRbdYHTwFa+JAC/tPR7hjsts+zi6DHv6LogrHtSCXC9Q63o61s+nEocjfYvXJIC76XNFuboslQtle8/dF7kE4v1kzR5bMb+lu88aJEqdbRGH9au72X7yuwr+hiugGaC9SWQQMEFnVa2X1QB1DE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791362528; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=EUCWfuW0peymBNY/EsRl+OrEPqZyXHIPcNkVNlx/pMw=; b=d+GDQuT7YM0b/0AInQFjeNnpnOTxhv0E2NvruYzCO5cBitlTSXqe2+zsavbttVfcEU2nu+YiJ4uf4zfwev7LSz92vrliUMWdB+q6KsX4E9Kct4PhtnIYz2TrLVFUyQozonV1WckLXubU4WAOFeTb6ftK50RutBvKMD96aE5Q9us= 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 1791362528607488.9922363274736; Wed, 7 Oct 2026 01:42:08 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xENBl-0000vj-Fn; Wed, 07 Oct 2026 04:39:57 -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 1xEIsw-0008Vd-UV for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:11 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEIsr-0004ZV-Ki for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:04 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.46]) by linux.microsoft.com (Postfix) with ESMTPSA id F25D220B7168; Tue, 6 Oct 2026 09:48:32 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com F25D220B7168 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1791305314; bh=EUCWfuW0peymBNY/EsRl+OrEPqZyXHIPcNkVNlx/pMw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P/I2AG4p33fM+dnGY1QgDK0CoYlUV8QDRvbKUfrHjorQhCEkS1na0tBBJFOio70Ur YS3WMVBF5MzAZpJi6mugXoU/+AzJOPKjyGy3pgvuvyARbSEryol2wUbrrsyPF/OCr9 OcK+J+ty/eMstwkAoH4bJPKfyANlIJVFbw0PXhmg= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Paolo Bonzini Subject: [PULL 01/11] target/i386/mshv: fix read/write memory across the page boundary Date: Tue, 6 Oct 2026 18:48:55 +0200 Message-Id: <20261006164905.776466-2-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261006164905.776466-1-magnuskulke@linux.microsoft.com> References: <20261006164905.776466-1-magnuskulke@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=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, 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: 1791362530097158500 From: Doru Bl=C3=A2nzeanu Previously, read_memory and write_memory performed a single GVA-to-GPA translation for the entire buffer. If the buffer spanned a page boundary, the translated GPA was only valid for the first page, causing incorrect reads/writes for the remainder. Fix both functions to loop over pages, translating and accessing each page-aligned chunk separately. Signed-off-by: Doru Bl=C3=A2nzeanu Fixes: 6dec60528c (target/i386/mshv: Implement mshv_vcpu_run()) Reviewed-by: Mohamed Mediouni Reviewed-by: Magnus Kulke Signed-off-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 69 +++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index f528dd2b9a..b8a6924088 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -1831,21 +1831,38 @@ static int read_memory(const CPUState *cpu, uint64_= t initial_gva, { int ret; uint64_t gpa, flags; + uint64_t cur_gva =3D gva; + size_t page_left, chunk; + uint8_t *cur_data =3D data; =20 - if (gva =3D=3D initial_gva) { - gpa =3D initial_gpa; - } else { - flags =3D HV_TRANSLATE_GVA_VALIDATE_READ; - ret =3D translate_gva(cpu, gva, &gpa, flags); - if (ret < 0) { - return -1; + /* + * If the read spans multiple pages, + * we need to translate and read each page separately + */ + while (len > 0) { + page_left =3D HV_HYP_PAGE_SIZE - (cur_gva & (HV_HYP_PAGE_SIZE - 1)= ); + chunk =3D MIN(len, page_left); + + if (cur_gva =3D=3D initial_gva) { + gpa =3D initial_gpa; + } else { + flags =3D HV_TRANSLATE_GVA_VALIDATE_READ; + ret =3D translate_gva(cpu, cur_gva, &gpa, flags); + if (ret < 0) { + return -1; + } } =20 - ret =3D mshv_guest_mem_read(gpa, data, len, false, false); + ret =3D mshv_guest_mem_read(gpa, cur_data, chunk, + false, false); if (ret < 0) { error_report("failed to read guest mem"); return -1; } + + cur_gva +=3D chunk; + cur_data +=3D chunk; + len -=3D chunk; } =20 return 0; @@ -1856,18 +1873,34 @@ static int write_memory(const CPUState *cpu, uint64= _t gva, const uint8_t *data, { int ret; uint64_t gpa, flags; + uint64_t cur_gva =3D gva; + size_t page_left, chunk; + const uint8_t *cur_data =3D data; =20 - flags =3D HV_TRANSLATE_GVA_VALIDATE_WRITE; - ret =3D translate_gva(cpu, gva, &gpa, flags); - if (ret < 0) { - error_report("failed to translate gva to gpa"); - return -1; - } + /* + * If the write spans multiple pages, + * we need to translate and write each page separately + */ + while (len > 0) { + page_left =3D HV_HYP_PAGE_SIZE - (cur_gva & (HV_HYP_PAGE_SIZE - 1)= ); + chunk =3D MIN(len, page_left); =20 - ret =3D mshv_guest_mem_write(gpa, data, len, false); - if (ret !=3D MEMTX_OK) { - error_report("failed to write to mmio"); - return -1; + flags =3D HV_TRANSLATE_GVA_VALIDATE_WRITE; + ret =3D translate_gva(cpu, cur_gva, &gpa, flags); + if (ret < 0) { + error_report("failed to translate gva to gpa"); + return -1; + } + + ret =3D mshv_guest_mem_write(gpa, cur_data, chunk, false); + if (ret !=3D MEMTX_OK) { + error_report("failed to write to mmio"); + return -1; + } + + cur_gva +=3D chunk; + cur_data +=3D chunk; + len -=3D chunk; } =20 return 0; --=20 2.34.1 From nobody Mon Nov 23 05:00:15 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=1791362296; cv=none; d=zohomail.com; s=zohoarc; b=NFzHQLNzCVlij6PYyzSKjqCU3Xggkh66xlXk1HahtxikJwNFIwebfrG84S0P6+19zmruBwt81kIi6kLaSJCafpVYEZoi5AcHDYFR4r0pxLxnt3Xo5PTVlcqoAWvS00FFveNMioUmM9IZLxNvzXvT1yyv07QlZCeN3Twra3r5xt4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791362296; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=sS6+v/KG0bNpCvXev6mchI5kX9JuCuUyBiKRAWCn5Uk=; b=H+IIk2HUyEOe3lHDk8w6iwfUFzWiYEdptiJUH7MmCdVLK7Q5YG3DkIOAQPyvWwbRU4j6hpyKkWH4ASvwZpAVWjDStQoIEfm7khCNHKIPsWEFUAooZc2f1wP7p/ZeX3VitrGkZ1+J+kcxCklaHOsU6LqbTDk/3KXZbO6B1wK7Yb4= 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 1791362296897832.816152870674; Wed, 7 Oct 2026 01:38:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEN5B-00053m-ES; Wed, 07 Oct 2026 04:33: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 1xEHrM-0007BJ-Hs for qemu-devel@nongnu.org; Tue, 06 Oct 2026 22:58:29 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEHr4-0005OS-L0 for qemu-devel@nongnu.org; Tue, 06 Oct 2026 22:58:11 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.46]) by linux.microsoft.com (Postfix) with ESMTPSA id A419A20B7169; Tue, 6 Oct 2026 09:48:35 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A419A20B7169 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1791305316; bh=sS6+v/KG0bNpCvXev6mchI5kX9JuCuUyBiKRAWCn5Uk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p+ED14+rnZ3nKg7wdvRA4zrK4r4yOKnZ97h1qT07hIWLoiwcHHwhFzWW+NBVan9gu dP+Paq4tCHH1T4zMCuJ4SP3Jky/MYU9j3w+KMckZTiGo2w9vQoTRHMBvMPEeiIxSxi x/1xCr6zFDRcxxWFm4K3MntEf1ePfMVqzCPCrnAE= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Paolo Bonzini Subject: [PULL 02/11] include/hw/hyperv: move hv_vp_register_page struct definition Date: Tue, 6 Oct 2026 18:48:56 +0200 Message-Id: <20261006164905.776466-3-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261006164905.776466-1-magnuskulke@linux.microsoft.com> References: <20261006164905.776466-1-magnuskulke@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=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, 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: 1791362298929158500 From: Doru Bl=C3=A2nzeanu 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 Signed-off-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 a5527d49cf..4ae20d2ac1 100644 --- a/include/hw/hyperv/hvgdk_mini.h +++ b/include/hw/hyperv/hvgdk_mini.h @@ -508,109 +508,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.34.1 From nobody Mon Nov 23 05:00:15 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=1791363120; cv=none; d=zohomail.com; s=zohoarc; b=Pf3cNIbG1SjCPvhAaGJpDG4wWMHHGU9Vuv1Zt/POs2TK0mYj70D8JYgGqMxo++z+ry3RNHJ5AEa2IWJdJ54pxsTuiOoKimShj1Yzyqv5RUpOjjburwt06v+K2ivg4Rii5Cp4LNhknetBOUts5Z6/GSkT/17lj4ydM/pdNjglYNk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791363120; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=9q6qbvRTkwKTu3YCkBzK1EfhMChrRvZ9BhOeFnGLE0k=; b=eElFAsQAVEb18ttRknAvmbvf/eqES9Df5ITGO+IyPJB1uBqIyh9Bd/YIQD/o4YiDXP23ePwL81dMiLcglZe9sb1JZG2BAhBecVDuWSAAuHWZGC9ZeuFI4KUgU/LVRhietyTqpqqik2IHgp1IVzUXibXu0crXbb0kJdRzu0GNwug= 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 1791363120778714.7901604552725; Wed, 7 Oct 2026 01:52:00 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xENE5-0004QV-9U; Wed, 07 Oct 2026 04:42: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 1xEIt0-0008WU-Q2 for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:19 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEIsy-0004cD-8i for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:10 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.46]) by linux.microsoft.com (Postfix) with ESMTPSA id 6EF9820B716A; Tue, 6 Oct 2026 09:48:38 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 6EF9820B716A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1791305319; bh=9q6qbvRTkwKTu3YCkBzK1EfhMChrRvZ9BhOeFnGLE0k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H7FilEUk5zg5yBpO/l52LFIQ49f1CAHu/lZ+uExmarXXtx3otQ+dEqSS1ANk/4ggP CSOf/f7oD2Lpk80EVOKMO5zjDQ6M10M7Q2zTpSS41jInPnk8i8LfcAA+2Tsc1Otvb4 ZTvLQ2hyqUC+B/p624+i+stMzXJgzQXfhuKQBNaM= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Paolo Bonzini Subject: [PULL 03/11] target/i386/mshv: abort when hv_vp_register_page setup fails Date: Tue, 6 Oct 2026 18:48:57 +0200 Message-Id: <20261006164905.776466-4-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261006164905.776466-1-magnuskulke@linux.microsoft.com> References: <20261006164905.776466-1-magnuskulke@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=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, 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: 1791363121574158500 From: Doru Bl=C3=A2nzeanu 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 Signed-off-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 b8a6924088..d236b9461f 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -2154,6 +2154,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; @@ -2165,15 +2166,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.34.1 From nobody Mon Nov 23 05:00:15 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=1791363890; cv=none; d=zohomail.com; s=zohoarc; b=I+yKgBdcrwqYuaIyYHZxB5NciHAZGSJ2LO1t8zBo7DvoScdXecTEaywWdbSTtNHVBWVMnR8uGQcCVYOqLY7yL33M+SiGpoQA8LkQ/lZDJiuZ9R/bcD6NfqrM2nOV70NnmyGeLz8Pd0L86uGvnyZWKh4R2HVRwfusEXDxsCuqr4g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791363890; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=KCY+msSUAAWnpPXgA8t8rOn1n03rmATYG58A1AjMq24=; b=D6RxeFVL/bfedCi3hELP0Uwu8GF+hSr256XSOi7UHwACu/Dy34irKzGBrDt7nHsKJ6ispievdPzpMjNPBj7c39CDf2v+cwP0IVxZtArBYeF0gsqyyCa2BM8qLyTvCpz+NVVVjILXiCcZWUdK26jMbzgQcS9FKYPGYXrEYkZxgT8= 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 1791363890615537.8902830043534; Wed, 7 Oct 2026 02:04:50 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xENE2-00049V-R1; Wed, 07 Oct 2026 04:42:10 -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 1xEIsw-0008Vh-VW for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:11 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEIsr-0004ZK-Jy for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:04 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.46]) by linux.microsoft.com (Postfix) with ESMTPSA id 04E8F20B716B; Tue, 6 Oct 2026 09:48:40 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 04E8F20B716B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1791305322; bh=KCY+msSUAAWnpPXgA8t8rOn1n03rmATYG58A1AjMq24=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SzUQsl5QFmoFre5Myy3z8S6Apd1AfGSVvi85NFVfJEmj3bUnozd+Tjv7/YjcqtfpD Ep87fXPiWE6E0s4qu9TShuO0bwM3jdkIJgVR+rH4mk4AcH1u91nF8PjUQ2RHSMC4gw IrAnMgi6HoPuS8dfOVd08bmzHWKQ9bcbllnigcrI= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Paolo Bonzini Subject: [PULL 04/11] target/i386/mshv: remove fallback for register page get registers Date: Tue, 6 Oct 2026 18:48:58 +0200 Message-Id: <20261006164905.776466-5-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261006164905.776466-1-magnuskulke@linux.microsoft.com> References: <20261006164905.776466-1-magnuskulke@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=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, 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: 1791363891973158500 From: Doru Bl=C3=A2nzeanu 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 Signed-off-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 d236b9461f..fe1645f87d 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -307,15 +307,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) { @@ -652,7 +643,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; @@ -680,13 +671,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 */ @@ -709,46 +706,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 @@ -772,29 +738,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) @@ -1637,11 +1600,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); @@ -1977,11 +1936,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 @@ -2118,7 +2073,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.34.1 From nobody Mon Nov 23 05:00:15 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=1791363413; cv=none; d=zohomail.com; s=zohoarc; b=i5t+Y71/5A/vN27vFyLcDF9LvfBLPbD6djLyHjgA3f5gIgXh2cxL513R73LIssd+Sz46QoULeAJWhalZg4S+styna5dTPWuItvCqL/av3/IqR9F3JM/tceauxmhb+KDp508HLdD+ishtC05MupY2mCyOxsCenogVJvmVxREsMes= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791363413; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=XTYPPY2H2Ob4ucufyR35QTIL+2W6byD+ZTLIgdA99Pc=; b=Kv+HIqRcTybzjwwYQvcUugWUzlBYK5a9rcWBPceCWm08eyxhHhoXN8yspfpAIFIhYiHoiJ9PKjYECe08LC7NaWBmBDSCwHkRRws2ra8mF7ndyxyhQMy3FsinusByZ24zU70DVnmeTWUWFfHiQmWCcmHuba07/WyF3eWAthk3zuw= 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 1791363413637613.3137244396626; Wed, 7 Oct 2026 01:56:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xENEK-0005zq-T1; Wed, 07 Oct 2026 04:42:28 -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 1xEIsw-0008Ve-V6 for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:11 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEIsr-0004Zl-Es for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:04 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.46]) by linux.microsoft.com (Postfix) with ESMTPSA id BCF7220B716C; Tue, 6 Oct 2026 09:48:43 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com BCF7220B716C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1791305324; bh=XTYPPY2H2Ob4ucufyR35QTIL+2W6byD+ZTLIgdA99Pc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cpr+IlHk2A+plLEXXVkSnfJCbKpDaO+sIQfPIoBGdVd6+9KvNFWxRMcZeOyL2ByAi Fh/qi6iCeBncBqJrtRzbLwYRtPeBIKm3AVGaZ/52EPWneUTioFBkr/glsKQKVLbze/ oNiINGqGYns7c4gG/juoYpRHqt9dvL6mneVZhJyM= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Paolo Bonzini Subject: [PULL 05/11] target/i386/mshv: remove fallback for register page set registers Date: Tue, 6 Oct 2026 18:48:59 +0200 Message-Id: <20261006164905.776466-6-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261006164905.776466-1-magnuskulke@linux.microsoft.com> References: <20261006164905.776466-1-magnuskulke@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=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, 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: 1791363415024158500 From: Doru Bl=C3=A2nzeanu 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 Signed-off-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 fe1645f87d..5a34357693 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -485,7 +485,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; @@ -515,29 +515,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, @@ -1590,14 +1587,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); @@ -1605,13 +1601,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, @@ -1647,13 +1637,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.34.1 From nobody Mon Nov 23 05:00:15 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=1791362921; cv=none; d=zohomail.com; s=zohoarc; b=WMLv6JeAqH2XssUmxD8S1+sgVkDKKI26U4pxagi1fRfbvabCWiUWoA2yqGEX0Mc5qqvofmsl4QY0CftznWL4StfLpatrba5MsGLnC2Ag8PijwkQmqogdBp6OmzudXwRpwfEQc/QgqCxP9nSZUocgPnc6akBJTLucCM+b6yqdAsI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791362921; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=AL4sp3TkGvdtEM1QJ08Inmb4FA5wGCa7jieuaUQo7KY=; b=GRMMNEUNTMSov8NlCjMU71bRqZ1qK9ysb2J9d5+EzXTebA6Uxp8zMeNfBHU1IWMr7k1SkYVQs89rfleDP8/X5OPzNBXcOJwrQxeok4AbvmnQlqKbeT3sk2Wxv77jllXXjAqcV2WdketMdBH8Ff78cEEhQkQH88LSaP7O+qsrkHo= 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 1791362921329679.9194485486751; Wed, 7 Oct 2026 01:48:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xENEC-00054t-B3; Wed, 07 Oct 2026 04:42:20 -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 1xEIt2-000057-C0 for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:28 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEIsy-0004cB-7w for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:11 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.46]) by linux.microsoft.com (Postfix) with ESMTPSA id 53FE420B716D; Tue, 6 Oct 2026 09:48:46 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 53FE420B716D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1791305327; bh=AL4sp3TkGvdtEM1QJ08Inmb4FA5wGCa7jieuaUQo7KY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cPcerY4JvuJtVTCrxvuhpzBi3J4hmeZPARiLwaZwiJOQuQ0xPL992pNc7DYej4g0B Ac/lxQObqVcPPyFj8r3/sFMpom7poaBhzU5mDSGx0GxYwgkGgpg31eqXFmKPtsjsJ2 5aXKXkJOecPdJkzSq2UmjlNMvAaD4J4NsU7+kAp4= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Paolo Bonzini Subject: [PULL 06/11] target/i386/mshv: fix pio handlers clobbering device-modified registers Date: Tue, 6 Oct 2026 18:49:00 +0200 Message-Id: <20261006164905.776466-7-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261006164905.776466-1-magnuskulke@linux.microsoft.com> References: <20261006164905.776466-1-magnuskulke@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=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, 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: 1791362922196158500 From: Doru Bl=C3=A2nzeanu 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 Signed-off-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 5a34357693..4f113da02d 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -1718,10 +1718,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 { @@ -1752,21 +1754,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 @@ -1915,6 +1932,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]; @@ -1944,18 +1962,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.34.1 From nobody Mon Nov 23 05:00:15 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=1791362823; cv=none; d=zohomail.com; s=zohoarc; b=FA3esv2yzovNMCkfrCGx7QsInU9gvNPgBdBabiO69BLCLqrgEL09+HtVI8qkbbK9Q5qrw0y7nZOshhEvbpbuq01R8n9wcabc2gF+HwEZvy/VrZ1rgqbUIhy6Ain2ncBMLzdQmUWHkdJ8YYpV1r3EiphHzZmEs7NbD9iylN7gcko= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791362823; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xI6tWcZTCbLAWkPtNSLKLdgb7p63zKUKpAxAVRvdW+Q=; b=gvCHah/TyNuS2UAWCh3HxEiRGEPA98mD+AZLEICQrWu58fuKfVQFdk9WTlFoCL8SFipSzhXDztPJuha8YEnfPvn2LhhEv3n2Z4PF8ARkiwiwccI7dVDJCQZOdGg8WdohDjBI/lxUwcQwZd4RlBrJjfEEuvo0vhgiRo7+jtu39jg= 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 1791362823649502.75500084304974; Wed, 7 Oct 2026 01:47:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xENBR-0000Qe-45; Wed, 07 Oct 2026 04:39:30 -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 1xEIsw-0008Vf-VB for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:11 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEIsr-0004ZH-Dx for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:04 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.46]) by linux.microsoft.com (Postfix) with ESMTPSA id 7D93720B716E; Tue, 6 Oct 2026 09:48:49 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 7D93720B716E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1791305330; bh=xI6tWcZTCbLAWkPtNSLKLdgb7p63zKUKpAxAVRvdW+Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C8ogSTiPUPrueXbOuhnc3ZEQTpRNC4pAfvjtt96Z39zZZWSuqvmNsIvGzwjfKSTwL 7TdOGLah2xJHI677BlFVsoShI8z4oKTb/Lq2t6eEGJV/c1WlFurAuteG2rwZIXE+sS sqdNbMQ2I8WwaKP4EuZh4HUZwt8pCLE1gnqvFPwo= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Paolo Bonzini Subject: [PULL 07/11] target/i386/mshv: zero-init msr hvcall args Date: Tue, 6 Oct 2026 18:49:01 +0200 Message-Id: <20261006164905.776466-8-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261006164905.776466-1-magnuskulke@linux.microsoft.com> References: <20261006164905.776466-1-magnuskulke@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=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, 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: 1791362825553158500 In the current code we leave the argument arrays for the get/set hvcall unitialized, setting only individual fields. some fields will be populated during the hvcall, but in any case the arg arrays will contain unitialized data (e.g. in reserved fields), to which static analysis objects to. Fixes: c38eb12c85 Signed-off-by: Magnus Kulke Reviewed-by: Doru Bl=C3=A2nzeanu Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- target/i386/mshv/msr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/target/i386/mshv/msr.c b/target/i386/mshv/msr.c index d3788d7715..96bd552916 100644 --- a/target/i386/mshv/msr.c +++ b/target/i386/mshv/msr.c @@ -328,7 +328,7 @@ int mshv_get_msrs(CPUState *cpu) { int ret =3D 0; size_t n_assocs =3D MSHV_MSR_TOTAL_COUNT; - struct hv_register_assoc assocs[MSHV_MSR_TOTAL_COUNT]; + struct hv_register_assoc assocs[MSHV_MSR_TOTAL_COUNT] =3D { 0 }; size_t i, j; uint32_t name; X86CPU *x86cpu =3D X86_CPU(cpu); @@ -409,7 +409,7 @@ static void load_from_env(const CPUState *cpu, struct h= v_register_assoc *assocs, int mshv_set_msrs(const CPUState *cpu) { size_t n_assocs =3D MSHV_MSR_TOTAL_COUNT; - struct hv_register_assoc assocs[MSHV_MSR_TOTAL_COUNT]; + struct hv_register_assoc assocs[MSHV_MSR_TOTAL_COUNT] =3D { 0 }; int ret; size_t i, j; X86CPU *x86cpu =3D X86_CPU(cpu); --=20 2.34.1 From nobody Mon Nov 23 05:00:15 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=1791362028; cv=none; d=zohomail.com; s=zohoarc; b=GJziR6HZU1iTRNyz6o8ij3PfZBUMCOL34CLKlthK4BizT3VhfF4SNVom81Mw6sL/LlBnPelKe7z4o5j9ezkGPdGVXM2i6N5LdUn/UweeK8TkMvCF2wv5xRiW6SYSPIoeZUZx+1R6IIQLodTlVlCfqYLRoC32oBXXVbiyoDcRUFg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791362028; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=/020cZP/SxmAv3MJqbdOEp/uZAEilcUIUL9EIhRvPUU=; b=QqcWccUtn9o61pkDVA2zenfYaAG4bGv4y1j3cl9cIGfh89xU9hhZUuKuq6kz6wllw1hJljdY2At8dg5sFWh+aekc0wbQ6F0N27WS2X0ciwq72gc/nfmheT9rFHS4DKhOQZnnYVMdi4Oa3TCFlgpCwmn2iqB2ikhnl2KGimoTrtk= 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 1791362028365746.8034502487393; Wed, 7 Oct 2026 01:33:48 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEN3K-0003Uz-PI; Wed, 07 Oct 2026 04:31:07 -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 1xEHta-0007bd-Pf for qemu-devel@nongnu.org; Tue, 06 Oct 2026 23:00:56 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEHtU-0005OR-GW for qemu-devel@nongnu.org; Tue, 06 Oct 2026 23:00:38 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.46]) by linux.microsoft.com (Postfix) with ESMTPSA id 0D3CE20B716F; Tue, 6 Oct 2026 09:48:51 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 0D3CE20B716F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1791305333; bh=/020cZP/SxmAv3MJqbdOEp/uZAEilcUIUL9EIhRvPUU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=esL5t01Bjqg5v2kKsornLr4SWta3LuxI0nVnj2sNRdSgOzG+o6VUKfHW/htkEYN8W zOJ2LMI/yly6mefwDNloZGQ/XtR47CIWr8IIz/pE4MrEliXtRbThxwoDvuZMO3lCO0 y5KB5qm8Cdovwc0i4gOV7oZqWCPTKuHFYtRY+se0= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Paolo Bonzini Subject: [PULL 08/11] target/i386/mshv: Fix cpu feature-leak from host Date: Tue, 6 Oct 2026 18:49:02 +0200 Message-Id: <20261006164905.776466-9-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261006164905.776466-1-magnuskulke@linux.microsoft.com> References: <20261006164905.776466-1-magnuskulke@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=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, 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: 1791362029647158500 In the current implementation there are subtle bugs that have to do with the termination of subleaves, resulting in features reported on the guest that it should not have according to the model. If a guest model has fewer valid subleaves stopping too early leaves unspecified gaps that are filled by the host, resulting in potentially invalid feature combinations, for example: The host reports features: CPUID[EAX=3D01H].ECX.AVX [bit 28] and CPUID[EAX=3D07H,ECX=3D01H].EAX.AVX_VNNI [bit 4] In the current implementation we would skip over 7.1 completely, not registering CPUID responses for the subleaf, resulting in a guest with qemu64 model (which does not feature AVX), reporting AVX_VNNI as available (passthrough from host). We need to walk through the max subleaves of leaf 7 and register them explicitly (also if they're 0). A second bug was about the propagation of "terminator" subleaves, all-zero responses that signal the end. We also want to register this terminator response , so the guest stops enumerating. In the current implementation we weren't doing that. Fixes: 4fa04dd1621 Signed-off-by: Magnus Kulke Reviewed-by: Doru Bl=C3=A2nzeanu --- target/i386/mshv/mshv-cpu.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 4f113da02d..f2b5641231 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -1079,7 +1079,7 @@ static void collect_cpuid_entries(const CPUState *cpu= , GList **cpuid_entries) CPUX86State *env =3D &x86_cpu->env; uint32_t eax, ebx, ecx, edx; uint32_t leaf, subleaf; - uint32_t max_basic_leaf, max_extended_leaf; + uint32_t max_basic_leaf, max_extended_leaf, max_subleaf_7; uint32_t max_subleaf =3D 0x20; uint32_t leaves_with_subleaves[] =3D {0x04, 0x07, 0x0d, 0x0f, 0x10}; int n_subleaf_leaves =3D ARRAY_SIZE(leaves_with_subleaves); @@ -1117,14 +1117,31 @@ static void collect_cpuid_entries(const CPUState *c= pu, GList **cpuid_entries) continue; } =20 + /* + * Valid subleaves for are reported in 7.0:EAX. We need to registe= r all + * subleaves to the maximum subleaf, even if they are 0. Otherwise= the + * host will supply its own values for a unregistered subleaf, whi= ch + * can result in an inconsistent feature set. + */ + if (leaf =3D=3D 0x07) { + cpu_x86_cpuid(env, leaf, 0, &max_subleaf_7, &ebx, &ecx, &edx); + for (subleaf =3D 0; subleaf <=3D max_subleaf_7; subleaf++) { + cpu_x86_cpuid(env, leaf, subleaf, &eax, &ebx, &ecx, &edx); + add_cpuid_entry(cpuid_entries, leaf, subleaf, + eax, ebx, ecx, edx); + } + continue; + } + subleaf =3D 0; while (subleaf < max_subleaf) { cpu_x86_cpuid(env, leaf, subleaf, &eax, &ebx, &ecx, &edx); =20 + /* register the "terminator" to the guest, before breaking */ + add_cpuid_entry(cpuid_entries, leaf, subleaf, eax, ebx, ecx, e= dx); if (eax =3D=3D 0 && ebx =3D=3D 0 && ecx =3D=3D 0 && edx =3D=3D= 0) { break; } - add_cpuid_entry(cpuid_entries, leaf, subleaf, eax, ebx, ecx, e= dx); subleaf++; } } --=20 2.34.1 From nobody Mon Nov 23 05:00:15 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=1791363210; cv=none; d=zohomail.com; s=zohoarc; b=kzlzGUxlm0KVLSotwhL72Bj3ws68UTIr1N8cM1vwH8s+ll03xXF0V4aRxofwFRsXSvAB6R95MfTMBf9s9gKg0OzWjDrSTuOZiy9voMNn8Rlsttz2w/HvB8QEKve5alSlVjwXQG4jAgoc1sekX5XQwdbwRNPtANgSzR3FE1plNmY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791363210; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=2ii5b00CUyR1UNtMlmJCJdrt3NnMzm03IXQ1lMcVGNc=; b=TPOD3QatOP0FeSOvJ4cd5S3LlI4KhTWDiXfvIuCM4E9Hl6UW6Z8tWnT2/BZw3k1vud6902Pg3ToD18jynqeoUM3LkW/kNvw+R93gvQAUGE+DtitZQW520Uyi2iESqimLmPXDhDrpcHgtCsxtBI8bJWzEZsdC42lGAVWYL/Rzs1Q= 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 1791363210115411.54808785862383; Wed, 7 Oct 2026 01:53:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xENCu-0001f5-Dv; Wed, 07 Oct 2026 04:41:02 -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 1xEIsw-0008Vg-VJ for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:11 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEIsr-0004ZN-DS for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:04:04 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.46]) by linux.microsoft.com (Postfix) with ESMTPSA id 7938320B7170; Tue, 6 Oct 2026 09:48:54 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 7938320B7170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1791305335; bh=2ii5b00CUyR1UNtMlmJCJdrt3NnMzm03IXQ1lMcVGNc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ej3nKIi6MZsLXjvRmSTXQYDO52eYmjSwRopdGpRpNMCfwrlzlUgkFNiK1S1kjBQdN uLf0hV9heEIesDYQccNFi1AEiK2VYwbKTUEspPUx6SUFIqD1njcTItUroeEIu61ij0 aTVJoFxEVIRzk/9FX0sReWGfA0MLGtsfLRbn7mM8= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Paolo Bonzini Subject: [PULL 09/11] include/hw/hyperv: add ABI for exception intercepts and pending events Date: Tue, 6 Oct 2026 18:49:03 +0200 Message-Id: <20261006164905.776466-10-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261006164905.776466-1-magnuskulke@linux.microsoft.com> References: <20261006164905.776466-1-magnuskulke@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=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, 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: 1791363212007158500 From: Doru Bl=C3=A2nzeanu Import the hypervisor definitions needed to intercept guest exceptions and to inject events back into a guest, in preparation for gdbstub debugging support in the MSHV accelerator. Add: - union hv_intercept_parameters, struct hv_input_install_intercept and HVCALL_INSTALL_INTERCEPT, used to install an exception intercept on the partition; - struct hv_x64_exception_intercept_message, the message delivered on an intercepted exception, carrying the faulting RIP, the exception vector and the instruction bytes; - HV_X64_PENDING_EVENT_EXCEPTION, the pending-event type for an exception. These definitions mirror the hypervisor headers and have no functional effect on their own. Signed-off-by: Doru Bl=C3=A2nzeanu Reviewed-by: Magnus Kulke Signed-off-by: Magnus Kulke --- include/hw/hyperv/hvgdk_mini.h | 56 ++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/include/hw/hyperv/hvgdk_mini.h b/include/hw/hyperv/hvgdk_mini.h index 4ae20d2ac1..0983e3a58d 100644 --- a/include/hw/hyperv/hvgdk_mini.h +++ b/include/hw/hyperv/hvgdk_mini.h @@ -22,6 +22,9 @@ #define HV_X64_MSR_TSC_FREQUENCY 0x40000022 #define HV_X64_MSR_APIC_FREQUENCY 0x40000023 =20 +/* event_type values for hv_x64_pending_exception_event */ +#define HV_X64_PENDING_EVENT_EXCEPTION 0 + typedef enum hv_register_name { /* VP Management Registers */ HV_REGISTER_INTERNAL_ACTIVITY_STATE =3D 0x00000004, @@ -240,6 +243,29 @@ enum hv_intercept_type { HV_INTERCEPT_TYPE_INVALID =3D 0XFFFFFFFF, }; =20 +union hv_intercept_parameters { + /* HV_INTERCEPT_PARAMETERS is defined to be an 8-byte field. */ + uint64_t as_uint64; + /* HV_INTERCEPT_TYPE_X64_IO_PORT */ + uint16_t io_port; + /* HV_INTERCEPT_TYPE_X64_CPUID */ + uint32_t cpuid_index; + /* HV_INTERCEPT_TYPE_X64_APIC_WRITE */ + uint32_t apic_write_mask; + /* HV_INTERCEPT_TYPE_EXCEPTION */ + uint16_t exception_vector; + /* HV_INTERCEPT_TYPE_X64_MSR_INDEX */ + uint32_t msr_index; + /* N.B. Other intercept types do not have any parameters. */ +}; + +struct hv_input_install_intercept { + uint64_t partition_id; + uint32_t access_type; /* mask */ + uint32_t intercept_type; /* enum hv_intercept_type */ + union hv_intercept_parameters intercept_parameter; +} QEMU_PACKED; + struct hv_u128 { uint64_t low_part; uint64_t high_part; @@ -737,6 +763,35 @@ struct hv_x64_memory_intercept_message { uint8_t instruction_bytes[16]; } QEMU_PACKED; =20 +struct hv_x64_exception_intercept_message { + struct hv_x64_intercept_message_header header; + uint16_t exception_vector; + uint8_t exception_info; + uint8_t instruction_byte_count; + uint32_t error_code; + uint64_t exception_parameter; /* DR6 for #DB, CR2 for #PF */ + uint64_t reserved; + uint8_t instruction_bytes[16]; + struct hv_x64_segment_register ds_segment; + struct hv_x64_segment_register ss_segment; + 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; + union hv_message_flags { uint8_t asu8; struct { @@ -845,6 +900,7 @@ struct hv_cpuid { =20 #define HVCALL_GET_PARTITION_PROPERTY 0x0044 #define HVCALL_SET_PARTITION_PROPERTY 0x0045 +#define HVCALL_INSTALL_INTERCEPT 0x004d #define HVCALL_GET_VP_REGISTERS 0x0050 #define HVCALL_SET_VP_REGISTERS 0x0051 #define HVCALL_TRANSLATE_VIRTUAL_ADDRESS 0x0052 --=20 2.34.1 From nobody Mon Nov 23 05:00:15 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=1791363221; cv=none; d=zohomail.com; s=zohoarc; b=AMfWFy/IqBN2da5miQayME/A+s/rN4I6UvCTsfbjtFa4JpCXZ8hDptze82FQ3s0JdN5dC9OPVgsagK8/Bjl8cQ4a/7lVdwXaidVnVIqGaaw0sdnv/5squFd0Nr75myv5dsBPok2evBJMXva48cBJRf3yaV5Q0ljyNmGoIUxXvVA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791363221; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=rpfCCQ+GbM5t0xKp54zEzxOJoexKBo4ENnKL7Lkkwug=; b=hZu+PI7my2O/1Nsd7YsDlDdREqLqjvQUCtMpKs4DGo2143ho686mYbWNYC0TpF1h/OPXwy0jZo+8u5Nx1ChxzAFBcTOb82u3EqcXvNpPKPpWNiroUThzB26sl/2wLhUIK5LVBy3hlEbHfKMaYbbR/iqE1YtwU+CsCGYZ/CxlGSc= 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 179136322120580.11228680281988; Wed, 7 Oct 2026 01:53:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xENFh-0000Kn-0z; Wed, 07 Oct 2026 04:43:54 -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 1xEIxk-0001Fk-SU for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:09:08 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEIxi-00062U-7W for qemu-devel@nongnu.org; Wed, 07 Oct 2026 00:09:04 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.46]) by linux.microsoft.com (Postfix) with ESMTPSA id DB45820B7171; Tue, 6 Oct 2026 09:48:56 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com DB45820B7171 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1791305338; bh=rpfCCQ+GbM5t0xKp54zEzxOJoexKBo4ENnKL7Lkkwug=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EAm4LKyT5NakAtK4R7GyDSn2cmPg/jsjv9uNH4l9KhjUA7M9XF29I1a47539+fP4X boba9AonmtEb2yJ5Qpd9ptvd1DW30mL+sw0+Wh8O5MDn0WEiSLF58kavSxfSAMKyf5 5S/U5AdYkQFr/AnQHRSJZZwpxRt0v0JCxzqYP8f8= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Paolo Bonzini Subject: [PULL 10/11] accel/mshv: add gdbstub software breakpoint support Date: Tue, 6 Oct 2026 18:49:04 +0200 Message-Id: <20261006164905.776466-11-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261006164905.776466-1-magnuskulke@linux.microsoft.com> References: <20261006164905.776466-1-magnuskulke@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=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, 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: 1791363222181158500 From: Doru Bl=C3=A2nzeanu Implement software breakpoint based guest debugging for the MSHV accelerator, modeled on the WHPX backend. The guest-visible debug registers are not used: a breakpoint is an INT1 (opcode 0xf1) patched into guest memory, which raises a #DB when executed. A partition-wide intercept on #DB is installed the first time a breakpoint (or single step) is requested. The intercept is not uninstalled, so it stays for the lifetime of the VM and every vmexit mshv_handle_debug() decides whether the #DB belongs to gdb (it hit one of our INT1 breakpoints)= or to the guest; a guest-owned #DB is handed back through a pending exception event so the guest takes it through its own IDT. Guest INT3 (#BP) is deliberately left to the guest's own IDT and is never intercepted. (adapted to the signature change of accel_ops->update_guest_debug) Signed-off-by: Doru Bl=C3=A2nzeanu Reviewed-by: Magnus Kulke Signed-off-by: Magnus Kulke --- accel/mshv/mshv-all.c | 170 +++++++++++++++++++++++++++++++++++- include/system/mshv_int.h | 14 +++ target/i386/mshv/mshv-cpu.c | 104 ++++++++++++++++++++++ 3 files changed, 287 insertions(+), 1 deletion(-) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index 5921ce693e..d820a67b95 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -29,6 +29,7 @@ =20 #include "qemu/accel.h" #include "qemu/guest-random.h" +#include "gdbstub/enums.h" #include "accel/accel-ops.h" #include "accel/accel-cpu-ops.h" #include "exec/cpu-common.h" @@ -563,6 +564,10 @@ static int mshv_init(AccelState *as, MachineState *ms) s->nr_as =3D 1; s->as =3D g_new0(MshvAddressSpace, s->nr_as); =20 + QTAILQ_INIT(&s->sw_breakpoints); + + as->gdbstub.sstep_flags =3D SSTEP_ENABLE; + mshv_state =3D s; =20 mshv_init_irq_routing(s); @@ -591,6 +596,158 @@ static int mshv_destroy_vcpu(CPUState *cpu) return 0; } =20 +struct MshvSwBreakpoint *mshv_find_sw_breakpoint(CPUState *cpu, vaddr pc) +{ + struct MshvSwBreakpoint *bp; + + QTAILQ_FOREACH(bp, &mshv_state->sw_breakpoints, entry) { + if (bp->pc =3D=3D pc) { + return bp; + } + } + return NULL; +} + +static int mshv_install_exception_intercept(int vm_fd, uint16_t vector) +{ + struct hv_input_install_intercept in =3D {0}; + struct mshv_root_hvcall args =3D {0}; + int ret; + + in.access_type =3D 1 << HV_X64_INTERCEPT_ACCESS_TYPE_EXECUTE; + in.intercept_type =3D HV_INTERCEPT_TYPE_EXCEPTION; + in.intercept_parameter.as_uint64 =3D vector; + + args.code =3D HVCALL_INSTALL_INTERCEPT; + args.in_sz =3D sizeof(in); + args.in_ptr =3D (uint64_t)∈ + + ret =3D mshv_hvcall(vm_fd, &args); + if (ret < 0) { + error_report("Failed to install exception intercept for vector %u", + vector); + } + return ret; +} + +/* + * Install the #DB intercept. This is a permanent, partition-wide latch: i= t is + * installed once on the first debug use and stays for the lifetime of the= VM. + */ +static int mshv_init_exception_intercept(void) +{ + int ret; + + if (mshv_state->exception_intercepts_installed) { + return 0; + } + + /* Only #DB is needed: gdb breakpoints are INT1 (0xf1) */ + ret =3D mshv_install_exception_intercept(mshv_state->vm, 1); /* #DB */ + if (ret < 0) { + return ret; + } + + mshv_state->exception_intercepts_installed =3D true; + return 0; +} + +static void mshv_update_guest_debug(CPUState *cpu) +{ + int ret; + + if (cpu_single_stepping(cpu)) { + /* Installs exception intercept only on the first call */ + ret =3D mshv_init_exception_intercept(); + if (ret < 0) { + error_report("Failed to install exception intercept for #DB"); + abort(); + } + } +} + +static int mshv_insert_gdbstub_breakpoint(CPUState *cpu, GdbBreakpointType= type, + vaddr addr, vaddr len) +{ + struct MshvSwBreakpoint *bp; + int err; + + /* We don't use the guest's debug registers. */ + if (type !=3D GDB_BREAKPOINT_SW) { + return -ENOSYS; + } + + bp =3D mshv_find_sw_breakpoint(cpu, addr); + if (bp) { + bp->use_count++; + return 0; + } + + bp =3D g_new(struct MshvSwBreakpoint, 1); + bp->pc =3D addr; + bp->use_count =3D 1; + err =3D mshv_arch_insert_sw_breakpoint(cpu, bp); + if (err) { + g_free(bp); + return err; + } + + QTAILQ_INSERT_HEAD(&mshv_state->sw_breakpoints, bp, entry); + + /* Installs exception intercept only on the first call */ + return mshv_init_exception_intercept(); +} + +static int mshv_remove_gdbstub_breakpoint(CPUState *cpu, GdbBreakpointType= type, + vaddr addr, vaddr len) +{ + struct MshvSwBreakpoint *bp; + int err; + + if (type !=3D GDB_BREAKPOINT_SW) { + return -ENOSYS; + } + + bp =3D mshv_find_sw_breakpoint(cpu, addr); + if (!bp) { + return -ENOENT; + } + + if (bp->use_count > 1) { + bp->use_count--; + return 0; + } + + err =3D mshv_arch_remove_sw_breakpoint(cpu, bp); + if (err) { + return err; + } + + QTAILQ_REMOVE(&mshv_state->sw_breakpoints, bp, entry); + g_free(bp); + + return 0; +} + +static void mshv_remove_all_gdbstub_breakpoints(CPUState *cpu) +{ + struct MshvSwBreakpoint *bp, *next; + CPUState *tmpcpu; + + QTAILQ_FOREACH_SAFE(bp, &mshv_state->sw_breakpoints, entry, next) { + if (mshv_arch_remove_sw_breakpoint(cpu, bp) !=3D 0) { + /* Fall back to whichever CPU still has it mapped. */ + CPU_FOREACH(tmpcpu) { + if (mshv_arch_remove_sw_breakpoint(tmpcpu, bp) =3D=3D 0) { + break; + } + } + } + QTAILQ_REMOVE(&mshv_state->sw_breakpoints, bp, entry); + g_free(bp); + } +} + static int mshv_cpu_exec(CPUState *cpu) { hv_message mshv_msg; @@ -628,6 +785,9 @@ static int mshv_cpu_exec(CPUState *cpu) switch (exit_reason) { case MshvVmExitIgnore: break; + case MshvVmExitDebug: + ret =3D EXCP_DEBUG; + break; default: ret =3D EXCP_INTERRUPT; break; @@ -695,7 +855,10 @@ static void *mshv_vcpu_thread(void *arg) do { qemu_process_cpu_events(cpu); if (cpu_can_run(cpu)) { - mshv_cpu_exec(cpu); + ret =3D mshv_cpu_exec(cpu); + if (ret =3D=3D EXCP_DEBUG) { + cpu_handle_guest_debug(cpu); + } } } while (!cpu->unplug || cpu_can_run(cpu)); =20 @@ -840,6 +1003,11 @@ static void mshv_accel_ops_class_init(ObjectClass *oc= , const void *data) ops->cpus_are_resettable =3D mshv_cpus_are_resettable; ops->cpu_thread_is_idle =3D mshv_vcpu_thread_is_idle; ops->handle_interrupt =3D generic_handle_interrupt; + + ops->update_guest_debug =3D mshv_update_guest_debug; + ops->insert_gdbstub_breakpoint =3D mshv_insert_gdbstub_breakpoint; + ops->remove_gdbstub_breakpoint =3D mshv_remove_gdbstub_breakpoint; + ops->remove_all_gdbstub_breakpoints =3D mshv_remove_all_gdbstub_breakp= oints; } =20 static const TypeInfo mshv_accel_ops_type =3D { diff --git a/include/system/mshv_int.h b/include/system/mshv_int.h index 3dffe3c5fb..1f77ffd152 100644 --- a/include/system/mshv_int.h +++ b/include/system/mshv_int.h @@ -15,6 +15,7 @@ #define QEMU_MSHV_INT_H =20 #include "hw/hyperv/hvhdk.h" +#include "exec/vaddr.h" =20 #define MSHV_MSR_ENTRIES_COUNT 64 =20 @@ -56,6 +57,13 @@ typedef struct MshvAddressSpace { AddressSpace *as; } MshvAddressSpace; =20 +struct MshvSwBreakpoint { + vaddr pc; + vaddr saved_insn; + int use_count; + QTAILQ_ENTRY(MshvSwBreakpoint) entry; +}; + struct MshvState { AccelState parent_obj; int vm; @@ -70,6 +78,8 @@ struct MshvState { unsigned long *used_gsi_bitmap; unsigned int gsi_count; union hv_partition_processor_features processor_features; + QTAILQ_HEAD(, MshvSwBreakpoint) sw_breakpoints; + bool exception_intercepts_installed; }; =20 typedef struct MshvMsiControl { @@ -85,6 +95,7 @@ typedef enum MshvVmExit { MshvVmExitIgnore =3D 0, MshvVmExitShutdown =3D 1, MshvVmExitSpecial =3D 2, + MshvVmExitDebug =3D 3, } MshvVmExit; =20 void mshv_init_mmio_emu(void); @@ -100,6 +111,9 @@ int mshv_arch_store_vcpu_state(const CPUState *cpu); int mshv_arch_load_vcpu_state(CPUState *cpu); int mshv_arch_set_partition_msrs(const CPUState *cpu); int mshv_arch_set_mp_state(const CPUState *cpu); +int mshv_arch_insert_sw_breakpoint(CPUState *cpu, struct MshvSwBreakpoint = *bp); +int mshv_arch_remove_sw_breakpoint(CPUState *cpu, struct MshvSwBreakpoint = *bp); +struct MshvSwBreakpoint *mshv_find_sw_breakpoint(CPUState *cpu, vaddr pc); void mshv_arch_init_vcpu(CPUState *cpu); void mshv_arch_destroy_vcpu(CPUState *cpu); void mshv_arch_amend_proc_features( diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index f2b5641231..0670e7a5ae 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -12,6 +12,7 @@ =20 #include "qemu/osdep.h" #include "qemu/error-report.h" +#include "qemu/main-loop.h" #include "qemu/memalign.h" =20 #include "system/mshv.h" @@ -27,6 +28,7 @@ #include "emulate/x86_decode.h" #include "emulate/x86_emu.h" #include "emulate/x86_flags.h" +#include "gdbstub/enums.h" =20 #include "accel/accel-cpu-target.h" =20 @@ -2023,6 +2025,72 @@ static int handle_pio(CPUState *cpu, const struct hy= perv_message *msg) return handle_pio_non_str(cpu, &info); } =20 +/* Re-inject a guest-owned #DB via a pending event */ +static int reinject_exception(CPUState *cpu, + const struct hv_x64_exception_intercept_message *info) +{ + hv_register_assoc assoc =3D { .name =3D HV_REGISTER_PENDING_EVENT0 }; + + assoc.value.pending_exception_event.event_pending =3D 1; + assoc.value.pending_exception_event.event_type =3D + HV_X64_PENDING_EVENT_EXCEPTION; + assoc.value.pending_exception_event.exception_parameter =3D + info->exception_parameter; + assoc.value.pending_exception_event.vector =3D info->exception_vector; + + return mshv_set_generic_regs(cpu, &assoc, 1); +} + +/* Check if the intercepted #DB has been set by gdb. */ +static bool is_debug_exception(CPUState *cpu, const hv_message *msg) +{ + struct hv_x64_exception_intercept_message *info =3D (void *)msg->paylo= ad; + + /* Only #DB is intercepted */ + if (info->exception_vector !=3D EXCP01_DB) { + return false; + } + + /* INT1 hit: RIP points at the breakpoint */ + if (mshv_find_sw_breakpoint(cpu, info->header.rip) !=3D NULL) { + return true; + } + + /* Check if single stepping */ + if (cpu_single_stepping(cpu)) { + return true; + } + + /* The guest's own #DB; caller re-injects it. */ + return false; +} + +static int handle_exception_interrupt(CPUState *cpu, + const struct hyperv_message *msg, + MshvVmExit *exit_reason) +{ + int ret; + + if (is_debug_exception(cpu, msg)) { + /* Exit early reporting debug exception */ + *exit_reason =3D MshvVmExitDebug; + return 0; + } + + /* Not ours - hand the #DB back to the guest and keep running. */ + ret =3D reinject_exception(cpu, + (struct hv_x64_exception_intercept_message *) msg->payload); + if (ret < 0) { + error_report("failed to reinject exception on vcpu %d", + cpu->cpu_index); + return -1; + } + + *exit_reason =3D MshvVmExitIgnore; + + return 0; +} + int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *e= xit) { int ret; @@ -2051,6 +2119,16 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_messa= ge *msg, MshvVmExit *exit) return MshvVmExitSpecial; } return MshvVmExitIgnore; + case HVMSG_X64_EXCEPTION_INTERCEPT: + bql_lock(); + ret =3D handle_exception_interrupt(cpu, msg, &exit_reason); + bql_unlock(); + if (ret < 0) { + error_report("failed to handle exception intercept"); + return -1; + } + *exit =3D exit_reason; + return exit_reason; default: break; } @@ -2064,6 +2142,32 @@ void mshv_remove_vcpu(int vm_fd, int cpu_fd) close(cpu_fd); } =20 +int mshv_arch_insert_sw_breakpoint(CPUState *cpu, struct MshvSwBreakpoint = *bp) +{ + static const uint8_t int1 =3D 0xf1; + + if (cpu_memory_rw_debug(cpu, bp->pc, (uint8_t *)&bp->saved_insn, 1, 0)= || + cpu_memory_rw_debug(cpu, bp->pc, (uint8_t *)&int1, 1, 1)) { + return -EINVAL; + } + return 0; +} + +int mshv_arch_remove_sw_breakpoint(CPUState *cpu, struct MshvSwBreakpoint = *bp) +{ + uint8_t int1; + + if (cpu_memory_rw_debug(cpu, bp->pc, &int1, 1, 0)) { + return -EINVAL; + } + if (int1 !=3D 0xf1) { + return 0; + } + if (cpu_memory_rw_debug(cpu, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)= ) { + return -EINVAL; + } + return 0; +} =20 int mshv_create_vcpu(int vm_fd, uint8_t vp_index, int *cpu_fd) { --=20 2.34.1 From nobody Mon Nov 23 05:00:15 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=1791363242; cv=none; d=zohomail.com; s=zohoarc; b=Vg7ZsGO3y1N4WrGQrw9UfMaQi0UsjqTGqb8oRhYUbvC94VgdO7zbz0+9fNnfeozg1rfrzVIZ92vN9oMXOofD4HaYZoaJrogrkpO2cx3NCJtWS0YgwPsj5I9hr5kqw0RPDqo1uDdpzlBdIrXB5MQ+Z0a71BwzX88Y7//YnJ/d65M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791363242; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=V7k5ksSW4RsHuCytgbLs9kBX9VPnsl0XS1+v389uO0s=; b=lUDcWh9ylsRL332s8hEgybga9uosWXUuqzDLaii5XgZpYmQ3ekChXz4R+NaUnjMO550TCslyhnDzBCW7FEn8rvHmDPf2JrmX6vaCq50QA4LtkYM8WL7qYrYRh7uEfN6KCe356Wuak+xbxDxbwImzxYqFNGbAVQHrH95Oxi6gvJQ= 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 1791363242304662.4145086559344; Wed, 7 Oct 2026 01:54:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xENIz-0008OQ-7w; Wed, 07 Oct 2026 04:47:17 -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 1xEIVk-0004ee-3k for qemu-devel@nongnu.org; Tue, 06 Oct 2026 23:40:18 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEIVb-0000gL-PZ for qemu-devel@nongnu.org; Tue, 06 Oct 2026 23:40:06 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.46]) by linux.microsoft.com (Postfix) with ESMTPSA id 0E98E20B7172; Tue, 6 Oct 2026 09:48:59 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 0E98E20B7172 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1791305341; bh=V7k5ksSW4RsHuCytgbLs9kBX9VPnsl0XS1+v389uO0s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=niOUKbq4WyvJm2J0dL8Ro2fTkM1NG1YHJczG50TpuemhgTfkH+YzyeRPcFDs4FqHj pZQpG0PXCNSa1TaWckpcBa8zRK/qM8NGnxNZOJpTZZzRbHczSU3lvUIHqwEpylX1QI KlLHBnL4JmW0DoSLX6ixjesfHpqAJUdnXCifqTBw= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Paolo Bonzini Subject: [PULL 11/11] target/i386/mshv: support single-stepping Date: Tue, 6 Oct 2026 18:49:05 +0200 Message-Id: <20261006164905.776466-12-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261006164905.776466-1-magnuskulke@linux.microsoft.com> References: <20261006164905.776466-1-magnuskulke@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=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, 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: 1791363244160158500 From: Doru Bl=C3=A2nzeanu Single-step by toggling RFLAGS.TF around the vCPU run, as WHPX does. TF is set only on the live register, never in env->eflags, so it is not read back and re-applied by a later register store. The resulting #DB is reported to gdb by mshv_handle_debug() whenever the vC= PU is single-stepping. Signed-off-by: Doru Bl=C3=A2nzeanu Reviewed-by: Magnus Kulke Signed-off-by: Magnus Kulke --- target/i386/mshv/mshv-cpu.c | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 0670e7a5ae..4df73bb497 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -2091,17 +2091,67 @@ static int handle_exception_interrupt(CPUState *cpu, return 0; } =20 +/* + * Flip RFLAGS.TF like WHPX. Set it only on the live register, not env->ef= lags, + * so a later store won't put it back. + */ +static int arch_set_single_step(CPUState *cpu, bool enable) +{ + X86CPU *x86cpu =3D X86_CPU(cpu); + CPUX86State *env =3D &x86cpu->env; + hv_register_assoc assoc =3D { .name =3D HV_X64_REGISTER_RFLAGS }; + uint64_t rflags; + int ret; + + if (env->regs_page && env->regs_page->isvalid !=3D 0) { + rflags =3D env->regs_page->rflags; + rflags =3D enable ? (rflags | TF_MASK) : (rflags & ~TF_MASK); + env->regs_page->rflags =3D rflags; + env->regs_page->dirty |=3D (1u << HV_X64_REGISTER_CLASS_FLAGS); + return 0; + } + + ret =3D mshv_get_generic_regs(cpu, &assoc, 1); + if (ret < 0) { + return ret; + } + rflags =3D assoc.value.reg64; + rflags =3D enable ? (rflags | TF_MASK) : (rflags & ~TF_MASK); + assoc.value.reg64 =3D rflags; + return mshv_set_generic_regs(cpu, &assoc, 1); +} + int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *e= xit) { int ret; enum MshvVmExit exit_reason; int cpu_fd =3D mshv_vcpufd(cpu); + bool single_step; + + /* enable single stepping by flipping RFLAGS.TF */ + single_step =3D cpu_single_stepping(cpu); + if (single_step) { + ret =3D arch_set_single_step(cpu, true); + if (ret < 0) { + error_report("Failed to arm single-step (TF) on vcpu %d: %s", + cpu->cpu_index, strerror(-ret)); + *exit =3D MshvVmExitShutdown; + return -1; + } + } =20 ret =3D ioctl(cpu_fd, MSHV_RUN_VP, msg); if (ret < 0) { return MshvVmExitShutdown; } =20 + /* disable single stepping by flipping RFLAGS.TF */ + if (single_step && arch_set_single_step(cpu, false) < 0) { + error_report("Failed to clear single-step (TF) on vcpu %d", + cpu->cpu_index); + return -1; + } + switch (msg->header.message_type) { case HVMSG_UNRECOVERABLE_EXCEPTION: return MshvVmExitShutdown; --=20 2.34.1