From 073653e858abf377fd1ebbdb071809c8830ce99d Mon Sep 17 00:00:00 2001 From: Subv Date: Tue, 14 Jun 2016 18:03:30 -0500 Subject: Kernel/IPC: Use Ports and Sessions as the fundamental building block of Inter Process Communication. All handles obtained via srv::GetServiceHandle or svcConnectToPort are references to ClientSessions. Service modules will wait on the counterpart of those ClientSessions (Called ServerSessions) using svcReplyAndReceive or svcWaitSynchronization[1|N], and will be awoken when a SyncRequest is performed. HLE Interfaces are now ClientPorts which override the HandleSyncRequest virtual member function to perform command handling immediately. --- src/core/hle/service/fs/archive.cpp | 12 ++++++------ src/core/hle/service/fs/archive.h | 12 +++++++----- src/core/hle/service/fs/fs_user.cpp | 9 +++++---- src/core/hle/service/service.cpp | 16 +++++++++++----- src/core/hle/service/service.h | 21 ++++++++++++++++----- src/core/hle/service/soc_u.cpp | 2 +- src/core/hle/service/srv.cpp | 17 ++++++++++++++++- 7 files changed, 62 insertions(+), 27 deletions(-) (limited to 'src/core/hle/service') diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp index 4c29784e8..da009df91 100644 --- a/src/core/hle/service/fs/archive.cpp +++ b/src/core/hle/service/fs/archive.cpp @@ -92,7 +92,7 @@ File::File(std::unique_ptr&& backend, const FileSys::Path& File::~File() {} -ResultVal File::SyncRequest() { +ResultCode File::HandleSyncRequest() { u32* cmd_buff = Kernel::GetCommandBuffer(); FileCommand cmd = static_cast(cmd_buff[0]); switch (cmd) { @@ -193,10 +193,10 @@ ResultVal File::SyncRequest() { LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd); ResultCode error = UnimplementedFunction(ErrorModule::FS); cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that. - return error; + return ServerSession::HandleSyncRequest(); } cmd_buff[1] = RESULT_SUCCESS.raw; // No error - return MakeResult(false); + return ServerSession::HandleSyncRequest(); } Directory::Directory(std::unique_ptr&& backend, @@ -205,7 +205,7 @@ Directory::Directory(std::unique_ptr&& backend, Directory::~Directory() {} -ResultVal Directory::SyncRequest() { +ResultCode Directory::HandleSyncRequest() { u32* cmd_buff = Kernel::GetCommandBuffer(); DirectoryCommand cmd = static_cast(cmd_buff[0]); switch (cmd) { @@ -236,10 +236,10 @@ ResultVal Directory::SyncRequest() { LOG_ERROR(Service_FS, "Unknown command=0x%08X!", cmd); ResultCode error = UnimplementedFunction(ErrorModule::FS); cmd_buff[1] = error.raw; // TODO(Link Mauve): use the correct error code for that. - return MakeResult(false); + return ServerSession::HandleSyncRequest(); } cmd_buff[1] = RESULT_SUCCESS.raw; // No error - return MakeResult(false); + return ServerSession::HandleSyncRequest(); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/hle/service/fs/archive.h b/src/core/hle/service/fs/archive.h index 21ed9717b..22e659c40 100644 --- a/src/core/hle/service/fs/archive.h +++ b/src/core/hle/service/fs/archive.h @@ -8,7 +8,7 @@ #include #include "common/common_types.h" #include "core/file_sys/archive_backend.h" -#include "core/hle/kernel/session.h" +#include "core/hle/kernel/server_session.h" #include "core/hle/result.h" namespace FileSys { @@ -41,7 +41,7 @@ enum class MediaType : u32 { NAND = 0, SDMC = 1 }; typedef u64 ArchiveHandle; -class File : public Kernel::Session { +class File : public Kernel::ServerSession { public: File(std::unique_ptr&& backend, const FileSys::Path& path); ~File(); @@ -49,14 +49,15 @@ public: std::string GetName() const override { return "Path: " + path.DebugStr(); } - ResultVal SyncRequest() override; + + ResultCode HandleSyncRequest() override; FileSys::Path path; ///< Path of the file u32 priority; ///< Priority of the file. TODO(Subv): Find out what this means std::unique_ptr backend; ///< File backend interface }; -class Directory : public Kernel::Session { +class Directory : public Kernel::ServerSession { public: Directory(std::unique_ptr&& backend, const FileSys::Path& path); ~Directory(); @@ -64,7 +65,8 @@ public: std::string GetName() const override { return "Directory: " + path.DebugStr(); } - ResultVal SyncRequest() override; + + ResultCode HandleSyncRequest() override; FileSys::Path path; ///< Path of the directory std::unique_ptr backend; ///< File backend interface diff --git a/src/core/hle/service/fs/fs_user.cpp b/src/core/hle/service/fs/fs_user.cpp index 9ec17b395..bb78091f9 100644 --- a/src/core/hle/service/fs/fs_user.cpp +++ b/src/core/hle/service/fs/fs_user.cpp @@ -8,6 +8,7 @@ #include "common/logging/log.h" #include "common/scope_exit.h" #include "common/string_util.h" +#include "core/hle/kernel/client_session.h" #include "core/hle/result.h" #include "core/hle/service/fs/archive.h" #include "core/hle/service/fs/fs_user.h" @@ -17,7 +18,7 @@ // Namespace FS_User using Kernel::SharedPtr; -using Kernel::Session; +using Kernel::ServerSession; namespace Service { namespace FS { @@ -70,7 +71,7 @@ static void OpenFile(Service::Interface* self) { ResultVal> file_res = OpenFileFromArchive(archive_handle, file_path, mode); cmd_buff[1] = file_res.Code().raw; if (file_res.Succeeded()) { - cmd_buff[3] = Kernel::g_handle_table.Create(*file_res).MoveFrom(); + cmd_buff[3] = Kernel::g_handle_table.Create((*file_res)->CreateClientSession()).MoveFrom(); } else { cmd_buff[3] = 0; LOG_ERROR(Service_FS, "failed to get a handle for file %s", file_path.DebugStr().c_str()); @@ -130,7 +131,7 @@ static void OpenFileDirectly(Service::Interface* self) { ResultVal> file_res = OpenFileFromArchive(*archive_handle, file_path, mode); cmd_buff[1] = file_res.Code().raw; if (file_res.Succeeded()) { - cmd_buff[3] = Kernel::g_handle_table.Create(*file_res).MoveFrom(); + cmd_buff[3] = Kernel::g_handle_table.Create((*file_res)->CreateClientSession()).MoveFrom(); } else { cmd_buff[3] = 0; LOG_ERROR(Service_FS, "failed to get a handle for file %s mode=%u attributes=%u", @@ -391,7 +392,7 @@ static void OpenDirectory(Service::Interface* self) { ResultVal> dir_res = OpenDirectoryFromArchive(archive_handle, dir_path); cmd_buff[1] = dir_res.Code().raw; if (dir_res.Succeeded()) { - cmd_buff[3] = Kernel::g_handle_table.Create(*dir_res).MoveFrom(); + cmd_buff[3] = Kernel::g_handle_table.Create((*dir_res)->CreateClientSession()).MoveFrom(); } else { LOG_ERROR(Service_FS, "failed to get a handle for directory type=%d size=%d data=%s", dirname_type, dirname_size, dir_path.DebugStr().c_str()); diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index ca7eeac8a..f51a042ff 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -41,8 +41,8 @@ namespace Service { -std::unordered_map> g_kernel_named_ports; -std::unordered_map> g_srv_services; +std::unordered_map> g_kernel_named_ports; +std::unordered_map> g_srv_services; /** * Creates a function string for logging, complete with the name (or header code, depending @@ -61,7 +61,7 @@ static std::string MakeFunctionString(const char* name, const char* port_name, return function_string; } -ResultVal Interface::SyncRequest() { +ResultCode Interface::HandleSyncRequest() { u32* cmd_buff = Kernel::GetCommandBuffer(); auto itr = m_functions.find(cmd_buff[0]); @@ -75,14 +75,14 @@ ResultVal Interface::SyncRequest() { // TODO(bunnei): Hack - ignore error cmd_buff[1] = 0; - return MakeResult(false); + return RESULT_SUCCESS; } LOG_TRACE(Service, "%s", MakeFunctionString(itr->second.name, GetPortName().c_str(), cmd_buff).c_str()); itr->second.func(this); - return MakeResult(false); // TODO: Implement return from actual function + return RESULT_SUCCESS; // TODO: Implement return from actual function, it should fail if the parameter translation fails } void Interface::Register(const FunctionInfo* functions, size_t n) { @@ -97,10 +97,16 @@ void Interface::Register(const FunctionInfo* functions, size_t n) { // Module interface static void AddNamedPort(Interface* interface_) { + interface_->name = interface_->GetPortName(); + interface_->active_sessions = 0; + interface_->max_sessions = interface_->GetMaxSessions(); g_kernel_named_ports.emplace(interface_->GetPortName(), interface_); } void AddService(Interface* interface_) { + interface_->name = interface_->GetPortName(); + interface_->active_sessions = 0; + interface_->max_sessions = interface_->GetMaxSessions(); g_srv_services.emplace(interface_->GetPortName(), interface_); } diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h index 29daacfc4..fd15ad03f 100644 --- a/src/core/hle/service/service.h +++ b/src/core/hle/service/service.h @@ -9,7 +9,8 @@ #include #include #include "common/common_types.h" -#include "core/hle/kernel/session.h" +#include "core/hle/kernel/client_port.h" +#include "core/hle/kernel/server_session.h" #include "core/hle/result.h" //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -18,9 +19,10 @@ namespace Service { static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters) +static const u32 DefaultMaxSessions = 10; ///< Arbitrary default number of maximum connections to an HLE port /// Interface to a CTROS service -class Interface : public Kernel::Session { +class Interface : public Kernel::ClientPort { // TODO(yuriks): An "Interface" being a Kernel::Object is mostly non-sense. Interface should be // just something that encapsulates a session and acts as a helper to implement service // processes. @@ -33,6 +35,15 @@ public: version.raw = raw_version; } + /** + * Gets the maximum allowed number of sessions that can be connected to this port at the same time. + * It should be overwritten by each service implementation for more fine-grained control. + * @returns The maximum number of connections allowed. + */ + virtual u32 GetMaxSessions() { return DefaultMaxSessions; } + + void AddWaitingSession(Kernel::SharedPtr server_session) override { } + typedef void (*Function)(Interface*); struct FunctionInfo { @@ -49,7 +60,7 @@ public: return "[UNKNOWN SERVICE PORT]"; } - ResultVal SyncRequest() override; + ResultCode HandleSyncRequest() override; protected: /** @@ -81,9 +92,9 @@ void Init(); void Shutdown(); /// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC. -extern std::unordered_map> g_kernel_named_ports; +extern std::unordered_map> g_kernel_named_ports; /// Map of services registered with the "srv:" service, retrieved using GetServiceHandle. -extern std::unordered_map> g_srv_services; +extern std::unordered_map> g_srv_services; /// Adds a service to the services table void AddService(Interface* interface_); diff --git a/src/core/hle/service/soc_u.cpp b/src/core/hle/service/soc_u.cpp index 46b75db25..2e8b2fc00 100644 --- a/src/core/hle/service/soc_u.cpp +++ b/src/core/hle/service/soc_u.cpp @@ -11,7 +11,7 @@ #include "common/common_types.h" #include "common/logging/log.h" #include "common/scope_exit.h" -#include "core/hle/kernel/session.h" +#include "core/hle/kernel/server_session.h" #include "core/hle/result.h" #include "core/hle/service/soc_u.h" #include "core/memory.h" diff --git a/src/core/hle/service/srv.cpp b/src/core/hle/service/srv.cpp index b25be413a..eb2e06041 100644 --- a/src/core/hle/service/srv.cpp +++ b/src/core/hle/service/srv.cpp @@ -2,8 +2,12 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include + #include "common/common_types.h" #include "common/logging/log.h" +#include "core/hle/service/srv.h" +#include "core/hle/kernel/client_session.h" #include "core/hle/kernel/event.h" #include "core/hle/service/srv.h" @@ -81,7 +85,18 @@ static void GetServiceHandle(Service::Interface* self) { auto it = Service::g_srv_services.find(port_name); if (it != Service::g_srv_services.end()) { - cmd_buff[3] = Kernel::g_handle_table.Create(it->second).MoveFrom(); + auto client_port = it->second; + + // Create a new session pair + auto sessions = Kernel::ServerSession::CreateSessionPair(client_port, port_name); + auto client_session = std::get>(sessions); + auto server_session = std::get>(sessions); + + // Add the server session to the port's queue + client_port->AddWaitingSession(server_session); + + // Return the client session + cmd_buff[3] = Kernel::g_handle_table.Create(client_session).MoveFrom(); LOG_TRACE(Service_SRV, "called port=%s, handle=0x%08X", port_name.c_str(), cmd_buff[3]); } else { LOG_ERROR(Service_SRV, "(UNIMPLEMENTED) called port=%s", port_name.c_str()); -- cgit v1.2.3