yuzu/src/common/address_space.inc

339 lines
15 KiB
PHP
Raw Normal View History

2022-07-01 05:13:52 +04:00
// SPDX-License-Identifier: GPLv3 or later
// Copyright © 2021 Skyline Team and Contributors (https://github.com/skyline-emu/)
2022-06-16 05:46:18 +04:00
#include "common/address_space.h"
#include "common/assert.h"
#define MAP_MEMBER(returnType) \
template <typename VaType, VaType UnmappedVa, typename PaType, PaType UnmappedPa, \
bool PaContigSplit, size_t AddressSpaceBits, typename ExtraBlockInfo> \
requires AddressSpaceValid<VaType, AddressSpaceBits> returnType FlatAddressSpaceMap< \
VaType, UnmappedVa, PaType, UnmappedPa, PaContigSplit, AddressSpaceBits, ExtraBlockInfo>
#define MAP_MEMBER_CONST() \
template <typename VaType, VaType UnmappedVa, typename PaType, PaType UnmappedPa, \
bool PaContigSplit, size_t AddressSpaceBits, typename ExtraBlockInfo> \
requires AddressSpaceValid<VaType, AddressSpaceBits> FlatAddressSpaceMap< \
VaType, UnmappedVa, PaType, UnmappedPa, PaContigSplit, AddressSpaceBits, ExtraBlockInfo>
#define MM_MEMBER(returnType) \
template <typename VaType, VaType UnmappedVa, size_t AddressSpaceBits> \
requires AddressSpaceValid<VaType, AddressSpaceBits> returnType \
FlatMemoryManager<VaType, UnmappedVa, AddressSpaceBits>
#define ALLOC_MEMBER(returnType) \
template <typename VaType, VaType UnmappedVa, size_t AddressSpaceBits> \
requires AddressSpaceValid<VaType, AddressSpaceBits> returnType \
FlatAllocator<VaType, UnmappedVa, AddressSpaceBits>
#define ALLOC_MEMBER_CONST() \
template <typename VaType, VaType UnmappedVa, size_t AddressSpaceBits> \
requires AddressSpaceValid<VaType, AddressSpaceBits> \
FlatAllocator<VaType, UnmappedVa, AddressSpaceBits>
namespace Common {
2022-07-01 05:13:52 +04:00
MAP_MEMBER_CONST()::FlatAddressSpaceMap(VaType vaLimit_,
std::function<void(VaType, VaType)> unmapCallback_)
: unmapCallback(std::move(unmapCallback_)), vaLimit(vaLimit_) {
if (vaLimit > VaMaximum)
2022-06-16 05:46:18 +04:00
UNREACHABLE_MSG("Invalid VA limit!");
}
2022-07-01 05:13:52 +04:00
MAP_MEMBER(void)::MapLocked(VaType virt, PaType phys, VaType size, ExtraBlockInfo extraInfo) {
VaType virtEnd{virt + size};
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
if (virtEnd > vaLimit)
UNREACHABLE_MSG("Trying to map a block past the VA limit: virtEnd: 0x{:X}, vaLimit: 0x{:X}",
virtEnd, vaLimit);
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
auto blockEndSuccessor{std::lower_bound(blocks.begin(), blocks.end(), virtEnd)};
if (blockEndSuccessor == blocks.begin())
UNREACHABLE_MSG("Trying to map a block before the VA start: virtEnd: 0x{:X}", virtEnd);
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
auto blockEndPredecessor{std::prev(blockEndSuccessor)};
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
if (blockEndSuccessor != blocks.end()) {
2022-06-16 05:46:18 +04:00
// We have blocks in front of us, if one is directly in front then we don't have to add a
// tail
2022-07-01 05:13:52 +04:00
if (blockEndSuccessor->virt != virtEnd) {
2022-06-16 05:46:18 +04:00
PaType tailPhys{[&]() -> PaType {
if constexpr (!PaContigSplit) {
2022-07-01 05:13:52 +04:00
return blockEndPredecessor
->phys; // Always propagate unmapped regions rather than calculating offset
2022-06-16 05:46:18 +04:00
} else {
2022-07-01 05:13:52 +04:00
if (blockEndPredecessor->Unmapped())
return blockEndPredecessor->phys; // Always propagate unmapped regions
// rather than calculating offset
else
return blockEndPredecessor->phys + virtEnd - blockEndPredecessor->virt;
2022-06-16 05:46:18 +04:00
}
}()};
2022-07-01 05:13:52 +04:00
if (blockEndPredecessor->virt >= virt) {
2022-06-16 05:46:18 +04:00
// If this block's start would be overlapped by the map then reuse it as a tail
// block
2022-07-01 05:13:52 +04:00
blockEndPredecessor->virt = virtEnd;
blockEndPredecessor->phys = tailPhys;
blockEndPredecessor->extraInfo = blockEndPredecessor->extraInfo;
2022-06-16 05:46:18 +04:00
// No longer predecessor anymore
2022-07-01 05:13:52 +04:00
blockEndSuccessor = blockEndPredecessor--;
2022-06-16 05:46:18 +04:00
} else {
// Else insert a new one and we're done
2022-07-01 05:13:52 +04:00
blocks.insert(blockEndSuccessor,
{Block(virt, phys, extraInfo),
Block(virtEnd, tailPhys, blockEndPredecessor->extraInfo)});
if (unmapCallback)
unmapCallback(virt, size);
2022-06-16 05:46:18 +04:00
return;
}
}
} else {
2022-07-01 05:13:52 +04:00
// blockEndPredecessor will always be unmapped as blocks has to be terminated by an unmapped
// chunk
if (blockEndPredecessor != blocks.begin() && blockEndPredecessor->virt >= virt) {
2022-06-16 05:46:18 +04:00
// Move the unmapped block start backwards
2022-07-01 05:13:52 +04:00
blockEndPredecessor->virt = virtEnd;
2022-06-16 05:46:18 +04:00
// No longer predecessor anymore
2022-07-01 05:13:52 +04:00
blockEndSuccessor = blockEndPredecessor--;
2022-06-16 05:46:18 +04:00
} else {
// Else insert a new one and we're done
2022-07-01 05:13:52 +04:00
blocks.insert(blockEndSuccessor,
{Block(virt, phys, extraInfo), Block(virtEnd, UnmappedPa, {})});
if (unmapCallback)
unmapCallback(virt, size);
2022-06-16 05:46:18 +04:00
return;
}
}
2022-07-01 05:13:52 +04:00
auto blockStartSuccessor{blockEndSuccessor};
2022-06-16 05:46:18 +04:00
// Walk the block vector to find the start successor as this is more efficient than another
// binary search in most scenarios
2022-07-01 05:13:52 +04:00
while (std::prev(blockStartSuccessor)->virt >= virt)
blockStartSuccessor--;
2022-06-16 05:46:18 +04:00
// Check that the start successor is either the end block or something in between
2022-07-01 05:13:52 +04:00
if (blockStartSuccessor->virt > virtEnd) {
UNREACHABLE_MSG("Unsorted block in AS map: virt: 0x{:X}", blockStartSuccessor->virt);
} else if (blockStartSuccessor->virt == virtEnd) {
2022-06-16 05:46:18 +04:00
// We need to create a new block as there are none spare that we would overwrite
2022-07-01 05:13:52 +04:00
blocks.insert(blockStartSuccessor, Block(virt, phys, extraInfo));
2022-06-16 05:46:18 +04:00
} else {
// Erase overwritten blocks
2022-07-01 05:13:52 +04:00
if (auto eraseStart{std::next(blockStartSuccessor)}; eraseStart != blockEndSuccessor)
blocks.erase(eraseStart, blockEndSuccessor);
2022-06-16 05:46:18 +04:00
// Reuse a block that would otherwise be overwritten as a start block
2022-07-01 05:13:52 +04:00
blockStartSuccessor->virt = virt;
blockStartSuccessor->phys = phys;
blockStartSuccessor->extraInfo = extraInfo;
2022-06-16 05:46:18 +04:00
}
2022-07-01 05:13:52 +04:00
if (unmapCallback)
unmapCallback(virt, size);
2022-06-16 05:46:18 +04:00
}
MAP_MEMBER(void)::UnmapLocked(VaType virt, VaType size) {
2022-07-01 05:13:52 +04:00
VaType virtEnd{virt + size};
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
if (virtEnd > vaLimit)
UNREACHABLE_MSG("Trying to map a block past the VA limit: virtEnd: 0x{:X}, vaLimit: 0x{:X}",
virtEnd, vaLimit);
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
auto blockEndSuccessor{std::lower_bound(blocks.begin(), blocks.end(), virtEnd)};
if (blockEndSuccessor == blocks.begin())
UNREACHABLE_MSG("Trying to unmap a block before the VA start: virtEnd: 0x{:X}", virtEnd);
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
auto blockEndPredecessor{std::prev(blockEndSuccessor)};
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
auto walkBackToPredecessor{[&](auto iter) {
while (iter->virt >= virt)
2022-06-16 05:46:18 +04:00
iter--;
return iter;
}};
2022-07-01 05:13:52 +04:00
auto eraseBlocksWithEndUnmapped{[&](auto unmappedEnd) {
auto blockStartPredecessor{walkBackToPredecessor(unmappedEnd)};
auto blockStartSuccessor{std::next(blockStartPredecessor)};
2022-06-16 05:46:18 +04:00
auto eraseEnd{[&]() {
2022-07-01 05:13:52 +04:00
if (blockStartPredecessor->Unmapped()) {
2022-06-16 05:46:18 +04:00
// If the start predecessor is unmapped then we can erase everything in our region
// and be done
return std::next(unmappedEnd);
} else {
// Else reuse the end predecessor as the start of our unmapped region then erase all
// up to it
unmappedEnd->virt = virt;
return unmappedEnd;
}
}()};
// We can't have two unmapped regions after each other
if (eraseEnd != blocks.end() &&
2022-07-01 05:13:52 +04:00
(eraseEnd == blockStartSuccessor ||
(blockStartPredecessor->Unmapped() && eraseEnd->Unmapped())))
2022-06-16 05:46:18 +04:00
UNREACHABLE_MSG("Multiple contiguous unmapped regions are unsupported!");
2022-07-01 05:13:52 +04:00
blocks.erase(blockStartSuccessor, eraseEnd);
2022-06-16 05:46:18 +04:00
}};
// We can avoid any splitting logic if these are the case
2022-07-01 05:13:52 +04:00
if (blockEndPredecessor->Unmapped()) {
if (blockEndPredecessor->virt > virt)
eraseBlocksWithEndUnmapped(blockEndPredecessor);
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
if (unmapCallback)
unmapCallback(virt, size);
2022-06-16 05:46:18 +04:00
return; // The region is unmapped, bail out early
2022-07-01 05:13:52 +04:00
} else if (blockEndSuccessor->virt == virtEnd && blockEndSuccessor->Unmapped()) {
eraseBlocksWithEndUnmapped(blockEndSuccessor);
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
if (unmapCallback)
unmapCallback(virt, size);
2022-06-16 05:46:18 +04:00
return; // The region is unmapped here and doesn't need splitting, bail out early
2022-07-01 05:13:52 +04:00
} else if (blockEndSuccessor == blocks.end()) {
2022-06-16 05:46:18 +04:00
// This should never happen as the end should always follow an unmapped block
UNREACHABLE_MSG("Unexpected Memory Manager state!");
2022-07-01 05:13:52 +04:00
} else if (blockEndSuccessor->virt != virtEnd) {
2022-06-16 05:46:18 +04:00
// If one block is directly in front then we don't have to add a tail
// The previous block is mapped so we will need to add a tail with an offset
PaType tailPhys{[&]() {
2022-07-01 05:13:52 +04:00
if constexpr (PaContigSplit)
return blockEndPredecessor->phys + virtEnd - blockEndPredecessor->virt;
else
return blockEndPredecessor->phys;
2022-06-16 05:46:18 +04:00
}()};
2022-07-01 05:13:52 +04:00
if (blockEndPredecessor->virt >= virt) {
2022-06-16 05:46:18 +04:00
// If this block's start would be overlapped by the unmap then reuse it as a tail block
2022-07-01 05:13:52 +04:00
blockEndPredecessor->virt = virtEnd;
blockEndPredecessor->phys = tailPhys;
2022-06-16 05:46:18 +04:00
// No longer predecessor anymore
2022-07-01 05:13:52 +04:00
blockEndSuccessor = blockEndPredecessor--;
2022-06-16 05:46:18 +04:00
} else {
2022-07-01 05:13:52 +04:00
blocks.insert(blockEndSuccessor,
2022-06-16 05:46:18 +04:00
{Block(virt, UnmappedPa, {}),
2022-07-01 05:13:52 +04:00
Block(virtEnd, tailPhys, blockEndPredecessor->extraInfo)});
if (unmapCallback)
unmapCallback(virt, size);
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
return; // The previous block is mapped and ends before
2022-06-16 05:46:18 +04:00
}
}
// Walk the block vector to find the start predecessor as this is more efficient than another
// binary search in most scenarios
2022-07-01 05:13:52 +04:00
auto blockStartPredecessor{walkBackToPredecessor(blockEndSuccessor)};
auto blockStartSuccessor{std::next(blockStartPredecessor)};
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
if (blockStartSuccessor->virt > virtEnd) {
UNREACHABLE_MSG("Unsorted block in AS map: virt: 0x{:X}", blockStartSuccessor->virt);
} else if (blockStartSuccessor->virt == virtEnd) {
2022-06-16 05:46:18 +04:00
// There are no blocks between the start and the end that would let us skip inserting a new
// one for head
// The previous block is may be unmapped, if so we don't need to insert any unmaps after it
2022-07-01 05:13:52 +04:00
if (blockStartPredecessor->Mapped())
blocks.insert(blockStartSuccessor, Block(virt, UnmappedPa, {}));
} else if (blockStartPredecessor->Unmapped()) {
2022-06-16 05:46:18 +04:00
// If the previous block is unmapped
2022-07-01 05:13:52 +04:00
blocks.erase(blockStartSuccessor, blockEndPredecessor);
2022-06-16 05:46:18 +04:00
} else {
// Erase overwritten blocks, skipping the first one as we have written the unmapped start
// block there
2022-07-01 05:13:52 +04:00
if (auto eraseStart{std::next(blockStartSuccessor)}; eraseStart != blockEndSuccessor)
blocks.erase(eraseStart, blockEndSuccessor);
2022-06-16 05:46:18 +04:00
// Add in the unmapped block header
2022-07-01 05:13:52 +04:00
blockStartSuccessor->virt = virt;
blockStartSuccessor->phys = UnmappedPa;
2022-06-16 05:46:18 +04:00
}
2022-07-01 05:13:52 +04:00
if (unmapCallback)
unmapCallback(virt, size);
2022-06-16 05:46:18 +04:00
}
2022-07-01 05:13:52 +04:00
ALLOC_MEMBER_CONST()::FlatAllocator(VaType vaStart_, VaType vaLimit_)
: Base(vaLimit_), currentLinearAllocEnd(vaStart_), vaStart(vaStart_) {}
2022-06-16 05:46:18 +04:00
ALLOC_MEMBER(VaType)::Allocate(VaType size) {
2022-07-01 05:13:52 +04:00
std::scoped_lock lock(this->blockMutex);
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
VaType allocStart{UnmappedVa};
VaType allocEnd{currentLinearAllocEnd + size};
2022-06-16 05:46:18 +04:00
// Avoid searching backwards in the address space if possible
2022-07-01 05:13:52 +04:00
if (allocEnd >= currentLinearAllocEnd && allocEnd <= this->vaLimit) {
auto allocEndSuccessor{
std::lower_bound(this->blocks.begin(), this->blocks.end(), allocEnd)};
if (allocEndSuccessor == this->blocks.begin())
2022-06-16 05:46:18 +04:00
UNREACHABLE_MSG("First block in AS map is invalid!");
2022-07-01 05:13:52 +04:00
auto allocEndPredecessor{std::prev(allocEndSuccessor)};
if (allocEndPredecessor->virt <= currentLinearAllocEnd) {
allocStart = currentLinearAllocEnd;
2022-06-16 05:46:18 +04:00
} else {
// Skip over fixed any mappings in front of us
2022-07-01 05:13:52 +04:00
while (allocEndSuccessor != this->blocks.end()) {
if (allocEndSuccessor->virt - allocEndPredecessor->virt < size ||
allocEndPredecessor->Mapped()) {
allocStart = allocEndPredecessor->virt;
2022-06-16 05:46:18 +04:00
break;
}
2022-07-01 05:13:52 +04:00
allocEndPredecessor = allocEndSuccessor++;
2022-06-16 05:46:18 +04:00
// Use the VA limit to calculate if we can fit in the final block since it has no
// successor
2022-07-01 05:13:52 +04:00
if (allocEndSuccessor == this->blocks.end()) {
allocEnd = allocEndPredecessor->virt + size;
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
if (allocEnd >= allocEndPredecessor->virt && allocEnd <= this->vaLimit)
allocStart = allocEndPredecessor->virt;
2022-06-16 05:46:18 +04:00
}
}
}
}
2022-07-01 05:13:52 +04:00
if (allocStart != UnmappedVa) {
currentLinearAllocEnd = allocStart + size;
2022-06-16 05:46:18 +04:00
} else { // If linear allocation overflows the AS then find a gap
2022-07-01 05:13:52 +04:00
if (this->blocks.size() <= 2)
2022-06-16 05:46:18 +04:00
UNREACHABLE_MSG("Unexpected allocator state!");
2022-07-01 05:13:52 +04:00
auto searchPredecessor{this->blocks.begin()};
auto searchSuccessor{std::next(searchPredecessor)};
2022-06-16 05:46:18 +04:00
2022-07-01 05:13:52 +04:00
while (searchSuccessor != this->blocks.end() &&
(searchSuccessor->virt - searchPredecessor->virt < size ||
searchPredecessor->Mapped())) {
searchPredecessor = searchSuccessor++;
2022-06-16 05:46:18 +04:00
}
2022-07-01 05:13:52 +04:00
if (searchSuccessor != this->blocks.end())
allocStart = searchPredecessor->virt;
else
2022-06-16 05:46:18 +04:00
return {}; // AS is full
}
2022-07-01 05:13:52 +04:00
this->MapLocked(allocStart, true, size, {});
return allocStart;
2022-06-16 05:46:18 +04:00
}
ALLOC_MEMBER(void)::AllocateFixed(VaType virt, VaType size) {
this->Map(virt, true, size);
}
ALLOC_MEMBER(void)::Free(VaType virt, VaType size) {
this->Unmap(virt, size);
}
} // namespace Common