yeahuh / rpms / qemu-kvm

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