Skip to contentSkip to conversation

exposing host cpuids to guest

PATCH In review Nitin A Kamble 2 participants 6 messages Jan 30, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 exposing host cpuids to guest

Avi,
I reworked the earlier patch for exposing the host cpuid bits to
guests. Attached is the patch for your kvm.git tree. With this new code in the kernel both the old and new qemu binaries are working.

There are also changes for the kvm-userspace.git tree. I will be
sending out those changes too.

Please apply or give feedback for this patch.

Thanks & Regards,
Nitin

arch/x86/include/asm/kvm_host.h +1 -1
index 55fd4c5..b450a69 100644
@@ -87,7 +87,7 @@
 #define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
 #define KVM_MIN_FREE_MMU_PAGES 5
 #define KVM_REFILL_PAGES 25
-#define KVM_MAX_CPUID_ENTRIES 40
+#define KVM_MAX_CPUID_ENTRIES 100
 #define KVM_NR_FIXED_MTRR_REGION 88
 #define KVM_NR_VAR_MTRR 8
 
arch/x86/kvm/x86.c +26 -10
index e96edda..4d5124b 100644
@@ -1060,19 +1060,25 @@ long kvm_arch_dev_ioctl(struct file *filp,
 	case KVM_GET_SUPPORTED_CPUID: {
 		struct kvm_cpuid2 __user *cpuid_arg = argp;
 		struct kvm_cpuid2 cpuid;
+		int retry = 0;
 
 		r = -EFAULT;
 		if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
 			goto out;
 		r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
 						      cpuid_arg->entries);
-		if (r)
+		if (r == -EAGAIN)
+			retry = 1;
+		else if (r)
 			goto out;
 
 		r = -EFAULT;
 		if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
 			goto out;
-		r = 0;
+		if (retry)
+			r = -EAGAIN;
+		else
+			r = 0;
 		break;
 	}
 	default:
@@ -1325,10 +1331,14 @@ static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
 {
 	struct kvm_cpuid_entry2 *cpuid_entries;
 	int limit, nent = 0, r = -E2BIG;
+	int sizer = 0;
 	u32 func;
 
-	if (cpuid->nent < 1)
-		goto out;
+	if (cpuid->nent == 0) {
+		sizer = 1;
+		cpuid->nent = KVM_MAX_CPUID_ENTRIES;
+	}
+
 	r = -ENOMEM;
 	cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
 	if (!cpuid_entries)
@@ -1339,6 +1349,7 @@ static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
 	for (func = 1; func <= limit && nent < cpuid->nent; ++func)
 		do_cpuid_ent(&cpuid_entries[nent], func, 0,
 			     &nent, cpuid->nent);
+
 	r = -E2BIG;
 	if (nent >= cpuid->nent)
 		goto out_free;
@@ -1348,16 +1359,21 @@ static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
 	for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
 		do_cpuid_ent(&cpuid_entries[nent], func, 0,
 			     &nent, cpuid->nent);
-	r = -EFAULT;
-	if (copy_to_user(entries, cpuid_entries,
-			 nent * sizeof(struct kvm_cpuid_entry2)))
-		goto out_free;
-	cpuid->nent = nent;
-	r = 0;
+
+	if (sizer)
+		r = -EAGAIN;
+	else {
+		r = -EFAULT;
+		if (copy_to_user(entries, cpuid_entries,
+				 nent * sizeof(struct kvm_cpuid_entry2)))
+			goto out_free;
+		r = 0;
+	}
 
 out_free:
 	vfree(cpuid_entries);
 out:
+	cpuid->nent = nent;
 	return r;
 }
 
#1 lore raw

PATCH 1/1 exposing host cpuids to guest

Avi,
Attached is the patch for kvm-userspace.git tree. With this patch
adding "-cpu host" to qemu commandline would expose the host cpuids to the guest. Some of the cpuid data is still filtered in the kernel kvm code.
Please apply or give comments for the patch.

Thanks & Regards,
Nitin

Quoted 5 lines · Nitin A Kamble

Avi,
I reworked the earlier patch for exposing the host cpuid bits to
guests. Attached is the patch for your kvm.git tree. With this new code in the kernel both the old and new qemu binaries are working.

Quoted 7 lines

There are also changes for the kvm-userspace.git tree. I will be
sending out those changes too.

Please apply or give feedback for this patch.

Thanks & Regards,
Nitin

libkvm/libkvm-x86.c +31 -0
index dcef548..10f6614 100644
@@ -379,6 +379,37 @@ int kvm_set_msrs(kvm_context_t kvm, int vcpu, struct kvm_msr_entry *msrs,
     return r;
 }
 
+/*
+ * Returns available host cpuid entries.  User must free.
+ */
+struct kvm_cpuid2 *kvm_get_host_cpuid_entries(kvm_context_t kvm)
+{
+	struct kvm_cpuid2 sizer, *cpuids;
+	int r, e;
+
+	sizer.nent = 0;
+	r = ioctl(kvm->fd, KVM_GET_SUPPORTED_CPUID, &sizer);
+	if (r != -1 && errno != EAGAIN)
+		return NULL;
+
+	cpuids = malloc(sizeof *cpuids + sizer.nent * sizeof *cpuids->entries);
+	if (!cpuids) {
+		errno = ENOMEM;
+		return NULL;
+	}
+
+	cpuids->nent = sizer.nent;
+	r = ioctl(kvm->fd, KVM_GET_SUPPORTED_CPUID, cpuids);
+	if (r == -1) {
+		e = errno;
+		free(cpuids);
+		errno = e;
+		return NULL;
+	}
+	return cpuids;
+}
+
+
 static void print_seg(FILE *file, const char *name, struct kvm_segment *seg)
 {
     	fprintf(stderr,
libkvm/libkvm.h +3 -0
index e79e4fd..5b7f063 100644
@@ -27,6 +27,9 @@ typedef struct kvm_context *kvm_context_t;
 struct kvm_msr_list *kvm_get_msr_list(kvm_context_t);
 int kvm_get_msrs(kvm_context_t, int vcpu, struct kvm_msr_entry *msrs, int n);
 int kvm_set_msrs(kvm_context_t, int vcpu, struct kvm_msr_entry *msrs, int n);
+struct kvm_cpuid2 *kvm_get_host_cpuid_entries(kvm_context_t);
+void get_host_cpuid_entry(uint32_t function, uint32_t index,
+				struct kvm_cpuid_entry2 *e);
 #endif
 
 /*!
qemu/qemu-kvm-x86.c +71 -15

137 lines not on this page · raw message

qemu/target-i386/cpu.h +1 -0
index 944e386..0a0fccf 100644
@@ -628,6 +628,7 @@ typedef struct CPUX86State {
     uint32_t cpuid_ext2_features;
     uint32_t cpuid_ext3_features;
     uint32_t cpuid_apic_id;
+    uint32_t cpuid_host_cpu;
 
 #ifdef USE_KQEMU
     int kqemu_enabled;
qemu/target-i386/helper.c +50 -0

66 lines not on this page · raw message

#2 lore raw
Quoted 6 lines · Nitin A Kamble

Avi,
Attached is the patch for kvm-userspace.git tree. With this patch
adding "-cpu host" to qemu commandline would expose the host cpuids to the guest. Some of the cpuid data is still filtered in the kernel kvm code.
Please apply or give comments for the patch.

Please split them into multiple patches; they do a lot of things here. It's easier to review once all the logical changes are separated out.

Also, split out the qemu-specific (qemu/target-i386/*) and kvm-specific parts so that the qemu-specific ones can be sent upstream.

Thanks,
Amit

#3 lore raw
Quoted 5 lines · Amit Shah

Please split them into multiple patches; they do a lot of things here. It's easier to review once all the logical changes are separated out.

Also, split out the qemu-specific (qemu/target-i386/*) and kvm-specific parts so that the qemu-specific ones can be sent upstream.

ok, I will try to split all the patches like that. My Concern is, The code is inter-dependent, if all splits are not in then it may break the tree's functionality. I will try to group the split patches for that reason.

Thanks & Regards,
Nitin

#4 lore raw
In reply to Nitin A Kamble
Quoted 4 lines · Nitin A Kamble

Avi,
I reworked the earlier patch for exposing the host cpuid bits to
guests. Attached is the patch for your kvm.git tree. With this new code in the kernel both the old and new qemu binaries are working.

Please add a Signed-Off-By and a git-style description for patches sent for inclusion.

Also, it'll be better to reply to the patch if you include in the message instead of attaching them (git send-email).

arch/x86/include/asm/kvm_host.h line 90

Quoted 2 lines
arch/x86/include/asm/kvm_host.h +1 -1
-#define KVM_MAX_CPUID_ENTRIES 40
+#define KVM_MAX_CPUID_ENTRIES 100

Are we already hitting this limit? I don't think so.

Quoted 20 lines
arch/x86/kvm/x86.c +4 -1
@@ -1060,19 +1060,25 @@ long kvm_arch_dev_ioctl(struct file *filp,
        case KVM_GET_SUPPORTED_CPUID: {
                struct kvm_cpuid2 __user *cpuid_arg = argp;
                struct kvm_cpuid2 cpuid;
+               int retry = 0;
 
                r = -EFAULT;
                if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
                        goto out;
                r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
                                                      cpuid_arg->entries);
-               if (r)
+               if (r == -EAGAIN)
+                       retry = 1;
+               else if (r)
                        goto out;
 
                r = -EFAULT;

arch/x86/kvm/x86.c long kvm_arch_dev_ioctl(struct file *filp, · line 1084

Quoted 10 lines
arch/x86/kvm/x86.c +4 -1
                if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
                        goto out;
-               r = 0;
+               if (retry)
+                       r = -EAGAIN;
+               else
+                       r = 0;
                break;
        }
        default:

I can't really get the point of doing this: do you want to return -EAGAIN when there are multiple values to be read off a given cpuid function? If that's the case, I'd suggest adding the necessary intelligence to the userspace to poll for as much data as can be fetched rather than (ab)using EAGAIN.

arch/x86/kvm/x86.c static int · line 1335

Quoted 6 lines
arch/x86/kvm/x86.c +1 -0
@@ -1325,10 +1331,14 @@ static int
 kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
 {
        struct kvm_cpuid_entry2 *cpuid_entries;
        int limit, nent = 0, r = -E2BIG;
+       int sizer = 0;

What does 'sizer' mean?

Also, I've added support for handling function 0xd in userspace; can you add the handler for that in get_supported_cpuid()?

Amit

#5 lore raw
Quoted 3 lines · Amit Shah

Please add a Signed-Off-By and a git-style description for patches sent for inclusion.

Will do.

Quoted 8 lines

Also, it'll be better to reply to the patch if you include in the message instead of attaching them (git send-email).

arch/x86/include/asm/kvm_host.h line 90

Quoted 2 lines
arch/x86/include/asm/kvm_host.h +1 -1
-#define KVM_MAX_CPUID_ENTRIES 40
+#define KVM_MAX_CPUID_ENTRIES 100

Are we already hitting this limit? I don't think so.

It is not hitting the limit yet. Bumped it up for future use.

Quoted 29 lines
Quoted 29 lines
arch/x86/kvm/x86.c +8 -2
@@ -1060,19 +1060,25 @@ long kvm_arch_dev_ioctl(struct file *filp,
        case KVM_GET_SUPPORTED_CPUID: {
                struct kvm_cpuid2 __user *cpuid_arg = argp;
                struct kvm_cpuid2 cpuid;
+               int retry = 0;
 
                r = -EFAULT;
                if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
                        goto out;
                r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
                                                      cpuid_arg->entries);
-               if (r)
+               if (r == -EAGAIN)
+                       retry = 1;
+               else if (r)
                        goto out;
 
                r = -EFAULT;
                if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
                        goto out;
-               r = 0;
+               if (retry)
+                       r = -EAGAIN;
+               else
+                       r = 0;
                break;
Quoted 8 lines

arch/x86/kvm/x86.c long kvm_arch_dev_ioctl(struct file *filp, · line 1084

Quoted 2 lines
arch/x86/kvm/x86.c +0 -0
        }
        default:

I can't really get the point of doing this: do you want to return -EAGAIN when there are multiple values to be read off a given cpuid function? If that's the case, I'd suggest adding the necessary intelligence to the userspace to poll for as much data as can be fetched rather than (ab)using EAGAIN.

This is based on the discussions with Avi last time. Sizer is to get the size of the list of items. Avi wanted to keep the backward compatibility of the ioctl API.

Quoted 10 lines

arch/x86/kvm/x86.c static int · line 1335

Quoted 7 lines
arch/x86/kvm/x86.c +1 -0
@@ -1325,10 +1331,14 @@ static int
 kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
 {
        struct kvm_cpuid_entry2 *cpuid_entries;
        int limit, nent = 0, r = -E2BIG;
+       int sizer = 0;

What does 'sizer' mean?

Quoted 3 lines

Also, I've added support for handling function 0xd in userspace; can you add the handler for that in get_supported_cpuid()?

Currently KVM is limiting the basic cpuid limit to 0xb in the kernel code. That will need to be changed for leaf 0xd to go through. Doing that would be the next step.

Quoted 2 lines

Amit