aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/MemoryEhMeilleure.cs
diff options
context:
space:
mode:
authorTSR Berry <20988865+TSRBerry@users.noreply.github.com>2023-04-08 01:22:00 +0200
committerMary <thog@protonmail.com>2023-04-27 23:51:14 +0200
commitcee712105850ac3385cd0091a923438167433f9f (patch)
tree4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /src/Ryujinx.Cpu/MemoryEhMeilleure.cs
parentcd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff)
Move solution and projects to src
Diffstat (limited to 'src/Ryujinx.Cpu/MemoryEhMeilleure.cs')
-rw-r--r--src/Ryujinx.Cpu/MemoryEhMeilleure.cs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/Ryujinx.Cpu/MemoryEhMeilleure.cs b/src/Ryujinx.Cpu/MemoryEhMeilleure.cs
new file mode 100644
index 00000000..0b434ea7
--- /dev/null
+++ b/src/Ryujinx.Cpu/MemoryEhMeilleure.cs
@@ -0,0 +1,62 @@
+using ARMeilleure.Signal;
+using Ryujinx.Memory;
+using Ryujinx.Memory.Tracking;
+using System;
+using System.Runtime.InteropServices;
+
+namespace Ryujinx.Cpu
+{
+ public class MemoryEhMeilleure : IDisposable
+ {
+ private delegate bool TrackingEventDelegate(ulong address, ulong size, bool write);
+
+ private readonly MemoryTracking _tracking;
+ private readonly TrackingEventDelegate _trackingEvent;
+
+ private readonly ulong _baseAddress;
+ private readonly ulong _mirrorAddress;
+
+ public MemoryEhMeilleure(MemoryBlock addressSpace, MemoryBlock addressSpaceMirror, MemoryTracking tracking)
+ {
+ _tracking = tracking;
+
+ _baseAddress = (ulong)addressSpace.Pointer;
+ ulong endAddress = _baseAddress + addressSpace.Size;
+
+ _trackingEvent = new TrackingEventDelegate(tracking.VirtualMemoryEvent);
+ bool added = NativeSignalHandler.AddTrackedRegion((nuint)_baseAddress, (nuint)endAddress, Marshal.GetFunctionPointerForDelegate(_trackingEvent));
+
+ if (!added)
+ {
+ throw new InvalidOperationException("Number of allowed tracked regions exceeded.");
+ }
+
+ if (OperatingSystem.IsWindows())
+ {
+ // Add a tracking event with no signal handler for the mirror on Windows.
+ // The native handler has its own code to check for the partial overlap race when regions are protected by accident,
+ // and when there is no signal handler present.
+
+ _mirrorAddress = (ulong)addressSpaceMirror.Pointer;
+ ulong endAddressMirror = _mirrorAddress + addressSpace.Size;
+
+ bool addedMirror = NativeSignalHandler.AddTrackedRegion((nuint)_mirrorAddress, (nuint)endAddressMirror, IntPtr.Zero);
+
+ if (!addedMirror)
+ {
+ throw new InvalidOperationException("Number of allowed tracked regions exceeded.");
+ }
+ }
+ }
+
+ public void Dispose()
+ {
+ NativeSignalHandler.RemoveTrackedRegion((nuint)_baseAddress);
+
+ if (_mirrorAddress != 0)
+ {
+ NativeSignalHandler.RemoveTrackedRegion((nuint)_mirrorAddress);
+ }
+ }
+ }
+}