aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Memory/WindowsShared/PlaceholderManager4KB.cs
blob: fc056a2f733ddd88abd519a68282b6e1edbc045b (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Runtime.Versioning;

namespace Ryujinx.Memory.WindowsShared
{
    /// <summary>
    /// Windows 4KB memory placeholder manager.
    /// </summary>
    [SupportedOSPlatform("windows")]
    class PlaceholderManager4KB
    {
        private const int PageSize = MemoryManagementWindows.PageSize;

        private readonly IntervalTree<ulong, byte> _mappings;

        /// <summary>
        /// Creates a new instance of the Windows 4KB memory placeholder manager.
        /// </summary>
        public PlaceholderManager4KB()
        {
            _mappings = new IntervalTree<ulong, byte>();
        }

        /// <summary>
        /// Unmaps the specified range of memory and marks it as mapped internally.
        /// </summary>
        /// <remarks>
        /// Since this marks the range as mapped, the expectation is that the range will be mapped after calling this method.
        /// </remarks>
        /// <param name="location">Memory address to unmap and mark as mapped</param>
        /// <param name="size">Size of the range in bytes</param>
        public void UnmapAndMarkRangeAsMapped(IntPtr location, IntPtr size)
        {
            ulong startAddress = (ulong)location;
            ulong unmapSize = (ulong)size;
            ulong endAddress = startAddress + unmapSize;

            var overlaps = Array.Empty<IntervalTreeNode<ulong, byte>>();
            int count = 0;

            lock (_mappings)
            {
                count = _mappings.Get(startAddress, endAddress, ref overlaps);
            }

            for (int index = 0; index < count; index++)
            {
                var overlap = overlaps[index];

                // Tree operations might modify the node start/end values, so save a copy before we modify the tree.
                ulong overlapStart = overlap.Start;
                ulong overlapEnd = overlap.End;
                ulong overlapValue = overlap.Value;

                _mappings.Remove(overlap);

                ulong unmapStart = Math.Max(overlapStart, startAddress);
                ulong unmapEnd = Math.Min(overlapEnd, endAddress);

                if (overlapStart < startAddress)
                {
                    startAddress = overlapStart;
                }

                if (overlapEnd > endAddress)
                {
                    endAddress = overlapEnd;
                }

                ulong currentAddress = unmapStart;
                while (currentAddress < unmapEnd)
                {
                    WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)currentAddress, 2);
                    currentAddress += PageSize;
                }
            }

            _mappings.Add(startAddress, endAddress, 0);
        }

        /// <summary>
        /// Unmaps views at the specified memory range.
        /// </summary>
        /// <param name="location">Address of the range</param>
        /// <param name="size">Size of the range in bytes</param>
        public void UnmapView(IntPtr location, IntPtr size)
        {
            ulong startAddress = (ulong)location;
            ulong unmapSize = (ulong)size;
            ulong endAddress = startAddress + unmapSize;

            var overlaps = Array.Empty<IntervalTreeNode<ulong, byte>>();
            int count = 0;

            lock (_mappings)
            {
                count = _mappings.Get(startAddress, endAddress, ref overlaps);
            }

            for (int index = 0; index < count; index++)
            {
                var overlap = overlaps[index];

                // Tree operations might modify the node start/end values, so save a copy before we modify the tree.
                ulong overlapStart = overlap.Start;
                ulong overlapEnd = overlap.End;

                _mappings.Remove(overlap);

                if (overlapStart < startAddress)
                {
                    _mappings.Add(overlapStart, startAddress, 0);
                }

                if (overlapEnd > endAddress)
                {
                    _mappings.Add(endAddress, overlapEnd, 0);
                }

                ulong unmapStart = Math.Max(overlapStart, startAddress);
                ulong unmapEnd = Math.Min(overlapEnd, endAddress);

                ulong currentAddress = unmapStart;
                while (currentAddress < unmapEnd)
                {
                    WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)currentAddress, 2);
                    currentAddress += PageSize;
                }
            }
        }

        /// <summary>
        /// Unmaps mapped memory at a given range.
        /// </summary>
        /// <param name="location">Address of the range</param>
        /// <param name="size">Size of the range in bytes</param>
        public void UnmapRange(IntPtr location, IntPtr size)
        {
            ulong startAddress = (ulong)location;
            ulong unmapSize = (ulong)size;
            ulong endAddress = startAddress + unmapSize;

            var overlaps = Array.Empty<IntervalTreeNode<ulong, byte>>();
            int count = 0;

            lock (_mappings)
            {
                count = _mappings.Get(startAddress, endAddress, ref overlaps);
            }

            for (int index = 0; index < count; index++)
            {
                var overlap = overlaps[index];

                // Tree operations might modify the node start/end values, so save a copy before we modify the tree.
                ulong unmapStart = Math.Max(overlap.Start, startAddress);
                ulong unmapEnd = Math.Min(overlap.End, endAddress);

                _mappings.Remove(overlap);

                ulong currentAddress = unmapStart;
                while (currentAddress < unmapEnd)
                {
                    WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)currentAddress, 2);
                    currentAddress += PageSize;
                }
            }
        }
    }
}