aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryRegionManager.cs
blob: 4596b15d5fcdef8c0a839e58e9d5cfc031659cef (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using Ryujinx.Horizon.Common;
using System.Diagnostics;

namespace Ryujinx.HLE.HOS.Kernel.Memory
{
    class KMemoryRegionManager
    {
        private readonly KPageHeap _pageHeap;

        public ulong Address { get; }
        public ulong Size { get; }
        public ulong EndAddr => Address + Size;

        private readonly ushort[] _pageReferenceCounts;

        public KMemoryRegionManager(ulong address, ulong size, ulong endAddr)
        {
            Address = address;
            Size = size;

            _pageReferenceCounts = new ushort[size / KPageTableBase.PageSize];

            _pageHeap = new KPageHeap(address, size);
            _pageHeap.Free(address, size / KPageTableBase.PageSize);
            _pageHeap.UpdateUsedSize();
        }

        public Result AllocatePages(out KPageList pageList, ulong pagesCount)
        {
            if (pagesCount == 0)
            {
                pageList = new KPageList();

                return Result.Success;
            }

            lock (_pageHeap)
            {
                Result result = AllocatePagesImpl(out pageList, pagesCount, false);

                if (result == Result.Success)
                {
                    foreach (var node in pageList)
                    {
                        IncrementPagesReferenceCount(node.Address, node.PagesCount);
                    }
                }

                return result;
            }
        }

        public ulong AllocatePagesContiguous(KernelContext context, ulong pagesCount, bool backwards)
        {
            if (pagesCount == 0)
            {
                return 0;
            }

            lock (_pageHeap)
            {
                ulong address = AllocatePagesContiguousImpl(pagesCount, 1, backwards);

                if (address != 0)
                {
                    IncrementPagesReferenceCount(address, pagesCount);
                    context.CommitMemory(address - DramMemoryMap.DramBase, pagesCount * KPageTableBase.PageSize);
                }

                return address;
            }
        }

        private Result AllocatePagesImpl(out KPageList pageList, ulong pagesCount, bool random)
        {
            pageList = new KPageList();

            int heapIndex = KPageHeap.GetBlockIndex(pagesCount);

            if (heapIndex < 0)
            {
                return KernelResult.OutOfMemory;
            }

            for (int index = heapIndex; index >= 0; index--)
            {
                ulong pagesPerAlloc = KPageHeap.GetBlockPagesCount(index);

                while (pagesCount >= pagesPerAlloc)
                {
                    ulong allocatedBlock = _pageHeap.AllocateBlock(index, random);

                    if (allocatedBlock == 0)
                    {
                        break;
                    }

                    Result result = pageList.AddRange(allocatedBlock, pagesPerAlloc);

                    if (result != Result.Success)
                    {
                        FreePages(pageList);
                        _pageHeap.Free(allocatedBlock, pagesPerAlloc);

                        return result;
                    }

                    pagesCount -= pagesPerAlloc;
                }
            }

            if (pagesCount != 0)
            {
                FreePages(pageList);

                return KernelResult.OutOfMemory;
            }

            return Result.Success;
        }

        private ulong AllocatePagesContiguousImpl(ulong pagesCount, ulong alignPages, bool random)
        {
            int heapIndex = KPageHeap.GetAlignedBlockIndex(pagesCount, alignPages);

            ulong allocatedBlock = _pageHeap.AllocateBlock(heapIndex, random);

            if (allocatedBlock == 0)
            {
                return 0;
            }

            ulong allocatedPages = KPageHeap.GetBlockPagesCount(heapIndex);

            if (allocatedPages > pagesCount)
            {
                _pageHeap.Free(allocatedBlock + pagesCount * KPageTableBase.PageSize, allocatedPages - pagesCount);
            }

            return allocatedBlock;
        }

        public void FreePage(ulong address)
        {
            lock (_pageHeap)
            {
                _pageHeap.Free(address, 1);
            }
        }

        public void FreePages(KPageList pageList)
        {
            lock (_pageHeap)
            {
                foreach (KPageNode pageNode in pageList)
                {
                    _pageHeap.Free(pageNode.Address, pageNode.PagesCount);
                }
            }
        }

        public void FreePages(ulong address, ulong pagesCount)
        {
            lock (_pageHeap)
            {
                _pageHeap.Free(address, pagesCount);
            }
        }

        public ulong GetFreePages()
        {
            lock (_pageHeap)
            {
                return _pageHeap.GetFreePagesCount();
            }
        }

        public void IncrementPagesReferenceCount(ulong address, ulong pagesCount)
        {
            ulong index = GetPageOffset(address);
            ulong endIndex = index + pagesCount;

            while (index < endIndex)
            {
                ushort referenceCount = ++_pageReferenceCounts[index];
                Debug.Assert(referenceCount >= 1);

                index++;
            }
        }

        public void DecrementPagesReferenceCount(ulong address, ulong pagesCount)
        {
            ulong index = GetPageOffset(address);
            ulong endIndex = index + pagesCount;

            ulong freeBaseIndex = 0;
            ulong freePagesCount = 0;

            while (index < endIndex)
            {
                Debug.Assert(_pageReferenceCounts[index] > 0);
                ushort referenceCount = --_pageReferenceCounts[index];

                if (referenceCount == 0)
                {
                    if (freePagesCount != 0)
                    {
                        freePagesCount++;
                    }
                    else
                    {
                        freeBaseIndex = index;
                        freePagesCount = 1;
                    }
                }
                else if (freePagesCount != 0)
                {
                    FreePages(Address + freeBaseIndex * KPageTableBase.PageSize, freePagesCount);
                    freePagesCount = 0;
                }

                index++;
            }

            if (freePagesCount != 0)
            {
                FreePages(Address + freeBaseIndex * KPageTableBase.PageSize, freePagesCount);
            }
        }

        public ulong GetPageOffset(ulong address)
        {
            return (address - Address) / KPageTableBase.PageSize;
        }

        public ulong GetPageOffsetFromEnd(ulong address)
        {
            return (EndAddr - address) / KPageTableBase.PageSize;
        }
    }
}