Make more code aarch64 friendly

This commit is contained in:
Justine Tunney 2023-05-02 13:38:16 -07:00
parent ca2860947f
commit 2b73e72d59
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
568 changed files with 2197 additions and 1061 deletions

View file

@ -49,6 +49,7 @@
*/
privileged int getpriority(int which, unsigned who) {
int rc;
#ifdef __x86_64__
char cf;
if (IsLinux()) {
asm volatile("syscall"
@ -73,6 +74,23 @@ privileged int getpriority(int which, unsigned who) {
} else {
rc = sys_getpriority_nt(which, who);
}
#elif defined(__aarch64__)
register long r0 asm("x0") = (long)which;
register long r1 asm("x1") = (long)who;
register long res_x0 asm("x0");
asm volatile("mov\tx8,%1\n"
"svc\t0"
: "=r"(res_x0)
: "i"(141), "r"(r0), "r"(r1)
: "x8", "memory");
rc = res_x0;
if (rc >= 0) {
rc = NZERO - rc;
} else {
errno = -rc;
rc = -1;
}
#endif
STRACE("getpriority(%s, %u) → %d% m", DescribeWhichPrio(which), who, rc);
return rc;
}

View file

@ -22,6 +22,7 @@
#include "libc/sysv/consts/pr.h"
privileged bool __is_linux_2_6_23(void) {
#ifdef __x86_64__
int rc;
if (!IsLinux()) return false;
if (IsGenuineBlink()) return true;
@ -30,4 +31,7 @@ privileged bool __is_linux_2_6_23(void) {
: "0"(157), "D"(PR_GET_SECCOMP)
: "rcx", "r11", "memory");
return rc != -EINVAL;
#else
return true;
#endif
}

View file

@ -26,7 +26,6 @@
#include "libc/str/oldutf16.internal.h"
#include "libc/str/str.h"
#include "libc/str/thompike.h"
#include "libc/str/tpdecode.internal.h"
#include "libc/str/utf16.h"
#include "libc/sysv/errfuns.h"

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/asmflag.h"
@ -34,34 +35,52 @@
* features like ASAN memory safety and kprintf() won't work as well.
*/
privileged void *sys_mremap(void *p, size_t n, size_t m, int f, void *q) {
#ifdef __x86_64__
bool cf;
uintptr_t rax, rdi, rsi, rdx;
uintptr_t res, rdi, rsi, rdx;
register uintptr_t r8 asm("r8");
register uintptr_t r10 asm("r10");
if (IsLinux()) {
r10 = f;
r8 = (uintptr_t)q;
asm("syscall"
: "=a"(rax)
: "=a"(res)
: "0"(0x019), "D"(p), "S"(n), "d"(m), "r"(r10), "r"(r8)
: "rcx", "r11", "memory", "cc");
if (rax > -4096ul) errno = -rax, rax = -1;
if (res > -4096ul) errno = -res, res = -1;
} else if (IsNetbsd()) {
if (f & MREMAP_MAYMOVE) {
rax = 0x19B;
res = 0x19B;
r10 = m;
r8 = (f & MREMAP_FIXED) ? MAP_FIXED : 0;
asm(CFLAG_ASM("syscall")
: CFLAG_CONSTRAINT(cf), "+a"(rax), "=d"(rdx)
: CFLAG_CONSTRAINT(cf), "+a"(res), "=d"(rdx)
: "D"(p), "S"(n), "2"(q), "r"(r10), "r"(r8)
: "rcx", "r9", "r11", "memory", "cc");
if (cf) errno = rax, rax = -1;
if (cf) errno = res, res = -1;
} else {
rax = einval();
res = einval();
}
} else {
rax = enosys();
res = enosys();
}
KERNTRACE("sys_mremap(%p, %'zu, %'zu, %#b, %p) → %p% m", p, n, m, f, q, rax);
return (void *)rax;
#elif defined(__aarch64__)
long res;
register long r0 asm("x0") = (long)p;
register long r1 asm("x1") = (long)n;
register long r2 asm("x2") = (long)m;
register long r3 asm("x3") = (long)f;
register long r4 asm("x4") = (long)q;
register long res_x0 asm("x0");
asm volatile("mov\tx8,%1\n"
"svc\t0"
: "=r"(res_x0)
: "i"(216), "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4)
: "x8", "memory");
res = _sysret64(res_x0);
#else
#error "arch unsupported"
#endif
KERNTRACE("sys_mremap(%p, %'zu, %'zu, %#b, %p) → %p% m", p, n, m, f, q, res);
return (void *)res;
}

View file

@ -18,6 +18,7 @@
*/
#include "libc/intrin/directmap.internal.h"
#include "libc/runtime/pc.internal.h"
#ifdef __x86_64__
noasan int sys_munmap_metal(void *addr, size_t size) {
size_t i;
@ -36,3 +37,5 @@ noasan int sys_munmap_metal(void *addr, size_t size) {
}
return 0;
}
#endif

View file

@ -35,6 +35,7 @@
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/pr.h"
#include "libc/sysv/consts/prot.h"
#ifdef __x86_64__
/**
* @fileoverview OpenBSD pledge() Polyfill Payload for GNU/Systemd
@ -2060,3 +2061,5 @@ privileged int sys_pledge_linux(unsigned long ipromises, int mode) {
return rc;
}
#endif

View file

@ -24,6 +24,7 @@
#include "libc/sock/internal.h"
#include "libc/sock/struct/pollfd.h"
#include "libc/sysv/consts/poll.h"
#ifdef __x86_64__
int sys_poll_metal(struct pollfd *fds, size_t nfds, unsigned timeout_ms) {
int rc;
@ -80,3 +81,5 @@ int sys_poll_metal(struct pollfd *fds, size_t nfds, unsigned timeout_ms) {
}
return rc;
}
#endif

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/describeflags.internal.h"
@ -41,6 +42,7 @@ privileged int prctl(int operation, ...) {
d = va_arg(va, intptr_t);
va_end(va);
#ifdef __x86_64__
if (IsLinux()) {
asm volatile("mov\t%5,%%r10\n\t"
"mov\t%6,%%r8\n\t"
@ -52,6 +54,22 @@ privileged int prctl(int operation, ...) {
} else {
rc = enosys();
}
#elif defined(__aarch64__)
register long r0 asm("x0") = (long)operation;
register long r1 asm("x1") = (long)a;
register long r2 asm("x2") = (long)b;
register long r3 asm("x3") = (long)c;
register long r4 asm("x4") = (long)d;
register long res_x0 asm("x0");
asm volatile("mov\tx8,%1\n"
"svc\t0"
: "=r"(res_x0)
: "i"(167), "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4)
: "x8", "memory");
rc = _sysret32(res_x0);
#else
#error "arch unsupported"
#endif
#ifdef SYSDEBUG
if (operation == PR_CAPBSET_READ || operation == PR_CAPBSET_DROP) {

View file

@ -20,6 +20,7 @@
#include "libc/calls/struct/iovec.internal.h"
#include "libc/nexgen32e/uart.internal.h"
#include "libc/runtime/pc.internal.h"
#ifdef __x86_64__
static bool IsDataAvailable(struct Fd *fd) {
return inb(fd->handle + UART_LSR) & UART_TTYDA;
@ -47,3 +48,5 @@ ssize_t sys_readv_serial(struct Fd *fd, const struct iovec *iov, int iovlen) {
return 0;
}
}
#endif

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/struct/seccomp.h"
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/describeflags.internal.h"
@ -36,6 +37,7 @@
*/
privileged int seccomp(unsigned operation, unsigned flags, void *args) {
int rc;
#ifdef __x86_64__
if (IsLinux()) {
asm volatile("syscall"
: "=a"(rc)
@ -62,6 +64,20 @@ privileged int seccomp(unsigned operation, unsigned flags, void *args) {
} else {
rc = enosys();
}
#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"
"svc\t0"
: "=r"(res_x0)
: "i"(211), "r"(r0), "r"(r1), "r"(r2)
: "x8", "memory");
rc = _sysret32(res_x0);
#else
#error "arch unsupported"
#endif
STRACE("seccomp(%s, %#x, %p) → %d% m", DescribeSeccompOperation(operation),
flags, args, rc);
return rc;

View file

@ -31,6 +31,7 @@
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/sa.h"
#ifdef __x86_64__
/**
* @fileoverview XNU kernel callback normalization.
@ -527,3 +528,5 @@ privileged void __sigenter_xnu(void *fn, int infostyle, int sig,
: "rcx", "r11", "memory", "cc");
notpossible;
}
#endif

View file

@ -10,6 +10,9 @@ COSMOPOLITAN_C_START_
cosmopolitan § syscalls » system five » structless synthetic jump slots
*/
int _sysret32(long) asm("_sysret");
long _sysret64(long) asm("_sysret");
axdx_t __sys_fork(void) _Hide;
axdx_t __sys_pipe(i32[hasatleast 2], i32) _Hide;
axdx_t sys_getpid(void) _Hide;
@ -62,7 +65,6 @@ i32 sys_getresgid(u32 *, u32 *, u32 *) _Hide;
i32 sys_getresuid(u32 *, u32 *, u32 *) _Hide;
i32 sys_getsid(i32) _Hide;
i32 sys_gettid(void) _Hide;
i32 sys_ioctl(i32, u64, ...) _Hide;
i32 sys_ioctl_cp(i32, u64, ...) _Hide;
i32 sys_issetugid(void) _Hide;
i32 sys_kill(i32, i32, i32) _Hide;
@ -131,7 +133,6 @@ i64 sys_readlink(const char *, char *, u64) _Hide;
i64 sys_readlinkat(i32, const char *, char *, u64) _Hide;
i64 sys_sendfile(i32, i32, i64 *, u64) _Hide;
i64 sys_splice(i32, i64 *, i32, i64 *, u64, u32) _Hide;
i64 sys_write(i32, const void *, u64) _Hide;
u32 sys_getegid(void) _Hide;
u32 sys_geteuid(void) _Hide;
u32 sys_getgid(void) _Hide;
@ -141,6 +142,45 @@ void *__sys_mmap(void *, u64, u32, u32, i64, i64, i64) _Hide;
void *sys_mremap(void *, u64, u64, i32, void *) _Hide;
void sys_exit(i32) _Hide;
#ifdef __x86_64__
i64 sys_write(i32, const void *, u64) _Hide;
#elif defined(__aarch64__)
static inline ssize_t sys_write(int f, const void *b, size_t c) {
register long r0 asm("x0") = (long)f;
register long r1 asm("x1") = (long)b;
register long r2 asm("x2") = (long)c;
register long res_x0 asm("x0");
asm volatile("mov\tx8,%1\n"
"svc\t0"
: "=r"(res_x0)
: "i"(64), "r"(r0), "r"(r1), "r"(r2)
: "x8", "memory");
return _sysret64(res_x0);
}
#endif
#ifdef __x86_64__
i32 sys_ioctl(i32, u64, ...) _Hide;
#elif defined(__aarch64__)
static inline int sys_ioctl(int d, int r, ...) {
void *a;
va_list va;
va_start(va, r);
a = va_arg(va, void *);
va_end(va);
register long r0 asm("x0") = (long)d;
register long r1 asm("x1") = (long)r;
register long r2 asm("x2") = (long)a;
register long res_x0 asm("x0");
asm volatile("mov\tx8,%1\n"
"svc\t0"
: "=r"(res_x0)
: "i"(29), "r"(r0), "r"(r1), "r"(r2)
: "x8", "memory");
return _sysret32(res_x0);
}
#endif
#undef i32
#undef i64
#undef u32

View file

@ -17,7 +17,8 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/intrin/lockxchg.h"
// TODO(jart): DELETE
/**
* Deletes file.
@ -29,6 +30,7 @@
* @asyncsignalsafe
*/
int unlink_s(const char **namep) {
const char *name = NULL;
return unlink(lockxchg(namep, &name));
const char *name = *namep;
*namep = 0;
return unlink(name);
}

View file

@ -21,6 +21,7 @@
#include "libc/calls/struct/iovec.internal.h"
#include "libc/nexgen32e/uart.internal.h"
#include "libc/runtime/pc.internal.h"
#ifdef __x86_64__
ssize_t sys_writev_serial(struct Fd *fd, const struct iovec *iov, int iovlen) {
size_t i, j, wrote = 0;
@ -35,3 +36,5 @@ ssize_t sys_writev_serial(struct Fd *fd, const struct iovec *iov, int iovlen) {
}
return wrote;
}
#endif