Kernel: Add function to retrieve boot time as timespec

This commit is contained in:
Bananymous
2023-08-04 15:44:32 +03:00
parent 1f8a5f0ce5
commit ca5a097ef5
7 changed files with 55 additions and 6 deletions

View File

@@ -22,7 +22,10 @@
#define HPET_Tn_PER_INT_CAP (1 << 4)
#define HPET_Tn_VAL_SET_CNF (1 << 6)
#define FS_PER_MS 1'000'000'000'000
#define FS_PER_S 1'000'000'000'000'000
#define FS_PER_MS 1'000'000'000'000
#define FS_PER_US 1'000'000'000
#define FS_PER_NS 1'000'000
namespace Kernel
{
@@ -66,6 +69,18 @@ namespace Kernel
m_counter_tick_period_fs = read_register(HPET_REG_CAPABILIES) >> 32;
uint64_t ticks_per_ms = FS_PER_MS / m_counter_tick_period_fs;
{
const char* units[] = { "fs", "ps", "ns", "us", "ms", "s" };
int index = 0;
uint64_t temp = m_counter_tick_period_fs;
while (temp >= 1000)
{
temp /= 1000;
index++;
}
dprintln("HPET percision {} {}", temp, units[index]);
}
uint64_t timer0_config = read_register(HPET_REG_TIMER_CONFIG(0));
if (!(timer0_config & HPET_Tn_PER_INT_CAP))
@@ -100,6 +115,15 @@ namespace Kernel
return read_register(HPET_REG_COUNTER) * m_counter_tick_period_fs / FS_PER_MS;
}
timespec HPET::time_since_boot() const
{
uint64_t time_fs = read_register(HPET_REG_COUNTER) * m_counter_tick_period_fs;
return timespec {
.tv_sec = time_fs / FS_PER_S,
.tv_nsec = (long)((time_fs % FS_PER_S) / FS_PER_NS)
};
}
void HPET::write_register(ptrdiff_t reg, uint64_t value) const
{
MMIO::write64(m_mmio_base + reg, value);

View File

@@ -21,6 +21,9 @@
#define BASE_FREQUENCY 1193182
#define TICKS_PER_SECOND 1000
#define MS_PER_S 1'000
#define NS_PER_S 1'000'000'000
namespace Kernel
{
@@ -34,7 +37,7 @@ namespace Kernel
BAN::ErrorOr<BAN::UniqPtr<PIT>> PIT::create()
{
PIT* pit = new PIT;
PIT* pit = new PIT();
if (pit == nullptr)
return BAN::Error::from_errno(ENOMEM);
pit->initialize();
@@ -57,7 +60,16 @@ namespace Kernel
uint64_t PIT::ms_since_boot() const
{
return s_system_time;
return s_system_time * (MS_PER_S / TICKS_PER_SECOND);
}
timespec PIT::time_since_boot() const
{
uint64_t ticks = s_system_time;
return timespec {
.tv_sec = ticks / TICKS_PER_SECOND,
.tv_nsec = (long)((ticks % TICKS_PER_SECOND) * (NS_PER_S / TICKS_PER_SECOND))
};
}
}

View File

@@ -59,6 +59,11 @@ namespace Kernel
return m_timer->ms_since_boot();
}
timespec TimerHandler::time_since_boot() const
{
return m_timer->time_since_boot();
}
void TimerHandler::sleep(uint64_t ms) const
{
if (ms == 0)