2018-01-13 21:22:39 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
2017-06-06 06:31:59 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <tuple>
|
2017-06-07 04:25:28 +00:00
|
|
|
#include "common/assert.h"
|
2021-07-01 01:06:47 +00:00
|
|
|
#include "common/scope_exit.h"
|
2018-08-28 16:30:33 +00:00
|
|
|
#include "core/core.h"
|
2017-10-15 02:18:42 +00:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
2021-04-22 04:43:25 +00:00
|
|
|
#include "core/hle/kernel/k_client_port.h"
|
2021-04-14 00:48:37 +00:00
|
|
|
#include "core/hle/kernel/k_client_session.h"
|
2021-04-24 00:00:15 +00:00
|
|
|
#include "core/hle/kernel/k_port.h"
|
2021-05-10 23:18:30 +00:00
|
|
|
#include "core/hle/kernel/k_scoped_resource_reservation.h"
|
2021-04-22 04:53:56 +00:00
|
|
|
#include "core/hle/kernel/k_server_port.h"
|
2017-06-06 06:31:59 +00:00
|
|
|
#include "core/hle/result.h"
|
|
|
|
#include "core/hle/service/sm/sm.h"
|
2021-07-14 04:52:17 +00:00
|
|
|
#include "core/hle/service/sm/sm_controller.h"
|
2017-06-06 06:31:59 +00:00
|
|
|
|
2018-04-20 01:41:44 +00:00
|
|
|
namespace Service::SM {
|
2017-06-06 06:31:59 +00:00
|
|
|
|
2021-05-10 23:18:30 +00:00
|
|
|
constexpr ResultCode ERR_NOT_INITIALIZED(ErrorModule::SM, 2);
|
2018-09-14 05:43:59 +00:00
|
|
|
constexpr ResultCode ERR_ALREADY_REGISTERED(ErrorModule::SM, 4);
|
|
|
|
constexpr ResultCode ERR_INVALID_NAME(ErrorModule::SM, 6);
|
|
|
|
constexpr ResultCode ERR_SERVICE_NOT_REGISTERED(ErrorModule::SM, 7);
|
|
|
|
|
2020-09-17 14:43:54 +00:00
|
|
|
ServiceManager::ServiceManager(Kernel::KernelCore& kernel_) : kernel{kernel_} {}
|
2018-04-20 23:29:04 +00:00
|
|
|
ServiceManager::~ServiceManager() = default;
|
|
|
|
|
2017-10-15 02:18:42 +00:00
|
|
|
void ServiceManager::InvokeControlRequest(Kernel::HLERequestContext& context) {
|
|
|
|
controller_interface->InvokeRequest(context);
|
|
|
|
}
|
|
|
|
|
2017-06-06 06:31:59 +00:00
|
|
|
static ResultCode ValidateServiceName(const std::string& name) {
|
2020-09-17 14:54:09 +00:00
|
|
|
if (name.empty() || name.size() > 8) {
|
2020-04-29 01:15:21 +00:00
|
|
|
LOG_ERROR(Service_SM, "Invalid service name! service={}", name);
|
2018-09-14 05:43:59 +00:00
|
|
|
return ERR_INVALID_NAME;
|
2017-06-06 06:31:59 +00:00
|
|
|
}
|
2021-05-21 05:05:04 +00:00
|
|
|
return ResultSuccess;
|
2017-06-06 06:31:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 23:18:30 +00:00
|
|
|
Kernel::KClientPort& ServiceManager::InterfaceFactory(ServiceManager& self, Core::System& system) {
|
2021-07-01 01:06:47 +00:00
|
|
|
self.sm_interface = std::make_shared<SM>(self, system);
|
2021-05-10 23:18:30 +00:00
|
|
|
self.controller_interface = std::make_unique<Controller>(system);
|
2021-07-01 01:06:47 +00:00
|
|
|
return self.sm_interface->CreatePort();
|
2017-06-07 04:25:28 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 01:06:47 +00:00
|
|
|
ResultCode ServiceManager::RegisterService(std::string name, u32 max_sessions,
|
|
|
|
Kernel::SessionRequestHandlerPtr handler) {
|
2017-06-06 06:31:59 +00:00
|
|
|
|
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
2017-09-24 05:12:58 +00:00
|
|
|
|
2020-04-29 01:15:21 +00:00
|
|
|
if (registered_services.find(name) != registered_services.end()) {
|
|
|
|
LOG_ERROR(Service_SM, "Service is already registered! service={}", name);
|
2017-09-24 05:12:58 +00:00
|
|
|
return ERR_ALREADY_REGISTERED;
|
2020-04-29 01:15:21 +00:00
|
|
|
}
|
2017-09-24 05:12:58 +00:00
|
|
|
|
2021-07-01 01:06:47 +00:00
|
|
|
registered_services.emplace(std::move(name), handler);
|
2017-06-06 06:31:59 +00:00
|
|
|
|
2021-07-01 01:06:47 +00:00
|
|
|
return ResultSuccess;
|
2017-06-06 06:31:59 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 05:04:07 +00:00
|
|
|
ResultCode ServiceManager::UnregisterService(const std::string& name) {
|
2018-11-04 00:02:18 +00:00
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
|
|
|
|
|
|
|
const auto iter = registered_services.find(name);
|
2020-04-29 01:15:21 +00:00
|
|
|
if (iter == registered_services.end()) {
|
|
|
|
LOG_ERROR(Service_SM, "Server is not registered! service={}", name);
|
2018-11-04 00:02:18 +00:00
|
|
|
return ERR_SERVICE_NOT_REGISTERED;
|
2020-04-29 01:15:21 +00:00
|
|
|
}
|
2021-04-22 04:43:25 +00:00
|
|
|
|
2018-11-04 00:02:18 +00:00
|
|
|
registered_services.erase(iter);
|
2021-05-21 05:05:04 +00:00
|
|
|
return ResultSuccess;
|
2018-11-04 00:02:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 00:00:15 +00:00
|
|
|
ResultVal<Kernel::KPort*> ServiceManager::GetServicePort(const std::string& name) {
|
2017-06-06 06:31:59 +00:00
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
|
|
|
auto it = registered_services.find(name);
|
|
|
|
if (it == registered_services.end()) {
|
2020-04-29 01:15:21 +00:00
|
|
|
LOG_ERROR(Service_SM, "Server is not registered! service={}", name);
|
2017-06-06 06:31:59 +00:00
|
|
|
return ERR_SERVICE_NOT_REGISTERED;
|
|
|
|
}
|
|
|
|
|
2021-07-01 01:06:47 +00:00
|
|
|
auto* port = Kernel::KPort::Create(kernel);
|
2022-03-11 08:09:01 +00:00
|
|
|
SCOPE_EXIT({ port->Close(); });
|
|
|
|
|
2021-07-01 01:06:47 +00:00
|
|
|
port->Initialize(ServerSessionCountMax, false, name);
|
|
|
|
auto handler = it->second;
|
|
|
|
port->GetServerPort().SetSessionHandler(std::move(handler));
|
2017-06-06 06:31:59 +00:00
|
|
|
|
2021-11-02 21:23:19 +00:00
|
|
|
return port;
|
2021-07-01 01:06:47 +00:00
|
|
|
}
|
2017-06-06 06:31:59 +00:00
|
|
|
|
2017-10-15 02:18:42 +00:00
|
|
|
/**
|
|
|
|
* SM::Initialize service function
|
|
|
|
* Inputs:
|
|
|
|
* 0: 0x00000000
|
|
|
|
* Outputs:
|
2017-10-15 05:24:22 +00:00
|
|
|
* 0: ResultCode
|
2017-10-15 02:18:42 +00:00
|
|
|
*/
|
|
|
|
void SM::Initialize(Kernel::HLERequestContext& ctx) {
|
2018-11-26 06:06:13 +00:00
|
|
|
LOG_DEBUG(Service_SM, "called");
|
|
|
|
|
2021-05-10 22:59:19 +00:00
|
|
|
is_initialized = true;
|
|
|
|
|
2018-01-24 00:52:18 +00:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
2021-05-21 05:05:04 +00:00
|
|
|
rb.Push(ResultSuccess);
|
2017-10-15 02:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SM::GetService(Kernel::HLERequestContext& ctx) {
|
2021-05-10 23:18:30 +00:00
|
|
|
auto result = GetServiceImpl(ctx);
|
|
|
|
if (result.Succeeded()) {
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles};
|
|
|
|
rb.Push(result.Code());
|
|
|
|
rb.PushMoveObjects(result.Unwrap());
|
|
|
|
} else {
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(result.Code());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SM::GetServiceTipc(Kernel::HLERequestContext& ctx) {
|
|
|
|
auto result = GetServiceImpl(ctx);
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles};
|
|
|
|
rb.Push(result.Code());
|
|
|
|
rb.PushMoveObjects(result.Succeeded() ? result.Unwrap() : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string PopServiceName(IPC::RequestParser& rp) {
|
2018-01-07 14:57:41 +00:00
|
|
|
auto name_buf = rp.PopRaw<std::array<char, 8>>();
|
2021-05-10 23:18:30 +00:00
|
|
|
std::string result;
|
|
|
|
for (const auto& c : name_buf) {
|
|
|
|
if (c >= ' ' && c <= '~') {
|
|
|
|
result.push_back(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-01-07 14:57:41 +00:00
|
|
|
|
2021-05-10 23:18:30 +00:00
|
|
|
ResultVal<Kernel::KClientSession*> SM::GetServiceImpl(Kernel::HLERequestContext& ctx) {
|
|
|
|
if (!is_initialized) {
|
|
|
|
return ERR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
std::string name(PopServiceName(rp));
|
2017-10-15 02:18:42 +00:00
|
|
|
|
2021-05-16 06:49:03 +00:00
|
|
|
// Find the named port.
|
2021-06-10 18:34:41 +00:00
|
|
|
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();
|
2017-10-15 02:18:42 +00:00
|
|
|
}
|
2021-07-01 01:06:47 +00:00
|
|
|
auto& port = port_result.Unwrap();
|
|
|
|
SCOPE_EXIT({ port->GetClientPort().Close(); });
|
|
|
|
|
2021-05-10 23:12:01 +00:00
|
|
|
|
2021-05-16 06:49:03 +00:00
|
|
|
// Create a new session.
|
2021-06-10 05:29:18 +00:00
|
|
|
Kernel::KClientSession* session{};
|
2021-07-01 01:06:47 +00:00
|
|
|
if (const auto result = port->GetClientPort().CreateSession(std::addressof(session));
|
|
|
|
result.IsError()) {
|
2021-06-10 18:34:41 +00:00
|
|
|
LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, result.raw);
|
|
|
|
return result;
|
|
|
|
}
|
2019-11-25 23:28:48 +00:00
|
|
|
|
2021-04-24 04:50:04 +00:00
|
|
|
LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetId());
|
2021-05-16 06:49:03 +00:00
|
|
|
|
2021-11-02 21:23:19 +00:00
|
|
|
return session;
|
2017-10-15 02:18:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 00:02:18 +00:00
|
|
|
void SM::RegisterService(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
2021-05-10 23:18:30 +00:00
|
|
|
std::string name(PopServiceName(rp));
|
2018-11-04 00:02:18 +00:00
|
|
|
|
2018-12-18 04:04:35 +00:00
|
|
|
const auto is_light = static_cast<bool>(rp.PopRaw<u32>());
|
|
|
|
const auto max_session_count = rp.PopRaw<u32>();
|
2018-11-04 00:02:18 +00:00
|
|
|
|
2018-12-18 04:04:35 +00:00
|
|
|
LOG_DEBUG(Service_SM, "called with name={}, max_session_count={}, is_light={}", name,
|
|
|
|
max_session_count, is_light);
|
2018-11-04 00:02:18 +00:00
|
|
|
|
2021-07-01 01:06:47 +00:00
|
|
|
if (const auto result = service_manager.RegisterService(name, max_session_count, nullptr);
|
|
|
|
result.IsError()) {
|
|
|
|
LOG_ERROR(Service_SM, "failed to register service with error_code={:08X}", result.raw);
|
2018-11-04 00:02:18 +00:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
2021-07-01 01:06:47 +00:00
|
|
|
rb.Push(result);
|
2018-11-04 00:02:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-01 01:06:47 +00:00
|
|
|
auto* port = Kernel::KPort::Create(kernel);
|
|
|
|
port->Initialize(ServerSessionCountMax, is_light, name);
|
|
|
|
SCOPE_EXIT({ port->GetClientPort().Close(); });
|
2021-04-04 04:21:22 +00:00
|
|
|
|
2021-07-01 01:06:47 +00:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles};
|
|
|
|
rb.Push(ResultSuccess);
|
|
|
|
rb.PushMoveObjects(port->GetServerPort());
|
2018-11-04 00:02:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SM::UnregisterService(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
2021-05-10 23:18:30 +00:00
|
|
|
std::string name(PopServiceName(rp));
|
2018-11-04 00:02:18 +00:00
|
|
|
|
2018-11-26 06:06:13 +00:00
|
|
|
LOG_DEBUG(Service_SM, "called with name={}", name);
|
2018-11-04 00:02:18 +00:00
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
2021-05-10 23:18:30 +00:00
|
|
|
rb.Push(service_manager.UnregisterService(name));
|
2018-11-04 00:02:18 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 23:18:30 +00:00
|
|
|
SM::SM(ServiceManager& service_manager_, Core::System& system_)
|
2022-03-31 04:13:53 +00:00
|
|
|
: ServiceFramework{system_, "sm:", ServiceThreadType::Default, 4},
|
2021-05-10 23:18:30 +00:00
|
|
|
service_manager{service_manager_}, kernel{system_.Kernel()} {
|
|
|
|
RegisterHandlers({
|
2021-04-08 20:13:33 +00:00
|
|
|
{0, &SM::Initialize, "Initialize"},
|
|
|
|
{1, &SM::GetService, "GetService"},
|
|
|
|
{2, &SM::RegisterService, "RegisterService"},
|
|
|
|
{3, &SM::UnregisterService, "UnregisterService"},
|
|
|
|
{4, nullptr, "DetachClient"},
|
2021-05-10 23:18:30 +00:00
|
|
|
});
|
|
|
|
RegisterHandlersTipc({
|
|
|
|
{0, &SM::Initialize, "Initialize"},
|
|
|
|
{1, &SM::GetServiceTipc, "GetService"},
|
|
|
|
{2, &SM::RegisterService, "RegisterService"},
|
|
|
|
{3, &SM::UnregisterService, "UnregisterService"},
|
|
|
|
{4, nullptr, "DetachClient"},
|
|
|
|
});
|
2017-10-15 02:18:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 00:37:00 +00:00
|
|
|
SM::~SM() = default;
|
2021-07-01 01:06:47 +00:00
|
|
|
|
2018-04-20 01:41:44 +00:00
|
|
|
} // namespace Service::SM
|