All of lore.kernel.org
 help / color / mirror / Atom feed
From: Glauber Costa <glommer@redhat.com>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, avi@redhat.com
Subject: [PATCH 3/5] Try using new kvm clock msrs
Date: Thu, 15 Apr 2010 14:37:26 -0400	[thread overview]
Message-ID: <1271356648-5108-4-git-send-email-glommer@redhat.com> (raw)
In-Reply-To: <1271356648-5108-3-git-send-email-glommer@redhat.com>

We now added a new set of clock-related msrs in replacement of the old
ones. In theory, we could just try to use them and get a return value
indicating they do not exist, due to our use of kvm_write_msr_save.

However, kvm clock registration happens very early, and if we ever
try to write to a non-existant MSR, we raise a lethal #GP, since our
idt handlers are not in place yet.

So this patch tests for a cpuid feature exported by the host to
decide which set of msrs are supported.

Signed-off-by: Glauber Costa <glommer@redhat.com>
---
 arch/x86/include/asm/kvm_para.h |    4 ++
 arch/x86/kernel/kvmclock.c      |   68 +++++++++++++++++++++++++++------------
 2 files changed, 51 insertions(+), 21 deletions(-)

diff --git a/arch/x86/include/asm/kvm_para.h b/arch/x86/include/asm/kvm_para.h
index 0cffb96..a32710a 100644
--- a/arch/x86/include/asm/kvm_para.h
+++ b/arch/x86/include/asm/kvm_para.h
@@ -16,6 +16,10 @@
 #define KVM_FEATURE_CLOCKSOURCE		0
 #define KVM_FEATURE_NOP_IO_DELAY	1
 #define KVM_FEATURE_MMU_OP		2
+/* We could just try to use new msr values, but they are queried very early,
+ * kernel does not have idt handlers yet, and failures are fatal */
+#define KVM_FEATURE_CLOCKSOURCE2	3
+
 
 #define MSR_KVM_WALL_CLOCK_OLD  0x11
 #define MSR_KVM_SYSTEM_TIME_OLD 0x12
diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c
index feaeb0d..6d814ce 100644
--- a/arch/x86/kernel/kvmclock.c
+++ b/arch/x86/kernel/kvmclock.c
@@ -29,6 +29,7 @@
 #define KVM_SCALE 22
 
 static int kvmclock = 1;
+static int kvm_use_new_msrs = 0;
 
 static int parse_no_kvmclock(char *arg)
 {
@@ -41,6 +42,18 @@ early_param("no-kvmclock", parse_no_kvmclock);
 static DEFINE_PER_CPU_SHARED_ALIGNED(struct pvclock_vcpu_time_info, hv_clock);
 static struct pvclock_wall_clock wall_clock;
 
+static int kvm_system_time_write_value(int low, int high)
+{
+	if (kvm_use_new_msrs)
+		return native_write_msr_safe(MSR_KVM_SYSTEM_TIME_OLD, low, high);
+	else
+		return native_write_msr_safe(MSR_KVM_SYSTEM_TIME, low, high);
+}
+
+static void kvm_turnoff_clock(void)
+{
+	kvm_system_time_write_value(0, 0);
+}
 /*
  * The wallclock is the time of day when we booted. Since then, some time may
  * have elapsed since the hypervisor wrote the data. So we try to account for
@@ -54,7 +67,11 @@ static unsigned long kvm_get_wallclock(void)
 
 	low = (int)__pa_symbol(&wall_clock);
 	high = ((u64)__pa_symbol(&wall_clock) >> 32);
-	native_write_msr(MSR_KVM_WALL_CLOCK, low, high);
+
+	if (kvm_use_new_msrs)
+		native_write_msr_safe(MSR_KVM_WALL_CLOCK, low, high);
+	else
+		native_write_msr(MSR_KVM_WALL_CLOCK_OLD, low, high);
 
 	vcpu_time = &get_cpu_var(hv_clock);
 	pvclock_read_wallclock(&wall_clock, vcpu_time, &ts);
@@ -130,7 +147,8 @@ static int kvm_register_clock(char *txt)
 	high = ((u64)__pa(&per_cpu(hv_clock, cpu)) >> 32);
 	printk(KERN_INFO "kvm-clock: cpu %d, msr %x:%x, %s\n",
 	       cpu, high, low, txt);
-	return native_write_msr_safe(MSR_KVM_SYSTEM_TIME, low, high);
+
+	return kvm_system_time_write_value(low, high);
 }
 
 #ifdef CONFIG_X86_LOCAL_APIC
@@ -165,14 +183,14 @@ static void __init kvm_smp_prepare_boot_cpu(void)
 #ifdef CONFIG_KEXEC
 static void kvm_crash_shutdown(struct pt_regs *regs)
 {
-	native_write_msr_safe(MSR_KVM_SYSTEM_TIME, 0, 0);
+	kvm_turnoff_clock();
 	native_machine_crash_shutdown(regs);
 }
 #endif
 
 static void kvm_shutdown(void)
 {
-	native_write_msr_safe(MSR_KVM_SYSTEM_TIME, 0, 0);
+	kvm_turnoff_clock();
 	native_machine_shutdown();
 }
 
@@ -181,27 +199,35 @@ void __init kvmclock_init(void)
 	if (!kvm_para_available())
 		return;
 
-	if (kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
-		if (kvm_register_clock("boot clock"))
-			return;
-		pv_time_ops.sched_clock = kvm_clock_read;
-		x86_platform.calibrate_tsc = kvm_get_tsc_khz;
-		x86_platform.get_wallclock = kvm_get_wallclock;
-		x86_platform.set_wallclock = kvm_set_wallclock;
+	if (kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2))
+		kvm_use_new_msrs = 1;
+	else if (kvmclock && kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE))
+		kvm_use_new_msrs = 0;
+	else
+		return;
+
+	printk(KERN_INFO "kvm-clock: %ssing clocksource new msrs",
+		kvm_use_new_msrs ? "U": "Not u");
+
+	if (kvm_register_clock("boot clock"))
+		return;
+	pv_time_ops.sched_clock = kvm_clock_read;
+	x86_platform.calibrate_tsc = kvm_get_tsc_khz;
+	x86_platform.get_wallclock = kvm_get_wallclock;
+	x86_platform.set_wallclock = kvm_set_wallclock;
 #ifdef CONFIG_X86_LOCAL_APIC
-		x86_cpuinit.setup_percpu_clockev =
-			kvm_setup_secondary_clock;
+	x86_cpuinit.setup_percpu_clockev =
+		kvm_setup_secondary_clock;
 #endif
 #ifdef CONFIG_SMP
-		smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
+	smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
 #endif
-		machine_ops.shutdown  = kvm_shutdown;
+	machine_ops.shutdown  = kvm_shutdown;
 #ifdef CONFIG_KEXEC
-		machine_ops.crash_shutdown  = kvm_crash_shutdown;
+	machine_ops.crash_shutdown  = kvm_crash_shutdown;
 #endif
-		kvm_get_preset_lpj();
-		clocksource_register(&kvm_clock);
-		pv_info.paravirt_enabled = 1;
-		pv_info.name = "KVM";
-	}
+	kvm_get_preset_lpj();
+	clocksource_register(&kvm_clock);
+	pv_info.paravirt_enabled = 1;
+	pv_info.name = "KVM";
 }
-- 
1.6.2.2


  reply	other threads:[~2010-04-15 18:40 UTC|newest]

Thread overview: 76+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-15 18:37 [PATCH 0/5] pv clock misc fixes Glauber Costa
2010-04-15 18:37 ` [PATCH 1/5] Add a global synchronization point for pvclock Glauber Costa
2010-04-15 18:37   ` [PATCH 2/5] change msr numbers for kvmclock Glauber Costa
2010-04-15 18:37     ` Glauber Costa [this message]
2010-04-15 18:37       ` [PATCH 4/5] export new cpuid KVM_CAP Glauber Costa
2010-04-15 18:37         ` [PATCH 5/5] add documentation about kvmclock Glauber Costa
2010-04-15 19:28           ` Randy Dunlap
2010-04-15 20:10             ` Glauber Costa
2010-04-17 18:58         ` [PATCH 4/5] export new cpuid KVM_CAP Avi Kivity
2010-04-19 14:50           ` Glauber Costa
2010-04-20  9:29             ` Avi Kivity
2010-04-17 18:55       ` [PATCH 3/5] Try using new kvm clock msrs Avi Kivity
2010-04-17 18:51     ` [PATCH 2/5] change msr numbers for kvmclock Avi Kivity
2010-04-16 20:23   ` [PATCH 1/5] Add a global synchronization point for pvclock Marcelo Tosatti
2010-04-16 20:36   ` Jeremy Fitzhardinge
2010-04-16 21:05     ` Zachary Amsden
2010-04-19 10:39     ` Peter Zijlstra
2010-04-19 10:50       ` Avi Kivity
2010-04-19 11:05         ` Peter Zijlstra
2010-04-19 11:10           ` Avi Kivity
2010-04-19 14:21             ` Glauber Costa
2010-04-19 14:33               ` Avi Kivity
2010-04-19 14:46                 ` Peter Zijlstra
2010-04-19 16:18                   ` Jeremy Fitzhardinge
2010-04-20  9:31                     ` Avi Kivity
2010-04-20 18:23                       ` Jeremy Fitzhardinge
2010-04-20 18:54                         ` Avi Kivity
2010-04-20 19:42                           ` Jeremy Fitzhardinge
2010-04-21  0:07                             ` Zachary Amsden
2010-04-22 13:11                             ` Glauber Costa
2010-04-23  1:44                               ` Zachary Amsden
2010-04-23  9:34                                 ` Avi Kivity
2010-04-23 19:22                                   ` Jeremy Fitzhardinge
2010-04-23 19:25                                     ` Avi Kivity
2010-04-23 21:31                                   ` Zachary Amsden
2010-04-23 21:35                                     ` Jeremy Fitzhardinge
2010-04-23 21:41                                       ` Zachary Amsden
2010-04-24  9:30                                         ` Avi Kivity
2010-04-24  9:29                                     ` Avi Kivity
2010-04-19 16:11                 ` Jeremy Fitzhardinge
2010-04-19 14:26     ` Glauber Costa
2010-04-19 16:19       ` Jeremy Fitzhardinge
2010-04-19 18:25         ` Glauber Costa
2010-04-20  1:57           ` Marcelo Tosatti
2010-04-20  9:35             ` Avi Kivity
2010-04-20 12:59               ` Glauber Costa
2010-04-20 15:16                 ` Avi Kivity
2010-04-21  0:01               ` Zachary Amsden
2010-04-21  8:06                 ` Avi Kivity
2010-04-17 18:48   ` Avi Kivity
2010-04-17 18:49     ` Avi Kivity
2010-04-19 10:43       ` Peter Zijlstra
2010-04-19 10:47         ` Avi Kivity
2010-04-19 10:56           ` Peter Zijlstra
2010-04-19 11:13             ` Avi Kivity
2010-04-19 11:19               ` Peter Zijlstra
2010-04-19 11:40                 ` Avi Kivity
2010-04-19 14:32                 ` Glauber Costa
2010-04-19 14:37                   ` Avi Kivity
2010-04-19 10:46     ` Peter Zijlstra
2010-04-19 10:49       ` Avi Kivity
2010-04-19 10:51         ` Peter Zijlstra
2010-04-19 10:54           ` Avi Kivity
2010-04-19 18:35             ` Zachary Amsden
2010-04-20  9:39               ` Avi Kivity
2010-04-21  0:05                 ` Zachary Amsden
2010-04-21  8:08                   ` Avi Kivity
2010-04-19 10:49       ` Peter Zijlstra
2010-04-19 10:53         ` Avi Kivity
2010-04-19 10:59           ` Peter Zijlstra
2010-04-19 11:35             ` Avi Kivity
2010-10-25 23:30   ` Jeremy Fitzhardinge
2010-10-25 23:30     ` Jeremy Fitzhardinge
2010-10-26  8:14     ` Avi Kivity
2010-10-26 10:49       ` Glauber Costa
2010-10-26 17:04       ` Jeremy Fitzhardinge

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1271356648-5108-4-git-send-email-glommer@redhat.com \
    --to=glommer@redhat.com \
    --cc=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.