Threads and signals

The threads, futexes and signals are a library of their own, scarletnx-gnuabi-kernel (src/gnuabi/kernel/, with kernel.h as its interface). It has no loader, file system or descriptor table. The runtime that uses it gives it the guest's memory (a checked read and write, and the host address of guest address 0), and a way to stop a thread's host call, end the process and leave a thread. This runtime and the Android bridge both use it.

The library also answers the system calls that need no loader, file system or descriptor table, with Linux's AArch64 behavior and the checked memory access: clock_gettime, clock_getres, gettimeofday, times, getrusage, sysinfo (the host's memory on Linux), uname, getrlimit, setrlimit, prlimit64, prctl (PR_SET_NAME, PR_GET_NAME, PR_SET_VMA, PR_SET_DUMPABLE and the other options of bionic and the C libraries; EINVAL for the rest, and for SVE, pointer authentication and tagged addresses), getcpu, getpid, getppid, the user and group ids, getgroups, setpriority, getpriority, the sched_* parameter calls, madvise (checked, then the runtime's), getrandom, membarrier (the host's barrier on every thread: Linux's membarrier, Windows' FlushProcessWriteBuffers; ENOSYS on macOS, FreeBSD and a Linux that refuses it) and rseq (ENOSYS). The Android bridge sends syscall() and the bionic functions that are one system call to it.

Threads. clone with CLONE_THREAD (it needs CLONE_SIGHAND and CLONE_VM) starts a host thread with the caller's registers, the new stack, CLONE_SETTLS, CLONE_PARENT_SETTID, CLONE_CHILD_SETTID and CLONE_CHILD_CLEARTID. Both tid words are written before either thread goes on. A thread's tid is its host thread's id on Linux, so tgkill from another process reaches it. At a thread's exit the runtime walks its robust list (set_robust_list), writes 0 to its clear_tid word and wakes one waiter there, as Linux does. exit ends one thread, and the last thread's exit ends the process. exit_group, a fault and a fatal signal end the process: the other threads leave their guest code at their next delivery point, and a thread blocked in a system call is not waited for. execve from any thread first stops all the others, and the thread takes the process's id. gettid, set_tid_address, tkill, tgkill, sched_yield, sched_getaffinity (the host's processors) and sched_setaffinity (accepted) are there.

Futexes. FUTEX_WAIT, WAKE, REQUEUE, CMP_REQUEUE, WAKE_OP, WAIT_BITSET and WAKE_BITSET, with the results Linux gives. A waiter reads the word and joins the list under one lock, so a wake after a change of the word finds it. A relative timeout is on the monotonic clock, and WAIT_BITSET's absolute one on the monotonic clock or, with FUTEX_CLOCK_REALTIME, on the realtime clock (converted once: a change of the realtime clock during the wait does not move it). FUTEX_PRIVATE_FLAG changes nothing: guest memory is never shared with another process. The PI operations return ENOSYS.

Signals. Each thread and the process have a queue: one entry of a standard signal, an entry for each real-time one sent (up to 64 in a queue, then EAGAIN). A signal to the process goes to its main thread if it does not block it, else to another thread that does not. The sender sets the target's poll word, wakes its wait in the runtime (futex, sleeps, rt_sigsuspend), ends its wait in the network stack (a wake pipe that its polls watch) and sends its host thread a host signal (SIGRTMIN+2), which stops another blocking host call with EINTR.

A signal is delivered only where all the guest's registers are in its CPU structure: at RtAPI.poll (each backward branch and dispatch of generated code) and at the end of each system call. Never in a host signal handler. The frame is Linux's rt_sigframe (src/common/runtime/linux_signal.c, which the Android bridge shares), written with the runtime's checked access; a frame that does not fit ends the process with SIGSEGV. The handler returns to sa_restorer with SA_RESTORER, else to the runtime's page at 0xfffff000, which holds mov x8, #139; svc #0 as the vDSO's __kernel_rt_sigreturn. rt_sigreturn restores the registers, the mask and the alternate stack from the frame, and the guest goes on at the restored pc through the dispatch table. SA_SIGINFO, SA_ONSTACK, SA_NODEFER, SA_RESETHAND and SA_RESTART work, and a handler's changes to its ucontext are the state it returns to.

A call that a signal stops follows Linux's restart rules: it restarts after a handler with SA_RESTART (ERESTARTSYS), and fails with EINTR without one. nanosleep, clock_nanosleep, poll, ppoll, select, pselect6, epoll_wait, rt_sigsuspend, rt_sigtimedwait and a futex wait with a timeout never restart after a handler. A call that a signal stops without a handler (the signal went to another thread) restarts. ppoll, pselect6 and epoll_pwait apply their mask while they wait, as rt_sigsuspend does. rt_sigsuspend, rt_sigtimedwait, rt_sigpending, sigaltstack, rt_sigqueueinfo and rt_tgsigqueueinfo are there.

With SIG_DFL the signal's default action runs: it is ignored, it stops the host process (SIGSTOP to itself), or it ends the host process with the same signal, so a parent sees what it would on Linux. A guest that handles a signal has the host relay it: the host handler writes the signal to a pipe, and a relay thread sends it to the process. A guest that ignores one has the host ignore it.

Limits:


Made from docs/platforms/gnuabi/README.md at 69237645142c. GPL-3.0-or-later.