summaryrefslogtreecommitdiff
path: root/Kernel/Time/TimeManagement.cpp
AgeCommit message (Collapse)Author
2022-12-28Kernel: Reorganize Arch/x86 directory to Arch/x86_64 after i686 removalLiav A
No functional change.
2022-12-28Kernel: Remove i686 supportLiav A
2022-11-24Kernel: Use AK::Time for InodeMetadata timestamps instead of time_tAndreas Kling
Before this change, we were truncating the nanosecond part of file timestamps in many different places.
2022-10-17Kernel: Implement TimeManagement for aarch64Timon Kruiper
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-17Kernel: Move InterruptDisabler out of Arch directoryTimon Kruiper
The code in this file is not architecture specific, so it can be moved to the base Kernel directory.
2022-10-14Kernel: Move Scheduler current time method to the TimeManagement codeLiav A
2022-09-23Kernel: Move x86-specific timer code handling to Arch/x86/Time directoryLiav A
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-20Kernel: Move x86-specific IRQ controller code to Arch/x86 directoryLiav A
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-20Kernel/x86: Move RTC and CMOS code to x86 arch-specific subdirectoryLiav A
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-06-15Kernel: Make TimeManagement::boot_time() staticAndreas Kling
2022-06-02Kernel: Implement InterruptDisabler using generic Processor functionsTimon Kruiper
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-04-01Everywhere: Run clang-formatIdan Horowitz
2022-02-21Kernel: Use TRY() when validating clock_id in TimeManagementBrian Gianforcaro
Gets rid of a bit of code duplication, and makes the API more consistent with the style we are moving towards.
2022-01-18Kernel: Create the time page region before initializing the timersIdan Horowitz
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.
2021-12-26Kernel: Encapsulate APIC initialization inside InterruptManagementPankaj Raghav
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-10-26Kernel: Add TimeManagement::is_initialized()Andreas Kling
This allows clients to check whether TimeManagement is available before trying to ask it about time related things.
2021-09-12Kernel: Move ACPI and BIOS code into the new Firmware directoryLiav A
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-06Kernel: Make kernel region allocators return KResultOr<NOP<Region>>Andreas Kling
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-08-23Kernel: Convert Processor::in_irq() to static current_in_irq()Andreas Kling
This closes the race window between Processor::current() and a context switch happening before in_irq().
2021-08-10Kernel: Add CLOCK_MONOTONIC_COARSE to the kernel time pageAndreas Kling
This allows clock_gettime(CLOCK_MONOTONIC_COARSE) without syscalls. Core::EventLoop takes advantage of this automatically. :^)
2021-08-10Kernel: Only expose CLOCK_REALTIME_COARSE via the kernel time pageAndreas Kling
Non-COARSE clock sources may probably still require a syscall.
2021-08-10Kernel: Close race window in timestamp update mechanismAndreas Kling
As pointed out by 8infy, this mechanism is racy: WRITER: 1. ++update1; 2. write_data(); 3. ++update2; READER: 1. do { auto saved = update1; 2. read_data(); 3. } while (saved != update2); The following sequence can lead to a bogus/partial read: R1 R2 R3 W1 W2 W3 We close this race by incrementing the second update counter first: WRITER: 1. ++update2; 2. write_data(); 3. ++update1;
2021-08-10Kernel+LibC: Allow clock_gettime() to run without syscallsAndreas Kling
This patch adds a vDSO-like mechanism for exposing the current time as an array of per-clock-source timestamps. LibC's clock_gettime() calls sys$map_time_page() to map the kernel's "time page" into the process address space (at a random address, ofc.) This is only done on first call, and from then on the timestamps are fetched from the time page. This first patch only adds support for CLOCK_REALTIME, but eventually we should be able to support all clock sources this way and get rid of sys$clock_gettime() in the kernel entirely. :^) Accesses are synchronized using two atomic integers that are incremented at the start and finish of the kernel's time page update cycle.
2021-08-08Everywhere: Replace AK::Singleton => SingletonAndreas Kling
2021-07-11Kernel: Remove unused header includes in Time subtreeBrian Gianforcaro
2021-06-24Kernel: Move special sections into Sections.hHendiadyoin1
This also removes a lot of CPU.h includes infavor for Sections.h
2021-06-24Kernel: Pull apart CPU.hHendiadyoin1
This does not add any functional changes
2021-05-17Kernel: Fix return value for {enable,disable}_profile_timer()Gunnar Beutner
These functions should return success when being called when profiling has been requested from multiple callers because enabling/disabling the timer is a no-op in that case and thus didn't fail.
2021-05-15Kernel: Don't use the profile timer if we don't have a timer to assignLiav A
2021-05-14Kernel: Use a separate timer for profiling the systemGunnar Beutner
This updates the profiling subsystem to use a separate timer to trigger CPU sampling. This timer has a higher resolution (1000Hz) and is independent from the scheduler. At a later time the resolution could even be made configurable with an argument for sys$profiling_enable() - but not today.
2021-05-05Kernel: Modify TimeManagement::current_time(..) API so it can't fail. (#6869)Brian Gianforcaro
The fact that current_time can "fail" makes its use a bit awkward. All callers in the Kernel are trusted besides syscalls, so assert that they never get there, and make sure all current callers perform validation of the clock_id with TimeManagement::is_valid_clock_id(). I have fuzzed this change locally for a bit to make sure I didn't miss any obvious regression.
2021-05-05Kernel: Add Processor::is_bootstrap_processor() function, and use it. (#6871)Brian Gianforcaro
The variety of checks for Processor::id() == 0 could use some assistance in the readability department. This change adds a new function to represent this check, and replaces the comparison everywhere it's used.
2021-04-22Everything: Move to SPDX license identifiers in all files.Brian Gianforcaro
SPDX License Identifiers are a more compact / standardized way of representing file license information. See: https://spdx.dev/resources/use/#identifiers This was done with the `ambr` search and replace tool. ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-03-12Kernel: Convert klog() => AK::Format in TimeManagementAndreas Kling
2021-03-03Kernel: Mark more of the kernel initialization as UNMAP_AFTER_INITBrian Gianforcaro
2021-03-03Kernel: Move Kernel CommandLine parsing to strongly typed API.Brian Gianforcaro
Previously all of the CommandLine parsing was spread out around the Kernel. Instead move it all into the Kernel CommandLine class, and expose a strongly typed API for querying the state of options.
2021-03-02Kernel: Make TimeManagement use AK::Time internallyBen Wiederhake
I don't dare touch the multi-threading logic and locking mechanism, so it stays timespec for now. However, this could and should be changed to AK::Time, and I bet it will simplify the "increment_time_since_boot()" code.
2021-02-23Everywhere: Rename ASSERT => VERIFYAndreas Kling
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED) Since all of these checks are done in release builds as well, let's rename them to VERIFY to prevent confusion, as everyone is used to assertions being compiled out in release. We can introduce a new ASSERT macro that is specifically for debug checks, but I'm doing this wholesale conversion first since we've accumulated thousands of these already, and it's not immediately obvious which ones are suitable for ASSERT.
2021-02-19Kernel: Slap UNMAP_AFTER_INIT on a whole bunch of functionsAndreas Kling
There's no real system here, I just added it to various functions that I don't believe we ever want to call after initialization has finished. With these changes, we're able to unmap 60 KiB of kernel text after init. :^)
2021-01-27Kernel: Make Processor::id a static functionTom
This eliminates the window between calling Processor::current and the member function where a thread could be moved to another processor. This is generally not as big of a concern as with Processor::current_thread, but also slightly more light weight.
2021-01-25Everywhere: Remove unnecessary debug comments.asynts
It would be tempting to uncomment these statements, but that won't work with the new changes. This was done with the following commands: find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/#define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/#define/ { toggle = 1 }' {} \; find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/ #define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/ #define/ { toggle = 1 }' {} \;
2021-01-09Everywhere: Replace a bundle of dbg with dbgln.asynts
These changes are arbitrarily divided into multiple commits to make it easier to find potentially introduced bugs with git bisect.Everything: The modifications in this commit were automatically made using the following command: find . -name '*.cpp' -exec sed -i -E 's/dbg\(\) << ("[^"{]*");/dbgln\(\1\);/' {} \;
2020-12-27Kernel: Take into account the time keeper's frequency (if no HPET)Tom
The PIT is now also running at a rate of ~250 ticks/second, so rather than assuming there are 1000 ticks/second we need to query the timer being used for the actual frequency. Fixes #4508
2020-12-21Kernel: Improve time keeping and dramatically reduce interrupt loadTom
This implements a number of changes related to time: * If a HPET is present, it is now used only as a system timer, unless the Local APIC timer is used (in which case the HPET timer will not trigger any interrupts at all). * If a HPET is present, the current time can now be as accurate as the chip can be, independently from the system timer. We now query the HPET main counter for the current time in CPU #0's system timer interrupt, and use that as a base line. If a high precision time is queried, that base line is used in combination with quering the HPET timer directly, which should give a much more accurate time stamp at the expense of more overhead. For faster time stamps, the more coarse value based on the last interrupt will be returned. This also means that any missed interrupts should not cause the time to drift. * The default system interrupt rate is reduced to about 250 per second. * Fix calculation of Thread CPU usage by using the amount of ticks they used rather than the number of times a context switch happened. * Implement CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE and use it for most cases where precise timestamps are not needed.
2020-12-02Kernel: Add CLOCK_REALTIME support to the TimerQueueTom
This allows us to use blocking timeouts with either monotonic or real time for all blockers. Which means that clock_nanosleep() now also supports CLOCK_REALTIME. Also, switch alarm() to use CLOCK_REALTIME as per specification.
2020-11-30Kernel: Move some time related code from Scheduler into TimeManagementTom
Use the TimerQueue to expire blocking operations, which is one less thing the Scheduler needs to check on every iteration. Also, add a BlockTimeout class that will automatically handle relative or absolute timeouts as well as overriding timeouts (e.g. socket timeouts) more consistently. Also, rework the TimerQueue class to be able to fire events from any processor, which requires Timer to be RefCounted. Also allow creating id-less timers for use by blocking operations.
2020-11-10Kernel+LibC: Add adjtime(2)Nico Weber
Most systems (Linux, OpenBSD) adjust 0.5 ms per second, or 0.5 us per 1 ms tick. That is, the clock is sped up or slowed down by at most 0.05%. This means adjusting the clock by 1 s takes 2000 s, and the clock an be adjusted by at most 1.8 s per hour. FreeBSD adjusts 5 ms per second if the remaining time adjustment is >= 1 s (0.5%) , else it adjusts by 0.5 ms as well. This allows adjusting by (almost) 18 s per hour. Since Serenity OS can lose more than 22 s per hour (#3429), this picks an adjustment rate up to 1% for now. This allows us to adjust up to 36s per hour, which should be sufficient to adjust the clock fast enough to keep up with how much time the clock currently loses. Once we have a fancier NTP implementation that can adjust tick rate in addition to offset, we can think about reducing this. adjtime is a bit old-school and most current POSIX-y OSs instead implement adjtimex/ntp_adjtime, but a) we have to start somewhere b) ntp_adjtime() is a fairly gnarly API. OpenBSD's adjfreq looks like it might provide similar functionality with a nicer API. But before worrying about all this, it's probably a good idea to get to a place where the kernel APIs are (barely) good enough so that we can write an ntp service, and once we have that we should write a way to automatically evaluate how well it keeps the time adjusted, and only then should we add improvements ot the adjustment mechanism.
2020-11-07Kernel: Update TimeManagement::m_epoch_time directly in ↵Nico Weber
increment_time_since_boot
2020-11-06Kernel: Fix HPET timer not firing in BochsTom
* Change the register structures to use the volatile keyword explicitly on the register values. This avoids accidentally omitting it as any access will be guaranteed volatile. * Don't assume we can read/write 64 bit value to the main counter and the comparator. Not all HPET implementations may support this. So, just use 32 bit words to access the registers. This ultimately works around a bug in Bochs 2.6.11 that loses 32 bits of a 64 bit write to a timer's comparator register (it internally writes one half and clears the Tn_VAL_SET_CNF bit, and then because it's cleared it fails to write the second half). * Properly calculate the tick duration in calculate_ticks_in_nanoseconds * As per specification, changing the frequency of one periodic timer requires a restart of all periodic timers as it requires the main counter to be reset.
2020-10-25Kernel: Set up and calibrate APIC timer, and enable timer on all CPUsTom
This enables the APIC timer on all CPUs, which means Scheduler::timer_tick is now called on all CPUs independently. We still don't do anything on the APs as it instantly crashes due to a number of other problems.