banan-os/kernel/kernel
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
..
ACPI Kernel: Add support for SA_SIGINFO 2025-11-17 05:26:07 +02:00
Audio Kernel: Add ioctls to select audio device's output pin 2026-01-06 22:26:11 +02:00
Device Kernel: Implement proper memory region splitting 2025-11-13 04:20:53 +02:00
FS Kernel: Decrease the number of syncs done by ext2 inodes 2026-01-01 23:54:09 +02:00
Input Kernel: Add support for DualShock 3 controllers 2026-01-03 20:07:08 +02:00
Lock Kernel: Track the number of recursive spinlocks a thread is holding 2025-06-06 06:51:15 +03:00
Memory Kernel: Fix bitmap byte index calculation in PMM 2026-01-04 01:16:43 +02:00
Networking Kernel: Rework socket binding to an address 2025-12-31 19:28:55 +02:00
Storage Kernel: Rework storage device and disk cache locking 2026-01-02 18:06:56 +02:00
Terminal Kernel: Implement FIONREAD for tty and pty 2025-11-24 18:15:10 +02:00
Timer Kernel: Add syscall-less clock_gettime 2026-01-08 17:13:59 +02:00
USB Kernel/LibInput: Rework Joystick handling 2026-01-07 19:01:07 +02:00
APIC.cpp Kernel: Fix IOAPIC max redirection entry fetching 2025-08-25 17:15:55 +03:00
BootInfo.cpp Kernel: Add support for multiboot 2025-08-05 03:09:24 +03:00
CPUID.cpp Kernel: Add syscall-less clock_gettime 2026-01-08 17:13:59 +02:00
Credentials.cpp Kernel/LibC: Implement {get,set,init}groups 2025-08-10 19:57:31 +03:00
Debug.cpp Kernel: Show QR code with panic logs on kernel panic 2025-10-28 05:50:19 +02:00
ELF.cpp Kernel/LibC: Implement chroot 2025-08-11 14:07:37 +03:00
Epoll.cpp Kernel: Remove minimum timeout of 100 ms from epoll 2025-08-15 18:28:11 +03:00
Errors.cpp Kernel: Add support for ATA CHS addressing and cleanup code 2024-11-19 20:39:32 +02:00
GDT.cpp Kernel: Fix 32 bit target 2025-11-17 02:33:00 +02:00
IDT.cpp Kernel: Don't save/restore sse or reschedule on spurious interrupts 2026-01-06 21:58:56 +02:00
InterruptController.cpp Kernel: Remove lai as a dependecy 2024-04-10 04:39:48 +03:00
Interruptable.cpp Kernel: Fix interrupt system 2024-09-27 15:31:31 +03:00
OpenFileDescriptorSet.cpp Kernel: Implement basic F_{GET,SET}LK{,W} 2026-01-06 21:58:56 +02:00
PCI.cpp Kernel: Implement basic AC97 driver 2025-07-15 14:17:49 +03:00
PIC.cpp Kernel: Start all processors on kernel boot 2024-03-03 02:19:43 +02:00
Panic.cpp BAN/Kernel: Rework assertion/panic system 2024-03-04 11:41:54 +02:00
Process.cpp Kernel: Add syscall-less clock_gettime 2026-01-08 17:13:59 +02:00
Processor.cpp Kernel: Add syscall-less clock_gettime 2026-01-08 17:13:59 +02:00
Random.cpp Kernel: Clobber flags in `rdrand` 2025-07-18 19:36:37 +03:00
SSP.cpp All: Cleanup all files 2024-01-24 15:53:38 +02:00
Scheduler.cpp Kernel: Add syscall-less clock_gettime 2026-01-08 17:13:59 +02:00
Syscall.cpp Kernel/LibC: Implement {recv,send}msg as syscalls 2025-11-10 01:40:33 +02:00
Thread.cpp Kernel: Add syscall-less clock_gettime 2026-01-08 17:13:59 +02:00
ThreadBlocker.cpp Kernel: Rewrite ThreadBlocker 2025-07-02 00:17:42 +03:00
kernel.cpp Kernel: Add syscall-less clock_gettime 2026-01-08 17:13:59 +02:00