2020-12-28 19:15:37 +04:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/hle/ipc_helpers.h"
|
2021-05-06 06:45:54 +04:00
|
|
|
#include "core/hle/kernel/k_client_port.h"
|
|
|
|
#include "core/hle/kernel/k_client_session.h"
|
|
|
|
#include "core/hle/kernel/k_port.h"
|
2021-05-11 04:22:43 +04:00
|
|
|
#include "core/hle/kernel/k_scoped_resource_reservation.h"
|
2021-05-06 06:45:54 +04:00
|
|
|
#include "core/hle/kernel/k_server_port.h"
|
|
|
|
#include "core/hle/kernel/k_server_session.h"
|
|
|
|
#include "core/hle/kernel/k_session.h"
|
2020-12-28 19:15:37 +04:00
|
|
|
#include "core/hle/result.h"
|
|
|
|
#include "core/hle/service/sm/controller.h"
|
|
|
|
#include "core/hle/service/sm/sm.h"
|
|
|
|
|
|
|
|
namespace Service::SM {
|
|
|
|
|
2021-05-11 04:22:43 +04:00
|
|
|
constexpr ResultCode ERR_NOT_INITIALIZED(ErrorModule::SM, 2);
|
2020-12-28 19:15:37 +04: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);
|
|
|
|
|
|
|
|
ServiceManager::ServiceManager(Kernel::KernelCore& kernel_) : kernel{kernel_} {}
|
|
|
|
ServiceManager::~ServiceManager() = default;
|
|
|
|
|
|
|
|
void ServiceManager::InvokeControlRequest(Kernel::HLERequestContext& context) {
|
|
|
|
controller_interface->InvokeRequest(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ResultCode ValidateServiceName(const std::string& name) {
|
|
|
|
if (name.empty() || name.size() > 8) {
|
|
|
|
LOG_ERROR(Service_SM, "Invalid service name! service={}", name);
|
|
|
|
return ERR_INVALID_NAME;
|
|
|
|
}
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-05-11 04:22:43 +04:00
|
|
|
Kernel::KClientPort& ServiceManager::InterfaceFactory(ServiceManager& self, Core::System& system) {
|
|
|
|
ASSERT(self.sm_interface.expired());
|
2020-12-28 19:15:37 +04:00
|
|
|
|
|
|
|
auto sm = std::make_shared<SM>(self, system);
|
2021-05-11 04:22:43 +04:00
|
|
|
self.sm_interface = sm;
|
|
|
|
self.controller_interface = std::make_unique<Controller>(system);
|
|
|
|
|
|
|
|
return sm->CreatePort(system.Kernel());
|
2020-12-28 19:15:37 +04:00
|
|
|
}
|
|
|
|
|
2021-05-06 06:45:54 +04:00
|
|
|
ResultVal<Kernel::KServerPort*> ServiceManager::RegisterService(std::string name,
|
|
|
|
u32 max_sessions) {
|
2020-12-28 19:15:37 +04:00
|
|
|
|
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
|
|
|
|
|
|
|
if (registered_services.find(name) != registered_services.end()) {
|
|
|
|
LOG_ERROR(Service_SM, "Service is already registered! service={}", name);
|
|
|
|
return ERR_ALREADY_REGISTERED;
|
|
|
|
}
|
|
|
|
|
2021-05-06 06:45:54 +04:00
|
|
|
auto* port = Kernel::KPort::Create(kernel);
|
|
|
|
port->Initialize(max_sessions, false, name);
|
2020-12-28 19:15:37 +04:00
|
|
|
|
2021-05-06 06:45:54 +04:00
|
|
|
registered_services.emplace(std::move(name), port);
|
|
|
|
|
|
|
|
return MakeResult(&port->GetServerPort());
|
2020-12-28 19:15:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode ServiceManager::UnregisterService(const std::string& name) {
|
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
|
|
|
|
|
|
|
const auto iter = registered_services.find(name);
|
|
|
|
if (iter == registered_services.end()) {
|
|
|
|
LOG_ERROR(Service_SM, "Server is not registered! service={}", name);
|
|
|
|
return ERR_SERVICE_NOT_REGISTERED;
|
|
|
|
}
|
2021-05-06 06:45:54 +04:00
|
|
|
|
|
|
|
iter->second->Close();
|
|
|
|
|
2020-12-28 19:15:37 +04:00
|
|
|
registered_services.erase(iter);
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-05-06 06:45:54 +04:00
|
|
|
ResultVal<Kernel::KPort*> ServiceManager::GetServicePort(const std::string& name) {
|
2020-12-28 19:15:37 +04:00
|
|
|
|
|
|
|
CASCADE_CODE(ValidateServiceName(name));
|
|
|
|
auto it = registered_services.find(name);
|
|
|
|
if (it == registered_services.end()) {
|
|
|
|
LOG_ERROR(Service_SM, "Server is not registered! service={}", name);
|
|
|
|
return ERR_SERVICE_NOT_REGISTERED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MakeResult(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
SM::~SM() = default;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SM::Initialize service function
|
|
|
|
* Inputs:
|
|
|
|
* 0: 0x00000000
|
|
|
|
* Outputs:
|
|
|
|
* 0: ResultCode
|
|
|
|
*/
|
|
|
|
void SM::Initialize(Kernel::HLERequestContext& ctx) {
|
|
|
|
LOG_DEBUG(Service_SM, "called");
|
|
|
|
|
2021-05-11 04:22:43 +04:00
|
|
|
is_initialized = true;
|
|
|
|
|
2020-12-28 19:15:37 +04:00
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SM::GetService(Kernel::HLERequestContext& ctx) {
|
2021-05-11 04:22:43 +04: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) {
|
2020-12-28 19:15:37 +04:00
|
|
|
auto name_buf = rp.PopRaw<std::array<char, 8>>();
|
2021-05-11 04:22:43 +04:00
|
|
|
std::string result;
|
|
|
|
for (const auto& c : name_buf) {
|
|
|
|
if (c >= ' ' && c <= '~') {
|
|
|
|
result.push_back(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-12-28 19:15:37 +04:00
|
|
|
|
2021-05-11 04:22:43 +04: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));
|
2020-12-28 19:15:37 +04:00
|
|
|
|
2021-05-19 08:44:49 +04:00
|
|
|
// Find the named port.
|
2021-05-11 04:22:43 +04:00
|
|
|
auto result = service_manager.GetServicePort(name);
|
2021-05-06 06:45:54 +04:00
|
|
|
if (result.Failed()) {
|
|
|
|
LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, result.Code().raw);
|
2021-05-11 04:22:43 +04:00
|
|
|
return result.Code();
|
2020-12-28 19:15:37 +04:00
|
|
|
}
|
2021-05-06 06:45:54 +04:00
|
|
|
auto* port = result.Unwrap();
|
2020-12-28 19:15:37 +04:00
|
|
|
|
2021-05-19 08:44:49 +04:00
|
|
|
// Reserve a new session from the process resource limit.
|
|
|
|
Kernel::KScopedResourceReservation session_reservation(
|
|
|
|
kernel.CurrentProcess()->GetResourceLimit(), Kernel::LimitableResource::Sessions);
|
|
|
|
R_UNLESS(session_reservation.Succeeded(), Kernel::ResultLimitReached);
|
2021-05-11 04:22:43 +04:00
|
|
|
|
2021-05-19 08:44:49 +04:00
|
|
|
// Create a new session.
|
2021-05-06 06:45:54 +04:00
|
|
|
auto* session = Kernel::KSession::Create(kernel);
|
|
|
|
session->Initialize(&port->GetClientPort(), std::move(name));
|
|
|
|
|
2021-05-11 04:22:43 +04:00
|
|
|
// Commit the session reservation.
|
2021-05-19 08:44:49 +04:00
|
|
|
session_reservation.Commit();
|
2021-05-11 04:22:43 +04:00
|
|
|
|
2021-05-19 08:44:49 +04:00
|
|
|
// Enqueue the session with the named port.
|
|
|
|
port->EnqueueSession(&session->GetServerSession());
|
2020-12-28 19:15:37 +04:00
|
|
|
|
2021-05-06 06:45:54 +04:00
|
|
|
LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetId());
|
2021-05-19 08:44:49 +04:00
|
|
|
|
2021-05-11 04:22:43 +04:00
|
|
|
return MakeResult(&session->GetClientSession());
|
2020-12-28 19:15:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SM::RegisterService(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
2021-05-11 04:22:43 +04:00
|
|
|
std::string name(PopServiceName(rp));
|
2020-12-28 19:15:37 +04:00
|
|
|
|
|
|
|
const auto is_light = static_cast<bool>(rp.PopRaw<u32>());
|
|
|
|
const auto max_session_count = rp.PopRaw<u32>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_SM, "called with name={}, max_session_count={}, is_light={}", name,
|
|
|
|
max_session_count, is_light);
|
|
|
|
|
2021-05-11 04:22:43 +04:00
|
|
|
auto handle = service_manager.RegisterService(name, max_session_count);
|
2020-12-28 19:15:37 +04: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-05-06 06:45:54 +04:00
|
|
|
|
|
|
|
auto server_port = handle.Unwrap();
|
|
|
|
rb.PushMoveObjects(server_port);
|
2020-12-28 19:15:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SM::UnregisterService(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
2021-05-11 04:22:43 +04:00
|
|
|
std::string name(PopServiceName(rp));
|
2020-12-28 19:15:37 +04:00
|
|
|
|
|
|
|
LOG_DEBUG(Service_SM, "called with name={}", name);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
2021-05-11 04:22:43 +04:00
|
|
|
rb.Push(service_manager.UnregisterService(name));
|
2020-12-28 19:15:37 +04:00
|
|
|
}
|
|
|
|
|
2021-05-11 04:22:43 +04:00
|
|
|
SM::SM(ServiceManager& service_manager_, Core::System& system_)
|
2020-12-28 19:15:37 +04:00
|
|
|
: ServiceFramework{system_, "sm:", 4},
|
2021-05-11 04:22:43 +04:00
|
|
|
service_manager{service_manager_}, kernel{system_.Kernel()} {
|
|
|
|
RegisterHandlers({
|
2021-04-10 10:39:53 +04:00
|
|
|
{0, &SM::Initialize, "Initialize"},
|
|
|
|
{1, &SM::GetService, "GetService"},
|
|
|
|
{2, &SM::RegisterService, "RegisterService"},
|
|
|
|
{3, &SM::UnregisterService, "UnregisterService"},
|
|
|
|
{4, nullptr, "DetachClient"},
|
2021-05-11 04:22:43 +04:00
|
|
|
});
|
|
|
|
RegisterHandlersTipc({
|
|
|
|
{0, &SM::Initialize, "Initialize"},
|
|
|
|
{1, &SM::GetServiceTipc, "GetService"},
|
|
|
|
{2, &SM::RegisterService, "RegisterService"},
|
|
|
|
{3, &SM::UnregisterService, "UnregisterService"},
|
|
|
|
{4, nullptr, "DetachClient"},
|
|
|
|
});
|
2020-12-28 19:15:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Service::SM
|