From f4b732d0c3b5cb5467dc6c1275202728e43430d5 Mon Sep 17 00:00:00 2001
From: Sheng Yang <sheng.yang@intel.com>
Date: Tue, 31 Jul 2007 10:21:32 +0800
Subject: [PATCH] Add cpu consistence check in vmx.

All the physical CPUs on the board should support the same VMX feature set.
Add check_processor_compatibility to kvm_arch_ops for the consistence
check.
---
 drivers/kvm/kvm.h      |    1 +
 drivers/kvm/kvm_main.c |    9 +++++++++
 drivers/kvm/svm.c      |    6 ++++++
 drivers/kvm/vmx.c      |   36 ++++++++++++++++++++++++++++--------
 4 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/drivers/kvm/kvm.h b/drivers/kvm/kvm.h
index f78729c..e4f11b6 100644
--- a/drivers/kvm/kvm.h
+++ b/drivers/kvm/kvm.h
@@ -420,6 +420,7 @@ struct kvm_arch_ops {
 	int (*disabled_by_bios)(void);             /* __init */
 	void (*hardware_enable)(void *dummy);      /* __init */
 	void (*hardware_disable)(void *dummy);
+	void (*check_processor_compatibility)(void *rtn);
 	int (*hardware_setup)(void);               /* __init */
 	void (*hardware_unsetup)(void);            /* __exit */
 
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c
index 4fd2074..ae14163 100644
--- a/drivers/kvm/kvm_main.c
+++ b/drivers/kvm/kvm_main.c
@@ -3095,6 +3095,7 @@ int kvm_init_arch(struct kvm_arch_ops *ops, unsigned int vcpu_size,
 		  struct module *module)
 {
 	int r;
+	int cpu;
 
 	if (kvm_arch_ops) {
 		printk(KERN_ERR "kvm: already loaded the other module\n");
@@ -3116,6 +3117,14 @@ int kvm_init_arch(struct kvm_arch_ops *ops, unsigned int vcpu_size,
 	if (r < 0)
 		goto out;
 
+	for_each_online_cpu(cpu) {
+		smp_call_function_single(cpu,
+				kvm_arch_ops->check_processor_compatibility,
+				&r, 0, 1);
+		if (r < 0)
+			goto out;
+	}
+
 	on_each_cpu(hardware_enable, NULL, 0, 1);
 	r = register_cpu_notifier(&kvm_cpu_notifier);
 	if (r)
diff --git a/drivers/kvm/svm.c b/drivers/kvm/svm.c
index 5277084..827bc27 100644
--- a/drivers/kvm/svm.c
+++ b/drivers/kvm/svm.c
@@ -1798,11 +1798,17 @@ svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
 	hypercall[3] = 0xc3;
 }
 
+static void svm_check_processor_compat(void *rtn)
+{
+	*(int *)rtn = 0;
+}
+
 static struct kvm_arch_ops svm_arch_ops = {
 	.cpu_has_kvm_support = has_svm,
 	.disabled_by_bios = is_disabled,
 	.hardware_setup = svm_hardware_setup,
 	.hardware_unsetup = svm_hardware_unsetup,
+	.check_processor_compatibility = svm_check_processor_compat,
 	.hardware_enable = svm_hardware_enable,
 	.hardware_disable = svm_hardware_disable,
 
diff --git a/drivers/kvm/vmx.c b/drivers/kvm/vmx.c
index 6e23600..41a4986 100644
--- a/drivers/kvm/vmx.c
+++ b/drivers/kvm/vmx.c
@@ -902,14 +902,26 @@ static __init int setup_vmcs_config(void)
 	if (((vmx_msr_high >> 18) & 15) != 6)
 		return -1;
 
-	vmcs_config.size = vmx_msr_high & 0x1fff;
-	vmcs_config.order = get_order(vmcs_config.size);
-	vmcs_config.revision_id = vmx_msr_low;
-
-	vmcs_config.pin_based_exec_ctrl = _pin_based_exec_control;
-	vmcs_config.cpu_based_exec_ctrl = _cpu_based_exec_control;
-	vmcs_config.vmexit_ctrl         = _vmexit_control;
-	vmcs_config.vmentry_ctrl        = _vmentry_control;
+	if (vmcs_config.size == 0) {
+		/* called in hardware_setup(), initialization */
+		vmcs_config.size = vmx_msr_high & 0x1fff;
+		vmcs_config.order = get_order(vmcs_config.size);
+		vmcs_config.revision_id = vmx_msr_low;
+
+		vmcs_config.pin_based_exec_ctrl = _pin_based_exec_control;
+		vmcs_config.cpu_based_exec_ctrl = _cpu_based_exec_control;
+		vmcs_config.vmexit_ctrl         = _vmexit_control;
+		vmcs_config.vmentry_ctrl        = _vmentry_control;
+	} else if ((vmcs_config.size != (vmx_msr_high & 0x1fff))
+		|| (vmcs_config.revision_id != vmx_msr_low)
+		|| (vmcs_config.pin_based_exec_ctrl != _pin_based_exec_control)
+		|| (vmcs_config.cpu_based_exec_ctrl != _cpu_based_exec_control)
+		|| (vmcs_config.vmexit_ctrl != _vmexit_control)
+		|| (vmcs_config.vmentry_ctrl != _vmentry_control)) {
+		/* called check_processor_compat(), check consistence */
+		printk(KERN_ERR "kvm: CPUs feature inconsistence!\n");
+		return -1;
+	}
 
 	return 0;
 }
@@ -2412,11 +2424,19 @@ free_vcpu:
 	return ERR_PTR(err);
 }
 
+void __init check_processor_compat(void *rtn)
+{
+	*(int *)rtn = 0;
+	if (setup_vmcs_config() < 0)
+		*(int *)rtn = -1;
+}
+
 static struct kvm_arch_ops vmx_arch_ops = {
 	.cpu_has_kvm_support = cpu_has_kvm_support,
 	.disabled_by_bios = vmx_disabled_by_bios,
 	.hardware_setup = hardware_setup,
 	.hardware_unsetup = hardware_unsetup,
+	.check_processor_compatibility = check_processor_compat,
 	.hardware_enable = hardware_enable,
 	.hardware_disable = hardware_disable,
 
-- 
1.5.2

