refactor(kernel): ♻️ rewrite time manager

This commit is contained in:
2025-05-23 23:30:04 +00:00
parent 9538589c11
commit 33c284091d
31 changed files with 572 additions and 573 deletions

View File

@ -1389,10 +1389,10 @@ static int linux_nanosleep(SysFrm *,
pReq->tv_nsec, pReq->tv_sec);
uint64_t nanoTime = pReq->tv_nsec;
uint64_t secTime = pReq->tv_sec * 1000000000; /* Nano */
uint64_t secTime = Time::FromSeconds(pReq->tv_sec);
uint64_t time = TimeManager->GetCounter();
uint64_t sleepTime = TimeManager->CalculateTarget(nanoTime + secTime, Time::Nanoseconds);
uint64_t time = TimeManager->GetTimeNs();
uint64_t sleepTime = TimeManager->GetTimeNs() + secTime + nanoTime;
debug("time=%ld secTime=%ld nanoTime=%ld sleepTime=%ld",
time, secTime, nanoTime, sleepTime);
@ -1406,7 +1406,7 @@ static int linux_nanosleep(SysFrm *,
}
pcb->GetContext()->Yield();
time = TimeManager->GetCounter();
time = TimeManager->GetTimeNs();
}
debug("time= %ld", time);
debug("sleepTime=%ld", sleepTime);
@ -2582,7 +2582,7 @@ static int linux_sysinfo(SysFrm *, struct sysinfo *info)
if (pInfo == nullptr)
return -linux_EFAULT;
uint64_t nano = TimeManager->GetNanosecondsSinceClassCreation();
uint64_t nano = TimeManager->GetTimeNs();
if (nano != 0)
nano /= 10000000;
@ -3185,18 +3185,18 @@ static int linux_clock_gettime(SysFrm *, clockid_t clockid, struct timespec *tp)
{
case linux_CLOCK_REALTIME:
{
uint64_t time = TimeManager->GetCounter();
pTp->tv_sec = time / Time::ConvertUnit(Time::Seconds);
pTp->tv_nsec = time / Time::ConvertUnit(Time::Nanoseconds);
uint64_t time = TimeManager->GetTimeNs();
pTp->tv_sec = Time::ToSeconds(time);
pTp->tv_nsec = time;
debug("time=%ld sec=%ld nsec=%ld",
time, pTp->tv_sec, pTp->tv_nsec);
break;
}
case linux_CLOCK_MONOTONIC:
{
uint64_t time = TimeManager->GetCounter();
pTp->tv_sec = time / Time::ConvertUnit(Time::Seconds);
pTp->tv_nsec = time / Time::ConvertUnit(Time::Nanoseconds);
uint64_t time = TimeManager->GetTimeNs();
pTp->tv_sec = Time::ToSeconds(time);
pTp->tv_nsec = time;
debug("time=%ld sec=%ld nsec=%ld",
time, pTp->tv_sec, pTp->tv_nsec);
break;
@ -3244,9 +3244,8 @@ static int linux_clock_nanosleep(SysFrm *, clockid_t clockid, int flags,
case linux_CLOCK_REALTIME:
case linux_CLOCK_MONOTONIC:
{
uint64_t time = TimeManager->GetCounter();
uint64_t rqTime = pRequest->tv_sec * Time::ConvertUnit(Time::Seconds) +
pRequest->tv_nsec * Time::ConvertUnit(Time::Nanoseconds);
uint64_t time = TimeManager->GetTimeNs();
uint64_t rqTime = Time::FromSeconds(pRequest->tv_sec) + pRequest->tv_nsec;
debug("Sleeping for %ld", rqTime - time);
if (rqTime > time)

View File

@ -0,0 +1,100 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <time.hpp>
#include <memory.hpp>
#include <acpi.hpp>
#include <debug.h>
#include <io.h>
#include "../../kernel.h"
namespace Time
{
bool HighPrecisionEventTimer::Sleep(size_t Nanoseconds)
{
uint64_t target = this->GetNanoseconds() + Nanoseconds;
while (this->GetNanoseconds() < target)
CPU::Pause();
return true;
}
uint64_t HighPrecisionEventTimer::GetNanoseconds()
{
#if defined(__amd64__)
uint64_t counter = mminq(&this->hpet->MainCounter);
#elif defined(__i386__)
uint64_t counter = mminl(&this->hpet->MainCounter);
#else
return 0;
#endif
/* convert ticks to nanoseconds: counter * period_fs / 1e6 */
return (counter * 1'000'000'000ULL) / this->Period;
}
HighPrecisionEventTimer::HighPrecisionEventTimer(void *hpet)
{
#if defined(__amd64__) || defined(__i386__)
ACPI::ACPI::HPETHeader *hdr = (ACPI::ACPI::HPETHeader *)hpet;
Memory::Virtual vmm;
vmm.Map((void *)hdr->Address.Address, (void *)hdr->Address.Address, Memory::RW | Memory::PCD | Memory::PWT);
this->hpet = reinterpret_cast<HPET *>(hdr->Address.Address);
debug("%s timer is at address %#lx", hdr->Header.OEMID, hdr->Address.Address);
uint64_t period_fs = this->hpet->CapabilitiesID >> 32;
if (period_fs == 0)
{
warn("HPET: Invalid period in CapabilitiesID");
return;
}
/* Hz = 1e15 / period_fs */
this->Period = 1'000'000'000'000'000ULL / period_fs;
KPrint("HPET tick period: %lu femtoseconds -> %u Hz", period_fs, this->Period);
#ifdef __amd64__
mmoutq(&this->hpet->Configuration, 0);
mmoutq(&this->hpet->MainCounter, 0);
mmoutq(&this->hpet->Configuration, 1);
#else
mmoutl(&this->hpet->Configuration, 0);
mmoutl(&this->hpet->MainCounter, 0);
mmoutl(&this->hpet->Configuration, 1);
#endif
for (int i = 0; i < 5; i++)
{
uint64_t val = mminq(&this->hpet->MainCounter);
KPrint("HPET counter test %d: %llu", i, val);
}
uint64_t cfg = mminq(&this->hpet->Configuration);
if (!(cfg & 1))
warn("HPET counter is not enabled!");
ClassCreationTime = this->GetNanoseconds();
#endif
}
HighPrecisionEventTimer::~HighPrecisionEventTimer()
{
#ifdef __amd64__
mmoutq(&this->hpet->Configuration, 0);
#else
mmoutl(&this->hpet->Configuration, 0);
#endif
}
}

View File

@ -0,0 +1,71 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <time.hpp>
#include <memory.hpp>
#include <acpi.hpp>
#include <debug.h>
#include <io.h>
#include "../../kernel.h"
#define KVM_CLOCK_PAIRING_WALLCLOCK 0
namespace Time
{
extern "C" void kvm_hc_clock_pairing(uint64_t phys_addr, uint64_t clock_type)
{
#if defined(__amd64__)
asm volatile(
"mov $9, %%eax\n\t" /* KVM_HC_CLOCK_PAIRING */
"mov %%rdi, %%rbx\n\t"
"mov %%rsi, %%rcx\n\t"
"vmcall\n\t"
:
: "D"(phys_addr), "S"(clock_type)
: "rax", "rbx", "rcx");
#else
#warning "KVM clock pairing not implemented for this architecture"
#endif
}
bool KVMClock::Sleep(uint64_t Nanoseconds)
{
return true;
}
uint64_t KVMClock::GetNanoseconds()
{
return 0;
}
KVMClock::KVMClock()
{
if (strcmp(CPU::Hypervisor(), x86_CPUID_VENDOR_KVM) != 0)
return;
this->Pairing = (kvm_clock_pairing *)KernelAllocator.RequestPages(TO_PAGES(sizeof(kvm_clock_pairing)));
kvm_hc_clock_pairing((uint64_t)this->Pairing, KVM_CLOCK_PAIRING_WALLCLOCK);
// KPrint("sec: %lld, nsec: %lld, tsc: %lld", this->Pairing->sec, this->Pairing->nsec, this->Pairing->tsc);
// KPrint("flags: %x", this->Pairing->flags);
}
KVMClock::~KVMClock()
{
}
}

View File

@ -0,0 +1,107 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <time.hpp>
#include <memory.hpp>
#include <acpi.hpp>
#include <debug.h>
#include <io.h>
#include "../../kernel.h"
namespace Time
{
void Manager::CheckActiveTimer()
{
if (unlikely(Timers[ActiveTimer]->IsAvailable() == false))
{
for (size_t i = Timers.size(); i-- > 0;)
{
if (Timers[i]->IsAvailable() == false)
continue;
ActiveTimer = i;
break;
}
}
}
bool Manager::Sleep(size_t Nanoseconds)
{
if (unlikely(Timers.empty()))
return false;
this->CheckActiveTimer();
debug("sleep for %d ns in timer %s", Nanoseconds, Timers[ActiveTimer]->Name());
return Timers[ActiveTimer]->Sleep(Nanoseconds);
}
uint64_t Manager::GetTimeNs()
{
if (unlikely(Timers.empty()))
return 0;
this->CheckActiveTimer();
return Timers[ActiveTimer]->GetNanoseconds();
}
const char *Manager::GetActiveTimerName()
{
if (unlikely(Timers.empty()))
return "\0";
this->CheckActiveTimer();
return Timers[ActiveTimer]->Name();
}
void Manager::InitializeTimers()
{
#if defined(__amd64__) || defined(__i386__)
/* TODO: RTC check */
/* TODO: PIT check */
if (acpi)
{
if (((ACPI::ACPI *)acpi)->HPET)
{
ITimer *hpet = new HighPrecisionEventTimer(((ACPI::ACPI *)acpi)->HPET);
ActiveTimer = Timers.size();
Timers.push_back(hpet);
}
/* TODO: ACPI check */
/* TODO: APIC check */
}
else
{
KPrint("\x1b[33mACPI not available");
}
ITimer *tsc = new TimeStampCounter;
ActiveTimer = Timers.size();
Timers.push_back(tsc);
ITimer *kvmclock = new KVMClock;
ActiveTimer = Timers.size();
Timers.push_back(kvmclock);
#endif
assert(Timers.empty() == false);
}
Manager::Manager(void *_acpi) : acpi(_acpi) {}
}

View File

@ -0,0 +1,111 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <time.hpp>
#include <debug.h>
#include <io.h>
namespace Time
{
Clock ReadClock()
{
Clock tm;
#if defined(__amd64__) || defined(__i386__)
uint32_t t = 0;
outb(0x70, 0x00);
t = inb(0x71);
tm.Second = ((t & 0x0F) + ((t >> 4) * 10));
outb(0x70, 0x02);
t = inb(0x71);
tm.Minute = ((t & 0x0F) + ((t >> 4) * 10));
outb(0x70, 0x04);
t = inb(0x71);
tm.Hour = ((t & 0x0F) + ((t >> 4) * 10));
outb(0x70, 0x07);
t = inb(0x71);
tm.Day = ((t & 0x0F) + ((t >> 4) * 10));
outb(0x70, 0x08);
t = inb(0x71);
tm.Month = ((t & 0x0F) + ((t >> 4) * 10));
outb(0x70, 0x09);
t = inb(0x71);
tm.Year = ((t & 0x0F) + ((t >> 4) * 10));
tm.Counter = 0;
#elif defined(__aarch64__)
tm.Year = 0;
tm.Month = 0;
tm.Day = 0;
tm.Hour = 0;
tm.Minute = 0;
tm.Second = 0;
tm.Counter = 0;
#endif
return tm;
}
Clock ConvertFromUnix(int Timestamp)
{
Clock result;
uint64_t Seconds = Timestamp;
uint64_t Minutes = Seconds / 60;
uint64_t Hours = Minutes / 60;
uint64_t Days = Hours / 24;
result.Year = 1970;
while (Days >= 365)
{
if (result.Year % 4 == 0 &&
(result.Year % 100 != 0 ||
result.Year % 400 == 0))
{
if (Days >= 366)
{
Days -= 366;
result.Year++;
}
else
break;
}
else
{
Days -= 365;
result.Year++;
}
}
int DaysInMonth[] = {31,
result.Year % 4 == 0
? 29
: 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (result.Month = 0; result.Month < 12; result.Month++)
{
if (Days < s_cst(uint64_t, (DaysInMonth[result.Month])))
break;
Days -= DaysInMonth[result.Month];
}
result.Month++;
result.Day = s_cst(int, (Days) + 1);
result.Hour = s_cst(int, (Hours % 24));
result.Minute = s_cst(int, (Minutes % 60));
result.Second = s_cst(int, (Seconds % 60));
result.Counter = s_cst(uint64_t, (Timestamp));
return result;
}
}

View File

@ -0,0 +1,105 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include <time.hpp>
#include <memory.hpp>
#include <acpi.hpp>
#include <debug.h>
#include <io.h>
#include "../../kernel.h"
namespace Time
{
static inline uint64_t rdtsc()
{
#if defined(__amd64__) || defined(__i386__)
unsigned int lo, hi;
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
return ((uint64_t)hi << 32) | lo;
#else
return 0;
#endif
}
bool TimeStampCounter::Sleep(uint64_t Nanoseconds)
{
uint64_t target = this->GetNanoseconds() + Nanoseconds;
while (this->GetNanoseconds() < target)
CPU::Pause();
return true;
}
uint64_t TimeStampCounter::GetNanoseconds()
{
uint64_t tsc = rdtsc();
return (tsc * 1000000000ULL) / this->clk;
}
TimeStampCounter::TimeStampCounter()
{
bool TSCInvariant = false;
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
{
CPU::x86::AMD::CPUID0x80000007 cpuid80000007;
if (cpuid80000007.EDX.TscInvariant)
TSCInvariant = true;
}
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
{
// TODO: Intel 0x80000007
CPU::x86::AMD::CPUID0x80000007 cpuid80000007;
if (cpuid80000007.EDX.TscInvariant)
TSCInvariant = true;
}
if (!TSCInvariant)
{
KPrint("\x1b[33mTSC is not invariant");
return;
}
const int attempts = 5;
uint64_t ns = 10000000ULL; /* 10 ms */
uint64_t total_clk = 0;
uint64_t overhead = 0;
for (int i = 0; i < attempts; ++i)
{
uint64_t t0 = rdtsc();
uint64_t t1 = rdtsc();
overhead += (t1 - t0);
}
overhead /= attempts;
for (int i = 0; i < attempts; ++i)
{
uint64_t tsc_start = rdtsc();
uint64_t hpet_start = TimeManager->GetTimeNs();
while (TimeManager->GetTimeNs() - hpet_start < ns)
CPU::Pause();
uint64_t tsc_end = rdtsc();
total_clk += (tsc_end - tsc_start - overhead) * 1000000000ULL / ns;
}
this->clk = total_clk / attempts;
KPrint("TSC frequency: %lu MHz", this->clk / 1000000);
this->ClassCreationTime = this->GetNanoseconds();
fixme("tsc not working as expected");
this->clk = 0; /* disable */
}
}