k_handle_table.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright 2021 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include "common/assert.h"
  7. #include "common/bit_field.h"
  8. #include "common/common_types.h"
  9. #include "core/hle/kernel/k_auto_object.h"
  10. #include "core/hle/kernel/k_spin_lock.h"
  11. #include "core/hle/kernel/k_thread.h"
  12. #include "core/hle/kernel/kernel.h"
  13. #include "core/hle/kernel/svc_common.h"
  14. #include "core/hle/kernel/svc_results.h"
  15. #include "core/hle/result.h"
  16. namespace Kernel {
  17. class KernelCore;
  18. class KHandleTable {
  19. YUZU_NON_COPYABLE(KHandleTable);
  20. YUZU_NON_MOVEABLE(KHandleTable);
  21. public:
  22. static constexpr size_t MaxTableSize = 1024;
  23. public:
  24. explicit KHandleTable(KernelCore& kernel_);
  25. ~KHandleTable();
  26. ResultCode Initialize(s32 size) {
  27. R_UNLESS(size <= static_cast<s32>(MaxTableSize), ResultOutOfMemory);
  28. // Initialize all fields.
  29. m_max_count = 0;
  30. m_table_size = static_cast<u16>((size <= 0) ? MaxTableSize : size);
  31. m_next_linear_id = MinLinearId;
  32. m_count = 0;
  33. m_free_head_index = -1;
  34. // Free all entries.
  35. for (s32 i = 0; i < static_cast<s32>(m_table_size); ++i) {
  36. m_objects[i] = nullptr;
  37. m_entry_infos[i].next_free_index = i - 1;
  38. m_free_head_index = i;
  39. }
  40. return ResultSuccess;
  41. }
  42. size_t GetTableSize() const {
  43. return m_table_size;
  44. }
  45. size_t GetCount() const {
  46. return m_count;
  47. }
  48. size_t GetMaxCount() const {
  49. return m_max_count;
  50. }
  51. ResultCode Finalize();
  52. bool Remove(Handle handle);
  53. template <typename T = KAutoObject>
  54. KScopedAutoObject<T> GetObjectWithoutPseudoHandle(Handle handle) const {
  55. // Lock and look up in table.
  56. KScopedSpinLock lk(m_lock);
  57. if constexpr (std::is_same_v<T, KAutoObject>) {
  58. return this->GetObjectImpl(handle);
  59. } else {
  60. if (auto* obj = this->GetObjectImpl(handle); obj != nullptr) {
  61. return obj->DynamicCast<T*>();
  62. } else {
  63. return nullptr;
  64. }
  65. }
  66. }
  67. template <typename T = KAutoObject>
  68. KScopedAutoObject<T> GetObject(Handle handle) const {
  69. // Handle pseudo-handles.
  70. if constexpr (std::derived_from<KProcess, T>) {
  71. if (handle == Svc::PseudoHandle::CurrentProcess) {
  72. auto* const cur_process = kernel.CurrentProcess();
  73. ASSERT(cur_process != nullptr);
  74. return cur_process;
  75. }
  76. } else if constexpr (std::derived_from<KThread, T>) {
  77. if (handle == Svc::PseudoHandle::CurrentThread) {
  78. auto* const cur_thread = GetCurrentThreadPointer(kernel);
  79. ASSERT(cur_thread != nullptr);
  80. return cur_thread;
  81. }
  82. }
  83. return this->template GetObjectWithoutPseudoHandle<T>(handle);
  84. }
  85. ResultCode Reserve(Handle* out_handle);
  86. void Unreserve(Handle handle);
  87. template <typename T>
  88. ResultCode Add(Handle* out_handle, T* obj) {
  89. static_assert(std::is_base_of_v<KAutoObject, T>);
  90. return this->Add(out_handle, obj, obj->GetTypeObj().GetClassToken());
  91. }
  92. template <typename T>
  93. void Register(Handle handle, T* obj) {
  94. static_assert(std::is_base_of_v<KAutoObject, T>);
  95. return this->Register(handle, obj, obj->GetTypeObj().GetClassToken());
  96. }
  97. template <typename T>
  98. bool GetMultipleObjects(T** out, const Handle* handles, size_t num_handles) const {
  99. // Try to convert and open all the handles.
  100. size_t num_opened;
  101. {
  102. // Lock the table.
  103. KScopedSpinLock lk(m_lock);
  104. for (num_opened = 0; num_opened < num_handles; num_opened++) {
  105. // Get the current handle.
  106. const auto cur_handle = handles[num_opened];
  107. // Get the object for the current handle.
  108. KAutoObject* cur_object = this->GetObjectImpl(cur_handle);
  109. if (cur_object == nullptr) {
  110. break;
  111. }
  112. // Cast the current object to the desired type.
  113. T* cur_t = cur_object->DynamicCast<T*>();
  114. if (cur_t == nullptr) {
  115. break;
  116. }
  117. // Open a reference to the current object.
  118. cur_t->Open();
  119. out[num_opened] = cur_t;
  120. }
  121. }
  122. // If we converted every object, succeed.
  123. if (num_opened == num_handles) {
  124. return true;
  125. }
  126. // If we didn't convert entry object, close the ones we opened.
  127. for (size_t i = 0; i < num_opened; i++) {
  128. out[i]->Close();
  129. }
  130. return false;
  131. }
  132. private:
  133. ResultCode Add(Handle* out_handle, KAutoObject* obj, u16 type);
  134. void Register(Handle handle, KAutoObject* obj, u16 type);
  135. s32 AllocateEntry() {
  136. ASSERT(m_count < m_table_size);
  137. const auto index = m_free_head_index;
  138. m_free_head_index = m_entry_infos[index].GetNextFreeIndex();
  139. m_max_count = std::max(m_max_count, ++m_count);
  140. return index;
  141. }
  142. void FreeEntry(s32 index) {
  143. ASSERT(m_count > 0);
  144. m_objects[index] = nullptr;
  145. m_entry_infos[index].next_free_index = m_free_head_index;
  146. m_free_head_index = index;
  147. --m_count;
  148. }
  149. u16 AllocateLinearId() {
  150. const u16 id = m_next_linear_id++;
  151. if (m_next_linear_id > MaxLinearId) {
  152. m_next_linear_id = MinLinearId;
  153. }
  154. return id;
  155. }
  156. bool IsValidHandle(Handle handle) const {
  157. // Unpack the handle.
  158. const auto handle_pack = HandlePack(handle);
  159. const auto raw_value = handle_pack.raw;
  160. const auto index = handle_pack.index;
  161. const auto linear_id = handle_pack.linear_id;
  162. const auto reserved = handle_pack.reserved;
  163. ASSERT(reserved == 0);
  164. // Validate our indexing information.
  165. if (raw_value == 0) {
  166. return false;
  167. }
  168. if (linear_id == 0) {
  169. return false;
  170. }
  171. if (index >= m_table_size) {
  172. return false;
  173. }
  174. // Check that there's an object, and our serial id is correct.
  175. if (m_objects[index] == nullptr) {
  176. return false;
  177. }
  178. if (m_entry_infos[index].GetLinearId() != linear_id) {
  179. return false;
  180. }
  181. return true;
  182. }
  183. KAutoObject* GetObjectImpl(Handle handle) const {
  184. // Handles must not have reserved bits set.
  185. const auto handle_pack = HandlePack(handle);
  186. if (handle_pack.reserved != 0) {
  187. return nullptr;
  188. }
  189. if (this->IsValidHandle(handle)) {
  190. return m_objects[handle_pack.index];
  191. } else {
  192. return nullptr;
  193. }
  194. }
  195. KAutoObject* GetObjectByIndexImpl(Handle* out_handle, size_t index) const {
  196. // Index must be in bounds.
  197. if (index >= m_table_size) {
  198. return nullptr;
  199. }
  200. // Ensure entry has an object.
  201. if (KAutoObject* obj = m_objects[index]; obj != nullptr) {
  202. *out_handle = EncodeHandle(static_cast<u16>(index), m_entry_infos[index].GetLinearId());
  203. return obj;
  204. } else {
  205. return nullptr;
  206. }
  207. }
  208. private:
  209. union HandlePack {
  210. HandlePack() = default;
  211. HandlePack(Handle handle) : raw{static_cast<u32>(handle)} {}
  212. u32 raw;
  213. BitField<0, 15, u32> index;
  214. BitField<15, 15, u32> linear_id;
  215. BitField<30, 2, u32> reserved;
  216. };
  217. static constexpr u16 MinLinearId = 1;
  218. static constexpr u16 MaxLinearId = 0x7FFF;
  219. static constexpr Handle EncodeHandle(u16 index, u16 linear_id) {
  220. HandlePack handle{};
  221. handle.index.Assign(index);
  222. handle.linear_id.Assign(linear_id);
  223. handle.reserved.Assign(0);
  224. return handle.raw;
  225. }
  226. union EntryInfo {
  227. struct {
  228. u16 linear_id;
  229. u16 type;
  230. } info;
  231. s32 next_free_index;
  232. constexpr u16 GetLinearId() const {
  233. return info.linear_id;
  234. }
  235. constexpr u16 GetType() const {
  236. return info.type;
  237. }
  238. constexpr s32 GetNextFreeIndex() const {
  239. return next_free_index;
  240. }
  241. };
  242. private:
  243. std::array<EntryInfo, MaxTableSize> m_entry_infos{};
  244. std::array<KAutoObject*, MaxTableSize> m_objects{};
  245. s32 m_free_head_index{-1};
  246. u16 m_table_size{};
  247. u16 m_max_count{};
  248. u16 m_next_linear_id{MinLinearId};
  249. u16 m_count{};
  250. mutable KSpinLock m_lock;
  251. KernelCore& kernel;
  252. };
  253. } // namespace Kernel