Skip to content

Commit 83107f7

Browse files
committed
Introduce FreeBSD ARM64 support
It's 100% passing test fleet. Solid as a rock.
1 parent 43fe595 commit 83107f7

File tree

455 files changed

+778
-551
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

455 files changed

+778
-551
lines changed

ape/BUILD.mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ APE_LOADER_FLAGS = \
7878
$<
7979

8080
o/$(MODE)/ape/ape.elf: o/$(MODE)/ape/ape.elf.dbg
81-
$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -g $< $@
81+
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -g $< $@
82+
@$(COMPILE) -AFIXUPOBJ -wT$@ $(FIXUPOBJ) $@
8283

8384
o/$(MODE)/ape/ape.elf.dbg: \
8485
o/$(MODE)/ape/start.o \
@@ -88,7 +89,7 @@ o/$(MODE)/ape/ape.elf.dbg: \
8889
@$(COMPILE) -ALINK.elf $(LD) $(APE_LOADER_LDFLAGS) -o $@ $(patsubst %.lds,-T %.lds,$^)
8990

9091
o/$(MODE)/ape/loader.o: ape/loader.c ape/ape.h
91-
@$(COMPILE) -AOBJECTIFY.c $(CC) -DSUPPORT_VECTOR=1 -g $(APE_LOADER_FLAGS)
92+
@$(COMPILE) -AOBJECTIFY.c $(CC) -DSUPPORT_VECTOR=33 -g $(APE_LOADER_FLAGS)
9293
o/$(MODE)/ape/start.o: ape/start.S
9394
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
9495
o/$(MODE)/ape/launch.o: ape/launch.S

ape/loader.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
#define MIN(X, Y) ((Y) > (X) ? (X) : (Y))
8888
#define MAX(X, Y) ((Y) < (X) ? (X) : (Y))
8989

90-
#define PATH_MAX 1024 /* XXX verify */
90+
#define PATH_MAX 1024 /* XXX verify */
9191

9292
#define SupportsLinux() (SUPPORT_VECTOR & LINUX)
9393
#define SupportsXnu() (SUPPORT_VECTOR & XNU)
@@ -224,8 +224,8 @@ struct ApeLoader {
224224
};
225225

226226
EXTERN_C long SystemCall(long, long, long, long, long, long, long, int);
227-
EXTERN_C void
228-
Launch(void *, long, void *, void *, int) __attribute__((__noreturn__));
227+
EXTERN_C void Launch(void *, long, void *, void *, int)
228+
__attribute__((__noreturn__));
229229

230230
extern char __executable_start[];
231231
extern char _end[];

build/bootstrap/ape.aarch64

168 Bytes
Binary file not shown.

build/bootstrap/ape.elf

0 Bytes
Binary file not shown.

build/bootstrap/ape.macho

0 Bytes
Binary file not shown.

build/bootstrap/fixupobj.com

18.7 KB
Binary file not shown.

build/definitions.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ IGNORE := $(shell $(MKDIR) $(TMPDIR))
8686

8787
ifneq ($(findstring aarch64,$(MODE)),)
8888
ARCH = aarch64
89-
HOSTS ?= pi studio
89+
HOSTS ?= pi studio freebsdarm
9090
else
9191
ARCH = x86_64
9292
HOSTS ?= freebsd rhel7 xnu win10 openbsd netbsd

examples/crashreport2.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#if 0
2+
/*─────────────────────────────────────────────────────────────────╗
3+
│ To the extent possible under law, Justine Tunney has waived │
4+
│ all copyright and related or neighboring rights to this file, │
5+
│ as it is written in the following disclaimers: │
6+
│ • http://unlicense.org/ │
7+
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
8+
╚─────────────────────────────────────────────────────────────────*/
9+
#endif
10+
#include "libc/math.h"
11+
#include "libc/runtime/runtime.h"
12+
13+
void crash(long x0, long x1, long x2, //
14+
double v0, double v1, double v2) {
15+
__builtin_trap();
16+
}
17+
18+
void (*pCrash)(long, long, long, double, double, double) = crash;
19+
20+
int main(int argc, char *argv[]) {
21+
ShowCrashReports();
22+
pCrash(1, 2, 3, NAN, NAN, NAN);
23+
}

libc/calls/seccomp.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/calls/struct/seccomp.internal.h"
2019
#include "libc/calls/calls.h"
20+
#include "libc/calls/struct/seccomp.internal.h"
2121
#include "libc/calls/syscall-sysv.internal.h"
2222
#include "libc/dce.h"
2323
#include "libc/errno.h"
@@ -62,16 +62,20 @@ int seccomp(unsigned operation, unsigned flags, void *args) {
6262
rc = -1;
6363
}
6464
#elif defined(__aarch64__)
65-
register long r0 asm("x0") = (long)operation;
66-
register long r1 asm("x1") = (long)flags;
67-
register long r2 asm("x2") = (long)args;
68-
register long res_x0 asm("x0");
69-
asm volatile("mov\tx8,%1\n\t"
70-
"svc\t0"
71-
: "=r"(res_x0)
72-
: "i"(211), "r"(r0), "r"(r1), "r"(r2)
73-
: "x8", "memory");
74-
rc = _sysret(res_x0);
65+
if (IsLinux()) {
66+
register long r0 asm("x0") = (long)operation;
67+
register long r1 asm("x1") = (long)flags;
68+
register long r2 asm("x2") = (long)args;
69+
register long res_x0 asm("x0");
70+
asm volatile("mov\tx8,%1\n\t"
71+
"svc\t0"
72+
: "=r"(res_x0)
73+
: "i"(211), "r"(r0), "r"(r1), "r"(r2)
74+
: "x8", "memory");
75+
rc = _sysret(res_x0);
76+
} else {
77+
rc = enosys();
78+
}
7579
#else
7680
#error "arch unsupported"
7781
#endif

libc/calls/sigenter-freebsd.c

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "libc/calls/calls.h"
2121
#include "libc/calls/internal.h"
2222
#include "libc/calls/state.internal.h"
23+
#include "libc/calls/struct/aarch64.internal.h"
2324
#include "libc/calls/struct/sigaction.h"
2425
#include "libc/calls/struct/siginfo-freebsd.internal.h"
2526
#include "libc/calls/struct/siginfo-meta.internal.h"
@@ -33,31 +34,39 @@
3334
#include "libc/str/str.h"
3435
#include "libc/sysv/consts/sa.h"
3536

36-
#ifdef __x86_64__
37-
3837
privileged void __sigenter_freebsd(int sig, struct siginfo_freebsd *freebsdinfo,
3938
struct ucontext_freebsd *ctx) {
39+
4040
#pragma GCC push_options
4141
#pragma GCC diagnostic ignored "-Wframe-larger-than="
42-
struct Goodies {
42+
struct {
4343
ucontext_t uc;
4444
siginfo_t si;
4545
} g;
4646
CheckLargeStackAllocation(&g, sizeof(g));
4747
#pragma GCC pop_options
48+
4849
int rva, flags;
4950
rva = __sighandrvas[sig];
5051
if (rva >= kSigactionMinRva) {
5152
flags = __sighandflags[sig];
5253
if (~flags & SA_SIGINFO) {
5354
((sigaction_f)(__executable_start + rva))(sig, 0, 0);
5455
} else {
56+
57+
//
58+
// TRANSLATE FREEBSD SIGNAL TO LINUX SIGNAL
59+
//
60+
5561
__repstosb(&g, 0, sizeof(g));
56-
g.uc.uc_mcontext.fpregs = &g.uc.__fpustate;
62+
__siginfo2cosmo(&g.si, (void *)freebsdinfo);
5763
g.uc.uc_stack.ss_sp = ctx->uc_stack.ss_sp;
5864
g.uc.uc_stack.ss_size = ctx->uc_stack.ss_size;
5965
g.uc.uc_stack.ss_flags = ctx->uc_stack.ss_flags;
6066
g.uc.uc_sigmask = ctx->uc_sigmask[0] | (uint64_t)ctx->uc_sigmask[0] << 32;
67+
68+
#ifdef __x86_64__
69+
g.uc.uc_mcontext.fpregs = &g.uc.__fpustate;
6170
g.uc.uc_mcontext.r8 = ctx->uc_mcontext.mc_r8;
6271
g.uc.uc_mcontext.r9 = ctx->uc_mcontext.mc_r9;
6372
g.uc.uc_mcontext.r10 = ctx->uc_mcontext.mc_r10;
@@ -81,14 +90,39 @@ privileged void __sigenter_freebsd(int sig, struct siginfo_freebsd *freebsdinfo,
8190
g.uc.uc_mcontext.err = ctx->uc_mcontext.mc_err;
8291
g.uc.uc_mcontext.trapno = ctx->uc_mcontext.mc_trapno;
8392
__repmovsb(&g.uc.__fpustate, &ctx->uc_mcontext.mc_fpstate, 512);
84-
__siginfo2cosmo(&g.si, (void *)freebsdinfo);
93+
#endif /* __x86_64__ */
94+
95+
#ifdef __aarch64__
96+
__memcpy(g.uc.uc_mcontext.regs, &ctx->uc_mcontext.mc_gpregs, 34 * 8);
97+
if (ctx->uc_mcontext.mc_flags & _MC_FP_VALID) {
98+
struct fpsimd_context *vc =
99+
(struct fpsimd_context *)g.uc.uc_mcontext.__reserved;
100+
vc->head.magic = FPSIMD_MAGIC;
101+
vc->head.size = sizeof(*vc);
102+
vc->fpsr = ctx->uc_mcontext.mc_fpregs.fp_sr;
103+
vc->fpcr = ctx->uc_mcontext.mc_fpregs.fp_cr;
104+
__memcpy(vc->vregs, ctx->uc_mcontext.mc_fpregs.fp_q, 32 * 16);
105+
}
106+
#endif /* __aarch64__ */
107+
108+
//
109+
// INVOKE SIGNAL HANDLER
110+
//
111+
85112
((sigaction_f)(__executable_start + rva))(sig, &g.si, &g.uc);
113+
114+
//
115+
// TRANSLATE LINUX SIGNAL TO FREEBSD SIGNAL
116+
//
117+
86118
ctx->uc_stack.ss_sp = g.uc.uc_stack.ss_sp;
87119
ctx->uc_stack.ss_size = g.uc.uc_stack.ss_size;
88120
ctx->uc_stack.ss_flags = g.uc.uc_stack.ss_flags;
89121
ctx->uc_flags = g.uc.uc_flags;
90122
ctx->uc_sigmask[0] = g.uc.uc_sigmask;
91123
ctx->uc_sigmask[1] = g.uc.uc_sigmask >> 32;
124+
125+
#ifdef __x86_64__
92126
ctx->uc_mcontext.mc_rdi = g.uc.uc_mcontext.rdi;
93127
ctx->uc_mcontext.mc_rsi = g.uc.uc_mcontext.rsi;
94128
ctx->uc_mcontext.mc_rdx = g.uc.uc_mcontext.rdx;
@@ -112,13 +146,24 @@ privileged void __sigenter_freebsd(int sig, struct siginfo_freebsd *freebsdinfo,
112146
ctx->uc_mcontext.mc_rip = g.uc.uc_mcontext.rip;
113147
ctx->uc_mcontext.mc_rsp = g.uc.uc_mcontext.rsp;
114148
__repmovsb(&ctx->uc_mcontext.mc_fpstate, &g.uc.__fpustate, 512);
149+
#endif /* __x86_64__ */
150+
151+
#ifdef __aarch64__
152+
__memcpy(&ctx->uc_mcontext.mc_gpregs, g.uc.uc_mcontext.regs, 34 * 8);
153+
struct fpsimd_context *vc =
154+
(struct fpsimd_context *)g.uc.uc_mcontext.__reserved;
155+
if (vc->head.magic == FPSIMD_MAGIC) {
156+
ctx->uc_mcontext.mc_flags |= _MC_FP_VALID;
157+
ctx->uc_mcontext.mc_fpregs.fp_sr = vc->fpsr;
158+
ctx->uc_mcontext.mc_fpregs.fp_cr = vc->fpcr;
159+
__memcpy(ctx->uc_mcontext.mc_fpregs.fp_q, vc->vregs, 32 * 16);
160+
}
161+
#endif /* __aarch64__ */
162+
163+
// done
115164
}
116165
}
117-
/*
118-
* When the FreeBSD kernel invokes this signal handler it pushes a
119-
* trampoline on the stack which does two things: 1) it calls this
120-
* function, and 2) calls sys_sigreturn() once this returns.
121-
*/
166+
// When the FreeBSD kernel invokes this signal handler it pushes a
167+
// trampoline on the stack which does two things: 1) it calls this
168+
// function, and 2) calls sys_sigreturn() once this returns.
122169
}
123-
124-
#endif /* __x86_64__ */

0 commit comments

Comments
 (0)