Get LIBC_RUNTIME and LIBC_CALLS building on aarch64

This commit is contained in:
Justine Tunney 2023-05-09 01:56:56 -07:00
parent 7e46645193
commit e5e3cdf447
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
1200 changed files with 5341 additions and 3677 deletions

View file

@ -20,6 +20,7 @@
#include "libc/calls/struct/sigset.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/sig.h"
#ifdef __x86_64__
// kudos rich felker for the brilliant design
_Hide int __sigsetjmp_tail(sigjmp_buf jb, int rc) {
@ -30,3 +31,5 @@ _Hide int __sigsetjmp_tail(sigjmp_buf jb, int rc) {
_npassert(!sigprocmask(SIG_SETMASK, rc ? p : 0, rc ? 0 : p));
return rc;
}
#endif /* __x86_64__ */

View file

@ -21,37 +21,55 @@
// Invokes clone() system call on GNU/Systemd.
//
// @param rdi is flags
// @param rsi is top of stack
// @param rdx is ptid
// @param rcx is ctid
// @param r8 is tls
// @param r9 is func(void*,int)→int
// @param 8(rsp) is arg
// @param rdi x0 is flags
// @param rsi x1 is top of stack
// @param rdx x2 is ptid
// @param rcx x3 is ctid
// @param r8 x4 is tls
// @param r9 x5 is func(void*,int)→int
// @param 8(rsp) x6 is arg
// @return tid of child on success, or -errno on error
sys_clone_linux:
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
push %rbx
mov %rcx,%r10
mov 16(%rbp),%rbx
mov $56,%eax # __NR_clone
mov $56,%eax // __NR_clone
syscall
test %rax,%rax
jz 2f
0: pop %rbx
pop %rbp
ret
2: xor %ebp,%ebp # child thread
mov %rbx,%rdi # arg
mov %r10,%r15 # experiment
mov (%r10),%esi # tid
call *%r9 # func(arg,tid)
xchg %eax,%edi # func(arg,tid) exitcode
mov (%r15),%eax # experiment
test %eax,%eax # experiment
jz 1f # experiment
mov $60,%eax # __NR_exit(exitcode)
2: xor %ebp,%ebp // child thread
mov %rbx,%rdi // arg
mov %r10,%r15 // experiment
mov (%r10),%esi // tid
call *%r9 // func(arg,tid)
xchg %eax,%edi // func(arg,tid) exitcode
mov (%r15),%eax // experiment
test %eax,%eax // experiment
jz 1f // experiment
mov $60,%eax // __NR_exit(exitcode)
syscall
1: hlt # ctid was corrupted by program!
1: hlt // ctid was corrupted by program!
#elif defined(__aarch64__)
and x1,x1,#-16 // align stack
stp x5,x6,[x1,#-16]! // save func and arg
mov x8,x3 // swap x3 and x4
mov x3,x4 // swap x3 and x4
mov x4,x8 // swap x3 and x4
mov x8,#220 // __NR_clone
svc #0
cbz x0,2f
ret
2: ldp x1,x0,[sp],#16 // child thread
blr x1
mov x8,#93 // __NR_exit
svc #0
#else
#error "unsupported architecture"
#endif
.endfn sys_clone_linux,globl,hidden

View file

@ -49,6 +49,8 @@
#include "libc/thread/tls2.h"
#include "libc/thread/xnu.internal.h"
#ifdef __x86_64__
#define __NR_thr_new 455
#define __NR_clone_linux 56
#define __NR__lwp_create 309
@ -425,6 +427,8 @@ static int CloneNetbsd(int (*func)(void *, int), char *stk, size_t stksz,
}
}
#endif /* __x86_64__ */
////////////////////////////////////////////////////////////////////////////////
// GNU/SYSTEMD
@ -594,6 +598,7 @@ errno_t clone(void *func, void *stk, size_t stksz, int flags, void *arg,
CLONE_SIGHAND)) {
STRACE("clone flag unsupported on this platform");
rc = EINVAL;
#ifdef __x86_64__
} else if (IsXnu()) {
rc = CloneXnu(func, stk, stksz, flags, arg, tls, ptid, ctid);
} else if (IsFreebsd()) {
@ -604,6 +609,7 @@ errno_t clone(void *func, void *stk, size_t stksz, int flags, void *arg,
rc = CloneOpenbsd(func, stk, stksz, flags, arg, tls, ptid, ctid);
} else if (IsWindows()) {
rc = CloneWindows(func, stk, stksz, flags, arg, tls, ptid, ctid);
#endif /* __x86_64__ */
} else {
rc = ENOSYS;
}

View file

@ -23,6 +23,7 @@
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/consts/map.h"
#include "libc/dce.h"
#ifdef __x86_64__
.text.startup
// Cosmopolitan runtime.
@ -215,3 +216,5 @@ cosmo: push %rbp
1: .init.end 307,_init_printargs
#endif
#endif
#endif /* __x86_64__ */

59
libc/runtime/cosmo2.c Normal file
View file

@ -0,0 +1,59 @@
/*-*- 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/runtime/runtime.h"
#ifndef __x86_64__
int main(int, char **, char **) __attribute__((__weak__));
#if 0
static inline long sys_set_tid_address(int *t) {
register long res asm("x0");
register long arg asm("x0") = (long)t;
asm volatile("mov\tx8,%1\n\t"
"svc\t0"
: "=r"(res)
: "i"(96), "r"(arg)
: "x8", "memory");
return res;
}
#endif
void cosmo(long *sp) {
int argc;
char **argv, **envp;
unsigned long *auxv;
argc = *sp;
argv = (char **)(sp + 1);
envp = (char **)(sp + 1 + argc + 1);
auxv = (unsigned long *)(sp + 1 + argc + 1);
for (;;) {
if (!*auxv++) {
break;
}
}
__argc = argc;
__argv = argv;
__envp = envp;
__auxv = auxv;
environ = envp;
if (argc) program_invocation_name = argv[0];
exit(main(argc, argv, envp));
}
#endif /* __aarch64__ */

View file

@ -31,6 +31,8 @@
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#ifdef __x86_64__
/* TODO: Why can't we change CR3? Could it really need PML5T? */
/* TODO: Why does QEMU in UEFI mode take ten seconds to boot? */
@ -257,3 +259,5 @@ __msabi noasan EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
_EfiPostboot(mm, pml4t, Args, ArgBlock->Args);
unreachable;
}
#endif /* __x86_64__ */

View file

@ -58,6 +58,8 @@
#include "libc/sysv/errfuns.h"
#include "libc/thread/tls.h"
#ifdef __x86_64__
STATIC_YOINK("_check_sigchld");
extern int64_t __wincrashearly;
@ -361,3 +363,5 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
}
return rc;
}
#endif /* __x86_64__ */

View file

@ -20,6 +20,8 @@
.privileged
ftrace_hook:
#ifdef __x86_64__
cmp $0,__ftrace(%rip)
jg 1f
ret
@ -65,5 +67,43 @@ ftrace_hook:
movaps 0x60(%rsp),%xmm6
movaps 0x70(%rsp),%xmm7
leave
#elif defined(__aarch64__)
stp x0,x1,[sp,#-16]!
stp x2,x3,[sp,#-16]!
stp x4,x5,[sp,#-16]!
stp x6,x7,[sp,#-16]!
stp x8,x9,[sp,#-16]!
stp x10,x11,[sp,#-16]!
stp x12,x13,[sp,#-16]!
stp x14,x15,[sp,#-16]!
stp x16,x17,[sp,#-16]!
stp x18,x19,[sp,#-16]!
stp x20,x21,[sp,#-16]!
stp x22,x23,[sp,#-16]!
stp x24,x25,[sp,#-16]!
stp x26,x27,[sp,#-16]!
stp x28,x29,[sp,#-16]!
str x30,[sp,#-16]!
bl ftracer
ldr x30,[sp,#16]!
ldp x28,x29,[sp,#16]!
ldp x26,x27,[sp,#16]!
ldp x24,x25,[sp,#16]!
ldp x22,x23,[sp,#16]!
ldp x20,x21,[sp,#16]!
ldp x18,x19,[sp,#16]!
ldp x16,x17,[sp,#16]!
ldp x14,x15,[sp,#16]!
ldp x12,x13,[sp,#16]!
ldp x10,x11,[sp,#16]!
ldp x8,x9,[sp,#16]!
ldp x6,x7,[sp,#16]!
ldp x4,x5,[sp,#16]!
ldp x2,x3,[sp,#16]!
ldp x0,x1,[sp,#16]!
#endif /* __x86_64__ */
ret
.endfn ftrace_hook,globl

View file

@ -25,6 +25,7 @@
#include "libc/macros.internal.h"
#include "libc/nexgen32e/stackframe.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/thread/tls.h"
@ -75,6 +76,7 @@ privileged void ftracer(void) {
struct CosmoTib *tib;
struct StackFrame *sf;
struct CosmoFtrace *ft;
if (__ftrace <= 0) return;
if (__tls_enabled) {
tib = __get_tls_privileged();
if (tib->tib_ftrace <= 0) return;

View file

@ -20,6 +20,8 @@
#include "libc/runtime/pc.internal.h"
#include "libc/runtime/runtime.h"
#ifdef __x86_64__
#define PUTC(C) \
do { \
while (!(inb(0x3F8 + UART_LSR) & UART_TTYTXR)) { \
@ -78,3 +80,5 @@ _Hide textreal void(MetalPrintf)(const char *fmt, ...) {
}
}
}
#endif /* __x86_64__ */

View file

@ -63,6 +63,8 @@
#include "tool/decode/lib/idname.h"
#include "tool/decode/lib/x86idnames.h"
#ifdef __x86_64__
STATIC_YOINK("strerror"); // for kprintf()
STATIC_YOINK("strsignal"); // for kprintf()
@ -711,3 +713,5 @@ textstartup void __printargs(const char *prologue) {
ftrace_enabled(+1);
errno = e;
}
#endif /* __x86_64__ */

View file

@ -113,6 +113,14 @@ o//libc/runtime/opensymboltable.greg.o: private \
OVERRIDE_CFLAGS += \
-Os
# these assembly files are safe to build on aarch64
o/$(MODE)/libc/runtime/vfork.o: libc/runtime/vfork.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/runtime/clone-linux.o: libc/runtime/clone-linux.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
o/$(MODE)/libc/runtime/ftrace-hook.o: libc/runtime/ftrace-hook.S
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $<
LIBC_RUNTIME_LIBS = $(foreach x,$(LIBC_RUNTIME_ARTIFACTS),$($(x)))
LIBC_RUNTIME_SRCS = $(foreach x,$(LIBC_RUNTIME_ARTIFACTS),$($(x)_SRCS))
LIBC_RUNTIME_HDRS = $(foreach x,$(LIBC_RUNTIME_ARTIFACTS),$($(x)_HDRS))

View file

@ -53,6 +53,9 @@
// @threadsafe
// @vforksafe
vfork:
#ifdef __x86_64__
#if !IsTiny()
push %rbp
mov %rsp,%rbp
@ -124,6 +127,19 @@ vfork:
pop %r9
pop %rbp
jmp 1b
#endif
#elif defined(__aarch64__)
mov x8,#220 // __NR_clone
mov x0,#0x4111 // SIGCHLD | CLONE_VM | CLONE_VFORK
mov x1,#0
svc 0
.hidden _sysret
b _sysret
#else
#error "architecture unsupported"
#endif
.endfn vfork,globl

View file

@ -41,6 +41,8 @@
#include "libc/runtime/winargs.internal.h"
#include "libc/sock/internal.h"
#ifdef __x86_64__
#if IsTiny()
__msabi extern typeof(CreateFileMapping) *const __imp_CreateFileMappingW;
__msabi extern typeof(MapViewOfFileEx) *const __imp_MapViewOfFileEx;
@ -245,3 +247,5 @@ __msabi textwindows int64_t WinMain(int64_t hInstance, int64_t hPrevInstance,
if (_weaken(WinMainForked)) _weaken(WinMainForked)();
WinMainNew(cmdline);
}
#endif /* __x86_64__ */