Subject: [PATCH 13/13] kvmclock: Ensure proper env->tsc value for kvmclock_current_nsec calculation

From: Marcelo Tosatti <[email-masked]>
Date: Thu, 26 Jun 2014 15:06:15 +0200

RH-Author: Marcelo Tosatti <[email-masked]>
Message-id: <[email-masked]>
Patchwork-id: 59386
O-Subject: [RHEL-7.1 qemu-kvm PATCH 2/2] kvmclock: Ensure proper env->tsc value for kvmclock_current_nsec calculation
Bugzilla: 1098602
RH-Acked-by: Paolo Bonzini <[email-masked]>
RH-Acked-by: Eduardo Habkost <[email-masked]>
RH-Acked-by: Andrew Jones <[email-masked]>

commit 9b1786829aefb83f37a8f3135e3ea91c56001b56 upstream

Ensure proper env->tsc value for kvmclock_current_nsec calculation.

Reported-by: Marcin Gibuła <[email-masked]>
Cc: [email-masked]
Signed-off-by: Marcelo Tosatti <[email-masked]>
Signed-off-by: Paolo Bonzini <[email-masked]>

BZ: 1098602

---
 hw/i386/kvm/clock.c |    4 ++++
 1 file changed, 4 insertions(+)

Signed-off-by: Miroslav Rezanina <[email-masked]>
---
 hw/i386/kvm/clock.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/hw/i386/kvm/clock.c b/hw/i386/kvm/clock.c
index 1f2a26e..d52c2ea 100644
--- a/hw/i386/kvm/clock.c
+++ b/hw/i386/kvm/clock.c
@@ -17,6 +17,7 @@
 #include "qemu/host-utils.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/kvm.h"
+#include "sysemu/cpus.h"
 #include "hw/sysbus.h"
 #include "hw/kvm/clock.h"
 
@@ -60,6 +61,7 @@ static uint64_t kvmclock_current_nsec(KVMClockState *s)
 
     cpu_physical_memory_read(kvmclock_struct_pa, &time, sizeof(time));
 
+    assert(time.tsc_timestamp <= migration_tsc);
     delta = migration_tsc - time.tsc_timestamp;
     if (time.tsc_shift < 0) {
         delta >>= -time.tsc_shift;
@@ -118,6 +120,8 @@ static void kvmclock_vm_state_change(void *opaque, int running,
         if (s->clock_valid) {
             return;
         }
+
+        cpu_synchronize_all_states();
         ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
         if (ret < 0) {
             fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
-- 
1.7.1