2018-01-13 22:22:39 +01:00
|
|
|
// Copyright 2018 yuzu emulator team
|
2017-06-06 08:31:59 +02:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <tuple>
|
2017-06-07 06:25:28 +02:00
|
|
|
#include "common/assert.h"
|
2018-08-28 18:30:33 +02:00
|
|
|
#include "core/core.h"
|
2017-10-15 04:18:42 +02:00
|
|
|
#include "core/hle/ipc_helpers.h"
|
2021-04-22 06:43:25 +02:00
|
|
|
#include "core/hle/kernel/k_client_port.h"
|
2021-04-14 02:48:37 +02:00
|
|
|
#include "core/hle/kernel/k_client_session.h"
|
2021-04-24 02:00:15 +02:00
|
|
|
#include "core/hle/kernel/k_port.h"
|
2021-04-22 06:53:56 +02:00
|
|
|
#include "core/hle/kernel/k_server_port.h"
|
2021-04-14 02:48:37 +02:00
|
|
|
#include "core/hle/kernel/k_server_session.h"
|
|
|
|
#include "core/hle/kernel/k_session.h"
|
2017-06-06 08:31:59 +02:00
|
|
|
#include "core/hle/result.h"
|
2017-10-15 04:18:42 +02:00
|
|
|
#include "core/hle/service/sm/controller.h"
|
2017-06-06 08:31:59 +02:00
|
|
|
#include "core/hle/service/sm/sm.h"
|
|
|
|
|
2018-04-20 03:41:44 +02:00
|
|
|
namespace Service::SM {
|
2017-06-06 08:31:59 +02:00
|
|
|
|
2018-09-14 07:43:59 +02: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 16:43:54 +02:00
|
|
|
ServiceManager::ServiceManager(Kernel::KernelCore& kernel_) : kernel{kernel_} {}
|
2018-04-21 01:29:04 +02:00
|
|
|
ServiceManager::~ServiceManager() = default;
|
|
|
|
|
2017-10-15 04:18:42 +02:00
|
|
|
void ServiceManager::InvokeControlRequest(Kernel::HLERequestContext& context) {
|
|
|
|
controller_interface->InvokeRequest(context);
|
|
|
|
}
|
|
|
|
|
2017-06-06 08:31:59 +02:00
|
|
|
static ResultCode ValidateServiceName(const std::string& name) {
|
2020-09-17 16:54:09 +02:00
|
|
|
if (name.empty() || name.size() > 8) {
|
2020-04-29 03:15:21 +02:00
|
|
|
LOG_ERROR(Service_SM, "Invalid service name! service={}", name);
|
2018-09-14 07:43:59 +02:00
|
|
|
return ERR_INVALID_NAME;
|
2017-06-06 08:31:59 +02:00
|
|
|
}
|
2020-09-17 16:54:09 +02:00
|
|
|
if (name.rfind('\0') != std::string::npos) {
|
2020-04-29 03:15:21 +02:00
|
|
|
LOG_ERROR(Service_SM, "A non null terminated service was passed");
|
2018-09-14 07:43:59 +02:00
|
|
|
return ERR_INVALID_NAME;
|
2017-06-06 08:31:59 +02:00
|
|
|
}
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-11-26 21:19:08 +01:00
|
|
|
void ServiceManager::InstallInterfaces(std::shared_ptr<ServiceManager> self, Core::System& system) {
|
2017-10-15 04:18:42 +02:00
|
|
|
ASSERT(self->sm_interface.expired());
|
2017-06-07 06:25:28 +02:00
|
|
|
|
2020-11-26 21:19:08 +01:00
|
|
|
auto sm = std::make_shared<SM>(self, system);
|
|
|
|
sm->InstallAsNamedPort(system.Kernel());
|
2017-10-15 04:18:42 +02:00
|
|
|
self->sm_interface = sm;
|
2020-11-26 21:19:08 +01:00
|
|
|
self->controller_interface = std::make_unique<Controller>(system);
|
2017-06-07 06:25:28 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 06:53:56 +02:00
|
|
|
ResultVal<Kernel::KServerPort*> ServiceManager::RegisterService(std::string name,
|
|
|
|
u32 max_sessions) {
|
2017-06-06 08:31:59 +02:00
|
|
|
|
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
2017-09-24 07:12:58 +02:00
|
|
|
|
2020-04-29 03:15:21 +02:00
|
|
|
if (registered_services.find(name) != registered_services.end()) {
|
|
|
|
LOG_ERROR(Service_SM, "Service is already registered! service={}", name);
|
2017-09-24 07:12:58 +02:00
|
|
|
return ERR_ALREADY_REGISTERED;
|
2020-04-29 03:15:21 +02:00
|
|
|
}
|
2017-09-24 07:12:58 +02:00
|
|
|
|
2021-04-24 02:00:15 +02:00
|
|
|
auto* port = Kernel::KPort::Create(kernel);
|
|
|
|
port->Initialize(max_sessions, false, name);
|
2017-06-06 08:31:59 +02:00
|
|
|
|
2021-04-24 02:00:15 +02:00
|
|
|
registered_services.emplace(std::move(name), port);
|
2021-04-22 06:43:25 +02:00
|
|
|
|
2021-04-24 02:00:15 +02:00
|
|
|
return MakeResult(&port->GetServerPort());
|
2017-06-06 08:31:59 +02:00
|
|
|
}
|
|
|
|
|
2018-11-24 06:04:07 +01:00
|
|
|
ResultCode ServiceManager::UnregisterService(const std::string& name) {
|
2018-11-04 01:02:18 +01:00
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
|
|
|
|
|
|
|
const auto iter = registered_services.find(name);
|
2020-04-29 03:15:21 +02:00
|
|
|
if (iter == registered_services.end()) {
|
|
|
|
LOG_ERROR(Service_SM, "Server is not registered! service={}", name);
|
2018-11-04 01:02:18 +01:00
|
|
|
return ERR_SERVICE_NOT_REGISTERED;
|
2020-04-29 03:15:21 +02:00
|
|
|
}
|
2021-04-22 06:43:25 +02:00
|
|
|
|
|
|
|
iter->second->Close();
|
|
|
|
|
2018-11-04 01:02:18 +01:00
|
|
|
registered_services.erase(iter);
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-04-24 02:00:15 +02:00
|
|
|
ResultVal<Kernel::KPort*> ServiceManager::GetServicePort(const std::string& name) {
|
2017-06-06 08:31:59 +02:00
|
|
|
|
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
|
|
|
auto it = registered_services.find(name);
|
|
|
|
if (it == registered_services.end()) {
|
2020-04-29 03:15:21 +02:00
|
|
|
LOG_ERROR(Service_SM, "Server is not registered! service={}", name);
|
2017-06-06 08:31:59 +02:00
|
|
|
return ERR_SERVICE_NOT_REGISTERED;
|
|
|
|
}
|
|
|
|
|
2018-12-06 07:33:19 +01:00
|
|
|
return MakeResult(it->second);
|
2017-06-06 08:31:59 +02:00
|
|
|
}
|
|
|
|
|
2018-04-21 01:29:04 +02:00
|
|
|
SM::~SM() = default;
|
2017-06-06 08:31:59 +02:00
|
|
|
|
2017-10-15 04:18:42 +02:00
|
|
|
/**
|
|
|
|
* SM::Initialize service function
|
|
|
|
* Inputs:
|
|
|
|
* 0: 0x00000000
|
|
|
|
* Outputs:
|
2017-10-15 07:24:22 +02:00
|
|
|
* 0: ResultCode
|
2017-10-15 04:18:42 +02:00
|
|
|
*/
|
|
|
|
void SM::Initialize(Kernel::HLERequestContext& ctx) {
|
2018-11-26 07:06:13 +01:00
|
|
|
LOG_DEBUG(Service_SM, "called");
|
|
|
|
|
2021-05-11 00:59:19 +02:00
|
|
|
is_initialized = true;
|
|
|
|
|
2018-01-24 01:52:18 +01:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
2017-10-15 04:18:42 +02:00
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SM::GetService(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
2018-01-07 15:57:41 +01:00
|
|
|
auto name_buf = rp.PopRaw<std::array<char, 8>>();
|
|
|
|
auto end = std::find(name_buf.begin(), name_buf.end(), '\0');
|
|
|
|
|
|
|
|
std::string name(name_buf.begin(), end);
|
2017-10-15 04:18:42 +02:00
|
|
|
|
2021-04-24 02:00:15 +02:00
|
|
|
auto result = service_manager->GetServicePort(name);
|
|
|
|
if (result.Failed()) {
|
2018-09-19 07:09:59 +02:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
2021-04-24 02:00:15 +02:00
|
|
|
rb.Push(result.Code());
|
|
|
|
LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, result.Code().raw);
|
2018-02-05 11:03:22 +01:00
|
|
|
if (name.length() == 0)
|
|
|
|
return; // LibNX Fix
|
2018-01-20 01:35:25 +01:00
|
|
|
UNIMPLEMENTED();
|
2017-10-15 04:18:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-24 02:00:15 +02:00
|
|
|
auto* port = result.Unwrap();
|
|
|
|
|
2021-05-11 01:12:01 +02:00
|
|
|
Kernel::KScopedResourceReservation session_reservation(
|
|
|
|
kernel.CurrentProcess()->GetResourceLimit(), Kernel::LimitableResource::Sessions);
|
|
|
|
R_UNLESS(session_reservation.Succeeded(), Kernel::ResultLimitReached);
|
|
|
|
|
2021-04-14 02:48:37 +02:00
|
|
|
auto* session = Kernel::KSession::Create(kernel);
|
2021-04-24 02:00:15 +02:00
|
|
|
session->Initialize(&port->GetClientPort(), std::move(name));
|
2019-11-26 00:28:48 +01:00
|
|
|
|
2021-05-11 01:12:01 +02:00
|
|
|
// Commit the session reservation.
|
|
|
|
session_reservation.Commit();
|
|
|
|
|
2021-04-24 02:00:15 +02:00
|
|
|
if (port->GetServerPort().GetHLEHandler()) {
|
|
|
|
port->GetServerPort().GetHLEHandler()->ClientConnected(&session->GetServerSession());
|
2019-11-26 00:28:48 +01:00
|
|
|
} else {
|
2021-04-24 02:00:15 +02:00
|
|
|
port->EnqueueSession(&session->GetServerSession());
|
2017-10-15 04:18:42 +02:00
|
|
|
}
|
2019-11-26 00:28:48 +01:00
|
|
|
|
2021-04-24 06:50:04 +02:00
|
|
|
LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetId());
|
2019-11-26 00:28:48 +01:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
2021-04-14 02:48:37 +02:00
|
|
|
rb.PushMoveObjects(session->GetClientSession());
|
2017-10-15 04:18:42 +02:00
|
|
|
}
|
|
|
|
|
2018-11-04 01:02:18 +01:00
|
|
|
void SM::RegisterService(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
|
|
|
|
const auto name_buf = rp.PopRaw<std::array<char, 8>>();
|
|
|
|
const auto end = std::find(name_buf.begin(), name_buf.end(), '\0');
|
|
|
|
|
|
|
|
const std::string name(name_buf.begin(), end);
|
|
|
|
|
2018-12-18 05:04:35 +01:00
|
|
|
const auto is_light = static_cast<bool>(rp.PopRaw<u32>());
|
|
|
|
const auto max_session_count = rp.PopRaw<u32>();
|
2018-11-04 01:02:18 +01:00
|
|
|
|
2018-12-18 05:04:35 +01:00
|
|
|
LOG_DEBUG(Service_SM, "called with name={}, max_session_count={}, is_light={}", name,
|
|
|
|
max_session_count, is_light);
|
2018-11-04 01:02:18 +01:00
|
|
|
|
2018-12-18 05:04:35 +01:00
|
|
|
auto handle = service_manager->RegisterService(name, max_session_count);
|
2018-11-04 01:02:18 +01:00
|
|
|
if (handle.Failed()) {
|
|
|
|
LOG_ERROR(Service_SM, "failed to register service with error_code={:08X}",
|
|
|
|
handle.Code().raw);
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(handle.Code());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles};
|
|
|
|
rb.Push(handle.Code());
|
2021-04-04 06:21:22 +02:00
|
|
|
|
|
|
|
auto server_port = handle.Unwrap();
|
2021-04-22 06:53:56 +02:00
|
|
|
rb.PushMoveObjects(server_port);
|
2018-11-04 01:02:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SM::UnregisterService(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
|
|
|
|
const auto name_buf = rp.PopRaw<std::array<char, 8>>();
|
|
|
|
const auto end = std::find(name_buf.begin(), name_buf.end(), '\0');
|
|
|
|
|
|
|
|
const std::string name(name_buf.begin(), end);
|
2018-11-26 07:06:13 +01:00
|
|
|
LOG_DEBUG(Service_SM, "called with name={}", name);
|
2018-11-04 01:02:18 +01:00
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(service_manager->UnregisterService(name));
|
|
|
|
}
|
|
|
|
|
2020-11-26 21:19:08 +01:00
|
|
|
SM::SM(std::shared_ptr<ServiceManager> service_manager_, Core::System& system_)
|
|
|
|
: ServiceFramework{system_, "sm:", 4},
|
|
|
|
service_manager{std::move(service_manager_)}, kernel{system_.Kernel()} {
|
2017-10-15 04:18:42 +02:00
|
|
|
static const FunctionInfo functions[] = {
|
2021-04-08 22:13:33 +02:00
|
|
|
{0, &SM::Initialize, "Initialize"},
|
|
|
|
{1, &SM::GetService, "GetService"},
|
|
|
|
{2, &SM::RegisterService, "RegisterService"},
|
|
|
|
{3, &SM::UnregisterService, "UnregisterService"},
|
|
|
|
{4, nullptr, "DetachClient"},
|
2017-10-15 04:18:42 +02:00
|
|
|
};
|
|
|
|
RegisterHandlers(functions);
|
|
|
|
}
|
|
|
|
|
2018-04-20 03:41:44 +02:00
|
|
|
} // namespace Service::SM
|