83 lines
3.4 KiB
Diff
83 lines
3.4 KiB
Diff
From 9671e0f658ef70bdadf18a060870f76eeb3f5f90 Mon Sep 17 00:00:00 2001
|
|
From: Eduard Abdullin <eabdullin@cloudlinux.com>
|
|
Date: Wed, 27 May 2026 11:48:00 +0500
|
|
Subject: [PATCH] Backport upstream commit 59dc5997: native riscv64 timer
|
|
|
|
The jitterentropy library v3.7.0 fails to build on riscv64 with -std=c11
|
|
because none of the architecture-specific branches in
|
|
jitterentropy-base-user.h match __riscv. Compilation falls through to
|
|
the generic clock_gettime() path, which glibc hides behind
|
|
_POSIX_C_SOURCE >= 199309L in C11 mode:
|
|
|
|
jitterentropy-base-user.h: In function 'jent_get_nstime':
|
|
jitterentropy-base-user.h:235:13: error: implicit declaration of
|
|
function 'clock_gettime' [-Wimplicit-function-declaration]
|
|
jitterentropy-base-user.h:235:27: error: 'CLOCK_REALTIME' undeclared
|
|
|
|
Upstream resolved this on 2026-04-25 in commit 59dc5997 ('refactor
|
|
architecture specific code'), which adds a native riscv branch using
|
|
the rdtime CSR via the rdtime pseudo-instruction. That commit is a
|
|
large refactor (17 files, +1544/-1499 lines) and has not yet appeared
|
|
in any tagged release; the next upstream release (3.7.1) will carry it.
|
|
|
|
Backport the riscv branch only, simplified for RV64. The timer
|
|
implementation is verbatim from upstream arch/jitterentropy-arch-timer.h;
|
|
only the structural wrapper differs (arch dispatch fused with the
|
|
function body, as in v3.7.0). RV32 (rdtimeh/rdtime retry pair) is
|
|
intentionally omitted because AlmaLinux only targets RV64 (riscv64).
|
|
|
|
Using the native time CSR rather than clock_gettime() also preserves
|
|
the entropy quality of the library: clock_gettime(CLOCK_REALTIME) is
|
|
subject to NTP steering and lacks the cycle-level resolution required
|
|
by the jitter entropy source on which all other architectures already
|
|
rely (rdtsc on x86, cntvct_el0 on aarch64, stcke on s390x, mftb on
|
|
ppc64le).
|
|
|
|
Drop this patch when jitterentropy is rebased to >= 3.7.1.
|
|
|
|
---
|
|
jitterentropy-base-user.h | 28 +++++++++++++++++++++++++++-
|
|
1 file changed, 27 insertions(+), 1 deletion(-)
|
|
|
|
diff --git jitterentropy-base-user.h jitterentropy-base-user.h
|
|
index 0cfc526..77e3b38 100644
|
|
--- jitterentropy-base-user.h
|
|
+++ jitterentropy-base-user.h
|
|
@@ -203,7 +203,33 @@ static inline void jent_get_nstime(uint64_t *out)
|
|
*out = (uint64_t)__builtin_ppc_get_timebase();
|
|
}
|
|
|
|
-#else /* (__x86_64__) || (__i386__) || (__aarch64__) || (__s390x__) || (__powerpc__) */
|
|
+#elif defined(__riscv)
|
|
+/*
|
|
+ * Backport of upstream commit 59dc5997 ("refactor architecture specific
|
|
+ * code", 2026-04-25) -- riscv branch only, simplified for RV64. To be
|
|
+ * obsoleted by jitterentropy >= 3.7.1, which carries the same logic in
|
|
+ * arch/jitterentropy-arch-timer.h (with full RV32 support).
|
|
+ *
|
|
+ * The "time" CSR is the platform timer and is reliably accessible from
|
|
+ * user mode on Linux (the kernel enables [s|m]counteren.TM). The "cycle"
|
|
+ * CSR has higher resolution but is not always user-readable. Override
|
|
+ * RISCV_NSTIME_INSN to switch sources, e.g. to "rdcycle".
|
|
+ *
|
|
+ * AlmaLinux only targets RV64 (riscv64); RV32 support is intentionally
|
|
+ * not backported (it would require a rdtimeh/rdtime retry pair).
|
|
+ */
|
|
+# ifndef RISCV_NSTIME_INSN
|
|
+# define RISCV_NSTIME_INSN "rdtime"
|
|
+# endif
|
|
+
|
|
+static inline void jent_get_nstime(uint64_t *out)
|
|
+{
|
|
+ uint64_t ctr_val;
|
|
+ __asm__ __volatile__(RISCV_NSTIME_INSN " %0" : "=r" (ctr_val));
|
|
+ *out = ctr_val;
|
|
+}
|
|
+
|
|
+#else /* (__x86_64__) || (__i386__) || (__aarch64__) || (__s390x__) || (__powerpc__) || (__riscv) */
|
|
|
|
static inline void jent_get_nstime(uint64_t *out)
|
|
{
|
|
--
|
|
2.51.0
|