[PATCH 4/5] lib: sbi: Add error check for sbi_timer_waitms_until
Xiang W
wxjstz at 126.com
Sun Apr 13 08:06:19 PDT 2025
sbi_timer_value may always return 0 when there is no suitable device,
which may cause sbi_timer_waitms_until to fall into an infinite loop.
Add error handling to prevent this from happening
Signed-off-by: Xiang W <wxjstz at 126.com>
---
lib/sbi/sbi_timer.c | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/lib/sbi/sbi_timer.c b/lib/sbi/sbi_timer.c
index 5266c612..5351e9e7 100644
--- a/lib/sbi/sbi_timer.c
+++ b/lib/sbi/sbi_timer.c
@@ -81,10 +81,19 @@ void sbi_timer_delay_loop(ulong units, u64 unit_freq,
bool sbi_timer_waitms_until(bool (*predicate)(void *), void *arg,
uint64_t timeout_ms)
{
- uint64_t start_time = sbi_timer_value();
- uint64_t ticks =
- (sbi_timer_get_device()->timer_freq / 1000) *
- timeout_ms;
+ uint64_t start_time, ticks;
+ struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+
+ /* Do nothing if we don't have timer device */
+ if (!timer_dev ||
+ (!sbi_hart_has_extension(scratch, SBI_HART_EXT_ZICNTR) &&
+ !timer_dev->timer_value)) {
+ sbi_printf("%s: called without timer device\n", __func__);
+ return;
+ }
+
+ start_time = sbi_timer_value();
+ ticks = (timer_dev->timer_freq / 1000) * timeout_ms;
while(!predicate(arg))
if (sbi_timer_value() - start_time >= ticks)
return false;
--
2.47.2
More information about the opensbi
mailing list