mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-07-11 07:19:20 +00:00
Restructured and rewritten entire codebase
This commit is contained in:
100
core/time/hpet.cpp
Normal file
100
core/time/hpet.cpp
Normal 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 Duration, Units Unit)
|
||||
{
|
||||
#if defined(a64)
|
||||
uint64_t Target = mminq(&((HPET *)hpet)->MainCounterValue) + (Duration * ConvertUnit(Unit)) / clk;
|
||||
while (mminq(&((HPET *)hpet)->MainCounterValue) < Target)
|
||||
CPU::Pause();
|
||||
return true;
|
||||
#elif defined(a32)
|
||||
uint64_t Target = mminl(&((HPET *)hpet)->MainCounterValue) + (Duration * ConvertUnit(Unit)) / clk;
|
||||
while (mminl(&((HPET *)hpet)->MainCounterValue) < Target)
|
||||
CPU::Pause();
|
||||
return true;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
uint64_t HighPrecisionEventTimer::GetCounter()
|
||||
{
|
||||
#if defined(a64)
|
||||
return mminq(&((HPET *)hpet)->MainCounterValue);
|
||||
#elif defined(a32)
|
||||
return mminl(&((HPET *)hpet)->MainCounterValue);
|
||||
#endif
|
||||
}
|
||||
|
||||
uint64_t HighPrecisionEventTimer::CalculateTarget(uint64_t Target, Units Unit)
|
||||
{
|
||||
#if defined(a64)
|
||||
return mminq(&((HPET *)hpet)->MainCounterValue) + (Target * ConvertUnit(Unit)) / clk;
|
||||
#elif defined(a32)
|
||||
return mminl(&((HPET *)hpet)->MainCounterValue) + (Target * ConvertUnit(Unit)) / clk;
|
||||
#endif
|
||||
}
|
||||
|
||||
uint64_t HighPrecisionEventTimer::GetNanosecondsSinceClassCreation()
|
||||
{
|
||||
#if defined(a86)
|
||||
uint64_t Subtraction = this->GetCounter() - this->ClassCreationTime;
|
||||
if (Subtraction <= 0 || this->clk <= 0)
|
||||
return 0;
|
||||
return uint64_t(Subtraction / (this->clk / ConvertUnit(Units::Nanoseconds)));
|
||||
#endif
|
||||
}
|
||||
|
||||
HighPrecisionEventTimer::HighPrecisionEventTimer(void *hpet)
|
||||
{
|
||||
#if defined(a86)
|
||||
ACPI::ACPI::HPETHeader *HPET_HDR = (ACPI::ACPI::HPETHeader *)hpet;
|
||||
Memory::Virtual().Map((void *)HPET_HDR->Address.Address,
|
||||
(void *)HPET_HDR->Address.Address,
|
||||
Memory::PTFlag::RW | Memory::PTFlag::PCD);
|
||||
this->hpet = (HPET *)HPET_HDR->Address.Address;
|
||||
trace("%s timer is at address %016p", HPET_HDR->Header.OEMID, (void *)HPET_HDR->Address.Address);
|
||||
clk = s_cst(uint32_t, (uint64_t)this->hpet->GeneralCapabilities >> 32);
|
||||
debug("HPET clock is %u Hz", clk);
|
||||
#ifdef a64
|
||||
mmoutq(&this->hpet->GeneralConfiguration, 0);
|
||||
mmoutq(&this->hpet->MainCounterValue, 0);
|
||||
mmoutq(&this->hpet->GeneralConfiguration, 1);
|
||||
#else
|
||||
mmoutl(&this->hpet->GeneralConfiguration, 0);
|
||||
mmoutl(&this->hpet->MainCounterValue, 0);
|
||||
mmoutl(&this->hpet->GeneralConfiguration, 1);
|
||||
#endif
|
||||
ClassCreationTime = this->GetCounter();
|
||||
#endif
|
||||
}
|
||||
|
||||
HighPrecisionEventTimer::~HighPrecisionEventTimer()
|
||||
{
|
||||
}
|
||||
}
|
111
core/time/time.cpp
Normal file
111
core/time/time.cpp
Normal 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(a86)
|
||||
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(aa64)
|
||||
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;
|
||||
}
|
||||
}
|
210
core/time/timer.cpp
Normal file
210
core/time/timer.cpp
Normal file
@ -0,0 +1,210 @@
|
||||
/*
|
||||
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 time::Sleep(size_t Duration, Units Unit)
|
||||
{
|
||||
switch (ActiveTimer)
|
||||
{
|
||||
case NONE:
|
||||
error("No timer is active");
|
||||
return false;
|
||||
case RTC:
|
||||
fixme("RTC sleep not implemented");
|
||||
return false;
|
||||
case PIT:
|
||||
fixme("PIT sleep not implemented");
|
||||
return false;
|
||||
case HPET:
|
||||
return this->hpet->Sleep(Duration, Unit);
|
||||
case ACPI:
|
||||
fixme("ACPI sleep not implemented");
|
||||
return false;
|
||||
case APIC:
|
||||
fixme("APIC sleep not implemented");
|
||||
return false;
|
||||
case TSC:
|
||||
return this->tsc->Sleep(Duration, Unit);
|
||||
default:
|
||||
error("Unknown timer");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t time::GetCounter()
|
||||
{
|
||||
switch (ActiveTimer)
|
||||
{
|
||||
case NONE:
|
||||
error("No timer is active");
|
||||
return 0;
|
||||
case RTC:
|
||||
fixme("RTC sleep not implemented");
|
||||
return 0;
|
||||
case PIT:
|
||||
fixme("PIT sleep not implemented");
|
||||
return 0;
|
||||
case HPET:
|
||||
return this->hpet->GetCounter();
|
||||
case ACPI:
|
||||
fixme("ACPI sleep not implemented");
|
||||
return 0;
|
||||
case APIC:
|
||||
fixme("APIC sleep not implemented");
|
||||
return 0;
|
||||
case TSC:
|
||||
return this->tsc->GetCounter();
|
||||
default:
|
||||
error("Unknown timer");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t time::CalculateTarget(uint64_t Target, Units Unit)
|
||||
{
|
||||
switch (ActiveTimer)
|
||||
{
|
||||
case NONE:
|
||||
error("No timer is active");
|
||||
return 0;
|
||||
case RTC:
|
||||
fixme("RTC sleep not implemented");
|
||||
return 0;
|
||||
case PIT:
|
||||
fixme("PIT sleep not implemented");
|
||||
return 0;
|
||||
case HPET:
|
||||
return this->hpet->CalculateTarget(Target, Unit);
|
||||
case ACPI:
|
||||
fixme("ACPI sleep not implemented");
|
||||
return 0;
|
||||
case APIC:
|
||||
fixme("APIC sleep not implemented");
|
||||
return 0;
|
||||
case TSC:
|
||||
return this->tsc->CalculateTarget(Target, Unit);
|
||||
default:
|
||||
error("Unknown timer");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t time::GetNanosecondsSinceClassCreation()
|
||||
{
|
||||
switch (ActiveTimer)
|
||||
{
|
||||
case NONE:
|
||||
error("No timer is active");
|
||||
return 0;
|
||||
case RTC:
|
||||
fixme("RTC sleep not implemented");
|
||||
return 0;
|
||||
case PIT:
|
||||
fixme("PIT sleep not implemented");
|
||||
return 0;
|
||||
case HPET:
|
||||
return this->hpet->GetNanosecondsSinceClassCreation();
|
||||
case ACPI:
|
||||
fixme("ACPI sleep not implemented");
|
||||
return 0;
|
||||
case APIC:
|
||||
fixme("APIC sleep not implemented");
|
||||
return 0;
|
||||
case TSC:
|
||||
return this->tsc->GetNanosecondsSinceClassCreation();
|
||||
default:
|
||||
error("Unknown timer");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void time::FindTimers(void *acpi)
|
||||
{
|
||||
#if defined(a86)
|
||||
/* TODO: RTC check */
|
||||
/* TODO: PIT check */
|
||||
|
||||
if (acpi)
|
||||
{
|
||||
if (((ACPI::ACPI *)acpi)->HPET)
|
||||
{
|
||||
hpet = new HighPrecisionEventTimer(((ACPI::ACPI *)acpi)->HPET);
|
||||
ActiveTimer = HPET;
|
||||
SupportedTimers |= HPET;
|
||||
KPrint("\e11FF11HPET found");
|
||||
}
|
||||
else
|
||||
{
|
||||
KPrint("\eFF2200HPET not found");
|
||||
}
|
||||
|
||||
/* TODO: ACPI check */
|
||||
/* TODO: APIC check */
|
||||
}
|
||||
else
|
||||
{
|
||||
KPrint("\eFF2200ACPI not found");
|
||||
}
|
||||
|
||||
bool TSCInvariant = false;
|
||||
if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_AMD) == 0)
|
||||
{
|
||||
CPU::x86::AMD::CPUID0x80000007 cpuid80000007;
|
||||
cpuid80000007.Get();
|
||||
if (cpuid80000007.EDX.TscInvariant)
|
||||
TSCInvariant = true;
|
||||
}
|
||||
else if (strcmp(CPU::Vendor(), x86_CPUID_VENDOR_INTEL) == 0)
|
||||
{
|
||||
// TODO: Intel 0x80000007
|
||||
CPU::x86::AMD::CPUID0x80000007 cpuid80000007;
|
||||
cpuid80000007.Get();
|
||||
if (cpuid80000007.EDX.TscInvariant)
|
||||
TSCInvariant = true;
|
||||
}
|
||||
|
||||
if (TSCInvariant)
|
||||
{
|
||||
tsc = new TimeStampCounter;
|
||||
// FIXME: ActiveTimer = TSC;
|
||||
SupportedTimers |= TSC;
|
||||
KPrint("\e11FF11Invariant TSC found");
|
||||
}
|
||||
else
|
||||
KPrint("\eFF2200TSC is not invariant");
|
||||
#endif
|
||||
}
|
||||
|
||||
time::time()
|
||||
{
|
||||
}
|
||||
|
||||
time::~time()
|
||||
{
|
||||
}
|
||||
}
|
76
core/time/tsc.cpp
Normal file
76
core/time/tsc.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
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 TimeStampCounter::Sleep(size_t Duration, Units Unit)
|
||||
{
|
||||
#if defined(a86)
|
||||
uint64_t Target = this->GetCounter() + (Duration * ConvertUnit(Unit)) / this->clk;
|
||||
while (this->GetCounter() < Target)
|
||||
CPU::Pause();
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
uint64_t TimeStampCounter::GetCounter()
|
||||
{
|
||||
#if defined(a86)
|
||||
return CPU::Counter();
|
||||
#endif
|
||||
}
|
||||
|
||||
uint64_t TimeStampCounter::CalculateTarget(uint64_t Target, Units Unit)
|
||||
{
|
||||
#if defined(a86)
|
||||
return uint64_t((this->GetCounter() + (Target * ConvertUnit(Unit))) / this->clk);
|
||||
#endif
|
||||
}
|
||||
|
||||
uint64_t TimeStampCounter::GetNanosecondsSinceClassCreation()
|
||||
{
|
||||
#if defined(a86)
|
||||
return uint64_t((this->GetCounter() - this->ClassCreationTime) / this->clk);
|
||||
#endif
|
||||
}
|
||||
|
||||
TimeStampCounter::TimeStampCounter()
|
||||
{
|
||||
#if defined(a86)
|
||||
fixme(""); // FIXME: This is not a good way to measure the clock speed
|
||||
uint64_t Start = CPU::Counter();
|
||||
TimeManager->Sleep(1, Units::Milliseconds);
|
||||
uint64_t End = CPU::Counter();
|
||||
|
||||
this->clk = End - Start;
|
||||
this->ClassCreationTime = this->GetCounter();
|
||||
#endif
|
||||
}
|
||||
|
||||
TimeStampCounter::~TimeStampCounter()
|
||||
{
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user