banan-os/kernel/arch/i686
Bananymous 9eb3834ae5 Kernel: Add syscall-less clock_gettime
If the processor has invariant TSC it can be used to measure time. We
keep track of the last nanosecond and TSC values and offset them based
on the current TSC. This allows getting current time in userspace.

The implementation maps a single RO page to every processes' address
space. The page contains the TSC info which gets updated every 100 ms.
If the processor does not have invariant TSC, this page will not
indicate the capability for TSC based timing.

There was the problem about how does a processor know which cpu it is
running without doing syscall. TSC counters may or may not be
synchronized between cores, so we need a separate TSC info for each
processor. I ended up adding sequence of bytes 0..255 at the start of
the shared page. When a scheduler gets a new thread, it updates the
threads gs/fs segment to point to the byte corresponding to the current
cpu.

This TSC based timing is also used in kernel. With 64 bit HPET this
probably does not bring much of a benefit, but on PIT or 32 bit HPET
this removes the need to aquire a spinlock to get the current time.

This change does force the userspace to not use gs/fs themselves and
they are both now reserved. Other one is used for TLS (this can be
technically used if user does not call libc code) and the other for
the current processor index (cannot be used as kernel unconditionally
resets it after each load balance).

I was looking at how many times timer's current time was polled
(userspace and kernel combined). When idling in window manager, it was
around 8k times/s. When running doom it peaked at over 1 million times
per second when loading and settled at ~30k times/s.
2026-01-08 17:13:59 +02:00
..
PageTable.cpp Kernel: Only send one smp message when reserving a range 2025-08-07 16:43:05 +03:00
Signal.S Kernel: Remove unused (and broken) code from signal trampoline 2025-12-28 03:53:38 +02:00
Syscall.S Kernel: Fix 32 bit target 2025-11-17 02:33:00 +02:00
Thread.S Kernel: Fix 32 bit target 2025-11-17 02:33:00 +02:00
boot.S Kernel: Add syscall-less clock_gettime 2026-01-08 17:13:59 +02:00
crti.S Kernel: Cleanup constructor calls 2024-08-31 02:23:08 +03:00
crtn.S Kernel: Cleanup constructor calls 2024-08-31 02:23:08 +03:00
interrupts.S Kernel: Fix 32 bit target 2025-11-17 02:33:00 +02:00
linker.ld Kernel: Cleanup constructor calls 2024-08-31 02:23:08 +03:00