diff --git a/kernel/include/kernel/Processor.h b/kernel/include/kernel/Processor.h index 6dd92a9f..66b85c26 100644 --- a/kernel/include/kernel/Processor.h +++ b/kernel/include/kernel/Processor.h @@ -82,8 +82,11 @@ namespace Kernel static InterruptState get_interrupt_state() { - uintptr_t flags; - asm volatile("pushf; pop %0" : "=rm"(flags)); +#if ARCH(x86_64) + const auto flags = __builtin_ia32_readeflags_u64(); +#elif ARCH(i686) + const auto flags = __builtin_ia32_readeflags_u32(); +#endif if (flags & (1 << 9)) return InterruptState::Enabled; return InterruptState::Disabled; diff --git a/kernel/kernel/Processor.cpp b/kernel/kernel/Processor.cpp index a302f7ef..cecea28d 100644 --- a/kernel/kernel/Processor.cpp +++ b/kernel/kernel/Processor.cpp @@ -304,29 +304,15 @@ namespace Kernel 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(high) << 32) | low; - }; - auto& sgettime = shared_page().cpus[current_index()].gettime_local; sgettime.seq = sgettime.seq + 1; 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; } 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(high) << 32) | low; - }; - const auto& shared_page = Processor::shared_page(); const auto& sgettime = shared_page.gettime_shared; const auto& lgettime = shared_page.cpus[current_index()].gettime_local; @@ -334,7 +320,7 @@ namespace Kernel auto state = get_interrupt_state(); 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); diff --git a/kernel/kernel/Thread.cpp b/kernel/kernel/Thread.cpp index 50031a21..475629a6 100644 --- a/kernel/kernel/Thread.cpp +++ b/kernel/kernel/Thread.cpp @@ -908,9 +908,9 @@ namespace Kernel void Thread::save_sse() { #if ARCH(x86_64) - asm volatile("fxsave64 %0" :: "m"(m_sse_storage)); + __builtin_ia32_fxsave64(m_sse_storage); #elif ARCH(i686) - asm volatile("fxsave %0" :: "m"(m_sse_storage)); + __builtin_ia32_fxsave(m_sse_storage); #else #error #endif @@ -919,9 +919,9 @@ namespace Kernel void Thread::load_sse() { #if ARCH(x86_64) - asm volatile("fxrstor64 %0" :: "m"(m_sse_storage)); + __builtin_ia32_fxrstor64(m_sse_storage); #elif ARCH(i686) - asm volatile("fxrstor %0" :: "m"(m_sse_storage)); + __builtin_ia32_fxrstor(m_sse_storage); #else #error #endif diff --git a/kernel/kernel/Timer/Timer.cpp b/kernel/kernel/Timer/Timer.cpp index 026017a8..891b6a64 100644 --- a/kernel/kernel/Timer/Timer.cpp +++ b/kernel/kernel/Timer/Timer.cpp @@ -80,13 +80,6 @@ namespace Kernel { // 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(high) << 32) | low; - }; - constexpr size_t tsc_sample_count = 5; 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_tsc = read_tsc(); + const auto start_tsc = __builtin_ia32_rdtsc(); while (m_timer->ns_since_boot() < start_ns + tsc_sample_ns) Processor::pause(); - const auto stop_tsc = read_tsc(); + const auto stop_tsc = __builtin_ia32_rdtsc(); const auto stop_ns = m_timer->ns_since_boot(); diff --git a/userspace/libraries/LibC/time.cpp b/userspace/libraries/LibC/time.cpp index 4b84b699..78dbd2e1 100644 --- a/userspace/libraries/LibC/time.cpp +++ b/userspace/libraries/LibC/time.cpp @@ -38,13 +38,6 @@ int clock_gettime(clockid_t clock_id, struct timespec* tp) return cpu; }; - const auto read_tsc = - []() -> uint64_t { - uint32_t high, low; - asm volatile("lfence; rdtsc" : "=d"(high), "=a"(low)); - return (static_cast(high) << 32) | low; - }; - for (;;) { const auto cpu = get_cpu(); @@ -56,7 +49,7 @@ int clock_gettime(clockid_t clock_id, struct timespec* tp) if (old_seq & 1) 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()) continue;