VirtualRegion.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Ryujinx.Memory.Range;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Memory.Tracking
  4. {
  5. /// <summary>
  6. /// A region of virtual memory.
  7. /// </summary>
  8. class VirtualRegion : AbstractRegion
  9. {
  10. public List<RegionHandle> Handles = new List<RegionHandle>();
  11. private List<PhysicalRegion> _physicalChildren;
  12. private readonly MemoryTracking _tracking;
  13. public VirtualRegion(MemoryTracking tracking, ulong address, ulong size) : base(address, size)
  14. {
  15. _tracking = tracking;
  16. UpdatePhysicalChildren();
  17. }
  18. public override void Signal(ulong address, ulong size, bool write)
  19. {
  20. foreach (var handle in Handles)
  21. {
  22. handle.Signal(address, size, write);
  23. }
  24. UpdateProtection();
  25. }
  26. /// <summary>
  27. /// Clears all physical children of this region. Assumes that the tracking lock has been obtained.
  28. /// </summary>
  29. private void ClearPhysicalChildren()
  30. {
  31. if (_physicalChildren != null)
  32. {
  33. foreach (PhysicalRegion child in _physicalChildren)
  34. {
  35. child.RemoveParent(this);
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// Updates the physical children of this region, assuming that they are clear and that the tracking lock has been obtained.
  41. /// </summary>
  42. private void UpdatePhysicalChildren()
  43. {
  44. _physicalChildren = _tracking.GetPhysicalRegionsForVirtual(Address, Size);
  45. foreach (PhysicalRegion child in _physicalChildren)
  46. {
  47. child.VirtualParents.Add(this);
  48. }
  49. }
  50. /// <summary>
  51. /// Recalculates the physical children for this virtual region. Assumes that the tracking lock has been obtained.
  52. /// </summary>
  53. public void RecalculatePhysicalChildren()
  54. {
  55. ClearPhysicalChildren();
  56. UpdatePhysicalChildren();
  57. }
  58. /// <summary>
  59. /// Signal that this region has been mapped or unmapped.
  60. /// </summary>
  61. /// <param name="mapped">True if the region has been mapped, false if unmapped</param>
  62. public void SignalMappingChanged(bool mapped)
  63. {
  64. foreach (RegionHandle handle in Handles)
  65. {
  66. handle.SignalMappingChanged(mapped);
  67. }
  68. }
  69. /// <summary>
  70. /// Gets the strictest permission that the child handles demand. Assumes that the tracking lock has been obtained.
  71. /// </summary>
  72. /// <returns>Protection level that this region demands</returns>
  73. public MemoryPermission GetRequiredPermission()
  74. {
  75. // Start with Read/Write, each handle can strip off permissions as necessary.
  76. // Assumes the tracking lock has already been obtained.
  77. MemoryPermission result = MemoryPermission.ReadAndWrite;
  78. foreach (var handle in Handles)
  79. {
  80. result &= handle.RequiredPermission;
  81. if (result == 0) return result;
  82. }
  83. return result;
  84. }
  85. /// <summary>
  86. /// Updates the protection for this virtual region, and all child physical regions.
  87. /// </summary>
  88. public void UpdateProtection()
  89. {
  90. // Re-evaluate protection for all physical children.
  91. _tracking.ProtectVirtualRegion(this, GetRequiredPermission());
  92. lock (_tracking.TrackingLock)
  93. {
  94. foreach (var child in _physicalChildren)
  95. {
  96. child.UpdateProtection();
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// Removes a handle from this virtual region. If there are no handles left, this virtual region is removed.
  102. /// </summary>
  103. /// <param name="handle">Handle to remove</param>
  104. public void RemoveHandle(RegionHandle handle)
  105. {
  106. bool removedRegions = false;
  107. lock (_tracking.TrackingLock)
  108. {
  109. Handles.Remove(handle);
  110. UpdateProtection();
  111. if (Handles.Count == 0)
  112. {
  113. _tracking.RemoveVirtual(this);
  114. foreach (var child in _physicalChildren)
  115. {
  116. removedRegions |= child.RemoveParent(this);
  117. }
  118. }
  119. }
  120. if (removedRegions)
  121. {
  122. // The first lock will unprotect any regions that have been removed. This second lock will remove them.
  123. lock (_tracking.TrackingLock)
  124. {
  125. foreach (var child in _physicalChildren)
  126. {
  127. child.TryDelete();
  128. }
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Add a child physical region to this virtual region. Assumes that the tracking lock has been obtained.
  134. /// </summary>
  135. /// <param name="region">Physical region to add as a child</param>
  136. public void AddChild(PhysicalRegion region)
  137. {
  138. _physicalChildren.Add(region);
  139. }
  140. public override INonOverlappingRange Split(ulong splitAddress)
  141. {
  142. ClearPhysicalChildren();
  143. VirtualRegion newRegion = new VirtualRegion(_tracking, splitAddress, EndAddress - splitAddress);
  144. Size = splitAddress - Address;
  145. UpdatePhysicalChildren();
  146. // The new region inherits all of our parents.
  147. newRegion.Handles = new List<RegionHandle>(Handles);
  148. foreach (var parent in Handles)
  149. {
  150. parent.AddChild(newRegion);
  151. }
  152. return newRegion;
  153. }
  154. }
  155. }