diff options
Diffstat (limited to 'src/common/address_space.h')
| -rw-r--r-- | src/common/address_space.h | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/common/address_space.h b/src/common/address_space.h new file mode 100644 index 000000000..fd2f32b7d --- /dev/null +++ b/src/common/address_space.h | |||
| @@ -0,0 +1,134 @@ | |||
| 1 | // Copyright © 2021 Skyline Team and Contributors (https://github.com/skyline-emu/) | ||
| 2 | // Licensed under GPLv3 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <concepts> | ||
| 8 | #include <functional> | ||
| 9 | #include <mutex> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "common/common_types.h" | ||
| 13 | |||
| 14 | namespace Common { | ||
| 15 | template <typename VaType, size_t AddressSpaceBits> | ||
| 16 | concept AddressSpaceValid = std::is_unsigned_v<VaType> && sizeof(VaType) * 8 >= AddressSpaceBits; | ||
| 17 | |||
| 18 | struct EmptyStruct {}; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * @brief FlatAddressSpaceMap provides a generic VA->PA mapping implementation using a sorted vector | ||
| 22 | */ | ||
| 23 | template <typename VaType, VaType UnmappedVa, typename PaType, PaType UnmappedPa, | ||
| 24 | bool PaContigSplit, size_t AddressSpaceBits, typename ExtraBlockInfo = EmptyStruct> | ||
| 25 | requires AddressSpaceValid<VaType, AddressSpaceBits> class FlatAddressSpaceMap { | ||
| 26 | private: | ||
| 27 | std::function<void(VaType, VaType)> | ||
| 28 | unmapCallback{}; //!< Callback called when the mappings in an region have changed | ||
| 29 | |||
| 30 | protected: | ||
| 31 | /** | ||
| 32 | * @brief Represents a block of memory in the AS, the physical mapping is contiguous until | ||
| 33 | * another block with a different phys address is hit | ||
| 34 | */ | ||
| 35 | struct Block { | ||
| 36 | VaType virt{UnmappedVa}; //!< VA of the block | ||
| 37 | PaType phys{UnmappedPa}; //!< PA of the block, will increase 1-1 with VA until a new block | ||
| 38 | //!< is encountered | ||
| 39 | [[no_unique_address]] ExtraBlockInfo extraInfo; | ||
| 40 | |||
| 41 | Block() = default; | ||
| 42 | |||
| 43 | Block(VaType virt, PaType phys, ExtraBlockInfo extraInfo) | ||
| 44 | : virt(virt), phys(phys), extraInfo(extraInfo) {} | ||
| 45 | |||
| 46 | constexpr bool Valid() { | ||
| 47 | return virt != UnmappedVa; | ||
| 48 | } | ||
| 49 | |||
| 50 | constexpr bool Mapped() { | ||
| 51 | return phys != UnmappedPa; | ||
| 52 | } | ||
| 53 | |||
| 54 | constexpr bool Unmapped() { | ||
| 55 | return phys == UnmappedPa; | ||
| 56 | } | ||
| 57 | |||
| 58 | bool operator<(const VaType& pVirt) const { | ||
| 59 | return virt < pVirt; | ||
| 60 | } | ||
| 61 | }; | ||
| 62 | |||
| 63 | std::mutex blockMutex; | ||
| 64 | std::vector<Block> blocks{Block{}}; | ||
| 65 | |||
| 66 | /** | ||
| 67 | * @brief Maps a PA range into the given AS region | ||
| 68 | * @note blockMutex MUST be locked when calling this | ||
| 69 | */ | ||
| 70 | void MapLocked(VaType virt, PaType phys, VaType size, ExtraBlockInfo extraInfo); | ||
| 71 | |||
| 72 | /** | ||
| 73 | * @brief Unmaps the given range and merges it with other unmapped regions | ||
| 74 | * @note blockMutex MUST be locked when calling this | ||
| 75 | */ | ||
| 76 | void UnmapLocked(VaType virt, VaType size); | ||
| 77 | |||
| 78 | public: | ||
| 79 | static constexpr VaType VaMaximum{(1ULL << (AddressSpaceBits - 1)) + | ||
| 80 | ((1ULL << (AddressSpaceBits - 1)) - | ||
| 81 | 1)}; //!< The maximum VA that this AS can technically reach | ||
| 82 | |||
| 83 | VaType vaLimit{VaMaximum}; //!< A soft limit on the maximum VA of the AS | ||
| 84 | |||
| 85 | FlatAddressSpaceMap(VaType vaLimit, std::function<void(VaType, VaType)> unmapCallback = {}); | ||
| 86 | |||
| 87 | FlatAddressSpaceMap() = default; | ||
| 88 | |||
| 89 | void Map(VaType virt, PaType phys, VaType size, ExtraBlockInfo extraInfo = {}) { | ||
| 90 | std::scoped_lock lock(blockMutex); | ||
| 91 | MapLocked(virt, phys, size, extraInfo); | ||
| 92 | } | ||
| 93 | |||
| 94 | void Unmap(VaType virt, VaType size) { | ||
| 95 | std::scoped_lock lock(blockMutex); | ||
| 96 | UnmapLocked(virt, size); | ||
| 97 | } | ||
| 98 | }; | ||
| 99 | |||
| 100 | /** | ||
| 101 | * @brief FlatMemoryManager specialises FlatAddressSpaceMap to work as an allocator, with an | ||
| 102 | * initial, fast linear pass and a subsequent slower pass that iterates until it finds a free block | ||
| 103 | */ | ||
| 104 | template <typename VaType, VaType UnmappedVa, size_t AddressSpaceBits> | ||
| 105 | requires AddressSpaceValid<VaType, AddressSpaceBits> class FlatAllocator | ||
| 106 | : public FlatAddressSpaceMap<VaType, UnmappedVa, bool, false, false, AddressSpaceBits> { | ||
| 107 | private: | ||
| 108 | using Base = FlatAddressSpaceMap<VaType, UnmappedVa, bool, false, false, AddressSpaceBits>; | ||
| 109 | |||
| 110 | VaType currentLinearAllocEnd; //!< The end address for the initial linear allocation pass, once | ||
| 111 | //!< this reaches the AS limit the slower allocation path will be | ||
| 112 | //!< used | ||
| 113 | |||
| 114 | public: | ||
| 115 | VaType vaStart; //!< The base VA of the allocator, no allocations will be below this | ||
| 116 | |||
| 117 | FlatAllocator(VaType vaStart, VaType vaLimit = Base::VaMaximum); | ||
| 118 | |||
| 119 | /** | ||
| 120 | * @brief Allocates a region in the AS of the given size and returns its address | ||
| 121 | */ | ||
| 122 | VaType Allocate(VaType size); | ||
| 123 | |||
| 124 | /** | ||
| 125 | * @brief Marks the given region in the AS as allocated | ||
| 126 | */ | ||
| 127 | void AllocateFixed(VaType virt, VaType size); | ||
| 128 | |||
| 129 | /** | ||
| 130 | * @brief Frees an AS region so it can be used again | ||
| 131 | */ | ||
| 132 | void Free(VaType virt, VaType size); | ||
| 133 | }; | ||
| 134 | } // namespace Common | ||