Skip to contentSkip to conversation

target/s390x: Fix missing clock-comparator interrupts

PATCH In review Ilya Leoshkevich 3 participants 11 messages Oct 16, 2025 – Nov 27, 2025

lore
Share & tools
lore
#0 lore raw

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

v3: https://lore.kernel.org/qemu-devel/[email protected]/
v3 -> v4: Add a patch for the missing address generation affecting
interrupts. Slightly adjust the test, keep Thomas' R-b. Add Thomas' R-b to the timer rearm fix.

v2: https://lore.kernel.org/qemu-devel/[email protected]/
v2 -> v3: Rearm the timer on control register load (Thomas).
Add Thomas' R-b to the test.

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 reproduce [1], I found two bugs in the clock comparator handling. This series fixes all three issues and adds a test.

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

Best regards,
Ilya

Ilya Leoshkevich (4):
target/s390x: Fix missing interrupts for small CKC values target/s390x: Fix missing clock-comparator interrupts after reset target/s390x: Use address generation for register branch targets tests/tcg/s390x: Test SET CLOCK COMPARATOR

 target/s390x/tcg/mem_helper.c           | 11 ++++-
 target/s390x/tcg/misc_helper.c          | 12 +++--
 target/s390x/tcg/translate.c            | 11 +++--
 tests/tcg/s390x/Makefile.softmmu-target |  1 +
 tests/tcg/s390x/sckc.S                  | 63 +++++++++++++++++++++++++
 5 files changed, 89 insertions(+), 9 deletions(-)
 create mode 100644 tests/tcg/s390x/sckc.S
Signature · 1 line
2.51.0
#1 lore raw

PATCH v4 1/4 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..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
#2 lore raw

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

Quoted 7 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.

Quoted 6 lines

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

---

Hi Ilya,

this patch unfortunately broke reverse debugging on the s390x target. Something like this used to work before:

qemu-img create -f qcow2 /tmp/disk.qcow2 2G ./qemu-system-s390x -nographic \
-icount shift=6,rr=record,rrfile=replay.bin,rrsnapshot=init \
-net none -drive file=/tmp/disk.qcow2,if=none
./qemu-system-s390x -nographic \
-icount shift=6,rr=replay,rrfile=replay.bin,rrsnapshot=init \
-net none -drive file=/tmp/disk.qcow2,if=none

With this commit and later, the replay hangs somewhere in an endless loop. Do you have any ideas what could go wrong here?

Thanks,
Thomas

Quoted 24 lines
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);
  }
#3 lore raw

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

Quoted 30 lines · Thomas Huth
Quoted 9 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.

Quoted 6 lines

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

---

  Hi Ilya,

this patch unfortunately broke reverse debugging on the s390x target. Something like this used to work before:

  qemu-img create -f qcow2 /tmp/disk.qcow2 2G ./qemu-system-s390x -nographic \
    -icount shift=6,rr=record,rrfile=replay.bin,rrsnapshot=init \
    -net none -drive file=/tmp/disk.qcow2,if=none ./qemu-system-s390x -nographic \
    -icount shift=6,rr=replay,rrfile=replay.bin,rrsnapshot=init \
    -net none -drive file=/tmp/disk.qcow2,if=none

Quoted 6 lines

With this commit and later, the replay hangs somewhere in an endless loop.
Do you have any ideas what could go wrong here?

  Thanks,
   Thomas

[...]

Hi Thomas,

Thanks for letting me know, I will look at this ASAP.

Best regards,
Ilya

#4 lore raw

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

Quoted 41 lines · Ilya Leoshkevich
Quoted 32 lines · Thomas Huth
Quoted 10 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.

Quoted 6 lines

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

---

  Hi Ilya,

this patch unfortunately broke reverse debugging on the s390x target.
Something like this used to work before:

  qemu-img create -f qcow2 /tmp/disk.qcow2 2G ./qemu-system-s390x -nographic \
    -icount shift=6,rr=record,rrfile=replay.bin,rrsnapshot=init \
    -net none -drive file=/tmp/disk.qcow2,if=none ./qemu-system-s390x -nographic \
    -icount shift=6,rr=replay,rrfile=replay.bin,rrsnapshot=init \
    -net none -drive file=/tmp/disk.qcow2,if=none

Quoted 7 lines

With this commit and later, the replay hangs somewhere in an endless
loop.
Do you have any ideas what could go wrong here?

  Thanks,
   Thomas

Quoted 8 lines

[...]

Hi Thomas,

Thanks for letting me know, I will look at this ASAP.

Best regards,
Ilya

Intermediate finding:

update_ckc_timer() is called only during replay, but not during normal runs or record. The call chain during replay is as follows:

main()
qemu_init()

    qmp_x_exit_preconfig()
      replay_vmstate_init()
        load_snapshot()
          qemu_loadvm_state()
            qemu_loadvm_state_main()
              qemu_loadvm_section_start_full()
                vmstate_load()
                  vmstate_load_state()
                    cpu_post_load()
                      tcg_s390_tod_updated()
                        update_ckc_timer()

The end result is that during record CHECKPOINT_CLOCK_VIRTUAL is not written to replay.bin. But during replay it's expected here:

        if (replay_mode != REPLAY_MODE_NONE
            && timer_list->clock->type == QEMU_CLOCK_VIRTUAL
            && !(ts->attributes & QEMU_TIMER_ATTR_EXTERNAL)
            && !replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL)) {
            qemu_mutex_unlock(&timer_list->active_timers_lock);
            goto out;
        }

The lack of it prevents the timer callback from running. So the timer associated with s390x_tod_timer() remains active forever and causes the rr_cpu_thread_fn() to loop.

IIUC these things really have to be symmetric between record and replay, so we probably need to add this call to some strategic location during record.

I will continue tomorrow.

#5 lore raw

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

In reply to 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 that loads the respective control register does not set tod_timer, so this does not happen.

Fix by adding a tcg_s390_tod_updated() call to LCTL and LCTLG.

---
 target/s390x/tcg/mem_helper.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
target/s390x/tcg/mem_helper.c +10 -1
index f1acb1618f7..24675fc818d 100644
@@ -1959,6 +1959,10 @@ void HELPER(lctlg)(CPUS390XState *env, uint32_t r1, uint64_t a2, uint32_t r3)
         if (env->cregs[i] != val && i >= 9 && i <= 11) {
             PERchanged = true;
         }
+        if (i == 0 && !(env->cregs[i] & CR0_CKC_SC) && (val & CR0_CKC_SC)) {
+            BQL_LOCK_GUARD();
+            tcg_s390_tod_updated(env_cpu(env), RUN_ON_CPU_NULL);
+        }
         env->cregs[i] = val;
         HELPER_LOG("load ctl %d from 0x%" PRIx64 " == 0x%" PRIx64 "\n",
                    i, src, val);
@@ -1989,10 +1993,15 @@ void HELPER(lctl)(CPUS390XState *env, uint32_t r1, uint64_t a2, uint32_t r3)
 
     for (i = r1;; i = (i + 1) % 16) {
         uint32_t val = cpu_ldl_data_ra(env, src, ra);
+        uint64_t val64 = deposit64(env->cregs[i], 0, 32, val);
         if ((uint32_t)env->cregs[i] != val && i >= 9 && i <= 11) {
             PERchanged = true;
         }
-        env->cregs[i] = deposit64(env->cregs[i], 0, 32, val);
+        if (i == 0 && !(env->cregs[i] & CR0_CKC_SC) && (val64 & CR0_CKC_SC)) {
+            BQL_LOCK_GUARD();
+            tcg_s390_tod_updated(env_cpu(env), RUN_ON_CPU_NULL);
+        }
+        env->cregs[i] = val64;
         HELPER_LOG("load ctl %d from 0x%" PRIx64 " == 0x%x\n", i, src, val);
         src += sizeof(uint32_t);
 
Signature · 1 line
2.51.0
#6 lore raw

PATCH v4 3/4 target/s390x: Use address generation for register branch targets

In reply to Ilya Leoshkevich

Indirect branches to addresses taken from registers go through address generation, e.g., for BRANCH ON CONDITION Principles of Operation says:

In the RR format, the contents of general register R2 are used to generate the branch address

QEMU uses r2_nz handler for the respective register operands. Currently it does not zero out extra bits in 24- and 31-bit addressing modes as required by address generation. The very frequently used s390x_tr_init_disas_context() function has a workaround for this, but the code for saving an old PSW during an interrupt does not.

Add the missing masking to r2_nz. Enforce PSW validity by replacing the workaround with an assersion.

---
 target/s390x/tcg/translate.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
target/s390x/tcg/translate.c +7 -4
index c7e8574438c..f9487f4044d 100644
@@ -5613,6 +5613,7 @@ static void in2_r2_nz(DisasContext *s, DisasOps *o)
     int r2 = get_field(s, r2);
     if (r2 != 0) {
         o->in2 = load_reg(r2);
+        gen_addi_and_wrap_i64(s, o->in2, o->in2, 0);
     }
 }
 #define SPEC_in2_r2_nz 0
@@ -6379,10 +6380,12 @@ static void s390x_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
 {
     DisasContext *dc = container_of(dcbase, DisasContext, base);
 
-    /* 31-bit mode */
-    if (!(dc->base.tb->flags & FLAG_MASK_64)) {
-        dc->base.pc_first &= 0x7fffffff;
-        dc->base.pc_next = dc->base.pc_first;
+    if (dc->base.tb->flags & FLAG_MASK_32) {
+        if (!(dc->base.tb->flags & FLAG_MASK_64)) {
+            assert(!(dc->base.pc_first & ~((1ULL << 31) - 1)));
+        }
+    } else {
+        assert(!(dc->base.pc_first & ~((1ULL << 24) - 1)));
     }
 
     dc->cc_op = CC_OP_DYNAMIC;
Signature · 1 line
2.51.0
Tested-by on v4 3/4
#7 lore raw

Re: [PATCH v4 3/4] target/s390x: Use address generation for register branch targets

Quoted 12 lines · Ilya Leoshkevich

Indirect branches to addresses taken from registers go through address generation, e.g., for BRANCH ON CONDITION Principles of Operation says:

In the RR format, the contents of general register R2 are used to generate the branch address

QEMU uses r2_nz handler for the respective register operands. Currently it does not zero out extra bits in 24- and 31-bit addressing modes as required by address generation. The very frequently used s390x_tr_init_disas_context() function has a workaround for this, but the code for saving an old PSW during an interrupt does not.

Quoted 8 lines

Add the missing masking to r2_nz. Enforce PSW validity by replacing the workaround with an assersion.

Thanks!

(...)

#8 lore raw

PATCH v4 4/4 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                  | 63 +++++++++++++++++++++++++
 2 files changed, 64 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 +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.51.0
#9 lore raw
In reply to Ilya Leoshkevich
Quoted 35 lines · Ilya Leoshkevich

v3:
https://lore.kernel.org/qemu-devel/[email protected]/
v3 -> v4: Add a patch for the missing address generation affecting interrupts. Slightly adjust the test, keep Thomas' R-b. Add Thomas' R-b to the timer rearm fix.

v2:
https://lore.kernel.org/qemu-devel/[email protected]/
v2 -> v3: Rearm the timer on control register load (Thomas). Add Thomas' R-b to the test.

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 reproduce [1], I found two bugs in the clock comparator
handling. This series fixes all three issues and adds a test.

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

Best regards,
Ilya

Ilya Leoshkevich (4):
  target/s390x: Fix missing interrupts for small CKC values target/s390x: Fix missing clock-comparator interrupts after reset target/s390x: Use address generation for register branch targets tests/tcg/s390x: Test SET CLOCK COMPARATOR

Quoted 8 lines

 target/s390x/tcg/mem_helper.c           | 11 ++++- target/s390x/tcg/misc_helper.c          | 12 +++-- target/s390x/tcg/translate.c            | 11 +++-- tests/tcg/s390x/Makefile.softmmu-target |  1 + tests/tcg/s390x/sckc.S                  | 63
+++++++++++++++++++++++++

 5 files changed, 89 insertions(+), 9 deletions(-)

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

Hello,

I would like to ping this series.

Patches 1, 2, and 4 are already reviewed (thanks, Thomas!). Patch 3 still needs a review.

Best regards,
Ilya

#10 lore raw
Quoted 45 lines · Ilya Leoshkevich
Quoted 35 lines · Ilya Leoshkevich

v3:
https://lore.kernel.org/qemu-devel/[email protected]/
v3 -> v4: Add a patch for the missing address generation affecting
          interrupts. Slightly adjust the test, keep Thomas' R-b. Add Thomas' R-b to the timer rearm fix.

v2:
https://lore.kernel.org/qemu-devel/[email protected]/
v2 -> v3: Rearm the timer on control register load (Thomas).
          Add Thomas' R-b to the test.

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 reproduce [1], I found two bugs in the clock comparator
handling. This series fixes all three issues and adds a test.

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

Best regards,
Ilya

Ilya Leoshkevich (4):
  target/s390x: Fix missing interrupts for small CKC values target/s390x: Fix missing clock-comparator interrupts after reset target/s390x: Use address generation for register branch targets tests/tcg/s390x: Test SET CLOCK COMPARATOR

Quoted 8 lines
  target/s390x/tcg/mem_helper.c           | 11 ++++-
  target/s390x/tcg/misc_helper.c          | 12 +++--
  target/s390x/tcg/translate.c            | 11 +++--
  tests/tcg/s390x/Makefile.softmmu-target |  1 +
  tests/tcg/s390x/sckc.S                  | 63
+++++++++++++++++++++++++
  5 files changed, 89 insertions(+), 9 deletions(-)

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

Quoted 6 lines

Hello,

I would like to ping this series.

Patches 1, 2, and 4 are already reviewed (thanks, Thomas!). Patch 3 still needs a review.

Hi Ilya,

I still got it on my radar, and indeed, it would be great to get an Ack from Richard or David for patch 3.

However, the main problem is that we need the fix for the C23 problem in the fourth patch first before this could get merged (https://gitlab.com/qemu-project/SLOF/-/merge_requests/2). It should hopefully get fixed with Harsh's next ppc pull request, see:

https://lore.kernel.org/qemu-devel/[email protected]

Thomas