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

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