Skip to contentSkip to conversation

spapr: support time base offset migration

RFC PATCH Superseded Alexey Kardashevskiy 5 participants 31 messages Sep 3, 2013 – Sep 13, 2013

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 spapr: support time base offset migration

This allows guests to have a different timebase origin from the host.

This is needed for migration, where a guest can migrate from one host to another and the two hosts might have a different timebase origin. However, the timebase seen by the guest must not go backwards, and should go forwards only by a small amount corresponding to the time taken for the migration.

This is only supported for recent POWER hardware which has the TBU40 (timebase upper 40 bits) register. That includes POWER6, 7, 8 but not 970.

This adds kvm_access_one_reg() to access a special register which is not in env->spr.

The feature must be present in the host kernel.

---

This is an RFC but not a final patch. Can break something but I just do not see what.

---
 hw/ppc/ppc.c         | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/ppc.h |  4 ++++
 target-ppc/kvm.c     | 23 +++++++++++++++++++++++
 target-ppc/machine.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 trace-events         |  3 +++
 5 files changed, 123 insertions(+)
hw/ppc/ppc.c +49 -0
index 1e3cab3..7d08c9a 100644
@@ -31,6 +31,7 @@
 #include "hw/loader.h"
 #include "sysemu/kvm.h"
 #include "kvm_ppc.h"
+#include "trace.h"
 
 //#define PPC_DEBUG_IRQ
 #define PPC_DEBUG_TB
@@ -796,6 +797,54 @@ static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
     cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
 }
 
+/*
+ * Calculate timebase on the destination side of migration
+ *
+ * We calculate new timebase offset as shown below:
+ * 1) Gtb2 = Gtb1 + max(tod2 - tod1, 0)
+ *    Gtb2 = tb2 + off2
+ *    Gtb1 = tb1 + off1
+ * 2) tb2 + off2 = tb1 + off1 + max(tod2 - tod1, 0)
+ * 3) off2 = tb1 - tb2 + off1 + max(tod2 - tod1, 0)
+ *
+ * where:
+ * Gtb2 - destination guest timebase
+ * tb2 - destination host timebase
+ * off2 - destination timebase offset
+ * tod2 - destination time of the day
+ * Gtb1 - source guest timebase
+ * tb1 - source host timebase
+ * off1 - source timebase offset
+ * tod1 - source time of the day
+ *
+ * The result we want is in @off2
+ *
+ * Two conditions must be met for @off2:
+ * 1) off2 must be multiple of 2^24 ticks as it will be set via TBU40 SPR
+ * 2) Gtb2 >= Gtb1
+ */
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env)
+{
+    uint64_t tb2, tod2, off2;
+    int ratio = tb_env->tb_freq / 1000000;
+    struct timeval tv;
+
+    tb2 = cpu_get_real_ticks();
+    gettimeofday(&tv, NULL);
+    tod2 = tv.tv_sec * 1000000 + tv.tv_usec;
+
+    off2 = tb_env->timebase - tb2 + tb_env->tb_offset;
+    if (tod2 > tb_env->time_of_the_day) {
+        off2 += (tod2 - tb_env->time_of_the_day) * ratio;
+    }
+    off2 = ROUND_UP(off2, 1 << 24);
+
+    trace_ppc_tb_adjust(tb_env->tb_offset, off2,
+                        (int64_t)off2 - tb_env->tb_offset);
+
+    tb_env->tb_offset = off2;
+}
+
 /* Set up (once) timebase frequency (in Hz) */
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)
 {
include/hw/ppc/ppc.h +4 -0
index 132ab97..235871c 100644
@@ -32,6 +32,9 @@ struct ppc_tb_t {
     uint64_t purr_start;
     void *opaque;
     uint32_t flags;
+    /* Cached values for live migration purposes */
+    uint64_t timebase;
+    uint64_t time_of_the_day;
 };
 
 /* PPC Timers flags */
@@ -46,6 +49,7 @@ struct ppc_tb_t {
                                                */
 
 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset);
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env);
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq);
 /* Embedded PowerPC DCR management */
 typedef uint32_t (*dcr_read_cb)(void *opaque, int dcrn);
target-ppc/kvm.c +23 -0
index 7af9e3d..93df955 100644
@@ -35,6 +35,7 @@
 #include "hw/sysbus.h"
 #include "hw/ppc/spapr.h"
 #include "hw/ppc/spapr_vio.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/watchdog.h"
 
 //#define DEBUG_KVM
@@ -761,6 +762,22 @@ static int kvm_put_vpa(CPUState *cs)
 }
 #endif /* TARGET_PPC64 */
 
+static int kvm_access_one_reg(CPUState *cs, bool set, __u64 id, void *addr)
+{
+    struct kvm_one_reg reg = {
+        .id = id,
+        .addr = (uintptr_t)addr,
+    };
+    int ret = kvm_vcpu_ioctl(cs, set ? KVM_SET_ONE_REG : KVM_GET_ONE_REG, &reg);
+
+    if (ret) {
+        DPRINTF("Unable to %s time base offset to KVM: %s\n",
+                set ? "set" : "get", strerror(errno));
+    }
+
+    return ret;
+}
+
 int kvm_arch_put_registers(CPUState *cs, int level)
 {
     PowerPCCPU *cpu = POWERPC_CPU(cs);
@@ -873,6 +890,9 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                 DPRINTF("Warning: Unable to set VPA information to KVM\n");
             }
         }
+
+        kvm_access_one_reg(cs, 1, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif /* TARGET_PPC64 */
     }
 
@@ -1082,6 +1102,9 @@ int kvm_arch_get_registers(CPUState *cs)
                 DPRINTF("Warning: Unable to get VPA information from KVM\n");
             }
         }
+
+        kvm_access_one_reg(cs, 0, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif
     }
 
target-ppc/machine.c +44 -0

61 lines not on this page · raw message

trace-events +3 -0
index 935b953..24cf4d2 100644
@@ -1141,6 +1141,9 @@ spapr_iommu_stuff(uint64_t liobn, uint64_t ioba, uint64_t tce, uint64_t ret) "li
 spapr_iommu_xlate(uint64_t liobn, uint64_t ioba, uint64_t tce, unsigned perm, unsigned pgsize) "liobn=%"PRIx64" 0x%"PRIx64" -> 0x%"PRIx64" perm=%u mask=%x"
 spapr_iommu_new_table(uint64_t liobn, void *tcet, void *table, int fd) "liobn=%"PRIx64" tcet=%p table=%p fd=%d"
 
+# hw/ppc/ppc.c
+ppc_tb_adjust(uint64_t offs1, uint64_t offs2, int64_t diff) "adjusted from %"PRIx64" to %"PRIx64", diff %"PRId64
+
 # util/hbitmap.c
 hbitmap_iter_skip_words(const void *hb, void *hbi, uint64_t pos, unsigned long cur) "hb %p hbi %p pos %"PRId64" cur 0x%lx"
 hbitmap_reset(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %"PRIu64",%"PRIu64" bits %"PRIu64"..%"PRIu64
Signature · 1 line
1.8.4.rc4
#1 lore raw

Am 03.09.2013 09:31, schrieb Alexey Kardashevskiy:

Quoted 232 lines

This allows guests to have a different timebase origin from the host.

This is needed for migration, where a guest can migrate from one host to another and the two hosts might have a different timebase origin. However, the timebase seen by the guest must not go backwards, and should go forwards only by a small amount corresponding to the time taken for the migration.

This is only supported for recent POWER hardware which has the TBU40 (timebase upper 40 bits) register. That includes POWER6, 7, 8 but not 970.

This adds kvm_access_one_reg() to access a special register which is not in env->spr.

The feature must be present in the host kernel.

---

This is an RFC but not a final patch. Can break something but I just do not see what.

---
 hw/ppc/ppc.c         | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/ppc.h |  4 ++++
 target-ppc/kvm.c     | 23 +++++++++++++++++++++++
 target-ppc/machine.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 trace-events         |  3 +++
 5 files changed, 123 insertions(+)
hw/ppc/ppc.c +49 -0
index 1e3cab3..7d08c9a 100644
@@ -31,6 +31,7 @@
 #include "hw/loader.h"
 #include "sysemu/kvm.h"
 #include "kvm_ppc.h"
+#include "trace.h"
 
 //#define PPC_DEBUG_IRQ
 #define PPC_DEBUG_TB
@@ -796,6 +797,54 @@ static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
     cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
 }
 
+/*
+ * Calculate timebase on the destination side of migration
+ *
+ * We calculate new timebase offset as shown below:
+ * 1) Gtb2 = Gtb1 + max(tod2 - tod1, 0)
+ *    Gtb2 = tb2 + off2
+ *    Gtb1 = tb1 + off1
+ * 2) tb2 + off2 = tb1 + off1 + max(tod2 - tod1, 0)
+ * 3) off2 = tb1 - tb2 + off1 + max(tod2 - tod1, 0)
+ *
+ * where:
+ * Gtb2 - destination guest timebase
+ * tb2 - destination host timebase
+ * off2 - destination timebase offset
+ * tod2 - destination time of the day
+ * Gtb1 - source guest timebase
+ * tb1 - source host timebase
+ * off1 - source timebase offset
+ * tod1 - source time of the day
+ *
+ * The result we want is in @off2
+ *
+ * Two conditions must be met for @off2:
+ * 1) off2 must be multiple of 2^24 ticks as it will be set via TBU40 SPR
+ * 2) Gtb2 >= Gtb1
+ */
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env)
+{
+    uint64_t tb2, tod2, off2;
+    int ratio = tb_env->tb_freq / 1000000;
+    struct timeval tv;
+
+    tb2 = cpu_get_real_ticks();
+    gettimeofday(&tv, NULL);
+    tod2 = tv.tv_sec * 1000000 + tv.tv_usec;
+
+    off2 = tb_env->timebase - tb2 + tb_env->tb_offset;
+    if (tod2 > tb_env->time_of_the_day) {
+        off2 += (tod2 - tb_env->time_of_the_day) * ratio;
+    }
+    off2 = ROUND_UP(off2, 1 << 24);
+
+    trace_ppc_tb_adjust(tb_env->tb_offset, off2,
+                        (int64_t)off2 - tb_env->tb_offset);
+
+    tb_env->tb_offset = off2;
+}
+
 /* Set up (once) timebase frequency (in Hz) */
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)
 {
include/hw/ppc/ppc.h +4 -0
index 132ab97..235871c 100644
@@ -32,6 +32,9 @@ struct ppc_tb_t {
     uint64_t purr_start;
     void *opaque;
     uint32_t flags;
+    /* Cached values for live migration purposes */
+    uint64_t timebase;
+    uint64_t time_of_the_day;
 };
 
 /* PPC Timers flags */
@@ -46,6 +49,7 @@ struct ppc_tb_t {
                                                */
 
 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset);
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env);
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq);
 /* Embedded PowerPC DCR management */
 typedef uint32_t (*dcr_read_cb)(void *opaque, int dcrn);
target-ppc/kvm.c +23 -0
index 7af9e3d..93df955 100644
@@ -35,6 +35,7 @@
 #include "hw/sysbus.h"
 #include "hw/ppc/spapr.h"
 #include "hw/ppc/spapr_vio.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/watchdog.h"
 
 //#define DEBUG_KVM
@@ -761,6 +762,22 @@ static int kvm_put_vpa(CPUState *cs)
 }
 #endif /* TARGET_PPC64 */
 
+static int kvm_access_one_reg(CPUState *cs, bool set, __u64 id, void *addr)
+{
+    struct kvm_one_reg reg = {
+        .id = id,
+        .addr = (uintptr_t)addr,
+    };
+    int ret = kvm_vcpu_ioctl(cs, set ? KVM_SET_ONE_REG : KVM_GET_ONE_REG, &reg);
+
+    if (ret) {
+        DPRINTF("Unable to %s time base offset to KVM: %s\n",
+                set ? "set" : "get", strerror(errno));
+    }
+
+    return ret;
+}
+
 int kvm_arch_put_registers(CPUState *cs, int level)
 {
     PowerPCCPU *cpu = POWERPC_CPU(cs);
@@ -873,6 +890,9 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                 DPRINTF("Warning: Unable to set VPA information to KVM\n");
             }
         }
+
+        kvm_access_one_reg(cs, 1, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif /* TARGET_PPC64 */
     }
 
@@ -1082,6 +1102,9 @@ int kvm_arch_get_registers(CPUState *cs)
                 DPRINTF("Warning: Unable to get VPA information from KVM\n");
             }
         }
+
+        kvm_access_one_reg(cs, 0, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif
     }
 
target-ppc/machine.c +40 -0
index 12e1512..d1ffc7f 100644
@@ -1,5 +1,6 @@
 #include "hw/hw.h"
 #include "hw/boards.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/kvm.h"
 #include "helper_regs.h"
 
@@ -459,6 +460,45 @@ static const VMStateDescription vmstate_tlbmas = {
     }
 };
 
+static void timebase_pre_save(void *opaque)
+{
+    ppc_tb_t *tb_env = opaque;
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+    tb_env->time_of_the_day = tv.tv_sec * 1000000 + tv.tv_usec;
+    tb_env->timebase = cpu_get_real_ticks();
+}
+
+static int timebase_post_load(void *opaque, int version_id)
+{
+    ppc_tb_t *tb_env = opaque;
+
+    if (!tb_env) {
+        printf("NO TB!\n");
+        return -1;
+    }
+    cpu_ppc_adjust_tb_offset(tb_env);
+
+    return 0;
+}
+
+static const VMStateDescription vmstate_timebase = {
+    .name = "cpu/timebase",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .pre_save = timebase_pre_save,
+    .post_load = timebase_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(timebase, ppc_tb_t),
+        VMSTATE_INT64(tb_offset, ppc_tb_t),
+        VMSTATE_UINT64(time_of_the_day, ppc_tb_t),
+        VMSTATE_UINT32_EQUAL(tb_freq, ppc_tb_t),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 const VMStateDescription vmstate_ppc_cpu = {
     .name = "cpu",
     .version_id = 5,

target-ppc/machine.c const VMStateDescription vmstate_ppc_cpu = { · line 547

Quoted 10 lines
target-ppc/machine.c +4 -0
@@ -498,6 +538,10 @@ const VMStateDescription vmstate_ppc_cpu = {
         VMSTATE_UINT64_EQUAL(env.insns_flags, PowerPCCPU),
         VMSTATE_UINT64_EQUAL(env.insns_flags2, PowerPCCPU),
         VMSTATE_UINT32_EQUAL(env.nb_BATs, PowerPCCPU),
+
+        /* Time offset */
+        VMSTATE_STRUCT_POINTER(env.tb_env, PowerPCCPU,
+                               vmstate_timebase, ppc_tb_t *),
         VMSTATE_END_OF_LIST()
     },
     .subsections = (VMStateSubsection []) {

Breaks the migration format. ;) You need to bump version_id and use a macro that accepts the version the field was added in as argument.

Andreas

Quoted 15 lines
trace-events +3 -0
index 935b953..24cf4d2 100644
@@ -1141,6 +1141,9 @@ spapr_iommu_stuff(uint64_t liobn, uint64_t ioba, uint64_t tce, uint64_t ret) "li
 spapr_iommu_xlate(uint64_t liobn, uint64_t ioba, uint64_t tce, unsigned perm, unsigned pgsize) "liobn=%"PRIx64" 0x%"PRIx64" -> 0x%"PRIx64" perm=%u mask=%x"
 spapr_iommu_new_table(uint64_t liobn, void *tcet, void *table, int fd) "liobn=%"PRIx64" tcet=%p table=%p fd=%d"
 
+# hw/ppc/ppc.c
+ppc_tb_adjust(uint64_t offs1, uint64_t offs2, int64_t diff) "adjusted from %"PRIx64" to %"PRIx64", diff %"PRId64
+
 # util/hbitmap.c
 hbitmap_iter_skip_words(const void *hb, void *hbi, uint64_t pos, unsigned long cur) "hb %p hbi %p pos %"PRId64" cur 0x%lx"
 hbitmap_reset(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %"PRIu64",%"PRIu64" bits %"PRIu64"..%"PRIu64
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#2 lore raw
Quoted 238 lines · Andreas Färber

Am 03.09.2013 09:31, schrieb Alexey Kardashevskiy:

Quoted 237 lines

This allows guests to have a different timebase origin from the host.

This is needed for migration, where a guest can migrate from one host to another and the two hosts might have a different timebase origin. However, the timebase seen by the guest must not go backwards, and should go forwards only by a small amount corresponding to the time taken for the migration.

This is only supported for recent POWER hardware which has the TBU40 (timebase upper 40 bits) register. That includes POWER6, 7, 8 but not 970.

This adds kvm_access_one_reg() to access a special register which is not in env->spr.

The feature must be present in the host kernel.

---

This is an RFC but not a final patch. Can break something but I just do not see what.

---
 hw/ppc/ppc.c         | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/ppc.h |  4 ++++
 target-ppc/kvm.c     | 23 +++++++++++++++++++++++
 target-ppc/machine.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 trace-events         |  3 +++
 5 files changed, 123 insertions(+)
hw/ppc/ppc.c +49 -0
index 1e3cab3..7d08c9a 100644
@@ -31,6 +31,7 @@
 #include "hw/loader.h"
 #include "sysemu/kvm.h"
 #include "kvm_ppc.h"
+#include "trace.h"
 
 //#define PPC_DEBUG_IRQ
 #define PPC_DEBUG_TB
@@ -796,6 +797,54 @@ static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
     cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
 }
 
+/*
+ * Calculate timebase on the destination side of migration
+ *
+ * We calculate new timebase offset as shown below:
+ * 1) Gtb2 = Gtb1 + max(tod2 - tod1, 0)
+ *    Gtb2 = tb2 + off2
+ *    Gtb1 = tb1 + off1
+ * 2) tb2 + off2 = tb1 + off1 + max(tod2 - tod1, 0)
+ * 3) off2 = tb1 - tb2 + off1 + max(tod2 - tod1, 0)
+ *
+ * where:
+ * Gtb2 - destination guest timebase
+ * tb2 - destination host timebase
+ * off2 - destination timebase offset
+ * tod2 - destination time of the day
+ * Gtb1 - source guest timebase
+ * tb1 - source host timebase
+ * off1 - source timebase offset
+ * tod1 - source time of the day
+ *
+ * The result we want is in @off2
+ *
+ * Two conditions must be met for @off2:
+ * 1) off2 must be multiple of 2^24 ticks as it will be set via TBU40 SPR
+ * 2) Gtb2 >= Gtb1
+ */
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env)
+{
+    uint64_t tb2, tod2, off2;
+    int ratio = tb_env->tb_freq / 1000000;
+    struct timeval tv;
+
+    tb2 = cpu_get_real_ticks();
+    gettimeofday(&tv, NULL);
+    tod2 = tv.tv_sec * 1000000 + tv.tv_usec;
+
+    off2 = tb_env->timebase - tb2 + tb_env->tb_offset;
+    if (tod2 > tb_env->time_of_the_day) {
+        off2 += (tod2 - tb_env->time_of_the_day) * ratio;
+    }
+    off2 = ROUND_UP(off2, 1 << 24);
+
+    trace_ppc_tb_adjust(tb_env->tb_offset, off2,
+                        (int64_t)off2 - tb_env->tb_offset);
+
+    tb_env->tb_offset = off2;
+}
+
 /* Set up (once) timebase frequency (in Hz) */
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)
 {
include/hw/ppc/ppc.h +4 -0
index 132ab97..235871c 100644
@@ -32,6 +32,9 @@ struct ppc_tb_t {
     uint64_t purr_start;
     void *opaque;
     uint32_t flags;
+    /* Cached values for live migration purposes */
+    uint64_t timebase;
+    uint64_t time_of_the_day;
 };
 
 /* PPC Timers flags */
@@ -46,6 +49,7 @@ struct ppc_tb_t {
                                                */
 
 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset);
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env);
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq);
 /* Embedded PowerPC DCR management */
 typedef uint32_t (*dcr_read_cb)(void *opaque, int dcrn);
target-ppc/kvm.c +23 -0
index 7af9e3d..93df955 100644
@@ -35,6 +35,7 @@
 #include "hw/sysbus.h"
 #include "hw/ppc/spapr.h"
 #include "hw/ppc/spapr_vio.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/watchdog.h"
 
 //#define DEBUG_KVM
@@ -761,6 +762,22 @@ static int kvm_put_vpa(CPUState *cs)
 }
 #endif /* TARGET_PPC64 */
 
+static int kvm_access_one_reg(CPUState *cs, bool set, __u64 id, void *addr)
+{
+    struct kvm_one_reg reg = {
+        .id = id,
+        .addr = (uintptr_t)addr,
+    };
+    int ret = kvm_vcpu_ioctl(cs, set ? KVM_SET_ONE_REG : KVM_GET_ONE_REG, &reg);
+
+    if (ret) {
+        DPRINTF("Unable to %s time base offset to KVM: %s\n",
+                set ? "set" : "get", strerror(errno));
+    }
+
+    return ret;
+}
+
 int kvm_arch_put_registers(CPUState *cs, int level)
 {
     PowerPCCPU *cpu = POWERPC_CPU(cs);
@@ -873,6 +890,9 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                 DPRINTF("Warning: Unable to set VPA information to KVM\n");
             }
         }
+
+        kvm_access_one_reg(cs, 1, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif /* TARGET_PPC64 */
     }
 
@@ -1082,6 +1102,9 @@ int kvm_arch_get_registers(CPUState *cs)
                 DPRINTF("Warning: Unable to get VPA information from KVM\n");
             }
         }
+
+        kvm_access_one_reg(cs, 0, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif
     }
 
target-ppc/machine.c +42 -0
index 12e1512..d1ffc7f 100644
@@ -1,5 +1,6 @@
 #include "hw/hw.h"
 #include "hw/boards.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/kvm.h"
 #include "helper_regs.h"
 
@@ -459,6 +460,45 @@ static const VMStateDescription vmstate_tlbmas = {
     }
 };
 
+static void timebase_pre_save(void *opaque)
+{
+    ppc_tb_t *tb_env = opaque;
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+    tb_env->time_of_the_day = tv.tv_sec * 1000000 + tv.tv_usec;
+    tb_env->timebase = cpu_get_real_ticks();
+}
+
+static int timebase_post_load(void *opaque, int version_id)
+{
+    ppc_tb_t *tb_env = opaque;
+
+    if (!tb_env) {
+        printf("NO TB!\n");
+        return -1;
+    }
+    cpu_ppc_adjust_tb_offset(tb_env);
+
+    return 0;
+}
+
+static const VMStateDescription vmstate_timebase = {
+    .name = "cpu/timebase",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .pre_save = timebase_pre_save,
+    .post_load = timebase_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(timebase, ppc_tb_t),
+        VMSTATE_INT64(tb_offset, ppc_tb_t),
+        VMSTATE_UINT64(time_of_the_day, ppc_tb_t),
+        VMSTATE_UINT32_EQUAL(tb_freq, ppc_tb_t),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 const VMStateDescription vmstate_ppc_cpu = {
     .name = "cpu",
     .version_id = 5,
@@ -498,6 +538,10 @@ const VMStateDescription vmstate_ppc_cpu = {
         VMSTATE_UINT64_EQUAL(env.insns_flags, PowerPCCPU),
         VMSTATE_UINT64_EQUAL(env.insns_flags2, PowerPCCPU),
         VMSTATE_UINT32_EQUAL(env.nb_BATs, PowerPCCPU),
+
+        /* Time offset */
Quoted 8 lines

target-ppc/machine.c const VMStateDescription vmstate_ppc_cpu = { · line 547

Quoted 5 lines
target-ppc/machine.c +2 -0
+        VMSTATE_STRUCT_POINTER(env.tb_env, PowerPCCPU,
+                               vmstate_timebase, ppc_tb_t *),
         VMSTATE_END_OF_LIST()
     },
     .subsections = (VMStateSubsection []) {

Breaks the migration format. ;) You need to bump version_id and use a macro that accepts the version the field was added in as argument.

Risking of being called ignorant, I'll still ask - is the patch below what you mean? I could not find VMSTATE_STRUCT_POINTER_V and I do not believe it is not there already.

btw why would it break? Just asking. Is it because the source may send what the destination cannot handle? Named fields should stop the migration the same way as version mismatch would have done.
Or the source won't sent what the destination expects and we do not want this destination guest to continue?
Once I was told that migration between different versions of QEMU is not supported - so what is the point of .version_id field at all?

Thanks!

alexey@ka1:~/pcipassthru/qemu$ git diff
include/migration/vmstate.h +9 -0
index 1c31b5d..7b624bf 100644
@@ -499,6 +499,15 @@ extern const VMStateInfo vmstate_info_bitmap;
 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
     VMSTATE_STRUCT_POINTER_TEST(_field, _state, NULL, _vmsd, _type)
 
+#define VMSTATE_STRUCT_POINTER_V(_field, _state,  _vmsd, _type, _version) { \
+    .name         = (stringify(_field)),                             \
+    .version_id = (_version),                                        \
+    .vmsd         = &(_vmsd),                                        \
+    .size         = sizeof(_type),                                   \
+    .flags        = VMS_STRUCT|VMS_POINTER,                          \
+    .offset       = vmstate_offset_value(_state, _field, _type),     \
+}
+
 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
     VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version,   \
             _vmsd, _type)
target-ppc/machine.c +3 -3
index b4f447c..f79f38e 100644
@@ -501,7 +501,7 @@ static const VMStateDescription vmstate_timebase = {
 
 const VMStateDescription vmstate_ppc_cpu = {
     .name = "cpu",
-    .version_id = 5,
+    .version_id = 6,
     .minimum_version_id = 5,
     .minimum_version_id_old = 4,
     .load_state_old = cpu_load_old,
@@ -540,8 +540,8 @@ const VMStateDescription vmstate_ppc_cpu = {
         VMSTATE_UINT32_EQUAL(env.nb_BATs, PowerPCCPU),
 
         /* Time offset */
-        VMSTATE_STRUCT_POINTER(env.tb_env, PowerPCCPU,
-                               vmstate_timebase, ppc_tb_t *),
+        VMSTATE_STRUCT_POINTER_V(env.tb_env, PowerPCCPU,
+                                 vmstate_timebase, ppc_tb_t *, 6),
         VMSTATE_END_OF_LIST()
     },
     .subsections = (VMStateSubsection []) {
Signature · 1 line
Alexey
#3 lore raw

Am 03.09.2013 11:07, schrieb Alexey Kardashevskiy:

Quoted 6 lines · Andreas Färber
Quoted 5 lines

Am 03.09.2013 09:31, schrieb Alexey Kardashevskiy:

Quoted 4 lines
target-ppc/machine.c +0 -0
index 12e1512..d1ffc7f 100644

[...]

Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
target-ppc/machine.c +18 -0
+static const VMStateDescription vmstate_timebase = {
+    .name = "cpu/timebase",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .pre_save = timebase_pre_save,
+    .post_load = timebase_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(timebase, ppc_tb_t),
+        VMSTATE_INT64(tb_offset, ppc_tb_t),
+        VMSTATE_UINT64(time_of_the_day, ppc_tb_t),
+        VMSTATE_UINT32_EQUAL(tb_freq, ppc_tb_t),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 const VMStateDescription vmstate_ppc_cpu = {
     .name = "cpu",
     .version_id = 5,
@@ -498,6 +538,10 @@ const VMStateDescription vmstate_ppc_cpu = {
         VMSTATE_UINT64_EQUAL(env.insns_flags, PowerPCCPU),
         VMSTATE_UINT64_EQUAL(env.insns_flags2, PowerPCCPU),
         VMSTATE_UINT32_EQUAL(env.nb_BATs, PowerPCCPU),
+
+        /* Time offset */
Quoted 8 lines

target-ppc/machine.c const VMStateDescription vmstate_ppc_cpu = { · line 547

Quoted 5 lines
target-ppc/machine.c +2 -0
+        VMSTATE_STRUCT_POINTER(env.tb_env, PowerPCCPU,
+                               vmstate_timebase, ppc_tb_t *),
         VMSTATE_END_OF_LIST()
     },
     .subsections = (VMStateSubsection []) {

Breaks the migration format. ;) You need to bump version_id and use a macro that accepts the version the field was added in as argument.

Quoted 3 lines

Risking of being called ignorant, I'll still ask - is the patch below what you mean? I could not find VMSTATE_STRUCT_POINTER_V and I do not believe it is not there already.

Usually the way we do it is to have VMSTATE_STRUCT_POINTER() call VMSTATE_STRUCT_POINTER_V() and thus VMSTATE_STRUCT_POINTER_TEST() call a new VMSTATE_STRUCT_POINTER_TEST_V(), to avoid code duplication of the core array entry. CC'ing Juan. Please do the VMState preparation in a separate patch.

ppc usage looks fine.

Quoted 3 lines

btw why would it break? Just asking. Is it because the source may send what the destination cannot handle? Named fields should stop the migration the same way as version mismatch would have done.

Nope, field names do not get transmitted, only the section names. (Otherwise random code refactorings could break the migration format.)

Quoted 2 lines

Or the source won't sent what the destination expects and we do not want this destination guest to continue?

There's an incoming stream of data from either live migration or a file, and QEMU must decide whether it can read and how to interpret the raw bytestream. It shouldn't just read random bytes into a new field when they were written from a different field.

Quoted 2 lines

Once I was told that migration between different versions of QEMU is not supported - so what is the point of .version_id field at all?

Not sure who told such a thing and in what context. On x86 we try to avoid version_id bumps by adding subsections to allow migration in both ways (including from newer to older) but for ppc, arm and all others we do require that new fields are marked as such. Whether migration is officially supported is a different matter from the VMState wire format.

Regards,
Andreas

Quoted 45 lines
alexey@ka1:~/pcipassthru/qemu$ git diff
include/migration/vmstate.h +9 -0
index 1c31b5d..7b624bf 100644
@@ -499,6 +499,15 @@ extern const VMStateInfo vmstate_info_bitmap;
 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
     VMSTATE_STRUCT_POINTER_TEST(_field, _state, NULL, _vmsd, _type)
 
+#define VMSTATE_STRUCT_POINTER_V(_field, _state,  _vmsd, _type, _version) { \
+    .name         = (stringify(_field)),                             \
+    .version_id = (_version),                                        \
+    .vmsd         = &(_vmsd),                                        \
+    .size         = sizeof(_type),                                   \
+    .flags        = VMS_STRUCT|VMS_POINTER,                          \
+    .offset       = vmstate_offset_value(_state, _field, _type),     \
+}
+
 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
     VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version,   \
             _vmsd, _type)
target-ppc/machine.c +3 -3
index b4f447c..f79f38e 100644
@@ -501,7 +501,7 @@ static const VMStateDescription vmstate_timebase = {
 
 const VMStateDescription vmstate_ppc_cpu = {
     .name = "cpu",
-    .version_id = 5,
+    .version_id = 6,
     .minimum_version_id = 5,
     .minimum_version_id_old = 4,
     .load_state_old = cpu_load_old,
@@ -540,8 +540,8 @@ const VMStateDescription vmstate_ppc_cpu = {
         VMSTATE_UINT32_EQUAL(env.nb_BATs, PowerPCCPU),
 
         /* Time offset */
-        VMSTATE_STRUCT_POINTER(env.tb_env, PowerPCCPU,
-                               vmstate_timebase, ppc_tb_t *),
+        VMSTATE_STRUCT_POINTER_V(env.tb_env, PowerPCCPU,
+                                 vmstate_timebase, ppc_tb_t *, 6),
         VMSTATE_END_OF_LIST()
     },
     .subsections = (VMStateSubsection []) {
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#4 lore raw
Quoted 69 lines · Andreas Färber

Am 03.09.2013 11:07, schrieb Alexey Kardashevskiy:

Quoted 6 lines · Andreas Färber
Quoted 5 lines

Am 03.09.2013 09:31, schrieb Alexey Kardashevskiy:

Quoted 4 lines
target-ppc/machine.c +0 -0
index 12e1512..d1ffc7f 100644

[...]

Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
target-ppc/machine.c +18 -0
+static const VMStateDescription vmstate_timebase = {
+    .name = "cpu/timebase",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .pre_save = timebase_pre_save,
+    .post_load = timebase_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(timebase, ppc_tb_t),
+        VMSTATE_INT64(tb_offset, ppc_tb_t),
+        VMSTATE_UINT64(time_of_the_day, ppc_tb_t),
+        VMSTATE_UINT32_EQUAL(tb_freq, ppc_tb_t),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 const VMStateDescription vmstate_ppc_cpu = {
     .name = "cpu",
     .version_id = 5,
@@ -498,6 +538,10 @@ const VMStateDescription vmstate_ppc_cpu = {
         VMSTATE_UINT64_EQUAL(env.insns_flags, PowerPCCPU),
         VMSTATE_UINT64_EQUAL(env.insns_flags2, PowerPCCPU),
         VMSTATE_UINT32_EQUAL(env.nb_BATs, PowerPCCPU),
+
+        /* Time offset */
Quoted 8 lines

target-ppc/machine.c const VMStateDescription vmstate_ppc_cpu = { · line 547

Quoted 5 lines
target-ppc/machine.c +2 -0
+        VMSTATE_STRUCT_POINTER(env.tb_env, PowerPCCPU,
+                               vmstate_timebase, ppc_tb_t *),
         VMSTATE_END_OF_LIST()
     },
     .subsections = (VMStateSubsection []) {

Breaks the migration format. ;) You need to bump version_id and use a macro that accepts the version the field was added in as argument.

Quoted 3 lines

Risking of being called ignorant, I'll still ask - is the patch below what you mean? I could not find VMSTATE_STRUCT_POINTER_V and I do not believe it is not there already.

Usually the way we do it is to have VMSTATE_STRUCT_POINTER() call VMSTATE_STRUCT_POINTER_V() and thus VMSTATE_STRUCT_POINTER_TEST() call a new VMSTATE_STRUCT_POINTER_TEST_V(), to avoid code duplication of the core array entry. CC'ing Juan. Please do the VMState preparation in a separate patch.

ppc usage looks fine.

Quoted 3 lines

btw why would it break? Just asking. Is it because the source may send what the destination cannot handle? Named fields should stop the migration the same way as version mismatch would have done.

Nope, field names do not get transmitted, only the section names. (Otherwise random code refactorings could break the migration format.)

Quoted 2 lines

Or the source won't sent what the destination expects and we do not want this destination guest to continue?

There's an incoming stream of data from either live migration or a file, and QEMU must decide whether it can read and how to interpret the raw bytestream. It shouldn't just read random bytes into a new field when they were written from a different field.

Quoted 8 lines
Quoted 2 lines

Once I was told that migration between different versions of QEMU is not supported - so what is the point of .version_id field at all?

Not sure who told such a thing and in what context. On x86 we try to avoid version_id bumps by adding subsections to allow migration in both ways (including from newer to older) but for ppc, arm and all others we do require that new fields are marked as such. Whether migration is officially supported is a different matter from the VMState wire format.

Why is the approach different for x86 and ppc here? I can convert VMSTATE_STRUCT_POINTER to a subsection, why should not I? Or ppc is not mature enough and therefore there is no need to keep compatibility? Thanks.

Quoted 51 lines

Regards,
Andreas

Quoted 45 lines
alexey@ka1:~/pcipassthru/qemu$ git diff
include/migration/vmstate.h +9 -0
index 1c31b5d..7b624bf 100644
@@ -499,6 +499,15 @@ extern const VMStateInfo vmstate_info_bitmap;
 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
     VMSTATE_STRUCT_POINTER_TEST(_field, _state, NULL, _vmsd, _type)
 
+#define VMSTATE_STRUCT_POINTER_V(_field, _state,  _vmsd, _type, _version) { \
+    .name         = (stringify(_field)),                             \
+    .version_id = (_version),                                        \
+    .vmsd         = &(_vmsd),                                        \
+    .size         = sizeof(_type),                                   \
+    .flags        = VMS_STRUCT|VMS_POINTER,                          \
+    .offset       = vmstate_offset_value(_state, _field, _type),     \
+}
+
 #define VMSTATE_STRUCT_ARRAY(_field, _state, _num, _version, _vmsd, _type) \
     VMSTATE_STRUCT_ARRAY_TEST(_field, _state, _num, NULL, _version,   \
             _vmsd, _type)
target-ppc/machine.c +3 -3
index b4f447c..f79f38e 100644
@@ -501,7 +501,7 @@ static const VMStateDescription vmstate_timebase = {
 
 const VMStateDescription vmstate_ppc_cpu = {
     .name = "cpu",
-    .version_id = 5,
+    .version_id = 6,
     .minimum_version_id = 5,
     .minimum_version_id_old = 4,
     .load_state_old = cpu_load_old,
@@ -540,8 +540,8 @@ const VMStateDescription vmstate_ppc_cpu = {
         VMSTATE_UINT32_EQUAL(env.nb_BATs, PowerPCCPU),
 
         /* Time offset */
-        VMSTATE_STRUCT_POINTER(env.tb_env, PowerPCCPU,
-                               vmstate_timebase, ppc_tb_t *),
+        VMSTATE_STRUCT_POINTER_V(env.tb_env, PowerPCCPU,
+                                 vmstate_timebase, ppc_tb_t *, 6),
         VMSTATE_END_OF_LIST()
     },
     .subsections = (VMStateSubsection []) {
Signature · 1 line
Alexey
#5 lore raw
Quoted 80 lines · Alexey Kardashevskiy
Quoted 69 lines · Andreas Färber

Am 03.09.2013 11:07, schrieb Alexey Kardashevskiy:

Quoted 6 lines · Andreas Färber
Quoted 5 lines

Am 03.09.2013 09:31, schrieb Alexey Kardashevskiy:

Quoted 4 lines
target-ppc/machine.c +0 -0
index 12e1512..d1ffc7f 100644

[...]

Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
target-ppc/machine.c +16 -0
+static const VMStateDescription vmstate_timebase = {
+    .name = "cpu/timebase",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .pre_save = timebase_pre_save,
+    .post_load = timebase_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(timebase, ppc_tb_t),
+        VMSTATE_INT64(tb_offset, ppc_tb_t),
+        VMSTATE_UINT64(time_of_the_day, ppc_tb_t),
+        VMSTATE_UINT32_EQUAL(tb_freq, ppc_tb_t),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
const VMStateDescription vmstate_ppc_cpu = {
    .name = "cpu",
    .version_id = 5,
target-ppc/machine.c +2 -0
@@ -498,6 +538,10 @@ const VMStateDescription vmstate_ppc_cpu = {
        VMSTATE_UINT64_EQUAL(env.insns_flags, PowerPCCPU),
        VMSTATE_UINT64_EQUAL(env.insns_flags2, PowerPCCPU),
        VMSTATE_UINT32_EQUAL(env.nb_BATs, PowerPCCPU),
+
+        /* Time offset */
Quoted 8 lines

target-ppc/machine.c const VMStateDescription vmstate_ppc_cpu = { · line 547

Quoted 5 lines
target-ppc/machine.c +2 -0
+        VMSTATE_STRUCT_POINTER(env.tb_env, PowerPCCPU,
+                               vmstate_timebase, ppc_tb_t *),
        VMSTATE_END_OF_LIST()
    },
    .subsections = (VMStateSubsection []) {

Breaks the migration format. ;) You need to bump version_id and use a macro that accepts the version the field was added in as argument.

Quoted 3 lines

Risking of being called ignorant, I'll still ask - is the patch below what you mean? I could not find VMSTATE_STRUCT_POINTER_V and I do not believe it is not there already.

Usually the way we do it is to have VMSTATE_STRUCT_POINTER() call VMSTATE_STRUCT_POINTER_V() and thus VMSTATE_STRUCT_POINTER_TEST() call a new VMSTATE_STRUCT_POINTER_TEST_V(), to avoid code duplication of the core array entry. CC'ing Juan. Please do the VMState preparation in a separate patch.

ppc usage looks fine.

Quoted 3 lines

btw why would it break? Just asking. Is it because the source may send what the destination cannot handle? Named fields should stop the migration the same way as version mismatch would have done.

Nope, field names do not get transmitted, only the section names. (Otherwise random code refactorings could break the migration format.)

Quoted 2 lines

Or the source won't sent what the destination expects and we do not want this destination guest to continue?

There's an incoming stream of data from either live migration or a file, and QEMU must decide whether it can read and how to interpret the raw bytestream. It shouldn't just read random bytes into a new field when they were written from a different field.

Quoted 8 lines
Quoted 2 lines

Once I was told that migration between different versions of QEMU is not supported - so what is the point of .version_id field at all?

Not sure who told such a thing and in what context. On x86 we try to avoid version_id bumps by adding subsections to allow migration in both ways (including from newer to older) but for ppc, arm and all others we do require that new fields are marked as such. Whether migration is officially supported is a different matter from the VMState wire format.

Quoted 3 lines

Why is the approach different for x86 and ppc here? I can convert VMSTATE_STRUCT_POINTER to a subsection, why should not I? Or ppc is not mature enough and therefore there is no need to keep compatibility? Thanks.

Just bump the version.

Alex

#6 lore raw
In reply to Alexey Kardashevskiy
Quoted 61 lines · Alexey Kardashevskiy

This allows guests to have a different timebase origin from the host.

This is needed for migration, where a guest can migrate from one host to another and the two hosts might have a different timebase origin. However, the timebase seen by the guest must not go backwards, and should go forwards only by a small amount corresponding to the time taken for the migration.

This is only supported for recent POWER hardware which has the TBU40 (timebase upper 40 bits) register. That includes POWER6, 7, 8 but not 970.

This adds kvm_access_one_reg() to access a special register which is not in env->spr.

The feature must be present in the host kernel.

---

This is an RFC but not a final patch. Can break something but I just do not see what.

---
 hw/ppc/ppc.c         | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/ppc.h |  4 ++++
 target-ppc/kvm.c     | 23 +++++++++++++++++++++++
 target-ppc/machine.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 trace-events         |  3 +++
 5 files changed, 123 insertions(+)
hw/ppc/ppc.c +15 -0
index 1e3cab3..7d08c9a 100644
@@ -31,6 +31,7 @@
 #include "hw/loader.h"
 #include "sysemu/kvm.h"
 #include "kvm_ppc.h"
+#include "trace.h"
 
 //#define PPC_DEBUG_IRQ
 #define PPC_DEBUG_TB
@@ -796,6 +797,54 @@ static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
     cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
 }
 
+/*
+ * Calculate timebase on the destination side of migration
 
+ * We calculate new timebase offset as shown below:
+ * 1) Gtb2 = Gtb1 + max(tod2 - tod1, 0)
+ *    Gtb2 = tb2 + off2
+ *    Gtb1 = tb1 + off1
+ * 2) tb2 + off2 = tb1 + off1 + max(tod2 - tod1, 0)
+ * 3) off2 = tb1 - tb2 + off1 + max(tod2 - tod1, 0)
+ *
+ * where:
+ * Gtb2 - destination guest timebase
+ * tb2 - destination host timebase
+ * off2 - destination timebase offset
+ * tod2 - destination time of the day

hw/ppc/ppc.c static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq) · line 824

Quoted 10 lines
hw/ppc/ppc.c +10 -0
+ * Gtb1 - source guest timebase
+ * tb1 - source host timebase
+ * off1 - source timebase offset
+ * tod1 - source time of the day
+ *
+ * The result we want is in @off2
+ *
+ * Two conditions must be met for @off2:
+ * 1) off2 must be multiple of 2^24 ticks as it will be set via TBU40 SPR
+ * 2) Gtb2 >= Gtb1

What about the TCG case, where there is not host timebase, only a a host system clock?

Quoted 31 lines
hw/ppc/ppc.c +23 -0
+ */
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env)
+{
+    uint64_t tb2, tod2, off2;
+    int ratio = tb_env->tb_freq / 1000000;
+    struct timeval tv;
+
+    tb2 = cpu_get_real_ticks();
+    gettimeofday(&tv, NULL);
+    tod2 = tv.tv_sec * 1000000 + tv.tv_usec;
+
+    off2 = tb_env->timebase - tb2 + tb_env->tb_offset;
+    if (tod2 > tb_env->time_of_the_day) {
+        off2 += (tod2 - tb_env->time_of_the_day) * ratio;
+    }
+    off2 = ROUND_UP(off2, 1 << 24);
+
+    trace_ppc_tb_adjust(tb_env->tb_offset, off2,
+                        (int64_t)off2 - tb_env->tb_offset);
+
+    tb_env->tb_offset = off2;
+}
+
 /* Set up (once) timebase frequency (in Hz) */
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)
 {
include/hw/ppc/ppc.h +0 -0
index 132ab97..235871c 100644

include/hw/ppc/ppc.h struct ppc_tb_t { · line 37

Quoted 6 lines
include/hw/ppc/ppc.h +3 -0
@@ -32,6 +32,9 @@ struct ppc_tb_t {
     uint64_t purr_start;
     void *opaque;
     uint32_t flags;
+    /* Cached values for live migration purposes */
+    uint64_t timebase;
+    uint64_t time_of_the_day;

How is the time of day encoded here?

Quoted 24 lines
include/hw/ppc/ppc.h +1 -0
 };
 
 /* PPC Timers flags */
@@ -46,6 +49,7 @@ struct ppc_tb_t {
                                                */
 
 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset);
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env);
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq);
 /* Embedded PowerPC DCR management */
 typedef uint32_t (*dcr_read_cb)(void *opaque, int dcrn);
target-ppc/kvm.c +1 -0
index 7af9e3d..93df955 100644
@@ -35,6 +35,7 @@
 #include "hw/sysbus.h"
 #include "hw/ppc/spapr.h"
 #include "hw/ppc/spapr_vio.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/watchdog.h"
 
 //#define DEBUG_KVM

target-ppc/kvm.c static int kvm_put_vpa(CPUState *cs) · line 766

Quoted 5 lines
target-ppc/kvm.c +1 -0
@@ -761,6 +762,22 @@ static int kvm_put_vpa(CPUState *cs)
 }
 #endif /* TARGET_PPC64 */
 
+static int kvm_access_one_reg(CPUState *cs, bool set, __u64 id,
 void *addr)

I think it would be nicer to have seperate set_one_reg and get_one_reg functions, rather than using a direction parameter.

Quoted 19 lines
target-ppc/kvm.c +15 -0
+{
+    struct kvm_one_reg reg = {
+        .id = id,
+        .addr = (uintptr_t)addr,
+    };
+    int ret = kvm_vcpu_ioctl(cs, set ? KVM_SET_ONE_REG : KVM_GET_ONE_REG, &reg);
+
+    if (ret) {
+        DPRINTF("Unable to %s time base offset to KVM: %s\n",
+                set ? "set" : "get", strerror(errno));
+    }
+
+    return ret;
+}
+
 int kvm_arch_put_registers(CPUState *cs, int level)
 {
     PowerPCCPU *cpu = POWERPC_CPU(cs);

target-ppc/kvm.c int kvm_arch_put_registers(CPUState *cs, int level) · line 895

Quoted 6 lines
target-ppc/kvm.c +3 -0
@@ -873,6 +890,9 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                 DPRINTF("Warning: Unable to set VPA information to KVM\n");
             }
         }
+
+        kvm_access_one_reg(cs, 1, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);

Hrm. It may be too late, but would it make more sense for qemu to just calculate the "ideal" offset - not 24-bit aligned, and have the kernel round that up to a value suitable for TBU40. I'm thinking that might be more robust if someone decides that POWER10 should have a TBU50 or a TBU30 instead of TBU40.

Quoted 51 lines
target-ppc/kvm.c +3 -0
 #endif /* TARGET_PPC64 */
     }
 
@@ -1082,6 +1102,9 @@ int kvm_arch_get_registers(CPUState *cs)
                 DPRINTF("Warning: Unable to get VPA information from KVM\n");
             }
         }
+
+        kvm_access_one_reg(cs, 0, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif
     }
 
target-ppc/machine.c +24 -0
index 12e1512..d1ffc7f 100644
@@ -1,5 +1,6 @@
 #include "hw/hw.h"
 #include "hw/boards.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/kvm.h"
 #include "helper_regs.h"
 
@@ -459,6 +460,45 @@ static const VMStateDescription vmstate_tlbmas = {
     }
 };
 
+static void timebase_pre_save(void *opaque)
+{
+    ppc_tb_t *tb_env = opaque;
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+    tb_env->time_of_the_day = tv.tv_sec * 1000000 + tv.tv_usec;
+    tb_env->timebase = cpu_get_real_ticks();
+}
+
+static int timebase_post_load(void *opaque, int version_id)
+{
+    ppc_tb_t *tb_env = opaque;
+
+    if (!tb_env) {
+        printf("NO TB!\n");
+        return -1;
+    }
+    cpu_ppc_adjust_tb_offset(tb_env);
+
+    return 0;
+}
+

target-ppc/machine.c static const VMStateDescription vmstate_tlbmas = { · line 495

Quoted 10 lines
target-ppc/machine.c +10 -0
+static const VMStateDescription vmstate_timebase = {
+    .name = "cpu/timebase",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .pre_save = timebase_pre_save,
+    .post_load = timebase_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(timebase, ppc_tb_t),
+        VMSTATE_INT64(tb_offset, ppc_tb_t),

tb_offset is inherently a local concept, since it depends on the host timebase. So how can it belong in the migration stream?

Quoted 34 lines
target-ppc/machine.c +10 -0
+        VMSTATE_UINT64(time_of_the_day, ppc_tb_t),
+        VMSTATE_UINT32_EQUAL(tb_freq, ppc_tb_t),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 const VMStateDescription vmstate_ppc_cpu = {
     .name = "cpu",
     .version_id = 5,
@@ -498,6 +538,10 @@ const VMStateDescription vmstate_ppc_cpu = {
         VMSTATE_UINT64_EQUAL(env.insns_flags, PowerPCCPU),
         VMSTATE_UINT64_EQUAL(env.insns_flags2, PowerPCCPU),
         VMSTATE_UINT32_EQUAL(env.nb_BATs, PowerPCCPU),
+
+        /* Time offset */
+        VMSTATE_STRUCT_POINTER(env.tb_env, PowerPCCPU,
+                               vmstate_timebase, ppc_tb_t *),
         VMSTATE_END_OF_LIST()
     },
     .subsections = (VMStateSubsection []) {
trace-events +3 -0
index 935b953..24cf4d2 100644
@@ -1141,6 +1141,9 @@ spapr_iommu_stuff(uint64_t liobn, uint64_t ioba, uint64_t tce, uint64_t ret) "li
 spapr_iommu_xlate(uint64_t liobn, uint64_t ioba, uint64_t tce, unsigned perm, unsigned pgsize) "liobn=%"PRIx64" 0x%"PRIx64" -> 0x%"PRIx64" perm=%u mask=%x"
 spapr_iommu_new_table(uint64_t liobn, void *tcet, void *table, int fd) "liobn=%"PRIx64" tcet=%p table=%p fd=%d"
 
+# hw/ppc/ppc.c
+ppc_tb_adjust(uint64_t offs1, uint64_t offs2, int64_t diff) "adjusted from %"PRIx64" to %"PRIx64", diff %"PRId64
+
 # util/hbitmap.c
 hbitmap_iter_skip_words(const void *hb, void *hbi, uint64_t pos, unsigned long cur) "hb %p hbi %p pos %"PRId64" cur 0x%lx"
 hbitmap_reset(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %"PRIu64",%"PRIu64" bits %"PRIu64"..%"PRIu64
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#7 lore raw
Quoted 73 lines · David Gibson
Quoted 61 lines · Alexey Kardashevskiy

This allows guests to have a different timebase origin from the host.

This is needed for migration, where a guest can migrate from one host to another and the two hosts might have a different timebase origin. However, the timebase seen by the guest must not go backwards, and should go forwards only by a small amount corresponding to the time taken for the migration.

This is only supported for recent POWER hardware which has the TBU40 (timebase upper 40 bits) register. That includes POWER6, 7, 8 but not 970.

This adds kvm_access_one_reg() to access a special register which is not in env->spr.

The feature must be present in the host kernel.

---

This is an RFC but not a final patch. Can break something but I just do not see what.

---
 hw/ppc/ppc.c         | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/ppc.h |  4 ++++
 target-ppc/kvm.c     | 23 +++++++++++++++++++++++
 target-ppc/machine.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 trace-events         |  3 +++
 5 files changed, 123 insertions(+)
hw/ppc/ppc.c +15 -0
index 1e3cab3..7d08c9a 100644
@@ -31,6 +31,7 @@
 #include "hw/loader.h"
 #include "sysemu/kvm.h"
 #include "kvm_ppc.h"
+#include "trace.h"
 
 //#define PPC_DEBUG_IRQ
 #define PPC_DEBUG_TB
@@ -796,6 +797,54 @@ static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
     cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
 }
 
+/*
+ * Calculate timebase on the destination side of migration
 
+ * We calculate new timebase offset as shown below:
+ * 1) Gtb2 = Gtb1 + max(tod2 - tod1, 0)
+ *    Gtb2 = tb2 + off2
+ *    Gtb1 = tb1 + off1
+ * 2) tb2 + off2 = tb1 + off1 + max(tod2 - tod1, 0)
+ * 3) off2 = tb1 - tb2 + off1 + max(tod2 - tod1, 0)
+ *
+ * where:
+ * Gtb2 - destination guest timebase
+ * tb2 - destination host timebase
+ * off2 - destination timebase offset
+ * tod2 - destination time of the day

hw/ppc/ppc.c static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq) · line 824

Quoted 10 lines
hw/ppc/ppc.c +10 -0
+ * Gtb1 - source guest timebase
+ * tb1 - source host timebase
+ * off1 - source timebase offset
+ * tod1 - source time of the day
+ *
+ * The result we want is in @off2
+ *
+ * Two conditions must be met for @off2:
+ * 1) off2 must be multiple of 2^24 ticks as it will be set via TBU40 SPR
+ * 2) Gtb2 >= Gtb1
Quoted 2 lines

What about the TCG case, where there is not host timebase, only a a host system clock?

cpu_get_real_ticks() returns ticks, this is what the patch cares about. What is the difference between KVM and TCG here?

Quoted 31 lines
Quoted 31 lines
hw/ppc/ppc.c +23 -0
+ */
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env)
+{
+    uint64_t tb2, tod2, off2;
+    int ratio = tb_env->tb_freq / 1000000;
+    struct timeval tv;
+
+    tb2 = cpu_get_real_ticks();
+    gettimeofday(&tv, NULL);
+    tod2 = tv.tv_sec * 1000000 + tv.tv_usec;
+
+    off2 = tb_env->timebase - tb2 + tb_env->tb_offset;
+    if (tod2 > tb_env->time_of_the_day) {
+        off2 += (tod2 - tb_env->time_of_the_day) * ratio;
+    }
+    off2 = ROUND_UP(off2, 1 << 24);
+
+    trace_ppc_tb_adjust(tb_env->tb_offset, off2,
+                        (int64_t)off2 - tb_env->tb_offset);
+
+    tb_env->tb_offset = off2;
+}
+
 /* Set up (once) timebase frequency (in Hz) */
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)
 {
include/hw/ppc/ppc.h +0 -0
index 132ab97..235871c 100644
@@ -32,6 +32,9 @@ struct ppc_tb_t {
Quoted 8 lines

include/hw/ppc/ppc.h struct ppc_tb_t { · line 37

Quoted 6 lines
include/hw/ppc/ppc.h +3 -0
     uint64_t purr_start;
     void *opaque;
     uint32_t flags;
+    /* Cached values for live migration purposes */
+    uint64_t timebase;
+    uint64_t time_of_the_day;

How is the time of day encoded here?

Microseconds. I'll put a comment here, I just thought it is quite obvious as gettimeofday() returns microseconds.

Quoted 27 lines
Quoted 27 lines
include/hw/ppc/ppc.h +1 -0
 };
 
 /* PPC Timers flags */
@@ -46,6 +49,7 @@ struct ppc_tb_t {
                                                */
 
 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset);
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env);
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq);
 /* Embedded PowerPC DCR management */
 typedef uint32_t (*dcr_read_cb)(void *opaque, int dcrn);
target-ppc/kvm.c +1 -0
index 7af9e3d..93df955 100644
@@ -35,6 +35,7 @@
 #include "hw/sysbus.h"
 #include "hw/ppc/spapr.h"
 #include "hw/ppc/spapr_vio.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/watchdog.h"
 
 //#define DEBUG_KVM
@@ -761,6 +762,22 @@ static int kvm_put_vpa(CPUState *cs)
 }
 #endif /* TARGET_PPC64 */
 
Quoted 5 lines

target-ppc/kvm.c static int kvm_put_vpa(CPUState *cs) · line 766

Quoted 2 lines
target-ppc/kvm.c +1 -0
+static int kvm_access_one_reg(CPUState *cs, bool set, __u64 id,
 void *addr)

I think it would be nicer to have seperate set_one_reg and get_one_reg functions, rather than using a direction parameter.

I am regularly getting requests from Alex to merge functions which look almost the same. Please do not make it worse :)

Quoted 23 lines
Quoted 23 lines
target-ppc/kvm.c +15 -0
+{
+    struct kvm_one_reg reg = {
+        .id = id,
+        .addr = (uintptr_t)addr,
+    };
+    int ret = kvm_vcpu_ioctl(cs, set ? KVM_SET_ONE_REG : KVM_GET_ONE_REG, &reg);
+
+    if (ret) {
+        DPRINTF("Unable to %s time base offset to KVM: %s\n",
+                set ? "set" : "get", strerror(errno));
+    }
+
+    return ret;
+}
+
 int kvm_arch_put_registers(CPUState *cs, int level)
 {
     PowerPCCPU *cpu = POWERPC_CPU(cs);
target-ppc/kvm.c +1 -0
@@ -873,6 +890,9 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                 DPRINTF("Warning: Unable to set VPA information to KVM\n");
             }
         }
+
Quoted 8 lines

target-ppc/kvm.c int kvm_arch_put_registers(CPUState *cs, int level) · line 895

Quoted 2 lines
target-ppc/kvm.c +2 -0
+        kvm_access_one_reg(cs, 1, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);

Hrm. It may be too late, but would it make more sense for qemu to just calculate the "ideal" offset - not 24-bit aligned, and have the kernel round that up to a value suitable for TBU40. I'm thinking that might be more robust if someone decides that POWER10 should have a TBU50 or a TBU30 instead of TBU40.

No, not late, the kernel patchset has not been noticed yet by anyone :) Just a little remark here - QEMU sets one value to the kernel as an offset but when it will be about to migrate again and read this offset from the kernel, it will be different (rounded up) from what QEMU set. Not a problem though.

Quoted 56 lines
Quoted 56 lines
target-ppc/kvm.c +3 -0
 #endif /* TARGET_PPC64 */
     }
 
@@ -1082,6 +1102,9 @@ int kvm_arch_get_registers(CPUState *cs)
                 DPRINTF("Warning: Unable to get VPA information from KVM\n");
             }
         }
+
+        kvm_access_one_reg(cs, 0, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif
     }
 
target-ppc/machine.c +29 -0
index 12e1512..d1ffc7f 100644
@@ -1,5 +1,6 @@
 #include "hw/hw.h"
 #include "hw/boards.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/kvm.h"
 #include "helper_regs.h"
 
@@ -459,6 +460,45 @@ static const VMStateDescription vmstate_tlbmas = {
     }
 };
 
+static void timebase_pre_save(void *opaque)
+{
+    ppc_tb_t *tb_env = opaque;
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+    tb_env->time_of_the_day = tv.tv_sec * 1000000 + tv.tv_usec;
+    tb_env->timebase = cpu_get_real_ticks();
+}
+
+static int timebase_post_load(void *opaque, int version_id)
+{
+    ppc_tb_t *tb_env = opaque;
+
+    if (!tb_env) {
+        printf("NO TB!\n");
+        return -1;
+    }
+    cpu_ppc_adjust_tb_offset(tb_env);
+
+    return 0;
+}
+
+static const VMStateDescription vmstate_timebase = {
+    .name = "cpu/timebase",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
Quoted 8 lines

target-ppc/machine.c static const VMStateDescription vmstate_tlbmas = { · line 495

Quoted 5 lines
target-ppc/machine.c +5 -0
+    .pre_save = timebase_pre_save,
+    .post_load = timebase_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(timebase, ppc_tb_t),
+        VMSTATE_INT64(tb_offset, ppc_tb_t),

tb_offset is inherently a local concept, since it depends on the host timebase. So how can it belong in the migration stream?

I do not have pure guest timebase in QEMU and I need it on the destination. But I have host timebase + offset to calculate it. And tb_offset is already in ppc_tb_t. It looked logical to me to send the existing field and add only the missing part.

Quoted 35 lines
Quoted 34 lines
target-ppc/machine.c +10 -0
+        VMSTATE_UINT64(time_of_the_day, ppc_tb_t),
+        VMSTATE_UINT32_EQUAL(tb_freq, ppc_tb_t),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 const VMStateDescription vmstate_ppc_cpu = {
     .name = "cpu",
     .version_id = 5,
@@ -498,6 +538,10 @@ const VMStateDescription vmstate_ppc_cpu = {
         VMSTATE_UINT64_EQUAL(env.insns_flags, PowerPCCPU),
         VMSTATE_UINT64_EQUAL(env.insns_flags2, PowerPCCPU),
         VMSTATE_UINT32_EQUAL(env.nb_BATs, PowerPCCPU),
+
+        /* Time offset */
+        VMSTATE_STRUCT_POINTER(env.tb_env, PowerPCCPU,
+                               vmstate_timebase, ppc_tb_t *),
         VMSTATE_END_OF_LIST()
     },
     .subsections = (VMStateSubsection []) {
trace-events +3 -0
index 935b953..24cf4d2 100644
@@ -1141,6 +1141,9 @@ spapr_iommu_stuff(uint64_t liobn, uint64_t ioba, uint64_t tce, uint64_t ret) "li
 spapr_iommu_xlate(uint64_t liobn, uint64_t ioba, uint64_t tce, unsigned perm, unsigned pgsize) "liobn=%"PRIx64" 0x%"PRIx64" -> 0x%"PRIx64" perm=%u mask=%x"
 spapr_iommu_new_table(uint64_t liobn, void *tcet, void *table, int fd) "liobn=%"PRIx64" tcet=%p table=%p fd=%d"
 
+# hw/ppc/ppc.c
+ppc_tb_adjust(uint64_t offs1, uint64_t offs2, int64_t diff) "adjusted from %"PRIx64" to %"PRIx64", diff %"PRId64
+
 # util/hbitmap.c
 hbitmap_iter_skip_words(const void *hb, void *hbi, uint64_t pos, unsigned long cur) "hb %p hbi %p pos %"PRId64" cur 0x%lx"
 hbitmap_reset(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %"PRIu64",%"PRIu64" bits %"PRIu64"..%"PRIu64
Signature · 1 line
Alexey
#8 lore raw
Quoted 161 lines · Alexey Kardashevskiy
Quoted 73 lines · David Gibson
Quoted 61 lines · Alexey Kardashevskiy

This allows guests to have a different timebase origin from the host.

This is needed for migration, where a guest can migrate from one host to another and the two hosts might have a different timebase origin. However, the timebase seen by the guest must not go backwards, and should go forwards only by a small amount corresponding to the time taken for the migration.

This is only supported for recent POWER hardware which has the TBU40 (timebase upper 40 bits) register. That includes POWER6, 7, 8 but not 970.

This adds kvm_access_one_reg() to access a special register which is not in env->spr.

The feature must be present in the host kernel.

---

This is an RFC but not a final patch. Can break something but I just do not see what.

---

hw/ppc/ppc.c         | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
include/hw/ppc/ppc.h |  4 ++++
target-ppc/kvm.c     | 23 +++++++++++++++++++++++
target-ppc/machine.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
trace-events         |  3 +++
5 files changed, 123 insertions(+)
hw/ppc/ppc.c +15 -0
index 1e3cab3..7d08c9a 100644
@@ -31,6 +31,7 @@
 #include "hw/loader.h"
 #include "sysemu/kvm.h"
 #include "kvm_ppc.h"
+#include "trace.h"
 
 //#define PPC_DEBUG_IRQ
 #define PPC_DEBUG_TB
@@ -796,6 +797,54 @@ static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
    cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
 }
 
+/*
+ * Calculate timebase on the destination side of migration
 
+ * We calculate new timebase offset as shown below:
+ * 1) Gtb2 = Gtb1 + max(tod2 - tod1, 0)
+ *    Gtb2 = tb2 + off2
+ *    Gtb1 = tb1 + off1
+ * 2) tb2 + off2 = tb1 + off1 + max(tod2 - tod1, 0)
+ * 3) off2 = tb1 - tb2 + off1 + max(tod2 - tod1, 0)
+ *
+ * where:
+ * Gtb2 - destination guest timebase
+ * tb2 - destination host timebase
+ * off2 - destination timebase offset
+ * tod2 - destination time of the day

hw/ppc/ppc.c static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq) · line 824

Quoted 10 lines
hw/ppc/ppc.c +10 -0
+ * Gtb1 - source guest timebase
+ * tb1 - source host timebase
+ * off1 - source timebase offset
+ * tod1 - source time of the day
+ *
+ * The result we want is in @off2
+ *
+ * Two conditions must be met for @off2:
+ * 1) off2 must be multiple of 2^24 ticks as it will be set via TBU40 SPR
+ * 2) Gtb2 >= Gtb1
Quoted 2 lines

What about the TCG case, where there is not host timebase, only a a host system clock?

cpu_get_real_ticks() returns ticks, this is what the patch cares about. What is the difference between KVM and TCG here?

Quoted 31 lines
Quoted 31 lines
hw/ppc/ppc.c +23 -0
+ */
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env)
+{
+    uint64_t tb2, tod2, off2;
+    int ratio = tb_env->tb_freq / 1000000;
+    struct timeval tv;
+
+    tb2 = cpu_get_real_ticks();
+    gettimeofday(&tv, NULL);
+    tod2 = tv.tv_sec * 1000000 + tv.tv_usec;
+
+    off2 = tb_env->timebase - tb2 + tb_env->tb_offset;
+    if (tod2 > tb_env->time_of_the_day) {
+        off2 += (tod2 - tb_env->time_of_the_day) * ratio;
+    }
+    off2 = ROUND_UP(off2, 1 << 24);
+
+    trace_ppc_tb_adjust(tb_env->tb_offset, off2,
+                        (int64_t)off2 - tb_env->tb_offset);
+
+    tb_env->tb_offset = off2;
+}
+
 /* Set up (once) timebase frequency (in Hz) */
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)

{

include/hw/ppc/ppc.h +0 -0
index 132ab97..235871c 100644
@@ -32,6 +32,9 @@ struct ppc_tb_t {
Quoted 8 lines

include/hw/ppc/ppc.h struct ppc_tb_t { · line 37

Quoted 6 lines
include/hw/ppc/ppc.h +3 -0
    uint64_t purr_start;
    void *opaque;
    uint32_t flags;
+    /* Cached values for live migration purposes */
+    uint64_t timebase;
+    uint64_t time_of_the_day;

How is the time of day encoded here?

Microseconds. I'll put a comment here, I just thought it is quite obvious as gettimeofday() returns microseconds.

Quoted 27 lines
Quoted 27 lines
include/hw/ppc/ppc.h +1 -0
 };
 
 /* PPC Timers flags */
@@ -46,6 +49,7 @@ struct ppc_tb_t {
                                               */
 
 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset);
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env);
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq);
 /* Embedded PowerPC DCR management */
 typedef uint32_t (*dcr_read_cb)(void *opaque, int dcrn);
target-ppc/kvm.c +1 -0
index 7af9e3d..93df955 100644
@@ -35,6 +35,7 @@
 #include "hw/sysbus.h"
 #include "hw/ppc/spapr.h"
 #include "hw/ppc/spapr_vio.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/watchdog.h"
 
 //#define DEBUG_KVM
@@ -761,6 +762,22 @@ static int kvm_put_vpa(CPUState *cs)
 }
 #endif /* TARGET_PPC64 */
 
Quoted 5 lines

target-ppc/kvm.c static int kvm_put_vpa(CPUState *cs) · line 766

Quoted 2 lines
target-ppc/kvm.c +1 -0
+static int kvm_access_one_reg(CPUState *cs, bool set, __u64 id,
 void *addr)

I think it would be nicer to have seperate set_one_reg and get_one_reg functions, rather than using a direction parameter.

Quoted 2 lines

I am regularly getting requests from Alex to merge functions which look almost the same. Please do not make it worse :)

The reason I ask you to merge function is to reduce code duplication. The API should still look sane and I think what David suggests makes a lot of sense. In normal QEMU fashion, that translates to:

static int kvm_access_one_reg(CPUState *cs, uint64_t id, void *addr, bool set)

{
    ....
}

int kvm_set_one_reg(CPUState *cs, uint64_t id, void *addr)

{
    return kvm_access_one_reg(cs, id, addr, true);
}

int kvm_get_one_reg(CPUState *cs, uint64_t id, void *addr)

{
    return kvm_access_one_reg(cs, id, addr, false);
}

Also, this code should live in generic KVM code that can be used by more than just the PPC target.

Quoted 108 lines
Quoted 25 lines
Quoted 25 lines
target-ppc/kvm.c +15 -0
+{
+    struct kvm_one_reg reg = {
+        .id = id,
+        .addr = (uintptr_t)addr,
+    };
+    int ret = kvm_vcpu_ioctl(cs, set ? KVM_SET_ONE_REG : KVM_GET_ONE_REG, &reg);
+
+    if (ret) {
+        DPRINTF("Unable to %s time base offset to KVM: %s\n",
+                set ? "set" : "get", strerror(errno));
+    }
+
+    return ret;
+}
+
 int kvm_arch_put_registers(CPUState *cs, int level)
 {
    PowerPCCPU *cpu = POWERPC_CPU(cs);
target-ppc/kvm.c +1 -0
@@ -873,6 +890,9 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                DPRINTF("Warning: Unable to set VPA information to KVM\n");
            }
        }
+
Quoted 8 lines

target-ppc/kvm.c int kvm_arch_put_registers(CPUState *cs, int level) · line 895

Quoted 2 lines
target-ppc/kvm.c +2 -0
+        kvm_access_one_reg(cs, 1, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);

Hrm. It may be too late, but would it make more sense for qemu to just calculate the "ideal" offset - not 24-bit aligned, and have the kernel round that up to a value suitable for TBU40. I'm thinking that might be more robust if someone decides that POWER10 should have a TBU50 or a TBU30 instead of TBU40.

No, not late, the kernel patchset has not been noticed yet by anyone :) Just a little remark here - QEMU sets one value to the kernel as an offset but when it will be about to migrate again and read this offset from the kernel, it will be different (rounded up) from what QEMU set. Not a problem though.

Quoted 56 lines
Quoted 56 lines
target-ppc/kvm.c +3 -0
 #endif /* TARGET_PPC64 */
    }
 
@@ -1082,6 +1102,9 @@ int kvm_arch_get_registers(CPUState *cs)
                DPRINTF("Warning: Unable to get VPA information from KVM\n");
            }
        }
+
+        kvm_access_one_reg(cs, 0, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif
    }
 
target-ppc/machine.c +29 -0
index 12e1512..d1ffc7f 100644
@@ -1,5 +1,6 @@
 #include "hw/hw.h"
 #include "hw/boards.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/kvm.h"
 #include "helper_regs.h"
 
@@ -459,6 +460,45 @@ static const VMStateDescription vmstate_tlbmas = {
    }
 };
 
+static void timebase_pre_save(void *opaque)
+{
+    ppc_tb_t *tb_env = opaque;
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+    tb_env->time_of_the_day = tv.tv_sec * 1000000 + tv.tv_usec;
+    tb_env->timebase = cpu_get_real_ticks();
+}
+
+static int timebase_post_load(void *opaque, int version_id)
+{
+    ppc_tb_t *tb_env = opaque;
+
+    if (!tb_env) {
+        printf("NO TB!\n");
+        return -1;
+    }
+    cpu_ppc_adjust_tb_offset(tb_env);
+
+    return 0;
+}
+
+static const VMStateDescription vmstate_timebase = {
+    .name = "cpu/timebase",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
Quoted 8 lines

target-ppc/machine.c static const VMStateDescription vmstate_tlbmas = { · line 495

Quoted 5 lines
target-ppc/machine.c +5 -0
+    .pre_save = timebase_pre_save,
+    .post_load = timebase_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(timebase, ppc_tb_t),
+        VMSTATE_INT64(tb_offset, ppc_tb_t),

tb_offset is inherently a local concept, since it depends on the host timebase. So how can it belong in the migration stream?

Quoted 4 lines

I do not have pure guest timebase in QEMU and I need it on the destination. But I have host timebase + offset to calculate it. And tb_offset is already in ppc_tb_t. It looked logical to me to send the existing field and add only the missing part.

I still don't understand. You want the guest visible timebase in the migration stream, no?

Alex

#9 lore raw
Quoted 295 lines · Alexander Graf
Quoted 161 lines · Alexey Kardashevskiy
Quoted 73 lines · David Gibson
Quoted 61 lines · Alexey Kardashevskiy

This allows guests to have a different timebase origin from the host.

This is needed for migration, where a guest can migrate from one host to another and the two hosts might have a different timebase origin. However, the timebase seen by the guest must not go backwards, and should go forwards only by a small amount corresponding to the time taken for the migration.

This is only supported for recent POWER hardware which has the TBU40 (timebase upper 40 bits) register. That includes POWER6, 7, 8 but not 970.

This adds kvm_access_one_reg() to access a special register which is not in env->spr.

The feature must be present in the host kernel.

---

This is an RFC but not a final patch. Can break something but I just do not see what.

---

hw/ppc/ppc.c         | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
include/hw/ppc/ppc.h |  4 ++++
target-ppc/kvm.c     | 23 +++++++++++++++++++++++
target-ppc/machine.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
trace-events         |  3 +++
5 files changed, 123 insertions(+)
hw/ppc/ppc.c +15 -0
index 1e3cab3..7d08c9a 100644
@@ -31,6 +31,7 @@
 #include "hw/loader.h"
 #include "sysemu/kvm.h"
 #include "kvm_ppc.h"
+#include "trace.h"
 
 //#define PPC_DEBUG_IRQ
 #define PPC_DEBUG_TB
@@ -796,6 +797,54 @@ static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
    cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
 }
 
+/*
+ * Calculate timebase on the destination side of migration
 
+ * We calculate new timebase offset as shown below:
+ * 1) Gtb2 = Gtb1 + max(tod2 - tod1, 0)
+ *    Gtb2 = tb2 + off2
+ *    Gtb1 = tb1 + off1
+ * 2) tb2 + off2 = tb1 + off1 + max(tod2 - tod1, 0)
+ * 3) off2 = tb1 - tb2 + off1 + max(tod2 - tod1, 0)
+ *
+ * where:
+ * Gtb2 - destination guest timebase
+ * tb2 - destination host timebase
+ * off2 - destination timebase offset
+ * tod2 - destination time of the day

hw/ppc/ppc.c static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq) · line 824

Quoted 10 lines
hw/ppc/ppc.c +10 -0
+ * Gtb1 - source guest timebase
+ * tb1 - source host timebase
+ * off1 - source timebase offset
+ * tod1 - source time of the day
+ *
+ * The result we want is in @off2
+ *
+ * Two conditions must be met for @off2:
+ * 1) off2 must be multiple of 2^24 ticks as it will be set via TBU40 SPR
+ * 2) Gtb2 >= Gtb1
Quoted 2 lines

What about the TCG case, where there is not host timebase, only a a host system clock?

cpu_get_real_ticks() returns ticks, this is what the patch cares about. What is the difference between KVM and TCG here?

Quoted 31 lines
Quoted 31 lines
hw/ppc/ppc.c +23 -0
+ */
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env)
+{
+    uint64_t tb2, tod2, off2;
+    int ratio = tb_env->tb_freq / 1000000;
+    struct timeval tv;
+
+    tb2 = cpu_get_real_ticks();
+    gettimeofday(&tv, NULL);
+    tod2 = tv.tv_sec * 1000000 + tv.tv_usec;
+
+    off2 = tb_env->timebase - tb2 + tb_env->tb_offset;
+    if (tod2 > tb_env->time_of_the_day) {
+        off2 += (tod2 - tb_env->time_of_the_day) * ratio;
+    }
+    off2 = ROUND_UP(off2, 1 << 24);
+
+    trace_ppc_tb_adjust(tb_env->tb_offset, off2,
+                        (int64_t)off2 - tb_env->tb_offset);
+
+    tb_env->tb_offset = off2;
+}
+
 /* Set up (once) timebase frequency (in Hz) */
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)

{

include/hw/ppc/ppc.h +0 -0
index 132ab97..235871c 100644
@@ -32,6 +32,9 @@ struct ppc_tb_t {
Quoted 8 lines

include/hw/ppc/ppc.h struct ppc_tb_t { · line 37

Quoted 6 lines
include/hw/ppc/ppc.h +3 -0
    uint64_t purr_start;
    void *opaque;
    uint32_t flags;
+    /* Cached values for live migration purposes */
+    uint64_t timebase;
+    uint64_t time_of_the_day;

How is the time of day encoded here?

Microseconds. I'll put a comment here, I just thought it is quite obvious as gettimeofday() returns microseconds.

Quoted 27 lines
Quoted 27 lines
include/hw/ppc/ppc.h +1 -0
 };
 
 /* PPC Timers flags */
@@ -46,6 +49,7 @@ struct ppc_tb_t {
                                               */
 
 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset);
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env);
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq);
 /* Embedded PowerPC DCR management */
 typedef uint32_t (*dcr_read_cb)(void *opaque, int dcrn);
target-ppc/kvm.c +1 -0
index 7af9e3d..93df955 100644
@@ -35,6 +35,7 @@
 #include "hw/sysbus.h"
 #include "hw/ppc/spapr.h"
 #include "hw/ppc/spapr_vio.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/watchdog.h"
 
 //#define DEBUG_KVM
@@ -761,6 +762,22 @@ static int kvm_put_vpa(CPUState *cs)
 }
 #endif /* TARGET_PPC64 */
 
Quoted 5 lines

target-ppc/kvm.c static int kvm_put_vpa(CPUState *cs) · line 766

Quoted 2 lines
target-ppc/kvm.c +1 -0
+static int kvm_access_one_reg(CPUState *cs, bool set, __u64 id,
 void *addr)

I think it would be nicer to have seperate set_one_reg and get_one_reg functions, rather than using a direction parameter.

Quoted 2 lines

I am regularly getting requests from Alex to merge functions which look almost the same. Please do not make it worse :)

The reason I ask you to merge function is to reduce code duplication. The API should still look sane and I think what David suggests makes a lot of sense. In normal QEMU fashion, that translates to:

static int kvm_access_one_reg(CPUState *cs, uint64_t id, void *addr, bool set)

{
    ....
}

int kvm_set_one_reg(CPUState *cs, uint64_t id, void *addr)

{
    return kvm_access_one_reg(cs, id, addr, true);
}

int kvm_get_one_reg(CPUState *cs, uint64_t id, void *addr)

{
    return kvm_access_one_reg(cs, id, addr, false);
}

Also, this code should live in generic KVM code that can be used by more than just the PPC target.

Quoted 108 lines
Quoted 25 lines
Quoted 25 lines
target-ppc/kvm.c +15 -0
+{
+    struct kvm_one_reg reg = {
+        .id = id,
+        .addr = (uintptr_t)addr,
+    };
+    int ret = kvm_vcpu_ioctl(cs, set ? KVM_SET_ONE_REG : KVM_GET_ONE_REG, &reg);
+
+    if (ret) {
+        DPRINTF("Unable to %s time base offset to KVM: %s\n",
+                set ? "set" : "get", strerror(errno));
+    }
+
+    return ret;
+}
+
 int kvm_arch_put_registers(CPUState *cs, int level)
 {
    PowerPCCPU *cpu = POWERPC_CPU(cs);
target-ppc/kvm.c +1 -0
@@ -873,6 +890,9 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                DPRINTF("Warning: Unable to set VPA information to KVM\n");
            }
        }
+
Quoted 8 lines

target-ppc/kvm.c int kvm_arch_put_registers(CPUState *cs, int level) · line 895

Quoted 2 lines
target-ppc/kvm.c +2 -0
+        kvm_access_one_reg(cs, 1, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);

Hrm. It may be too late, but would it make more sense for qemu to just calculate the "ideal" offset - not 24-bit aligned, and have the kernel round that up to a value suitable for TBU40. I'm thinking that might be more robust if someone decides that POWER10 should have a TBU50 or a TBU30 instead of TBU40.

No, not late, the kernel patchset has not been noticed yet by anyone :) Just a little remark here - QEMU sets one value to the kernel as an offset but when it will be about to migrate again and read this offset from the kernel, it will be different (rounded up) from what QEMU set. Not a problem though.

Quoted 64 lines
Quoted 51 lines
target-ppc/kvm.c +3 -0
 #endif /* TARGET_PPC64 */
    }
 
@@ -1082,6 +1102,9 @@ int kvm_arch_get_registers(CPUState *cs)
                DPRINTF("Warning: Unable to get VPA information from KVM\n");
            }
        }
+
+        kvm_access_one_reg(cs, 0, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif
    }
 
target-ppc/machine.c +24 -0
index 12e1512..d1ffc7f 100644
@@ -1,5 +1,6 @@
 #include "hw/hw.h"
 #include "hw/boards.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/kvm.h"
 #include "helper_regs.h"
 
@@ -459,6 +460,45 @@ static const VMStateDescription vmstate_tlbmas = {
    }
 };
 
+static void timebase_pre_save(void *opaque)
+{
+    ppc_tb_t *tb_env = opaque;
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+    tb_env->time_of_the_day = tv.tv_sec * 1000000 + tv.tv_usec;
+    tb_env->timebase = cpu_get_real_ticks();
+}
+
+static int timebase_post_load(void *opaque, int version_id)
+{
+    ppc_tb_t *tb_env = opaque;
+
+    if (!tb_env) {
+        printf("NO TB!\n");
+        return -1;
+    }
+    cpu_ppc_adjust_tb_offset(tb_env);
+
+    return 0;
+}
+

target-ppc/machine.c static const VMStateDescription vmstate_tlbmas = { · line 495

Quoted 10 lines
target-ppc/machine.c +10 -0
+static const VMStateDescription vmstate_timebase = {
+    .name = "cpu/timebase",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .pre_save = timebase_pre_save,
+    .post_load = timebase_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(timebase, ppc_tb_t),
+        VMSTATE_INT64(tb_offset, ppc_tb_t),

tb_offset is inherently a local concept, since it depends on the host timebase. So how can it belong in the migration stream?

Quoted 6 lines
Quoted 4 lines

I do not have pure guest timebase in QEMU and I need it on the destination. But I have host timebase + offset to calculate it. And tb_offset is already in ppc_tb_t. It looked logical to me to send the existing field and add only the missing part.

I still don't understand. You want the guest visible timebase in the migration stream, no?

Yes. I do not really understand the problem here (and I am not playing dump). Do you suggest sending just the guest timebase and do not send the host timebase and the offset (one number instead of two)? I can do that, makes sense, no problem, thanks for the idea.

Signature · 1 line
Alexey
#10 lore raw
Quoted 5 lines · Alexey Kardashevskiy
Quoted 4 lines · Alexander Graf
Quoted 1 line · Alexey Kardashevskiy

On 09/05/2013 02:30 PM, David Gibson wrote:

[...]

Quoted 72 lines
Quoted 72 lines
Quoted 71 lines
Quoted 57 lines
Quoted 57 lines
#endif /* TARGET_PPC64 */
   }
target-ppc/kvm.c +3 -0
@@ -1082,6 +1102,9 @@ int kvm_arch_get_registers(CPUState *cs)
               DPRINTF("Warning: Unable to get VPA information from KVM\n");
           }
       }
+
+        kvm_access_one_reg(cs, 0, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif
   }
 
target-ppc/machine.c +29 -0
index 12e1512..d1ffc7f 100644
@@ -1,5 +1,6 @@
 #include "hw/hw.h"
 #include "hw/boards.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/kvm.h"
 #include "helper_regs.h"
 
@@ -459,6 +460,45 @@ static const VMStateDescription vmstate_tlbmas = {
   }
 };
 
+static void timebase_pre_save(void *opaque)
+{
+    ppc_tb_t *tb_env = opaque;
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+    tb_env->time_of_the_day = tv.tv_sec * 1000000 + tv.tv_usec;
+    tb_env->timebase = cpu_get_real_ticks();
+}
+
+static int timebase_post_load(void *opaque, int version_id)
+{
+    ppc_tb_t *tb_env = opaque;
+
+    if (!tb_env) {
+        printf("NO TB!\n");
+        return -1;
+    }
+    cpu_ppc_adjust_tb_offset(tb_env);
+
+    return 0;
+}
+
+static const VMStateDescription vmstate_timebase = {
+    .name = "cpu/timebase",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
Quoted 8 lines

target-ppc/machine.c static const VMStateDescription vmstate_tlbmas = { · line 495

Quoted 5 lines
target-ppc/machine.c +5 -0
+    .pre_save = timebase_pre_save,
+    .post_load = timebase_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(timebase, ppc_tb_t),
+        VMSTATE_INT64(tb_offset, ppc_tb_t),

tb_offset is inherently a local concept, since it depends on the host timebase. So how can it belong in the migration stream?

I do not have pure guest timebase in QEMU and I need it on the destination. But I have host timebase + offset to calculate it. And tb_offset is already in ppc_tb_t. It looked logical to me to send the existing field and add only the missing part.

Quoted 7 lines
Quoted 1 line

I still don't understand. You want the guest visible timebase in the migration stream, no?

Yes. I do not really understand the problem here (and I am not playing dump). Do you suggest sending just the guest timebase and do not send the host timebase and the offset (one number instead of two)? I can do that, makes sense, no problem, thanks for the idea.

Yup, pretty much :). The receiving end should have no business in knowing how far off the guest and the host timebase were skewed on the sending end :).

Alex

#11 lore raw
Quoted 11 lines · Alexander Graf
Quoted 1 line

Yes. I do not really understand the problem here (and I am not

playing

Quoted 1 line

dump). Do you suggest sending just the guest timebase and do not

send the

Quoted 1 line

host timebase and the offset (one number instead of two)? I can do

that,

Quoted 1 line

makes sense, no problem, thanks for the idea.

Yup, pretty much :). The receiving end should have no business in knowing how far off the guest and the host timebase were skewed on the sending end :).

Well, yes and no ... we'd like to account for the migration latency on the timebase or the guest view of real time will get skewed since it uses the TB to maintain it's clock.

So assuming both hosts are reasonably synchronized with NTP, we want a correlation guest TB / TOD in order to properly make the adjustment on the target.

This may not be what Alexey implemented but I think that's what Paulus and I asked for :-)

Cheers,
Ben.

#12 lore raw
Quoted 14 lines · Benjamin Herrenschmidt
Quoted 11 lines · Alexander Graf
Quoted 1 line

Yes. I do not really understand the problem here (and I am not

playing

Quoted 1 line

dump). Do you suggest sending just the guest timebase and do not

send the

Quoted 1 line

host timebase and the offset (one number instead of two)? I can do

that,

Quoted 1 line

makes sense, no problem, thanks for the idea.

Yup, pretty much :). The receiving end should have no business in knowing how far off the guest and the host timebase were skewed on the sending end :).

Quoted 7 lines

Well, yes and no ... we'd like to account for the migration latency on the timebase or the guest view of real time will get skewed since it uses the TB to maintain it's clock.

So assuming both hosts are reasonably synchronized with NTP, we want a correlation guest TB / TOD in order to properly make the adjustment on the target.

Hrm, I think I'm starting to understand what this is about. So what we want is

- timebase in guest
- timebase frequency in guest
- wall clock time in host

That way the receiving end can then take the timebase and add (new_timebase - old_timebase) * tb_freq to the guest's time base.

Which gets me to the next question. Can we modify the tb frequency in guests?

Alex

#13 lore raw
Quoted 9 lines · Alexander Graf

Hrm, I think I'm starting to understand what this is about. So what we want is

- timebase in guest
- timebase frequency in guest
- wall clock time in host

That way the receiving end can then take the timebase and add (new_timebase - old_timebase) * tb_freq to the guest's time base.

Which gets me to the next question. Can we modify the tb frequency in guests?

No. It's architected at 512Mhz however since P7 I think. Not sure how we did before, it's possible that P6 was the same (at least it's sourced from more/less the same chip TOD facility).

Cheers,
Ben.

#14 lore raw
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 6 lines · Alexander Graf

Hrm, I think I'm starting to understand what this is about. So what we want is

- timebase in guest
- timebase frequency in guest
- wall clock time in host

Quoted 7 lines
Quoted 3 lines

That way the receiving end can then take the timebase and add (new_timebase - old_timebase) * tb_freq to the guest's time base.

Which gets me to the next question. Can we modify the tb frequency in guests?

No. It's architected at 512Mhz however since P7 I think. Not sure how we did before, it's possible that P6 was the same (at least it's sourced from more/less the same chip TOD facility).

I think we should transmit the tb frequency as well to at least allow us to adjust if a later chip derives here.

But yes, without frequency adjustment I see where you're coming from. We still only need the timebase the guest sees, not the offset. But we also need the host wall clock to allow for adjustments :).

Alex

#15 lore raw

Am 05.09.2013 15:39, schrieb Alexander Graf:

Quoted 13 lines · Benjamin Herrenschmidt
Quoted 11 lines · Alexander Graf
Quoted 9 lines

Hrm, I think I'm starting to understand what this is about. So what we want is

- timebase in guest
- timebase frequency in guest
- wall clock time in host

That way the receiving end can then take the timebase and add (new_timebase - old_timebase) * tb_freq to the guest's time base.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Which gets me to the next question. Can we modify the tb frequency in guests?

No. It's architected at 512Mhz however since P7 I think. Not sure how we did before, it's possible that P6 was the same (at least it's sourced from more/less the same chip TOD facility).

I think we should transmit the tb frequency as well to at least allow us to adjust if a later chip derives here.

Are you thinking of POWER8 having a different frequency than POWER8 in compat mode? Because migration from one -cpu to another is not supported elsewhere.

Even if we want to migrate from one POWER7 revision to another, we should let the destination use the revision of the source (guest ABI!), via property if need be. Anything else will lead to confusion as to what is supported and what is not. That -cpu host is the default for convenience shouldn't relieve admins/libvirt to think about sensible setups like they have to on x86.

Andreas

Quoted 6 lines

But yes, without frequency adjustment I see where you're coming from. We still only need the timebase the guest sees, not the offset. But we also need the host wall clock to allow for adjustments :).

Alex

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#16 lore raw
Quoted 10 lines · Andreas Färber

Are you thinking of POWER8 having a different frequency than POWER8 in compat mode? Because migration from one -cpu to another is not supported elsewhere.

Even if we want to migrate from one POWER7 revision to another, we should let the destination use the revision of the source (guest ABI!), via property if need be. Anything else will lead to confusion as to what is supported and what is not. That -cpu host is the default for convenience shouldn't relieve admins/libvirt to think about sensible setups like they have to on x86.

Besides POWER8 uses 512Mhz too :-) It's been architected so it's unlikely to change from now on.

Cheers,
Ben.

Quoted 10 lines

Andreas

Quoted 6 lines

But yes, without frequency adjustment I see where you're coming from. We still only need the timebase the guest sees, not the offset. But we also need the host wall clock to allow for adjustments :).

Alex

#17 lore raw
Quoted 13 lines · Benjamin Herrenschmidt
Quoted 10 lines · Andreas Färber

Are you thinking of POWER8 having a different frequency than POWER8 in compat mode? Because migration from one -cpu to another is not supported elsewhere.

Even if we want to migrate from one POWER7 revision to another, we should let the destination use the revision of the source (guest ABI!), via property if need be. Anything else will lead to confusion as to what is supported and what is not. That -cpu host is the default for convenience shouldn't relieve admins/libvirt to think about sensible setups like they have to on x86.

Quoted 2 lines

Besides POWER8 uses 512Mhz too :-) It's been architected so it's unlikely to change from now on.

Ok, ditch the tb frequency then. We can always default to 512Mhz when it's absent.

Alex

#18 lore raw
Quoted 16 lines · Alexander Graf
Quoted 13 lines · Benjamin Herrenschmidt
Quoted 10 lines · Andreas Färber

Are you thinking of POWER8 having a different frequency than POWER8 in compat mode? Because migration from one -cpu to another is not supported elsewhere.

Even if we want to migrate from one POWER7 revision to another, we should let the destination use the revision of the source (guest ABI!), via property if need be. Anything else will lead to confusion as to what is supported and what is not. That -cpu host is the default for convenience shouldn't relieve admins/libvirt to think about sensible setups like they have to on x86.

Quoted 4 lines
Quoted 2 lines

Besides POWER8 uses 512Mhz too :-) It's been architected so it's unlikely to change from now on.

Ok, ditch the tb frequency then. We can always default to 512Mhz when it's absent.

QEMU uses what kvmppc_get_tbfreq() returns. And ppc_tb_t carries this value. It does not use 512MHz automatically but migration should always assume 512MHz :-/

If we remove it, I would like to add assert(tb_freq!=512MHz) into timebase_pre_save() and timebase_post_load(), is that ok?

Signature · 1 line
Alexey
#19 lore raw

Am 09.09.2013 um 04:40 schrieb Alexey Kardashevskiy <[email protected]>:

Quoted 23 lines · Alexander Graf
Quoted 16 lines · Benjamin Herrenschmidt
Quoted 14 lines · Andreas Färber
Quoted 11 lines

Are you thinking of POWER8 having a different frequency than POWER8 in compat mode? Because migration from one -cpu to another is not supported elsewhere.

Even if we want to migrate from one POWER7 revision to another, we should let the destination use the revision of the source (guest ABI!), via property if need be. Anything else will lead to confusion as to what is supported and what is not. That -cpu host is the default for convenience shouldn't relieve admins/libvirt to think about sensible setups like they have to on x86.

Quoted 4 lines
Quoted 2 lines

Besides POWER8 uses 512Mhz too :-) It's been architected so it's unlikely to change from now on.

Ok, ditch the tb frequency then. We can always default to 512Mhz when it's absent.

Quoted 6 lines

QEMU uses what kvmppc_get_tbfreq() returns. And ppc_tb_t carries this value. It does not use 512MHz automatically but migration should always assume 512MHz :-/

If we remove it, I would like to add assert(tb_freq!=512MHz) into timebase_pre_save() and timebase_post_load(), is that ok?

Good point. This would break TCG migration, right?

Alex

Quoted 4 lines
Signature · 2 lines
-- 
Alexey
#20 lore raw
Quoted 27 lines · Alexander Graf

Am 09.09.2013 um 04:40 schrieb Alexey Kardashevskiy <[email protected]>:

Quoted 23 lines · Alexander Graf
Quoted 20 lines · Benjamin Herrenschmidt
Quoted 14 lines · Andreas Färber
Quoted 11 lines

Are you thinking of POWER8 having a different frequency than POWER8 in compat mode? Because migration from one -cpu to another is not supported elsewhere.

Even if we want to migrate from one POWER7 revision to another, we should let the destination use the revision of the source (guest ABI!), via property if need be. Anything else will lead to confusion as to what is supported and what is not. That -cpu host is the default for convenience shouldn't relieve admins/libvirt to think about sensible setups like they have to on x86.

Quoted 2 lines

Besides POWER8 uses 512Mhz too :-) It's been architected so it's unlikely to change from now on.

Ok, ditch the tb frequency then. We can always default to 512Mhz when it's absent.

Quoted 8 lines
Quoted 6 lines

QEMU uses what kvmppc_get_tbfreq() returns. And ppc_tb_t carries this value. It does not use 512MHz automatically but migration should always assume 512MHz :-/

If we remove it, I would like to add assert(tb_freq!=512MHz) into timebase_pre_save() and timebase_post_load(), is that ok?

Good point. This would break TCG migration, right?

In TCG we do not need any timebase offset at all, the time should migrate as is, no? :)

It could break something like power5 migration under PR KVM, do not know...

Signature · 1 line
Alexey
#21 lore raw

Am 09.09.2013 um 07:58 schrieb Alexey Kardashevskiy <[email protected]>:

Quoted 32 lines · Alexander Graf
Quoted 31 lines

Am 09.09.2013 um 04:40 schrieb Alexey Kardashevskiy <[email protected]>:

Quoted 27 lines · Alexander Graf
Quoted 16 lines · Benjamin Herrenschmidt
Quoted 14 lines · Andreas Färber
Quoted 11 lines

Are you thinking of POWER8 having a different frequency than POWER8 in compat mode? Because migration from one -cpu to another is not supported elsewhere.

Even if we want to migrate from one POWER7 revision to another, we should let the destination use the revision of the source (guest ABI!), via property if need be. Anything else will lead to confusion as to what is supported and what is not. That -cpu host is the default for convenience shouldn't relieve admins/libvirt to think about sensible setups like they have to on x86.

Quoted 4 lines
Quoted 2 lines

Besides POWER8 uses 512Mhz too :-) It's been architected so it's unlikely to change from now on.

Ok, ditch the tb frequency then. We can always default to 512Mhz when it's absent.

QEMU uses what kvmppc_get_tbfreq() returns. And ppc_tb_t carries this value. It does not use 512MHz automatically but migration should always assume 512MHz :-/

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

If we remove it, I would like to add assert(tb_freq!=512MHz) into timebase_pre_save() and timebase_post_load(), is that ok?

Good point. This would break TCG migration, right?

In TCG we do not need any timebase offset at all, the time should migrate as is, no? :)

I hope so, but we need to make sure we don't assert() in that case :).

Quoted 2 lines

It could break something like power5 migration under PR KVM, do not know...

Well, today we don't support full migration with PR KVM yet - IIRC we don't have access to all state. But that may change in the future.

I think it's ok to restrict live migration to machines with the same tb frequency when kvm is enabled. Whether you implement it through a hardcoded 512Mhz or through a timebase value that gets live migrated and then compared is up to you - both ways work for me :).

Alex

Quoted 4 lines
Signature · 2 lines
-- 
Alexey
#22 lore raw
Quoted 4 lines · Alexander Graf

I think it's ok to restrict live migration to machines with the same tb frequency when kvm is enabled. Whether you implement it through a hardcoded 512Mhz or through a timebase value that gets live migrated and then compared is up to you - both ways work for me :).

The latter might be handy if we want to support migration on 970, though we don't have the TBU40 stuff there so adjusting the TB in the host kernel would be ... problematic.

Cheers,
Ben.

#23 lore raw
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 4 lines · Alexander Graf

I think it's ok to restrict live migration to machines with the same tb frequency when kvm is enabled. Whether you implement it through a hardcoded 512Mhz or through a timebase value that gets live migrated and then compared is up to you - both ways work for me :).

The latter might be handy if we want to support migration on 970, though we don't have the TBU40 stuff there so adjusting the TB in the host kernel would be ... problematic.

Well, we could save/restore TB when we enter/exit the guest, no? But I really only want to have something that doesn't block the door for someone who wants to enable things.

Alex

#24 lore raw
Quoted 8 lines · Alexander Graf
Quoted 6 lines · Benjamin Herrenschmidt
Quoted 4 lines · Alexander Graf

I think it's ok to restrict live migration to machines with the same tb frequency when kvm is enabled. Whether you implement it through a hardcoded 512Mhz or through a timebase value that gets live migrated and then compared is up to you - both ways work for me :).

Quoted 5 lines
Quoted 3 lines

The latter might be handy if we want to support migration on 970, though we don't have the TBU40 stuff there so adjusting the TB in the host kernel would be ... problematic.

Well, we could save/restore TB when we enter/exit the guest, no?

Hard to do without introducing drift...

Quoted 4 lines

But I really only want to have something that doesn't block the door for someone who wants to enable things.

Alex

#25 lore raw
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 8 lines · Alexander Graf
Quoted 6 lines · Benjamin Herrenschmidt
Quoted 4 lines · Alexander Graf

I think it's ok to restrict live migration to machines with the same tb frequency when kvm is enabled. Whether you implement it through a hardcoded 512Mhz or through a timebase value that gets live migrated and then compared is up to you - both ways work for me :).

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

The latter might be handy if we want to support migration on 970, though we don't have the TBU40 stuff there so adjusting the TB in the host kernel would be ... problematic.

Well, we could save/restore TB when we enter/exit the guest, no?

Hard to do without introducing drift...

We could probably quantify the drift through DEC. But I'm personally not too eager to worry about live migration on 970 :).

Alex

#26 lore raw
In reply to Alexander Graf
Quoted 39 lines · Alexander Graf

Am 09.09.2013 um 07:58 schrieb Alexey Kardashevskiy <[email protected]>:

Quoted 35 lines · Alexander Graf
Quoted 34 lines

Am 09.09.2013 um 04:40 schrieb Alexey Kardashevskiy <[email protected]>:

Quoted 29 lines · Alexander Graf
Quoted 16 lines · Benjamin Herrenschmidt
Quoted 14 lines · Andreas Färber
Quoted 11 lines

Are you thinking of POWER8 having a different frequency than POWER8 in compat mode? Because migration from one -cpu to another is not supported elsewhere.

Even if we want to migrate from one POWER7 revision to another, we should let the destination use the revision of the source (guest ABI!), via property if need be. Anything else will lead to confusion as to what is supported and what is not. That -cpu host is the default for convenience shouldn't relieve admins/libvirt to think about sensible setups like they have to on x86.

Quoted 4 lines
Quoted 2 lines

Besides POWER8 uses 512Mhz too :-) It's been architected so it's unlikely to change from now on.

Ok, ditch the tb frequency then. We can always default to 512Mhz when it's absent.

QEMU uses what kvmppc_get_tbfreq() returns. And ppc_tb_t carries this value. It does not use 512MHz automatically but migration should always assume 512MHz :-/

If we remove it, I would like to add assert(tb_freq!=512MHz) into timebase_pre_save() and timebase_post_load(), is that ok?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Good point. This would break TCG migration, right?

In TCG we do not need any timebase offset at all, the time should migrate as is, no? :)

I hope so, but we need to make sure we don't assert() in that case :).

Hrm.. that doesn't sound right. Whether you have KVM or TCG, what you need in the migration stream is enough data points that you can deduce the guest timebase from the host real time. On KVM that translates into a diff between guest and host timebase, but on TCG you'll need to implement that differently

Quoted 9 lines
Quoted 2 lines

It could break something like power5 migration under PR KVM, do not know...

Well, today we don't support full migration with PR KVM yet - IIRC we don't have access to all state. But that may change in the future.

I think it's ok to restrict live migration to machines with the same tb frequency when kvm is enabled. Whether you implement it through a hardcoded 512Mhz or through a timebase value that gets live migrated and then compared is up to you - both ways work for me :).

I don't think it's possible to allow KVM migration between hosts of different tb frequency. But I think encoding the frequency in the stream and verifying it is a better option. It might be useful for migration on 970, and it might be useful for TCG migration and it might be useful for TCG<->KVM migration. (For a TCG migration destination you *can* potentially set the tb frequency according to what you're told).

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#27 lore raw
Quoted 46 lines · David Gibson
Quoted 45 lines · Alexander Graf

Am 09.09.2013 um 07:58 schrieb Alexey Kardashevskiy <[email protected]>:

Quoted 40 lines · Alexander Graf
Quoted 27 lines

Am 09.09.2013 um 04:40 schrieb Alexey Kardashevskiy <[email protected]>:

Quoted 23 lines · Alexander Graf
Quoted 20 lines · Benjamin Herrenschmidt
Quoted 14 lines · Andreas Färber
Quoted 11 lines

Are you thinking of POWER8 having a different frequency than POWER8 in compat mode? Because migration from one -cpu to another is not supported elsewhere.

Even if we want to migrate from one POWER7 revision to another, we should let the destination use the revision of the source (guest ABI!), via property if need be. Anything else will lead to confusion as to what is supported and what is not. That -cpu host is the default for convenience shouldn't relieve admins/libvirt to think about sensible setups like they have to on x86.

Quoted 2 lines

Besides POWER8 uses 512Mhz too :-) It's been architected so it's unlikely to change from now on.

Ok, ditch the tb frequency then. We can always default to 512Mhz when it's absent.

Quoted 8 lines
Quoted 6 lines

QEMU uses what kvmppc_get_tbfreq() returns. And ppc_tb_t carries this value. It does not use 512MHz automatically but migration should always assume 512MHz :-/

If we remove it, I would like to add assert(tb_freq!=512MHz) into timebase_pre_save() and timebase_post_load(), is that ok?

Good point. This would break TCG migration, right?

In TCG we do not need any timebase offset at all, the time should migrate as is, no? :)

Quoted 7 lines
Quoted 1 line

I hope so, but we need to make sure we don't assert() in that case :).

Hrm.. that doesn't sound right. Whether you have KVM or TCG, what you need in the migration stream is enough data points that you can deduce the guest timebase from the host real time. On KVM that translates into a diff between guest and host timebase, but on TCG you'll need to implement that differently

I'd claim that for TCG we don't care about accuracy of the time base across live migration, so I'd be happy to simply ignore the adjustments we'd do with KVM there. Otherwise things would get heavily complicated.

Quoted 11 lines
Quoted 10 lines
Quoted 3 lines

It could break something like power5 migration under PR KVM, do not know...

Well, today we don't support full migration with PR KVM yet - IIRC we don't have access to all state. But that may change in the future.

I think it's ok to restrict live migration to machines with the same tb frequency when kvm is enabled. Whether you implement it through a hardcoded 512Mhz or through a timebase value that gets live migrated and then compared is up to you - both ways work for me :).

Quoted 7 lines

I don't think it's possible to allow KVM migration between hosts of different tb frequency. But I think encoding the frequency in the stream and verifying it is a better option. It might be useful for migration on 970, and it might be useful for TCG migration and it might be useful for TCG<->KVM migration. (For a TCG migration destination you *can* potentially set the tb frequency according to what you're told).

Yup, IIUC that's what Alexey's gut feeling told him too (and I concur) and is what he's doing ;).

Alex

#28 lore raw
In reply to Alexey Kardashevskiy
Quoted 5 lines · Alexey Kardashevskiy
Quoted 5 lines
Quoted 4 lines

I do not have pure guest timebase in QEMU and I need it on the destination. But I have host timebase + offset to calculate it. And tb_offset is already in ppc_tb_t. It looked logical to me to send the existing field and add only the missing part.

Quoted 7 lines
Quoted 1 line

I still don't understand. You want the guest visible timebase in the migration stream, no?

Yes. I do not really understand the problem here (and I am not playing dump). Do you suggest sending just the guest timebase and do not send the host timebase and the offset (one number instead of two)? I can do that, makes sense, no problem, thanks for the idea.

No, we want to send the guest TB and the corresponding "time of day" so that the target can adjust the TB based on how long the migration took.

Cheers,
Ben.

#29 lore raw
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 8 lines · Alexey Kardashevskiy
Quoted 6 lines
Quoted 4 lines

I do not have pure guest timebase in QEMU and I need it on the destination. But I have host timebase + offset to calculate it. And tb_offset is already in ppc_tb_t. It looked logical to me to send the existing field and add only the missing part.

I still don't understand. You want the guest visible timebase in the migration stream, no?

Quoted 7 lines
Quoted 4 lines

Yes. I do not really understand the problem here (and I am not playing dump). Do you suggest sending just the guest timebase and do not send the host timebase and the offset (one number instead of two)? I can do that, makes sense, no problem, thanks for the idea.

No, we want to send the guest TB and the corresponding "time of day" so that the target can adjust the TB based on how long the migration took.

Nobody is discussing time_of_the_day, it is there and transmitted. The only question is whether to send host_timebase and tb_offset (2 values) or just guest timebase (1 value).

Signature · 1 line
Alexey
#30 lore raw
In reply to Alexey Kardashevskiy
Quoted 74 lines · Alexey Kardashevskiy
Quoted 73 lines · David Gibson
Quoted 71 lines · Alexey Kardashevskiy

This allows guests to have a different timebase origin from the host.

This is needed for migration, where a guest can migrate from one host to another and the two hosts might have a different timebase origin. However, the timebase seen by the guest must not go backwards, and should go forwards only by a small amount corresponding to the time taken for the migration.

This is only supported for recent POWER hardware which has the TBU40 (timebase upper 40 bits) register. That includes POWER6, 7, 8 but not 970.

This adds kvm_access_one_reg() to access a special register which is not in env->spr.

The feature must be present in the host kernel.

---

This is an RFC but not a final patch. Can break something but I just do not see what.

---
 hw/ppc/ppc.c         | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/ppc/ppc.h |  4 ++++
 target-ppc/kvm.c     | 23 +++++++++++++++++++++++
 target-ppc/machine.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 trace-events         |  3 +++
 5 files changed, 123 insertions(+)
hw/ppc/ppc.c +25 -0
index 1e3cab3..7d08c9a 100644
@@ -31,6 +31,7 @@
 #include "hw/loader.h"
 #include "sysemu/kvm.h"
 #include "kvm_ppc.h"
+#include "trace.h"
 
 //#define PPC_DEBUG_IRQ
 #define PPC_DEBUG_TB
@@ -796,6 +797,54 @@ static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq)
     cpu_ppc_store_purr(cpu, 0x0000000000000000ULL);
 }
 
+/*
+ * Calculate timebase on the destination side of migration
 
+ * We calculate new timebase offset as shown below:
+ * 1) Gtb2 = Gtb1 + max(tod2 - tod1, 0)
+ *    Gtb2 = tb2 + off2
+ *    Gtb1 = tb1 + off1
+ * 2) tb2 + off2 = tb1 + off1 + max(tod2 - tod1, 0)
+ * 3) off2 = tb1 - tb2 + off1 + max(tod2 - tod1, 0)
+ *
+ * where:
+ * Gtb2 - destination guest timebase
+ * tb2 - destination host timebase
+ * off2 - destination timebase offset
+ * tod2 - destination time of the day
+ * Gtb1 - source guest timebase
+ * tb1 - source host timebase
+ * off1 - source timebase offset
+ * tod1 - source time of the day
+ *
+ * The result we want is in @off2
+ *
+ * Two conditions must be met for @off2:
+ * 1) off2 must be multiple of 2^24 ticks as it will be set via TBU40 SPR
+ * 2) Gtb2 >= Gtb1
Quoted 6 lines
Quoted 2 lines

What about the TCG case, where there is not host timebase, only a a host system clock?

cpu_get_real_ticks() returns ticks, this is what the patch cares about. What is the difference between KVM and TCG here?

Hrm. It may just be a question of variable naming and comments. But my point is that "host tb" is not a concept that always exists, so it shouldn't appear in anything external like a migration stream.

Quoted 189 lines
Quoted 31 lines
Quoted 31 lines
hw/ppc/ppc.c +23 -0
+ */
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env)
+{
+    uint64_t tb2, tod2, off2;
+    int ratio = tb_env->tb_freq / 1000000;
+    struct timeval tv;
+
+    tb2 = cpu_get_real_ticks();
+    gettimeofday(&tv, NULL);
+    tod2 = tv.tv_sec * 1000000 + tv.tv_usec;
+
+    off2 = tb_env->timebase - tb2 + tb_env->tb_offset;
+    if (tod2 > tb_env->time_of_the_day) {
+        off2 += (tod2 - tb_env->time_of_the_day) * ratio;
+    }
+    off2 = ROUND_UP(off2, 1 << 24);
+
+    trace_ppc_tb_adjust(tb_env->tb_offset, off2,
+                        (int64_t)off2 - tb_env->tb_offset);
+
+    tb_env->tb_offset = off2;
+}
+
 /* Set up (once) timebase frequency (in Hz) */
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq)
 {
include/hw/ppc/ppc.h +0 -0
index 132ab97..235871c 100644
@@ -32,6 +32,9 @@ struct ppc_tb_t {
Quoted 8 lines

include/hw/ppc/ppc.h struct ppc_tb_t { · line 37

Quoted 6 lines
include/hw/ppc/ppc.h +3 -0
     uint64_t purr_start;
     void *opaque;
     uint32_t flags;
+    /* Cached values for live migration purposes */
+    uint64_t timebase;
+    uint64_t time_of_the_day;

How is the time of day encoded here?

Microseconds. I'll put a comment here, I just thought it is quite obvious as gettimeofday() returns microseconds.

Quoted 27 lines
Quoted 27 lines
include/hw/ppc/ppc.h +1 -0
 };
 
 /* PPC Timers flags */
@@ -46,6 +49,7 @@ struct ppc_tb_t {
                                                */
 
 uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, int64_t tb_offset);
+void cpu_ppc_adjust_tb_offset(ppc_tb_t *tb_env);
 clk_setup_cb cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq);
 /* Embedded PowerPC DCR management */
 typedef uint32_t (*dcr_read_cb)(void *opaque, int dcrn);
target-ppc/kvm.c +1 -0
index 7af9e3d..93df955 100644
@@ -35,6 +35,7 @@
 #include "hw/sysbus.h"
 #include "hw/ppc/spapr.h"
 #include "hw/ppc/spapr_vio.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/watchdog.h"
 
 //#define DEBUG_KVM
@@ -761,6 +762,22 @@ static int kvm_put_vpa(CPUState *cs)
 }
 #endif /* TARGET_PPC64 */
 
Quoted 5 lines

target-ppc/kvm.c static int kvm_put_vpa(CPUState *cs) · line 766

Quoted 2 lines
target-ppc/kvm.c +1 -0
+static int kvm_access_one_reg(CPUState *cs, bool set, __u64 id,
 void *addr)

I think it would be nicer to have seperate set_one_reg and get_one_reg functions, rather than using a direction parameter.

I am regularly getting requests from Alex to merge functions which look almost the same. Please do not make it worse :)

Quoted 23 lines
Quoted 23 lines
target-ppc/kvm.c +15 -0
+{
+    struct kvm_one_reg reg = {
+        .id = id,
+        .addr = (uintptr_t)addr,
+    };
+    int ret = kvm_vcpu_ioctl(cs, set ? KVM_SET_ONE_REG : KVM_GET_ONE_REG, &reg);
+
+    if (ret) {
+        DPRINTF("Unable to %s time base offset to KVM: %s\n",
+                set ? "set" : "get", strerror(errno));
+    }
+
+    return ret;
+}
+
 int kvm_arch_put_registers(CPUState *cs, int level)
 {
     PowerPCCPU *cpu = POWERPC_CPU(cs);
target-ppc/kvm.c +1 -0
@@ -873,6 +890,9 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                 DPRINTF("Warning: Unable to set VPA information to KVM\n");
             }
         }
+
Quoted 8 lines

target-ppc/kvm.c int kvm_arch_put_registers(CPUState *cs, int level) · line 895

Quoted 2 lines
target-ppc/kvm.c +2 -0
+        kvm_access_one_reg(cs, 1, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);

Hrm. It may be too late, but would it make more sense for qemu to just calculate the "ideal" offset - not 24-bit aligned, and have the kernel round that up to a value suitable for TBU40. I'm thinking that might be more robust if someone decides that POWER10 should have a TBU50 or a TBU30 instead of TBU40.

No, not late, the kernel patchset has not been noticed yet by anyone :) Just a little remark here - QEMU sets one value to the kernel as an offset but when it will be about to migrate again and read this offset from the kernel, it will be different (rounded up) from what QEMU set. Not a problem though.

Quoted 56 lines
Quoted 56 lines
target-ppc/kvm.c +3 -0
 #endif /* TARGET_PPC64 */
     }
 
@@ -1082,6 +1102,9 @@ int kvm_arch_get_registers(CPUState *cs)
                 DPRINTF("Warning: Unable to get VPA information from KVM\n");
             }
         }
+
+        kvm_access_one_reg(cs, 0, KVM_REG_PPC_TB_OFFSET,
+                           &env->tb_env->tb_offset);
 #endif
     }
 
target-ppc/machine.c +29 -0
index 12e1512..d1ffc7f 100644
@@ -1,5 +1,6 @@
 #include "hw/hw.h"
 #include "hw/boards.h"
+#include "hw/ppc/ppc.h"
 #include "sysemu/kvm.h"
 #include "helper_regs.h"
 
@@ -459,6 +460,45 @@ static const VMStateDescription vmstate_tlbmas = {
     }
 };
 
+static void timebase_pre_save(void *opaque)
+{
+    ppc_tb_t *tb_env = opaque;
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+    tb_env->time_of_the_day = tv.tv_sec * 1000000 + tv.tv_usec;
+    tb_env->timebase = cpu_get_real_ticks();
+}
+
+static int timebase_post_load(void *opaque, int version_id)
+{
+    ppc_tb_t *tb_env = opaque;
+
+    if (!tb_env) {
+        printf("NO TB!\n");
+        return -1;
+    }
+    cpu_ppc_adjust_tb_offset(tb_env);
+
+    return 0;
+}
+
+static const VMStateDescription vmstate_timebase = {
+    .name = "cpu/timebase",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
Quoted 8 lines

target-ppc/machine.c static const VMStateDescription vmstate_tlbmas = { · line 495

Quoted 5 lines
target-ppc/machine.c +5 -0
+    .pre_save = timebase_pre_save,
+    .post_load = timebase_post_load,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(timebase, ppc_tb_t),
+        VMSTATE_INT64(tb_offset, ppc_tb_t),

tb_offset is inherently a local concept, since it depends on the host timebase. So how can it belong in the migration stream?

Quoted 4 lines

I do not have pure guest timebase in QEMU and I need it on the destination. But I have host timebase + offset to calculate it. And tb_offset is already in ppc_tb_t. It looked logical to me to send the existing field and add only the missing part.

Yeah, as others have said though, guest timebase is the right thing to send, though, since the host timebase is not relevant to the other end of the migration and won't even exist in the case of TCG.

Quoted 37 lines
Quoted 35 lines
Quoted 34 lines
target-ppc/machine.c +10 -0
+        VMSTATE_UINT64(time_of_the_day, ppc_tb_t),
+        VMSTATE_UINT32_EQUAL(tb_freq, ppc_tb_t),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 const VMStateDescription vmstate_ppc_cpu = {
     .name = "cpu",
     .version_id = 5,
@@ -498,6 +538,10 @@ const VMStateDescription vmstate_ppc_cpu = {
         VMSTATE_UINT64_EQUAL(env.insns_flags, PowerPCCPU),
         VMSTATE_UINT64_EQUAL(env.insns_flags2, PowerPCCPU),
         VMSTATE_UINT32_EQUAL(env.nb_BATs, PowerPCCPU),
+
+        /* Time offset */
+        VMSTATE_STRUCT_POINTER(env.tb_env, PowerPCCPU,
+                               vmstate_timebase, ppc_tb_t *),
         VMSTATE_END_OF_LIST()
     },
     .subsections = (VMStateSubsection []) {
trace-events +3 -0
index 935b953..24cf4d2 100644
@@ -1141,6 +1141,9 @@ spapr_iommu_stuff(uint64_t liobn, uint64_t ioba, uint64_t tce, uint64_t ret) "li
 spapr_iommu_xlate(uint64_t liobn, uint64_t ioba, uint64_t tce, unsigned perm, unsigned pgsize) "liobn=%"PRIx64" 0x%"PRIx64" -> 0x%"PRIx64" perm=%u mask=%x"
 spapr_iommu_new_table(uint64_t liobn, void *tcet, void *table, int fd) "liobn=%"PRIx64" tcet=%p table=%p fd=%d"
 
+# hw/ppc/ppc.c
+ppc_tb_adjust(uint64_t offs1, uint64_t offs2, int64_t diff) "adjusted from %"PRIx64" to %"PRIx64", diff %"PRId64
+
 # util/hbitmap.c
 hbitmap_iter_skip_words(const void *hb, void *hbi, uint64_t pos, unsigned long cur) "hb %p hbi %p pos %"PRId64" cur 0x%lx"
 hbitmap_reset(void *hb, uint64_t start, uint64_t count, uint64_t sbit, uint64_t ebit) "hb %p items %"PRIu64",%"PRIu64" bits %"PRIu64"..%"PRIu64
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson