mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 15:03:34 +00:00
- 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.
156 lines
5.5 KiB
C
156 lines
5.5 KiB
C
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright 2023 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. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
|
#include "libc/dce.h"
|
|
#include "libc/errno.h"
|
|
#include "libc/intrin/atomic.h"
|
|
#include "libc/intrin/weaken.h"
|
|
#include "libc/thread/posixthread.internal.h"
|
|
#include "libc/thread/tls.h"
|
|
#ifndef __x86_64__
|
|
|
|
// todo(jart): dismal llvm
|
|
register long x0 asm("x0");
|
|
register long x1 asm("x1");
|
|
register long x2 asm("x2");
|
|
register long x3 asm("x3");
|
|
register long x4 asm("x4");
|
|
register long x5 asm("x5");
|
|
register long sysv_ordinal asm("x8");
|
|
register long xnu_ordinal asm("x16");
|
|
register long cosmo_tls_register asm("x28");
|
|
|
|
void report_cancelation_point(void);
|
|
|
|
dontinline long systemfive_cancel(void) {
|
|
return _weaken(_pthread_cancel_ack)();
|
|
}
|
|
|
|
// special region of executable memory where cancelation is safe
|
|
dontinline long systemfive_cancellable(void) {
|
|
|
|
// check (1) this is a cancelation point
|
|
// plus (2) cancelations aren't disabled
|
|
struct PosixThread *pth = 0;
|
|
if (cosmo_tls_register && //
|
|
_weaken(_pthread_cancel_ack) && //
|
|
(pth = _pthread_self())) {
|
|
// check if cancelation is already pending
|
|
if (!(pth->pt_flags & PT_NOCANCEL) &&
|
|
atomic_load_explicit(&pth->pt_canceled, memory_order_acquire)) {
|
|
return systemfive_cancel();
|
|
}
|
|
#if IsModeDbg()
|
|
if (!(pth->flags & PT_INCANCEL)) {
|
|
if (_weaken(report_cancelation_point)) {
|
|
_weaken(report_cancelation_point)();
|
|
}
|
|
__builtin_trap();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
// invoke cancellable system call
|
|
// this works for both linux and bsd
|
|
asm volatile("mov\tx9,0\n\t" // clear carry flag
|
|
"adds\tx9,x9,0\n\t" // clear carry flag
|
|
"svc\t0\n"
|
|
"systemfive_cancellable_end:\n\t"
|
|
".globl\tsystemfive_cancellable_end\n\t"
|
|
"bcs\t1f\n\t"
|
|
"b\t2f\n1:\t"
|
|
"neg\tx0,x0\n2:"
|
|
: /* global output */
|
|
: /* global inputs */
|
|
: "x9", "memory");
|
|
|
|
// if it succeeded then we're done
|
|
if (x0 < -4095ul) {
|
|
return x0;
|
|
}
|
|
|
|
// check if i/o call was interrupted by sigthr
|
|
if (pth && x0 == -EINTR && !(pth->pt_flags & PT_NOCANCEL) &&
|
|
atomic_load_explicit(&pth->pt_canceled, memory_order_acquire)) {
|
|
return systemfive_cancel();
|
|
}
|
|
|
|
// otherwise go down error path
|
|
return _sysret(x0);
|
|
}
|
|
|
|
/**
|
|
* System Five System Call Support.
|
|
*
|
|
* This supports POSIX thread cancelation only when the caller flips a
|
|
* bit in TLS storage that indicates we're inside a cancelation point.
|
|
*
|
|
* @param x0 is first argument
|
|
* @param x1 is second argument
|
|
* @param x2 is third argument
|
|
* @param x3 is fourth argument
|
|
* @param x4 is fifth argument
|
|
* @param x5 is sixth argument
|
|
* @param sysv_ordinal is linux ordinal
|
|
* @param xnu_ordinal is xnu ordinal
|
|
* @return x0
|
|
*/
|
|
long systemfive(void) {
|
|
|
|
// handle special cases
|
|
if (IsLinux()) {
|
|
if (sysv_ordinal == 0xfff) {
|
|
return _sysret(-ENOSYS);
|
|
}
|
|
if (sysv_ordinal & 0x800) {
|
|
sysv_ordinal &= ~0x800;
|
|
return systemfive_cancellable();
|
|
}
|
|
}
|
|
if (IsXnu()) {
|
|
if (xnu_ordinal == 0xfff) {
|
|
return _sysret(-ENOSYS);
|
|
}
|
|
if (xnu_ordinal & 0x800) {
|
|
xnu_ordinal &= ~0x800;
|
|
return systemfive_cancellable();
|
|
}
|
|
}
|
|
|
|
// invoke non-blocking system call
|
|
// this works for both linux and bsd
|
|
asm volatile("mov\tx9,0\n\t" // clear carry flag
|
|
"adds\tx9,x9,0\n\t" // clear carry flag
|
|
"svc\t0\n\t"
|
|
"bcs\t1f\n\t"
|
|
"b\t2f\n1:\t"
|
|
"neg\tx0,x0\n2:"
|
|
: /* global output */
|
|
: /* global inputs */
|
|
: "x9", "memory");
|
|
|
|
// check result
|
|
if (x0 < -4095ul) {
|
|
return x0;
|
|
} else {
|
|
return _sysret(x0);
|
|
}
|
|
}
|
|
|
|
#endif /* __x86_64__ */
|