Skip to contentSkip to conversation

kvm: x86: Emulate MSR_PLATFORM_INFO

PATCH New Bandan Das 1 participant 3 messages Jun 21, 2013

Subsystems (3)
lore
Share & tools
lore

Bandan Das

author
#0 lore raw

v3 cover kvm: x86: Emulate MSR_PLATFORM_INFO

These patches add an emulated MSR_PLATFORM_INFO that kvm guests can read as described in section 14.3.2.4 of the Intel SDM. The relevant changes and details are in [2/2]; [1/2] makes vendor_intel generic. There are atleat two known applications that fail to run because of this MSR missing - Sandra and vTune.

v3: Some more minor corrections
v2: Addressed suggested changes

Bandan Das (2):
kvm: make vendor_intel a generic function
kvm: x86: emulate MSR_PLATFORM_INFO

 arch/x86/include/asm/kvm_emulate.h    | 13 ----------
 arch/x86/include/asm/kvm_host.h       | 20 +++++++++++++++
 arch/x86/include/uapi/asm/msr-index.h |  2 ++
 arch/x86/kvm/cpuid.c                  | 19 ++++++++++++++
 arch/x86/kvm/cpuid.h                  | 13 ++++++++++
 arch/x86/kvm/emulate.c                | 16 +++---------
 arch/x86/kvm/x86.c                    | 48 +++++++++++++++++++++++++++++++++++
 7 files changed, 106 insertions(+), 25 deletions(-)
Signature · 1 line
1.8.1.4
author
#1 lore raw

PATCH v3 1/2 kvm: make vendor_intel a generic function

Make vendor_intel generic so that functions in x86.c can use it.

---

v2:
Change vendor_intel function signature because the emulator shouldn't be dealing with struct vcpu

 arch/x86/include/asm/kvm_emulate.h | 13 -------------
 arch/x86/include/asm/kvm_host.h    | 20 ++++++++++++++++++++
 arch/x86/kvm/emulate.c             | 16 ++++------------
 3 files changed, 24 insertions(+), 25 deletions(-)
arch/x86/include/asm/kvm_emulate.h +0 -13
index 15f960c..611a55f 100644
@@ -319,19 +319,6 @@ struct x86_emulate_ctxt {
 #define REPE_PREFIX	0xf3
 #define REPNE_PREFIX	0xf2
 
-/* CPUID vendors */
-#define X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx 0x68747541
-#define X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx 0x444d4163
-#define X86EMUL_CPUID_VENDOR_AuthenticAMD_edx 0x69746e65
-
-#define X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx 0x69444d41
-#define X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx 0x21726574
-#define X86EMUL_CPUID_VENDOR_AMDisbetterI_edx 0x74656273
-
-#define X86EMUL_CPUID_VENDOR_GenuineIntel_ebx 0x756e6547
-#define X86EMUL_CPUID_VENDOR_GenuineIntel_ecx 0x6c65746e
-#define X86EMUL_CPUID_VENDOR_GenuineIntel_edx 0x49656e69
-
 enum x86_intercept_stage {
 	X86_ICTP_NONE = 0,   /* Allow zero-init to not match anything */
 	X86_ICPT_PRE_EXCEPT,
arch/x86/include/asm/kvm_host.h +20 -0
index 3741c65..ce9a44f 100644
@@ -144,6 +144,19 @@ enum {
 
 #include <asm/kvm_emulate.h>
 
+/* CPUID vendors */
+#define X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx 0x68747541
+#define X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx 0x444d4163
+#define X86EMUL_CPUID_VENDOR_AuthenticAMD_edx 0x69746e65
+
+#define X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx 0x69444d41
+#define X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx 0x21726574
+#define X86EMUL_CPUID_VENDOR_AMDisbetterI_edx 0x74656273
+
+#define X86EMUL_CPUID_VENDOR_GenuineIntel_ebx 0x756e6547
+#define X86EMUL_CPUID_VENDOR_GenuineIntel_ecx 0x6c65746e
+#define X86EMUL_CPUID_VENDOR_GenuineIntel_edx 0x49656e69
+
 #define KVM_NR_MEM_OBJS 40
 
 #define KVM_NR_DB_REGS	4
@@ -942,6 +955,13 @@ static inline unsigned long read_msr(unsigned long msr)
 }
 #endif
 
+static inline bool vendor_intel(u32 ebx, u32 ecx, u32 edx)
+{
+	return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
+		&& ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
+		&& edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
+}
+
 static inline u32 get_rdx_init_val(void)
 {
 	return 0x600; /* P6 family */
arch/x86/kvm/emulate.c +4 -12
index 8db0010..87f12fc 100644
@@ -2280,17 +2280,6 @@ setup_syscalls_segments(struct x86_emulate_ctxt *ctxt,
 	ss->avl = 0;
 }
 
-static bool vendor_intel(struct x86_emulate_ctxt *ctxt)
-{
-	u32 eax, ebx, ecx, edx;
-
-	eax = ecx = 0;
-	ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
-	return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx
-		&& ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx
-		&& edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
-}
-
 static bool em_syscall_is_enabled(struct x86_emulate_ctxt *ctxt)
 {
 	const struct x86_emulate_ops *ops = ctxt->ops;
@@ -2400,6 +2389,7 @@ static int em_sysenter(struct x86_emulate_ctxt *ctxt)
 	u64 msr_data;
 	u16 cs_sel, ss_sel;
 	u64 efer = 0;
+	u32 eax, ebx, ecx, edx;
 
 	ops->get_msr(ctxt, MSR_EFER, &efer);
 	/* inject #GP if in real mode */
@@ -2410,8 +2400,10 @@ static int em_sysenter(struct x86_emulate_ctxt *ctxt)
 	 * Not recognized on AMD in compat mode (but is recognized in legacy
 	 * mode).
 	 */
+	eax = ecx = 0;
+	ctxt->ops->get_cpuid(ctxt, &eax, &ebx, &ecx, &edx);
 	if ((ctxt->mode == X86EMUL_MODE_PROT32) && (efer & EFER_LMA)
-	    && !vendor_intel(ctxt))
+	    && !vendor_intel(ebx, ecx, edx))
 		return emulate_ud(ctxt);
 
 	/* XXX sysenter/sysexit have not been tested in 64bit mode.
Signature · 1 line
1.8.1.4
author
#2 lore raw

PATCH v3 2/2 kvm: x86: emulate MSR_PLATFORM_INFO

In reply to Bandan Das

To emulate MSR_PLATFORM_INFO, we divide guest virtual_tsc_khz by the base clock - which is guest CPU model dependent. The relevant bits in this emulated MSR are :
Max Non-Turbo Ratio (15:8) - virtual_tsc_khz/bclk
Max Effi Ratio (47:40) - virtual_tsc_khz/bclk
Prog Ratio Limit for Trubo (28) - 0
Prog TDC-TDP Limit for Turbo (29) - 0

---

v3:
Change bclk to base_clock_khz

v2:
Change function name to kvm_cpuid_get_intel_model and call the new vendor_intel()

 arch/x86/include/uapi/asm/msr-index.h |  2 ++
 arch/x86/kvm/cpuid.c                  | 19 ++++++++++++++
 arch/x86/kvm/cpuid.h                  | 13 ++++++++++
 arch/x86/kvm/x86.c                    | 48 +++++++++++++++++++++++++++++++++++
 4 files changed, 82 insertions(+)
arch/x86/include/uapi/asm/msr-index.h +2 -0
index 2af848d..b0268d5 100644
@@ -331,6 +331,8 @@
 
 #define MSR_IA32_MISC_ENABLE		0x000001a0
 
+#define MSR_IA32_PLATFORM_INFO		0x000000ce
+
 #define MSR_IA32_TEMPERATURE_TARGET	0x000001a2
 
 #define MSR_IA32_ENERGY_PERF_BIAS	0x000001b0
arch/x86/kvm/cpuid.c +19 -0
index a20ecb5..ae970d3 100644
@@ -608,6 +608,25 @@ struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
 }
 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
 
+u8 kvm_cpuid_get_intel_model(struct kvm_vcpu *vcpu)
+{
+	struct kvm_cpuid_entry2 *best;
+	u8 cpuid_model, cpuid_extmodel;
+
+	best = kvm_find_cpuid_entry(vcpu, 0, 0);
+	if (!vendor_intel(best->ebx, best->ecx, best->edx))
+		return CPUID_MODEL_UNKNOWN;
+
+	best = kvm_find_cpuid_entry(vcpu, 1, 0);
+
+	cpuid_model = (best->eax >> 4) & 0xf;
+	cpuid_extmodel = (best->eax >> 16) & 0xf;
+
+	cpuid_model += (cpuid_extmodel << 4);
+
+	return cpuid_model;
+}
+
 int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
 {
 	struct kvm_cpuid_entry2 *best;
arch/x86/kvm/cpuid.h +13 -0
index b7fd079..2613f37 100644
@@ -3,6 +3,18 @@
 
 #include "x86.h"
 
+#define MODEL_NEHALEM_CLARKSFIELD        0x1e /* Core i7 and Ex BCLK: 133Mhz */
+#define MODEL_NEHALEM_BLOOMFIELD         0x1a /* Core i7 Xeon 3000 BCLK: 133 Mhz */
+#define MODEL_NEHALEM_EX                 0x2e /* Core i7 and i5 Nehalem BCLK: 133 Mhz */
+#define MODEL_WESTMERE_ARRANDALE         0x25 /* Celeron/Pentium/Core i3/i5/i7 BCLK: 133 Mhz */
+#define MODEL_WESTMERE_EX                0x2f /* Xeon E7 BCLK: 133 Mhz */
+#define MODEL_WESTMERE_GULFTOWN          0x2c /* Core i7 Xeon 3000 BCLK: 133 Mhz */
+#define MODEL_SANDYBRIDGE_SANDY          0x2a /* Core/Celeron/Pentium/Xeon BCLK: 133 Mhz */
+#define MODEL_SANDYBRIDGE_E              0x2d /* Core i7 and Ex BCLK: 100 Mhz */
+#define MODEL_IVYBRIDGE_IVY              0x3a /* Core i3/i5/i7 (Ex) and Xeon E3 BCLK: 100 Mhz */
+#define MODEL_HASWELL_HASWELL            0x3c /* BCLK: 100 Mhz */
+#define CPUID_MODEL_UNKNOWN              0x0  /* Everything else */
+
 void kvm_update_cpuid(struct kvm_vcpu *vcpu);
 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
 					      u32 function, u32 index);
@@ -18,6 +30,7 @@ int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
 			      struct kvm_cpuid2 *cpuid,
 			      struct kvm_cpuid_entry2 __user *entries);
 void kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx);
+u8 kvm_cpuid_get_intel_model(struct kvm_vcpu *vcpu);
 
 
 static inline bool guest_cpuid_has_xsave(struct kvm_vcpu *vcpu)
arch/x86/kvm/x86.c +48 -0

66 lines not on this page · raw message

Signature · 1 line
1.8.1.4