mirror of
https://github.com/RGBCube/serenity
synced 2025-05-30 22:28:12 +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:
parent
5ec39ca363
commit
fcf6ccd771
2 changed files with 12 additions and 16 deletions
|
@ -30,7 +30,7 @@ UNMAP_AFTER_INIT KernelRng::KernelRng()
|
||||||
bool supports_rdrand = Processor::current().has_feature(CPUFeature::RDRAND);
|
bool supports_rdrand = Processor::current().has_feature(CPUFeature::RDRAND);
|
||||||
if (supports_rdseed || supports_rdrand) {
|
if (supports_rdseed || supports_rdrand) {
|
||||||
dmesgln("KernelRng: Using RDSEED or RDRAND as entropy source");
|
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;
|
u32 value = 0;
|
||||||
if (supports_rdseed) {
|
if (supports_rdseed) {
|
||||||
asm volatile(
|
asm volatile(
|
||||||
|
@ -46,22 +46,22 @@ UNMAP_AFTER_INIT KernelRng::KernelRng()
|
||||||
: "=r"(value));
|
: "=r"(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
this->resource().add_random_event(value, i % 32);
|
add_random_event(value, i % 32);
|
||||||
}
|
}
|
||||||
} else if (TimeManagement::the().can_query_precise_time()) {
|
} else if (TimeManagement::the().can_query_precise_time()) {
|
||||||
// Add HPET as entropy source if we don't have anything better.
|
// Add HPET as entropy source if we don't have anything better.
|
||||||
dmesgln("KernelRng: Using HPET as entropy source");
|
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();
|
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 {
|
} else {
|
||||||
// Fallback to RTC
|
// Fallback to RTC
|
||||||
dmesgln("KernelRng: Using RTC as entropy source (bad!)");
|
dmesgln("KernelRng: Using RTC as entropy source (bad!)");
|
||||||
auto current_time = static_cast<u64>(RTC::now());
|
auto current_time = static_cast<u64>(RTC::now());
|
||||||
for (size_t i = 0; i < resource().pool_count * resource().reseed_threshold; ++i) {
|
for (size_t i = 0; i < pool_count * reseed_threshold; ++i) {
|
||||||
this->resource().add_random_event(current_time, i % 32);
|
add_random_event(current_time, i % 32);
|
||||||
current_time *= 0x574au;
|
current_time *= 0x574au;
|
||||||
current_time += 0x40b2u;
|
current_time += 0x40b2u;
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,7 @@ UNMAP_AFTER_INIT KernelRng::KernelRng()
|
||||||
void KernelRng::wait_for_entropy()
|
void KernelRng::wait_for_entropy()
|
||||||
{
|
{
|
||||||
SpinlockLocker lock(get_lock());
|
SpinlockLocker lock(get_lock());
|
||||||
if (!resource().is_ready()) {
|
if (!is_ready()) {
|
||||||
dbgln("Entropy starvation...");
|
dbgln("Entropy starvation...");
|
||||||
m_seed_queue.wait_forever("KernelRng");
|
m_seed_queue.wait_forever("KernelRng");
|
||||||
}
|
}
|
||||||
|
@ -80,7 +80,7 @@ void KernelRng::wait_for_entropy()
|
||||||
void KernelRng::wake_if_ready()
|
void KernelRng::wake_if_ready()
|
||||||
{
|
{
|
||||||
VERIFY(get_lock().is_locked());
|
VERIFY(get_lock().is_locked());
|
||||||
if (resource().is_ready()) {
|
if (is_ready()) {
|
||||||
m_seed_queue.wake_all();
|
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) {
|
if (can_wait && allow_wait) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
{
|
{
|
||||||
MutexLocker locker(KernelRng::the().lock());
|
if (kernel_rng.get_random_bytes(buffer)) {
|
||||||
if (kernel_rng.resource().get_random_bytes(buffer)) {
|
|
||||||
result = true;
|
result = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -136,7 +135,7 @@ bool get_good_random_bytes(Bytes buffer, bool allow_wait, bool fallback_to_fast)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// We can't wait/block here, or we are not allowed to block/wait
|
// 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;
|
result = true;
|
||||||
} else if (fallback_to_fast) {
|
} else if (fallback_to_fast) {
|
||||||
// If interrupts are disabled
|
// If interrupts are disabled
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
#include <AK/ByteBuffer.h>
|
#include <AK/ByteBuffer.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <Kernel/Locking/Lockable.h>
|
|
||||||
#include <Kernel/Locking/Mutex.h>
|
#include <Kernel/Locking/Mutex.h>
|
||||||
#include <Kernel/StdLib.h>
|
#include <Kernel/StdLib.h>
|
||||||
#include <LibCrypto/Cipher/AES.h>
|
#include <LibCrypto/Cipher/AES.h>
|
||||||
|
@ -120,7 +119,7 @@ private:
|
||||||
Spinlock m_lock;
|
Spinlock m_lock;
|
||||||
};
|
};
|
||||||
|
|
||||||
class KernelRng : public Lockable<FortunaPRNG<Crypto::Cipher::AESCipher, Crypto::Hash::SHA256, 256>> {
|
class KernelRng : public FortunaPRNG<Crypto::Cipher::AESCipher, Crypto::Hash::SHA256, 256> {
|
||||||
AK_MAKE_ETERNAL;
|
AK_MAKE_ETERNAL;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -131,8 +130,6 @@ public:
|
||||||
|
|
||||||
void wake_if_ready();
|
void wake_if_ready();
|
||||||
|
|
||||||
Spinlock& get_lock() { return resource().get_lock(); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
WaitQueue m_seed_queue;
|
WaitQueue m_seed_queue;
|
||||||
};
|
};
|
||||||
|
@ -168,7 +165,7 @@ public:
|
||||||
SpinlockLocker lock(kernel_rng.get_lock());
|
SpinlockLocker lock(kernel_rng.get_lock());
|
||||||
// We don't lock this because on the off chance a pool is corrupted, entropy isn't lost.
|
// We don't lock this because on the off chance a pool is corrupted, entropy isn't lost.
|
||||||
Event<T> event = { read_tsc(), m_source, event_data };
|
Event<T> event = { read_tsc(), m_source, event_data };
|
||||||
kernel_rng.resource().add_random_event(event, m_pool);
|
kernel_rng.add_random_event(event, m_pool);
|
||||||
m_pool++;
|
m_pool++;
|
||||||
kernel_rng.wake_if_ready();
|
kernel_rng.wake_if_ready();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue