qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] target/s390x: Fix missing clock-comparator interrupts
@ 2025-10-14 16:05 Ilya Leoshkevich
  2025-10-14 16:05 ` [PATCH 1/3] target/s390x: Fix missing interrupts for small CKC values Ilya Leoshkevich
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Ilya Leoshkevich @ 2025-10-14 16:05 UTC (permalink / raw)
  To: Thomas Huth, Richard Henderson, David Hildenbrand
  Cc: qemu-s390x, qemu-devel, 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/ab3131a2-c42a-47ff-bf03-e9f68ac053c0@t-8ch.de/

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

-- 
2.51.0



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/3] target/s390x: Fix missing interrupts for small CKC values
  2025-10-14 16:05 [PATCH 0/3] target/s390x: Fix missing clock-comparator interrupts Ilya Leoshkevich
@ 2025-10-14 16:05 ` Ilya Leoshkevich
  2025-10-15 13:01   ` Thomas Huth
  2025-10-14 16:05 ` [PATCH 2/3] target/s390x: Fix missing clock-comparator interrupts after reset Ilya Leoshkevich
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: Ilya Leoshkevich @ 2025-10-14 16:05 UTC (permalink / raw)
  To: Thomas Huth, Richard Henderson, David Hildenbrand
  Cc: qemu-s390x, qemu-devel, 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.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
 target/s390x/tcg/misc_helper.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/target/s390x/tcg/misc_helper.c b/target/s390x/tcg/misc_helper.c
index 6d9d601d29a..afcf7688eda 100644
--- a/target/s390x/tcg/misc_helper.c
+++ b/target/s390x/tcg/misc_helper.c
@@ -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);
 }
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/3] target/s390x: Fix missing clock-comparator interrupts after reset
  2025-10-14 16:05 [PATCH 0/3] target/s390x: Fix missing clock-comparator interrupts Ilya Leoshkevich
  2025-10-14 16:05 ` [PATCH 1/3] target/s390x: Fix missing interrupts for small CKC values Ilya Leoshkevich
@ 2025-10-14 16:05 ` Ilya Leoshkevich
  2025-10-15  7:28   ` Ilya Leoshkevich
  2025-10-14 16:05 ` [PATCH 3/3] tests/tcg/s390x: Test SET CLOCK COMPARATOR Ilya Leoshkevich
  2025-10-15  7:56 ` [PATCH 0/3] target/s390x: Fix missing clock-comparator interrupts Michael Tokarev
  3 siblings, 1 reply; 8+ messages in thread
From: Ilya Leoshkevich @ 2025-10-14 16:05 UTC (permalink / raw)
  To: Thomas Huth, Richard Henderson, David Hildenbrand
  Cc: qemu-s390x, qemu-devel, 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.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
 hw/s390x/s390-virtio-ccw.c | 6 +++---
 target/s390x/cpu.c         | 8 ++++++++
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/hw/s390x/s390-virtio-ccw.c b/hw/s390x/s390-virtio-ccw.c
index ad2c48188a8..9f37a96ae5a 100644
--- a/hw/s390x/s390-virtio-ccw.c
+++ b/hw/s390x/s390-virtio-ccw.c
@@ -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);
diff --git a/target/s390x/cpu.c b/target/s390x/cpu.c
index f05ce317da1..cc2de6ce08e 100644
--- a/target/s390x/cpu.c
+++ b/target/s390x/cpu.c
@@ -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)
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/3] tests/tcg/s390x: Test SET CLOCK COMPARATOR
  2025-10-14 16:05 [PATCH 0/3] target/s390x: Fix missing clock-comparator interrupts Ilya Leoshkevich
  2025-10-14 16:05 ` [PATCH 1/3] target/s390x: Fix missing interrupts for small CKC values Ilya Leoshkevich
  2025-10-14 16:05 ` [PATCH 2/3] target/s390x: Fix missing clock-comparator interrupts after reset Ilya Leoshkevich
@ 2025-10-14 16:05 ` Ilya Leoshkevich
  2025-10-15  7:30   ` Ilya Leoshkevich
  2025-10-15  7:56 ` [PATCH 0/3] target/s390x: Fix missing clock-comparator interrupts Michael Tokarev
  3 siblings, 1 reply; 8+ messages in thread
From: Ilya Leoshkevich @ 2025-10-14 16:05 UTC (permalink / raw)
  To: Thomas Huth, Richard Henderson, David Hildenbrand
  Cc: qemu-s390x, qemu-devel, Ilya Leoshkevich

Add a small test to prevent regressions.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
---
 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

diff --git a/tests/tcg/s390x/Makefile.softmmu-target b/tests/tcg/s390x/Makefile.softmmu-target
index 8cd4667c63b..a4425d3184a 100644
--- a/tests/tcg/s390x/Makefile.softmmu-target
+++ b/tests/tcg/s390x/Makefile.softmmu-target
@@ -28,6 +28,7 @@ ASM_TESTS =                                                                    \
     mc                                                                         \
     per                                                                        \
     precise-smc-softmmu                                                        \
+    sckc                                                                       \
     ssm-early                                                                  \
     stosm-early                                                                \
     stpq                                                                       \
diff --git a/tests/tcg/s390x/sckc.S b/tests/tcg/s390x/sckc.S
new file mode 100644
index 00000000000..0fadec0dd65
--- /dev/null
+++ b/tests/tcg/s390x/sckc.S
@@ -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 subclass mask */
+    lctlg %c0,%c0,c0
+0:
+    cghsi ext_counter,0x1000
+    jnz 0b
+    lpswe success_psw
+
+_ext:
+    stg %r0,ext_saved_r0
+
+    lg %r0,ext_counter
+    aghi %r0,1
+    stg %r0,ext_counter
+
+    stck clock
+    lg %r0,clock
+    agfi %r0,0x40000                   /* 64us * 0x1000 =~ 0.25s */
+    stg %r0,clock
+    sckc clock
+
+    lg %r0,ext_saved_r0
+    lpswe ext_old_psw
+
+    .align 8
+start31_psw:
+    .quad 0x100000080000000,_start31   /* EX, 31-bit mode */
+success_psw:
+    .quad 0x2000000000000,0xfff        /* see is_special_wait_psw() */
+c0:
+    .skip 8
+clock:
+    .quad 0
+ext_counter:
+    .quad 0
+ext_saved_r0:
+    .skip 8
-- 
2.51.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] target/s390x: Fix missing clock-comparator interrupts after reset
  2025-10-14 16:05 ` [PATCH 2/3] target/s390x: Fix missing clock-comparator interrupts after reset Ilya Leoshkevich
@ 2025-10-15  7:28   ` Ilya Leoshkevich
  0 siblings, 0 replies; 8+ messages in thread
From: Ilya Leoshkevich @ 2025-10-15  7:28 UTC (permalink / raw)
  To: Thomas Huth, Richard Henderson, David Hildenbrand; +Cc: qemu-s390x, qemu-devel

On Tue, 2025-10-14 at 18:05 +0200, Ilya Leoshkevich wrote:
> 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.

> 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.
> 
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
>  hw/s390x/s390-virtio-ccw.c | 6 +++---
>  target/s390x/cpu.c         | 8 ++++++++
>  2 files changed, 11 insertions(+), 3 deletions(-)

[...]


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/3] tests/tcg/s390x: Test SET CLOCK COMPARATOR
  2025-10-14 16:05 ` [PATCH 3/3] tests/tcg/s390x: Test SET CLOCK COMPARATOR Ilya Leoshkevich
@ 2025-10-15  7:30   ` Ilya Leoshkevich
  0 siblings, 0 replies; 8+ messages in thread
From: Ilya Leoshkevich @ 2025-10-15  7:30 UTC (permalink / raw)
  To: Thomas Huth, Richard Henderson, David Hildenbrand; +Cc: qemu-s390x, qemu-devel

On Tue, 2025-10-14 at 18:05 +0200, Ilya Leoshkevich wrote:
> Add a small test to prevent regressions.
> 
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
>  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

[...]

> diff --git a/tests/tcg/s390x/sckc.S b/tests/tcg/s390x/sckc.S
> new file mode 100644
> index 00000000000..0fadec0dd65
> --- /dev/null
> +++ b/tests/tcg/s390x/sckc.S
> @@ -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
> 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.

> +    lpswe success_psw
> +
> +_ext:
> +    stg %r0,ext_saved_r0
> +
> +    lg %r0,ext_counter
> +    aghi %r0,1
> +    stg %r0,ext_counter

[...]


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 0/3] target/s390x: Fix missing clock-comparator interrupts
  2025-10-14 16:05 [PATCH 0/3] target/s390x: Fix missing clock-comparator interrupts Ilya Leoshkevich
                   ` (2 preceding siblings ...)
  2025-10-14 16:05 ` [PATCH 3/3] tests/tcg/s390x: Test SET CLOCK COMPARATOR Ilya Leoshkevich
@ 2025-10-15  7:56 ` Michael Tokarev
  3 siblings, 0 replies; 8+ messages in thread
From: Michael Tokarev @ 2025-10-15  7:56 UTC (permalink / raw)
  To: Ilya Leoshkevich, Thomas Huth, Richard Henderson,
	David Hildenbrand
  Cc: qemu-s390x, qemu-devel

14.10.2025 19:05, Ilya Leoshkevich wrote:
> 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/ab3131a2-c42a-47ff-bf03-e9f68ac053c0@t-8ch.de/
> 
> 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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] target/s390x: Fix missing interrupts for small CKC values
  2025-10-14 16:05 ` [PATCH 1/3] target/s390x: Fix missing interrupts for small CKC values Ilya Leoshkevich
@ 2025-10-15 13:01   ` Thomas Huth
  0 siblings, 0 replies; 8+ messages in thread
From: Thomas Huth @ 2025-10-15 13:01 UTC (permalink / raw)
  To: Ilya Leoshkevich, Richard Henderson, David Hildenbrand
  Cc: qemu-s390x, qemu-devel

On 14/10/2025 18.05, Ilya Leoshkevich wrote:
> 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.
> 
> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
> ---
>   target/s390x/tcg/misc_helper.c | 12 ++++++++----
>   1 file changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/target/s390x/tcg/misc_helper.c b/target/s390x/tcg/misc_helper.c
> index 6d9d601d29a..afcf7688eda 100644
> --- a/target/s390x/tcg/misc_helper.c
> +++ b/target/s390x/tcg/misc_helper.c
> @@ -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);
>   }

Yes, this looks right.

Reviewed-by: Thomas Huth <thuth@redhat.com>



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-10-15 13:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-14 16:05 [PATCH 0/3] target/s390x: Fix missing clock-comparator interrupts Ilya Leoshkevich
2025-10-14 16:05 ` [PATCH 1/3] target/s390x: Fix missing interrupts for small CKC values Ilya Leoshkevich
2025-10-15 13:01   ` Thomas Huth
2025-10-14 16:05 ` [PATCH 2/3] target/s390x: Fix missing clock-comparator interrupts after reset Ilya Leoshkevich
2025-10-15  7:28   ` Ilya Leoshkevich
2025-10-14 16:05 ` [PATCH 3/3] tests/tcg/s390x: Test SET CLOCK COMPARATOR Ilya Leoshkevich
2025-10-15  7:30   ` Ilya Leoshkevich
2025-10-15  7:56 ` [PATCH 0/3] target/s390x: Fix missing clock-comparator interrupts Michael Tokarev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).