Skip to contentSkip to conversation

hw/timer/npcm7xx_timer: Prevent timer from counting down past zero

PATCH Applied Chris Rauer 3 participants 4 messages Sep 22, 2023 – Oct 17, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/timer/npcm7xx_timer: Prevent timer from counting down past zero

The counter register is only 24-bits and counts down. If the timer is running but the qtimer to reset it hasn't fired off yet, there is a chance the regster read can return an invalid result.

---
 hw/timer/npcm7xx_timer.c | 3 +++
 1 file changed, 3 insertions(+)
hw/timer/npcm7xx_timer.c +3 -0
index 32f5e021f8..a8bd93aeb2 100644
@@ -138,6 +138,9 @@ static int64_t npcm7xx_timer_count_to_ns(NPCM7xxTimer *t, uint32_t count)
 /* Convert a time interval in nanoseconds to a timer cycle count. */
 static uint32_t npcm7xx_timer_ns_to_count(NPCM7xxTimer *t, int64_t ns)
 {
+    if (ns < 0) {
+        return 0;
+    }
     return clock_ns_to_ticks(t->ctrl->clock, ns) /
         npcm7xx_tcsr_prescaler(t->tcsr);
 }
Signature · 1 line
2.42.0.515.g380fc7ccd1-goog
#1 lore raw

Is this related to this error?

https://lists.gnu.org/archive/html/qemu-devel/2023-09/msg04903.html

Quoted 28 lines · Chris Rauer

The counter register is only 24-bits and counts down. If the timer is running but the qtimer to reset it hasn't fired off yet, there is a chance the regster read can return an invalid result.

---
 hw/timer/npcm7xx_timer.c | 3 +++
 1 file changed, 3 insertions(+)
hw/timer/npcm7xx_timer.c +3 -0
index 32f5e021f8..a8bd93aeb2 100644
@@ -138,6 +138,9 @@ static int64_t npcm7xx_timer_count_to_ns(NPCM7xxTimer
 *t, uint32_t count)
 /* Convert a time interval in nanoseconds to a timer cycle count. */
 static uint32_t npcm7xx_timer_ns_to_count(NPCM7xxTimer *t, int64_t ns)
 {
+    if (ns < 0) {
+        return 0;
+    }
     return clock_ns_to_ticks(t->ctrl->clock, ns) /
         npcm7xx_tcsr_prescaler(t->tcsr);

}
--
2.42.0.515.g380fc7ccd1-goog

#2 lore raw

No. This patch does not address that issue and is not related. I was able to reproduce it about 2/1000 iterations with and without this patch. I will look into that issue separately.

-Chris

Quoted 34 lines · Hao Wu

Is this related to this error?

https://lists.gnu.org/archive/html/qemu-devel/2023-09/msg04903.html

Quoted 28 lines · Chris Rauer

The counter register is only 24-bits and counts down. If the timer is running but the qtimer to reset it hasn't fired off yet, there is a chance the regster read can return an invalid result.

---
 hw/timer/npcm7xx_timer.c | 3 +++
 1 file changed, 3 insertions(+)
hw/timer/npcm7xx_timer.c +3 -0
index 32f5e021f8..a8bd93aeb2 100644
@@ -138,6 +138,9 @@ static int64_t npcm7xx_timer_count_to_ns(NPCM7xxTimer
 *t, uint32_t count)
 /* Convert a time interval in nanoseconds to a timer cycle count. */
 static uint32_t npcm7xx_timer_ns_to_count(NPCM7xxTimer *t, int64_t ns)
 {
+    if (ns < 0) {
+        return 0;
+    }
     return clock_ns_to_ticks(t->ctrl->clock, ns) /
         npcm7xx_tcsr_prescaler(t->tcsr);

}
--
2.42.0.515.g380fc7ccd1-goog

#3 lore raw
In reply to Chris Rauer
Quoted 6 lines · Chris Rauer

The counter register is only 24-bits and counts down. If the timer is running but the qtimer to reset it hasn't fired off yet, there is a chance the regster read can return an invalid result.

Applied to target-arm.next, thanks.

(As a side note, if you'd used the ptimer countdown-timer functions to implement this timer, this is one of the corner cases that it would have got right for you ;-))

-- PMM