2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-06-06 02:31:59 -04:00
|
|
|
|
|
|
|
#include <tuple>
|
2017-06-07 00:25:28 -04:00
|
|
|
#include "common/assert.h"
|
2021-06-30 21:06:47 -04:00
|
|
|
#include "common/scope_exit.h"
|
2018-08-28 12:30:33 -04:00
|
|
|
#include "core/core.h"
|
2017-10-14 22:18:42 -04:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
2021-04-22 00:43:25 -04:00
|
|
|
#include "core/hle/kernel/k_client_port.h"
|
2021-04-13 20:48:37 -04:00
|
|
|
#include "core/hle/kernel/k_client_session.h"
|
2021-04-23 20:00:15 -04:00
|
|
|
#include "core/hle/kernel/k_port.h"
|
2021-05-10 19:18:30 -04:00
|
|
|
#include "core/hle/kernel/k_scoped_resource_reservation.h"
|
2021-04-22 00:53:56 -04:00
|
|
|
#include "core/hle/kernel/k_server_port.h"
|
2017-06-06 02:31:59 -04:00
|
|
|
#include "core/hle/result.h"
|
2023-02-18 16:26:48 -05:00
|
|
|
#include "core/hle/service/server_manager.h"
|
2017-06-06 02:31:59 -04:00
|
|
|
#include "core/hle/service/sm/sm.h"
|
2021-07-14 00:52:17 -04:00
|
|
|
#include "core/hle/service/sm/sm_controller.h"
|
2017-06-06 02:31:59 -04:00
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
namespace Service::SM {
|
2017-06-06 02:31:59 -04:00
|
|
|
|
2022-06-25 23:44:19 -04:00
|
|
|
constexpr Result ERR_NOT_INITIALIZED(ErrorModule::SM, 2);
|
|
|
|
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);
|
2018-09-14 01:43:59 -04:00
|
|
|
|
2023-02-18 16:26:48 -05:00
|
|
|
ServiceManager::ServiceManager(Kernel::KernelCore& kernel_) : kernel{kernel_} {
|
|
|
|
controller_interface = std::make_unique<Controller>(kernel.System());
|
|
|
|
}
|
2022-10-30 22:22:14 -04:00
|
|
|
|
|
|
|
ServiceManager::~ServiceManager() {
|
|
|
|
for (auto& [name, port] : service_ports) {
|
|
|
|
port->GetClientPort().Close();
|
|
|
|
port->GetServerPort().Close();
|
|
|
|
}
|
2023-02-19 08:44:54 -05:00
|
|
|
|
|
|
|
if (deferral_event) {
|
|
|
|
deferral_event->Close();
|
|
|
|
}
|
2022-10-30 22:22:14 -04:00
|
|
|
}
|
2018-04-20 19:29:04 -04:00
|
|
|
|
2017-10-14 22:18:42 -04:00
|
|
|
void ServiceManager::InvokeControlRequest(Kernel::HLERequestContext& context) {
|
|
|
|
controller_interface->InvokeRequest(context);
|
|
|
|
}
|
|
|
|
|
2022-06-25 23:44:19 -04:00
|
|
|
static Result ValidateServiceName(const std::string& name) {
|
2020-09-17 10:54:09 -04:00
|
|
|
if (name.empty() || name.size() > 8) {
|
2020-04-28 21:15:21 -04:00
|
|
|
LOG_ERROR(Service_SM, "Invalid service name! service={}", name);
|
2018-09-14 01:43:59 -04:00
|
|
|
return ERR_INVALID_NAME;
|
2017-06-06 02:31:59 -04:00
|
|
|
}
|
2021-05-21 01:05:04 -04:00
|
|
|
return ResultSuccess;
|
2017-06-06 02:31:59 -04:00
|
|
|
}
|
|
|
|
|
2022-06-25 23:44:19 -04:00
|
|
|
Result ServiceManager::RegisterService(std::string name, u32 max_sessions,
|
|
|
|
Kernel::SessionRequestHandlerPtr handler) {
|
2017-06-06 02:31:59 -04:00
|
|
|
|
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
2017-09-24 01:12:58 -04:00
|
|
|
|
2023-02-18 16:26:48 -05:00
|
|
|
std::scoped_lock lk{lock};
|
2020-04-28 21:15:21 -04:00
|
|
|
if (registered_services.find(name) != registered_services.end()) {
|
|
|
|
LOG_ERROR(Service_SM, "Service is already registered! service={}", name);
|
2017-09-24 01:12:58 -04:00
|
|
|
return ERR_ALREADY_REGISTERED;
|
2020-04-28 21:15:21 -04:00
|
|
|
}
|
2017-09-24 01:12:58 -04:00
|
|
|
|
2022-10-30 22:22:14 -04:00
|
|
|
auto* port = Kernel::KPort::Create(kernel);
|
|
|
|
port->Initialize(ServerSessionCountMax, false, name);
|
|
|
|
|
|
|
|
service_ports.emplace(name, port);
|
|
|
|
registered_services.emplace(name, handler);
|
2023-02-19 08:44:54 -05:00
|
|
|
if (deferral_event) {
|
|
|
|
deferral_event->Signal();
|
|
|
|
}
|
2017-06-06 02:31:59 -04:00
|
|
|
|
2021-06-30 21:06:47 -04:00
|
|
|
return ResultSuccess;
|
2017-06-06 02:31:59 -04:00
|
|
|
}
|
|
|
|
|
2022-06-25 23:44:19 -04:00
|
|
|
Result ServiceManager::UnregisterService(const std::string& name) {
|
2018-11-03 20:02:18 -04:00
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
|
|
|
|
2023-02-18 16:26:48 -05:00
|
|
|
std::scoped_lock lk{lock};
|
2018-11-03 20:02:18 -04:00
|
|
|
const auto iter = registered_services.find(name);
|
2020-04-28 21:15:21 -04:00
|
|
|
if (iter == registered_services.end()) {
|
|
|
|
LOG_ERROR(Service_SM, "Server is not registered! service={}", name);
|
2018-11-03 20:02:18 -04:00
|
|
|
return ERR_SERVICE_NOT_REGISTERED;
|
2020-04-28 21:15:21 -04:00
|
|
|
}
|
2021-04-22 00:43:25 -04:00
|
|
|
|
2018-11-03 20:02:18 -04:00
|
|
|
registered_services.erase(iter);
|
2022-10-30 22:22:14 -04:00
|
|
|
service_ports.erase(name);
|
|
|
|
|
2021-05-21 01:05:04 -04:00
|
|
|
return ResultSuccess;
|
2018-11-03 20:02:18 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 20:00:15 -04:00
|
|
|
ResultVal<Kernel::KPort*> ServiceManager::GetServicePort(const std::string& name) {
|
2017-06-06 02:31:59 -04:00
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
2023-02-18 16:26:48 -05:00
|
|
|
|
|
|
|
std::scoped_lock lk{lock};
|
2022-10-30 22:22:14 -04:00
|
|
|
auto it = service_ports.find(name);
|
|
|
|
if (it == service_ports.end()) {
|
2023-02-19 08:44:54 -05:00
|
|
|
LOG_WARNING(Service_SM, "Server is not registered! service={}", name);
|
2017-06-06 02:31:59 -04:00
|
|
|
return ERR_SERVICE_NOT_REGISTERED;
|
|
|
|
}
|
|
|
|
|
2022-10-30 22:22:14 -04:00
|
|
|
return it->second;
|
2021-06-30 21:06:47 -04:00
|
|
|
}
|
2017-06-06 02:31:59 -04:00
|
|
|
|
2017-10-14 22:18:42 -04:00
|
|
|
/**
|
|
|
|
* SM::Initialize service function
|
|
|
|
* Inputs:
|
|
|
|
* 0: 0x00000000
|
|
|
|
* Outputs:
|
2022-06-25 23:44:19 -04:00
|
|
|
* 0: Result
|
2017-10-14 22:18:42 -04:00
|
|
|
*/
|
|
|
|
void SM::Initialize(Kernel::HLERequestContext& ctx) {
|
2018-11-26 01:06:13 -05:00
|
|
|
LOG_DEBUG(Service_SM, "called");
|
|
|
|
|
2023-02-21 12:19:12 -05:00
|
|
|
ctx.GetManager()->SetIsInitializedForSm();
|
2021-05-10 18:59:19 -04:00
|
|
|
|
2018-01-23 19:52:18 -05:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
2021-05-21 01:05:04 -04:00
|
|
|
rb.Push(ResultSuccess);
|
2017-10-14 22:18:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SM::GetService(Kernel::HLERequestContext& ctx) {
|
2021-05-10 19:18:30 -04:00
|
|
|
auto result = GetServiceImpl(ctx);
|
2023-02-19 08:44:54 -05:00
|
|
|
if (ctx.GetIsDeferred()) {
|
|
|
|
// Don't overwrite the command buffer.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-10 19:18:30 -04:00
|
|
|
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);
|
2023-02-19 08:44:54 -05:00
|
|
|
if (ctx.GetIsDeferred()) {
|
|
|
|
// Don't overwrite the command buffer.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-10 19:18:30 -04:00
|
|
|
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 09:57:41 -05:00
|
|
|
auto name_buf = rp.PopRaw<std::array<char, 8>>();
|
2021-05-10 19:18:30 -04:00
|
|
|
std::string result;
|
|
|
|
for (const auto& c : name_buf) {
|
|
|
|
if (c >= ' ' && c <= '~') {
|
|
|
|
result.push_back(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-01-07 09:57:41 -05:00
|
|
|
|
2021-05-10 19:18:30 -04:00
|
|
|
ResultVal<Kernel::KClientSession*> SM::GetServiceImpl(Kernel::HLERequestContext& ctx) {
|
2023-02-21 12:19:12 -05:00
|
|
|
if (!ctx.GetManager()->GetIsInitializedForSm()) {
|
2021-05-10 19:18:30 -04:00
|
|
|
return ERR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
std::string name(PopServiceName(rp));
|
2017-10-14 22:18:42 -04:00
|
|
|
|
2021-05-16 02:49:03 -04:00
|
|
|
// Find the named port.
|
2021-06-10 14:34:41 -04:00
|
|
|
auto port_result = service_manager.GetServicePort(name);
|
2023-02-21 12:19:12 -05:00
|
|
|
if (port_result.Code() == ERR_INVALID_NAME) {
|
|
|
|
LOG_ERROR(Service_SM, "Invalid service name '{}'", name);
|
|
|
|
return ERR_INVALID_NAME;
|
|
|
|
}
|
|
|
|
|
2023-02-18 16:26:48 -05:00
|
|
|
if (port_result.Failed()) {
|
2023-02-19 08:44:54 -05:00
|
|
|
LOG_INFO(Service_SM, "Waiting for service {} to become available", name);
|
|
|
|
ctx.SetIsDeferred();
|
|
|
|
return ERR_SERVICE_NOT_REGISTERED;
|
2017-10-14 22:18:42 -04:00
|
|
|
}
|
2021-06-30 21:06:47 -04:00
|
|
|
auto& port = port_result.Unwrap();
|
2021-05-10 19:12:01 -04:00
|
|
|
|
2021-05-16 02:49:03 -04:00
|
|
|
// Create a new session.
|
2021-06-10 01:29:18 -04:00
|
|
|
Kernel::KClientSession* session{};
|
2022-10-16 01:53:56 -04:00
|
|
|
if (const auto result = port->GetClientPort().CreateSession(&session); result.IsError()) {
|
2021-06-10 14:34:41 -04:00
|
|
|
LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, result.raw);
|
|
|
|
return result;
|
|
|
|
}
|
2019-11-25 18:28:48 -05:00
|
|
|
|
2021-04-24 00:50:04 -04:00
|
|
|
LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetId());
|
2021-05-16 02:49:03 -04:00
|
|
|
|
2021-11-02 17:23:19 -04:00
|
|
|
return session;
|
2017-10-14 22:18:42 -04:00
|
|
|
}
|
|
|
|
|
2018-11-03 20:02:18 -04:00
|
|
|
void SM::RegisterService(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
2021-05-10 19:18:30 -04:00
|
|
|
std::string name(PopServiceName(rp));
|
2018-11-03 20:02:18 -04:00
|
|
|
|
2018-12-17 23:04:35 -05:00
|
|
|
const auto is_light = static_cast<bool>(rp.PopRaw<u32>());
|
|
|
|
const auto max_session_count = rp.PopRaw<u32>();
|
2018-11-03 20:02:18 -04:00
|
|
|
|
2018-12-17 23:04:35 -05:00
|
|
|
LOG_DEBUG(Service_SM, "called with name={}, max_session_count={}, is_light={}", name,
|
|
|
|
max_session_count, is_light);
|
2018-11-03 20:02:18 -04:00
|
|
|
|
2021-06-30 21:06:47 -04: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-03 20:02:18 -04:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
2021-06-30 21:06:47 -04:00
|
|
|
rb.Push(result);
|
2018-11-03 20:02:18 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-30 21:06:47 -04:00
|
|
|
auto* port = Kernel::KPort::Create(kernel);
|
|
|
|
port->Initialize(ServerSessionCountMax, is_light, name);
|
|
|
|
SCOPE_EXIT({ port->GetClientPort().Close(); });
|
2021-04-04 00:21:22 -04:00
|
|
|
|
2021-06-30 21:06:47 -04:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles};
|
|
|
|
rb.Push(ResultSuccess);
|
|
|
|
rb.PushMoveObjects(port->GetServerPort());
|
2018-11-03 20:02:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SM::UnregisterService(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
2021-05-10 19:18:30 -04:00
|
|
|
std::string name(PopServiceName(rp));
|
2018-11-03 20:02:18 -04:00
|
|
|
|
2018-11-26 01:06:13 -05:00
|
|
|
LOG_DEBUG(Service_SM, "called with name={}", name);
|
2018-11-03 20:02:18 -04:00
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
2021-05-10 19:18:30 -04:00
|
|
|
rb.Push(service_manager.UnregisterService(name));
|
2018-11-03 20:02:18 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 19:18:30 -04:00
|
|
|
SM::SM(ServiceManager& service_manager_, Core::System& system_)
|
2023-02-18 16:26:48 -05:00
|
|
|
: ServiceFramework{system_, "sm:", 4},
|
2021-05-10 19:18:30 -04:00
|
|
|
service_manager{service_manager_}, kernel{system_.Kernel()} {
|
|
|
|
RegisterHandlers({
|
2021-04-08 16:13:33 -04:00
|
|
|
{0, &SM::Initialize, "Initialize"},
|
|
|
|
{1, &SM::GetService, "GetService"},
|
|
|
|
{2, &SM::RegisterService, "RegisterService"},
|
|
|
|
{3, &SM::UnregisterService, "UnregisterService"},
|
|
|
|
{4, nullptr, "DetachClient"},
|
2021-05-10 19:18:30 -04:00
|
|
|
});
|
|
|
|
RegisterHandlersTipc({
|
|
|
|
{0, &SM::Initialize, "Initialize"},
|
|
|
|
{1, &SM::GetServiceTipc, "GetService"},
|
|
|
|
{2, &SM::RegisterService, "RegisterService"},
|
|
|
|
{3, &SM::UnregisterService, "UnregisterService"},
|
|
|
|
{4, nullptr, "DetachClient"},
|
|
|
|
});
|
2017-10-14 22:18:42 -04:00
|
|
|
}
|
|
|
|
|
2022-04-06 20:37:00 -04:00
|
|
|
SM::~SM() = default;
|
2021-06-30 21:06:47 -04:00
|
|
|
|
2023-02-18 16:26:48 -05:00
|
|
|
void LoopProcess(Core::System& system) {
|
2023-02-19 08:44:54 -05:00
|
|
|
auto& service_manager = system.ServiceManager();
|
2023-02-18 16:26:48 -05:00
|
|
|
auto server_manager = std::make_unique<ServerManager>(system);
|
|
|
|
|
2023-02-19 08:44:54 -05:00
|
|
|
Kernel::KEvent* deferral_event{};
|
|
|
|
server_manager->ManageDeferral(&deferral_event);
|
|
|
|
service_manager.SetDeferralEvent(deferral_event);
|
|
|
|
|
2023-02-18 16:26:48 -05:00
|
|
|
server_manager->ManageNamedPort("sm:", std::make_shared<SM>(system.ServiceManager(), system));
|
|
|
|
ServerManager::RunServer(std::move(server_manager));
|
|
|
|
}
|
|
|
|
|
2018-04-19 21:41:44 -04:00
|
|
|
} // namespace Service::SM
|