Attention is currently required from: Julius Werner.
Yidi Lin has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/78800?usp=email )
Change subject: arch/arm64/arch_timer: Fix possible overflow in multiplication ......................................................................
arch/arm64/arch_timer: Fix possible overflow in multiplication
The value from raw_read_cntfrq_el0() could be large enough to cause overflow when multiplied by 1000000. To prevent this, both 1000000 and tfreq can be reduced by dividing them by their GCD.
BUG=b:307790895 TEST=emerge-geralt coreboot TEST=boot to kernel and check the timestamps from `cbmem`
Change-Id: I366667de05392913150414f0fa9058725be71c52 Signed-off-by: Yidi Lin yidilin@chromium.org --- M src/arch/arm64/arch_timer.c 1 file changed, 15 insertions(+), 2 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/00/78800/1
diff --git a/src/arch/arm64/arch_timer.c b/src/arch/arm64/arch_timer.c index 089afee..86d9220 100644 --- a/src/arch/arm64/arch_timer.c +++ b/src/arch/arm64/arch_timer.c @@ -1,12 +1,25 @@ /* SPDX-License-Identifier: GPL-2.0-only */
+#include <assert.h> +#include <gcd.h> #include <timer.h> #include <arch/lib_helpers.h>
void timer_monotonic_get(struct mono_time *mt) { uint64_t tvalue = raw_read_cntpct_el0(); - uint32_t tfreq = raw_read_cntfrq_el0(); - long usecs = (tvalue * 1000000) / tfreq; + static uint32_t tfreq, mult; + uint32_t div; + + if (tfreq == 0) { + tfreq = raw_read_cntfrq_el0(); + assert(tfreq > 0) + mult = 1000000; + div = gcd(mult, tfreq); + tfreq /= div; + mult /=div; + } + + long usecs = (tvalue * mult) / tfreq; mono_time_set_usecs(mt, usecs); }