From 0297448fbc6bf909b0bc061723c38208b9667b66 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 21 Apr 2021 21:43:25 -0700 Subject: hle: kernel: Migrate KClientPort to KAutoObject. --- src/core/CMakeLists.txt | 4 +- src/core/core.cpp | 2 +- src/core/hle/ipc_helpers.h | 2 +- src/core/hle/kernel/client_port.cpp | 47 --------------------- src/core/hle/kernel/client_port.h | 63 ---------------------------- src/core/hle/kernel/k_client_port.cpp | 58 +++++++++++++++++++++++++ src/core/hle/kernel/k_client_port.h | 72 ++++++++++++++++++++++++++++++++ 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 +- 20 files changed, 174 insertions(+), 145 deletions(-) delete mode 100644 src/core/hle/kernel/client_port.cpp delete mode 100644 src/core/hle/kernel/client_port.h create mode 100644 src/core/hle/kernel/k_client_port.cpp create mode 100644 src/core/hle/kernel/k_client_port.h (limited to 'src') diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 8e3b51ae7..cab1f5ae2 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 b5bc903cd..4bb96d77d 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 99b7d3d82..18aebf6ea 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/client_port.cpp deleted file mode 100644 index ce88da1c3..000000000 --- a/src/core/hle/kernel/client_port.cpp +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2016 Citra Emulator Project -// 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_session.h" -#include "core/hle/kernel/object.h" -#include "core/hle/kernel/server_port.h" -#include "core/hle/kernel/svc_results.h" - -namespace Kernel { - -ClientPort::ClientPort(KernelCore& kernel) : Object{kernel} {} -ClientPort::~ClientPort() = default; - -std::shared_ptr ClientPort::GetServerPort() const { - return server_port; -} - -ResultVal ClientPort::Connect() { - if (active_sessions >= max_sessions) { - return ResultOutOfSessions; - } - active_sessions++; - - auto* session = Kernel::KSession::Create(kernel); - session->Initialize(name + ":ClientPort"); - - if (server_port->HasHLEHandler()) { - server_port->GetHLEHandler()->ClientConnected(session); - } else { - server_port->AppendPendingSession(std::addressof(session->GetServerSession())); - } - - return MakeResult(std::addressof(session->GetClientSession())); -} - -void ClientPort::ConnectionClosed() { - if (active_sessions == 0) { - return; - } - - --active_sessions; -} - -} // namespace Kernel diff --git a/src/core/hle/kernel/client_port.h b/src/core/hle/kernel/client_port.h deleted file mode 100644 index 0b20fef40..000000000 --- a/src/core/hle/kernel/client_port.h +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2016 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include - -#include "common/common_types.h" -#include "core/hle/kernel/object.h" -#include "core/hle/result.h" - -namespace Kernel { - -class KClientSession; -class KernelCore; -class ServerPort; - -class ClientPort final : public Object { -public: - explicit ClientPort(KernelCore& kernel); - ~ClientPort() 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; - } - - std::shared_ptr GetServerPort() const; - - /** - * Creates a new Session pair, adds the created ServerSession to the associated ServerPort's - * list of pending sessions, and signals the ServerPort, causing any threads - * waiting on it to awake. - * @returns ClientSession The client endpoint of the created Session pair, or error code. - */ - ResultVal Connect(); - - /** - * Signifies that a previously active connection has been closed, - * decreasing the total number of active connections to this port. - */ - void ConnectionClosed(); - - void Finalize() override {} - -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) -}; - -} // namespace Kernel diff --git a/src/core/hle/kernel/k_client_port.cpp b/src/core/hle/kernel/k_client_port.cpp new file mode 100644 index 000000000..15bf0d4fc --- /dev/null +++ b/src/core/hle/kernel/k_client_port.cpp @@ -0,0 +1,58 @@ +// Copyright 2016 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#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" +#include "core/hle/kernel/svc_results.h" + +namespace Kernel { + +KClientPort::KClientPort(KernelCore& kernel) : KSynchronizationObject{kernel} {} +KClientPort::~KClientPort() = default; + +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 KClientPort::Connect() { + if (num_sessions >= max_sessions) { + return ResultOutOfSessions; + } + num_sessions++; + + auto* session = Kernel::KSession::Create(kernel); + session->Initialize(name + ":ClientPort"); + + if (server_port->HasHLEHandler()) { + server_port->GetHLEHandler()->ClientConnected(session); + } else { + server_port->AppendPendingSession(std::addressof(session->GetServerSession())); + } + + return MakeResult(std::addressof(session->GetClientSession())); +} + +void KClientPort::ConnectionClosed() { + if (num_sessions == 0) { + return; + } + + --num_sessions; +} + +void KClientPort::Destroy() {} + +bool KClientPort::IsSignaled() const { + return num_sessions < max_sessions; +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_client_port.h b/src/core/hle/kernel/k_client_port.h new file mode 100644 index 000000000..04ee2d664 --- /dev/null +++ b/src/core/hle/kernel/k_client_port.h @@ -0,0 +1,72 @@ +// Copyright 2016 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +#include "common/common_types.h" +#include "core/hle/kernel/k_synchronization_object.h" +#include "core/hle/result.h" + +namespace Kernel { + +class KClientSession; +class KernelCore; +class ServerPort; + +class KClientPort final : public KSynchronizationObject { + KERNEL_AUTOOBJECT_TRAITS(KClientPort, KSynchronizationObject); + +public: + explicit KClientPort(KernelCore& kernel); + virtual ~KClientPort() override; + + friend class ServerPort; + + void Initialize(s32 max_sessions_, std::string&& name_); + + std::shared_ptr GetServerPort() const; + + /** + * Creates a new Session pair, adds the created ServerSession to the associated ServerPort's + * list of pending sessions, and signals the ServerPort, causing any threads + * waiting on it to awake. + * @returns ClientSession The client endpoint of the created Session pair, or error code. + */ + ResultVal Connect(); + + /** + * Signifies that a previously active connection has been closed, + * decreasing the total number of active connections to this port. + */ + void ConnectionClosed(); + + // 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. + 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 8cd2c283c..46ba7081b 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 1d24e80cd..6a6fcb588 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 ada993f46..c939bb903 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 43e49603b..19b3530b4 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 8626b56fd..addaaa5cf 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); - server_port->name = name + "_Server"; - client_port->name = name + "_Client"; + KAutoObject::Create(client_port); + + client_port->Initialize(max_sessions, 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)); + server_port->name = name + "_Server"; + + 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 eebceaa2a..f7d443df8 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 fa85bd631..8d8d3dd5a 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 d831aa214..49c17fd14 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 41a502d8d..d8ad09b49 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 5909fdd85..4f1ffe55f 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 66e41277f..39575ce3d 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 8f6862fa9..aee9aefec 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 413a00ae0..e7991012b 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" -- cgit v1.2.3