Kernel: Add syscall-less clock_gettime

If the processor has invariant TSC it can be used to measure time. We
keep track of the last nanosecond and TSC values and offset them based
on the current TSC. This allows getting current time in userspace.

The implementation maps a single RO page to every processes' address
space. The page contains the TSC info which gets updated every 100 ms.
If the processor does not have invariant TSC, this page will not
indicate the capability for TSC based timing.

There was the problem about how does a processor know which cpu it is
running without doing syscall. TSC counters may or may not be
synchronized between cores, so we need a separate TSC info for each
processor. I ended up adding sequence of bytes 0..255 at the start of
the shared page. When a scheduler gets a new thread, it updates the
threads gs/fs segment to point to the byte corresponding to the current
cpu.

This TSC based timing is also used in kernel. With 64 bit HPET this
probably does not bring much of a benefit, but on PIT or 32 bit HPET
this removes the need to aquire a spinlock to get the current time.

This change does force the userspace to not use gs/fs themselves and
they are both now reserved. Other one is used for TLS (this can be
technically used if user does not call libc code) and the other for
the current processor index (cannot be used as kernel unconditionally
resets it after each load balance).

I was looking at how many times timer's current time was polled
(userspace and kernel combined). When idling in window manager, it was
around 8k times/s. When running doom it peaked at over 1 million times
per second when loading and settled at ~30k times/s.
This commit is contained in:
2026-01-08 13:30:04 +02:00
parent ee57cf3e9a
commit 9eb3834ae5
20 changed files with 448 additions and 15 deletions

View File

@@ -0,0 +1,37 @@
#pragma once
#include <stdint.h>
namespace Kernel::API
{
enum SharedPageFeature : uint32_t
{
SPF_GETTIME = 1 << 0,
};
struct SharedPage
{
uint8_t __sequence[0x100];
uint32_t features;
struct
{
uint8_t shift;
uint64_t mult;
uint64_t realtime_seconds;
} gettime_shared;
struct
{
struct
{
uint32_t seq;
uint64_t last_ns;
uint64_t last_tsc;
} gettime_local;
} cpus[];
};
}

View File

@@ -81,5 +81,6 @@ namespace CPUID
bool has_pge();
bool has_pat();
bool has_1gib_pages();
bool has_invariant_tsc();
}

View File

@@ -228,6 +228,8 @@ namespace Kernel
static Process& current() { return Thread::current().process(); }
vaddr_t shared_page_vaddr() const { return m_shared_page_vaddr; }
PageTable& page_table() { return m_page_table ? *m_page_table : PageTable::kernel(); }
size_t proc_meminfo(off_t offset, BAN::ByteSpan) const;
@@ -342,6 +344,8 @@ namespace Kernel
VirtualFileSystem::File m_working_directory;
VirtualFileSystem::File m_root_file;
vaddr_t m_shared_page_vaddr { 0 };
BAN::Vector<Thread*> m_threads;
struct pthread_info_t

View File

@@ -3,10 +3,12 @@
#include <BAN/Atomic.h>
#include <BAN/ForwardList.h>
#include <kernel/API/SharedPage.h>
#include <kernel/Arch.h>
#include <kernel/GDT.h>
#include <kernel/IDT.h>
#include <kernel/InterruptStack.h>
#include <kernel/Memory/Types.h>
#include <kernel/ProcessorID.h>
#include <kernel/Scheduler.h>
@@ -33,6 +35,7 @@ namespace Kernel
FlushTLB,
NewThread,
UnblockThread,
UpdateTSC,
StackTrace,
};
SMPMessage* next { nullptr };
@@ -55,6 +58,7 @@ namespace Kernel
static Processor& initialize();
static ProcessorID current_id() { return read_gs_sized<ProcessorID>(offsetof(Processor, m_id)); }
static uint8_t current_index() { return read_gs_sized<uint8_t>(offsetof(Processor, m_index)); }
static ProcessorID id_from_index(size_t index);
static uint8_t count() { return s_processor_count; }
@@ -107,6 +111,13 @@ namespace Kernel
static void yield();
static Scheduler& scheduler() { return *read_gs_sized<Scheduler*>(offsetof(Processor, m_scheduler)); }
static void initialize_tsc(uint8_t shift, uint64_t mult, uint64_t realtime_seconds);
static void update_tsc();
static uint64_t ns_since_boot_tsc();
static paddr_t shared_page_paddr() { return s_shared_page_paddr; }
static volatile API::SharedPage& shared_page() { return *reinterpret_cast<API::SharedPage*>(s_shared_page_vaddr); }
static void handle_ipi();
static void handle_smp_messages();
@@ -124,6 +135,7 @@ namespace Kernel
static ProcessorID read_processor_id();
static void initialize_smp();
static void initialize_shared_page();
template<typename T>
static T read_gs_sized(uintptr_t offset) requires(sizeof(T) <= 8)
@@ -162,8 +174,11 @@ namespace Kernel
static BAN::Atomic<uint8_t> s_processor_count;
static BAN::Atomic<bool> s_is_smp_enabled;
static BAN::Atomic<bool> s_should_print_cpu_load;
static paddr_t s_shared_page_paddr;
static vaddr_t s_shared_page_vaddr;
ProcessorID m_id { 0 };
uint8_t m_index { 0xFF };
static constexpr size_t s_stack_size { 4096 };
void* m_stack { nullptr };

View File

@@ -122,6 +122,8 @@ namespace Kernel
void set_cpu_time_start();
void set_cpu_time_stop();
void update_processor_index_address();
void set_fsbase(vaddr_t base) { m_fsbase = base; }
vaddr_t get_fsbase() const { return m_fsbase; }
void set_gsbase(vaddr_t base) { m_gsbase = base; }

View File

@@ -35,6 +35,8 @@ namespace Kernel
static SystemTimer& get();
static bool is_initialized();
void initialize_tsc();
virtual uint64_t ms_since_boot() const override;
virtual uint64_t ns_since_boot() const override;
virtual timespec time_since_boot() const override;
@@ -47,6 +49,9 @@ namespace Kernel
void dont_invoke_scheduler() { m_timer->m_should_invoke_scheduler = false; }
void update_tsc() const;
uint64_t ns_since_boot_no_tsc() const;
timespec real_time() const;
private:
@@ -54,10 +59,14 @@ namespace Kernel
void initialize_timers(bool force_pic);
uint64_t get_tsc_frequency() const;
private:
uint64_t m_boot_time { 0 };
BAN::UniqPtr<RTC> m_rtc;
BAN::UniqPtr<Timer> m_timer;
bool m_has_invariant_tsc { false };
mutable uint32_t m_timer_ticks { 0 };
};
}