From a9369726147c7499e0016e183d5d56a7b44efe4b Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 18 Feb 2023 16:26:48 -0500 Subject: service: refactor server architecture Converts services to have their own processes --- src/core/hle/service/sm/sm.cpp | 32 ++++++++++++++++--------------- src/core/hle/service/sm/sm.h | 8 +++++--- src/core/hle/service/sm/sm_controller.cpp | 7 ++++--- 3 files changed, 26 insertions(+), 21 deletions(-) (limited to 'src/core/hle/service/sm') diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 84720094f..0de32b05d 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -12,6 +12,7 @@ #include "core/hle/kernel/k_scoped_resource_reservation.h" #include "core/hle/kernel/k_server_port.h" #include "core/hle/result.h" +#include "core/hle/service/server_manager.h" #include "core/hle/service/sm/sm.h" #include "core/hle/service/sm/sm_controller.h" @@ -22,7 +23,9 @@ constexpr Result ERR_ALREADY_REGISTERED(ErrorModule::SM, 4); constexpr Result ERR_INVALID_NAME(ErrorModule::SM, 6); constexpr Result ERR_SERVICE_NOT_REGISTERED(ErrorModule::SM, 7); -ServiceManager::ServiceManager(Kernel::KernelCore& kernel_) : kernel{kernel_} {} +ServiceManager::ServiceManager(Kernel::KernelCore& kernel_) : kernel{kernel_} { + controller_interface = std::make_unique(kernel.System()); +} ServiceManager::~ServiceManager() { for (auto& [name, port] : service_ports) { @@ -43,21 +46,12 @@ static Result ValidateServiceName(const std::string& name) { return ResultSuccess; } -Kernel::KClientPort& ServiceManager::InterfaceFactory(ServiceManager& self, Core::System& system) { - self.sm_interface = std::make_shared(self, system); - self.controller_interface = std::make_unique(system); - return self.sm_interface->CreatePort(); -} - -void ServiceManager::SessionHandler(ServiceManager& self, Kernel::KServerPort* server_port) { - self.sm_interface->AcceptSession(server_port); -} - Result ServiceManager::RegisterService(std::string name, u32 max_sessions, Kernel::SessionRequestHandlerPtr handler) { CASCADE_CODE(ValidateServiceName(name)); + std::scoped_lock lk{lock}; if (registered_services.find(name) != registered_services.end()) { LOG_ERROR(Service_SM, "Service is already registered! service={}", name); return ERR_ALREADY_REGISTERED; @@ -75,6 +69,7 @@ Result ServiceManager::RegisterService(std::string name, u32 max_sessions, Result ServiceManager::UnregisterService(const std::string& name) { CASCADE_CODE(ValidateServiceName(name)); + std::scoped_lock lk{lock}; const auto iter = registered_services.find(name); if (iter == registered_services.end()) { LOG_ERROR(Service_SM, "Server is not registered! service={}", name); @@ -89,6 +84,8 @@ Result ServiceManager::UnregisterService(const std::string& name) { ResultVal ServiceManager::GetServicePort(const std::string& name) { CASCADE_CODE(ValidateServiceName(name)); + + std::scoped_lock lk{lock}; auto it = service_ports.find(name); if (it == service_ports.end()) { LOG_ERROR(Service_SM, "Server is not registered! service={}", name); @@ -154,8 +151,7 @@ ResultVal SM::GetServiceImpl(Kernel::HLERequestContext& // Find the named port. auto port_result = service_manager.GetServicePort(name); - auto service = service_manager.GetService(name); - if (port_result.Failed() || !service) { + if (port_result.Failed()) { LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, port_result.Code().raw); return port_result.Code(); } @@ -167,7 +163,6 @@ ResultVal SM::GetServiceImpl(Kernel::HLERequestContext& LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, result.raw); return result; } - service->AcceptSession(&port->GetServerPort()); LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetId()); @@ -212,7 +207,7 @@ void SM::UnregisterService(Kernel::HLERequestContext& ctx) { } SM::SM(ServiceManager& service_manager_, Core::System& system_) - : ServiceFramework{system_, "sm:", ServiceThreadType::Default, 4}, + : ServiceFramework{system_, "sm:", 4}, service_manager{service_manager_}, kernel{system_.Kernel()} { RegisterHandlers({ {0, &SM::Initialize, "Initialize"}, @@ -232,4 +227,11 @@ SM::SM(ServiceManager& service_manager_, Core::System& system_) SM::~SM() = default; +void LoopProcess(Core::System& system) { + auto server_manager = std::make_unique(system); + + server_manager->ManageNamedPort("sm:", std::make_shared(system.ServiceManager(), system)); + ServerManager::RunServer(std::move(server_manager)); +} + } // namespace Service::SM diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h index 02a5dde9e..22ca720f8 100644 --- a/src/core/hle/service/sm/sm.h +++ b/src/core/hle/service/sm/sm.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include @@ -50,9 +51,6 @@ private: class ServiceManager { public: - static Kernel::KClientPort& InterfaceFactory(ServiceManager& self, Core::System& system); - static void SessionHandler(ServiceManager& self, Kernel::KServerPort* server_port); - explicit ServiceManager(Kernel::KernelCore& kernel_); ~ServiceManager(); @@ -78,6 +76,7 @@ private: std::unique_ptr controller_interface; /// Map of registered services, retrieved using GetServicePort. + std::mutex lock; std::unordered_map registered_services; std::unordered_map service_ports; @@ -85,4 +84,7 @@ private: Kernel::KernelCore& kernel; }; +/// Runs SM services. +void LoopProcess(Core::System& system); + } // namespace Service::SM diff --git a/src/core/hle/service/sm/sm_controller.cpp b/src/core/hle/service/sm/sm_controller.cpp index 1cf9dd1c4..f52522d1d 100644 --- a/src/core/hle/service/sm/sm_controller.cpp +++ b/src/core/hle/service/sm/sm_controller.cpp @@ -10,6 +10,7 @@ #include "core/hle/kernel/k_scoped_resource_reservation.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_session.h" +#include "core/hle/service/server_manager.h" #include "core/hle/service/sm/sm_controller.h" namespace Service::SM { @@ -48,9 +49,9 @@ void Controller::CloneCurrentObject(Kernel::HLERequestContext& ctx) { // Commit the session reservation. session_reservation.Commit(); - // Register with manager. - session_manager->SessionHandler().RegisterSession(&session->GetServerSession(), - session_manager); + // Register with server manager. + session_manager->GetServerManager().RegisterSession(&session->GetServerSession(), + session_manager); // We succeeded. IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles}; -- cgit v1.2.3 From 6e0a33089b97747ea0e3dc9d57e19223d420c98a Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 19 Feb 2023 08:44:54 -0500 Subject: sm:: support service registration deferral --- src/core/hle/service/sm/sm.cpp | 29 ++++++++++++++++++++++++++--- src/core/hle/service/sm/sm.h | 5 +++++ 2 files changed, 31 insertions(+), 3 deletions(-) (limited to 'src/core/hle/service/sm') diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 0de32b05d..6eba48f03 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -32,6 +32,10 @@ ServiceManager::~ServiceManager() { port->GetClientPort().Close(); port->GetServerPort().Close(); } + + if (deferral_event) { + deferral_event->Close(); + } } void ServiceManager::InvokeControlRequest(Kernel::HLERequestContext& context) { @@ -62,6 +66,9 @@ Result ServiceManager::RegisterService(std::string name, u32 max_sessions, service_ports.emplace(name, port); registered_services.emplace(name, handler); + if (deferral_event) { + deferral_event->Signal(); + } return ResultSuccess; } @@ -88,7 +95,7 @@ ResultVal ServiceManager::GetServicePort(const std::string& name std::scoped_lock lk{lock}; auto it = service_ports.find(name); if (it == service_ports.end()) { - LOG_ERROR(Service_SM, "Server is not registered! service={}", name); + LOG_WARNING(Service_SM, "Server is not registered! service={}", name); return ERR_SERVICE_NOT_REGISTERED; } @@ -113,6 +120,11 @@ void SM::Initialize(Kernel::HLERequestContext& ctx) { void SM::GetService(Kernel::HLERequestContext& ctx) { auto result = GetServiceImpl(ctx); + if (ctx.GetIsDeferred()) { + // Don't overwrite the command buffer. + return; + } + if (result.Succeeded()) { IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles}; rb.Push(result.Code()); @@ -125,6 +137,11 @@ void SM::GetService(Kernel::HLERequestContext& ctx) { void SM::GetServiceTipc(Kernel::HLERequestContext& ctx) { auto result = GetServiceImpl(ctx); + if (ctx.GetIsDeferred()) { + // Don't overwrite the command buffer. + return; + } + IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles}; rb.Push(result.Code()); rb.PushMoveObjects(result.Succeeded() ? result.Unwrap() : nullptr); @@ -152,8 +169,9 @@ ResultVal SM::GetServiceImpl(Kernel::HLERequestContext& // Find the named port. auto port_result = service_manager.GetServicePort(name); if (port_result.Failed()) { - LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, port_result.Code().raw); - return port_result.Code(); + LOG_INFO(Service_SM, "Waiting for service {} to become available", name); + ctx.SetIsDeferred(); + return ERR_SERVICE_NOT_REGISTERED; } auto& port = port_result.Unwrap(); @@ -228,8 +246,13 @@ SM::SM(ServiceManager& service_manager_, Core::System& system_) SM::~SM() = default; void LoopProcess(Core::System& system) { + auto& service_manager = system.ServiceManager(); auto server_manager = std::make_unique(system); + Kernel::KEvent* deferral_event{}; + server_manager->ManageDeferral(&deferral_event); + service_manager.SetDeferralEvent(deferral_event); + server_manager->ManageNamedPort("sm:", std::make_shared(system.ServiceManager(), system)); ServerManager::RunServer(std::move(server_manager)); } diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h index 22ca720f8..b7eeafdd6 100644 --- a/src/core/hle/service/sm/sm.h +++ b/src/core/hle/service/sm/sm.h @@ -71,6 +71,10 @@ public: void InvokeControlRequest(Kernel::HLERequestContext& context); + void SetDeferralEvent(Kernel::KEvent* deferral_event_) { + deferral_event = deferral_event_; + } + private: std::shared_ptr sm_interface; std::unique_ptr controller_interface; @@ -82,6 +86,7 @@ private: /// Kernel context Kernel::KernelCore& kernel; + Kernel::KEvent* deferral_event{}; }; /// Runs SM services. -- cgit v1.2.3 From 72e5552409305fe57781b83c3145fb2b66552be2 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 21 Feb 2023 12:19:12 -0500 Subject: sm:: fix lingering session initialization issues --- src/core/hle/service/sm/sm.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/core/hle/service/sm') diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 6eba48f03..53c877836 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -112,7 +112,7 @@ ResultVal ServiceManager::GetServicePort(const std::string& name void SM::Initialize(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_SM, "called"); - is_initialized = true; + ctx.GetManager()->SetIsInitializedForSm(); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ResultSuccess); @@ -159,7 +159,7 @@ static std::string PopServiceName(IPC::RequestParser& rp) { } ResultVal SM::GetServiceImpl(Kernel::HLERequestContext& ctx) { - if (!is_initialized) { + if (!ctx.GetManager()->GetIsInitializedForSm()) { return ERR_NOT_INITIALIZED; } @@ -168,6 +168,11 @@ ResultVal SM::GetServiceImpl(Kernel::HLERequestContext& // Find the named port. auto port_result = service_manager.GetServicePort(name); + if (port_result.Code() == ERR_INVALID_NAME) { + LOG_ERROR(Service_SM, "Invalid service name '{}'", name); + return ERR_INVALID_NAME; + } + if (port_result.Failed()) { LOG_INFO(Service_SM, "Waiting for service {} to become available", name); ctx.SetIsDeferred(); -- cgit v1.2.3