2022-05-13 00:52:13 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2022-05-13 00:52:13 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2023-04-27 03:45:01 +00:00
|
|
|
#include "libc/sysv/consts/clone.h"
|
2022-11-08 18:09:47 +00:00
|
|
|
#include "libc/assert.h"
|
2023-06-04 01:32:33 +00:00
|
|
|
#include "libc/atomic.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/calls/calls.h"
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2023-08-21 09:28:24 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/calls/struct/ucontext-netbsd.internal.h"
|
2022-06-26 01:17:31 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2023-07-09 16:51:59 +00:00
|
|
|
#include "libc/calls/wincrash.internal.h"
|
2022-05-16 20:20:08 +00:00
|
|
|
#include "libc/dce.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/errno.h"
|
|
|
|
#include "libc/intrin/asan.internal.h"
|
2023-06-04 01:32:33 +00:00
|
|
|
#include "libc/intrin/atomic.h"
|
2022-11-08 18:09:47 +00:00
|
|
|
#include "libc/intrin/describeflags.internal.h"
|
2023-10-03 21:47:20 +00:00
|
|
|
#include "libc/intrin/ulock.h"
|
2023-07-09 16:51:59 +00:00
|
|
|
#include "libc/intrin/weaken.h"
|
2022-05-27 20:25:46 +00:00
|
|
|
#include "libc/limits.h"
|
2022-06-26 01:17:31 +00:00
|
|
|
#include "libc/macros.internal.h"
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
#include "libc/mem/alloca.h"
|
|
|
|
#include "libc/nt/enum/processcreationflags.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/nt/runtime.h"
|
2023-07-09 16:51:59 +00:00
|
|
|
#include "libc/nt/signals.h"
|
2022-09-16 21:02:06 +00:00
|
|
|
#include "libc/nt/synchronization.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/nt/thread.h"
|
|
|
|
#include "libc/nt/thunk/msabi.h"
|
2022-06-15 23:19:50 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
#include "libc/runtime/stack.h"
|
2023-06-04 01:32:33 +00:00
|
|
|
#include "libc/runtime/syslib.internal.h"
|
2022-11-08 18:09:47 +00:00
|
|
|
#include "libc/sock/internal.h"
|
2022-09-04 11:53:52 +00:00
|
|
|
#include "libc/stdalign.internal.h"
|
2022-06-09 03:01:28 +00:00
|
|
|
#include "libc/str/str.h"
|
2024-01-06 04:36:57 +00:00
|
|
|
#include "libc/sysv/consts/arch.h"
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
#include "libc/sysv/consts/clone.h"
|
2022-05-27 20:25:46 +00:00
|
|
|
#include "libc/sysv/consts/futex.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/sysv/consts/nr.h"
|
2022-05-16 20:20:08 +00:00
|
|
|
#include "libc/sysv/consts/nrlinux.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
#include "libc/thread/freebsd.internal.h"
|
2022-06-26 01:17:31 +00:00
|
|
|
#include "libc/thread/openbsd.internal.h"
|
2022-09-10 09:56:25 +00:00
|
|
|
#include "libc/thread/thread.h"
|
|
|
|
#include "libc/thread/tls.h"
|
2023-09-02 03:49:13 +00:00
|
|
|
#include "libc/thread/tls2.internal.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/thread/xnu.internal.h"
|
|
|
|
|
2023-06-04 08:57:10 +00:00
|
|
|
#define kMaxThreadIds 32768
|
|
|
|
#define kMinThreadId 262144
|
|
|
|
|
2024-01-06 04:36:57 +00:00
|
|
|
#define AMD64_SET_FSBASE 129
|
|
|
|
#define AMD64_SET_GSBASE 131
|
|
|
|
|
2022-05-13 00:52:13 +00:00
|
|
|
#define __NR_thr_new 455
|
|
|
|
#define __NR_clone_linux 56
|
|
|
|
#define __NR__lwp_create 309
|
|
|
|
#define __NR_getcontext_netbsd 307
|
|
|
|
#define __NR_bsdthread_create 0x02000168
|
|
|
|
#define __NR_thread_fast_set_cthread_self 0x03000003
|
|
|
|
#define PTHREAD_START_CUSTOM_XNU 0x01000000
|
|
|
|
#define LWP_DETACHED 0x00000040
|
|
|
|
#define LWP_SUSPENDED 0x00000080
|
|
|
|
|
2022-05-17 14:40:00 +00:00
|
|
|
struct CloneArgs {
|
2023-06-04 08:57:10 +00:00
|
|
|
_Alignas(16) union {
|
|
|
|
struct {
|
|
|
|
int tid;
|
|
|
|
int this;
|
|
|
|
};
|
2022-05-17 14:40:00 +00:00
|
|
|
uint32_t utid;
|
|
|
|
int64_t tid64;
|
|
|
|
};
|
2022-07-10 11:01:17 +00:00
|
|
|
int *ptid;
|
2022-05-17 11:14:28 +00:00
|
|
|
int *ctid;
|
2022-05-19 23:57:49 +00:00
|
|
|
int *ztid;
|
2022-05-17 14:40:00 +00:00
|
|
|
char *tls;
|
2022-07-10 11:01:17 +00:00
|
|
|
int (*func)(void *, int);
|
2022-05-17 11:14:28 +00:00
|
|
|
void *arg;
|
|
|
|
};
|
2022-05-16 20:20:08 +00:00
|
|
|
|
2024-01-06 04:36:57 +00:00
|
|
|
int sys_set_tls();
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
int __stack_call(void *, int, long, long, int (*)(void *, int), void *);
|
|
|
|
|
2023-06-04 08:57:10 +00:00
|
|
|
static struct CloneArgs *AllocateCloneArgs(char *stk, size_t stksz) {
|
|
|
|
return (struct CloneArgs *)(((uintptr_t)(stk + stksz) -
|
|
|
|
sizeof(struct CloneArgs)) &
|
|
|
|
-16);
|
|
|
|
}
|
|
|
|
|
2023-06-04 01:32:33 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2022-05-17 14:40:00 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// THE NEW TECHNOLOGY
|
|
|
|
|
2023-06-04 01:32:33 +00:00
|
|
|
__msabi extern typeof(TlsSetValue) *const __imp_TlsSetValue;
|
|
|
|
__msabi extern typeof(ExitThread) *const __imp_ExitThread;
|
|
|
|
__msabi extern typeof(WakeByAddressAll) *const __imp_WakeByAddressAll;
|
|
|
|
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
static textwindows dontinstrument wontreturn void //
|
|
|
|
WinThreadEntry(int rdi, // rcx
|
|
|
|
int rsi, // rdx
|
|
|
|
int rdx, // r8
|
|
|
|
struct CloneArgs *wt) { // r9
|
2022-05-13 00:52:13 +00:00
|
|
|
int rc;
|
2022-10-13 20:44:41 +00:00
|
|
|
if (wt->tls) __set_tls_win32(wt->tls);
|
2022-05-19 23:57:49 +00:00
|
|
|
*wt->ctid = wt->tid;
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
rc = __stack_call(wt->arg, wt->tid, 0, 0, wt->func, wt);
|
2022-05-19 23:57:49 +00:00
|
|
|
// we can now clear ctid directly since we're no longer using our own
|
|
|
|
// stack memory, which can now be safely free'd by the parent thread.
|
|
|
|
*wt->ztid = 0;
|
2022-09-16 21:02:06 +00:00
|
|
|
__imp_WakeByAddressAll(wt->ztid);
|
2022-05-19 23:57:49 +00:00
|
|
|
// since we didn't indirect this function through NT2SYSV() it's not
|
2022-11-09 11:58:57 +00:00
|
|
|
// safe to simply return, and as such, we need ExitThread().
|
2022-05-19 23:57:49 +00:00
|
|
|
__imp_ExitThread(rc);
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
__builtin_unreachable();
|
2022-05-13 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 18:09:47 +00:00
|
|
|
static textwindows errno_t CloneWindows(int (*func)(void *, int), char *stk,
|
|
|
|
size_t stksz, int flags, void *arg,
|
|
|
|
void *tls, int *ptid, int *ctid) {
|
2022-05-13 00:52:13 +00:00
|
|
|
int64_t h;
|
2022-05-17 14:40:00 +00:00
|
|
|
struct CloneArgs *wt;
|
2023-06-04 08:57:10 +00:00
|
|
|
wt = AllocateCloneArgs(stk, stksz);
|
2022-05-19 23:57:49 +00:00
|
|
|
wt->ctid = flags & CLONE_CHILD_SETTID ? ctid : &wt->tid;
|
|
|
|
wt->ztid = flags & CLONE_CHILD_CLEARTID ? ctid : &wt->tid;
|
2022-05-13 00:52:13 +00:00
|
|
|
wt->func = func;
|
|
|
|
wt->arg = arg;
|
2022-05-19 23:57:49 +00:00
|
|
|
wt->tls = flags & CLONE_SETTLS ? tls : 0;
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
if ((h = CreateThread(&kNtIsInheritable, 65536, (void *)WinThreadEntry, wt,
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
kNtStackSizeParamIsAReservation, &wt->utid))) {
|
|
|
|
if (flags & CLONE_SETTLS) {
|
|
|
|
struct CosmoTib *tib = tls;
|
|
|
|
tib->tib_syshand = h;
|
|
|
|
}
|
2022-11-08 18:09:47 +00:00
|
|
|
if (flags & CLONE_PARENT_SETTID) {
|
|
|
|
*ptid = wt->tid;
|
|
|
|
}
|
|
|
|
return 0;
|
2022-05-13 00:52:13 +00:00
|
|
|
} else {
|
2022-11-08 18:09:47 +00:00
|
|
|
return __dos2errno(GetLastError());
|
2022-05-13 00:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 14:40:00 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// XNU'S NOT UNIX
|
|
|
|
|
2023-06-04 01:32:33 +00:00
|
|
|
void XnuThreadThunk(void *pthread, // rdi x0
|
|
|
|
int machport, // rsi x1
|
|
|
|
void *(*func)(void *), // rdx x2
|
|
|
|
void *arg, // rcx x3
|
|
|
|
intptr_t *stack, // r8 x4
|
|
|
|
unsigned xnuflags); // r9 x5
|
2022-05-27 20:25:46 +00:00
|
|
|
asm("XnuThreadThunk:\n\t"
|
2022-05-13 00:52:13 +00:00
|
|
|
"xor\t%ebp,%ebp\n\t"
|
|
|
|
"mov\t%r8,%rsp\n\t"
|
2022-05-17 14:40:00 +00:00
|
|
|
"push\t%rax\n\t"
|
2022-05-13 00:52:13 +00:00
|
|
|
"jmp\tXnuThreadMain\n\t"
|
|
|
|
".size\tXnuThreadThunk,.-XnuThreadThunk");
|
2023-07-10 11:29:46 +00:00
|
|
|
__attribute__((__used__))
|
2022-05-13 00:52:13 +00:00
|
|
|
|
|
|
|
static wontreturn void
|
2022-07-10 11:01:17 +00:00
|
|
|
XnuThreadMain(void *pthread, // rdi
|
|
|
|
int tid, // rsi
|
|
|
|
int (*func)(void *arg, int tid), // rdx
|
|
|
|
void *arg, // rcx
|
|
|
|
struct CloneArgs *wt, // r8
|
|
|
|
unsigned xnuflags) { // r9
|
2022-05-17 14:40:00 +00:00
|
|
|
int ax;
|
|
|
|
wt->tid = tid;
|
2022-07-10 11:01:17 +00:00
|
|
|
*wt->ctid = tid;
|
2022-11-08 18:09:47 +00:00
|
|
|
*wt->ptid = tid;
|
2022-07-10 11:01:17 +00:00
|
|
|
|
2022-05-19 23:57:49 +00:00
|
|
|
if (wt->tls) {
|
2022-05-13 00:52:13 +00:00
|
|
|
// XNU uses the same 0x30 offset as the WIN32 TIB x64. They told the
|
|
|
|
// Go team at Google that they Apply stands by our ability to use it
|
|
|
|
// https://github.com/golang/go/issues/23617#issuecomment-376662373
|
|
|
|
asm volatile("syscall"
|
2022-05-17 14:40:00 +00:00
|
|
|
: "=a"(ax)
|
|
|
|
: "0"(__NR_thread_fast_set_cthread_self), "D"(wt->tls - 0x30)
|
2022-09-09 11:07:08 +00:00
|
|
|
: "rcx", "rdx", "r8", "r9", "r10", "r11", "memory", "cc");
|
2022-05-13 00:52:13 +00:00
|
|
|
}
|
2022-07-10 11:01:17 +00:00
|
|
|
|
|
|
|
func(arg, tid);
|
|
|
|
|
2022-05-19 23:57:49 +00:00
|
|
|
// we no longer use the stack after this point
|
|
|
|
// %rax = int bsdthread_terminate(%rdi = void *stackaddr,
|
|
|
|
// %rsi = size_t freesize,
|
|
|
|
// %rdx = uint32_t port,
|
|
|
|
// %r10 = uint32_t sem);
|
2023-10-03 21:47:20 +00:00
|
|
|
asm volatile("movl\t$0,(%%rsi)\n\t" // *wt->ztid = 0
|
|
|
|
"mov\t$0x101,%%edi\n\t" // wake all
|
|
|
|
"xor\t%%edx,%%edx\n\t" // wake_value
|
|
|
|
"mov\t$0x02000204,%%eax\n\t" // ulock_wake()
|
|
|
|
"syscall\n\t" //
|
|
|
|
"xor\t%%edi,%%edi\n\t" // freeaddr
|
|
|
|
"xor\t%%esi,%%esi\n\t" // freesize
|
|
|
|
"xor\t%%edx,%%edx\n\t" // kport
|
|
|
|
"xor\t%%r10d,%%r10d\n\t" // joinsem
|
|
|
|
"mov\t$0x02000169,%%eax\n\t" // bsdthread_terminate()
|
|
|
|
"syscall"
|
|
|
|
: /* no outputs */
|
|
|
|
: "S"(wt->ztid)
|
|
|
|
: "rax", "rcx", "r10", "r11", "memory");
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
__builtin_unreachable();
|
2022-05-13 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 18:09:47 +00:00
|
|
|
static errno_t CloneXnu(int (*fn)(void *), char *stk, size_t stksz, int flags,
|
|
|
|
void *arg, void *tls, int *ptid, int *ctid) {
|
2022-05-13 00:52:13 +00:00
|
|
|
static bool once;
|
2022-05-17 14:40:00 +00:00
|
|
|
struct CloneArgs *wt;
|
2022-05-13 00:52:13 +00:00
|
|
|
if (!once) {
|
2023-07-26 20:54:49 +00:00
|
|
|
npassert(sys_bsdthread_register(XnuThreadThunk, 0, 0, 0, 0, 0, 0) != -1);
|
2022-05-13 00:52:13 +00:00
|
|
|
once = true;
|
|
|
|
}
|
2023-06-04 08:57:10 +00:00
|
|
|
wt = AllocateCloneArgs(stk, stksz);
|
2022-07-10 11:01:17 +00:00
|
|
|
wt->ptid = flags & CLONE_PARENT_SETTID ? ptid : &wt->tid;
|
2022-05-19 23:57:49 +00:00
|
|
|
wt->ctid = flags & CLONE_CHILD_SETTID ? ctid : &wt->tid;
|
|
|
|
wt->ztid = flags & CLONE_CHILD_CLEARTID ? ctid : &wt->tid;
|
|
|
|
wt->tls = flags & CLONE_SETTLS ? tls : 0;
|
2022-11-08 18:09:47 +00:00
|
|
|
return sys_clone_xnu(fn, arg, wt, 0, PTHREAD_START_CUSTOM_XNU);
|
2022-05-13 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 14:40:00 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// OPEN BESIYATA DISHMAYA
|
2022-05-13 00:52:13 +00:00
|
|
|
|
2022-07-11 12:55:17 +00:00
|
|
|
// we can't use address sanitizer because:
|
|
|
|
// 1. __asan_handle_no_return wipes stack [todo?]
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
static wontreturn void OpenbsdThreadMain(void *p) {
|
2022-06-26 01:17:31 +00:00
|
|
|
struct CloneArgs *wt = p;
|
2022-07-10 11:01:17 +00:00
|
|
|
*wt->ctid = wt->tid;
|
|
|
|
wt->func(wt->arg, wt->tid);
|
2022-10-09 06:54:05 +00:00
|
|
|
asm volatile("mov\t%2,%%rsp\n\t" // so syscall can validate stack exists
|
|
|
|
"movl\t$0,(%%rdi)\n\t" // *wt->ztid = 0 (old stack now free'd)
|
|
|
|
"syscall\n\t" // futex(int*, op, val) will wake wait0
|
|
|
|
"xor\t%%edi,%%edi\n\t" // so kernel doesn't write to old stack
|
|
|
|
"mov\t$302,%%eax\n\t" // __threxit(int *notdead) doesn't wake
|
2022-07-18 02:59:49 +00:00
|
|
|
"syscall"
|
2022-05-19 23:57:49 +00:00
|
|
|
: "=m"(*wt->ztid)
|
2022-10-09 06:54:05 +00:00
|
|
|
: "a"(83), "m"(__oldstack), "D"(wt->ztid),
|
|
|
|
"S"(2 /* FUTEX_WAKE */), "d"(INT_MAX)
|
2022-05-19 23:57:49 +00:00
|
|
|
: "rcx", "r11", "memory");
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
__builtin_unreachable();
|
2022-05-13 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 18:09:47 +00:00
|
|
|
static errno_t CloneOpenbsd(int (*func)(void *, int), char *stk, size_t stksz,
|
|
|
|
int flags, void *arg, void *tls, int *ptid,
|
|
|
|
int *ctid) {
|
|
|
|
int rc;
|
2022-06-26 01:17:31 +00:00
|
|
|
intptr_t sp;
|
|
|
|
struct __tfork *tf;
|
2022-05-17 14:40:00 +00:00
|
|
|
struct CloneArgs *wt;
|
2022-06-26 01:17:31 +00:00
|
|
|
sp = (intptr_t)stk + stksz;
|
|
|
|
sp -= sizeof(struct __tfork);
|
|
|
|
sp &= -alignof(struct __tfork);
|
|
|
|
tf = (struct __tfork *)sp;
|
|
|
|
sp -= sizeof(struct CloneArgs);
|
2023-06-04 15:19:45 +00:00
|
|
|
sp &= -alignof(struct CloneArgs);
|
2022-06-26 01:17:31 +00:00
|
|
|
wt = (struct CloneArgs *)sp;
|
2022-07-10 11:01:17 +00:00
|
|
|
wt->ctid = flags & CLONE_CHILD_SETTID ? ctid : &wt->tid;
|
2022-05-19 23:57:49 +00:00
|
|
|
wt->ztid = flags & CLONE_CHILD_CLEARTID ? ctid : &wt->tid;
|
2022-05-17 14:40:00 +00:00
|
|
|
wt->arg = arg;
|
2022-06-26 01:17:31 +00:00
|
|
|
wt->func = func;
|
|
|
|
tf->tf_stack = (char *)wt - 8;
|
|
|
|
tf->tf_tcb = flags & CLONE_SETTLS ? tls : 0;
|
2022-07-10 11:01:17 +00:00
|
|
|
tf->tf_tid = &wt->tid;
|
2022-11-08 18:09:47 +00:00
|
|
|
if ((rc = __tfork_thread(tf, sizeof(*tf), OpenbsdThreadMain, wt)) >= 0) {
|
2023-07-26 20:54:49 +00:00
|
|
|
npassert(rc);
|
2022-11-08 18:09:47 +00:00
|
|
|
if (flags & CLONE_PARENT_SETTID) {
|
|
|
|
*ptid = rc;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -rc;
|
2022-05-13 00:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 14:40:00 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// NET BESIYATA DISHMAYA
|
|
|
|
|
2022-07-10 11:01:17 +00:00
|
|
|
static wontreturn void NetbsdThreadMain(void *arg, // rdi
|
|
|
|
int (*func)(void *, int), // rsi
|
|
|
|
int *tid, // rdx
|
|
|
|
int *ctid, // rcx
|
2022-11-08 18:09:47 +00:00
|
|
|
int *ztid) { // r9
|
2022-05-19 23:57:49 +00:00
|
|
|
int ax, dx;
|
2022-08-16 02:52:00 +00:00
|
|
|
// TODO(jart): Why are we seeing flakes where *tid is zero?
|
|
|
|
// ax = *tid;
|
|
|
|
ax = sys_gettid();
|
2022-07-10 11:01:17 +00:00
|
|
|
*ctid = ax;
|
|
|
|
func(arg, ax);
|
2022-05-19 23:57:49 +00:00
|
|
|
// we no longer use the stack after this point
|
|
|
|
// %eax = int __lwp_exit(void);
|
|
|
|
asm volatile("movl\t$0,%2\n\t" // *wt->ztid = 0
|
2023-06-04 08:57:10 +00:00
|
|
|
"syscall" // __lwp_exit()
|
2022-05-19 23:57:49 +00:00
|
|
|
: "=a"(ax), "=d"(dx), "=m"(*ztid)
|
|
|
|
: "0"(310)
|
|
|
|
: "rcx", "r11", "memory");
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
__builtin_unreachable();
|
2022-05-13 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 11:01:17 +00:00
|
|
|
static int CloneNetbsd(int (*func)(void *, int), char *stk, size_t stksz,
|
2022-07-18 10:33:32 +00:00
|
|
|
int flags, void *arg, void *tls, int *ptid, int *ctid) {
|
2022-05-13 00:52:13 +00:00
|
|
|
// NetBSD has its own clone() and it works, but it's technically a
|
2022-06-19 08:13:03 +00:00
|
|
|
// second-class API, intended to help Linux folks migrate to this.
|
2022-05-13 00:52:13 +00:00
|
|
|
bool failed;
|
|
|
|
int ax, *tid;
|
|
|
|
intptr_t dx, sp;
|
|
|
|
static bool once;
|
2022-06-19 08:13:03 +00:00
|
|
|
struct ucontext_netbsd *ctx;
|
2022-05-13 00:52:13 +00:00
|
|
|
static struct ucontext_netbsd netbsd_clone_template;
|
2022-05-19 23:57:49 +00:00
|
|
|
|
|
|
|
// memoize arbitrary valid processor state structure
|
2022-05-13 00:52:13 +00:00
|
|
|
if (!once) {
|
|
|
|
asm volatile(CFLAG_ASM("syscall")
|
|
|
|
: CFLAG_CONSTRAINT(failed), "=a"(ax)
|
|
|
|
: "1"(__NR_getcontext_netbsd), "D"(&netbsd_clone_template)
|
2022-09-09 11:07:08 +00:00
|
|
|
: "rcx", "rdx", "r8", "r9", "r10", "r11", "memory");
|
2023-07-26 20:54:49 +00:00
|
|
|
npassert(!failed);
|
2022-05-13 00:52:13 +00:00
|
|
|
once = true;
|
|
|
|
}
|
|
|
|
sp = (intptr_t)(stk + stksz);
|
2022-06-19 08:13:03 +00:00
|
|
|
|
2022-07-10 11:01:17 +00:00
|
|
|
// allocate memory for tid
|
2022-05-13 00:52:13 +00:00
|
|
|
sp -= sizeof(int);
|
2022-05-19 23:57:49 +00:00
|
|
|
sp = sp & -alignof(int);
|
2022-05-13 00:52:13 +00:00
|
|
|
tid = (int *)sp;
|
2022-08-16 02:52:00 +00:00
|
|
|
*tid = 0;
|
2022-06-19 08:13:03 +00:00
|
|
|
|
|
|
|
// align the stack
|
2022-05-19 23:57:49 +00:00
|
|
|
sp = sp & -16;
|
2022-06-19 08:13:03 +00:00
|
|
|
|
|
|
|
// simulate call to misalign stack and ensure backtrace looks good
|
2022-05-19 23:57:49 +00:00
|
|
|
sp -= 8;
|
2022-06-19 08:13:03 +00:00
|
|
|
*(intptr_t *)sp = (intptr_t)CloneNetbsd + 1;
|
|
|
|
|
|
|
|
// place the giant 784 byte ucontext structure in the red zone!
|
|
|
|
// it only has to live long enough for the thread to come alive
|
|
|
|
ctx = (struct ucontext_netbsd *)((sp - sizeof(struct ucontext_netbsd)) &
|
|
|
|
-alignof(struct ucontext_netbsd));
|
|
|
|
|
2022-05-19 23:57:49 +00:00
|
|
|
// pass parameters in process state
|
2022-06-19 08:13:03 +00:00
|
|
|
memcpy(ctx, &netbsd_clone_template, sizeof(*ctx));
|
|
|
|
ctx->uc_link = 0;
|
|
|
|
ctx->uc_mcontext.rbp = 0;
|
|
|
|
ctx->uc_mcontext.rsp = sp;
|
|
|
|
ctx->uc_mcontext.rip = (intptr_t)NetbsdThreadMain;
|
|
|
|
ctx->uc_mcontext.rdi = (intptr_t)arg;
|
|
|
|
ctx->uc_mcontext.rsi = (intptr_t)func;
|
|
|
|
ctx->uc_mcontext.rdx = (intptr_t)tid;
|
|
|
|
ctx->uc_mcontext.rcx = (intptr_t)(flags & CLONE_CHILD_SETTID ? ctid : tid);
|
|
|
|
ctx->uc_mcontext.r8 = (intptr_t)(flags & CLONE_CHILD_CLEARTID ? ctid : tid);
|
|
|
|
ctx->uc_flags |= _UC_STACK;
|
|
|
|
ctx->uc_stack.ss_sp = stk;
|
|
|
|
ctx->uc_stack.ss_size = stksz;
|
|
|
|
ctx->uc_stack.ss_flags = 0;
|
2022-05-13 00:52:13 +00:00
|
|
|
if (flags & CLONE_SETTLS) {
|
2022-06-19 08:13:03 +00:00
|
|
|
ctx->uc_flags |= _UC_TLSBASE;
|
|
|
|
ctx->uc_mcontext._mc_tlsbase = (intptr_t)tls;
|
2022-05-13 00:52:13 +00:00
|
|
|
}
|
2022-05-19 23:57:49 +00:00
|
|
|
|
|
|
|
// perform the system call
|
2022-05-13 00:52:13 +00:00
|
|
|
asm volatile(CFLAG_ASM("syscall")
|
|
|
|
: CFLAG_CONSTRAINT(failed), "=a"(ax), "=d"(dx)
|
2022-06-19 08:13:03 +00:00
|
|
|
: "1"(__NR__lwp_create), "D"(ctx), "S"(LWP_DETACHED), "2"(tid)
|
2022-09-09 11:07:08 +00:00
|
|
|
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
2022-05-13 00:52:13 +00:00
|
|
|
if (!failed) {
|
2023-07-26 20:54:49 +00:00
|
|
|
npassert(*tid);
|
2022-11-08 18:09:47 +00:00
|
|
|
if (flags & CLONE_PARENT_SETTID) {
|
|
|
|
*ptid = *tid;
|
|
|
|
}
|
|
|
|
return 0;
|
2022-05-13 00:52:13 +00:00
|
|
|
} else {
|
2022-11-08 18:09:47 +00:00
|
|
|
return ax;
|
2022-05-13 00:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 08:56:56 +00:00
|
|
|
#endif /* __x86_64__ */
|
|
|
|
|
2023-12-30 04:11:23 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// FREE BESIYATA DISHMAYA
|
|
|
|
|
|
|
|
static wontreturn void FreebsdThreadMain(void *p) {
|
|
|
|
struct CloneArgs *wt = p;
|
|
|
|
#ifdef __aarch64__
|
|
|
|
asm volatile("mov\tx28,%0" : /* no outputs */ : "r"(wt->tls));
|
2024-01-06 04:36:57 +00:00
|
|
|
#elif defined(__x86_64__)
|
|
|
|
if (__tls_morphed) {
|
|
|
|
sys_set_tls(AMD64_SET_GSBASE, wt->tls);
|
|
|
|
}
|
2023-12-30 04:11:23 +00:00
|
|
|
#endif
|
|
|
|
*wt->ctid = wt->tid;
|
|
|
|
wt->func(wt->arg, wt->tid);
|
|
|
|
// we no longer use the stack after this point
|
|
|
|
// void thr_exit(%rdi = long *state);
|
|
|
|
#ifdef __x86_64__
|
|
|
|
asm volatile("movl\t$0,%0\n\t" // *wt->ztid = 0
|
|
|
|
"syscall\n\t" // _umtx_op(wt->ztid, WAKE, INT_MAX)
|
|
|
|
"movl\t$431,%%eax\n\t" // thr_exit(long *nonzeroes_and_wake)
|
|
|
|
"xor\t%%edi,%%edi\n\t" // sad we can't use this free futex op
|
|
|
|
"syscall\n\t" // thr_exit() fails if thread is orphaned
|
|
|
|
"movl\t$1,%%eax\n\t" // _exit()
|
|
|
|
"syscall" //
|
|
|
|
: "=m"(*wt->ztid)
|
|
|
|
: "a"(454), "D"(wt->ztid), "S"(UMTX_OP_WAKE), "d"(INT_MAX)
|
|
|
|
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
register long x0 asm("x0") = (long)wt->ztid;
|
|
|
|
register long x1 asm("x1") = UMTX_OP_WAKE;
|
|
|
|
register long x2 asm("x2") = INT_MAX;
|
|
|
|
register long x8 asm("x8") = 454; // _umtx_op
|
|
|
|
asm volatile("str\twzr,%0\n\t" // *wt->ztid = 0
|
|
|
|
"svc\t0\n\t" // _umtx_op(wt->ztid, WAKE, INT_MAX)
|
|
|
|
"mov\tx0,#0\n\t" // arg0 = 0
|
|
|
|
"mov\tx8,#431\n\t" // thr_exit
|
|
|
|
"svc\t0\n\t" // thr_exit(long *nonzeroes_and_wake = 0)
|
|
|
|
"mov\tx8,#1\n\t" // _exit
|
|
|
|
"svc\t0" // _exit(long *nonzeroes_and_wake = 0)
|
|
|
|
: "=m"(*wt->ztid)
|
|
|
|
: "r"(x0), "r"(x1), "r"(x2), "r"(x8));
|
|
|
|
#else
|
|
|
|
#error "unsupported architecture"
|
|
|
|
#endif
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
static errno_t CloneFreebsd(int (*func)(void *, int), char *stk, size_t stksz,
|
|
|
|
int flags, void *arg, void *tls, int *ptid,
|
|
|
|
int *ctid) {
|
|
|
|
int64_t tid;
|
|
|
|
struct CloneArgs *wt;
|
|
|
|
wt = AllocateCloneArgs(stk, stksz);
|
|
|
|
wt->ctid = flags & CLONE_CHILD_SETTID ? ctid : &wt->tid;
|
|
|
|
wt->ztid = flags & CLONE_CHILD_CLEARTID ? ctid : &wt->tid;
|
|
|
|
wt->tls = tls;
|
|
|
|
wt->func = func;
|
|
|
|
wt->arg = arg;
|
|
|
|
struct thr_param params = {
|
|
|
|
.start_func = FreebsdThreadMain,
|
|
|
|
.arg = wt,
|
|
|
|
.stack_base = stk,
|
|
|
|
.stack_size = (uintptr_t)wt - (uintptr_t)stk,
|
|
|
|
.tls_base = flags & CLONE_SETTLS ? tls : 0,
|
|
|
|
.tls_size = 64,
|
|
|
|
.child_tid = &wt->tid64,
|
|
|
|
.parent_tid = &tid,
|
|
|
|
};
|
|
|
|
#ifdef __x86_64__
|
|
|
|
int ax;
|
|
|
|
bool failed;
|
|
|
|
asm volatile(CFLAG_ASM("syscall")
|
|
|
|
: CFLAG_CONSTRAINT(failed), "=a"(ax)
|
|
|
|
: "1"(__NR_thr_new), "D"(¶ms), "S"(sizeof(params))
|
|
|
|
: "rcx", "rdx", "r8", "r9", "r10", "r11", "memory");
|
|
|
|
if (failed) return ax;
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
register long x0 asm("x0") = (long)¶ms;
|
|
|
|
register long x1 asm("x1") = sizeof(params);
|
|
|
|
register int x8 asm("x8") = 0x1c7; // thr_new
|
|
|
|
asm volatile("svc\t0" : "+r"(x0) : "r"(x1), "r"(x8) : "memory");
|
|
|
|
if (x0) return x0;
|
|
|
|
#else
|
|
|
|
#error "unsupported architecture"
|
|
|
|
#endif
|
|
|
|
if (flags & CLONE_PARENT_SETTID) *ptid = tid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-04 01:32:33 +00:00
|
|
|
#ifdef __aarch64__
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// APPLE SILICON
|
|
|
|
|
|
|
|
static void *SiliconThreadMain(void *arg) {
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
struct CloneArgs *wt = arg;
|
|
|
|
asm volatile("mov\tx28,%0" : /* no outputs */ : "r"(wt->tls));
|
2023-06-04 08:57:10 +00:00
|
|
|
*wt->ctid = wt->this;
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
__stack_call(wt->arg, wt->this, 0, 0, wt->func, wt);
|
2023-06-04 01:32:33 +00:00
|
|
|
*wt->ztid = 0;
|
2023-10-03 21:47:20 +00:00
|
|
|
ulock_wake(UL_COMPARE_AND_WAIT | ULF_WAKE_ALL, wt->ztid, 0);
|
2023-06-04 01:32:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static errno_t CloneSilicon(int (*fn)(void *, int), char *stk, size_t stksz,
|
|
|
|
int flags, void *arg, void *tls, int *ptid,
|
|
|
|
int *ctid) {
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
void *attr;
|
2023-06-04 08:57:10 +00:00
|
|
|
errno_t res;
|
|
|
|
unsigned tid;
|
2023-06-04 01:32:33 +00:00
|
|
|
pthread_t th;
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
size_t babystack;
|
2023-06-04 01:32:33 +00:00
|
|
|
struct CloneArgs *wt;
|
2023-06-04 08:57:10 +00:00
|
|
|
static atomic_uint tids;
|
|
|
|
wt = AllocateCloneArgs(stk, stksz);
|
|
|
|
tid = atomic_fetch_add_explicit(&tids, 1, memory_order_acq_rel);
|
|
|
|
wt->this = tid = (tid & (kMaxThreadIds - 1)) + kMinThreadId;
|
2023-06-04 01:32:33 +00:00
|
|
|
wt->ctid = flags & CLONE_CHILD_SETTID ? ctid : &wt->tid;
|
|
|
|
wt->ztid = flags & CLONE_CHILD_CLEARTID ? ctid : &wt->tid;
|
|
|
|
wt->tls = flags & CLONE_SETTLS ? tls : 0;
|
|
|
|
wt->func = fn;
|
|
|
|
wt->arg = arg;
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
babystack = __syslib->__pthread_stack_min;
|
|
|
|
#pragma GCC push_options
|
|
|
|
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
|
|
|
attr = alloca(__syslib->__sizeof_pthread_attr_t);
|
|
|
|
#pragma GCC pop_options
|
|
|
|
unassert(!__syslib->__pthread_attr_init(attr));
|
|
|
|
unassert(!__syslib->__pthread_attr_setguardsize(attr, 0));
|
|
|
|
unassert(!__syslib->__pthread_attr_setstacksize(attr, babystack));
|
|
|
|
if (!(res = __syslib->__pthread_create(&th, attr, SiliconThreadMain, wt)) &&
|
2023-06-04 08:57:10 +00:00
|
|
|
(flags & CLONE_PARENT_SETTID)) {
|
|
|
|
*ptid = tid;
|
2023-09-11 20:51:37 +00:00
|
|
|
if (flags & CLONE_SETTLS) {
|
|
|
|
struct CosmoTib *tib = tls;
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
tib[-1].tib_syshand = th;
|
2023-09-11 20:51:37 +00:00
|
|
|
}
|
2023-06-04 08:57:10 +00:00
|
|
|
}
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
unassert(!__syslib->__pthread_attr_destroy(attr));
|
2023-06-04 08:57:10 +00:00
|
|
|
return res;
|
2023-06-04 01:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __aarch64__ */
|
|
|
|
|
2022-05-17 14:40:00 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// GNU/SYSTEMD
|
|
|
|
|
2024-01-06 04:36:57 +00:00
|
|
|
struct LinuxCloneArgs {
|
|
|
|
int (*func)(void *, int);
|
|
|
|
void *arg;
|
|
|
|
char *tls;
|
|
|
|
int ctid;
|
|
|
|
};
|
|
|
|
|
2022-07-10 11:01:17 +00:00
|
|
|
int sys_clone_linux(int flags, // rdi
|
|
|
|
long sp, // rsi
|
|
|
|
int *ptid, // rdx
|
|
|
|
int *ctid, // rcx
|
|
|
|
void *tls, // r8
|
|
|
|
void *func, // r9
|
|
|
|
void *arg); // 8(rsp)
|
|
|
|
|
2024-01-06 04:36:57 +00:00
|
|
|
static int LinuxThreadEntry(void *arg, int tid) {
|
|
|
|
struct LinuxCloneArgs *wt = arg;
|
|
|
|
sys_set_tls(ARCH_SET_GS, wt->tls);
|
|
|
|
return wt->func(wt->arg, tid);
|
|
|
|
}
|
|
|
|
|
2022-11-08 18:09:47 +00:00
|
|
|
static int CloneLinux(int (*func)(void *arg, int rc), char *stk, size_t stksz,
|
2022-07-18 10:33:32 +00:00
|
|
|
int flags, void *arg, void *tls, int *ptid, int *ctid) {
|
2022-11-08 18:09:47 +00:00
|
|
|
int rc;
|
2022-07-10 11:01:17 +00:00
|
|
|
long sp;
|
2024-01-06 04:36:57 +00:00
|
|
|
struct LinuxCloneArgs *wt;
|
2022-07-10 11:01:17 +00:00
|
|
|
sp = (intptr_t)(stk + stksz);
|
2024-01-06 04:36:57 +00:00
|
|
|
sp -= sizeof(struct LinuxCloneArgs);
|
2023-05-10 05:41:57 +00:00
|
|
|
// align the stack
|
|
|
|
#ifdef __aarch64__
|
|
|
|
sp = sp & -128; // for kernel 4.6 and earlier
|
|
|
|
#else
|
|
|
|
sp = sp & -16;
|
|
|
|
#endif
|
2024-01-06 04:36:57 +00:00
|
|
|
wt = (struct LinuxCloneArgs *)sp;
|
|
|
|
#ifdef __x86_64__
|
|
|
|
if ((flags & CLONE_SETTLS) && __tls_morphed) {
|
|
|
|
flags &= ~CLONE_SETTLS;
|
|
|
|
wt->arg = arg;
|
|
|
|
wt->tls = tls;
|
|
|
|
wt->func = func;
|
|
|
|
func = LinuxThreadEntry;
|
|
|
|
arg = wt;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (~flags & CLONE_CHILD_SETTID) {
|
|
|
|
flags |= CLONE_CHILD_SETTID;
|
|
|
|
ctid = &wt->ctid;
|
|
|
|
}
|
2022-11-08 18:09:47 +00:00
|
|
|
if ((rc = sys_clone_linux(flags, sp, ptid, ctid, tls, func, arg)) >= 0) {
|
|
|
|
// clone() is documented as setting ptid before return
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -rc;
|
|
|
|
}
|
2022-07-10 11:01:17 +00:00
|
|
|
}
|
2022-05-13 00:52:13 +00:00
|
|
|
|
2022-05-17 14:40:00 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// COSMOPOLITAN
|
|
|
|
|
2022-05-13 00:52:13 +00:00
|
|
|
/**
|
2022-10-05 06:32:16 +00:00
|
|
|
* Creates thread without malloc being linked.
|
2022-09-13 06:10:38 +00:00
|
|
|
*
|
2023-06-15 00:02:57 +00:00
|
|
|
* If you use clone() you're on your own. Example:
|
2022-09-13 06:10:38 +00:00
|
|
|
*
|
2022-10-05 06:32:16 +00:00
|
|
|
* int worker(void *arg) { return 0; }
|
2022-09-13 21:57:38 +00:00
|
|
|
* struct CosmoTib tib = {.tib_self = &tib, .tib_tid = -1};
|
2022-11-08 18:09:47 +00:00
|
|
|
* atomic_int tid;
|
2023-07-02 10:50:29 +00:00
|
|
|
* char *stk = NewCosmoStack();
|
2022-11-08 18:09:47 +00:00
|
|
|
* clone(worker, stk, GetStackSize() - 16,
|
|
|
|
* CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES |
|
2023-05-10 06:35:10 +00:00
|
|
|
* CLONE_SYSVSEM | CLONE_SIGHAND | CLONE_PARENT_SETTID |
|
|
|
|
* CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | CLONE_SETTLS,
|
2022-11-08 18:09:47 +00:00
|
|
|
* arg, &tid, &tib, &tib.tib_tid);
|
|
|
|
* while (atomic_load(&tid) == 0) sched_yield();
|
|
|
|
* // thread is known
|
|
|
|
* while (atomic_load(&tib.tib_tid) < 0) sched_yield();
|
|
|
|
* // thread is running
|
|
|
|
* while (atomic_load(&tib.tib_tid) > 0) sched_yield();
|
|
|
|
* // thread has terminated
|
2023-07-02 10:50:29 +00:00
|
|
|
* FreeCosmoStack(stk);
|
2022-05-13 00:52:13 +00:00
|
|
|
*
|
|
|
|
* Threads are created in a detached manner. They currently can't be
|
2022-05-17 11:14:28 +00:00
|
|
|
* synchronized using wait() or posix signals. Threads created by this
|
2022-05-13 00:52:13 +00:00
|
|
|
* function should be synchronized using shared memory operations.
|
|
|
|
*
|
|
|
|
* Any memory that's required by this system call wrapper is allocated
|
2022-05-19 23:57:49 +00:00
|
|
|
* to the top of your stack. This shouldn't be more than 128 bytes.
|
2022-05-13 00:52:13 +00:00
|
|
|
*
|
2022-05-17 11:14:28 +00:00
|
|
|
* Your function is called from within the stack you specify. A return
|
|
|
|
* address is pushed onto your stack, that causes returning to jump to
|
|
|
|
* _Exit1() which terminates the thread. Even though the callback says
|
|
|
|
* it supports a return code, that'll only work on Linux and Windows.
|
|
|
|
*
|
2022-05-13 00:52:13 +00:00
|
|
|
* This function follows the same ABI convention as the Linux userspace
|
|
|
|
* libraries, with a few small changes. The varargs has been removed to
|
|
|
|
* help prevent broken code, and the stack size and tls size parameters
|
|
|
|
* are introduced for compatibility with FreeBSD.
|
|
|
|
*
|
2022-05-17 11:14:28 +00:00
|
|
|
* To keep this system call lightweight, only the thread creation use
|
|
|
|
* case is polyfilled across platforms. For example, if you want fork
|
|
|
|
* that works on OpenBSD for example, don't do it with clone(SIGCHLD)
|
|
|
|
* and please just call fork(). Even if you do that on Linux, it will
|
|
|
|
* effectively work around libc features like atfork(), so that means
|
|
|
|
* other calls like getpid() may return incorrect values.
|
|
|
|
*
|
2022-05-28 12:50:01 +00:00
|
|
|
* @param func is your callback function, which this wrapper requires
|
2022-07-10 11:01:17 +00:00
|
|
|
* not be null, otherwise EINVAL is raised. It is passed two args
|
|
|
|
* within the child thread: (1) the caller-supplied `arg` and (2)
|
|
|
|
* the new tid is always passed in the second arg for convenience
|
|
|
|
*
|
2022-05-13 00:52:13 +00:00
|
|
|
* @param stk points to the bottom of a caller allocated stack, which
|
2022-05-17 11:14:28 +00:00
|
|
|
* must be allocated via mmap() using the MAP_STACK flag, or else
|
|
|
|
* you won't get optimal performance and it won't work on OpenBSD
|
2022-07-10 11:01:17 +00:00
|
|
|
*
|
2022-05-17 11:14:28 +00:00
|
|
|
* @param stksz is the size of that stack in bytes, we recommend that
|
|
|
|
* that this be set to GetStackSize() or else memory safety tools
|
|
|
|
* like kprintf() can't do as good and quick of a job; this value
|
|
|
|
* must be 16-aligned plus it must be at least 4192 bytes in size
|
|
|
|
* and it's advised to have the bottom-most page, be a guard page
|
2022-07-10 11:01:17 +00:00
|
|
|
*
|
|
|
|
* @param flags which SHOULD always have all of these flags:
|
|
|
|
*
|
|
|
|
* - `CLONE_THREAD`
|
|
|
|
* - `CLONE_VM`
|
|
|
|
* - `CLONE_FS`
|
|
|
|
* - `CLONE_FILES`
|
|
|
|
* - `CLONE_SIGHAND`
|
2023-05-10 06:35:10 +00:00
|
|
|
* - `CLONE_SYSVSEM`
|
2022-07-10 11:01:17 +00:00
|
|
|
*
|
|
|
|
* This system call wrapper is intended for threads, and as such, we
|
|
|
|
* won't polyfill Linux's ability to simulate unrelated calls (e.g.
|
|
|
|
* fork, vfork) via clone() on other platforms. Please just call
|
|
|
|
* fork() and vfork() when that's what you want.
|
|
|
|
*
|
|
|
|
* Your `flags` may also optionally also additionally bitwise-OR any
|
|
|
|
* combination of the following additional flags:
|
|
|
|
*
|
|
|
|
* - `CLONE_CHILD_SETTID` must be specified if you intend to set the
|
2022-11-08 18:09:47 +00:00
|
|
|
* `ctid` argument, which will updated with the child tid once the
|
|
|
|
* child has started.
|
|
|
|
*
|
|
|
|
* - `CLONE_PARENT_SETTID` must be specified if you intend to set
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
* the `ptid` argument, and it is updated at the most opportune
|
|
|
|
* moment. On all platforms except XNU x86, this happens before
|
|
|
|
* clone() returns. But since it might not be available yet you
|
|
|
|
* need to use pthread_getunique_np() to obtain it.
|
2022-07-10 11:01:17 +00:00
|
|
|
*
|
|
|
|
* - `CLONE_CHILD_CLEARTID` causes `*ctid = 0` upon child thread
|
|
|
|
* termination. This is used to implement join so that the parent
|
|
|
|
* may know when it's safe to free the child's stack memory, and
|
|
|
|
* as such, is guaranteed to happen AFTER the child thread has
|
|
|
|
* either terminated or has finished using its stack memory
|
|
|
|
*
|
|
|
|
* - `CLONE_SETTLS` is needed if you intend to specify the `tls`
|
2022-07-19 05:26:11 +00:00
|
|
|
* argument, which after thread creation may be accessed using
|
|
|
|
* __get_tls(). Doing this means that `errno`, gettid(), etc.
|
|
|
|
* correctly work. Caveat emptor if you choose not to do this.
|
2022-07-10 11:01:17 +00:00
|
|
|
*
|
|
|
|
* @param arg is passed as an argument to `func` in the child thread
|
2022-05-13 00:52:13 +00:00
|
|
|
* @param tls may be used to set the thread local storage segment;
|
|
|
|
* this parameter is ignored if `CLONE_SETTLS` is not set
|
2022-05-17 11:14:28 +00:00
|
|
|
* @param ctid lets the child receive its thread id without having to
|
|
|
|
* call gettid() and is ignored if `CLONE_CHILD_SETTID` isn't set
|
2022-11-08 18:09:47 +00:00
|
|
|
* @return 0 on success, or errno on errno
|
2022-05-13 00:52:13 +00:00
|
|
|
*/
|
2022-11-08 18:09:47 +00:00
|
|
|
errno_t clone(void *func, void *stk, size_t stksz, int flags, void *arg,
|
|
|
|
void *ptid, void *tls, void *ctid) {
|
2022-06-15 23:19:50 +00:00
|
|
|
int rc;
|
2022-05-17 14:40:00 +00:00
|
|
|
|
2022-07-19 05:26:11 +00:00
|
|
|
if (flags & CLONE_THREAD) {
|
|
|
|
__enable_threads();
|
|
|
|
}
|
2022-05-13 00:52:13 +00:00
|
|
|
|
2022-05-28 12:50:01 +00:00
|
|
|
if (!func) {
|
2022-11-08 18:09:47 +00:00
|
|
|
rc = EINVAL;
|
2022-05-28 12:50:01 +00:00
|
|
|
} else if (IsAsan() &&
|
2022-09-09 13:19:05 +00:00
|
|
|
(((flags & CLONE_SETTLS) && !__asan_is_valid(tls, 64)) ||
|
2022-05-28 12:50:01 +00:00
|
|
|
((flags & CLONE_PARENT_SETTID) &&
|
2022-11-08 18:09:47 +00:00
|
|
|
!__asan_is_valid(ptid, sizeof(int))) ||
|
2022-05-28 12:50:01 +00:00
|
|
|
((flags & CLONE_CHILD_SETTID) &&
|
2022-09-13 06:10:38 +00:00
|
|
|
!__asan_is_valid(ctid, sizeof(int))))) {
|
2022-11-08 18:09:47 +00:00
|
|
|
rc = EFAULT;
|
2022-05-17 11:14:28 +00:00
|
|
|
} else if (IsLinux()) {
|
2022-07-18 10:33:32 +00:00
|
|
|
rc = CloneLinux(func, stk, stksz, flags, arg, tls, ptid, ctid);
|
2022-05-19 23:57:49 +00:00
|
|
|
} else if (!IsTiny() &&
|
|
|
|
(flags & ~(CLONE_SETTLS | CLONE_PARENT_SETTID |
|
|
|
|
CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) !=
|
|
|
|
(CLONE_THREAD | CLONE_VM | CLONE_FS | CLONE_FILES |
|
2023-05-10 06:35:10 +00:00
|
|
|
CLONE_SIGHAND | CLONE_SYSVSEM)) {
|
2022-11-08 18:09:47 +00:00
|
|
|
rc = EINVAL;
|
2022-05-13 00:52:13 +00:00
|
|
|
} else if (IsXnu()) {
|
2023-06-04 01:32:33 +00:00
|
|
|
#ifdef __x86_64__
|
2022-07-18 10:33:32 +00:00
|
|
|
rc = CloneXnu(func, stk, stksz, flags, arg, tls, ptid, ctid);
|
2023-06-04 01:32:33 +00:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
rc = CloneSilicon(func, stk, stksz, flags, arg, tls, ptid, ctid);
|
|
|
|
#else
|
|
|
|
#error "unsupported architecture"
|
|
|
|
#endif
|
2022-05-13 00:52:13 +00:00
|
|
|
} else if (IsFreebsd()) {
|
2022-07-18 10:33:32 +00:00
|
|
|
rc = CloneFreebsd(func, stk, stksz, flags, arg, tls, ptid, ctid);
|
2023-12-30 04:11:23 +00:00
|
|
|
#ifdef __x86_64__
|
2022-05-13 00:52:13 +00:00
|
|
|
} else if (IsNetbsd()) {
|
2022-07-18 10:33:32 +00:00
|
|
|
rc = CloneNetbsd(func, stk, stksz, flags, arg, tls, ptid, ctid);
|
2022-05-13 00:52:13 +00:00
|
|
|
} else if (IsOpenbsd()) {
|
2022-07-18 10:33:32 +00:00
|
|
|
rc = CloneOpenbsd(func, stk, stksz, flags, arg, tls, ptid, ctid);
|
2022-05-17 11:14:28 +00:00
|
|
|
} else if (IsWindows()) {
|
2022-07-18 10:33:32 +00:00
|
|
|
rc = CloneWindows(func, stk, stksz, flags, arg, tls, ptid, ctid);
|
2023-05-09 08:56:56 +00:00
|
|
|
#endif /* __x86_64__ */
|
2022-05-13 00:52:13 +00:00
|
|
|
} else {
|
2022-11-08 18:09:47 +00:00
|
|
|
rc = ENOSYS;
|
2022-05-19 23:57:49 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 10:24:46 +00:00
|
|
|
if (SupportsBsd() && rc == EPROCLIM) {
|
|
|
|
rc = EAGAIN;
|
|
|
|
}
|
|
|
|
|
2022-05-13 00:52:13 +00:00
|
|
|
return rc;
|
|
|
|
}
|