using Ryujinx.Memory.Range;
using System.Collections.Generic;
namespace Ryujinx.Memory.Tracking
{
///
/// A region of virtual memory.
///
class VirtualRegion : AbstractRegion
{
public List Handles = new();
private readonly MemoryTracking _tracking;
private MemoryPermission _lastPermission;
public bool Guest { get; }
public VirtualRegion(MemoryTracking tracking, ulong address, ulong size, bool guest, MemoryPermission lastPermission = MemoryPermission.Invalid) : base(address, size)
{
_lastPermission = lastPermission;
_tracking = tracking;
Guest = guest;
}
///
public override void Signal(ulong address, ulong size, bool write, int? exemptId)
{
IList handles = Handles;
for (int i = 0; i < handles.Count; i++)
{
if (exemptId == null || handles[i].Id != exemptId.Value)
{
handles[i].Signal(address, size, write, ref handles);
}
}
UpdateProtection();
}
///
public override void SignalPrecise(ulong address, ulong size, bool write, int? exemptId)
{
IList handles = Handles;
bool allPrecise = true;
for (int i = 0; i < handles.Count; i++)
{
if (exemptId == null || handles[i].Id != exemptId.Value)
{
allPrecise &= handles[i].SignalPrecise(address, size, write, ref handles);
}
}
// Only update protection if a regular signal handler was called.
// This allows precise actions to skip reprotection costs if they want (they can still do it manually).
if (!allPrecise)
{
UpdateProtection();
}
}
///
/// Signal that this region has been mapped or unmapped.
///
/// True if the region has been mapped, false if unmapped
public void SignalMappingChanged(bool mapped)
{
_lastPermission = MemoryPermission.Invalid;
if (!Guest)
{
foreach (RegionHandle handle in Handles)
{
handle.SignalMappingChanged(mapped);
}
}
}
///
/// Gets the strictest permission that the child handles demand. Assumes that the tracking lock has been obtained.
///
/// Protection level that this region demands
public MemoryPermission GetRequiredPermission()
{
// Start with Read/Write, each handle can strip off permissions as necessary.
// Assumes the tracking lock has already been obtained.
MemoryPermission result = MemoryPermission.ReadAndWrite;
foreach (var handle in Handles)
{
result &= handle.RequiredPermission;
if (result == 0)
{
return result;
}
}
return result;
}
///
/// Updates the protection for this virtual region.
///
public bool UpdateProtection()
{
MemoryPermission permission = GetRequiredPermission();
if (_lastPermission != permission)
{
_tracking.ProtectVirtualRegion(this, permission, Guest);
_lastPermission = permission;
return true;
}
return false;
}
///
/// Removes a handle from this virtual region. If there are no handles left, this virtual region is removed.
///
/// Handle to remove
public void RemoveHandle(RegionHandle handle)
{
lock (_tracking.TrackingLock)
{
Handles.Remove(handle);
UpdateProtection();
if (Handles.Count == 0)
{
_tracking.RemoveVirtual(this);
}
}
}
public override INonOverlappingRange Split(ulong splitAddress)
{
VirtualRegion newRegion = new(_tracking, splitAddress, EndAddress - splitAddress, Guest, _lastPermission);
Size = splitAddress - Address;
// The new region inherits all of our parents.
newRegion.Handles = new List(Handles);
foreach (var parent in Handles)
{
parent.AddChild(newRegion);
}
return newRegion;
}
}
}