k_handle_table.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "core/hle/kernel/k_handle_table.h"
  4. #include "core/hle/kernel/k_process.h"
  5. namespace Kernel {
  6. Result KHandleTable::Finalize() {
  7. // Get the table and clear our record of it.
  8. u16 saved_table_size = 0;
  9. {
  10. KScopedDisableDispatch dd{m_kernel};
  11. KScopedSpinLock lk(m_lock);
  12. std::swap(m_table_size, saved_table_size);
  13. }
  14. // Close and free all entries.
  15. for (size_t i = 0; i < saved_table_size; i++) {
  16. if (KAutoObject* obj = m_objects[i]; obj != nullptr) {
  17. obj->Close();
  18. }
  19. }
  20. R_SUCCEED();
  21. }
  22. bool KHandleTable::Remove(Handle handle) {
  23. // Don't allow removal of a pseudo-handle.
  24. if (Svc::IsPseudoHandle(handle)) [[unlikely]] {
  25. return false;
  26. }
  27. // Handles must not have reserved bits set.
  28. const auto handle_pack = HandlePack(handle);
  29. if (handle_pack.reserved != 0) [[unlikely]] {
  30. return false;
  31. }
  32. // Find the object and free the entry.
  33. KAutoObject* obj = nullptr;
  34. {
  35. KScopedDisableDispatch dd{m_kernel};
  36. KScopedSpinLock lk(m_lock);
  37. if (this->IsValidHandle(handle)) [[likely]] {
  38. const auto index = handle_pack.index;
  39. obj = m_objects[index];
  40. this->FreeEntry(index);
  41. } else {
  42. return false;
  43. }
  44. }
  45. // Close the object.
  46. m_kernel.UnregisterInUseObject(obj);
  47. obj->Close();
  48. return true;
  49. }
  50. Result KHandleTable::Add(Handle* out_handle, KAutoObject* obj) {
  51. KScopedDisableDispatch dd{m_kernel};
  52. KScopedSpinLock lk(m_lock);
  53. // Never exceed our capacity.
  54. R_UNLESS(m_count < m_table_size, ResultOutOfHandles);
  55. // Allocate entry, set output handle.
  56. {
  57. const auto linear_id = this->AllocateLinearId();
  58. const auto index = this->AllocateEntry();
  59. m_entry_infos[index].linear_id = linear_id;
  60. m_objects[index] = obj;
  61. obj->Open();
  62. *out_handle = EncodeHandle(static_cast<u16>(index), linear_id);
  63. }
  64. R_SUCCEED();
  65. }
  66. KScopedAutoObject<KAutoObject> KHandleTable::GetObjectForIpc(Handle handle,
  67. KThread* cur_thread) const {
  68. // Handle pseudo-handles.
  69. ASSERT(cur_thread != nullptr);
  70. if (handle == Svc::PseudoHandle::CurrentProcess) {
  71. auto* const cur_process = cur_thread->GetOwnerProcess();
  72. ASSERT(cur_process != nullptr);
  73. return cur_process;
  74. }
  75. if (handle == Svc::PseudoHandle::CurrentThread) {
  76. return cur_thread;
  77. }
  78. return GetObjectForIpcWithoutPseudoHandle(handle);
  79. }
  80. Result KHandleTable::Reserve(Handle* out_handle) {
  81. KScopedDisableDispatch dd{m_kernel};
  82. KScopedSpinLock lk(m_lock);
  83. // Never exceed our capacity.
  84. R_UNLESS(m_count < m_table_size, ResultOutOfHandles);
  85. *out_handle = EncodeHandle(static_cast<u16>(this->AllocateEntry()), this->AllocateLinearId());
  86. R_SUCCEED();
  87. }
  88. void KHandleTable::Unreserve(Handle handle) {
  89. KScopedDisableDispatch dd{m_kernel};
  90. KScopedSpinLock lk(m_lock);
  91. // Unpack the handle.
  92. const auto handle_pack = HandlePack(handle);
  93. const auto index = handle_pack.index;
  94. const auto linear_id = handle_pack.linear_id;
  95. const auto reserved = handle_pack.reserved;
  96. ASSERT(reserved == 0);
  97. ASSERT(linear_id != 0);
  98. if (index < m_table_size) [[likely]] {
  99. // NOTE: This code does not check the linear id.
  100. ASSERT(m_objects[index] == nullptr);
  101. this->FreeEntry(index);
  102. }
  103. }
  104. void KHandleTable::Register(Handle handle, KAutoObject* obj) {
  105. KScopedDisableDispatch dd{m_kernel};
  106. KScopedSpinLock lk(m_lock);
  107. // Unpack the handle.
  108. const auto handle_pack = HandlePack(handle);
  109. const auto index = handle_pack.index;
  110. const auto linear_id = handle_pack.linear_id;
  111. const auto reserved = handle_pack.reserved;
  112. ASSERT(reserved == 0);
  113. ASSERT(linear_id != 0);
  114. if (index < m_table_size) [[likely]] {
  115. // Set the entry.
  116. ASSERT(m_objects[index] == nullptr);
  117. m_entry_infos[index].linear_id = static_cast<u16>(linear_id);
  118. m_objects[index] = obj;
  119. obj->Open();
  120. }
  121. }
  122. } // namespace Kernel