aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Kernel/Process/KTlsPageInfo.cs
blob: f55e3c10eb2a1d5e42bc1b3b27b98065e607540f (plain) (blame)
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
using Ryujinx.HLE.HOS.Kernel.Memory;

namespace Ryujinx.HLE.HOS.Kernel.Process
{
    class KTlsPageInfo
    {
        public const int TlsEntrySize = 0x200;

        public ulong PageVirtualAddress { get; }
        public ulong PagePhysicalAddress { get; }

        private readonly bool[] _isSlotFree;

        public KTlsPageInfo(ulong pageVirtualAddress, ulong pagePhysicalAddress)
        {
            PageVirtualAddress = pageVirtualAddress;
            PagePhysicalAddress = pagePhysicalAddress;

            _isSlotFree = new bool[KPageTableBase.PageSize / TlsEntrySize];

            for (int index = 0; index < _isSlotFree.Length; index++)
            {
                _isSlotFree[index] = true;
            }
        }

        public bool TryGetFreePage(out ulong address)
        {
            address = PageVirtualAddress;

            for (int index = 0; index < _isSlotFree.Length; index++)
            {
                if (_isSlotFree[index])
                {
                    _isSlotFree[index] = false;

                    return true;
                }

                address += TlsEntrySize;
            }

            address = 0;

            return false;
        }

        public bool IsFull()
        {
            bool hasFree = false;

            for (int index = 0; index < _isSlotFree.Length; index++)
            {
                hasFree |= _isSlotFree[index];
            }

            return !hasFree;
        }

        public bool IsEmpty()
        {
            bool allFree = true;

            for (int index = 0; index < _isSlotFree.Length; index++)
            {
                allFree &= _isSlotFree[index];
            }

            return allFree;
        }

        public void FreeTlsSlot(ulong address)
        {
            _isSlotFree[(address - PageVirtualAddress) / TlsEntrySize] = true;
        }
    }
}