Skip to contentSkip to conversation

Support for cpuid functions with subleaves

PATCH In review Amit Shah 2 participants 8 messages Jan 13, 2009 – Jan 14, 2009

Subsystems (3)
lore
Share & tools
lore

Amit Shah

author
#0 lore raw

These patches add support for handling sub-leaf values of some cpuid functions.

The kernel patch updates a userspace-visible for a typo. That value is not yet used in the userspace so this should be harmless.

With these patches, a VM with the core2duo cpu type boots successfully.

Amit.

author
#1 lore raw

PATCH 1/1 KVM: x86: Fix typos and whitespace errors

Some typos, comments, whitespace errors corrected in the cpuid code

---
 arch/x86/include/asm/kvm.h |    6 ++--
 arch/x86/kvm/x86.c         |   45 +++++++++++++++++++++----------------------
 2 files changed, 25 insertions(+), 26 deletions(-)
arch/x86/include/asm/kvm.h +3 -3
index a031102..58e28ab 100644
@@ -177,9 +177,9 @@ struct kvm_cpuid_entry2 {
 	__u32 padding[3];
 };
 
-#define KVM_CPUID_FLAG_SIGNIFCANT_INDEX 1
-#define KVM_CPUID_FLAG_STATEFUL_FUNC    2
-#define KVM_CPUID_FLAG_STATE_READ_NEXT  4
+#define KVM_CPUID_FLAG_SIGNIFICANT_INDEX 1
+#define KVM_CPUID_FLAG_STATEFUL_FUNC     2
+#define KVM_CPUID_FLAG_STATE_READ_NEXT   4
 
 /* for KVM_SET_CPUID2 */
 struct kvm_cpuid2 {
arch/x86/kvm/x86.c +22 -23

158 lines not on this page · raw message

Signature · 1 line
1.6.0.6
author
#2 lore raw

PATCH 1/2 kvm: libkvm: Add a wrapper for an ioctl for the KVM_SET_CPUID2 interface

kvm_set_cpuid2() builds on top of kvm_set_cpuid() and correctly populates cpuid functions that have sub-leaves.

---
 libkvm/libkvm-x86.c |   18 ++++++++++++++++++
 libkvm/libkvm.h     |   16 ++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)
libkvm/libkvm-x86.c +18 -0
index a8cca15..93a9c4e 100644
@@ -475,6 +475,24 @@ int kvm_setup_cpuid(kvm_context_t kvm, int vcpu, int nent,
 	return r;
 }
 
+int kvm_setup_cpuid2(kvm_context_t kvm, int vcpu, int nent,
+		     struct kvm_cpuid_entry2 *entries)
+{
+	struct kvm_cpuid2 *cpuid;
+	int r;
+
+	cpuid = malloc(sizeof(*cpuid) + nent * sizeof(*entries));
+	if (!cpuid)
+		return -ENOMEM;
+
+	cpuid->nent = nent;
+	memcpy(cpuid->entries, entries, nent * sizeof(*entries));
+	r = ioctl(kvm->vcpu_fd[vcpu], KVM_SET_CPUID2, cpuid);
+
+	free(cpuid);
+	return r;
+}
+
 int kvm_set_shadow_pages(kvm_context_t kvm, unsigned int nrshadow_pages)
 {
 #ifdef KVM_CAP_MMU_SHADOW_CACHE_CONTROL
libkvm/libkvm.h +16 -0
index ee1ba68..5728b1c 100644
@@ -373,6 +373,22 @@ int kvm_setup_cpuid(kvm_context_t kvm, int vcpu, int nent,
 		    struct kvm_cpuid_entry *entries);
 
 /*!
+ * \brief Setup a vcpu's cpuid instruction emulation
+ *
+ * Set up a table of cpuid function to cpuid outputs.
+ * This call replaces the older kvm_setup_cpuid interface by adding a few
+ * parameters to support cpuid functions that have sub-leaf values.
+ *
+ * \param kvm Pointer to the current kvm_context
+ * \param vcpu Which virtual CPU should be initialized
+ * \param nent number of entries to be installed
+ * \param entries cpuid function entries table
+ * \return 0 on success, or -errno on error
+ */
+int kvm_setup_cpuid2(kvm_context_t kvm, int vcpu, int nent,
+		     struct kvm_cpuid_entry2 *entries);
+
+/*!
  * \brief Setting the number of shadow pages to be allocated to the vm
  *
  * \param kvm pointer to kvm_context
Signature · 1 line
1.6.0.6
author
#3 lore raw

PATCH 2/2 KVM: userspace: Fetch sub-leaf cpuid values for functions 4, 0xb, 0xd.

CPUID functions 4, 0xb and 0xd have sub-leaf values which depend on the input value of ECX. Fetch these cpuid values and pass them on to the kernel.

We also switch to the kvm_set_cpuid2() ioctl for this; kvm_set_cpuid() can't handle the extra parameters we need to support sub-leaves.

---
 qemu/qemu-kvm-x86.c |   37 ++++++++++++++++++++++++++++---------
 1 files changed, 28 insertions(+), 9 deletions(-)
qemu/qemu-kvm-x86.c +28 -9
index 4fad2af..d9736b4 100644
@@ -462,10 +462,11 @@ void kvm_arch_save_regs(CPUState *env)
     }
 }
 
-static void do_cpuid_ent(struct kvm_cpuid_entry *e, uint32_t function,
-			 CPUState *env)
+static void do_cpuid_ent(struct kvm_cpuid_entry2 *e, uint32_t function,
+                         uint32_t count, CPUState *env)
 {
     env->regs[R_EAX] = function;
+    env->regs[R_ECX] = count;
     qemu_kvm_cpuid_on_env(env);
     e->function = function;
     e->eax = env->regs[R_EAX];
@@ -507,14 +508,14 @@ static int get_para_features(kvm_context_t kvm_context)
 
 int kvm_arch_qemu_init_env(CPUState *cenv)
 {
-    struct kvm_cpuid_entry cpuid_ent[100];
+    struct kvm_cpuid_entry2 cpuid_ent[100];
 #ifdef KVM_CPUID_SIGNATURE
-    struct kvm_cpuid_entry *pv_ent;
+    struct kvm_cpuid_entry2 *pv_ent;
     uint32_t signature[3];
 #endif
     int cpuid_nent = 0;
     CPUState copy;
-    uint32_t i, limit;
+    uint32_t i, j, limit;
 
     copy = *cenv;
 
@@ -539,17 +540,35 @@ int kvm_arch_qemu_init_env(CPUState *cenv)
     qemu_kvm_cpuid_on_env(&copy);
     limit = copy.regs[R_EAX];
 
-    for (i = 0; i <= limit; ++i)
-	do_cpuid_ent(&cpuid_ent[cpuid_nent++], i, &copy);
+    for (i = 0; i <= limit; ++i) {
+        if (i == 4 || i == 0xb || i == 0xd) {
+            for (j = 0; ; ++j) {
+                do_cpuid_ent(&cpuid_ent[cpuid_nent], i, j, &copy);
+
+                cpuid_ent[cpuid_nent].flags = KVM_CPUID_FLAG_SIGNIFICANT_INDEX;
+                cpuid_ent[cpuid_nent].index = j;
+
+                cpuid_nent++;
+
+                if (i == 4 && copy.regs[R_EAX] == 0)
+                    break;
+                if (i == 0xb && !(copy.regs[R_ECX] & 0xff00))
+                    break;
+                if (i == 0xd && copy.regs[R_EAX] == 0)
+                    break;
+            }
+        } else
+            do_cpuid_ent(&cpuid_ent[cpuid_nent++], i, 0, &copy);
+    }
 
     copy.regs[R_EAX] = 0x80000000;
     qemu_kvm_cpuid_on_env(&copy);
     limit = copy.regs[R_EAX];
 
     for (i = 0x80000000; i <= limit; ++i)
-	do_cpuid_ent(&cpuid_ent[cpuid_nent++], i, &copy);
+	do_cpuid_ent(&cpuid_ent[cpuid_nent++], i, 0, &copy);
 
-    kvm_setup_cpuid(kvm_context, cenv->cpu_index, cpuid_nent, cpuid_ent);
+    kvm_setup_cpuid2(kvm_context, cenv->cpu_index, cpuid_nent, cpuid_ent);
     return 0;
 }
 
Signature · 1 line
1.6.0.6
#4 lore raw

Re: [PATCH 2/2] KVM: userspace: Fetch sub-leaf cpuid values for functions 4, 0xb, 0xd.

Quoted 6 lines · Amit Shah

CPUID functions 4, 0xb and 0xd have sub-leaf values which depend on the input value of ECX. Fetch these cpuid values and pass them on to the kernel.

We also switch to the kvm_set_cpuid2() ioctl for this; kvm_set_cpuid() can't handle the extra parameters we need to support sub-leaves.

Looks fine, just need the first patch fixed.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw

Re: [PATCH 1/2] kvm: libkvm: Add a wrapper for an ioctl for the KVM_SET_CPUID2 interface

In reply to Amit Shah
Quoted 25 lines · Amit Shah

kvm_set_cpuid2() builds on top of kvm_set_cpuid() and correctly populates cpuid functions that have sub-leaves.

---
 libkvm/libkvm-x86.c |   18 ++++++++++++++++++
 libkvm/libkvm.h     |   16 ++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)
libkvm/libkvm-x86.c +8 -0
index a8cca15..93a9c4e 100644
@@ -475,6 +475,24 @@ int kvm_setup_cpuid(kvm_context_t kvm, int vcpu, int nent,
 	return r;
 }
 
+int kvm_setup_cpuid2(kvm_context_t kvm, int vcpu, int nent,
+		     struct kvm_cpuid_entry2 *entries)
+{
+	struct kvm_cpuid2 *cpuid;
+	int r;
+
+	cpuid = malloc(sizeof(*cpuid) + nent * sizeof(*entries));
+	if (!cpuid)

libkvm/libkvm-x86.c int kvm_setup_cpuid(kvm_context_t kvm, int vcpu, int nent, · line 495

Quoted 10 lines
libkvm/libkvm-x86.c +9 -0
+		return -ENOMEM;
+
+	cpuid->nent = nent;
+	memcpy(cpuid->entries, entries, nent * sizeof(*entries));
+	r = ioctl(kvm->vcpu_fd[vcpu], KVM_SET_CPUID2, cpuid);
+
+	free(cpuid);
+	return r;
+}
 

Should return -errno, not -EPERM, if the ioctl fails.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#6 lore raw

Re: [PATCH] KVM: x86: Fix typos and whitespace errors

In reply to Amit Shah
Quoted 13 lines · Amit Shah

Some typos, comments, whitespace errors corrected in the cpuid code

---
 arch/x86/include/asm/kvm.h |    6 ++--
 arch/x86/kvm/x86.c         |   45 +++++++++++++++++++++----------------------
 2 files changed, 25 insertions(+), 26 deletions(-)
arch/x86/include/asm/kvm.h +0 -0
index a031102..58e28ab 100644

arch/x86/include/asm/kvm.h struct kvm_cpuid_entry2 { · line 180

Quoted 7 lines
arch/x86/include/asm/kvm.h +1 -3
@@ -177,9 +177,9 @@ struct kvm_cpuid_entry2 {
 	__u32 padding[3];
 };
 
-#define KVM_CPUID_FLAG_SIGNIFCANT_INDEX 1
-#define KVM_CPUID_FLAG_STATEFUL_FUNC    2
-#define KVM_CPUID_FLAG_STATE_READ_NEXT  4
+#define KVM_CPUID_FLAG_SIGNIFICANT_INDEX 1

Avi, this is assuming we can push this fix to the stable kernel series. Can that be done or should we live with the typo? (Or adding a #define KVM_CPUID_FLAG_SIGNIFICANT_INDEX KVM_CPUID_FLAG_SIGNIFCANT_INDEX ?)

#7 lore raw

Re: [PATCH] KVM: x86: Fix typos and whitespace errors

Quoted 11 lines · Amit Shah

arch/x86/include/asm/kvm.h struct kvm_cpuid_entry2 { · line 180

Quoted 5 lines
arch/x86/include/asm/kvm.h +1 -3
-#define KVM_CPUID_FLAG_SIGNIFCANT_INDEX 1
-#define KVM_CPUID_FLAG_STATEFUL_FUNC    2
-#define KVM_CPUID_FLAG_STATE_READ_NEXT  4
+#define KVM_CPUID_FLAG_SIGNIFICANT_INDEX 1

Avi, this is assuming we can push this fix to the stable kernel series. Can that be done or should we live with the typo? (Or adding a #define KVM_CPUID_FLAG_SIGNIFICANT_INDEX KVM_CPUID_FLAG_SIGNIFCANT_INDEX ?)

I think we can live with the typo. We have enough compatibility headaches.

Signature · 1 line
error compiling committee.c: too many arguments to function