Skip to contentSkip to conversation

mips: Handle late r4k timers

PATCH In review [email protected] 2 participants 12 messages Jan 17, 2011 – Jan 18, 2011

lore
Share & tools
lore
#0 lore raw

cover mips: Handle late r4k timers

From: Edgar E. Iglesias <[email protected]>

Linux runs a test to check if the internal r4k timer is usable prior to enabling it. The test basically starts a timer, repeatedly compares cp0_count with cp0_compare and finally when the timer should have hit it verifies that the irq line is asserted. Depending on host timing, qemu may fail this test. On my x86_64 host, we basically always fail it.

The following set of patches makes qemu pass the test reliably.

Edgar E. Iglesias (3):
mips: Break TBs after mfc0_count
mips: Break out cpu_mips_timer_expire
mips: Expire late timers when reading cp0_count

 hw/mips_timer.c         |   43 +++++++++++++++++++++++++++++--------------
 target-mips/translate.c |    4 +++-
 2 files changed, 32 insertions(+), 15 deletions(-)
Signature · 1 line
1.7.2.2
#1 lore raw

PATCH 1/3 mips: Break TBs after mfc0_count

From: Edgar E. Iglesias <[email protected]>

Break the TB after reading the count register. This makes it possible to take timer interrupts immediately after a read of a possibly expired timer.

---
 target-mips/translate.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
target-mips/translate.c +3 -1
index cce77be..313cc29 100644
@@ -3410,8 +3410,10 @@ static void gen_mfc0 (CPUState *env, DisasContext *ctx, TCGv arg, int reg, int s
             gen_helper_mfc0_count(arg);
             if (use_icount) {
                 gen_io_end();
-                ctx->bstate = BS_STOP;
             }
+            /* Break the TB to be able to take timer interrupts immediately
+               after reading count.  */
+            ctx->bstate = BS_STOP;
             rn = "Count";
             break;
         /* 6,7 are implementation dependent */
Signature · 1 line
1.7.2.2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] mips: Break TBs after mfc0_count

Quoted 17 lines · On Tue, Jan 18, 2011 at 12:29:40AM +0100, [email protected] wrote:

From: Edgar E. Iglesias <[email protected]>

Break the TB after reading the count register. This makes it possible to take timer interrupts immediately after a read of a possibly expired timer.

---
 target-mips/translate.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
target-mips/translate.c +0 -0
index cce77be..313cc29 100644
@@ -3410,8 +3410,10 @@ static void gen_mfc0 (CPUState *env, DisasContext *ctx, TCGv arg, int reg, int s
             gen_helper_mfc0_count(arg);

target-mips/translate.c static void gen_mfc0 (CPUState *env, DisasContext *ctx, TCGv arg, int reg, int s · line 3419

Quoted 10 lines
target-mips/translate.c +3 -1
             if (use_icount) {
                 gen_io_end();
-                ctx->bstate = BS_STOP;
             }
+            /* Break the TB to be able to take timer interrupts immediately
+               after reading count.  */
+            ctx->bstate = BS_STOP;
             rn = "Count";
             break;
         /* 6,7 are implementation dependent */

This looks fine, however it should probably be done the same way for dmfc0 on 64-bit MIPS.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#3 lore raw

Re: [Qemu-devel] [PATCH 1/3] mips: Break TBs after mfc0_count

Quoted 23 lines · Aurelien Jarno
Quoted 22 lines · On Tue, Jan 18, 2011 at 12:29:40AM +0100, [email protected] wrote:

From: Edgar E. Iglesias <[email protected]>

Break the TB after reading the count register. This makes it possible to take timer interrupts immediately after a read of a possibly expired timer.

---
 target-mips/translate.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
target-mips/translate.c +1 -1
index cce77be..313cc29 100644
@@ -3410,8 +3410,10 @@ static void gen_mfc0 (CPUState *env, DisasContext *ctx, TCGv arg, int reg, int s
             gen_helper_mfc0_count(arg);
             if (use_icount) {
                 gen_io_end();
-                ctx->bstate = BS_STOP;
             }
+            /* Break the TB to be able to take timer interrupts immediately
Quoted 8 lines

target-mips/translate.c static void gen_mfc0 (CPUState *env, DisasContext *ctx, TCGv arg, int reg, int s · line 3419

Quoted 5 lines
target-mips/translate.c +2 -0
+               after reading count.  */
+            ctx->bstate = BS_STOP;
             rn = "Count";
             break;
         /* 6,7 are implementation dependent */

This looks fine, however it should probably be done the same way for dmfc0 on 64-bit MIPS.

You're right, I'll fix that. Thanks.

#4 lore raw

Re: [Qemu-devel] [PATCH 1/3] mips: Break TBs after mfc0_count

In reply to Aurelien Jarno
Quoted 23 lines · Aurelien Jarno
Quoted 22 lines · On Tue, Jan 18, 2011 at 12:29:40AM +0100, [email protected] wrote:

From: Edgar E. Iglesias <[email protected]>

Break the TB after reading the count register. This makes it possible to take timer interrupts immediately after a read of a possibly expired timer.

---
 target-mips/translate.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
target-mips/translate.c +1 -1
index cce77be..313cc29 100644
@@ -3410,8 +3410,10 @@ static void gen_mfc0 (CPUState *env, DisasContext *ctx, TCGv arg, int reg, int s
             gen_helper_mfc0_count(arg);
             if (use_icount) {
                 gen_io_end();
-                ctx->bstate = BS_STOP;
             }
+            /* Break the TB to be able to take timer interrupts immediately
Quoted 8 lines

target-mips/translate.c static void gen_mfc0 (CPUState *env, DisasContext *ctx, TCGv arg, int reg, int s · line 3419

Quoted 5 lines
target-mips/translate.c +2 -0
+               after reading count.  */
+            ctx->bstate = BS_STOP;
             rn = "Count";
             break;
         /* 6,7 are implementation dependent */

This looks fine, however it should probably be done the same way for dmfc0 on 64-bit MIPS.

Thanks for the quick review, I've pushed the series with this suggested change.

Cheers

#5 lore raw

PATCH 2/3 mips: Break out cpu_mips_timer_expire

In reply to [email protected]

From: Edgar E. Iglesias <[email protected]>

Reorganize for future patches, no functional change.

---
 hw/mips_timer.c |   36 ++++++++++++++++++++++--------------
 1 files changed, 22 insertions(+), 14 deletions(-)
hw/mips_timer.c +22 -14
index e3beee8..8c32087 100644
@@ -42,16 +42,6 @@ uint32_t cpu_mips_get_random (CPUState *env)
 }
 
 /* MIPS R4K timer */
-uint32_t cpu_mips_get_count (CPUState *env)
-{
-    if (env->CP0_Cause & (1 << CP0Ca_DC))
-        return env->CP0_Count;
-    else
-        return env->CP0_Count +
-            (uint32_t)muldiv64(qemu_get_clock(vm_clock),
-                               TIMER_FREQ, get_ticks_per_sec());
-}
-
 static void cpu_mips_timer_update(CPUState *env)
 {
     uint64_t now, next;
@@ -64,6 +54,27 @@ static void cpu_mips_timer_update(CPUState *env)
     qemu_mod_timer(env->timer, next);
 }
 
+/* Expire the timer.  */
+static void cpu_mips_timer_expire(CPUState *env)
+{
+    cpu_mips_timer_update(env);
+    if (env->insn_flags & ISA_MIPS32R2) {
+        env->CP0_Cause |= 1 << CP0Ca_TI;
+    }
+    qemu_irq_raise(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]);
+}
+
+uint32_t cpu_mips_get_count (CPUState *env)
+{
+    if (env->CP0_Cause & (1 << CP0Ca_DC)) {
+        return env->CP0_Count;
+    } else {
+        return env->CP0_Count +
+            (uint32_t)muldiv64(qemu_get_clock(vm_clock),
+                               TIMER_FREQ, get_ticks_per_sec());
+    }
+}
+
 void cpu_mips_store_count (CPUState *env, uint32_t count)
 {
     if (env->CP0_Cause & (1 << CP0Ca_DC))
@@ -116,11 +127,8 @@ static void mips_timer_cb (void *opaque)
        the comparator value.  Offset the count by one to avoid immediately
        retriggering the callback before any virtual time has passed.  */
     env->CP0_Count++;
-    cpu_mips_timer_update(env);
+    cpu_mips_timer_expire(env);
     env->CP0_Count--;
-    if (env->insn_flags & ISA_MIPS32R2)
-        env->CP0_Cause |= 1 << CP0Ca_TI;
-    qemu_irq_raise(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]);
 }
 
 void cpu_mips_clock_init (CPUState *env)
Signature · 1 line
1.7.2.2
Acked-by on v1 2/3
#6 lore raw

[Qemu-devel] Re: [PATCH 2/3] mips: Break out cpu_mips_timer_expire

Quoted 75 lines · On Tue, Jan 18, 2011 at 12:29:41AM +0100, [email protected] wrote:

From: Edgar E. Iglesias <[email protected]>

Reorganize for future patches, no functional change.

---
 hw/mips_timer.c |   36 ++++++++++++++++++++++--------------
 1 files changed, 22 insertions(+), 14 deletions(-)
hw/mips_timer.c +22 -14
index e3beee8..8c32087 100644
@@ -42,16 +42,6 @@ uint32_t cpu_mips_get_random (CPUState *env)
 }
 
 /* MIPS R4K timer */
-uint32_t cpu_mips_get_count (CPUState *env)
-{
-    if (env->CP0_Cause & (1 << CP0Ca_DC))
-        return env->CP0_Count;
-    else
-        return env->CP0_Count +
-            (uint32_t)muldiv64(qemu_get_clock(vm_clock),
-                               TIMER_FREQ, get_ticks_per_sec());
-}
-
 static void cpu_mips_timer_update(CPUState *env)
 {
     uint64_t now, next;
@@ -64,6 +54,27 @@ static void cpu_mips_timer_update(CPUState *env)
     qemu_mod_timer(env->timer, next);
 }
 
+/* Expire the timer.  */
+static void cpu_mips_timer_expire(CPUState *env)
+{
+    cpu_mips_timer_update(env);
+    if (env->insn_flags & ISA_MIPS32R2) {
+        env->CP0_Cause |= 1 << CP0Ca_TI;
+    }
+    qemu_irq_raise(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]);
+}
+
+uint32_t cpu_mips_get_count (CPUState *env)
+{
+    if (env->CP0_Cause & (1 << CP0Ca_DC)) {
+        return env->CP0_Count;
+    } else {
+        return env->CP0_Count +
+            (uint32_t)muldiv64(qemu_get_clock(vm_clock),
+                               TIMER_FREQ, get_ticks_per_sec());
+    }
+}
+
 void cpu_mips_store_count (CPUState *env, uint32_t count)
 {
     if (env->CP0_Cause & (1 << CP0Ca_DC))
@@ -116,11 +127,8 @@ static void mips_timer_cb (void *opaque)
        the comparator value.  Offset the count by one to avoid immediately
        retriggering the callback before any virtual time has passed.  */
     env->CP0_Count++;
-    cpu_mips_timer_update(env);
+    cpu_mips_timer_expire(env);
     env->CP0_Count--;
-    if (env->insn_flags & ISA_MIPS32R2)
-        env->CP0_Cause |= 1 << CP0Ca_TI;
-    qemu_irq_raise(env->irq[(env->CP0_IntCtl >> CP0IntCtl_IPTI) & 0x7]);
 }
 
 void cpu_mips_clock_init (CPUState *env)
Signature · 1 line
1.7.2.2
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#7 lore raw

PATCH 3/3 mips: Expire late timers when reading cp0_count

In reply to [email protected]

From: Edgar E. Iglesias <[email protected]>

When reading cp0_count from a timer with a late trigger that should already have expired, expire it and raise the timer irq.

This makes it possible for guest code (e.g, Linux) that first read cp0_count, then compare it with cp0_compare and check for raised timer interrupt lines to run reliably.

---
 hw/mips_timer.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
hw/mips_timer.c +9 -2
index 8c32087..4e5c0b5 100644
@@ -69,9 +69,16 @@ uint32_t cpu_mips_get_count (CPUState *env)
     if (env->CP0_Cause & (1 << CP0Ca_DC)) {
         return env->CP0_Count;
     } else {
+        uint64_t now;
+
+        now = qemu_get_clock(vm_clock);
+        if (qemu_timer_expired(env->timer, now)) {
+            /* The timer has already expired.  */
+            cpu_mips_timer_expire(env);
+        }
+
         return env->CP0_Count +
-            (uint32_t)muldiv64(qemu_get_clock(vm_clock),
-                               TIMER_FREQ, get_ticks_per_sec());
+            (uint32_t)muldiv64(now, TIMER_FREQ, get_ticks_per_sec());
     }
 }
 
Signature · 1 line
1.7.2.2
#8 lore raw

[Qemu-devel] Re: [PATCH 3/3] mips: Expire late timers when reading cp0_count

Quoted 10 lines · On Tue, Jan 18, 2011 at 12:29:42AM +0100, [email protected] wrote:

From: Edgar E. Iglesias <[email protected]>

When reading cp0_count from a timer with a late trigger that should already have expired, expire it and raise the timer irq.

This makes it possible for guest code (e.g, Linux) that first read cp0_count, then compare it with cp0_compare and check for raised timer interrupt lines to run reliably.

Sorry sent the wrong version of this one. It's supposed to be the following:

commit 139330de404209528712fd703952c0b5ad4459a1

Author: Edgar E. Iglesias <[email protected]>
Date:   Tue Jan 18 00:12:22 2011 +0100

mips: Expire late timers when reading cp0_count

When reading cp0_count from a timer with a late trigger that should already have expired, expire it and raise the timer irq.

This makes it possible for guest code (e.g, Linux) that first read cp0_count, then compare it with cp0_compare and check for raised timer interrupt lines to run reliably.

hw/mips_timer.c +10 -2
index 8c32087..9c95f28 100644
@@ -69,9 +69,17 @@ uint32_t cpu_mips_get_count (CPUState *env)
     if (env->CP0_Cause & (1 << CP0Ca_DC)) {
         return env->CP0_Count;
     } else {
+        uint64_t now;
+
+        now = qemu_get_clock(vm_clock);
+        if (qemu_timer_pending(env->timer)
+            && qemu_timer_expired(env->timer, now)) {
+            /* The timer has already expired.  */
+            cpu_mips_timer_expire(env);
+        }
+
         return env->CP0_Count +
-            (uint32_t)muldiv64(qemu_get_clock(vm_clock),
-                               TIMER_FREQ, get_ticks_per_sec());
+            (uint32_t)muldiv64(now, TIMER_FREQ, get_ticks_per_sec());
     }
 }
 
#9 lore raw

Re: [Qemu-devel] Re: [PATCH 3/3] mips: Expire late timers when reading cp0_count

Quoted 44 lines · Edgar E. Iglesias
Quoted 10 lines · On Tue, Jan 18, 2011 at 12:29:42AM +0100, [email protected] wrote:

From: Edgar E. Iglesias <[email protected]>

When reading cp0_count from a timer with a late trigger that should already have expired, expire it and raise the timer irq.

This makes it possible for guest code (e.g, Linux) that first read cp0_count, then compare it with cp0_compare and check for raised timer interrupt lines to run reliably.

Sorry sent the wrong version of this one. It's supposed to be the following:

commit 139330de404209528712fd703952c0b5ad4459a1

Author: Edgar E. Iglesias <[email protected]>
Date:   Tue Jan 18 00:12:22 2011 +0100

mips: Expire late timers when reading cp0_count

When reading cp0_count from a timer with a late trigger that should already have expired, expire it and raise the timer irq.

This makes it possible for guest code (e.g, Linux) that first read cp0_count, then compare it with cp0_compare and check for raised timer interrupt lines to run reliably.

hw/mips_timer.c +6 -0
index 8c32087..9c95f28 100644
@@ -69,9 +69,17 @@ uint32_t cpu_mips_get_count (CPUState *env)
     if (env->CP0_Cause & (1 << CP0Ca_DC)) {
         return env->CP0_Count;
     } else {
+        uint64_t now;
+
+        now = qemu_get_clock(vm_clock);
+        if (qemu_timer_pending(env->timer)
+            && qemu_timer_expired(env->timer, now)) {
+            /* The timer has already expired.  */

hw/mips_timer.c uint32_t cpu_mips_get_count (CPUState *env) · line 85

Quoted 10 lines
hw/mips_timer.c +4 -2
+            cpu_mips_timer_expire(env);
+        }
+
         return env->CP0_Count +
-            (uint32_t)muldiv64(qemu_get_clock(vm_clock),
-                               TIMER_FREQ, get_ticks_per_sec());
+            (uint32_t)muldiv64(now, TIMER_FREQ, get_ticks_per_sec());
     }
 }
 

Given the TB is now ended after this instruction (due to patch 1), isn't the interrupt handled before starting the next TB, where the interrupt line (I guess CP0_Cause) read?

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#10 lore raw

Re: [Qemu-devel] Re: [PATCH 3/3] mips: Expire late timers when reading cp0_count

Quoted 51 lines · Aurelien Jarno
Quoted 50 lines · Edgar E. Iglesias
Quoted 10 lines · On Tue, Jan 18, 2011 at 12:29:42AM +0100, [email protected] wrote:

From: Edgar E. Iglesias <[email protected]>

When reading cp0_count from a timer with a late trigger that should already have expired, expire it and raise the timer irq.

This makes it possible for guest code (e.g, Linux) that first read cp0_count, then compare it with cp0_compare and check for raised timer interrupt lines to run reliably.

Sorry sent the wrong version of this one. It's supposed to be the following:

commit 139330de404209528712fd703952c0b5ad4459a1

Author: Edgar E. Iglesias <[email protected]>
Date:   Tue Jan 18 00:12:22 2011 +0100

mips: Expire late timers when reading cp0_count

When reading cp0_count from a timer with a late trigger that should already have expired, expire it and raise the timer irq.

This makes it possible for guest code (e.g, Linux) that first read cp0_count, then compare it with cp0_compare and check for raised timer interrupt lines to run reliably.

hw/mips_timer.c +9 -2
index 8c32087..9c95f28 100644
@@ -69,9 +69,17 @@ uint32_t cpu_mips_get_count (CPUState *env)
     if (env->CP0_Cause & (1 << CP0Ca_DC)) {
         return env->CP0_Count;
     } else {
+        uint64_t now;
+
+        now = qemu_get_clock(vm_clock);
+        if (qemu_timer_pending(env->timer)
+            && qemu_timer_expired(env->timer, now)) {
+            /* The timer has already expired.  */
+            cpu_mips_timer_expire(env);
+        }
+
         return env->CP0_Count +
-            (uint32_t)muldiv64(qemu_get_clock(vm_clock),
-                               TIMER_FREQ, get_ticks_per_sec());
Quoted 8 lines

hw/mips_timer.c uint32_t cpu_mips_get_count (CPUState *env) · line 85

Quoted 4 lines
hw/mips_timer.c +1 -0
+            (uint32_t)muldiv64(now, TIMER_FREQ, get_ticks_per_sec());
     }
 }
 

Given the TB is now ended after this instruction (due to patch 1), isn't the interrupt handled before starting the next TB, where the interrupt line (I guess CP0_Cause) read?

Hi,

The problem here is different. Due to host timing granularity, the timer might expire later than it's precise scheduled time. If that happens, get_count will return a count value that goes beyond the trigger time but the interrupt may come later (when the host timer expires).

This patch catches that case and expires the timer in-band, raising the timer interrupt if needed.

Cheers

#11 lore raw

Re: [Qemu-devel] Re: [PATCH 3/3] mips: Expire late timers when reading cp0_count

Quoted 63 lines · Edgar E. Iglesias
Quoted 51 lines · Aurelien Jarno
Quoted 50 lines · Edgar E. Iglesias
Quoted 10 lines · On Tue, Jan 18, 2011 at 12:29:42AM +0100, [email protected] wrote:

From: Edgar E. Iglesias <[email protected]>

When reading cp0_count from a timer with a late trigger that should already have expired, expire it and raise the timer irq.

This makes it possible for guest code (e.g, Linux) that first read cp0_count, then compare it with cp0_compare and check for raised timer interrupt lines to run reliably.

Sorry sent the wrong version of this one. It's supposed to be the following:

commit 139330de404209528712fd703952c0b5ad4459a1

Author: Edgar E. Iglesias <[email protected]>
Date:   Tue Jan 18 00:12:22 2011 +0100

mips: Expire late timers when reading cp0_count

When reading cp0_count from a timer with a late trigger that should already have expired, expire it and raise the timer irq.

This makes it possible for guest code (e.g, Linux) that first read cp0_count, then compare it with cp0_compare and check for raised timer interrupt lines to run reliably.

hw/mips_timer.c +9 -2
index 8c32087..9c95f28 100644
@@ -69,9 +69,17 @@ uint32_t cpu_mips_get_count (CPUState *env)
     if (env->CP0_Cause & (1 << CP0Ca_DC)) {
         return env->CP0_Count;
     } else {
+        uint64_t now;
+
+        now = qemu_get_clock(vm_clock);
+        if (qemu_timer_pending(env->timer)
+            && qemu_timer_expired(env->timer, now)) {
+            /* The timer has already expired.  */
+            cpu_mips_timer_expire(env);
+        }
+
         return env->CP0_Count +
-            (uint32_t)muldiv64(qemu_get_clock(vm_clock),
-                               TIMER_FREQ, get_ticks_per_sec());
Quoted 8 lines

hw/mips_timer.c uint32_t cpu_mips_get_count (CPUState *env) · line 85

Quoted 4 lines
hw/mips_timer.c +1 -0
+            (uint32_t)muldiv64(now, TIMER_FREQ, get_ticks_per_sec());
     }
 }
 

Given the TB is now ended after this instruction (due to patch 1), isn't the interrupt handled before starting the next TB, where the interrupt line (I guess CP0_Cause) read?

Hi,

Quoted 9 lines

The problem here is different. Due to host timing granularity, the timer might expire later than it's precise scheduled time. If that happens, get_count will return a count value that goes beyond the trigger time but the interrupt may come later (when the host timer expires).

This patch catches that case and expires the timer in-band, raising the timer interrupt if needed.

Ok, thanks for the explanation.

Signature · 2 lines
Aurelien Jarno	                        GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net