k_page_bitmap.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <bit>
  7. #include "common/alignment.h"
  8. #include "common/assert.h"
  9. #include "common/bit_util.h"
  10. #include "common/common_types.h"
  11. #include "common/tiny_mt.h"
  12. #include "core/hle/kernel/k_system_control.h"
  13. namespace Kernel {
  14. class KPageBitmap {
  15. private:
  16. class RandomBitGenerator {
  17. private:
  18. Common::TinyMT rng{};
  19. u32 entropy{};
  20. u32 bits_available{};
  21. private:
  22. void RefreshEntropy() {
  23. entropy = rng.GenerateRandomU32();
  24. bits_available = static_cast<u32>(Common::BitSize<decltype(entropy)>());
  25. }
  26. bool GenerateRandomBit() {
  27. if (bits_available == 0) {
  28. this->RefreshEntropy();
  29. }
  30. const bool rnd_bit = (entropy & 1) != 0;
  31. entropy >>= 1;
  32. --bits_available;
  33. return rnd_bit;
  34. }
  35. public:
  36. RandomBitGenerator() {
  37. rng.Initialize(static_cast<u32>(KSystemControl::GenerateRandomU64()));
  38. }
  39. std::size_t SelectRandomBit(u64 bitmap) {
  40. u64 selected = 0;
  41. u64 cur_num_bits = Common::BitSize<decltype(bitmap)>() / 2;
  42. u64 cur_mask = (1ULL << cur_num_bits) - 1;
  43. while (cur_num_bits) {
  44. const u64 low = (bitmap >> 0) & cur_mask;
  45. const u64 high = (bitmap >> cur_num_bits) & cur_mask;
  46. bool choose_low;
  47. if (high == 0) {
  48. // If only low val is set, choose low.
  49. choose_low = true;
  50. } else if (low == 0) {
  51. // If only high val is set, choose high.
  52. choose_low = false;
  53. } else {
  54. // If both are set, choose random.
  55. choose_low = this->GenerateRandomBit();
  56. }
  57. // If we chose low, proceed with low.
  58. if (choose_low) {
  59. bitmap = low;
  60. selected += 0;
  61. } else {
  62. bitmap = high;
  63. selected += cur_num_bits;
  64. }
  65. // Proceed.
  66. cur_num_bits /= 2;
  67. cur_mask >>= cur_num_bits;
  68. }
  69. return selected;
  70. }
  71. };
  72. public:
  73. static constexpr std::size_t MaxDepth = 4;
  74. private:
  75. std::array<u64*, MaxDepth> bit_storages{};
  76. RandomBitGenerator rng{};
  77. std::size_t num_bits{};
  78. std::size_t used_depths{};
  79. public:
  80. KPageBitmap() = default;
  81. constexpr std::size_t GetNumBits() const {
  82. return num_bits;
  83. }
  84. constexpr s32 GetHighestDepthIndex() const {
  85. return static_cast<s32>(used_depths) - 1;
  86. }
  87. u64* Initialize(u64* storage, std::size_t size) {
  88. // Initially, everything is un-set.
  89. num_bits = 0;
  90. // Calculate the needed bitmap depth.
  91. used_depths = static_cast<std::size_t>(GetRequiredDepth(size));
  92. ASSERT(used_depths <= MaxDepth);
  93. // Set the bitmap pointers.
  94. for (s32 depth = this->GetHighestDepthIndex(); depth >= 0; depth--) {
  95. bit_storages[depth] = storage;
  96. size = Common::AlignUp(size, Common::BitSize<u64>()) / Common::BitSize<u64>();
  97. storage += size;
  98. }
  99. return storage;
  100. }
  101. s64 FindFreeBlock(bool random) {
  102. uintptr_t offset = 0;
  103. s32 depth = 0;
  104. if (random) {
  105. do {
  106. const u64 v = bit_storages[depth][offset];
  107. if (v == 0) {
  108. // If depth is bigger than zero, then a previous level indicated a block was
  109. // free.
  110. ASSERT(depth == 0);
  111. return -1;
  112. }
  113. offset = offset * Common::BitSize<u64>() + rng.SelectRandomBit(v);
  114. ++depth;
  115. } while (depth < static_cast<s32>(used_depths));
  116. } else {
  117. do {
  118. const u64 v = bit_storages[depth][offset];
  119. if (v == 0) {
  120. // If depth is bigger than zero, then a previous level indicated a block was
  121. // free.
  122. ASSERT(depth == 0);
  123. return -1;
  124. }
  125. offset = offset * Common::BitSize<u64>() + std::countr_zero(v);
  126. ++depth;
  127. } while (depth < static_cast<s32>(used_depths));
  128. }
  129. return static_cast<s64>(offset);
  130. }
  131. void SetBit(std::size_t offset) {
  132. this->SetBit(this->GetHighestDepthIndex(), offset);
  133. num_bits++;
  134. }
  135. void ClearBit(std::size_t offset) {
  136. this->ClearBit(this->GetHighestDepthIndex(), offset);
  137. num_bits--;
  138. }
  139. bool ClearRange(std::size_t offset, std::size_t count) {
  140. s32 depth = this->GetHighestDepthIndex();
  141. u64* bits = bit_storages[depth];
  142. std::size_t bit_ind = offset / Common::BitSize<u64>();
  143. if (count < Common::BitSize<u64>()) {
  144. const std::size_t shift = offset % Common::BitSize<u64>();
  145. ASSERT(shift + count <= Common::BitSize<u64>());
  146. // Check that all the bits are set.
  147. const u64 mask = ((u64(1) << count) - 1) << shift;
  148. u64 v = bits[bit_ind];
  149. if ((v & mask) != mask) {
  150. return false;
  151. }
  152. // Clear the bits.
  153. v &= ~mask;
  154. bits[bit_ind] = v;
  155. if (v == 0) {
  156. this->ClearBit(depth - 1, bit_ind);
  157. }
  158. } else {
  159. ASSERT(offset % Common::BitSize<u64>() == 0);
  160. ASSERT(count % Common::BitSize<u64>() == 0);
  161. // Check that all the bits are set.
  162. std::size_t remaining = count;
  163. std::size_t i = 0;
  164. do {
  165. if (bits[bit_ind + i++] != ~u64(0)) {
  166. return false;
  167. }
  168. remaining -= Common::BitSize<u64>();
  169. } while (remaining > 0);
  170. // Clear the bits.
  171. remaining = count;
  172. i = 0;
  173. do {
  174. bits[bit_ind + i] = 0;
  175. this->ClearBit(depth - 1, bit_ind + i);
  176. i++;
  177. remaining -= Common::BitSize<u64>();
  178. } while (remaining > 0);
  179. }
  180. num_bits -= count;
  181. return true;
  182. }
  183. private:
  184. void SetBit(s32 depth, std::size_t offset) {
  185. while (depth >= 0) {
  186. std::size_t ind = offset / Common::BitSize<u64>();
  187. std::size_t which = offset % Common::BitSize<u64>();
  188. const u64 mask = u64(1) << which;
  189. u64* bit = std::addressof(bit_storages[depth][ind]);
  190. u64 v = *bit;
  191. ASSERT((v & mask) == 0);
  192. *bit = v | mask;
  193. if (v) {
  194. break;
  195. }
  196. offset = ind;
  197. depth--;
  198. }
  199. }
  200. void ClearBit(s32 depth, std::size_t offset) {
  201. while (depth >= 0) {
  202. std::size_t ind = offset / Common::BitSize<u64>();
  203. std::size_t which = offset % Common::BitSize<u64>();
  204. const u64 mask = u64(1) << which;
  205. u64* bit = std::addressof(bit_storages[depth][ind]);
  206. u64 v = *bit;
  207. ASSERT((v & mask) != 0);
  208. v &= ~mask;
  209. *bit = v;
  210. if (v) {
  211. break;
  212. }
  213. offset = ind;
  214. depth--;
  215. }
  216. }
  217. private:
  218. static constexpr s32 GetRequiredDepth(std::size_t region_size) {
  219. s32 depth = 0;
  220. while (true) {
  221. region_size /= Common::BitSize<u64>();
  222. depth++;
  223. if (region_size == 0) {
  224. return depth;
  225. }
  226. }
  227. }
  228. public:
  229. static constexpr std::size_t CalculateManagementOverheadSize(std::size_t region_size) {
  230. std::size_t overhead_bits = 0;
  231. for (s32 depth = GetRequiredDepth(region_size) - 1; depth >= 0; depth--) {
  232. region_size =
  233. Common::AlignUp(region_size, Common::BitSize<u64>()) / Common::BitSize<u64>();
  234. overhead_bits += region_size;
  235. }
  236. return overhead_bits * sizeof(u64);
  237. }
  238. };
  239. } // namespace Kernel