Serenity Operating System
1/*
2 * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
3 * Copyright (c) 2022, Timon Kruiper <timonkruiper@gmail.com>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <AK/Singleton.h>
9#include <AK/StdLibExtras.h>
10#include <AK/Time.h>
11#if ARCH(X86_64)
12# include <Kernel/Arch/x86_64/Interrupts/APIC.h>
13# include <Kernel/Arch/x86_64/RTC.h>
14# include <Kernel/Arch/x86_64/Time/APICTimer.h>
15# include <Kernel/Arch/x86_64/Time/HPET.h>
16# include <Kernel/Arch/x86_64/Time/HPETComparator.h>
17# include <Kernel/Arch/x86_64/Time/PIT.h>
18# include <Kernel/Arch/x86_64/Time/RTC.h>
19#elif ARCH(AARCH64)
20# include <Kernel/Arch/aarch64/RPi/Timer.h>
21#else
22# error Unknown architecture
23#endif
24
25#include <Kernel/Arch/CurrentTime.h>
26#include <Kernel/CommandLine.h>
27#include <Kernel/Firmware/ACPI/Parser.h>
28#include <Kernel/InterruptDisabler.h>
29#include <Kernel/PerformanceManager.h>
30#include <Kernel/Scheduler.h>
31#include <Kernel/Sections.h>
32#include <Kernel/Time/HardwareTimer.h>
33#include <Kernel/Time/TimeManagement.h>
34#include <Kernel/TimerQueue.h>
35
36namespace Kernel {
37
38static Singleton<TimeManagement> s_the;
39
40bool TimeManagement::is_initialized()
41{
42 return s_the.is_initialized();
43}
44
45TimeManagement& TimeManagement::the()
46{
47 return *s_the;
48}
49
50// The s_scheduler_specific_current_time function provides a current time for scheduling purposes,
51// which may not necessarily relate to wall time
52static u64 (*s_scheduler_current_time)();
53
54static u64 current_time_monotonic()
55{
56 // We always need a precise timestamp here, we cannot rely on a coarse timestamp
57 return (u64)TimeManagement::the().monotonic_time(TimePrecision::Precise).to_nanoseconds();
58}
59
60u64 TimeManagement::scheduler_current_time()
61{
62 VERIFY(s_scheduler_current_time);
63 return s_scheduler_current_time();
64}
65
66ErrorOr<void> TimeManagement::validate_clock_id(clockid_t clock_id)
67{
68 switch (clock_id) {
69 case CLOCK_MONOTONIC:
70 case CLOCK_MONOTONIC_COARSE:
71 case CLOCK_MONOTONIC_RAW:
72 case CLOCK_REALTIME:
73 case CLOCK_REALTIME_COARSE:
74 return {};
75 default:
76 return EINVAL;
77 };
78}
79
80Time TimeManagement::current_time(clockid_t clock_id) const
81{
82 switch (clock_id) {
83 case CLOCK_MONOTONIC:
84 return monotonic_time(TimePrecision::Precise);
85 case CLOCK_MONOTONIC_COARSE:
86 return monotonic_time(TimePrecision::Coarse);
87 case CLOCK_MONOTONIC_RAW:
88 return monotonic_time_raw();
89 case CLOCK_REALTIME:
90 return epoch_time(TimePrecision::Precise);
91 case CLOCK_REALTIME_COARSE:
92 return epoch_time(TimePrecision::Coarse);
93 default:
94 // Syscall entrypoint is missing a is_valid_clock_id(..) check?
95 VERIFY_NOT_REACHED();
96 }
97}
98
99bool TimeManagement::is_system_timer(HardwareTimerBase const& timer) const
100{
101 return &timer == m_system_timer.ptr();
102}
103
104void TimeManagement::set_epoch_time(Time ts)
105{
106 InterruptDisabler disabler;
107 // FIXME: Should use AK::Time internally
108 m_epoch_time = ts.to_timespec();
109 m_remaining_epoch_time_adjustment = { 0, 0 };
110}
111
112Time TimeManagement::monotonic_time(TimePrecision precision) const
113{
114 // This is the time when last updated by an interrupt.
115 u64 seconds;
116 u32 ticks;
117
118 bool do_query = precision == TimePrecision::Precise && m_can_query_precise_time;
119
120 u32 update_iteration;
121 do {
122 update_iteration = m_update1.load(AK::MemoryOrder::memory_order_acquire);
123 seconds = m_seconds_since_boot;
124 ticks = m_ticks_this_second;
125
126 if (do_query) {
127#if ARCH(X86_64)
128 // We may have to do this over again if the timer interrupt fires
129 // while we're trying to query the information. In that case, our
130 // seconds and ticks became invalid, producing an incorrect time.
131 // Be sure to not modify m_seconds_since_boot and m_ticks_this_second
132 // because this may only be modified by the interrupt handler
133 HPET::the().update_time(seconds, ticks, true);
134#elif ARCH(AARCH64)
135 // FIXME: Get rid of these horrible casts
136 const_cast<RPi::Timer*>(static_cast<RPi::Timer const*>(m_system_timer.ptr()))->update_time(seconds, ticks, true);
137#else
138# error Unknown architecture
139#endif
140 }
141 } while (update_iteration != m_update2.load(AK::MemoryOrder::memory_order_acquire));
142
143 VERIFY(m_time_ticks_per_second > 0);
144 VERIFY(ticks < m_time_ticks_per_second);
145 u64 ns = ((u64)ticks * 1000000000ull) / m_time_ticks_per_second;
146 VERIFY(ns < 1000000000ull);
147 return Time::from_timespec({ (i64)seconds, (i32)ns });
148}
149
150Time TimeManagement::epoch_time(TimePrecision) const
151{
152 // TODO: Take into account precision
153 timespec ts;
154 u32 update_iteration;
155 do {
156 update_iteration = m_update1.load(AK::MemoryOrder::memory_order_acquire);
157 ts = m_epoch_time;
158 } while (update_iteration != m_update2.load(AK::MemoryOrder::memory_order_acquire));
159 return Time::from_timespec(ts);
160}
161
162u64 TimeManagement::uptime_ms() const
163{
164 auto mtime = monotonic_time().to_timespec();
165 // This overflows after 292 million years of uptime.
166 // Since this is only used for performance timestamps and sys$times, that's probably enough.
167 u64 ms = mtime.tv_sec * 1000ull;
168 ms += mtime.tv_nsec / 1000000;
169 return ms;
170}
171
172UNMAP_AFTER_INIT void TimeManagement::initialize([[maybe_unused]] u32 cpu)
173{
174 // Note: We must disable interrupts, because the timers interrupt might fire before
175 // the TimeManagement class is completely initialized.
176 InterruptDisabler disabler;
177
178#if ARCH(X86_64)
179 if (cpu == 0) {
180 VERIFY(!s_the.is_initialized());
181 s_the.ensure_instance();
182
183 if (APIC::initialized()) {
184 // Initialize the APIC timers after the other timers as the
185 // initialization needs to briefly enable interrupts, which then
186 // would trigger a deadlock trying to get the s_the instance while
187 // creating it.
188 if (auto* apic_timer = APIC::the().initialize_timers(*s_the->m_system_timer)) {
189 dmesgln("Time: Using APIC timer as system timer");
190 s_the->set_system_timer(*apic_timer);
191 }
192 }
193 } else {
194 VERIFY(s_the.is_initialized());
195 if (auto* apic_timer = APIC::the().get_timer()) {
196 dmesgln("Time: Enable APIC timer on CPU #{}", cpu);
197 apic_timer->enable_local_timer();
198 }
199 }
200#elif ARCH(AARCH64)
201 if (cpu == 0) {
202 VERIFY(!s_the.is_initialized());
203 s_the.ensure_instance();
204 }
205#else
206# error Unknown architecture
207#endif
208 auto* possible_arch_specific_current_time_function = optional_current_time();
209 if (possible_arch_specific_current_time_function)
210 s_scheduler_current_time = possible_arch_specific_current_time_function;
211 else
212 s_scheduler_current_time = current_time_monotonic;
213}
214
215void TimeManagement::set_system_timer(HardwareTimerBase& timer)
216{
217 VERIFY(Processor::is_bootstrap_processor()); // This should only be called on the BSP!
218 auto original_callback = m_system_timer->set_callback(nullptr);
219 m_system_timer->disable();
220 timer.set_callback(move(original_callback));
221 m_system_timer = timer;
222}
223
224time_t TimeManagement::ticks_per_second() const
225{
226 return m_time_keeper_timer->ticks_per_second();
227}
228
229Time TimeManagement::boot_time()
230{
231#if ARCH(X86_64)
232 return RTC::boot_time();
233#elif ARCH(AARCH64)
234 TODO_AARCH64();
235#else
236# error Unknown architecture
237#endif
238}
239
240Time TimeManagement::clock_resolution() const
241{
242 long nanoseconds_per_tick = 1'000'000'000 / m_time_keeper_timer->ticks_per_second();
243 return Time::from_nanoseconds(nanoseconds_per_tick);
244}
245
246UNMAP_AFTER_INIT TimeManagement::TimeManagement()
247 : m_time_page_region(MM.allocate_kernel_region(PAGE_SIZE, "Time page"sv, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow).release_value_but_fixme_should_propagate_errors())
248{
249#if ARCH(X86_64)
250 bool probe_non_legacy_hardware_timers = !(kernel_command_line().is_legacy_time_enabled());
251 if (ACPI::is_enabled()) {
252 if (!ACPI::Parser::the()->x86_specific_flags().cmos_rtc_not_present) {
253 RTC::initialize();
254 m_epoch_time.tv_sec += boot_time().to_timespec().tv_sec;
255 } else {
256 dmesgln("ACPI: RTC CMOS Not present");
257 }
258 } else {
259 // We just assume that we can access RTC CMOS, if ACPI isn't usable.
260 RTC::initialize();
261 m_epoch_time.tv_sec += boot_time().to_timespec().tv_sec;
262 }
263 if (probe_non_legacy_hardware_timers) {
264 if (!probe_and_set_x86_non_legacy_hardware_timers())
265 if (!probe_and_set_x86_legacy_hardware_timers())
266 VERIFY_NOT_REACHED();
267 } else if (!probe_and_set_x86_legacy_hardware_timers()) {
268 VERIFY_NOT_REACHED();
269 }
270#elif ARCH(AARCH64)
271 probe_and_set_aarch64_hardware_timers();
272#else
273# error Unknown architecture
274#endif
275}
276
277Time TimeManagement::now()
278{
279 return s_the.ptr()->epoch_time();
280}
281
282UNMAP_AFTER_INIT Vector<HardwareTimerBase*> TimeManagement::scan_and_initialize_periodic_timers()
283{
284 bool should_enable = is_hpet_periodic_mode_allowed();
285 dbgln("Time: Scanning for periodic timers");
286 Vector<HardwareTimerBase*> timers;
287 for (auto& hardware_timer : m_hardware_timers) {
288 if (hardware_timer->is_periodic_capable()) {
289 timers.append(hardware_timer);
290 if (should_enable)
291 hardware_timer->set_periodic();
292 }
293 }
294 return timers;
295}
296
297UNMAP_AFTER_INIT Vector<HardwareTimerBase*> TimeManagement::scan_for_non_periodic_timers()
298{
299 dbgln("Time: Scanning for non-periodic timers");
300 Vector<HardwareTimerBase*> timers;
301 for (auto& hardware_timer : m_hardware_timers) {
302 if (!hardware_timer->is_periodic_capable())
303 timers.append(hardware_timer);
304 }
305 return timers;
306}
307
308bool TimeManagement::is_hpet_periodic_mode_allowed()
309{
310 switch (kernel_command_line().hpet_mode()) {
311 case HPETMode::Periodic:
312 return true;
313 case HPETMode::NonPeriodic:
314 return false;
315 default:
316 VERIFY_NOT_REACHED();
317 }
318}
319
320#if ARCH(X86_64)
321UNMAP_AFTER_INIT bool TimeManagement::probe_and_set_x86_non_legacy_hardware_timers()
322{
323 if (!ACPI::is_enabled())
324 return false;
325 if (!HPET::test_and_initialize())
326 return false;
327 if (!HPET::the().comparators().size()) {
328 dbgln("HPET initialization aborted.");
329 return false;
330 }
331 dbgln("HPET: Setting appropriate functions to timers.");
332
333 for (auto& hpet_comparator : HPET::the().comparators())
334 m_hardware_timers.append(hpet_comparator);
335
336 auto periodic_timers = scan_and_initialize_periodic_timers();
337 auto non_periodic_timers = scan_for_non_periodic_timers();
338
339 if (is_hpet_periodic_mode_allowed())
340 VERIFY(!periodic_timers.is_empty());
341
342 VERIFY(periodic_timers.size() + non_periodic_timers.size() > 0);
343
344 size_t taken_periodic_timers_count = 0;
345 size_t taken_non_periodic_timers_count = 0;
346
347 if (periodic_timers.size() > taken_periodic_timers_count) {
348 m_system_timer = periodic_timers[taken_periodic_timers_count];
349 taken_periodic_timers_count += 1;
350 } else if (non_periodic_timers.size() > taken_non_periodic_timers_count) {
351 m_system_timer = non_periodic_timers[taken_non_periodic_timers_count];
352 taken_non_periodic_timers_count += 1;
353 }
354
355 m_system_timer->set_callback([this](RegisterState const& regs) {
356 // Update the time. We don't really care too much about the
357 // frequency of the interrupt because we'll query the main
358 // counter to get an accurate time.
359 if (Processor::is_bootstrap_processor()) {
360 // TODO: Have the other CPUs call system_timer_tick directly
361 increment_time_since_boot_hpet();
362 }
363
364 system_timer_tick(regs);
365 });
366
367 // Use the HPET main counter frequency for time purposes. This is likely
368 // a much higher frequency than the interrupt itself and allows us to
369 // keep a more accurate time
370 m_can_query_precise_time = true;
371 m_time_ticks_per_second = HPET::the().frequency();
372
373 m_system_timer->try_to_set_frequency(m_system_timer->calculate_nearest_possible_frequency(OPTIMAL_TICKS_PER_SECOND_RATE));
374
375 // We don't need an interrupt for time keeping purposes because we
376 // can query the timer.
377 m_time_keeper_timer = m_system_timer;
378
379 if (periodic_timers.size() > taken_periodic_timers_count) {
380 m_profile_timer = periodic_timers[taken_periodic_timers_count];
381 taken_periodic_timers_count += 1;
382 } else if (non_periodic_timers.size() > taken_non_periodic_timers_count) {
383 m_profile_timer = non_periodic_timers[taken_non_periodic_timers_count];
384 taken_non_periodic_timers_count += 1;
385 }
386
387 if (m_profile_timer) {
388 m_profile_timer->set_callback(PerformanceManager::timer_tick);
389 m_profile_timer->try_to_set_frequency(m_profile_timer->calculate_nearest_possible_frequency(1));
390 }
391
392 return true;
393}
394
395UNMAP_AFTER_INIT bool TimeManagement::probe_and_set_x86_legacy_hardware_timers()
396{
397 if (ACPI::is_enabled()) {
398 if (ACPI::Parser::the()->x86_specific_flags().cmos_rtc_not_present) {
399 dbgln("ACPI: CMOS RTC Not Present");
400 return false;
401 } else {
402 dbgln("ACPI: CMOS RTC Present");
403 }
404 }
405
406 m_hardware_timers.append(PIT::initialize(TimeManagement::update_time));
407 m_hardware_timers.append(RealTimeClock::create(TimeManagement::system_timer_tick));
408 m_time_keeper_timer = m_hardware_timers[0];
409 m_system_timer = m_hardware_timers[1];
410
411 // The timer is only as accurate as the interrupts...
412 m_time_ticks_per_second = m_time_keeper_timer->ticks_per_second();
413 return true;
414}
415
416void TimeManagement::update_time(RegisterState const&)
417{
418 TimeManagement::the().increment_time_since_boot();
419}
420
421void TimeManagement::increment_time_since_boot_hpet()
422{
423 VERIFY(!m_time_keeper_timer.is_null());
424 VERIFY(m_time_keeper_timer->timer_type() == HardwareTimerType::HighPrecisionEventTimer);
425
426 // NOTE: m_seconds_since_boot and m_ticks_this_second are only ever
427 // updated here! So we can safely read that information, query the clock,
428 // and when we're all done we can update the information. This reduces
429 // contention when other processors attempt to read the clock.
430 auto seconds_since_boot = m_seconds_since_boot;
431 auto ticks_this_second = m_ticks_this_second;
432 auto delta_ns = HPET::the().update_time(seconds_since_boot, ticks_this_second, false);
433
434 // Now that we have a precise time, go update it as quickly as we can
435 u32 update_iteration = m_update2.fetch_add(1, AK::MemoryOrder::memory_order_acquire);
436 m_seconds_since_boot = seconds_since_boot;
437 m_ticks_this_second = ticks_this_second;
438 // TODO: Apply m_remaining_epoch_time_adjustment
439 timespec_add(m_epoch_time, { (time_t)(delta_ns / 1000000000), (long)(delta_ns % 1000000000) }, m_epoch_time);
440
441 m_update1.store(update_iteration + 1, AK::MemoryOrder::memory_order_release);
442
443 update_time_page();
444}
445#elif ARCH(AARCH64)
446UNMAP_AFTER_INIT bool TimeManagement::probe_and_set_aarch64_hardware_timers()
447{
448 m_hardware_timers.append(RPi::Timer::initialize());
449 m_system_timer = m_hardware_timers[0];
450 m_time_ticks_per_second = m_system_timer->frequency();
451
452 m_system_timer->set_callback([this](RegisterState const& regs) {
453 auto seconds_since_boot = m_seconds_since_boot;
454 auto ticks_this_second = m_ticks_this_second;
455 auto delta_ns = static_cast<RPi::Timer*>(m_system_timer.ptr())->update_time(seconds_since_boot, ticks_this_second, false);
456
457 u32 update_iteration = m_update2.fetch_add(1, AK::MemoryOrder::memory_order_acquire);
458 m_seconds_since_boot = seconds_since_boot;
459 m_ticks_this_second = ticks_this_second;
460
461 timespec_add(m_epoch_time, { (time_t)(delta_ns / 1000000000), (long)(delta_ns % 1000000000) }, m_epoch_time);
462
463 m_update1.store(update_iteration + 1, AK::MemoryOrder::memory_order_release);
464
465 update_time_page();
466
467 system_timer_tick(regs);
468 });
469
470 m_time_keeper_timer = m_system_timer;
471
472 return true;
473}
474#else
475# error Unknown architecture
476#endif
477
478void TimeManagement::increment_time_since_boot()
479{
480 VERIFY(!m_time_keeper_timer.is_null());
481
482 // Compute time adjustment for adjtime. Let the clock run up to 1% fast or slow.
483 // That way, adjtime can adjust up to 36 seconds per hour, without time getting very jumpy.
484 // Once we have a smarter NTP service that also adjusts the frequency instead of just slewing time, maybe we can lower this.
485 long NanosPerTick = 1'000'000'000 / m_time_keeper_timer->frequency();
486 time_t MaxSlewNanos = NanosPerTick / 100;
487
488 u32 update_iteration = m_update2.fetch_add(1, AK::MemoryOrder::memory_order_acquire);
489
490 // Clamp twice, to make sure intermediate fits into a long.
491 long slew_nanos = clamp(clamp(m_remaining_epoch_time_adjustment.tv_sec, (time_t)-1, (time_t)1) * 1'000'000'000 + m_remaining_epoch_time_adjustment.tv_nsec, -MaxSlewNanos, MaxSlewNanos);
492 timespec slew_nanos_ts;
493 timespec_sub({ 0, slew_nanos }, { 0, 0 }, slew_nanos_ts); // Normalize tv_nsec to be positive.
494 timespec_sub(m_remaining_epoch_time_adjustment, slew_nanos_ts, m_remaining_epoch_time_adjustment);
495
496 timespec epoch_tick = { .tv_sec = 0, .tv_nsec = NanosPerTick };
497 epoch_tick.tv_nsec += slew_nanos; // No need for timespec_add(), guaranteed to be in range.
498 timespec_add(m_epoch_time, epoch_tick, m_epoch_time);
499
500 if (++m_ticks_this_second >= m_time_keeper_timer->ticks_per_second()) {
501 // FIXME: Synchronize with other clock somehow to prevent drifting apart.
502 ++m_seconds_since_boot;
503 m_ticks_this_second = 0;
504 }
505
506 m_update1.store(update_iteration + 1, AK::MemoryOrder::memory_order_release);
507
508 update_time_page();
509}
510
511void TimeManagement::system_timer_tick(RegisterState const& regs)
512{
513 if (Processor::current_in_irq() <= 1) {
514 // Don't expire timers while handling IRQs
515 TimerQueue::the().fire();
516 }
517 Scheduler::timer_tick(regs);
518}
519
520bool TimeManagement::enable_profile_timer()
521{
522 if (!m_profile_timer)
523 return false;
524 if (m_profile_enable_count.fetch_add(1) == 0)
525 return m_profile_timer->try_to_set_frequency(m_profile_timer->calculate_nearest_possible_frequency(OPTIMAL_PROFILE_TICKS_PER_SECOND_RATE));
526 return true;
527}
528
529bool TimeManagement::disable_profile_timer()
530{
531 if (!m_profile_timer)
532 return false;
533 if (m_profile_enable_count.fetch_sub(1) == 1)
534 return m_profile_timer->try_to_set_frequency(m_profile_timer->calculate_nearest_possible_frequency(1));
535 return true;
536}
537
538void TimeManagement::update_time_page()
539{
540 auto& page = time_page();
541 u32 update_iteration = AK::atomic_fetch_add(&page.update2, 1u, AK::MemoryOrder::memory_order_acquire);
542 page.clocks[CLOCK_REALTIME_COARSE] = m_epoch_time;
543 page.clocks[CLOCK_MONOTONIC_COARSE] = monotonic_time(TimePrecision::Coarse).to_timespec();
544 AK::atomic_store(&page.update1, update_iteration + 1u, AK::MemoryOrder::memory_order_release);
545}
546
547TimePage& TimeManagement::time_page()
548{
549 return *static_cast<TimePage*>((void*)m_time_page_region->vaddr().as_ptr());
550}
551
552Memory::VMObject& TimeManagement::time_page_vmobject()
553{
554 return m_time_page_region->vmobject();
555}
556
557}