k_page_group.h 4.9 KB

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