From 00c69f2098bf78cecefd897599c95f68450d88a1 Mon Sep 17 00:00:00 2001 From: Mary Date: Wed, 8 Dec 2021 22:24:26 +0100 Subject: [PATCH] Remove usage of Mono.Posix.NETStandard accross all projects (#2906) * Remove usage of Mono.Posix.NETStandard in Ryujinx project * Remove usage of Mono.Posix.NETStandard in ARMeilleure project * Remove usage of Mono.Posix.NETStandard in Ryujinx.Memory project * Address gdkchan's comments --- ARMeilleure/ARMeilleure.csproj | 4 - .../Signal/UnixSignalHandlerRegistration.cs | 8 +- Ryujinx.Memory/MemoryManagementUnix.cs | 34 ++--- Ryujinx.Memory/MemoryManagerUnixHelper.cs | 128 ++++++++++++++++++ Ryujinx.Memory/Ryujinx.Memory.csproj | 4 - Ryujinx/Modules/Updater/UpdateDialog.cs | 2 - Ryujinx/Modules/Updater/Updater.cs | 7 +- Ryujinx/Program.cs | 3 - Ryujinx/Ryujinx.csproj | 1 - 9 files changed, 149 insertions(+), 42 deletions(-) create mode 100644 Ryujinx.Memory/MemoryManagerUnixHelper.cs diff --git a/ARMeilleure/ARMeilleure.csproj b/ARMeilleure/ARMeilleure.csproj index 1fd95ad0..e29e33e8 100644 --- a/ARMeilleure/ARMeilleure.csproj +++ b/ARMeilleure/ARMeilleure.csproj @@ -5,10 +5,6 @@ true - - - - diff --git a/ARMeilleure/Signal/UnixSignalHandlerRegistration.cs b/ARMeilleure/Signal/UnixSignalHandlerRegistration.cs index 9e87749e..40268a91 100644 --- a/ARMeilleure/Signal/UnixSignalHandlerRegistration.cs +++ b/ARMeilleure/Signal/UnixSignalHandlerRegistration.cs @@ -1,5 +1,4 @@ -using Mono.Unix.Native; -using System; +using System; using System.Runtime.InteropServices; namespace ARMeilleure.Signal @@ -21,6 +20,7 @@ namespace ARMeilleure.Signal static class UnixSignalHandlerRegistration { + private const int SIGSEGV = 11; private const int SA_SIGINFO = 0x00000004; [DllImport("libc", SetLastError = true)] @@ -39,7 +39,7 @@ namespace ARMeilleure.Signal sigemptyset(ref sig.sa_mask); - int result = sigaction((int)Signum.SIGSEGV, ref sig, out SigAction old); + int result = sigaction(SIGSEGV, ref sig, out SigAction old); if (result != 0) { @@ -51,7 +51,7 @@ namespace ARMeilleure.Signal public static bool RestoreExceptionHandler(SigAction oldAction) { - return sigaction((int)Signum.SIGSEGV, ref oldAction, out SigAction _) == 0; + return sigaction(SIGSEGV, ref oldAction, out SigAction _) == 0; } } } diff --git a/Ryujinx.Memory/MemoryManagementUnix.cs b/Ryujinx.Memory/MemoryManagementUnix.cs index edfc98dd..9e3ec48a 100644 --- a/Ryujinx.Memory/MemoryManagementUnix.cs +++ b/Ryujinx.Memory/MemoryManagementUnix.cs @@ -1,10 +1,11 @@ -using Mono.Unix.Native; -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Runtime.Versioning; +using static Ryujinx.Memory.MemoryManagerUnixHelper; + namespace Ryujinx.Memory { [SupportedOSPlatform("linux")] @@ -18,15 +19,6 @@ namespace Ryujinx.Memory public IntPtr SourcePointer; } - [DllImport("libc", SetLastError = true)] - public static extern IntPtr mremap(IntPtr old_address, ulong old_size, ulong new_size, MremapFlags flags, IntPtr new_address); - - [DllImport("libc", SetLastError = true)] - public static extern int madvise(IntPtr address, ulong size, int advice); - - private const int MADV_DONTNEED = 4; - private const int MADV_REMOVE = 9; - private static readonly List _sharedMemory = new List(); private static readonly ConcurrentDictionary _sharedMemorySource = new ConcurrentDictionary(); private static readonly ConcurrentDictionary _allocations = new ConcurrentDictionary(); @@ -47,7 +39,7 @@ namespace Ryujinx.Memory if (shared) { - flags |= MmapFlags.MAP_SHARED | (MmapFlags)0x80000; + flags |= MmapFlags.MAP_SHARED | MmapFlags.MAP_UNLOCKED; } else { @@ -59,7 +51,7 @@ namespace Ryujinx.Memory flags |= MmapFlags.MAP_NORESERVE; } - IntPtr ptr = Syscall.mmap(IntPtr.Zero, size, prot, flags, -1, 0); + IntPtr ptr = mmap(IntPtr.Zero, size, prot, flags, -1, 0); if (ptr == new IntPtr(-1L)) { @@ -77,7 +69,7 @@ namespace Ryujinx.Memory public static bool Commit(IntPtr address, ulong size) { - bool success = Syscall.mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0; + bool success = mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0; if (success) { @@ -87,7 +79,7 @@ namespace Ryujinx.Memory { ulong sharedAddress = ((ulong)address - (ulong)shared.SourcePointer) + (ulong)shared.Pointer; - if (Syscall.mprotect((IntPtr)sharedAddress, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) != 0) + if (mprotect((IntPtr)sharedAddress, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) != 0) { return false; } @@ -108,16 +100,16 @@ namespace Ryujinx.Memory } // Must be writable for madvise to work properly. - Syscall.mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE); + mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE); madvise(address, size, isShared ? MADV_REMOVE : MADV_DONTNEED); - return Syscall.mprotect(address, size, MmapProts.PROT_NONE) == 0; + return mprotect(address, size, MmapProts.PROT_NONE) == 0; } public static bool Reprotect(IntPtr address, ulong size, MemoryPermission permission) { - return Syscall.mprotect(address, size, GetProtection(permission)) == 0; + return mprotect(address, size, GetProtection(permission)) == 0; } private static MmapProts GetProtection(MemoryPermission permission) @@ -138,7 +130,7 @@ namespace Ryujinx.Memory { if (_allocations.TryRemove(address, out ulong size)) { - return Syscall.munmap(address, size) == 0; + return munmap(address, size) == 0; } return false; @@ -146,14 +138,14 @@ namespace Ryujinx.Memory public static IntPtr Remap(IntPtr target, IntPtr source, ulong size) { - int flags = (int)MremapFlags.MREMAP_MAYMOVE; + int flags = 1; if (target != IntPtr.Zero) { flags |= 2; } - IntPtr result = mremap(source, 0, size, (MremapFlags)(flags), target); + IntPtr result = mremap(source, 0, size, flags, target); if (result == IntPtr.Zero) { diff --git a/Ryujinx.Memory/MemoryManagerUnixHelper.cs b/Ryujinx.Memory/MemoryManagerUnixHelper.cs new file mode 100644 index 00000000..4409ccee --- /dev/null +++ b/Ryujinx.Memory/MemoryManagerUnixHelper.cs @@ -0,0 +1,128 @@ +using System; +using System.Runtime.InteropServices; + +namespace Ryujinx.Memory +{ + public static class MemoryManagerUnixHelper + { + [Flags] + public enum MmapProts : uint + { + PROT_NONE = 0, + PROT_READ = 1, + PROT_WRITE = 2, + PROT_EXEC = 4 + } + + [Flags] + public enum MmapFlags : uint + { + MAP_SHARED = 1, + MAP_PRIVATE = 2, + MAP_ANONYMOUS = 4, + MAP_NORESERVE = 8, + MAP_UNLOCKED = 16 + } + + private const int MAP_ANONYMOUS_LINUX_GENERIC = 0x20; + private const int MAP_NORESERVE_LINUX_GENERIC = 0x4000; + private const int MAP_UNLOCKED_LINUX_GENERIC = 0x80000; + + private const int MAP_NORESERVE_DARWIN = 0x40; + private const int MAP_JIT_DARWIN = 0x800; + private const int MAP_ANONYMOUS_DARWIN = 0x1000; + + public const int MADV_DONTNEED = 4; + public const int MADV_REMOVE = 9; + + [DllImport("libc", EntryPoint = "mmap", SetLastError = true)] + private static extern IntPtr Internal_mmap(IntPtr address, ulong length, MmapProts prot, int flags, int fd, long offset); + + [DllImport("libc", SetLastError = true)] + public static extern int mprotect(IntPtr address, ulong length, MmapProts prot); + + [DllImport("libc", SetLastError = true)] + public static extern int munmap(IntPtr address, ulong length); + + [DllImport("libc", SetLastError = true)] + public static extern IntPtr mremap(IntPtr old_address, ulong old_size, ulong new_size, int flags, IntPtr new_address); + + [DllImport("libc", SetLastError = true)] + public static extern int madvise(IntPtr address, ulong size, int advice); + + private static int MmapFlagsToSystemFlags(MmapFlags flags) + { + int result = 0; + + if (flags.HasFlag(MmapFlags.MAP_SHARED)) + { + result |= (int)MmapFlags.MAP_SHARED; + } + + if (flags.HasFlag(MmapFlags.MAP_PRIVATE)) + { + result |= (int)MmapFlags.MAP_PRIVATE; + } + + if (flags.HasFlag(MmapFlags.MAP_ANONYMOUS)) + { + if (OperatingSystem.IsLinux()) + { + result |= MAP_ANONYMOUS_LINUX_GENERIC; + } + else if (OperatingSystem.IsMacOS()) + { + result |= MAP_ANONYMOUS_DARWIN; + } + else + { + throw new NotImplementedException(); + } + } + + if (flags.HasFlag(MmapFlags.MAP_NORESERVE)) + { + if (OperatingSystem.IsLinux()) + { + result |= MAP_NORESERVE_LINUX_GENERIC; + } + else if (OperatingSystem.IsMacOS()) + { + result |= MAP_NORESERVE_DARWIN; + } + else + { + throw new NotImplementedException(); + } + } + + if (flags.HasFlag(MmapFlags.MAP_UNLOCKED)) + { + if (OperatingSystem.IsLinux()) + { + result |= MAP_UNLOCKED_LINUX_GENERIC; + } + else if (OperatingSystem.IsMacOS()) + { + // FIXME: Doesn't exist on Darwin + } + else + { + throw new NotImplementedException(); + } + } + + if (OperatingSystem.IsMacOSVersionAtLeast(10, 14)) + { + result |= MAP_JIT_DARWIN; + } + + return result; + } + + public static IntPtr mmap(IntPtr address, ulong length, MmapProts prot, MmapFlags flags, int fd, long offset) + { + return Internal_mmap(address, length, prot, MmapFlagsToSystemFlags(flags), fd, offset); + } + } +} \ No newline at end of file diff --git a/Ryujinx.Memory/Ryujinx.Memory.csproj b/Ryujinx.Memory/Ryujinx.Memory.csproj index 5ece9322..0b2ed706 100644 --- a/Ryujinx.Memory/Ryujinx.Memory.csproj +++ b/Ryujinx.Memory/Ryujinx.Memory.csproj @@ -5,10 +5,6 @@ true - - - - diff --git a/Ryujinx/Modules/Updater/UpdateDialog.cs b/Ryujinx/Modules/Updater/UpdateDialog.cs index 804f7ab5..385b3357 100644 --- a/Ryujinx/Modules/Updater/UpdateDialog.cs +++ b/Ryujinx/Modules/Updater/UpdateDialog.cs @@ -1,12 +1,10 @@ using Gdk; using Gtk; -using Mono.Unix; using Ryujinx.Ui; using System; using System.Diagnostics; using System.Linq; using System.Reflection; -using System.Runtime.InteropServices; namespace Ryujinx.Modules { diff --git a/Ryujinx/Modules/Updater/Updater.cs b/Ryujinx/Modules/Updater/Updater.cs index 933e59d8..5ce896e5 100644 --- a/Ryujinx/Modules/Updater/Updater.cs +++ b/Ryujinx/Modules/Updater/Updater.cs @@ -2,7 +2,6 @@ using Gtk; using ICSharpCode.SharpZipLib.GZip; using ICSharpCode.SharpZipLib.Tar; using ICSharpCode.SharpZipLib.Zip; -using Mono.Unix; using Newtonsoft.Json.Linq; using Ryujinx.Common.Logging; using Ryujinx.Ui; @@ -355,14 +354,16 @@ namespace Ryujinx.Modules worker.Start(); } + [DllImport("libc", SetLastError = true)] + private static extern int chmod(string path, uint mode); + private static void SetUnixPermissions() { string ryuBin = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Ryujinx"); if (!OperatingSystem.IsWindows()) { - UnixFileInfo unixFileInfo = new UnixFileInfo(ryuBin); - unixFileInfo.FileAccessPermissions |= FileAccessPermissions.UserExecute; + chmod(ryuBin, 0777); } } diff --git a/Ryujinx/Program.cs b/Ryujinx/Program.cs index a4528fb0..1e0fdd3a 100644 --- a/Ryujinx/Program.cs +++ b/Ryujinx/Program.cs @@ -61,9 +61,6 @@ namespace Ryujinx } } - // Enforce loading of Mono.Posix to avoid .NET runtime lazy loading it during an update. - Assembly.Load("Mono.Posix.NETStandard"); - // Make process DPI aware for proper window sizing on high-res screens. ForceDpiAware.Windows(); WindowScaleFactor = ForceDpiAware.GetWindowScaleFactor(); diff --git a/Ryujinx/Ryujinx.csproj b/Ryujinx/Ryujinx.csproj index 9d002d39..64d42342 100644 --- a/Ryujinx/Ryujinx.csproj +++ b/Ryujinx/Ryujinx.csproj @@ -25,7 +25,6 @@ -