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 #12

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ if (DSERVER_TOOLS)
add_subdirectory(tools)
endif()

if (TARGET_ARM64)
# Unlike Linux, Apple's uses `__arm64__` instead of `__aarch64__`
add_definitions(-D__arm64__)
endif (TARGET_ARM64)

if (DSERVER_ASAN)
add_compile_definitions(
DSERVER_ASAN=1
Expand Down
23 changes: 20 additions & 3 deletions duct-tape/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ add_compile_definitions(
__DARWIN_BYTE_ORDER=__DARWIN_LITTLE_ENDIAN
)

if(TARGET_ARM64)
add_compile_definitions(
ARM64_BOARD_CONFIG_VMAPPLE
)
endif(TARGET_ARM64)


add_compile_options(
-fblocks
-ffunction-sections
Expand Down Expand Up @@ -253,7 +260,7 @@ add_custom_target(kernel_mig_generate
${CMAKE_CURRENT_BINARY_DIR}/xnu/osfmk/UserNotification/UNDReplyServer.c
)

add_library(darlingserver_duct_tape STATIC
set(darlingserver_duct_tape_source
src/init.c
src/misc.c
src/stubs.c
Expand Down Expand Up @@ -321,8 +328,6 @@ add_library(darlingserver_duct_tape STATIC
xnu/osfmk/ipc/mig_log.c
xnu/osfmk/ipc/ipc_eventlink.c

xnu/osfmk/i386/rtclock.c

xnu/osfmk/prng/prng_random.c

xnu/osfmk/vm/vm32_user.c
Expand Down Expand Up @@ -357,6 +362,18 @@ add_library(darlingserver_duct_tape STATIC
pthread/kern_synch.c
)

if (TARGET_i386 OR TARGET_x86_64)
set(darlingserver_duct_tape_source ${darlingserver_duct_tape_source}
xnu/osfmk/i386/rtclock.c
)
elseif (TARGET_ARM64)
set(darlingserver_duct_tape_source ${darlingserver_duct_tape_source}
xnu/osfmk/arm/rtclock.c
)
endif()

add_library(darlingserver_duct_tape STATIC ${darlingserver_duct_tape_source})

set_source_files_properties(pthread/kern_synch.c PROPERTIES
COMPILE_FLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/pthread"
)
Expand Down
6 changes: 6 additions & 0 deletions duct-tape/internal-include/darlingserver/duct-tape/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ typedef struct dtape_thread_user_state {
#if __x86_64__
x86_thread_state_t thread_state;
x86_float_state_t float_state;
#elif __aarch64__
arm_thread_state64_t thread_state;
// arm_neon_state64_t neon_state;
#warning "TODO: Implement other ARM64 states"
#else
#error "Missing user states"
#endif
} dtape_thread_user_state_t;

Expand Down
3 changes: 3 additions & 0 deletions duct-tape/src/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ kern_return_t host_info(host_t host, host_flavor_t flavor, host_info_t info, mac
#else
basic_info->cpu_subtype = CPU_SUBTYPE_I386_ALL;
#endif
#elif __aarch64__
basic_info->cpu_type = CPU_TYPE_ARM64;
basic_info->cpu_subtype = CPU_SUBTYPE_ARM64_ALL;
#else
#error Unknown CPU type
#endif
Expand Down
12 changes: 10 additions & 2 deletions duct-tape/src/locks.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ void dtape_mutex_lock(dtape_mutex_t* mutex) {
return;
}
libsimple_lock_unlock(&mutex->dtape_queue_lock);
#if defined(__x86_64__) || defined(__i386__)
__builtin_ia32_pause();
#elif __aarch64__
asm volatile("yield");
#else
#error Missing CPU pause for this architecture
#endif
}
}

Expand Down Expand Up @@ -526,10 +532,12 @@ hw_wait_while_equals(void **address, void *current)
for (;;) {
for (int i = 0; i < LOCK_SNOOP_SPINS; i++) {
#ifdef __DARLING__
#if __x86_64__ || __i386__
#if defined(__x86_64__) || defined(__i386__)
__builtin_ia32_pause();
#elif __aarch64__
asm volatile("yield");
#else
#warning Missing CPU pause for this architecture
#error Missing CPU pause for this architecture
#endif
#else
cpu_pause();
Expand Down
4 changes: 3 additions & 1 deletion duct-tape/src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ int ftruncate(int fd, off_t length);

#define MFD_CLOEXEC 0x1

void dtape_memory_init(void) {
int PAGE_SHIFT_CONST;

void dtape_memory_init(void) {
PAGE_SHIFT_CONST = __builtin_ctzl(sysconf(_SC_PAGESIZE));
};

static uint64_t dtape_byte_count_to_page_count_round_up(uint64_t byte_count) {
Expand Down
45 changes: 44 additions & 1 deletion duct-tape/src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ char version[] = "Darling 11.5";
[x86_PAGEIN_STATE] = x86_PAGEIN_STATE_COUNT
};
// </copied>
#elif defined(__aarch64__)
// <copied from="xnu://7195.141.2/osfmk/arm64/status.c"
unsigned int _MachineStateCount[] = {
[ARM_UNIFIED_THREAD_STATE] = ARM_UNIFIED_THREAD_STATE_COUNT,
[ARM_VFP_STATE] = ARM_VFP_STATE_COUNT,
[ARM_EXCEPTION_STATE] = ARM_EXCEPTION_STATE_COUNT,
[ARM_DEBUG_STATE] = ARM_DEBUG_STATE_COUNT,
[ARM_THREAD_STATE64] = ARM_THREAD_STATE64_COUNT,
[ARM_EXCEPTION_STATE64] = ARM_EXCEPTION_STATE64_COUNT,
[ARM_THREAD_STATE32] = ARM_THREAD_STATE32_COUNT,
[ARM_DEBUG_STATE32] = ARM_DEBUG_STATE32_COUNT,
[ARM_DEBUG_STATE64] = ARM_DEBUG_STATE64_COUNT,
[ARM_NEON_STATE] = ARM_NEON_STATE_COUNT,
[ARM_NEON_STATE64] = ARM_NEON_STATE64_COUNT,
[ARM_PAGEIN_STATE] = ARM_PAGEIN_STATE_COUNT,
};
// </copied>
#else
#error _MachineStateCount not defined on this architecture
#endif
Expand Down Expand Up @@ -101,10 +118,11 @@ unsigned int waitq_held(struct waitq* wq) {
return wq->dtape_waitq_interlock.dtape_interlock.dtape_interlock.dtape_mutex.dtape_owner == (uintptr_t)current_thread();
};

#if __x86_64__
#if __x86_64__ || __aarch64__

//
// <copied from="xnu://7195.141.2/osfmk/x86_64/loose_ends.c">
// <copied from="xnu://7195.141.2/osfmk/arm64/loose_ends.c">
//

/*
Expand All @@ -120,8 +138,33 @@ fls(unsigned int mask)
return (sizeof(mask) << 3) - __builtin_clz(mask);
}

#endif

//
// Since this is the only method we need from `bsd_kern.c`, I rather not
// `#ifndef` all of the source code that we don't need in that file.
//
// <copied from="xnu://7195.141.2/osfmk/kern/bsd_kern.c">
//

#ifdef __aarch64__

task_t
get_threadtask(thread_t th)
{
return th->task;
}

#endif

//
// </copied>
//

// Unlike i386, the preemption methods are not
// inline for arm. So we will need to create stubs.
#ifdef __aarch64__
int get_preemption_level(void) { return 0; }
void _enable_preemption(void) {}
void _disable_preemption(void) {}
#endif
3 changes: 3 additions & 0 deletions duct-tape/src/processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ kern_return_t processor_info(processor_t processor, processor_flavor_t flavor, h
#else
info->cpu_subtype = CPU_SUBTYPE_I386_ALL;
#endif
#elif __aarch64__
info->cpu_type = CPU_TYPE_ARM64;
info->cpu_subtype = CPU_SUBTYPE_ARM64_ALL;
#else
#error Unknown CPU type
#endif
Expand Down
Loading