From 11578ad9faa24b1dea2ae7fc18163ac506e48720 Mon Sep 17 00:00:00 2001 From: Stossy11 <69031796+stossy11@users.noreply.github.com> Date: Tue, 3 Dec 2024 19:16:16 +1100 Subject: [PATCH] Add Missing iOS Patches --- src/ARMeilleure/Native/JitSupportDarwin.cs | 30 +++++++++++++++++++ src/ARMeilleure/Translation/Cache/JitCache.cs | 14 +++++++-- src/Ryujinx.Headless.SDL2/Program.cs | 3 ++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/ARMeilleure/Native/JitSupportDarwin.cs b/src/ARMeilleure/Native/JitSupportDarwin.cs index 39df3878f..77fb2258b 100644 --- a/src/ARMeilleure/Native/JitSupportDarwin.cs +++ b/src/ARMeilleure/Native/JitSupportDarwin.cs @@ -10,4 +10,34 @@ namespace ARMeilleure.Native [LibraryImport("libarmeilleure-jitsupport", EntryPoint = "armeilleure_jit_memcpy")] public static partial void Copy(nint dst, nint src, ulong n); } + [SupportedOSPlatform("ios")] + internal static partial class JitSupportDarwinAot + { + [LibraryImport("pthread", EntryPoint = "pthread_jit_write_protect_np")] + private static partial void pthread_jit_write_protect_np(int enabled); + + [LibraryImport("libc", EntryPoint = "sys_icache_invalidate")] + private static partial void sys_icache_invalidate(IntPtr start, IntPtr length); + + public static unsafe void Copy(IntPtr dst, IntPtr src, ulong n) { + // When NativeAOT is in use, we can toggle per-thread write protection without worrying about breaking .NET code. + + //pthread_jit_write_protect_np(0); + + var srcSpan = new Span(src.ToPointer(), (int)n); + var dstSpan = new Span(dst.ToPointer(), (int)n); + srcSpan.CopyTo(dstSpan); + + //pthread_jit_write_protect_np(1); + + // Ensure that the instruction cache for this range is invalidated. + sys_icache_invalidate(dst, (IntPtr)n); + } + + public static unsafe void Invalidate(IntPtr dst, ulong n) + { + // Ensure that the instruction cache for this range is invalidated. + sys_icache_invalidate(dst, (IntPtr)n); + } + } } diff --git a/src/ARMeilleure/Translation/Cache/JitCache.cs b/src/ARMeilleure/Translation/Cache/JitCache.cs index cf13cd6cb..6515b89c4 100644 --- a/src/ARMeilleure/Translation/Cache/JitCache.cs +++ b/src/ARMeilleure/Translation/Cache/JitCache.cs @@ -18,7 +18,7 @@ namespace ARMeilleure.Translation.Cache private const int CodeAlignment = 4; // Bytes. private const int CacheSize = 2047 * 1024 * 1024; - + private const int CacheSizeIOS = 512 * 1024 * 1024; private static ReservedRegion _jitRegion; private static JitCacheInvalidation _jitCacheInvalidator; @@ -49,7 +49,7 @@ namespace ARMeilleure.Translation.Cache _jitRegion = new ReservedRegion(allocator, CacheSize); - if (!OperatingSystem.IsWindows() && !OperatingSystem.IsMacOS()) + if (!OperatingSystem.IsWindows() && !OperatingSystem.IsMacOS() && !OperatingSystem.IsIOS()) { _jitCacheInvalidator = new JitCacheInvalidation(allocator); } @@ -77,7 +77,15 @@ namespace ARMeilleure.Translation.Cache nint funcPtr = _jitRegion.Pointer + funcOffset; - if (OperatingSystem.IsMacOS() && RuntimeInformation.ProcessArchitecture == Architecture.Arm64) + + if (OperatingSystem.IsIOS()) + { + Marshal.Copy(code, 0, funcPtr, code.Length); + ReprotectAsExecutable(funcOffset, code.Length); + + JitSupportDarwinAot.Invalidate(funcPtr, (ulong)code.Length); + } + else if (OperatingSystem.IsMacOS() && RuntimeInformation.ProcessArchitecture == Architecture.Arm64) { unsafe { diff --git a/src/Ryujinx.Headless.SDL2/Program.cs b/src/Ryujinx.Headless.SDL2/Program.cs index 2d6cfcaca..a58f4299f 100644 --- a/src/Ryujinx.Headless.SDL2/Program.cs +++ b/src/Ryujinx.Headless.SDL2/Program.cs @@ -104,6 +104,9 @@ namespace Ryujinx.Headless.SDL2 ForceDpiAware.Windows(); CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; + CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture; + CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture; + Silk.NET.Core.Loader.SearchPathContainer.Platform = Silk.NET.Core.Loader.UnderlyingPlatform.MacOS; if (!OperatingSystem.IsIOS())