diff --git a/libstratosphere/include/stratosphere/pgl/pgl_event_observer.hpp b/libstratosphere/include/stratosphere/pgl/pgl_event_observer.hpp index 5e918153..ecfcbdcb 100644 --- a/libstratosphere/include/stratosphere/pgl/pgl_event_observer.hpp +++ b/libstratosphere/include/stratosphere/pgl/pgl_event_observer.hpp @@ -19,19 +19,79 @@ #include #include #include +#include namespace ams::pgl { + namespace impl { + + class EventObserverInterface { + NON_COPYABLE(EventObserverInterface); + NON_MOVEABLE(EventObserverInterface); + public: + constexpr EventObserverInterface() = default; + + virtual ~EventObserverInterface() { /* ... */ } + + virtual Result GetSystemEvent(os::SystemEventType *out) = 0; + virtual Result GetProcessEventInfo(pm::ProcessEventInfo *out) = 0; + }; + + class EventObserverByCmif final : public EventObserverInterface { + NON_COPYABLE(EventObserverByCmif); + NON_MOVEABLE(EventObserverByCmif); + private: + ams::sf::SharedPointer m_cmif_interface; + public: + explicit EventObserverByCmif(ams::sf::SharedPointer intf) : m_cmif_interface(intf) { /* ... */ } + public: + virtual Result GetSystemEvent(os::SystemEventType *out) override { + ams::sf::CopyHandle handle; + R_TRY(m_cmif_interface->GetProcessEventHandle(std::addressof(handle))); + os::AttachSystemEvent(out, handle.GetValue(), true, svc::InvalidHandle, false, os::EventClearMode_AutoClear); + return ResultSuccess(); + } + + virtual Result GetProcessEventInfo(pm::ProcessEventInfo *out) override { + return m_cmif_interface->GetProcessEventInfo(out); + } + }; + + template requires tipc::IsIEventObserver + class EventObserverByTipc final : public EventObserverInterface { + NON_COPYABLE(EventObserverByTipc); + NON_MOVEABLE(EventObserverByTipc); + private: + T m_tipc_interface; + public: + template + explicit EventObserverByTipc(Args &&... args) : m_tipc_interface(std::forward(args)...) { /* ... */ } + public: + virtual Result GetSystemEvent(os::SystemEventType *out) override { + ams::tipc::CopyHandle handle; + R_TRY(m_tipc_interface.GetProcessEventHandle(std::addressof(handle))); + os::AttachSystemEvent(out, handle.GetValue(), true, svc::InvalidHandle, false, os::EventClearMode_AutoClear); + return ResultSuccess(); + } + + virtual Result GetProcessEventInfo(pm::ProcessEventInfo *out) override { + return m_tipc_interface.GetProcessEventInfo(ams::tipc::Out(out)); + } + }; + + } + class EventObserver { NON_COPYABLE(EventObserver); private: - ams::sf::SharedPointer interface; + std::unique_ptr m_impl; public: EventObserver() { /* ... */ } - explicit EventObserver(ams::sf::SharedPointer intf) : interface(intf) { /* ... */ } + + explicit EventObserver(std::unique_ptr impl) : m_impl(std::move(impl)) { /* ... */ } EventObserver(EventObserver &&rhs) { - this->interface = std::move(rhs.interface); + m_impl = std::move(rhs.m_impl); } EventObserver &operator=(EventObserver &&rhs) { @@ -40,18 +100,15 @@ namespace ams::pgl { } void Swap(EventObserver &rhs) { - std::swap(this->interface, rhs.interface); + std::swap(m_impl, rhs.m_impl); } public: Result GetSystemEvent(os::SystemEventType *out) { - ams::sf::CopyHandle handle; - R_TRY(this->interface->GetProcessEventHandle(std::addressof(handle))); - os::AttachSystemEvent(out, handle.GetValue(), true, svc::InvalidHandle, false, os::EventClearMode_AutoClear); - return ResultSuccess(); + return m_impl->GetSystemEvent(out); } Result GetProcessEventInfo(pm::ProcessEventInfo *out) { - return this->interface->GetProcessEventInfo(out); + return m_impl->GetProcessEventInfo(out); } }; diff --git a/libstratosphere/include/stratosphere/pgl/sf/pgl_sf_i_event_observer.hpp b/libstratosphere/include/stratosphere/pgl/sf/pgl_sf_i_event_observer.hpp index 1bf5e31a..11fa19a1 100644 --- a/libstratosphere/include/stratosphere/pgl/sf/pgl_sf_i_event_observer.hpp +++ b/libstratosphere/include/stratosphere/pgl/sf/pgl_sf_i_event_observer.hpp @@ -20,8 +20,9 @@ #include #include -#define AMS_PGL_I_EVENT_OBSERVER_INTERFACE_INFO(C, H) \ +#define AMS_PGL_SF_I_EVENT_OBSERVER_INTERFACE_INFO(C, H) \ AMS_SF_METHOD_INFO(C, H, 0, Result, GetProcessEventHandle, (ams::sf::OutCopyHandle out), (out)) \ AMS_SF_METHOD_INFO(C, H, 1, Result, GetProcessEventInfo, (ams::sf::Out out), (out)) -AMS_SF_DEFINE_INTERFACE(ams::pgl::sf, IEventObserver, AMS_PGL_I_EVENT_OBSERVER_INTERFACE_INFO); +AMS_SF_DEFINE_INTERFACE(ams::pgl::sf, IEventObserver, AMS_PGL_SF_I_EVENT_OBSERVER_INTERFACE_INFO); + diff --git a/libstratosphere/include/stratosphere/pgl/srv/pgl_srv_api.hpp b/libstratosphere/include/stratosphere/pgl/srv/pgl_srv_api.hpp index 723ddad5..a9f20514 100644 --- a/libstratosphere/include/stratosphere/pgl/srv/pgl_srv_api.hpp +++ b/libstratosphere/include/stratosphere/pgl/srv/pgl_srv_api.hpp @@ -20,6 +20,10 @@ namespace ams::pgl::srv { - void Initialize(); + void InitializeHeap(); + void *Allocate(size_t size); + void Deallocate(void *p, size_t size); + + void StartServer(); } diff --git a/libstratosphere/include/stratosphere/pgl/srv/pgl_srv_shell_interface.hpp b/libstratosphere/include/stratosphere/pgl/srv/pgl_srv_shell_interface.hpp index 1c510a1a..abc40e59 100644 --- a/libstratosphere/include/stratosphere/pgl/srv/pgl_srv_shell_interface.hpp +++ b/libstratosphere/include/stratosphere/pgl/srv/pgl_srv_shell_interface.hpp @@ -17,19 +17,39 @@ #include #include #include +#include namespace ams::pgl::srv { - class ShellInterface { - NON_COPYABLE(ShellInterface); - NON_MOVEABLE(ShellInterface); + class ShellInterfaceCommon { + NON_COPYABLE(ShellInterfaceCommon); + NON_MOVEABLE(ShellInterfaceCommon); + public: + constexpr ShellInterfaceCommon() = default; + public: + Result LaunchProgramImpl(os::ProcessId *out, const ncm::ProgramLocation &loc, u32 pm_flags, u8 pgl_flags); + Result TerminateProcessImpl(os::ProcessId process_id); + Result LaunchProgramFromHostImpl(os::ProcessId *out, const void *content_path, size_t content_path_size, u32 pm_flags); + Result GetHostContentMetaInfoImpl(pgl::ContentMetaInfo *out, const void *content_path, size_t content_path_size); + Result GetApplicationProcessIdImpl(os::ProcessId *out); + Result BoostSystemMemoryResourceLimitImpl(u64 size); + Result IsProcessTrackedImpl(bool *out, os::ProcessId process_id); + Result EnableApplicationCrashReportImpl(bool enabled); + Result IsApplicationCrashReportEnabledImpl(bool *out); + Result EnableApplicationAllThreadDumpOnCrashImpl(bool enabled); + Result TriggerApplicationSnapShotDumperImpl(SnapShotDumpType dump_type, const void *arg, size_t arg_size); + }; + + class ShellInterfaceCmif : public ShellInterfaceCommon { + NON_COPYABLE(ShellInterfaceCmif); + NON_MOVEABLE(ShellInterfaceCmif); private: using Allocator = ams::sf::ExpHeapAllocator; using ObjectFactory = ams::sf::ObjectFactory; private: Allocator *m_allocator; public: - constexpr ShellInterface(Allocator *a) : m_allocator(a) { /* ... */ } + constexpr ShellInterfaceCmif(Allocator *a) : ShellInterfaceCommon(), m_allocator(a) { /* ... */ } public: /* Interface commands. */ Result LaunchProgram(ams::sf::Out out, const ncm::ProgramLocation &loc, u32 pm_flags, u8 pgl_flags); @@ -47,6 +67,27 @@ namespace ams::pgl::srv { Result GetShellEventObserver(ams::sf::Out> out); Result Command21NotImplemented(ams::sf::Out out, u32 in, const ams::sf::InBuffer &buf1, const ams::sf::InBuffer &buf2); }; - static_assert(pgl::sf::IsIShellInterface); + static_assert(pgl::sf::IsIShellInterface); + + class ShellInterfaceTipc : public ShellInterfaceCommon { + NON_COPYABLE(ShellInterfaceTipc); + NON_MOVEABLE(ShellInterfaceTipc); + public: + constexpr ShellInterfaceTipc() : ShellInterfaceCommon() { /* ... */ } + public: + /* Interface commands. */ + Result LaunchProgram(ams::tipc::Out out, const ncm::ProgramLocation loc, u32 pm_flags, u8 pgl_flags); + Result TerminateProcess(os::ProcessId process_id); + Result LaunchProgramFromHost(ams::tipc::Out out, const ams::tipc::InBuffer content_path, u32 pm_flags); + Result GetHostContentMetaInfo(ams::tipc::Out out, const ams::tipc::InBuffer content_path); + Result GetApplicationProcessId(ams::tipc::Out out); + Result BoostSystemMemoryResourceLimit(u64 size); + Result IsProcessTracked(ams::tipc::Out out, os::ProcessId process_id); + Result EnableApplicationCrashReport(bool enabled); + Result IsApplicationCrashReportEnabled(ams::tipc::Out out); + Result EnableApplicationAllThreadDumpOnCrash(bool enabled); + Result GetShellEventObserver(ams::tipc::OutMoveHandle out); + }; + static_assert(pgl::tipc::IsIShellInterface); } diff --git a/libstratosphere/include/stratosphere/pgl/tipc/pgl_tipc_i_event_observer.hpp b/libstratosphere/include/stratosphere/pgl/tipc/pgl_tipc_i_event_observer.hpp new file mode 100644 index 00000000..c35c3585 --- /dev/null +++ b/libstratosphere/include/stratosphere/pgl/tipc/pgl_tipc_i_event_observer.hpp @@ -0,0 +1,29 @@ +/* + * 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 . + */ + +#pragma once +#include +#include +#include +#include +#include + +#define AMS_PGL_TIPC_I_EVENT_OBSERVER_INTERFACE_INFO(C, H) \ + AMS_TIPC_METHOD_INFO(C, H, 0, Result, GetProcessEventHandle, (ams::tipc::OutCopyHandle out), (out)) \ + AMS_TIPC_METHOD_INFO(C, H, 1, Result, GetProcessEventInfo, (ams::tipc::Out out), (out)) + +AMS_TIPC_DEFINE_INTERFACE(ams::pgl::tipc, IEventObserver, AMS_PGL_TIPC_I_EVENT_OBSERVER_INTERFACE_INFO); + diff --git a/libstratosphere/include/stratosphere/pgl/tipc/pgl_tipc_i_shell_interface.hpp b/libstratosphere/include/stratosphere/pgl/tipc/pgl_tipc_i_shell_interface.hpp new file mode 100644 index 00000000..a97a39bf --- /dev/null +++ b/libstratosphere/include/stratosphere/pgl/tipc/pgl_tipc_i_shell_interface.hpp @@ -0,0 +1,38 @@ +/* + * 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 . + */ + +#pragma once +#include +#include +#include +#include +#include +#include + +#define AMS_PGL_TIPC_I_SHELL_INTERFACE_INTERFACE_INFO(C, H) \ + AMS_TIPC_METHOD_INFO(C, H, 0, Result, LaunchProgram, (ams::tipc::Out out, const ncm::ProgramLocation loc, u32 pm_flags, u8 pgl_flags), (out, loc, pm_flags, pgl_flags)) \ + AMS_TIPC_METHOD_INFO(C, H, 1, Result, TerminateProcess, (os::ProcessId process_id), (process_id)) \ + AMS_TIPC_METHOD_INFO(C, H, 2, Result, LaunchProgramFromHost, (ams::tipc::Out out, const ams::tipc::InBuffer content_path, u32 pm_flags), (out, content_path, pm_flags)) \ + AMS_TIPC_METHOD_INFO(C, H, 4, Result, GetHostContentMetaInfo, (ams::tipc::Out out, const ams::tipc::InBuffer content_path), (out, content_path)) \ + AMS_TIPC_METHOD_INFO(C, H, 5, Result, GetApplicationProcessId, (ams::tipc::Out out), (out)) \ + AMS_TIPC_METHOD_INFO(C, H, 6, Result, BoostSystemMemoryResourceLimit, (u64 size), (size)) \ + AMS_TIPC_METHOD_INFO(C, H, 7, Result, IsProcessTracked, (ams::tipc::Out out, os::ProcessId process_id), (out, process_id)) \ + AMS_TIPC_METHOD_INFO(C, H, 8, Result, EnableApplicationCrashReport, (bool enabled), (enabled)) \ + AMS_TIPC_METHOD_INFO(C, H, 9, Result, IsApplicationCrashReportEnabled, (ams::tipc::Out out), (out)) \ + AMS_TIPC_METHOD_INFO(C, H, 10, Result, EnableApplicationAllThreadDumpOnCrash, (bool enabled), (enabled)) \ + AMS_TIPC_METHOD_INFO(C, H, 20, Result, GetShellEventObserver, (ams::tipc::OutMoveHandle out), (out)) + +AMS_TIPC_DEFINE_INTERFACE(ams::pgl::tipc, IShellInterface, AMS_PGL_TIPC_I_SHELL_INTERFACE_INTERFACE_INFO); diff --git a/libstratosphere/include/stratosphere/tipc/tipc_allocators.hpp b/libstratosphere/include/stratosphere/tipc/tipc_allocators.hpp index 0cf758d1..83dcf29c 100644 --- a/libstratosphere/include/stratosphere/tipc/tipc_allocators.hpp +++ b/libstratosphere/include/stratosphere/tipc/tipc_allocators.hpp @@ -59,7 +59,7 @@ namespace ams::tipc { } } - AMS_ABORT("Failed to allocate entry in SlabAllocator"); + return nullptr; } void Deallocate(ServiceObjectBase *object) { diff --git a/libstratosphere/include/stratosphere/tipc/tipc_server_manager.hpp b/libstratosphere/include/stratosphere/tipc/tipc_server_manager.hpp index 465d2fc4..389e266b 100644 --- a/libstratosphere/include/stratosphere/tipc/tipc_server_manager.hpp +++ b/libstratosphere/include/stratosphere/tipc/tipc_server_manager.hpp @@ -165,6 +165,17 @@ namespace ams::tipc { return m_object_manager->ReplyAndReceive(out_holder, out_object, reply_target, std::addressof(m_waitable_manager)); } + void AddSession(svc::Handle session_handle, tipc::ServiceObjectBase *service_object) { + /* Create a waitable object for the session. */ + tipc::WaitableObject object; + + /* Setup the object. */ + object.InitializeAsSession(session_handle, true, service_object); + + /* Register the object. */ + m_object_manager->AddObject(object); + } + void ProcessMessages() { /* While we have messages in our queue, receive and handle them. */ uintptr_t message_type, message_data; @@ -182,14 +193,8 @@ namespace ams::tipc { /* Allocate a service object for the port. */ auto *service_object = m_server_manager->AllocateObject(static_cast(message_data)); - /* Create a waitable object for the session. */ - tipc::WaitableObject object; - - /* Setup the object. */ - object.InitializeAsSession(session_handle, true, service_object); - - /* Register the object. */ - m_object_manager->AddObject(object); + /* Add the newly-created service object. */ + this->AddSession(session_handle, service_object); } break; case MessageType_TriggerResume: @@ -402,6 +407,16 @@ namespace ams::tipc { this->GetPortManager().RegisterPort(static_cast(Ix), port_handle); } + template + void RegisterPort(sm::ServiceName service_name, size_t max_sessions) { + /* Register service. */ + svc::Handle port_handle = svc::InvalidHandle; + R_ABORT_UNLESS(sm::RegisterService(std::addressof(port_handle), service_name, max_sessions, false)); + + /* Register the port handle. */ + this->RegisterPort(port_handle); + } + void LoopAuto() { /* If we have additional threads, create and start them. */ if constexpr (NumPorts > 1) { @@ -441,6 +456,27 @@ namespace ams::tipc { (this->TriggerResumeImpl(resume_key), ...); }(std::make_index_sequence()); } + + Result AddSession(svc::Handle *out, tipc::ServiceObjectBase *object) { + /* Acquire exclusive access to ourselves. */ + std::scoped_lock lk(m_mutex); + + /* Create a handle for the session. */ + svc::Handle session_handle; + R_TRY(svc::CreateSession(std::addressof(session_handle), out, false, 0)); + + /* Select the best port manager. */ + PortManagerBase *best_manager = nullptr; + s32 best_sessions = -1; + [this, &best_manager, &best_sessions](std::index_sequence) ALWAYS_INLINE_LAMBDA { + (this->TrySelectBetterPort(best_manager, best_sessions), ...); + }(std::make_index_sequence()); + + /* Add the session to the least burdened manager. */ + best_manager->AddSession(session_handle, object); + + return ResultSuccess(); + } private: template requires (Ix < NumPorts) void TryAllocateObject(size_t port_index, tipc::ServiceObjectBase *&allocated) { diff --git a/libstratosphere/source/pgl/pgl_remote_event_observer.hpp b/libstratosphere/source/pgl/pgl_remote_event_observer.hpp index d4bc334f..ff70c49f 100644 --- a/libstratosphere/source/pgl/pgl_remote_event_observer.hpp +++ b/libstratosphere/source/pgl/pgl_remote_event_observer.hpp @@ -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 out) { + static_assert(sizeof(*out.GetPointer()) == sizeof(::PmProcessEventInfo)); + return ::pglEventObserverGetProcessEventInfo(std::addressof(this->observer), reinterpret_cast<::PmProcessEventInfo *>(out.GetPointer())); + } }; static_assert(pgl::sf::IsIEventObserver); + static_assert(pgl::tipc::IsIEventObserver); } \ No newline at end of file diff --git a/libstratosphere/source/pgl/pgl_shell_api.cpp b/libstratosphere/source/pgl/pgl_shell_api.cpp index 4525d030..83e1f03f 100644 --- a/libstratosphere/source/pgl/pgl_shell_api.cpp +++ b/libstratosphere/source/pgl/pgl_shell_api.cpp @@ -80,10 +80,21 @@ namespace ams::pgl { R_TRY(::pglGetEventObserver(std::addressof(obs))); /* TODO: Real allocator */ - auto remote_observer = ams::sf::CreateSharedObjectEmplaced(obs); - R_UNLESS(remote_observer != nullptr, pgl::ResultOutOfMemory()); + if (hos::GetVersion() >= hos::Version_12_0_0) { + auto observer_holder = std::make_unique>(obs); + R_UNLESS(observer_holder != nullptr, pgl::ResultOutOfMemory()); + + *out = pgl::EventObserver(std::move(observer_holder)); + } else { + auto remote_observer = ams::sf::CreateSharedObjectEmplaced(obs); + R_UNLESS(remote_observer != nullptr, pgl::ResultOutOfMemory()); + + auto observer_holder = std::make_unique(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(); } diff --git a/libstratosphere/source/pgl/srv/pgl_srv_api.cpp b/libstratosphere/source/pgl/srv/pgl_srv_api.cpp index c6c63441..d235e171 100644 --- a/libstratosphere/source/pgl/srv/pgl_srv_api.cpp +++ b/libstratosphere/source/pgl/srv/pgl_srv_api.cpp @@ -15,10 +15,145 @@ */ #include #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; + + constexpr size_t ObserverMaxSessions = 4; + + using ShellPortMeta = ams::tipc::PortMeta; + + using TipcServerManager = ams::tipc::ServerManager; + + /* 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 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, ObserverMaxSessions> observer_allocator; + }; + + constinit union { + util::TypedStorage cmif; + util::TypedStorage 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 + ALWAYS_INLINE void InitializeHeapImpl(util::TypedStorage &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(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; + } + } \ No newline at end of file diff --git a/libstratosphere/source/pgl/srv/pgl_srv_shell_event_observer.cpp b/libstratosphere/source/pgl/srv/pgl_srv_shell_event_observer.cpp index ad78718a..0b2d01b0 100644 --- a/libstratosphere/source/pgl/srv/pgl_srv_shell_event_observer.cpp +++ b/libstratosphere/source/pgl/srv/pgl_srv_shell_event_observer.cpp @@ -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(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 out) { + Result ShellEventObserverCmif::GetProcessEventInfo(ams::sf::Out 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 out) { return this->PopEventInfo(out.GetPointer()); } diff --git a/libstratosphere/source/pgl/srv/pgl_srv_shell_event_observer.hpp b/libstratosphere/source/pgl/srv/pgl_srv_shell_event_observer.hpp index 71e7e653..3e49f45d 100644 --- a/libstratosphere/source/pgl/srv/pgl_srv_shell_event_observer.hpp +++ b/libstratosphere/source/pgl/srv/pgl_srv_shell_event_observer.hpp @@ -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 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 out); }; - static_assert(pgl::sf::IsIEventObserver); + static_assert(pgl::sf::IsIEventObserver); + + class ShellEventObserverTipc : public ShellEventObserverImpl { + public: + Result GetProcessEventHandle(ams::tipc::OutCopyHandle out); + Result GetProcessEventInfo(ams::tipc::Out out); + }; + static_assert(pgl::tipc::IsIEventObserver); } diff --git a/libstratosphere/source/pgl/srv/pgl_srv_shell_interface.cpp b/libstratosphere/source/pgl/srv/pgl_srv_shell_interface.cpp index 4d4b3480..e7d85636 100644 --- a/libstratosphere/source/pgl/srv/pgl_srv_shell_interface.cpp +++ b/libstratosphere/source/pgl/srv/pgl_srv_shell_interface.cpp @@ -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 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 out, const ams::sf::InBuffer &content_path, u32 pm_flags) { - return pgl::srv::LaunchProgramFromHost(out.GetPointer(), reinterpret_cast(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(content_path), pm_flags); } - Result ShellInterface::GetHostContentMetaInfo(ams::sf::Out out, const ams::sf::InBuffer &content_path) { - return pgl::srv::GetHostContentMetaInfo(out.GetPointer(), reinterpret_cast(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(content_path)); } - Result ShellInterface::GetApplicationProcessId(ams::sf::Out 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 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 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(arg.GetPointer())); + Result ShellInterfaceCommon::TriggerApplicationSnapShotDumperImpl(SnapShotDumpType dump_type, const void *arg, size_t arg_size) { + return pgl::srv::TriggerApplicationSnapShotDumper(dump_type, static_cast(arg)); } - Result ShellInterface::GetShellEventObserver(ams::sf::Out> out) { + Result ShellInterfaceCmif::LaunchProgram(ams::sf::Out 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 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 out, const ams::sf::InBuffer &content_path) { + return this->GetHostContentMetaInfoImpl(out.GetPointer(), content_path.GetPointer(), content_path.GetSize()); + } + + Result ShellInterfaceCmif::GetApplicationProcessId(ams::sf::Out out) { + return this->GetApplicationProcessIdImpl(out.GetPointer()); + } + + Result ShellInterfaceCmif::BoostSystemMemoryResourceLimit(u64 size) { + return this->BoostSystemMemoryResourceLimitImpl(size); + } + + Result ShellInterfaceCmif::IsProcessTracked(ams::sf::Out 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 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> out) { /* Allocate a new interface. */ - auto session = ObjectFactory::CreateSharedEmplaced(m_allocator); + auto session = ObjectFactory::CreateSharedEmplaced(m_allocator); R_UNLESS(session != nullptr, pgl::ResultOutOfMemory()); *out = std::move(session); return ResultSuccess(); } - Result ShellInterface::Command21NotImplemented(ams::sf::Out out, u32 in, const ams::sf::InBuffer &buf1, const ams::sf::InBuffer &buf2) { + Result ShellInterfaceCmif::Command21NotImplemented(ams::sf::Out out, u32 in, const ams::sf::InBuffer &buf1, const ams::sf::InBuffer &buf2) { return pgl::ResultNotImplemented(); } + Result ShellInterfaceTipc::LaunchProgram(ams::tipc::Out 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 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 out, const ams::tipc::InBuffer content_path) { + return this->GetHostContentMetaInfoImpl(out.GetPointer(), content_path.GetPointer(), content_path.GetSize()); + } + + Result ShellInterfaceTipc::GetApplicationProcessId(ams::tipc::Out out) { + return this->GetApplicationProcessIdImpl(out.GetPointer()); + } + + Result ShellInterfaceTipc::BoostSystemMemoryResourceLimit(u64 size) { + return this->BoostSystemMemoryResourceLimitImpl(size); + } + + Result ShellInterfaceTipc::IsProcessTracked(ams::tipc::Out 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 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()); + } + } diff --git a/libstratosphere/source/pgl/srv/pgl_srv_tipc_utils.hpp b/libstratosphere/source/pgl/srv/pgl_srv_tipc_utils.hpp new file mode 100644 index 00000000..307710ba --- /dev/null +++ b/libstratosphere/source/pgl/srv/pgl_srv_tipc_utils.hpp @@ -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 . + */ +#pragma once +#include + +namespace ams::pgl::srv { + + Result AllocateShellEventObserverForTipc(svc::Handle *out); + +}