pgl: update to use tipc (untested)

This commit is contained in:
Michael Scire 2021-04-11 01:07:55 -07:00 committed by SciresM
parent 1118421fa6
commit b2b0c50802
16 changed files with 600 additions and 130 deletions

View file

@ -40,7 +40,20 @@ namespace ams::pgl {
static_assert(sizeof(*out.GetPointer()) == sizeof(::PmProcessEventInfo));
return ::pglEventObserverGetProcessEventInfo(std::addressof(this->observer), reinterpret_cast<::PmProcessEventInfo *>(out.GetPointer()));
}
Result GetProcessEventHandle(ams::tipc::OutCopyHandle out) {
::Event ev;
R_TRY(::pglEventObserverGetProcessEvent(std::addressof(this->observer), std::addressof(ev)));
out.SetValue(ev.revent);
return ResultSuccess();
}
Result GetProcessEventInfo(ams::tipc::Out<pm::ProcessEventInfo> out) {
static_assert(sizeof(*out.GetPointer()) == sizeof(::PmProcessEventInfo));
return ::pglEventObserverGetProcessEventInfo(std::addressof(this->observer), reinterpret_cast<::PmProcessEventInfo *>(out.GetPointer()));
}
};
static_assert(pgl::sf::IsIEventObserver<RemoteEventObserver>);
static_assert(pgl::tipc::IsIEventObserver<RemoteEventObserver>);
}

View file

@ -80,10 +80,21 @@ namespace ams::pgl {
R_TRY(::pglGetEventObserver(std::addressof(obs)));
/* TODO: Real allocator */
auto remote_observer = ams::sf::CreateSharedObjectEmplaced<pgl::sf::IEventObserver, RemoteEventObserver>(obs);
R_UNLESS(remote_observer != nullptr, pgl::ResultOutOfMemory());
if (hos::GetVersion() >= hos::Version_12_0_0) {
auto observer_holder = std::make_unique<impl::EventObserverByTipc<RemoteEventObserver>>(obs);
R_UNLESS(observer_holder != nullptr, pgl::ResultOutOfMemory());
*out = pgl::EventObserver(std::move(observer_holder));
} else {
auto remote_observer = ams::sf::CreateSharedObjectEmplaced<pgl::sf::IEventObserver, RemoteEventObserver>(obs);
R_UNLESS(remote_observer != nullptr, pgl::ResultOutOfMemory());
auto observer_holder = std::make_unique<impl::EventObserverByCmif>(std::move(remote_observer));
R_UNLESS(observer_holder != nullptr, pgl::ResultOutOfMemory());
*out = pgl::EventObserver(std::move(observer_holder));
}
*out = pgl::EventObserver(std::move(remote_observer));
return ResultSuccess();
}

View file

@ -15,10 +15,145 @@
*/
#include <stratosphere.hpp>
#include "pgl_srv_shell.hpp"
#include "pgl_srv_shell_event_observer.hpp"
#include "pgl_srv_tipc_utils.hpp"
namespace ams::pgl::srv {
void Initialize() {
namespace {
/* pgl. */
enum PortIndex {
PortIndex_Shell,
PortIndex_Count,
};
constexpr sm::ServiceName ShellServiceName = sm::ServiceName::Encode("pgl");
constexpr size_t ShellMaxSessions = 8; /* Official maximum is 8. */
using CmifServerManager = ams::sf::hipc::ServerManager<PortIndex_Count>;
constexpr size_t ObserverMaxSessions = 4;
using ShellPortMeta = ams::tipc::PortMeta<ShellMaxSessions + ObserverMaxSessions, pgl::tipc::IShellInterface, pgl::srv::ShellInterfaceTipc, ams::tipc::SingletonAllocator>;
using TipcServerManager = ams::tipc::ServerManager<ShellPortMeta>;
/* NOTE: Nintendo reserves only 0x2000 bytes for heap, which is used "mostly" to allocate shell event observers. */
/* However, we would like very much for homebrew sysmodules to be able to subscribe to events if they so choose */
/* And so we will use a larger heap (32 KB). Note that we reduce the heap size for tipc, where objects are */
/* allocated statically. */
/* We should have a smaller memory footprint than N in the end, regardless. */
struct CmifGlobals {
u8 heap_memory[32_KB];
lmem::HeapHandle heap_handle;
ams::sf::ExpHeapAllocator server_allocator;
ams::sf::UnmanagedServiceObject<pgl::sf::IShellInterface, pgl::srv::ShellInterfaceCmif> shell_interface{std::addressof(server_allocator)};
CmifServerManager server_manager;
};
struct TipcGlobals {
u8 heap_memory[24_KB];
lmem::HeapHandle heap_handle;
TipcServerManager server_manager;
ams::tipc::SlabAllocator<ams::tipc::ServiceObject<pgl::tipc::IEventObserver, pgl::srv::ShellEventObserverTipc>, ObserverMaxSessions> observer_allocator;
};
constinit union {
util::TypedStorage<CmifGlobals> cmif;
util::TypedStorage<TipcGlobals> tipc;
} g_globals;
ALWAYS_INLINE CmifGlobals &GetGlobalsForCmif() {
return GetReference(g_globals.cmif);
}
ALWAYS_INLINE TipcGlobals &GetGlobalsForTipc() {
return GetReference(g_globals.tipc);
}
ALWAYS_INLINE bool UseTipcServer() {
return hos::GetVersion() >= hos::Version_12_0_0;
}
ALWAYS_INLINE lmem::HeapHandle GetHeapHandle() {
if (UseTipcServer()) {
return GetGlobalsForTipc().heap_handle;
} else {
return GetGlobalsForCmif().heap_handle;
}
}
template<typename T>
ALWAYS_INLINE void InitializeHeapImpl(util::TypedStorage<T> &globals_storage) {
/* Construct the globals object. */
util::ConstructAt(globals_storage);
/* Get reference to the globals. */
auto &globals = GetReference(globals_storage);
/* Set the heap handle. */
globals.heap_handle = lmem::CreateExpHeap(globals.heap_memory, sizeof(globals.heap_memory), lmem::CreateOption_ThreadSafe);
/* If we should, setup the server allocator. */
if constexpr (requires (T &t) { t.server_allocator; }) {
globals.server_allocator.Attach(globals.heap_handle);
}
}
void RegisterServiceSession() {
/* Register "pgl" with the appropriate server manager. */
if (UseTipcServer()) {
/* Get the globals. */
auto &globals = GetGlobalsForTipc();
/* Initialize the server manager. */
globals.server_manager.Initialize();
/* Register the pgl service. */
globals.server_manager.RegisterPort<PortIndex_Shell>(ShellServiceName, ShellMaxSessions);
} else {
/* Get the globals. */
auto &globals = GetGlobalsForCmif();
/* Register the shell server with the cmif server manager. */
R_ABORT_UNLESS(globals.server_manager.RegisterObjectForServer(globals.shell_interface.GetShared(), ShellServiceName, ShellMaxSessions));
}
}
void LoopProcessServer() {
/* Loop processing for the appropriate server manager. */
if (UseTipcServer()) {
GetGlobalsForTipc().server_manager.LoopAuto();
} else {
GetGlobalsForCmif().server_manager.LoopProcess();
}
}
}
void InitializeHeap() {
/* Initialize the heap (and construct the globals object) for the appropriate ipc protocol. */
if (UseTipcServer()) {
/* We're servicing via tipc. */
InitializeHeapImpl(g_globals.tipc);
} else {
/* We're servicing via cmif. */
InitializeHeapImpl(g_globals.cmif);
}
}
void *Allocate(size_t size) {
return lmem::AllocateFromExpHeap(GetHeapHandle(), size);
}
void Deallocate(void *p, size_t size) {
return lmem::FreeToExpHeap(GetHeapHandle(), p);
}
void StartServer() {
/* Enable extra application threads, if we should. */
u8 enable_application_extra_thread;
const size_t sz = settings::fwdbg::GetSettingsItemValue(std::addressof(enable_application_extra_thread), sizeof(enable_application_extra_thread), "application_extra_thread", "enable_application_extra_thread");
@ -27,9 +162,45 @@ namespace ams::pgl::srv {
pm::shell::EnableApplicationExtraThread();
}
/* Register service session. */
RegisterServiceSession();
/* Start the Process Tracking thread. */
pgl::srv::InitializeProcessControlTask();
/* TODO: Loop process. */
LoopProcessServer();
}
Result AllocateShellEventObserverForTipc(svc::Handle *out) {
/* Get the shell event observer allocator. */
auto &allocator = GetGlobalsForTipc().observer_allocator;
/* Allocate an object. */
auto *object = allocator.Allocate();
R_UNLESS(object != nullptr, pgl::ResultOutOfMemory());
/* Set the object's deleter. */
object->SetDeleter(std::addressof(allocator));
/* Add the session to the server manager. */
/* NOTE: If this fails, the object will be leaked. */
/* TODO: Should we avoid leaking the object? Nintendo does not. */
R_TRY(GetGlobalsForTipc().server_manager.AddSession(out, object));
return ResultSuccess();
}
ams::tipc::ServiceObjectBase *AllocateShellEventObserverForTipc() {
auto &allocator = GetGlobalsForTipc().observer_allocator;
auto *object = allocator.Allocate();
if (object != nullptr) {
object->SetDeleter(std::addressof(allocator));
}
return object;
}
}

View file

@ -19,18 +19,18 @@
namespace ams::pgl::srv {
ShellEventObserver::ShellEventObserver() : message_queue(queue_buffer, QueueCapacity), event(os::EventClearMode_AutoClear, true) {
ShellEventObserverImpl::ShellEventObserverImpl() : message_queue(queue_buffer, QueueCapacity), event(os::EventClearMode_AutoClear, true) {
this->heap_handle = lmem::CreateUnitHeap(this->event_info_data, sizeof(this->event_info_data), sizeof(this->event_info_data[0]), lmem::CreateOption_ThreadSafe, 8, GetPointer(this->heap_head));
RegisterShellEventObserver(util::ConstructAt(this->holder, this));
}
ShellEventObserver::~ShellEventObserver() {
ShellEventObserverImpl::~ShellEventObserverImpl() {
UnregisterShellEventObserver(GetPointer(this->holder));
util::DestroyAt(this->holder);
}
Result ShellEventObserver::PopEventInfo(pm::ProcessEventInfo *out) {
Result ShellEventObserverImpl::PopEventInfo(pm::ProcessEventInfo *out) {
/* Receive an info from the queue. */
uintptr_t info_address;
R_UNLESS(this->message_queue.TryReceive(std::addressof(info_address)), pgl::ResultNotAvailable());
@ -45,7 +45,7 @@ namespace ams::pgl::srv {
return ResultSuccess();
}
void ShellEventObserver::Notify(const pm::ProcessEventInfo &info) {
void ShellEventObserverImpl::Notify(const pm::ProcessEventInfo &info) {
/* Allocate a new info. */
auto allocated = reinterpret_cast<pm::ProcessEventInfo *>(lmem::AllocateFromUnitHeap(this->heap_handle));
if (!allocated) {
@ -65,12 +65,21 @@ namespace ams::pgl::srv {
this->event.Signal();
}
Result ShellEventObserver::GetProcessEventHandle(ams::sf::OutCopyHandle out) {
Result ShellEventObserverCmif::GetProcessEventHandle(ams::sf::OutCopyHandle out) {
out.SetValue(this->GetEvent().GetReadableHandle());
return ResultSuccess();
}
Result ShellEventObserver::GetProcessEventInfo(ams::sf::Out<pm::ProcessEventInfo> out) {
Result ShellEventObserverCmif::GetProcessEventInfo(ams::sf::Out<pm::ProcessEventInfo> out) {
return this->PopEventInfo(out.GetPointer());
}
Result ShellEventObserverTipc::GetProcessEventHandle(ams::tipc::OutCopyHandle out) {
out.SetValue(this->GetEvent().GetReadableHandle());
return ResultSuccess();
}
Result ShellEventObserverTipc::GetProcessEventInfo(ams::tipc::Out<pm::ProcessEventInfo> out) {
return this->PopEventInfo(out.GetPointer());
}

View file

@ -34,7 +34,7 @@ namespace ams::pgl::srv {
}
};
class ShellEventObserver : public IShellEventObserver {
class ShellEventObserverImpl : public IShellEventObserver {
private:
static constexpr size_t QueueCapacity = 0x20;
private:
@ -46,8 +46,8 @@ namespace ams::pgl::srv {
pm::ProcessEventInfo event_info_data[QueueCapacity];
util::TypedStorage<ShellEventObserverHolder> holder;
public:
ShellEventObserver();
~ShellEventObserver();
ShellEventObserverImpl();
~ShellEventObserverImpl();
os::SystemEvent &GetEvent() {
return this->event;
@ -56,10 +56,20 @@ namespace ams::pgl::srv {
Result PopEventInfo(pm::ProcessEventInfo *out);
virtual void Notify(const pm::ProcessEventInfo &info) override final;
};
class ShellEventObserverCmif : public ShellEventObserverImpl {
public:
Result GetProcessEventHandle(ams::sf::OutCopyHandle out);
Result GetProcessEventInfo(ams::sf::Out<pm::ProcessEventInfo> out);
};
static_assert(pgl::sf::IsIEventObserver<ShellEventObserver>);
static_assert(pgl::sf::IsIEventObserver<ShellEventObserverCmif>);
class ShellEventObserverTipc : public ShellEventObserverImpl {
public:
Result GetProcessEventHandle(ams::tipc::OutCopyHandle out);
Result GetProcessEventInfo(ams::tipc::Out<pm::ProcessEventInfo> out);
};
static_assert(pgl::tipc::IsIEventObserver<ShellEventObserverTipc>);
}

View file

@ -17,68 +17,157 @@
#include "pgl_srv_shell.hpp"
#include "pgl_srv_shell_event_observer.hpp"
#include "pgl_srv_shell_host_utils.hpp"
#include "pgl_srv_tipc_utils.hpp"
namespace ams::pgl::srv {
Result ShellInterface::LaunchProgram(ams::sf::Out<os::ProcessId> out, const ncm::ProgramLocation &loc, u32 pm_flags, u8 pgl_flags) {
return pgl::srv::LaunchProgram(out.GetPointer(), loc, pm_flags, pgl_flags);
Result ShellInterfaceCommon::LaunchProgramImpl(os::ProcessId *out, const ncm::ProgramLocation &loc, u32 pm_flags, u8 pgl_flags) {
return pgl::srv::LaunchProgram(out, loc, pm_flags, pgl_flags);
}
Result ShellInterface::TerminateProcess(os::ProcessId process_id) {
Result ShellInterfaceCommon::TerminateProcessImpl(os::ProcessId process_id) {
return pgl::srv::TerminateProcess(process_id);
}
Result ShellInterface::LaunchProgramFromHost(ams::sf::Out<os::ProcessId> out, const ams::sf::InBuffer &content_path, u32 pm_flags) {
return pgl::srv::LaunchProgramFromHost(out.GetPointer(), reinterpret_cast<const char *>(content_path.GetPointer()), pm_flags);
Result ShellInterfaceCommon::LaunchProgramFromHostImpl(os::ProcessId *out, const void *content_path, size_t content_path_size, u32 pm_flags) {
return pgl::srv::LaunchProgramFromHost(out, static_cast<const char *>(content_path), pm_flags);
}
Result ShellInterface::GetHostContentMetaInfo(ams::sf::Out<pgl::ContentMetaInfo> out, const ams::sf::InBuffer &content_path) {
return pgl::srv::GetHostContentMetaInfo(out.GetPointer(), reinterpret_cast<const char *>(content_path.GetPointer()));
Result ShellInterfaceCommon::GetHostContentMetaInfoImpl(pgl::ContentMetaInfo *out, const void *content_path, size_t content_path_size) {
return pgl::srv::GetHostContentMetaInfo(out, static_cast<const char *>(content_path));
}
Result ShellInterface::GetApplicationProcessId(ams::sf::Out<os::ProcessId> out) {
return pgl::srv::GetApplicationProcessId(out.GetPointer());
Result ShellInterfaceCommon::GetApplicationProcessIdImpl(os::ProcessId *out) {
return pgl::srv::GetApplicationProcessId(out);
}
Result ShellInterface::BoostSystemMemoryResourceLimit(u64 size) {
Result ShellInterfaceCommon::BoostSystemMemoryResourceLimitImpl(u64 size) {
return pgl::srv::BoostSystemMemoryResourceLimit(size);
}
Result ShellInterface::IsProcessTracked(ams::sf::Out<bool> out, os::ProcessId process_id) {
out.SetValue(pgl::srv::IsProcessTracked(process_id));
Result ShellInterfaceCommon::IsProcessTrackedImpl(bool *out, os::ProcessId process_id) {
*out = pgl::srv::IsProcessTracked(process_id);
return ResultSuccess();
}
Result ShellInterface::EnableApplicationCrashReport(bool enabled) {
Result ShellInterfaceCommon::EnableApplicationCrashReportImpl(bool enabled) {
pgl::srv::EnableApplicationCrashReport(enabled);
return ResultSuccess();
}
Result ShellInterface::IsApplicationCrashReportEnabled(ams::sf::Out<bool> out) {
out.SetValue(pgl::srv::IsApplicationCrashReportEnabled());
Result ShellInterfaceCommon::IsApplicationCrashReportEnabledImpl(bool *out) {
*out = pgl::srv::IsApplicationCrashReportEnabled();
return ResultSuccess();
}
Result ShellInterface::EnableApplicationAllThreadDumpOnCrash(bool enabled) {
Result ShellInterfaceCommon::EnableApplicationAllThreadDumpOnCrashImpl(bool enabled) {
pgl::srv::EnableApplicationAllThreadDumpOnCrash(enabled);
return ResultSuccess();
}
Result ShellInterface::TriggerApplicationSnapShotDumper(SnapShotDumpType dump_type, const ams::sf::InBuffer &arg) {
return pgl::srv::TriggerApplicationSnapShotDumper(dump_type, reinterpret_cast<const char *>(arg.GetPointer()));
Result ShellInterfaceCommon::TriggerApplicationSnapShotDumperImpl(SnapShotDumpType dump_type, const void *arg, size_t arg_size) {
return pgl::srv::TriggerApplicationSnapShotDumper(dump_type, static_cast<const char *>(arg));
}
Result ShellInterface::GetShellEventObserver(ams::sf::Out<ams::sf::SharedPointer<pgl::sf::IEventObserver>> out) {
Result ShellInterfaceCmif::LaunchProgram(ams::sf::Out<os::ProcessId> out, const ncm::ProgramLocation &loc, u32 pm_flags, u8 pgl_flags) {
return this->LaunchProgramImpl(out.GetPointer(), loc, pm_flags, pgl_flags);
}
Result ShellInterfaceCmif::TerminateProcess(os::ProcessId process_id) {
return this->TerminateProcessImpl(process_id);
}
Result ShellInterfaceCmif::LaunchProgramFromHost(ams::sf::Out<os::ProcessId> out, const ams::sf::InBuffer &content_path, u32 pm_flags) {
return this->LaunchProgramFromHostImpl(out.GetPointer(), content_path.GetPointer(), content_path.GetSize(), pm_flags);
}
Result ShellInterfaceCmif::GetHostContentMetaInfo(ams::sf::Out<pgl::ContentMetaInfo> out, const ams::sf::InBuffer &content_path) {
return this->GetHostContentMetaInfoImpl(out.GetPointer(), content_path.GetPointer(), content_path.GetSize());
}
Result ShellInterfaceCmif::GetApplicationProcessId(ams::sf::Out<os::ProcessId> out) {
return this->GetApplicationProcessIdImpl(out.GetPointer());
}
Result ShellInterfaceCmif::BoostSystemMemoryResourceLimit(u64 size) {
return this->BoostSystemMemoryResourceLimitImpl(size);
}
Result ShellInterfaceCmif::IsProcessTracked(ams::sf::Out<bool> out, os::ProcessId process_id) {
return this->IsProcessTrackedImpl(out.GetPointer(), process_id);
}
Result ShellInterfaceCmif::EnableApplicationCrashReport(bool enabled) {
return this->EnableApplicationCrashReportImpl(enabled);
}
Result ShellInterfaceCmif::IsApplicationCrashReportEnabled(ams::sf::Out<bool> out) {
return this->IsApplicationCrashReportEnabledImpl(out.GetPointer());
}
Result ShellInterfaceCmif::EnableApplicationAllThreadDumpOnCrash(bool enabled) {
return this->EnableApplicationAllThreadDumpOnCrashImpl(enabled);
}
Result ShellInterfaceCmif::TriggerApplicationSnapShotDumper(SnapShotDumpType dump_type, const ams::sf::InBuffer &arg) {
return this->TriggerApplicationSnapShotDumperImpl(dump_type, arg.GetPointer(), arg.GetSize());
}
Result ShellInterfaceCmif::GetShellEventObserver(ams::sf::Out<ams::sf::SharedPointer<pgl::sf::IEventObserver>> out) {
/* Allocate a new interface. */
auto session = ObjectFactory::CreateSharedEmplaced<pgl::sf::IEventObserver, ShellEventObserver>(m_allocator);
auto session = ObjectFactory::CreateSharedEmplaced<pgl::sf::IEventObserver, ShellEventObserverCmif>(m_allocator);
R_UNLESS(session != nullptr, pgl::ResultOutOfMemory());
*out = std::move(session);
return ResultSuccess();
}
Result ShellInterface::Command21NotImplemented(ams::sf::Out<u64> out, u32 in, const ams::sf::InBuffer &buf1, const ams::sf::InBuffer &buf2) {
Result ShellInterfaceCmif::Command21NotImplemented(ams::sf::Out<u64> out, u32 in, const ams::sf::InBuffer &buf1, const ams::sf::InBuffer &buf2) {
return pgl::ResultNotImplemented();
}
Result ShellInterfaceTipc::LaunchProgram(ams::tipc::Out<os::ProcessId> out, const ncm::ProgramLocation loc, u32 pm_flags, u8 pgl_flags) {
return this->LaunchProgramImpl(out.GetPointer(), loc, pm_flags, pgl_flags);
}
Result ShellInterfaceTipc::TerminateProcess(os::ProcessId process_id) {
return this->TerminateProcessImpl(process_id);
}
Result ShellInterfaceTipc::LaunchProgramFromHost(ams::tipc::Out<os::ProcessId> out, const ams::tipc::InBuffer content_path, u32 pm_flags) {
return this->LaunchProgramFromHostImpl(out.GetPointer(), content_path.GetPointer(), content_path.GetSize(), pm_flags);
}
Result ShellInterfaceTipc::GetHostContentMetaInfo(ams::tipc::Out<pgl::ContentMetaInfo> out, const ams::tipc::InBuffer content_path) {
return this->GetHostContentMetaInfoImpl(out.GetPointer(), content_path.GetPointer(), content_path.GetSize());
}
Result ShellInterfaceTipc::GetApplicationProcessId(ams::tipc::Out<os::ProcessId> out) {
return this->GetApplicationProcessIdImpl(out.GetPointer());
}
Result ShellInterfaceTipc::BoostSystemMemoryResourceLimit(u64 size) {
return this->BoostSystemMemoryResourceLimitImpl(size);
}
Result ShellInterfaceTipc::IsProcessTracked(ams::tipc::Out<bool> out, os::ProcessId process_id) {
return this->IsProcessTrackedImpl(out.GetPointer(), process_id);
}
Result ShellInterfaceTipc::EnableApplicationCrashReport(bool enabled) {
return this->EnableApplicationCrashReportImpl(enabled);
}
Result ShellInterfaceTipc::IsApplicationCrashReportEnabled(ams::tipc::Out<bool> out) {
return this->IsApplicationCrashReportEnabledImpl(out.GetPointer());
}
Result ShellInterfaceTipc::EnableApplicationAllThreadDumpOnCrash(bool enabled) {
return this->EnableApplicationAllThreadDumpOnCrashImpl(enabled);
}
Result ShellInterfaceTipc::GetShellEventObserver(ams::tipc::OutMoveHandle out) {
return pgl::srv::AllocateShellEventObserverForTipc(out.GetHandlePointer());
}
}

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2018-2020 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::pgl::srv {
Result AllocateShellEventObserverForTipc(svc::Handle *out);
}