Hunt down more bugs

After going through the MODE=dbg and MODE=zero build modes, a bunch of
little issues were identified, which have been addressed. Fixing those
issues created even more troubles for the project, because it improved
our ability to detect latent problems which are getting fixed so fast.
This commit is contained in:
Justine Tunney 2023-07-03 17:35:11 -07:00
parent 73c0faa1b5
commit 97b7116953
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
39 changed files with 557 additions and 754 deletions

View file

@ -20,6 +20,12 @@
// Gets machine state.
//
// This function goes 14x slower if sigaction() has ever been used to
// install a signal handling function. If you don't care about signal
// safety and just want fast fibers, then you may override the global
// variable `__interruptible` to disable the sigprocmask() calls, for
// pure userspace context switching.
//
// @return 0 on success, or -1 w/ errno
// @see makecontext()
// @see swapcontext()

View file

@ -63,7 +63,10 @@
#elif defined(__aarch64__)
#define REGS(i) 184+i*8
stp xzr,x1,[x0,REGS(0)] // x0 = 0
// x14 and x15 are clobbered
// all other registers are preserved
stp xzr,x1,[x0,REGS(0)] // context.x0 = 0
stp x2,x3,[x0,REGS(2)]
stp x4,x5,[x0,REGS(4)]
stp x6,x7,[x0,REGS(6)]
@ -84,6 +87,44 @@
str xzr,[x0,448] // pstate = 0
str xzr,[x0,456] // no vectors yet
/*void getfpsimd(ucontext_t *uc) {
struct fpsimd_context *fp;
fp = (struct fpsimd_context *)uc->uc_mcontext.__reserved;
fp[0].head.magic = FPSIMD_MAGIC;
fp[0].head.size = sizeof(*fp);
fp[1].head.magic = 0;
fp[1].head.size = 0;
asm("mrs\t%0,fpsr" : "=r"(fp->fpsr));
asm("mrs\t%0,fpcr" : "=r"(fp->fpcr));
asm("stp\tq0,q1,%0" : "=m"(fp->vregs[0]));
asm("stp\tq2,q3,%0" : "=m"(fp->vregs[2]));
asm("stp\tq4,q5,%0" : "=m"(fp->vregs[4]));
asm("stp\tq6,q7,%0" : "=m"(fp->vregs[6]));
asm("stp\tq8,q9,%0" : "=m"(fp->vregs[8]));
asm("stp\tq10,q11,%0" : "=m"(fp->vregs[10]));
asm("stp\tq12,q13,%0" : "=m"(fp->vregs[12]));
asm("stp\tq14,q15,%0" : "=m"(fp->vregs[14]));
}*/
add x15,x0,464
mov x14,0x8001
movk x14,0x4650,lsl 16
str xzr,[x0,992]
movk x14,0x210,lsl 32
str x14,[x0,464]
mrs x14,fpsr
str w14,[x15,8]
mrs x14,fpcr
str w14,[x15,12]
stp q0,q1,[x15,16]
stp q2,q3,[x15,48]
stp q4,q5,[x15,80]
stp q6,q7,[x15,112]
stp q8,q9,[x15,144]
stp q10,q11,[x15,176]
stp q12,q13,[x15,208]
stp q14,q15,[x15,240]
#else
#error "unsupported architecture"
#endif

View file

@ -467,6 +467,13 @@ static int __sigaction(int sig, const struct sigaction *act,
* spawned your process, happened to call `setrlimit()`. Doing this is
* a wonderful idea.
*
* Using signals might make your C runtime slower. Upon successfully
* installing its first signal handling function, sigaction() will set
* the global variable `__interruptible` to true, to let everything else
* know that signals are in play. That way code which would otherwise be
* frequently calling sigprocmask() out of an abundance of caution, will
* no longer need to pay its outrageous cost.
*
* @return 0 on success or -1 w/ errno
* @see xsigaction() for a much better api
* @asyncsignalsafe
@ -478,6 +485,13 @@ int sigaction(int sig, const struct sigaction *act, struct sigaction *oldact) {
rc = einval();
} else {
rc = __sigaction(sig, act, oldact);
if (!rc && act && (uintptr_t)act->sa_handler >= kSigactionMinRva) {
static bool once;
if (!once) {
__interruptible = true;
once = true;
}
}
}
STRACE("sigaction(%G, %s, [%s]) → %d% m", sig, DescribeSigaction(0, act),
DescribeSigaction(rc, oldact), rc);

View file

@ -1,13 +1,14 @@
#ifndef COSMOPOLITAN_LIBC_CALLS_STRUCT_AARCH64_H_
#define COSMOPOLITAN_LIBC_CALLS_STRUCT_AARCH64_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define FPSIMD_MAGIC 0x46508001
#define ESR_MAGIC 0x45535201
#define EXTRA_MAGIC 0x45585401
#define SVE_MAGIC 0x53564501
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct _aarch64_ctx {
uint32_t magic;
uint32_t size;

View file

@ -27,6 +27,12 @@
//
// swapcontext(x, y);
//
// This function goes 14x slower if sigaction() has ever been used to
// install a signal handling function. If you don't care about signal
// safety and just want fast fibers, then you may override the global
// variable `__interruptible` to disable the sigprocmask() calls, for
// pure userspace context switching.
//
// @return 0 on success, or -1 w/ errno
// @returnstwice
// @threadsafe

View file

@ -64,6 +64,47 @@ __tailcontext:
#elif defined(__aarch64__)
#define REGS(i) 184+i*8
/*void setfpsimd(const ucontext_t *uc) {
struct fpsimd_context *fp;
fp = (struct fpsimd_context *)uc->uc_mcontext.__reserved;
if (fp[0].head.magic == FPSIMD_MAGIC) {
asm("msr\tfpsr,%0" ::"r"(fp->fpsr));
asm("msr\tfpcr,%0" ::"r"(fp->fpcr));
asm("ldp\tq0,q1,%0" ::"m"(fp->vregs[0]));
asm("ldp\tq2,q3,%0" ::"m"(fp->vregs[2]));
asm("ldp\tq4,q5,%0" ::"m"(fp->vregs[4]));
asm("ldp\tq6,q7,%0" ::"m"(fp->vregs[6]));
asm("ldp\tq8,q9,%0" ::"m"(fp->vregs[8]));
asm("ldp\tq10,q11,%0" ::"m"(fp->vregs[10]));
asm("ldp\tq12,q13,%0" ::"m"(fp->vregs[12]));
asm("ldp\tq14,q15,%0" ::"m"(fp->vregs[14]));
}
}*/
ldr w14,[x0,464]
add x15,x0,464
mov w13,0x8001
movk w13,0x4650,lsl 16
cmp w14,w13
bne 1f
ldr w13,[x15,8]
msr fpsr,x13
ldr w13,[x15,12]
msr fpcr,x13
ldp q0,q1,[x15,16]
ldp q2,q3,[x15,48]
ldp q4,q5,[x15,80]
ldp q6,q7,[x15,112]
ldp q8,q9,[x15,144]
ldp q10,q11,[x15,176]
ldp q12,q13,[x15,208]
ldp q14,q15,[x15,240]
1:
// x16 is clobbered
// x18 belongs to apple
// all other registers are restored
ldp x1,x16,[x0,REGS(31)] // sp, pc
mov sp,x1
ldr x30,[x0,REGS(30)]
@ -72,7 +113,7 @@ __tailcontext:
ldp x24,x25,[x0,REGS(24)]
ldp x22,x23,[x0,REGS(22)]
ldp x20,x21,[x0,REGS(20)]
ldp x18,x19,[x0,REGS(18)]
ldr x19,[x0,REGS(19)]
ldr x17,[x0,REGS(17)]
ldp x14,x15,[x0,REGS(14)]
ldp x12,x13,[x0,REGS(12)]

View file

@ -18,13 +18,27 @@
*/
#include "libc/calls/ucontext.h"
#include "libc/calls/struct/sigset.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/sig.h"
int __tailcontext(const ucontext_t *);
static int __contextmask(const sigset_t *opt_set, sigset_t *opt_out_oldset) {
if (!__interruptible) return 0;
// signal handling functions might exist
// now context switching needs to go 14x slower
return sigprocmask(SIG_SETMASK, opt_set, opt_out_oldset);
}
/**
* Sets machine context.
*
* This function goes 14x slower if sigaction() has ever been used to
* install a signal handling function. If you don't care about signal
* safety and just want fast fibers, then you may override the global
* variable `__interruptible` to disable the sigprocmask() calls, for
* pure userspace context switching.
*
* @return -1 on error w/ errno, otherwise won't return unless sent back
* @see swapcontext()
* @see makecontext()
@ -32,14 +46,14 @@ int __tailcontext(const ucontext_t *);
* @threadsafe
*/
int setcontext(const ucontext_t *uc) {
if (sigprocmask(SIG_SETMASK, &uc->uc_sigmask, 0)) return -1;
if (__contextmask(&uc->uc_sigmask, 0)) return -1;
return __tailcontext(uc);
}
int __getcontextsig(ucontext_t *uc) {
return sigprocmask(SIG_SETMASK, 0, &uc->uc_sigmask);
return __contextmask(0, &uc->uc_sigmask);
}
int __swapcontextsig(ucontext_t *x, const ucontext_t *y) {
return sigprocmask(SIG_SETMASK, &y->uc_sigmask, &x->uc_sigmask);
return __contextmask(&y->uc_sigmask, &x->uc_sigmask);
}