sf: implement service framework enough for ro to work.

This completely re-does the whole interface for ipc servers.
This commit is contained in:
Michael Scire
2019-10-10 23:49:28 -07:00
committed by SciresM
parent bd341d5c00
commit f4dcd1db9b
47 changed files with 3545 additions and 166 deletions

View File

@@ -16,11 +16,11 @@
#include <stratosphere.hpp>
namespace sts::ams {
namespace sts::hos {
namespace {
FirmwareVersion g_firmware_version;
hos::Version g_hos_version;
bool g_has_cached;
os::Mutex g_mutex;
@@ -49,35 +49,35 @@ namespace sts::ams {
switch (static_cast<ams::TargetFirmware>(target_fw)) {
case ams::TargetFirmware_100:
g_firmware_version = FirmwareVersion_100;
g_hos_version = hos::Version_100;
break;
case ams::TargetFirmware_200:
g_firmware_version = FirmwareVersion_200;
g_hos_version = hos::Version_200;
break;
case ams::TargetFirmware_300:
g_firmware_version = FirmwareVersion_300;
g_hos_version = hos::Version_300;
break;
case ams::TargetFirmware_400:
g_firmware_version = FirmwareVersion_400;
g_hos_version = hos::Version_400;
break;
case ams::TargetFirmware_500:
g_firmware_version = FirmwareVersion_500;
g_hos_version = hos::Version_500;
break;
case ams::TargetFirmware_600:
case ams::TargetFirmware_620:
g_firmware_version = FirmwareVersion_600;
g_hos_version = hos::Version_600;
break;
case ams::TargetFirmware_700:
g_firmware_version = FirmwareVersion_700;
g_hos_version = hos::Version_700;
break;
case ams::TargetFirmware_800:
g_firmware_version = FirmwareVersion_800;
g_hos_version = hos::Version_800;
break;
case ams::TargetFirmware_810:
g_firmware_version = FirmwareVersion_810;
g_hos_version = hos::Version_810;
break;
case ams::TargetFirmware_900:
g_firmware_version = FirmwareVersion_900;
g_hos_version = hos::Version_900;
break;
STS_UNREACHABLE_DEFAULT_CASE();
}
@@ -87,60 +87,60 @@ namespace sts::ams {
}
FirmwareVersion GetRuntimeFirmwareVersion() {
::sts::hos::Version GetVersion() {
CacheValues();
return g_firmware_version;
return g_hos_version;
}
void SetFirmwareVersionForLibnx() {
void SetVersionForLibnx() {
u32 major = 0, minor = 0, micro = 0;
switch (GetRuntimeFirmwareVersion()) {
case FirmwareVersion_100:
switch (hos::GetVersion()) {
case hos::Version_100:
major = 1;
minor = 0;
micro = 0;
break;
case FirmwareVersion_200:
case hos::Version_200:
major = 2;
minor = 0;
micro = 0;
break;
case FirmwareVersion_300:
case hos::Version_300:
major = 3;
minor = 0;
micro = 0;
break;
case FirmwareVersion_400:
case hos::Version_400:
major = 4;
minor = 0;
micro = 0;
break;
case FirmwareVersion_500:
case hos::Version_500:
major = 5;
minor = 0;
micro = 0;
break;
case FirmwareVersion_600:
case hos::Version_600:
major = 6;
minor = 0;
micro = 0;
break;
case FirmwareVersion_700:
case hos::Version_700:
major = 7;
minor = 0;
micro = 0;
break;
case FirmwareVersion_800:
case hos::Version_800:
major = 8;
minor = 0;
micro = 0;
break;
case FirmwareVersion_810:
case hos::Version_810:
major = 8;
minor = 1;
micro = 0;
break;
case FirmwareVersion_900:
case hos::Version_900:
major = 9;
minor = 0;
micro = 0;

View File

@@ -23,26 +23,26 @@ namespace sts::cfg {
namespace {
/* Convenience definitions. */
constexpr u64 InitialProcessIdMinDeprecated = 0x00;
constexpr u64 InitialProcessIdMaxDeprecated = 0x50;
constexpr os::ProcessId InitialProcessIdMinDeprecated = {0x00};
constexpr os::ProcessId InitialProcessIdMaxDeprecated = {0x50};
/* Privileged process globals. */
os::Mutex g_lock;
bool g_got_privileged_process_status = false;
u64 g_min_initial_process_id = 0, g_max_initial_process_id = 0;
u64 g_cur_process_id = 0;
os::ProcessId g_min_initial_process_id = os::InvalidProcessId, g_max_initial_process_id = os::InvalidProcessId;
os::ProcessId g_cur_process_id = os::InvalidProcessId;
/* SD card helpers. */
void GetPrivilegedProcessIdRange(u64 *out_min, u64 *out_max) {
u64 min = 0, max = 0;
if (ams::GetRuntimeFirmwareVersion() >= FirmwareVersion_500) {
void GetPrivilegedProcessIdRange(os::ProcessId *out_min, os::ProcessId *out_max) {
os::ProcessId min = os::InvalidProcessId, max = os::InvalidProcessId;
if (hos::GetVersion() >= hos::Version_500) {
/* On 5.0.0+, we can get precise limits from svcGetSystemInfo. */
R_ASSERT(svcGetSystemInfo(&min, SystemInfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Minimum));
R_ASSERT(svcGetSystemInfo(&max, SystemInfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Maximum));
} else if (ams::GetRuntimeFirmwareVersion() >= FirmwareVersion_400) {
R_ASSERT(svcGetSystemInfo(reinterpret_cast<u64 *>(&min), SystemInfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Minimum));
R_ASSERT(svcGetSystemInfo(reinterpret_cast<u64 *>(&max), SystemInfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Maximum));
} else if (hos::GetVersion() >= hos::Version_400) {
/* On 4.0.0-4.1.0, we can get the precise limits from normal svcGetInfo. */
R_ASSERT(svcGetInfo(&min, InfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Minimum));
R_ASSERT(svcGetInfo(&max, InfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Maximum));
R_ASSERT(svcGetInfo(reinterpret_cast<u64 *>(&min), InfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Minimum));
R_ASSERT(svcGetInfo(reinterpret_cast<u64 *>(&max), InfoType_InitialProcessIdRange, INVALID_HANDLE, InitialProcessIdRangeInfo_Maximum));
} else {
/* On < 4.0.0, we just use hardcoded extents. */
min = InitialProcessIdMinDeprecated;
@@ -53,15 +53,9 @@ namespace sts::cfg {
*out_max = max;
}
u64 GetCurrentProcessId() {
u64 process_id = 0;
R_ASSERT(svcGetProcessId(&process_id, CUR_PROCESS_HANDLE));
return process_id;
}
void GetPrivilegedProcessStatus() {
GetPrivilegedProcessIdRange(&g_min_initial_process_id, &g_max_initial_process_id);
g_cur_process_id = GetCurrentProcessId();
g_cur_process_id = os::GetCurrentProcessId();
g_got_privileged_process_status = true;
}
@@ -80,7 +74,7 @@ namespace sts::cfg {
return g_min_initial_process_id <= g_cur_process_id && g_cur_process_id <= g_max_initial_process_id;
}
void GetInitialProcessRange(u64 *out_min, u64 *out_max) {
void GetInitialProcessRange(os::ProcessId *out_min, os::ProcessId *out_max) {
std::scoped_lock lk(g_lock);
/* If we've not detected, do detection. */

View File

@@ -184,7 +184,7 @@ namespace sts::map {
R_TRY(svcGetInfo(&out->heap_size, InfoType_HeapRegionSize, process_h, 0));
R_TRY(svcGetInfo(&out->alias_base, InfoType_AliasRegionAddress, process_h, 0));
R_TRY(svcGetInfo(&out->alias_size, InfoType_AliasRegionSize, process_h, 0));
if (ams::GetRuntimeFirmwareVersion() >= FirmwareVersion_200) {
if (hos::GetVersion() >= hos::Version_200) {
R_TRY(svcGetInfo(&out->aslr_base, InfoType_AslrRegionAddress, process_h, 0));
R_TRY(svcGetInfo(&out->aslr_size, InfoType_AslrRegionSize, process_h, 0));
} else {
@@ -205,7 +205,7 @@ namespace sts::map {
}
Result LocateMappableSpace(uintptr_t *out_address, size_t size) {
if (ams::GetRuntimeFirmwareVersion() >= FirmwareVersion_200) {
if (hos::GetVersion() >= hos::Version_200) {
return LocateMappableSpaceModern(out_address, size);
} else {
return LocateMappableSpaceDeprecated(out_address, size);
@@ -213,7 +213,7 @@ namespace sts::map {
}
Result MapCodeMemoryInProcess(MappedCodeMemory &out_mcm, Handle process_handle, uintptr_t base_address, size_t size) {
if (ams::GetRuntimeFirmwareVersion() >= FirmwareVersion_200) {
if (hos::GetVersion() >= hos::Version_200) {
return MapCodeMemoryInProcessModern(out_mcm, process_handle, base_address, size);
} else {
return MapCodeMemoryInProcessDeprecated(out_mcm, process_handle, base_address, size);

View File

@@ -0,0 +1,108 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* 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>
namespace sts::sf::cmif {
Result impl::ServiceDispatchTableBase::ProcessMessageImpl(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data, const ServiceCommandMeta *entries, const size_t entry_count) const {
/* Get versioning info. */
const auto hos_version = hos::GetVersion();
const u32 max_cmif_version = hos_version >= hos::Version_500 ? 1 : 0;
/* Parse the CMIF in header. */
const CmifInHeader *in_header = reinterpret_cast<const CmifInHeader *>(in_raw_data.GetPointer());
R_UNLESS(in_raw_data.GetSize() >= sizeof(*in_header), ResultServiceFrameworkInvalidCmifHeaderSize);
R_UNLESS(in_header->magic == CMIF_IN_HEADER_MAGIC && in_header->version <= max_cmif_version, ResultServiceFrameworkInvalidCmifInHeader);
const u32 cmd_id = in_header->command_id;
/* Find a handler. */
decltype(ServiceCommandMeta::handler) cmd_handler = nullptr;
for (size_t i = 0; i < entry_count; i++) {
if (entries[i].Matches(cmd_id, hos_version)) {
cmd_handler = entries[i].GetHandler();
break;
}
}
R_UNLESS(cmd_handler != nullptr, ResultServiceFrameworkUnknownCmifCommandId);
/* Invoke handler. */
CmifOutHeader *out_header = nullptr;
Result command_result = cmd_handler(&out_header, ctx, cmif::PointerAndSize(in_raw_data.GetAddress() + sizeof(*out_header), in_raw_data.GetSize() - sizeof(*out_header)));
/* Forward forwardable results, otherwise ensure we can send result to user. */
R_TRY_CATCH(command_result) {
R_CATCH(ResultServiceFrameworkRequestDeferredByUser) { return ResultServiceFrameworkRequestDeferredByUser; }
R_CATCH_ALL() { STS_ASSERT(out_header != nullptr); }
} R_END_TRY_CATCH;
/* Write output header to raw data. */
if (out_header != nullptr) {
*out_header = CmifOutHeader{CMIF_OUT_HEADER_MAGIC, 0, command_result, 0};
}
return ResultSuccess;
}
Result impl::ServiceDispatchTableBase::ProcessMessageForMitmImpl(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data, const ServiceCommandMeta *entries, const size_t entry_count) const {
/* Get versioning info. */
const auto hos_version = hos::GetVersion();
const u32 max_cmif_version = hos_version >= hos::Version_500 ? 1 : 0;
/* Parse the CMIF in header. */
const CmifInHeader *in_header = reinterpret_cast<const CmifInHeader *>(in_raw_data.GetPointer());
R_UNLESS(in_raw_data.GetSize() >= sizeof(*in_header), ResultServiceFrameworkInvalidCmifHeaderSize);
R_UNLESS(in_header->magic == CMIF_IN_HEADER_MAGIC && in_header->version <= max_cmif_version, ResultServiceFrameworkInvalidCmifInHeader);
const u32 cmd_id = in_header->command_id;
/* Find a handler. */
decltype(ServiceCommandMeta::handler) cmd_handler = nullptr;
for (size_t i = 0; i < entry_count; i++) {
if (entries[i].Matches(cmd_id, hos_version)) {
cmd_handler = entries[i].GetHandler();
break;
}
}
/* If we didn't find a handler, forward the request. */
if (cmd_handler == nullptr) {
/* TODO: FORWARD REQUEST */
STS_ASSERT(false);
}
/* Invoke handler. */
CmifOutHeader *out_header = nullptr;
Result command_result = cmd_handler(&out_header, ctx, cmif::PointerAndSize(in_raw_data.GetAddress() + sizeof(*out_header), in_raw_data.GetSize() - sizeof(*out_header)));
/* Forward forwardable results, otherwise ensure we can send result to user. */
R_TRY_CATCH(command_result) {
R_CATCH(ResultServiceFrameworkRequestDeferredByUser) { return ResultServiceFrameworkRequestDeferredByUser; }
R_CATCH(ResultAtmosphereMitmShouldForwardToSession) {
/* TODO: Restore TLS. */
/* TODO: FORWARD REQUEST */
STS_ASSERT(false);
}
R_CATCH_ALL() { STS_ASSERT(out_header != nullptr); }
} R_END_TRY_CATCH;
/* Write output header to raw data. */
if (out_header != nullptr) {
*out_header = CmifOutHeader{CMIF_OUT_HEADER_MAGIC, 0, command_result, 0};
}
return ResultSuccess;
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* 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 <functional>
#include <stratosphere.hpp>
namespace sts::sf::cmif {
Result ServiceObjectHolder::ProcessMessage(ServiceDispatchContext &ctx, const cmif::PointerAndSize &in_raw_data) const {
const auto ProcessHandler = this->dispatch_meta->ProcessHandler;
const auto *DispatchTable = this->dispatch_meta->DispatchTable;
return (DispatchTable->*ProcessHandler)(ctx, in_raw_data);
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* 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>
namespace sts::sf::hipc {
namespace {
NX_INLINE Result ReceiveImpl(Handle session_handle, void *message_buf, size_t message_buf_size) {
s32 unused_index;
if (message_buf == armGetTls()) {
/* Consider: STS_ASSERT(message_buf_size == TlsMessageBufferSize); */
return svcReplyAndReceive(&unused_index, &session_handle, 1, INVALID_HANDLE, U64_MAX);
} else {
return svcReplyAndReceiveWithUserBuffer(&unused_index, message_buf, message_buf_size, &session_handle, 1, INVALID_HANDLE, U64_MAX);
}
}
NX_INLINE Result ReplyImpl(Handle session_handle, void *message_buf, size_t message_buf_size) {
s32 unused_index;
if (message_buf == armGetTls()) {
/* Consider: STS_ASSERT(message_buf_size == TlsMessageBufferSize); */
return svcReplyAndReceive(&unused_index, &session_handle, 0, session_handle, 0);
} else {
return svcReplyAndReceiveWithUserBuffer(&unused_index, message_buf, message_buf_size, &session_handle, 0, session_handle, 0);
}
}
}
Result Receive(ReceiveResult *out_recv_result, Handle session_handle, const cmif::PointerAndSize &message_buffer) {
R_TRY_CATCH(ReceiveImpl(session_handle, message_buffer.GetPointer(), message_buffer.GetSize())) {
R_CATCH(ResultKernelConnectionClosed) {
*out_recv_result = ReceiveResult::Closed;
return ResultSuccess;
}
R_CATCH(ResultKernelReceiveListBroken) {
*out_recv_result = ReceiveResult::NeedsRetry;
return ResultSuccess;
}
} R_END_TRY_CATCH;
*out_recv_result = ReceiveResult::Success;
return ResultSuccess;
}
Result Receive(bool *out_closed, Handle session_handle, const cmif::PointerAndSize &message_buffer) {
R_TRY_CATCH(ReceiveImpl(session_handle, message_buffer.GetPointer(), message_buffer.GetSize())) {
R_CATCH(ResultKernelConnectionClosed) {
*out_closed = true;
return ResultSuccess;
}
} R_END_TRY_CATCH;
*out_closed = false;
return ResultSuccess;
}
Result Reply(Handle session_handle, const cmif::PointerAndSize &message_buffer) {
R_TRY_CATCH(ReplyImpl(session_handle, message_buffer.GetPointer(), message_buffer.GetSize())) {
R_CATCH(ResultKernelTimedOut) { return ResultSuccess; }
R_CATCH(ResultKernelConnectionClosed) { return ResultSuccess; }
} R_END_TRY_CATCH;
/* ReplyImpl should *always* return an error. */
STS_ASSERT(false);
}
Result CreateSession(Handle *out_server_handle, Handle *out_client_handle) {
R_TRY_CATCH(svcCreateSession(out_server_handle, out_client_handle, 0, 0)) {
R_CATCH(ResultKernelResourceExhausted) { return ResultHipcOutOfSessions; }
} R_END_TRY_CATCH;
return ResultSuccess;
}
}

View File

@@ -0,0 +1,165 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* 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>
namespace sts::sf::hipc {
ServerManagerBase::ServerBase::~ServerBase() { /* Pure virtual destructor, to prevent linker errors. */ }
void ServerManagerBase::RegisterSessionToWaitList(ServerSession *session) {
session->has_received = false;
/* Set user data tag. */
session->SetUserData(static_cast<uintptr_t>(UserDataTag::Session));
this->RegisterToWaitList(session);
}
void ServerManagerBase::RegisterToWaitList(os::WaitableHolder *holder) {
std::scoped_lock lk(this->waitlist_mutex);
this->waitlist.LinkWaitableHolder(holder);
this->notify_event.Signal();
}
void ServerManagerBase::ProcessWaitList() {
std::scoped_lock lk(this->waitlist_mutex);
this->waitable_manager.MoveAllFrom(&this->waitlist);
}
os::WaitableHolder *ServerManagerBase::WaitSignaled() {
std::scoped_lock lk(this->waitable_selection_mutex);
while (true) {
this->ProcessWaitList();
auto selected = this->waitable_manager.WaitAny();
if (selected == &this->request_stop_event_holder) {
return nullptr;
} else if (selected == &this->notify_event_holder) {
this->notify_event.Reset();
} else {
selected->UnlinkFromWaitableManager();
return selected;
}
}
}
void ServerManagerBase::ResumeProcessing() {
this->request_stop_event.Reset();
}
void ServerManagerBase::RequestStopProcessing() {
this->request_stop_event.Signal();
}
void ServerManagerBase::AddUserWaitableHolder(os::WaitableHolder *waitable) {
const auto user_data_tag = static_cast<UserDataTag>(waitable->GetUserData());
STS_ASSERT(user_data_tag != UserDataTag::Server);
STS_ASSERT(user_data_tag != UserDataTag::MitmServer);
STS_ASSERT(user_data_tag != UserDataTag::Session);
this->RegisterToWaitList(waitable);
}
Result ServerManagerBase::ProcessForServer(os::WaitableHolder *holder) {
STS_ASSERT(static_cast<UserDataTag>(holder->GetUserData()) == UserDataTag::Server);
ServerBase *server = static_cast<ServerBase *>(holder);
ON_SCOPE_EXIT { this->RegisterToWaitList(server); };
/* Create resources for new session. */
cmif::ServiceObjectHolder obj;
std::shared_ptr<::Service> fsrv;
server->CreateSessionObjectHolder(&obj, &fsrv);
/* Not a mitm server, so we must have no forward service. */
STS_ASSERT(fsrv == nullptr);
/* Try to accept. */
return this->AcceptSession(server->port_handle, std::move(obj));
}
Result ServerManagerBase::ProcessForMitmServer(os::WaitableHolder *holder) {
STS_ASSERT(static_cast<UserDataTag>(holder->GetUserData()) == UserDataTag::MitmServer);
ServerBase *server = static_cast<ServerBase *>(holder);
ON_SCOPE_EXIT { this->RegisterToWaitList(server); };
/* Create resources for new session. */
cmif::ServiceObjectHolder obj;
std::shared_ptr<::Service> fsrv;
server->CreateSessionObjectHolder(&obj, &fsrv);
/* Mitm server, so we must have forward service. */
STS_ASSERT(fsrv != nullptr);
/* Try to accept. */
return this->AcceptMitmSession(server->port_handle, std::move(obj), std::move(fsrv));
}
Result ServerManagerBase::ProcessForSession(os::WaitableHolder *holder) {
STS_ASSERT(static_cast<UserDataTag>(holder->GetUserData()) == UserDataTag::Session);
ServerSession *session = static_cast<ServerSession *>(holder);
cmif::PointerAndSize tls_message(armGetTls(), hipc::TlsMessageBufferSize);
const cmif::PointerAndSize &saved_message = session->saved_message;
STS_ASSERT(tls_message.GetSize() == saved_message.GetSize());
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());
}
return this->ProcessRequest(session, tls_message);
}
Result ServerManagerBase::Process(os::WaitableHolder *holder) {
switch (static_cast<UserDataTag>(holder->GetUserData())) {
case UserDataTag::Server:
return this->ProcessForServer(holder);
break;
case UserDataTag::MitmServer:
return this->ProcessForMitmServer(holder);
break;
case UserDataTag::Session:
/* TODO: Process deferred. */
return this->ProcessForSession(holder);
break;
STS_UNREACHABLE_DEFAULT_CASE();
}
}
bool ServerManagerBase::WaitAndProcessImpl() {
auto waitable = this->WaitSignaled();
if (!waitable) {
return false;
}
R_ASSERT(this->Process(waitable));
return true;
}
void ServerManagerBase::WaitAndProcess() {
this->WaitAndProcessImpl();
}
void ServerManagerBase::LoopProcess() {
while (this->WaitAndProcessImpl()) {
/* ... */
}
}
}

View File

@@ -0,0 +1,246 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* 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>
namespace sts::sf::hipc {
void ServerSessionManager::DestroySession(ServerSession *session) {
/* Destroy object. */
session->~ServerSession();
/* Free object memory. */
this->FreeSession(session);
}
void ServerSessionManager::CloseSessionImpl(ServerSession *session) {
const Handle session_handle = session->session_handle;
this->DestroySession(session);
R_ASSERT(svcCloseHandle(session_handle));
}
Result ServerSessionManager::RegisterSessionImpl(ServerSession *session_memory, Handle session_handle, cmif::ServiceObjectHolder &&obj) {
/* Create session object. */
new (session_memory) ServerSession(session_handle, std::forward<cmif::ServiceObjectHolder>(obj));
/* Assign session resources. */
session_memory->pointer_buffer = this->GetSessionPointerBuffer(session_memory);
session_memory->saved_message = this->GetSessionSavedMessageBuffer(session_memory);
/* Register to wait list. */
this->RegisterSessionToWaitList(session_memory);
return ResultSuccess;
}
Result ServerSessionManager::AcceptSessionImpl(ServerSession *session_memory, Handle port_handle, cmif::ServiceObjectHolder &&obj) {
/* Create session handle. */
Handle session_handle;
R_TRY(svcAcceptSession(&session_handle, port_handle));
bool succeeded = false;
ON_SCOPE_EXIT {
if (!succeeded) {
R_ASSERT(svcCloseHandle(session_handle));
}
};
/* Register session. */
R_TRY(this->RegisterSessionImpl(session_memory, session_handle, std::forward<cmif::ServiceObjectHolder>(obj)));
succeeded = true;
return ResultSuccess;
}
Result ServerSessionManager::RegisterMitmSessionImpl(ServerSession *session_memory, Handle mitm_session_handle, cmif::ServiceObjectHolder &&obj, std::shared_ptr<::Service> &&fsrv) {
/* Create session object. */
new (session_memory) ServerSession(mitm_session_handle, std::forward<cmif::ServiceObjectHolder>(obj), std::forward<std::shared_ptr<::Service>>(fsrv));
/* Assign session resources. */
session_memory->pointer_buffer = this->GetSessionPointerBuffer(session_memory);
session_memory->saved_message = this->GetSessionSavedMessageBuffer(session_memory);
/* Register to wait list. */
this->RegisterSessionToWaitList(session_memory);
return ResultSuccess;
}
Result ServerSessionManager::AcceptMitmSessionImpl(ServerSession *session_memory, Handle mitm_port_handle, cmif::ServiceObjectHolder &&obj, std::shared_ptr<::Service> &&fsrv) {
/* Create session handle. */
Handle mitm_session_handle;
R_TRY(svcAcceptSession(&mitm_session_handle, mitm_port_handle));
bool succeeded = false;
ON_SCOPE_EXIT {
if (!succeeded) {
R_ASSERT(svcCloseHandle(mitm_session_handle));
}
};
/* Register session. */
R_TRY(this->RegisterMitmSessionImpl(session_memory, mitm_session_handle, std::forward<cmif::ServiceObjectHolder>(obj), std::forward<std::shared_ptr<::Service>>(fsrv)));
succeeded = true;
return ResultSuccess;
}
Result ServerSessionManager::RegisterSession(Handle session_handle, cmif::ServiceObjectHolder &&obj) {
/* We don't actually care about what happens to the session. It'll get linked. */
ServerSession *session_ptr = nullptr;
return this->RegisterSession(&session_ptr, session_handle,std::forward<cmif::ServiceObjectHolder>(obj));
}
Result ServerSessionManager::AcceptSession(Handle port_handle, cmif::ServiceObjectHolder &&obj) {
/* We don't actually care about what happens to the session. It'll get linked. */
ServerSession *session_ptr = nullptr;
return this->AcceptSession(&session_ptr, port_handle, std::forward<cmif::ServiceObjectHolder>(obj));
}
Result ServerSessionManager::RegisterMitmSession(Handle mitm_session_handle, cmif::ServiceObjectHolder &&obj, std::shared_ptr<::Service> &&fsrv) {
/* We don't actually care about what happens to the session. It'll get linked. */
ServerSession *session_ptr = nullptr;
return this->RegisterMitmSession(&session_ptr, mitm_session_handle, std::forward<cmif::ServiceObjectHolder>(obj), std::forward<std::shared_ptr<::Service>>(fsrv));
}
Result ServerSessionManager::AcceptMitmSession(Handle mitm_port_handle, cmif::ServiceObjectHolder &&obj, std::shared_ptr<::Service> &&fsrv) {
/* We don't actually care about what happens to the session. It'll get linked. */
ServerSession *session_ptr = nullptr;
return this->AcceptMitmSession(&session_ptr, mitm_port_handle, std::forward<cmif::ServiceObjectHolder>(obj), std::forward<std::shared_ptr<::Service>>(fsrv));
}
Result ServerSessionManager::ReceiveRequestImpl(ServerSession *session, const cmif::PointerAndSize &message) {
const cmif::PointerAndSize &pointer_buffer = session->pointer_buffer;
/* If the receive list is odd, we may need to receive repeatedly. */
while (true) {
if (pointer_buffer.GetPointer()) {
hipcMakeRequestInline(message.GetPointer(),
.type = CmifCommandType_Invalid,
.num_recv_statics = HIPC_AUTO_RECV_STATIC,
).recv_list[0] = hipcMakeRecvStatic(pointer_buffer.GetPointer(), pointer_buffer.GetSize());
} else {
hipcMakeRequestInline(message.GetPointer(),
.type = CmifCommandType_Invalid,
);
}
hipc::ReceiveResult recv_result;
R_TRY(hipc::Receive(&recv_result, session->session_handle, message));
switch (recv_result) {
case hipc::ReceiveResult::Success:
session->is_closed = false;
return ResultSuccess;
case hipc::ReceiveResult::Closed:
session->is_closed = true;
return ResultSuccess;
case hipc::ReceiveResult::NeedsRetry:
continue;
STS_UNREACHABLE_DEFAULT_CASE();
}
}
}
namespace {
NX_CONSTEXPR u32 GetCmifCommandType(const cmif::PointerAndSize &message) {
HipcHeader hdr = {};
__builtin_memcpy(&hdr, message.GetPointer(), sizeof(hdr));
return hdr.type;
}
}
Result ServerSessionManager::ProcessRequest(ServerSession *session, const cmif::PointerAndSize &message) {
if (session->is_closed) {
this->CloseSessionImpl(session);
return ResultSuccess;
}
switch (GetCmifCommandType(message)) {
case CmifCommandType_Close:
{
this->CloseSessionImpl(session);
return ResultSuccess;
}
default:
{
R_TRY_CATCH(this->ProcessRequestImpl(session, message, message)) {
R_CATCH(ResultServiceFrameworkRequestDeferredByUser) { /* TODO: Properly include entire range Nintendo does */
return ResultServiceFrameworkRequestDeferredByUser;
}
R_CATCH_ALL() {
/* All other results indicate something went very wrong. */
this->CloseSessionImpl(session);
return ResultSuccess;
}
} R_END_TRY_CATCH;
/* We succeeded, so we can process future messages on this session. */
this->RegisterSessionToWaitList(session);
return ResultSuccess;
}
}
}
Result ServerSessionManager::ProcessRequestImpl(ServerSession *session, const cmif::PointerAndSize &in_message, const cmif::PointerAndSize &out_message) {
/* TODO: Inline context support, retrieve from raw data + 0xC. */
const auto cmif_command_type = GetCmifCommandType(in_message);
switch (cmif_command_type) {
case CmifCommandType_Request:
case CmifCommandType_RequestWithContext:
return this->DispatchRequest(session->srv_obj_holder.Clone(), session, in_message, out_message);
case CmifCommandType_Control:
case CmifCommandType_ControlWithContext:
return this->DispatchManagerRequest(session, in_message, out_message);
default:
return ResultHipcUnknownCommandType;
}
}
Result ServerSessionManager::DispatchManagerRequest(ServerSession *session, const cmif::PointerAndSize &in_message, const cmif::PointerAndSize &out_message) {
/* This will get overridden by ... WithDomain class. */
return ResultServiceFrameworkNotSupported;
}
Result ServerSessionManager::DispatchRequest(cmif::ServiceObjectHolder &&obj_holder, ServerSession *session, const cmif::PointerAndSize &in_message, const cmif::PointerAndSize &out_message) {
/* Create request context. */
cmif::HandlesToClose handles_to_close = {};
cmif::ServiceDispatchContext dispatch_ctx = {
.srv_obj = obj_holder.GetServiceObjectUnsafe(),
.manager = this,
.processor = nullptr, /* Filled in by template implementations. */
.handles_to_close = &handles_to_close,
.pointer_buffer = session->pointer_buffer,
.in_message_buffer = in_message,
.out_message_buffer = out_message,
.request = hipcParseRequest(in_message.GetPointer()),
};
/* Validate message sizes. */
const uintptr_t in_message_buffer_end = in_message.GetAddress() + in_message.GetSize();
const uintptr_t in_raw_addr = reinterpret_cast<uintptr_t>(dispatch_ctx.request.data.data_words);
const size_t in_raw_size = dispatch_ctx.request.meta.num_data_words * sizeof(u32);
/* Note: Nintendo does not validate this size before subtracting 0x10 from it. This is not exploitable. */
R_UNLESS(in_raw_size >= 0x10, ResultHipcInvalidRequestSize);
R_UNLESS(in_raw_addr + in_raw_size <= in_message_buffer_end, ResultHipcInvalidRequestSize);
const uintptr_t recv_list_end = reinterpret_cast<uintptr_t>(dispatch_ctx.request.data.recv_list + dispatch_ctx.request.meta.num_recv_statics);
R_UNLESS(recv_list_end <= in_message_buffer_end, ResultHipcInvalidRequestSize);
/* CMIF has 0x10 of padding in raw data, and requires 0x10 alignment. */
const cmif::PointerAndSize in_raw_data(util::AlignUp(in_raw_addr, 0x10), in_raw_size - 0x10);
/* Invoke command handler. */
R_TRY(obj_holder.ProcessMessage(dispatch_ctx, in_raw_data));
/* Reply. */
{
ON_SCOPE_EXIT {
for (size_t i = 0; i < handles_to_close.num_handles; i++) {
R_ASSERT(svcCloseHandle(handles_to_close.handles[i]));
}
};
R_TRY(hipc::Reply(session->session_handle, out_message));
}
return ResultSuccess;
}
}

View File

@@ -42,9 +42,9 @@ namespace sts::sm::mitm {
});
}
Result AcknowledgeSession(Service *out_service, u64 *out_pid, ncm::TitleId *out_tid, ServiceName name) {
Result AcknowledgeSession(Service *out_service, os::ProcessId *out_pid, ncm::TitleId *out_tid, ServiceName name) {
return impl::DoWithMitmSession([&]() {
return smAtmosphereMitmAcknowledgeSession(out_service, out_pid, &out_tid->value, name.name);
return smAtmosphereMitmAcknowledgeSession(out_service, &out_pid->value, &out_tid->value, name.name);
});
}