VirtualRegion.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 readonly MemoryTracking _tracking;
  12. private MemoryPermission _lastPermission;
  13. public VirtualRegion(MemoryTracking tracking, ulong address, ulong size, MemoryPermission lastPermission = MemoryPermission.Invalid) : base(address, size)
  14. {
  15. _lastPermission = lastPermission;
  16. _tracking = tracking;
  17. }
  18. /// <inheritdoc/>
  19. public override void Signal(ulong address, ulong size, bool write, int? exemptId)
  20. {
  21. IList<RegionHandle> handles = Handles;
  22. for (int i = 0; i < handles.Count; i++)
  23. {
  24. if (exemptId == null || handles[i].Id != exemptId.Value)
  25. {
  26. handles[i].Signal(address, size, write, ref handles);
  27. }
  28. }
  29. UpdateProtection();
  30. }
  31. /// <inheritdoc/>
  32. public override void SignalPrecise(ulong address, ulong size, bool write, int? exemptId)
  33. {
  34. IList<RegionHandle> handles = Handles;
  35. bool allPrecise = true;
  36. for (int i = 0; i < handles.Count; i++)
  37. {
  38. if (exemptId == null || handles[i].Id != exemptId.Value)
  39. {
  40. allPrecise &= handles[i].SignalPrecise(address, size, write, ref handles);
  41. }
  42. }
  43. // Only update protection if a regular signal handler was called.
  44. // This allows precise actions to skip reprotection costs if they want (they can still do it manually).
  45. if (!allPrecise)
  46. {
  47. UpdateProtection();
  48. }
  49. }
  50. /// <summary>
  51. /// Signal that this region has been mapped or unmapped.
  52. /// </summary>
  53. /// <param name="mapped">True if the region has been mapped, false if unmapped</param>
  54. public void SignalMappingChanged(bool mapped)
  55. {
  56. _lastPermission = MemoryPermission.Invalid;
  57. foreach (RegionHandle handle in Handles)
  58. {
  59. handle.SignalMappingChanged(mapped);
  60. }
  61. }
  62. /// <summary>
  63. /// Gets the strictest permission that the child handles demand. Assumes that the tracking lock has been obtained.
  64. /// </summary>
  65. /// <returns>Protection level that this region demands</returns>
  66. public MemoryPermission GetRequiredPermission()
  67. {
  68. // Start with Read/Write, each handle can strip off permissions as necessary.
  69. // Assumes the tracking lock has already been obtained.
  70. MemoryPermission result = MemoryPermission.ReadAndWrite;
  71. foreach (var handle in Handles)
  72. {
  73. result &= handle.RequiredPermission;
  74. if (result == 0) return result;
  75. }
  76. return result;
  77. }
  78. /// <summary>
  79. /// Updates the protection for this virtual region.
  80. /// </summary>
  81. public bool UpdateProtection()
  82. {
  83. MemoryPermission permission = GetRequiredPermission();
  84. if (_lastPermission != permission)
  85. {
  86. _tracking.ProtectVirtualRegion(this, permission);
  87. _lastPermission = permission;
  88. return true;
  89. }
  90. return false;
  91. }
  92. /// <summary>
  93. /// Removes a handle from this virtual region. If there are no handles left, this virtual region is removed.
  94. /// </summary>
  95. /// <param name="handle">Handle to remove</param>
  96. public void RemoveHandle(RegionHandle handle)
  97. {
  98. lock (_tracking.TrackingLock)
  99. {
  100. Handles.Remove(handle);
  101. UpdateProtection();
  102. if (Handles.Count == 0)
  103. {
  104. _tracking.RemoveVirtual(this);
  105. }
  106. }
  107. }
  108. public override INonOverlappingRange Split(ulong splitAddress)
  109. {
  110. VirtualRegion newRegion = new VirtualRegion(_tracking, splitAddress, EndAddress - splitAddress, _lastPermission);
  111. Size = splitAddress - Address;
  112. // The new region inherits all of our parents.
  113. newRegion.Handles = new List<RegionHandle>(Handles);
  114. foreach (var parent in Handles)
  115. {
  116. parent.AddChild(newRegion);
  117. }
  118. return newRegion;
  119. }
  120. }
  121. }