Skip to content

Commit 8e2ec43

Browse files
committed
cpu cycles: on arm64 use clock_gettime and PMCCNTR_EL0 for freestanding
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 mirage#216
1 parent 98f01b1 commit 8e2ec43

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/native/entropy_cpu_stubs.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,22 @@ static inline uint32_t read_virtual_count (void)
111111
#endif /* arm */
112112

113113
#if defined (__aarch64__)
114-
#define isb() __asm__ __volatile__("isb" : : : "memory")
114+
#if defined(__ocaml_freestanding__) || defined(__ocaml_solo5__)
115115
static inline uint64_t read_virtual_count(void)
116116
{
117117
uint64_t c;
118-
isb();
119-
__asm__ __volatile__("mrs %0, cntvct_el0":"=r"(c));
118+
__asm__ __volatile__("mrs %0, PMCCNTR_EL0":"=r"(c));
120119
return c;
121120
}
121+
#else
122+
/* see https://github.com/mirage/mirage-crypto/issues/216 */
123+
static inline uint64_t read_virtual_count (void)
124+
{
125+
struct timespec now;
126+
clock_gettime (CLOCK_MONOTONIC, &now);
127+
return now.tv_nsec;
128+
}
129+
#endif /* __ocaml_freestanding__ || __ocaml_solo5__ */
122130
#endif /* aarch64 */
123131

124132
#if defined (__powerpc64__)

0 commit comments

Comments
 (0)