Skip to content

Commit

Permalink
Kernel: Introduce the new Time management subsystem
Browse files Browse the repository at this point in the history
This new subsystem includes better abstractions of how time will be
handled in the OS. We take advantage of the existing RTC timer to aid
in keeping time synchronized. This is standing in contrast to how we
handled time-keeping in the kernel, where the PIT was responsible for
that function in addition to update the scheduler about ticks.
With that new advantage, we can easily change the ticking dynamically
and still keep the time synchronized.

In the process context, we no longer use a fixed declaration of
TICKS_PER_SECOND, but we call the TimeManagement singleton class to
provide us the right value. This allows us to use dynamic ticking in
the future, a feature known as tickless kernel.

The scheduler no longer does by himself the calculation of real time
(Unix time), and just calls the TimeManagment singleton class to provide
the value.

Also, we can use 2 new boot arguments:
- the "time" boot argument accpets either the value "modern", or
  "legacy". If "modern" is specified, the time management subsystem will
  try to setup HPET. Otherwise, for "legacy" value, the time subsystem
  will revert to use the PIT & RTC, leaving HPET disabled.
  If this boot argument is not specified, the default pattern is to try
  to setup HPET.
- the "hpet" boot argumet accepts either the value "periodic" or
  "nonperiodic". If "periodic" is specified, the HPET will scan for
  periodic timers, and will assert if none are found. If only one is
  found, that timer will be assigned for the time-keeping task. If more
  than one is found, both time-keeping task & scheduler-ticking task
  will be assigned to periodic timers.
  If this boot argument is not specified, the default pattern is to try
  to scan for HPET periodic timers. This boot argument has no effect if
  HPET is disabled.

In hardware context, PIT & RealTimeClock classes are merely inheriting
from the HardwareTimer class, and they allow to use the old i8254 (PIT)
and RTC devices, managing them via IO ports. By default, the RTC will be
programmed to a frequency of 1024Hz. The PIT will be programmed to a
frequency close to 1000Hz.

About HPET, depending if we need to scan for periodic timers or not,
we try to set a frequency close to 1000Hz for the time-keeping timer
and scheduler-ticking timer. Also, if possible, we try to enable the
Legacy replacement feature of the HPET. This feature if exists,
instructs the chipset to disconnect both i8254 (PIT) and RTC.
This behavior is observable on QEMU, and was verified against the source
code:
qemu/qemu@ce967e2

The HPETComparator class is inheriting from HardwareTimer class, and is
responsible for an individual HPET comparator, which is essentially a
timer. Therefore, it needs to call the singleton HPET class to perform
HPET-related operations.

The new abstraction of Hardware timers brings an opportunity of more new
features in the foreseeable future. For example, we can change the
callback function of each hardware timer, thus it makes it possible to
swap missions between hardware timers, or to allow to use a hardware
timer for other temporary missions (e.g. calibrating the LAPIC timer,
measuring the CPU frequency, etc).
  • Loading branch information
supercomputer7 authored and awesomekling committed Mar 19, 2020
1 parent 5d90e9c commit 9db291d
Show file tree
Hide file tree
Showing 18 changed files with 1,513 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Kernel/Devices/PCSpeaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include <Kernel/Arch/i386/CPU.h>
#include <Kernel/Devices/PCSpeaker.h>
#include <Kernel/Devices/PIT.h>
#include <Kernel/Time/PIT.h>
#include <LibBareMetal/IO.h>

void PCSpeaker::tone_on(int frequency)
Expand Down
8 changes: 6 additions & 2 deletions Kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ OBJS = \
Interrupts/IRQHandler.o \
Interrupts/SharedIRQHandler.o \
CMOS.o \
Devices/PIT.o \
Time/PIT.o \
Time/TimeManagement.o \
Time/HardwareTimer.o \
Time/RTC.o \
Time/HPET.o \
Time/HPETComparator.o \
Devices/BXVGADevice.o \
Devices/BlockDevice.o \
Devices/CharacterDevice.o \
Expand All @@ -48,7 +53,6 @@ OBJS = \
Devices/SerialDevice.o \
Devices/ZeroDevice.o \
Devices/VMWareBackdoor.o \
Devices/HardwareTimer.o \
DoubleBuffer.o \
FileSystem/Custody.o \
FileSystem/DevPtsFS.o \
Expand Down
22 changes: 13 additions & 9 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <Kernel/Devices/KeyboardDevice.h>
#include <Kernel/Devices/NullDevice.h>
#include <Kernel/Devices/PCSpeaker.h>
#include <Kernel/Devices/PIT.h>
#include <Kernel/Devices/RandomDevice.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/DevPtsFS.h>
Expand Down Expand Up @@ -67,6 +66,7 @@
#include <Kernel/TTY/MasterPTY.h>
#include <Kernel/TTY/TTY.h>
#include <Kernel/Thread.h>
#include <Kernel/Time/TimeManagement.h>
#include <Kernel/VM/PageDirectory.h>
#include <Kernel/VM/PrivateInodeVMObject.h>
#include <Kernel/VM/PurgeableVMObject.h>
Expand Down Expand Up @@ -2105,13 +2105,13 @@ unsigned Process::sys$alarm(unsigned seconds)
REQUIRE_PROMISE(stdio);
unsigned previous_alarm_remaining = 0;
if (m_alarm_deadline && m_alarm_deadline > g_uptime) {
previous_alarm_remaining = (m_alarm_deadline - g_uptime) / TICKS_PER_SECOND;
previous_alarm_remaining = (m_alarm_deadline - g_uptime) / TimeManagement::the().ticks_per_second();
}
if (!seconds) {
m_alarm_deadline = 0;
return previous_alarm_remaining;
}
m_alarm_deadline = g_uptime + seconds * TICKS_PER_SECOND;
m_alarm_deadline = g_uptime + seconds * TimeManagement::the().ticks_per_second();
return previous_alarm_remaining;
}

Expand Down Expand Up @@ -2231,10 +2231,10 @@ int Process::sys$sleep(unsigned seconds)
REQUIRE_PROMISE(stdio);
if (!seconds)
return 0;
u64 wakeup_time = Thread::current->sleep(seconds * TICKS_PER_SECOND);
u64 wakeup_time = Thread::current->sleep(seconds * TimeManagement::the().ticks_per_second());
if (wakeup_time > g_uptime) {
u32 ticks_left_until_original_wakeup_time = wakeup_time - g_uptime;
return ticks_left_until_original_wakeup_time / TICKS_PER_SECOND;
return ticks_left_until_original_wakeup_time / TimeManagement::the().ticks_per_second();
}
return 0;
}
Expand Down Expand Up @@ -4268,8 +4268,12 @@ int Process::sys$clock_gettime(clockid_t clock_id, timespec* user_ts)

switch (clock_id) {
case CLOCK_MONOTONIC:
ts.tv_sec = g_uptime / TICKS_PER_SECOND;
ts.tv_nsec = (g_uptime % TICKS_PER_SECOND) * 1000000;
ts.tv_sec = TimeManagement::the().seconds_since_boot();
ts.tv_nsec = TimeManagement::the().ticks_this_second() * 1000000;
break;
case CLOCK_REALTIME:
ts.tv_sec = TimeManagement::the().epoch_time();
ts.tv_nsec = TimeManagement::the().ticks_this_second() * 1000000;
break;
default:
return -EINVAL;
Expand Down Expand Up @@ -4322,8 +4326,8 @@ int Process::sys$clock_nanosleep(const Syscall::SC_clock_nanosleep_params* user_

timespec remaining_sleep;
memset(&remaining_sleep, 0, sizeof(timespec));
remaining_sleep.tv_sec = ticks_left / TICKS_PER_SECOND;
ticks_left -= remaining_sleep.tv_sec * TICKS_PER_SECOND;
remaining_sleep.tv_sec = ticks_left / TimeManagement::the().ticks_per_second();
ticks_left -= remaining_sleep.tv_sec * TimeManagement::the().ticks_per_second();
remaining_sleep.tv_nsec = ticks_left * 1000000;
copy_to_user(params.remaining_sleep, &remaining_sleep);
}
Expand Down
6 changes: 3 additions & 3 deletions Kernel/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@

#include <AK/QuickSort.h>
#include <AK/TemporaryChange.h>
#include <Kernel/Devices/PIT.h>
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/Net/Socket.h>
#include <Kernel/Process.h>
#include <Kernel/Profiling.h>
#include <Kernel/RTC.h>
#include <Kernel/Scheduler.h>
#include <Kernel/Time/TimeManagement.h>
#include <Kernel/TimerQueue.h>

//#define LOG_EVERY_CONTEXT_SWITCH
Expand Down Expand Up @@ -588,8 +588,8 @@ void Scheduler::timer_tick(const RegisterState& regs)
++g_uptime;

timeval tv;
tv.tv_sec = RTC::boot_time() + PIT::the().seconds_since_boot();
tv.tv_usec = PIT::the().ticks_this_second() * 1000;
tv.tv_sec = TimeManagement::the().epoch_time();
tv.tv_usec = TimeManagement::the().ticks_this_second() * 1000;
Process::update_info_page_timestamp(tv);

if (Process::current->is_profiling()) {
Expand Down
Loading

0 comments on commit 9db291d

Please sign in to comment.