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.
This commit is contained in:
Subv 2016-06-14 18:03:30 -05:00
parent 68c00ee771
commit 073653e858
16 changed files with 315 additions and 89 deletions

View file

@ -92,7 +92,7 @@ File::File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path&
File::~File() {}
ResultVal<bool> File::SyncRequest() {
ResultCode File::HandleSyncRequest() {
u32* cmd_buff = Kernel::GetCommandBuffer();
FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]);
switch (cmd) {
@ -193,10 +193,10 @@ ResultVal<bool> 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<bool>(false);
return ServerSession::HandleSyncRequest();
}
Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend,
@ -205,7 +205,7 @@ Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend,
Directory::~Directory() {}
ResultVal<bool> Directory::SyncRequest() {
ResultCode Directory::HandleSyncRequest() {
u32* cmd_buff = Kernel::GetCommandBuffer();
DirectoryCommand cmd = static_cast<DirectoryCommand>(cmd_buff[0]);
switch (cmd) {
@ -236,10 +236,10 @@ ResultVal<bool> 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<bool>(false);
return ServerSession::HandleSyncRequest();
}
cmd_buff[1] = RESULT_SUCCESS.raw; // No error
return MakeResult<bool>(false);
return ServerSession::HandleSyncRequest();
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View file

@ -8,7 +8,7 @@
#include <string>
#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<FileSys::FileBackend>&& backend, const FileSys::Path& path);
~File();
@ -49,14 +49,15 @@ public:
std::string GetName() const override {
return "Path: " + path.DebugStr();
}
ResultVal<bool> 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<FileSys::FileBackend> backend; ///< File backend interface
};
class Directory : public Kernel::Session {
class Directory : public Kernel::ServerSession {
public:
Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend, const FileSys::Path& path);
~Directory();
@ -64,7 +65,8 @@ public:
std::string GetName() const override {
return "Directory: " + path.DebugStr();
}
ResultVal<bool> SyncRequest() override;
ResultCode HandleSyncRequest() override;
FileSys::Path path; ///< Path of the directory
std::unique_ptr<FileSys::DirectoryBackend> backend; ///< File backend interface

View file

@ -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<SharedPtr<File>> 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<SharedPtr<File>> 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<SharedPtr<Directory>> 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());

View file

@ -41,8 +41,8 @@
namespace Service {
std::unordered_map<std::string, Kernel::SharedPtr<Interface>> g_kernel_named_ports;
std::unordered_map<std::string, Kernel::SharedPtr<Interface>> g_srv_services;
std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports;
std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> 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<bool> Interface::SyncRequest() {
ResultCode Interface::HandleSyncRequest() {
u32* cmd_buff = Kernel::GetCommandBuffer();
auto itr = m_functions.find(cmd_buff[0]);
@ -75,14 +75,14 @@ ResultVal<bool> Interface::SyncRequest() {
// TODO(bunnei): Hack - ignore error
cmd_buff[1] = 0;
return MakeResult<bool>(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<bool>(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_);
}

View file

@ -9,7 +9,8 @@
#include <unordered_map>
#include <boost/container/flat_map.hpp>
#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<Kernel::ServerSession> server_session) override { }
typedef void (*Function)(Interface*);
struct FunctionInfo {
@ -49,7 +60,7 @@ public:
return "[UNKNOWN SERVICE PORT]";
}
ResultVal<bool> 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<std::string, Kernel::SharedPtr<Interface>> g_kernel_named_ports;
extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports;
/// Map of services registered with the "srv:" service, retrieved using GetServiceHandle.
extern std::unordered_map<std::string, Kernel::SharedPtr<Interface>> g_srv_services;
extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_srv_services;
/// Adds a service to the services table
void AddService(Interface* interface_);

View file

@ -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"

View file

@ -2,8 +2,12 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <tuple>
#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<Kernel::SharedPtr<Kernel::ClientSession>>(sessions);
auto server_session = std::get<Kernel::SharedPtr<Kernel::ServerSession>>(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());