Skip to content

Commit

Permalink
cpu cycles: on arm64 use clock_gettime and PMCCNTR_EL0 for freestanding
Browse files Browse the repository at this point in the history
previously, cntvct_el0 was used which is capped to 24 MHz, leading to entropy
test failures on macOS and linux-aarch64. The PMCCNTR_EL0 is not available in
user-space normally (for Linux, there is a kernel module).

So, what we do is similar to the ARM32 code path now.

Fixes #216
  • Loading branch information
hannesm committed Jun 12, 2024
1 parent 98f01b1 commit 8e2ec43
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/native/entropy_cpu_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,22 @@ static inline uint32_t read_virtual_count (void)
#endif /* arm */

#if defined (__aarch64__)
#define isb() __asm__ __volatile__("isb" : : : "memory")
#if defined(__ocaml_freestanding__) || defined(__ocaml_solo5__)
static inline uint64_t read_virtual_count(void)
{
uint64_t c;
isb();
__asm__ __volatile__("mrs %0, cntvct_el0":"=r"(c));
__asm__ __volatile__("mrs %0, PMCCNTR_EL0":"=r"(c));
return c;
}
#else
/* see https://github.com/mirage/mirage-crypto/issues/216 */
static inline uint64_t read_virtual_count (void)
{
struct timespec now;
clock_gettime (CLOCK_MONOTONIC, &now);
return now.tv_nsec;
}
#endif /* __ocaml_freestanding__ || __ocaml_solo5__ */
#endif /* aarch64 */

#if defined (__powerpc64__)
Expand Down

0 comments on commit 8e2ec43

Please sign in to comment.