Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ARM64 Support #7

Draft
wants to merge 43 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a067216
Store "nop" Instructions Inside XTRACE_RESERVED_SPACE Macro
CuriousTommy Nov 2, 2023
69882db
Fix "undeclared function" Build Error
CuriousTommy Nov 2, 2023
a97ac60
Rework "epoll_create" & "sys_getpgrp" To Call Equivalent Methods
CuriousTommy Nov 2, 2023
3a3386a
printf_file_init: Replace "__NR_open" with "__NR_openat"
CuriousTommy Nov 2, 2023
d703bf3
_sig_restorer: Document Syscall Number & Throw Error If Arch Code Is …
CuriousTommy Nov 2, 2023
918b8fb
sigexe: Store States Inside A Struct & Misc Code Changes
CuriousTommy Nov 2, 2023
bbbbaaf
Workaround Issues Building Migcom on Linux
CuriousTommy Nov 2, 2023
6eab595
Update linux-generic.h
CuriousTommy Nov 2, 2023
e21adcc
sysctl_machdep: Don't Build x86 Specific Code On Non-x86 Platforms
CuriousTommy Nov 2, 2023
35f45a3
sigaction: Add Documentation. Throw Errors If Missing Code For Arch.
CuriousTommy Nov 3, 2023
cbe01d7
sigalstack: Add Documentation
CuriousTommy Nov 3, 2023
826432a
sigaction: Don't Build x86 Specific Code On Non-x86 Device & Throw Er…
CuriousTommy Nov 3, 2023
1e02200
Rework "stat" code to better support ARM64
CuriousTommy Nov 4, 2023
a2f80b1
Add ARM64 Support
CuriousTommy Nov 4, 2023
9f7364e
Force Build Failure If sys_thread_set_tsd_base Implementation Missing…
CuriousTommy Dec 10, 2023
9518292
Align __darling_handle_svc
CuriousTommy Dec 10, 2023
83f827a
Add Alignment For ARM64
CuriousTommy Dec 20, 2023
5b059eb
Don't Forget To Preserve Link Register
CuriousTommy Dec 20, 2023
934d9af
Implement ARM64 Support For "sys_thread_set_tsd_base"
CuriousTommy Dec 20, 2023
d806e28
Use TPIDR_EL0 Instead Of TPIDRRO_EL0
CuriousTommy Dec 20, 2023
d8ba7e4
Implement Platform Syscall For ARM64
CuriousTommy Dec 20, 2023
9b73823
syscall-tables.S: Formatting
CuriousTommy Dec 20, 2023
f6e9f91
Fixing Setting Linux Syscall Number
CuriousTommy Dec 24, 2023
d786856
Rework __linux_syscall
CuriousTommy Jan 2, 2024
f7f9c69
Add "__darling_thread_get_tsd" & "__darling_thread_set_tsd" Method
CuriousTommy Jan 24, 2024
8b33ee6
Use ___darling_thread_set_tsd & ___darling_thread_get_tsd Instead of …
CuriousTommy Jan 24, 2024
a1e0995
Make Sure NULL Value Is Set To "reserved_neon_context" & "reserved_es…
CuriousTommy Jan 29, 2024
d370896
Don't Forget To Dereference Table Pointer
CuriousTommy Feb 4, 2024
542043e
Fixup Code That Calls __darling_handle_svc
CuriousTommy Feb 4, 2024
59096eb
Set Carry Flag If Returned Value Is Between [-1, -4095]
CuriousTommy Feb 12, 2024
402aa37
Don't Use Other linux_stat On Non-x86-64 Platforms
CuriousTommy Feb 24, 2024
51f070b
Add linux_stat for ARM64
CuriousTommy Feb 24, 2024
4a32480
Handle Syscalls That Are Not Implemented. Reduce Number Of Instructio…
CuriousTommy Feb 25, 2024
dcba065
Only Include "sse" Flags For i386/x86_64
CuriousTommy May 22, 2024
de2f2be
[libsyscall] Add ARM64 Arch For MIG
CuriousTommy May 22, 2024
626ffcb
Have LINUX_O_* Constants Better Match Offical Implementation
CuriousTommy Jun 5, 2024
dc5db75
Add ARM64 Specific LINUX_O_* Constants
CuriousTommy Jun 5, 2024
0ff8532
Update "sys_thread_set_tsd_base" to use "__darling_thread_set_tsd"
CuriousTommy Jun 9, 2024
91d7d50
Make Sure Error Code Is Positive & Update Notes
CuriousTommy Jun 15, 2024
2a86389
Only Add "__attribute__((__packed__))" For "epoll_event" If Building …
CuriousTommy Jun 21, 2024
989284d
Add "__EPOLL_PACKED" For ARM64
CuriousTommy Jun 21, 2024
ac09590
Fix "__fork" & "__vfork"
CuriousTommy Jun 22, 2024
8b5c76f
Don't Store FD Values Into Clobbered X9 Register
CuriousTommy Jun 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bsd/arm/_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ typedef short __int16_t;
typedef unsigned short __uint16_t;
typedef int __int32_t;
typedef unsigned int __uint32_t;
#ifndef DARLING_LINUX_MIGCOM
typedef long long __int64_t;
typedef unsigned long long __uint64_t;
#endif

typedef long __darwin_intptr_t;
typedef unsigned int __darwin_natural_t;
Expand Down Expand Up @@ -52,9 +54,14 @@ typedef int __darwin_ct_rune_t; /* ct_rune_t */
typedef union {
char __mbstate8[128];
long long _mbstateL; /* for alignment */
#ifndef DARLING_LINUX_MIGCOM
} __mbstate_t;

typedef __mbstate_t __darwin_mbstate_t; /* mbstate_t */
#else
// Redefinition conflict
} __darwin_mbstate_t;
#endif

#if defined(__PTRDIFF_TYPE__)
typedef __PTRDIFF_TYPE__ __darwin_ptrdiff_t; /* ptr1 - ptr2 */
Expand Down
6 changes: 5 additions & 1 deletion darling/src/libsystem_kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(emulation/linux)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse -msse2 -msse3 -w -fblocks -ffreestanding")
if(TARGET_i386 OR TARGET_x86_64)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse -msse2 -msse3")
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w -fblocks -ffreestanding")

# Why -bind_at_load is necessary:
# I couldn't find something like -Bsymbolic in Apple's ld
Expand Down
38 changes: 37 additions & 1 deletion darling/src/libsystem_kernel/emulation/linux/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,47 @@ long __unknown_syscall_machdep(int nr, ...)
}

VISIBLE
int __linux_syscall(int nr, long a1, long a2, long a3, long a4, long a5, long a6)
int __linux_syscall_6args(int nr, long a1, long a2, long a3, long a4, long a5, long a6)
{
return linux_syscall(a1, a2, a3, a4, a5, a6, nr);
}

VISIBLE
int __linux_syscall_noargs(int nr)
{
return __linux_syscall_6args(nr, 0, 0, 0, 0, 0, 0);
}

VISIBLE
int __linux_syscall_1arg(int nr, long a1)
{
return __linux_syscall_6args(nr, a1, 0, 0, 0, 0, 0);
}

VISIBLE
int __linux_syscall_2args(int nr, long a1, long a2)
{
return __linux_syscall_6args(nr, a1, a2, 0, 0, 0, 0);
}

VISIBLE
int __linux_syscall_3args(int nr, long a1, long a2, long a3)
{
return __linux_syscall_6args(nr, a1, a2, a3, 0, 0, 0);
}

VISIBLE
int __linux_syscall_4args(int nr, long a1, long a2, long a3, long a4)
{
return __linux_syscall_6args(nr, a1, a2, a3, a4, 0, 0);
}

VISIBLE
int __linux_syscall_5args(int nr, long a1, long a2, long a3, long a4, long a5)
{
return __linux_syscall_6args(nr, a1, a2, a3, a4, a5, 0);
}

#ifdef __TESTING
void _start()
{
Expand Down
9 changes: 7 additions & 2 deletions darling/src/libsystem_kernel/emulation/linux/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ long linux_syscall(long a1, long a2, long a3, long a4, long a5, long a6, int nr)

#ifndef BUILDING_BASE_C

VISIBLE
int __linux_syscall(int nr, ...);
VISIBLE int __linux_syscall_noargs(int);
VISIBLE int __linux_syscall_1arg(int,long);
VISIBLE int __linux_syscall_2args(int,long,long);
VISIBLE int __linux_syscall_3args(int,long,long,long);
VISIBLE int __linux_syscall_4args(int,long,long,long,long);
VISIBLE int __linux_syscall_5args(int,long,long,long,long,long);
VISIBLE int __linux_syscall_6args(int,long,long,long,long,long,long);

#endif /* BUILDING_BASE_C */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ void pthread_entry_point_wrapper(void* self, int thread_port, void* funptr,
register void* arg4 asm("edx") = funarg;
register unsigned long arg5 asm("edi") = stack_addr;
register unsigned int arg6 asm("esi") = flags;
#elif __arm64__
register void* arg1 asm("x0") = self;
register int arg2 asm("w1") = thread_port;
register void* arg3 asm("x2") = funptr;
register void* arg4 asm("x3") = funarg;
register unsigned long arg5 asm("x4") = stack_addr;
register unsigned int arg6 asm("w5") = flags;
#endif

#if __x86_64__
Expand All @@ -105,6 +112,14 @@ void pthread_entry_point_wrapper(void* self, int thread_port, void* funptr,
"r"(arg1),"r"(arg2),"r"(arg3),"r"(arg4),"r"(arg5),"r"(arg6),
[pthread_entry_point] "rmi"(pthread_entry_point)
);
#elif __arm64__
__asm__ __volatile__ (
"br %[pthread_entry_point]\n"
::
// Arguments follow the usual arm64 calling conventions.
"r"(arg1),"r"(arg2),"r"(arg3),"r"(arg4),"r"(arg5),"r"(arg6),
[pthread_entry_point] "r"(pthread_entry_point)
);
#else
#error "Missing assembly for architecture"
// pthread_entry_point(self, thread_port, funptr, funarg, stack_addr, flags);
Expand Down Expand Up @@ -146,6 +161,13 @@ void wqueue_entry_point_asm_jump(void* self, int thread_port, void* stackaddr,
register void* arg4 asm("edx") = item;
register int arg5 asm("edi") = reuse;
register int arg6 asm("esi") = nevents;
#elif __arm64__
register void* arg1 asm("x0") = self;
register int arg2 asm("w1") = thread_port;
register void* arg3 asm("x2") = stackaddr;
register void* arg4 asm("x3") = item;
register int arg5 asm("w4") = reuse;
register int arg6 asm("w5") = nevents;
#endif

#if __x86_64__
Expand All @@ -170,6 +192,14 @@ void wqueue_entry_point_asm_jump(void* self, int thread_port, void* stackaddr,
"r"(arg1),"r"(arg2),"r"(arg3),"r"(arg4),"r"(arg5),"r"(arg6),
[wqueue_entry_point] "rmi"(wqueue_entry_point)
);
#elif defined(__arm64__)
__asm__ __volatile__ (
"br %[wqueue_entry_point]\n"
::
// Arguments follow the usual arm64 calling conventions.
"r"(arg1),"r"(arg2),"r"(arg3),"r"(arg4),"r"(arg5),"r"(arg6),
[wqueue_entry_point] "r"(wqueue_entry_point)
);
#else
#error "Missing assembly for architecture"
// wqueue_entry_point(self, thread_port, stackaddr, item, reuse, nevents);
Expand Down
11 changes: 11 additions & 0 deletions darling/src/libsystem_kernel/emulation/linux/elfcalls_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <elfcalls.h>
#include <dlfcn.h>
#include "simple.h"
#include "base.h"

extern struct elf_calls* _elfcalls;

Expand Down Expand Up @@ -92,6 +93,16 @@ void __dserver_close_socket(int socket) {
return elfcalls()->dserver_close_socket(socket);
};

VISIBLE
void* __darling_thread_get_tsd() {
return elfcalls()->get_tsd();
}

VISIBLE
void __darling_thread_set_tsd(void* value) {
elfcalls()->set_tsd(value);
}

int __dserver_get_process_lifetime_pipe() {
return elfcalls()->dserver_get_process_lifetime_pipe();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ int __dserver_per_thread_socket(void);
void __dserver_per_thread_socket_refresh(void);
void __dserver_close_socket(int socket);

void* __darling_thread_get_tsd();
void __darling_thread_set_tsd(void* value);

int __dserver_get_process_lifetime_pipe(void);
int __dserver_process_lifetime_pipe_refresh(void);
void __dserver_close_process_lifetime_pipe(int fd);
Expand Down
18 changes: 2 additions & 16 deletions darling/src/libsystem_kernel/emulation/linux/ext/epoll_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,8 @@
extern long cerror(int __err);

VISIBLE
int epoll_create (int __size)
int epoll_create (__unused int __size)
{
#if defined(__NR_epoll_create)
int rv;

rv = LINUX_SYSCALL(__NR_epoll_create, __size);
if (rv < 0)
{
cerror(errno_linux_to_bsd(-rv));
return -1;
}

return rv;
#else
// The size argument in __NR_epoll_create is ignored
return epoll_create1(0);
#endif
return epoll_create1(0);
}

18 changes: 11 additions & 7 deletions darling/src/libsystem_kernel/emulation/linux/ext/sys/epoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
#define LINUX_EPOLL_H
#include <stdint.h>

#define epoll_create __linux_epoll_create
#define epoll_create1 __linux_epoll_create1
#define epoll_ctl __linux_epoll_ctl
#define epoll_wait __linux_epoll_wait
#define epoll_pwait __linux_epoll_pwait

#ifndef __THROW
# define __THROW
#endif
Expand All @@ -34,6 +28,14 @@ enum EPOLL_EVENTS
#define EPOLL_CTL_DEL 2
#define EPOLL_CTL_MOD 3

#if defined(__x86_64__) || defined(__i386__)
#define __EPOLL_PACKED __attribute__ ((__packed__))
#elif defined(__arm64__)
#define __EPOLL_PACKED
#else
#error "Missing __EPOLL_PACKED definition for arch"
#endif

typedef union epoll_data
{
void *ptr;
Expand All @@ -46,10 +48,12 @@ struct epoll_event
{
uint32_t events;
epoll_data_t data;
} __attribute__((packed));
} __EPOLL_PACKED;

extern int epoll_create (int __size) __THROW;

extern int epoll_create1 (int __flags);

extern int epoll_ctl (int __epfd, int __op, int __fd,
struct epoll_event *__event) __THROW;

Expand Down
60 changes: 58 additions & 2 deletions darling/src/libsystem_kernel/emulation/linux/fcntl/open.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,77 @@
long sys_open(const char* filename, int flags, unsigned int mode);
long sys_open_nocancel(const char* filename, int flags, unsigned int mode);


#if defined(__arm64__)
#define LINUX_O_DIRECTORY 040000
#define LINUX_O_NOFOLLOW 0100000
#define LINUX_O_DIRECT 0200000
#define LINUX_O_LARGEFILE 0400000
#endif

// Based on <bits/fcntl-linux.h>
#define LINUX_O_ACCMODE 0003
#define LINUX_O_RDONLY 00
#define LINUX_O_WRONLY 01
#define LINUX_O_RDWR 02

#ifndef LINUX_O_CREAT
#define LINUX_O_CREAT 0100
#endif
#ifndef LINUX_O_EXCL
#define LINUX_O_EXCL 0200
#endif
#ifndef LINUX_O_NOCTTY
#define LINUX_O_NOCTTY 0400
#endif
#ifndef LINUX_O_TRUNC
#define LINUX_O_TRUNC 01000
#endif
#ifndef LINUX_O_APPEND
#define LINUX_O_APPEND 02000
#endif
#ifndef LINUX_O_NONBLOCK
#define LINUX_O_NONBLOCK 04000
#endif
#ifndef LINUX_O_NDELAY
#define LINUX_O_NDELAY O_NONBLOCK
#endif
#ifndef LINUX_O_SYNC
#define LINUX_O_SYNC 04010000
#endif
#ifndef LINUX_O_ASYNC
#define LINUX_O_ASYNC 020000
#define LINUX_O_CLOEXEC 02000000
#endif
#ifndef LINUX_O_LARGEFILE
#define LINUX_O_LARGEFILE 0100000
#define LINUX_O_NOFOLLOW 0400000
#endif
#ifndef LINUX_O_DIRECTORY
#define LINUX_O_DIRECTORY 0200000
#endif
#ifndef LINUX_O_NOFOLLOW
#define LINUX_O_NOFOLLOW 0400000
#endif
#ifndef LINUX_O_CLOEXEC
#define LINUX_O_CLOEXEC 02000000
#endif
#ifndef LINUX_O_DIRECT
#define LINUX_O_DIRECT 040000
#endif
#ifndef LINUX_O_NOATIME
#define LINUX_O_NOATIME 01000000
#endif
#ifndef LINUX_O_PATH
#define LINUX_O_PATH 010000000
#endif
#ifndef LINUX_O_DSYNC
#define LINUX_O_DSYNC 010000
#endif
#ifndef LINUX_O_TMPFILE
#define LINUX_O_TMPFILE (020000000 | LINUX_O_DIRECTORY)
#endif

#define LINUX_O_FSYNC LINUX_O_SYNC


#define BSD_O_RDONLY 0
#define BSD_O_WRONLY 1
Expand Down
8 changes: 8 additions & 0 deletions darling/src/libsystem_kernel/emulation/linux/linux-syscall.S
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
.globl _linux_syscall
.private_extern _linux_syscall

#if defined(__arm64__)
.balign 4
#endif
_linux_syscall:

#if defined(__x86_64__)
Expand Down Expand Up @@ -69,6 +72,11 @@ _linux_syscall:
pop %ebp
ret

#elif defined(__arm64__)
mov x8, x6
svc #0
ret

#else

# error Missing assembly!
Expand Down
Loading