Kernel: Abstract platform-specific current time methods from Scheduler

This change ensures that the scheduler doesn't depend on a platform
specific or arch-specific code when it initializes itself, but rather we
ensure that in compile-time we will generate the appropriate code to
find the correct arch-specific current time methods.
This commit is contained in:
Liav A 2022-10-08 04:27:48 +03:00 committed by Linus Groh
parent c439a34ff7
commit 3651d9701e
Notes: sideshowbarker 2024-07-17 10:10:18 +09:00
5 changed files with 71 additions and 11 deletions

17
Kernel/Arch/CurrentTime.h Normal file
View file

@ -0,0 +1,17 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
namespace Kernel {
typedef u64 (*fptr)();
fptr optional_current_time();
}

View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <Kernel/Arch/CurrentTime.h>
namespace Kernel {
fptr optional_current_time()
{
return nullptr;
}
}

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Arch/CurrentTime.h>
#include <Kernel/Arch/x86/ASM_wrapper.h>
#include <Kernel/Arch/x86/Processor.h>
namespace Kernel {
static u64 current_time_tsc()
{
return read_tsc();
}
fptr optional_current_time()
{
VERIFY(Processor::is_initialized()); // sanity check
// Figure out a good scheduling time source
if (Processor::current().has_feature(CPUFeature::TSC) && Processor::current().has_feature(CPUFeature::CONSTANT_TSC)) {
return current_time_tsc;
}
return nullptr;
}
}

View file

@ -327,6 +327,8 @@ if ("${SERENITY_ARCH}" STREQUAL "i686" OR "${SERENITY_ARCH}" STREQUAL "x86_64")
Arch/x86/common/SmapDisabler.cpp
Arch/x86/common/Shutdown.cpp
Arch/x86/CurrentTime.cpp
Arch/x86/Hypervisor/BochsDisplayConnector.cpp
Arch/x86/Hypervisor/VMWareBackdoor.cpp
@ -477,6 +479,7 @@ else()
Arch/aarch64/boot.S
Arch/aarch64/BootPPMParser.cpp
Arch/aarch64/CrashHandler.cpp
Arch/aarch64/CurrentTime.cpp
Arch/aarch64/Dummy.cpp
Arch/aarch64/Exceptions.cpp
Arch/aarch64/init.cpp

View file

@ -8,6 +8,7 @@
#include <AK/ScopeGuard.h>
#include <AK/Singleton.h>
#include <AK/Time.h>
#include <Kernel/Arch/CurrentTime.h>
#include <Kernel/Arch/InterruptDisabler.h>
#include <Kernel/Arch/x86/TrapFrame.h>
#include <Kernel/Debug.h>
@ -365,11 +366,6 @@ Process* Scheduler::colonel()
return s_colonel_process;
}
static u64 current_time_tsc()
{
return read_tsc();
}
static u64 current_time_monotonic()
{
// We always need a precise timestamp here, we cannot rely on a coarse timestamp
@ -380,13 +376,11 @@ UNMAP_AFTER_INIT void Scheduler::initialize()
{
VERIFY(Processor::is_initialized()); // sanity check
// Figure out a good scheduling time source
if (Processor::current().has_feature(CPUFeature::TSC) && Processor::current().has_feature(CPUFeature::CONSTANT_TSC)) {
current_time = current_time_tsc;
} else {
// TODO: Using HPET is rather slow, can we use any other time source that may be faster?
auto* possible_arch_specific_current_time_function = optional_current_time();
if (possible_arch_specific_current_time_function)
current_time = possible_arch_specific_current_time_function;
else
current_time = current_time_monotonic;
}
LockRefPtr<Thread> idle_thread;
g_finalizer_wait_queue = new WaitQueue;