diff --git a/kernel/cycle.h b/kernel/cycle.h index 16dfdc98f..ca2b3b706 100644 --- a/kernel/cycle.h +++ b/kernel/cycle.h @@ -562,3 +562,28 @@ static inline ticks getticks(void) INLINE_ELAPSED(inline) #define HAVE_TICK_COUNTER #endif + +#if defined(__riscv_xlen) && !defined(HAVE_TICK_COUNTER) +typedef uint64_t ticks; +static inline ticks getticks(void) +{ + uint64_t result; +#if __riscv_xlen == 64 + asm volatile("rdtime %0" : "=r" (result)); +#elif __riscv_xlen == 32 + uint32_t l, h, h2; + asm volatile( "start:\n" + "rdtimeh %0\n" + "rdtime %1\n" + "rdtimeh %2\n" + "bne %0, %2, start\n" + : "=r" (h), "=r" (l), "=r" (h2)); + result = (((uint64_t)h)<<32) | ((uint64_t)l); +#else +#error "unknown __riscv_xlen" +#endif + return result; +} +INLINE_ELAPSED(inline) +#define HAVE_TICK_COUNTER +#endif