Skip to contentSkip to conversation

Expose host cpuid to guest

RFC In review Dan Kenigsberg 4 participants 12 messages Nov 21, 2007 – Dec 25, 2007 7 messages not in the archive KVM

lore
Share & tools
lore
#0 lore raw

RFC 1/1 Expose host cpuid to guest

These patches expose host CPU features (that are known to work under KVM) to guests. It makes a couple of benchmarks run faster, and generally gives kvm's user better info on its host.

The kernel-space patch adds KVM_GET_SUPPORTED_CPUID ioctl to obtain the table of cpuid functions supported by the host. The user-space patch allows fine-tuning this table from the command-line.

I had to define struct kvm_cpuid2, KVM_SET_CPUID2 etc., because cpuid functions are a little more complex than just function-value pairs.

Dan.

commit e9775d0a16097cfb71779cb2fb985fb3e5040dc8

Author: Dan Kenigsberg <danken-atKUWr5tajBWk0Htik3J/[email protected]>
Date:   Sun Nov 18 13:55:26 2007 +0200

Support -cpu host option. Negotiate cpuid table with userspace.

drivers/kvm/x86.c +314 -8
index b42c9d4..5240e5f 100644
@@ -626,6 +626,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
 	case KVM_CAP_USER_MEMORY:
 	case KVM_CAP_SET_TSS_ADDR:
+	case KVM_CAP_EXT_CPUID:
 		r = 1;
 		break;
 	default:
@@ -688,13 +689,17 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 	kvm_put_guest_fpu(vcpu);
 }
 
+static int is_efer_nx(void) {
+	u64 efer;
+	rdmsrl(MSR_EFER, efer);
+	return efer & EFER_NX;
+}
+
 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
 {
-	u64 efer;
 	int i;
-	struct kvm_cpuid_entry *e, *entry;
+	struct kvm_cpuid_entry2 *e, *entry;
 
-	rdmsrl(MSR_EFER, efer);
 	entry = NULL;
 	for (i = 0; i < vcpu->cpuid_nent; ++i) {
 		e = &vcpu->cpuid_entries[i];
@@ -703,16 +708,56 @@ static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
 			break;
 		}
 	}
-	if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
+	if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
 		entry->edx &= ~(1 << 20);
 		printk(KERN_INFO "kvm: guest NX capability removed\n");
 	}
 }
 
+/* when an old userspace process fills a new kernel module */
 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 				    struct kvm_cpuid *cpuid,
 				    struct kvm_cpuid_entry __user *entries)
 {
+	int r, i;
+	struct kvm_cpuid_entry *cpuid_entries;
+
+	r = -E2BIG;
+	if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
+		goto out;
+	r = -ENOMEM;
+	cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
+	if (!cpuid_entries)
+		goto out;
+	r = -EFAULT;
+	if (copy_from_user(cpuid_entries, entries,
+			   cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+		goto out_free;
+	for (i = 0; i < cpuid->nent; i++) {
+		vcpu->cpuid_entries[i].function = cpuid_entries[i].function;
+		vcpu->cpuid_entries[i].eax = cpuid_entries[i].eax;
+		vcpu->cpuid_entries[i].ebx = cpuid_entries[i].ebx;
+		vcpu->cpuid_entries[i].ecx = cpuid_entries[i].ecx;
+		vcpu->cpuid_entries[i].edx = cpuid_entries[i].edx;
+		vcpu->cpuid_entries[i].index = 0;
+		vcpu->cpuid_entries[i].flags = 0;
+		vcpu->cpuid_entries[i].padding[0] = 0;
+		vcpu->cpuid_entries[i].padding[1] = 0;
+		vcpu->cpuid_entries[i].padding[2] = 0;
+	}
+	vcpu->cpuid_nent = cpuid->nent;
+	r = 0;
+
+out_free:
+	vfree(cpuid_entries);
+out:
+	return r;
+}
+
+static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
+				    struct kvm_cpuid2 *cpuid,
+				    struct kvm_cpuid_entry2 __user *entries)
+{
 	int r;
 
 	r = -E2BIG;
@@ -720,7 +765,7 @@ static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 		goto out;
 	r = -EFAULT;
 	if (copy_from_user(&vcpu->cpuid_entries, entries,
-			   cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+			   cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
 		goto out;
 	vcpu->cpuid_nent = cpuid->nent;
 	cpuid_fix_nx_cap(vcpu);
@@ -730,6 +775,192 @@ out:
 	return r;
 }
 
+static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
+				    struct kvm_cpuid2 *cpuid,
+				    struct kvm_cpuid_entry2 __user *entries)
+{
+	int r;
+
+	r = -E2BIG;
+	if (cpuid->nent < vcpu->cpuid_nent)
+		goto out;
+	r = -EFAULT;
+	if (copy_to_user(entries, &vcpu->cpuid_entries,
+			   vcpu->cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
+		goto out;
+	return 0;
+
+out:
+        cpuid->nent = vcpu->cpuid_nent;
+	return r;
+}
+
+static inline u32 bit(int bitno)
+{
+	return 1 << (bitno & 31);
+}
+
+static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
+			  u32 index)
+{
+	entry->function = function;
+	entry->index = index;
+	cpuid_count(entry->function, entry->index,
+		&entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
+	entry->flags = 0;
+}
+
+static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
+			 u32 index, int *nent, int maxnent)
+{
+	const __u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
+		bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
+		bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
+		bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
+		bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
+		bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
+		bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
+		bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
+		bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
+		bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
+	const __u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
+		bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
+		bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
+		bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
+		bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
+		bit(X86_FEATURE_PGE) |
+		bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
+		bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
+		bit(X86_FEATURE_SYSCALL) |
+		(bit(X86_FEATURE_NX) && is_efer_nx()) |
+#ifdef CONFIG_X86_64
+		bit(X86_FEATURE_LM) |
+#endif
+		/* TODO: make sure the following features are
+		 * safe for SVM guests */
+		bit(X86_FEATURE_MMXEXT) |
+		bit(X86_FEATURE_RDTSCP) | bit(X86_FEATURE_3DNOWEXT) |
+		bit(X86_FEATURE_3DNOW);
+	const __u32 kvm_supported_word3_x86_features =
+		bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
+	const __u32 kvm_supported_word6_x86_features =
+		bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
+
+	/* all func 2 cpuid_count() should be called on the same cpu */
+	if (function==2)
+		get_cpu();
+	do_cpuid_1_ent(entry, function, index);
+	++*nent;
+
+	switch (function) {
+	case 0:
+		entry->eax = min(entry->eax, (u32)0xb);
+		break;
+	case 1:
+		entry->edx &= kvm_supported_word0_x86_features;
+		entry->ecx &= kvm_supported_word3_x86_features;
+		break;
+	/* function 2 entries are STATEFUL. That is, repeated cpuid commands
+	 * may return different values. This forces us to get_cpu() before
+	 * issuing the first command, and also to emulate this annoying behavior
+	 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
+	case 2: {
+			int t, times = entry->eax & 0xff;
+
+			entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
+			for (t = 1; t < times && *nent < maxnent; ++t) {
+				do_cpuid_1_ent(&entry[t], function, 0);
+				entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
+				++*nent;
+			}
+			break;
+		}
+	/* function 4 and 0xb have additional index. */
+	case 4: {
+			int index, cache_type;
+
+			entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+			/* read more entries until cache_type is zero */
+			for (index = 1; *nent < maxnent; ++index) {
+				cache_type = entry[index - 1].eax & 0x1f;
+				if (!cache_type)
+					break;
+				do_cpuid_1_ent(&entry[index], function, index);
+				entry[index].flags |=
+				       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+				++*nent;
+			}
+			break;
+		}
+	case 0xb: {
+			int index, level_type;
+
+			entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+			/* read more entries until level_type is zero */
+			for (index = 1; *nent < maxnent; ++index) {
+				level_type = entry[index - 1].ecx & 0xff;
+				if (!level_type)
+					break;
+				do_cpuid_1_ent(&entry[index], function, index);
+				entry[index].flags |=
+				       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+				++*nent;
+			}
+			break;
+		}
+	case 0x80000000:
+		entry->eax = min(entry->eax, 0x8000001a);
+                break;
+	case 0x80000001:
+		entry->edx &= kvm_supported_word1_x86_features;
+		entry->ecx &= kvm_supported_word6_x86_features;
+		break;
+	}
+	if (function==2)
+		put_cpu();
+}
+
+static int kvm_vm_ioctl_get_supported_cpuid(struct kvm *kvm,
+				    struct kvm_cpuid2 *cpuid,
+				    struct kvm_cpuid_entry2 __user *entries)
+{
+	struct kvm_cpuid_entry2 *cpuid_entries;
+	int limit, nent = 0, r = -E2BIG;
+        u32 func;
+
+	if (cpuid->nent < 1)
+		goto out;
+	r = -ENOMEM;
+	cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
+	if (!cpuid_entries)
+		goto out;
+
+	do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
+	limit = cpuid_entries[0].eax;
+	for (func = 1; func <= limit && nent < cpuid->nent; ++func)
+		do_cpuid_ent(&cpuid_entries[nent], func, 0,
+				&nent, cpuid->nent);
+
+	if (nent >= cpuid->nent)
+		goto out_free;
+	do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
+	limit = cpuid_entries[nent - 1].eax;
+	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;
+
+out_free:
+	vfree(cpuid_entries);
+out:
+	return r;
+}
+
 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
 				    struct kvm_lapic_state *s)
 {
@@ -796,6 +1027,36 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 			goto out;
 		break;
 	}
+	case KVM_SET_CPUID2: {
+		struct kvm_cpuid2 __user *cpuid_arg = argp;
+		struct kvm_cpuid2 cpuid;
+
+		r = -EFAULT;
+		if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
+			goto out;
+		r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
+				cpuid_arg->entries);
+		if (r)
+			goto out;
+		break;
+	}
+	case KVM_GET_CPUID2: {
+		struct kvm_cpuid2 __user *cpuid_arg = argp;
+		struct kvm_cpuid2 cpuid;
+
+		r = -EFAULT;
+		if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
+			goto out;
+		r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
+				cpuid_arg->entries);
+		if (r)
+			goto out;
+		r = -EFAULT;
+		if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
+			goto out;
+		r = 0;
+		break;
+	}
 	case KVM_GET_MSRS:
 		r = msr_io(vcpu, argp, kvm_get_msr, 1);
 		break;
@@ -1091,6 +1352,24 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		r = 0;
 		break;
 	}
+	case KVM_GET_SUPPORTED_CPUID: {
+		struct kvm_cpuid2 __user *cpuid_arg = argp;
+		struct kvm_cpuid2 cpuid;
+
+		r = -EFAULT;
+		if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
+			goto out;
+		r = kvm_vm_ioctl_get_supported_cpuid(kvm, &cpuid,
+			cpuid_arg->entries);
+		if (r)
+			goto out;
+
+		r = -EFAULT;
+		if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
+			goto out;
+		r = 0;
+		break;
+	}
 	default:
 		;
 	}
@@ -1887,14 +2166,33 @@ void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
 	}
 }
 
+static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
+{
+	struct kvm_cpuid_entry2 *e = &vcpu->cpuid_entries[i];
+	int j, nent = vcpu->cpuid_nent;
+
+	e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
+	/* when no next entry is found, the current entry[i] is reselected */
+	for (j = i + 1; j == i; j = (j + 1) % nent) {
+		struct kvm_cpuid_entry2 *ej =
+			&vcpu->cpuid_entries[j];
+		if (ej->function == e->function) {
+			ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
+			return j;
+		}
+	}
+	return 0; /* silence gcc, even though control never reaches here */
+}
+
 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
 {
 	int i;
-	u32 function;
-	struct kvm_cpuid_entry *e, *best;
+	u32 function, index;
+	struct kvm_cpuid_entry2 *e, *best;
 
 	kvm_x86_ops->cache_regs(vcpu);
 	function = vcpu->regs[VCPU_REGS_RAX];
+	index = vcpu->regs[VCPU_REGS_RCX];
 	vcpu->regs[VCPU_REGS_RAX] = 0;
 	vcpu->regs[VCPU_REGS_RBX] = 0;
 	vcpu->regs[VCPU_REGS_RCX] = 0;
@@ -1902,7 +2200,15 @@ void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
 	best = NULL;
 	for (i = 0; i < vcpu->cpuid_nent; ++i) {
 		e = &vcpu->cpuid_entries[i];
-		if (e->function == function) {
+		/* find an entry with matching function, matching index (if
+		 * needed), and that should be read next (if it's stateful) */
+		if (e->function == function &&
+		    (!(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX)
+		      || e->index == index) &&
+		    (!(e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
+		      || (e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT) )) {
+			if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
+				move_to_next_stateful_cpuid_entry(vcpu, i);
 			best = e;
 			break;
 		}
drivers/kvm/x86.h +1 -1
index b1528c9..78ab1e1 100644
@@ -149,7 +149,7 @@ struct kvm_vcpu {
 	int halt_request; /* real mode on Intel only */
 
 	int cpuid_nent;
-	struct kvm_cpuid_entry cpuid_entries[KVM_MAX_CPUID_ENTRIES];
+	struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
 
 	/* emulate context */
 
include/asm-x86/kvm.h +21 -0
index 4837d75..17afa81 100644
@@ -151,5 +151,26 @@ struct kvm_cpuid {
 	struct kvm_cpuid_entry entries[0];
 };
 
+struct kvm_cpuid_entry2 {
+	__u32 function;
+	__u32 index;
+	__u32 flags;
+	__u32 eax;
+	__u32 ebx;
+	__u32 ecx;
+	__u32 edx;
+	__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
+
+/* for KVM_SET_CPUID2 */
+struct kvm_cpuid2 {
+	__u32 nent;
+	__u32 padding;
+	struct kvm_cpuid_entry2 entries[0];
+};
 
 #endif
include/linux/kvm.h +4 -0
index fd4f900..b751552 100644
@@ -224,6 +224,7 @@ struct kvm_signal_mask {
 #define KVM_CAP_MMU_SHADOW_CACHE_CONTROL 2
 #define KVM_CAP_USER_MEMORY 3
 #define KVM_CAP_SET_TSS_ADDR 4
+#define KVM_CAP_EXT_CPUID 5
 
 /*
  * ioctls for VM fds
@@ -241,6 +242,7 @@ struct kvm_signal_mask {
 #define KVM_CREATE_VCPU           _IO(KVMIO,  0x41)
 #define KVM_GET_DIRTY_LOG         _IOW(KVMIO, 0x42, struct kvm_dirty_log)
 #define KVM_SET_MEMORY_ALIAS      _IOW(KVMIO, 0x43, struct kvm_memory_alias)
+#define KVM_GET_SUPPORTED_CPUID   _IOWR(KVMIO, 0x48, struct kvm_cpuid2)
 /* Device model IOC */
 #define KVM_CREATE_IRQCHIP	  _IO(KVMIO,  0x60)
 #define KVM_IRQ_LINE		  _IOW(KVMIO, 0x61, struct kvm_irq_level)
@@ -266,5 +268,7 @@ struct kvm_signal_mask {
 #define KVM_SET_FPU               _IOW(KVMIO,  0x8d, struct kvm_fpu)
 #define KVM_GET_LAPIC             _IOR(KVMIO,  0x8e, struct kvm_lapic_state)
 #define KVM_SET_LAPIC             _IOW(KVMIO,  0x8f, struct kvm_lapic_state)
+#define KVM_SET_CPUID2            _IOW(KVMIO,  0x90, struct kvm_cpuid2)
+#define KVM_GET_CPUID2            _IOWR(KVMIO, 0x91, struct kvm_cpuid2)
 
 #endif

commit e5a590065ab37fb5eb0481e176d455fba98310dd

Author: Dan Kenigsberg <danken-atKUWr5tajBWk0Htik3J/[email protected]>
Date:   Sun Nov 18 13:36:41 2007 +0200

Add -cpu host option. Negotiate cpuid table with kernel.

kernel/external-module-compat.h +3 -11
index e3f81fe..ef5d70b 100644
@@ -530,14 +530,6 @@ out:
 #define dest_ExtINT       7
 #endif
 
-/* empty_zero_page isn't exported in all kernels */
-#include <asm/pgtable.h>
-
-#define empty_zero_page kvm_empty_zero_page
-
-static char empty_zero_page[PAGE_SIZE];
-
-static inline void blahblah(void)
-{
-	(void)empty_zero_page[0];
-}
+#ifndef X86_FEATURE_NX
+#define X86_FEATURE_NX (1*32+20)
+#endif
libkvm/libkvm-x86.c +51 -3

69 lines not on this page · raw message

libkvm/libkvm.h +30 -1
index 2094dfd..d23b52b 100644
@@ -360,7 +360,36 @@ int kvm_guest_debug(kvm_context_t, int vcpu, struct kvm_debug_guest *dbg);
  * \return 0 on success, or -errno on error
  */
 int kvm_setup_cpuid(kvm_context_t kvm, int vcpu, int nent,
-		    struct kvm_cpuid_entry *entries);
+		    struct kvm_cpuid_entry2 *entries);
+
+/*!
+ * \brief Setup a vcpu's cpuid instruction emulation
+ *
+ * Get the currently-held table of cpuid functions and their states.\n
+ *
+ * \param kvm Pointer to the current kvm_context
+ * \param vcpu Which virtual CPU should be initialized
+ * \param nent Pointer to the number of entries to be obtained.
+ *             On return, the number that was actually obtained.
+ * \param entries cpuid function entries table
+ * \return 0 on success, or -errno on error
+ */
+int kvm_get_cpuid(kvm_context_t kvm, int vcpu, int *nent,
+		    struct kvm_cpuid_entry2 *entries);
+
+/*!
+ * \brief Obtain cpuid supported by kvm
+ *
+ * Obtain a table of cpuid function supported by kvm.\n
+ *
+ * \param kvm Pointer to the current kvm_context
+ * \param nent Pointer to the number of entries to be obtained.
+ *             On return, the number that was actually obtained.
+ * \param entries cpuid function entries table
+ * \return 0 on success, or -errno on error
+ */
+int kvm_get_supported_cpuid(kvm_context_t kvm, int *nent,
+	struct kvm_cpuid_entry2 *entries);
 
 /*!
  * \brief Set a vcpu's signal mask for guest mode
qemu/hw/pc.c +10 -3
index 6c71b09..588c3ba 100644
@@ -683,7 +683,7 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size, int boot_device,
                      DisplayState *ds, const char **fd_filename, int snapshot,
                      const char *kernel_filename, const char *kernel_cmdline,
                      const char *initrd_filename,
-                     int pci_enabled)
+                     int pci_enabled, const char *cpu_model)
 {
     char buf[1024];
     int ret, linux_boot, i;
@@ -705,6 +705,13 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size, int boot_device,
     linux_boot = (kernel_filename != NULL);
 
     /* init CPUs */
+    if (cpu_model == NULL)
+        cpu_model = "basic";
+
+    if (x86_find_cpu_by_name(cpu_model)) {
+        fprintf(stderr, "Unable to find x86 CPU definition\n");
+        exit(1);
+    }
     for(i = 0; i < smp_cpus; i++) {
         env = cpu_init();
         if (i != 0)
@@ -1038,7 +1045,7 @@ static void pc_init_pci(ram_addr_t ram_size, int vga_ram_size, int boot_device,
     pc_init1(ram_size, vga_ram_size, boot_device,
              ds, fd_filename, snapshot,
              kernel_filename, kernel_cmdline,
-             initrd_filename, 1);
+             initrd_filename, 1, cpu_model);
 }
 
 static void pc_init_isa(ram_addr_t ram_size, int vga_ram_size, int boot_device,
@@ -1052,7 +1059,7 @@ static void pc_init_isa(ram_addr_t ram_size, int vga_ram_size, int boot_device,
     pc_init1(ram_size, vga_ram_size, boot_device,
              ds, fd_filename, snapshot,
              kernel_filename, kernel_cmdline,
-             initrd_filename, 0);
+             initrd_filename, 0, cpu_model);
 }
 
 QEMUMachine pc_machine = {
qemu/qemu-kvm.c +80 -17

124 lines not on this page · raw message

qemu/target-i386/cpu.h +45 -1

80 lines not on this page · raw message

qemu/target-i386/helper2.c +305 -65

394 lines not on this page · raw message

qemu/vl.c +2 -0
index 70d8c81..70558dc 100644
@@ -8087,6 +8087,8 @@ int main(int argc, char **argv)
                     mips_cpu_list(stdout, &fprintf);
 #elif defined(TARGET_SPARC)
                     sparc_cpu_list(stdout, &fprintf);
+#elif defined(TARGET_I386)
+                    x86_cpu_list(stdout, &fprintf);
 #endif
                     exit(0);
                 } else {

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel

A message that is not in the archive (sent off-list or not yet received): 20071121110631.GA14292-iWbx9bcAnq+Hk9JtIoIkgNBPR1lH4CV8@public.gmane.org

#2 lore raw
Quoted 4 lines · Dan Kenigsberg

These patches expose host CPU features (that are known to work under KVM) to guests. It makes a couple of benchmarks run faster, and generally gives kvm's user better info on its host.

Quoted 8 lines

The kernel-space patch adds KVM_GET_SUPPORTED_CPUID ioctl to obtain the table of cpuid functions supported by the host. The user-space patch allows fine-tuning this table from the command-line.

I had to define struct kvm_cpuid2, KVM_SET_CPUID2 etc., because cpuid functions are a little more complex than just function-value pairs.

Dan.

Hey Dan, were those amd-specific things sorted out?

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#3 lore raw
In reply to a message not in the archive
Quoted 8 lines · Dan Kenigsberg

These patches expose host CPU features (that are known to work under KVM) to guests. It makes a couple of benchmarks run faster, and generally gives kvm's user better info on its host.

The kernel-space patch adds KVM_GET_SUPPORTED_CPUID ioctl to obtain the table of cpuid functions supported by the host. The user-space patch allows fine-tuning this table from the command-line.

Quoted 9 lines

I had to define struct kvm_cpuid2, KVM_SET_CPUID2 etc., because cpuid functions are a little more complex than just function-value pairs.

commit e9775d0a16097cfb71779cb2fb985fb3e5040dc8

Author: Dan Kenigsberg <danken-atKUWr5tajBWk0Htik3J/[email protected]>
Date:   Sun Nov 18 13:55:26 2007 +0200

Support -cpu host option. Negotiate cpuid table with userspace.

The kernel doesn't have a -cpu option. The description needs to be more descriptive (motivation, special cases in cpuid).

drivers/kvm/x86.c void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) · line 693

Quoted 5 lines
drivers/kvm/x86.c +2 -0
+static int is_efer_nx(void) {
+	u64 efer;

blank line

Quoted 15 lines
drivers/kvm/x86.c +11 -0
+	rdmsrl(MSR_EFER, efer);
+	return efer & EFER_NX;
+}
+
 
 
 
+static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
+				    struct kvm_cpuid2 *cpuid,
+				    struct kvm_cpuid_entry2 __user *entries)
+{
+	int r;
+
+	r = -E2BIG;

drivers/kvm/x86.c out: · line 794

Quoted 10 lines
drivers/kvm/x86.c +10 -0
+	if (cpuid->nent < vcpu->cpuid_nent)
+		goto out;
+	r = -EFAULT;
+	if (copy_to_user(entries, &vcpu->cpuid_entries,
+			   vcpu->cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
+		goto out;
+	return 0;
+
+out:
+        cpuid->nent = vcpu->cpuid_nent;

whitespace damage here

Quoted 6 lines
drivers/kvm/x86.c +5 -0
+	return r;
+
+static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
+			 u32 index, int *nent, int maxnent)
+{

drivers/kvm/x86.c out: · line 825

Quoted 10 lines
drivers/kvm/x86.c +10 -0
+	const __u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
+		bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
+		bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
+		bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
+		bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
+		bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
+		bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
+		bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
+		bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
+		bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);

u32, not __u32.

Quoted 9 lines
drivers/kvm/x86.c +8 -0
+	const __u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
+		bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
+		bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
+		bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
+		bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
+		bit(X86_FEATURE_PGE) |
+		bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
+		bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |

drivers/kvm/x86.c out: · line 843

Quoted 10 lines
drivers/kvm/x86.c +10 -0
+		bit(X86_FEATURE_SYSCALL) |
+		(bit(X86_FEATURE_NX) && is_efer_nx()) |
+#ifdef CONFIG_X86_64
+		bit(X86_FEATURE_LM) |
+#endif
+		/* TODO: make sure the following features are
+		 * safe for SVM guests */
+		bit(X86_FEATURE_MMXEXT) |
+		bit(X86_FEATURE_RDTSCP) | bit(X86_FEATURE_3DNOWEXT) |
+		bit(X86_FEATURE_3DNOW);

rdtscp isn't, I believe.

drivers/kvm/x86.c out: · line 851

Quoted 9 lines
drivers/kvm/x86.c +8 -0
+	const __u32 kvm_supported_word3_x86_features =
+		bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
+	const __u32 kvm_supported_word6_x86_features =
+		bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
+
+	/* all func 2 cpuid_count() should be called on the same cpu */
+	if (function==2)
+		get_cpu();

Avoid the special case, just to it unconditionally.

Quoted 8 lines
drivers/kvm/x86.c +7 -0
+	do_cpuid_1_ent(entry, function, index);
+	++*nent;
+
+	switch (function) {
+	case 0:
+		entry->eax = min(entry->eax, (u32)0xb);
+		break;

drivers/kvm/x86.c out: · line 868

Quoted 10 lines
drivers/kvm/x86.c +10 -0
+	case 1:
+		entry->edx &= kvm_supported_word0_x86_features;
+		entry->ecx &= kvm_supported_word3_x86_features;
+		break;
+	/* function 2 entries are STATEFUL. That is, repeated cpuid commands
+	 * may return different values. This forces us to get_cpu() before
+	 * issuing the first command, and also to emulate this annoying behavior
+	 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
+	case 2: {
+			int t, times = entry->eax & 0xff;

Indent this normally relative to other entries.

Quoted 51 lines
drivers/kvm/x86.c +51 -0
+
+			entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
+			for (t = 1; t < times && *nent < maxnent; ++t) {
+				do_cpuid_1_ent(&entry[t], function, 0);
+				entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
+				++*nent;
+			}
+			break;
+		}
+	/* function 4 and 0xb have additional index. */
+	case 4: {
+			int index, cache_type;
+
+			entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+			/* read more entries until cache_type is zero */
+			for (index = 1; *nent < maxnent; ++index) {
+				cache_type = entry[index - 1].eax & 0x1f;
+				if (!cache_type)
+					break;
+				do_cpuid_1_ent(&entry[index], function, index);
+				entry[index].flags |=
+				       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+				++*nent;
+			}
+			break;
+		}
+	case 0xb: {
+			int index, level_type;
+
+			entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+			/* read more entries until level_type is zero */
+			for (index = 1; *nent < maxnent; ++index) {
+				level_type = entry[index - 1].ecx & 0xff;
+				if (!level_type)
+					break;
+				do_cpuid_1_ent(&entry[index], function, index);
+				entry[index].flags |=
+				       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+				++*nent;
+			}
+			break;
+		}
+	case 0x80000000:
+		entry->eax = min(entry->eax, 0x8000001a);
+                break;
+	case 0x80000001:
+		entry->edx &= kvm_supported_word1_x86_features;
+		entry->ecx &= kvm_supported_word6_x86_features;
+		break;
+	}
+	if (function==2)

drivers/kvm/x86.c out:

Quoted 10 lines
drivers/kvm/x86.c +10 -0
+		put_cpu();
+}
+
+static int kvm_vm_ioctl_get_supported_cpuid(struct kvm *kvm,
+				    struct kvm_cpuid2 *cpuid,
+				    struct kvm_cpuid_entry2 __user *entries)
+{
+	struct kvm_cpuid_entry2 *cpuid_entries;
+	int limit, nent = 0, r = -E2BIG;
+        u32 func

whitespace damage

Quoted 8 lines

;

drivers/kvm/x86.c +6 -0
+
+	if (cpuid->nent < 1)
+		goto out;
+	r = -ENOMEM;
+	cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
+	if (!cpuid_entries)

drivers/kvm/x86.c out: · line 945

Quoted 10 lines
drivers/kvm/x86.c +10 -0
+		goto out;
+
+	do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
+	limit = cpuid_entries[0].eax;
+	for (func = 1; func <= limit && nent < cpuid->nent; ++func)
+		do_cpuid_ent(&cpuid_entries[nent], func, 0,
+				&nent, cpuid->nent);
+
+	if (nent >= cpuid->nent)
+		goto out_free;

Should exit with an E2BIG here. ENOMEM means kernel's out of memory, which isn't the case here.

Quoted 88 lines
drivers/kvm/x86.c +67 -0
+	do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
+	limit = cpuid_entries[nent - 1].eax;
+	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;
+
+out_free:
+	vfree(cpuid_entries);
+out:
+	return r;
+}
+
 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
 				    struct kvm_lapic_state *s)
 {
@@ -796,6 +1027,36 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 			goto out;
 		break;
 	}
+	case KVM_SET_CPUID2: {
+		struct kvm_cpuid2 __user *cpuid_arg = argp;
+		struct kvm_cpuid2 cpuid;
+
+		r = -EFAULT;
+		if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
+			goto out;
+		r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
+				cpuid_arg->entries);
+		if (r)
+			goto out;
+		break;
+	}
+	case KVM_GET_CPUID2: {
+		struct kvm_cpuid2 __user *cpuid_arg = argp;
+		struct kvm_cpuid2 cpuid;
+
+		r = -EFAULT;
+		if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
+			goto out;
+		r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
+				cpuid_arg->entries);
+		if (r)
+			goto out;
+		r = -EFAULT;
+		if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
+			goto out;
+		r = 0;
+		break;
+	}
 	case KVM_GET_MSRS:
 		r = msr_io(vcpu, argp, kvm_get_msr, 1);
 		break;
@@ -1091,6 +1352,24 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		r = 0;
 		break;
 	}
+	case KVM_GET_SUPPORTED_CPUID: {
+		struct kvm_cpuid2 __user *cpuid_arg = argp;
+		struct kvm_cpuid2 cpuid;
+
+		r = -EFAULT;
+		if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
+			goto out;
+		r = kvm_vm_ioctl_get_supported_cpuid(kvm, &cpuid,
+			cpuid_arg->entries);
+		if (r)
+			goto out;
+
+		r = -EFAULT;
+		if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
+			goto out;
+		r = 0;
+		break;
+	}
 	default:
 		;
 	}
@@ -1887,14 +2166,33 @@ void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
 	}
 }
 
+static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)

drivers/kvm/x86.c void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val, · line 2179

Quoted 10 lines
drivers/kvm/x86.c +9 -0
+{
+	struct kvm_cpuid_entry2 *e = &vcpu->cpuid_entries[i];
+	int j, nent = vcpu->cpuid_nent;
+
+	e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
+	/* when no next entry is found, the current entry[i] is reselected */
+	for (j = i + 1; j == i; j = (j + 1) % nent) {
+		struct kvm_cpuid_entry2 *ej =
+			&vcpu->cpuid_entries[j];
 

80 column limit, not 40.

Quoted 28 lines
drivers/kvm/x86.c +11 -2
+		if (ej->function == e->function) {
+			ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
+			return j;
+		}
+	}
+	return 0; /* silence gcc, even though control never reaches here */
+}
+
 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
 {
 	int i;
-	u32 function;
-	struct kvm_cpuid_entry *e, *best;
+	u32 function, index;
+	struct kvm_cpuid_entry2 *e, *best;
 
 	kvm_x86_ops->cache_regs(vcpu);
 	function = vcpu->regs[VCPU_REGS_RAX];
+	index = vcpu->regs[VCPU_REGS_RCX];
 	vcpu->regs[VCPU_REGS_RAX] = 0;
 	vcpu->regs[VCPU_REGS_RBX] = 0;
drivers/kvm/x86.c +1 -1
 	vcpu->regs[VCPU_REGS_RCX] = 0;
@@ -1902,7 +2200,15 @@ void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
 	best = NULL;
 	for (i = 0; i < vcpu->cpuid_nent; ++i) {
 		e = &vcpu->cpuid_entries[i];
-		if (e->function == function) {
+		/* find an entry with matching function, matching index (if

drivers/kvm/x86.c void kvm_emulate_cpuid(struct kvm_vcpu *vcpu) · line 2213

Quoted 10 lines
drivers/kvm/x86.c +8 -0
+		 * needed), and that should be read next (if it's stateful) */
+		if (e->function == function &&
+		    (!(e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX)
+		      || e->index == index) &&
+		    (!(e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
+		      || (e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT) )) {
+			if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
+				move_to_next_stateful_cpuid_entry(vcpu, i);
 			best = e;
 

Please move this to a function with multiple if ()s and return statements.

Quoted 20 lines
(quoted diff) +0 -1
------------------------------------------------------------------------

commit e5a590065ab37fb5eb0481e176d455fba98310dd

Author: Dan Kenigsberg <danken-atKUWr5tajBWk0Htik3J/[email protected]>
Date:   Sun Nov 18 13:36:41 2007 +0200

Add -cpu host option. Negotiate cpuid table with kernel.

kernel/external-module-compat.h +0 -1
index e3f81fe..ef5d70b 100644
@@ -530,14 +530,6 @@ out:
 #define dest_ExtINT       7
 #endif
 
-/* empty_zero_page isn't exported in all kernels */

kernel/external-module-compat.h out: · line 543

Quoted 10 lines
kernel/external-module-compat.h +0 -10
-#include <asm/pgtable.h>
-
-#define empty_zero_page kvm_empty_zero_page
-
-static char empty_zero_page[PAGE_SIZE];
-
-static inline void blahblah(void)
-{
-	(void)empty_zero_page[0];
-}

Remove this removal.

kernel/external-module-compat.h out:

Quoted 7 lines
kernel/external-module-compat.h +5 -0
+int kvm_get_supported_cpuid(kvm_context_t kvm, int *nent,
+		struct kvm_cpuid_entry2 *entries)
+{
+	struct kvm_cpuid2 *cpuid;
+	int r = -1;

blank line.

kernel/external-module-compat.h

Quoted 5 lines
kernel/external-module-compat.h +4 -0
+#ifdef KVM_CAP_EXT_CPUID
+	r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_EXT_CPUID);
+	if (!r)
+		return -1;

return -errno on errors

kernel/external-module-compat.h

Quoted 9 lines
kernel/external-module-compat.h +8 -0
+
+	cpuid = malloc(sizeof(*cpuid) + *nent * sizeof(*entries));
+	if (!cpuid)
+		return -ENOMEM;
+	cpuid->nent = *nent;
+
+	r = ioctl(kvm->vm_fd, KVM_GET_SUPPORTED_CPUID, cpuid);
+

error check

kernel/external-module-compat.h

Quoted 13 lines
kernel/external-module-compat.h +12 -0
+	memcpy(entries, cpuid->entries, *nent * sizeof(*entries));
+	*nent = cpuid->nent;
+	free(cpuid);
+#endif
+	return r;
+}
+
+int kvm_get_cpuid(kvm_context_t kvm, int vcpu, int *nent,
+		    struct kvm_cpuid_entry2 *entries)
+{
+	struct kvm_cpuid2 *cpuid;
+	int r = -1;

blank line

kernel/external-module-compat.h

Quoted 5 lines
kernel/external-module-compat.h +4 -0
+#ifdef KVM_CAP_EXT_CPUID
+	r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_EXT_CPUID);
+	if (!r)
+		return -1;

-errno

kernel/external-module-compat.h

Quoted 9 lines
kernel/external-module-compat.h +8 -0
+
+	cpuid = malloc(sizeof(*cpuid) + *nent * sizeof(*entries));
+	if (!cpuid)
+		return -ENOMEM;
+	cpuid->nent = *nent;
+
+	r = ioctl(kvm->vcpu_fd[vcpu], KVM_GET_CPUID2, cpuid);
+

error check

Quoted 8 lines
kernel/external-module-compat.h +7 -0
+	memcpy(entries, cpuid->entries, *nent * sizeof(*entries));
+	*nent = cpuid->nent;
+	free(cpuid);
+#endif
+	return r;
+}
+
Signature · 7 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

A message that is not in the archive (sent off-list or not yet received): 47441BEF.4070207-atKUWr5tajBWk0Htik3J/[email protected]

#5 lore raw
Quoted 9 lines · Avi Kivity
Quoted 8 lines · Dan Kenigsberg

These patches expose host CPU features (that are known to work under KVM) to guests. It makes a couple of benchmarks run faster, and generally gives kvm's user better info on its host.

The kernel-space patch adds KVM_GET_SUPPORTED_CPUID ioctl to obtain the table of cpuid functions supported by the host. The user-space patch allows fine-tuning this table from the command-line.

Quoted 7 lines
Quoted 7 lines

I had to define struct kvm_cpuid2, KVM_SET_CPUID2 etc., because cpuid functions are a little more complex than just function-value pairs.
commit e9775d0a16097cfb71779cb2fb985fb3e5040dc8

Author: Dan Kenigsberg <danken-atKUWr5tajBWk0Htik3J/[email protected]>
Date:   Sun Nov 18 13:55:26 2007 +0200

Support -cpu host option. Negotiate cpuid table with userspace.

I believe the attached two patches apply all your comments.

Dan.

commit 53c12a2602642ed01488ae382d23d350e5464a71

Author: Dan Kenigsberg <danken-atKUWr5tajBWk0Htik3J/[email protected]>
Date:   Wed Nov 21 16:03:55 2007 +0200

Expose host CPU features to guests. a new KVM_GET_SUPPORTED_CPUID ioctl is used to obtain the table of cpuid functions supported by the host & kvm. The user-space may fine-tune this table and use the KVM_SET_CPUID2 ioctl to set the table seen by guests.

This patch exposes CPU features that are hitherto hidden (e.g. SSE, cache structure) and makes a couple of benchmarks faster.

The table of cpuid entries and kvm_emulate_cpuid() had to be changed because for a couple of cpuid functions, edx - and not only eax - is significant. Worse, function 2 is stateful; its value may change between consequtive calls.

drivers/kvm/x86.c +320 -8
index b42c9d4..c14967a 100644
@@ -626,6 +626,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
 	case KVM_CAP_USER_MEMORY:
 	case KVM_CAP_SET_TSS_ADDR:
+	case KVM_CAP_EXT_CPUID:
 		r = 1;
 		break;
 	default:
@@ -688,13 +689,18 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 	kvm_put_guest_fpu(vcpu);
 }
 
+static int is_efer_nx(void) {
+	u64 efer;
+
+	rdmsrl(MSR_EFER, efer);
+	return efer & EFER_NX;
+}
+
 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
 {
-	u64 efer;
 	int i;
-	struct kvm_cpuid_entry *e, *entry;
+	struct kvm_cpuid_entry2 *e, *entry;
 
-	rdmsrl(MSR_EFER, efer);
 	entry = NULL;
 	for (i = 0; i < vcpu->cpuid_nent; ++i) {
 		e = &vcpu->cpuid_entries[i];
@@ -703,16 +709,56 @@ static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
 			break;
 		}
 	}
-	if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
+	if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
 		entry->edx &= ~(1 << 20);
 		printk(KERN_INFO "kvm: guest NX capability removed\n");
 	}
 }
 
+/* when an old userspace process fills a new kernel module */
 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 				    struct kvm_cpuid *cpuid,
 				    struct kvm_cpuid_entry __user *entries)
 {
+	int r, i;
+	struct kvm_cpuid_entry *cpuid_entries;
+
+	r = -E2BIG;
+	if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
+		goto out;
+	r = -ENOMEM;
+	cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
+	if (!cpuid_entries)
+		goto out;
+	r = -EFAULT;
+	if (copy_from_user(cpuid_entries, entries,
+			   cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+		goto out_free;
+	for (i = 0; i < cpuid->nent; i++) {
+		vcpu->cpuid_entries[i].function = cpuid_entries[i].function;
+		vcpu->cpuid_entries[i].eax = cpuid_entries[i].eax;
+		vcpu->cpuid_entries[i].ebx = cpuid_entries[i].ebx;
+		vcpu->cpuid_entries[i].ecx = cpuid_entries[i].ecx;
+		vcpu->cpuid_entries[i].edx = cpuid_entries[i].edx;
+		vcpu->cpuid_entries[i].index = 0;
+		vcpu->cpuid_entries[i].flags = 0;
+		vcpu->cpuid_entries[i].padding[0] = 0;
+		vcpu->cpuid_entries[i].padding[1] = 0;
+		vcpu->cpuid_entries[i].padding[2] = 0;
+	}
+	vcpu->cpuid_nent = cpuid->nent;
+	r = 0;
+
+out_free:
+	vfree(cpuid_entries);
+out:
+	return r;
+}
+
+static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
+				    struct kvm_cpuid2 *cpuid,
+				    struct kvm_cpuid_entry2 __user *entries)
+{
 	int r;
 
 	r = -E2BIG;
@@ -720,7 +766,7 @@ static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 		goto out;
 	r = -EFAULT;
 	if (copy_from_user(&vcpu->cpuid_entries, entries,
-			   cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+			   cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
 		goto out;
 	vcpu->cpuid_nent = cpuid->nent;
 	cpuid_fix_nx_cap(vcpu);
@@ -730,6 +776,189 @@ out:
 	return r;
 }
 
+static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
+				    struct kvm_cpuid2 *cpuid,
+				    struct kvm_cpuid_entry2 __user *entries)
+{
+	int r;
+
+	r = -E2BIG;
+	if (cpuid->nent < vcpu->cpuid_nent)
+		goto out;
+	r = -EFAULT;
+	if (copy_to_user(entries, &vcpu->cpuid_entries,
+			   vcpu->cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
+		goto out;
+	return 0;
+
+out:
+	cpuid->nent = vcpu->cpuid_nent;
+	return r;
+}
+
+static inline u32 bit(int bitno)
+{
+	return 1 << (bitno & 31);
+}
+
+static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
+			  u32 index)
+{
+	entry->function = function;
+	entry->index = index;
+	cpuid_count(entry->function, entry->index,
+		&entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
+	entry->flags = 0;
+}
+
+static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
+			 u32 index, int *nent, int maxnent)
+{
+	const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
+		bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
+		bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
+		bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
+		bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
+		bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
+		bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
+		bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
+		bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
+		bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
+	const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
+		bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
+		bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
+		bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
+		bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
+		bit(X86_FEATURE_PGE) |
+		bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
+		bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
+		bit(X86_FEATURE_SYSCALL) |
+		(bit(X86_FEATURE_NX) && is_efer_nx()) |
+#ifdef CONFIG_X86_64
+		bit(X86_FEATURE_LM) |
+#endif
+		bit(X86_FEATURE_MMXEXT) |
+		bit(X86_FEATURE_3DNOWEXT) |
+		bit(X86_FEATURE_3DNOW);
+	const u32 kvm_supported_word3_x86_features =
+		bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
+	const u32 kvm_supported_word6_x86_features =
+		bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
+
+	/* all func 2 cpuid_count() should be called on the same cpu */
+	get_cpu();
+	do_cpuid_1_ent(entry, function, index);
+	++*nent;
+
+	switch (function) {
+	case 0:
+		entry->eax = min(entry->eax, (u32)0xb);
+		break;
+	case 1:
+		entry->edx &= kvm_supported_word0_x86_features;
+		entry->ecx &= kvm_supported_word3_x86_features;
+		break;
+	/* function 2 entries are STATEFUL. That is, repeated cpuid commands
+	 * may return different values. This forces us to get_cpu() before
+	 * issuing the first command, and also to emulate this annoying behavior
+	 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
+	case 2: {
+		int t, times = entry->eax & 0xff;
+
+		entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
+		for (t = 1; t < times && *nent < maxnent; ++t) {
+			do_cpuid_1_ent(&entry[t], function, 0);
+			entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
+			++*nent;
+		}
+		break;
+	}
+	/* function 4 and 0xb have additional index. */
+	case 4: {
+		int index, cache_type;
+
+		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+		/* read more entries until cache_type is zero */
+		for (index = 1; *nent < maxnent; ++index) {
+			cache_type = entry[index - 1].eax & 0x1f;
+			if (!cache_type)
+				break;
+			do_cpuid_1_ent(&entry[index], function, index);
+			entry[index].flags |=
+			       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+			++*nent;
+		}
+		break;
+	}
+	case 0xb: {
+		int index, level_type;
+
+		entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+		/* read more entries until level_type is zero */
+		for (index = 1; *nent < maxnent; ++index) {
+			level_type = entry[index - 1].ecx & 0xff;
+			if (!level_type)
+				break;
+			do_cpuid_1_ent(&entry[index], function, index);
+			entry[index].flags |=
+			       KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+			++*nent;
+		}
+		break;
+	}
+	case 0x80000000:
+		entry->eax = min(entry->eax, 0x8000001a);
+		break;
+	case 0x80000001:
+		entry->edx &= kvm_supported_word1_x86_features;
+		entry->ecx &= kvm_supported_word6_x86_features;
+		break;
+	}
+	put_cpu();
+}
+
+static int kvm_vm_ioctl_get_supported_cpuid(struct kvm *kvm,
+				    struct kvm_cpuid2 *cpuid,
+				    struct kvm_cpuid_entry2 __user *entries)
+{
+	struct kvm_cpuid_entry2 *cpuid_entries;
+	int limit, nent = 0, r = -E2BIG;
+	u32 func;
+
+	if (cpuid->nent < 1)
+		goto out;
+	r = -ENOMEM;
+	cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
+	if (!cpuid_entries)
+		goto out;
+
+	do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
+	limit = cpuid_entries[0].eax;
+	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;
+
+	do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
+	limit = cpuid_entries[nent - 1].eax;
+	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;
+
+out_free:
+	vfree(cpuid_entries);
+out:
+	return r;
+}
+
 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
 				    struct kvm_lapic_state *s)
 {
@@ -796,6 +1025,36 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 			goto out;
 		break;
 	}
+	case KVM_SET_CPUID2: {
+		struct kvm_cpuid2 __user *cpuid_arg = argp;
+		struct kvm_cpuid2 cpuid;
+
+		r = -EFAULT;
+		if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
+			goto out;
+		r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
+				cpuid_arg->entries);
+		if (r)
+			goto out;
+		break;
+	}
+	case KVM_GET_CPUID2: {
+		struct kvm_cpuid2 __user *cpuid_arg = argp;
+		struct kvm_cpuid2 cpuid;
+
+		r = -EFAULT;
+		if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
+			goto out;
+		r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
+				cpuid_arg->entries);
+		if (r)
+			goto out;
+		r = -EFAULT;
+		if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
+			goto out;
+		r = 0;
+		break;
+	}
 	case KVM_GET_MSRS:
 		r = msr_io(vcpu, argp, kvm_get_msr, 1);
 		break;
@@ -1091,6 +1350,24 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		r = 0;
 		break;
 	}
+	case KVM_GET_SUPPORTED_CPUID: {
+		struct kvm_cpuid2 __user *cpuid_arg = argp;
+		struct kvm_cpuid2 cpuid;
+
+		r = -EFAULT;
+		if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
+			goto out;
+		r = kvm_vm_ioctl_get_supported_cpuid(kvm, &cpuid,
+			cpuid_arg->entries);
+		if (r)
+			goto out;
+
+		r = -EFAULT;
+		if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
+			goto out;
+		r = 0;
+		break;
+	}
 	default:
 		;
 	}
@@ -1887,14 +2164,47 @@ void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
 	}
 }
 
+static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
+{
+	struct kvm_cpuid_entry2 *e = &vcpu->cpuid_entries[i];
+	int j, nent = vcpu->cpuid_nent;
+
+	e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
+	/* when no next entry is found, the current entry[i] is reselected */
+	for (j = i + 1; j == i; j = (j + 1) % nent) {
+		struct kvm_cpuid_entry2 *ej = &vcpu->cpuid_entries[j];
+		if (ej->function == e->function) {
+			ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
+			return j;
+		}
+	}
+	return 0; /* silence gcc, even though control never reaches here */
+}
+
+/* find an entry with matching function, matching index (if needed), and that
+ * should be read next (if it's stateful) */
+static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
+	u32 function, u32 index)
+{
+	if (e->function != function)
+		return 0;
+	if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
+		return 0;
+	if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) && 
+		!(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
+		return 0;
+	return 1;
+}
+
 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
 {
 	int i;
-	u32 function;
-	struct kvm_cpuid_entry *e, *best;
+	u32 function, index;
+	struct kvm_cpuid_entry2 *e, *best;
 
 	kvm_x86_ops->cache_regs(vcpu);
 	function = vcpu->regs[VCPU_REGS_RAX];
+	index = vcpu->regs[VCPU_REGS_RCX];
 	vcpu->regs[VCPU_REGS_RAX] = 0;
 	vcpu->regs[VCPU_REGS_RBX] = 0;
 	vcpu->regs[VCPU_REGS_RCX] = 0;
@@ -1902,7 +2212,9 @@ void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
 	best = NULL;
 	for (i = 0; i < vcpu->cpuid_nent; ++i) {
 		e = &vcpu->cpuid_entries[i];
-		if (e->function == function) {
+		if (is_matching_cpuid_entry(e, function, index)) {
+			if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
+				move_to_next_stateful_cpuid_entry(vcpu, i);
 			best = e;
 			break;
 		}
drivers/kvm/x86.h +1 -1
index b1528c9..78ab1e1 100644
@@ -149,7 +149,7 @@ struct kvm_vcpu {
 	int halt_request; /* real mode on Intel only */
 
 	int cpuid_nent;
-	struct kvm_cpuid_entry cpuid_entries[KVM_MAX_CPUID_ENTRIES];
+	struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
 
 	/* emulate context */
 
include/asm-x86/kvm.h +21 -0
index 4837d75..17afa81 100644
@@ -151,5 +151,26 @@ struct kvm_cpuid {
 	struct kvm_cpuid_entry entries[0];
 };
 
+struct kvm_cpuid_entry2 {
+	__u32 function;
+	__u32 index;
+	__u32 flags;
+	__u32 eax;
+	__u32 ebx;
+	__u32 ecx;
+	__u32 edx;
+	__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
+
+/* for KVM_SET_CPUID2 */
+struct kvm_cpuid2 {
+	__u32 nent;
+	__u32 padding;
+	struct kvm_cpuid_entry2 entries[0];
+};
 
 #endif
include/linux/kvm.h +4 -0
index fd4f900..b751552 100644
@@ -224,6 +224,7 @@ struct kvm_signal_mask {
 #define KVM_CAP_MMU_SHADOW_CACHE_CONTROL 2
 #define KVM_CAP_USER_MEMORY 3
 #define KVM_CAP_SET_TSS_ADDR 4
+#define KVM_CAP_EXT_CPUID 5
 
 /*
  * ioctls for VM fds
@@ -241,6 +242,7 @@ struct kvm_signal_mask {
 #define KVM_CREATE_VCPU           _IO(KVMIO,  0x41)
 #define KVM_GET_DIRTY_LOG         _IOW(KVMIO, 0x42, struct kvm_dirty_log)
 #define KVM_SET_MEMORY_ALIAS      _IOW(KVMIO, 0x43, struct kvm_memory_alias)
+#define KVM_GET_SUPPORTED_CPUID   _IOWR(KVMIO, 0x48, struct kvm_cpuid2)
 /* Device model IOC */
 #define KVM_CREATE_IRQCHIP	  _IO(KVMIO,  0x60)
 #define KVM_IRQ_LINE		  _IOW(KVMIO, 0x61, struct kvm_irq_level)
@@ -266,5 +268,7 @@ struct kvm_signal_mask {
 #define KVM_SET_FPU               _IOW(KVMIO,  0x8d, struct kvm_fpu)
 #define KVM_GET_LAPIC             _IOR(KVMIO,  0x8e, struct kvm_lapic_state)
 #define KVM_SET_LAPIC             _IOW(KVMIO,  0x8f, struct kvm_lapic_state)
+#define KVM_SET_CPUID2            _IOW(KVMIO,  0x90, struct kvm_cpuid2)
+#define KVM_GET_CPUID2            _IOWR(KVMIO, 0x91, struct kvm_cpuid2)
 
 #endif

commit c628e2861b63a7495f999b3f90afab2d7799e122

Author: Dan Kenigsberg <danken-atKUWr5tajBWk0Htik3J/[email protected]>
Date:   Wed Nov 21 16:37:07 2007 +0200

Add -cpu host option. Expose to guest the CPUID that is supported by the host (or specifically requested in the command line)

kernel/external-module-compat.h +4 -0
index e3f81fe..dbb975a 100644
@@ -541,3 +541,7 @@ static inline void blahblah(void)
 {
 	(void)empty_zero_page[0];
 }
+
+#ifndef X86_FEATURE_NX
+#define X86_FEATURE_NX (1*32+20)
+#endif
libkvm/libkvm-x86.c +57 -3

75 lines not on this page · raw message

libkvm/libkvm.h +30 -1
index 2094dfd..d23b52b 100644
@@ -360,7 +360,36 @@ int kvm_guest_debug(kvm_context_t, int vcpu, struct kvm_debug_guest *dbg);
  * \return 0 on success, or -errno on error
  */
 int kvm_setup_cpuid(kvm_context_t kvm, int vcpu, int nent,
-		    struct kvm_cpuid_entry *entries);
+		    struct kvm_cpuid_entry2 *entries);
+
+/*!
+ * \brief Setup a vcpu's cpuid instruction emulation
+ *
+ * Get the currently-held table of cpuid functions and their states.\n
+ *
+ * \param kvm Pointer to the current kvm_context
+ * \param vcpu Which virtual CPU should be initialized
+ * \param nent Pointer to the number of entries to be obtained.
+ *             On return, the number that was actually obtained.
+ * \param entries cpuid function entries table
+ * \return 0 on success, or -errno on error
+ */
+int kvm_get_cpuid(kvm_context_t kvm, int vcpu, int *nent,
+		    struct kvm_cpuid_entry2 *entries);
+
+/*!
+ * \brief Obtain cpuid supported by kvm
+ *
+ * Obtain a table of cpuid function supported by kvm.\n
+ *
+ * \param kvm Pointer to the current kvm_context
+ * \param nent Pointer to the number of entries to be obtained.
+ *             On return, the number that was actually obtained.
+ * \param entries cpuid function entries table
+ * \return 0 on success, or -errno on error
+ */
+int kvm_get_supported_cpuid(kvm_context_t kvm, int *nent,
+	struct kvm_cpuid_entry2 *entries);
 
 /*!
  * \brief Set a vcpu's signal mask for guest mode
qemu/hw/pc.c +10 -3
index 6c71b09..588c3ba 100644
@@ -683,7 +683,7 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size, int boot_device,
                      DisplayState *ds, const char **fd_filename, int snapshot,
                      const char *kernel_filename, const char *kernel_cmdline,
                      const char *initrd_filename,
-                     int pci_enabled)
+                     int pci_enabled, const char *cpu_model)
 {
     char buf[1024];
     int ret, linux_boot, i;
@@ -705,6 +705,13 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size, int boot_device,
     linux_boot = (kernel_filename != NULL);
 
     /* init CPUs */
+    if (cpu_model == NULL)
+        cpu_model = "basic";
+
+    if (x86_find_cpu_by_name(cpu_model)) {
+        fprintf(stderr, "Unable to find x86 CPU definition\n");
+        exit(1);
+    }
     for(i = 0; i < smp_cpus; i++) {
         env = cpu_init();
         if (i != 0)
@@ -1038,7 +1045,7 @@ static void pc_init_pci(ram_addr_t ram_size, int vga_ram_size, int boot_device,
     pc_init1(ram_size, vga_ram_size, boot_device,
              ds, fd_filename, snapshot,
              kernel_filename, kernel_cmdline,
-             initrd_filename, 1);
+             initrd_filename, 1, cpu_model);
 }
 
 static void pc_init_isa(ram_addr_t ram_size, int vga_ram_size, int boot_device,
@@ -1052,7 +1059,7 @@ static void pc_init_isa(ram_addr_t ram_size, int vga_ram_size, int boot_device,
     pc_init1(ram_size, vga_ram_size, boot_device,
              ds, fd_filename, snapshot,
              kernel_filename, kernel_cmdline,
-             initrd_filename, 0);
+             initrd_filename, 0, cpu_model);
 }
 
 QEMUMachine pc_machine = {
qemu/qemu-kvm.c +81 -17

125 lines not on this page · raw message

qemu/target-i386/cpu.h +45 -1

80 lines not on this page · raw message

qemu/target-i386/helper2.c +305 -65

394 lines not on this page · raw message

qemu/vl.c +2 -0
index 70d8c81..70558dc 100644
@@ -8087,6 +8087,8 @@ int main(int argc, char **argv)
                     mips_cpu_list(stdout, &fprintf);
 #elif defined(TARGET_SPARC)
                     sparc_cpu_list(stdout, &fprintf);
+#elif defined(TARGET_I386)
+                    x86_cpu_list(stdout, &fprintf);
 #endif
                     exit(0);
                 } else {

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel

A message that is not in the archive (sent off-list or not yet received): 20071121144128.GA26036-iWbx9bcAnq+Hk9JtIoIkgNBPR1lH4CV8@public.gmane.org

#7 lore raw
Quoted 18 lines · Dan Kenigsberg
Quoted 16 lines · Avi Kivity
Quoted 14 lines · Dan Kenigsberg

These patches expose host CPU features (that are known to work under KVM) to guests. It makes a couple of benchmarks run faster, and generally gives kvm's user better info on its host.

The kernel-space patch adds KVM_GET_SUPPORTED_CPUID ioctl to obtain the table of cpuid functions supported by the host. The user-space patch allows fine-tuning this table from the command-line.

I had to define struct kvm_cpuid2, KVM_SET_CPUID2 etc., because cpuid functions are a little more complex than just function-value pairs.
commit e9775d0a16097cfb71779cb2fb985fb3e5040dc8

Author: Dan Kenigsberg <danken-atKUWr5tajBWk0Htik3J/[email protected]>
Date:   Sun Nov 18 13:55:26 2007 +0200
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

Support -cpu host option. Negotiate cpuid table with userspace.

I believe the attached two patches apply all your comments.

Applied kernel patch. Will wait with userspace until next qemu merge (which is pending qemu-cvs regressions).

Signature · 7 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#8 lore raw
In reply to a message not in the archive
Quoted 4 lines · Dan Kenigsberg

These patches expose host CPU features (that are known to work under KVM) to guests. It makes a couple of benchmarks run faster, and generally gives kvm's user better info on its host.

Quoted 8 lines

The kernel-space patch adds KVM_GET_SUPPORTED_CPUID ioctl to obtain the table of cpuid functions supported by the host. The user-space patch allows fine-tuning this table from the command-line.

I had to define struct kvm_cpuid2, KVM_SET_CPUID2 etc., because cpuid functions are a little more complex than just function-value pairs.

Dan.

With current kvm-git (commit 51727a110220681f6f43b005d069e28c58f5d151) (userspace is current to commit 6a385c9539f9746d7ff51ef34c064c3eba86448b) and the userspace portion of this patch I cannot boot a 64 bit guest (Mandriva 2008 x64) on my AMD x2 without -cpu host. The guest kernel says "Your CPU does not support long mode. Use a 32bit distribution.". Works fine without the userspace portion of the patch.

With -cpu host, the 64bit kernel starts to boot then panics almost immediately:

Code: 0f 30 e9 ed fc ff ff b8 66 00 00 00 f0 0f ab 45 08 e9 e2 fa
RIP [<ffffffff8020ff99> identify_cpu+0x609/0x700
 RSP <ffffffff8057df58>
Kernel panic - not syncing: Attempted to kill the idle task!

In the host log I see: cpu0 unhandled wrmsr: 0xc0010015, which might not have anything to do with this patch of course :)

Mike

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

A message that is not in the archive (sent off-list or not yet received): 200711221012.05384.mike-/[email protected]

#10 lore raw
Quoted 12 lines · Mike Lampard
Quoted 11 lines · Dan Kenigsberg

These patches expose host CPU features (that are known to work under KVM) to guests. It makes a couple of benchmarks run faster, and generally gives kvm's user better info on its host.

The kernel-space patch adds KVM_GET_SUPPORTED_CPUID ioctl to obtain the table of cpuid functions supported by the host. The user-space patch allows fine-tuning this table from the command-line.

I had to define struct kvm_cpuid2, KVM_SET_CPUID2 etc., because cpuid functions are a little more complex than just function-value pairs.

Quoted 8 lines
Quoted 1 line

Dan.

With current kvm-git (commit 51727a110220681f6f43b005d069e28c58f5d151) (userspace is current to commit 6a385c9539f9746d7ff51ef34c064c3eba86448b) and the userspace portion of this patch I cannot boot a 64 bit guest (Mandriva 2008 x64) on my AMD x2 without -cpu host. The guest kernel says "Your CPU does not support long mode. Use a 32bit distribution.". Works fine without the userspace portion of the patch.

Thanks for your report. I'm probably not exposing one(?) of AMD's x86_64 required cpuid features. However, I fail to reproduce this on Dual-Core AMD Opteron(tm) Processor 2210 running 2.6.18-8.1.15.el5. What is your host?

Dan.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

A message that is not in the archive (sent off-list or not yet received): 20071122130118.GA14295-iWbx9bcAnq+Hk9JtIoIkgNBPR1lH4CV8@public.gmane.org

#12 lore raw
Quoted 22 lines · Dan Kenigsberg
Quoted 12 lines · Mike Lampard
Quoted 11 lines · Dan Kenigsberg

These patches expose host CPU features (that are known to work under KVM) to guests. It makes a couple of benchmarks run faster, and generally gives kvm's user better info on its host.

The kernel-space patch adds KVM_GET_SUPPORTED_CPUID ioctl to obtain the table of cpuid functions supported by the host. The user-space patch allows fine-tuning this table from the command-line.

I had to define struct kvm_cpuid2, KVM_SET_CPUID2 etc., because cpuid functions are a little more complex than just function-value pairs.

Quoted 8 lines
Quoted 1 line

Dan.

With current kvm-git (commit 51727a110220681f6f43b005d069e28c58f5d151) (userspace is current to commit 6a385c9539f9746d7ff51ef34c064c3eba86448b) and the userspace portion of this patch I cannot boot a 64 bit guest (Mandriva 2008 x64) on my AMD x2 without -cpu host. The guest kernel says "Your CPU does not support long mode. Use a 32bit distribution.". Works fine without the userspace portion of the patch.

Quoted 4 lines

Thanks for your report. I'm probably not exposing one(?) of AMD's x86_64 required cpuid features. However, I fail to reproduce this on Dual-Core AMD Opteron(tm) Processor 2210 running 2.6.18-8.1.15.el5. What is your host?

64bit 2.6.23.8 on an AMD Athlon X2 4400.
cpuid flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm 3dnowext 3dnow rep_good pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy misalignsse

If you need further info, just yell. I didnt want to clutter up the mailinglist with the full output of cpuid, but will if it helps you.

Mike

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

A message that is not in the archive (sent off-list or not yet received): 200711222350.14746.mike-/[email protected]

#14 lore raw
Quoted 28 lines · Mike Lampard
Quoted 22 lines · Dan Kenigsberg
Quoted 12 lines · Mike Lampard
Quoted 11 lines · Dan Kenigsberg

These patches expose host CPU features (that are known to work under KVM) to guests. It makes a couple of benchmarks run faster, and generally gives kvm's user better info on its host.

The kernel-space patch adds KVM_GET_SUPPORTED_CPUID ioctl to obtain the table of cpuid functions supported by the host. The user-space patch allows fine-tuning this table from the command-line.

I had to define struct kvm_cpuid2, KVM_SET_CPUID2 etc., because cpuid functions are a little more complex than just function-value pairs.

Quoted 8 lines
Quoted 1 line

Dan.

With current kvm-git (commit 51727a110220681f6f43b005d069e28c58f5d151) (userspace is current to commit 6a385c9539f9746d7ff51ef34c064c3eba86448b) and the userspace portion of this patch I cannot boot a 64 bit guest (Mandriva 2008 x64) on my AMD x2 without -cpu host. The guest kernel says "Your CPU does not support long mode. Use a 32bit distribution.". Works fine without the userspace portion of the patch.

Quoted 4 lines

Thanks for your report. I'm probably not exposing one(?) of AMD's x86_64 required cpuid features. However, I fail to reproduce this on Dual-Core AMD Opteron(tm) Processor 2210 running 2.6.18-8.1.15.el5. What is your host?

Quoted 8 lines

64bit 2.6.23.8 on an AMD Athlon X2 4400.
cpuid flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm 3dnowext 3dnow rep_good pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy misalignsse

If you need further info, just yell. I didnt want to clutter up the mailinglist with the full output of cpuid, but will if it helps you.

Since I'm currently out of ideas, would you try it with -no-kvm? Since current Qemu cvs has a -cpu userspace patch, it might be worthwhile to test that, too.

Dan.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#15 lore raw
In reply to a message not in the archive
Quoted 9 lines · Mike Lampard

With current kvm-git (commit 51727a110220681f6f43b005d069e28c58f5d151) (userspace is current to commit 6a385c9539f9746d7ff51ef34c064c3eba86448b) and the userspace portion of this patch I cannot boot a 64 bit guest (Mandriva 2008 x64) on my AMD x2 without -cpu host. The guest kernel says "Your CPU does not support long mode. Use a 32bit distribution.". Works fine without the userspace portion of the patch.

With -cpu host, the 64bit kernel starts to boot then panics almost immediately:

Quoted 8 lines
Code: 0f 30 e9 ed fc ff ff b8 66 00 00 00 f0 0f ab 45 08 e9 e2 fa
RIP [<ffffffff8020ff99> identify_cpu+0x609/0x700
 RSP <ffffffff8057df58>
Kernel panic - not syncing: Attempted to kill the idle task!

In the host log I see: cpu0 unhandled wrmsr: 0xc0010015, which might not have anything to do with this patch of course :)

A listing/disassembly of the function will help show where the problem is.

Signature · 7 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

A message that is not in the archive (sent off-list or not yet received): 47458458.3030104-atKUWr5tajBWk0Htik3J/[email protected]

#17 lore raw
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Mike Lampard

With current kvm-git (commit 51727a110220681f6f43b005d069e28c58f5d151) (userspace is current to commit 6a385c9539f9746d7ff51ef34c064c3eba86448b) and the userspace portion of this patch I cannot boot a 64 bit guest (Mandriva 2008 x64) on my AMD x2 without -cpu host. The guest kernel says "Your CPU does not support long mode. Use a 32bit distribution.". Works fine without the userspace portion of the patch.

erm.. sorry, seems the patch mis-applied @ my end, I just re-applied the userspace patch and the above problem is no longer, 1 down, 1 to go:

Quoted 8 lines
Quoted 8 lines

With -cpu host, the 64bit kernel starts to boot then panics almost immediately:

Code: 0f 30 e9 ed fc ff ff b8 66 00 00 00 f0 0f ab 45 08 e9 e2 fa
RIP [<ffffffff8020ff99> identify_cpu+0x609/0x700
 RSP <ffffffff8057df58>
Kernel panic - not syncing: Attempted to kill the idle task!
Quoted 4 lines
Quoted 2 lines

In the host log I see: cpu0 unhandled wrmsr: 0xc0010015, which might not have anything to do with this patch of course :)

A listing/disassembly of the function will help show where the problem is.

yes, of course.. I'll post it as soon as I can.

Thanks for the help
Mike

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#18 lore raw

RFC 1/1 Expose host cpuid to guest: userspace patch

In reply to a message not in the archive

The first little patch (already swallowed by qemu-devel) corrects the cpuid_level exposed for various cpu models, and lets us change it.

The main attached patch is the userspace part of cpuid support. Please comment if you find something that is not to your liking.

Regards,
Dan.

qemu/target-i386/helper2.c +8 -1
index 1874b73..da7a54c 100644
@@ -125,6 +125,7 @@ CPUX86State *cpu_x86_init(const char *cpu_model)
 
 typedef struct x86_def_t {
     const char *name;
+    uint32_t level;
     uint32_t vendor1, vendor2, vendor3;
     int family;
     int model;
@@ -141,6 +142,7 @@ static x86_def_t x86_defs[] = {
 #ifdef TARGET_X86_64
     {
         .name = "qemu64",
+        .level = 2,
         .vendor1 = 0x68747541, /* "Auth" */
         .vendor2 = 0x69746e65, /* "enti" */
         .vendor3 = 0x444d4163, /* "cAMD" */
@@ -161,6 +163,7 @@ static x86_def_t x86_defs[] = {
 #endif
     {
         .name = "qemu32",
+        .level = 2,
         .family = 6,
         .model = 3,
         .stepping = 3,
@@ -170,6 +173,7 @@ static x86_def_t x86_defs[] = {
     },
     {
         .name = "486",
+        .level = 0,
         .family = 4,
         .model = 0,
         .stepping = 0,
@@ -178,6 +182,7 @@ static x86_def_t x86_defs[] = {
     },
     {
         .name = "pentium",
+        .level = 1,
         .family = 5,
         .model = 4,
         .stepping = 3,
@@ -186,6 +191,7 @@ static x86_def_t x86_defs[] = {
     },
     {
         .name = "pentium2",
+        .level = 2,
         .family = 6,
         .model = 5,
         .stepping = 2,
@@ -194,6 +200,7 @@ static x86_def_t x86_defs[] = {
     },
     {
         .name = "pentium3",
+        .level = 2,
         .family = 6,
         .model = 7,
         .stepping = 3,
@@ -312,7 +319,7 @@ static int cpu_x86_register (CPUX86State *env, const char *cpu_model)
         env->cpuid_vendor2 = 0x49656e69; /* "ineI" */
         env->cpuid_vendor3 = 0x6c65746e; /* "ntel" */
     }
-    env->cpuid_level = 2;
+    env->cpuid_level = def->level;
     env->cpuid_version = (def->family << 8) | (def->model << 4) | def->stepping;
     env->cpuid_features = def->features;
     env->pat = 0x0007040600070406ULL;

commit cd7d63ca03061e8c0dd7d71606779ab1d7b6c4cd

Author: Dan Kenigsberg <danken-atKUWr5tajBWk0Htik3J/[email protected]>
Date:   Thu Dec 20 19:34:18 2007 +0200

Add -cpu host option. Expose to guest the CPUID that is supported by the host (or specifically requested in the command line).
Note that the default cpu is now host' instead of qemu64'

libkvm/libkvm-x86.c +94 -4

113 lines not on this page · raw message

libkvm/libkvm.h +43 -1

56 lines not on this page · raw message

qemu/hw/pc.c +4 -0
index dde40c3..b653c46 100644
@@ -751,6 +751,10 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
 #else
         cpu_model = "qemu32";
 #endif
+#ifdef USE_KVM
+        if (kvm_allowed)
+            cpu_model = "host";
+#endif
     }
     
     for(i = 0; i < smp_cpus; i++) {
qemu/qemu-kvm-x86.c +89 -18

140 lines not on this page · raw message

qemu/target-i386/cpu.h +3 -0
index 7143ab3..2034381 100644
@@ -588,6 +588,9 @@ typedef struct CPUX86State {
     uint32_t cpuid_ext2_features;
     uint32_t cpuid_ext3_features;
     uint32_t cpuid_apic_id;
+#ifdef USE_KVM
+    int cpuid_hostlike;
+#endif
 
 #ifdef USE_KQEMU
     int kqemu_enabled;
qemu/target-i386/helper2.c +35 -0
index da7a54c..78c5cf9 100644
@@ -123,6 +123,15 @@ CPUX86State *cpu_x86_init(const char *cpu_model)
     return env;
 }
 
+#ifdef USE_KVM
+struct hostlike_user_disable {
+    uint32_t features;
+    uint32_t ext_features;
+    uint32_t ext2_features;
+    uint32_t ext3_features;
+} hostlike_user_disable;
+#endif
+
 typedef struct x86_def_t {
     const char *name;
     uint32_t level;
@@ -131,6 +140,9 @@ typedef struct x86_def_t {
     int model;
     int stepping;
     uint32_t features, ext_features, ext2_features, ext3_features;
+#ifdef USE_KVM
+    int hostlike;
+#endif
     uint32_t xlevel;
 } x86_def_t;
 
@@ -207,6 +219,12 @@ static x86_def_t x86_defs[] = {
         .features = 0x0383F9FF,
         .xlevel = 0,
     },
+#ifdef USE_KVM
+    {
+        .name = "host",
+        .hostlike = 1,
+    },
+#endif
 };
 
 static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
@@ -229,6 +247,12 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
     }
     if (!def)
         goto error;
+#ifdef USE_KVM
+    if (!kvm_allowed && def->hostlike) {
+        fprintf(stderr, "hostlike cpu requires KVM.\n");
+        goto error;
+    }
+#endif
     memcpy(x86_cpu_def, def, sizeof(*def));
 
     featurestr = strtok(NULL, ",");
@@ -288,6 +312,14 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)
     x86_cpu_def->ext_features &= ~minus_ext_features;
     x86_cpu_def->ext2_features &= ~minus_ext2_features;
     x86_cpu_def->ext3_features &= ~minus_ext3_features;
+#ifdef USE_KVM
+    if (kvm_allowed) {
+        hostlike_user_disable.features = minus_features;
+        hostlike_user_disable.ext_features = minus_ext_features;
+        hostlike_user_disable.ext2_features = minus_ext2_features;
+        hostlike_user_disable.ext3_features = minus_ext3_features;
+    }
+#endif
     free(s);
     return 0;
 
@@ -341,6 +373,9 @@ static int cpu_x86_register (CPUX86State *env, const char *cpu_model)
             env->cpuid_model[i >> 2] |= c << (8 * (i & 3));
         }
     }
+#ifdef USE_KVM
+    env->cpuid_hostlike = def->hostlike;
+#endif
     return 0;
 }
 

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel