From nobody Fri Apr 3 22:28:16 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=1774267065; cv=none; d=zohomail.com; s=zohoarc; b=W9ZHe8vti51j5b9UYOaz79zSWI93QcwfzjdWYysUCp5ReMnzRLr4HtFNZsUkxeiauYlVmA9K3iVN69H4tkvGzkbesN8rI40h5cVM8+vL9su7fQ2oL+/HJu4FS4TzW4dd6T7fP4QzxJHjwhgWyy0o8BLoPC21zwvrE1hSYWvS41Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1774267065; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ItrHOC5nPemd66dEK2NgZsgS7P6ajPcgEYGrhuuCrYE=; b=bBPGLdqC5NCay6H12xCgdfkeWlWzAL5cetvIRGzKmOECDhD8fVi+J9SHSBfSDXTFz9cZ0zW2sOHJ9hftaNiLJPRlFiRsX/zTFCTrDYclwUgrmCRrZqcFxAmHC6LwTi2ykBmhfvB60c6b/bJMhoAcTrkR5T6jFIQqH3LyQjulTR0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1774267065664238.80217629785375; Mon, 23 Mar 2026 04:57:45 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w4duS-0000KX-B4; Mon, 23 Mar 2026 07:57:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w4duP-0000I6-A6 for qemu-devel@nongnu.org; Mon, 23 Mar 2026 07:57:25 -0400 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w4duN-0003e5-Df for qemu-devel@nongnu.org; Mon, 23 Mar 2026 07:57:25 -0400 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.76]) by linux.microsoft.com (Postfix) with ESMTPSA id DCE0120B7129; Mon, 23 Mar 2026 04:57:20 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com DCE0120B7129 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1774267042; bh=ItrHOC5nPemd66dEK2NgZsgS7P6ajPcgEYGrhuuCrYE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DptyRsTEo1WwaFePrC8rerBu1dyjI+vHErQHzknt4LDChCBrTtlGN1KD8kpI22K0R 8pTkPRlLw/UwGwV1pku9HDM5IcAYkreGoLKp6a7TmqPprG6320T8i4kZO/Mg/033Ig TNeD+3hOFUXgwZn0ghYzwZLwcMQn+iZCaEddbaPI= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , Paolo Bonzini , Wei Liu , Magnus Kulke , Magnus Kulke , Zhao Liu Subject: [PATCH v3 3/9] target/i386/mshv: fix various cpuid traversal bugs Date: Mon, 23 Mar 2026 12:57:05 +0100 Message-Id: <20260323115711.353793-4-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260323115711.353793-1-magnuskulke@linux.microsoft.com> References: <20260323115711.353793-1-magnuskulke@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=magnuskulke@linux.microsoft.com; helo=linux.microsoft.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: 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: 1774267066555154100 Content-Type: text/plain; charset="utf-8" - Hardcoded max_leaf was not accurate. We query leaf 0x0 and 0x80000000 to get the actual max leaves - On all 0 zeroes on leaf 0x0d, we register 0-63 subleaves with zeros indicating XSAVE is disabled - Subleaf 0 was hardcoded, so the Hypervisor returned defaults for other subleaves - Subleaf 0 was hardcoded, so we were passing 0 instead of actual subleaf when adding entries. We now pass the correct subleaf value to add_cpuid_entry() - Leaves 0x04,0x07,0d,0f,10 weren't marked as subleaf-specific Signed-off-by: Magnus Kulke Acked-by: Wei Liu --- target/i386/mshv/mshv-cpu.c | 88 +++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 27 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index f1cc4abc5c..4183727a86 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -465,14 +465,17 @@ 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; - size_t max_leaf =3D 0x1F; - size_t max_subleaf =3D 0x20; - - uint32_t leaves_with_subleaves[] =3D {0x4, 0x7, 0xD, 0xF, 0x10}; + uint32_t max_basic_leaf, max_extended_leaf; + 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); =20 - /* Regular leaves without subleaves */ - for (leaf =3D 0; leaf <=3D max_leaf; leaf++) { + /* Get maximum basic and and extended CPUID leaves */ + cpu_x86_cpuid(env, 0, 0, &max_basic_leaf, &ebx, &ecx, &edx); + cpu_x86_cpuid(env, 0x80000000, 0, &max_extended_leaf, &ebx, &ecx, &edx= ); + + /* Collect basic leaves (0x0 to max_basic_leaf) */ + for (leaf =3D 0; leaf <=3D max_basic_leaf; leaf++) { bool has_subleaves =3D false; for (int i =3D 0; i < n_subleaf_leaves; i++) { if (leaf =3D=3D leaves_with_subleaves[i]) { @@ -483,27 +486,40 @@ static void collect_cpuid_entries(const CPUState *cpu= , GList **cpuid_entries) =20 if (!has_subleaves) { cpu_x86_cpuid(env, leaf, 0, &eax, &ebx, &ecx, &edx); - if (eax =3D=3D 0 && ebx =3D=3D 0 && ecx =3D=3D 0 && edx =3D=3D= 0) { - /* all zeroes indicates no more leaves */ - continue; - } - add_cpuid_entry(cpuid_entries, leaf, 0, eax, ebx, ecx, edx); continue; } =20 + /* + * Valid XSAVE components can exist at a higher index se we need t= o set + * all subleaves for leaf 0x0d, even if we encounter an empty one. + */ + if (leaf =3D=3D 0x0d) { + for (subleaf =3D 0; subleaf <=3D 63; 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 if (eax =3D=3D 0 && ebx =3D=3D 0 && ecx =3D=3D 0 && edx =3D=3D= 0) { - /* all zeroes indicates no more leaves */ break; } - add_cpuid_entry(cpuid_entries, leaf, 0, eax, ebx, ecx, edx); + add_cpuid_entry(cpuid_entries, leaf, subleaf, eax, ebx, ecx, e= dx); subleaf++; } } + + /* Collect extended leaves (0x80000000 to max_extended_leaf) */ + for (leaf =3D 0x80000000; leaf <=3D max_extended_leaf; leaf++) { + cpu_x86_cpuid(env, leaf, 0, &eax, &ebx, &ecx, &edx); + add_cpuid_entry(cpuid_entries, leaf, 0, eax, ebx, ecx, edx); + } } =20 static int register_intercept_result_cpuid_entry(const CPUState *cpu, @@ -576,22 +592,40 @@ static int register_intercept_result_cpuid(const CPUS= tate *cpu, subleaf_specific =3D 0; always_override =3D 1; =20 - /* Intel */ - /* 0xb - Extended Topology Enumeration Leaf */ - /* 0x1f - V2 Extended Topology Enumeration Leaf */ - /* AMD */ - /* 0x8000_001e - Processor Topology Information */ - /* 0x8000_0026 - Extended CPU Topology */ - if (entry->function =3D=3D 0xb - || entry->function =3D=3D 0x1f - || entry->function =3D=3D 0x8000001e - || entry->function =3D=3D 0x80000026) { + /* + * Intel + * 0xb - Extended Topology Enumeration Leaf + * 0x1f - V2 Extended Topology Enumeration Leaf + * AMD + * 0x8000_001e - Processor Topology Information + * 0x8000_0026 - Extended CPU Topology + */ + if (entry->function =3D=3D 0xb || + entry->function =3D=3D 0x1f || + entry->function =3D=3D 0x8000001e || + entry->function =3D=3D 0x80000026) { + subleaf_specific =3D 1; + always_override =3D 1; + /* + * Feature enumeration leaves (subleaf-specific) + * 0x04: Deterministic Cache Parameters + * 0x07: Structured Extended Feature Flags + * 0x0D: Processor Extended State Enumeration + * 0x0F: Platform QoS Monitoring + * 0x10: Platform QoS Enforcement + */ + } else if (entry->function =3D=3D 0x04 || + entry->function =3D=3D 0x07 || + entry->function =3D=3D 0x0d || + entry->function =3D=3D 0x0f || + entry->function =3D=3D 0x10) { subleaf_specific =3D 1; always_override =3D 1; - } else if (entry->function =3D=3D 0x00000001 - || entry->function =3D=3D 0x80000000 - || entry->function =3D=3D 0x80000001 - || entry->function =3D=3D 0x80000008) { + /* Basic feature leaves (no subleaves) */ + } else if (entry->function =3D=3D 0x00000001 || + entry->function =3D=3D 0x80000000 || + entry->function =3D=3D 0x80000001 || + entry->function =3D=3D 0x80000008) { subleaf_specific =3D 0; always_override =3D 1; } --=20 2.34.1