PlaceholderManager4KB.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Runtime.Versioning;
  3. namespace Ryujinx.Memory.WindowsShared
  4. {
  5. /// <summary>
  6. /// Windows 4KB memory placeholder manager.
  7. /// </summary>
  8. [SupportedOSPlatform("windows")]
  9. class PlaceholderManager4KB
  10. {
  11. private const int PageSize = MemoryManagementWindows.PageSize;
  12. private readonly IntervalTree<ulong, byte> _mappings;
  13. /// <summary>
  14. /// Creates a new instance of the Windows 4KB memory placeholder manager.
  15. /// </summary>
  16. public PlaceholderManager4KB()
  17. {
  18. _mappings = new IntervalTree<ulong, byte>();
  19. }
  20. /// <summary>
  21. /// Unmaps the specified range of memory and marks it as mapped internally.
  22. /// </summary>
  23. /// <remarks>
  24. /// Since this marks the range as mapped, the expectation is that the range will be mapped after calling this method.
  25. /// </remarks>
  26. /// <param name="location">Memory address to unmap and mark as mapped</param>
  27. /// <param name="size">Size of the range in bytes</param>
  28. public void UnmapAndMarkRangeAsMapped(IntPtr location, IntPtr size)
  29. {
  30. ulong startAddress = (ulong)location;
  31. ulong unmapSize = (ulong)size;
  32. ulong endAddress = startAddress + unmapSize;
  33. var overlaps = Array.Empty<IntervalTreeNode<ulong, byte>>();
  34. int count = 0;
  35. lock (_mappings)
  36. {
  37. count = _mappings.Get(startAddress, endAddress, ref overlaps);
  38. }
  39. for (int index = 0; index < count; index++)
  40. {
  41. var overlap = overlaps[index];
  42. // Tree operations might modify the node start/end values, so save a copy before we modify the tree.
  43. ulong overlapStart = overlap.Start;
  44. ulong overlapEnd = overlap.End;
  45. ulong overlapValue = overlap.Value;
  46. _mappings.Remove(overlap);
  47. ulong unmapStart = Math.Max(overlapStart, startAddress);
  48. ulong unmapEnd = Math.Min(overlapEnd, endAddress);
  49. if (overlapStart < startAddress)
  50. {
  51. startAddress = overlapStart;
  52. }
  53. if (overlapEnd > endAddress)
  54. {
  55. endAddress = overlapEnd;
  56. }
  57. ulong currentAddress = unmapStart;
  58. while (currentAddress < unmapEnd)
  59. {
  60. WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)currentAddress, 2);
  61. currentAddress += PageSize;
  62. }
  63. }
  64. _mappings.Add(startAddress, endAddress, 0);
  65. }
  66. /// <summary>
  67. /// Unmaps views at the specified memory range.
  68. /// </summary>
  69. /// <param name="location">Address of the range</param>
  70. /// <param name="size">Size of the range in bytes</param>
  71. public void UnmapView(IntPtr location, IntPtr size)
  72. {
  73. ulong startAddress = (ulong)location;
  74. ulong unmapSize = (ulong)size;
  75. ulong endAddress = startAddress + unmapSize;
  76. var overlaps = Array.Empty<IntervalTreeNode<ulong, byte>>();
  77. int count = 0;
  78. lock (_mappings)
  79. {
  80. count = _mappings.Get(startAddress, endAddress, ref overlaps);
  81. }
  82. for (int index = 0; index < count; index++)
  83. {
  84. var overlap = overlaps[index];
  85. // Tree operations might modify the node start/end values, so save a copy before we modify the tree.
  86. ulong overlapStart = overlap.Start;
  87. ulong overlapEnd = overlap.End;
  88. _mappings.Remove(overlap);
  89. if (overlapStart < startAddress)
  90. {
  91. _mappings.Add(overlapStart, startAddress, 0);
  92. }
  93. if (overlapEnd > endAddress)
  94. {
  95. _mappings.Add(endAddress, overlapEnd, 0);
  96. }
  97. ulong unmapStart = Math.Max(overlapStart, startAddress);
  98. ulong unmapEnd = Math.Min(overlapEnd, endAddress);
  99. ulong currentAddress = unmapStart;
  100. while (currentAddress < unmapEnd)
  101. {
  102. WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)currentAddress, 2);
  103. currentAddress += PageSize;
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Unmaps mapped memory at a given range.
  109. /// </summary>
  110. /// <param name="location">Address of the range</param>
  111. /// <param name="size">Size of the range in bytes</param>
  112. public void UnmapRange(IntPtr location, IntPtr size)
  113. {
  114. ulong startAddress = (ulong)location;
  115. ulong unmapSize = (ulong)size;
  116. ulong endAddress = startAddress + unmapSize;
  117. var overlaps = Array.Empty<IntervalTreeNode<ulong, byte>>();
  118. int count = 0;
  119. lock (_mappings)
  120. {
  121. count = _mappings.Get(startAddress, endAddress, ref overlaps);
  122. }
  123. for (int index = 0; index < count; index++)
  124. {
  125. var overlap = overlaps[index];
  126. // Tree operations might modify the node start/end values, so save a copy before we modify the tree.
  127. ulong unmapStart = Math.Max(overlap.Start, startAddress);
  128. ulong unmapEnd = Math.Min(overlap.End, endAddress);
  129. _mappings.Remove(overlap);
  130. ulong currentAddress = unmapStart;
  131. while (currentAddress < unmapEnd)
  132. {
  133. WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)currentAddress, 2);
  134. currentAddress += PageSize;
  135. }
  136. }
  137. }
  138. }
  139. }