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

@ -16,8 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/seccomp.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/seccomp.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
@ -62,16 +62,20 @@ int seccomp(unsigned operation, unsigned flags, void *args) {
rc = -1;
}
#elif defined(__aarch64__)
register long r0 asm("x0") = (long)operation;
register long r1 asm("x1") = (long)flags;
register long r2 asm("x2") = (long)args;
register long res_x0 asm("x0");
asm volatile("mov\tx8,%1\n\t"
"svc\t0"
: "=r"(res_x0)
: "i"(211), "r"(r0), "r"(r1), "r"(r2)
: "x8", "memory");
rc = _sysret(res_x0);
if (IsLinux()) {
register long r0 asm("x0") = (long)operation;
register long r1 asm("x1") = (long)flags;
register long r2 asm("x2") = (long)args;
register long res_x0 asm("x0");
asm volatile("mov\tx8,%1\n\t"
"svc\t0"
: "=r"(res_x0)
: "i"(211), "r"(r0), "r"(r1), "r"(r2)
: "x8", "memory");
rc = _sysret(res_x0);
} else {
rc = enosys();
}
#else
#error "arch unsupported"
#endif

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__ */

View file

@ -2,6 +2,22 @@
#define COSMOPOLITAN_LIBC_CALLS_STRUCT_UCONTEXT_FREEBSD_INTERNAL_H_
COSMOPOLITAN_C_START_
struct gpregs_freebsd_aarch64 {
int64_t gp_x[30];
int64_t gp_lr;
int64_t gp_sp;
int64_t gp_elr; /* pc */
uint64_t gp_spsr; /* pstate or cpsr */
};
struct fpregs_freebsd_aarch64 {
uint128_t fp_q[32];
uint32_t fp_sr;
uint32_t fp_cr;
int fp_flags;
int fp_pad;
};
struct stack_freebsd {
void *ss_sp;
uint64_t ss_size;
@ -9,6 +25,7 @@ struct stack_freebsd {
};
struct mcontext_freebsd {
#ifdef __x86_64__
int64_t mc_onstack;
int64_t mc_rdi;
int64_t mc_rsi;
@ -47,6 +64,14 @@ struct mcontext_freebsd {
int64_t mc_xfpustate;
int64_t mc_xfpustate_len;
int64_t mc_spare[4];
#elif defined(__aarch64__)
struct gpregs_freebsd_aarch64 mc_gpregs;
struct fpregs_freebsd_aarch64 mc_fpregs;
int mc_flags;
#define _MC_FP_VALID 0x1 /* Set when mc_fpregs has valid data */
int mc_pad; /* Padding */
uint64_t mc_spare[8]; /* Space for expansion, set to zero */
#endif
};
struct ucontext_freebsd {