-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathPageAllocator.cpp
231 lines (183 loc) · 6.9 KB
/
PageAllocator.cpp
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
//--------------------------------------------------------------------------------------
// PageAllocator.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "PageAllocator.h"
namespace ATG
{
//--------------------------------------------------
// PageRange
bool Range::Precedes(const Range& subsequent) const
{
assert(Disjoint(*this, subsequent)); // No page range should intersect each other.
return start + count <= subsequent.start;
}
bool Range::Succeeds(const Range& precedent) const
{
// Equivalent to a reversed 'Precedes' test.
return precedent.Precedes(*this);
}
bool Range::MergeLeft(Range& left, const Range& right)
{
assert(Disjoint(left, right));
if (left.start + left.count == right.start)
{
left.count += right.count;
return true;
}
return false;
}
bool Range::MergeRight(const Range& left, Range& right)
{
if (left.start + left.count == right.start)
{
right.start = left.start;
right.count += left.count;
return true;
}
return false;
}
bool Range::Disjoint(const Range& r0, const Range& r1)
{
// Ensure this range is either entirely on the left or the right side.
return (r0.start + r0.count) <= r1.start || r0.start >= (r1.start + r1.count);
}
bool Range::Adjacent(const Range& left, const Range& right)
{
assert(Disjoint(left, right));
return (left.start + left.count) == right.start;
}
//--------------------------------------------------
// PageBlock
bool PageBlock::AllocateRange(int count, Range& outRange)
{
if (IsExhausted())
{
return false;
}
auto& range = ranges[0];
// Allocate pages from the current range.
int allocStart = range.start;
int allocCount = std::min(count, range.count);
// Perform the allocation.
count -= allocCount;
range.start += allocCount;
range.count -= allocCount;
// Erase the range if it just became exhausted.
if (range.count == 0)
{
ranges.erase(ranges.begin());
}
outRange.start = allocStart;
outRange.count = allocCount;
return true;
}
void PageBlock::FreeRange(const Range& range)
{
// Find the correct location for the released range within the block.
int i = 0;
while (i < ranges.size() && ranges[i].Precedes(range))
{
++i;
}
// Determine whether to merge with existing block, or create a new range.
bool merged = false;
if (i != 0)
{
// Try merging with the range to the left.
merged = Range::MergeLeft(ranges[i - 1], range);
}
if (i != ranges.size())
{
// Try merging with the range to the right.
merged = merged || Range::MergeRight(range, ranges[i]);
}
if (!merged)
{
// No merge occurred, insert a new range.
ranges.insert(ranges.begin() + i, range);
}
}
void PageBlock::Reset()
{
ranges.resize(1);
ranges[0] = { 0, pagePoolCount };
}
//------------------------------------
// EsramMappingPolicy
void EsramMappingPolicy::InitBlock(ID3D12Device* device, void* address, int index, PageBlock& block)
{
UNREFERENCED_PARAMETER(index);
// Map the virtual range to the entirety of ESRAM.
UINT pages[s_blockPageCount];
for (int i = 0; i < s_blockPageCount; ++i)
{
pages[i] = i;
}
DX::ThrowIfFailed(D3DMapEsramMemory(D3D11_MAP_ESRAM_LARGE_PAGES, address, s_blockPageCount, pages));
// Create the new page pool for the block.
HANDLE handle = {};
D3D12_GPU_VIRTUAL_ADDRESS gpuAddress = reinterpret_cast<D3D12_GPU_VIRTUAL_ADDRESS>(address);
DX::ThrowIfFailed(device->RegisterPagePoolX(gpuAddress, s_blockPageCount, &handle));
block.pagePoolHandle = handle;
block.Reset();
}
void EsramMappingPolicy::DeinitBlock(ID3D12Device* device, void* address, int index, PageBlock& block)
{
UNREFERENCED_PARAMETER(index);
// Release the page pool.
device->UnregisterPagePoolX(block.pagePoolHandle);
block.pagePoolHandle = NULL;
block.ranges.clear();
DX::ThrowIfFailed(D3DUnmapEsramMemory(D3D11_MAP_ESRAM_LARGE_PAGES, address, s_blockPageCount));
}
//------------------------------------
// DramMappingPolicy
bool DramMappingPolicy::CanExpand(int newBlockCount)
{
return s_blockSizeBytes * newBlockCount <= m_maxSizeBytes;
}
void DramMappingPolicy::InitBlock(ID3D12Device* device, void* address, int index, PageBlock& block)
{
// Allocate some memory to maintain list of allocated physical pages - needed to release the pages.
int start = index * s_blockPageCount;
int end = start + s_blockPageCount;
if (end > m_pageCache.size())
{
m_pageCache.resize(end);
}
// Allocate physical memory
ULONG_PTR count = c_dramBlockPageCount;
PULONG_PTR pagePtr = m_pageCache.data() + start;
BOOL success = AllocateTitlePhysicalPages(GetCurrentProcess(), MEM_LARGE_PAGES | MEM_GRAPHICS, &count, pagePtr);
if (!success)
{
throw std::exception("AllocateTitlePhysicalPages failed to allocate physical pages!");
}
// Map the block's virtual address space to the physical system pages.
void* blockAddress = MapTitlePhysicalPages(address, count, MEM_LARGE_PAGES | MEM_GRAPHICS, PAGE_READWRITE | PAGE_WRITECOMBINE, pagePtr);
if (blockAddress == nullptr)
{
throw std::exception("MapTitlePhysicalPages failed to map physical pages!");
}
// Create the new page pool for the block.
HANDLE handle = {};
DX::ThrowIfFailed(device->RegisterPagePoolX(reinterpret_cast<D3D12_GPU_VIRTUAL_ADDRESS>(blockAddress), c_dramBlockPageCount, &handle));
block.pagePoolHandle = handle;
block.Reset();
}
void DramMappingPolicy::DeinitBlock(ID3D12Device* device, void* address, int index, PageBlock& block)
{
UNREFERENCED_PARAMETER(index);
// Release the page pool.
device->UnregisterPagePoolX(block.pagePoolHandle);
// Decommit the memory.
VirtualFree(address, s_blockSizeBytes, MEM_DECOMMIT);
// Free the physical pages back to the OS.
PULONG_PTR pagePtr = m_pageCache.data() + (m_pageCache.size() - s_blockPageCount);
FreeTitlePhysicalPages(GetModuleHandle(NULL), s_blockPageCount, pagePtr);
}
}