Blame SOURCES/kvm-s390x-tcg-properly-implement-the-TOD.patch

28c80a
From f87cbf4e70d1df758df90f56934a39a0a018e2cc Mon Sep 17 00:00:00 2001
28c80a
From: David Hildenbrand <david@redhat.com>
28c80a
Date: Fri, 21 Dec 2018 15:39:50 +0100
28c80a
Subject: [PATCH 06/14] s390x/tcg: properly implement the TOD
28c80a
28c80a
RH-Author: David Hildenbrand <david@redhat.com>
28c80a
Message-id: <20181221153957.28183-6-david@redhat.com>
28c80a
Patchwork-id: 83760
28c80a
O-Subject: [RHEL-7.6.z qemu-kvm-ma PATCH 05/12] s390x/tcg: properly implement the TOD
28c80a
Bugzilla: 1672920
28c80a
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
28c80a
RH-Acked-by: Thomas Huth <thuth@redhat.com>
28c80a
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
28c80a
28c80a
Right now, each CPU has its own TOD. Especially, the TOD will differ
28c80a
based on creation time of a CPU - e.g. when hotplugging a CPU the times
28c80a
will differ quite a lot, resulting in stall warnings in the guest.
28c80a
28c80a
Let's use a single TOD by implementing our new TOD device. Prepare it
28c80a
for TOD-clock epoch extension.
28c80a
28c80a
Most importantly, whenever we set the TOD, we have to update the CKC
28c80a
timer.
28c80a
28c80a
Introduce "tcg_s390x.h" just like "kvm_s390x.h" for tcg specific
28c80a
function declarations that should not go into cpu.h.
28c80a
28c80a
Reviewed-by: Thomas Huth <thuth@redhat.com>
28c80a
Signed-off-by: David Hildenbrand <david@redhat.com>
28c80a
Message-Id: <20180627134410.4901-6-david@redhat.com>
28c80a
Signed-off-by: Cornelia Huck <cohuck@redhat.com>
28c80a
(cherry picked from commit 7de3b1cdc67dcb572c1761c2051252e91a438b22)
28c80a
Signed-off-by: David Hildenbrand <david@redhat.com>
28c80a
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
28c80a
---
28c80a
 hw/s390x/tod-qemu.c        | 46 ++++++++++++++++++++++++++++++++++++++++++----
28c80a
 hw/s390x/tod.c             | 11 +++++++++++
28c80a
 include/hw/s390x/tod.h     | 19 +++++++++++++++++++
28c80a
 target/s390x/cpu.c         |  7 -------
28c80a
 target/s390x/cpu.h         |  1 -
28c80a
 target/s390x/internal.h    | 16 ----------------
28c80a
 target/s390x/misc_helper.c | 25 +++++++++++++++++++------
28c80a
 target/s390x/tcg_s390x.h   | 18 ++++++++++++++++++
28c80a
 8 files changed, 109 insertions(+), 34 deletions(-)
28c80a
 create mode 100644 target/s390x/tcg_s390x.h
28c80a
28c80a
diff --git a/hw/s390x/tod-qemu.c b/hw/s390x/tod-qemu.c
28c80a
index 03ea1ce..59c015c 100644
28c80a
--- a/hw/s390x/tod-qemu.c
28c80a
+++ b/hw/s390x/tod-qemu.c
28c80a
@@ -11,19 +11,43 @@
28c80a
 #include "qemu/osdep.h"
28c80a
 #include "qapi/error.h"
28c80a
 #include "hw/s390x/tod.h"
28c80a
+#include "qemu/timer.h"
28c80a
+#include "qemu/cutils.h"
28c80a
+#include "cpu.h"
28c80a
+#include "tcg_s390x.h"
28c80a
 
28c80a
 static void qemu_s390_tod_get(const S390TODState *td, S390TOD *tod,
28c80a
                               Error **errp)
28c80a
 {
28c80a
-    /* FIXME */
28c80a
-    tod->high = 0;
28c80a
-    tod->low = 0;
28c80a
+    *tod = td->base;
28c80a
+
28c80a
+    tod->low += time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
28c80a
+    if (tod->low < td->base.low) {
28c80a
+        tod->high++;
28c80a
+    }
28c80a
 }
28c80a
 
28c80a
 static void qemu_s390_tod_set(S390TODState *td, const S390TOD *tod,
28c80a
                               Error **errp)
28c80a
 {
28c80a
-    /* FIXME */
28c80a
+    CPUState *cpu;
28c80a
+
28c80a
+    td->base = *tod;
28c80a
+
28c80a
+    td->base.low -= time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
28c80a
+    if (td->base.low > tod->low) {
28c80a
+        td->base.high--;
28c80a
+    }
28c80a
+
28c80a
+    /*
28c80a
+     * The TOD has been changed and we have to recalculate the CKC values
28c80a
+     * for all CPUs. We do this asynchronously, as "SET CLOCK should be
28c80a
+     * issued only while all other activity on all CPUs .. has been
28c80a
+     * suspended".
28c80a
+     */
28c80a
+    CPU_FOREACH(cpu) {
28c80a
+        async_run_on_cpu(cpu, tcg_s390_tod_updated, RUN_ON_CPU_NULL);
28c80a
+    }
28c80a
 }
28c80a
 
28c80a
 static void qemu_s390_tod_class_init(ObjectClass *oc, void *data)
28c80a
@@ -34,10 +58,24 @@ static void qemu_s390_tod_class_init(ObjectClass *oc, void *data)
28c80a
     tdc->set = qemu_s390_tod_set;
28c80a
 }
28c80a
 
28c80a
+static void qemu_s390_tod_init(Object *obj)
28c80a
+{
28c80a
+    S390TODState *td = S390_TOD(obj);
28c80a
+    struct tm tm;
28c80a
+
28c80a
+    qemu_get_timedate(&tm, 0);
28c80a
+    td->base.high = 0;
28c80a
+    td->base.low = TOD_UNIX_EPOCH + (time2tod(mktimegm(&tm)) * 1000000000ULL);
28c80a
+    if (td->base.low < TOD_UNIX_EPOCH) {
28c80a
+        td->base.high += 1;
28c80a
+    }
28c80a
+}
28c80a
+
28c80a
 static TypeInfo qemu_s390_tod_info = {
28c80a
     .name = TYPE_QEMU_S390_TOD,
28c80a
     .parent = TYPE_S390_TOD,
28c80a
     .instance_size = sizeof(S390TODState),
28c80a
+    .instance_init = qemu_s390_tod_init,
28c80a
     .class_init = qemu_s390_tod_class_init,
28c80a
     .class_size = sizeof(S390TODClass),
28c80a
 };
28c80a
diff --git a/hw/s390x/tod.c b/hw/s390x/tod.c
28c80a
index 0501aff..1c63f41 100644
28c80a
--- a/hw/s390x/tod.c
28c80a
+++ b/hw/s390x/tod.c
28c80a
@@ -30,6 +30,17 @@ void s390_init_tod(void)
28c80a
     qdev_init_nofail(DEVICE(obj));
28c80a
 }
28c80a
 
28c80a
+S390TODState *s390_get_todstate(void)
28c80a
+{
28c80a
+    static S390TODState *ts;
28c80a
+
28c80a
+    if (!ts) {
28c80a
+        ts = S390_TOD(object_resolve_path_type("", TYPE_S390_TOD, NULL));
28c80a
+    }
28c80a
+
28c80a
+    return ts;
28c80a
+}
28c80a
+
28c80a
 #define S390_TOD_CLOCK_VALUE_MISSING    0x00
28c80a
 #define S390_TOD_CLOCK_VALUE_PRESENT    0x01
28c80a
 
28c80a
diff --git a/include/hw/s390x/tod.h b/include/hw/s390x/tod.h
28c80a
index 7096b57..413c0d7 100644
28c80a
--- a/include/hw/s390x/tod.h
28c80a
+++ b/include/hw/s390x/tod.h
28c80a
@@ -30,6 +30,9 @@ typedef struct S390TOD {
28c80a
 typedef struct S390TODState {
28c80a
     /* private */
28c80a
     DeviceState parent_obj;
28c80a
+
28c80a
+    /* unused by KVM implementation */
28c80a
+    S390TOD base;
28c80a
 } S390TODState;
28c80a
 
28c80a
 typedef struct S390TODClass {
28c80a
@@ -41,6 +44,22 @@ typedef struct S390TODClass {
28c80a
     void (*set)(S390TODState *td, const S390TOD *tod, Error **errp);
28c80a
 } S390TODClass;
28c80a
 
28c80a
+/* The value of the TOD clock for 1.1.1970. */
28c80a
+#define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
28c80a
+
28c80a
+/* Converts ns to s390's clock format */
28c80a
+static inline uint64_t time2tod(uint64_t ns)
28c80a
+{
28c80a
+    return (ns << 9) / 125 + (((ns & 0xff10000000000000ull) / 125) << 9);
28c80a
+}
28c80a
+
28c80a
+/* Converts s390's clock format to ns */
28c80a
+static inline uint64_t tod2time(uint64_t t)
28c80a
+{
28c80a
+    return ((t >> 9) * 125) + (((t & 0x1ff) * 125) >> 9);
28c80a
+}
28c80a
+
28c80a
 void s390_init_tod(void);
28c80a
+S390TODState *s390_get_todstate(void);
28c80a
 
28c80a
 #endif
28c80a
diff --git a/target/s390x/cpu.c b/target/s390x/cpu.c
28c80a
index 5570741..f74d2a3 100644
28c80a
--- a/target/s390x/cpu.c
28c80a
+++ b/target/s390x/cpu.c
28c80a
@@ -30,7 +30,6 @@
28c80a
 #include "kvm_s390x.h"
28c80a
 #include "sysemu/kvm.h"
28c80a
 #include "qemu-common.h"
28c80a
-#include "qemu/cutils.h"
28c80a
 #include "qemu/timer.h"
28c80a
 #include "qemu/error-report.h"
28c80a
 #include "trace.h"
28c80a
@@ -276,9 +275,6 @@ static void s390_cpu_initfn(Object *obj)
28c80a
     CPUState *cs = CPU(obj);
28c80a
     S390CPU *cpu = S390_CPU(obj);
28c80a
     CPUS390XState *env = &cpu->env;
28c80a
-#if !defined(CONFIG_USER_ONLY)
28c80a
-    struct tm tm;
28c80a
-#endif
28c80a
 
28c80a
     cs->env_ptr = env;
28c80a
     cs->halted = 1;
28c80a
@@ -287,9 +283,6 @@ static void s390_cpu_initfn(Object *obj)
28c80a
                         s390_cpu_get_crash_info_qom, NULL, NULL, NULL, NULL);
28c80a
     s390_cpu_model_register_props(obj);
28c80a
 #if !defined(CONFIG_USER_ONLY)
28c80a
-    qemu_get_timedate(&tm, 0);
28c80a
-    env->tod_offset = TOD_UNIX_EPOCH +
28c80a
-                      (time2tod(mktimegm(&tm)) * 1000000000ULL);
28c80a
     env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
28c80a
     env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
28c80a
     s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
28c80a
diff --git a/target/s390x/cpu.h b/target/s390x/cpu.h
28c80a
index 04f9adf..6500f42 100644
28c80a
--- a/target/s390x/cpu.h
28c80a
+++ b/target/s390x/cpu.h
28c80a
@@ -133,7 +133,6 @@ struct CPUS390XState {
28c80a
     uint64_t cpuid;
28c80a
 #endif
28c80a
 
28c80a
-    uint64_t tod_offset;
28c80a
     QEMUTimer *tod_timer;
28c80a
 
28c80a
     QEMUTimer *cpu_timer;
28c80a
diff --git a/target/s390x/internal.h b/target/s390x/internal.h
28c80a
index d1ed06f..61a509d 100644
28c80a
--- a/target/s390x/internal.h
28c80a
+++ b/target/s390x/internal.h
28c80a
@@ -237,22 +237,6 @@ enum cc_op {
28c80a
     CC_OP_MAX
28c80a
 };
28c80a
 
28c80a
-/* The value of the TOD clock for 1.1.1970. */
28c80a
-#define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
28c80a
-
28c80a
-/* Converts ns to s390's clock format */
28c80a
-static inline uint64_t time2tod(uint64_t ns)
28c80a
-{
28c80a
-    return (ns << 9) / 125 + (((ns & 0xff10000000000000ull) / 125) << 9);
28c80a
-
28c80a
-}
28c80a
-
28c80a
-/* Converts s390's clock format to ns */
28c80a
-static inline uint64_t tod2time(uint64_t t)
28c80a
-{
28c80a
-    return ((t >> 9) * 125) + (((t & 0x1ff) * 125) >> 9);
28c80a
-}
28c80a
-
28c80a
 static inline hwaddr decode_basedisp_s(CPUS390XState *env, uint32_t ipb,
28c80a
                                        uint8_t *ar)
28c80a
 {
28c80a
diff --git a/target/s390x/misc_helper.c b/target/s390x/misc_helper.c
28c80a
index 8b3b040..4f675c7 100644
28c80a
--- a/target/s390x/misc_helper.c
28c80a
+++ b/target/s390x/misc_helper.c
28c80a
@@ -29,6 +29,8 @@
28c80a
 #include "exec/address-spaces.h"
28c80a
 #include "exec/exec-all.h"
28c80a
 #include "exec/cpu_ldst.h"
28c80a
+#include "qapi/error.h"
28c80a
+#include "tcg_s390x.h"
28c80a
 
28c80a
 #if !defined(CONFIG_USER_ONLY)
28c80a
 #include "sysemu/cpus.h"
28c80a
@@ -40,6 +42,7 @@
28c80a
 #include "hw/s390x/ioinst.h"
28c80a
 #include "hw/s390x/s390-pci-inst.h"
28c80a
 #include "hw/boards.h"
28c80a
+#include "hw/s390x/tod.h"
28c80a
 #endif
28c80a
 
28c80a
 /* #define DEBUG_HELPER */
28c80a
@@ -139,17 +142,19 @@ void HELPER(spx)(CPUS390XState *env, uint64_t a1)
28c80a
 /* Store Clock */
28c80a
 uint64_t HELPER(stck)(CPUS390XState *env)
28c80a
 {
28c80a
-    uint64_t time;
28c80a
+    S390TODState *td = s390_get_todstate();
28c80a
+    S390TODClass *tdc = S390_TOD_GET_CLASS(td);
28c80a
+    S390TOD tod;
28c80a
 
28c80a
-    time = env->tod_offset +
28c80a
-        time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
28c80a
-
28c80a
-    return time;
28c80a
+    tdc->get(td, &tod, &error_abort);
28c80a
+    return tod.low;
28c80a
 }
28c80a
 
28c80a
 /* Set Clock Comparator */
28c80a
 void HELPER(sckc)(CPUS390XState *env, uint64_t time)
28c80a
 {
28c80a
+    S390TODState *td = s390_get_todstate();
28c80a
+
28c80a
     if (time == -1ULL) {
28c80a
         return;
28c80a
     }
28c80a
@@ -157,7 +162,7 @@ void HELPER(sckc)(CPUS390XState *env, uint64_t time)
28c80a
     env->ckc = time;
28c80a
 
28c80a
     /* difference between origins */
28c80a
-    time -= env->tod_offset;
28c80a
+    time -= td->base.low;
28c80a
 
28c80a
     /* nanoseconds */
28c80a
     time = tod2time(time);
28c80a
@@ -165,6 +170,14 @@ void HELPER(sckc)(CPUS390XState *env, uint64_t time)
28c80a
     timer_mod(env->tod_timer, time);
28c80a
 }
28c80a
 
28c80a
+void tcg_s390_tod_updated(CPUState *cs, run_on_cpu_data opaque)
28c80a
+{
28c80a
+    S390CPU *cpu = S390_CPU(cs);
28c80a
+    CPUS390XState *env = &cpu->env;
28c80a
+
28c80a
+    helper_sckc(env, env->ckc);
28c80a
+}
28c80a
+
28c80a
 /* Set Tod Programmable Field */
28c80a
 void HELPER(sckpf)(CPUS390XState *env, uint64_t r0)
28c80a
 {
28c80a
diff --git a/target/s390x/tcg_s390x.h b/target/s390x/tcg_s390x.h
28c80a
new file mode 100644
28c80a
index 0000000..4e308aa
28c80a
--- /dev/null
28c80a
+++ b/target/s390x/tcg_s390x.h
28c80a
@@ -0,0 +1,18 @@
28c80a
+/*
28c80a
+ * QEMU TCG support -- s390x specific functions.
28c80a
+ *
28c80a
+ * Copyright 2018 Red Hat, Inc.
28c80a
+ *
28c80a
+ * Authors:
28c80a
+ *   David Hildenbrand <david@redhat.com>
28c80a
+ *
28c80a
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
28c80a
+ * See the COPYING file in the top-level directory.
28c80a
+ */
28c80a
+
28c80a
+#ifndef TCG_S390X_H
28c80a
+#define TCG_S390X_H
28c80a
+
28c80a
+void tcg_s390_tod_updated(CPUState *cs, run_on_cpu_data opaque);
28c80a
+
28c80a
+#endif /* TCG_S390X_H */
28c80a
-- 
28c80a
1.8.3.1
28c80a