diff --git a/CMakeLists.txt b/CMakeLists.txt index bde16bb813..76b334c70a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,7 +112,9 @@ if(WIN32) endif() else() set(X86 OFF) - if(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm") + if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)$") + set(Architecture "arm64") + elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm") set(Architecture "arm") elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$") set(X86 ON) @@ -388,4 +390,4 @@ endif() if(BuildTests) enable_testing() add_subdirectory("tests") -endif() \ No newline at end of file +endif() diff --git a/code/client/cl_cgame.cpp b/code/client/cl_cgame.cpp index 70b6534353..b9d7b6fc11 100644 --- a/code/client/cl_cgame.cpp +++ b/code/client/cl_cgame.cpp @@ -64,7 +64,11 @@ qboolean CL_InitCGameVM( void *gameLibrary ) typedef void DllEntryProc( SyscallProc * ); DllEntryProc *dllEntry = (DllEntryProc *)Sys_LoadFunction( gameLibrary, "dllEntry" ); - cgvm.entryPoint = (intptr_t (*)(int,...))Sys_LoadFunction( gameLibrary, "vmMain" ); + + // NOTE: arm64 mac has a different calling convention for fixed parameters vs. variadic parameters. + // As the cgame entryPoints (vmMain) in jk2 and jka use fixed arg0 to arg7 we can't use "..." around here or we end up with undefined behavior. + // See: https://developer.apple.com/documentation/apple-silicon/addressing-architectural-differences-in-your-macos-code + cgvm.entryPoint = (intptr_t (*)(int,intptr_t,intptr_t,intptr_t,intptr_t,intptr_t,intptr_t,intptr_t,intptr_t))Sys_LoadFunction( gameLibrary, "vmMain" ); if ( !cgvm.entryPoint || !dllEntry ) { #ifdef JK2_MODE diff --git a/code/client/vmachine.cpp b/code/client/vmachine.cpp index e18b3aef06..0ee1464468 100644 --- a/code/client/vmachine.cpp +++ b/code/client/vmachine.cpp @@ -34,7 +34,7 @@ VIRTUAL MACHINE */ intptr_t VM_Call( int callnum, ... ) { - intptr_t args[10] = { 0 }; + intptr_t args[8] = { 0 }; va_list ap; if ( cgvm.entryPoint ) { @@ -43,8 +43,7 @@ intptr_t VM_Call( int callnum, ... ) args[i] = va_arg( ap, intptr_t ); va_end(ap); - return cgvm.entryPoint( callnum, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], - args[8], args[9]); + return cgvm.entryPoint( callnum, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7] ); } return -1; } diff --git a/code/client/vmachine.h b/code/client/vmachine.h index 8102d75db5..6d7b0eca47 100644 --- a/code/client/vmachine.h +++ b/code/client/vmachine.h @@ -71,7 +71,10 @@ VIRTUAL MACHINE ============================================================== */ typedef struct vm_s { - intptr_t (*entryPoint)( int callNum, ... ); + // NOTE: arm64 mac has a different calling convention for fixed parameters vs. variadic parameters. + // As the cgame entryPoints (vmMain) in jk2 and jka use fixed arg0 to arg7 we can't use "..." around here or we end up with undefined behavior. + // See: https://developer.apple.com/documentation/apple-silicon/addressing-architectural-differences-in-your-macos-code + intptr_t (*entryPoint)( int callNum, intptr_t arg0, intptr_t arg1, intptr_t arg2, intptr_t arg3, intptr_t arg4, intptr_t arg5, intptr_t arg6, intptr_t arg7 ); } vm_t; extern vm_t cgvm; diff --git a/shared/qcommon/q_platform.h b/shared/qcommon/q_platform.h index 1c450d7d80..b843d0ad51 100644 --- a/shared/qcommon/q_platform.h +++ b/shared/qcommon/q_platform.h @@ -108,6 +108,9 @@ along with this program; if not, see . #define idx64 #define ARCH_STRING "x86_64" #define Q3_LITTLE_ENDIAN + #elif defined(__arm64__) + #define ARCH_STRING "arm64" + #define Q3_LITTLE_ENDIAN #endif #define DLL_EXT ".dylib"