Skip to contentSkip to conversation

target-i386: Eliminate unnecessary has_msr_* global variables

PATCH In review Eduardo Habkost 2 participants 8 messages Sep 26, 2016 – Sep 27, 2016 KVM

lore
Share & tools
lore
#0 lore raw

cover target-i386: Eliminate unnecessary has_msr_* global variables

Some of the has_msr_* global variables are set depending on X86PCU fields and are unnecessary because we can look at the X86CPU object directly. This series eliminates some of them.

Eduardo Habkost (5):
target-i386: Remove has_msr_mtrr global variable
target-i386: Remove has_msr_hv_apic global variable
target-i386: Remove has_msr_hv_tsc global variable
target-i386: Clear KVM CPUID features if KVM is disabled
target-i386: Remove has_msr_* global vars for KVM features

 target-i386/cpu.c |  4 ++++
 target-i386/kvm.c | 51 +++++++++++++++++++--------------------------------
 2 files changed, 23 insertions(+), 32 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/5 target-i386: Remove has_msr_mtrr global variable

The global variable is not necessary because we can check the CPU feature flags directly.

---
 target-i386/kvm.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
target-i386/kvm.c +2 -6
index a0e42b2..5118562 100644
@@ -99,7 +99,6 @@ static bool has_msr_hv_vpindex;
 static bool has_msr_hv_runtime;
 static bool has_msr_hv_synic;
 static bool has_msr_hv_stimer;
-static bool has_msr_mtrr;
 static bool has_msr_xss;
 
 static bool has_msr_architectural_pmu;
@@ -975,9 +974,6 @@ int kvm_arch_init_vcpu(CPUState *cs)
     }
     cpu->kvm_msr_buf = g_malloc0(MSR_BUF_SIZE);
 
-    if (env->features[FEAT_1_EDX] & CPUID_MTRR) {
-        has_msr_mtrr = true;
-    }
     if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_RDTSCP)) {
         has_msr_tsc_aux = false;
     }
@@ -1735,7 +1731,7 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
                                 env->msr_hv_stimer_count[j]);
             }
         }
-        if (has_msr_mtrr) {
+        if (env->features[FEAT_1_EDX] & CPUID_MTRR) {
             uint64_t phys_mask = MAKE_64BIT_MASK(0, cpu->phys_bits);
 
             kvm_msr_entry_add(cpu, MSR_MTRRdefType, env->mtrr_deftype);
@@ -2125,7 +2121,7 @@ static int kvm_get_msrs(X86CPU *cpu)
             kvm_msr_entry_add(cpu, msr, 0);
         }
     }
-    if (has_msr_mtrr) {
+    if (env->features[FEAT_1_EDX] & CPUID_MTRR) {
         kvm_msr_entry_add(cpu, MSR_MTRRdefType, 0);
         kvm_msr_entry_add(cpu, MSR_MTRRfix64K_00000, 0);
         kvm_msr_entry_add(cpu, MSR_MTRRfix16K_80000, 0);
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/5 target-i386: Remove has_msr_hv_apic global variable

In reply to Eduardo Habkost

The global variable is not necessary because we can check cpu->hyperv_vapic directly.

---
 target-i386/kvm.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
target-i386/kvm.c +3 -5
index 5118562..031ae90 100644
@@ -91,7 +91,6 @@ static bool has_msr_bndcfgs;
 static bool has_msr_kvm_steal_time;
 static int lm_capable_kernel;
 static bool has_msr_hv_hypercall;
-static bool has_msr_hv_vapic;
 static bool has_msr_hv_tsc;
 static bool has_msr_hv_crash;
 static bool has_msr_hv_reset;
@@ -609,7 +608,6 @@ static int hyperv_handle_properties(CPUState *cs)
     if (cpu->hyperv_vapic) {
         env->features[FEAT_HYPERV_EAX] |= HV_X64_MSR_HYPERCALL_AVAILABLE;
         env->features[FEAT_HYPERV_EAX] |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
-        has_msr_hv_vapic = true;
     }
     if (cpu->hyperv_time &&
             kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) > 0) {
@@ -728,7 +726,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
         if (cpu->hyperv_relaxed_timing) {
             c->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
         }
-        if (has_msr_hv_vapic) {
+        if (cpu->hyperv_vapic) {
             c->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
         }
         c->ebx = cpu->hyperv_spinlock_attempts;
@@ -1681,7 +1679,7 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
             kvm_msr_entry_add(cpu, HV_X64_MSR_HYPERCALL,
                               env->msr_hv_hypercall);
         }
-        if (has_msr_hv_vapic) {
+        if (cpu->hyperv_vapic) {
             kvm_msr_entry_add(cpu, HV_X64_MSR_APIC_ASSIST_PAGE,
                               env->msr_hv_vapic);
         }
@@ -2086,7 +2084,7 @@ static int kvm_get_msrs(X86CPU *cpu)
         kvm_msr_entry_add(cpu, HV_X64_MSR_HYPERCALL, 0);
         kvm_msr_entry_add(cpu, HV_X64_MSR_GUEST_OS_ID, 0);
     }
-    if (has_msr_hv_vapic) {
+    if (cpu->hyperv_vapic) {
         kvm_msr_entry_add(cpu, HV_X64_MSR_APIC_ASSIST_PAGE, 0);
     }
     if (has_msr_hv_tsc) {
Signature · 1 line
2.7.4
#3 lore raw

PATCH 3/5 target-i386: Remove has_msr_hv_tsc global variable

In reply to Eduardo Habkost

The global variable is not necessary because we can check cpu->hyperv_time directly.

We just need to ensure cpu->hyperv_time will be cleared if the feature is not really being exposed to the guest due to missing KVM_CAP_HYPERV_TIME capability.

---
 target-i386/kvm.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
target-i386/kvm.c +8 -6
index 031ae90..4046030 100644
@@ -91,7 +91,6 @@ static bool has_msr_bndcfgs;
 static bool has_msr_kvm_steal_time;
 static int lm_capable_kernel;
 static bool has_msr_hv_hypercall;
-static bool has_msr_hv_tsc;
 static bool has_msr_hv_crash;
 static bool has_msr_hv_reset;
 static bool has_msr_hv_vpindex;
@@ -602,6 +601,11 @@ static int hyperv_handle_properties(CPUState *cs)
     X86CPU *cpu = X86_CPU(cs);
     CPUX86State *env = &cpu->env;
 
+    if (cpu->hyperv_time &&
+            kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) <= 0) {
+        cpu->hyperv_time = false;
+    }
+
     if (cpu->hyperv_relaxed_timing) {
         env->features[FEAT_HYPERV_EAX] |= HV_X64_MSR_HYPERCALL_AVAILABLE;
     }
@@ -609,12 +613,10 @@ static int hyperv_handle_properties(CPUState *cs)
         env->features[FEAT_HYPERV_EAX] |= HV_X64_MSR_HYPERCALL_AVAILABLE;
         env->features[FEAT_HYPERV_EAX] |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
     }
-    if (cpu->hyperv_time &&
-            kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) > 0) {
+    if (cpu->hyperv_time) {
         env->features[FEAT_HYPERV_EAX] |= HV_X64_MSR_HYPERCALL_AVAILABLE;
         env->features[FEAT_HYPERV_EAX] |= HV_X64_MSR_TIME_REF_COUNT_AVAILABLE;
         env->features[FEAT_HYPERV_EAX] |= 0x200;
-        has_msr_hv_tsc = true;
     }
     if (cpu->hyperv_crash && has_msr_hv_crash) {
         env->features[FEAT_HYPERV_EDX] |= HV_X64_GUEST_CRASH_MSR_AVAILABLE;
@@ -1683,7 +1685,7 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
             kvm_msr_entry_add(cpu, HV_X64_MSR_APIC_ASSIST_PAGE,
                               env->msr_hv_vapic);
         }
-        if (has_msr_hv_tsc) {
+        if (cpu->hyperv_time) {
             kvm_msr_entry_add(cpu, HV_X64_MSR_REFERENCE_TSC, env->msr_hv_tsc);
         }
         if (has_msr_hv_crash) {
@@ -2087,7 +2089,7 @@ static int kvm_get_msrs(X86CPU *cpu)
     if (cpu->hyperv_vapic) {
         kvm_msr_entry_add(cpu, HV_X64_MSR_APIC_ASSIST_PAGE, 0);
     }
-    if (has_msr_hv_tsc) {
+    if (cpu->hyperv_time) {
         kvm_msr_entry_add(cpu, HV_X64_MSR_REFERENCE_TSC, 0);
     }
     if (has_msr_hv_crash) {
Signature · 1 line
2.7.4
#4 lore raw

PATCH 4/5 target-i386: Clear KVM CPUID features if KVM is disabled

In reply to Eduardo Habkost

This will ensure all checks for features[FEAT_KVM] in the code will be correct in case the KVM CPUID leaf is completely disabled.

---
 target-i386/cpu.c | 4 ++++
 1 file changed, 4 insertions(+)
target-i386/cpu.c +4 -0
index db12728..8aa2b0f 100644
@@ -2998,6 +2998,10 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
         cpu->env.features[w] &= ~minus_features[w];
     }
 
+    if (!kvm_enabled() || !cpu->expose_kvm) {
+        env->features[FEAT_KVM] = 0;
+    }
+
     if (env->features[FEAT_7_0_EBX] && env->cpuid_level < 7) {
         env->cpuid_level = 7;
     }
Signature · 1 line
2.7.4
#5 lore raw

PATCH 5/5 target-i386: Remove has_msr_* global vars for KVM features

In reply to Eduardo Habkost

The global variables are not necessary because we can check KVM feature flags in X86CPU directly.

---
 target-i386/kvm.c | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)
target-i386/kvm.c +6 -15
index 4046030..30b63b7 100644
@@ -83,12 +83,9 @@ static bool has_msr_tsc_aux;
 static bool has_msr_tsc_adjust;
 static bool has_msr_tsc_deadline;
 static bool has_msr_feature_control;
-static bool has_msr_async_pf_en;
-static bool has_msr_pv_eoi_en;
 static bool has_msr_misc_enable;
 static bool has_msr_smbase;
 static bool has_msr_bndcfgs;
-static bool has_msr_kvm_steal_time;
 static int lm_capable_kernel;
 static bool has_msr_hv_hypercall;
 static bool has_msr_hv_crash;
@@ -754,12 +751,6 @@ int kvm_arch_init_vcpu(CPUState *cs)
         c = &cpuid_data.entries[cpuid_i++];
         c->function = KVM_CPUID_FEATURES | kvm_base;
         c->eax = env->features[FEAT_KVM];
-
-        has_msr_async_pf_en = c->eax & (1 << KVM_FEATURE_ASYNC_PF);
-
-        has_msr_pv_eoi_en = c->eax & (1 << KVM_FEATURE_PV_EOI);
-
-        has_msr_kvm_steal_time = c->eax & (1 << KVM_FEATURE_STEAL_TIME);
     }
 
     cpu_x86_cpuid(env, 0, 0, &limit, &unused, &unused, &unused);
@@ -1639,13 +1630,13 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
         kvm_msr_entry_add(cpu, MSR_IA32_TSC, env->tsc);
         kvm_msr_entry_add(cpu, MSR_KVM_SYSTEM_TIME, env->system_time_msr);
         kvm_msr_entry_add(cpu, MSR_KVM_WALL_CLOCK, env->wall_clock_msr);
-        if (has_msr_async_pf_en) {
+        if (env->features[FEAT_KVM] & (1 << KVM_FEATURE_ASYNC_PF)) {
             kvm_msr_entry_add(cpu, MSR_KVM_ASYNC_PF_EN, env->async_pf_en_msr);
         }
-        if (has_msr_pv_eoi_en) {
+        if (env->features[FEAT_KVM] & (1 << KVM_FEATURE_PV_EOI)) {
             kvm_msr_entry_add(cpu, MSR_KVM_PV_EOI_EN, env->pv_eoi_en_msr);
         }
-        if (has_msr_kvm_steal_time) {
+        if (env->features[FEAT_KVM] & (1 << KVM_FEATURE_STEAL_TIME)) {
             kvm_msr_entry_add(cpu, MSR_KVM_STEAL_TIME, env->steal_time_msr);
         }
         if (has_msr_architectural_pmu) {
@@ -2048,13 +2039,13 @@ static int kvm_get_msrs(X86CPU *cpu)
 #endif
     kvm_msr_entry_add(cpu, MSR_KVM_SYSTEM_TIME, 0);
     kvm_msr_entry_add(cpu, MSR_KVM_WALL_CLOCK, 0);
-    if (has_msr_async_pf_en) {
+    if (env->features[FEAT_KVM] & (1 << KVM_FEATURE_ASYNC_PF)) {
         kvm_msr_entry_add(cpu, MSR_KVM_ASYNC_PF_EN, 0);
     }
-    if (has_msr_pv_eoi_en) {
+    if (env->features[FEAT_KVM] & (1 << KVM_FEATURE_PV_EOI)) {
         kvm_msr_entry_add(cpu, MSR_KVM_PV_EOI_EN, 0);
     }
-    if (has_msr_kvm_steal_time) {
+    if (env->features[FEAT_KVM] & (1 << KVM_FEATURE_STEAL_TIME)) {
         kvm_msr_entry_add(cpu, MSR_KVM_STEAL_TIME, 0);
     }
     if (has_msr_architectural_pmu) {
Signature · 1 line
2.7.4
maintainer Reviewed-by on the v1 series
#6 lore raw
In reply to Eduardo Habkost

----- Original Message -----

Quoted 17 lines

From: "Eduardo Habkost" <[email protected]>
To: "Paolo Bonzini" <[email protected]>, "Marcelo Tosatti" <[email protected]>
Cc: [email protected], [email protected]
Sent: Tuesday, September 27, 2016 12:24:05 AM
Subject: [PATCH 0/5] target-i386: Eliminate unnecessary has_msr_* global variables

Some of the has_msr_* global variables are set depending on X86PCU fields and are unnecessary because we can look at the X86CPU object directly. This series eliminates some of them.

Eduardo Habkost (5):
target-i386: Remove has_msr_mtrr global variable
target-i386: Remove has_msr_hv_apic global variable
target-i386: Remove has_msr_hv_tsc global variable
target-i386: Clear KVM CPUID features if KVM is disabled
target-i386: Remove has_msr_* global vars for KVM features

Quoted 3 lines
 target-i386/cpu.c |  4 ++++
 target-i386/kvm.c | 51 +++++++++++++++++++--------------------------------
 2 files changed, 23 insertions(+), 32 deletions(-)
#7 lore raw
Quoted 18 lines · Paolo Bonzini

----- Original Message -----

Quoted 17 lines

From: "Eduardo Habkost" <[email protected]>
To: "Paolo Bonzini" <[email protected]>, "Marcelo Tosatti" <[email protected]>
Cc: [email protected], [email protected]
Sent: Tuesday, September 27, 2016 12:24:05 AM
Subject: [PATCH 0/5] target-i386: Eliminate unnecessary has_msr_* global variables

Some of the has_msr_* global variables are set depending on X86PCU fields and are unnecessary because we can look at the X86CPU object directly. This series eliminates some of them.

Eduardo Habkost (5):
target-i386: Remove has_msr_mtrr global variable
target-i386: Remove has_msr_hv_apic global variable
target-i386: Remove has_msr_hv_tsc global variable
target-i386: Clear KVM CPUID features if KVM is disabled
target-i386: Remove has_msr_* global vars for KVM features

Quoted 6 lines
Quoted 3 lines
 target-i386/cpu.c |  4 ++++
 target-i386/kvm.c | 51 +++++++++++++++++++--------------------------------
 2 files changed, 23 insertions(+), 32 deletions(-)

Thanks, queued on x86-next.

Signature · 1 line
Eduardo