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

@ -19,19 +19,79 @@
#include <stratosphere/pm.hpp>
#include <stratosphere/pgl/pgl_types.hpp>
#include <stratosphere/pgl/sf/pgl_sf_i_event_observer.hpp>
#include <stratosphere/pgl/tipc/pgl_tipc_i_event_observer.hpp>
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<pgl::sf::IEventObserver> m_cmif_interface;
public:
explicit EventObserverByCmif(ams::sf::SharedPointer<pgl::sf::IEventObserver> 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<typename T> requires tipc::IsIEventObserver<T>
class EventObserverByTipc final : public EventObserverInterface {
NON_COPYABLE(EventObserverByTipc);
NON_MOVEABLE(EventObserverByTipc);
private:
T m_tipc_interface;
public:
template<typename... Args>
explicit EventObserverByTipc(Args &&... args) : m_tipc_interface(std::forward<Args>(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<pm::ProcessEventInfo>(out));
}
};
}
class EventObserver {
NON_COPYABLE(EventObserver);
private:
ams::sf::SharedPointer<pgl::sf::IEventObserver> interface;
std::unique_ptr<impl::EventObserverInterface> m_impl;
public:
EventObserver() { /* ... */ }
explicit EventObserver(ams::sf::SharedPointer<pgl::sf::IEventObserver> intf) : interface(intf) { /* ... */ }
explicit EventObserver(std::unique_ptr<impl::EventObserverInterface> 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);
}
};

View file

@ -20,8 +20,9 @@
#include <stratosphere/pm.hpp>
#include <stratosphere/pgl/pgl_types.hpp>
#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<pm::ProcessEventInfo> 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);

View file

@ -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();
}

View file

@ -17,19 +17,39 @@
#include <vapours.hpp>
#include <stratosphere/pgl/pgl_types.hpp>
#include <stratosphere/pgl/sf/pgl_sf_i_shell_interface.hpp>
#include <stratosphere/pgl/tipc/pgl_tipc_i_shell_interface.hpp>
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<ams::sf::ExpHeapAllocator::Policy>;
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<os::ProcessId> out, const ncm::ProgramLocation &loc, u32 pm_flags, u8 pgl_flags);
@ -47,6 +67,27 @@ namespace ams::pgl::srv {
Result GetShellEventObserver(ams::sf::Out<ams::sf::SharedPointer<pgl::sf::IEventObserver>> out);
Result Command21NotImplemented(ams::sf::Out<u64> out, u32 in, const ams::sf::InBuffer &buf1, const ams::sf::InBuffer &buf2);
};
static_assert(pgl::sf::IsIShellInterface<ShellInterface>);
static_assert(pgl::sf::IsIShellInterface<ShellInterfaceCmif>);
class ShellInterfaceTipc : public ShellInterfaceCommon {
NON_COPYABLE(ShellInterfaceTipc);
NON_MOVEABLE(ShellInterfaceTipc);
public:
constexpr ShellInterfaceTipc() : ShellInterfaceCommon() { /* ... */ }
public:
/* Interface commands. */
Result LaunchProgram(ams::tipc::Out<os::ProcessId> out, const ncm::ProgramLocation loc, u32 pm_flags, u8 pgl_flags);
Result TerminateProcess(os::ProcessId process_id);
Result LaunchProgramFromHost(ams::tipc::Out<os::ProcessId> out, const ams::tipc::InBuffer content_path, u32 pm_flags);
Result GetHostContentMetaInfo(ams::tipc::Out<pgl::ContentMetaInfo> out, const ams::tipc::InBuffer content_path);
Result GetApplicationProcessId(ams::tipc::Out<os::ProcessId> out);
Result BoostSystemMemoryResourceLimit(u64 size);
Result IsProcessTracked(ams::tipc::Out<bool> out, os::ProcessId process_id);
Result EnableApplicationCrashReport(bool enabled);
Result IsApplicationCrashReportEnabled(ams::tipc::Out<bool> out);
Result EnableApplicationAllThreadDumpOnCrash(bool enabled);
Result GetShellEventObserver(ams::tipc::OutMoveHandle out);
};
static_assert(pgl::tipc::IsIShellInterface<ShellInterfaceTipc>);
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os.hpp>
#include <stratosphere/pm.hpp>
#include <stratosphere/tipc.hpp>
#include <stratosphere/pgl/pgl_types.hpp>
#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<pm::ProcessEventInfo> out), (out))
AMS_TIPC_DEFINE_INTERFACE(ams::pgl::tipc, IEventObserver, AMS_PGL_TIPC_I_EVENT_OBSERVER_INTERFACE_INFO);

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os.hpp>
#include <stratosphere/pm.hpp>
#include <stratosphere/tipc.hpp>
#include <stratosphere/pgl/pgl_types.hpp>
#include <stratosphere/pgl/tipc/pgl_tipc_i_event_observer.hpp>
#define AMS_PGL_TIPC_I_SHELL_INTERFACE_INTERFACE_INFO(C, H) \
AMS_TIPC_METHOD_INFO(C, H, 0, Result, LaunchProgram, (ams::tipc::Out<os::ProcessId> 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<os::ProcessId> 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<pgl::ContentMetaInfo> out, const ams::tipc::InBuffer content_path), (out, content_path)) \
AMS_TIPC_METHOD_INFO(C, H, 5, Result, GetApplicationProcessId, (ams::tipc::Out<os::ProcessId> 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<bool> 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<bool> 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);

View file

@ -59,7 +59,7 @@ namespace ams::tipc {
}
}
AMS_ABORT("Failed to allocate entry in SlabAllocator<T, N>");
return nullptr;
}
void Deallocate(ServiceObjectBase *object) {

View file

@ -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<size_t>(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<Ix>().RegisterPort(static_cast<s32>(Ix), port_handle);
}
template<size_t Ix>
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<Ix>(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<Ix>(resume_key), ...);
}(std::make_index_sequence<NumPorts>());
}
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]<size_t... Ix>(std::index_sequence<Ix...>) ALWAYS_INLINE_LAMBDA {
(this->TrySelectBetterPort<Ix>(best_manager, best_sessions), ...);
}(std::make_index_sequence<NumPorts>());
/* Add the session to the least burdened manager. */
best_manager->AddSession(session_handle, object);
return ResultSuccess();
}
private:
template<size_t Ix> requires (Ix < NumPorts)
void TryAllocateObject(size_t port_index, tipc::ServiceObjectBase *&allocated) {

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 */
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());
*out = pgl::EventObserver(std::move(remote_observer));
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));
}
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);
}

View file

@ -51,62 +51,6 @@ void __libnx_exception_handler(ThreadExceptionDump *ctx) {
ams::CrashHandler(ctx);
}
namespace ams::pgl {
namespace {
/* NOTE: Nintendo reserves only 0x2000 bytes for this 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). */
/* We should have a smaller memory footprint than N in the end, regardless. */
constinit u8 g_heap_memory[32_KB];
lmem::HeapHandle g_server_heap_handle;
constinit ams::sf::ExpHeapAllocator g_server_allocator;
void *Allocate(size_t size) {
return lmem::AllocateFromExpHeap(g_server_heap_handle, size);
}
void Deallocate(void *p, size_t size) {
return lmem::FreeToExpHeap(g_server_heap_handle, p);
}
void InitializeHeap() {
g_server_heap_handle = lmem::CreateExpHeap(g_heap_memory, sizeof(g_heap_memory), lmem::CreateOption_ThreadSafe);
g_server_allocator.Attach(g_server_heap_handle);
}
}
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 ServerManager = ams::sf::hipc::ServerManager<PortIndex_Count>;
ServerManager g_server_manager;
constinit ams::sf::UnmanagedServiceObject<pgl::sf::IShellInterface, pgl::srv::ShellInterface> g_shell_interface(std::addressof(g_server_allocator));
void RegisterServiceSession() {
R_ABORT_UNLESS(g_server_manager.RegisterObjectForServer(g_shell_interface.GetShared(), ShellServiceName, ShellMaxSessions));
}
void LoopProcess() {
g_server_manager.LoopProcess();
}
}
}
void __libnx_initheap(void) {
void* addr = nx_inner_heap;
size_t size = nx_inner_heap_size;
@ -117,14 +61,14 @@ void __libnx_initheap(void) {
fake_heap_start = (char*)addr;
fake_heap_end = (char*)addr + size;
ams::pgl::InitializeHeap();
}
void __appInit(void) {
hos::InitializeForStratosphere();
fs::SetAllocator(pgl::Allocate, pgl::Deallocate);
ams::pgl::srv::InitializeHeap();
fs::SetAllocator(pgl::srv::Allocate, pgl::srv::Deallocate);
sm::DoWithSession([&]() {
R_ABORT_UNLESS(setInitialize());
@ -160,11 +104,11 @@ namespace ams {
}
void *operator new(size_t size) {
return pgl::Allocate(size);
return pgl::srv::Allocate(size);
}
void operator delete(void *p) {
return pgl::Deallocate(p, 0);
return pgl::srv::Deallocate(p, 0);
}
void *__libnx_alloc(size_t size) {
@ -188,14 +132,8 @@ int main(int argc, char **argv)
os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(pgl, Main));
AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(pgl, Main));
/* Register the pgl service. */
pgl::RegisterServiceSession();
/* Initialize the server library. */
pgl::srv::Initialize();
/* Loop forever, servicing our services. */
pgl::LoopProcess();
/* Initialize and start the server. */
pgl::srv::StartServer();
/* Cleanup */
return 0;