2021-04-09 07:06:03 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2021-04-09 07:06:03 +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 <vapours.hpp>
|
|
|
|
#include <stratosphere/tipc/tipc_common.hpp>
|
2022-03-06 20:08:20 +00:00
|
|
|
#include <stratosphere/tipc/impl/tipc_impl_message_api.hpp>
|
2021-04-09 07:06:03 +00:00
|
|
|
#include <stratosphere/tipc/tipc_service_object.hpp>
|
2021-10-01 02:00:47 +00:00
|
|
|
#include <stratosphere/tipc/tipc_object_holder.hpp>
|
2021-04-09 07:06:03 +00:00
|
|
|
|
|
|
|
namespace ams::tipc {
|
|
|
|
|
|
|
|
/* TODO: Put this in a better header. */
|
|
|
|
constexpr inline u16 MethodId_Invalid = 0x0;
|
|
|
|
constexpr inline u16 MethodId_CloseSession = 0xF;
|
|
|
|
|
|
|
|
class ObjectManagerBase {
|
2021-04-27 03:05:56 +00:00
|
|
|
protected:
|
2021-04-09 07:06:03 +00:00
|
|
|
struct Entry {
|
2021-10-01 02:00:47 +00:00
|
|
|
util::TypedStorage<ObjectHolder> object;
|
|
|
|
os::MultiWaitHolderType multi_wait_holder;
|
2021-04-09 07:06:03 +00:00
|
|
|
};
|
|
|
|
private:
|
|
|
|
os::SdkMutex m_mutex{};
|
|
|
|
Entry *m_entries_start{};
|
|
|
|
Entry *m_entries_end{};
|
2021-10-01 02:00:47 +00:00
|
|
|
os::MultiWaitType *m_multi_wait{};
|
2021-04-09 07:06:03 +00:00
|
|
|
private:
|
2022-03-06 20:08:20 +00:00
|
|
|
Entry *FindEntry(tipc::NativeHandle handle) {
|
2021-04-09 07:06:03 +00:00
|
|
|
for (Entry *cur = m_entries_start; cur != m_entries_end; ++cur) {
|
|
|
|
if (GetReference(cur->object).GetHandle() == handle) {
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
Entry *FindEntry(os::MultiWaitHolderType *holder) {
|
2021-04-09 07:06:03 +00:00
|
|
|
for (Entry *cur = m_entries_start; cur != m_entries_end; ++cur) {
|
2021-10-01 02:00:47 +00:00
|
|
|
if (std::addressof(cur->multi_wait_holder) == holder) {
|
2021-04-09 07:06:03 +00:00
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
constexpr ObjectManagerBase() = default;
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void InitializeImpl(os::MultiWaitType *multi_wait, Entry *entries, size_t max_objects) {
|
|
|
|
/* Set our multi wait. */
|
|
|
|
m_multi_wait = multi_wait;
|
2021-04-09 07:06:03 +00:00
|
|
|
|
|
|
|
/* Setup entry pointers. */
|
|
|
|
m_entries_start = entries;
|
|
|
|
m_entries_end = entries + max_objects;
|
|
|
|
|
|
|
|
/* Construct all entries. */
|
|
|
|
for (size_t i = 0; i < max_objects; ++i) {
|
|
|
|
util::ConstructAt(m_entries_start[i].object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void AddObject(ObjectHolder &object) {
|
2021-04-09 07:06:03 +00:00
|
|
|
/* Lock ourselves. */
|
|
|
|
std::scoped_lock lk(m_mutex);
|
|
|
|
|
|
|
|
/* Find an empty entry. */
|
2022-03-06 20:08:20 +00:00
|
|
|
auto *entry = this->FindEntry(tipc::InvalidNativeHandle);
|
2021-04-09 07:06:03 +00:00
|
|
|
AMS_ABORT_UNLESS(entry != nullptr);
|
|
|
|
|
|
|
|
/* Set the entry's object. */
|
|
|
|
GetReference(entry->object) = object;
|
|
|
|
|
|
|
|
/* Setup the entry's holder. */
|
2021-10-01 02:00:47 +00:00
|
|
|
os::InitializeMultiWaitHolder(std::addressof(entry->multi_wait_holder), object.GetHandle());
|
|
|
|
os::LinkMultiWaitHolder(m_multi_wait, std::addressof(entry->multi_wait_holder));
|
2021-04-09 07:06:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
void CloseObject(tipc::NativeHandle handle) {
|
2021-04-09 07:06:03 +00:00
|
|
|
/* Lock ourselves. */
|
|
|
|
std::scoped_lock lk(m_mutex);
|
|
|
|
|
|
|
|
/* Find the matching entry. */
|
|
|
|
auto *entry = this->FindEntry(handle);
|
|
|
|
AMS_ABORT_UNLESS(entry != nullptr);
|
|
|
|
|
|
|
|
/* Finalize the entry's holder. */
|
2021-10-01 02:00:47 +00:00
|
|
|
os::UnlinkMultiWaitHolder(std::addressof(entry->multi_wait_holder));
|
|
|
|
os::FinalizeMultiWaitHolder(std::addressof(entry->multi_wait_holder));
|
2021-04-09 07:06:03 +00:00
|
|
|
|
|
|
|
/* Destroy the object. */
|
|
|
|
GetReference(entry->object).Destroy();
|
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
Result ReplyAndReceive(os::MultiWaitHolderType **out_holder, ObjectHolder *out_object, tipc::NativeHandle reply_target, os::MultiWaitType *multi_wait) {
|
2021-04-09 07:06:03 +00:00
|
|
|
/* Declare signaled holder for processing ahead of time. */
|
2021-10-01 02:00:47 +00:00
|
|
|
os::MultiWaitHolderType *signaled_holder;
|
2021-04-09 07:06:03 +00:00
|
|
|
|
|
|
|
/* Reply and receive until we get a newly signaled target. */
|
2021-10-01 02:00:47 +00:00
|
|
|
Result result = os::SdkReplyAndReceive(out_holder, reply_target, multi_wait);
|
2021-04-09 07:06:03 +00:00
|
|
|
for (signaled_holder = *out_holder; signaled_holder == nullptr; signaled_holder = *out_holder) {
|
2022-03-06 20:08:20 +00:00
|
|
|
result = os::SdkReplyAndReceive(out_holder, tipc::InvalidNativeHandle, multi_wait);
|
2021-04-09 07:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the entry matching the signaled holder. */
|
|
|
|
if (auto *entry = this->FindEntry(signaled_holder); entry != nullptr) {
|
|
|
|
/* Get the output object. */
|
|
|
|
*out_object = GetReference(entry->object);
|
|
|
|
*out_holder = nullptr;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
void Reply(tipc::NativeHandle reply_target) {
|
|
|
|
return tipc::impl::Reply(reply_target);
|
2021-04-09 07:06:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
Result ProcessRequest(ObjectHolder &object) {
|
2021-10-12 07:57:54 +00:00
|
|
|
/* Get the message buffer. */
|
2022-03-06 20:08:20 +00:00
|
|
|
const MessageBuffer message_buffer(tipc::GetMessageBuffer());
|
2021-10-12 07:57:54 +00:00
|
|
|
|
2021-04-09 07:06:03 +00:00
|
|
|
/* Get the method id. */
|
2022-03-06 20:08:20 +00:00
|
|
|
const auto method_id = MessageBuffer::MessageHeader(message_buffer).GetTag();
|
2021-10-12 07:57:54 +00:00
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
/* Process for the method id. */
|
|
|
|
{
|
|
|
|
/* Ensure that if we fail, we clean up any handles that get sent our way. */
|
|
|
|
ON_RESULT_FAILURE {
|
2022-03-06 20:08:20 +00:00
|
|
|
const MessageBuffer::MessageHeader message_header(message_buffer);
|
|
|
|
const MessageBuffer::SpecialHeader special_header(message_buffer, message_header);
|
2022-02-14 22:45:32 +00:00
|
|
|
|
|
|
|
/* Determine the offset to the start of handles. */
|
|
|
|
auto offset = message_buffer.GetSpecialDataIndex(message_header, special_header);
|
|
|
|
if (special_header.GetHasProcessId()) {
|
|
|
|
offset += sizeof(u64) / sizeof(u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close all copy handles. */
|
|
|
|
for (auto i = 0; i < special_header.GetCopyHandleCount(); ++i) {
|
2022-03-06 20:08:20 +00:00
|
|
|
tipc::impl::CloseHandle(message_buffer.GetHandle(offset));
|
|
|
|
offset += sizeof(typename std::remove_reference<decltype(message_buffer.GetHandle(offset))>::type) / sizeof(u32);
|
2022-02-14 22:45:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Check that the method id is valid. */
|
|
|
|
R_UNLESS(method_id != MethodId_Invalid, tipc::ResultInvalidMethod());
|
|
|
|
|
|
|
|
/* Process the request. */
|
|
|
|
if (method_id != MethodId_CloseSession) {
|
|
|
|
/* Process the generic method for the object. */
|
|
|
|
R_TRY(object.GetObject()->ProcessRequest());
|
|
|
|
} else {
|
|
|
|
/* Validate that the close request is of valid format. */
|
2021-10-12 07:57:54 +00:00
|
|
|
using CloseSessionCommandMeta = impl::CommandMetaInfo<MethodId_CloseSession, std::tuple<>>;
|
|
|
|
using CloseSessionProcessor = impl::CommandProcessor<CloseSessionCommandMeta>;
|
|
|
|
|
|
|
|
R_TRY(CloseSessionProcessor::ValidateCommandFormat(message_buffer));
|
|
|
|
}
|
2022-02-14 22:45:32 +00:00
|
|
|
}
|
2021-10-12 07:57:54 +00:00
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
/* If we were asked to close the object, do so. */
|
|
|
|
if (method_id == MethodId_CloseSession) {
|
2021-10-12 07:57:54 +00:00
|
|
|
/* Get the object handle. */
|
2021-04-09 07:06:03 +00:00
|
|
|
const auto handle = object.GetHandle();
|
|
|
|
|
|
|
|
/* Close the object itself. */
|
|
|
|
this->CloseObject(handle);
|
|
|
|
|
|
|
|
/* Close the object's handle. */
|
|
|
|
/* NOTE: Nintendo does not check that this succeeds. */
|
2022-03-06 20:08:20 +00:00
|
|
|
R_ABORT_UNLESS(tipc::impl::CloseHandle(handle));
|
2021-04-09 07:06:03 +00:00
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
/* Return an error to signify we closed the object. */
|
|
|
|
R_THROW(tipc::ResultSessionClosed());
|
2021-04-09 07:06:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 07:57:54 +00:00
|
|
|
/* We successfully processed, so we don't need to clean up handles. */
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2021-04-09 07:06:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t MaxObjects>
|
|
|
|
class ObjectManager : public ObjectManagerBase {
|
|
|
|
private:
|
|
|
|
Entry m_entries_storage[MaxObjects]{};
|
|
|
|
public:
|
|
|
|
constexpr ObjectManager() = default;
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void Initialize(os::MultiWaitType *multi_wait) {
|
|
|
|
this->InitializeImpl(multi_wait, m_entries_storage, MaxObjects);
|
2021-04-09 07:06:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|