1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:38:11 +00:00

Kernel+LibC: Allow clock_gettime() to run without syscalls

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.
This commit is contained in:
Andreas Kling 2021-08-10 13:44:01 +02:00
parent f02d73db4d
commit fdfc66db61
7 changed files with 117 additions and 0 deletions

View file

@ -10,6 +10,24 @@
namespace Kernel {
KResultOr<FlatPtr> Process::sys$map_time_page()
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
REQUIRE_PROMISE(stdio);
auto& vmobject = TimeManagement::the().time_page_vmobject();
auto range = address_space().page_directory().range_allocator().allocate_randomized(PAGE_SIZE, PAGE_SIZE);
if (!range.has_value())
return ENOMEM;
auto region_or_error = address_space().allocate_region_with_vmobject(range.value(), vmobject, 0, "Kernel time page"sv, PROT_READ, true);
if (region_or_error.is_error())
return region_or_error.error();
return region_or_error.value()->vaddr().get();
}
KResultOr<FlatPtr> Process::sys$clock_gettime(clockid_t clock_id, Userspace<timespec*> user_ts)
{
VERIFY_NO_PROCESS_BIG_LOCK(this);