From 48fae4a971b8978d352918e2825e1c0e3c5706fd Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 18 Sep 2022 15:22:43 +0200 Subject: [PATCH] Cache RDSEED and RDRAND presence --- kernel/src/rand/Init.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kernel/src/rand/Init.cpp b/kernel/src/rand/Init.cpp index 65dc9531..d91d7d91 100644 --- a/kernel/src/rand/Init.cpp +++ b/kernel/src/rand/Init.cpp @@ -10,6 +10,8 @@ static uint64_t state = 0xf5026f5ae96319e9; extern "C" int _test_rdseed(); +static bool has_rdrand = false; +static bool has_rdseed = false; static uint64_t rdtsc() { @@ -37,6 +39,9 @@ void Mersenne::init() { kdbgln("Preparing random number generator"); + has_rdrand = CPU::has_feature(CPU::Features::RDRAND); + has_rdseed = _test_rdseed(); + state ^= (0x45fe1024UL + MOON_MAJOR) * (MOON_MINOR ^ 200UL); state ^= 0xe0e4f5332ea75b; @@ -54,9 +59,9 @@ void Mersenne::reseed() { state ^= rdtsc(); - if (CPU::has_feature(CPU::Features::RDRAND)) { state ^= rdrand(); } + if (has_rdrand) { state ^= rdrand(); } - if (_test_rdseed()) { state ^= rdseed(); } + if (has_rdseed) { state ^= rdseed(); } state ^= rdtsc();