100 Commits
Author SHA1 Message Date
Lucas Chollet d62c97ce48 Kernel/x86: Prefer the TSC over the HPET to update the system's time
As it requires MMIO access, reading the raw value of the HPET timer is
quite expensive, so doing it every time the system timer fired (~4ms)
resulted in a heavy overload over the system. As shown in [1] it is
quite known that HPET timers are slow to read. Moreover, when using QEMU
reading from MMIO causes a VM exit and makes the whole virtualized world
stop. The consequence is that reading the HPET timer would not appear in
profiles as it is not slow from the perspective of the VM.

To solve that issue, we're now using the Time Stamp Counter (TSC) as our
time-keeping source. Whenever the system interrupt fires, we read the
TSC (which is cheap, see [1], and doesn't cause a VM exit) and compute
the elapsed time based on how many cycle the CPU made during that time.

[1] https://docs.redhat.com/en/documentation/red_hat_enterprise_linux_for_real_time/7/html/reference_guide/chap-timestamping#Reading_hardware_clock_sources
2026-07-26 12:06:38 +02:00
Lucas Chollet 42179edb94 Kernel/Time: Consolidate TimeManagement by sharing the system callback
This makes all non-legacy timers use the same callback implementation,
and remove some code duplication between platforms.

Now, platform specific code provides a `m_read_time_from_hardware`
function that is then called by the generic code.
2026-07-25 19:29:54 +02:00
Sönke Holz 14cc6d8e40 Kernel/riscv64: Support the Google Goldfish real-time clock
The QEMU virt machine uses a Goldfish RTC device as its real-time clock.
This means that we now display the correct time in riscv64 QEMU and
date-dependent tests no longer fail!

This driver is based on the PL031 driver.
2026-04-06 16:18:35 +02:00
implicitfield ed2ba8af0b Kernel: VERIFY that s_hardware_timers is empty when adding legacy timers 2025-12-14 16:38:55 +01:00
implicitfield 6dc36b6f6b Kernel: Remove an unnecessary check
`Processor::current_in_irq()` will always be <= 1 since we don't do
nested interrupts.
2025-12-09 09:58:10 +01:00
Sönke Holz 7327ed417a Kernel/DeviceTree: Don't use DeviceRecipes for hardware timers
Now that interrupt controllers are probed in a separate stage before
ProbeStage::Early (used by timer drivers), there is no need to delay
timer driver initialization anymore.

Previously, this was needed since any device driver using interrupts
depends on InterruptManagement being initialized.

This required making the list of hardware timers accessible before
TimeManagement is initialized by converting it to a static variable.
2025-11-24 16:56:52 +01:00
Sönke Holz e8d9734a9c AK+Kernel+Userland: Don't allow Vector::append() in the kernel
This makes it not as easy to forgot to handle OOMs in the kernel.

This commit replaces most usages of this function with
`try_append(...).release_value_but_fixme_should_propagate_errors()`.
But in some cases, using the `TRY` macro or `unchecked_append()` is
already possible.

In places where allocations should not fail or an OOM would be fatal
anyways, `MUST(try_append(...))` should be used explicitly.
2025-11-17 11:08:45 +01:00
Sönke Holz aa89d387fe Kernel/aarch64: Make TimePrecision::Precise work
AArch64 already implements `monotonic_time(TimePrecision::Precise)`, but
didn't advertise support for it by setting `m_can_query_precise_time`.

This causes SystemMonitor to no longer claim that all CPU time is spent
in the kernel.
2025-03-24 19:23:41 +01:00
Sönke Holz 31c1660e38 Kernel/riscv64: Make TimePrecision::Precise work
This causes SystemMonitor to no longer claim that all CPU time is spent
in the kernel.
2025-03-24 19:23:15 +01:00
implicitfield 96c2ee922d Kernel/aarch64: Add support for the PL031
With this, it's now possible to properly set the system time on boot
when using the QEMU virt machine.
2025-03-22 21:27:31 +01:00
Sönke Holz 0d6d716e94 Kernel/aarch64: Add ARMv8Timer support
This timer is used by the Raspberry Pi 3+4 and the QEMU virt machine.

Remove the `#if ARCH`s from HardwareTimerType, as all other enum values
would otherwise have to be updated to keep the values consistent across
all architectures.
2024-12-12 15:06:28 +01:00
Sönke Holz d6a3500973 Kernel/aarch64: Make RPi::Timer a DeviceTree::Driver
This driver doesn't actually get any info (like the physical address)
from the devicetree node for now.
2024-11-02 13:53:10 -04:00
Liav A. 0482f4e117 Kernel: Remove passing of register state to IRQ handlers
Linux did the same thing 18 years ago and their reasons for the change
are similar to ours - https://github.com/torvalds/linux/commit/7d12e78

Most interrupt handlers (i.e. IRQ handlers) never used the register
state reference anywhere so there's simply no need of passing it around.
I didn't measure the performance boost but surely this change can't make
things worse anyway.
2024-09-01 21:00:18 +02:00
Idan Horowitz d3e285c253 Kernel: Deduplicate HardwareTimerBase::frequency()/ticks_per_second() 2024-04-30 06:01:26 -06:00
Liav A. 2bba9411ca Kernel: Use the AK SetOnce container class in various cases
We have many places in the kernel code that we have boolean flags that
are only set once, and never reset again but are checked multiple times
before and after the time they're being set, which matches the purpose
of the SetOnce class.
2024-04-26 23:46:23 -06:00
Sönke Holz 6bc16ad62e Kernel/riscv64: Add RISC-V support to TimeManagement 2024-01-02 06:50:59 -07:00
Sönke Holz da88d766b2 Kernel/riscv64: Make the kernel compile
This commits inserts TODOs into all necessary places to make the kernel
compile on riscv64!
2023-11-10 15:51:31 -07:00
Liav A 336fb4f313 Kernel: Move InterruptDisabler to the Interrupts subdirectory 2023-06-04 21:32:34 +02:00
Liav A 927926b924 Kernel: Move Performance-measurement code to the Tasks subdirectory 2023-06-04 21:32:34 +02:00
Liav A b88c1d90e1 Kernel: Move TimerQueue code to the Time subdirectory 2023-06-04 21:32:34 +02:00
Liav A 8f21420a1d Kernel: Move all boot-related code to the new Boot subdirectory 2023-06-04 21:32:34 +02:00
Liav A 1b04726c85 Kernel: Move all tasks-related code to the Tasks subdirectory 2023-06-04 21:32:34 +02:00
Daniel Bertalan 906abbdf53 Kernel/aarch64: Fix build after #17842 2023-05-25 08:26:07 -07:00
kleines Filmröllchen fc5cab5c21 Everywhere: Use MonotonicTime instead of Duration
This is easily identifiable by anyone who uses Duration::now_monotonic,
and any downstream users of that data.
2023-05-24 23:18:07 +02:00
kleines Filmröllchen 939600d2d4 Kernel: Use UnixDateTime wherever applicable
"Wherever applicable" = most places, actually :^), especially for
networking and filesystem timestamps.

This includes changes to unzip, which uses DOSPackedTime, since that is
changed for the FAT file systems.
2023-05-24 23:18:07 +02:00
kleines Filmröllchen 213025f210 AK: Rename Time to Duration
That's what this class really is; in fact that's what the first line of
the comment says it is.

This commit does not rename the main files, since those will contain
other time-related classes in a little bit.
2023-05-24 23:18:07 +02:00
Timon Kruiper 188a52db01 Kernel: Implement TimeManagement::boot_time() for aarch64
For now just return 0 as we have no RTC support on aarch64 yet, and add
a FIXME to return the correct value.
2023-04-06 21:19:58 +03:00
Andreas Kling 7369d0ab5f Kernel: Stop using NonnullLockRefPtrVector 2023-03-06 23:46:36 +01:00
Humberto Alves f6eb155167 Kernel: Support more clocks in sys$clock_getres()
Support all the available clocks in clock_getres(). Also, fix this
function to use the actual ticks per second value, not the constant
`_SC_CLK_TCK` (which is always equal to 8) and move the resolution
computation logic to TimeManagement.
2023-02-21 01:00:06 +01:00
Liav A 91db482ad3 Kernel: Reorganize Arch/x86 directory to Arch/x86_64 after i686 removal
No functional change.
2022-12-28 11:53:41 +01:00
Liav A 5ff318cf3a Kernel: Remove i686 support 2022-12-28 11:53:41 +01:00
Andreas Kling 10fa72d451 Kernel: Use AK::Time for InodeMetadata timestamps instead of time_t
Before this change, we were truncating the nanosecond part of file
timestamps in many different places.
2022-11-24 16:56:27 +01:00
Timon Kruiper 01a14ac7af Kernel: Implement TimeManagement for aarch64
This sets up the RPi::Timer to trigger an interurpt every 4ms using one
of the comparators. The actual time is calculated by looking at the main
counter of the RPi::Timer using the Timer::update_time function.

A stub for Scheduler::timer_tick is also added, since the TimeManagement
code now calls the function.
2022-10-17 20:11:31 +02:00
Timon Kruiper 9827c11d8b Kernel: Move InterruptDisabler out of Arch directory
The code in this file is not architecture specific, so it can be moved
to the base Kernel directory.
2022-10-17 20:11:31 +02:00
Liav A 7520acd4eb Kernel: Move Scheduler current time method to the TimeManagement code 2022-10-14 14:13:51 +02:00
Liav A fe2bd8e3dd Kernel: Move x86-specific timer code handling to Arch/x86/Time directory
The APICTimer, HPET and RTC (the RTC timer is in the context of the PC
RTC here) are timers that exist only in x86 platforms, therefore, we
move the handling code and the initialization code to the Arch/x86/Time
directory. Other related code patterns in the TimeManagement singleton
and in the Random.cpp file are guarded with #ifdef to ensure they are
only compiled for x86 builds.
2022-09-23 17:22:15 +01:00
Liav A 1b7b360ca1 Kernel: Move x86-specific IRQ controller code to Arch/x86 directory
The PIC and APIC code are specific to x86 platforms, so move them out of
the general Interrupts directory to Arch/x86/common/Interrupts directory
instead.
2022-09-20 18:43:05 +01:00
Liav A d5ee03ef5b Kernel/x86: Move RTC and CMOS code to x86 arch-specific subdirectory
The RTC and CMOS are currently only supported for x86 platforms and use
specific x86 instructions to produce only certain x86 plaform operations
and results, therefore, we move them to the Arch/x86 specific directory.
2022-09-20 18:43:05 +01:00
Andreas Kling adaaea4c9a Kernel: Make TimeManagement::boot_time() static 2022-06-15 17:15:04 +02:00
Timon Kruiper a4534678f9 Kernel: Implement InterruptDisabler using generic Processor functions
Now that the code does not use architectural specific code, it is moved
to the generic Arch directory and the paths are modified accordingly.
2022-06-02 13:14:12 +01:00
Idan Horowitz 086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Brian Gianforcaro d05fa14e52 Kernel: Use TRY() when validating clock_id in TimeManagement
Gets rid of a bit of code duplication, and makes the API more consistent
with the style we are moving towards.
2022-02-21 15:47:51 -08:00
Idan Horowitz 57ba67ed2a Kernel: Create the time page region before initializing the timers
We were unconditionally trying to update it in the interrupt, which
would depend on the timer interrupt not being received too soon after
the timers are initialized (before the time page was initialized),
which was the case when using HPET timers via the ACPI tables, but not
when using the PIT when ACPI was disabled.
2022-01-18 21:00:46 +02:00
Pankaj Raghav 1a27220bca Kernel: Encapsulate APIC initialization inside InterruptManagement
Currently the APIC class is constructed irrespective of whether it
is used or not.

So, move APIC initialization from init to the InterruptManagement
class and construct the APIC class only when it is needed.
2021-12-26 16:22:09 +02:00
Andreas Kling 0592f80186 Kernel: Add TimeManagement::is_initialized()
This allows clients to check whether TimeManagement is available before
trying to ask it about time related things.
2021-10-26 01:00:54 +02:00
Liav A 9132596b8e Kernel: Move ACPI and BIOS code into the new Firmware directory
This will somwhat help unify them also under the same SysFS directory in
the commit.
Also, it feels much more like this change reflects the reality that both
ACPI and the BIOS are part of the firmware on x86 computers.
2021-09-12 11:52:16 +02:00
Andreas Kling 75564b4a5f Kernel: Make kernel region allocators return KResultOr<NOP<Region>>
This expands the reach of error propagation greatly throughout the
kernel. Sadly, it also exposes the fact that we're allocating (and
doing other fallible things) in constructors all over the place.

This patch doesn't attempt to address that of course. That's work for
our future selves.
2021-09-06 01:55:27 +02:00
Andreas Kling d60635cb9d Kernel: Convert Processor::in_irq() to static current_in_irq()
This closes the race window between Processor::current() and a context
switch happening before in_irq().
2021-08-23 00:02:09 +02:00
Andreas Kling b7dae4f90e Kernel: Add CLOCK_MONOTONIC_COARSE to the kernel time page
This allows clock_gettime(CLOCK_MONOTONIC_COARSE) without syscalls.
Core::EventLoop takes advantage of this automatically. :^)
2021-08-10 21:51:05 +02:00
Andreas Kling aaead6f332 Kernel: Only expose CLOCK_REALTIME_COARSE via the kernel time page
Non-COARSE clock sources may probably still require a syscall.
2021-08-10 21:51:05 +02:00