2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-09 11:57:37 +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 <stratosphere.hpp>
|
|
|
|
#include "sf_hipc_mitm_query_api.hpp"
|
|
|
|
|
|
|
|
namespace ams::sf::hipc {
|
|
|
|
|
2021-10-05 00:12:32 +00:00
|
|
|
Result ServerManagerBase::InstallMitmServerImpl(os::NativeHandle *out_port_handle, sm::ServiceName service_name, ServerManagerBase::MitmQueryFunction query_func) {
|
2019-12-09 11:57:37 +00:00
|
|
|
/* Install the Mitm. */
|
2021-10-05 00:12:32 +00:00
|
|
|
os::NativeHandle query_handle;
|
2021-10-09 21:49:53 +00:00
|
|
|
R_TRY(sm::mitm::InstallMitm(out_port_handle, std::addressof(query_handle), service_name));
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
/* Register the query handle. */
|
|
|
|
impl::RegisterMitmQueryHandle(query_handle, query_func);
|
2020-09-07 17:40:43 +00:00
|
|
|
|
|
|
|
/* Clear future declarations if any, now that our query handler is present. */
|
|
|
|
R_ABORT_UNLESS(sm::mitm::ClearFutureMitm(service_name));
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void ServerManagerBase::RegisterServerSessionToWait(ServerSession *session) {
|
2019-12-09 11:57:37 +00:00
|
|
|
session->has_received = false;
|
|
|
|
|
|
|
|
/* Set user data tag. */
|
2021-10-01 02:00:47 +00:00
|
|
|
os::SetMultiWaitHolderUserData(session, static_cast<uintptr_t>(UserDataTag::Session));
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
this->LinkToDeferredList(session);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void ServerManagerBase::LinkToDeferredList(os::MultiWaitHolderType *holder) {
|
|
|
|
std::scoped_lock lk(this->deferred_list_mutex);
|
|
|
|
os::LinkMultiWaitHolder(std::addressof(this->deferred_list), holder);
|
2019-12-09 11:57:37 +00:00
|
|
|
this->notify_event.Signal();
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void ServerManagerBase::LinkDeferred() {
|
|
|
|
std::scoped_lock lk(this->deferred_list_mutex);
|
|
|
|
os::MoveAllMultiWaitHolder(std::addressof(this->multi_wait), std::addressof(this->deferred_list));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
os::MultiWaitHolderType *ServerManagerBase::WaitSignaled() {
|
|
|
|
std::scoped_lock lk(this->selection_mutex);
|
2019-12-09 11:57:37 +00:00
|
|
|
while (true) {
|
2021-10-01 02:00:47 +00:00
|
|
|
this->LinkDeferred();
|
|
|
|
auto selected = os::WaitAny(std::addressof(this->multi_wait));
|
2021-10-09 21:49:53 +00:00
|
|
|
if (selected == std::addressof(this->request_stop_event_holder)) {
|
2019-12-09 11:57:37 +00:00
|
|
|
return nullptr;
|
2021-10-09 21:49:53 +00:00
|
|
|
} else if (selected == std::addressof(this->notify_event_holder)) {
|
2020-04-08 09:21:35 +00:00
|
|
|
this->notify_event.Clear();
|
2019-12-09 11:57:37 +00:00
|
|
|
} else {
|
2021-10-01 02:00:47 +00:00
|
|
|
os::UnlinkMultiWaitHolder(selected);
|
2019-12-09 11:57:37 +00:00
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerManagerBase::ResumeProcessing() {
|
2020-04-08 09:21:35 +00:00
|
|
|
this->request_stop_event.Clear();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerManagerBase::RequestStopProcessing() {
|
|
|
|
this->request_stop_event.Signal();
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void ServerManagerBase::AddUserMultiWaitHolder(os::MultiWaitHolderType *holder) {
|
|
|
|
const auto user_data_tag = static_cast<UserDataTag>(os::GetMultiWaitHolderUserData(holder));
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(user_data_tag != UserDataTag::Server);
|
|
|
|
AMS_ABORT_UNLESS(user_data_tag != UserDataTag::MitmServer);
|
|
|
|
AMS_ABORT_UNLESS(user_data_tag != UserDataTag::Session);
|
2021-10-01 02:00:47 +00:00
|
|
|
this->LinkToDeferredList(holder);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
Result ServerManagerBase::ProcessForServer(os::MultiWaitHolderType *holder) {
|
|
|
|
AMS_ABORT_UNLESS(static_cast<UserDataTag>(os::GetMultiWaitHolderUserData(holder)) == UserDataTag::Server);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-01-17 15:55:32 +00:00
|
|
|
Server *server = static_cast<Server *>(holder);
|
2021-10-01 02:00:47 +00:00
|
|
|
ON_SCOPE_EXIT { this->LinkToDeferredList(server); };
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-01-17 15:55:32 +00:00
|
|
|
/* Create new session. */
|
|
|
|
if (server->static_object) {
|
|
|
|
return this->AcceptSession(server->port_handle, server->static_object.Clone());
|
|
|
|
} else {
|
|
|
|
return this->OnNeedsToAccept(server->index, server);
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
Result ServerManagerBase::ProcessForMitmServer(os::MultiWaitHolderType *holder) {
|
|
|
|
AMS_ABORT_UNLESS(static_cast<UserDataTag>(os::GetMultiWaitHolderUserData(holder)) == UserDataTag::MitmServer);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-01-17 15:55:32 +00:00
|
|
|
Server *server = static_cast<Server *>(holder);
|
2021-10-01 02:00:47 +00:00
|
|
|
ON_SCOPE_EXIT { this->LinkToDeferredList(server); };
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
/* Create resources for new session. */
|
2021-01-17 15:55:32 +00:00
|
|
|
return this->OnNeedsToAccept(server->index, server);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
Result ServerManagerBase::ProcessForSession(os::MultiWaitHolderType *holder) {
|
|
|
|
AMS_ABORT_UNLESS(static_cast<UserDataTag>(os::GetMultiWaitHolderUserData(holder)) == UserDataTag::Session);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
ServerSession *session = static_cast<ServerSession *>(holder);
|
|
|
|
|
2021-10-04 21:54:13 +00:00
|
|
|
cmif::PointerAndSize tls_message(svc::GetThreadLocalRegion()->message_buffer, hipc::TlsMessageBufferSize);
|
2019-12-09 11:57:37 +00:00
|
|
|
const cmif::PointerAndSize &saved_message = session->saved_message;
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(tls_message.GetSize() == saved_message.GetSize());
|
2019-12-09 11:57:37 +00:00
|
|
|
if (!session->has_received) {
|
|
|
|
R_TRY(this->ReceiveRequest(session, tls_message));
|
|
|
|
session->has_received = true;
|
|
|
|
std::memcpy(saved_message.GetPointer(), tls_message.GetPointer(), tls_message.GetSize());
|
|
|
|
} else {
|
|
|
|
/* We were deferred and are re-receiving, so just memcpy. */
|
|
|
|
std::memcpy(tls_message.GetPointer(), saved_message.GetPointer(), tls_message.GetSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Treat a meta "Context Invalidated" message as a success. */
|
|
|
|
R_TRY_CATCH(this->ProcessRequest(session, tls_message)) {
|
|
|
|
R_CONVERT(sf::impl::ResultRequestInvalidated, ResultSuccess());
|
|
|
|
} R_END_TRY_CATCH;
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
Result ServerManagerBase::Process(os::MultiWaitHolderType *holder) {
|
|
|
|
switch (static_cast<UserDataTag>(os::GetMultiWaitHolderUserData(holder))) {
|
2019-12-09 11:57:37 +00:00
|
|
|
case UserDataTag::Server:
|
|
|
|
return this->ProcessForServer(holder);
|
|
|
|
case UserDataTag::MitmServer:
|
|
|
|
return this->ProcessForMitmServer(holder);
|
|
|
|
case UserDataTag::Session:
|
2020-12-31 08:29:06 +00:00
|
|
|
return this->ProcessForSession(holder);
|
2019-12-09 11:57:37 +00:00
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ServerManagerBase::WaitAndProcessImpl() {
|
2021-10-01 02:00:47 +00:00
|
|
|
if (auto *signaled_holder = this->WaitSignaled(); signaled_holder != nullptr) {
|
|
|
|
R_ABORT_UNLESS(this->Process(signaled_holder));
|
|
|
|
return true;
|
|
|
|
} else {
|
2019-12-09 11:57:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerManagerBase::WaitAndProcess() {
|
|
|
|
this->WaitAndProcessImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerManagerBase::LoopProcess() {
|
|
|
|
while (this->WaitAndProcessImpl()) {
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|