2020-01-30 23:29:51 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-01-30 23:29:51 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <mesosphere/kern_common.hpp>
|
|
|
|
#include <mesosphere/kern_k_auto_object.hpp>
|
|
|
|
#include <mesosphere/kern_k_spin_lock.hpp>
|
|
|
|
#include <mesosphere/kern_k_thread.hpp>
|
2020-02-20 03:38:20 +00:00
|
|
|
#include <mesosphere/kern_k_interrupt_event.hpp>
|
2020-01-30 23:29:51 +00:00
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE util::BitPack32 GetHandleBitPack(ams::svc::Handle handle) {
|
|
|
|
return util::BitPack32{handle};
|
|
|
|
}
|
|
|
|
|
2020-02-20 17:05:01 +00:00
|
|
|
class KProcess;
|
|
|
|
class KThread;
|
|
|
|
|
2020-01-30 23:29:51 +00:00
|
|
|
class KHandleTable {
|
|
|
|
NON_COPYABLE(KHandleTable);
|
|
|
|
NON_MOVEABLE(KHandleTable);
|
|
|
|
public:
|
|
|
|
static constexpr size_t MaxTableSize = 1024;
|
|
|
|
private:
|
|
|
|
using HandleRawValue = util::BitPack32::Field<0, BITSIZEOF(u32), u32>;
|
|
|
|
using HandleEncoded = util::BitPack32::Field<0, BITSIZEOF(ams::svc::Handle), ams::svc::Handle>;
|
|
|
|
|
|
|
|
using HandleIndex = util::BitPack32::Field<0, 15, u16>;
|
|
|
|
using HandleLinearId = util::BitPack32::Field<HandleIndex::Next, 15, u16>;
|
|
|
|
using HandleReserved = util::BitPack32::Field<HandleLinearId::Next, 2, u32>;
|
|
|
|
|
|
|
|
static constexpr u16 MinLinearId = 1;
|
|
|
|
static constexpr u16 MaxLinearId = util::BitPack32{std::numeric_limits<u32>::max()}.Get<HandleLinearId>();
|
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE ams::svc::Handle EncodeHandle(u16 index, u16 linear_id) {
|
|
|
|
util::BitPack32 pack = {0};
|
|
|
|
pack.Set<HandleIndex>(index);
|
|
|
|
pack.Set<HandleLinearId>(linear_id);
|
|
|
|
pack.Set<HandleReserved>(0);
|
|
|
|
return pack.Get<HandleEncoded>();
|
|
|
|
}
|
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
union EntryInfo {
|
2021-09-17 22:03:21 +00:00
|
|
|
u16 linear_id;
|
|
|
|
s16 next_free_index;
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE u16 GetLinearId() const { return linear_id; }
|
2021-04-07 21:07:10 +00:00
|
|
|
constexpr ALWAYS_INLINE s32 GetNextFreeIndex() const { return next_free_index; }
|
2020-01-30 23:29:51 +00:00
|
|
|
};
|
|
|
|
private:
|
2021-04-07 21:07:10 +00:00
|
|
|
EntryInfo m_entry_infos[MaxTableSize];
|
|
|
|
KAutoObject *m_objects[MaxTableSize];
|
2021-10-23 22:25:20 +00:00
|
|
|
mutable KSpinLock m_lock;
|
2021-04-07 21:07:10 +00:00
|
|
|
s32 m_free_head_index;
|
2020-12-18 01:18:47 +00:00
|
|
|
u16 m_table_size;
|
|
|
|
u16 m_max_count;
|
|
|
|
u16 m_next_linear_id;
|
|
|
|
u16 m_count;
|
2020-01-30 23:29:51 +00:00
|
|
|
public:
|
2021-10-23 22:25:20 +00:00
|
|
|
constexpr explicit KHandleTable(util::ConstantInitializeTag) : m_entry_infos(), m_objects(), m_lock(), m_free_head_index(-1), m_table_size(), m_max_count(), m_next_linear_id(MinLinearId), m_count() { /* ... */ }
|
|
|
|
|
2021-10-24 20:08:08 +00:00
|
|
|
explicit KHandleTable() : m_lock(), m_free_head_index(-1), m_count() { MESOSPHERE_ASSERT_THIS(); }
|
2020-01-30 23:29:51 +00:00
|
|
|
|
|
|
|
constexpr NOINLINE Result Initialize(s32 size) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
R_UNLESS(size <= static_cast<s32>(MaxTableSize), svc::ResultOutOfMemory());
|
|
|
|
|
|
|
|
/* Initialize all fields. */
|
2021-04-07 21:07:10 +00:00
|
|
|
m_max_count = 0;
|
|
|
|
m_table_size = (size <= 0) ? MaxTableSize : size;
|
|
|
|
m_next_linear_id = MinLinearId;
|
|
|
|
m_count = 0;
|
|
|
|
m_free_head_index = -1;
|
2020-01-30 23:29:51 +00:00
|
|
|
|
|
|
|
/* Free all entries. */
|
2021-04-07 21:07:10 +00:00
|
|
|
for (s32 i = 0; i < static_cast<s32>(m_table_size); ++i) {
|
|
|
|
m_objects[i] = nullptr;
|
|
|
|
m_entry_infos[i].next_free_index = i - 1;
|
|
|
|
m_free_head_index = i;
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ALWAYS_INLINE size_t GetTableSize() const { return m_table_size; }
|
|
|
|
constexpr ALWAYS_INLINE size_t GetCount() const { return m_count; }
|
|
|
|
constexpr ALWAYS_INLINE size_t GetMaxCount() const { return m_max_count; }
|
2020-01-30 23:29:51 +00:00
|
|
|
|
|
|
|
NOINLINE Result Finalize();
|
|
|
|
NOINLINE bool Remove(ams::svc::Handle handle);
|
|
|
|
|
|
|
|
template<typename T = KAutoObject>
|
2020-07-21 12:22:58 +00:00
|
|
|
ALWAYS_INLINE KScopedAutoObject<T> GetObjectWithoutPseudoHandle(ams::svc::Handle handle) const {
|
2020-02-20 17:05:01 +00:00
|
|
|
/* Lock and look up in table. */
|
2020-01-30 23:29:51 +00:00
|
|
|
KScopedDisableDispatch dd;
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedSpinLock lk(m_lock);
|
2020-01-30 23:29:51 +00:00
|
|
|
|
|
|
|
if constexpr (std::is_same<T, KAutoObject>::value) {
|
|
|
|
return this->GetObjectImpl(handle);
|
|
|
|
} else {
|
2021-04-07 21:07:10 +00:00
|
|
|
if (auto *obj = this->GetObjectImpl(handle); AMS_LIKELY(obj != nullptr)) {
|
2020-05-29 07:57:25 +00:00
|
|
|
return obj->DynamicCast<T*>();
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 12:22:58 +00:00
|
|
|
template<typename T = KAutoObject>
|
|
|
|
ALWAYS_INLINE KScopedAutoObject<T> GetObject(ams::svc::Handle handle) const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Handle pseudo-handles. */
|
|
|
|
if constexpr (std::derived_from<KProcess, T>) {
|
|
|
|
if (handle == ams::svc::PseudoHandle::CurrentProcess) {
|
2021-04-20 01:04:02 +00:00
|
|
|
auto * const cur_process = GetCurrentProcessPointer();
|
|
|
|
AMS_ASSUME(cur_process != nullptr);
|
|
|
|
return cur_process;
|
2020-07-21 12:22:58 +00:00
|
|
|
}
|
|
|
|
} else if constexpr (std::derived_from<KThread, T>) {
|
|
|
|
if (handle == ams::svc::PseudoHandle::CurrentThread) {
|
2021-04-20 01:04:02 +00:00
|
|
|
auto * const cur_thread = GetCurrentThreadPointer();
|
|
|
|
AMS_ASSUME(cur_thread != nullptr);
|
|
|
|
return cur_thread;
|
2020-07-21 12:22:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->template GetObjectWithoutPseudoHandle<T>(handle);
|
|
|
|
}
|
|
|
|
|
2020-12-02 12:07:01 +00:00
|
|
|
KScopedAutoObject<KAutoObject> GetObjectForIpcWithoutPseudoHandle(ams::svc::Handle handle) const {
|
2020-02-20 17:05:01 +00:00
|
|
|
/* Lock and look up in table. */
|
|
|
|
KScopedDisableDispatch dd;
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedSpinLock lk(m_lock);
|
2020-02-20 17:05:01 +00:00
|
|
|
|
2020-01-30 23:29:51 +00:00
|
|
|
KAutoObject *obj = this->GetObjectImpl(handle);
|
2020-12-02 12:05:16 +00:00
|
|
|
if (AMS_LIKELY(obj != nullptr)) {
|
|
|
|
if (AMS_UNLIKELY(obj->DynamicCast<KInterruptEvent *>() != nullptr)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
2020-07-10 07:03:50 +00:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE KScopedAutoObject<KAutoObject> GetObjectForIpc(ams::svc::Handle handle, KThread *cur_thread) const {
|
|
|
|
/* Handle pseudo-handles. */
|
2021-04-20 01:04:02 +00:00
|
|
|
AMS_ASSUME(cur_thread != nullptr);
|
2020-07-10 07:03:50 +00:00
|
|
|
if (handle == ams::svc::PseudoHandle::CurrentProcess) {
|
2021-04-20 01:04:02 +00:00
|
|
|
auto * const cur_process = static_cast<KAutoObject *>(static_cast<void *>(cur_thread->GetOwnerProcess()));
|
|
|
|
AMS_ASSUME(cur_process != nullptr);
|
|
|
|
return cur_process;
|
2020-07-10 07:03:50 +00:00
|
|
|
}
|
|
|
|
if (handle == ams::svc::PseudoHandle::CurrentThread) {
|
|
|
|
return static_cast<KAutoObject *>(cur_thread);
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
2020-07-10 07:03:50 +00:00
|
|
|
|
|
|
|
return GetObjectForIpcWithoutPseudoHandle(handle);
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE KScopedAutoObject<KAutoObject> GetObjectByIndex(ams::svc::Handle *out_handle, size_t index) const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
KScopedDisableDispatch dd;
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedSpinLock lk(m_lock);
|
2020-01-30 23:29:51 +00:00
|
|
|
|
|
|
|
return this->GetObjectByIndexImpl(out_handle, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
NOINLINE Result Reserve(ams::svc::Handle *out_handle);
|
|
|
|
NOINLINE void Unreserve(ams::svc::Handle handle);
|
|
|
|
|
2021-09-17 22:03:21 +00:00
|
|
|
NOINLINE Result Add(ams::svc::Handle *out_handle, KAutoObject *obj);
|
|
|
|
NOINLINE void Register(ams::svc::Handle handle, KAutoObject *obj);
|
2020-07-10 00:21:47 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
ALWAYS_INLINE bool GetMultipleObjects(T **out, const ams::svc::Handle *handles, size_t num_handles) const {
|
|
|
|
/* Try to convert and open all the handles. */
|
|
|
|
size_t num_opened;
|
|
|
|
{
|
|
|
|
/* Lock the table. */
|
|
|
|
KScopedDisableDispatch dd;
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedSpinLock lk(m_lock);
|
2020-07-10 00:21:47 +00:00
|
|
|
for (num_opened = 0; num_opened < num_handles; num_opened++) {
|
|
|
|
/* Get the current handle. */
|
|
|
|
const auto cur_handle = handles[num_opened];
|
|
|
|
|
|
|
|
/* Get the object for the current handle. */
|
|
|
|
KAutoObject *cur_object = this->GetObjectImpl(cur_handle);
|
|
|
|
if (AMS_UNLIKELY(cur_object == nullptr)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cast the current object to the desired type. */
|
|
|
|
T *cur_t = cur_object->DynamicCast<T*>();
|
|
|
|
if (AMS_UNLIKELY(cur_t == nullptr)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open a reference to the current object. */
|
|
|
|
cur_t->Open();
|
|
|
|
out[num_opened] = cur_t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we converted every object, succeed. */
|
|
|
|
if (AMS_LIKELY(num_opened == num_handles)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we didn't convert entry object, close the ones we opened. */
|
|
|
|
for (size_t i = 0; i < num_opened; i++) {
|
|
|
|
out[i]->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-30 23:29:51 +00:00
|
|
|
private:
|
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
constexpr ALWAYS_INLINE s32 AllocateEntry() {
|
2020-01-30 23:29:51 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-12-18 01:18:47 +00:00
|
|
|
MESOSPHERE_ASSERT(m_count < m_table_size);
|
2020-01-30 23:29:51 +00:00
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
const auto index = m_free_head_index;
|
|
|
|
|
|
|
|
m_free_head_index = m_entry_infos[index].GetNextFreeIndex();
|
2020-01-30 23:29:51 +00:00
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
m_max_count = std::max(m_max_count, ++m_count);
|
2020-01-30 23:29:51 +00:00
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
return index;
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
constexpr ALWAYS_INLINE void FreeEntry(s32 index) {
|
2020-01-30 23:29:51 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-12-18 01:18:47 +00:00
|
|
|
MESOSPHERE_ASSERT(m_count > 0);
|
2020-01-30 23:29:51 +00:00
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
m_objects[index] = nullptr;
|
|
|
|
m_entry_infos[index].next_free_index = m_free_head_index;
|
2020-01-30 23:29:51 +00:00
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
m_free_head_index = index;
|
|
|
|
|
|
|
|
--m_count;
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE u16 AllocateLinearId() {
|
2020-12-18 01:18:47 +00:00
|
|
|
const u16 id = m_next_linear_id++;
|
|
|
|
if (m_next_linear_id > MaxLinearId) {
|
|
|
|
m_next_linear_id = MinLinearId;
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
constexpr ALWAYS_INLINE bool IsValidHandle(ams::svc::Handle handle) const {
|
2020-01-30 23:29:51 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Unpack the handle. */
|
|
|
|
const auto handle_pack = GetHandleBitPack(handle);
|
|
|
|
const auto raw_value = handle_pack.Get<HandleRawValue>();
|
|
|
|
const auto index = handle_pack.Get<HandleIndex>();
|
|
|
|
const auto linear_id = handle_pack.Get<HandleLinearId>();
|
|
|
|
const auto reserved = handle_pack.Get<HandleReserved>();
|
|
|
|
MESOSPHERE_ASSERT(reserved == 0);
|
2021-01-08 10:13:36 +00:00
|
|
|
MESOSPHERE_UNUSED(reserved);
|
2020-01-30 23:29:51 +00:00
|
|
|
|
|
|
|
/* Validate our indexing information. */
|
2021-04-07 21:07:10 +00:00
|
|
|
if (AMS_UNLIKELY(raw_value == 0)) {
|
|
|
|
return false;
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
2021-04-07 21:07:10 +00:00
|
|
|
if (AMS_UNLIKELY(linear_id == 0)) {
|
|
|
|
return false;
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
2021-04-07 21:07:10 +00:00
|
|
|
if (AMS_UNLIKELY(index >= m_table_size)) {
|
|
|
|
return false;
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
/* Check that there's an object, and our serial id is correct. */
|
|
|
|
if (AMS_UNLIKELY(m_objects[index] == nullptr)) {
|
|
|
|
return false;
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
2021-04-07 21:07:10 +00:00
|
|
|
if (AMS_UNLIKELY(m_entry_infos[index].GetLinearId() != linear_id)) {
|
|
|
|
return false;
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
return true;
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 21:25:06 +00:00
|
|
|
constexpr NOINLINE KAutoObject *GetObjectImpl(ams::svc::Handle handle) const {
|
2020-01-30 23:29:51 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Handles must not have reserved bits set. */
|
2021-04-07 21:07:10 +00:00
|
|
|
const auto handle_pack = GetHandleBitPack(handle);
|
|
|
|
if (AMS_UNLIKELY(handle_pack.Get<HandleReserved>() != 0)) {
|
2020-01-30 23:29:51 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-07 21:07:10 +00:00
|
|
|
if (AMS_LIKELY(this->IsValidHandle(handle))) {
|
|
|
|
return m_objects[handle_pack.Get<HandleIndex>()];
|
2020-01-30 23:29:51 +00:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 11:54:53 +00:00
|
|
|
constexpr ALWAYS_INLINE KAutoObject *GetObjectByIndexImpl(ams::svc::Handle *out_handle, size_t index) const {
|
2020-01-30 23:29:51 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Index must be in bounds. */
|
2021-04-07 21:07:10 +00:00
|
|
|
if (AMS_UNLIKELY(index >= m_table_size)) {
|
2020-01-30 23:29:51 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure entry has an object. */
|
2021-04-07 21:07:10 +00:00
|
|
|
if (KAutoObject *obj = m_objects[index]; obj != nullptr) {
|
|
|
|
*out_handle = EncodeHandle(index, m_entry_infos[index].GetLinearId());
|
|
|
|
return obj;
|
|
|
|
} else {
|
2020-01-30 23:29:51 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|