Skip to contentSkip to conversation

Add CPU topology extension support for mshv guests

PATCH In review Doru Blânzeanu 2 participants 5 messages Nov 24 – Nov 30

lore
Share & tools
lore
#0 lore raw

v3 cover Add CPU topology extension support for mshv guests

This patch changes how MSHV constructs and registers the guest CPUID table. It consists of three commits:

1. mshv/mshv-cpu.c: Collect all Intel topology subleaves and AMD cache subleaves
2. target/i386/mshv: Initialize the extended CPUID limit for host/max CPUs

Previous to the first commit, collect_cpuid_entries treated 0x0b and 0x1f like ordinary leaves and collected only sub-leaf zero. But these Intel topology leaves contain one sub-leaf per topology level.
With this patch, now it repeatedly calls cpu_x86_cpuid and stores every result until ECX[15:8] becomes zero (same as kvm does).
This fix correctly reports CPU topology and cache leaves when TOPOEXT is enabled.

The second commit changes the minimum extended CPUID level to 0x8000_001E during mshv instance initialization for host/max CPUs (the ones that have TOPOEXT enabled).

Changes since V2:
- Drop the unconditional SMT support in the mshv partition. It will be submitted in a later patch
- Change how xlevel is set, from generic accelerator to mshv specific. Now, mshv follows kvm's approach of setting xlevel to the host's visible extended CPUID maximum for host/max CPUs.

Changes since V1:
- Enable SMT support in the mshv partition

Doru Blânzeanu (2):
mshv/mshv-cpu.c: Collect all Intel topology subleaves and AMD cache
subleaves
target/i386/mshv: Initialize the extended CPUID limit for host/max
CPUs

 target/i386/mshv/mshv-cpu.c | 48 ++++++++++++++++++++++++++++++-------
 1 file changed, 40 insertions(+), 8 deletions(-)
Signature · 1 line
2.48.1.vfs.0.0
#1 lore raw

PATCH v3 1/2 mshv/mshv-cpu.c: Collect all Intel topology subleaves and AMD cache subleaves

This fix makes AMD cache overrides subleaf-specific, makes 0x8000_001E apply regardless of ECX (not subleaf specific) and collects Intel topology subleaves (0x0b, 0x1f).

---
 target/i386/mshv/mshv-cpu.c | 41 +++++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 8 deletions(-)
target/i386/mshv/mshv-cpu.c +33 -8
index c145bbc447..79fe357c13 100644
@@ -1093,6 +1093,21 @@ static void collect_cpuid_entries(const CPUState *cpu, GList **cpuid_entries)
     /* Collect basic leaves (0x0 to max_basic_leaf) */
     for (leaf = 0; leaf <= max_basic_leaf; leaf++) {
         bool has_subleaves = false;
+
+        if (leaf == 0x0b || leaf == 0x1f) {
+            for (subleaf = 0; subleaf < max_subleaf; subleaf++) {
+                cpu_x86_cpuid(env, leaf, subleaf,
+                              &eax, &ebx, &ecx, &edx);
+                add_cpuid_entry(cpuid_entries, leaf, subleaf,
+                                eax, ebx, ecx, edx);
+
+                if ((ecx & 0xff00) == 0) {
+                    break;
+                }
+            }
+            continue;
+        }
+
         for (int i = 0; i < n_subleaf_leaves; i++) {
             if (leaf == leaves_with_subleaves[i]) {
                 has_subleaves = true;
@@ -1150,6 +1165,20 @@ static void collect_cpuid_entries(const CPUState *cpu, GList **cpuid_entries)
 
     /* Collect extended leaves (0x80000000 to max_extended_leaf) */
     for (leaf = 0x80000000; leaf <= max_extended_leaf; leaf++) {
+        if (leaf == 0x8000001d) {
+            for (subleaf = 0; subleaf < max_subleaf; subleaf++) {
+                cpu_x86_cpuid(env, leaf, subleaf,
+                              &eax, &ebx, &ecx, &edx);
+
+                add_cpuid_entry(cpuid_entries, leaf, subleaf,
+                                eax, ebx, ecx, edx);
+                if ((eax & 0x1f) == 0) {
+                    break;
+                }
+            }
+            continue;
+        }
+
         cpu_x86_cpuid(env, leaf, 0, &eax, &ebx, &ecx, &edx);
         add_cpuid_entry(cpuid_entries, leaf, 0, eax, ebx, ecx, edx);
     }
@@ -1229,12 +1258,10 @@ static int register_intercept_result_cpuid(const CPUState *cpu,
          * 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 == 0xb ||
             entry->function == 0x1f ||
-            entry->function == 0x8000001e ||
             entry->function == 0x80000026) {
             subleaf_specific = 1;
             always_override = 1;
@@ -1245,12 +1272,14 @@ static int register_intercept_result_cpuid(const CPUState *cpu,
          * 0x0D: Processor Extended State Enumeration
          * 0x0F: Platform QoS Monitoring
          * 0x10: Platform QoS Enforcement
+         * 0x8000_001D: AMD Cache Topology
          */
         } else if (entry->function == 0x04 ||
                    entry->function == 0x07 ||
                    entry->function == 0x0d ||
                    entry->function == 0x0f ||
-                   entry->function == 0x10) {
+                   entry->function == 0x10 ||
+                   entry->function == 0x8000001d) {
             subleaf_specific = 1;
             always_override = 1;
         /* Basic feature leaves (no subleaves) */
@@ -2351,11 +2380,6 @@ void mshv_arch_init_vcpu(CPUState *cpu)
     header = (X86XSaveHeader *)(env->xsave_buf + sizeof(X86LegacyXSaveArea));
     header->xcomp_bv = header->xstate_bv | (1ULL << 63);
 
-    /*
-     * TODO: populate topology info:
-     * X86CPUTopoInfo *topo_info = &env->topo_info;
-     */
-
     ret = init_cpuid2(cpu);
     assert(ret == 0);
 
@@ -2406,6 +2430,7 @@ uint32_t mshv_get_supported_cpuid(uint32_t func, uint32_t idx, int reg)
     /* Disable nested virtualization features not yet supported by MSHV */
     if (func == 0x80000001 && reg == R_ECX) {
         ret &= ~CPUID_EXT3_SVM;
+        ret |= CPUID_EXT3_TOPOEXT;
     }
     if (func == 0x01       && reg == R_ECX) {
         ret &= ~CPUID_EXT_VMX;
Signature · 1 line
2.48.1.vfs.0.0
#3 lore raw

PATCH v3 2/2 target/i386/mshv: Initialize the extended CPUID limit for host/max CPUs

In reply to Doru Blânzeanu

Initialize cpuid_min_level from the host visible extended CPUID maximum for host/max CPUd, following kvm's initialization pattern.

Let feature expansion determine xlevel while preserving explicit CLI limits and named CPU models defaults.

---
 target/i386/mshv/mshv-cpu.c | 7 +++++++
 1 file changed, 7 insertions(+)
target/i386/mshv/mshv-cpu.c +7 -0
index 79fe357c13..5b13c0915e 100644
@@ -2564,8 +2564,15 @@ int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state)
 static void mshv_cpu_instance_init(CPUState *cs)
 {
     X86CPU *cpu = X86_CPU(cs);
+    X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
 
     host_cpu_instance_init(cpu);
+
+    if (xcc->max_features) {
+        cpu->env.cpuid_min_xlevel =
+            mshv_get_supported_cpuid(0x80000000, 0, R_EAX);
+    }
+
     mshv_cpu_xsave_init();
 }
 
Signature · 1 line
2.48.1.vfs.0.0
Reviewed-by on v3 2/2
#4 lore raw

Re: [PATCH v3 2/2] target/i386/mshv: Initialize the extended CPUID limit for host/max CPUs

Quoted 33 lines · Doru Blânzeanu

Initialize cpuid_min_level from the host visible extended CPUID maximum for host/max CPUd, following kvm's initialization pattern.

Let feature expansion determine xlevel while preserving explicit CLI limits and named CPU models defaults.

---
 target/i386/mshv/mshv-cpu.c | 7 +++++++
 1 file changed, 7 insertions(+)
target/i386/mshv/mshv-cpu.c +7 -0
index 79fe357c13..5b13c0915e 100644
@@ -2564,8 +2564,15 @@ int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state)
 static void mshv_cpu_instance_init(CPUState *cs)
 {
     X86CPU *cpu = X86_CPU(cs);
+    X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
 
     host_cpu_instance_init(cpu);
+
+    if (xcc->max_features) {
+        cpu->env.cpuid_min_xlevel =
+            mshv_get_supported_cpuid(0x80000000, 0, R_EAX);
+    }
+
     mshv_cpu_xsave_init();
 }
 
Signature · 1 line
2.48.1.vfs.0.0
#2 lore raw

Re: [PATCH v3 1/2] mshv/mshv-cpu.c: Collect all Intel topology subleaves and AMD cache subleaves

In reply to Doru Blânzeanu
Quoted 21 lines · Doru Blânzeanu

This fix makes AMD cache overrides subleaf-specific, makes 0x8000_001E apply regardless of ECX (not subleaf specific) and collects Intel topology subleaves (0x0b, 0x1f).

---
 target/i386/mshv/mshv-cpu.c | 41 +++++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 8 deletions(-)
target/i386/mshv/mshv-cpu.c +4 -0
index c145bbc447..79fe357c13 100644
@@ -1093,6 +1093,21 @@ static void collect_cpuid_entries(const CPUState *cpu, GList **cpuid_entries)
     /* Collect basic leaves (0x0 to max_basic_leaf) */
     for (leaf = 0; leaf <= max_basic_leaf; leaf++) {
         bool has_subleaves = false;
+
+        if (leaf == 0x0b || leaf == 0x1f) {
+            for (subleaf = 0; subleaf < max_subleaf; subleaf++) {
+                cpu_x86_cpuid(env, leaf, subleaf,

target/i386/mshv/mshv-cpu.c static void collect_cpuid_entries(const CPUState *cpu, GList **cpuid_entries) · line 1109

Quoted 10 lines
target/i386/mshv/mshv-cpu.c +10 -0
+                              &eax, &ebx, &ecx, &edx);
+                add_cpuid_entry(cpuid_entries, leaf, subleaf,
+                                eax, ebx, ecx, edx);
+
+                if ((ecx & 0xff00) == 0) {
+                    break;
+                }
+            }
+            continue;
+        }

for consistency sake we probably want to add 0x0b & 0x1f to the leaves_with_subleaves array and move it below the "if (!has_subleaves)" block.

Quoted 12 lines
target/i386/mshv/mshv-cpu.c +5 -0
+
         for (int i = 0; i < n_subleaf_leaves; i++) {
             if (leaf == leaves_with_subleaves[i]) {
                 has_subleaves = true;
@@ -1150,6 +1165,20 @@ static void collect_cpuid_entries(const CPUState *cpu, GList **cpuid_entries)
 
     /* Collect extended leaves (0x80000000 to max_extended_leaf) */
     for (leaf = 0x80000000; leaf <= max_extended_leaf; leaf++) {
+        if (leaf == 0x8000001d) {
+            for (subleaf = 0; subleaf < max_subleaf; subleaf++) {
+                cpu_x86_cpuid(env, leaf, subleaf,
+                              &eax, &ebx, &ecx, &edx);

target/i386/mshv/mshv-cpu.c static void collect_cpuid_entries(const CPUState *cpu, GList **cpuid_entries) · line 1181

Quoted 10 lines
target/i386/mshv/mshv-cpu.c +10 -0
+
+                add_cpuid_entry(cpuid_entries, leaf, subleaf,
+                                eax, ebx, ecx, edx);
+                if ((eax & 0x1f) == 0) {
+                    break;
+                }
+            }
+            continue;
+        }
+

same here, for consistency we probably want to add 0x8000001d to the leaves_with_subleaves array and have a similar "if (!has_subleaves)" logic in the extended leaves loop. this is all a bit clunky admittedly and can probably be refactored into a more elegant switch statement.

switch (leaf) {
case 0x0b:
case 0x1f:
... (special handling)
break;
case 0x0d:
... (special handling)
break;
case 0x07:
... (special handling)
break;
case 0x04:
case 0x0f:
case 0x10:
... (default subleaf handling)
break;
default:

    cpu_x86_cpuid(env, leaf, 0, &eax, &ebx, &ecx, &edx);
    add_cpuid_entry(cpuid_entries, leaf, 0, eax, ebx, ecx, edx);
    break;
}

up to you.

Quoted 54 lines
target/i386/mshv/mshv-cpu.c +4 -8
         cpu_x86_cpuid(env, leaf, 0, &eax, &ebx, &ecx, &edx);
         add_cpuid_entry(cpuid_entries, leaf, 0, eax, ebx, ecx, edx);
     }
@@ -1229,12 +1258,10 @@ static int register_intercept_result_cpuid(const CPUState *cpu,
          * 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 == 0xb ||
             entry->function == 0x1f ||
-            entry->function == 0x8000001e ||
             entry->function == 0x80000026) {
             subleaf_specific = 1;
             always_override = 1;
@@ -1245,12 +1272,14 @@ static int register_intercept_result_cpuid(const CPUState *cpu,
          * 0x0D: Processor Extended State Enumeration
          * 0x0F: Platform QoS Monitoring
          * 0x10: Platform QoS Enforcement
+         * 0x8000_001D: AMD Cache Topology
          */
         } else if (entry->function == 0x04 ||
                    entry->function == 0x07 ||
                    entry->function == 0x0d ||
                    entry->function == 0x0f ||
-                   entry->function == 0x10) {
+                   entry->function == 0x10 ||
+                   entry->function == 0x8000001d) {
             subleaf_specific = 1;
             always_override = 1;
         /* Basic feature leaves (no subleaves) */
@@ -2351,11 +2380,6 @@ void mshv_arch_init_vcpu(CPUState *cpu)
     header = (X86XSaveHeader *)(env->xsave_buf + sizeof(X86LegacyXSaveArea));
     header->xcomp_bv = header->xstate_bv | (1ULL << 63);
 
-    /*
-     * TODO: populate topology info:
-     * X86CPUTopoInfo *topo_info = &env->topo_info;
-     */
-
     ret = init_cpuid2(cpu);
     assert(ret == 0);
 
@@ -2406,6 +2430,7 @@ uint32_t mshv_get_supported_cpuid(uint32_t func, uint32_t idx, int reg)
     /* Disable nested virtualization features not yet supported by MSHV */
     if (func == 0x80000001 && reg == R_ECX) {
         ret &= ~CPUID_EXT3_SVM;
+        ret |= CPUID_EXT3_TOPOEXT;
     }
     if (func == 0x01       && reg == R_ECX) {
         ret &= ~CPUID_EXT_VMX;
Signature · 1 line
2.48.1.vfs.0.0