From 3391cc41be9ca13db7a7efc86329fdc922bc64ee Mon Sep 17 00:00:00 2001 From: Alberto Mardegan Date: Sat, 25 Apr 2026 11:06:00 +0300 Subject: [PATCH] Fix clock_gettime() and tick_nanosecs() macro clock_gettime() uses the tick_nanosecs() macro to extract the sub-second component of a time expressed in PPC ticks, and returns the sub-second component expressed in nanoseconds. To do so, it relied on the PPCTicksToNs() function, which will overflow and return incorrect results if its argument is sufficiently large (in my case it was 50446082889576108). This in turn causes clock_gettime() to return the wrong number of nanoseconds, which confuses applications because they occasionally see the time jump back. We fix it by passint to tick_nanosecs() the number of ticks modulo the number of tics per second, so it's guaranteed not to overflow. We also adjust tick_microsecs() accordingly, even though it was not wrong. --- gc/ogc/lwp_watchdog.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gc/ogc/lwp_watchdog.h b/gc/ogc/lwp_watchdog.h index df4cb8c83..4d2739c6d 100644 --- a/gc/ogc/lwp_watchdog.h +++ b/gc/ogc/lwp_watchdog.h @@ -15,8 +15,8 @@ #define ticks_to_microsecs(ticks) PPCTicksToUs(ticks) #define ticks_to_nanosecs(ticks) PPCTicksToNs(ticks) -#define tick_microsecs(ticks) (PPCTicksToUs(ticks)%TB_USPERSEC) -#define tick_nanosecs(ticks) (PPCTicksToNs(ticks)%TB_NSPERSEC) +#define tick_microsecs(ticks) PPCTicksToUs((ticks)%PPC_TIMER_CLOCK) +#define tick_nanosecs(ticks) PPCTicksToNs((ticks)%PPC_TIMER_CLOCK) #define secs_to_ticks(sec) ((u64)(sec)*PPC_TIMER_CLOCK) #define millisecs_to_ticks(msec) PPCMsToTicks(msec)