aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Memory/MemoryManagementWindows.cs
blob: 3d51b64f78842789d15cb141ba7a955428ce2fb2 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using Ryujinx.Memory.WindowsShared;
using System;
using System.Runtime.Versioning;

namespace Ryujinx.Memory
{
    [SupportedOSPlatform("windows")]
    static class MemoryManagementWindows
    {
        public const int PageSize = 0x1000;

        private static readonly PlaceholderManager _placeholders = new PlaceholderManager();
        private static readonly PlaceholderManager4KB _placeholders4KB = new PlaceholderManager4KB();

        public static IntPtr Allocate(IntPtr size)
        {
            return AllocateInternal(size, AllocationType.Reserve | AllocationType.Commit);
        }

        public static IntPtr Reserve(IntPtr size, bool viewCompatible, bool force4KBMap)
        {
            if (viewCompatible)
            {
                IntPtr baseAddress = AllocateInternal2(size, AllocationType.Reserve | AllocationType.ReservePlaceholder);

                if (!force4KBMap)
                {
                    _placeholders.ReserveRange((ulong)baseAddress, (ulong)size);
                }

                return baseAddress;
            }

            return AllocateInternal(size, AllocationType.Reserve);
        }

        private static IntPtr AllocateInternal(IntPtr size, AllocationType flags = 0)
        {
            IntPtr ptr = WindowsApi.VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite);

            if (ptr == IntPtr.Zero)
            {
                throw new OutOfMemoryException();
            }

            return ptr;
        }

        private static IntPtr AllocateInternal2(IntPtr size, AllocationType flags = 0)
        {
            IntPtr ptr = WindowsApi.VirtualAlloc2(WindowsApi.CurrentProcessHandle, IntPtr.Zero, size, flags, MemoryProtection.NoAccess, IntPtr.Zero, 0);

            if (ptr == IntPtr.Zero)
            {
                throw new OutOfMemoryException();
            }

            return ptr;
        }

        public static bool Commit(IntPtr location, IntPtr size)
        {
            return WindowsApi.VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) != IntPtr.Zero;
        }

        public static bool Decommit(IntPtr location, IntPtr size)
        {
            return WindowsApi.VirtualFree(location, size, AllocationType.Decommit);
        }

        public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
        {
            _placeholders.MapView(sharedMemory, srcOffset, location, size);
        }

        public static void MapView4KB(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
        {
            _placeholders4KB.UnmapAndMarkRangeAsMapped(location, size);

            ulong uaddress = (ulong)location;
            ulong usize = (ulong)size;
            IntPtr endLocation = (IntPtr)(uaddress + usize);

            while (location != endLocation)
            {
                WindowsApi.VirtualFree(location, (IntPtr)PageSize, AllocationType.Release | AllocationType.PreservePlaceholder);

                var ptr = WindowsApi.MapViewOfFile3(
                    sharedMemory,
                    WindowsApi.CurrentProcessHandle,
                    location,
                    srcOffset,
                    (IntPtr)PageSize,
                    0x4000,
                    MemoryProtection.ReadWrite,
                    IntPtr.Zero,
                    0);

                if (ptr == IntPtr.Zero)
                {
                    throw new WindowsApiException("MapViewOfFile3");
                }

                location += PageSize;
                srcOffset += PageSize;
            }
        }

        public static void UnmapView(IntPtr sharedMemory, IntPtr location, IntPtr size)
        {
            _placeholders.UnmapView(sharedMemory, location, size);
        }

        public static void UnmapView4KB(IntPtr location, IntPtr size)
        {
            _placeholders4KB.UnmapView(location, size);
        }

        public static bool Reprotect(IntPtr address, IntPtr size, MemoryPermission permission, bool forView)
        {
            if (forView)
            {
                return _placeholders.ReprotectView(address, size, permission);
            }
            else
            {
                return WindowsApi.VirtualProtect(address, size, WindowsApi.GetProtection(permission), out _);
            }
        }

        public static bool Reprotect4KB(IntPtr address, IntPtr size, MemoryPermission permission, bool forView)
        {
            ulong uaddress = (ulong)address;
            ulong usize = (ulong)size;
            while (usize > 0)
            {
                if (!WindowsApi.VirtualProtect((IntPtr)uaddress, (IntPtr)PageSize, WindowsApi.GetProtection(permission), out _))
                {
                    return false;
                }

                uaddress += PageSize;
                usize -= PageSize;
            }

            return true;
        }

        public static bool Free(IntPtr address, IntPtr size, bool force4KBMap)
        {
            if (force4KBMap)
            {
                _placeholders4KB.UnmapRange(address, size);
            }
            else
            {
                _placeholders.UnmapView(IntPtr.Zero, address, size);
            }

            return WindowsApi.VirtualFree(address, IntPtr.Zero, AllocationType.Release);
        }

        public static IntPtr CreateSharedMemory(IntPtr size, bool reserve)
        {
            var prot = reserve ? FileMapProtection.SectionReserve : FileMapProtection.SectionCommit;

            IntPtr handle = WindowsApi.CreateFileMapping(
                WindowsApi.InvalidHandleValue,
                IntPtr.Zero,
                FileMapProtection.PageReadWrite | prot,
                (uint)(size.ToInt64() >> 32),
                (uint)size.ToInt64(),
                null);

            if (handle == IntPtr.Zero)
            {
                throw new OutOfMemoryException();
            }

            return handle;
        }

        public static void DestroySharedMemory(IntPtr handle)
        {
            if (!WindowsApi.CloseHandle(handle))
            {
                throw new ArgumentException("Invalid handle.", nameof(handle));
            }
        }

        public static IntPtr MapSharedMemory(IntPtr handle)
        {
            IntPtr ptr = WindowsApi.MapViewOfFile(handle, 4 | 2, 0, 0, IntPtr.Zero);

            if (ptr == IntPtr.Zero)
            {
                throw new OutOfMemoryException();
            }

            return ptr;
        }

        public static void UnmapSharedMemory(IntPtr address)
        {
            if (!WindowsApi.UnmapViewOfFile(address))
            {
                throw new ArgumentException("Invalid address.", nameof(address));
            }
        }

        public static bool RetryFromAccessViolation()
        {
            return _placeholders.RetryFromAccessViolation();
        }
    }
}