aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/Jit/HostTracked/AddressSpacePartitionMultiAllocation.cs
blob: 3b065583f89de8d379bfb625c44f5fb248dc0d53 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Ryujinx.Memory;
using System;
using System.Diagnostics;

namespace Ryujinx.Cpu.Jit.HostTracked
{
    class AddressSpacePartitionMultiAllocation : IDisposable
    {
        private readonly AddressSpacePartitionAllocation _baseMemory;
        private AddressSpacePartitionAllocation _baseMemoryRo;
        private AddressSpacePartitionAllocation _baseMemoryNone;

        public AddressSpacePartitionMultiAllocation(AddressSpacePartitionAllocation baseMemory)
        {
            _baseMemory = baseMemory;
        }

        public void MapView(MemoryBlock srcBlock, ulong srcOffset, ulong dstOffset, ulong size)
        {
            _baseMemory.MapView(srcBlock, srcOffset, dstOffset, size);

            if (_baseMemoryRo.IsValid)
            {
                _baseMemoryRo.MapView(srcBlock, srcOffset, dstOffset, size);
                _baseMemoryRo.Reprotect(dstOffset, size, MemoryPermission.Read, false);
            }
        }

        public void LateMapView(MemoryBlock srcBlock, ulong srcOffset, ulong dstOffset, ulong size)
        {
            _baseMemoryRo.MapView(srcBlock, srcOffset, dstOffset, size);
            _baseMemoryRo.Reprotect(dstOffset, size, MemoryPermission.Read, false);
        }

        public void UnmapView(MemoryBlock srcBlock, ulong offset, ulong size)
        {
            _baseMemory.UnmapView(srcBlock, offset, size);

            if (_baseMemoryRo.IsValid)
            {
                _baseMemoryRo.UnmapView(srcBlock, offset, size);
            }
        }

        public void Reprotect(ulong offset, ulong size, MemoryPermission permission, bool throwOnFail)
        {
            _baseMemory.Reprotect(offset, size, permission, throwOnFail);
        }

        public IntPtr GetPointer(ulong offset, ulong size)
        {
            return _baseMemory.GetPointer(offset, size);
        }

        public bool LazyInitMirrorForProtection(AddressSpacePartitioned addressSpace, ulong blockAddress, ulong blockSize, MemoryPermission permission)
        {
            if (permission == MemoryPermission.None && !_baseMemoryNone.IsValid)
            {
                _baseMemoryNone = addressSpace.CreateAsPartitionAllocation(blockAddress, blockSize);
            }
            else if (permission == MemoryPermission.Read && !_baseMemoryRo.IsValid)
            {
                _baseMemoryRo = addressSpace.CreateAsPartitionAllocation(blockAddress, blockSize);

                return true;
            }

            return false;
        }

        public IntPtr GetPointerForProtection(ulong offset, ulong size, MemoryPermission permission)
        {
            AddressSpacePartitionAllocation allocation = permission switch
            {
                MemoryPermission.ReadAndWrite => _baseMemory,
                MemoryPermission.Read => _baseMemoryRo,
                MemoryPermission.None => _baseMemoryNone,
                _ => throw new ArgumentException($"Invalid protection \"{permission}\"."),
            };

            Debug.Assert(allocation.IsValid);

            return allocation.GetPointer(offset, size);
        }

        public void Dispose()
        {
            _baseMemory.Dispose();

            if (_baseMemoryRo.IsValid)
            {
                _baseMemoryRo.Dispose();
            }

            if (_baseMemoryNone.IsValid)
            {
                _baseMemoryNone.Dispose();
            }
        }
    }
}