k_handle_table.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. public:
  20. YUZU_NON_COPYABLE(KHandleTable);
  21. YUZU_NON_MOVEABLE(KHandleTable);
  22. static constexpr size_t MaxTableSize = 1024;
  23. explicit KHandleTable(KernelCore& kernel_);
  24. ~KHandleTable();
  25. ResultCode Initialize(s32 size) {
  26. R_UNLESS(size <= static_cast<s32>(MaxTableSize), ResultOutOfMemory);
  27. // Initialize all fields.
  28. m_max_count = 0;
  29. m_table_size = static_cast<u16>((size <= 0) ? MaxTableSize : size);
  30. m_next_linear_id = MinLinearId;
  31. m_count = 0;
  32. m_free_head_index = -1;
  33. // Free all entries.
  34. for (s32 i = 0; i < static_cast<s32>(m_table_size); ++i) {
  35. m_objects[i] = nullptr;
  36. m_entry_infos[i].next_free_index = i - 1;
  37. m_free_head_index = i;
  38. }
  39. return ResultSuccess;
  40. }
  41. size_t GetTableSize() const {
  42. return m_table_size;
  43. }
  44. size_t GetCount() const {
  45. return m_count;
  46. }
  47. size_t GetMaxCount() const {
  48. return m_max_count;
  49. }
  50. ResultCode Finalize();
  51. bool Remove(Handle handle);
  52. template <typename T = KAutoObject>
  53. KScopedAutoObject<T> GetObjectWithoutPseudoHandle(Handle handle) const {
  54. // Lock and look up in table.
  55. KScopedDisableDispatch dd(kernel);
  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. KScopedDisableDispatch dd(kernel);
  104. KScopedSpinLock lk(m_lock);
  105. for (num_opened = 0; num_opened < num_handles; num_opened++) {
  106. // Get the current handle.
  107. const auto cur_handle = handles[num_opened];
  108. // Get the object for the current handle.
  109. KAutoObject* cur_object = this->GetObjectImpl(cur_handle);
  110. if (cur_object == nullptr) {
  111. break;
  112. }
  113. // Cast the current object to the desired type.
  114. T* cur_t = cur_object->DynamicCast<T*>();
  115. if (cur_t == nullptr) {
  116. break;
  117. }
  118. // Open a reference to the current object.
  119. cur_t->Open();
  120. out[num_opened] = cur_t;
  121. }
  122. }
  123. // If we converted every object, succeed.
  124. if (num_opened == num_handles) {
  125. return true;
  126. }
  127. // If we didn't convert entry object, close the ones we opened.
  128. for (size_t i = 0; i < num_opened; i++) {
  129. out[i]->Close();
  130. }
  131. return false;
  132. }
  133. private:
  134. ResultCode Add(Handle* out_handle, KAutoObject* obj, u16 type);
  135. void Register(Handle handle, KAutoObject* obj, u16 type);
  136. s32 AllocateEntry() {
  137. ASSERT(m_count < m_table_size);
  138. const auto index = m_free_head_index;
  139. m_free_head_index = m_entry_infos[index].GetNextFreeIndex();
  140. m_max_count = std::max(m_max_count, ++m_count);
  141. return index;
  142. }
  143. void FreeEntry(s32 index) {
  144. ASSERT(m_count > 0);
  145. m_objects[index] = nullptr;
  146. m_entry_infos[index].next_free_index = m_free_head_index;
  147. m_free_head_index = index;
  148. --m_count;
  149. }
  150. u16 AllocateLinearId() {
  151. const u16 id = m_next_linear_id++;
  152. if (m_next_linear_id > MaxLinearId) {
  153. m_next_linear_id = MinLinearId;
  154. }
  155. return id;
  156. }
  157. bool IsValidHandle(Handle handle) const {
  158. // Unpack the handle.
  159. const auto handle_pack = HandlePack(handle);
  160. const auto raw_value = handle_pack.raw;
  161. const auto index = handle_pack.index;
  162. const auto linear_id = handle_pack.linear_id;
  163. const auto reserved = handle_pack.reserved;
  164. ASSERT(reserved == 0);
  165. // Validate our indexing information.
  166. if (raw_value == 0) {
  167. return false;
  168. }
  169. if (linear_id == 0) {
  170. return false;
  171. }
  172. if (index >= m_table_size) {
  173. return false;
  174. }
  175. // Check that there's an object, and our serial id is correct.
  176. if (m_objects[index] == nullptr) {
  177. return false;
  178. }
  179. if (m_entry_infos[index].GetLinearId() != linear_id) {
  180. return false;
  181. }
  182. return true;
  183. }
  184. KAutoObject* GetObjectImpl(Handle handle) const {
  185. // Handles must not have reserved bits set.
  186. const auto handle_pack = HandlePack(handle);
  187. if (handle_pack.reserved != 0) {
  188. return nullptr;
  189. }
  190. if (this->IsValidHandle(handle)) {
  191. return m_objects[handle_pack.index];
  192. } else {
  193. return nullptr;
  194. }
  195. }
  196. KAutoObject* GetObjectByIndexImpl(Handle* out_handle, size_t index) const {
  197. // Index must be in bounds.
  198. if (index >= m_table_size) {
  199. return nullptr;
  200. }
  201. // Ensure entry has an object.
  202. if (KAutoObject* obj = m_objects[index]; obj != nullptr) {
  203. *out_handle = EncodeHandle(static_cast<u16>(index), m_entry_infos[index].GetLinearId());
  204. return obj;
  205. } else {
  206. return nullptr;
  207. }
  208. }
  209. private:
  210. union HandlePack {
  211. HandlePack() = default;
  212. HandlePack(Handle handle) : raw{static_cast<u32>(handle)} {}
  213. u32 raw;
  214. BitField<0, 15, u32> index;
  215. BitField<15, 15, u32> linear_id;
  216. BitField<30, 2, u32> reserved;
  217. };
  218. static constexpr u16 MinLinearId = 1;
  219. static constexpr u16 MaxLinearId = 0x7FFF;
  220. static constexpr Handle EncodeHandle(u16 index, u16 linear_id) {
  221. HandlePack handle{};
  222. handle.index.Assign(index);
  223. handle.linear_id.Assign(linear_id);
  224. handle.reserved.Assign(0);
  225. return handle.raw;
  226. }
  227. union EntryInfo {
  228. struct {
  229. u16 linear_id;
  230. u16 type;
  231. } info;
  232. s32 next_free_index;
  233. constexpr u16 GetLinearId() const {
  234. return info.linear_id;
  235. }
  236. constexpr u16 GetType() const {
  237. return info.type;
  238. }
  239. constexpr s32 GetNextFreeIndex() const {
  240. return next_free_index;
  241. }
  242. };
  243. private:
  244. std::array<EntryInfo, MaxTableSize> m_entry_infos{};
  245. std::array<KAutoObject*, MaxTableSize> m_objects{};
  246. s32 m_free_head_index{-1};
  247. u16 m_table_size{};
  248. u16 m_max_count{};
  249. u16 m_next_linear_id{MinLinearId};
  250. u16 m_count{};
  251. mutable KSpinLock m_lock;
  252. KernelCore& kernel;
  253. };
  254. } // namespace Kernel