mirror of
https://github.com/SerenityOS/serenity.git
synced 2026-11-07 04:29:30 -10:00
Prekernel: Move the generate_secure_seed function out of init.cpp
As part of a commit series trying to make the prekernel nicer, we should move and refactor functions out of the init.cpp file if possible.
This commit is contained in:
1 parent
f6e01aae9a
commit
bd474dab14
5 files changed
+55
-32
No files matched your search
@@ -12,6 +12,7 @@ set(SOURCES
|
||||
DebugOutput.cpp
|
||||
kmalloc.cpp
|
||||
Runtime.cpp
|
||||
Random.cpp
|
||||
../../Userland/Libraries/LibELF/Relocation.cpp
|
||||
)
|
||||
|
||||
|
||||
@@ -20,6 +20,9 @@
|
||||
#define KERNEL_MAPPING_BASE 0x2000000000
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
extern "C" multiboot_info_t* multiboot_info_ptr;
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
# if ARCH(X86_64)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2024, Liav A. <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Platform.h>
|
||||
#include <Kernel/Boot/Multiboot.h>
|
||||
#include <Kernel/Prekernel/Prekernel.h>
|
||||
#include <Kernel/Prekernel/Random.h>
|
||||
#if ARCH(X86_64)
|
||||
# include <Kernel/Arch/x86_64/ASM_wrapper.h>
|
||||
# include <Kernel/Arch/x86_64/CPUID.h>
|
||||
#endif
|
||||
|
||||
u64 generate_secure_seed()
|
||||
{
|
||||
u32 seed = 0xFEEBDAED;
|
||||
|
||||
#if ARCH(X86_64)
|
||||
Kernel::CPUID processor_info(0x1);
|
||||
if (processor_info.edx() & (1 << 4)) // TSC
|
||||
seed ^= Kernel::read_tsc();
|
||||
|
||||
if (processor_info.ecx() & (1 << 30)) // RDRAND
|
||||
seed ^= Kernel::read_rdrand();
|
||||
|
||||
Kernel::CPUID extended_features(0x7);
|
||||
if (extended_features.ebx() & (1 << 18)) // RDSEED
|
||||
seed ^= Kernel::read_rdseed();
|
||||
#else
|
||||
# warning No native randomness source available for this architecture
|
||||
#endif
|
||||
|
||||
seed ^= multiboot_info_ptr->mods_addr;
|
||||
seed ^= multiboot_info_ptr->framebuffer_addr;
|
||||
|
||||
return seed;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Copyright (c) 2024, Liav A. <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Types.h>
|
||||
|
||||
u64 generate_secure_seed();
|
||||
@@ -12,15 +12,11 @@
|
||||
#include <Kernel/Memory/VirtualAddress.h>
|
||||
#include <Kernel/Prekernel/DebugOutput.h>
|
||||
#include <Kernel/Prekernel/Prekernel.h>
|
||||
#include <Kernel/Prekernel/Random.h>
|
||||
#include <Kernel/Prekernel/Runtime.h>
|
||||
#include <LibELF/ELFABI.h>
|
||||
#include <LibELF/Relocation.h>
|
||||
|
||||
#if ARCH(X86_64)
|
||||
# include <Kernel/Arch/x86_64/ASM_wrapper.h>
|
||||
# include <Kernel/Arch/x86_64/CPUID.h>
|
||||
#endif
|
||||
|
||||
// Defined in the linker script
|
||||
extern uintptr_t __stack_chk_guard;
|
||||
uintptr_t __stack_chk_guard __attribute__((used));
|
||||
@@ -66,8 +62,6 @@ extern "C" [[noreturn]] void init();
|
||||
// This is where C++ execution begins, after boot.S transfers control here.
|
||||
//
|
||||
|
||||
u64 generate_secure_seed();
|
||||
|
||||
static void memmove_virt(void* dest_virt, FlatPtr dest_phys, void* src, size_t n)
|
||||
{
|
||||
if (dest_phys < (FlatPtr)src) {
|
||||
@@ -258,31 +252,6 @@ extern "C" [[noreturn]] void init()
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
u64 generate_secure_seed()
|
||||
{
|
||||
u32 seed = 0xFEEBDAED;
|
||||
|
||||
#if ARCH(X86_64)
|
||||
CPUID processor_info(0x1);
|
||||
if (processor_info.edx() & (1 << 4)) // TSC
|
||||
seed ^= read_tsc();
|
||||
|
||||
if (processor_info.ecx() & (1 << 30)) // RDRAND
|
||||
seed ^= read_rdrand();
|
||||
|
||||
CPUID extended_features(0x7);
|
||||
if (extended_features.ebx() & (1 << 18)) // RDSEED
|
||||
seed ^= read_rdseed();
|
||||
#else
|
||||
# warning No native randomness source available for this architecture
|
||||
#endif
|
||||
|
||||
seed ^= multiboot_info_ptr->mods_addr;
|
||||
seed ^= multiboot_info_ptr->framebuffer_addr;
|
||||
|
||||
return seed;
|
||||
}
|
||||
|
||||
// Define some Itanium C++ ABI methods to stop the linker from complaining.
|
||||
// If we actually call these something has gone horribly wrong
|
||||
void* __dso_handle __attribute__((visibility("hidden")));
|
||||
|
||||
Reference in new issue
Block a user