VirtualRegion.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. public VirtualRegion(MemoryTracking tracking, ulong address, ulong size) : base(address, size)
  13. {
  14. _tracking = tracking;
  15. }
  16. public override void Signal(ulong address, ulong size, bool write)
  17. {
  18. foreach (var handle in Handles)
  19. {
  20. handle.Signal(address, size, write);
  21. }
  22. UpdateProtection();
  23. }
  24. /// <summary>
  25. /// Signal that this region has been mapped or unmapped.
  26. /// </summary>
  27. /// <param name="mapped">True if the region has been mapped, false if unmapped</param>
  28. public void SignalMappingChanged(bool mapped)
  29. {
  30. foreach (RegionHandle handle in Handles)
  31. {
  32. handle.SignalMappingChanged(mapped);
  33. }
  34. }
  35. /// <summary>
  36. /// Gets the strictest permission that the child handles demand. Assumes that the tracking lock has been obtained.
  37. /// </summary>
  38. /// <returns>Protection level that this region demands</returns>
  39. public MemoryPermission GetRequiredPermission()
  40. {
  41. // Start with Read/Write, each handle can strip off permissions as necessary.
  42. // Assumes the tracking lock has already been obtained.
  43. MemoryPermission result = MemoryPermission.ReadAndWrite;
  44. foreach (var handle in Handles)
  45. {
  46. result &= handle.RequiredPermission;
  47. if (result == 0) return result;
  48. }
  49. return result;
  50. }
  51. /// <summary>
  52. /// Updates the protection for this virtual region.
  53. /// </summary>
  54. public void UpdateProtection()
  55. {
  56. _tracking.ProtectVirtualRegion(this, GetRequiredPermission());
  57. }
  58. /// <summary>
  59. /// Removes a handle from this virtual region. If there are no handles left, this virtual region is removed.
  60. /// </summary>
  61. /// <param name="handle">Handle to remove</param>
  62. public void RemoveHandle(RegionHandle handle)
  63. {
  64. lock (_tracking.TrackingLock)
  65. {
  66. Handles.Remove(handle);
  67. UpdateProtection();
  68. if (Handles.Count == 0)
  69. {
  70. _tracking.RemoveVirtual(this);
  71. }
  72. }
  73. }
  74. public override INonOverlappingRange Split(ulong splitAddress)
  75. {
  76. VirtualRegion newRegion = new VirtualRegion(_tracking, splitAddress, EndAddress - splitAddress);
  77. Size = splitAddress - Address;
  78. // The new region inherits all of our parents.
  79. newRegion.Handles = new List<RegionHandle>(Handles);
  80. foreach (var parent in Handles)
  81. {
  82. parent.AddChild(newRegion);
  83. }
  84. return newRegion;
  85. }
  86. }
  87. }