k_page_group.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <list>
  5. #include "common/alignment.h"
  6. #include "common/assert.h"
  7. #include "common/common_types.h"
  8. #include "core/hle/kernel/memory_types.h"
  9. #include "core/hle/result.h"
  10. namespace Kernel {
  11. class KBlockInfoManager;
  12. class KernelCore;
  13. class KPageGroup;
  14. class KBlockInfo {
  15. public:
  16. constexpr explicit KBlockInfo() : m_next(nullptr) {}
  17. constexpr void Initialize(KPhysicalAddress addr, size_t np) {
  18. ASSERT(Common::IsAligned(addr, PageSize));
  19. ASSERT(static_cast<u32>(np) == np);
  20. m_page_index = static_cast<u32>(addr / PageSize);
  21. m_num_pages = static_cast<u32>(np);
  22. }
  23. constexpr KPhysicalAddress GetAddress() const {
  24. return m_page_index * PageSize;
  25. }
  26. constexpr size_t GetNumPages() const {
  27. return m_num_pages;
  28. }
  29. constexpr size_t GetSize() const {
  30. return this->GetNumPages() * PageSize;
  31. }
  32. constexpr KPhysicalAddress GetEndAddress() const {
  33. return (m_page_index + m_num_pages) * PageSize;
  34. }
  35. constexpr KPhysicalAddress GetLastAddress() const {
  36. return this->GetEndAddress() - 1;
  37. }
  38. constexpr KBlockInfo* GetNext() const {
  39. return m_next;
  40. }
  41. constexpr bool IsEquivalentTo(const KBlockInfo& rhs) const {
  42. return m_page_index == rhs.m_page_index && m_num_pages == rhs.m_num_pages;
  43. }
  44. constexpr bool operator==(const KBlockInfo& rhs) const {
  45. return this->IsEquivalentTo(rhs);
  46. }
  47. constexpr bool operator!=(const KBlockInfo& rhs) const {
  48. return !(*this == rhs);
  49. }
  50. constexpr bool IsStrictlyBefore(KPhysicalAddress addr) const {
  51. const KPhysicalAddress end = this->GetEndAddress();
  52. if (m_page_index != 0 && end == 0) {
  53. return false;
  54. }
  55. return end < addr;
  56. }
  57. constexpr bool operator<(KPhysicalAddress addr) const {
  58. return this->IsStrictlyBefore(addr);
  59. }
  60. constexpr bool TryConcatenate(KPhysicalAddress addr, size_t np) {
  61. if (addr != 0 && addr == this->GetEndAddress()) {
  62. m_num_pages += static_cast<u32>(np);
  63. return true;
  64. }
  65. return false;
  66. }
  67. private:
  68. constexpr void SetNext(KBlockInfo* next) {
  69. m_next = next;
  70. }
  71. private:
  72. friend class KPageGroup;
  73. KBlockInfo* m_next{};
  74. u32 m_page_index{};
  75. u32 m_num_pages{};
  76. };
  77. static_assert(sizeof(KBlockInfo) <= 0x10);
  78. class KPageGroup {
  79. public:
  80. class Iterator {
  81. public:
  82. using iterator_category = std::forward_iterator_tag;
  83. using value_type = const KBlockInfo;
  84. using difference_type = std::ptrdiff_t;
  85. using pointer = value_type*;
  86. using reference = value_type&;
  87. constexpr explicit Iterator(pointer n) : m_node(n) {}
  88. constexpr bool operator==(const Iterator& rhs) const {
  89. return m_node == rhs.m_node;
  90. }
  91. constexpr bool operator!=(const Iterator& rhs) const {
  92. return !(*this == rhs);
  93. }
  94. constexpr pointer operator->() const {
  95. return m_node;
  96. }
  97. constexpr reference operator*() const {
  98. return *m_node;
  99. }
  100. constexpr Iterator& operator++() {
  101. m_node = m_node->GetNext();
  102. return *this;
  103. }
  104. constexpr Iterator operator++(int) {
  105. const Iterator it{*this};
  106. ++(*this);
  107. return it;
  108. }
  109. private:
  110. pointer m_node{};
  111. };
  112. explicit KPageGroup(KernelCore& kernel, KBlockInfoManager* m)
  113. : m_kernel{kernel}, m_manager{m} {}
  114. ~KPageGroup() {
  115. this->Finalize();
  116. }
  117. void CloseAndReset();
  118. void Finalize();
  119. Iterator begin() const {
  120. return Iterator{m_first_block};
  121. }
  122. Iterator end() const {
  123. return Iterator{nullptr};
  124. }
  125. bool empty() const {
  126. return m_first_block == nullptr;
  127. }
  128. Result AddBlock(KPhysicalAddress addr, size_t num_pages);
  129. void Open() const;
  130. void OpenFirst() const;
  131. void Close() const;
  132. size_t GetNumPages() const;
  133. bool IsEquivalentTo(const KPageGroup& rhs) const;
  134. bool operator==(const KPageGroup& rhs) const {
  135. return this->IsEquivalentTo(rhs);
  136. }
  137. bool operator!=(const KPageGroup& rhs) const {
  138. return !(*this == rhs);
  139. }
  140. private:
  141. KernelCore& m_kernel;
  142. KBlockInfo* m_first_block{};
  143. KBlockInfo* m_last_block{};
  144. KBlockInfoManager* m_manager{};
  145. };
  146. class KScopedPageGroup {
  147. public:
  148. explicit KScopedPageGroup(const KPageGroup* gp) : m_pg(gp) {
  149. if (m_pg) {
  150. m_pg->Open();
  151. }
  152. }
  153. explicit KScopedPageGroup(const KPageGroup& gp) : KScopedPageGroup(std::addressof(gp)) {}
  154. ~KScopedPageGroup() {
  155. if (m_pg) {
  156. m_pg->Close();
  157. }
  158. }
  159. void CancelClose() {
  160. m_pg = nullptr;
  161. }
  162. private:
  163. const KPageGroup* m_pg{};
  164. };
  165. } // namespace Kernel