PlaceholderManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. namespace Ryujinx.Memory.WindowsShared
  5. {
  6. /// <summary>
  7. /// Windows memory placeholder manager.
  8. /// </summary>
  9. class PlaceholderManager
  10. {
  11. private const ulong MinimumPageSize = 0x1000;
  12. [ThreadStatic]
  13. private static int _threadLocalPartialUnmapsCount;
  14. private readonly IntervalTree<ulong, ulong> _mappings;
  15. private readonly IntervalTree<ulong, MemoryPermission> _protections;
  16. private readonly ReaderWriterLock _partialUnmapLock;
  17. private int _partialUnmapsCount;
  18. /// <summary>
  19. /// Creates a new instance of the Windows memory placeholder manager.
  20. /// </summary>
  21. public PlaceholderManager()
  22. {
  23. _mappings = new IntervalTree<ulong, ulong>();
  24. _protections = new IntervalTree<ulong, MemoryPermission>();
  25. _partialUnmapLock = new ReaderWriterLock();
  26. }
  27. /// <summary>
  28. /// Reserves a range of the address space to be later mapped as shared memory views.
  29. /// </summary>
  30. /// <param name="address">Start address of the region to reserve</param>
  31. /// <param name="size">Size in bytes of the region to reserve</param>
  32. public void ReserveRange(ulong address, ulong size)
  33. {
  34. lock (_mappings)
  35. {
  36. _mappings.Add(address, address + size, ulong.MaxValue);
  37. }
  38. }
  39. /// <summary>
  40. /// Maps a shared memory view on a previously reserved memory region.
  41. /// </summary>
  42. /// <param name="sharedMemory">Shared memory that will be the backing storage for the view</param>
  43. /// <param name="srcOffset">Offset in the shared memory to map</param>
  44. /// <param name="location">Address to map the view into</param>
  45. /// <param name="size">Size of the view in bytes</param>
  46. public void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
  47. {
  48. _partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
  49. try
  50. {
  51. UnmapViewInternal(sharedMemory, location, size);
  52. MapViewInternal(sharedMemory, srcOffset, location, size);
  53. }
  54. finally
  55. {
  56. _partialUnmapLock.ReleaseReaderLock();
  57. }
  58. }
  59. /// <summary>
  60. /// Maps a shared memory view on a previously reserved memory region.
  61. /// </summary>
  62. /// <param name="sharedMemory">Shared memory that will be the backing storage for the view</param>
  63. /// <param name="srcOffset">Offset in the shared memory to map</param>
  64. /// <param name="location">Address to map the view into</param>
  65. /// <param name="size">Size of the view in bytes</param>
  66. /// <exception cref="WindowsApiException">Thrown when the Windows API returns an error mapping the memory</exception>
  67. private void MapViewInternal(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
  68. {
  69. SplitForMap((ulong)location, (ulong)size, srcOffset);
  70. var ptr = WindowsApi.MapViewOfFile3(
  71. sharedMemory,
  72. WindowsApi.CurrentProcessHandle,
  73. location,
  74. srcOffset,
  75. size,
  76. 0x4000,
  77. MemoryProtection.ReadWrite,
  78. IntPtr.Zero,
  79. 0);
  80. if (ptr == IntPtr.Zero)
  81. {
  82. throw new WindowsApiException("MapViewOfFile3");
  83. }
  84. }
  85. /// <summary>
  86. /// Splits a larger placeholder, slicing at the start and end address, for a new memory mapping.
  87. /// </summary>
  88. /// <param name="address">Address to split</param>
  89. /// <param name="size">Size of the new region</param>
  90. /// <param name="backingOffset">Offset in the shared memory that will be mapped</param>
  91. private void SplitForMap(ulong address, ulong size, ulong backingOffset)
  92. {
  93. ulong endAddress = address + size;
  94. var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
  95. lock (_mappings)
  96. {
  97. int count = _mappings.Get(address, endAddress, ref overlaps);
  98. Debug.Assert(count == 1);
  99. Debug.Assert(!IsMapped(overlaps[0].Value));
  100. var overlap = overlaps[0];
  101. // Tree operations might modify the node start/end values, so save a copy before we modify the tree.
  102. ulong overlapStart = overlap.Start;
  103. ulong overlapEnd = overlap.End;
  104. ulong overlapValue = overlap.Value;
  105. _mappings.Remove(overlap);
  106. bool overlapStartsBefore = overlapStart < address;
  107. bool overlapEndsAfter = overlapEnd > endAddress;
  108. if (overlapStartsBefore && overlapEndsAfter)
  109. {
  110. CheckFreeResult(WindowsApi.VirtualFree(
  111. (IntPtr)address,
  112. (IntPtr)size,
  113. AllocationType.Release | AllocationType.PreservePlaceholder));
  114. _mappings.Add(overlapStart, address, overlapValue);
  115. _mappings.Add(endAddress, overlapEnd, AddBackingOffset(overlapValue, endAddress - overlapStart));
  116. }
  117. else if (overlapStartsBefore)
  118. {
  119. ulong overlappedSize = overlapEnd - address;
  120. CheckFreeResult(WindowsApi.VirtualFree(
  121. (IntPtr)address,
  122. (IntPtr)overlappedSize,
  123. AllocationType.Release | AllocationType.PreservePlaceholder));
  124. _mappings.Add(overlapStart, address, overlapValue);
  125. }
  126. else if (overlapEndsAfter)
  127. {
  128. ulong overlappedSize = endAddress - overlapStart;
  129. CheckFreeResult(WindowsApi.VirtualFree(
  130. (IntPtr)overlapStart,
  131. (IntPtr)overlappedSize,
  132. AllocationType.Release | AllocationType.PreservePlaceholder));
  133. _mappings.Add(endAddress, overlapEnd, AddBackingOffset(overlapValue, overlappedSize));
  134. }
  135. _mappings.Add(address, endAddress, backingOffset);
  136. }
  137. }
  138. /// <summary>
  139. /// Unmaps a view that has been previously mapped with <see cref="MapView"/>.
  140. /// </summary>
  141. /// <remarks>
  142. /// For "partial unmaps" (when not the entire mapped range is being unmapped), it might be
  143. /// necessary to unmap the whole range and then remap the sub-ranges that should remain mapped.
  144. /// </remarks>
  145. /// <param name="sharedMemory">Shared memory that the view being unmapped belongs to</param>
  146. /// <param name="location">Address to unmap</param>
  147. /// <param name="size">Size of the region to unmap in bytes</param>
  148. public void UnmapView(IntPtr sharedMemory, IntPtr location, IntPtr size)
  149. {
  150. _partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
  151. try
  152. {
  153. UnmapViewInternal(sharedMemory, location, size);
  154. }
  155. finally
  156. {
  157. _partialUnmapLock.ReleaseReaderLock();
  158. }
  159. }
  160. /// <summary>
  161. /// Unmaps a view that has been previously mapped with <see cref="MapView"/>.
  162. /// </summary>
  163. /// <remarks>
  164. /// For "partial unmaps" (when not the entire mapped range is being unmapped), it might be
  165. /// necessary to unmap the whole range and then remap the sub-ranges that should remain mapped.
  166. /// </remarks>
  167. /// <param name="sharedMemory">Shared memory that the view being unmapped belongs to</param>
  168. /// <param name="location">Address to unmap</param>
  169. /// <param name="size">Size of the region to unmap in bytes</param>
  170. /// <exception cref="WindowsApiException">Thrown when the Windows API returns an error unmapping or remapping the memory</exception>
  171. private void UnmapViewInternal(IntPtr sharedMemory, IntPtr location, IntPtr size)
  172. {
  173. ulong startAddress = (ulong)location;
  174. ulong unmapSize = (ulong)size;
  175. ulong endAddress = startAddress + unmapSize;
  176. var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
  177. int count = 0;
  178. lock (_mappings)
  179. {
  180. count = _mappings.Get(startAddress, endAddress, ref overlaps);
  181. }
  182. for (int index = 0; index < count; index++)
  183. {
  184. var overlap = overlaps[index];
  185. if (IsMapped(overlap.Value))
  186. {
  187. if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2))
  188. {
  189. throw new WindowsApiException("UnmapViewOfFile2");
  190. }
  191. // Tree operations might modify the node start/end values, so save a copy before we modify the tree.
  192. ulong overlapStart = overlap.Start;
  193. ulong overlapEnd = overlap.End;
  194. ulong overlapValue = overlap.Value;
  195. _mappings.Remove(overlap);
  196. _mappings.Add(overlapStart, overlapEnd, ulong.MaxValue);
  197. bool overlapStartsBefore = overlapStart < startAddress;
  198. bool overlapEndsAfter = overlapEnd > endAddress;
  199. if (overlapStartsBefore || overlapEndsAfter)
  200. {
  201. // If the overlap extends beyond the region we are unmapping,
  202. // then we need to re-map the regions that are supposed to remain mapped.
  203. // This is necessary because Windows does not support partial view unmaps.
  204. // That is, you can only fully unmap a view that was previously mapped, you can't just unmap a chunck of it.
  205. LockCookie lockCookie = _partialUnmapLock.UpgradeToWriterLock(Timeout.Infinite);
  206. _partialUnmapsCount++;
  207. if (overlapStartsBefore)
  208. {
  209. ulong remapSize = startAddress - overlapStart;
  210. MapViewInternal(sharedMemory, overlapValue, (IntPtr)overlapStart, (IntPtr)remapSize);
  211. RestoreRangeProtection(overlapStart, remapSize);
  212. }
  213. if (overlapEndsAfter)
  214. {
  215. ulong overlappedSize = endAddress - overlapStart;
  216. ulong remapBackingOffset = overlapValue + overlappedSize;
  217. ulong remapAddress = overlapStart + overlappedSize;
  218. ulong remapSize = overlapEnd - endAddress;
  219. MapViewInternal(sharedMemory, remapBackingOffset, (IntPtr)remapAddress, (IntPtr)remapSize);
  220. RestoreRangeProtection(remapAddress, remapSize);
  221. }
  222. _partialUnmapLock.DowngradeFromWriterLock(ref lockCookie);
  223. }
  224. }
  225. }
  226. CoalesceForUnmap(startAddress, unmapSize);
  227. RemoveProtection(startAddress, unmapSize);
  228. }
  229. /// <summary>
  230. /// Coalesces adjacent placeholders after unmap.
  231. /// </summary>
  232. /// <param name="address">Address of the region that was unmapped</param>
  233. /// <param name="size">Size of the region that was unmapped in bytes</param>
  234. private void CoalesceForUnmap(ulong address, ulong size)
  235. {
  236. ulong endAddress = address + size;
  237. var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
  238. int unmappedCount = 0;
  239. lock (_mappings)
  240. {
  241. int count = _mappings.Get(address - MinimumPageSize, endAddress + MinimumPageSize, ref overlaps);
  242. if (count < 2)
  243. {
  244. // Nothing to coalesce if we only have 1 or no overlaps.
  245. return;
  246. }
  247. for (int index = 0; index < count; index++)
  248. {
  249. var overlap = overlaps[index];
  250. if (!IsMapped(overlap.Value))
  251. {
  252. if (address > overlap.Start)
  253. {
  254. address = overlap.Start;
  255. }
  256. if (endAddress < overlap.End)
  257. {
  258. endAddress = overlap.End;
  259. }
  260. _mappings.Remove(overlap);
  261. unmappedCount++;
  262. }
  263. }
  264. _mappings.Add(address, endAddress, ulong.MaxValue);
  265. }
  266. if (unmappedCount > 1)
  267. {
  268. size = endAddress - address;
  269. CheckFreeResult(WindowsApi.VirtualFree(
  270. (IntPtr)address,
  271. (IntPtr)size,
  272. AllocationType.Release | AllocationType.CoalescePlaceholders));
  273. }
  274. }
  275. /// <summary>
  276. /// Reprotects a region of memory that has been mapped.
  277. /// </summary>
  278. /// <param name="address">Address of the region to reprotect</param>
  279. /// <param name="size">Size of the region to reprotect in bytes</param>
  280. /// <param name="permission">New permissions</param>
  281. /// <returns>True if the reprotection was successful, false otherwise</returns>
  282. public bool ReprotectView(IntPtr address, IntPtr size, MemoryPermission permission)
  283. {
  284. _partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
  285. try
  286. {
  287. return ReprotectViewInternal(address, size, permission, false);
  288. }
  289. finally
  290. {
  291. _partialUnmapLock.ReleaseReaderLock();
  292. }
  293. }
  294. /// <summary>
  295. /// Reprotects a region of memory that has been mapped.
  296. /// </summary>
  297. /// <param name="address">Address of the region to reprotect</param>
  298. /// <param name="size">Size of the region to reprotect in bytes</param>
  299. /// <param name="permission">New permissions</param>
  300. /// <param name="throwOnError">Throw an exception instead of returning an error if the operation fails</param>
  301. /// <returns>True if the reprotection was successful or if <paramref name="throwOnError"/> is true, false otherwise</returns>
  302. /// <exception cref="WindowsApiException">If <paramref name="throwOnError"/> is true, it is thrown when the Windows API returns an error reprotecting the memory</exception>
  303. private bool ReprotectViewInternal(IntPtr address, IntPtr size, MemoryPermission permission, bool throwOnError)
  304. {
  305. ulong reprotectAddress = (ulong)address;
  306. ulong reprotectSize = (ulong)size;
  307. ulong endAddress = reprotectAddress + reprotectSize;
  308. var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
  309. int count = 0;
  310. lock (_mappings)
  311. {
  312. count = _mappings.Get(reprotectAddress, endAddress, ref overlaps);
  313. }
  314. bool success = true;
  315. for (int index = 0; index < count; index++)
  316. {
  317. var overlap = overlaps[index];
  318. ulong mappedAddress = overlap.Start;
  319. ulong mappedSize = overlap.End - overlap.Start;
  320. if (mappedAddress < reprotectAddress)
  321. {
  322. ulong delta = reprotectAddress - mappedAddress;
  323. mappedAddress = reprotectAddress;
  324. mappedSize -= delta;
  325. }
  326. ulong mappedEndAddress = mappedAddress + mappedSize;
  327. if (mappedEndAddress > endAddress)
  328. {
  329. ulong delta = mappedEndAddress - endAddress;
  330. mappedSize -= delta;
  331. }
  332. if (!WindowsApi.VirtualProtect((IntPtr)mappedAddress, (IntPtr)mappedSize, WindowsApi.GetProtection(permission), out _))
  333. {
  334. if (throwOnError)
  335. {
  336. throw new WindowsApiException("VirtualProtect");
  337. }
  338. success = false;
  339. }
  340. // We only keep track of "non-standard" protections,
  341. // that is, everything that is not just RW (which is the default when views are mapped).
  342. if (permission == MemoryPermission.ReadAndWrite)
  343. {
  344. RemoveProtection(mappedAddress, mappedSize);
  345. }
  346. else
  347. {
  348. AddProtection(mappedAddress, mappedSize, permission);
  349. }
  350. }
  351. return success;
  352. }
  353. /// <summary>
  354. /// Checks the result of a VirtualFree operation, throwing if needed.
  355. /// </summary>
  356. /// <param name="success">Operation result</param>
  357. /// <exception cref="WindowsApiException">Thrown if <paramref name="success"/> is false</exception>
  358. private static void CheckFreeResult(bool success)
  359. {
  360. if (!success)
  361. {
  362. throw new WindowsApiException("VirtualFree");
  363. }
  364. }
  365. /// <summary>
  366. /// Adds an offset to a backing offset. This will do nothing if the backing offset is the special "unmapped" value.
  367. /// </summary>
  368. /// <param name="backingOffset">Backing offset</param>
  369. /// <param name="offset">Offset to be added</param>
  370. /// <returns>Added offset or just <paramref name="backingOffset"/> if the region is unmapped</returns>
  371. private static ulong AddBackingOffset(ulong backingOffset, ulong offset)
  372. {
  373. if (backingOffset == ulong.MaxValue)
  374. {
  375. return backingOffset;
  376. }
  377. return backingOffset + offset;
  378. }
  379. /// <summary>
  380. /// Checks if a region is unmapped.
  381. /// </summary>
  382. /// <param name="backingOffset">Backing offset to check</param>
  383. /// <returns>True if the backing offset is the special "unmapped" value, false otherwise</returns>
  384. private static bool IsMapped(ulong backingOffset)
  385. {
  386. return backingOffset != ulong.MaxValue;
  387. }
  388. /// <summary>
  389. /// Adds a protection to the list of protections.
  390. /// </summary>
  391. /// <param name="address">Address of the protected region</param>
  392. /// <param name="size">Size of the protected region in bytes</param>
  393. /// <param name="permission">Memory permissions of the region</param>
  394. private void AddProtection(ulong address, ulong size, MemoryPermission permission)
  395. {
  396. ulong endAddress = address + size;
  397. var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
  398. int count = 0;
  399. lock (_protections)
  400. {
  401. count = _protections.Get(address, endAddress, ref overlaps);
  402. Debug.Assert(count > 0);
  403. if (count == 1 &&
  404. overlaps[0].Start <= address &&
  405. overlaps[0].End >= endAddress &&
  406. overlaps[0].Value == permission)
  407. {
  408. return;
  409. }
  410. ulong startAddress = address;
  411. for (int index = 0; index < count; index++)
  412. {
  413. var protection = overlaps[index];
  414. ulong protAddress = protection.Start;
  415. ulong protEndAddress = protection.End;
  416. MemoryPermission protPermission = protection.Value;
  417. _protections.Remove(protection);
  418. if (protection.Value == permission)
  419. {
  420. if (startAddress > protAddress)
  421. {
  422. startAddress = protAddress;
  423. }
  424. if (endAddress < protEndAddress)
  425. {
  426. endAddress = protEndAddress;
  427. }
  428. }
  429. else
  430. {
  431. if (startAddress > protAddress)
  432. {
  433. _protections.Add(protAddress, startAddress, protPermission);
  434. }
  435. if (endAddress < protEndAddress)
  436. {
  437. _protections.Add(endAddress, protEndAddress, protPermission);
  438. }
  439. }
  440. }
  441. _protections.Add(startAddress, endAddress, permission);
  442. }
  443. }
  444. /// <summary>
  445. /// Removes protection from the list of protections.
  446. /// </summary>
  447. /// <param name="address">Address of the protected region</param>
  448. /// <param name="size">Size of the protected region in bytes</param>
  449. private void RemoveProtection(ulong address, ulong size)
  450. {
  451. ulong endAddress = address + size;
  452. var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
  453. int count = 0;
  454. lock (_protections)
  455. {
  456. count = _protections.Get(address, endAddress, ref overlaps);
  457. for (int index = 0; index < count; index++)
  458. {
  459. var protection = overlaps[index];
  460. ulong protAddress = protection.Start;
  461. ulong protEndAddress = protection.End;
  462. MemoryPermission protPermission = protection.Value;
  463. _protections.Remove(protection);
  464. if (address > protAddress)
  465. {
  466. _protections.Add(protAddress, address, protPermission);
  467. }
  468. if (endAddress < protEndAddress)
  469. {
  470. _protections.Add(endAddress, protEndAddress, protPermission);
  471. }
  472. }
  473. }
  474. }
  475. /// <summary>
  476. /// Restores the protection of a given memory region that was remapped, using the protections list.
  477. /// </summary>
  478. /// <param name="address">Address of the remapped region</param>
  479. /// <param name="size">Size of the remapped region in bytes</param>
  480. private void RestoreRangeProtection(ulong address, ulong size)
  481. {
  482. ulong endAddress = address + size;
  483. var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
  484. int count = 0;
  485. lock (_protections)
  486. {
  487. count = _protections.Get(address, endAddress, ref overlaps);
  488. }
  489. ulong startAddress = address;
  490. for (int index = 0; index < count; index++)
  491. {
  492. var protection = overlaps[index];
  493. ulong protAddress = protection.Start;
  494. ulong protEndAddress = protection.End;
  495. if (protAddress < address)
  496. {
  497. protAddress = address;
  498. }
  499. if (protEndAddress > endAddress)
  500. {
  501. protEndAddress = endAddress;
  502. }
  503. ReprotectViewInternal((IntPtr)protAddress, (IntPtr)(protEndAddress - protAddress), protection.Value, true);
  504. }
  505. }
  506. /// <summary>
  507. /// Checks if an access violation handler should retry execution due to a fault caused by partial unmap.
  508. /// </summary>
  509. /// <remarks>
  510. /// Due to Windows limitations, <see cref="UnmapView"/> might need to unmap more memory than requested.
  511. /// The additional memory that was unmapped is later remapped, however this leaves a time gap where the
  512. /// memory might be accessed but is unmapped. Users of the API must compensate for that by catching the
  513. /// access violation and retrying if it happened between the unmap and remap operation.
  514. /// This method can be used to decide if retrying in such cases is necessary or not.
  515. /// </remarks>
  516. /// <returns>True if execution should be retried, false otherwise</returns>
  517. public bool RetryFromAccessViolation()
  518. {
  519. _partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
  520. bool retry = _threadLocalPartialUnmapsCount != _partialUnmapsCount;
  521. if (retry)
  522. {
  523. _threadLocalPartialUnmapsCount = _partialUnmapsCount;
  524. }
  525. _partialUnmapLock.ReleaseReaderLock();
  526. return retry;
  527. }
  528. }
  529. }