RegionHandle.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using Ryujinx.Memory.Range;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace Ryujinx.Memory.Tracking
  6. {
  7. /// <summary>
  8. /// A tracking handle for a given region of virtual memory. The Dirty flag is updated whenever any changes are made,
  9. /// and an action can be performed when the region is read to or written from.
  10. /// </summary>
  11. public class RegionHandle : IRegionHandle, IRange
  12. {
  13. public bool Dirty { get; private set; }
  14. public bool Unmapped { get; private set; }
  15. public ulong Address { get; }
  16. public ulong Size { get; }
  17. public ulong EndAddress { get; }
  18. internal IMultiRegionHandle Parent { get; set; }
  19. internal int SequenceNumber { get; set; }
  20. private event Action _onDirty;
  21. private RegionSignal _preAction; // Action to perform before a read or write. This will block the memory access.
  22. private readonly List<VirtualRegion> _regions;
  23. private readonly MemoryTracking _tracking;
  24. private bool _disposed;
  25. internal MemoryPermission RequiredPermission => _preAction != null ? MemoryPermission.None : (Dirty ? MemoryPermission.ReadAndWrite : MemoryPermission.Read);
  26. internal RegionSignal PreAction => _preAction;
  27. /// <summary>
  28. /// Create a new region handle. The handle is registered with the given tracking object,
  29. /// and will be notified of any changes to the specified region.
  30. /// </summary>
  31. /// <param name="tracking">Tracking object for the target memory block</param>
  32. /// <param name="address">Virtual address of the region to track</param>
  33. /// <param name="size">Size of the region to track</param>
  34. /// <param name="mapped">True if the region handle starts mapped</param>
  35. internal RegionHandle(MemoryTracking tracking, ulong address, ulong size, bool mapped = true)
  36. {
  37. Dirty = mapped;
  38. Unmapped = !mapped;
  39. Address = address;
  40. Size = size;
  41. EndAddress = address + size;
  42. _tracking = tracking;
  43. _regions = tracking.GetVirtualRegionsForHandle(address, size);
  44. foreach (var region in _regions)
  45. {
  46. region.Handles.Add(this);
  47. }
  48. }
  49. /// <summary>
  50. /// Signal that a memory action occurred within this handle's virtual regions.
  51. /// </summary>
  52. /// <param name="write">Whether the region was written to or read</param>
  53. internal void Signal(ulong address, ulong size, bool write)
  54. {
  55. RegionSignal action = Interlocked.Exchange(ref _preAction, null);
  56. action?.Invoke(address, size);
  57. if (write)
  58. {
  59. bool oldDirty = Dirty;
  60. Dirty = true;
  61. if (!oldDirty)
  62. {
  63. _onDirty?.Invoke();
  64. }
  65. Parent?.SignalWrite();
  66. }
  67. }
  68. /// <summary>
  69. /// Consume the dirty flag for this handle, and reprotect so it can be set on the next write.
  70. /// </summary>
  71. public void Reprotect(bool asDirty = false)
  72. {
  73. Dirty = asDirty;
  74. lock (_tracking.TrackingLock)
  75. {
  76. foreach (VirtualRegion region in _regions)
  77. {
  78. region.UpdateProtection();
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// Register an action to perform when the tracked region is read or written.
  84. /// The action is automatically removed after it runs.
  85. /// </summary>
  86. /// <param name="action">Action to call on read or write</param>
  87. public void RegisterAction(RegionSignal action)
  88. {
  89. RegionSignal lastAction = Interlocked.Exchange(ref _preAction, action);
  90. if (lastAction == null && action != lastAction)
  91. {
  92. lock (_tracking.TrackingLock)
  93. {
  94. foreach (VirtualRegion region in _regions)
  95. {
  96. region.UpdateProtection();
  97. }
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// Register an action to perform when the region is written to.
  103. /// This action will not be removed when it is called - it is called each time the dirty flag is set.
  104. /// </summary>
  105. /// <param name="action">Action to call on dirty</param>
  106. public void RegisterDirtyEvent(Action action)
  107. {
  108. _onDirty += action;
  109. }
  110. /// <summary>
  111. /// Add a child virtual region to this handle.
  112. /// </summary>
  113. /// <param name="region">Virtual region to add as a child</param>
  114. internal void AddChild(VirtualRegion region)
  115. {
  116. _regions.Add(region);
  117. }
  118. /// <summary>
  119. /// Signal that this handle has been mapped or unmapped.
  120. /// </summary>
  121. /// <param name="mapped">True if the handle has been mapped, false if unmapped</param>
  122. internal void SignalMappingChanged(bool mapped)
  123. {
  124. if (Unmapped == mapped)
  125. {
  126. Unmapped = !mapped;
  127. if (Unmapped)
  128. {
  129. Dirty = false;
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// Check if this region overlaps with another.
  135. /// </summary>
  136. /// <param name="address">Base address</param>
  137. /// <param name="size">Size of the region</param>
  138. /// <returns>True if overlapping, false otherwise</returns>
  139. public bool OverlapsWith(ulong address, ulong size)
  140. {
  141. return Address < address + size && address < EndAddress;
  142. }
  143. /// <summary>
  144. /// Dispose the handle. Within the tracking lock, this removes references from virtual regions.
  145. /// </summary>
  146. public void Dispose()
  147. {
  148. if (_disposed)
  149. {
  150. throw new ObjectDisposedException(GetType().FullName);
  151. }
  152. _disposed = true;
  153. lock (_tracking.TrackingLock)
  154. {
  155. foreach (VirtualRegion region in _regions)
  156. {
  157. region.RemoveHandle(this);
  158. }
  159. }
  160. }
  161. }
  162. }