RegionHandle.cs 4.7 KB

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