Kernel/LibC: Use builtin functions over inline asm

Getting flags and saving/restoring sse state and reading TSC can be done
using compiler builtins
This commit is contained in:
Bananymous 2026-01-09 15:19:19 +02:00
parent a9ceab0415
commit 74f70ae4bd
5 changed files with 14 additions and 39 deletions

View File

@ -82,8 +82,11 @@ namespace Kernel
static InterruptState get_interrupt_state() static InterruptState get_interrupt_state()
{ {
uintptr_t flags; #if ARCH(x86_64)
asm volatile("pushf; pop %0" : "=rm"(flags)); const auto flags = __builtin_ia32_readeflags_u64();
#elif ARCH(i686)
const auto flags = __builtin_ia32_readeflags_u32();
#endif
if (flags & (1 << 9)) if (flags & (1 << 9))
return InterruptState::Enabled; return InterruptState::Enabled;
return InterruptState::Disabled; return InterruptState::Disabled;

View File

@ -304,29 +304,15 @@ namespace Kernel
void Processor::update_tsc() void Processor::update_tsc()
{ {
const auto read_tsc =
[]() -> uint64_t {
uint32_t high, low;
asm volatile("lfence; rdtsc" : "=d"(high), "=a"(low));
return (static_cast<uint64_t>(high) << 32) | low;
};
auto& sgettime = shared_page().cpus[current_index()].gettime_local; auto& sgettime = shared_page().cpus[current_index()].gettime_local;
sgettime.seq = sgettime.seq + 1; sgettime.seq = sgettime.seq + 1;
sgettime.last_ns = SystemTimer::get().ns_since_boot_no_tsc(); sgettime.last_ns = SystemTimer::get().ns_since_boot_no_tsc();
sgettime.last_tsc = read_tsc(); sgettime.last_tsc = __builtin_ia32_rdtsc();
sgettime.seq = sgettime.seq + 1; sgettime.seq = sgettime.seq + 1;
} }
uint64_t Processor::ns_since_boot_tsc() uint64_t Processor::ns_since_boot_tsc()
{ {
const auto read_tsc =
[]() -> uint64_t {
uint32_t high, low;
asm volatile("lfence; rdtsc" : "=d"(high), "=a"(low));
return (static_cast<uint64_t>(high) << 32) | low;
};
const auto& shared_page = Processor::shared_page(); const auto& shared_page = Processor::shared_page();
const auto& sgettime = shared_page.gettime_shared; const auto& sgettime = shared_page.gettime_shared;
const auto& lgettime = shared_page.cpus[current_index()].gettime_local; const auto& lgettime = shared_page.cpus[current_index()].gettime_local;
@ -334,7 +320,7 @@ namespace Kernel
auto state = get_interrupt_state(); auto state = get_interrupt_state();
set_interrupt_state(InterruptState::Disabled); set_interrupt_state(InterruptState::Disabled);
const auto current_ns = lgettime.last_ns + (((read_tsc() - lgettime.last_tsc) * sgettime.mult) >> sgettime.shift); const auto current_ns = lgettime.last_ns + (((__builtin_ia32_rdtsc() - lgettime.last_tsc) * sgettime.mult) >> sgettime.shift);
set_interrupt_state(state); set_interrupt_state(state);

View File

@ -908,9 +908,9 @@ namespace Kernel
void Thread::save_sse() void Thread::save_sse()
{ {
#if ARCH(x86_64) #if ARCH(x86_64)
asm volatile("fxsave64 %0" :: "m"(m_sse_storage)); __builtin_ia32_fxsave64(m_sse_storage);
#elif ARCH(i686) #elif ARCH(i686)
asm volatile("fxsave %0" :: "m"(m_sse_storage)); __builtin_ia32_fxsave(m_sse_storage);
#else #else
#error #error
#endif #endif
@ -919,9 +919,9 @@ namespace Kernel
void Thread::load_sse() void Thread::load_sse()
{ {
#if ARCH(x86_64) #if ARCH(x86_64)
asm volatile("fxrstor64 %0" :: "m"(m_sse_storage)); __builtin_ia32_fxrstor64(m_sse_storage);
#elif ARCH(i686) #elif ARCH(i686)
asm volatile("fxrstor %0" :: "m"(m_sse_storage)); __builtin_ia32_fxrstor(m_sse_storage);
#else #else
#error #error
#endif #endif

View File

@ -80,13 +80,6 @@ namespace Kernel
{ {
// take 5x 50 ms samples and use the median value // take 5x 50 ms samples and use the median value
const auto read_tsc =
[]() -> uint64_t {
uint32_t high, low;
asm volatile("lfence; rdtsc" : "=d"(high), "=a"(low));
return (static_cast<uint64_t>(high) << 32) | low;
};
constexpr size_t tsc_sample_count = 5; constexpr size_t tsc_sample_count = 5;
constexpr size_t tsc_sample_ns = 50'000'000; constexpr size_t tsc_sample_ns = 50'000'000;
@ -95,10 +88,10 @@ namespace Kernel
{ {
const auto start_ns = m_timer->ns_since_boot(); const auto start_ns = m_timer->ns_since_boot();
const auto start_tsc = read_tsc(); const auto start_tsc = __builtin_ia32_rdtsc();
while (m_timer->ns_since_boot() < start_ns + tsc_sample_ns) while (m_timer->ns_since_boot() < start_ns + tsc_sample_ns)
Processor::pause(); Processor::pause();
const auto stop_tsc = read_tsc(); const auto stop_tsc = __builtin_ia32_rdtsc();
const auto stop_ns = m_timer->ns_since_boot(); const auto stop_ns = m_timer->ns_since_boot();

View File

@ -38,13 +38,6 @@ int clock_gettime(clockid_t clock_id, struct timespec* tp)
return cpu; return cpu;
}; };
const auto read_tsc =
[]() -> uint64_t {
uint32_t high, low;
asm volatile("lfence; rdtsc" : "=d"(high), "=a"(low));
return (static_cast<uint64_t>(high) << 32) | low;
};
for (;;) for (;;)
{ {
const auto cpu = get_cpu(); const auto cpu = get_cpu();
@ -56,7 +49,7 @@ int clock_gettime(clockid_t clock_id, struct timespec* tp)
if (old_seq & 1) if (old_seq & 1)
continue; continue;
const auto monotonic_ns = lgettime.last_ns + (((read_tsc() - lgettime.last_tsc) * sgettime.mult) >> sgettime.shift); const auto monotonic_ns = lgettime.last_ns + (((__builtin_ia32_rdtsc() - lgettime.last_tsc) * sgettime.mult) >> sgettime.shift);
if (old_seq != lgettime.seq || cpu != get_cpu()) if (old_seq != lgettime.seq || cpu != get_cpu())
continue; continue;