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 Support For Building core Component And Executing Hello World On ARM64 #1450

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e78b751
Add `TARGET_ARM64` Option. Add Arch And Build Target.
CuriousTommy Nov 6, 2021
4efe474
[cmake/compiler_include] Use omp-tools.h Instead
CuriousTommy May 23, 2023
66136c5
Fallback To "stdbool.h" Header If Build For Non-x86 Platform
CuriousTommy May 24, 2023
110ece2
Fix cpuid typo
CuriousTommy May 24, 2023
3793081
[coredump] Define "__arm64__" If Building For arm64
CuriousTommy May 24, 2023
d0c2981
[xtrace] Add Documentation On How "hook" Struct Works
CuriousTommy Nov 5, 2023
1d50645
[xtrace] Throw Error If "hook" Code Is Missing For Arch
CuriousTommy Nov 5, 2023
eefe2b1
[xtrace] Implement hook support for ARM64
CuriousTommy Nov 5, 2023
9270f96
Add Missing Headers Required For Building "core" on ARM64
CuriousTommy Nov 5, 2023
02be3f7
[libm] Fix Building For ARM64 & Add Stubs
CuriousTommy Nov 5, 2023
3b6e38f
[startup/mldr] Add Missing ARM64 Headers
CuriousTommy Nov 5, 2023
a5091d0
[launchd] Set ARM_THREAD_STATE For ARM64
CuriousTommy Nov 5, 2023
20221da
[mldr] Add Documentation For "entryPoint" And Throw Error If Implemen…
CuriousTommy Jan 1, 2024
7a349a0
[coredump] Implement WIP ARM64 Support
CuriousTommy Jan 2, 2024
b0322f0
[mldr] Store ARM64 "pc" Into entryPoint
CuriousTommy Jan 2, 2024
3068fb1
[mldr] Don't Include "cpuid.h" If Building For Non-x86 Platforms
CuriousTommy Jan 10, 2024
3f5e59c
[mldr] Only Execute "execv" Logic For "reexec32" On Supported Platforms
CuriousTommy Jan 11, 2024
7c561ba
[mldr] Rework How "load" And "setup_stack" Are Handled
CuriousTommy Jan 11, 2024
2f2a61e
[mldr] Avoid Building/Using x86 Specific Logic On Non-x86 Devices
CuriousTommy Jan 11, 2024
adc418a
[mldr] Implement WIP ARM64 Support
CuriousTommy Jan 11, 2024
17bab56
Update Submodules
CuriousTommy Jan 12, 2024
01c5f72
[coredump] Bug Fix
CuriousTommy Jan 12, 2024
0af99f1
[mldr] Bug Fix
CuriousTommy Jan 12, 2024
c633c12
[mldr] Add __arm64__ Macro If Building For ARM64
CuriousTommy Jan 22, 2024
47bace7
[mldr] Implement __darling_thread_get_tsd & __darling_thread_set_tsd
CuriousTommy Jan 24, 2024
90e903e
Add elfcalls_wrapper.h to SDK
CuriousTommy Jan 24, 2024
424286e
Update Submodule
CuriousTommy Jan 24, 2024
921cf8d
Update Submodules
CuriousTommy Mar 28, 2024
524fd54
Update Submodules
CuriousTommy May 25, 2024
7841e42
Update Submodules
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ enable_language(ASM-ATT)

option(TARGET_i386 "Enable i386 slices" ON)
option(TARGET_x86_64 "Enable x86_64 slices" ON)
option(TARGET_ARM64 "Enable arm64 slices" OFF)
option(DEBIAN_PACKAGING "Packaging for Debian" OFF)
option(ENABLE_TESTS "Install in-prefix unit tests" OFF)
option(REGENERATE_SDK "Regenerate Header Files For Open Source SDK" OFF)
Expand Down
5 changes: 5 additions & 0 deletions cmake/architecture.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
macro(generate_architecture)
if (TARGET_x86_64)
set(APPLE_ARCH_64BIT "x86_64")
elseif (TARGET_ARM64)
set(APPLE_ARCH_64BIT "arm64")
else ()
set(APPLE_ARCH_64BIT "")
endif ()
Expand All @@ -14,6 +16,9 @@ macro(generate_architecture)
if (TARGET_x86_64)
set(BUILD_TARGET_64BIT TRUE)
set(APPLE_TARGET_TRIPLET_64BIT "x86_64-apple-darwin20")
elseif (TARGET_ARM64)
set(BUILD_TARGET_64BIT TRUE)
set(APPLE_TARGET_TRIPLET_64BIT "arm64-apple-darwin20")
else ()
set(BUILD_TARGET_64BIT FALSE)
set(APPLE_TARGET_TRIPLET_64BIT "")
Expand Down
14 changes: 10 additions & 4 deletions cmake/compiler_include.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Detect the location of compiler-specific header files
# such as stdbool.h or xmmintrin.h
function(GetCompilerInclude OutputVar)
file(WRITE "${CMAKE_BINARY_DIR}/cinctest.c" "#include <cpuid.h>")
if (TARGET_x86_64 OR TARGET_i386)
set(COMPILER_INCLUDE_HEADER_NAME "cpuid")
else()
set(COMPILER_INCLUDE_HEADER_NAME "stdbool")
endif ()

file(WRITE "${CMAKE_BINARY_DIR}/cinctest.c" "#include <${COMPILER_INCLUDE_HEADER_NAME}.h>")
execute_process(COMMAND "${CMAKE_C_COMPILER}" -M "${CMAKE_BINARY_DIR}/cinctest.c"
RESULT_VARIABLE BuildResult
OUTPUT_VARIABLE BuildOutput
Expand All @@ -17,10 +23,10 @@ function(GetCompilerInclude OutputVar)
string(REGEX REPLACE "\n$" "" str "${str}")
# message(STATUS "Output: ${str}")

if (str MATCHES "cpuid.h$")
if (str MATCHES "${COMPILER_INCLUDE_HEADER_NAME}.h$")
# message(STATUS "Str matched: ${str}")
string(REGEX REPLACE "cpuid\\.h" "" IncPath "${str}")
endif (str MATCHES "cpuid.h$")
string(REGEX REPLACE "${COMPILER_INCLUDE_HEADER_NAME}\\.h" "" IncPath "${str}")
endif (str MATCHES "${COMPILER_INCLUDE_HEADER_NAME}.h$")
endforeach (str)

if (NOT IncPath)
Expand Down
2 changes: 1 addition & 1 deletion src/external/bootstrap_cmds
2 changes: 1 addition & 1 deletion src/external/corefoundation
2 changes: 1 addition & 1 deletion src/external/dyld
2 changes: 1 addition & 1 deletion src/external/libc
2 changes: 1 addition & 1 deletion src/external/libkqueue
2 changes: 1 addition & 1 deletion src/external/libplatform
Submodule libplatform updated 1 files
+3 −0 CMakeLists.txt
2 changes: 1 addition & 1 deletion src/external/libpthread
Submodule libpthread updated 1 files
+1 −1 src/CMakeLists.txt
2 changes: 1 addition & 1 deletion src/external/libunwind
Submodule libunwind updated 1 files
+5 −1 CMakeLists.txt
2 changes: 1 addition & 1 deletion src/external/libxpc
2 changes: 1 addition & 1 deletion src/external/objc4
2 changes: 1 addition & 1 deletion src/external/xnu
Submodule xnu updated 54 files
+7 −0 bsd/arm/_types.h
+5 −1 darling/src/libsystem_kernel/CMakeLists.txt
+37 −1 darling/src/libsystem_kernel/emulation/linux/base.c
+7 −2 darling/src/libsystem_kernel/emulation/linux/base.h
+30 −0 darling/src/libsystem_kernel/emulation/linux/bsdthread/bsdthread_register.c
+11 −0 darling/src/libsystem_kernel/emulation/linux/elfcalls_wrapper.c
+3 −0 darling/src/libsystem_kernel/emulation/linux/elfcalls_wrapper.h
+2 −16 darling/src/libsystem_kernel/emulation/linux/ext/epoll_create.c
+11 −7 darling/src/libsystem_kernel/emulation/linux/ext/sys/epoll.h
+58 −2 darling/src/libsystem_kernel/emulation/linux/fcntl/open.h
+8 −0 darling/src/libsystem_kernel/emulation/linux/linux-syscall.S
+82 −113 darling/src/libsystem_kernel/emulation/linux/linux-syscalls/linux-generic.h
+71 −4 darling/src/libsystem_kernel/emulation/linux/mach/darling_mach_syscall.S
+3 −0 darling/src/libsystem_kernel/emulation/linux/machdep/machdep-table.S
+5 −0 darling/src/libsystem_kernel/emulation/linux/machdep/tls.c
+36 −3 darling/src/libsystem_kernel/emulation/linux/misc/sysctl_machdep.c
+3 −1 darling/src/libsystem_kernel/emulation/linux/resources/dserver-rpc-defs.c
+1 −0 darling/src/libsystem_kernel/emulation/linux/select/select.c
+14 −3 darling/src/libsystem_kernel/emulation/linux/signal/sig_restorer.S
+136 −6 darling/src/libsystem_kernel/emulation/linux/signal/sigaction.c
+82 −3 darling/src/libsystem_kernel/emulation/linux/signal/sigaction.h
+2 −0 darling/src/libsystem_kernel/emulation/linux/signal/sigaltstack.h
+133 −40 darling/src/libsystem_kernel/emulation/linux/signal/sigexc.c
+11 −0 darling/src/libsystem_kernel/emulation/linux/stat/common.c
+45 −1 darling/src/libsystem_kernel/emulation/linux/stat/common.h
+10 −0 darling/src/libsystem_kernel/emulation/linux/stat/fstat.c
+5 −0 darling/src/libsystem_kernel/emulation/linux/stat/fstat.h
+4 −0 darling/src/libsystem_kernel/emulation/linux/stat/fstat64_extended.c
+5 −1 darling/src/libsystem_kernel/emulation/linux/stat/fstat64_extended.h
+10 −0 darling/src/libsystem_kernel/emulation/linux/stat/fstatat.c
+5 −0 darling/src/libsystem_kernel/emulation/linux/stat/fstatat.h
+10 −0 darling/src/libsystem_kernel/emulation/linux/stat/lstat.c
+5 −0 darling/src/libsystem_kernel/emulation/linux/stat/lstat.h
+4 −0 darling/src/libsystem_kernel/emulation/linux/stat/lstat64_extended.c
+5 −1 darling/src/libsystem_kernel/emulation/linux/stat/lstat64_extended.h
+4 −0 darling/src/libsystem_kernel/emulation/linux/stat/stat.c
+5 −0 darling/src/libsystem_kernel/emulation/linux/stat/stat.h
+4 −0 darling/src/libsystem_kernel/emulation/linux/stat/stat64_extended.c
+5 −1 darling/src/libsystem_kernel/emulation/linux/stat/stat64_extended.h
+156 −4 darling/src/libsystem_kernel/emulation/linux/syscalls-table.S
+2 −11 darling/src/libsystem_kernel/emulation/linux/unistd/getpgrp.c
+15 −0 darling/src/libsystem_kernel/emulation/linux/xtrace-hooks.S
+24 −0 gen/bsdsyscalls/SYS.h
+11 −0 gen/bsdsyscalls/___fork.S
+11 −0 gen/bsdsyscalls/___pipe.S
+14 −0 gen/bsdsyscalls/___vfork.S
+12 −0 gen/bsdsyscalls/custom.S
+19 −5 libsyscall/CMakeLists.txt
+24 −0 libsyscall/custom/SYS.h
+11 −0 libsyscall/custom/__fork.S
+14 −0 libsyscall/custom/__vfork.S
+12 −0 libsyscall/custom/custom.S
+18 −3 libsyscall/os/tsd.h
+18 −1 osfmk/mach/arm/syscall_sw.h
2 changes: 1 addition & 1 deletion src/external/zlib
Submodule zlib updated 1 files
+5 −1 CMakeLists.txt
66 changes: 66 additions & 0 deletions src/hosttools/include/coredump/arm64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef _DARLING_COREDUMP_ARM64_H_
#define _DARLING_COREDUMP_ARM64_H_

// Linux
// Structs taken from <sys/user.h>

struct nt_prstatus_registers_aarch64
{
unsigned long long regs[31];
unsigned long long sp;
unsigned long long pc;
unsigned long long pstate;
};

// struct user_fpsimd_struct
// {
// __uint128_t vregs[32];
// unsigned int fpsr;
// unsigned int fpcr;
// };

// XNU
// Taken from the following headers
// * <mach/arm/_structs.h>
// * <mach/arm/thread_status.h>

typedef struct arm_thread_state64
{
__uint64_t x[29]; /* General purpose registers x0-x28 */
__uint64_t fp; /* Frame pointer x29 */
__uint64_t lr; /* Link register x30 */
__uint64_t sp; /* Stack pointer x31 */
__uint64_t pc; /* Program counter */
__uint32_t cpsr; /* Current program status register */
__uint32_t __pad; /* Same size for 32-bit or 64-bit clients */
} arm_thread_state64_t;

// typedef struct arm_neon_state64
// {
// __uint128_t q[32];
// uint32_t fpsr;
// uint32_t fpcr;
// } arm_neon_state64_t;

typedef struct arm_exception_state64
{
__uint64_t far; /* Virtual Fault Address */
__uint32_t esr; /* Exception syndrome */
__uint32_t exception; /* number of arm exception taken */
} arm_exception_state64_t;

typedef unsigned int __darwin_natural_t;
typedef __darwin_natural_t natural_t;
typedef natural_t mach_msg_type_number_t;

#define ARM_THREAD_STATE64 6
#define ARM_EXCEPTION_STATE64 7

// #define ARM_NEON_STATE64 17

#define ARM_THREAD_STATE64_COUNT ((mach_msg_type_number_t) (sizeof (arm_thread_state64_t)/sizeof(uint32_t)))
#define ARM_EXCEPTION_STATE64_COUNT ((mach_msg_type_number_t) (sizeof (arm_exception_state64_t)/sizeof(uint32_t)))
// #define ARM_NEON_STATE64_COUNT ((mach_msg_type_number_t) (sizeof (arm_neon_state64_t)/sizeof(uint32_t)))


#endif // _DARLING_COREDUMP_ARM64_H_
54 changes: 54 additions & 0 deletions src/hosttools/src/coredump/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#ifdef __aarch64__
// Apple uses the __arm64__ macro instead of __aarch64__
#define __arm64__
#endif

#include <filesystem>

#include <stdio.h>
Expand All @@ -18,6 +23,7 @@
#include <linux/time_types.h>

#include <coredump/x86_64.h>
#include <coredump/arm64.h>

#include <darling-config.h>

Expand Down Expand Up @@ -87,6 +93,7 @@ union elf32_nt_prstatus_registers {

union elf64_nt_prstatus_registers {
struct nt_prstatus_registers_x86_64 x86_64;
struct nt_prstatus_registers_aarch64 aarch64;
};

struct elf32_kernel_old_timeval {
Expand Down Expand Up @@ -360,6 +367,7 @@ int main(int argc, char** argv) {
switch (get_elf_machine_type(&cprm)) {
case EM_X86_64:
case EM_386:
case EM_AARCH64:
cprm.input_header = (const union Elf_Ehdr*)cprm.input_corefile_mapping;
break;
default:
Expand Down Expand Up @@ -756,6 +764,28 @@ void fill_x86_float_state64(x86_float_state64_t* state, const struct thread_info
memset(state, 0, sizeof(*state));
}

static
void fill_arm_thread_state64(arm_thread_state64_t* state, const struct thread_info* info)
{
for (int i = 0; i < sizeof(state->x) / sizeof(state->x[0]); i++) {
state->x[i] = info->prstatus->elf64.general_registers.aarch64.regs[i];
}

state->fp = info->prstatus->elf64.general_registers.aarch64.regs[29];
state->lr = info->prstatus->elf64.general_registers.aarch64.regs[30];
state->sp = info->prstatus->elf64.general_registers.aarch64.sp;
state->pc = info->prstatus->elf64.general_registers.aarch64.pc;

state->cpsr = info->prstatus->elf64.general_registers.aarch64.pstate & 0xFFFFFFFF;
state->__pad = (info->prstatus->elf64.general_registers.aarch64.pstate & 0xFFFFFFFF00000000) >> 32;
}

static
void fill_arm_exception_state64(arm_exception_state64_t* state, const struct thread_info* info) {
// TODO: Need to figure out where the exception state lives on an ELF ARM64 coredump
memset(state, 0, sizeof(arm_exception_state64_t));
}

static
bool macho_dump_headers32(struct coredump_params* cprm)
{
Expand Down Expand Up @@ -938,6 +968,14 @@ bool macho_dump_headers64(struct coredump_params* cprm)
statesize += (DUMP_FLOAT_STATE ? sizeof(struct thread_flavor) + sizeof(x86_float_state64_t) : 0);
break;

case EM_AARCH64:
mh.cputype = CPU_TYPE_ARM64;
mh.cpusubtype = CPU_SUBTYPE_ARM64_ALL;

statesize = sizeof(struct thread_flavor) + sizeof(arm_thread_state64_t);
statesize += sizeof(struct thread_flavor) + sizeof(arm_exception_state64_t);
break;

default:
// Missing code for this arch
abort();
Expand Down Expand Up @@ -1033,6 +1071,22 @@ bool macho_dump_headers64(struct coredump_params* cprm)

break;

case EM_AARCH64:
tf = (struct thread_flavor*)(tc+1);
tf->flavor = ARM_THREAD_STATE64;
tf->count = ARM_THREAD_STATE64_COUNT;
fill_arm_thread_state64((arm_thread_state64_t*)tf->state, thread_info);

// DUMP_FLOAT_STATE
// For ARM64, the float state doesn't exist in LC_THREAD

tf = (struct thread_flavor*) (tf->state + sizeof(arm_thread_state64_t));
tf->flavor = ARM_EXCEPTION_STATE64;
tf->count = ARM_EXCEPTION_STATE64_COUNT;
fill_arm_exception_state64((arm_exception_state64_t*)tf->state, thread_info);

break;

default:
// Missing code for this arch
abort();
Expand Down
2 changes: 1 addition & 1 deletion src/launchd/src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -6448,7 +6448,7 @@ job_setup_exception_port(job_t j, task_t target_task)
f = PPC_THREAD_STATE64;
#elif defined(__i386__) || defined(__x86_64__)
f = x86_THREAD_STATE;
#elif defined(__arm__)
#elif defined(__arm__) || defined(__arm64__)
f = ARM_THREAD_STATE;
#else
#error "unknown architecture"
Expand Down
Loading
Loading