Skip to content

Commit

Permalink
riscv64: Replace rdcycle64 with rdtime64 when running in userspace (#194
Browse files Browse the repository at this point in the history
)

Reviewed-by: Török Edwin <edwintorok@users.noreply.github.com>
  • Loading branch information
AdrianBunk authored Feb 24, 2024
1 parent 9ce288d commit 482d4b2
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/native/entropy_cpu_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,15 @@ static inline uint64_t read_cycle_counter(void)
#endif

#if defined (__riscv) && (64 == __riscv_xlen)
static inline uint64_t rdcycle64(void)
//since rdcycle is a privileged instruction since linux 6.6, we use rdtime when in user-space
static inline uint64_t cycle_count(void)
{
uint64_t rval;
#if defined(__ocaml_freestanding__) || defined(__ocaml_solo5__)
__asm__ __volatile__ ("rdcycle %0" : "=r" (rval));
#else
__asm__ __volatile__ ("rdtime %0" : "=r" (rval));
#endif /* __ocaml_freestanding__ || __ocaml_solo5__ */
return rval;
}
#endif
Expand Down Expand Up @@ -176,7 +181,7 @@ CAMLprim value mc_cycle_counter (value __unused(unit)) {
#elif defined(__powerpc64__)
return Val_long (read_cycle_counter ());
#elif defined(__riscv) && (64 == __riscv_xlen)
return Val_long (rdcycle64 ());
return Val_long (cycle_count ());
#elif defined (__s390x__)
return Val_long (getticks ());
#elif defined(__mips__)
Expand Down

0 comments on commit 482d4b2

Please sign in to comment.