1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 17:45:09 +00:00

Kernel: Make KernelRng not inherit from Lockable

This class was misusing the outdate Lockable template and didn't take
advantage of the lock/resource separation mechanism fully anyway.

Since the underlying PRNG has its own SpinLock, and we already use that
for synchronization everywhere anyway, we can simply remove the Lockable
inheritance from this class.
This commit is contained in:
Andreas Kling 2021-12-26 14:31:45 +01:00
parent 5ec39ca363
commit fcf6ccd771
2 changed files with 12 additions and 16 deletions

View file

@ -30,7 +30,7 @@ UNMAP_AFTER_INIT KernelRng::KernelRng()
bool supports_rdrand = Processor::current().has_feature(CPUFeature::RDRAND);
if (supports_rdseed || supports_rdrand) {
dmesgln("KernelRng: Using RDSEED or RDRAND as entropy source");
for (size_t i = 0; i < resource().pool_count * resource().reseed_threshold; ++i) {
for (size_t i = 0; i < pool_count * reseed_threshold; ++i) {
u32 value = 0;
if (supports_rdseed) {
asm volatile(
@ -46,22 +46,22 @@ UNMAP_AFTER_INIT KernelRng::KernelRng()
: "=r"(value));
}
this->resource().add_random_event(value, i % 32);
add_random_event(value, i % 32);
}
} else if (TimeManagement::the().can_query_precise_time()) {
// Add HPET as entropy source if we don't have anything better.
dmesgln("KernelRng: Using HPET as entropy source");
for (size_t i = 0; i < resource().pool_count * resource().reseed_threshold; ++i) {
for (size_t i = 0; i < pool_count * reseed_threshold; ++i) {
u64 hpet_time = HPET::the().read_main_counter_unsafe();
this->resource().add_random_event(hpet_time, i % 32);
add_random_event(hpet_time, i % 32);
}
} else {
// Fallback to RTC
dmesgln("KernelRng: Using RTC as entropy source (bad!)");
auto current_time = static_cast<u64>(RTC::now());
for (size_t i = 0; i < resource().pool_count * resource().reseed_threshold; ++i) {
this->resource().add_random_event(current_time, i % 32);
for (size_t i = 0; i < pool_count * reseed_threshold; ++i) {
add_random_event(current_time, i % 32);
current_time *= 0x574au;
current_time += 0x40b2u;
}
@ -71,7 +71,7 @@ UNMAP_AFTER_INIT KernelRng::KernelRng()
void KernelRng::wait_for_entropy()
{
SpinlockLocker lock(get_lock());
if (!resource().is_ready()) {
if (!is_ready()) {
dbgln("Entropy starvation...");
m_seed_queue.wait_forever("KernelRng");
}
@ -80,7 +80,7 @@ void KernelRng::wait_for_entropy()
void KernelRng::wake_if_ready()
{
VERIFY(get_lock().is_locked());
if (resource().is_ready()) {
if (is_ready()) {
m_seed_queue.wake_all();
}
}
@ -126,8 +126,7 @@ bool get_good_random_bytes(Bytes buffer, bool allow_wait, bool fallback_to_fast)
if (can_wait && allow_wait) {
for (;;) {
{
MutexLocker locker(KernelRng::the().lock());
if (kernel_rng.resource().get_random_bytes(buffer)) {
if (kernel_rng.get_random_bytes(buffer)) {
result = true;
break;
}
@ -136,7 +135,7 @@ bool get_good_random_bytes(Bytes buffer, bool allow_wait, bool fallback_to_fast)
}
} else {
// We can't wait/block here, or we are not allowed to block/wait
if (kernel_rng.resource().get_random_bytes(buffer)) {
if (kernel_rng.get_random_bytes(buffer)) {
result = true;
} else if (fallback_to_fast) {
// If interrupts are disabled