2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 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 {
|
|
|
|
|
|
|
|
Result ServerManagerBase::InstallMitmServerImpl(Handle *out_port_handle, sm::ServiceName service_name, ServerManagerBase::MitmQueryFunction query_func) {
|
|
|
|
/* Install the Mitm. */
|
|
|
|
Handle query_handle;
|
|
|
|
R_TRY(sm::mitm::InstallMitm(out_port_handle, &query_handle, service_name));
|
|
|
|
|
|
|
|
/* 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerManagerBase::RegisterSessionToWaitList(ServerSession *session) {
|
|
|
|
session->has_received = false;
|
|
|
|
|
|
|
|
/* Set user data tag. */
|
2020-04-08 09:21:35 +00:00
|
|
|
os::SetWaitableHolderUserData(session, static_cast<uintptr_t>(UserDataTag::Session));
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
this->RegisterToWaitList(session);
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
void ServerManagerBase::RegisterToWaitList(os::WaitableHolderType *holder) {
|
2019-12-09 11:57:37 +00:00
|
|
|
std::scoped_lock lk(this->waitlist_mutex);
|
2020-04-08 09:21:35 +00:00
|
|
|
os::LinkWaitableHolder(std::addressof(this->waitlist), holder);
|
2019-12-09 11:57:37 +00:00
|
|
|
this->notify_event.Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerManagerBase::ProcessWaitList() {
|
|
|
|
std::scoped_lock lk(this->waitlist_mutex);
|
2020-04-08 09:21:35 +00:00
|
|
|
os::MoveAllWaitableHolder(std::addressof(this->waitable_manager), std::addressof(this->waitlist));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
os::WaitableHolderType *ServerManagerBase::WaitSignaled() {
|
2019-12-09 11:57:37 +00:00
|
|
|
std::scoped_lock lk(this->waitable_selection_mutex);
|
|
|
|
while (true) {
|
|
|
|
this->ProcessWaitList();
|
2020-04-08 09:21:35 +00:00
|
|
|
auto selected = os::WaitAny(std::addressof(this->waitable_manager));
|
2019-12-09 11:57:37 +00:00
|
|
|
if (selected == &this->request_stop_event_holder) {
|
|
|
|
return nullptr;
|
|
|
|
} else if (selected == &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 {
|
2020-04-08 09:21:35 +00:00
|
|
|
os::UnlinkWaitableHolder(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();
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
void ServerManagerBase::AddUserWaitableHolder(os::WaitableHolderType *waitable) {
|
|
|
|
const auto user_data_tag = static_cast<UserDataTag>(os::GetWaitableHolderUserData(waitable));
|
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);
|
2019-12-09 11:57:37 +00:00
|
|
|
this->RegisterToWaitList(waitable);
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
Result ServerManagerBase::ProcessForServer(os::WaitableHolderType *holder) {
|
|
|
|
AMS_ABORT_UNLESS(static_cast<UserDataTag>(os::GetWaitableHolderUserData(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);
|
2019-12-09 11:57:37 +00:00
|
|
|
ON_SCOPE_EXIT { this->RegisterToWaitList(server); };
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
Result ServerManagerBase::ProcessForMitmServer(os::WaitableHolderType *holder) {
|
|
|
|
AMS_ABORT_UNLESS(static_cast<UserDataTag>(os::GetWaitableHolderUserData(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);
|
2019-12-09 11:57:37 +00:00
|
|
|
ON_SCOPE_EXIT { this->RegisterToWaitList(server); };
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
Result ServerManagerBase::ProcessForSession(os::WaitableHolderType *holder) {
|
|
|
|
AMS_ABORT_UNLESS(static_cast<UserDataTag>(os::GetWaitableHolderUserData(holder)) == UserDataTag::Session);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
ServerSession *session = static_cast<ServerSession *>(holder);
|
|
|
|
|
|
|
|
cmif::PointerAndSize tls_message(armGetTls(), hipc::TlsMessageBufferSize);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
Result ServerManagerBase::Process(os::WaitableHolderType *holder) {
|
|
|
|
switch (static_cast<UserDataTag>(os::GetWaitableHolderUserData(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() {
|
|
|
|
auto waitable = this->WaitSignaled();
|
|
|
|
if (!waitable) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-23 07:05:14 +00:00
|
|
|
R_ABORT_UNLESS(this->Process(waitable));
|
2019-12-09 11:57:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerManagerBase::WaitAndProcess() {
|
|
|
|
this->WaitAndProcessImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerManagerBase::LoopProcess() {
|
|
|
|
while (this->WaitAndProcessImpl()) {
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|