Introduce FreeBSD ARM64 support

It's 100% passing test fleet. Solid as a rock.
This commit is contained in:
Justine Tunney 2023-12-29 20:11:23 -08:00
parent 43fe5956ad
commit 83107f78ed
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
455 changed files with 778 additions and 551 deletions

View file

@ -20,6 +20,7 @@
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/struct/aarch64.internal.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/siginfo-freebsd.internal.h"
#include "libc/calls/struct/siginfo-meta.internal.h"
@ -33,18 +34,18 @@
#include "libc/str/str.h"
#include "libc/sysv/consts/sa.h"
#ifdef __x86_64__
privileged void __sigenter_freebsd(int sig, struct siginfo_freebsd *freebsdinfo,
struct ucontext_freebsd *ctx) {
#pragma GCC push_options
#pragma GCC diagnostic ignored "-Wframe-larger-than="
struct Goodies {
struct {
ucontext_t uc;
siginfo_t si;
} g;
CheckLargeStackAllocation(&g, sizeof(g));
#pragma GCC pop_options
int rva, flags;
rva = __sighandrvas[sig];
if (rva >= kSigactionMinRva) {
@ -52,12 +53,20 @@ privileged void __sigenter_freebsd(int sig, struct siginfo_freebsd *freebsdinfo,
if (~flags & SA_SIGINFO) {
((sigaction_f)(__executable_start + rva))(sig, 0, 0);
} else {
//
// TRANSLATE FREEBSD SIGNAL TO LINUX SIGNAL
//
__repstosb(&g, 0, sizeof(g));
g.uc.uc_mcontext.fpregs = &g.uc.__fpustate;
__siginfo2cosmo(&g.si, (void *)freebsdinfo);
g.uc.uc_stack.ss_sp = ctx->uc_stack.ss_sp;
g.uc.uc_stack.ss_size = ctx->uc_stack.ss_size;
g.uc.uc_stack.ss_flags = ctx->uc_stack.ss_flags;
g.uc.uc_sigmask = ctx->uc_sigmask[0] | (uint64_t)ctx->uc_sigmask[0] << 32;
#ifdef __x86_64__
g.uc.uc_mcontext.fpregs = &g.uc.__fpustate;
g.uc.uc_mcontext.r8 = ctx->uc_mcontext.mc_r8;
g.uc.uc_mcontext.r9 = ctx->uc_mcontext.mc_r9;
g.uc.uc_mcontext.r10 = ctx->uc_mcontext.mc_r10;
@ -81,14 +90,39 @@ privileged void __sigenter_freebsd(int sig, struct siginfo_freebsd *freebsdinfo,
g.uc.uc_mcontext.err = ctx->uc_mcontext.mc_err;
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);
#endif /* __x86_64__ */
#ifdef __aarch64__
__memcpy(g.uc.uc_mcontext.regs, &ctx->uc_mcontext.mc_gpregs, 34 * 8);
if (ctx->uc_mcontext.mc_flags & _MC_FP_VALID) {
struct fpsimd_context *vc =
(struct fpsimd_context *)g.uc.uc_mcontext.__reserved;
vc->head.magic = FPSIMD_MAGIC;
vc->head.size = sizeof(*vc);
vc->fpsr = ctx->uc_mcontext.mc_fpregs.fp_sr;
vc->fpcr = ctx->uc_mcontext.mc_fpregs.fp_cr;
__memcpy(vc->vregs, ctx->uc_mcontext.mc_fpregs.fp_q, 32 * 16);
}
#endif /* __aarch64__ */
//
// INVOKE SIGNAL HANDLER
//
((sigaction_f)(__executable_start + rva))(sig, &g.si, &g.uc);
//
// TRANSLATE LINUX SIGNAL TO FREEBSD SIGNAL
//
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;
ctx->uc_flags = g.uc.uc_flags;
ctx->uc_sigmask[0] = g.uc.uc_sigmask;
ctx->uc_sigmask[1] = g.uc.uc_sigmask >> 32;
#ifdef __x86_64__
ctx->uc_mcontext.mc_rdi = g.uc.uc_mcontext.rdi;
ctx->uc_mcontext.mc_rsi = g.uc.uc_mcontext.rsi;
ctx->uc_mcontext.mc_rdx = g.uc.uc_mcontext.rdx;
@ -112,13 +146,24 @@ privileged void __sigenter_freebsd(int sig, struct siginfo_freebsd *freebsdinfo,
ctx->uc_mcontext.mc_rip = g.uc.uc_mcontext.rip;
ctx->uc_mcontext.mc_rsp = g.uc.uc_mcontext.rsp;
__repmovsb(&ctx->uc_mcontext.mc_fpstate, &g.uc.__fpustate, 512);
#endif /* __x86_64__ */
#ifdef __aarch64__
__memcpy(&ctx->uc_mcontext.mc_gpregs, g.uc.uc_mcontext.regs, 34 * 8);
struct fpsimd_context *vc =
(struct fpsimd_context *)g.uc.uc_mcontext.__reserved;
if (vc->head.magic == FPSIMD_MAGIC) {
ctx->uc_mcontext.mc_flags |= _MC_FP_VALID;
ctx->uc_mcontext.mc_fpregs.fp_sr = vc->fpsr;
ctx->uc_mcontext.mc_fpregs.fp_cr = vc->fpcr;
__memcpy(ctx->uc_mcontext.mc_fpregs.fp_q, vc->vregs, 32 * 16);
}
#endif /* __aarch64__ */
// done
}
}
/*
* When the FreeBSD kernel invokes this signal handler it pushes a
* trampoline on the stack which does two things: 1) it calls this
* function, and 2) calls sys_sigreturn() once this returns.
*/
// When the FreeBSD kernel invokes this signal handler it pushes a
// trampoline on the stack which does two things: 1) it calls this
// function, and 2) calls sys_sigreturn() once this returns.
}
#endif /* __x86_64__ */