RegionHandle.cs 4.9 KB

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