aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/MemoryEhMeilleure.cs
blob: f3a5b056bc7bc419d2beceacc9f3cea32c892a54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Ryujinx.Cpu.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 TrackingEventDelegate _trackingEvent;

        private readonly ulong _baseAddress;
        private readonly ulong _mirrorAddress;

        public MemoryEhMeilleure(MemoryBlock addressSpace, MemoryBlock addressSpaceMirror, MemoryTracking tracking)
        {
            _baseAddress = (ulong)addressSpace.Pointer;
            ulong endAddress = _baseAddress + addressSpace.Size;

            _trackingEvent = 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()
        {
            GC.SuppressFinalize(this);

            NativeSignalHandler.RemoveTrackedRegion((nuint)_baseAddress);

            if (_mirrorAddress != 0)
            {
                NativeSignalHandler.RemoveTrackedRegion((nuint)_mirrorAddress);
            }
        }
    }
}