RegionHandle.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /// <summary>
  23. /// Create a new region handle. The handle is registered with the given tracking object,
  24. /// and will be notified of any changes to the specified region.
  25. /// </summary>
  26. /// <param name="tracking">Tracking object for the target memory block</param>
  27. /// <param name="address">Virtual address of the region to track</param>
  28. /// <param name="size">Size of the region to track</param>
  29. /// <param name="dirty">Initial value of the dirty flag</param>
  30. internal RegionHandle(MemoryTracking tracking, ulong address, ulong size, bool dirty = true)
  31. {
  32. Dirty = dirty;
  33. Address = address;
  34. Size = size;
  35. EndAddress = address + size;
  36. _tracking = tracking;
  37. _regions = tracking.GetVirtualRegionsForHandle(address, size);
  38. foreach (var region in _regions)
  39. {
  40. region.Handles.Add(this);
  41. }
  42. }
  43. /// <summary>
  44. /// Signal that a memory action occurred within this handle's virtual regions.
  45. /// </summary>
  46. /// <param name="write">Whether the region was written to or read</param>
  47. internal void Signal(ulong address, ulong size, bool write)
  48. {
  49. RegionSignal action = Interlocked.Exchange(ref _preAction, null);
  50. action?.Invoke(address, size);
  51. if (write)
  52. {
  53. Dirty = true;
  54. Parent?.SignalWrite();
  55. }
  56. }
  57. /// <summary>
  58. /// Consume the dirty flag for this handle, and reprotect so it can be set on the next write.
  59. /// </summary>
  60. public void Reprotect()
  61. {
  62. Dirty = false;
  63. lock (_tracking.TrackingLock)
  64. {
  65. foreach (VirtualRegion region in _regions)
  66. {
  67. region.UpdateProtection();
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// Register an action to perform when the tracked region is read or written.
  73. /// The action is automatically removed after it runs.
  74. /// </summary>
  75. /// <param name="action">Action to call on read or write</param>
  76. public void RegisterAction(RegionSignal action)
  77. {
  78. RegionSignal lastAction = Interlocked.Exchange(ref _preAction, action);
  79. if (lastAction == null && action != lastAction)
  80. {
  81. lock (_tracking.TrackingLock)
  82. {
  83. foreach (VirtualRegion region in _regions)
  84. {
  85. region.UpdateProtection();
  86. }
  87. }
  88. }
  89. }
  90. /// <summary>
  91. /// Add a child virtual region to this handle.
  92. /// </summary>
  93. /// <param name="region">Virtual region to add as a child</param>
  94. internal void AddChild(VirtualRegion region)
  95. {
  96. _regions.Add(region);
  97. }
  98. /// <summary>
  99. /// Check if this region overlaps with another.
  100. /// </summary>
  101. /// <param name="address">Base address</param>
  102. /// <param name="size">Size of the region</param>
  103. /// <returns>True if overlapping, false otherwise</returns>
  104. public bool OverlapsWith(ulong address, ulong size)
  105. {
  106. return Address < address + size && address < EndAddress;
  107. }
  108. /// <summary>
  109. /// Dispose the handle. Within the tracking lock, this removes references from virtual and physical regions.
  110. /// </summary>
  111. public void Dispose()
  112. {
  113. lock (_tracking.TrackingLock)
  114. {
  115. foreach (VirtualRegion region in _regions)
  116. {
  117. region.RemoveHandle(this);
  118. }
  119. }
  120. }
  121. }
  122. }