Skip to contentSkip to conversation

target/s390x: Fix missing clock-comparator interrupts

PATCH Superseded Ilya Leoshkevich 3 participants 8 messages Oct 14, 2025 – Oct 15, 2025

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover target/s390x: Fix missing clock-comparator interrupts

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                  | 53 +++++++++++++++++++++++++
 5 files changed, 73 insertions(+), 7 deletions(-)
 create mode 100644 tests/tcg/s390x/sckc.S
Signature · 1 line
2.51.0
#1 lore raw

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

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..afcf7688eda 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
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] target/s390x: Fix missing interrupts for small CKC values

Quoted 28 lines · 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 +4 -2
index 6d9d601d29a..afcf7688eda 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 */

target/s390x/tcg/misc_helper.c static void update_ckc_timer(CPUS390XState *env) · line 213

Quoted 10 lines
target/s390x/tcg/misc_helper.c +4 -2
+	time = env->ckc - td->base.low;
 
-    /* nanoseconds */
-    time = tod2time(time);
+	/* nanoseconds */
+	time = tod2time(time);
+    }
 
      timer_mod(env->tod_timer, time);
  }

Yes, this looks right.

#3 lore raw

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

In reply to Ilya Leoshkevich

After reset, both TOD and CKC values are set to 0, so if clock-comparator interrupts are enabled, one should occur 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
#4 lore raw

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

Quoted 1 line · Ilya Leoshkevich

After reset, both TOD and CKC values are set to 0, so if

                    ^^^

This is inaccurate, only the TOD programmable register is set to 0. I will fix this in v2.

Quoted 14 lines

clock-comparator interrupts are enabled, one should occur 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(-)

[...]

#5 lore raw

PATCH 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                  | 53 +++++++++++++++++++++++++
 2 files changed, 54 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 +53 -0

53 lines not on this page · raw message

Signature · 1 line
2.51.0
#6 lore raw

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

Quoted 9 lines · Ilya Leoshkevich

Add a small test to prevent regressions.

---
 tests/tcg/s390x/Makefile.softmmu-target |  1 +
 tests/tcg/s390x/sckc.S                  | 53
+++++++++++++++++++++++++

 2 files changed, 54 insertions(+)

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

[...]

Quoted 24 lines
tests/tcg/s390x/sckc.S new file +18 -0
new file mode 100644
index 00000000000..0fadec0dd65
@@ -0,0 +1,53 @@
+/*
+ * Test clock comparator.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+    .org 0x130
+ext_old_psw:
+    .org 0x1b0
+ext_new_psw:
+    .quad 0x180000000, _ext            /* 64-bit mode */
+    .org 0x200                         /* lowcore padding */
+
+    .globl _start
+_start:
+    lpswe start31_psw
+_start31:
+    stctg %c0,%c0,c0
+    oi c0+6,8                          /* set clock-comparator
Quoted 5 lines

subclass mask */
+    lctlg %c0,%c0,c0
+0:
+    cghsi ext_counter,0x1000
+    jnz 0b

It's better to move this check to the interrupt handler, otherwise there is a risk we get two interrupts and the counter moves from 0xfff to 0x1001, causing an infinite loop. I will fix this in v2.

Quoted 8 lines

+    lpswe success_psw

tests/tcg/s390x/sckc.S new file +7 -0
+
+_ext:
+    stg %r0,ext_saved_r0
+
+    lg %r0,ext_counter
+    aghi %r0,1
+    stg %r0,ext_counter

[...]

#7 lore raw
In reply to Ilya Leoshkevich
Quoted 8 lines · Ilya Leoshkevich

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]/

Quoted 7 lines

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

Are these worth to have in qemu stable series?

(And please don't forget to add Cc: qemu-stable@ to other future changes which are also good to have in stable series).

Thanks,

/mjt