Introduce native support for MacOS ARM64

There's a new program named ape/ape-m1.c which will be used to build an
embeddable binary that can load ape and elf executables. The support is
mostly working so far, but still chasing down ABI issues.
This commit is contained in:
Justine Tunney 2023-05-18 19:05:08 -07:00
parent b852650c08
commit 1422e96b4e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
757 changed files with 2988 additions and 1321 deletions

View file

@ -134,7 +134,7 @@ static bool __sig_deliver(bool restartable, int sig, int si_code,
}
// handover control to user
((sigaction_f)(_base + rva))(sig, infop, ctx);
((sigaction_f)(__executable_start + rva))(sig, infop, ctx);
if ((~flags & SA_NODEFER) && (~flags & SA_RESETHAND)) {
// it's now safe to reenter the signal so we need to restore it.

View file

@ -188,6 +188,10 @@ o/$(MODE)/libc/calls/unveil.o: private \
OVERRIDE_CFLAGS += \
-DSTACK_FRAME_UNLIMITED
ifeq ($(ARCH), aarch64)
o/$(MODE)/libc/calls/sigaction.o: private OVERRIDE_CFLAGS += -mcmodel=large
endif
# we want -Os because:
# it makes a big difference
# we need pic because:

26
libc/calls/err.c Normal file
View file

@ -0,0 +1,26 @@
/*-*- 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/calls.h"
#include "libc/errno.h"
extern _Thread_local int hog;
int dog(void) {
return hog;
}

View file

@ -24,7 +24,6 @@
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/promises.internal.h"
#include "libc/intrin/strace.internal.h"

View file

@ -63,7 +63,7 @@ textstartup noasan void InitializeMetalFile(void) {
* The zipos code will automatically arrange to do this. Alternatively,
* user code can STATIC_YOINK this symbol.
*/
size_t size = ROUNDUP(_ezip - _base, 4096);
size_t size = ROUNDUP(_ezip - __executable_start, 4096);
void *copied_base;
struct DirectMap dm;
dm = sys_mmap_metal(NULL, size, PROT_READ | PROT_WRITE,

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/sigaction.h"
#include "ape/sections.internal.h"
#include "libc/assert.h"
#include "libc/calls/blocksigs.internal.h"
#include "libc/calls/calls.h"
@ -168,9 +169,11 @@ static int __sigaction(int sig, const struct sigaction *act,
rva = (int32_t)(intptr_t)SIG_DFL;
} else if ((intptr_t)act->sa_handler < kSigactionMinRva) {
rva = (int)(intptr_t)act->sa_handler;
} else if ((intptr_t)act->sa_handler >= (intptr_t)&_base + kSigactionMinRva &&
(intptr_t)act->sa_handler < (intptr_t)&_base + INT_MAX) {
rva = (int)((uintptr_t)act->sa_handler - (uintptr_t)&_base);
} else if ((intptr_t)act->sa_handler >=
(intptr_t)&__executable_start + kSigactionMinRva &&
(intptr_t)act->sa_handler <
(intptr_t)&__executable_start + INT_MAX) {
rva = (int)((uintptr_t)act->sa_handler - (uintptr_t)&__executable_start);
} else {
return efault();
}
@ -249,8 +252,9 @@ static int __sigaction(int sig, const struct sigaction *act,
if (oldact) {
oldrva = __sighandrvas[sig];
oldact->sa_sigaction =
(sigaction_f)(oldrva < kSigactionMinRva ? oldrva
: (intptr_t)&_base + oldrva);
(sigaction_f)(oldrva < kSigactionMinRva
? oldrva
: (intptr_t)&__executable_start + oldrva);
}
if (act) {
__sighandrvas[sig] = rva;

View file

@ -45,7 +45,7 @@ privileged void __sigenter_freebsd(int sig, struct siginfo_freebsd *freebsdinfo,
if (rva >= kSigactionMinRva) {
flags = __sighandflags[sig & (NSIG - 1)];
if (~flags & SA_SIGINFO) {
((sigaction_f)(_base + rva))(sig, 0, 0);
((sigaction_f)(__executable_start + rva))(sig, 0, 0);
} else {
__repstosb(&g, 0, sizeof(g));
g.uc.uc_mcontext.fpregs = &g.uc.__fpustate;
@ -78,7 +78,7 @@ privileged void __sigenter_freebsd(int sig, struct siginfo_freebsd *freebsdinfo,
g.uc.uc_mcontext.trapno = ctx->uc_mcontext.mc_trapno;
__repmovsb(&g.uc.__fpustate, &ctx->uc_mcontext.mc_fpstate, 512);
__siginfo2cosmo(&g.si, (void *)freebsdinfo);
((sigaction_f)(_base + rva))(sig, &g.si, &g.uc);
((sigaction_f)(__executable_start + rva))(sig, &g.si, &g.uc);
ctx->uc_stack.ss_sp = g.uc.uc_stack.ss_sp;
ctx->uc_stack.ss_size = g.uc.uc_stack.ss_size;
ctx->uc_stack.ss_flags = g.uc.uc_stack.ss_flags;

View file

@ -44,7 +44,7 @@ privileged void __sigenter_wsl(int sig, struct siginfo *info, ucontext_t *ctx) {
memcpy(ctx->__fpustate.st + i, &nan, 16);
}
}
((sigaction_f)(_base + rva))(sig, info, ctx);
((sigaction_f)(__executable_start + rva))(sig, info, ctx);
}
}

View file

@ -43,7 +43,7 @@ privileged void __sigenter_netbsd(int sig, struct siginfo_netbsd *si,
if (rva >= kSigactionMinRva) {
flags = __sighandflags[sig & (NSIG - 1)];
if (~flags & SA_SIGINFO) {
((sigaction_f)(_base + rva))(sig, 0, 0);
((sigaction_f)(__executable_start + rva))(sig, 0, 0);
} else {
__repstosb(&uc, 0, sizeof(uc));
__siginfo2cosmo(&si2, (void *)si);
@ -75,7 +75,7 @@ privileged void __sigenter_netbsd(int sig, struct siginfo_netbsd *si,
uc.uc_mcontext.rip = ctx->uc_mcontext.rip;
uc.uc_mcontext.rsp = ctx->uc_mcontext.rsp;
*uc.uc_mcontext.fpregs = ctx->uc_mcontext.__fpregs;
((sigaction_f)(_base + rva))(sig, &si2, &uc);
((sigaction_f)(__executable_start + rva))(sig, &si2, &uc);
ctx->uc_stack.ss_sp = uc.uc_stack.ss_sp;
ctx->uc_stack.ss_size = uc.uc_stack.ss_size;
ctx->uc_stack.ss_flags = uc.uc_stack.ss_flags;

View file

@ -45,7 +45,7 @@ privileged void __sigenter_openbsd(int sig, struct siginfo_openbsd *openbsdinfo,
if (rva >= kSigactionMinRva) {
flags = __sighandflags[sig & (NSIG - 1)];
if (~flags & SA_SIGINFO) {
((sigaction_f)(_base + rva))(sig, 0, 0);
((sigaction_f)(__executable_start + rva))(sig, 0, 0);
} else {
__repstosb(&g.uc, 0, sizeof(g.uc));
__siginfo2cosmo(&g.si, (void *)openbsdinfo);
@ -76,7 +76,7 @@ privileged void __sigenter_openbsd(int sig, struct siginfo_openbsd *openbsdinfo,
if (ctx->sc_fpstate) {
*g.uc.uc_mcontext.fpregs = *ctx->sc_fpstate;
}
((sigaction_f)(_base + rva))(sig, &g.si, &g.uc);
((sigaction_f)(__executable_start + rva))(sig, &g.si, &g.uc);
ctx->sc_mask = g.uc.uc_sigmask.__bits[0];
ctx->sc_rdi = g.uc.uc_mcontext.rdi;
ctx->sc_rsi = g.uc.uc_mcontext.rsi;

View file

@ -31,7 +31,6 @@
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/sa.h"
#ifdef __x86_64__
/**
* @fileoverview XNU kernel callback normalization.
@ -345,10 +344,37 @@ struct __darwin_mcontext_avx512_64_full {
struct __darwin_x86_avx512_state64 __fs;
};
struct __darwin_arm_exception_state64 {
uint64_t far;
uint32_t esr;
uint32_t exception;
};
struct __darwin_arm_thread_state64 {
uint64_t __x[29];
uint64_t __fp;
uint64_t __lr;
uint64_t __sp;
uint64_t __pc;
uint32_t __cpsr;
uint32_t __pad;
};
struct __darwin_arm_vfp_state {
uint32_t __r[64];
uint32_t __fpscr;
};
struct __darwin_mcontext64 {
#ifdef __x86_64__
struct __darwin_x86_exception_state64 __es;
struct __darwin_x86_thread_state64 __ss;
struct __darwin_x86_float_state64 __fs;
#elif defined(__aarch64__)
struct __darwin_arm_exception_state64 __es;
struct __darwin_arm_thread_state64 __ss;
struct __darwin_arm_vfp_state __fs;
#endif
};
struct __darwin_ucontext {
@ -360,6 +386,8 @@ struct __darwin_ucontext {
struct __darwin_mcontext64 *uc_mcontext;
};
#ifdef __x86_64__
static privileged void xnuexceptionstate2linux(
mcontext_t *mc, struct __darwin_x86_exception_state64 *xnues) {
mc->trapno = xnues->__trapno;
@ -455,6 +483,8 @@ static privileged void linuxssefpustate2xnu(
CopyFpXmmRegs(&xnufs->__fpu_stmm0, fs->st);
}
#endif /* __x86_64__ */
privileged void __sigenter_xnu(void *fn, int infostyle, int sig,
struct siginfo_xnu *xnuinfo,
struct __darwin_ucontext *xnuctx) {
@ -468,15 +498,17 @@ privileged void __sigenter_xnu(void *fn, int infostyle, int sig,
if (rva >= kSigactionMinRva) {
flags = __sighandflags[sig & (NSIG - 1)];
if (~flags & SA_SIGINFO) {
((sigaction_f)(_base + rva))(sig, 0, 0);
((sigaction_f)(__executable_start + rva))(sig, 0, 0);
} else {
__repstosb(&g, 0, sizeof(g));
if (xnuctx) {
g.uc.uc_sigmask.__bits[0] = xnuctx->uc_sigmask;
g.uc.uc_sigmask.__bits[1] = 0;
g.uc.uc_stack.ss_sp = xnuctx->uc_stack.ss_sp;
g.uc.uc_stack.ss_flags = xnuctx->uc_stack.ss_flags;
g.uc.uc_stack.ss_size = xnuctx->uc_stack.ss_size;
#ifdef __x86_64__
g.uc.uc_mcontext.fpregs = &g.uc.__fpustate;
if (xnuctx->uc_mcontext) {
if (xnuctx->uc_mcsize >=
@ -493,16 +525,24 @@ privileged void __sigenter_xnu(void *fn, int infostyle, int sig,
xnussefpustate2linux(&g.uc.__fpustate, &xnuctx->uc_mcontext->__fs);
}
}
#elif defined(__aarch64__)
if (xnuctx->uc_mcontext) {
memcpy(g.uc.uc_mcontext.regs, &xnuctx->uc_mcontext->__ss.__x, 33 * 8);
}
#endif /* __x86_64__ */
}
if (xnuinfo) {
__siginfo2cosmo(&g.si, (void *)xnuinfo);
}
((sigaction_f)(_base + rva))(sig, &g.si, &g.uc);
((sigaction_f)(__executable_start + rva))(sig, &g.si, &g.uc);
if (xnuctx) {
xnuctx->uc_stack.ss_sp = g.uc.uc_stack.ss_sp;
xnuctx->uc_stack.ss_flags = g.uc.uc_stack.ss_flags;
xnuctx->uc_stack.ss_size = g.uc.uc_stack.ss_size;
xnuctx->uc_sigmask = g.uc.uc_sigmask.__bits[0];
#ifdef __x86_64__
if (xnuctx->uc_mcontext) {
if (xnuctx->uc_mcsize >=
sizeof(struct __darwin_x86_exception_state64)) {
@ -519,14 +559,29 @@ privileged void __sigenter_xnu(void *fn, int infostyle, int sig,
linuxssefpustate2xnu(&xnuctx->uc_mcontext->__fs, &g.uc.__fpustate);
}
}
#elif defined(__aarch64__)
if (xnuctx->uc_mcontext) {
memcpy(&xnuctx->uc_mcontext->__ss.__x, g.uc.uc_mcontext.regs, 33 * 8);
}
#endif /* __x86_64__ */
}
}
}
#ifdef __x86_64__
asm volatile("syscall"
: "=a"(ax)
: "0"(0x20000b8 /* sigreturn */), "D"(xnuctx), "S"(infostyle)
: "rcx", "r11", "memory", "cc");
#else
register long r0 asm("x0") = (long)xnuctx;
register long r1 asm("x1") = (long)infostyle;
asm volatile("mov\tx16,%0\n\t"
"svc\t0"
: /* no outputs */
: "i"(0x0b8 /* sigreturn */), "r"(r0), "r"(r1)
: "x16", "memory");
#endif /* __x86_64__ */
notpossible;
}
#endif

View file

@ -17,9 +17,27 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/consts/nr.h"
static inline unsigned sys_umask(unsigned newmask) {
#ifdef __x86_64__
unsigned res;
asm volatile("syscall"
: "=a"(res)
: "0"(__NR_umask), "D"(newmask)
: "memory", "cc");
#elif defined(__aarch64__)
// xnu m1 doesn't manage carry flag
register long r0 asm("x0") = newmask;
register long r8 asm("x8") = __NR_umask & 0x7ff;
register long r16 asm("x16") = __NR_umask & 0x7ff;
register unsigned res asm("x0");
asm volatile("svc\t0" : "=r"(res) : "r"(r0), "r"(r8), "r"(r16) : "memory");
#endif
return res;
}
/**
* Sets file mode creation mask.

View file

@ -43,7 +43,7 @@
#define nlink_t uint64_t
#elif defined(__aarch64__)
#define blksize_t int32_t
#define nlink_t uint32_t
#define nlink_t uint32_t /* uint16_t on xnu */
#endif
#define TIME_T_MAX __INT64_MAX__