diff options
| author | 2021-02-11 18:48:02 -0800 | |
|---|---|---|
| committer | 2021-02-18 16:16:24 -0800 | |
| commit | 6a19086001b5d7229728199d074b72c1d0dd34af (patch) | |
| tree | 73091c9902c03d909ae9ba2e9667332811e40c96 /src | |
| parent | hle: kernel: Add KPageBitmap class. (diff) | |
| download | yuzu-6a19086001b5d7229728199d074b72c1d0dd34af.tar.gz yuzu-6a19086001b5d7229728199d074b72c1d0dd34af.tar.xz yuzu-6a19086001b5d7229728199d074b72c1d0dd34af.zip | |
hle: kernel: memory: PageHeap: Migrate to KPageBitmap class.
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/hle/kernel/memory/memory_manager.cpp | 6 | ||||
| -rw-r--r-- | src/core/hle/kernel/memory/memory_manager.h | 4 | ||||
| -rw-r--r-- | src/core/hle/kernel/memory/page_heap.cpp | 8 | ||||
| -rw-r--r-- | src/core/hle/kernel/memory/page_heap.h | 202 |
4 files changed, 23 insertions, 197 deletions
diff --git a/src/core/hle/kernel/memory/memory_manager.cpp b/src/core/hle/kernel/memory/memory_manager.cpp index 77f135cdc..c373d9947 100644 --- a/src/core/hle/kernel/memory/memory_manager.cpp +++ b/src/core/hle/kernel/memory/memory_manager.cpp | |||
| @@ -21,7 +21,7 @@ std::size_t MemoryManager::Impl::Initialize(Pool new_pool, u64 start_address, u6 | |||
| 21 | const auto ref_count_size{(size / PageSize) * sizeof(u16)}; | 21 | const auto ref_count_size{(size / PageSize) * sizeof(u16)}; |
| 22 | const auto optimize_map_size{(Common::AlignUp((size / PageSize), 64) / 64) * sizeof(u64)}; | 22 | const auto optimize_map_size{(Common::AlignUp((size / PageSize), 64) / 64) * sizeof(u64)}; |
| 23 | const auto manager_size{Common::AlignUp(optimize_map_size + ref_count_size, PageSize)}; | 23 | const auto manager_size{Common::AlignUp(optimize_map_size + ref_count_size, PageSize)}; |
| 24 | const auto page_heap_size{PageHeap::CalculateMetadataOverheadSize(size)}; | 24 | const auto page_heap_size{PageHeap::CalculateManagementOverheadSize(size)}; |
| 25 | const auto total_metadata_size{manager_size + page_heap_size}; | 25 | const auto total_metadata_size{manager_size + page_heap_size}; |
| 26 | ASSERT(manager_size <= total_metadata_size); | 26 | ASSERT(manager_size <= total_metadata_size); |
| 27 | ASSERT(Common::IsAligned(total_metadata_size, PageSize)); | 27 | ASSERT(Common::IsAligned(total_metadata_size, PageSize)); |
| @@ -63,7 +63,7 @@ VAddr MemoryManager::AllocateContinuous(std::size_t num_pages, std::size_t align | |||
| 63 | // Loop, trying to iterate from each block | 63 | // Loop, trying to iterate from each block |
| 64 | // TODO (bunnei): Support multiple managers | 64 | // TODO (bunnei): Support multiple managers |
| 65 | Impl& chosen_manager{managers[pool_index]}; | 65 | Impl& chosen_manager{managers[pool_index]}; |
| 66 | VAddr allocated_block{chosen_manager.AllocateBlock(heap_index)}; | 66 | VAddr allocated_block{chosen_manager.AllocateBlock(heap_index, false)}; |
| 67 | 67 | ||
| 68 | // If we failed to allocate, quit now | 68 | // If we failed to allocate, quit now |
| 69 | if (!allocated_block) { | 69 | if (!allocated_block) { |
| @@ -116,7 +116,7 @@ ResultCode MemoryManager::Allocate(PageLinkedList& page_list, std::size_t num_pa | |||
| 116 | 116 | ||
| 117 | while (num_pages >= pages_per_alloc) { | 117 | while (num_pages >= pages_per_alloc) { |
| 118 | // Allocate a block | 118 | // Allocate a block |
| 119 | VAddr allocated_block{chosen_manager.AllocateBlock(index)}; | 119 | VAddr allocated_block{chosen_manager.AllocateBlock(index, false)}; |
| 120 | if (!allocated_block) { | 120 | if (!allocated_block) { |
| 121 | break; | 121 | break; |
| 122 | } | 122 | } |
diff --git a/src/core/hle/kernel/memory/memory_manager.h b/src/core/hle/kernel/memory/memory_manager.h index 3cf444857..00c04eebd 100644 --- a/src/core/hle/kernel/memory/memory_manager.h +++ b/src/core/hle/kernel/memory/memory_manager.h | |||
| @@ -67,8 +67,8 @@ private: | |||
| 67 | 67 | ||
| 68 | std::size_t Initialize(Pool new_pool, u64 start_address, u64 end_address); | 68 | std::size_t Initialize(Pool new_pool, u64 start_address, u64 end_address); |
| 69 | 69 | ||
| 70 | VAddr AllocateBlock(s32 index) { | 70 | VAddr AllocateBlock(s32 index, bool random) { |
| 71 | return heap.AllocateBlock(index); | 71 | return heap.AllocateBlock(index, random); |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | void Free(VAddr addr, std::size_t num_pages) { | 74 | void Free(VAddr addr, std::size_t num_pages) { |
diff --git a/src/core/hle/kernel/memory/page_heap.cpp b/src/core/hle/kernel/memory/page_heap.cpp index 0ab1f7205..8fb53a0e8 100644 --- a/src/core/hle/kernel/memory/page_heap.cpp +++ b/src/core/hle/kernel/memory/page_heap.cpp | |||
| @@ -32,11 +32,11 @@ void PageHeap::Initialize(VAddr address, std::size_t size, std::size_t metadata_ | |||
| 32 | } | 32 | } |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | VAddr PageHeap::AllocateBlock(s32 index) { | 35 | VAddr PageHeap::AllocateBlock(s32 index, bool random) { |
| 36 | const std::size_t needed_size{blocks[index].GetSize()}; | 36 | const std::size_t needed_size{blocks[index].GetSize()}; |
| 37 | 37 | ||
| 38 | for (s32 i{index}; i < static_cast<s32>(MemoryBlockPageShifts.size()); i++) { | 38 | for (s32 i{index}; i < static_cast<s32>(MemoryBlockPageShifts.size()); i++) { |
| 39 | if (const VAddr addr{blocks[i].PopBlock()}; addr) { | 39 | if (const VAddr addr{blocks[i].PopBlock(random)}; addr) { |
| 40 | if (const std::size_t allocated_size{blocks[i].GetSize()}; | 40 | if (const std::size_t allocated_size{blocks[i].GetSize()}; |
| 41 | allocated_size > needed_size) { | 41 | allocated_size > needed_size) { |
| 42 | Free(addr + needed_size, (allocated_size - needed_size) / PageSize); | 42 | Free(addr + needed_size, (allocated_size - needed_size) / PageSize); |
| @@ -104,13 +104,13 @@ void PageHeap::Free(VAddr addr, std::size_t num_pages) { | |||
| 104 | } | 104 | } |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | std::size_t PageHeap::CalculateMetadataOverheadSize(std::size_t region_size) { | 107 | std::size_t PageHeap::CalculateManagementOverheadSize(std::size_t region_size) { |
| 108 | std::size_t overhead_size = 0; | 108 | std::size_t overhead_size = 0; |
| 109 | for (std::size_t i = 0; i < MemoryBlockPageShifts.size(); i++) { | 109 | for (std::size_t i = 0; i < MemoryBlockPageShifts.size(); i++) { |
| 110 | const std::size_t cur_block_shift{MemoryBlockPageShifts[i]}; | 110 | const std::size_t cur_block_shift{MemoryBlockPageShifts[i]}; |
| 111 | const std::size_t next_block_shift{ | 111 | const std::size_t next_block_shift{ |
| 112 | (i != MemoryBlockPageShifts.size() - 1) ? MemoryBlockPageShifts[i + 1] : 0}; | 112 | (i != MemoryBlockPageShifts.size() - 1) ? MemoryBlockPageShifts[i + 1] : 0}; |
| 113 | overhead_size += PageHeap::Block::CalculateMetadataOverheadSize( | 113 | overhead_size += PageHeap::Block::CalculateManagementOverheadSize( |
| 114 | region_size, cur_block_shift, next_block_shift); | 114 | region_size, cur_block_shift, next_block_shift); |
| 115 | } | 115 | } |
| 116 | return Common::AlignUp(overhead_size, PageSize); | 116 | return Common::AlignUp(overhead_size, PageSize); |
diff --git a/src/core/hle/kernel/memory/page_heap.h b/src/core/hle/kernel/memory/page_heap.h index 131093284..ee339f329 100644 --- a/src/core/hle/kernel/memory/page_heap.h +++ b/src/core/hle/kernel/memory/page_heap.h | |||
| @@ -15,6 +15,7 @@ | |||
| 15 | #include "common/assert.h" | 15 | #include "common/assert.h" |
| 16 | #include "common/common_funcs.h" | 16 | #include "common/common_funcs.h" |
| 17 | #include "common/common_types.h" | 17 | #include "common/common_types.h" |
| 18 | #include "core/hle/kernel/k_page_bitmap.h" | ||
| 18 | #include "core/hle/kernel/memory/memory_types.h" | 19 | #include "core/hle/kernel/memory/memory_types.h" |
| 19 | 20 | ||
| 20 | namespace Kernel::Memory { | 21 | namespace Kernel::Memory { |
| @@ -57,189 +58,14 @@ private: | |||
| 57 | 58 | ||
| 58 | class Block final : NonCopyable { | 59 | class Block final : NonCopyable { |
| 59 | private: | 60 | private: |
| 60 | class Bitmap final : NonCopyable { | 61 | KPageBitmap bitmap; |
| 61 | public: | ||
| 62 | static constexpr std::size_t MaxDepth{4}; | ||
| 63 | |||
| 64 | private: | ||
| 65 | std::array<u64*, MaxDepth> bit_storages{}; | ||
| 66 | std::size_t num_bits{}; | ||
| 67 | std::size_t used_depths{}; | ||
| 68 | |||
| 69 | public: | ||
| 70 | constexpr Bitmap() = default; | ||
| 71 | |||
| 72 | constexpr std::size_t GetNumBits() const { | ||
| 73 | return num_bits; | ||
| 74 | } | ||
| 75 | constexpr s32 GetHighestDepthIndex() const { | ||
| 76 | return static_cast<s32>(used_depths) - 1; | ||
| 77 | } | ||
| 78 | |||
| 79 | constexpr u64* Initialize(u64* storage, std::size_t size) { | ||
| 80 | //* Initially, everything is un-set | ||
| 81 | num_bits = 0; | ||
| 82 | |||
| 83 | // Calculate the needed bitmap depth | ||
| 84 | used_depths = static_cast<std::size_t>(GetRequiredDepth(size)); | ||
| 85 | ASSERT(used_depths <= MaxDepth); | ||
| 86 | |||
| 87 | // Set the bitmap pointers | ||
| 88 | for (s32 depth{GetHighestDepthIndex()}; depth >= 0; depth--) { | ||
| 89 | bit_storages[depth] = storage; | ||
| 90 | size = Common::AlignUp(size, 64) / 64; | ||
| 91 | storage += size; | ||
| 92 | } | ||
| 93 | |||
| 94 | return storage; | ||
| 95 | } | ||
| 96 | |||
| 97 | s64 FindFreeBlock() const { | ||
| 98 | uintptr_t offset{}; | ||
| 99 | s32 depth{}; | ||
| 100 | |||
| 101 | do { | ||
| 102 | const u64 v{bit_storages[depth][offset]}; | ||
| 103 | if (v == 0) { | ||
| 104 | // Non-zero depth indicates that a previous level had a free block | ||
| 105 | ASSERT(depth == 0); | ||
| 106 | return -1; | ||
| 107 | } | ||
| 108 | offset = offset * 64 + static_cast<u32>(std::countr_zero(v)); | ||
| 109 | ++depth; | ||
| 110 | } while (depth < static_cast<s32>(used_depths)); | ||
| 111 | |||
| 112 | return static_cast<s64>(offset); | ||
| 113 | } | ||
| 114 | |||
| 115 | constexpr void SetBit(std::size_t offset) { | ||
| 116 | SetBit(GetHighestDepthIndex(), offset); | ||
| 117 | num_bits++; | ||
| 118 | } | ||
| 119 | |||
| 120 | constexpr void ClearBit(std::size_t offset) { | ||
| 121 | ClearBit(GetHighestDepthIndex(), offset); | ||
| 122 | num_bits--; | ||
| 123 | } | ||
| 124 | |||
| 125 | constexpr bool ClearRange(std::size_t offset, std::size_t count) { | ||
| 126 | const s32 depth{GetHighestDepthIndex()}; | ||
| 127 | const auto bit_ind{offset / 64}; | ||
| 128 | u64* bits{bit_storages[depth]}; | ||
| 129 | if (count < 64) { | ||
| 130 | const auto shift{offset % 64}; | ||
| 131 | ASSERT(shift + count <= 64); | ||
| 132 | // Check that all the bits are set | ||
| 133 | const u64 mask{((1ULL << count) - 1) << shift}; | ||
| 134 | u64 v{bits[bit_ind]}; | ||
| 135 | if ((v & mask) != mask) { | ||
| 136 | return false; | ||
| 137 | } | ||
| 138 | |||
| 139 | // Clear the bits | ||
| 140 | v &= ~mask; | ||
| 141 | bits[bit_ind] = v; | ||
| 142 | if (v == 0) { | ||
| 143 | ClearBit(depth - 1, bit_ind); | ||
| 144 | } | ||
| 145 | } else { | ||
| 146 | ASSERT(offset % 64 == 0); | ||
| 147 | ASSERT(count % 64 == 0); | ||
| 148 | // Check that all the bits are set | ||
| 149 | std::size_t remaining{count}; | ||
| 150 | std::size_t i = 0; | ||
| 151 | do { | ||
| 152 | if (bits[bit_ind + i++] != ~u64(0)) { | ||
| 153 | return false; | ||
| 154 | } | ||
| 155 | remaining -= 64; | ||
| 156 | } while (remaining > 0); | ||
| 157 | |||
| 158 | // Clear the bits | ||
| 159 | remaining = count; | ||
| 160 | i = 0; | ||
| 161 | do { | ||
| 162 | bits[bit_ind + i] = 0; | ||
| 163 | ClearBit(depth - 1, bit_ind + i); | ||
| 164 | i++; | ||
| 165 | remaining -= 64; | ||
| 166 | } while (remaining > 0); | ||
| 167 | } | ||
| 168 | |||
| 169 | num_bits -= count; | ||
| 170 | return true; | ||
| 171 | } | ||
| 172 | |||
| 173 | private: | ||
| 174 | constexpr void SetBit(s32 depth, std::size_t offset) { | ||
| 175 | while (depth >= 0) { | ||
| 176 | const auto ind{offset / 64}; | ||
| 177 | const auto which{offset % 64}; | ||
| 178 | const u64 mask{1ULL << which}; | ||
| 179 | |||
| 180 | u64* bit{std::addressof(bit_storages[depth][ind])}; | ||
| 181 | const u64 v{*bit}; | ||
| 182 | ASSERT((v & mask) == 0); | ||
| 183 | *bit = v | mask; | ||
| 184 | if (v) { | ||
| 185 | break; | ||
| 186 | } | ||
| 187 | offset = ind; | ||
| 188 | depth--; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | constexpr void ClearBit(s32 depth, std::size_t offset) { | ||
| 193 | while (depth >= 0) { | ||
| 194 | const auto ind{offset / 64}; | ||
| 195 | const auto which{offset % 64}; | ||
| 196 | const u64 mask{1ULL << which}; | ||
| 197 | |||
| 198 | u64* bit{std::addressof(bit_storages[depth][ind])}; | ||
| 199 | u64 v{*bit}; | ||
| 200 | ASSERT((v & mask) != 0); | ||
| 201 | v &= ~mask; | ||
| 202 | *bit = v; | ||
| 203 | if (v) { | ||
| 204 | break; | ||
| 205 | } | ||
| 206 | offset = ind; | ||
| 207 | depth--; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | private: | ||
| 212 | static constexpr s32 GetRequiredDepth(std::size_t region_size) { | ||
| 213 | s32 depth = 0; | ||
| 214 | while (true) { | ||
| 215 | region_size /= 64; | ||
| 216 | depth++; | ||
| 217 | if (region_size == 0) { | ||
| 218 | return depth; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | public: | ||
| 224 | static constexpr std::size_t CalculateMetadataOverheadSize(std::size_t region_size) { | ||
| 225 | std::size_t overhead_bits = 0; | ||
| 226 | for (s32 depth{GetRequiredDepth(region_size) - 1}; depth >= 0; depth--) { | ||
| 227 | region_size = Common::AlignUp(region_size, 64) / 64; | ||
| 228 | overhead_bits += region_size; | ||
| 229 | } | ||
| 230 | return overhead_bits * sizeof(u64); | ||
| 231 | } | ||
| 232 | }; | ||
| 233 | |||
| 234 | private: | ||
| 235 | Bitmap bitmap; | ||
| 236 | VAddr heap_address{}; | 62 | VAddr heap_address{}; |
| 237 | uintptr_t end_offset{}; | 63 | uintptr_t end_offset{}; |
| 238 | std::size_t block_shift{}; | 64 | std::size_t block_shift{}; |
| 239 | std::size_t next_block_shift{}; | 65 | std::size_t next_block_shift{}; |
| 240 | 66 | ||
| 241 | public: | 67 | public: |
| 242 | constexpr Block() = default; | 68 | Block() = default; |
| 243 | 69 | ||
| 244 | constexpr std::size_t GetShift() const { | 70 | constexpr std::size_t GetShift() const { |
| 245 | return block_shift; | 71 | return block_shift; |
| @@ -260,8 +86,8 @@ private: | |||
| 260 | return GetNumFreeBlocks() * GetNumPages(); | 86 | return GetNumFreeBlocks() * GetNumPages(); |
| 261 | } | 87 | } |
| 262 | 88 | ||
| 263 | constexpr u64* Initialize(VAddr addr, std::size_t size, std::size_t bs, std::size_t nbs, | 89 | u64* Initialize(VAddr addr, std::size_t size, std::size_t bs, std::size_t nbs, |
| 264 | u64* bit_storage) { | 90 | u64* bit_storage) { |
| 265 | // Set shifts | 91 | // Set shifts |
| 266 | block_shift = bs; | 92 | block_shift = bs; |
| 267 | next_block_shift = nbs; | 93 | next_block_shift = nbs; |
| @@ -278,7 +104,7 @@ private: | |||
| 278 | return bitmap.Initialize(bit_storage, end_offset); | 104 | return bitmap.Initialize(bit_storage, end_offset); |
| 279 | } | 105 | } |
| 280 | 106 | ||
| 281 | constexpr VAddr PushBlock(VAddr address) { | 107 | VAddr PushBlock(VAddr address) { |
| 282 | // Set the bit for the free block | 108 | // Set the bit for the free block |
| 283 | std::size_t offset{(address - heap_address) >> GetShift()}; | 109 | std::size_t offset{(address - heap_address) >> GetShift()}; |
| 284 | bitmap.SetBit(offset); | 110 | bitmap.SetBit(offset); |
| @@ -296,9 +122,9 @@ private: | |||
| 296 | return 0; | 122 | return 0; |
| 297 | } | 123 | } |
| 298 | 124 | ||
| 299 | VAddr PopBlock() { | 125 | VAddr PopBlock(bool random) { |
| 300 | // Find a free block | 126 | // Find a free block |
| 301 | const s64 soffset{bitmap.FindFreeBlock()}; | 127 | const s64 soffset{bitmap.FindFreeBlock(random)}; |
| 302 | if (soffset < 0) { | 128 | if (soffset < 0) { |
| 303 | return 0; | 129 | return 0; |
| 304 | } | 130 | } |
| @@ -310,13 +136,13 @@ private: | |||
| 310 | } | 136 | } |
| 311 | 137 | ||
| 312 | public: | 138 | public: |
| 313 | static constexpr std::size_t CalculateMetadataOverheadSize(std::size_t region_size, | 139 | static constexpr std::size_t CalculateManagementOverheadSize(std::size_t region_size, |
| 314 | std::size_t cur_block_shift, | 140 | std::size_t cur_block_shift, |
| 315 | std::size_t next_block_shift) { | 141 | std::size_t next_block_shift) { |
| 316 | const auto cur_block_size{(1ULL << cur_block_shift)}; | 142 | const auto cur_block_size{(1ULL << cur_block_shift)}; |
| 317 | const auto next_block_size{(1ULL << next_block_shift)}; | 143 | const auto next_block_size{(1ULL << next_block_shift)}; |
| 318 | const auto align{(next_block_shift != 0) ? next_block_size : cur_block_size}; | 144 | const auto align{(next_block_shift != 0) ? next_block_size : cur_block_size}; |
| 319 | return Bitmap::CalculateMetadataOverheadSize( | 145 | return KPageBitmap::CalculateManagementOverheadSize( |
| 320 | (align * 2 + Common::AlignUp(region_size, align)) / cur_block_size); | 146 | (align * 2 + Common::AlignUp(region_size, align)) / cur_block_size); |
| 321 | } | 147 | } |
| 322 | }; | 148 | }; |
| @@ -338,14 +164,14 @@ public: | |||
| 338 | } | 164 | } |
| 339 | 165 | ||
| 340 | void Initialize(VAddr heap_address, std::size_t heap_size, std::size_t metadata_size); | 166 | void Initialize(VAddr heap_address, std::size_t heap_size, std::size_t metadata_size); |
| 341 | VAddr AllocateBlock(s32 index); | 167 | VAddr AllocateBlock(s32 index, bool random); |
| 342 | void Free(VAddr addr, std::size_t num_pages); | 168 | void Free(VAddr addr, std::size_t num_pages); |
| 343 | 169 | ||
| 344 | void UpdateUsedSize() { | 170 | void UpdateUsedSize() { |
| 345 | used_size = heap_size - (GetNumFreePages() * PageSize); | 171 | used_size = heap_size - (GetNumFreePages() * PageSize); |
| 346 | } | 172 | } |
| 347 | 173 | ||
| 348 | static std::size_t CalculateMetadataOverheadSize(std::size_t region_size); | 174 | static std::size_t CalculateManagementOverheadSize(std::size_t region_size); |
| 349 | 175 | ||
| 350 | private: | 176 | private: |
| 351 | constexpr std::size_t GetNumFreePages() const { | 177 | constexpr std::size_t GetNumFreePages() const { |