public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] disable clock before rebooting.
@ 2008-03-06 16:14 Glauber Costa
  2008-03-07  9:10 ` Avi Kivity
  2008-03-07  9:15 ` Avi Kivity
  0 siblings, 2 replies; 8+ messages in thread
From: Glauber Costa @ 2008-03-06 16:14 UTC (permalink / raw)
  To: kvm-devel; +Cc: avi, Glauber Costa

This patch writes 0 (actually, what really matters is that the
LSB is cleared) to the system time msr before rebooting/shutting down
the machine.

Without it, we can have a random memory location being written
when the guest comes back

Signed-off-by: Glauber Costa <gcosta@redhat.com>
---
 arch/x86/kernel/kvmclock.c |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)

diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c
index f654a12..5c9ff8d 100644
--- a/arch/x86/kernel/kvmclock.c
+++ b/arch/x86/kernel/kvmclock.c
@@ -21,6 +21,7 @@ #include <linux/kvm_para.h>
 #include <asm/arch_hooks.h>
 #include <asm/msr.h>
 #include <linux/percpu.h>
+#include <asm/reboot.h>
 
 #define KVM_SCALE 22
 
@@ -142,6 +143,32 @@ static void kvm_setup_secondary_clock(vo
 	setup_secondary_APIC_clock();
 }
 
+/*
+ * After the clock is registered, the host will keep writing to the
+ * registered memory location. If the guest happens to shutdown, or restart,
+ * this memory won't be valid. In cases like kexec, in which you install a new kernel,
+ * this will mean a random memory location will be kept being written. So before
+ * any kind of shutdown from our side, we unregister the clock by writting anything
+ * that does not have the 'enable' bit set in the msr
+ */ 
+static void kvm_restart(char *unused) {
+	native_write_msr_safe(MSR_KVM_SYSTEM_TIME, 0, 0);
+	native_machine_restart(unused);
+}
+
+/* Forgive me dear lord, for my laziness */
+#define kvm_reboot_fn(x) \
+static void kvm_##x(void) { \
+	native_write_msr_safe(MSR_KVM_SYSTEM_TIME, 0, 0); \
+	native_machine_##x(); \
+}
+
+kvm_reboot_fn(emergency_restart)
+kvm_reboot_fn(shutdown)
+kvm_reboot_fn(halt)
+kvm_reboot_fn(power_off)
+#undef kvm_reboot_fn
+
 void __init kvmclock_init(void)
 {
 	if (!kvm_para_available())
@@ -154,6 +181,11 @@ void __init kvmclock_init(void)
 		pv_time_ops.set_wallclock = kvm_set_wallclock;
 		pv_time_ops.sched_clock = kvm_clock_read;
 		pv_apic_ops.setup_secondary_clock = kvm_setup_secondary_clock;
+		machine_ops.emergency_restart = kvm_emergency_restart;
+		machine_ops.shutdown  = kvm_shutdown;
+		machine_ops.restart  = kvm_restart;
+		machine_ops.halt  = kvm_halt;
+		machine_ops.power_off  = kvm_power_off;
 		clocksource_register(&kvm_clock);
 	}
 }
-- 
1.4.2


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

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-03-07 16:10 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-03-06 16:14 [PATCH] disable clock before rebooting Glauber Costa
2008-03-07  9:10 ` Avi Kivity
2008-03-07 14:04   ` Glauber Costa
2008-03-07 15:55     ` Avi Kivity
2008-03-07  9:15 ` Avi Kivity
2008-03-07 14:23   ` Glauber Costa
2008-03-07 16:07     ` Avi Kivity
2008-03-07 16:10       ` Glauber Costa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox