aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Memory/Tracking/PhysicalRegion.cs
blob: 683186b16f835562484e843f825c3a04501cd15d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Ryujinx.Memory.Range;
using System.Collections.Generic;

namespace Ryujinx.Memory.Tracking
{
    /// <summary>
    /// A region of physical memory.
    /// </summary>
    class PhysicalRegion : AbstractRegion
    {
        public List<VirtualRegion> VirtualParents = new List<VirtualRegion>();
        public MemoryPermission Protection { get; private set; }
        public MemoryTracking Tracking;

        public PhysicalRegion(MemoryTracking tracking, ulong address, ulong size) : base(address, size)
        {
            Tracking = tracking;
            Protection = MemoryPermission.ReadAndWrite;
        }

        public override void Signal(ulong address, ulong size, bool write)
        {
            Protection = MemoryPermission.ReadAndWrite;
            Tracking.ProtectPhysicalRegion(this, MemoryPermission.ReadAndWrite); // Remove our protection immedately.
            foreach (var parent in VirtualParents)
            {
                parent.Signal(address, size, write);
            }
        }

        /// <summary>
        /// Update the protection of this region, based on our parent's requested protection.
        /// </summary>
        public void UpdateProtection()
        {
            // Re-evaluate protection, and commit to the block.

            lock (Tracking.TrackingLock)
            {
                MemoryPermission result = MemoryPermission.ReadAndWrite;
                foreach (var parent in VirtualParents)
                {
                    result &= parent.GetRequiredPermission();
                    if (result == 0) break;
                }

                if (Protection != result)
                {
                    Protection = result;
                    Tracking.ProtectPhysicalRegion(this, result);
                }
            }
        }

        public override INonOverlappingRange Split(ulong splitAddress)
        {
            PhysicalRegion newRegion = new PhysicalRegion(Tracking, splitAddress, EndAddress - splitAddress);
            Size = splitAddress - Address;

            // The new region inherits all of our parents.
            newRegion.VirtualParents = new List<VirtualRegion>(VirtualParents);
            foreach (var parent in VirtualParents)
            {
                parent.AddChild(newRegion);
            }

            return newRegion;
        }

        /// <summary>
        /// Remove a parent virtual region from this physical region. Assumes that the tracking lock has been obtained.
        /// </summary>
        /// <param name="region">Region to remove</param>
        /// <returns>True if there are no more parents and we should be removed, false otherwise.</returns>
        public bool RemoveParent(VirtualRegion region)
        {
            VirtualParents.Remove(region);
            UpdateProtection();
            if (VirtualParents.Count == 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Deletes this physical region if there are no more virtual parents.
        /// </summary>
        public void TryDelete()
        {
            if (VirtualParents.Count == 0)
            {
                Tracking.RemovePhysical(this);
            }
        }
    }
}