VirtualRegion.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /// Gets the strictest permission that the child handles demand. Assumes that the tracking lock has been obtained.
  60. /// </summary>
  61. /// <returns>Protection level that this region demands</returns>
  62. public MemoryPermission GetRequiredPermission()
  63. {
  64. // Start with Read/Write, each handle can strip off permissions as necessary.
  65. // Assumes the tracking lock has already been obtained.
  66. MemoryPermission result = MemoryPermission.ReadAndWrite;
  67. foreach (var handle in Handles)
  68. {
  69. result &= handle.RequiredPermission;
  70. if (result == 0) return result;
  71. }
  72. return result;
  73. }
  74. /// <summary>
  75. /// Updates the protection for this virtual region, and all child physical regions.
  76. /// </summary>
  77. public void UpdateProtection()
  78. {
  79. // Re-evaluate protection for all physical children.
  80. _tracking.ProtectVirtualRegion(this, GetRequiredPermission());
  81. lock (_tracking.TrackingLock)
  82. {
  83. foreach (var child in _physicalChildren)
  84. {
  85. child.UpdateProtection();
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// Removes a handle from this virtual region. If there are no handles left, this virtual region is removed.
  91. /// </summary>
  92. /// <param name="handle">Handle to remove</param>
  93. public void RemoveHandle(RegionHandle handle)
  94. {
  95. bool removedRegions = false;
  96. lock (_tracking.TrackingLock)
  97. {
  98. Handles.Remove(handle);
  99. UpdateProtection();
  100. if (Handles.Count == 0)
  101. {
  102. _tracking.RemoveVirtual(this);
  103. foreach (var child in _physicalChildren)
  104. {
  105. removedRegions |= child.RemoveParent(this);
  106. }
  107. }
  108. }
  109. if (removedRegions)
  110. {
  111. // The first lock will unprotect any regions that have been removed. This second lock will remove them.
  112. lock (_tracking.TrackingLock)
  113. {
  114. foreach (var child in _physicalChildren)
  115. {
  116. child.TryDelete();
  117. }
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// Add a child physical region to this virtual region. Assumes that the tracking lock has been obtained.
  123. /// </summary>
  124. /// <param name="region">Physical region to add as a child</param>
  125. public void AddChild(PhysicalRegion region)
  126. {
  127. _physicalChildren.Add(region);
  128. }
  129. public override INonOverlappingRange Split(ulong splitAddress)
  130. {
  131. ClearPhysicalChildren();
  132. VirtualRegion newRegion = new VirtualRegion(_tracking, splitAddress, EndAddress - splitAddress);
  133. Size = splitAddress - Address;
  134. UpdatePhysicalChildren();
  135. // The new region inherits all of our parents.
  136. newRegion.Handles = new List<RegionHandle>(Handles);
  137. foreach (var parent in Handles)
  138. {
  139. parent.AddChild(newRegion);
  140. }
  141. return newRegion;
  142. }
  143. }
  144. }