From 0297448fbc6bf909b0bc061723c38208b9667b66 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 21 Apr 2021 21:43:25 -0700 Subject: [PATCH] hle: kernel: Migrate KClientPort to KAutoObject. --- src/core/CMakeLists.txt | 4 +- src/core/core.cpp | 2 +- src/core/hle/ipc_helpers.h | 2 +- .../{client_port.cpp => k_client_port.cpp} | 31 ++++++++----- .../kernel/{client_port.h => k_client_port.h} | 45 +++++++++++-------- src/core/hle/kernel/k_server_session.cpp | 2 +- src/core/hle/kernel/k_session.h | 4 +- src/core/hle/kernel/kernel.cpp | 10 +++-- src/core/hle/kernel/kernel.h | 6 +-- src/core/hle/kernel/server_port.cpp | 15 ++++--- src/core/hle/kernel/server_port.h | 4 +- src/core/hle/kernel/svc.cpp | 2 +- src/core/hle/service/hid/hid.cpp | 2 +- src/core/hle/service/service.cpp | 4 +- src/core/hle/service/set/set_sys.cpp | 2 +- src/core/hle/service/sm/sm.cpp | 10 +++-- src/core/hle/service/sm/sm.h | 8 ++-- src/core/hle/service/time/time.cpp | 2 +- 18 files changed, 92 insertions(+), 63 deletions(-) rename src/core/hle/kernel/{client_port.cpp => k_client_port.cpp} (55%) rename src/core/hle/kernel/{client_port.h => k_client_port.h} (66%) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 8e3b51ae75..cab1f5ae27 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -144,8 +144,6 @@ add_library(core STATIC hle/kernel/board/nintendo/nx/k_system_control.cpp hle/kernel/board/nintendo/nx/k_system_control.h hle/kernel/board/nintendo/nx/secure_monitor.h - hle/kernel/client_port.cpp - hle/kernel/client_port.h hle/kernel/code_set.cpp hle/kernel/code_set.h hle/kernel/svc_results.h @@ -168,6 +166,8 @@ add_library(core STATIC hle/kernel/k_affinity_mask.h hle/kernel/k_class_token.cpp hle/kernel/k_class_token.h + hle/kernel/k_client_port.cpp + hle/kernel/k_client_port.h hle/kernel/k_client_session.cpp hle/kernel/k_client_session.h hle/kernel/k_condition_variable.cpp diff --git a/src/core/core.cpp b/src/core/core.cpp index b5bc903cd1..4bb96d77dc 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -27,7 +27,7 @@ #include "core/file_sys/vfs_concat.h" #include "core/file_sys/vfs_real.h" #include "core/hardware_interrupt_manager.h" -#include "core/hle/kernel/client_port.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index 99b7d3d82f..18aebf6eae 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -13,8 +13,8 @@ #include "common/assert.h" #include "common/common_types.h" #include "core/hle/ipc.h" -#include "core/hle/kernel/client_port.h" #include "core/hle/kernel/hle_ipc.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/object.h" #include "core/hle/result.h" diff --git a/src/core/hle/kernel/client_port.cpp b/src/core/hle/kernel/k_client_port.cpp similarity index 55% rename from src/core/hle/kernel/client_port.cpp rename to src/core/hle/kernel/k_client_port.cpp index ce88da1c32..15bf0d4fc2 100644 --- a/src/core/hle/kernel/client_port.cpp +++ b/src/core/hle/kernel/k_client_port.cpp @@ -2,8 +2,8 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include "core/hle/kernel/client_port.h" #include "core/hle/kernel/hle_ipc.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/object.h" #include "core/hle/kernel/server_port.h" @@ -11,18 +11,23 @@ namespace Kernel { -ClientPort::ClientPort(KernelCore& kernel) : Object{kernel} {} -ClientPort::~ClientPort() = default; +KClientPort::KClientPort(KernelCore& kernel) : KSynchronizationObject{kernel} {} +KClientPort::~KClientPort() = default; -std::shared_ptr ClientPort::GetServerPort() const { +void KClientPort::Initialize(s32 max_sessions_, std::string&& name_) { + max_sessions = max_sessions_; + name = std::move(name_); +} + +std::shared_ptr KClientPort::GetServerPort() const { return server_port; } -ResultVal ClientPort::Connect() { - if (active_sessions >= max_sessions) { +ResultVal KClientPort::Connect() { + if (num_sessions >= max_sessions) { return ResultOutOfSessions; } - active_sessions++; + num_sessions++; auto* session = Kernel::KSession::Create(kernel); session->Initialize(name + ":ClientPort"); @@ -36,12 +41,18 @@ ResultVal ClientPort::Connect() { return MakeResult(std::addressof(session->GetClientSession())); } -void ClientPort::ConnectionClosed() { - if (active_sessions == 0) { +void KClientPort::ConnectionClosed() { + if (num_sessions == 0) { return; } - --active_sessions; + --num_sessions; +} + +void KClientPort::Destroy() {} + +bool KClientPort::IsSignaled() const { + return num_sessions < max_sessions; } } // namespace Kernel diff --git a/src/core/hle/kernel/client_port.h b/src/core/hle/kernel/k_client_port.h similarity index 66% rename from src/core/hle/kernel/client_port.h rename to src/core/hle/kernel/k_client_port.h index 0b20fef40f..04ee2d6647 100644 --- a/src/core/hle/kernel/client_port.h +++ b/src/core/hle/kernel/k_client_port.h @@ -8,7 +8,7 @@ #include #include "common/common_types.h" -#include "core/hle/kernel/object.h" +#include "core/hle/kernel/k_synchronization_object.h" #include "core/hle/result.h" namespace Kernel { @@ -17,23 +17,16 @@ class KClientSession; class KernelCore; class ServerPort; -class ClientPort final : public Object { +class KClientPort final : public KSynchronizationObject { + KERNEL_AUTOOBJECT_TRAITS(KClientPort, KSynchronizationObject); + public: - explicit ClientPort(KernelCore& kernel); - ~ClientPort() override; + explicit KClientPort(KernelCore& kernel); + virtual ~KClientPort() override; friend class ServerPort; - std::string GetTypeName() const override { - return "ClientPort"; - } - std::string GetName() const override { - return name; - } - static constexpr HandleType HANDLE_TYPE = HandleType::ClientPort; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } + void Initialize(s32 max_sessions_, std::string&& name_); std::shared_ptr GetServerPort() const; @@ -51,13 +44,29 @@ public: */ void ConnectionClosed(); - void Finalize() override {} + // Overridden virtual functions. + virtual void Destroy() override; + virtual bool IsSignaled() const override; + + // DEPRECATED + + std::string GetTypeName() const override { + return "ClientPort"; + } + std::string GetName() const override { + return name; + } + + static constexpr HandleType HANDLE_TYPE = HandleType::ClientPort; + HandleType GetHandleType() const override { + return HANDLE_TYPE; + } private: std::shared_ptr server_port; ///< ServerPort associated with this client port. - u32 max_sessions = 0; ///< Maximum number of simultaneous sessions the port can have - u32 active_sessions = 0; ///< Number of currently open sessions to this port - std::string name; ///< Name of client port (optional) + s32 max_sessions = 0; ///< Maximum number of simultaneous sessions the port can have + std::atomic num_sessions = 0; ///< Number of currently open sessions to this port + std::string name; ///< Name of client port (optional) }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_server_session.cpp b/src/core/hle/kernel/k_server_session.cpp index 8cd2c283cd..46ba7081bd 100644 --- a/src/core/hle/kernel/k_server_session.cpp +++ b/src/core/hle/kernel/k_server_session.cpp @@ -10,9 +10,9 @@ #include "common/logging/log.h" #include "core/core_timing.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/client_port.h" #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/hle_ipc.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_session.h" diff --git a/src/core/hle/kernel/k_session.h b/src/core/hle/kernel/k_session.h index 1d24e80cdd..6a6fcb5881 100644 --- a/src/core/hle/kernel/k_session.h +++ b/src/core/hle/kernel/k_session.h @@ -70,7 +70,7 @@ public: return server; } - const ClientPort* GetParent() const { + const KClientPort* GetParent() const { return port; } @@ -99,7 +99,7 @@ private: KClientSession client; std::atomic::type> atomic_state{ static_cast::type>(State::Invalid)}; - ClientPort* port{}; + KClientPort* port{}; std::string name; Process* process{}; bool initialized{}; diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index ada993f46d..c939bb9030 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -26,9 +26,9 @@ #include "core/cpu_manager.h" #include "core/device_memory.h" #include "core/hardware_properties.h" -#include "core/hle/kernel/client_port.h" #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/init/init_slab_setup.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" #include "core/hle/kernel/k_resource_limit.h" @@ -122,6 +122,9 @@ struct KernelCore::Impl { preemption_event = nullptr; + for (auto& iter : named_ports) { + iter.second->Close(); + } named_ports.clear(); exclusive_monitor.reset(); @@ -843,8 +846,9 @@ void KernelCore::PrepareReschedule(std::size_t id) { // TODO: Reimplement, this } -void KernelCore::AddNamedPort(std::string name, std::shared_ptr port) { - impl->named_ports.emplace(std::move(name), std::move(port)); +void KernelCore::AddNamedPort(std::string name, KClientPort* port) { + port->Open(); + impl->named_ports.emplace(std::move(name), port); } KernelCore::NamedPortTable::iterator KernelCore::FindNamedPort(const std::string& name) { diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 43e49603ba..19b3530b48 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -29,7 +29,7 @@ struct EventType; namespace Kernel { -class ClientPort; +class KClientPort; class GlobalSchedulerContext; class HandleTable; class KAutoObjectWithListContainer; @@ -60,7 +60,7 @@ constexpr EmuThreadHandle EmuThreadHandleReserved{1ULL << 63}; /// Represents a single instance of the kernel. class KernelCore { private: - using NamedPortTable = std::unordered_map>; + using NamedPortTable = std::unordered_map; public: /// Constructs an instance of the kernel using the given System @@ -168,7 +168,7 @@ public: void InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size); /// Adds a port to the named port table - void AddNamedPort(std::string name, std::shared_ptr port); + void AddNamedPort(std::string name, KClientPort* port); /// Finds a port within the named port table with the given name. NamedPortTable::iterator FindNamedPort(const std::string& name); diff --git a/src/core/hle/kernel/server_port.cpp b/src/core/hle/kernel/server_port.cpp index 8626b56fd3..addaaa5cfb 100644 --- a/src/core/hle/kernel/server_port.cpp +++ b/src/core/hle/kernel/server_port.cpp @@ -4,7 +4,7 @@ #include #include "common/assert.h" -#include "core/hle/kernel/client_port.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/object.h" @@ -40,15 +40,16 @@ bool ServerPort::IsSignaled() const { ServerPort::PortPair ServerPort::CreatePortPair(KernelCore& kernel, u32 max_sessions, std::string name) { std::shared_ptr server_port = std::make_shared(kernel); - std::shared_ptr client_port = std::make_shared(kernel); + KClientPort* client_port = new KClientPort(kernel); + + KAutoObject::Create(client_port); + + client_port->Initialize(max_sessions, name + "_Client"); + client_port->server_port = server_port; server_port->name = name + "_Server"; - client_port->name = name + "_Client"; - client_port->server_port = server_port; - client_port->max_sessions = max_sessions; - client_port->active_sessions = 0; - return std::make_pair(std::move(server_port), std::move(client_port)); + return std::make_pair(std::move(server_port), client_port); } } // namespace Kernel diff --git a/src/core/hle/kernel/server_port.h b/src/core/hle/kernel/server_port.h index eebceaa2a1..f7d443df86 100644 --- a/src/core/hle/kernel/server_port.h +++ b/src/core/hle/kernel/server_port.h @@ -15,7 +15,7 @@ namespace Kernel { -class ClientPort; +class KClientPort; class KernelCore; class KServerSession; class SessionRequestHandler; @@ -26,7 +26,7 @@ public: ~ServerPort() override; using HLEHandler = std::shared_ptr; - using PortPair = std::pair, std::shared_ptr>; + using PortPair = std::pair, KClientPort*>; /** * Creates a pair of ServerPort and an associated ClientPort. diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index fa85bd6318..8d8d3dd5a8 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -21,9 +21,9 @@ #include "core/core_timing.h" #include "core/core_timing_util.h" #include "core/cpu_manager.h" -#include "core/hle/kernel/client_port.h" #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/k_address_arbiter.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_client_session.h" #include "core/hle/kernel/k_condition_variable.h" #include "core/hle/kernel/k_event.h" diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index d831aa2143..49c17fd149 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -13,7 +13,7 @@ #include "core/frontend/input.h" #include "core/hardware_properties.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/client_port.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_shared_memory.h" #include "core/hle/kernel/k_transfer_memory.h" diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index 41a502d8dd..d8ad09b495 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -11,7 +11,7 @@ #include "core/core.h" #include "core/hle/ipc.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/client_port.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" @@ -119,7 +119,7 @@ void ServiceFrameworkBase::InstallAsNamedPort(Kernel::KernelCore& kernel) { auto [server_port, client_port] = Kernel::ServerPort::CreatePortPair(kernel, max_sessions, service_name); server_port->SetHleHandler(shared_from_this()); - kernel.AddNamedPort(service_name, std::move(client_port)); + kernel.AddNamedPort(service_name, client_port); port_installed = true; } diff --git a/src/core/hle/service/set/set_sys.cpp b/src/core/hle/service/set/set_sys.cpp index 5909fdd855..4f1ffe55f6 100644 --- a/src/core/hle/service/set/set_sys.cpp +++ b/src/core/hle/service/set/set_sys.cpp @@ -7,7 +7,7 @@ #include "core/file_sys/errors.h" #include "core/file_sys/system_archive/system_version.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/client_port.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/hle/service/set/set_sys.h" diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 66e41277f5..39575ce3d4 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -6,7 +6,7 @@ #include "common/assert.h" #include "core/core.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/client_port.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_client_session.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_session.h" @@ -62,6 +62,8 @@ ResultVal> ServiceManager::RegisterService(s auto [server_port, client_port] = Kernel::ServerPort::CreatePortPair(kernel, max_sessions, name); + client_port->Open(); + registered_services.emplace(std::move(name), std::move(client_port)); return MakeResult(std::move(server_port)); } @@ -74,12 +76,14 @@ ResultCode ServiceManager::UnregisterService(const std::string& name) { LOG_ERROR(Service_SM, "Server is not registered! service={}", name); return ERR_SERVICE_NOT_REGISTERED; } + + iter->second->Close(); + registered_services.erase(iter); return RESULT_SUCCESS; } -ResultVal> ServiceManager::GetServicePort( - const std::string& name) { +ResultVal ServiceManager::GetServicePort(const std::string& name) { CASCADE_CODE(ValidateServiceName(name)); auto it = registered_services.find(name); diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h index 8f6862fa9a..aee9aefec4 100644 --- a/src/core/hle/service/sm/sm.h +++ b/src/core/hle/service/sm/sm.h @@ -10,7 +10,7 @@ #include #include "common/concepts.h" -#include "core/hle/kernel/client_port.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/object.h" #include "core/hle/kernel/server_port.h" #include "core/hle/result.h" @@ -21,7 +21,7 @@ class System; } namespace Kernel { -class ClientPort; +class KClientPort; class KClientSession; class KernelCore; class ServerPort; @@ -58,7 +58,7 @@ public: ResultVal> RegisterService(std::string name, u32 max_sessions); ResultCode UnregisterService(const std::string& name); - ResultVal> GetServicePort(const std::string& name); + ResultVal GetServicePort(const std::string& name); template T> std::shared_ptr GetService(const std::string& service_name) const { @@ -81,7 +81,7 @@ private: std::unique_ptr controller_interface; /// Map of registered services, retrieved using GetServicePort. - std::unordered_map> registered_services; + std::unordered_map registered_services; /// Kernel context Kernel::KernelCore& kernel; diff --git a/src/core/hle/service/time/time.cpp b/src/core/hle/service/time/time.cpp index 413a00ae02..e7991012b1 100644 --- a/src/core/hle/service/time/time.cpp +++ b/src/core/hle/service/time/time.cpp @@ -8,7 +8,7 @@ #include "core/core_timing_util.h" #include "core/hardware_properties.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/client_port.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/kernel.h" #include "core/hle/service/time/interface.h"