k_page_heap.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <vector>
  6. #include "common/alignment.h"
  7. #include "common/common_funcs.h"
  8. #include "common/common_types.h"
  9. #include "core/hle/kernel/k_page_bitmap.h"
  10. #include "core/hle/kernel/memory_types.h"
  11. namespace Kernel {
  12. class KPageHeap {
  13. public:
  14. KPageHeap() = default;
  15. constexpr PAddr GetAddress() const {
  16. return m_heap_address;
  17. }
  18. constexpr size_t GetSize() const {
  19. return m_heap_size;
  20. }
  21. constexpr PAddr GetEndAddress() const {
  22. return this->GetAddress() + this->GetSize();
  23. }
  24. constexpr size_t GetPageOffset(PAddr block) const {
  25. return (block - this->GetAddress()) / PageSize;
  26. }
  27. constexpr size_t GetPageOffsetToEnd(PAddr block) const {
  28. return (this->GetEndAddress() - block) / PageSize;
  29. }
  30. void Initialize(PAddr heap_address, size_t heap_size, VAddr management_address,
  31. size_t management_size) {
  32. return this->Initialize(heap_address, heap_size, management_address, management_size,
  33. MemoryBlockPageShifts.data(), NumMemoryBlockPageShifts);
  34. }
  35. size_t GetFreeSize() const {
  36. return this->GetNumFreePages() * PageSize;
  37. }
  38. void SetInitialUsedSize(size_t reserved_size) {
  39. // Check that the reserved size is valid.
  40. const size_t free_size = this->GetNumFreePages() * PageSize;
  41. ASSERT(m_heap_size >= free_size + reserved_size);
  42. // Set the initial used size.
  43. m_initial_used_size = m_heap_size - free_size - reserved_size;
  44. }
  45. PAddr AllocateBlock(s32 index, bool random) {
  46. if (random) {
  47. const size_t block_pages = m_blocks[index].GetNumPages();
  48. return this->AllocateByRandom(index, block_pages, block_pages);
  49. } else {
  50. return this->AllocateByLinearSearch(index);
  51. }
  52. }
  53. PAddr AllocateAligned(s32 index, size_t num_pages, size_t align_pages) {
  54. // TODO: linear search support?
  55. return this->AllocateByRandom(index, num_pages, align_pages);
  56. }
  57. void Free(PAddr addr, size_t num_pages);
  58. static size_t CalculateManagementOverheadSize(size_t region_size) {
  59. return CalculateManagementOverheadSize(region_size, MemoryBlockPageShifts.data(),
  60. NumMemoryBlockPageShifts);
  61. }
  62. static constexpr s32 GetAlignedBlockIndex(size_t num_pages, size_t align_pages) {
  63. const size_t target_pages = std::max(num_pages, align_pages);
  64. for (size_t i = 0; i < NumMemoryBlockPageShifts; i++) {
  65. if (target_pages <= (static_cast<size_t>(1) << MemoryBlockPageShifts[i]) / PageSize) {
  66. return static_cast<s32>(i);
  67. }
  68. }
  69. return -1;
  70. }
  71. static constexpr s32 GetBlockIndex(size_t num_pages) {
  72. for (s32 i = static_cast<s32>(NumMemoryBlockPageShifts) - 1; i >= 0; i--) {
  73. if (num_pages >= (static_cast<size_t>(1) << MemoryBlockPageShifts[i]) / PageSize) {
  74. return i;
  75. }
  76. }
  77. return -1;
  78. }
  79. static constexpr size_t GetBlockSize(size_t index) {
  80. return static_cast<size_t>(1) << MemoryBlockPageShifts[index];
  81. }
  82. static constexpr size_t GetBlockNumPages(size_t index) {
  83. return GetBlockSize(index) / PageSize;
  84. }
  85. private:
  86. class Block {
  87. public:
  88. Block() = default;
  89. constexpr size_t GetShift() const {
  90. return m_block_shift;
  91. }
  92. constexpr size_t GetNextShift() const {
  93. return m_next_block_shift;
  94. }
  95. constexpr size_t GetSize() const {
  96. return u64(1) << this->GetShift();
  97. }
  98. constexpr size_t GetNumPages() const {
  99. return this->GetSize() / PageSize;
  100. }
  101. constexpr size_t GetNumFreeBlocks() const {
  102. return m_bitmap.GetNumBits();
  103. }
  104. constexpr size_t GetNumFreePages() const {
  105. return this->GetNumFreeBlocks() * this->GetNumPages();
  106. }
  107. u64* Initialize(PAddr addr, size_t size, size_t bs, size_t nbs, u64* bit_storage) {
  108. // Set shifts.
  109. m_block_shift = bs;
  110. m_next_block_shift = nbs;
  111. // Align up the address.
  112. PAddr end = addr + size;
  113. const size_t align = (m_next_block_shift != 0) ? (u64(1) << m_next_block_shift)
  114. : (u64(1) << m_block_shift);
  115. addr = Common::AlignDown(addr, align);
  116. end = Common::AlignUp(end, align);
  117. m_heap_address = addr;
  118. m_end_offset = (end - addr) / (u64(1) << m_block_shift);
  119. return m_bitmap.Initialize(bit_storage, m_end_offset);
  120. }
  121. PAddr PushBlock(PAddr address) {
  122. // Set the bit for the free block.
  123. size_t offset = (address - m_heap_address) >> this->GetShift();
  124. m_bitmap.SetBit(offset);
  125. // If we have a next shift, try to clear the blocks below this one and return the new
  126. // address.
  127. if (this->GetNextShift()) {
  128. const size_t diff = u64(1) << (this->GetNextShift() - this->GetShift());
  129. offset = Common::AlignDown(offset, diff);
  130. if (m_bitmap.ClearRange(offset, diff)) {
  131. return m_heap_address + (offset << this->GetShift());
  132. }
  133. }
  134. // We couldn't coalesce, or we're already as big as possible.
  135. return {};
  136. }
  137. PAddr PopBlock(bool random) {
  138. // Find a free block.
  139. s64 soffset = m_bitmap.FindFreeBlock(random);
  140. if (soffset < 0) {
  141. return {};
  142. }
  143. const size_t offset = static_cast<size_t>(soffset);
  144. // Update our tracking and return it.
  145. m_bitmap.ClearBit(offset);
  146. return m_heap_address + (offset << this->GetShift());
  147. }
  148. public:
  149. static constexpr size_t CalculateManagementOverheadSize(size_t region_size,
  150. size_t cur_block_shift,
  151. size_t next_block_shift) {
  152. const size_t cur_block_size = (u64(1) << cur_block_shift);
  153. const size_t next_block_size = (u64(1) << next_block_shift);
  154. const size_t align = (next_block_shift != 0) ? next_block_size : cur_block_size;
  155. return KPageBitmap::CalculateManagementOverheadSize(
  156. (align * 2 + Common::AlignUp(region_size, align)) / cur_block_size);
  157. }
  158. private:
  159. KPageBitmap m_bitmap;
  160. PAddr m_heap_address{};
  161. uintptr_t m_end_offset{};
  162. size_t m_block_shift{};
  163. size_t m_next_block_shift{};
  164. };
  165. private:
  166. void Initialize(PAddr heap_address, size_t heap_size, VAddr management_address,
  167. size_t management_size, const size_t* block_shifts, size_t num_block_shifts);
  168. size_t GetNumFreePages() const;
  169. void FreeBlock(PAddr block, s32 index);
  170. static constexpr size_t NumMemoryBlockPageShifts{7};
  171. static constexpr std::array<size_t, NumMemoryBlockPageShifts> MemoryBlockPageShifts{
  172. 0xC, 0x10, 0x15, 0x16, 0x19, 0x1D, 0x1E,
  173. };
  174. private:
  175. PAddr AllocateByLinearSearch(s32 index);
  176. PAddr AllocateByRandom(s32 index, size_t num_pages, size_t align_pages);
  177. static size_t CalculateManagementOverheadSize(size_t region_size, const size_t* block_shifts,
  178. size_t num_block_shifts);
  179. private:
  180. PAddr m_heap_address{};
  181. size_t m_heap_size{};
  182. size_t m_initial_used_size{};
  183. size_t m_num_blocks{};
  184. std::array<Block, NumMemoryBlockPageShifts> m_blocks;
  185. KPageBitmap::RandomBitGenerator m_rng;
  186. std::vector<u64> m_management_data;
  187. };
  188. } // namespace Kernel