1
0
Fork 0
forked from suyu/suyu
suyu/src/common/address_space.inc

366 lines
15 KiB
PHP
Raw Normal View History

// SPDX-FileCopyrightText: 2021 Skyline Team and Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
2021-11-14 20:55:52 +01: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-06-30 02:33:04 +02:00
MAP_MEMBER_CONST()::FlatAddressSpaceMap(VaType va_limit_,
std::function<void(VaType, VaType)> unmap_callback_)
: va_limit{va_limit_}, unmap_callback{std::move(unmap_callback_)} {
if (va_limit > VaMaximum) {
2021-11-14 20:55:52 +01:00
UNREACHABLE_MSG("Invalid VA limit!");
2022-06-30 02:33:04 +02:00
}
2021-11-14 20:55:52 +01:00
}
2022-06-30 02:33:04 +02:00
MAP_MEMBER(void)::MapLocked(VaType virt, PaType phys, VaType size, ExtraBlockInfo extra_info) {
VaType virt_end{virt + size};
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
if (virt_end > va_limit) {
UNREACHABLE_MSG(
"Trying to map a block past the VA limit: virt_end: 0x{:X}, va_limit: 0x{:X}", virt_end,
va_limit);
}
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
auto block_end_successor{std::lower_bound(blocks.begin(), blocks.end(), virt_end)};
if (block_end_successor == blocks.begin()) {
UNREACHABLE_MSG("Trying to map a block before the VA start: virt_end: 0x{:X}", virt_end);
}
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
auto block_end_predecessor{std::prev(block_end_successor)};
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
if (block_end_successor != blocks.end()) {
2021-11-14 20:55:52 +01:00
// We have blocks in front of us, if one is directly in front then we don't have to add a
// tail
2022-06-30 02:33:04 +02:00
if (block_end_successor->virt != virt_end) {
2021-11-14 20:55:52 +01:00
PaType tailPhys{[&]() -> PaType {
if constexpr (!PaContigSplit) {
2022-06-30 02:33:04 +02:00
// Always propagate unmapped regions rather than calculating offset
return block_end_predecessor->phys;
2021-11-14 20:55:52 +01:00
} else {
2022-06-30 02:33:04 +02:00
if (block_end_predecessor->Unmapped()) {
// Always propagate unmapped regions rather than calculating offset
return block_end_predecessor->phys;
} else {
return block_end_predecessor->phys + virt_end - block_end_predecessor->virt;
}
2021-11-14 20:55:52 +01:00
}
}()};
2022-06-30 02:33:04 +02:00
if (block_end_predecessor->virt >= virt) {
2021-11-14 20:55:52 +01:00
// If this block's start would be overlapped by the map then reuse it as a tail
// block
2022-06-30 02:33:04 +02:00
block_end_predecessor->virt = virt_end;
block_end_predecessor->phys = tailPhys;
block_end_predecessor->extra_info = block_end_predecessor->extra_info;
2021-11-14 20:55:52 +01:00
// No longer predecessor anymore
2022-06-30 02:33:04 +02:00
block_end_successor = block_end_predecessor--;
2021-11-14 20:55:52 +01:00
} else {
// Else insert a new one and we're done
2022-06-30 02:33:04 +02:00
blocks.insert(block_end_successor,
{Block(virt, phys, extra_info),
Block(virt_end, tailPhys, block_end_predecessor->extra_info)});
if (unmap_callback) {
unmap_callback(virt, size);
}
2021-11-14 20:55:52 +01:00
return;
}
}
} else {
2022-06-30 02:33:04 +02:00
// block_end_predecessor will always be unmapped as blocks has to be terminated by an
// unmapped chunk
if (block_end_predecessor != blocks.begin() && block_end_predecessor->virt >= virt) {
2021-11-14 20:55:52 +01:00
// Move the unmapped block start backwards
2022-06-30 02:33:04 +02:00
block_end_predecessor->virt = virt_end;
2021-11-14 20:55:52 +01:00
// No longer predecessor anymore
2022-06-30 02:33:04 +02:00
block_end_successor = block_end_predecessor--;
2021-11-14 20:55:52 +01:00
} else {
// Else insert a new one and we're done
2022-06-30 02:33:04 +02:00
blocks.insert(block_end_successor,
{Block(virt, phys, extra_info), Block(virt_end, UnmappedPa, {})});
if (unmap_callback) {
unmap_callback(virt, size);
}
2021-11-14 20:55:52 +01:00
return;
}
}
2022-06-30 02:33:04 +02:00
auto block_start_successor{block_end_successor};
2021-11-14 20:55:52 +01:00
// Walk the block vector to find the start successor as this is more efficient than another
// binary search in most scenarios
2022-06-30 02:33:04 +02:00
while (std::prev(block_start_successor)->virt >= virt) {
block_start_successor--;
}
2021-11-14 20:55:52 +01:00
// Check that the start successor is either the end block or something in between
2022-06-30 02:33:04 +02:00
if (block_start_successor->virt > virt_end) {
UNREACHABLE_MSG("Unsorted block in AS map: virt: 0x{:X}", block_start_successor->virt);
} else if (block_start_successor->virt == virt_end) {
2021-11-14 20:55:52 +01:00
// We need to create a new block as there are none spare that we would overwrite
2022-06-30 02:33:04 +02:00
blocks.insert(block_start_successor, Block(virt, phys, extra_info));
2021-11-14 20:55:52 +01:00
} else {
// Erase overwritten blocks
2022-06-30 02:33:04 +02:00
if (auto eraseStart{std::next(block_start_successor)}; eraseStart != block_end_successor) {
blocks.erase(eraseStart, block_end_successor);
}
2021-11-14 20:55:52 +01:00
// Reuse a block that would otherwise be overwritten as a start block
2022-06-30 02:33:04 +02:00
block_start_successor->virt = virt;
block_start_successor->phys = phys;
block_start_successor->extra_info = extra_info;
2021-11-14 20:55:52 +01:00
}
2022-06-30 02:33:04 +02:00
if (unmap_callback) {
unmap_callback(virt, size);
}
2021-11-14 20:55:52 +01:00
}
MAP_MEMBER(void)::UnmapLocked(VaType virt, VaType size) {
2022-06-30 02:33:04 +02:00
VaType virt_end{virt + size};
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
if (virt_end > va_limit) {
UNREACHABLE_MSG(
"Trying to map a block past the VA limit: virt_end: 0x{:X}, va_limit: 0x{:X}", virt_end,
va_limit);
}
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
auto block_end_successor{std::lower_bound(blocks.begin(), blocks.end(), virt_end)};
if (block_end_successor == blocks.begin()) {
UNREACHABLE_MSG("Trying to unmap a block before the VA start: virt_end: 0x{:X}", virt_end);
}
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
auto block_end_predecessor{std::prev(block_end_successor)};
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
auto walk_back_to_predecessor{[&](auto iter) {
while (iter->virt >= virt) {
2021-11-14 20:55:52 +01:00
iter--;
2022-06-30 02:33:04 +02:00
}
2021-11-14 20:55:52 +01:00
return iter;
}};
2022-06-30 02:33:04 +02:00
auto erase_blocks_with_end_unmapped{[&](auto unmappedEnd) {
auto block_start_predecessor{walk_back_to_predecessor(unmappedEnd)};
auto block_start_successor{std::next(block_start_predecessor)};
2021-11-14 20:55:52 +01:00
auto eraseEnd{[&]() {
2022-06-30 02:33:04 +02:00
if (block_start_predecessor->Unmapped()) {
2021-11-14 20:55:52 +01: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-06-30 02:33:04 +02:00
(eraseEnd == block_start_successor ||
(block_start_predecessor->Unmapped() && eraseEnd->Unmapped()))) {
2021-11-14 20:55:52 +01:00
UNREACHABLE_MSG("Multiple contiguous unmapped regions are unsupported!");
2022-06-30 02:33:04 +02:00
}
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
blocks.erase(block_start_successor, eraseEnd);
2021-11-14 20:55:52 +01:00
}};
// We can avoid any splitting logic if these are the case
2022-06-30 02:33:04 +02:00
if (block_end_predecessor->Unmapped()) {
if (block_end_predecessor->virt > virt) {
erase_blocks_with_end_unmapped(block_end_predecessor);
}
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
if (unmap_callback) {
unmap_callback(virt, size);
}
2021-11-14 20:55:52 +01:00
return; // The region is unmapped, bail out early
2022-06-30 02:33:04 +02:00
} else if (block_end_successor->virt == virt_end && block_end_successor->Unmapped()) {
erase_blocks_with_end_unmapped(block_end_successor);
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
if (unmap_callback) {
unmap_callback(virt, size);
}
2021-11-14 20:55:52 +01:00
return; // The region is unmapped here and doesn't need splitting, bail out early
2022-06-30 02:33:04 +02:00
} else if (block_end_successor == blocks.end()) {
2021-11-14 20:55:52 +01:00
// This should never happen as the end should always follow an unmapped block
UNREACHABLE_MSG("Unexpected Memory Manager state!");
2022-06-30 02:33:04 +02:00
} else if (block_end_successor->virt != virt_end) {
2021-11-14 20:55:52 +01: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-06-30 02:33:04 +02:00
if constexpr (PaContigSplit) {
return block_end_predecessor->phys + virt_end - block_end_predecessor->virt;
} else {
return block_end_predecessor->phys;
}
2021-11-14 20:55:52 +01:00
}()};
2022-06-30 02:33:04 +02:00
if (block_end_predecessor->virt >= virt) {
2021-11-14 20:55:52 +01:00
// If this block's start would be overlapped by the unmap then reuse it as a tail block
2022-06-30 02:33:04 +02:00
block_end_predecessor->virt = virt_end;
block_end_predecessor->phys = tailPhys;
2021-11-14 20:55:52 +01:00
// No longer predecessor anymore
2022-06-30 02:33:04 +02:00
block_end_successor = block_end_predecessor--;
2021-11-14 20:55:52 +01:00
} else {
2022-06-30 02:33:04 +02:00
blocks.insert(block_end_successor,
2021-11-14 20:55:52 +01:00
{Block(virt, UnmappedPa, {}),
2022-06-30 02:33:04 +02:00
Block(virt_end, tailPhys, block_end_predecessor->extra_info)});
if (unmap_callback) {
unmap_callback(virt, size);
}
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
// The previous block is mapped and ends before
return;
2021-11-14 20:55:52 +01:00
}
}
// Walk the block vector to find the start predecessor as this is more efficient than another
// binary search in most scenarios
2022-06-30 02:33:04 +02:00
auto block_start_predecessor{walk_back_to_predecessor(block_end_successor)};
auto block_start_successor{std::next(block_start_predecessor)};
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
if (block_start_successor->virt > virt_end) {
UNREACHABLE_MSG("Unsorted block in AS map: virt: 0x{:X}", block_start_successor->virt);
} else if (block_start_successor->virt == virt_end) {
2021-11-14 20:55:52 +01: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-06-30 02:33:04 +02:00
if (block_start_predecessor->Mapped()) {
blocks.insert(block_start_successor, Block(virt, UnmappedPa, {}));
}
} else if (block_start_predecessor->Unmapped()) {
2021-11-14 20:55:52 +01:00
// If the previous block is unmapped
2022-06-30 02:33:04 +02:00
blocks.erase(block_start_successor, block_end_predecessor);
2021-11-14 20:55:52 +01:00
} else {
// Erase overwritten blocks, skipping the first one as we have written the unmapped start
// block there
2022-06-30 02:33:04 +02:00
if (auto eraseStart{std::next(block_start_successor)}; eraseStart != block_end_successor) {
blocks.erase(eraseStart, block_end_successor);
}
2021-11-14 20:55:52 +01:00
// Add in the unmapped block header
2022-06-30 02:33:04 +02:00
block_start_successor->virt = virt;
block_start_successor->phys = UnmappedPa;
2021-11-14 20:55:52 +01:00
}
2022-06-30 02:33:04 +02:00
if (unmap_callback)
unmap_callback(virt, size);
2021-11-14 20:55:52 +01:00
}
ALLOC_MEMBER_CONST()::FlatAllocator(VaType virt_start_, VaType va_limit_)
: Base{va_limit_}, virt_start{virt_start_}, current_linear_alloc_end{virt_start_} {}
2021-11-14 20:55:52 +01:00
ALLOC_MEMBER(VaType)::Allocate(VaType size) {
2022-06-30 02:33:04 +02:00
std::scoped_lock lock(this->block_mutex);
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
VaType alloc_start{UnmappedVa};
VaType alloc_end{current_linear_alloc_end + size};
2021-11-14 20:55:52 +01:00
// Avoid searching backwards in the address space if possible
2022-06-30 02:33:04 +02:00
if (alloc_end >= current_linear_alloc_end && alloc_end <= this->va_limit) {
auto alloc_end_successor{
std::lower_bound(this->blocks.begin(), this->blocks.end(), alloc_end)};
if (alloc_end_successor == this->blocks.begin()) {
2021-11-14 20:55:52 +01:00
UNREACHABLE_MSG("First block in AS map is invalid!");
2022-06-30 02:33:04 +02:00
}
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
auto alloc_end_predecessor{std::prev(alloc_end_successor)};
if (alloc_end_predecessor->virt <= current_linear_alloc_end) {
alloc_start = current_linear_alloc_end;
2021-11-14 20:55:52 +01:00
} else {
// Skip over fixed any mappings in front of us
2022-06-30 02:33:04 +02:00
while (alloc_end_successor != this->blocks.end()) {
if (alloc_end_successor->virt - alloc_end_predecessor->virt < size ||
alloc_end_predecessor->Mapped()) {
alloc_start = alloc_end_predecessor->virt;
2021-11-14 20:55:52 +01:00
break;
}
2022-06-30 02:33:04 +02:00
alloc_end_predecessor = alloc_end_successor++;
2021-11-14 20:55:52 +01:00
// Use the VA limit to calculate if we can fit in the final block since it has no
// successor
2022-06-30 02:33:04 +02:00
if (alloc_end_successor == this->blocks.end()) {
alloc_end = alloc_end_predecessor->virt + size;
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
if (alloc_end >= alloc_end_predecessor->virt && alloc_end <= this->va_limit) {
alloc_start = alloc_end_predecessor->virt;
}
2021-11-14 20:55:52 +01:00
}
}
}
}
2022-06-30 02:33:04 +02:00
if (alloc_start != UnmappedVa) {
current_linear_alloc_end = alloc_start + size;
2021-11-14 20:55:52 +01:00
} else { // If linear allocation overflows the AS then find a gap
2022-06-30 02:33:04 +02:00
if (this->blocks.size() <= 2) {
2021-11-14 20:55:52 +01:00
UNREACHABLE_MSG("Unexpected allocator state!");
2022-06-30 02:33:04 +02:00
}
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
auto search_predecessor{this->blocks.begin()};
auto search_successor{std::next(search_predecessor)};
2021-11-14 20:55:52 +01:00
2022-06-30 02:33:04 +02:00
while (search_successor != this->blocks.end() &&
(search_successor->virt - search_predecessor->virt < size ||
search_predecessor->Mapped())) {
search_predecessor = search_successor++;
2021-11-14 20:55:52 +01:00
}
2022-06-30 02:33:04 +02:00
if (search_successor != this->blocks.end()) {
alloc_start = search_predecessor->virt;
} else {
2021-11-14 20:55:52 +01:00
return {}; // AS is full
2022-06-30 02:33:04 +02:00
}
2021-11-14 20:55:52 +01:00
}
2022-06-30 02:33:04 +02:00
this->MapLocked(alloc_start, true, size, {});
return alloc_start;
2021-11-14 20:55:52 +01: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