From df3c105aac7935f8ed99458d853b364989b47334 Mon Sep 17 00:00:00 2001
Message-Id: <df3c105aac7935f8ed99458d853b364989b47334.1337678792.git.minovotn@redhat.com>
From: Eduardo Habkost <ehabkost@redhat.com>
Date: Mon, 21 May 2012 13:54:40 +0200
Subject: [PATCH 1/2] kvm: x86: Pass KVMState to kvm_arch_get_supported_cpuid

RH-Author: Eduardo Habkost <ehabkost@redhat.com>
Message-id: <1337608481-29002-2-git-send-email-ehabkost@redhat.com>
Patchwork-id: 39748
O-Subject: [RHEL6.3 qemu-kvm PATCH 1/2] kvm: x86: Pass KVMState to kvm_arch_get_supported_cpuid
Bugzilla: 819562
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Gleb Natapov <gleb@redhat.com>
RH-Acked-by: Marcelo Tosatti <mtosatti@redhat.com>

kvm_arch_get_supported_cpuid checks for global cpuid restrictions, it
does not require any CPUState reference. Changing its interface allows
to call it before any VCPU is initialized.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
(cherry picked from commit ba9bc59e1f5dc91caf35e0ef08da137b3a5e7386)

Conflicts:

	target-i386/cpuid.c
	target-i386/kvm.c

Additional changes, to code that doesn't exist upstream anymore:
	qemu-kvm.h
	qemu-kvm-x86.c

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 kvm.h               |    2 +-
 qemu-kvm-x86.c      |   12 ++++++------
 qemu-kvm.h          |    3 ++-
 target-i386/cpuid.c |   30 +++++++++++++++++-------------
 target-i386/kvm.c   |   17 +++++++++--------
 5 files changed, 35 insertions(+), 29 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 kvm.h               |    2 +-
 qemu-kvm-x86.c      |   12 ++++++------
 qemu-kvm.h          |    3 ++-
 target-i386/cpuid.c |   30 +++++++++++++++++-------------
 target-i386/kvm.c   |   17 +++++++++--------
 5 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/kvm.h b/kvm.h
index 884d55a..c10d4a0 100644
--- a/kvm.h
+++ b/kvm.h
@@ -143,7 +143,7 @@ void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
 
 int kvm_check_extension(KVMState *s, unsigned int extension);
 
-uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
+uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function,
                                       uint32_t index, int reg);
 void kvm_cpu_synchronize_state(CPUState *env);
 
diff --git a/qemu-kvm-x86.c b/qemu-kvm-x86.c
index 54c9102..59d32f1 100644
--- a/qemu-kvm-x86.c
+++ b/qemu-kvm-x86.c
@@ -1400,23 +1400,23 @@ int kvm_arch_init_vcpu(CPUState *cenv)
     pv_ent = &cpuid_ent[cpuid_nent++];
     memset(pv_ent, 0, sizeof(*pv_ent));
     pv_ent->function = KVM_CPUID_FEATURES;
-    pv_ent->eax = cenv->cpuid_kvm_features & kvm_arch_get_supported_cpuid(cenv,
+    pv_ent->eax = cenv->cpuid_kvm_features & kvm_arch_get_supported_cpuid(cenv->kvm_state,
 						KVM_CPUID_FEATURES, 0, R_EAX);
 #endif
 
     kvm_trim_features(&cenv->cpuid_features,
-                      kvm_arch_get_supported_cpuid(cenv, 1, 0, R_EDX));
+                      kvm_arch_get_supported_cpuid(cenv->kvm_state, 1, 0, R_EDX));
 
     /* prevent the hypervisor bit from being cleared by the kernel */
     i = cenv->cpuid_ext_features & CPUID_EXT_HYPERVISOR;
     kvm_trim_features(&cenv->cpuid_ext_features,
-                      kvm_arch_get_supported_cpuid(cenv, 1, 0, R_ECX));
+                      kvm_arch_get_supported_cpuid(cenv->kvm_state, 1, 0, R_ECX));
     cenv->cpuid_ext_features |= i;
 
     kvm_trim_features(&cenv->cpuid_ext2_features,
-                      kvm_arch_get_supported_cpuid(cenv, 0x80000001, 0, R_EDX));
+                      kvm_arch_get_supported_cpuid(cenv->kvm_state, 0x80000001, 0, R_EDX));
     kvm_trim_features(&cenv->cpuid_ext3_features,
-                      kvm_arch_get_supported_cpuid(cenv, 0x80000001, 0, R_ECX));
+                      kvm_arch_get_supported_cpuid(cenv->kvm_state, 0x80000001, 0, R_ECX));
 
     copy = *cenv;
 
@@ -1816,7 +1816,7 @@ int kvm_arch_init_irq_routing(void)
     return 0;
 }
 
-uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
+uint32_t kvm_arch_get_supported_cpuid(KVMState *env, uint32_t function,
                                       uint32_t index, int reg)
 {
     return kvm_get_supported_cpuid(kvm_context, function, index, reg);
diff --git a/qemu-kvm.h b/qemu-kvm.h
index 738e2fb..ee0980c 100644
--- a/qemu-kvm.h
+++ b/qemu-kvm.h
@@ -1143,7 +1143,8 @@ static inline void cpu_synchronize_state(CPUState *env)
     }
 }
 
-uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
+struct KVMState;
+uint32_t kvm_arch_get_supported_cpuid(struct KVMState *env, uint32_t function,
                                       uint32_t index, int reg);
 
 
diff --git a/target-i386/cpuid.c b/target-i386/cpuid.c
index 70f9c93..a24c228 100644
--- a/target-i386/cpuid.c
+++ b/target-i386/cpuid.c
@@ -493,7 +493,7 @@ static void summary_cpuid_features(CPUX86State *env, x86_def_t *hd)
         for (p = fmap; p->pfeat; ++p) {
             if (p->mask) {
                 *p->pfeat |= p->mask &
-                    kvm_arch_get_supported_cpuid(env, p->cmd, 0, p->reg);
+                    kvm_arch_get_supported_cpuid(env->kvm_state, p->cmd, 0, p->reg);
             }
         }
     }
@@ -1074,10 +1074,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
         break;
    case 7:
        if (kvm_enabled()) {
-           *eax = kvm_arch_get_supported_cpuid(env, 0x7, 0, R_EAX);
-           *ebx = kvm_arch_get_supported_cpuid(env, 0x7, 0, R_EBX);
-           *ecx = kvm_arch_get_supported_cpuid(env, 0x7, 0, R_ECX);
-           *edx = kvm_arch_get_supported_cpuid(env, 0x7, 0, R_EDX);
+           KVMState *s = env->kvm_state;
+           *eax = kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EAX);
+           *ebx = kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EBX);
+           *ecx = kvm_arch_get_supported_cpuid(s, 0x7, 0, R_ECX);
+           *edx = kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EDX);
        } else {
            *eax = 0;
            *ebx = 0;
@@ -1095,10 +1096,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
     case 0xA:
         /* Architectural Performance Monitoring Leaf */
         if (kvm_enabled() && !cpuid_leaf10_disabled) {
-            *eax = kvm_arch_get_supported_cpuid(env, 0xA, count, R_EAX);
-            *ebx = kvm_arch_get_supported_cpuid(env, 0xA, count, R_EBX);
-            *ecx = kvm_arch_get_supported_cpuid(env, 0xA, count, R_ECX);
-            *edx = kvm_arch_get_supported_cpuid(env, 0xA, count, R_EDX);
+            KVMState *s = env->kvm_state;
+            *eax = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EAX);
+            *ebx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EBX);
+            *ecx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_ECX);
+            *edx = kvm_arch_get_supported_cpuid(s, 0xA, count, R_EDX);
         } else {
             *eax = 0;
             *ebx = 0;
@@ -1116,10 +1118,12 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
             break;
         }
         if (kvm_enabled()) {
-            *eax = kvm_arch_get_supported_cpuid(env, 0xd, count, R_EAX);
-            *ebx = kvm_arch_get_supported_cpuid(env, 0xd, count, R_EBX);
-            *ecx = kvm_arch_get_supported_cpuid(env, 0xd, count, R_ECX);
-            *edx = kvm_arch_get_supported_cpuid(env, 0xd, count, R_EDX);
+            KVMState *s = env->kvm_state;
+
+            *eax = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EAX);
+            *ebx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EBX);
+            *ecx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_ECX);
+            *edx = kvm_arch_get_supported_cpuid(s, 0xd, count, R_EDX);
         } else {
             *eax = 0;
             *ebx = 0;
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index a4ef91f..a3e87dd 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -66,7 +66,7 @@ static struct kvm_cpuid2 *try_get_cpuid(KVMState *s, int max)
     return cpuid;
 }
 
-uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
+uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function,
                                       uint32_t index, int reg)
 {
     struct kvm_cpuid2 *cpuid;
@@ -74,12 +74,12 @@ uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
     uint32_t ret = 0;
     uint32_t cpuid_1_edx;
 
-    if (!kvm_check_extension(env->kvm_state, KVM_CAP_EXT_CPUID)) {
+    if (!kvm_check_extension(struct, KVM_CAP_EXT_CPUID)) {
         return -1U;
     }
 
     max = 1;
-    while ((cpuid = try_get_cpuid(env->kvm_state, max)) == NULL) {
+    while ((cpuid = try_get_cpuid(s, max)) == NULL) {
         max *= 2;
     }
 
@@ -102,7 +102,7 @@ uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function,
                     /* On Intel, kvm returns cpuid according to the Intel spec,
                      * so add missing bits according to the AMD spec:
                      */
-                    cpuid_1_edx = kvm_arch_get_supported_cpuid(env, 1, 0, R_EDX);
+                    cpuid_1_edx = kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX);
                     ret |= cpuid_1_edx & 0xdfeff7ff;
                 }
                 break;
@@ -144,23 +144,24 @@ int kvm_arch_init_vcpu(CPUState *env)
         struct kvm_cpuid2 cpuid;
         struct kvm_cpuid_entry2 entries[100];
     } __attribute__((packed)) cpuid_data;
+    KVMState *s = env->kvm_state;
     uint32_t limit, i, j, cpuid_i;
     uint32_t unused;
 
     env->mp_state = KVM_MP_STATE_RUNNABLE;
 
     kvm_trim_features(&env->cpuid_features,
-        kvm_arch_get_supported_cpuid(env, 1, 0, R_EDX));
+        kvm_arch_get_supported_cpuid(s, 1, 0, R_EDX));
 
     i = env->cpuid_ext_features & CPUID_EXT_HYPERVISOR;
     kvm_trim_features(&env->cpuid_ext_features,
-        kvm_arch_get_supported_cpuid(env, 1, 0, R_ECX));
+        kvm_arch_get_supported_cpuid(s, 1, 0, R_ECX));
     env->cpuid_ext_features |= i;
 
     kvm_trim_features(&env->cpuid_ext2_features,
-        kvm_arch_get_supported_cpuid(env, 0x80000001, 0, R_EDX));
+        kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_EDX));
     kvm_trim_features(&env->cpuid_ext3_features,
-        kvm_arch_get_supported_cpuid(env, 0x80000001, 0, R_ECX));
+        kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_ECX));
 
     cpuid_i = 0;
 
-- 
1.7.7.6

