jart / blink

tiniest x86-64-linux emulator
ISC License
7k stars 225 forks source link

[WIP] HaikuOS Support #38

Closed trungnt2910 closed 1 year ago

trungnt2910 commented 1 year ago

This PR tracks the patches made for blink to be built on HaikuOS and additional work to be done for make check to work on Haiku.

Currently, these tests are still failing:

HAIKU_PROBLEMATIC_TESTS =                                   \
        o/$(MODE)/third_party/cosmo/2/nanosleep_test.com.ok             \
        o/$(MODE)/third_party/cosmo/2/clock_nanosleep_test.com.ok       \
        o/$(MODE)/third_party/cosmo/2/note_test.com.ok                  \
        o/$(MODE)/third_party/cosmo/2/mu_test.com.ok                    \
        o/$(MODE)/third_party/cosmo/2/setitimer_test.com.ok             \
        o/$(MODE)/third_party/cosmo/2/test_suite_ecp.com.ok             \
        o/$(MODE)/third_party/cosmo/2/counter_test.com.ok               \
        o/$(MODE)/third_party/cosmo/2/pthread_exit_test.com.ok          \
        o/$(MODE)/third_party/cosmo/2/pthread_create_test.com.ok            \
        o/$(MODE)/third_party/cosmo/2/pthread_detach_test.com.ok            \
        o/$(MODE)/third_party/cosmo/2/pthread_mutex_lock_test.com.ok            \
        o/$(MODE)/third_party/cosmo/2/pthread_mutex_lock2_test.com.ok           \
        o/$(MODE)/third_party/cosmo/2/pthread_spin_lock_test.com.ok     \
        o/$(MODE)/third_party/cosmo/2/test_suite_aes.cbc.com.ok             \
        o/$(MODE)/third_party/cosmo/2/test_suite_cipher.gcm.com.ok          \
        o/$(MODE)/third_party/cosmo/2/test_suite_ctr_drbg.com.ok            \
        o/$(MODE)/third_party/cosmo/2/test_suite_entropy.com.ok             \
        o/$(MODE)/third_party/cosmo/2/test_suite_mpi.com.ok             \
        o/$(MODE)/third_party/cosmo/2/test_suite_md.com.ok              \
        o/$(MODE)/third_party/cosmo/5/sigaction_test.com.ok         \
        o/$(MODE)/third_party/cosmo/2/mu_starvation_test.com.ok     \
        o/$(MODE)/third_party/cosmo/2/dup_test.com.ok               \
        o/$(MODE)/third_party/cosmo/2/cv_test.com.ok                \
        o/$(MODE)/third_party/cosmo/2/setsockopt_test.com.ok        \
        o/$(MODE)/third_party/cosmo/5/pipe_test.com.ok              \
        o/$(MODE)/third_party/cosmo/2/fork_test.com.ok              \
        o/$(MODE)/third_party/cosmo/2/execve_test.com.ok            \
        o/$(MODE)/third_party/cosmo/2/cv_wait_example_test.com.ok           \
        o/$(MODE)/third_party/cosmo/2/daemon_test.com.ok            \
        o/$(MODE)/third_party/cosmo/2/socket_test.com.ok            \
        o/$(MODE)/third_party/cosmo/5/unix_test.com.ok                  \

Most of these tests fail at some syscall. In the best case, it fails with a weird errno, in other cases, with a ud2 "Undefined instruction" error in the JIT, as mentioned in #35.

Is there any way to see verbose logs of how Cosmopolitan detects the OS? Reading through the codebase, I see that Cosmopolitan implements syscalls as some kind of optimized form of assembly that is platform-specific, for example this. I wonder if these assembly calls are routed through blink or executed directly on the host's system. If the latter is true, this is a huge problem for Haiku, since while it conforms to POSIX, it has a very different way of exposing kernel functionality.

Also, this PR, while focusing on support for Haiku, has some other patches that are not Haiku-specific too.

jart commented 1 year ago

Is there any way to see verbose logs of how Cosmopolitan detects the OS? Reading through the codebase, I see that Cosmopolitan implements syscalls as some kind of optimized form of assembly that is platform-specific, for example this. I wonder if these assembly calls are routed through blink or executed directly on the host's system. If the latter is true, this is a huge problem for Haiku, since while it conforms to POSIX, it has a very different way of exposing kernel functionality.

When you're running a Cosmopolitan binary, you can use the --strace and --ftrace flags to see what it's doing. For example, system call tracing looks like:

master jart@nightmare:~/blink$ o//blink/blink third_party/cosmo/2/a64l_test.com --strace |& head
SYS  24060            259'931 bell system five system call support 309 magnums loaded on gnu/systemd
SYS  24060            625'384 mprotect(0x700000000000, 16'384, 0) → 0
SYS  24060            763'952 mmap(0x700000000000, 65'536, PROT_READ|PROT_WRITE, MAP_STACK|MAP_ANONYMOUS, -1, 0) → 0x700000000000 (65'536 bytes total)
SYS  24060          1'321'726 getenv("TMPDIR") → NULL
SYS  24060          1'487'868 sigaction(SIGXCPU, {.sa_handler=&403c1f}, [NULL]) → 0
SYS  24060          1'537'816 sigaction(SIGXFSZ, {.sa_handler=&403c02}, [NULL]) → 0
SYS  24060          1'640'749 getcwd(0x473280, 1'024) → "/home/jart/blink"
SYS  24060          1'827'178 openat(AT_FDCWD, "/home/jart/blink/third_party/cosmo/2/a64l_test.com", 0, 0) → 3
SYS  24060          1'891'552 getfiledescriptorsize(3) → 299'008
SYS  24060          1'965'276 mmap(0, 299'008, PROT_READ, MAP_SHARED, 3, 0) → 0x100080040000 (458'752 bytes total)

When a Cosmopolitan binary issues a system call under Blink, it goes through Blink's OpSyscall. Guest binaries have no way of interfacing the host system without going through that opcode. If you want to learn more about how Cosmopolitan works, read https://justine.lol/ape.html and visit our Discord https://discord.gg/nMvdGvyz where I can answer fine grained questions in real time.

jart commented 1 year ago

Oh one more thing. My "Size Optimization Tricks" blog post has a section that deep dives into how APE platform detection works. https://justine.lol/sizetricks/#elf