diff options
Diffstat (limited to '')
-rw-r--r-- | src/common/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/common/address_space.cpp | 10 | ||||
-rw-r--r-- | src/common/address_space.h | 150 | ||||
-rw-r--r-- | src/common/address_space.inc | 366 | ||||
-rw-r--r-- | src/common/algorithm.h | 8 | ||||
-rw-r--r-- | src/common/hash.h | 7 | ||||
-rw-r--r-- | src/common/multi_level_page_table.cpp | 9 | ||||
-rw-r--r-- | src/common/multi_level_page_table.h | 78 | ||||
-rw-r--r-- | src/common/multi_level_page_table.inc | 84 |
9 files changed, 716 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 3447fabd8..a02696873 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -17,6 +17,8 @@ endif () include(GenerateSCMRev) add_library(common STATIC + address_space.cpp + address_space.h algorithm.h alignment.h announce_multiplayer_room.h @@ -81,6 +83,8 @@ add_library(common STATIC microprofile.cpp microprofile.h microprofileui.h + multi_level_page_table.cpp + multi_level_page_table.h nvidia_flags.cpp nvidia_flags.h page_table.cpp diff --git a/src/common/address_space.cpp b/src/common/address_space.cpp new file mode 100644 index 000000000..866e78dbe --- /dev/null +++ b/src/common/address_space.cpp @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2021 Skyline Team and Contributors +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "common/address_space.inc" + +namespace Common { + +template class Common::FlatAllocator<u32, 0, 32>; + +} diff --git a/src/common/address_space.h b/src/common/address_space.h new file mode 100644 index 000000000..9222b2fdc --- /dev/null +++ b/src/common/address_space.h @@ -0,0 +1,150 @@ +// SPDX-FileCopyrightText: 2021 Skyline Team and Contributors +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include <concepts> +#include <functional> +#include <mutex> +#include <vector> + +#include "common/common_types.h" + +namespace Common { +template <typename VaType, size_t AddressSpaceBits> +concept AddressSpaceValid = std::is_unsigned_v<VaType> && sizeof(VaType) * 8 >= AddressSpaceBits; + +struct EmptyStruct {}; + +/** + * @brief FlatAddressSpaceMap provides a generic VA->PA mapping implementation using a sorted vector + */ +template <typename VaType, VaType UnmappedVa, typename PaType, PaType UnmappedPa, + bool PaContigSplit, size_t AddressSpaceBits, typename ExtraBlockInfo = EmptyStruct> +requires AddressSpaceValid<VaType, AddressSpaceBits> +class FlatAddressSpaceMap { +public: + /// The maximum VA that this AS can technically reach + static constexpr VaType VaMaximum{(1ULL << (AddressSpaceBits - 1)) + + ((1ULL << (AddressSpaceBits - 1)) - 1)}; + + explicit FlatAddressSpaceMap(VaType va_limit, + std::function<void(VaType, VaType)> unmap_callback = {}); + + FlatAddressSpaceMap() = default; + + void Map(VaType virt, PaType phys, VaType size, ExtraBlockInfo extra_info = {}) { + std::scoped_lock lock(block_mutex); + MapLocked(virt, phys, size, extra_info); + } + + void Unmap(VaType virt, VaType size) { + std::scoped_lock lock(block_mutex); + UnmapLocked(virt, size); + } + + VaType GetVALimit() const { + return va_limit; + } + +protected: + /** + * @brief Represents a block of memory in the AS, the physical mapping is contiguous until + * another block with a different phys address is hit + */ + struct Block { + /// VA of the block + VaType virt{UnmappedVa}; + /// PA of the block, will increase 1-1 with VA until a new block is encountered + PaType phys{UnmappedPa}; + [[no_unique_address]] ExtraBlockInfo extra_info; + + Block() = default; + + Block(VaType virt_, PaType phys_, ExtraBlockInfo extra_info_) + : virt(virt_), phys(phys_), extra_info(extra_info_) {} + + bool Valid() const { + return virt != UnmappedVa; + } + + bool Mapped() const { + return phys != UnmappedPa; + } + + bool Unmapped() const { + return phys == UnmappedPa; + } + + bool operator<(const VaType& p_virt) const { + return virt < p_virt; + } + }; + + /** + * @brief Maps a PA range into the given AS region + * @note block_mutex MUST be locked when calling this + */ + void MapLocked(VaType virt, PaType phys, VaType size, ExtraBlockInfo extra_info); + + /** + * @brief Unmaps the given range and merges it with other unmapped regions + * @note block_mutex MUST be locked when calling this + */ + void UnmapLocked(VaType virt, VaType size); + + std::mutex block_mutex; + std::vector<Block> blocks{Block{}}; + + /// a soft limit on the maximum VA of the AS + VaType va_limit{VaMaximum}; + +private: + /// Callback called when the mappings in an region have changed + std::function<void(VaType, VaType)> unmap_callback{}; +}; + +/** + * @brief FlatMemoryManager specialises FlatAddressSpaceMap to work as an allocator, with an + * initial, fast linear pass and a subsequent slower pass that iterates until it finds a free block + */ +template <typename VaType, VaType UnmappedVa, size_t AddressSpaceBits> +requires AddressSpaceValid<VaType, AddressSpaceBits> +class FlatAllocator + : public FlatAddressSpaceMap<VaType, UnmappedVa, bool, false, false, AddressSpaceBits> { +private: + using Base = FlatAddressSpaceMap<VaType, UnmappedVa, bool, false, false, AddressSpaceBits>; + +public: + explicit FlatAllocator(VaType virt_start, VaType va_limit = Base::VaMaximum); + + /** + * @brief Allocates a region in the AS of the given size and returns its address + */ + VaType Allocate(VaType size); + + /** + * @brief Marks the given region in the AS as allocated + */ + void AllocateFixed(VaType virt, VaType size); + + /** + * @brief Frees an AS region so it can be used again + */ + void Free(VaType virt, VaType size); + + VaType GetVAStart() const { + return virt_start; + } + +private: + /// The base VA of the allocator, no allocations will be below this + VaType virt_start; + + /** + * The end address for the initial linear allocation pass + * Once this reaches the AS limit the slower allocation path will be used + */ + VaType current_linear_alloc_end; +}; +} // namespace Common diff --git a/src/common/address_space.inc b/src/common/address_space.inc new file mode 100644 index 000000000..2195dabd5 --- /dev/null +++ b/src/common/address_space.inc @@ -0,0 +1,366 @@ +// SPDX-FileCopyrightText: 2021 Skyline Team and Contributors +// SPDX-License-Identifier: GPL-3.0-or-later + +#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 { +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) { + ASSERT_MSG(false, "Invalid VA limit!"); + } +} + +MAP_MEMBER(void)::MapLocked(VaType virt, PaType phys, VaType size, ExtraBlockInfo extra_info) { + VaType virt_end{virt + size}; + + if (virt_end > va_limit) { + ASSERT_MSG(false, + "Trying to map a block past the VA limit: virt_end: 0x{:X}, va_limit: 0x{:X}", + virt_end, va_limit); + } + + auto block_end_successor{std::lower_bound(blocks.begin(), blocks.end(), virt_end)}; + if (block_end_successor == blocks.begin()) { + ASSERT_MSG(false, "Trying to map a block before the VA start: virt_end: 0x{:X}", virt_end); + } + + auto block_end_predecessor{std::prev(block_end_successor)}; + + if (block_end_successor != blocks.end()) { + // We have blocks in front of us, if one is directly in front then we don't have to add a + // tail + if (block_end_successor->virt != virt_end) { + PaType tailPhys{[&]() -> PaType { + if constexpr (!PaContigSplit) { + // Always propagate unmapped regions rather than calculating offset + return block_end_predecessor->phys; + } else { + 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; + } + } + }()}; + + if (block_end_predecessor->virt >= virt) { + // If this block's start would be overlapped by the map then reuse it as a tail + // block + block_end_predecessor->virt = virt_end; + block_end_predecessor->phys = tailPhys; + block_end_predecessor->extra_info = block_end_predecessor->extra_info; + + // No longer predecessor anymore + block_end_successor = block_end_predecessor--; + } else { + // Else insert a new one and we're done + 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); + } + + return; + } + } + } else { + // 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) { + // Move the unmapped block start backwards + block_end_predecessor->virt = virt_end; + + // No longer predecessor anymore + block_end_successor = block_end_predecessor--; + } else { + // Else insert a new one and we're done + blocks.insert(block_end_successor, + {Block(virt, phys, extra_info), Block(virt_end, UnmappedPa, {})}); + if (unmap_callback) { + unmap_callback(virt, size); + } + + return; + } + } + + auto block_start_successor{block_end_successor}; + + // Walk the block vector to find the start successor as this is more efficient than another + // binary search in most scenarios + while (std::prev(block_start_successor)->virt >= virt) { + block_start_successor--; + } + + // Check that the start successor is either the end block or something in between + if (block_start_successor->virt > virt_end) { + ASSERT_MSG(false, "Unsorted block in AS map: virt: 0x{:X}", block_start_successor->virt); + } else if (block_start_successor->virt == virt_end) { + // We need to create a new block as there are none spare that we would overwrite + blocks.insert(block_start_successor, Block(virt, phys, extra_info)); + } else { + // Erase overwritten blocks + if (auto eraseStart{std::next(block_start_successor)}; eraseStart != block_end_successor) { + blocks.erase(eraseStart, block_end_successor); + } + + // Reuse a block that would otherwise be overwritten as a start block + block_start_successor->virt = virt; + block_start_successor->phys = phys; + block_start_successor->extra_info = extra_info; + } + + if (unmap_callback) { + unmap_callback(virt, size); + } +} + +MAP_MEMBER(void)::UnmapLocked(VaType virt, VaType size) { + VaType virt_end{virt + size}; + + if (virt_end > va_limit) { + ASSERT_MSG(false, + "Trying to map a block past the VA limit: virt_end: 0x{:X}, va_limit: 0x{:X}", + virt_end, va_limit); + } + + auto block_end_successor{std::lower_bound(blocks.begin(), blocks.end(), virt_end)}; + if (block_end_successor == blocks.begin()) { + ASSERT_MSG(false, "Trying to unmap a block before the VA start: virt_end: 0x{:X}", + virt_end); + } + + auto block_end_predecessor{std::prev(block_end_successor)}; + + auto walk_back_to_predecessor{[&](auto iter) { + while (iter->virt >= virt) { + iter--; + } + + return iter; + }}; + + 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)}; + + auto eraseEnd{[&]() { + if (block_start_predecessor->Unmapped()) { + // 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() && + (eraseEnd == block_start_successor || + (block_start_predecessor->Unmapped() && eraseEnd->Unmapped()))) { + ASSERT_MSG(false, "Multiple contiguous unmapped regions are unsupported!"); + } + + blocks.erase(block_start_successor, eraseEnd); + }}; + + // We can avoid any splitting logic if these are the case + if (block_end_predecessor->Unmapped()) { + if (block_end_predecessor->virt > virt) { + erase_blocks_with_end_unmapped(block_end_predecessor); + } + + if (unmap_callback) { + unmap_callback(virt, size); + } + + return; // The region is unmapped, bail out early + } else if (block_end_successor->virt == virt_end && block_end_successor->Unmapped()) { + erase_blocks_with_end_unmapped(block_end_successor); + + if (unmap_callback) { + unmap_callback(virt, size); + } + + return; // The region is unmapped here and doesn't need splitting, bail out early + } else if (block_end_successor == blocks.end()) { + // This should never happen as the end should always follow an unmapped block + ASSERT_MSG(false, "Unexpected Memory Manager state!"); + } else if (block_end_successor->virt != virt_end) { + // 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{[&]() { + if constexpr (PaContigSplit) { + return block_end_predecessor->phys + virt_end - block_end_predecessor->virt; + } else { + return block_end_predecessor->phys; + } + }()}; + + if (block_end_predecessor->virt >= virt) { + // If this block's start would be overlapped by the unmap then reuse it as a tail block + block_end_predecessor->virt = virt_end; + block_end_predecessor->phys = tailPhys; + + // No longer predecessor anymore + block_end_successor = block_end_predecessor--; + } else { + blocks.insert(block_end_successor, + {Block(virt, UnmappedPa, {}), + Block(virt_end, tailPhys, block_end_predecessor->extra_info)}); + if (unmap_callback) { + unmap_callback(virt, size); + } + + // The previous block is mapped and ends before + return; + } + } + + // Walk the block vector to find the start predecessor as this is more efficient than another + // binary search in most scenarios + auto block_start_predecessor{walk_back_to_predecessor(block_end_successor)}; + auto block_start_successor{std::next(block_start_predecessor)}; + + if (block_start_successor->virt > virt_end) { + ASSERT_MSG(false, "Unsorted block in AS map: virt: 0x{:X}", block_start_successor->virt); + } else if (block_start_successor->virt == virt_end) { + // 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 + if (block_start_predecessor->Mapped()) { + blocks.insert(block_start_successor, Block(virt, UnmappedPa, {})); + } + } else if (block_start_predecessor->Unmapped()) { + // If the previous block is unmapped + blocks.erase(block_start_successor, block_end_predecessor); + } else { + // Erase overwritten blocks, skipping the first one as we have written the unmapped start + // block there + if (auto eraseStart{std::next(block_start_successor)}; eraseStart != block_end_successor) { + blocks.erase(eraseStart, block_end_successor); + } + + // Add in the unmapped block header + block_start_successor->virt = virt; + block_start_successor->phys = UnmappedPa; + } + + if (unmap_callback) + unmap_callback(virt, size); +} + +ALLOC_MEMBER_CONST()::FlatAllocator(VaType virt_start_, VaType va_limit_) + : Base{va_limit_}, virt_start{virt_start_}, current_linear_alloc_end{virt_start_} {} + +ALLOC_MEMBER(VaType)::Allocate(VaType size) { + std::scoped_lock lock(this->block_mutex); + + VaType alloc_start{UnmappedVa}; + VaType alloc_end{current_linear_alloc_end + size}; + + // Avoid searching backwards in the address space if possible + 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()) { + ASSERT_MSG(false, "First block in AS map is invalid!"); + } + + auto alloc_end_predecessor{std::prev(alloc_end_successor)}; + if (alloc_end_predecessor->virt <= current_linear_alloc_end) { + alloc_start = current_linear_alloc_end; + } else { + // Skip over fixed any mappings in front of us + 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; + break; + } + + alloc_end_predecessor = alloc_end_successor++; + + // Use the VA limit to calculate if we can fit in the final block since it has no + // successor + if (alloc_end_successor == this->blocks.end()) { + alloc_end = alloc_end_predecessor->virt + size; + + if (alloc_end >= alloc_end_predecessor->virt && alloc_end <= this->va_limit) { + alloc_start = alloc_end_predecessor->virt; + } + } + } + } + } + + if (alloc_start != UnmappedVa) { + current_linear_alloc_end = alloc_start + size; + } else { // If linear allocation overflows the AS then find a gap + if (this->blocks.size() <= 2) { + ASSERT_MSG(false, "Unexpected allocator state!"); + } + + auto search_predecessor{this->blocks.begin()}; + auto search_successor{std::next(search_predecessor)}; + + while (search_successor != this->blocks.end() && + (search_successor->virt - search_predecessor->virt < size || + search_predecessor->Mapped())) { + search_predecessor = search_successor++; + } + + if (search_successor != this->blocks.end()) { + alloc_start = search_predecessor->virt; + } else { + return {}; // AS is full + } + } + + this->MapLocked(alloc_start, true, size, {}); + return alloc_start; +} + +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 diff --git a/src/common/algorithm.h b/src/common/algorithm.h index 9ddfd637b..c27c9241d 100644 --- a/src/common/algorithm.h +++ b/src/common/algorithm.h @@ -24,4 +24,12 @@ template <class ForwardIt, class T, class Compare = std::less<>> return first != last && !comp(value, *first) ? first : last; } +template <typename T, typename Func, typename... Args> +T FoldRight(T initial_value, Func&& func, Args&&... args) { + T value{initial_value}; + const auto high_func = [&value, &func]<typename U>(U x) { value = func(value, x); }; + (std::invoke(high_func, std::forward<Args>(args)), ...); + return value; +} + } // namespace Common diff --git a/src/common/hash.h b/src/common/hash.h index b6f3e6d6f..e8fe78b07 100644 --- a/src/common/hash.h +++ b/src/common/hash.h @@ -18,4 +18,11 @@ struct PairHash { } }; +template <typename T> +struct IdentityHash { + [[nodiscard]] size_t operator()(T value) const noexcept { + return static_cast<size_t>(value); + } +}; + } // namespace Common diff --git a/src/common/multi_level_page_table.cpp b/src/common/multi_level_page_table.cpp new file mode 100644 index 000000000..46e362f3b --- /dev/null +++ b/src/common/multi_level_page_table.cpp @@ -0,0 +1,9 @@ +// SPDX-FileCopyrightText: 2021 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "common/multi_level_page_table.inc" + +namespace Common { +template class Common::MultiLevelPageTable<u64>; +template class Common::MultiLevelPageTable<u32>; +} // namespace Common diff --git a/src/common/multi_level_page_table.h b/src/common/multi_level_page_table.h new file mode 100644 index 000000000..31f6676a0 --- /dev/null +++ b/src/common/multi_level_page_table.h @@ -0,0 +1,78 @@ +// SPDX-FileCopyrightText: 2021 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include <type_traits> +#include <utility> +#include <vector> + +#include "common/common_types.h" + +namespace Common { + +template <typename BaseAddr> +class MultiLevelPageTable final { +public: + constexpr MultiLevelPageTable() = default; + explicit MultiLevelPageTable(std::size_t address_space_bits, std::size_t first_level_bits, + std::size_t page_bits); + + ~MultiLevelPageTable() noexcept; + + MultiLevelPageTable(const MultiLevelPageTable&) = delete; + MultiLevelPageTable& operator=(const MultiLevelPageTable&) = delete; + + MultiLevelPageTable(MultiLevelPageTable&& other) noexcept + : address_space_bits{std::exchange(other.address_space_bits, 0)}, + first_level_bits{std::exchange(other.first_level_bits, 0)}, page_bits{std::exchange( + other.page_bits, 0)}, + first_level_shift{std::exchange(other.first_level_shift, 0)}, + first_level_chunk_size{std::exchange(other.first_level_chunk_size, 0)}, + first_level_map{std::move(other.first_level_map)}, base_ptr{std::exchange(other.base_ptr, + nullptr)} {} + + MultiLevelPageTable& operator=(MultiLevelPageTable&& other) noexcept { + address_space_bits = std::exchange(other.address_space_bits, 0); + first_level_bits = std::exchange(other.first_level_bits, 0); + page_bits = std::exchange(other.page_bits, 0); + first_level_shift = std::exchange(other.first_level_shift, 0); + first_level_chunk_size = std::exchange(other.first_level_chunk_size, 0); + alloc_size = std::exchange(other.alloc_size, 0); + first_level_map = std::move(other.first_level_map); + base_ptr = std::exchange(other.base_ptr, nullptr); + return *this; + } + + void ReserveRange(u64 start, std::size_t size); + + [[nodiscard]] const BaseAddr& operator[](std::size_t index) const { + return base_ptr[index]; + } + + [[nodiscard]] BaseAddr& operator[](std::size_t index) { + return base_ptr[index]; + } + + [[nodiscard]] BaseAddr* data() { + return base_ptr; + } + + [[nodiscard]] const BaseAddr* data() const { + return base_ptr; + } + +private: + void AllocateLevel(u64 level); + + std::size_t address_space_bits{}; + std::size_t first_level_bits{}; + std::size_t page_bits{}; + std::size_t first_level_shift{}; + std::size_t first_level_chunk_size{}; + std::size_t alloc_size{}; + std::vector<void*> first_level_map{}; + BaseAddr* base_ptr{}; +}; + +} // namespace Common diff --git a/src/common/multi_level_page_table.inc b/src/common/multi_level_page_table.inc new file mode 100644 index 000000000..8ac506fa0 --- /dev/null +++ b/src/common/multi_level_page_table.inc @@ -0,0 +1,84 @@ +// SPDX-FileCopyrightText: 2021 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#ifdef _WIN32 +#include <windows.h> +#else +#include <sys/mman.h> +#endif + +#include "common/assert.h" +#include "common/multi_level_page_table.h" + +namespace Common { + +template <typename BaseAddr> +MultiLevelPageTable<BaseAddr>::MultiLevelPageTable(std::size_t address_space_bits_, + std::size_t first_level_bits_, + std::size_t page_bits_) + : address_space_bits{address_space_bits_}, + first_level_bits{first_level_bits_}, page_bits{page_bits_} { + if (page_bits == 0) { + return; + } + first_level_shift = address_space_bits - first_level_bits; + first_level_chunk_size = (1ULL << (first_level_shift - page_bits)) * sizeof(BaseAddr); + alloc_size = (1ULL << (address_space_bits - page_bits)) * sizeof(BaseAddr); + std::size_t first_level_size = 1ULL << first_level_bits; + first_level_map.resize(first_level_size, nullptr); +#ifdef _WIN32 + void* base{VirtualAlloc(nullptr, alloc_size, MEM_RESERVE, PAGE_READWRITE)}; +#else + void* base{mmap(nullptr, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)}; + + if (base == MAP_FAILED) { + base = nullptr; + } +#endif + + ASSERT(base); + base_ptr = reinterpret_cast<BaseAddr*>(base); +} + +template <typename BaseAddr> +MultiLevelPageTable<BaseAddr>::~MultiLevelPageTable() noexcept { + if (!base_ptr) { + return; + } +#ifdef _WIN32 + ASSERT(VirtualFree(base_ptr, 0, MEM_RELEASE)); +#else + ASSERT(munmap(base_ptr, alloc_size) == 0); +#endif +} + +template <typename BaseAddr> +void MultiLevelPageTable<BaseAddr>::ReserveRange(u64 start, std::size_t size) { + const u64 new_start = start >> first_level_shift; + const u64 new_end = (start + size) >> first_level_shift; + for (u64 i = new_start; i <= new_end; i++) { + if (!first_level_map[i]) { + AllocateLevel(i); + } + } +} + +template <typename BaseAddr> +void MultiLevelPageTable<BaseAddr>::AllocateLevel(u64 level) { + void* ptr = reinterpret_cast<char *>(base_ptr) + level * first_level_chunk_size; +#ifdef _WIN32 + void* base{VirtualAlloc(ptr, first_level_chunk_size, MEM_COMMIT, PAGE_READWRITE)}; +#else + void* base{mmap(ptr, first_level_chunk_size, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)}; + + if (base == MAP_FAILED) { + base = nullptr; + } +#endif + ASSERT(base); + + first_level_map[level] = base; +} + +} // namespace Common |