summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaciej Zygmanowski <sppmacd@pm.me>2021-01-24 12:25:03 +0100
committerAndreas Kling <kling@serenityos.org>2021-01-26 21:14:44 +0100
commit645657865d17eb4b7257946562e5047c98026aca (patch)
tree5716fcc271f356ed6868de5d73ecaefe6724727c
parent0a61924727082ba5f4d53b19392ba3dd66880bf8 (diff)
downloadserenity-645657865d17eb4b7257946562e5047c98026aca.zip
Kernel: Add RTC as fallback entropy source if HPET is not found
-rw-r--r--Kernel/Random.cpp14
-rw-r--r--Kernel/Time/TimeManagement.h2
2 files changed, 14 insertions, 2 deletions
diff --git a/Kernel/Random.cpp b/Kernel/Random.cpp
index 719bf1c45b..3545bb326a 100644
--- a/Kernel/Random.cpp
+++ b/Kernel/Random.cpp
@@ -30,6 +30,7 @@
#include <Kernel/Devices/RandomDevice.h>
#include <Kernel/Random.h>
#include <Kernel/Time/HPET.h>
+#include <Kernel/Time/RTC.h>
#include <Kernel/Time/TimeManagement.h>
namespace Kernel {
@@ -65,14 +66,23 @@ KernelRng::KernelRng()
this->resource().add_random_event(value, i % 32);
}
- } else {
+ } else if (TimeManagement::the().can_query_precise_time()) {
// Add HPET as entropy source if we don't have anything better.
- klog() << "KernelRng: Using HPET as entropy source (bad!)";
+ klog() << "KernelRng: Using HPET as entropy source";
for (size_t i = 0; i < resource().pool_count * resource().reseed_threshold; ++i) {
u64 hpet_time = HPET::the().read_main_counter();
this->resource().add_random_event(hpet_time, i % 32);
}
+ } else {
+ // Fallback to RTC
+ klog() << "KernelRng: Using RTC as entropy source (bad!)";
+ time_t current_time = RTC::now();
+ for (size_t i = 0; i < resource().pool_count * resource().reseed_threshold; ++i) {
+ this->resource().add_random_event(current_time, i % 32);
+ current_time *= 0x574a;
+ current_time += 0x40b2;
+ }
}
}
diff --git a/Kernel/Time/TimeManagement.h b/Kernel/Time/TimeManagement.h
index bed0fd281d..f6c40aee1e 100644
--- a/Kernel/Time/TimeManagement.h
+++ b/Kernel/Time/TimeManagement.h
@@ -80,6 +80,8 @@ public:
timespec remaining_epoch_time_adjustment() const { return m_remaining_epoch_time_adjustment; }
void set_remaining_epoch_time_adjustment(const timespec& adjustment) { m_remaining_epoch_time_adjustment = adjustment; }
+ bool can_query_precise_time() const { return m_can_query_precise_time; }
+
private:
bool probe_and_set_legacy_hardware_timers();
bool probe_and_set_non_legacy_hardware_timers();