Merge pull request #2249 from Subv/sessions_v3

Kernel/IPC: Use Ports and Sessions as the fundamental building block of Inter Process Communication.
This commit is contained in:
Yuri Kunde Schlesner 2016-12-14 20:35:33 -08:00 committed by GitHub
commit 905fc92ce1
25 changed files with 591 additions and 171 deletions

View file

@ -14,6 +14,9 @@ class Interface;
namespace APT {
/// Each APT service can only have up to 2 sessions connected at the same time.
static const u32 MaxAPTSessions = 2;
/// Holds information about the parameters used in Send/Glance/ReceiveParameter
struct MessageParameter {
u32 sender_id = 0;

View file

@ -102,7 +102,7 @@ const Interface::FunctionInfo FunctionTable[] = {
{0x01050100, nullptr, "IsTitleAllowed"},
};
APT_A_Interface::APT_A_Interface() {
APT_A_Interface::APT_A_Interface() : Interface(MaxAPTSessions) {
Register(FunctionTable);
}

View file

@ -102,7 +102,7 @@ const Interface::FunctionInfo FunctionTable[] = {
{0x01050100, nullptr, "IsTitleAllowed"},
};
APT_S_Interface::APT_S_Interface() {
APT_S_Interface::APT_S_Interface() : Interface(MaxAPTSessions) {
Register(FunctionTable);
}

View file

@ -99,7 +99,7 @@ const Interface::FunctionInfo FunctionTable[] = {
{0x01020000, CheckNew3DS, "CheckNew3DS"},
};
APT_U_Interface::APT_U_Interface() {
APT_U_Interface::APT_U_Interface() : Interface(MaxAPTSessions) {
Register(FunctionTable);
}

View file

@ -24,6 +24,7 @@
#include "core/file_sys/directory_backend.h"
#include "core/file_sys/file_backend.h"
#include "core/hle/hle.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"
@ -93,7 +94,7 @@ File::File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path&
File::~File() {}
ResultVal<bool> File::SyncRequest() {
void File::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
u32* cmd_buff = Kernel::GetCommandBuffer();
FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]);
switch (cmd) {
@ -116,7 +117,7 @@ ResultVal<bool> File::SyncRequest() {
ResultVal<size_t> read = backend->Read(offset, data.size(), data.data());
if (read.Failed()) {
cmd_buff[1] = read.Code().raw;
return read.Code();
return;
}
Memory::WriteBlock(address, data.data(), *read);
cmd_buff[2] = static_cast<u32>(*read);
@ -137,7 +138,7 @@ ResultVal<bool> File::SyncRequest() {
ResultVal<size_t> written = backend->Write(offset, data.size(), flush != 0, data.data());
if (written.Failed()) {
cmd_buff[1] = written.Code().raw;
return written.Code();
return;
}
cmd_buff[2] = static_cast<u32>(*written);
break;
@ -173,7 +174,11 @@ ResultVal<bool> File::SyncRequest() {
case FileCommand::OpenLinkFile: {
LOG_WARNING(Service_FS, "(STUBBED) File command OpenLinkFile %s", GetName().c_str());
cmd_buff[3] = Kernel::g_handle_table.Create(this).ValueOr(INVALID_HANDLE);
auto sessions = Kernel::ServerSession::CreateSessionPair(GetName(), shared_from_this());
ClientConnected(std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions));
cmd_buff[3] = Kernel::g_handle_table
.Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions))
.ValueOr(INVALID_HANDLE);
break;
}
@ -194,10 +199,9 @@ 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;
}
cmd_buff[1] = RESULT_SUCCESS.raw; // No error
return MakeResult<bool>(false);
}
Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend,
@ -206,11 +210,10 @@ Directory::Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend,
Directory::~Directory() {}
ResultVal<bool> Directory::SyncRequest() {
void Directory::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
u32* cmd_buff = Kernel::GetCommandBuffer();
DirectoryCommand cmd = static_cast<DirectoryCommand>(cmd_buff[0]);
switch (cmd) {
// Read from directory...
case DirectoryCommand::Read: {
u32 count = cmd_buff[1];
@ -237,10 +240,9 @@ 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;
}
cmd_buff[1] = RESULT_SUCCESS.raw; // No error
return MakeResult<bool>(false);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -307,9 +309,9 @@ ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factor
return RESULT_SUCCESS;
}
ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
const FileSys::Path& path,
const FileSys::Mode mode) {
ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
const FileSys::Path& path,
const FileSys::Mode mode) {
ArchiveBackend* archive = GetArchive(archive_handle);
if (archive == nullptr)
return ERR_INVALID_ARCHIVE_HANDLE;
@ -318,8 +320,8 @@ ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_han
if (backend.Failed())
return backend.Code();
auto file = Kernel::SharedPtr<File>(new File(backend.MoveFrom(), path));
return MakeResult<Kernel::SharedPtr<File>>(std::move(file));
auto file = std::shared_ptr<File>(new File(backend.MoveFrom(), path));
return MakeResult<std::shared_ptr<File>>(std::move(file));
}
ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
@ -398,8 +400,8 @@ ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
}
}
ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
const FileSys::Path& path) {
ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
const FileSys::Path& path) {
ArchiveBackend* archive = GetArchive(archive_handle);
if (archive == nullptr)
return ERR_INVALID_ARCHIVE_HANDLE;
@ -408,8 +410,8 @@ ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle a
if (backend.Failed())
return backend.Code();
auto directory = Kernel::SharedPtr<Directory>(new Directory(backend.MoveFrom(), path));
return MakeResult<Kernel::SharedPtr<Directory>>(std::move(directory));
auto directory = std::shared_ptr<Directory>(new Directory(backend.MoveFrom(), path));
return MakeResult<std::shared_ptr<Directory>>(std::move(directory));
}
ResultVal<u64> GetFreeBytesInArchive(ArchiveHandle archive_handle) {

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 {
@ -43,33 +43,37 @@ enum class MediaType : u32 { NAND = 0, SDMC = 1, GameCard = 2 };
typedef u64 ArchiveHandle;
class File : public Kernel::Session {
class File final : public SessionRequestHandler, public std::enable_shared_from_this<File> {
public:
File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path);
~File();
std::string GetName() const override {
std::string GetName() const {
return "Path: " + path.DebugStr();
}
ResultVal<bool> SyncRequest() 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
protected:
void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
};
class Directory : public Kernel::Session {
class Directory final : public SessionRequestHandler {
public:
Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend, const FileSys::Path& path);
~Directory();
std::string GetName() const override {
std::string GetName() const {
return "Directory: " + path.DebugStr();
}
ResultVal<bool> SyncRequest() override;
FileSys::Path path; ///< Path of the directory
std::unique_ptr<FileSys::DirectoryBackend> backend; ///< File backend interface
protected:
void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
};
/**
@ -99,11 +103,11 @@ ResultCode RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factor
* @param archive_handle Handle to an open Archive object
* @param path Path to the File inside of the Archive
* @param mode Mode under which to open the File
* @return The opened File object as a Session
* @return The opened File object
*/
ResultVal<Kernel::SharedPtr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
const FileSys::Path& path,
const FileSys::Mode mode);
ResultVal<std::shared_ptr<File>> OpenFileFromArchive(ArchiveHandle archive_handle,
const FileSys::Path& path,
const FileSys::Mode mode);
/**
* Delete a File from an Archive
@ -178,10 +182,10 @@ ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
* Open a Directory from an Archive
* @param archive_handle Handle to an open Archive object
* @param path Path to the Directory inside of the Archive
* @return The opened Directory object as a Session
* @return The opened Directory object
*/
ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
const FileSys::Path& path);
ResultVal<std::shared_ptr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
const FileSys::Path& path);
/**
* Get the free space in an Archive

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 {
@ -67,10 +68,16 @@ static void OpenFile(Service::Interface* self) {
LOG_DEBUG(Service_FS, "path=%s, mode=%u attrs=%u", file_path.DebugStr().c_str(), mode.hex,
attributes);
ResultVal<SharedPtr<File>> file_res = OpenFileFromArchive(archive_handle, file_path, mode);
ResultVal<std::shared_ptr<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();
std::shared_ptr<File> file = *file_res;
auto sessions = ServerSession::CreateSessionPair(file->GetName(), file);
file->ClientConnected(std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions));
cmd_buff[3] = Kernel::g_handle_table
.Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions))
.MoveFrom();
} else {
cmd_buff[3] = 0;
LOG_ERROR(Service_FS, "failed to get a handle for file %s", file_path.DebugStr().c_str());
@ -127,10 +134,16 @@ static void OpenFileDirectly(Service::Interface* self) {
}
SCOPE_EXIT({ CloseArchive(*archive_handle); });
ResultVal<SharedPtr<File>> file_res = OpenFileFromArchive(*archive_handle, file_path, mode);
ResultVal<std::shared_ptr<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();
std::shared_ptr<File> file = *file_res;
auto sessions = ServerSession::CreateSessionPair(file->GetName(), file);
file->ClientConnected(std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions));
cmd_buff[3] = Kernel::g_handle_table
.Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions))
.MoveFrom();
} else {
cmd_buff[3] = 0;
LOG_ERROR(Service_FS, "failed to get a handle for file %s mode=%u attributes=%u",
@ -388,10 +401,16 @@ static void OpenDirectory(Service::Interface* self) {
LOG_DEBUG(Service_FS, "type=%u size=%u data=%s", static_cast<u32>(dirname_type), dirname_size,
dir_path.DebugStr().c_str());
ResultVal<SharedPtr<Directory>> dir_res = OpenDirectoryFromArchive(archive_handle, dir_path);
ResultVal<std::shared_ptr<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();
std::shared_ptr<Directory> directory = *dir_res;
auto sessions = ServerSession::CreateSessionPair(directory->GetName(), directory);
directory->ClientConnected(std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions));
cmd_buff[3] = Kernel::g_handle_table
.Create(std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions))
.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

@ -2,8 +2,12 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <boost/range/algorithm_ext/erase.hpp>
#include "common/logging/log.h"
#include "common/string_util.h"
#include "core/hle/kernel/server_port.h"
#include "core/hle/service/ac_u.h"
#include "core/hle/service/act_a.h"
#include "core/hle/service/act_u.h"
@ -44,8 +48,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
@ -64,7 +68,23 @@ static std::string MakeFunctionString(const char* name, const char* port_name,
return function_string;
}
ResultVal<bool> Interface::SyncRequest() {
void SessionRequestHandler::ClientConnected(
Kernel::SharedPtr<Kernel::ServerSession> server_session) {
connected_sessions.push_back(server_session);
}
void SessionRequestHandler::ClientDisconnected(
Kernel::SharedPtr<Kernel::ServerSession> server_session) {
boost::range::remove_erase(connected_sessions, server_session);
}
Interface::Interface(u32 max_sessions) : max_sessions(max_sessions) {}
Interface::~Interface() = default;
void Interface::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) {
// TODO(Subv): Make use of the server_session in the HLE service handlers to distinguish which
// session triggered each command.
u32* cmd_buff = Kernel::GetCommandBuffer();
auto itr = m_functions.find(cmd_buff[0]);
@ -78,14 +98,12 @@ ResultVal<bool> Interface::SyncRequest() {
// TODO(bunnei): Hack - ignore error
cmd_buff[1] = 0;
return MakeResult<bool>(false);
return;
}
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
}
void Interface::Register(const FunctionInfo* functions, size_t n) {
@ -100,11 +118,19 @@ void Interface::Register(const FunctionInfo* functions, size_t n) {
// Module interface
static void AddNamedPort(Interface* interface_) {
g_kernel_named_ports.emplace(interface_->GetPortName(), interface_);
auto ports =
Kernel::ServerPort::CreatePortPair(interface_->GetMaxSessions(), interface_->GetPortName(),
std::shared_ptr<Interface>(interface_));
auto client_port = std::get<Kernel::SharedPtr<Kernel::ClientPort>>(ports);
g_kernel_named_ports.emplace(interface_->GetPortName(), std::move(client_port));
}
void AddService(Interface* interface_) {
g_srv_services.emplace(interface_->GetPortName(), interface_);
auto ports =
Kernel::ServerPort::CreatePortPair(interface_->GetMaxSessions(), interface_->GetPortName(),
std::shared_ptr<Interface>(interface_));
auto client_port = std::get<Kernel::SharedPtr<Kernel::ClientPort>>(ports);
g_srv_services.emplace(interface_->GetPortName(), std::move(client_port));
}
/// Initialize ServiceManager

View file

@ -9,8 +9,15 @@
#include <unordered_map>
#include <boost/container/flat_map.hpp>
#include "common/common_types.h"
#include "core/hle/kernel/session.h"
#include "core/hle/ipc.h"
#include "core/hle/kernel/client_port.h"
#include "core/hle/kernel/thread.h"
#include "core/hle/result.h"
#include "core/memory.h"
namespace Kernel {
class ServerSession;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Namespace Service
@ -18,14 +25,63 @@
namespace Service {
static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
/// Arbitrary default number of maximum connections to an HLE service.
static const u32 DefaultMaxSessions = 10;
/// Interface to a CTROS service
class Interface : public Kernel::Session {
// 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.
/**
* Interface implemented by HLE Session handlers.
* This can be provided to a ServerSession in order to hook into several relevant events
* (such as a new connection or a SyncRequest) so they can be implemented in the emulator.
*/
class SessionRequestHandler {
public:
std::string GetName() const override {
/**
* Handles a sync request from the emulated application.
* @param server_session The ServerSession that was triggered for this sync request,
* it should be used to differentiate which client (As in ClientSession) we're answering to.
* TODO(Subv): Use a wrapper structure to hold all the information relevant to
* this request (ServerSession, Originator thread, Translated command buffer, etc).
* @returns ResultCode the result code of the translate operation.
*/
virtual void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) = 0;
/**
* Signals that a client has just connected to this HLE handler and keeps the
* associated ServerSession alive for the duration of the connection.
* @param server_session Owning pointer to the ServerSession associated with the connection.
*/
void ClientConnected(Kernel::SharedPtr<Kernel::ServerSession> server_session);
/**
* Signals that a client has just disconnected from this HLE handler and releases the
* associated ServerSession.
* @param server_session ServerSession associated with the connection.
*/
void ClientDisconnected(Kernel::SharedPtr<Kernel::ServerSession> server_session);
protected:
/// List of sessions that are connected to this handler.
/// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list
// for the duration of the connection.
std::vector<Kernel::SharedPtr<Kernel::ServerSession>> connected_sessions;
};
/**
* Framework for implementing HLE service handlers which dispatch incoming SyncRequests based on a
* table mapping header ids to handler functions.
*/
class Interface : public SessionRequestHandler {
public:
/**
* Creates an HLE interface with the specified max sessions.
* @param max_sessions Maximum number of sessions that can be
* connected to this service at the same time.
*/
Interface(u32 max_sessions = DefaultMaxSessions);
virtual ~Interface();
std::string GetName() const {
return GetPortName();
}
@ -33,6 +89,15 @@ public:
version.raw = raw_version;
}
/**
* Gets the maximum allowed number of sessions that can be connected to this service
* at the same time.
* @returns The maximum number of connections allowed.
*/
u32 GetMaxSessions() const {
return max_sessions;
}
typedef void (*Function)(Interface*);
struct FunctionInfo {
@ -49,9 +114,9 @@ public:
return "[UNKNOWN SERVICE PORT]";
}
ResultVal<bool> SyncRequest() override;
protected:
void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
/**
* Registers the functions in the service
*/
@ -71,6 +136,7 @@ protected:
} version = {};
private:
u32 max_sessions; ///< Maximum number of concurrent sessions that this service can handle.
boost::container::flat_map<u32, FunctionInfo> m_functions;
};
@ -81,9 +147,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,9 +2,13 @@
// 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/kernel/client_session.h"
#include "core/hle/kernel/event.h"
#include "core/hle/kernel/server_session.h"
#include "core/hle/service/srv.h"
namespace Service {
@ -79,7 +83,15 @@ static void GetServiceHandle(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;
auto client_session = client_port->Connect();
res = client_session.Code();
if (client_session.Succeeded()) {
// 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());