darlinghq / darling

Darwin/macOS emulation layer for Linux

Home Page:http://www.darlinghq.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unimplemented syscall (482) when calling clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...)

xhub opened this issue · comments

Expected Result
Call to clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...) to succeed.

Actual Result
Call to clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...) does not succeed and the message "Unimplemented syscall (482)" is printed. In xnu/gen/syscall.h, 482 corresponds to SYS_thread_selfusage.

Steps To Reproduce

  1. Compile the program test_clock.txt (couldn't upload it as a C file).
  2. Run the resulting binary.
  3. Notice the 2 message and the zero time for the line per clock_gettime(CLOCK_THREAD_CPUTIME_ID.

System Information
What system are you using?

Software Version
Linux Kernel 6.6.12
Darling fe0da14

I believe I have a partial fix, but I need to resolve some issues with my machine before I can test them. In the meantime, here's what I've come up with:
The error occurs when test_clock.c asks the system for CLOCK_THREAD_CPUTIME_ID. OSX's implementation of clock_gettime in turn just asks for thread_selfusage. In theory, replacing the syscall would be accomplished by simply returning Linux's result for clock_gettime(CLOCK_THREAD_CPUTIME_ID). It would resolve the issue, but I'm not certain it's entirely accurate.
In xnu/bsd/kern/kern_resource.c, thread_selfusage calls thread_get_runtime_self, which is defined in darling-xnu/osfmk/kern/thread.c. That function updates the processor's thread timer by calling mach_absolute_time(), and according to Apple's documentation, the return value of that function is equivalent to clock_gettime_nsec_np(CLOCK_UPTIME_RAW) in nanoseconds. Since that clock doesn't have a definition on Linux, I'm not sure if replacing thread_selfusage in the way I've described is entirely accurate, even if it is the closest analogue. I'll properly test it once I've resolved the issues on my end, but I'd like some feedback on my approach.