2020-01-30 23:29:51 +00:00
|
|
|
/*
|
2021-10-04 20:59:10 +01: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/>.
|
|
|
|
*/
|
|
|
|
#include <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
Result KHandleTable::Finalize() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Get the table and clear our record of it. */
|
|
|
|
u16 saved_table_size = 0;
|
|
|
|
{
|
|
|
|
KScopedDisableDispatch dd;
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedSpinLock lk(m_lock);
|
2020-01-30 23:29:51 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
std::swap(m_table_size, saved_table_size);
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Close and free all entries. */
|
|
|
|
for (size_t i = 0; i < saved_table_size; i++) {
|
2021-04-07 22:07:10 +01:00
|
|
|
if (KAutoObject *obj = m_objects[i]; obj != nullptr) {
|
2020-01-30 23:29:51 +00:00
|
|
|
obj->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool KHandleTable::Remove(ams::svc::Handle handle) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Don't allow removal of a pseudo-handle. */
|
2021-04-07 22:07:10 +01:00
|
|
|
if (AMS_UNLIKELY(ams::svc::IsPseudoHandle(handle))) {
|
2020-01-30 23:29:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handles must not have reserved bits set. */
|
2021-04-07 22:07:10 +01:00
|
|
|
const auto handle_pack = GetHandleBitPack(handle);
|
|
|
|
if (AMS_UNLIKELY(handle_pack.Get<HandleReserved>() != 0)) {
|
2020-01-30 23:29:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the object and free the entry. */
|
|
|
|
KAutoObject *obj = nullptr;
|
|
|
|
{
|
|
|
|
KScopedDisableDispatch dd;
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedSpinLock lk(m_lock);
|
2020-01-30 23:29:51 +00:00
|
|
|
|
2021-04-07 22:07:10 +01:00
|
|
|
if (AMS_LIKELY(this->IsValidHandle(handle))) {
|
|
|
|
const auto index = handle_pack.Get<HandleIndex>();
|
|
|
|
|
|
|
|
obj = m_objects[index];
|
|
|
|
this->FreeEntry(index);
|
2020-01-30 23:29:51 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the object. */
|
|
|
|
obj->Close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-17 23:03:21 +01:00
|
|
|
Result KHandleTable::Add(ams::svc::Handle *out_handle, KAutoObject *obj) {
|
2020-01-30 23:29:51 +00:00
|
|
|
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
|
|
|
|
|
|
|
/* Never exceed our capacity. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_UNLESS(m_count < m_table_size, svc::ResultOutOfHandles());
|
2020-01-30 23:29:51 +00:00
|
|
|
|
|
|
|
/* Allocate entry, set output handle. */
|
|
|
|
{
|
|
|
|
const auto linear_id = this->AllocateLinearId();
|
2021-04-07 22:07:10 +01:00
|
|
|
const auto index = this->AllocateEntry();
|
|
|
|
|
2021-09-17 23:03:21 +01:00
|
|
|
m_entry_infos[index].linear_id = linear_id;
|
|
|
|
m_objects[index] = obj;
|
2021-04-07 22:07:10 +01:00
|
|
|
|
2020-01-30 23:29:51 +00:00
|
|
|
obj->Open();
|
2021-04-07 22:07:10 +01:00
|
|
|
|
|
|
|
*out_handle = EncodeHandle(index, linear_id);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Result KHandleTable::Reserve(ams::svc::Handle *out_handle) {
|
|
|
|
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
|
|
|
|
|
|
|
/* Never exceed our capacity. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_UNLESS(m_count < m_table_size, svc::ResultOutOfHandles());
|
2020-01-30 23:29:51 +00:00
|
|
|
|
2021-04-07 22:07:10 +01:00
|
|
|
*out_handle = EncodeHandle(this->AllocateEntry(), this->AllocateLinearId());
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KHandleTable::Unreserve(ams::svc::Handle handle) {
|
|
|
|
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
|
|
|
|
|
|
|
/* Unpack the handle. */
|
|
|
|
const auto handle_pack = GetHandleBitPack(handle);
|
|
|
|
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);
|
|
|
|
MESOSPHERE_ASSERT(linear_id != 0);
|
2021-01-08 10:13:36 +00:00
|
|
|
MESOSPHERE_UNUSED(linear_id, reserved);
|
2020-01-30 23:29:51 +00:00
|
|
|
|
2021-04-07 22:07:10 +01:00
|
|
|
if (AMS_LIKELY(index < m_table_size)) {
|
2021-04-07 18:07:24 +01:00
|
|
|
/* NOTE: This code does not check the linear id. */
|
2021-04-07 22:07:10 +01:00
|
|
|
MESOSPHERE_ASSERT(m_objects[index] == nullptr);
|
|
|
|
this->FreeEntry(index);
|
2021-04-07 18:07:24 +01:00
|
|
|
}
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 23:03:21 +01:00
|
|
|
void KHandleTable::Register(ams::svc::Handle handle, KAutoObject *obj) {
|
2020-01-30 23:29:51 +00:00
|
|
|
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
|
|
|
|
|
|
|
/* Unpack the handle. */
|
|
|
|
const auto handle_pack = GetHandleBitPack(handle);
|
|
|
|
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);
|
|
|
|
MESOSPHERE_ASSERT(linear_id != 0);
|
2021-01-08 10:13:36 +00:00
|
|
|
MESOSPHERE_UNUSED(reserved);
|
2020-01-30 23:29:51 +00:00
|
|
|
|
2021-04-07 22:07:10 +01:00
|
|
|
if (AMS_LIKELY(index < m_table_size)) {
|
2021-04-07 18:07:24 +01:00
|
|
|
/* Set the entry. */
|
2021-04-07 22:07:10 +01:00
|
|
|
MESOSPHERE_ASSERT(m_objects[index] == nullptr);
|
|
|
|
|
2021-09-17 23:03:21 +01:00
|
|
|
m_entry_infos[index].linear_id = linear_id;
|
|
|
|
m_objects[index] = obj;
|
2020-01-30 23:29:51 +00:00
|
|
|
|
2021-04-07 18:07:24 +01:00
|
|
|
obj->Open();
|
|
|
|
}
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|