Skip to contentSkip to conversation

target/s390x: Fix missing clock-comparator interrupts

PATCH Superseded Ilya Leoshkevich 2 participants 7 messages Oct 15, 2025 – Oct 16, 2025

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover target/s390x: Fix missing clock-comparator interrupts

v1: https://lore.kernel.org/qemu-devel/[email protected]/
v1 -> v2: Add Thomas' R-b.
Cc: stable (Michael).
Improve formatting, commit messages, and test (Ilya).

Hi,

While trying to (so far unsuccessfully) reproduce [1], I found two bugs in the clock comparator handling. This series fixes both and adds a test.

[1] https://lore.kernel.org/lkml/[email protected]/

Best regards,
Ilya

Ilya Leoshkevich (3):
target/s390x: Fix missing interrupts for small CKC values target/s390x: Fix missing clock-comparator interrupts after reset tests/tcg/s390x: Test SET CLOCK COMPARATOR

 hw/s390x/s390-virtio-ccw.c              |  6 +--
 target/s390x/cpu.c                      |  8 ++++
 target/s390x/tcg/misc_helper.c          | 12 ++++--
 tests/tcg/s390x/Makefile.softmmu-target |  1 +
 tests/tcg/s390x/sckc.S                  | 55 +++++++++++++++++++++++++
 5 files changed, 75 insertions(+), 7 deletions(-)
 create mode 100644 tests/tcg/s390x/sckc.S
Signature · 1 line
2.51.0
#2 lore raw

PATCH v2 2/3 target/s390x: Fix missing clock-comparator interrupts after reset

After reset, CKC value is set to 0, so if clock-comparator interrupts are enabled, one should occur very shortly thereafter.

Currently the code does not set tod_timer, so this does not happen.

Fix by adding a tcg_s390_tod_updated() call. Initialize TOD clock before CPUs in order to ensure that the respective object exists during the CPU reset.

---
 hw/s390x/s390-virtio-ccw.c | 6 +++---
 target/s390x/cpu.c         | 8 ++++++++
 2 files changed, 11 insertions(+), 3 deletions(-)
hw/s390x/s390-virtio-ccw.c +3 -3
index ad2c48188a8..9f37a96ae5a 100644
@@ -286,6 +286,9 @@ static void ccw_init(MachineState *machine)
     /* init memory + setup max page size. Required for the CPU model */
     s390_memory_init(machine);
 
+    /* init the TOD clock */
+    s390_init_tod();
+
     /* init CPUs (incl. CPU model) early so s390_has_feature() works */
     s390_init_cpus(machine);
 
@@ -332,9 +335,6 @@ static void ccw_init(MachineState *machine)
         s390_create_sclpconsole(ms->sclp, "sclplmconsole", serial_hd(1));
     }
 
-    /* init the TOD clock */
-    s390_init_tod();
-
     /* init SCLP event Control-Program Identification */
     if (s390mc->use_cpi) {
         s390_create_sclpcpi(ms->sclp);
target/s390x/cpu.c +8 -0
index f05ce317da1..cc2de6ce08e 100644
@@ -40,6 +40,7 @@
 #include "system/reset.h"
 #endif
 #include "hw/s390x/cpu-topology.h"
+#include "tcg/tcg_s390x.h"
 
 #define CR0_RESET       0xE0UL
 #define CR14_RESET      0xC2000000UL;
@@ -215,6 +216,13 @@ static void s390_cpu_reset_hold(Object *obj, ResetType type)
             break;
         }
     }
+
+#ifndef CONFIG_USER_ONLY
+    if (tcg_enabled()) {
+        /* Rearm the CKC timer if necessary */
+        tcg_s390_tod_updated(CPU(cpu), RUN_ON_CPU_NULL);
+    }
+#endif
 }
 
 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/3 target/s390x: Fix missing interrupts for small CKC values

In reply to Ilya Leoshkevich

Suppose TOD clock value is 0x1111111111111111 and clock-comparator value is 0, in which case clock-comparator interruption should occur immediately.

With the current code, tod2time(env->ckc - td->base.low) ends up being a very large number, so this interruption never happens.

Fix by firing the timer immediately if env->ckc < td->base.low.

---
 target/s390x/tcg/misc_helper.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
target/s390x/tcg/misc_helper.c +8 -4
index 6d9d601d29a..215b5b9d933 100644
@@ -199,11 +199,15 @@ static void update_ckc_timer(CPUS390XState *env)
         return;
     }
 
-    /* difference between origins */
-    time = env->ckc - td->base.low;
+    if (env->ckc < td->base.low) {
+        time = 0;
+    } else {
+        /* difference between origins */
+        time = env->ckc - td->base.low;
 
-    /* nanoseconds */
-    time = tod2time(time);
+        /* nanoseconds */
+        time = tod2time(time);
+    }
 
     timer_mod(env->tod_timer, time);
 }
Signature · 1 line
2.51.0
#5 lore raw

PATCH v2 3/3 tests/tcg/s390x: Test SET CLOCK COMPARATOR

In reply to Ilya Leoshkevich

Add a small test to prevent regressions.

---
 tests/tcg/s390x/Makefile.softmmu-target |  1 +
 tests/tcg/s390x/sckc.S                  | 55 +++++++++++++++++++++++++
 2 files changed, 56 insertions(+)
 create mode 100644 tests/tcg/s390x/sckc.S
tests/tcg/s390x/Makefile.softmmu-target +1 -0
index 8cd4667c63b..a4425d3184a 100644
@@ -28,6 +28,7 @@ ASM_TESTS =                                                                    \
     mc                                                                         \
     per                                                                        \
     precise-smc-softmmu                                                        \
+    sckc                                                                       \
     ssm-early                                                                  \
     stosm-early                                                                \
     stpq                                                                       \
tests/tcg/s390x/sckc.S new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
2.51.0
#3 lore raw

Re: [PATCH v2 2/3] target/s390x: Fix missing clock-comparator interrupts after reset

In reply to Ilya Leoshkevich

Hi Ilya!

Quoted 8 lines · Ilya Leoshkevich

After reset, CKC value is set to 0, so if clock-comparator interrupts are enabled, one should occur very shortly thereafter.

Currently the code does not set tod_timer, so this does not happen.

Fix by adding a tcg_s390_tod_updated() call. Initialize TOD clock before CPUs in order to ensure that the respective object exists during the CPU reset.

Can this really happen? Looking at CPUS390XState in target/s390x/cpu.h, the ckc is next to the cregs[] in the start_initial_reset_fields section, so if ckc gets cleared, the cregs get cleared, too. I.e. if ckc gets set to 0 here, there is no way that the clock comparator interrupt could trigger immediately without the guest writing to the control registers first. So I think this patch is not really necessary. Or do I miss something?

Thomas

Quoted 56 lines
---
  hw/s390x/s390-virtio-ccw.c | 6 +++---
  target/s390x/cpu.c         | 8 ++++++++
  2 files changed, 11 insertions(+), 3 deletions(-)
hw/s390x/s390-virtio-ccw.c +3 -3
index ad2c48188a8..9f37a96ae5a 100644
@@ -286,6 +286,9 @@ static void ccw_init(MachineState *machine)
      /* init memory + setup max page size. Required for the CPU model */
      s390_memory_init(machine);
 
+    /* init the TOD clock */
+    s390_init_tod();
+
      /* init CPUs (incl. CPU model) early so s390_has_feature() works */
      s390_init_cpus(machine);
 
@@ -332,9 +335,6 @@ static void ccw_init(MachineState *machine)
          s390_create_sclpconsole(ms->sclp, "sclplmconsole", serial_hd(1));
      }
 
-    /* init the TOD clock */
-    s390_init_tod();
-
      /* init SCLP event Control-Program Identification */
      if (s390mc->use_cpi) {
          s390_create_sclpcpi(ms->sclp);
target/s390x/cpu.c +8 -0
index f05ce317da1..cc2de6ce08e 100644
@@ -40,6 +40,7 @@
  #include "system/reset.h"
  #endif
  #include "hw/s390x/cpu-topology.h"
+#include "tcg/tcg_s390x.h"
 
  #define CR0_RESET       0xE0UL
  #define CR14_RESET      0xC2000000UL;
@@ -215,6 +216,13 @@ static void s390_cpu_reset_hold(Object *obj, ResetType type)
              break;
          }
      }
+
+#ifndef CONFIG_USER_ONLY
+    if (tcg_enabled()) {
+        /* Rearm the CKC timer if necessary */
+        tcg_s390_tod_updated(CPU(cpu), RUN_ON_CPU_NULL);
+    }
+#endif
  }
 
  static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH v2 3/3] tests/tcg/s390x: Test SET CLOCK COMPARATOR

In reply to Ilya Leoshkevich
Quoted 9 lines · Ilya Leoshkevich

Add a small test to prevent regressions.

---
  tests/tcg/s390x/Makefile.softmmu-target |  1 +
  tests/tcg/s390x/sckc.S                  | 55 +++++++++++++++++++++++++
  2 files changed, 56 insertions(+)

create mode 100644 tests/tcg/s390x/sckc.S

#4 lore raw

Re: [PATCH v2 2/3] target/s390x: Fix missing clock-comparator interrupts after reset

In reply to Thomas Huth
Quoted 6 lines · Thomas Huth

 Hi Ilya!

Quoted 3 lines · Ilya Leoshkevich

After reset, CKC value is set to 0, so if clock-comparator interrupts are enabled, one should occur very shortly thereafter.

Quoted 7 lines
Quoted 5 lines

Currently the code does not set tod_timer, so this does not happen.

Fix by adding a tcg_s390_tod_updated() call. Initialize TOD clock before CPUs in order to ensure that the respective object exists during the CPU reset.

Can this really happen? Looking at CPUS390XState in target/s390x/cpu.h, the ckc is next to the cregs[] in the start_initial_reset_fields section, so if ckc gets cleared, the cregs get cleared, too. I.e. if ckc gets set to 0 here, there is no way that the clock comparator interrupt could trigger immediately without the guest writing to the control registers first. So I think this patch is not really necessary. Or do I miss something?

... but it looks like your TCG test is failing without this second patch.

Ok, I think I understood it now: We don't re-arm the CKC timer in case the guest writes to the CR0. So we need to have the timer started during reset already to make the interrupt pending, so that it can fire when the guest changes CR0.

So I guess this patch is fine. Or alternatively you could maybe re-arm the CKC during stctg, would that be better? What do you think?

Thomas