Commit Graph

19 Commits

Author SHA1 Message Date
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
Bananymous 9d6656451a LibC: Make time_t signed integer
Some port like python3 assumes this is the case
2025-08-19 16:23:30 +03:00
Bananymous 6ed0e84421 LibC: Fix mktime argument updating 2025-08-11 19:00:23 +03:00
Bananymous 9b09d2b47a LibC: Update struct tm to be POSIX issue 8 compliant 2025-08-11 18:59:20 +03:00
Bananymous 064d9009a2 LibC: Add clock_getres stub
This is needed for our python3 port
2025-08-07 02:50:24 +03:00
Bananymous dbdefa0f4a LibC: Implement pthread cancelation
This code is not tested at all but it looks correct xD
2025-06-01 13:48:03 +03:00
Bananymous 7eade002a7 LibC: Fix `mktime`
yday calculation was off so results were always off for march-december
2025-05-28 03:10:01 +03:00
Bananymous a8f8d27f4e LibC: Implement basic tzset()
I still don't have timezone support so this just sets values to UTC
2025-05-28 03:10:01 +03:00
Bananymous abf7c8e68a LibC: Implement `difftime` 2025-05-04 13:42:17 +03:00
Bananymous e62cc17797 LibC: Use `nl_langinfo` for strings in `strftime` 2025-04-19 20:03:56 +03:00
Bananymous 60bffb5f49 LibC: Implement `clock` in terms of `clock_gettime`
This allows clock to "just work" after I update clock_gettime to support
CLOCK_PROCESS_CPUTIME_ID.
2025-01-24 19:58:44 +02:00
Bananymous b6455e0661 LibC: Implement stub for clock 2025-01-24 19:23:33 +02:00
Bananymous b6d0950ee9 LibC: Fix strftime implementation
This implementation now passes every musl-libc test :)
2024-12-05 06:46:31 +02:00
Bananymous c1a32a4041 LibC: Implement mktime 2024-12-05 06:43:35 +02:00
Bananymous f0e54cdd51 LibC: Implement {asctime,ctime,gmtime,localtime}_r
Old non _r prefixed functions are now just wrappers around these new
functions :)
2024-12-02 20:13:37 +02:00
Bananymous 500f774b7f LibC: Add a lot of stub implementations for different libc functions
some ports want to link against these, but they are not ever called
based on simple testing

This patch adds stubs for
- openlog
- syslog
- getrusage
- mlock
- mprotect
- getpeername
- shutdown
- tzset
- mktime
2024-08-22 15:10:41 +03:00
Bananymous 5dce4e6aea LibC: Implement strftime()
I did not test this at all, this might very well be broken
2024-08-05 00:49:44 +03:00
Bananymous 10ce03a1e7 LibC: Implement ctime() and asctime() 2024-07-31 23:26:10 +03:00
Bananymous c69919738b BuildSystem: Move all userpace libraries under the userspace directory
As the number of libraries is increasing, root directory starts to
expand. This adds better organization for libraries
2024-06-18 13:14:35 +03:00