libpayload/libc/time: Fix possible overflow in multiplication

The value from raw_read_cntfrq_el0() could be large enough to cause
overflow when multiplied by USECS_PER_SEC. To prevent this, both
USECS_PER_SEC and hz can be reduced by dividing them by their GCD.

This patch also modifies the return type of `timer_hz()` from
`uint64_t` to `uint32_t`, assuming that in practice the timestamp
counter should never be that fast.

BUG=b:307790895
TEST=boot to kernel and check the timestamps from `cbmem`

Change-Id: Ia55532490651fcf47128b83a8554751f050bcc89
Signed-off-by: Yidi Lin <yidilin@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/78888
Reviewed-by: Julius Werner <jwerner@chromium.org>
Reviewed-by: Yu-Ping Wu <yupingso@google.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Yidi Lin
2023-11-02 14:17:02 +08:00
committed by Julius Werner
parent e91785dfd8
commit eabdd0252a
5 changed files with 16 additions and 9 deletions

View File

@ -519,7 +519,7 @@ void lib_sysinfo_get_memranges(struct memrange **ranges,
/* Timer functions. */
/* Defined by each architecture. */
uint64_t timer_hz(void);
uint32_t timer_hz(void);
uint64_t timer_raw_value(void);
uint64_t timer_us(uint64_t base);
/* Generic. */