target/arm: fix crash on pmu register access
[qemu/ar7.git] / hw / s390x / tod-kvm.c
blob2456bf7b24e299c5ec9e99ab9935c1f5a6a8029d
1 /*
2 * TOD (Time Of Day) clock - KVM implementation
4 * Copyright 2018 Red Hat, Inc.
5 * Author(s): David Hildenbrand <david@redhat.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "sysemu/sysemu.h"
14 #include "hw/s390x/tod.h"
15 #include "kvm_s390x.h"
17 static void kvm_s390_get_tod_raw(S390TOD *tod, Error **errp)
19 int r;
21 r = kvm_s390_get_clock_ext(&tod->high, &tod->low);
22 if (r == -ENXIO) {
23 r = kvm_s390_get_clock(&tod->high, &tod->low);
25 if (r) {
26 error_setg(errp, "Unable to get KVM guest TOD clock: %s",
27 strerror(-r));
31 static void kvm_s390_tod_get(const S390TODState *td, S390TOD *tod, Error **errp)
33 if (td->stopped) {
34 *tod = td->base;
35 return;
38 kvm_s390_get_tod_raw(tod, errp);
41 static void kvm_s390_set_tod_raw(const S390TOD *tod, Error **errp)
43 int r;
45 r = kvm_s390_set_clock_ext(tod->high, tod->low);
46 if (r == -ENXIO) {
47 r = kvm_s390_set_clock(tod->high, tod->low);
49 if (r) {
50 error_setg(errp, "Unable to set KVM guest TOD clock: %s",
51 strerror(-r));
55 static void kvm_s390_tod_set(S390TODState *td, const S390TOD *tod, Error **errp)
57 Error *local_err = NULL;
60 * Somebody (e.g. migration) set the TOD. We'll store it into KVM to
61 * properly detect errors now but take a look at the runstate to decide
62 * whether really to keep the tod running. E.g. during migration, this
63 * is the point where we want to stop the initially running TOD to fire
64 * it back up when actually starting the migrated guest.
66 kvm_s390_set_tod_raw(tod, &local_err);
67 if (local_err) {
68 error_propagate(errp, local_err);
69 return;
72 if (runstate_is_running()) {
73 td->stopped = false;
74 } else {
75 td->stopped = true;
76 td->base = *tod;
80 static void kvm_s390_tod_vm_state_change(void *opaque, int running,
81 RunState state)
83 S390TODState *td = opaque;
84 Error *local_err = NULL;
86 if (running && td->stopped) {
87 /* Set the old TOD when running the VM - start the TOD clock. */
88 kvm_s390_set_tod_raw(&td->base, &local_err);
89 if (local_err) {
90 warn_report_err(local_err);
92 /* Treat errors like the TOD was running all the time. */
93 td->stopped = false;
94 } else if (!running && !td->stopped) {
95 /* Store the TOD when stopping the VM - stop the TOD clock. */
96 kvm_s390_get_tod_raw(&td->base, &local_err);
97 if (local_err) {
98 /* Keep the TOD running in case we could not back it up. */
99 warn_report_err(local_err);
100 } else {
101 td->stopped = true;
106 static void kvm_s390_tod_realize(DeviceState *dev, Error **errp)
108 S390TODState *td = S390_TOD(dev);
109 S390TODClass *tdc = S390_TOD_GET_CLASS(td);
110 Error *local_err = NULL;
112 tdc->parent_realize(dev, &local_err);
113 if (local_err) {
114 error_propagate(errp, local_err);
115 return;
119 * We need to know when the VM gets started/stopped to start/stop the TOD.
120 * As we can never have more than one TOD instance (and that will never be
121 * removed), registering here and never unregistering is good enough.
123 qemu_add_vm_change_state_handler(kvm_s390_tod_vm_state_change, td);
126 static void kvm_s390_tod_class_init(ObjectClass *oc, void *data)
128 S390TODClass *tdc = S390_TOD_CLASS(oc);
130 device_class_set_parent_realize(DEVICE_CLASS(oc), kvm_s390_tod_realize,
131 &tdc->parent_realize);
132 tdc->get = kvm_s390_tod_get;
133 tdc->set = kvm_s390_tod_set;
136 static void kvm_s390_tod_init(Object *obj)
138 S390TODState *td = S390_TOD(obj);
141 * The TOD is initially running (value stored in KVM). Avoid needless
142 * loading/storing of the TOD when starting a simple VM, so let it
143 * run although the (never started) VM is stopped. For migration, we
144 * will properly set the TOD later.
146 td->stopped = false;
149 static TypeInfo kvm_s390_tod_info = {
150 .name = TYPE_KVM_S390_TOD,
151 .parent = TYPE_S390_TOD,
152 .instance_size = sizeof(S390TODState),
153 .instance_init = kvm_s390_tod_init,
154 .class_init = kvm_s390_tod_class_init,
155 .class_size = sizeof(S390TODClass),
158 static void register_types(void)
160 type_register_static(&kvm_s390_tod_info);
162 type_init(register_types);