From b99fc701912e7ef87a6c1a7aca7ec285279da43a Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 31 Mar 2021 14:19:26 -0700 Subject: [PATCH 01/82] common: common_funcs: Add helper macros for non-copyable and non-moveable. - Useful for scenarios where we do not want to inherit from NonCopyable. --- src/common/common_funcs.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index 4ace2cd33..73c8c9354 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h @@ -108,6 +108,14 @@ __declspec(dllimport) void __stdcall DebugBreak(void); } \ } +#define NON_COPYABLE(cls) \ + cls(const cls&) = delete; \ + cls& operator=(const cls&) = delete + +#define NON_MOVEABLE(cls) \ + cls(cls&&) = delete; \ + cls& operator=(cls&&) = delete + #define R_SUCCEEDED(res) (res.IsSuccess()) /// Evaluates an expression that returns a result, and returns the result if it would fail. From d9205f82b35c61de9eaa381578f4aceaf94b75b1 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 31 Mar 2021 14:35:46 -0700 Subject: [PATCH 02/82] common: intrusive_red_black_tree: Disable static_assert that will not evaluate as constant on MSVC. --- src/common/intrusive_red_black_tree.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/common/intrusive_red_black_tree.h b/src/common/intrusive_red_black_tree.h index c0bbcd457..15f972054 100644 --- a/src/common/intrusive_red_black_tree.h +++ b/src/common/intrusive_red_black_tree.h @@ -509,7 +509,11 @@ private: private: static constexpr TypedStorage DerivedStorage = {}; + +#ifndef _MSC_VER + // TODO(bunnei): Enable on MSVC once this can be const evaluated by the compiler static_assert(GetParent(GetNode(GetPointer(DerivedStorage))) == GetPointer(DerivedStorage)); +#endif }; template > From b8751630e20914482b6ba0b6347c5f8f668e9748 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 31 Mar 2021 14:43:39 -0700 Subject: [PATCH 03/82] hle: kernel: Add initial impl. of KAutoObject. --- src/core/CMakeLists.txt | 2 + src/core/hle/kernel/k_auto_object.cpp | 14 ++ src/core/hle/kernel/k_auto_object.h | 290 ++++++++++++++++++++++++++ 3 files changed, 306 insertions(+) create mode 100644 src/core/hle/kernel/k_auto_object.cpp create mode 100644 src/core/hle/kernel/k_auto_object.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index c28abc24c..aa83b8733 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -161,6 +161,8 @@ add_library(core STATIC hle/kernel/k_address_arbiter.h hle/kernel/k_address_space_info.cpp hle/kernel/k_address_space_info.h + hle/kernel/k_auto_object.cpp + hle/kernel/k_auto_object.h hle/kernel/k_affinity_mask.h hle/kernel/k_condition_variable.cpp hle/kernel/k_condition_variable.h diff --git a/src/core/hle/kernel/k_auto_object.cpp b/src/core/hle/kernel/k_auto_object.cpp new file mode 100644 index 000000000..dbe237f09 --- /dev/null +++ b/src/core/hle/kernel/k_auto_object.cpp @@ -0,0 +1,14 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/hle/kernel/k_auto_object.h" + +namespace Kernel { + +KAutoObject* KAutoObject::Create(KAutoObject* obj) { + obj->m_ref_count = 1; + return obj; +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_auto_object.h b/src/core/hle/kernel/k_auto_object.h new file mode 100644 index 000000000..567dad204 --- /dev/null +++ b/src/core/hle/kernel/k_auto_object.h @@ -0,0 +1,290 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include + +#include "common/assert.h" +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/intrusive_red_black_tree.h" +#include "core/hle/kernel/k_class_token.h" + +namespace Kernel { + +class Process; + +#define KERNEL_AUTOOBJECT_TRAITS(CLASS, BASE_CLASS) \ + NON_COPYABLE(CLASS); \ + NON_MOVEABLE(CLASS); \ + \ +private: \ + friend class ::Kernel::KClassTokenGenerator; \ + static constexpr inline auto ObjectType = ::Kernel::KClassTokenGenerator::ObjectType::CLASS; \ + static constexpr inline const char* const TypeName = #CLASS; \ + static constexpr inline ClassTokenType ClassToken() { \ + return ::Kernel::ClassToken; \ + } \ + \ +public: \ + using BaseClass = BASE_CLASS; \ + static constexpr TypeObj GetStaticTypeObj() { \ + constexpr ClassTokenType Token = ClassToken(); \ + return TypeObj(TypeName, Token); \ + } \ + static constexpr const char* GetStaticTypeName() { \ + return TypeName; \ + } \ + virtual TypeObj GetTypeObj() const { \ + return GetStaticTypeObj(); \ + } \ + virtual const char* GetTypeName() { \ + return GetStaticTypeName(); \ + } \ + \ +private: + +class KAutoObject { +protected: + class TypeObj { + private: + const char* m_name; + ClassTokenType m_class_token; + + public: + constexpr explicit TypeObj(const char* n, ClassTokenType tok) + : m_name(n), m_class_token(tok) { // ... + } + + constexpr const char* GetName() const { + return m_name; + } + constexpr ClassTokenType GetClassToken() const { + return m_class_token; + } + + constexpr bool operator==(const TypeObj& rhs) { + return this->GetClassToken() == rhs.GetClassToken(); + } + + constexpr bool operator!=(const TypeObj& rhs) { + return this->GetClassToken() != rhs.GetClassToken(); + } + + constexpr bool IsDerivedFrom(const TypeObj& rhs) { + return (this->GetClassToken() | rhs.GetClassToken()) == this->GetClassToken(); + } + }; + +private: + KERNEL_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject); + +private: + std::atomic m_ref_count; + +public: + static KAutoObject* Create(KAutoObject* ptr); + +public: + constexpr explicit KAutoObject() : m_ref_count(0) {} + virtual ~KAutoObject() {} + + // Destroy is responsible for destroying the auto object's resources when ref_count hits zero. + virtual void Destroy() { + UNIMPLEMENTED(); + } + + // Finalize is responsible for cleaning up resource, but does not destroy the object. + virtual void Finalize() { + UNIMPLEMENTED(); + } + + virtual Process* GetOwner() const { + return nullptr; + } + + u32 GetReferenceCount() const { + return m_ref_count.load(); + } + + bool IsDerivedFrom(const TypeObj& rhs) const { + return this->GetTypeObj().IsDerivedFrom(rhs); + } + + bool IsDerivedFrom(const KAutoObject& rhs) const { + return this->IsDerivedFrom(rhs.GetTypeObj()); + } + + template + Derived DynamicCast() { + static_assert(std::is_pointer::value); + using DerivedType = typename std::remove_pointer::type; + + if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) { + return static_cast(this); + } else { + return nullptr; + } + } + + template + const Derived DynamicCast() const { + static_assert(std::is_pointer::value); + using DerivedType = typename std::remove_pointer::type; + + if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) { + return static_cast(this); + } else { + return nullptr; + } + } + + bool Open() { + // Atomically increment the reference count, only if it's positive. + u32 cur_ref_count = m_ref_count.load(std::memory_order_acquire); + do { + if (cur_ref_count == 0) { + return false; + } + ASSERT(cur_ref_count < cur_ref_count + 1); + } while (!m_ref_count.compare_exchange_weak(cur_ref_count, cur_ref_count + 1, + std::memory_order_relaxed)); + + return true; + } + + void Close() { + // Atomically decrement the reference count, not allowing it to become negative. + u32 cur_ref_count = m_ref_count.load(std::memory_order_acquire); + do { + ASSERT(cur_ref_count > 0); + } while (!m_ref_count.compare_exchange_weak(cur_ref_count, cur_ref_count - 1, + std::memory_order_relaxed)); + + // If ref count hits zero, destroy the object. + if (cur_ref_count - 1 == 0) { + this->Destroy(); + } + } +}; + +class KAutoObjectWithListContainer; + +class KAutoObjectWithList : public KAutoObject { +private: + friend class KAutoObjectWithListContainer; + +private: + Common::IntrusiveRedBlackTreeNode list_node; + +public: + static int Compare(const KAutoObjectWithList& lhs, const KAutoObjectWithList& rhs) { + const u64 lid = lhs.GetId(); + const u64 rid = rhs.GetId(); + + if (lid < rid) { + return -1; + } else if (lid > rid) { + return 1; + } else { + return 0; + } + } + +public: + virtual u64 GetId() const { + return reinterpret_cast(this); + } +}; + +template +class KScopedAutoObject { + NON_COPYABLE(KScopedAutoObject); + +private: + template + friend class KScopedAutoObject; + +private: + T* m_obj; + +private: + constexpr void Swap(KScopedAutoObject& rhs) { + std::swap(m_obj, rhs.m_obj); + } + +public: + constexpr KScopedAutoObject() : m_obj(nullptr) { // ... + } + constexpr KScopedAutoObject(T* o) : m_obj(o) { + if (m_obj != nullptr) { + m_obj->Open(); + } + } + + ~KScopedAutoObject() { + if (m_obj != nullptr) { + m_obj->Close(); + } + m_obj = nullptr; + } + + template + requires(std::derived_from || + std::derived_from) constexpr KScopedAutoObject(KScopedAutoObject&& rhs) { + if constexpr (std::derived_from) { + // Upcast. + m_obj = rhs.m_obj; + rhs.m_obj = nullptr; + } else { + // Downcast. + T* derived = nullptr; + if (rhs.m_obj != nullptr) { + derived = rhs.m_obj->template DynamicCast(); + if (derived == nullptr) { + rhs.m_obj->Close(); + } + } + + m_obj = derived; + rhs.m_obj = nullptr; + } + } + + constexpr KScopedAutoObject& operator=(KScopedAutoObject&& rhs) { + rhs.Swap(*this); + return *this; + } + + constexpr T* operator->() { + return m_obj; + } + constexpr T& operator*() { + return *m_obj; + } + + constexpr void Reset(T* o) { + KScopedAutoObject(o).Swap(*this); + } + + constexpr T* GetPointerUnsafe() { + return m_obj; + } + + constexpr T* ReleasePointerUnsafe() { + T* ret = m_obj; + m_obj = nullptr; + return ret; + } + + constexpr bool IsNull() const { + return m_obj == nullptr; + } + constexpr bool IsNotNull() const { + return m_obj != nullptr; + } +}; + +} // namespace Kernel From 34ce1dd7c724a4014ba4f8e7719f12352d0fddcb Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 31 Mar 2021 21:06:02 -0700 Subject: [PATCH 04/82] hle: kernel: Add initial impl. of KAutoObjectWithListContainer. --- src/core/CMakeLists.txt | 2 + .../hle/kernel/k_auto_object_container.cpp | 35 +++++++++ src/core/hle/kernel/k_auto_object_container.h | 72 +++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 src/core/hle/kernel/k_auto_object_container.cpp create mode 100644 src/core/hle/kernel/k_auto_object_container.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index aa83b8733..3cc5b7fbc 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -163,6 +163,8 @@ add_library(core STATIC hle/kernel/k_address_space_info.h hle/kernel/k_auto_object.cpp hle/kernel/k_auto_object.h + hle/kernel/k_auto_object_container.cpp + hle/kernel/k_auto_object_container.h hle/kernel/k_affinity_mask.h hle/kernel/k_condition_variable.cpp hle/kernel/k_condition_variable.h diff --git a/src/core/hle/kernel/k_auto_object_container.cpp b/src/core/hle/kernel/k_auto_object_container.cpp new file mode 100644 index 000000000..9ba8a54c7 --- /dev/null +++ b/src/core/hle/kernel/k_auto_object_container.cpp @@ -0,0 +1,35 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/hle/kernel/k_auto_object_container.h" + +namespace Kernel { + +void KAutoObjectWithListContainer::Register(KAutoObjectWithList* obj) { + KScopedLightLock lk(m_lock); + + m_object_list.insert(*obj); +} + +void KAutoObjectWithListContainer::Unregister(KAutoObjectWithList* obj) { + KScopedLightLock lk(m_lock); + + m_object_list.erase(m_object_list.iterator_to(*obj)); +} + +size_t KAutoObjectWithListContainer::GetOwnedCount(Process* owner) { + KScopedLightLock lk(m_lock); + + size_t count = 0; + + for (auto& obj : m_object_list) { + if (obj.GetOwner() == owner) { + count++; + } + } + + return count; +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_auto_object_container.h b/src/core/hle/kernel/k_auto_object_container.h new file mode 100644 index 000000000..4b599b7c3 --- /dev/null +++ b/src/core/hle/kernel/k_auto_object_container.h @@ -0,0 +1,72 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include + +#include "common/assert.h" +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/intrusive_red_black_tree.h" +#include "core/hle/kernel/k_auto_object.h" +#include "core/hle/kernel/k_light_lock.h" + +namespace Kernel { + +class KernelCore; +class Process; + +class KAutoObjectWithListContainer { + NON_COPYABLE(KAutoObjectWithListContainer); + NON_MOVEABLE(KAutoObjectWithListContainer); + +public: + using ListType = Common::IntrusiveRedBlackTreeMemberTraits< + &KAutoObjectWithList::list_node>::TreeType; + +public: + class ListAccessor : public KScopedLightLock { + private: + ListType& m_list; + + public: + explicit ListAccessor(KAutoObjectWithListContainer* container) + : KScopedLightLock(container->m_lock), m_list(container->m_object_list) { /* ... */ + } + explicit ListAccessor(KAutoObjectWithListContainer& container) + : KScopedLightLock(container.m_lock), m_list(container.m_object_list) { /* ... */ + } + + typename ListType::iterator begin() const { + return m_list.begin(); + } + + typename ListType::iterator end() const { + return m_list.end(); + } + + typename ListType::iterator find(typename ListType::const_reference ref) const { + return m_list.find(ref); + } + }; + + friend class ListAccessor; + +private: + KLightLock m_lock; + ListType m_object_list; + +public: + KAutoObjectWithListContainer(KernelCore& kernel) : m_lock(kernel), m_object_list() {} + + void Initialize() {} + void Finalize() {} + + void Register(KAutoObjectWithList* obj); + void Unregister(KAutoObjectWithList* obj); + size_t GetOwnedCount(Process* owner); +}; + +} // namespace Kernel From f93d939426be2ebe0f334b157f3dad70f52ad418 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 31 Mar 2021 21:12:33 -0700 Subject: [PATCH 05/82] hle: kernel: Add initial impl. of KSlabAllocated. --- src/core/CMakeLists.txt | 1 + src/core/hle/kernel/slab_helpers.h | 152 +++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 src/core/hle/kernel/slab_helpers.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 3cc5b7fbc..f5e06045f 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -234,6 +234,7 @@ add_library(core STATIC hle/kernel/service_thread.h hle/kernel/session.cpp hle/kernel/session.h + hle/kernel/slab_helpers.h hle/kernel/svc.cpp hle/kernel/svc.h hle/kernel/svc_common.h diff --git a/src/core/hle/kernel/slab_helpers.h b/src/core/hle/kernel/slab_helpers.h new file mode 100644 index 000000000..d95095da3 --- /dev/null +++ b/src/core/hle/kernel/slab_helpers.h @@ -0,0 +1,152 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include + +#include "common/assert.h" +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/intrusive_red_black_tree.h" +#include "core/hle/kernel/k_auto_object.h" +#include "core/hle/kernel/k_auto_object_container.h" +#include "core/hle/kernel/k_light_lock.h" +#include "core/hle/kernel/k_slab_heap.h" + +namespace Kernel { + +template +class KSlabAllocated { +private: + static inline KSlabHeap s_slab_heap; + +public: + constexpr KSlabAllocated() = default; + + size_t GetSlabIndex() const { + return s_slab_heap.GetIndex(static_cast(this)); + } + +public: + static void InitializeSlabHeap(void* memory, size_t memory_size) { + s_slab_heap.Initialize(memory, memory_size); + } + + static Derived* Allocate() { + return s_slab_heap.Allocate(); + } + + static void Free(Derived* obj) { + s_slab_heap.Free(obj); + } + + static size_t GetObjectSize() { + return s_slab_heap.GetObjectSize(); + } + static size_t GetSlabHeapSize() { + return s_slab_heap.GetSlabHeapSize(); + } + static size_t GetPeakIndex() { + return s_slab_heap.GetPeakIndex(); + } + static uintptr_t GetSlabHeapAddress() { + return s_slab_heap.GetSlabHeapAddress(); + } + + static size_t GetNumRemaining() { + return s_slab_heap.GetNumRemaining(); + } +}; + +template +class KAutoObjectWithSlabHeapAndContainer : public Base { + static_assert(std::is_base_of::value); + +private: + static inline KSlabHeap s_slab_heap; + static inline KAutoObjectWithListContainer s_container; + +private: + static Derived* Allocate() { + return s_slab_heap.Allocate(); + } + + static void Free(Derived* obj) { + s_slab_heap.Free(obj); + } + +public: + class ListAccessor : public KAutoObjectWithListContainer::ListAccessor { + public: + ListAccessor() : KAutoObjectWithListContainer::ListAccessor(s_container) {} + ~ListAccessor() = default; + }; + +public: + constexpr KAutoObjectWithSlabHeapAndContainer() : Base() {} + virtual ~KAutoObjectWithSlabHeapAndContainer() {} + + virtual void Destroy() override { + const bool is_initialized = this->IsInitialized(); + uintptr_t arg = 0; + if (is_initialized) { + s_container.Unregister(this); + arg = this->GetPostDestroyArgument(); + this->Finalize(); + } + Free(static_cast(this)); + if (is_initialized) { + Derived::PostDestroy(arg); + } + } + + virtual bool IsInitialized() const { + return true; + } + virtual uintptr_t GetPostDestroyArgument() const { + return 0; + } + + size_t GetSlabIndex() const { + return s_slab_heap.GetObjectIndex(static_cast(this)); + } + +public: + static void InitializeSlabHeap(void* memory, size_t memory_size) { + s_slab_heap.Initialize(memory, memory_size); + s_container.Initialize(); + } + + static Derived* Create() { + Derived* obj = Allocate(); + if (AMS_LIKELY(obj != nullptr)) { + KAutoObject::Create(obj); + } + return obj; + } + + static void Register(Derived* obj) { + return s_container.Register(obj); + } + + static size_t GetObjectSize() { + return s_slab_heap.GetObjectSize(); + } + static size_t GetSlabHeapSize() { + return s_slab_heap.GetSlabHeapSize(); + } + static size_t GetPeakIndex() { + return s_slab_heap.GetPeakIndex(); + } + static uintptr_t GetSlabHeapAddress() { + return s_slab_heap.GetSlabHeapAddress(); + } + + static size_t GetNumRemaining() { + return s_slab_heap.GetNumRemaining(); + } +}; + +} // namespace Kernel From 74120c5e3a721bb05840873e88cddad0e966f17d Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 1 Apr 2021 14:47:35 -0700 Subject: [PATCH 06/82] common: bit_util: Add BIT macro. --- src/common/bit_util.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 64520ca4e..683f09158 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -44,4 +44,6 @@ template return static_cast(log2_f + static_cast((value ^ (1ULL << log2_f)) != 0ULL)); } +#define BIT(n) (1U << (n)) + } // namespace Common From 66f2ad716be8f880e5bb6cc572c76812073f6db4 Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 1 Apr 2021 23:04:18 -0700 Subject: [PATCH 07/82] hle: kernel: Add initial impl. of KLinkedList. --- src/core/CMakeLists.txt | 1 + src/core/hle/kernel/k_linked_list.h | 233 ++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+) create mode 100644 src/core/hle/kernel/k_linked_list.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index f5e06045f..4a7589fe8 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -173,6 +173,7 @@ add_library(core STATIC hle/kernel/k_light_condition_variable.h hle/kernel/k_light_lock.cpp hle/kernel/k_light_lock.h + hle/kernel/k_linked_list.h hle/kernel/k_memory_block.h hle/kernel/k_memory_block_manager.cpp hle/kernel/k_memory_block_manager.h diff --git a/src/core/hle/kernel/k_linked_list.h b/src/core/hle/kernel/k_linked_list.h new file mode 100644 index 000000000..8362b6eda --- /dev/null +++ b/src/core/hle/kernel/k_linked_list.h @@ -0,0 +1,233 @@ +// Copyright 2021 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include + +#include "common/assert.h" +#include "core/hle/kernel/slab_helpers.h" + +namespace Kernel { + +class KLinkedListNode : public boost::intrusive::list_base_hook<>, + public KSlabAllocated { +private: + void* m_item; + +public: + KLinkedListNode() : m_item(nullptr) {} + + constexpr void Initialize(void* it) { + m_item = it; + } + + constexpr void* GetItem() const { + return m_item; + } +}; + +template +class KLinkedList : private boost::intrusive::list { +private: + using BaseList = boost::intrusive::list; + +public: + template + class Iterator; + + using value_type = T; + using size_type = size_t; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using const_pointer = const value_type*; + using reference = value_type&; + using const_reference = const value_type&; + using iterator = Iterator; + using const_iterator = Iterator; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + + template + class Iterator { + private: + using BaseIterator = BaseList::iterator; + friend class KLinkedList; + + public: + using iterator_category = std::bidirectional_iterator_tag; + using value_type = typename KLinkedList::value_type; + using difference_type = typename KLinkedList::difference_type; + using pointer = typename std::conditional::type; + using reference = typename std::conditional::type; + + private: + BaseIterator m_base_it; + + public: + explicit Iterator(BaseIterator it) : m_base_it(it) {} + + pointer GetItem() const { + return static_cast(m_base_it->GetItem()); + } + + bool operator==(const Iterator& rhs) const { + return m_base_it == rhs.m_base_it; + } + + bool operator!=(const Iterator& rhs) const { + return !(*this == rhs); + } + + pointer operator->() const { + return this->GetItem(); + } + + reference operator*() const { + return *this->GetItem(); + } + + Iterator& operator++() { + ++m_base_it; + return *this; + } + + Iterator& operator--() { + --m_base_it; + return *this; + } + + Iterator operator++(int) { + const Iterator it{*this}; + ++(*this); + return it; + } + + Iterator operator--(int) { + const Iterator it{*this}; + --(*this); + return it; + } + + operator Iterator() const { + return Iterator(m_base_it); + } + }; + +public: + constexpr KLinkedList() : BaseList() {} + + ~KLinkedList() { + // Erase all elements. + for (auto it = this->begin(); it != this->end(); it = this->erase(it)) { + } + + // Ensure we succeeded. + ASSERT(this->empty()); + } + + // Iterator accessors. + iterator begin() { + return iterator(BaseList::begin()); + } + + const_iterator begin() const { + return const_iterator(BaseList::begin()); + } + + iterator end() { + return iterator(BaseList::end()); + } + + const_iterator end() const { + return const_iterator(BaseList::end()); + } + + const_iterator cbegin() const { + return this->begin(); + } + + const_iterator cend() const { + return this->end(); + } + + reverse_iterator rbegin() { + return reverse_iterator(this->end()); + } + + const_reverse_iterator rbegin() const { + return const_reverse_iterator(this->end()); + } + + reverse_iterator rend() { + return reverse_iterator(this->begin()); + } + + const_reverse_iterator rend() const { + return const_reverse_iterator(this->begin()); + } + + const_reverse_iterator crbegin() const { + return this->rbegin(); + } + + const_reverse_iterator crend() const { + return this->rend(); + } + + // Content management. + using BaseList::empty; + using BaseList::size; + + reference back() { + return *(--this->end()); + } + + const_reference back() const { + return *(--this->end()); + } + + reference front() { + return *this->begin(); + } + + const_reference front() const { + return *this->begin(); + } + + iterator insert(const_iterator pos, reference ref) { + KLinkedListNode* node = KLinkedListNode::Allocate(); + ASSERT(node != nullptr); + node->Initialize(std::addressof(ref)); + return iterator(BaseList::insert(pos.m_base_it, *node)); + } + + void push_back(reference ref) { + this->insert(this->end(), ref); + } + + void push_front(reference ref) { + this->insert(this->begin(), ref); + } + + void pop_back() { + this->erase(--this->end()); + } + + void pop_front() { + this->erase(this->begin()); + } + + iterator erase(const iterator pos) { + KLinkedListNode* freed_node = std::addressof(*pos.m_base_it); + iterator ret = iterator(BaseList::erase(pos.m_base_it)); + KLinkedListNode::Free(freed_node); + + return ret; + } +}; + +} // namespace Kernel From 02c2b28cd0f341fd955f9648d9174777e0b60689 Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 1 Apr 2021 23:04:54 -0700 Subject: [PATCH 08/82] common: common_funcs: Add Size helper function. --- src/common/common_funcs.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index 73c8c9354..19bb021e0 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h @@ -136,4 +136,19 @@ namespace Common { return u32(a) | u32(b) << 8 | u32(c) << 16 | u32(d) << 24; } +// std::size() does not support zero-size C arrays. We're fixing that. +template +constexpr auto Size(const C& c) -> decltype(c.size()) { + return std::size(c); +} + +template +constexpr std::size_t Size(const C& c) { + if constexpr (sizeof(C) == 0) { + return 0; + } else { + return std::size(c); + } +} + } // namespace Common From 340167676828bf4595bc267c927f156a11b288aa Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 2 Apr 2021 17:06:21 -0700 Subject: [PATCH 09/82] core: memory: Add a work-around to allocate and access kernel memory regions by vaddr. --- src/core/core.cpp | 1 + src/core/memory.cpp | 30 +++++++++++++++++++++++++++++- src/core/memory.h | 16 ++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index d459d6c34..66500a0d4 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -311,6 +311,7 @@ struct System::Impl { gpu_core.reset(); perf_stats.reset(); kernel.Shutdown(); + memory.Reset(); applet_manager.ClearAll(); LOG_DEBUG(Core, "Shutdown OK"); diff --git a/src/core/memory.cpp b/src/core/memory.cpp index b9dd3e275..ee4599063 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -82,6 +82,22 @@ struct Memory::Impl { return nullptr; } + u8* GetKernelBuffer(VAddr start_vaddr, size_t size) { + // TODO(bunnei): This is just a workaround until we have kernel memory layout mapped & + // managed. Until then, we use this to allocate and access kernel memory regions. + + auto search = kernel_memory_regions.find(start_vaddr); + if (search != kernel_memory_regions.end()) { + return search->second.get(); + } + + std::unique_ptr new_memory_region{new u8[size]}; + u8* raw_ptr = new_memory_region.get(); + kernel_memory_regions[start_vaddr] = std::move(new_memory_region); + + return raw_ptr; + } + u8 Read8(const VAddr addr) { return Read(addr); } @@ -711,12 +727,20 @@ struct Memory::Impl { } Common::PageTable* current_page_table = nullptr; + std::unordered_map> kernel_memory_regions; Core::System& system; }; -Memory::Memory(Core::System& system) : impl{std::make_unique(system)} {} +Memory::Memory(Core::System& system_) : system{system_} { + Reset(); +} + Memory::~Memory() = default; +void Memory::Reset() { + impl = std::make_unique(system); +} + void Memory::SetCurrentPageTable(Kernel::Process& process, u32 core_id) { impl->SetCurrentPageTable(process, core_id); } @@ -741,6 +765,10 @@ u8* Memory::GetPointer(VAddr vaddr) { return impl->GetPointer(vaddr); } +u8* Memory::GetKernelBuffer(VAddr start_vaddr, size_t size) { + return impl->GetKernelBuffer(start_vaddr, size); +} + const u8* Memory::GetPointer(VAddr vaddr) const { return impl->GetPointer(vaddr); } diff --git a/src/core/memory.h b/src/core/memory.h index 6d34fcfe2..9a706a9ac 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -58,6 +58,11 @@ public: Memory(Memory&&) = default; Memory& operator=(Memory&&) = default; + /** + * Resets the state of the Memory system. + */ + void Reset(); + /** * Changes the currently active page table to that of the given process instance. * @@ -116,6 +121,15 @@ public: */ u8* GetPointer(VAddr vaddr); + /** + * Gets a pointer to the start of a kernel heap allocated memory region. Will allocate one if it + * does not already exist. + * + * @param start_vaddr Start virtual address for the memory region. + * @param size Size of the memory region. + */ + u8* GetKernelBuffer(VAddr start_vaddr, size_t size); + template T* GetPointer(VAddr vaddr) { return reinterpret_cast(GetPointer(vaddr)); @@ -524,6 +538,8 @@ public: void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached); private: + Core::System& system; + struct Impl; std::unique_ptr impl; }; From d9df63583fb3d64512a70ceb3a4b66390c044e98 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 2 Apr 2021 17:59:04 -0700 Subject: [PATCH 10/82] core: Defer CoreTiming initialization. --- src/core/core.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 66500a0d4..fdaa82c8f 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -166,9 +166,9 @@ struct System::Impl { cpu_manager.SetAsyncGpu(is_async_gpu); core_timing.SetMulticore(is_multicore); - core_timing.Initialize([&system]() { system.RegisterHostThread(); }); kernel.Initialize(); cpu_manager.Initialize(); + core_timing.Initialize([&system]() { system.RegisterHostThread(); }); const auto current_time = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()); From 34bed1ab41a296f8ccccc47d7c06ab03de2018b5 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 2 Apr 2021 18:02:10 -0700 Subject: [PATCH 11/82] hle: kernel: Refactor out various KThread std::shared_ptr usage. --- src/core/hle/kernel/client_session.cpp | 3 +-- src/core/hle/kernel/client_session.h | 2 +- .../hle/kernel/global_scheduler_context.cpp | 6 ++--- .../hle/kernel/global_scheduler_context.h | 8 +++---- src/core/hle/kernel/hle_ipc.cpp | 10 ++++---- src/core/hle/kernel/hle_ipc.h | 10 +++----- src/core/hle/kernel/kernel.cpp | 4 ++-- src/core/hle/kernel/server_session.cpp | 23 ++++--------------- src/core/hle/kernel/server_session.h | 14 ++--------- src/core/hle/service/prepo/prepo.cpp | 8 +++---- 10 files changed, 30 insertions(+), 58 deletions(-) diff --git a/src/core/hle/kernel/client_session.cpp b/src/core/hle/kernel/client_session.cpp index e230f365a..13c55abe4 100644 --- a/src/core/hle/kernel/client_session.cpp +++ b/src/core/hle/kernel/client_session.cpp @@ -38,8 +38,7 @@ ResultVal> ClientSession::Create(KernelCore& kern return MakeResult(std::move(client_session)); } -ResultCode ClientSession::SendSyncRequest(std::shared_ptr thread, - Core::Memory::Memory& memory, +ResultCode ClientSession::SendSyncRequest(KThread* thread, Core::Memory::Memory& memory, Core::Timing::CoreTiming& core_timing) { // Keep ServerSession alive until we're done working with it. if (!parent->Server()) { diff --git a/src/core/hle/kernel/client_session.h b/src/core/hle/kernel/client_session.h index 85aafeaf4..7a1d15d0c 100644 --- a/src/core/hle/kernel/client_session.h +++ b/src/core/hle/kernel/client_session.h @@ -46,7 +46,7 @@ public: return HANDLE_TYPE; } - ResultCode SendSyncRequest(std::shared_ptr thread, Core::Memory::Memory& memory, + ResultCode SendSyncRequest(KThread* thread, Core::Memory::Memory& memory, Core::Timing::CoreTiming& core_timing); bool IsSignaled() const override; diff --git a/src/core/hle/kernel/global_scheduler_context.cpp b/src/core/hle/kernel/global_scheduler_context.cpp index c6838649f..7c87cbada 100644 --- a/src/core/hle/kernel/global_scheduler_context.cpp +++ b/src/core/hle/kernel/global_scheduler_context.cpp @@ -17,12 +17,12 @@ GlobalSchedulerContext::GlobalSchedulerContext(KernelCore& kernel) GlobalSchedulerContext::~GlobalSchedulerContext() = default; -void GlobalSchedulerContext::AddThread(std::shared_ptr thread) { +void GlobalSchedulerContext::AddThread(KThread* thread) { std::scoped_lock lock{global_list_guard}; - thread_list.push_back(std::move(thread)); + thread_list.push_back(thread); } -void GlobalSchedulerContext::RemoveThread(std::shared_ptr thread) { +void GlobalSchedulerContext::RemoveThread(KThread* thread) { std::scoped_lock lock{global_list_guard}; thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread), thread_list.end()); diff --git a/src/core/hle/kernel/global_scheduler_context.h b/src/core/hle/kernel/global_scheduler_context.h index 11592843e..ba8b67fd1 100644 --- a/src/core/hle/kernel/global_scheduler_context.h +++ b/src/core/hle/kernel/global_scheduler_context.h @@ -38,13 +38,13 @@ public: ~GlobalSchedulerContext(); /// Adds a new thread to the scheduler - void AddThread(std::shared_ptr thread); + void AddThread(KThread* thread); /// Removes a thread from the scheduler - void RemoveThread(std::shared_ptr thread); + void RemoveThread(KThread* thread); /// Returns a list of all threads managed by the scheduler - [[nodiscard]] const std::vector>& GetThreadList() const { + [[nodiscard]] const std::vector& GetThreadList() const { return thread_list; } @@ -79,7 +79,7 @@ private: LockType scheduler_lock; /// Lists all thread ids that aren't deleted/etc. - std::vector> thread_list; + std::vector thread_list; Common::SpinLock global_list_guard{}; }; diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index 2b363b1d9..6ffe6ac41 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -46,11 +46,11 @@ void SessionRequestHandler::ClientDisconnected( boost::range::remove_erase(connected_sessions, server_session); } -HLERequestContext::HLERequestContext(KernelCore& kernel, Core::Memory::Memory& memory, - std::shared_ptr server_session, - std::shared_ptr thread) - : server_session(std::move(server_session)), - thread(std::move(thread)), kernel{kernel}, memory{memory} { +HLERequestContext::HLERequestContext(KernelCore& kernel_, Core::Memory::Memory& memory_, + std::shared_ptr server_session_, + KThread* thread_) + : server_session(std::move(server_session_)), + thread(thread_), kernel{kernel_}, memory{memory_} { cmd_buf[0] = 0; } diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index 6fba42615..75617bff0 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -109,8 +109,7 @@ protected: class HLERequestContext { public: explicit HLERequestContext(KernelCore& kernel, Core::Memory::Memory& memory, - std::shared_ptr session, - std::shared_ptr thread); + std::shared_ptr session, KThread* thread); ~HLERequestContext(); /// Returns a pointer to the IPC command buffer for this request. @@ -276,10 +275,6 @@ public: return *thread; } - const KThread& GetThread() const { - return *thread; - } - bool IsThreadWaiting() const { return is_thread_waiting; } @@ -291,7 +286,8 @@ private: std::array cmd_buf; std::shared_ptr server_session; - std::shared_ptr thread; + KThread* thread; + // TODO(yuriks): Check common usage of this and optimize size accordingly boost::container::small_vector move_handles; boost::container::small_vector copy_handles; diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 5c4f45ab4..17fe1d59f 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -60,8 +60,6 @@ struct KernelCore::Impl { void Initialize(KernelCore& kernel) { global_scheduler_context = std::make_unique(kernel); - RegisterHostThread(); - service_thread_manager = std::make_unique(1, "yuzu:ServiceThreadManager"); is_phantom_mode_for_singlecore = false; @@ -77,6 +75,8 @@ struct KernelCore::Impl { InitializeSchedulers(); InitializeSuspendThreads(); InitializePreemption(kernel); + + RegisterHostThread(); } void InitializeCores() { diff --git a/src/core/hle/kernel/server_session.cpp b/src/core/hle/kernel/server_session.cpp index 790dbb998..bb247959c 100644 --- a/src/core/hle/kernel/server_session.cpp +++ b/src/core/hle/kernel/server_session.cpp @@ -44,12 +44,7 @@ ResultVal> ServerSession::Create(KernelCore& kern bool ServerSession::IsSignaled() const { // Closed sessions should never wait, an error will be returned from svcReplyAndReceive. - if (!parent->Client()) { - return true; - } - - // Wait if we have no pending requests, or if we're currently handling a request. - return !pending_requesting_threads.empty() && currently_handling == nullptr; + return !parent->Client(); } void ServerSession::ClientDisconnected() { @@ -62,11 +57,6 @@ void ServerSession::ClientDisconnected() { // invalidated (set to null). handler->ClientDisconnected(SharedFrom(this)); } - - // Clean up the list of client threads with pending requests, they are unneeded now that the - // client endpoint is closed. - pending_requesting_threads.clear(); - currently_handling = nullptr; } void ServerSession::AppendDomainRequestHandler(std::shared_ptr handler) { @@ -116,11 +106,9 @@ ResultCode ServerSession::HandleDomainSyncRequest(Kernel::HLERequestContext& con return RESULT_SUCCESS; } -ResultCode ServerSession::QueueSyncRequest(std::shared_ptr thread, - Core::Memory::Memory& memory) { +ResultCode ServerSession::QueueSyncRequest(KThread* thread, Core::Memory::Memory& memory) { u32* cmd_buf{reinterpret_cast(memory.GetPointer(thread->GetTLSAddress()))}; - auto context = - std::make_shared(kernel, memory, SharedFrom(this), std::move(thread)); + auto context = std::make_shared(kernel, memory, SharedFrom(this), thread); context->PopulateFromIncomingCommandBuffer(kernel.CurrentProcess()->GetHandleTable(), cmd_buf); @@ -161,10 +149,9 @@ ResultCode ServerSession::CompleteSyncRequest(HLERequestContext& context) { return result; } -ResultCode ServerSession::HandleSyncRequest(std::shared_ptr thread, - Core::Memory::Memory& memory, +ResultCode ServerSession::HandleSyncRequest(KThread* thread, Core::Memory::Memory& memory, Core::Timing::CoreTiming& core_timing) { - return QueueSyncRequest(std::move(thread), memory); + return QueueSyncRequest(thread, memory); } } // namespace Kernel diff --git a/src/core/hle/kernel/server_session.h b/src/core/hle/kernel/server_session.h index c42d5ee59..77ed18c60 100644 --- a/src/core/hle/kernel/server_session.h +++ b/src/core/hle/kernel/server_session.h @@ -95,7 +95,7 @@ public: * * @returns ResultCode from the operation. */ - ResultCode HandleSyncRequest(std::shared_ptr thread, Core::Memory::Memory& memory, + ResultCode HandleSyncRequest(KThread* thread, Core::Memory::Memory& memory, Core::Timing::CoreTiming& core_timing); /// Called when a client disconnection occurs. @@ -130,7 +130,7 @@ public: private: /// Queues a sync request from the emulated application. - ResultCode QueueSyncRequest(std::shared_ptr thread, Core::Memory::Memory& memory); + ResultCode QueueSyncRequest(KThread* thread, Core::Memory::Memory& memory); /// Completes a sync request from the emulated application. ResultCode CompleteSyncRequest(HLERequestContext& context); @@ -148,16 +148,6 @@ private: /// This is the list of domain request handlers (after conversion to a domain) std::vector> domain_request_handlers; - /// List of threads that are pending a response after a sync request. This list is processed in - /// a LIFO manner, thus, the last request will be dispatched first. - /// TODO(Subv): Verify if this is indeed processed in LIFO using a hardware test. - std::vector> pending_requesting_threads; - - /// Thread whose request is currently being handled. A request is considered "handled" when a - /// response is sent via svcReplyAndReceive. - /// TODO(Subv): Find a better name for this. - std::shared_ptr currently_handling; - /// When set to True, converts the session to a domain at the end of the command bool convert_to_domain{}; diff --git a/src/core/hle/service/prepo/prepo.cpp b/src/core/hle/service/prepo/prepo.cpp index d5b3b17a5..449e8d63b 100644 --- a/src/core/hle/service/prepo/prepo.cpp +++ b/src/core/hle/service/prepo/prepo.cpp @@ -60,7 +60,7 @@ private: const auto process_id = rp.PopRaw(); const auto data1 = ctx.ReadBuffer(0); - const auto data2 = [ctx] { + const auto data2 = [&ctx] { if (ctx.CanReadBuffer(1)) { return ctx.ReadBuffer(1); } @@ -87,7 +87,7 @@ private: const auto process_id = rp.PopRaw(); const auto data1 = ctx.ReadBuffer(0); - const auto data2 = [ctx] { + const auto data2 = [&ctx] { if (ctx.CanReadBuffer(1)) { return ctx.ReadBuffer(1); } @@ -139,7 +139,7 @@ private: const auto title_id = rp.PopRaw(); const auto data1 = ctx.ReadBuffer(0); - const auto data2 = [ctx] { + const auto data2 = [&ctx] { if (ctx.CanReadBuffer(1)) { return ctx.ReadBuffer(1); } @@ -163,7 +163,7 @@ private: const auto title_id = rp.PopRaw(); const auto data1 = ctx.ReadBuffer(0); - const auto data2 = [ctx] { + const auto data2 = [&ctx] { if (ctx.CanReadBuffer(1)) { return ctx.ReadBuffer(1); } From d3d0f2f451041a23214978ee224051a8575cf119 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 2 Apr 2021 22:16:58 -0700 Subject: [PATCH 12/82] hle: kernel: Add initial impl. of slab setup. --- src/core/CMakeLists.txt | 2 + src/core/hle/kernel/init/init_slab_setup.cpp | 183 +++++++++++++++++++ src/core/hle/kernel/init/init_slab_setup.h | 42 +++++ 3 files changed, 227 insertions(+) create mode 100644 src/core/hle/kernel/init/init_slab_setup.cpp create mode 100644 src/core/hle/kernel/init/init_slab_setup.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 4a7589fe8..d149e2ba6 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -157,6 +157,8 @@ add_library(core STATIC hle/kernel/handle_table.h hle/kernel/hle_ipc.cpp hle/kernel/hle_ipc.h + hle/kernel/init/init_slab_setup.cpp + hle/kernel/init/init_slab_setup.h hle/kernel/k_address_arbiter.cpp hle/kernel/k_address_arbiter.h hle/kernel/k_address_space_info.cpp diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp new file mode 100644 index 000000000..a290249c7 --- /dev/null +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -0,0 +1,183 @@ +// Copyright 2021 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/alignment.h" +#include "common/assert.h" +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "core/core.h" +#include "core/hardware_properties.h" +#include "core/hle/kernel/init/init_slab_setup.h" +#include "core/hle/kernel/k_memory_layout.h" +#include "core/hle/kernel/k_memory_manager.h" +#include "core/hle/kernel/k_system_control.h" +#include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/memory_types.h" +#include "core/memory.h" + +namespace Kernel::Init { + +#define SLAB_COUNT(CLASS) g_slab_resource_counts.num_##CLASS + +#define FOREACH_SLAB_TYPE(HANDLER, ...) HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) + +namespace { + +#define DEFINE_SLAB_TYPE_ENUM_MEMBER(NAME, COUNT, ...) KSlabType_##NAME, + +enum KSlabType : u32 { + FOREACH_SLAB_TYPE(DEFINE_SLAB_TYPE_ENUM_MEMBER) KSlabType_Count, +}; + +#undef DEFINE_SLAB_TYPE_ENUM_MEMBER + +// Constexpr counts. +constexpr size_t SlabCountKProcess = 80; +constexpr size_t SlabCountKThread = 800; +constexpr size_t SlabCountKEvent = 700; +constexpr size_t SlabCountKInterruptEvent = 100; +constexpr size_t SlabCountKPort = 256 + 0x20; // Extra 0x20 ports over Nintendo for homebrew. +constexpr size_t SlabCountKSharedMemory = 80; +constexpr size_t SlabCountKTransferMemory = 200; +constexpr size_t SlabCountKCodeMemory = 10; +constexpr size_t SlabCountKDeviceAddressSpace = 300; +constexpr size_t SlabCountKSession = 933; +constexpr size_t SlabCountKLightSession = 100; +constexpr size_t SlabCountKObjectName = 7; +constexpr size_t SlabCountKResourceLimit = 5; +constexpr size_t SlabCountKDebug = Core::Hardware::NUM_CPU_CORES; +constexpr size_t SlabCountKAlpha = 1; +constexpr size_t SlabCountKBeta = 6; + +constexpr size_t SlabCountExtraKThread = 160; + +// Global to hold our resource counts. +KSlabResourceCounts g_slab_resource_counts = { + .num_KProcess = SlabCountKProcess, + .num_KThread = SlabCountKThread, + .num_KEvent = SlabCountKEvent, + .num_KInterruptEvent = SlabCountKInterruptEvent, + .num_KPort = SlabCountKPort, + .num_KSharedMemory = SlabCountKSharedMemory, + .num_KTransferMemory = SlabCountKTransferMemory, + .num_KCodeMemory = SlabCountKCodeMemory, + .num_KDeviceAddressSpace = SlabCountKDeviceAddressSpace, + .num_KSession = SlabCountKSession, + .num_KLightSession = SlabCountKLightSession, + .num_KObjectName = SlabCountKObjectName, + .num_KResourceLimit = SlabCountKResourceLimit, + .num_KDebug = SlabCountKDebug, + .num_KAlpha = SlabCountKAlpha, + .num_KBeta = SlabCountKBeta, +}; + +template +VAddr InitializeSlabHeap(Core::System& system, KMemoryLayout& memory_layout, VAddr address, + size_t num_objects) { + const size_t size = Common::AlignUp(sizeof(T) * num_objects, alignof(void*)); + VAddr start = Common::AlignUp(address, alignof(T)); + + if (size > 0) { + const KMemoryRegion* region = memory_layout.FindVirtual(start + size - 1); + ASSERT(region != nullptr); + ASSERT(region->IsDerivedFrom(KMemoryRegionType_KernelSlab)); + T::InitializeSlabHeap(system.Kernel(), system.Memory().GetKernelBuffer(start, size), size); + } + + return start + size; +} + +} // namespace + +const KSlabResourceCounts& GetSlabResourceCounts() { + return g_slab_resource_counts; +} + +void InitializeSlabResourceCounts() { + // Note: Nintendo initializes all fields here, but we initialize all constants at compile-time. + + if (KSystemControl::Init::ShouldIncreaseThreadResourceLimit()) { + g_slab_resource_counts.num_KThread += SlabCountExtraKThread; + } +} + +size_t CalculateSlabHeapGapSize() { + return KernelSlabHeapGapsSize; +} + +size_t CalculateTotalSlabHeapSize() { + size_t size = 0; + +#define ADD_SLAB_SIZE(NAME, COUNT, ...) \ + { \ + size += alignof(NAME); \ + size += Common::AlignUp(sizeof(NAME) * (COUNT), alignof(void*)); \ + }; + + // Add the size required for each slab. + FOREACH_SLAB_TYPE(ADD_SLAB_SIZE) + +#undef ADD_SLAB_SIZE + + // Add the reserved size. + size += CalculateSlabHeapGapSize(); + + return size; +} + +void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) { + // Get the start of the slab region, since that's where we'll be working. + VAddr address = memory_layout.GetSlabRegionAddress(); + + // Initialize slab type array to be in sorted order. + KSlabType slab_types[KSlabType_Count]; + for (size_t i = 0; i < Common::Size(slab_types); i++) { + slab_types[i] = static_cast(i); + } + + // N shuffles the slab type array with the following simple algorithm. + for (size_t i = 0; i < Common::Size(slab_types); i++) { + const size_t rnd = KSystemControl::GenerateRandomRange(0, Common::Size(slab_types) - 1); + std::swap(slab_types[i], slab_types[rnd]); + } + + // Create an array to represent the gaps between the slabs. + const size_t total_gap_size = CalculateSlabHeapGapSize(); + size_t slab_gaps[Common::Size(slab_types)]; + for (size_t i = 0; i < Common::Size(slab_gaps); i++) { + // Note: This is an off-by-one error from Nintendo's intention, because GenerateRandomRange + // is inclusive. However, Nintendo also has the off-by-one error, and it's "harmless", so we + // will include it ourselves. + slab_gaps[i] = KSystemControl::GenerateRandomRange(0, total_gap_size); + } + + // Sort the array, so that we can treat differences between values as offsets to the starts of + // slabs. + for (size_t i = 1; i < Common::Size(slab_gaps); i++) { + for (size_t j = i; j > 0 && slab_gaps[j - 1] > slab_gaps[j]; j--) { + std::swap(slab_gaps[j], slab_gaps[j - 1]); + } + } + + for (size_t i = 0; i < Common::Size(slab_types); i++) { + // Add the random gap to the address. + address += (i == 0) ? slab_gaps[0] : slab_gaps[i] - slab_gaps[i - 1]; + +#define INITIALIZE_SLAB_HEAP(NAME, COUNT, ...) \ + case KSlabType_##NAME: \ + address = InitializeSlabHeap(system, memory_layout, address, COUNT); \ + break; + + // Initialize the slabheap. + switch (slab_types[i]) { + // For each of the slab types, we want to initialize that heap. + FOREACH_SLAB_TYPE(INITIALIZE_SLAB_HEAP) + // If we somehow get an invalid type, abort. + default: + UNREACHABLE(); + } + } +} + +} // namespace Kernel::Init diff --git a/src/core/hle/kernel/init/init_slab_setup.h b/src/core/hle/kernel/init/init_slab_setup.h new file mode 100644 index 000000000..6418b97ac --- /dev/null +++ b/src/core/hle/kernel/init/init_slab_setup.h @@ -0,0 +1,42 @@ +// Copyright 2021 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +namespace Core { +class System; +} // namespace Core + +namespace Kernel { +class KMemoryLayout; +} // namespace Kernel + +namespace Kernel::Init { + +struct KSlabResourceCounts { + size_t num_KProcess; + size_t num_KThread; + size_t num_KEvent; + size_t num_KInterruptEvent; + size_t num_KPort; + size_t num_KSharedMemory; + size_t num_KTransferMemory; + size_t num_KCodeMemory; + size_t num_KDeviceAddressSpace; + size_t num_KSession; + size_t num_KLightSession; + size_t num_KObjectName; + size_t num_KResourceLimit; + size_t num_KDebug; + size_t num_KAlpha; + size_t num_KBeta; +}; + +void InitializeSlabResourceCounts(); +const KSlabResourceCounts& GetSlabResourceCounts(); + +size_t CalculateTotalSlabHeapSize(); +void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout); + +} // namespace Kernel::Init From 479bd50b96ecb7258528dea98d7528568ba67bee Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 2 Apr 2021 23:24:20 -0700 Subject: [PATCH 13/82] hle: kernel: Migrate KThread to KAutoObject. --- src/core/hle/kernel/k_thread.cpp | 100 +++++++++++++++---------------- src/core/hle/kernel/k_thread.h | 100 +++++++++++++------------------ 2 files changed, 91 insertions(+), 109 deletions(-) diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp index e0f53287c..5d0b266c5 100644 --- a/src/core/hle/kernel/k_thread.cpp +++ b/src/core/hle/kernel/k_thread.cpp @@ -62,7 +62,7 @@ static void ResetThreadContext64(Core::ARM_Interface::ThreadContext64& context, namespace Kernel { KThread::KThread(KernelCore& kernel) - : KSynchronizationObject{kernel}, activity_pause_lock{kernel} {} + : KAutoObjectWithSlabHeapAndContainer{kernel}, activity_pause_lock{kernel} {} KThread::~KThread() = default; ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_stack_top, s32 prio, @@ -177,6 +177,7 @@ ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_s // Set parent, if relevant. if (owner != nullptr) { parent = owner; + parent->Open(); parent->IncrementThreadCount(); } @@ -210,13 +211,55 @@ ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_s ResultCode KThread::InitializeThread(KThread* thread, KThreadFunction func, uintptr_t arg, VAddr user_stack_top, s32 prio, s32 core, Process* owner, - ThreadType type) { + ThreadType type, std::function&& init_func, + void* init_func_parameter) { // Initialize the thread. R_TRY(thread->Initialize(func, arg, user_stack_top, prio, core, owner, type)); + // Initialize host context. + thread->host_context = + std::make_shared(std::move(init_func), init_func_parameter); + return RESULT_SUCCESS; } +ResultCode KThread::InitializeDummyThread(KThread* thread) { + return thread->Initialize({}, {}, {}, DefaultThreadPriority, 3, {}, ThreadType::Main); +} + +ResultCode KThread::InitializeIdleThread(Core::System& system, KThread* thread, s32 virt_core) { + return InitializeThread(thread, {}, {}, {}, IdleThreadPriority, virt_core, {}, ThreadType::Main, + Core::CpuManager::GetIdleThreadStartFunc(), + system.GetCpuManager().GetStartFuncParamater()); +} + +ResultCode KThread::InitializeHighPriorityThread(Core::System& system, KThread* thread, + KThreadFunction func, uintptr_t arg, + s32 virt_core) { + return InitializeThread(thread, func, arg, {}, {}, virt_core, nullptr, ThreadType::HighPriority, + Core::CpuManager::GetSuspendThreadStartFunc(), + system.GetCpuManager().GetStartFuncParamater()); +} + +ResultCode KThread::InitializeUserThread(Core::System& system, KThread* thread, + KThreadFunction func, uintptr_t arg, VAddr user_stack_top, + s32 prio, s32 virt_core, Process* owner) { + system.Kernel().GlobalSchedulerContext().AddThread(thread); + return InitializeThread(thread, func, arg, user_stack_top, prio, virt_core, owner, + ThreadType::User, Core::CpuManager::GetGuestThreadStartFunc(), + system.GetCpuManager().GetStartFuncParamater()); +} + +void KThread::PostDestroy(uintptr_t arg) { + Process* owner = reinterpret_cast(arg & ~1ULL); + const bool resource_limit_release_hint = (arg & 1); + const s64 hint_value = (resource_limit_release_hint ? 0 : 1); + if (owner != nullptr) { + owner->GetResourceLimit()->Release(Kernel::LimitableResource::Threads, 1, hint_value); + owner->Close(); + } +} + void KThread::Finalize() { // If the thread has an owner process, unregister it. if (parent != nullptr) { @@ -294,6 +337,9 @@ void KThread::StartTermination() { // Register terminated dpc flag. RegisterDpc(DpcFlag::Terminated); + + // Close the thread. + this->Close(); } void KThread::Pin() { @@ -995,56 +1041,6 @@ std::shared_ptr& KThread::GetHostContext() { return host_context; } -ResultVal> KThread::CreateThread(Core::System& system, - ThreadType type_flags, std::string name, - VAddr entry_point, u32 priority, u64 arg, - s32 processor_id, VAddr stack_top, - Process* owner_process) { - auto& kernel = system.Kernel(); - - std::shared_ptr thread = std::make_shared(kernel); - - if (const auto result = - thread->InitializeThread(thread.get(), entry_point, arg, stack_top, priority, - processor_id, owner_process, type_flags); - result.IsError()) { - return result; - } - - thread->name = name; - - auto& scheduler = kernel.GlobalSchedulerContext(); - scheduler.AddThread(thread); - - return MakeResult>(std::move(thread)); -} - -ResultVal> KThread::CreateThread( - Core::System& system, ThreadType type_flags, std::string name, VAddr entry_point, u32 priority, - u64 arg, s32 processor_id, VAddr stack_top, Process* owner_process, - std::function&& thread_start_func, void* thread_start_parameter) { - auto thread_result = CreateThread(system, type_flags, name, entry_point, priority, arg, - processor_id, stack_top, owner_process); - - if (thread_result.Succeeded()) { - (*thread_result)->host_context = - std::make_shared(std::move(thread_start_func), thread_start_parameter); - } - - return thread_result; -} - -ResultVal> KThread::CreateUserThread( - Core::System& system, ThreadType type_flags, std::string name, VAddr entry_point, u32 priority, - u64 arg, s32 processor_id, VAddr stack_top, Process* owner_process) { - std::function init_func = Core::CpuManager::GetGuestThreadStartFunc(); - - void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater(); - - return CreateThread(system, type_flags, name, entry_point, priority, arg, processor_id, - stack_top, owner_process, std::move(init_func), init_func_parameter); -} - KThread* GetCurrentThreadPointer(KernelCore& kernel) { return kernel.GetCurrentEmuThread(); } diff --git a/src/core/hle/kernel/k_thread.h b/src/core/hle/kernel/k_thread.h index b442dfe57..5c1c17d48 100644 --- a/src/core/hle/kernel/k_thread.h +++ b/src/core/hle/kernel/k_thread.h @@ -20,6 +20,7 @@ #include "core/hle/kernel/k_spin_lock.h" #include "core/hle/kernel/k_synchronization_object.h" #include "core/hle/kernel/object.h" +#include "core/hle/kernel/slab_helpers.h" #include "core/hle/kernel/svc_common.h" #include "core/hle/kernel/svc_types.h" #include "core/hle/result.h" @@ -99,7 +100,11 @@ enum class ThreadWaitReasonForDebugging : u32 { [[nodiscard]] KThread& GetCurrentThread(KernelCore& kernel); [[nodiscard]] s32 GetCurrentCoreId(KernelCore& kernel); -class KThread final : public KSynchronizationObject, public boost::intrusive::list_base_hook<> { +class KThread final : public KAutoObjectWithSlabHeapAndContainer, + public boost::intrusive::list_base_hook<> { + KERNEL_AUTOOBJECT_TRAITS(KThread, KSynchronizationObject); + +private: friend class KScheduler; friend class Process; @@ -115,57 +120,6 @@ public: using ThreadContext64 = Core::ARM_Interface::ThreadContext64; using WaiterList = boost::intrusive::list; - /** - * Creates and returns a new thread. - * @param system The instance of the whole system - * @param name The friendly name desired for the thread - * @param entry_point The address at which the thread should start execution - * @param priority The thread's priority - * @param arg User data to pass to the thread - * @param processor_id The ID(s) of the processors on which the thread is desired to be run - * @param stack_top The address of the thread's stack top - * @param owner_process The parent process for the thread, if null, it's a kernel thread - * @return A shared pointer to the newly created thread - */ - [[nodiscard]] static ResultVal> CreateThread( - Core::System& system, ThreadType type_flags, std::string name, VAddr entry_point, - u32 priority, u64 arg, s32 processor_id, VAddr stack_top, Process* owner_process); - - /** - * Creates and returns a new thread, with a specified entry point. - * @param system The instance of the whole system - * @param name The friendly name desired for the thread - * @param entry_point The address at which the thread should start execution - * @param priority The thread's priority - * @param arg User data to pass to the thread - * @param processor_id The ID(s) of the processors on which the thread is desired to be run - * @param stack_top The address of the thread's stack top - * @param owner_process The parent process for the thread, if null, it's a kernel thread - * @param thread_start_func The function where the host context will start. - * @param thread_start_parameter The parameter which will passed to host context on init - * @return A shared pointer to the newly created thread - */ - [[nodiscard]] static ResultVal> CreateThread( - Core::System& system, ThreadType type_flags, std::string name, VAddr entry_point, - u32 priority, u64 arg, s32 processor_id, VAddr stack_top, Process* owner_process, - std::function&& thread_start_func, void* thread_start_parameter); - - /** - * Creates and returns a new thread for the emulated "user" process. - * @param system The instance of the whole system - * @param name The friendly name desired for the thread - * @param entry_point The address at which the thread should start execution - * @param priority The thread's priority - * @param arg User data to pass to the thread - * @param processor_id The ID(s) of the processors on which the thread is desired to be run - * @param stack_top The address of the thread's stack top - * @param owner_process The parent process for the thread, if null, it's a kernel thread - * @return A shared pointer to the newly created thread - */ - [[nodiscard]] static ResultVal> CreateUserThread( - Core::System& system, ThreadType type_flags, std::string name, VAddr entry_point, - u32 priority, u64 arg, s32 processor_id, VAddr stack_top, Process* owner_process); - [[nodiscard]] std::string GetName() const override { return name; } @@ -257,10 +211,6 @@ public: void Suspend(); - void Finalize() override; - - bool IsSignaled() const override; - void SetSyncedObject(KSynchronizationObject* obj, ResultCode wait_res) { synced_object = obj; wait_result = wait_res; @@ -422,6 +372,40 @@ public: return termination_requested || GetRawState() == ThreadState::Terminated; } + [[nodiscard]] virtual u64 GetId() const override final { + return this->GetThreadID(); + } + + [[nodiscard]] virtual bool IsInitialized() const override { + return initialized; + } + + [[nodiscard]] virtual uintptr_t GetPostDestroyArgument() const override { + return reinterpret_cast(parent) | (resource_limit_release_hint ? 1 : 0); + } + + virtual void Finalize() override; + + [[nodiscard]] virtual bool IsSignaled() const override; + + static void PostDestroy(uintptr_t arg); + + [[nodiscard]] static ResultCode InitializeDummyThread(KThread* thread); + + [[nodiscard]] static ResultCode InitializeIdleThread(Core::System& system, KThread* thread, + s32 virt_core); + + [[nodiscard]] static ResultCode InitializeHighPriorityThread(Core::System& system, + KThread* thread, + KThreadFunction func, + uintptr_t arg, s32 virt_core); + + [[nodiscard]] static ResultCode InitializeUserThread(Core::System& system, KThread* thread, + KThreadFunction func, uintptr_t arg, + VAddr user_stack_top, s32 prio, + s32 virt_core, Process* owner); + +public: struct StackParameters { u8 svc_permission[0x10]; std::atomic dpc_flags; @@ -675,7 +659,9 @@ private: [[nodiscard]] static ResultCode InitializeThread(KThread* thread, KThreadFunction func, uintptr_t arg, VAddr user_stack_top, s32 prio, - s32 core, Process* owner, ThreadType type); + s32 core, Process* owner, ThreadType type, + std::function&& init_func, + void* init_func_parameter); static void RestorePriority(KernelCore& kernel, KThread* thread); From 0eeecde67c89cf33521711ae61d567b2f43ff80a Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 2 Apr 2021 23:25:32 -0700 Subject: [PATCH 14/82] hle: kernel: Migrate idle threads. --- src/core/hle/kernel/k_scheduler.cpp | 16 ++++++---------- src/core/hle/kernel/k_scheduler.h | 6 +++--- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index d1df97305..c048d86a3 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -623,7 +623,7 @@ KThread* KScheduler::GetCurrentThread() const { if (auto result = current_thread.load(); result) { return result; } - return idle_thread; + return idle_thread.get(); } u64 KScheduler::GetLastContextSwitchTicks() const { @@ -708,7 +708,7 @@ void KScheduler::ScheduleImpl() { // We never want to schedule a null thread, so use the idle thread if we don't have a next. if (next_thread == nullptr) { - next_thread = idle_thread; + next_thread = idle_thread.get(); } // If we're not actually switching thread, there's nothing to do. @@ -769,7 +769,7 @@ void KScheduler::SwitchToCurrent() { break; } } - auto thread = next_thread ? next_thread : idle_thread; + auto thread = next_thread ? next_thread : idle_thread.get(); Common::Fiber::YieldTo(switch_fiber, *thread->GetHostContext()); } while (!is_switch_pending()); } @@ -792,13 +792,9 @@ void KScheduler::UpdateLastContextSwitchTime(KThread* thread, Process* process) } void KScheduler::Initialize() { - std::string name = "Idle Thread Id:" + std::to_string(core_id); - std::function init_func = Core::CpuManager::GetIdleThreadStartFunc(); - void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater(); - auto thread_res = KThread::CreateThread( - system, ThreadType::Main, name, 0, KThread::IdleThreadPriority, 0, - static_cast(core_id), 0, nullptr, std::move(init_func), init_func_parameter); - idle_thread = thread_res.Unwrap().get(); + idle_thread = std::make_unique(system.Kernel()); + ASSERT(KThread::InitializeIdleThread(system, idle_thread.get(), core_id).IsSuccess()); + idle_thread->SetName(fmt::format("IdleThread:{}", core_id)); } KScopedSchedulerLock::KScopedSchedulerLock(KernelCore& kernel) diff --git a/src/core/hle/kernel/k_scheduler.h b/src/core/hle/kernel/k_scheduler.h index 8e32865aa..8cb5f6f36 100644 --- a/src/core/hle/kernel/k_scheduler.h +++ b/src/core/hle/kernel/k_scheduler.h @@ -51,7 +51,7 @@ public: /// Returns true if the scheduler is idle [[nodiscard]] bool IsIdle() const { - return GetCurrentThread() == idle_thread; + return GetCurrentThread() == idle_thread.get(); } /// Gets the timestamp for the last context switch in ticks. @@ -173,12 +173,12 @@ private: KThread* prev_thread{}; std::atomic current_thread{}; - KThread* idle_thread; + std::unique_ptr idle_thread; std::shared_ptr switch_fiber{}; struct SchedulingState { - std::atomic needs_scheduling; + std::atomic needs_scheduling{}; bool interrupt_task_thread_runnable{}; bool should_count_idle{}; u64 idle_count{}; From de4746ff69d595645bb0511d857829ca6bcc25d5 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 2 Apr 2021 23:50:39 -0700 Subject: [PATCH 15/82] hle: kernel: svc: Migrate CreateThread. --- src/core/hle/kernel/svc.cpp | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index bebb86154..036603315 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -1521,6 +1521,7 @@ static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr e return ResultInvalidPriority; } + // Reserve a new thread from the process resource limit (waiting up to 100ms). KScopedResourceReservation thread_reservation( kernel.CurrentProcess(), LimitableResource::Threads, 1, system.CoreTiming().GetGlobalTimeNs().count() + 100000000); @@ -1529,27 +1530,33 @@ static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr e return ResultResourceLimitedExceeded; } - std::shared_ptr thread; + // Create the thread. + KThread* thread = KThread::CreateWithKernel(kernel); + if (!thread) { + LOG_ERROR(Kernel_SVC, "Unable to create new threads. Thread creation limit reached."); + return ResultOutOfResource; + } + SCOPE_EXIT({ thread->Close(); }); + + // Initialize the thread. { KScopedLightLock lk{process.GetStateLock()}; - CASCADE_RESULT(thread, - KThread::CreateUserThread(system, ThreadType::User, "", entry_point, - priority, arg, core_id, stack_bottom, &process)); + R_TRY(KThread::InitializeUserThread(system, thread, entry_point, arg, stack_bottom, + priority, core_id, &process)); } - const auto new_thread_handle = process.GetHandleTable().Create(thread); - if (new_thread_handle.Failed()) { - LOG_ERROR(Kernel_SVC, "Failed to create handle with error=0x{:X}", - new_thread_handle.Code().raw); - return new_thread_handle.Code(); - } - *out_handle = *new_thread_handle; - // Set the thread name for debugging purposes. - thread->SetName( - fmt::format("thread[entry_point={:X}, handle={:X}]", entry_point, *new_thread_handle)); + thread->SetName(fmt::format("thread[entry_point={:X}, handle={:X}]", entry_point, *out_handle)); + + // Commit the thread reservation. thread_reservation.Commit(); + // Register the new thread. + KThread::Register(thread); + + // Add the thread to the handle table. + R_TRY(process.GetHandleTable().Add(out_handle, thread)); + return RESULT_SUCCESS; } From 6fca1c82fd98b284cef2fb45fbaeae0cbc6241ac Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 2 Apr 2021 23:53:31 -0700 Subject: [PATCH 16/82] hle: kernel: svc: Migrate GetThreadPriority, StartThread, and ExitThread. --- src/core/hle/kernel/svc.cpp | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 036603315..395962885 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -1150,12 +1150,9 @@ static ResultCode GetThreadPriority(Core::System& system, u32* out_priority, Han LOG_TRACE(Kernel_SVC, "called"); // Get the thread from its handle. - const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - const std::shared_ptr thread = handle_table.Get(handle); - if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle provided (handle={:08X})", handle); - return ResultInvalidHandle; - } + KScopedAutoObject thread = + system.Kernel().CurrentProcess()->GetHandleTable().GetObject(handle); + R_UNLESS(thread.IsNotNull(), ResultInvalidHandle); // Get the thread's priority. *out_priority = thread->GetPriority(); @@ -1552,7 +1549,7 @@ static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr e thread_reservation.Commit(); // Register the new thread. - KThread::Register(thread); + KThread::Register(kernel, thread); // Add the thread to the handle table. R_TRY(process.GetHandleTable().Add(out_handle, thread)); @@ -1570,21 +1567,15 @@ static ResultCode StartThread(Core::System& system, Handle thread_handle) { LOG_DEBUG(Kernel_SVC, "called thread=0x{:08X}", thread_handle); // Get the thread from its handle. - const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - const std::shared_ptr thread = handle_table.Get(thread_handle); - if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle provided (handle={:08X})", thread_handle); - return ResultInvalidHandle; - } + KScopedAutoObject thread = + system.Kernel().CurrentProcess()->GetHandleTable().GetObject(thread_handle); + R_UNLESS(thread.IsNotNull(), ResultInvalidHandle); // Try to start the thread. - const auto run_result = thread->Run(); - if (run_result.IsError()) { - LOG_ERROR(Kernel_SVC, - "Unable to successfuly start thread (thread handle={:08X}, result={})", - thread_handle, run_result.raw); - return run_result; - } + R_TRY(thread->Run()); + + // If we succeeded, persist a reference to the thread. + thread->Open(); return RESULT_SUCCESS; } @@ -1598,7 +1589,7 @@ static void ExitThread(Core::System& system) { LOG_DEBUG(Kernel_SVC, "called, pc=0x{:08X}", system.CurrentArmInterface().GetPC()); auto* const current_thread = system.Kernel().CurrentScheduler()->GetCurrentThread(); - system.GlobalSchedulerContext().RemoveThread(SharedFrom(current_thread)); + system.GlobalSchedulerContext().RemoveThread(current_thread); current_thread->Exit(); } From da7e9553dea4b1eaefb71aca8642ccce7c7f50fb Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 3 Apr 2021 19:11:46 -0700 Subject: [PATCH 17/82] hle: kernel: Migrate more of KThread to KAutoObject. --- src/core/CMakeLists.txt | 2 + src/core/hle/kernel/handle_table.cpp | 48 +++- src/core/hle/kernel/handle_table.h | 40 ++- src/core/hle/kernel/hle_ipc.h | 4 +- src/core/hle/kernel/k_auto_object.h | 28 +- src/core/hle/kernel/k_class_token.cpp | 7 + src/core/hle/kernel/k_class_token.h | 131 +++++++++ src/core/hle/kernel/k_condition_variable.cpp | 30 +- src/core/hle/kernel/k_slab_heap.h | 8 + .../hle/kernel/k_synchronization_object.cpp | 10 +- .../hle/kernel/k_synchronization_object.h | 11 +- src/core/hle/kernel/kernel.cpp | 72 +++-- src/core/hle/kernel/kernel.h | 11 +- src/core/hle/kernel/process.cpp | 15 +- src/core/hle/kernel/slab_helpers.h | 32 ++- src/core/hle/kernel/svc.cpp | 268 +++++------------- src/core/hle/kernel/time_manager.cpp | 16 +- src/yuzu/debugger/wait_tree.cpp | 12 +- 18 files changed, 451 insertions(+), 294 deletions(-) create mode 100644 src/core/hle/kernel/k_class_token.cpp create mode 100644 src/core/hle/kernel/k_class_token.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index d149e2ba6..569b4802e 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -168,6 +168,8 @@ add_library(core STATIC hle/kernel/k_auto_object_container.cpp hle/kernel/k_auto_object_container.h hle/kernel/k_affinity_mask.h + hle/kernel/k_class_token.cpp + hle/kernel/k_class_token.h hle/kernel/k_condition_variable.cpp hle/kernel/k_condition_variable.h hle/kernel/k_event.cpp diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index f96d34078..8eec8a3b5 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -72,6 +72,33 @@ ResultVal HandleTable::Create(std::shared_ptr obj) { return MakeResult(handle); } +ResultCode HandleTable::Add(Handle* out_handle, KAutoObject* obj, u16 type) { + ASSERT(obj != nullptr); + + const u16 slot = next_free_slot; + if (slot >= table_size) { + LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use."); + return ResultHandleTableFull; + } + next_free_slot = generations[slot]; + + const u16 generation = next_generation++; + + // Overflow count so it fits in the 15 bits dedicated to the generation in the handle. + // Horizon OS uses zero to represent an invalid handle, so skip to 1. + if (next_generation >= (1 << 15)) { + next_generation = 1; + } + + generations[slot] = generation; + objects_new[slot] = obj; + obj->Open(); + + *out_handle = generation | (slot << 15); + + return RESULT_SUCCESS; +} + ResultVal HandleTable::Duplicate(Handle handle) { std::shared_ptr object = GetGeneric(handle); if (object == nullptr) { @@ -81,30 +108,36 @@ ResultVal HandleTable::Duplicate(Handle handle) { return Create(std::move(object)); } -ResultCode HandleTable::Close(Handle handle) { +bool HandleTable::Remove(Handle handle) { if (!IsValid(handle)) { LOG_ERROR(Kernel, "Handle is not valid! handle={:08X}", handle); - return ResultInvalidHandle; + return {}; } const u16 slot = GetSlot(handle); - if (objects[slot].use_count() == 1) { - objects[slot]->Finalize(); + if (objects[slot]) { + objects[slot]->Close(); + } + + if (objects_new[slot]) { + objects_new[slot]->Close(); } objects[slot] = nullptr; + objects_new[slot] = nullptr; generations[slot] = next_free_slot; next_free_slot = slot; - return RESULT_SUCCESS; + + return true; } bool HandleTable::IsValid(Handle handle) const { const std::size_t slot = GetSlot(handle); const u16 generation = GetGeneration(handle); - - return slot < table_size && objects[slot] != nullptr && generations[slot] == generation; + const bool is_object_valid = (objects[slot] != nullptr) || (objects_new[slot] != nullptr); + return slot < table_size && is_object_valid && generations[slot] == generation; } std::shared_ptr HandleTable::GetGeneric(Handle handle) const { @@ -124,6 +157,7 @@ void HandleTable::Clear() { for (u16 i = 0; i < table_size; ++i) { generations[i] = static_cast(i + 1); objects[i] = nullptr; + objects_new[i] = nullptr; } next_free_slot = 0; } diff --git a/src/core/hle/kernel/handle_table.h b/src/core/hle/kernel/handle_table.h index c9dab8cdd..555fb20e5 100644 --- a/src/core/hle/kernel/handle_table.h +++ b/src/core/hle/kernel/handle_table.h @@ -9,6 +9,8 @@ #include #include "common/common_types.h" +#include "core/hle/kernel/k_auto_object.h" +#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/object.h" #include "core/hle/result.h" @@ -87,7 +89,7 @@ public: * @return `RESULT_SUCCESS` or one of the following errors: * - `ERR_INVALID_HANDLE`: an invalid handle was passed in. */ - ResultCode Close(Handle handle); + bool Remove(Handle handle); /// Checks if a handle is valid and points to an existing object. bool IsValid(Handle handle) const; @@ -108,12 +110,48 @@ public: return DynamicObjectCast(GetGeneric(handle)); } + template + KScopedAutoObject GetObject(Handle handle) const { + if (handle == CurrentThread) { + return kernel.CurrentScheduler()->GetCurrentThread()->DynamicCast(); + } else if (handle == CurrentProcess) { + return kernel.CurrentProcess()->DynamicCast(); + } + + if (!IsValid(handle)) { + return nullptr; + } + + auto* obj = objects_new[static_cast(handle >> 15)]; + return obj->DynamicCast(); + } + + template + KScopedAutoObject GetObjectWithoutPseudoHandle(Handle handle) const { + if (!IsValid(handle)) { + return nullptr; + } + auto* obj = objects_new[static_cast(handle >> 15)]; + return obj->DynamicCast(); + } + /// Closes all handles held in this table. void Clear(); + // NEW IMPL + + template + ResultCode Add(Handle* out_handle, T* obj) { + static_assert(std::is_base_of::value); + return this->Add(out_handle, obj, obj->GetTypeObj().GetClassToken()); + } + + ResultCode Add(Handle* out_handle, KAutoObject* obj, u16 type); + private: /// Stores the Object referenced by the handle or null if the slot is empty. std::array, MAX_COUNT> objects; + std::array objects_new{}; /** * The value of `next_generation` when the handle was created, used to check for validity. For diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index 75617bff0..398f1c467 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -291,8 +291,8 @@ private: // TODO(yuriks): Check common usage of this and optimize size accordingly boost::container::small_vector move_handles; boost::container::small_vector copy_handles; - boost::container::small_vector, 8> move_objects; - boost::container::small_vector, 8> copy_objects; + boost::container::small_vector move_objects; + boost::container::small_vector copy_objects; boost::container::small_vector, 8> domain_objects; std::optional command_header; diff --git a/src/core/hle/kernel/k_auto_object.h b/src/core/hle/kernel/k_auto_object.h index 567dad204..64c012d44 100644 --- a/src/core/hle/kernel/k_auto_object.h +++ b/src/core/hle/kernel/k_auto_object.h @@ -11,9 +11,11 @@ #include "common/common_types.h" #include "common/intrusive_red_black_tree.h" #include "core/hle/kernel/k_class_token.h" +#include "core/hle/kernel/object.h" namespace Kernel { +class KernelCore; class Process; #define KERNEL_AUTOOBJECT_TRAITS(CLASS, BASE_CLASS) \ @@ -46,7 +48,7 @@ public: \ private: -class KAutoObject { +class KAutoObject : public Object { protected: class TypeObj { private: @@ -84,11 +86,14 @@ private: private: std::atomic m_ref_count; +protected: + KernelCore& kernel; + public: static KAutoObject* Create(KAutoObject* ptr); public: - constexpr explicit KAutoObject() : m_ref_count(0) {} + explicit KAutoObject(KernelCore& kernel_) : Object{kernel_}, m_ref_count(0), kernel(kernel_) {} virtual ~KAutoObject() {} // Destroy is responsible for destroying the auto object's resources when ref_count hits zero. @@ -97,9 +102,7 @@ public: } // Finalize is responsible for cleaning up resource, but does not destroy the object. - virtual void Finalize() { - UNIMPLEMENTED(); - } + virtual void Finalize() {} virtual Process* GetOwner() const { return nullptr; @@ -179,7 +182,12 @@ private: private: Common::IntrusiveRedBlackTreeNode list_node; +protected: + KernelCore& kernel; + public: + explicit KAutoObjectWithList(KernelCore& kernel_) : KAutoObject(kernel_), kernel(kernel_) {} + static int Compare(const KAutoObjectWithList& lhs, const KAutoObjectWithList& rhs) { const u64 lid = lhs.GetId(); const u64 rid = rhs.GetId(); @@ -208,7 +216,7 @@ private: friend class KScopedAutoObject; private: - T* m_obj; + T* m_obj{}; private: constexpr void Swap(KScopedAutoObject& rhs) { @@ -216,8 +224,8 @@ private: } public: - constexpr KScopedAutoObject() : m_obj(nullptr) { // ... - } + constexpr KScopedAutoObject() = default; + constexpr KScopedAutoObject(T* o) : m_obj(o) { if (m_obj != nullptr) { m_obj->Open(); @@ -273,6 +281,10 @@ public: return m_obj; } + constexpr T* GetPointerUnsafe() const { + return m_obj; + } + constexpr T* ReleasePointerUnsafe() { T* ret = m_obj; m_obj = nullptr; diff --git a/src/core/hle/kernel/k_class_token.cpp b/src/core/hle/kernel/k_class_token.cpp new file mode 100644 index 000000000..ec2f8ccad --- /dev/null +++ b/src/core/hle/kernel/k_class_token.cpp @@ -0,0 +1,7 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/hle/kernel/k_class_token.h" + +namespace Kernel {} // namespace Kernel diff --git a/src/core/hle/kernel/k_class_token.h b/src/core/hle/kernel/k_class_token.h new file mode 100644 index 000000000..89b80a341 --- /dev/null +++ b/src/core/hle/kernel/k_class_token.h @@ -0,0 +1,131 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include + +#include "common/assert.h" +#include "common/bit_util.h" +#include "common/common_types.h" + +namespace Kernel { + +class KAutoObject; + +class KClassTokenGenerator { +public: + using TokenBaseType = u16; + +public: + static constexpr size_t BaseClassBits = 8; + static constexpr size_t FinalClassBits = (sizeof(TokenBaseType) * CHAR_BIT) - BaseClassBits; + // One bit per base class. + static constexpr size_t NumBaseClasses = BaseClassBits; + // Final classes are permutations of three bits. + static constexpr size_t NumFinalClasses = [] { + TokenBaseType index = 0; + for (size_t i = 0; i < FinalClassBits; i++) { + for (size_t j = i + 1; j < FinalClassBits; j++) { + for (size_t k = j + 1; k < FinalClassBits; k++) { + index++; + } + } + } + return index; + }(); + +private: + template + static constexpr inline TokenBaseType BaseClassToken = BIT(Index); + + template + static constexpr inline TokenBaseType FinalClassToken = [] { + TokenBaseType index = 0; + for (size_t i = 0; i < FinalClassBits; i++) { + for (size_t j = i + 1; j < FinalClassBits; j++) { + for (size_t k = j + 1; k < FinalClassBits; k++) { + if ((index++) == Index) { + return static_cast(((1ULL << i) | (1ULL << j) | (1ULL << k)) + << BaseClassBits); + } + } + } + } + }(); + + template + static constexpr inline TokenBaseType GetClassToken() { + static_assert(std::is_base_of::value); + if constexpr (std::is_same::value) { + static_assert(T::ObjectType == ObjectType::KAutoObject); + return 0; + } else if constexpr (!std::is_final::value) { + static_assert(ObjectType::BaseClassesStart <= T::ObjectType && + T::ObjectType < ObjectType::BaseClassesEnd); + constexpr auto ClassIndex = static_cast(T::ObjectType) - + static_cast(ObjectType::BaseClassesStart); + return BaseClassToken | GetClassToken(); + } else if constexpr (ObjectType::FinalClassesStart <= T::ObjectType && + T::ObjectType < ObjectType::FinalClassesEnd) { + constexpr auto ClassIndex = static_cast(T::ObjectType) - + static_cast(ObjectType::FinalClassesStart); + return FinalClassToken | GetClassToken(); + } else { + static_assert(!std::is_same::value, "GetClassToken: Invalid Type"); + } + }; + +public: + enum class ObjectType { + KAutoObject, + + BaseClassesStart, + + KSynchronizationObject = BaseClassesStart, + KReadableEvent, + + BaseClassesEnd, + + FinalClassesStart = BaseClassesEnd, + + KInterruptEvent = FinalClassesStart, + KDebug, + KThread, + KServerPort, + KServerSession, + KClientPort, + KClientSession, + Process, + KResourceLimit, + KLightSession, + KPort, + KSession, + KSharedMemory, + KEvent, + KWritableEvent, + KLightClientSession, + KLightServerSession, + KTransferMemory, + KDeviceAddressSpace, + KSessionRequest, + KCodeMemory, + + // NOTE: True order for these has not been determined yet. + KAlpha, + KBeta, + + FinalClassesEnd = FinalClassesStart + NumFinalClasses, + }; + + template + static constexpr inline TokenBaseType ClassToken = GetClassToken(); +}; + +using ClassTokenType = KClassTokenGenerator::TokenBaseType; + +template +static constexpr inline ClassTokenType ClassToken = KClassTokenGenerator::ClassToken; + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_condition_variable.cpp b/src/core/hle/kernel/k_condition_variable.cpp index 170d8fa0d..930f78974 100644 --- a/src/core/hle/kernel/k_condition_variable.cpp +++ b/src/core/hle/kernel/k_condition_variable.cpp @@ -7,6 +7,7 @@ #include "core/arm/exclusive_monitor.h" #include "core/core.h" #include "core/hle/kernel/k_condition_variable.h" +#include "core/hle/kernel/k_linked_list.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h" #include "core/hle/kernel/k_synchronization_object.h" @@ -107,8 +108,8 @@ ResultCode KConditionVariable::WaitForAddress(Handle handle, VAddr addr, u32 val // Wait for the address. { - std::shared_ptr owner_thread; - ASSERT(!owner_thread); + KScopedAutoObject owner_thread; + ASSERT(owner_thread.IsNull()); { KScopedSchedulerLock sl(kernel); cur_thread->SetSyncedObject(nullptr, RESULT_SUCCESS); @@ -126,8 +127,10 @@ ResultCode KConditionVariable::WaitForAddress(Handle handle, VAddr addr, u32 val R_UNLESS(test_tag == (handle | Svc::HandleWaitMask), RESULT_SUCCESS); // Get the lock owner thread. - owner_thread = kernel.CurrentProcess()->GetHandleTable().Get(handle); - R_UNLESS(owner_thread, ResultInvalidHandle); + owner_thread = + kernel.CurrentProcess()->GetHandleTable().GetObjectWithoutPseudoHandle( + handle); + R_UNLESS(owner_thread.IsNotNull(), ResultInvalidHandle); // Update the lock. cur_thread->SetAddressKey(addr, value); @@ -137,7 +140,7 @@ ResultCode KConditionVariable::WaitForAddress(Handle handle, VAddr addr, u32 val cur_thread->SetMutexWaitAddressForDebugging(addr); } } - ASSERT(owner_thread); + ASSERT(owner_thread.IsNotNull()); } // Remove the thread as a waiter from the lock owner. @@ -182,13 +185,16 @@ KThread* KConditionVariable::SignalImpl(KThread* thread) { thread->Wakeup(); } else { // Get the previous owner. - auto owner_thread = kernel.CurrentProcess()->GetHandleTable().Get( - prev_tag & ~Svc::HandleWaitMask); + KThread* owner_thread = + kernel.CurrentProcess()->GetHandleTable() + .GetObjectWithoutPseudoHandle( + static_cast(prev_tag & ~Svc::HandleWaitMask)) + .ReleasePointerUnsafe(); if (owner_thread) { // Add the thread as a waiter on the owner. owner_thread->AddWaiter(thread); - thread_to_close = owner_thread.get(); + thread_to_close = owner_thread; } else { // The lock was tagged with a thread that doesn't exist. thread->SetSyncedObject(nullptr, ResultInvalidState); @@ -208,9 +214,7 @@ void KConditionVariable::Signal(u64 cv_key, s32 count) { // Prepare for signaling. constexpr int MaxThreads = 16; - // TODO(bunnei): This should just be Thread once we implement KAutoObject instead of using - // std::shared_ptr. - std::vector> thread_list; + KLinkedList thread_list; std::array thread_array; s32 num_to_close{}; @@ -228,7 +232,7 @@ void KConditionVariable::Signal(u64 cv_key, s32 count) { if (num_to_close < MaxThreads) { thread_array[num_to_close++] = thread; } else { - thread_list.push_back(SharedFrom(thread)); + thread_list.push_back(*thread); } } @@ -251,7 +255,7 @@ void KConditionVariable::Signal(u64 cv_key, s32 count) { // Close threads in the list. for (auto it = thread_list.begin(); it != thread_list.end(); it = thread_list.erase(it)) { - (*it)->Close(); + (*it).Close(); } } diff --git a/src/core/hle/kernel/k_slab_heap.h b/src/core/hle/kernel/k_slab_heap.h index aa4471d2f..a3948cd27 100644 --- a/src/core/hle/kernel/k_slab_heap.h +++ b/src/core/hle/kernel/k_slab_heap.h @@ -148,6 +148,14 @@ public: return obj; } + T* AllocateWithKernel(KernelCore& kernel) { + T* obj = static_cast(AllocateImpl()); + if (obj != nullptr) { + new (obj) T(kernel); + } + return obj; + } + void Free(T* obj) { FreeImpl(obj); } diff --git a/src/core/hle/kernel/k_synchronization_object.cpp b/src/core/hle/kernel/k_synchronization_object.cpp index 82f72a0fe..460b8a714 100644 --- a/src/core/hle/kernel/k_synchronization_object.cpp +++ b/src/core/hle/kernel/k_synchronization_object.cpp @@ -13,6 +13,11 @@ namespace Kernel { +void KSynchronizationObject::Finalize() { + this->OnFinalizeSynchronizationObject(); + KAutoObject::Finalize(); +} + ResultCode KSynchronizationObject::Wait(KernelCore& kernel, s32* out_index, KSynchronizationObject** objects, const s32 num_objects, s64 timeout) { @@ -130,10 +135,7 @@ ResultCode KSynchronizationObject::Wait(KernelCore& kernel, s32* out_index, return wait_result; } -KSynchronizationObject::KSynchronizationObject(KernelCore& kernel) : Object{kernel} {} - -KSynchronizationObject::KSynchronizationObject(KernelCore& kernel, std::string&& name) - : Object{kernel, std::move(name)} {} +KSynchronizationObject::KSynchronizationObject(KernelCore& kernel) : KAutoObjectWithList{kernel} {} KSynchronizationObject::~KSynchronizationObject() = default; diff --git a/src/core/hle/kernel/k_synchronization_object.h b/src/core/hle/kernel/k_synchronization_object.h index 5803718fd..c8e840d89 100644 --- a/src/core/hle/kernel/k_synchronization_object.h +++ b/src/core/hle/kernel/k_synchronization_object.h @@ -6,7 +6,7 @@ #include -#include "core/hle/kernel/object.h" +#include "core/hle/kernel/k_auto_object.h" #include "core/hle/result.h" namespace Kernel { @@ -16,7 +16,9 @@ class Synchronization; class KThread; /// Class that represents a Kernel object that a thread can be waiting on -class KSynchronizationObject : public Object { +class KSynchronizationObject : public KAutoObjectWithList { + KERNEL_AUTOOBJECT_TRAITS(KSynchronizationObject, KAutoObject); + public: struct ThreadListNode { ThreadListNode* next{}; @@ -27,15 +29,18 @@ public: KSynchronizationObject** objects, const s32 num_objects, s64 timeout); + virtual void Finalize() override; + [[nodiscard]] virtual bool IsSignaled() const = 0; [[nodiscard]] std::vector GetWaitingThreadsForDebugging() const; protected: explicit KSynchronizationObject(KernelCore& kernel); - explicit KSynchronizationObject(KernelCore& kernel, std::string&& name); virtual ~KSynchronizationObject(); + virtual void OnFinalizeSynchronizationObject() {} + void NotifyAvailable(ResultCode result); void NotifyAvailable() { return this->NotifyAvailable(RESULT_SUCCESS); diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 17fe1d59f..d1359e434 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -28,6 +28,7 @@ #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_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" #include "core/hle/kernel/k_resource_limit.h" @@ -51,7 +52,8 @@ namespace Kernel { struct KernelCore::Impl { explicit Impl(Core::System& system, KernelCore& kernel) - : time_manager{system}, global_handle_table{kernel}, system{system} {} + : time_manager{system}, global_handle_table{kernel}, + object_list_container{kernel}, system{system} {} void SetMulticore(bool is_multicore) { this->is_multicore = is_multicore; @@ -69,9 +71,12 @@ struct KernelCore::Impl { // Derive the initial memory layout from the emulated board KMemoryLayout memory_layout; DeriveInitialMemoryLayout(memory_layout); + Init::InitializeSlabHeaps(system, memory_layout); + + // Initialize kernel memory and resources. InitializeMemoryLayout(memory_layout); InitializeSystemResourceLimit(kernel, system.CoreTiming(), memory_layout); - InitializeSlabHeaps(); + InitializePageSlab(); InitializeSchedulers(); InitializeSuspendThreads(); InitializePreemption(kernel); @@ -99,7 +104,7 @@ struct KernelCore::Impl { for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) { if (suspend_threads[i]) { - suspend_threads[i].reset(); + suspend_threads[i]->Close(); } } @@ -189,15 +194,12 @@ struct KernelCore::Impl { } void InitializeSuspendThreads() { - for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) { - std::string name = "Suspend Thread Id:" + std::to_string(i); - std::function init_func = Core::CpuManager::GetSuspendThreadStartFunc(); - void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater(); - auto thread_res = KThread::CreateThread( - system, ThreadType::HighPriority, std::move(name), 0, 0, 0, static_cast(i), 0, - nullptr, std::move(init_func), init_func_parameter); - - suspend_threads[i] = std::move(thread_res).Unwrap(); + for (s32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { + suspend_threads[core_id] = KThread::CreateWithKernel(system.Kernel()); + ASSERT(KThread::InitializeHighPriorityThread(system, suspend_threads[core_id], {}, {}, + core_id) + .IsSuccess()); + suspend_threads[core_id]->SetName(fmt::format("SuspendThread:{}", core_id)); } } @@ -232,12 +234,15 @@ struct KernelCore::Impl { // Gets the dummy KThread for the caller, allocating a new one if this is the first time KThread* GetHostDummyThread() { - const thread_local auto thread = - KThread::CreateThread( - system, ThreadType::Main, fmt::format("DummyThread:{}", GetHostThreadId()), 0, - KThread::DefaultThreadPriority, 0, static_cast(3), 0, nullptr) - .Unwrap(); - return thread.get(); + auto make_thread = [this]() { + KThread* thread = KThread::CreateWithKernel(system.Kernel()); + ASSERT(KThread::InitializeDummyThread(thread).IsSuccess()); + thread->SetName(fmt::format("DummyThread:{}", GetHostThreadId())); + return thread; + }; + + thread_local auto thread = make_thread(); + return thread; } /// Registers a CPU core thread by allocating a host thread ID for it @@ -371,7 +376,8 @@ struct KernelCore::Impl { const size_t resource_region_size = memory_layout.GetResourceRegionSizeForInit(); // Determine the size of the slab region. - const size_t slab_region_size = Common::AlignUp(KernelSlabHeapSize, PageSize); + const size_t slab_region_size = + Common::AlignUp(Init::CalculateTotalSlabHeapSize(), PageSize); ASSERT(slab_region_size <= resource_region_size); // Setup the slab region. @@ -587,7 +593,7 @@ struct KernelCore::Impl { "Time:SharedMemory"); } - void InitializeSlabHeaps() { + void InitializePageSlab() { // Allocate slab heaps user_slab_heap_pages = std::make_unique>(); @@ -596,7 +602,7 @@ struct KernelCore::Impl { // Reserve slab heaps ASSERT( system_resource_limit->Reserve(LimitableResource::PhysicalMemory, user_slab_heap_size)); - // Initialize slab heaps + // Initialize slab heap user_slab_heap_pages->Initialize( system.DeviceMemory().GetPointer(Core::DramMemoryMap::SlabHeapBase), user_slab_heap_size); @@ -621,6 +627,8 @@ struct KernelCore::Impl { // stores all the objects in place. HandleTable global_handle_table; + KAutoObjectWithListContainer object_list_container; + /// Map of named ports managed by the kernel, which can be retrieved using /// the ConnectToPort SVC. NamedPortTable named_ports; @@ -648,7 +656,7 @@ struct KernelCore::Impl { // the release of itself std::unique_ptr service_thread_manager; - std::array, Core::Hardware::NUM_CPU_CORES> suspend_threads{}; + std::array suspend_threads{}; std::array interrupts{}; std::array, Core::Hardware::NUM_CPU_CORES> schedulers{}; @@ -687,8 +695,8 @@ std::shared_ptr KernelCore::GetSystemResourceLimit() const { return impl->system_resource_limit; } -std::shared_ptr KernelCore::RetrieveThreadFromGlobalHandleTable(Handle handle) const { - return impl->global_handle_table.Get(handle); +KScopedAutoObject KernelCore::RetrieveThreadFromGlobalHandleTable(Handle handle) const { + return impl->global_handle_table.GetObject(handle); } void KernelCore::AppendNewProcess(std::shared_ptr process) { @@ -781,6 +789,14 @@ const Core::ExclusiveMonitor& KernelCore::GetExclusiveMonitor() const { return *impl->exclusive_monitor; } +KAutoObjectWithListContainer& KernelCore::ObjectListContainer() { + return impl->object_list_container; +} + +const KAutoObjectWithListContainer& KernelCore::ObjectListContainer() const { + return impl->object_list_container; +} + void KernelCore::InvalidateAllInstructionCaches() { for (auto& physical_core : impl->cores) { physical_core.ArmInterface().ClearInstructionCache(); @@ -960,4 +976,12 @@ void KernelCore::SetIsPhantomModeForSingleCore(bool value) { impl->SetIsPhantomModeForSingleCore(value); } +Core::System& KernelCore::System() { + return impl->system; +} + +const Core::System& KernelCore::System() const { + return impl->system; +} + } // namespace Kernel diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index a500e63bc..3f5c2aec7 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -13,6 +13,7 @@ #include "core/hardware_properties.h" #include "core/hle/kernel/memory_types.h" #include "core/hle/kernel/object.h" +#include "core/hle/kernel/k_auto_object.h" namespace Core { class CPUInterruptHandler; @@ -30,6 +31,7 @@ namespace Kernel { class ClientPort; class GlobalSchedulerContext; class HandleTable; +class KAutoObjectWithListContainer; class KMemoryManager; class KResourceLimit; class KScheduler; @@ -86,7 +88,7 @@ public: std::shared_ptr GetSystemResourceLimit() const; /// Retrieves a shared pointer to a Thread instance within the thread wakeup handle table. - std::shared_ptr RetrieveThreadFromGlobalHandleTable(Handle handle) const; + KScopedAutoObject RetrieveThreadFromGlobalHandleTable(Handle handle) const; /// Adds the given shared pointer to an internal list of active processes. void AppendNewProcess(std::shared_ptr process); @@ -143,6 +145,10 @@ public: const Core::ExclusiveMonitor& GetExclusiveMonitor() const; + KAutoObjectWithListContainer& ObjectListContainer(); + + const KAutoObjectWithListContainer& ObjectListContainer() const; + std::array& Interrupts(); const std::array& Interrupts() const; @@ -243,6 +249,9 @@ public: bool IsPhantomModeForSingleCore() const; void SetIsPhantomModeForSingleCore(bool value); + Core::System& System(); + const Core::System& System() const; + private: friend class Object; friend class Process; diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index e35deb8e2..796dca5ef 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -40,14 +40,15 @@ namespace { void SetupMainThread(Core::System& system, Process& owner_process, u32 priority, VAddr stack_top) { const VAddr entry_point = owner_process.PageTable().GetCodeRegionStart(); ASSERT(owner_process.GetResourceLimit()->Reserve(LimitableResource::Threads, 1)); - auto thread_res = - KThread::CreateUserThread(system, ThreadType::User, "main", entry_point, priority, 0, - owner_process.GetIdealCoreId(), stack_top, &owner_process); - std::shared_ptr thread = std::move(thread_res).Unwrap(); + KThread* thread = KThread::CreateWithKernel(system.Kernel()); + ASSERT(KThread::InitializeUserThread(system, thread, entry_point, 0, stack_top, priority, + owner_process.GetIdealCoreId(), &owner_process) + .IsSuccess()); // Register 1 must be a handle to the main thread - const Handle thread_handle = owner_process.GetHandleTable().Create(thread).Unwrap(); + Handle thread_handle{}; + owner_process.GetHandleTable().Add(&thread_handle, thread); thread->GetContext32().cpu_registers[0] = 0; thread->GetContext64().cpu_registers[0] = 0; thread->GetContext32().cpu_registers[1] = thread_handle; @@ -337,12 +338,12 @@ void Process::Run(s32 main_thread_priority, u64 stack_size) { void Process::PrepareForTermination() { ChangeStatus(ProcessStatus::Exiting); - const auto stop_threads = [this](const std::vector>& thread_list) { + const auto stop_threads = [this](const std::vector& thread_list) { for (auto& thread : thread_list) { if (thread->GetOwnerProcess() != this) continue; - if (thread.get() == kernel.CurrentScheduler()->GetCurrentThread()) + if (thread == kernel.CurrentScheduler()->GetCurrentThread()) continue; // TODO(Subv): When are the other running/ready threads terminated? diff --git a/src/core/hle/kernel/slab_helpers.h b/src/core/hle/kernel/slab_helpers.h index d95095da3..ae9d097da 100644 --- a/src/core/hle/kernel/slab_helpers.h +++ b/src/core/hle/kernel/slab_helpers.h @@ -14,6 +14,7 @@ #include "core/hle/kernel/k_auto_object_container.h" #include "core/hle/kernel/k_light_lock.h" #include "core/hle/kernel/k_slab_heap.h" +#include "core/hle/kernel/kernel.h" namespace Kernel { @@ -66,13 +67,17 @@ class KAutoObjectWithSlabHeapAndContainer : public Base { private: static inline KSlabHeap s_slab_heap; - static inline KAutoObjectWithListContainer s_container; + KernelCore& m_kernel; private: static Derived* Allocate() { return s_slab_heap.Allocate(); } + static Derived* AllocateWithKernel(KernelCore& kernel) { + return s_slab_heap.AllocateWithKernel(kernel); + } + static void Free(Derived* obj) { s_slab_heap.Free(obj); } @@ -80,19 +85,20 @@ private: public: class ListAccessor : public KAutoObjectWithListContainer::ListAccessor { public: - ListAccessor() : KAutoObjectWithListContainer::ListAccessor(s_container) {} + ListAccessor() + : KAutoObjectWithListContainer::ListAccessor(m_kernel.ObjectListContainer()) {} ~ListAccessor() = default; }; public: - constexpr KAutoObjectWithSlabHeapAndContainer() : Base() {} + KAutoObjectWithSlabHeapAndContainer(KernelCore& kernel) : Base(kernel), m_kernel(kernel) {} virtual ~KAutoObjectWithSlabHeapAndContainer() {} virtual void Destroy() override { const bool is_initialized = this->IsInitialized(); uintptr_t arg = 0; if (is_initialized) { - s_container.Unregister(this); + m_kernel.ObjectListContainer().Unregister(this); arg = this->GetPostDestroyArgument(); this->Finalize(); } @@ -114,21 +120,29 @@ public: } public: - static void InitializeSlabHeap(void* memory, size_t memory_size) { + static void InitializeSlabHeap(KernelCore& kernel, void* memory, size_t memory_size) { s_slab_heap.Initialize(memory, memory_size); - s_container.Initialize(); + kernel.ObjectListContainer().Initialize(); } static Derived* Create() { Derived* obj = Allocate(); - if (AMS_LIKELY(obj != nullptr)) { + if (obj != nullptr) { KAutoObject::Create(obj); } return obj; } - static void Register(Derived* obj) { - return s_container.Register(obj); + static Derived* CreateWithKernel(KernelCore& kernel) { + Derived* obj = AllocateWithKernel(kernel); + if (obj != nullptr) { + KAutoObject::Create(obj); + } + return obj; + } + + static void Register(KernelCore& kernel, Derived* obj) { + return kernel.ObjectListContainer().Register(obj); } static size_t GetObjectSize() { diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 395962885..813450115 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -355,7 +355,7 @@ static ResultCode SendSyncRequest(Core::System& system, Handle handle) { KScopedSchedulerLock lock(kernel); thread->SetState(ThreadState::Waiting); thread->SetWaitReasonForDebugging(ThreadWaitReasonForDebugging::IPC); - session->SendSyncRequest(SharedFrom(thread), system.Memory(), system.CoreTiming()); + session->SendSyncRequest(thread, system.Memory(), system.CoreTiming()); } KSynchronizationObject* dummy{}; @@ -368,18 +368,13 @@ static ResultCode SendSyncRequest32(Core::System& system, Handle handle) { /// Get the ID for the specified thread. static ResultCode GetThreadId(Core::System& system, u64* out_thread_id, Handle thread_handle) { - LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle); - // Get the thread from its handle. - const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - const std::shared_ptr thread = handle_table.Get(thread_handle); - if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle provided (handle={:08X})", thread_handle); - return ResultInvalidHandle; - } + KScopedAutoObject thread = + system.Kernel().CurrentProcess()->GetHandleTable().GetObject(thread_handle); + R_UNLESS(thread.IsNotNull(), ResultInvalidHandle); // Get the thread's id. - *out_thread_id = thread->GetThreadID(); + *out_thread_id = thread->GetId(); return RESULT_SUCCESS; } @@ -396,30 +391,7 @@ static ResultCode GetThreadId32(Core::System& system, u32* out_thread_id_low, /// Gets the ID of the specified process or a specified thread's owning process. static ResultCode GetProcessId(Core::System& system, u64* process_id, Handle handle) { - LOG_DEBUG(Kernel_SVC, "called handle=0x{:08X}", handle); - - const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - const std::shared_ptr process = handle_table.Get(handle); - if (process) { - *process_id = process->GetProcessID(); - return RESULT_SUCCESS; - } - - const std::shared_ptr thread = handle_table.Get(handle); - if (thread) { - const Process* const owner_process = thread->GetOwnerProcess(); - if (!owner_process) { - LOG_ERROR(Kernel_SVC, "Non-existent owning process encountered."); - return ResultInvalidHandle; - } - - *process_id = owner_process->GetProcessID(); - return RESULT_SUCCESS; - } - - // NOTE: This should also handle debug objects before returning. - - LOG_ERROR(Kernel_SVC, "Handle does not exist, handle=0x{:08X}", handle); + __debugbreak(); return ResultInvalidHandle; } @@ -460,14 +432,30 @@ static ResultCode WaitSynchronization(Core::System& system, s32* index, VAddr ha for (u64 i = 0; i < handle_count; ++i) { const Handle handle = memory.Read32(handles_address + i * sizeof(Handle)); - const auto object = handle_table.Get(handle); - if (object == nullptr) { - LOG_ERROR(Kernel_SVC, "Object is a nullptr"); - return ResultInvalidHandle; + bool succeeded{}; + { + auto object = handle_table.Get(handle); + if (object) { + objects[i] = object.get(); + succeeded = true; + } } - objects[i] = object.get(); + // TODO(bunnei): WORKAROUND WHILE WE HAVE TWO HANDLE TABLES + if (!succeeded) { + { + auto object = handle_table.GetObject(handle); + + if (object.IsNull()) { + LOG_ERROR(Kernel_SVC, "Object is a nullptr"); + return ResultInvalidHandle; + } + + objects[i] = object.GetPointerUnsafe(); + succeeded = true; + } + } } return KSynchronizationObject::Wait(kernel, index, objects.data(), static_cast(objects.size()), nano_seconds); @@ -481,19 +469,7 @@ static ResultCode WaitSynchronization32(Core::System& system, u32 timeout_low, u /// Resumes a thread waiting on WaitSynchronization static ResultCode CancelSynchronization(Core::System& system, Handle thread_handle) { - LOG_TRACE(Kernel_SVC, "called thread=0x{:X}", thread_handle); - - // Get the thread from its handle. - const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - std::shared_ptr thread = handle_table.Get(thread_handle); - - if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle provided (handle={:08X})", thread_handle); - return ResultInvalidHandle; - } - - // Cancel the thread's wait. - thread->WaitCancel(); + __debugbreak(); return RESULT_SUCCESS; } @@ -899,9 +875,10 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, u64 ha return ResultInvalidCombination; } - const auto thread = system.Kernel().CurrentProcess()->GetHandleTable().Get( - static_cast(handle)); - if (!thread) { + KScopedAutoObject thread = + system.Kernel().CurrentProcess()->GetHandleTable().GetObject( + static_cast(handle)); + if (thread.IsNull()) { LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", static_cast(handle)); return ResultInvalidHandle; @@ -910,7 +887,7 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, u64 ha const auto& core_timing = system.CoreTiming(); const auto& scheduler = *system.Kernel().CurrentScheduler(); const auto* const current_thread = scheduler.GetCurrentThread(); - const bool same_thread = current_thread == thread.get(); + const bool same_thread = current_thread == thread.GetPointerUnsafe(); const u64 prev_ctx_ticks = scheduler.GetLastContextSwitchTicks(); u64 out_ticks = 0; @@ -1055,45 +1032,7 @@ static ResultCode UnmapPhysicalMemory32(Core::System& system, u32 addr, u32 size /// Sets the thread activity static ResultCode SetThreadActivity(Core::System& system, Handle thread_handle, ThreadActivity thread_activity) { - LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, activity=0x{:08X}", thread_handle, - thread_activity); - - // Validate the activity. - constexpr auto IsValidThreadActivity = [](ThreadActivity activity) { - return activity == ThreadActivity::Runnable || activity == ThreadActivity::Paused; - }; - if (!IsValidThreadActivity(thread_activity)) { - LOG_ERROR(Kernel_SVC, "Invalid thread activity value provided (activity={})", - thread_activity); - return ResultInvalidEnumValue; - } - - // Get the thread from its handle. - auto& kernel = system.Kernel(); - const auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); - const std::shared_ptr thread = handle_table.Get(thread_handle); - if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle provided (handle={:08X})", thread_handle); - return ResultInvalidHandle; - } - - // Check that the activity is being set on a non-current thread for the current process. - if (thread->GetOwnerProcess() != kernel.CurrentProcess()) { - LOG_ERROR(Kernel_SVC, "Invalid owning process for the created thread."); - return ResultInvalidHandle; - } - if (thread.get() == GetCurrentThreadPointer(kernel)) { - LOG_ERROR(Kernel_SVC, "Thread is busy"); - return ResultBusy; - } - - // Set the activity. - const auto set_result = thread->SetActivity(thread_activity); - if (set_result.IsError()) { - LOG_ERROR(Kernel_SVC, "Failed to set thread activity."); - return set_result; - } - + __debugbreak(); return RESULT_SUCCESS; } @@ -1107,36 +1046,7 @@ static ResultCode GetThreadContext(Core::System& system, VAddr out_context, Hand LOG_DEBUG(Kernel_SVC, "called, out_context=0x{:08X}, thread_handle=0x{:X}", out_context, thread_handle); - // Get the thread from its handle. - const auto* current_process = system.Kernel().CurrentProcess(); - const std::shared_ptr thread = - current_process->GetHandleTable().Get(thread_handle); - if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle provided (handle={})", thread_handle); - return ResultInvalidHandle; - } - - // Require the handle be to a non-current thread in the current process. - if (thread->GetOwnerProcess() != current_process) { - LOG_ERROR(Kernel_SVC, "Thread owning process is not the current process."); - return ResultInvalidHandle; - } - if (thread.get() == system.Kernel().CurrentScheduler()->GetCurrentThread()) { - LOG_ERROR(Kernel_SVC, "Current thread is busy."); - return ResultBusy; - } - - // Get the thread context. - std::vector context; - const auto context_result = thread->GetThreadContext3(context); - if (context_result.IsError()) { - LOG_ERROR(Kernel_SVC, "Unable to successfully retrieve thread context (result: {})", - context_result.raw); - return context_result; - } - - // Copy the thread context to user space. - system.Memory().WriteBlock(out_context, context.data(), context.size()); + __debugbreak(); return RESULT_SUCCESS; } @@ -1164,30 +1074,26 @@ static ResultCode GetThreadPriority32(Core::System& system, u32* out_priority, H } /// Sets the priority for the specified thread -static ResultCode SetThreadPriority(Core::System& system, Handle handle, u32 priority) { - LOG_TRACE(Kernel_SVC, "called"); +static ResultCode SetThreadPriority(Core::System& system, Handle thread_handle, u32 priority) { + // Get the current process. + Process& process = *system.Kernel().CurrentProcess(); // Validate the priority. - if (HighestThreadPriority > priority || priority > LowestThreadPriority) { - LOG_ERROR(Kernel_SVC, "Invalid thread priority specified (priority={})", priority); - return ResultInvalidPriority; - } + R_UNLESS(HighestThreadPriority <= priority && priority <= LowestThreadPriority, + ResultInvalidPriority); + R_UNLESS(process.CheckThreadPriority(priority), ResultInvalidPriority); // Get the thread from its handle. - const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - const std::shared_ptr thread = handle_table.Get(handle); - if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid handle provided (handle={:08X})", handle); - return ResultInvalidHandle; - } + KScopedAutoObject thread = process.GetHandleTable().GetObject(thread_handle); + R_UNLESS(thread.IsNotNull(), ResultInvalidHandle); // Set the thread priority. thread->SetBasePriority(priority); return RESULT_SUCCESS; } -static ResultCode SetThreadPriority32(Core::System& system, Handle handle, u32 priority) { - return SetThreadPriority(system, handle, priority); +static ResultCode SetThreadPriority32(Core::System& system, Handle thread_handle, u32 priority) { + return SetThreadPriority(system, thread_handle, priority); } /// Get which CPU core is executing the current thread @@ -1480,7 +1386,7 @@ static void ExitProcess32(Core::System& system) { ExitProcess(system); } -static constexpr bool IsValidCoreId(int32_t core_id) { +static constexpr bool IsValidVirtualCoreId(int32_t core_id) { return (0 <= core_id && core_id < static_cast(Core::Hardware::NUM_CPU_CORES)); } @@ -1500,7 +1406,7 @@ static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr e } // Validate arguments. - if (!IsValidCoreId(core_id)) { + if (!IsValidVirtualCoreId(core_id)) { LOG_ERROR(Kernel_SVC, "Invalid Core ID specified (id={})", core_id); return ResultInvalidCoreId; } @@ -1822,8 +1728,11 @@ static void GetSystemTick32(Core::System& system, u32* time_low, u32* time_high) static ResultCode CloseHandle(Core::System& system, Handle handle) { LOG_TRACE(Kernel_SVC, "Closing handle 0x{:08X}", handle); - auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - return handle_table.Close(handle); + // Remove the handle. + R_UNLESS(system.Kernel().CurrentProcess()->GetHandleTable().Remove(handle), + ResultInvalidHandle); + + return RESULT_SUCCESS; } static ResultCode CloseHandle32(Core::System& system, Handle handle) { @@ -1925,23 +1834,7 @@ static ResultCode CreateTransferMemory32(Core::System& system, Handle* handle, u static ResultCode GetThreadCoreMask(Core::System& system, Handle thread_handle, s32* out_core_id, u64* out_affinity_mask) { - LOG_TRACE(Kernel_SVC, "called, handle=0x{:08X}", thread_handle); - - // Get the thread from its handle. - const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - const std::shared_ptr thread = handle_table.Get(thread_handle); - if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle specified (handle={:08X})", thread_handle); - return ResultInvalidHandle; - } - - // Get the core mask. - const auto result = thread->GetCoreMask(out_core_id, out_affinity_mask); - if (result.IsError()) { - LOG_ERROR(Kernel_SVC, "Unable to successfully retrieve core mask (result={})", result.raw); - return result; - } - + __debugbreak(); return RESULT_SUCCESS; } @@ -1956,58 +1849,33 @@ static ResultCode GetThreadCoreMask32(Core::System& system, Handle thread_handle static ResultCode SetThreadCoreMask(Core::System& system, Handle thread_handle, s32 core_id, u64 affinity_mask) { - LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, core_id=0x{:X}, affinity_mask=0x{:016X}", - thread_handle, core_id, affinity_mask); - - const auto& current_process = *system.Kernel().CurrentProcess(); - // Determine the core id/affinity mask. - if (core_id == Svc::IdealCoreUseProcessValue) { - core_id = current_process.GetIdealCoreId(); + if (core_id == IdealCoreUseProcessValue) { + core_id = system.Kernel().CurrentProcess()->GetIdealCoreId(); affinity_mask = (1ULL << core_id); } else { // Validate the affinity mask. - const u64 process_core_mask = current_process.GetCoreMask(); - if ((affinity_mask | process_core_mask) != process_core_mask) { - LOG_ERROR(Kernel_SVC, - "Affinity mask does match the process core mask (affinity mask={:016X}, core " - "mask={:016X})", - affinity_mask, process_core_mask); - return ResultInvalidCoreId; - } - if (affinity_mask == 0) { - LOG_ERROR(Kernel_SVC, "Affinity mask is zero."); - return ResultInvalidCombination; - } + const u64 process_core_mask = system.Kernel().CurrentProcess()->GetCoreMask(); + R_UNLESS((affinity_mask | process_core_mask) == process_core_mask, ResultInvalidCoreId); + R_UNLESS(affinity_mask != 0, ResultInvalidCombination); // Validate the core id. - if (IsValidCoreId(core_id)) { - if (((1ULL << core_id) & affinity_mask) == 0) { - LOG_ERROR(Kernel_SVC, "Invalid core ID (ID={})", core_id); - return ResultInvalidCombination; - } + if (IsValidVirtualCoreId(core_id)) { + R_UNLESS(((1ULL << core_id) & affinity_mask) != 0, ResultInvalidCombination); } else { - if (core_id != IdealCoreNoUpdate && core_id != IdealCoreDontCare) { - LOG_ERROR(Kernel_SVC, "Invalid core ID (ID={})", core_id); - return ResultInvalidCoreId; - } + R_UNLESS(core_id == IdealCoreNoUpdate || core_id == IdealCoreDontCare, + ResultInvalidCoreId); } } // Get the thread from its handle. - const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - const std::shared_ptr thread = handle_table.Get(thread_handle); - if (!thread) { - LOG_ERROR(Kernel_SVC, "Invalid thread handle (handle={:08X})", thread_handle); - return ResultInvalidHandle; - } + KScopedAutoObject thread = + system.Kernel().CurrentProcess()->GetHandleTable().GetObject(thread_handle); + R_UNLESS(thread.IsNotNull(), ResultInvalidHandle); // Set the core mask. - const auto set_result = thread->SetCoreMask(core_id, affinity_mask); - if (set_result.IsError()) { - LOG_ERROR(Kernel_SVC, "Unable to successfully set core mask (result={})", set_result.raw); - return set_result; - } + R_TRY(thread->SetCoreMask(core_id, affinity_mask)); + return RESULT_SUCCESS; } @@ -2105,7 +1973,7 @@ static ResultCode CreateEvent(Core::System& system, Handle* out_write, Handle* o *out_write = *write_create_result; // Add the writable event to the handle table. - auto handle_guard = SCOPE_GUARD({ handle_table.Close(*write_create_result); }); + auto handle_guard = SCOPE_GUARD({ handle_table.Remove(*write_create_result); }); // Add the readable event to the handle table. const auto read_create_result = handle_table.Create(event->GetReadableEvent()); diff --git a/src/core/hle/kernel/time_manager.cpp b/src/core/hle/kernel/time_manager.cpp index fd0630019..59ebfc51f 100644 --- a/src/core/hle/kernel/time_manager.cpp +++ b/src/core/hle/kernel/time_manager.cpp @@ -15,16 +15,12 @@ namespace Kernel { TimeManager::TimeManager(Core::System& system_) : system{system_} { - time_manager_event_type = Core::Timing::CreateEvent( - "Kernel::TimeManagerCallback", - [this](std::uintptr_t thread_handle, std::chrono::nanoseconds) { - std::shared_ptr thread; - { - std::lock_guard lock{mutex}; - thread = SharedFrom(reinterpret_cast(thread_handle)); - } - thread->Wakeup(); - }); + time_manager_event_type = + Core::Timing::CreateEvent("Kernel::TimeManagerCallback", + [this](std::uintptr_t thread_handle, std::chrono::nanoseconds) { + KThread* thread = reinterpret_cast(thread_handle); + thread->Wakeup(); + }); } void TimeManager::ScheduleTimeEvent(KThread* thread, s64 nanoseconds) { diff --git a/src/yuzu/debugger/wait_tree.cpp b/src/yuzu/debugger/wait_tree.cpp index 3bca6277b..ef0ab0960 100644 --- a/src/yuzu/debugger/wait_tree.cpp +++ b/src/yuzu/debugger/wait_tree.cpp @@ -91,7 +91,7 @@ std::size_t WaitTreeItem::Row() const { std::vector> WaitTreeItem::MakeThreadItemList() { std::vector> item_list; std::size_t row = 0; - auto add_threads = [&](const std::vector>& threads) { + auto add_threads = [&](const std::vector& threads) { for (std::size_t i = 0; i < threads.size(); ++i) { if (threads[i]->GetThreadTypeForDebugging() == Kernel::ThreadType::User) { item_list.push_back(std::make_unique(*threads[i])); @@ -183,10 +183,12 @@ bool WaitTreeExpandableItem::IsExpandable() const { } QString WaitTreeSynchronizationObject::GetText() const { - return tr("[%1]%2 %3") - .arg(object.GetObjectId()) - .arg(QString::fromStdString(object.GetTypeName()), - QString::fromStdString(object.GetName())); + // return tr("[%1]%2 %3") + // .arg(object.GetObjectId()) + // .arg(QString::fromStdString(object.GetTypeName()), + // QString::fromStdString(object.GetName())); + + return tr("UNIMPLEMENTED"); } std::unique_ptr WaitTreeSynchronizationObject::make( From 5e5933256b022f6890fc3f14164ae9e9c3ee9ae3 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 3 Apr 2021 21:21:22 -0700 Subject: [PATCH 18/82] hle: kernel: Refactor IPC interfaces to not use std::shared_ptr. --- src/core/hle/ipc_helpers.h | 20 +++++++++---------- src/core/hle/kernel/client_port.cpp | 2 +- src/core/hle/kernel/hle_ipc.cpp | 12 ++++++----- src/core/hle/kernel/hle_ipc.h | 19 ++++++++++-------- src/core/hle/kernel/k_event.h | 4 ++-- src/core/hle/kernel/svc.cpp | 2 +- src/core/hle/service/am/am.cpp | 5 ++--- src/core/hle/service/am/am.h | 4 ++-- src/core/hle/service/am/applets/applets.cpp | 6 +++--- src/core/hle/service/am/applets/applets.h | 6 +++--- src/core/hle/service/bcat/backend/backend.cpp | 2 +- src/core/hle/service/bcat/module.cpp | 2 +- src/core/hle/service/hid/controllers/npad.cpp | 3 +-- src/core/hle/service/hid/controllers/npad.h | 2 +- src/core/hle/service/hid/hid.cpp | 2 +- src/core/hle/service/hid/irs.cpp | 2 +- src/core/hle/service/nfp/nfp.cpp | 2 +- src/core/hle/service/nfp/nfp.h | 2 +- src/core/hle/service/ns/pl_u.cpp | 2 +- src/core/hle/service/nvdrv/interface.cpp | 2 +- src/core/hle/service/nvdrv/nvdrv.cpp | 2 +- .../hle/service/nvflinger/buffer_queue.cpp | 2 +- src/core/hle/service/nvflinger/buffer_queue.h | 1 + src/core/hle/service/sm/controller.cpp | 2 +- src/core/hle/service/sm/sm.cpp | 8 +++++--- src/core/hle/service/time/time.cpp | 2 +- .../hle/service/vi/display/vi_display.cpp | 2 +- src/core/hle/service/vi/vi.cpp | 4 ++-- 28 files changed, 65 insertions(+), 59 deletions(-) diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index 56cc911d1..224bee950 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -138,8 +138,8 @@ public: context->AddDomainObject(std::move(iface)); } else { auto [client, server] = Kernel::Session::Create(kernel, iface->GetServiceName()); - context->AddMoveObject(std::move(client)); - iface->ClientConnected(std::move(server)); + context->AddMoveObject(client.get()); + iface->ClientConnected(std::move(client), std::move(server)); } } @@ -215,10 +215,10 @@ public: void PushRaw(const T& value); template - void PushMoveObjects(std::shared_ptr... pointers); + void PushMoveObjects(O*... pointers); template - void PushCopyObjects(std::shared_ptr... pointers); + void PushCopyObjects(O*... pointers); private: u32 normal_params_size{}; @@ -301,7 +301,7 @@ void ResponseBuilder::Push(const First& first_value, const Other&... other_value } template -inline void ResponseBuilder::PushCopyObjects(std::shared_ptr... pointers) { +inline void ResponseBuilder::PushCopyObjects(O*... pointers) { auto objects = {pointers...}; for (auto& object : objects) { context->AddCopyObject(std::move(object)); @@ -309,7 +309,7 @@ inline void ResponseBuilder::PushCopyObjects(std::shared_ptr... pointers) { } template -inline void ResponseBuilder::PushMoveObjects(std::shared_ptr... pointers) { +inline void ResponseBuilder::PushMoveObjects(O*... pointers) { auto objects = {pointers...}; for (auto& object : objects) { context->AddMoveObject(std::move(object)); @@ -360,10 +360,10 @@ public: T PopRaw(); template - std::shared_ptr GetMoveObject(std::size_t index); + T* GetMoveObject(std::size_t index); template - std::shared_ptr GetCopyObject(std::size_t index); + T* GetCopyObject(std::size_t index); template std::shared_ptr PopIpcInterface() { @@ -470,12 +470,12 @@ void RequestParser::Pop(First& first_value, Other&... other_values) { } template -std::shared_ptr RequestParser::GetMoveObject(std::size_t index) { +T* RequestParser::GetMoveObject(std::size_t index) { return context->GetMoveObject(index); } template -std::shared_ptr RequestParser::GetCopyObject(std::size_t index) { +T* RequestParser::GetCopyObject(std::size_t index) { return context->GetCopyObject(index); } diff --git a/src/core/hle/kernel/client_port.cpp b/src/core/hle/kernel/client_port.cpp index 0b6957e31..431a90d82 100644 --- a/src/core/hle/kernel/client_port.cpp +++ b/src/core/hle/kernel/client_port.cpp @@ -28,7 +28,7 @@ ResultVal> ClientPort::Connect() { auto [client, server] = Kernel::Session::Create(kernel, name); if (server_port->HasHLEHandler()) { - server_port->GetHLEHandler()->ClientConnected(std::move(server)); + server_port->GetHLEHandler()->ClientConnected(client, std::move(server)); } else { server_port->AppendPendingSession(std::move(server)); } diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index 6ffe6ac41..a9702539f 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -35,15 +35,17 @@ SessionRequestHandler::SessionRequestHandler() = default; SessionRequestHandler::~SessionRequestHandler() = default; -void SessionRequestHandler::ClientConnected(std::shared_ptr server_session) { +void SessionRequestHandler::ClientConnected(std::shared_ptr client_session, + std::shared_ptr server_session) { server_session->SetHleHandler(shared_from_this()); - connected_sessions.push_back(std::move(server_session)); + client_sessions.push_back(std::move(client_session)); + server_sessions.push_back(std::move(server_session)); } void SessionRequestHandler::ClientDisconnected( const std::shared_ptr& server_session) { server_session->SetHleHandler(nullptr); - boost::range::remove_erase(connected_sessions, server_session); + boost::range::remove_erase(server_sessions, server_session); } HLERequestContext::HLERequestContext(KernelCore& kernel_, Core::Memory::Memory& memory_, @@ -223,12 +225,12 @@ ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(KThread& thread) { // for specific values in each of these descriptors. for (auto& object : copy_objects) { ASSERT(object != nullptr); - dst_cmdbuf[current_offset++] = handle_table.Create(object).Unwrap(); + dst_cmdbuf[current_offset++] = handle_table.Create(SharedFrom(object)).Unwrap(); } for (auto& object : move_objects) { ASSERT(object != nullptr); - dst_cmdbuf[current_offset++] = handle_table.Create(object).Unwrap(); + dst_cmdbuf[current_offset++] = handle_table.Create(SharedFrom(object)).Unwrap(); } } diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index 398f1c467..74a95bc76 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -39,6 +39,7 @@ class HandleTable; class HLERequestContext; class KernelCore; class Process; +class ClientSession; class ServerSession; class KThread; class KReadableEvent; @@ -71,7 +72,8 @@ public: * associated ServerSession alive for the duration of the connection. * @param server_session Owning pointer to the ServerSession associated with the connection. */ - void ClientConnected(std::shared_ptr server_session); + void ClientConnected( + std::shared_ptr client_session, std::shared_ptr server_session); /** * Signals that a client has just disconnected from this HLE handler and releases the @@ -84,7 +86,8 @@ protected: /// List of sessions that are connected to this handler. /// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list /// for the duration of the connection. - std::vector> connected_sessions; + std::vector> client_sessions; + std::vector> server_sessions; }; /** @@ -218,21 +221,21 @@ public: } template - std::shared_ptr GetCopyObject(std::size_t index) { + T* GetCopyObject(std::size_t index) { return DynamicObjectCast(copy_objects.at(index)); } template - std::shared_ptr GetMoveObject(std::size_t index) { + T* GetMoveObject(std::size_t index) { return DynamicObjectCast(move_objects.at(index)); } - void AddMoveObject(std::shared_ptr object) { - move_objects.emplace_back(std::move(object)); + void AddMoveObject(Object* object) { + move_objects.emplace_back(object); } - void AddCopyObject(std::shared_ptr object) { - copy_objects.emplace_back(std::move(object)); + void AddCopyObject(Object* object) { + copy_objects.emplace_back(object); } void AddDomainObject(std::shared_ptr object) { diff --git a/src/core/hle/kernel/k_event.h b/src/core/hle/kernel/k_event.h index 2fb887129..ec6894b16 100644 --- a/src/core/hle/kernel/k_event.h +++ b/src/core/hle/kernel/k_event.h @@ -32,8 +32,8 @@ public: return HANDLE_TYPE; } - std::shared_ptr& GetReadableEvent() { - return readable_event; + KReadableEvent* GetReadableEvent() { + return readable_event.get(); } std::shared_ptr& GetWritableEvent() { diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 813450115..dca1bcc92 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -1976,7 +1976,7 @@ static ResultCode CreateEvent(Core::System& system, Handle* out_write, Handle* o auto handle_guard = SCOPE_GUARD({ handle_table.Remove(*write_create_result); }); // Add the readable event to the handle table. - const auto read_create_result = handle_table.Create(event->GetReadableEvent()); + const auto read_create_result = handle_table.Create(SharedFrom(event->GetReadableEvent())); if (read_create_result.Failed()) { return read_create_result.Code(); } diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 58c7f2930..7be94446a 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -588,12 +588,11 @@ AppletMessageQueue::AppletMessageQueue(Kernel::KernelCore& kernel) { AppletMessageQueue::~AppletMessageQueue() = default; -const std::shared_ptr& AppletMessageQueue::GetMessageReceiveEvent() const { +Kernel::KReadableEvent* AppletMessageQueue::GetMessageReceiveEvent() const { return on_new_message->GetReadableEvent(); } -const std::shared_ptr& AppletMessageQueue::GetOperationModeChangedEvent() - const { +Kernel::KReadableEvent* AppletMessageQueue::GetOperationModeChangedEvent() const { return on_operation_mode_changed->GetReadableEvent(); } diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index 5d302e155..f8daeb437 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -56,8 +56,8 @@ public: explicit AppletMessageQueue(Kernel::KernelCore& kernel); ~AppletMessageQueue(); - const std::shared_ptr& GetMessageReceiveEvent() const; - const std::shared_ptr& GetOperationModeChangedEvent() const; + Kernel::KReadableEvent* GetMessageReceiveEvent() const; + Kernel::KReadableEvent* GetOperationModeChangedEvent() const; void PushMessage(AppletMessage msg); AppletMessage PopMessage(); std::size_t GetMessageCount() const; diff --git a/src/core/hle/service/am/applets/applets.cpp b/src/core/hle/service/am/applets/applets.cpp index a56df6a7e..c093813fe 100644 --- a/src/core/hle/service/am/applets/applets.cpp +++ b/src/core/hle/service/am/applets/applets.cpp @@ -142,15 +142,15 @@ void AppletDataBroker::SignalStateChanged() const { } } -std::shared_ptr AppletDataBroker::GetNormalDataEvent() const { +Kernel::KReadableEvent* AppletDataBroker::GetNormalDataEvent() const { return pop_out_data_event->GetReadableEvent(); } -std::shared_ptr AppletDataBroker::GetInteractiveDataEvent() const { +Kernel::KReadableEvent* AppletDataBroker::GetInteractiveDataEvent() const { return pop_interactive_out_data_event->GetReadableEvent(); } -std::shared_ptr AppletDataBroker::GetStateChangedEvent() const { +Kernel::KReadableEvent* AppletDataBroker::GetStateChangedEvent() const { return state_changed_event->GetReadableEvent(); } diff --git a/src/core/hle/service/am/applets/applets.h b/src/core/hle/service/am/applets/applets.h index 4215d2232..ffde8ced0 100644 --- a/src/core/hle/service/am/applets/applets.h +++ b/src/core/hle/service/am/applets/applets.h @@ -97,9 +97,9 @@ public: void SignalStateChanged() const; - std::shared_ptr GetNormalDataEvent() const; - std::shared_ptr GetInteractiveDataEvent() const; - std::shared_ptr GetStateChangedEvent() const; + Kernel::KReadableEvent* GetNormalDataEvent() const; + Kernel::KReadableEvent* GetInteractiveDataEvent() const; + Kernel::KReadableEvent* GetStateChangedEvent() const; private: Core::System& system; diff --git a/src/core/hle/service/bcat/backend/backend.cpp b/src/core/hle/service/bcat/backend/backend.cpp index 92d25dbe4..7f301fdeb 100644 --- a/src/core/hle/service/bcat/backend/backend.cpp +++ b/src/core/hle/service/bcat/backend/backend.cpp @@ -21,7 +21,7 @@ ProgressServiceBackend::ProgressServiceBackend(Kernel::KernelCore& kernel, } std::shared_ptr ProgressServiceBackend::GetEvent() const { - return event->GetReadableEvent(); + return SharedFrom(event->GetReadableEvent()); } DeliveryCacheProgressImpl& ProgressServiceBackend::GetImpl() { diff --git a/src/core/hle/service/bcat/module.cpp b/src/core/hle/service/bcat/module.cpp index 285085f2a..8473b716b 100644 --- a/src/core/hle/service/bcat/module.cpp +++ b/src/core/hle/service/bcat/module.cpp @@ -109,7 +109,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(event); + rb.PushCopyObjects(event.get()); } void GetImpl(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp index d4678ef49..9d07ca09c 100644 --- a/src/core/hle/service/hid/controllers/npad.cpp +++ b/src/core/hle/service/hid/controllers/npad.cpp @@ -955,8 +955,7 @@ bool Controller_NPad::IsVibrationDeviceMounted(const DeviceHandle& vibration_dev return vibration_devices_mounted[npad_index][device_index]; } -std::shared_ptr Controller_NPad::GetStyleSetChangedEvent( - u32 npad_id) const { +Kernel::KReadableEvent* Controller_NPad::GetStyleSetChangedEvent(u32 npad_id) const { const auto& styleset_event = styleset_changed_events[NPadIdToIndex(npad_id)]; return styleset_event->GetReadableEvent(); } diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h index ea484d4bf..8c24728b1 100644 --- a/src/core/hle/service/hid/controllers/npad.h +++ b/src/core/hle/service/hid/controllers/npad.h @@ -199,7 +199,7 @@ public: bool IsVibrationDeviceMounted(const DeviceHandle& vibration_device_handle) const; - std::shared_ptr GetStyleSetChangedEvent(u32 npad_id) const; + Kernel::KReadableEvent* GetStyleSetChangedEvent(u32 npad_id) const; void SignalStyleSetChangedEvent(u32 npad_id) const; // Adds a new controller at an index. diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index 9c4bf6d16..d2a6375f5 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -118,7 +118,7 @@ void IAppletResource::GetSharedMemoryHandle(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(shared_mem); + rb.PushCopyObjects(shared_mem.get()); } void IAppletResource::UpdateControllers(std::uintptr_t user_data, diff --git a/src/core/hle/service/hid/irs.cpp b/src/core/hle/service/hid/irs.cpp index 2dfa936fb..b50a51eb7 100644 --- a/src/core/hle/service/hid/irs.cpp +++ b/src/core/hle/service/hid/irs.cpp @@ -62,7 +62,7 @@ void IRS::GetIrsensorSharedMemoryHandle(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(shared_mem); + rb.PushCopyObjects(shared_mem.get()); } void IRS::StopImageProcessor(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 2d1d4d67f..1446c0bcf 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -343,7 +343,7 @@ bool Module::Interface::LoadAmiibo(const std::vector& buffer) { return true; } -const std::shared_ptr& Module::Interface::GetNFCEvent() const { +Kernel::KReadableEvent* Module::Interface::GetNFCEvent() const { return nfc_tag_load->GetReadableEvent(); } diff --git a/src/core/hle/service/nfp/nfp.h b/src/core/hle/service/nfp/nfp.h index c46551760..7a97caffb 100644 --- a/src/core/hle/service/nfp/nfp.h +++ b/src/core/hle/service/nfp/nfp.h @@ -38,7 +38,7 @@ public: void CreateUserInterface(Kernel::HLERequestContext& ctx); bool LoadAmiibo(const std::vector& buffer); - const std::shared_ptr& GetNFCEvent() const; + Kernel::KReadableEvent* GetNFCEvent() const; const AmiiboFile& GetAmiiboBuffer() const; private: diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp index da139fdc4..e6616a3b9 100644 --- a/src/core/hle/service/ns/pl_u.cpp +++ b/src/core/hle/service/ns/pl_u.cpp @@ -267,7 +267,7 @@ void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(impl->shared_font_mem); + rb.PushCopyObjects(impl->shared_font_mem.get()); } void PL_U::GetSharedFontInOrderOfPriority(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/nvdrv/interface.cpp b/src/core/hle/service/nvdrv/interface.cpp index eff9c3cc9..8e359040f 100644 --- a/src/core/hle/service/nvdrv/interface.cpp +++ b/src/core/hle/service/nvdrv/interface.cpp @@ -189,7 +189,7 @@ void NVDRV::QueryEvent(Kernel::HLERequestContext& ctx) { rb.Push(RESULT_SUCCESS); auto event = nvdrv->GetEvent(event_id); event->Clear(); - rb.PushCopyObjects(event); + rb.PushCopyObjects(event.get()); rb.PushEnum(NvResult::Success); } else { IPC::ResponseBuilder rb{ctx, 3}; diff --git a/src/core/hle/service/nvdrv/nvdrv.cpp b/src/core/hle/service/nvdrv/nvdrv.cpp index ede77858a..6bba9c0b3 100644 --- a/src/core/hle/service/nvdrv/nvdrv.cpp +++ b/src/core/hle/service/nvdrv/nvdrv.cpp @@ -178,7 +178,7 @@ void Module::SignalSyncpt(const u32 syncpoint_id, const u32 value) { } std::shared_ptr Module::GetEvent(const u32 event_id) const { - return events_interface.events[event_id].event->GetReadableEvent(); + return SharedFrom(events_interface.events[event_id].event->GetReadableEvent()); } std::shared_ptr Module::GetEventWriteable(const u32 event_id) const { diff --git a/src/core/hle/service/nvflinger/buffer_queue.cpp b/src/core/hle/service/nvflinger/buffer_queue.cpp index 7842a82ed..f783ae54f 100644 --- a/src/core/hle/service/nvflinger/buffer_queue.cpp +++ b/src/core/hle/service/nvflinger/buffer_queue.cpp @@ -194,7 +194,7 @@ std::shared_ptr BufferQueue::GetWritableBufferWaitEvent( } std::shared_ptr BufferQueue::GetBufferWaitEvent() const { - return buffer_wait_event->GetReadableEvent(); + return SharedFrom(buffer_wait_event->GetReadableEvent()); } } // namespace Service::NVFlinger diff --git a/src/core/hle/service/nvflinger/buffer_queue.h b/src/core/hle/service/nvflinger/buffer_queue.h index 163fa4c54..9a21c7426 100644 --- a/src/core/hle/service/nvflinger/buffer_queue.h +++ b/src/core/hle/service/nvflinger/buffer_queue.h @@ -13,6 +13,7 @@ #include "common/common_funcs.h" #include "common/math_util.h" #include "common/swap.h" +#include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/object.h" #include "core/hle/service/nvdrv/nvdata.h" diff --git a/src/core/hle/service/sm/controller.cpp b/src/core/hle/service/sm/controller.cpp index 916177efd..b34fe4bc2 100644 --- a/src/core/hle/service/sm/controller.cpp +++ b/src/core/hle/service/sm/controller.cpp @@ -30,7 +30,7 @@ void Controller::CloneCurrentObject(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles}; rb.Push(RESULT_SUCCESS); - rb.PushMoveObjects(ctx.Session()->GetParent()->Client()); + rb.PushMoveObjects(ctx.Session()->GetParent()->Client().get()); } void Controller::CloneCurrentObjectEx(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 94608d529..62f7a5358 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -134,7 +134,7 @@ void SM::GetService(Kernel::HLERequestContext& ctx) { const auto& server_port = client_port.Unwrap()->GetServerPort(); if (server_port->GetHLEHandler()) { - server_port->GetHLEHandler()->ClientConnected(server); + server_port->GetHLEHandler()->ClientConnected(client, server); } else { server_port->AppendPendingSession(server); } @@ -142,7 +142,7 @@ void SM::GetService(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_SM, "called service={} -> session={}", name, client->GetObjectId()); IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles}; rb.Push(RESULT_SUCCESS); - rb.PushMoveObjects(std::move(client)); + rb.PushMoveObjects(client.get()); } void SM::RegisterService(Kernel::HLERequestContext& ctx) { @@ -170,7 +170,9 @@ void SM::RegisterService(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles}; rb.Push(handle.Code()); - rb.PushMoveObjects(std::move(handle).Unwrap()); + + auto server_port = handle.Unwrap(); + rb.PushMoveObjects(server_port.get()); } void SM::UnregisterService(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/time/time.cpp b/src/core/hle/service/time/time.cpp index 32f372d71..30283f239 100644 --- a/src/core/hle/service/time/time.cpp +++ b/src/core/hle/service/time/time.cpp @@ -393,7 +393,7 @@ void Module::Interface::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& c LOG_DEBUG(Service_Time, "called"); IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(SharedFrom(&system.Kernel().GetTimeSharedMem())); + rb.PushCopyObjects(&system.Kernel().GetTimeSharedMem()); } Module::Interface::Interface(std::shared_ptr module_, Core::System& system_, diff --git a/src/core/hle/service/vi/display/vi_display.cpp b/src/core/hle/service/vi/display/vi_display.cpp index ac9e87338..9ffa71352 100644 --- a/src/core/hle/service/vi/display/vi_display.cpp +++ b/src/core/hle/service/vi/display/vi_display.cpp @@ -34,7 +34,7 @@ const Layer& Display::GetLayer(std::size_t index) const { } std::shared_ptr Display::GetVSyncEvent() const { - return vsync_event->GetReadableEvent(); + return SharedFrom(vsync_event->GetReadableEvent()); } void Display::SignalVSyncEvent() { diff --git a/src/core/hle/service/vi/vi.cpp b/src/core/hle/service/vi/vi.cpp index 7ae07d072..6e507bfb8 100644 --- a/src/core/hle/service/vi/vi.cpp +++ b/src/core/hle/service/vi/vi.cpp @@ -674,7 +674,7 @@ private: // TODO(Subv): Find out what this actually is. IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(buffer_queue.GetBufferWaitEvent()); + rb.PushCopyObjects(buffer_queue.GetBufferWaitEvent().get()); } NVFlinger::NVFlinger& nv_flinger; @@ -1209,7 +1209,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(vsync_event); + rb.PushCopyObjects(vsync_event.get()); } void ConvertScalingMode(Kernel::HLERequestContext& ctx) { From 7ccbdd4d8d3dea7294d2cac38779cceea9745d52 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 3 Apr 2021 22:22:36 -0700 Subject: [PATCH 19/82] hle: kernel: Migrate KProcess to KAutoObject. --- src/core/core.cpp | 9 ++++-- src/core/hle/kernel/handle_table.cpp | 10 +++--- src/core/hle/kernel/handle_table.h | 4 +-- src/core/hle/kernel/init/init_slab_setup.cpp | 9 ++++-- src/core/hle/kernel/init/init_slab_setup.h | 2 +- .../hle/kernel/k_synchronization_object.h | 5 ++- src/core/hle/kernel/kernel.cpp | 8 ++--- src/core/hle/kernel/kernel.h | 4 +-- src/core/hle/kernel/object.h | 4 +-- src/core/hle/kernel/process.cpp | 31 ++++++++++-------- src/core/hle/kernel/process.h | 32 +++++++++++++------ src/core/hle/kernel/svc.cpp | 6 ++-- src/core/hle/service/pm/pm.cpp | 12 +++---- src/yuzu/debugger/wait_tree.cpp | 2 +- 14 files changed, 80 insertions(+), 58 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index fdaa82c8f..f050a8d4b 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -233,8 +233,11 @@ struct System::Impl { } telemetry_session->AddInitialInfo(*app_loader, fs_controller, *content_provider); - auto main_process = - Kernel::Process::Create(system, "main", Kernel::Process::ProcessType::Userland); + auto main_process = Kernel::Process::CreateWithKernel(system.Kernel()); + ASSERT(Kernel::Process::Initialize(main_process, system, "main", + Kernel::Process::ProcessType::Userland) + .IsSuccess()); + main_process->Open(); const auto [load_result, load_parameters] = app_loader->Load(*main_process, system); if (load_result != Loader::ResultStatus::Success) { LOG_CRITICAL(Core, "Failed to load ROM (Error {})!", load_result); @@ -244,7 +247,7 @@ struct System::Impl { static_cast(load_result)); } AddGlueRegistrationForProcess(*app_loader, *main_process); - kernel.MakeCurrentProcess(main_process.get()); + kernel.MakeCurrentProcess(main_process); kernel.InitializeCores(); // Initialize cheat engine diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index 8eec8a3b5..f746c4888 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -100,7 +100,7 @@ ResultCode HandleTable::Add(Handle* out_handle, KAutoObject* obj, u16 type) { } ResultVal HandleTable::Duplicate(Handle handle) { - std::shared_ptr object = GetGeneric(handle); + std::shared_ptr object = SharedFrom(GetGeneric(handle)); if (object == nullptr) { LOG_ERROR(Kernel, "Tried to duplicate invalid handle: {:08X}", handle); return ResultInvalidHandle; @@ -140,17 +140,17 @@ bool HandleTable::IsValid(Handle handle) const { return slot < table_size && is_object_valid && generations[slot] == generation; } -std::shared_ptr HandleTable::GetGeneric(Handle handle) const { +Object* HandleTable::GetGeneric(Handle handle) const { if (handle == CurrentThread) { - return SharedFrom(kernel.CurrentScheduler()->GetCurrentThread()); + return (kernel.CurrentScheduler()->GetCurrentThread()); } else if (handle == CurrentProcess) { - return SharedFrom(kernel.CurrentProcess()); + return (kernel.CurrentProcess()); } if (!IsValid(handle)) { return nullptr; } - return objects[GetSlot(handle)]; + return objects[GetSlot(handle)].get(); } void HandleTable::Clear() { diff --git a/src/core/hle/kernel/handle_table.h b/src/core/hle/kernel/handle_table.h index 555fb20e5..24a26b81d 100644 --- a/src/core/hle/kernel/handle_table.h +++ b/src/core/hle/kernel/handle_table.h @@ -98,7 +98,7 @@ public: * Looks up a handle. * @return Pointer to the looked-up object, or `nullptr` if the handle is not valid. */ - std::shared_ptr GetGeneric(Handle handle) const; + Object* GetGeneric(Handle handle) const; /** * Looks up a handle while verifying its type. @@ -106,7 +106,7 @@ public: * type differs from the requested one. */ template - std::shared_ptr Get(Handle handle) const { + T* Get(Handle handle) const { return DynamicObjectCast(GetGeneric(handle)); } diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index a290249c7..a6fed524b 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -14,13 +14,16 @@ #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/memory_types.h" +#include "core/hle/kernel/process.h" #include "core/memory.h" namespace Kernel::Init { #define SLAB_COUNT(CLASS) g_slab_resource_counts.num_##CLASS -#define FOREACH_SLAB_TYPE(HANDLER, ...) HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) +#define FOREACH_SLAB_TYPE(HANDLER, ...) \ + HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ + HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) namespace { @@ -33,7 +36,7 @@ enum KSlabType : u32 { #undef DEFINE_SLAB_TYPE_ENUM_MEMBER // Constexpr counts. -constexpr size_t SlabCountKProcess = 80; +constexpr size_t SlabCountProcess = 80; constexpr size_t SlabCountKThread = 800; constexpr size_t SlabCountKEvent = 700; constexpr size_t SlabCountKInterruptEvent = 100; @@ -54,7 +57,7 @@ constexpr size_t SlabCountExtraKThread = 160; // Global to hold our resource counts. KSlabResourceCounts g_slab_resource_counts = { - .num_KProcess = SlabCountKProcess, + .num_Process = SlabCountProcess, .num_KThread = SlabCountKThread, .num_KEvent = SlabCountKEvent, .num_KInterruptEvent = SlabCountKInterruptEvent, diff --git a/src/core/hle/kernel/init/init_slab_setup.h b/src/core/hle/kernel/init/init_slab_setup.h index 6418b97ac..8876678b3 100644 --- a/src/core/hle/kernel/init/init_slab_setup.h +++ b/src/core/hle/kernel/init/init_slab_setup.h @@ -15,7 +15,7 @@ class KMemoryLayout; namespace Kernel::Init { struct KSlabResourceCounts { - size_t num_KProcess; + size_t num_Process; size_t num_KThread; size_t num_KEvent; size_t num_KInterruptEvent; diff --git a/src/core/hle/kernel/k_synchronization_object.h b/src/core/hle/kernel/k_synchronization_object.h index c8e840d89..5a99dbd46 100644 --- a/src/core/hle/kernel/k_synchronization_object.h +++ b/src/core/hle/kernel/k_synchronization_object.h @@ -53,10 +53,9 @@ private: // Specialization of DynamicObjectCast for KSynchronizationObjects template <> -inline std::shared_ptr DynamicObjectCast( - std::shared_ptr object) { +inline KSynchronizationObject* DynamicObjectCast(Object* object) { if (object != nullptr && object->IsWaitable()) { - return std::static_pointer_cast(object); + return reinterpret_cast(object); } return nullptr; } diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index d1359e434..1b5b11564 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -614,7 +614,7 @@ struct KernelCore::Impl { std::atomic next_thread_id{1}; // Lists all processes that exist in the current session. - std::vector> process_list; + std::vector process_list; Process* current_process = nullptr; std::unique_ptr global_scheduler_context; Kernel::TimeManager time_manager; @@ -699,8 +699,8 @@ KScopedAutoObject KernelCore::RetrieveThreadFromGlobalHandleTable(Handl return impl->global_handle_table.GetObject(handle); } -void KernelCore::AppendNewProcess(std::shared_ptr process) { - impl->process_list.push_back(std::move(process)); +void KernelCore::AppendNewProcess(Process* process) { + impl->process_list.push_back(process); } void KernelCore::MakeCurrentProcess(Process* process) { @@ -715,7 +715,7 @@ const Process* KernelCore::CurrentProcess() const { return impl->current_process; } -const std::vector>& KernelCore::GetProcessList() const { +const std::vector& KernelCore::GetProcessList() const { return impl->process_list; } diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 3f5c2aec7..b78602f46 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -91,7 +91,7 @@ public: KScopedAutoObject RetrieveThreadFromGlobalHandleTable(Handle handle) const; /// Adds the given shared pointer to an internal list of active processes. - void AppendNewProcess(std::shared_ptr process); + void AppendNewProcess(Process* process); /// Makes the given process the new current process. void MakeCurrentProcess(Process* process); @@ -103,7 +103,7 @@ public: const Process* CurrentProcess() const; /// Retrieves the list of processes. - const std::vector>& GetProcessList() const; + const std::vector& GetProcessList() const; /// Gets the sole instance of the global scheduler Kernel::GlobalSchedulerContext& GlobalSchedulerContext(); diff --git a/src/core/hle/kernel/object.h b/src/core/hle/kernel/object.h index 501e58b33..5c14aa46f 100644 --- a/src/core/hle/kernel/object.h +++ b/src/core/hle/kernel/object.h @@ -86,9 +86,9 @@ std::shared_ptr SharedFrom(T* raw) { * @return Derived pointer to the object, or `nullptr` if `object` isn't of type T. */ template -inline std::shared_ptr DynamicObjectCast(std::shared_ptr object) { +inline T* DynamicObjectCast(Object* object) { if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) { - return std::static_pointer_cast(object); + return reinterpret_cast(object); } return nullptr; } diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 796dca5ef..fe4558648 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -49,6 +49,8 @@ void SetupMainThread(Core::System& system, Process& owner_process, u32 priority, // Register 1 must be a handle to the main thread Handle thread_handle{}; owner_process.GetHandleTable().Add(&thread_handle, thread); + + thread->SetName("main"); thread->GetContext32().cpu_registers[0] = 0; thread->GetContext64().cpu_registers[0] = 0; thread->GetContext32().cpu_registers[1] = thread_handle; @@ -115,10 +117,10 @@ private: std::bitset is_slot_used; }; -std::shared_ptr Process::Create(Core::System& system, std::string name, ProcessType type) { +ResultCode Process::Initialize(Process* process, Core::System& system, std::string name, + ProcessType type) { auto& kernel = system.Kernel(); - std::shared_ptr process = std::make_shared(system); process->name = std::move(name); process->resource_limit = kernel.GetSystemResourceLimit(); @@ -127,6 +129,7 @@ std::shared_ptr Process::Create(Core::System& system, std::string name, process->process_id = type == ProcessType::KernelInternal ? kernel.CreateNewKernelProcessID() : kernel.CreateNewUserProcessID(); process->capabilities.InitializeForMetadatalessProcess(); + process->is_initialized = true; std::mt19937 rng(Settings::values.rng_seed.GetValue().value_or(std::time(nullptr))); std::uniform_int_distribution distribution; @@ -134,7 +137,8 @@ std::shared_ptr Process::Create(Core::System& system, std::string name, [&] { return distribution(rng); }); kernel.AppendNewProcess(process); - return process; + + return RESULT_SUCCESS; } std::shared_ptr Process::GetResourceLimit() const { @@ -332,7 +336,7 @@ void Process::Run(s32 main_thread_priority, u64 stack_size) { ChangeStatus(ProcessStatus::Running); - SetupMainThread(system, *this, main_thread_priority, main_thread_stack_top); + SetupMainThread(kernel.System(), *this, main_thread_priority, main_thread_stack_top); } void Process::PrepareForTermination() { @@ -354,7 +358,7 @@ void Process::PrepareForTermination() { } }; - stop_threads(system.GlobalSchedulerContext().GetThreadList()); + stop_threads(kernel.System().GlobalSchedulerContext().GetThreadList()); FreeTLSRegion(tls_region_address); tls_region_address = 0; @@ -381,7 +385,7 @@ static auto FindTLSPageWithAvailableSlots(std::vector& tls_pages) { } VAddr Process::CreateTLSRegion() { - KScopedSchedulerLock lock(system.Kernel()); + KScopedSchedulerLock lock(kernel); if (auto tls_page_iter{FindTLSPageWithAvailableSlots(tls_pages)}; tls_page_iter != tls_pages.cend()) { return *tls_page_iter->ReserveSlot(); @@ -392,7 +396,7 @@ VAddr Process::CreateTLSRegion() { const VAddr start{page_table->GetKernelMapRegionStart()}; const VAddr size{page_table->GetKernelMapRegionEnd() - start}; - const PAddr tls_map_addr{system.DeviceMemory().GetPhysicalAddr(tls_page_ptr)}; + const PAddr tls_map_addr{kernel.System().DeviceMemory().GetPhysicalAddr(tls_page_ptr)}; const VAddr tls_page_addr{page_table ->AllocateAndMapMemory(1, PageSize, true, start, size / PageSize, KMemoryState::ThreadLocal, @@ -412,7 +416,7 @@ VAddr Process::CreateTLSRegion() { } void Process::FreeTLSRegion(VAddr tls_address) { - KScopedSchedulerLock lock(system.Kernel()); + KScopedSchedulerLock lock(kernel); const VAddr aligned_address = Common::AlignDown(tls_address, Core::Memory::PAGE_SIZE); auto iter = std::find_if(tls_pages.begin(), tls_pages.end(), [aligned_address](const auto& page) { @@ -433,7 +437,8 @@ void Process::LoadModule(CodeSet code_set, VAddr base_addr) { page_table->SetCodeMemoryPermission(segment.addr + base_addr, segment.size, permission); }; - system.Memory().WriteBlock(*this, base_addr, code_set.memory.data(), code_set.memory.size()); + kernel.System().Memory().WriteBlock(*this, base_addr, code_set.memory.data(), + code_set.memory.size()); ReprotectSegment(code_set.CodeSegment(), KMemoryPermission::ReadAndExecute); ReprotectSegment(code_set.RODataSegment(), KMemoryPermission::Read); @@ -445,10 +450,10 @@ bool Process::IsSignaled() const { return is_signaled; } -Process::Process(Core::System& system) - : KSynchronizationObject{system.Kernel()}, page_table{std::make_unique(system)}, - handle_table{system.Kernel()}, address_arbiter{system}, condition_var{system}, - state_lock{system.Kernel()}, system{system} {} +Process::Process(KernelCore& kernel) + : KAutoObjectWithSlabHeapAndContainer{kernel}, + page_table{std::make_unique(kernel.System())}, handle_table{kernel}, + address_arbiter{kernel.System()}, condition_var{kernel.System()}, state_lock{kernel} {} Process::~Process() = default; diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index 45eefb90e..df3c7997d 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -13,9 +13,11 @@ #include "common/common_types.h" #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/k_address_arbiter.h" +#include "core/hle/kernel/k_auto_object.h" #include "core/hle/kernel/k_condition_variable.h" #include "core/hle/kernel/k_synchronization_object.h" #include "core/hle/kernel/process_capability.h" +#include "core/hle/kernel/slab_helpers.h" #include "core/hle/result.h" namespace Core { @@ -60,9 +62,11 @@ enum class ProcessStatus { DebugBreak, }; -class Process final : public KSynchronizationObject { +class Process final : public KAutoObjectWithSlabHeapAndContainer { + KERNEL_AUTOOBJECT_TRAITS(Process, KSynchronizationObject); + public: - explicit Process(Core::System& system); + explicit Process(KernelCore& kernel); ~Process() override; enum : u64 { @@ -85,8 +89,8 @@ public: static constexpr std::size_t RANDOM_ENTROPY_SIZE = 4; - static std::shared_ptr Create(Core::System& system, std::string name, - ProcessType type); + static ResultCode Initialize(Process* process, Core::System& system, std::string name, + ProcessType type); std::string GetTypeName() const override { return "Process"; @@ -338,9 +342,21 @@ public: void LoadModule(CodeSet code_set, VAddr base_addr); - bool IsSignaled() const override; + virtual bool IsInitialized() const override { + return is_initialized; + } - void Finalize() override {} + static void PostDestroy([[maybe_unused]] uintptr_t arg) {} + + virtual void Finalize() override { + UNIMPLEMENTED(); + } + + virtual u64 GetId() const override final { + return GetProcessID(); + } + + virtual bool IsSignaled() const override; void PinCurrentThread(); void UnpinCurrentThread(); @@ -462,6 +478,7 @@ private: bool is_signaled{}; bool is_suspended{}; + bool is_initialized{}; std::atomic num_created_threads{}; std::atomic num_threads{}; @@ -474,9 +491,6 @@ private: KThread* exception_thread{}; KLightLock state_lock; - - /// System context - Core::System& system; }; } // namespace Kernel diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index dca1bcc92..7d676e5f7 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -342,7 +342,7 @@ static ResultCode ConnectToNamedPort32(Core::System& system, Handle* out_handle, static ResultCode SendSyncRequest(Core::System& system, Handle handle) { auto& kernel = system.Kernel(); const auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); - std::shared_ptr session = handle_table.Get(handle); + auto session = handle_table.Get(handle); if (!session) { LOG_ERROR(Kernel_SVC, "called with invalid handle=0x{:08X}", handle); return ResultInvalidHandle; @@ -437,7 +437,7 @@ static ResultCode WaitSynchronization(Core::System& system, s32* index, VAddr ha { auto object = handle_table.Get(handle); if (object) { - objects[i] = object.get(); + objects[i] = object; succeeded = true; } } @@ -1190,7 +1190,7 @@ static ResultCode QueryProcessMemory(Core::System& system, VAddr memory_info_add std::lock_guard lock{HLE::g_hle_lock}; LOG_TRACE(Kernel_SVC, "called process=0x{:08X} address={:X}", process_handle, address); const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - std::shared_ptr process = handle_table.Get(process_handle); + auto process = handle_table.Get(process_handle); if (!process) { LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", process_handle); diff --git a/src/core/hle/service/pm/pm.cpp b/src/core/hle/service/pm/pm.cpp index 68736c40c..3a00849e1 100644 --- a/src/core/hle/service/pm/pm.cpp +++ b/src/core/hle/service/pm/pm.cpp @@ -17,9 +17,8 @@ constexpr ResultCode ERROR_PROCESS_NOT_FOUND{ErrorModule::PM, 1}; constexpr u64 NO_PROCESS_FOUND_PID{0}; -std::optional> SearchProcessList( - const std::vector>& process_list, - std::function&)> predicate) { +std::optional SearchProcessList(const std::vector& process_list, + std::function predicate) { const auto iter = std::find_if(process_list.begin(), process_list.end(), predicate); if (iter == process_list.end()) { @@ -30,7 +29,7 @@ std::optional> SearchProcessList( } void GetApplicationPidGeneric(Kernel::HLERequestContext& ctx, - const std::vector>& process_list) { + const std::vector& process_list) { const auto process = SearchProcessList(process_list, [](const auto& process) { return process->GetProcessID() == Kernel::Process::ProcessIDMin; }); @@ -125,8 +124,7 @@ private: class Info final : public ServiceFramework { public: - explicit Info(Core::System& system_, - const std::vector>& process_list_) + explicit Info(Core::System& system_, const std::vector& process_list_) : ServiceFramework{system_, "pm:info"}, process_list{process_list_} { static const FunctionInfo functions[] = { {0, &Info::GetTitleId, "GetTitleId"}, @@ -156,7 +154,7 @@ private: rb.Push((*process)->GetTitleID()); } - const std::vector>& process_list; + const std::vector& process_list; }; class Shell final : public ServiceFramework { diff --git a/src/yuzu/debugger/wait_tree.cpp b/src/yuzu/debugger/wait_tree.cpp index ef0ab0960..f4eeba2c5 100644 --- a/src/yuzu/debugger/wait_tree.cpp +++ b/src/yuzu/debugger/wait_tree.cpp @@ -118,7 +118,7 @@ WaitTreeMutexInfo::WaitTreeMutexInfo(VAddr mutex_address, const Kernel::HandleTa : mutex_address(mutex_address) { mutex_value = Core::System::GetInstance().Memory().Read32(mutex_address); owner_handle = static_cast(mutex_value & Kernel::Svc::HandleWaitMask); - owner = handle_table.Get(owner_handle); + owner = SharedFrom(handle_table.Get(owner_handle)); } WaitTreeMutexInfo::~WaitTreeMutexInfo() = default; From 086db71e942dc3468bccb741cabf62fdd221e790 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 3 Apr 2021 23:22:07 -0700 Subject: [PATCH 20/82] hle: kernel: Migrate KSharedMemory to KAutoObject. --- src/core/hle/kernel/handle_table.cpp | 20 +++++-- src/core/hle/kernel/handle_table.h | 2 +- src/core/hle/kernel/hle_ipc.cpp | 4 +- src/core/hle/kernel/init/init_slab_setup.cpp | 4 +- src/core/hle/kernel/k_shared_memory.cpp | 55 ++++++++++++------- src/core/hle/kernel/k_shared_memory.h | 34 +++++++----- src/core/hle/kernel/k_thread.cpp | 3 + src/core/hle/kernel/kernel.cpp | 45 ++++++++------- src/core/hle/kernel/svc.cpp | 21 +++---- src/core/hle/service/hid/hid.cpp | 10 ++-- src/core/hle/service/hid/hid.h | 6 -- src/core/hle/service/hid/irs.cpp | 6 +- src/core/hle/service/hid/irs.h | 5 -- src/core/hle/service/ns/pl_u.cpp | 8 +-- .../hle/service/time/time_sharedmemory.cpp | 15 ++--- src/core/hle/service/time/time_sharedmemory.h | 4 -- 16 files changed, 128 insertions(+), 114 deletions(-) diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index f746c4888..427c6fc1b 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -47,9 +47,21 @@ ResultCode HandleTable::SetSize(s32 handle_table_size) { return RESULT_SUCCESS; } -ResultVal HandleTable::Create(std::shared_ptr obj) { +ResultVal HandleTable::Create(Object* obj) { DEBUG_ASSERT(obj != nullptr); + switch (obj->GetHandleType()) { + case HandleType::SharedMemory: + case HandleType::Thread: + case HandleType::Process: { + Handle handle{}; + Add(&handle, reinterpret_cast(obj), {}); + return MakeResult(handle); + } + default: + break; + } + const u16 slot = next_free_slot; if (slot >= table_size) { LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use."); @@ -66,7 +78,7 @@ ResultVal HandleTable::Create(std::shared_ptr obj) { } generations[slot] = generation; - objects[slot] = std::move(obj); + objects[slot] = std::move(SharedFrom(obj)); Handle handle = generation | (slot << 15); return MakeResult(handle); @@ -100,12 +112,12 @@ ResultCode HandleTable::Add(Handle* out_handle, KAutoObject* obj, u16 type) { } ResultVal HandleTable::Duplicate(Handle handle) { - std::shared_ptr object = SharedFrom(GetGeneric(handle)); + auto object = GetGeneric(handle); if (object == nullptr) { LOG_ERROR(Kernel, "Tried to duplicate invalid handle: {:08X}", handle); return ResultInvalidHandle; } - return Create(std::move(object)); + return Create(object); } bool HandleTable::Remove(Handle handle) { diff --git a/src/core/hle/kernel/handle_table.h b/src/core/hle/kernel/handle_table.h index 24a26b81d..d57188844 100644 --- a/src/core/hle/kernel/handle_table.h +++ b/src/core/hle/kernel/handle_table.h @@ -74,7 +74,7 @@ public: * @return The created Handle or one of the following errors: * - `ERR_HANDLE_TABLE_FULL`: the maximum number of handles has been exceeded. */ - ResultVal Create(std::shared_ptr obj); + ResultVal Create(Object* obj); /** * Returns a new handle that points to the same object as the passed in handle. diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index a9702539f..1e831aaca 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -225,12 +225,12 @@ ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(KThread& thread) { // for specific values in each of these descriptors. for (auto& object : copy_objects) { ASSERT(object != nullptr); - dst_cmdbuf[current_offset++] = handle_table.Create(SharedFrom(object)).Unwrap(); + dst_cmdbuf[current_offset++] = handle_table.Create(object).Unwrap(); } for (auto& object : move_objects) { ASSERT(object != nullptr); - dst_cmdbuf[current_offset++] = handle_table.Create(SharedFrom(object)).Unwrap(); + dst_cmdbuf[current_offset++] = handle_table.Create(object).Unwrap(); } } diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index a6fed524b..eb9c8e2e4 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -11,6 +11,7 @@ #include "core/hle/kernel/init/init_slab_setup.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" +#include "core/hle/kernel/k_shared_memory.h" #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/memory_types.h" @@ -23,7 +24,8 @@ namespace Kernel::Init { #define FOREACH_SLAB_TYPE(HANDLER, ...) \ HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ - HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) + HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ + HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) namespace { diff --git a/src/core/hle/kernel/k_shared_memory.cpp b/src/core/hle/kernel/k_shared_memory.cpp index 9b14f42b5..9e20c2350 100644 --- a/src/core/hle/kernel/k_shared_memory.cpp +++ b/src/core/hle/kernel/k_shared_memory.cpp @@ -11,36 +11,53 @@ namespace Kernel { -KSharedMemory::KSharedMemory(KernelCore& kernel, Core::DeviceMemory& device_memory) - : Object{kernel}, device_memory{device_memory} {} +KSharedMemory::KSharedMemory(KernelCore& kernel) : KAutoObjectWithSlabHeapAndContainer{kernel} {} KSharedMemory::~KSharedMemory() { kernel.GetSystemResourceLimit()->Release(LimitableResource::PhysicalMemory, size); } -std::shared_ptr KSharedMemory::Create( - KernelCore& kernel, Core::DeviceMemory& device_memory, Process* owner_process, - KPageLinkedList&& page_list, KMemoryPermission owner_permission, - KMemoryPermission user_permission, PAddr physical_address, std::size_t size, std::string name) { +ResultCode KSharedMemory::Initialize(KernelCore& kernel_, Core::DeviceMemory& device_memory_, + Process* owner_process_, KPageLinkedList&& page_list_, + KMemoryPermission owner_permission_, + KMemoryPermission user_permission_, PAddr physical_address_, + std::size_t size_, std::string name_) { - const auto resource_limit = kernel.GetSystemResourceLimit(); + resource_limit = kernel_.GetSystemResourceLimit(); KScopedResourceReservation memory_reservation(resource_limit, LimitableResource::PhysicalMemory, - size); + size_); ASSERT(memory_reservation.Succeeded()); - std::shared_ptr shared_memory{ - std::make_shared(kernel, device_memory)}; - - shared_memory->owner_process = owner_process; - shared_memory->page_list = std::move(page_list); - shared_memory->owner_permission = owner_permission; - shared_memory->user_permission = user_permission; - shared_memory->physical_address = physical_address; - shared_memory->size = size; - shared_memory->name = name; + owner_process = owner_process_; + device_memory = &device_memory_; + page_list = std::move(page_list_); + owner_permission = owner_permission_; + user_permission = user_permission_; + physical_address = physical_address_; + size = size_; + name = name_; + is_initialized = true; memory_reservation.Commit(); - return shared_memory; + + return RESULT_SUCCESS; +} + +void KSharedMemory::Finalize() { + ///* Get the number of pages. */ + // const size_t num_pages = m_page_group.GetNumPages(); + // const size_t size = num_pages * PageSize; + + ///* Close and finalize the page group. */ + // m_page_group.Close(); + // m_page_group.Finalize(); + + // Release the memory reservation. + resource_limit->Release(LimitableResource::PhysicalMemory, size); + resource_limit->Close(); + + // Perform inherited finalization. + KAutoObjectWithSlabHeapAndContainer::Finalize(); } ResultCode KSharedMemory::Map(Process& target_process, VAddr address, std::size_t size, diff --git a/src/core/hle/kernel/k_shared_memory.h b/src/core/hle/kernel/k_shared_memory.h index 016e34be5..aaa773bfc 100644 --- a/src/core/hle/kernel/k_shared_memory.h +++ b/src/core/hle/kernel/k_shared_memory.h @@ -11,24 +11,26 @@ #include "core/device_memory.h" #include "core/hle/kernel/k_memory_block.h" #include "core/hle/kernel/k_page_linked_list.h" -#include "core/hle/kernel/object.h" #include "core/hle/kernel/process.h" +#include "core/hle/kernel/slab_helpers.h" #include "core/hle/result.h" namespace Kernel { class KernelCore; -class KSharedMemory final : public Object { +class KSharedMemory final + : public KAutoObjectWithSlabHeapAndContainer { + KERNEL_AUTOOBJECT_TRAITS(KSharedMemory, KAutoObject); + public: - explicit KSharedMemory(KernelCore& kernel, Core::DeviceMemory& device_memory); + explicit KSharedMemory(KernelCore& kernel); ~KSharedMemory() override; - static std::shared_ptr Create( - KernelCore& kernel, Core::DeviceMemory& device_memory, Process* owner_process, - KPageLinkedList&& page_list, KMemoryPermission owner_permission, - KMemoryPermission user_permission, PAddr physical_address, std::size_t size, - std::string name); + ResultCode Initialize(KernelCore& kernel_, Core::DeviceMemory& device_memory_, + Process* owner_process_, KPageLinkedList&& page_list_, + KMemoryPermission owner_permission_, KMemoryPermission user_permission_, + PAddr physical_address_, std::size_t size_, std::string name_); std::string GetTypeName() const override { return "SharedMemory"; @@ -59,7 +61,7 @@ public: * @return A pointer to the shared memory block from the specified offset */ u8* GetPointer(std::size_t offset = 0) { - return device_memory.GetPointer(physical_address + offset); + return device_memory->GetPointer(physical_address + offset); } /** @@ -68,20 +70,26 @@ public: * @return A pointer to the shared memory block from the specified offset */ const u8* GetPointer(std::size_t offset = 0) const { - return device_memory.GetPointer(physical_address + offset); + return device_memory->GetPointer(physical_address + offset); } - void Finalize() override {} + virtual void Finalize() override; + + virtual bool IsInitialized() const override { + return is_initialized; + } + static void PostDestroy([[maybe_unused]] uintptr_t arg) {} private: - Core::DeviceMemory& device_memory; + Core::DeviceMemory* device_memory; Process* owner_process{}; KPageLinkedList page_list; KMemoryPermission owner_permission{}; KMemoryPermission user_permission{}; PAddr physical_address{}; std::size_t size{}; - std::string name; + std::shared_ptr resource_limit; + bool is_initialized{}; }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp index 5d0b266c5..fbab5ecb7 100644 --- a/src/core/hle/kernel/k_thread.cpp +++ b/src/core/hle/kernel/k_thread.cpp @@ -291,6 +291,9 @@ void KThread::Finalize() { parent->DecrementThreadCount(); parent->GetResourceLimit()->Release(LimitableResource::Threads, 1); } + + // Perform inherited finalization. + KAutoObjectWithSlabHeapAndContainer::Finalize(); } bool KThread::IsSignaled() const { diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 1b5b11564..cac76a6ec 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -575,22 +575,27 @@ struct KernelCore::Impl { const PAddr irs_phys_addr{system_pool.GetAddress() + hid_size + font_size}; const PAddr time_phys_addr{system_pool.GetAddress() + hid_size + font_size + irs_size}; - hid_shared_mem = Kernel::KSharedMemory::Create( - system.Kernel(), system.DeviceMemory(), nullptr, {hid_phys_addr, hid_size / PageSize}, - KMemoryPermission::None, KMemoryPermission::Read, hid_phys_addr, hid_size, - "HID:SharedMemory"); - font_shared_mem = Kernel::KSharedMemory::Create( - system.Kernel(), system.DeviceMemory(), nullptr, {font_phys_addr, font_size / PageSize}, - KMemoryPermission::None, KMemoryPermission::Read, font_phys_addr, font_size, - "Font:SharedMemory"); - irs_shared_mem = Kernel::KSharedMemory::Create( - system.Kernel(), system.DeviceMemory(), nullptr, {irs_phys_addr, irs_size / PageSize}, - KMemoryPermission::None, KMemoryPermission::Read, irs_phys_addr, irs_size, - "IRS:SharedMemory"); - time_shared_mem = Kernel::KSharedMemory::Create( - system.Kernel(), system.DeviceMemory(), nullptr, {time_phys_addr, time_size / PageSize}, - KMemoryPermission::None, KMemoryPermission::Read, time_phys_addr, time_size, - "Time:SharedMemory"); + hid_shared_mem = std::make_unique(system.Kernel()); + font_shared_mem = std::make_unique(system.Kernel()); + irs_shared_mem = std::make_unique(system.Kernel()); + time_shared_mem = std::make_unique(system.Kernel()); + + hid_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr, + {hid_phys_addr, hid_size / PageSize}, KMemoryPermission::None, + KMemoryPermission::Read, hid_phys_addr, hid_size, + "HID:SharedMemory"); + font_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr, + {font_phys_addr, font_size / PageSize}, KMemoryPermission::None, + KMemoryPermission::Read, font_phys_addr, font_size, + "Font:SharedMemory"); + irs_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr, + {irs_phys_addr, irs_size / PageSize}, KMemoryPermission::None, + KMemoryPermission::Read, irs_phys_addr, irs_size, + "IRS:SharedMemory"); + time_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr, + {time_phys_addr, time_size / PageSize}, KMemoryPermission::None, + KMemoryPermission::Read, time_phys_addr, time_size, + "Time:SharedMemory"); } void InitializePageSlab() { @@ -644,10 +649,10 @@ struct KernelCore::Impl { std::unique_ptr> user_slab_heap_pages; // Shared memory for services - std::shared_ptr hid_shared_mem; - std::shared_ptr font_shared_mem; - std::shared_ptr irs_shared_mem; - std::shared_ptr time_shared_mem; + std::unique_ptr hid_shared_mem; + std::unique_ptr font_shared_mem; + std::unique_ptr irs_shared_mem; + std::unique_ptr time_shared_mem; // Threads used for services std::unordered_set> service_threads; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 7d676e5f7..17d63658a 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -328,7 +328,7 @@ static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle, // Return the client session auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); - CASCADE_RESULT(*out_handle, handle_table.Create(client_session)); + CASCADE_RESULT(*out_handle, handle_table.Create(client_session.get())); return RESULT_SUCCESS; } @@ -836,7 +836,7 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, u64 ha return RESULT_SUCCESS; } - const auto table_result = handle_table.Create(resource_limit); + const auto table_result = handle_table.Create(resource_limit.get()); if (table_result.Failed()) { return table_result.Code(); } @@ -1168,12 +1168,9 @@ static ResultCode MapSharedMemory(Core::System& system, Handle shared_memory_han return ResultInvalidMemoryRange; } - auto shared_memory{current_process->GetHandleTable().Get(shared_memory_handle)}; - if (!shared_memory) { - LOG_ERROR(Kernel_SVC, "Shared memory does not exist, shared_memory_handle=0x{:08X}", - shared_memory_handle); - return ResultInvalidHandle; - } + auto shared_memory{ + current_process->GetHandleTable().GetObject(shared_memory_handle)}; + R_UNLESS(shared_memory.IsNotNull(), ResultInvalidHandle); return shared_memory->Map(*current_process, addr, size, static_cast(permission_type)); @@ -1817,7 +1814,7 @@ static ResultCode CreateTransferMemory(Core::System& system, Handle* handle, VAd } auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); - const auto result{handle_table.Create(std::move(transfer_mem_handle))}; + const auto result{handle_table.Create(transfer_mem_handle.get())}; if (result.Failed()) { return result.Code(); } @@ -1966,7 +1963,7 @@ static ResultCode CreateEvent(Core::System& system, Handle* out_write, Handle* o event->Initialize(); // Add the writable event to the handle table. - const auto write_create_result = handle_table.Create(event->GetWritableEvent()); + const auto write_create_result = handle_table.Create(event->GetWritableEvent().get()); if (write_create_result.Failed()) { return write_create_result.Code(); } @@ -1976,7 +1973,7 @@ static ResultCode CreateEvent(Core::System& system, Handle* out_write, Handle* o auto handle_guard = SCOPE_GUARD({ handle_table.Remove(*write_create_result); }); // Add the readable event to the handle table. - const auto read_create_result = handle_table.Create(SharedFrom(event->GetReadableEvent())); + const auto read_create_result = handle_table.Create(event->GetReadableEvent()); if (read_create_result.Failed()) { return read_create_result.Code(); } @@ -2027,7 +2024,7 @@ static ResultCode CreateResourceLimit(Core::System& system, Handle* out_handle) auto* const current_process = kernel.CurrentProcess(); ASSERT(current_process != nullptr); - const auto handle = current_process->GetHandleTable().Create(std::move(resource_limit)); + const auto handle = current_process->GetHandleTable().Create(resource_limit.get()); if (handle.Failed()) { return handle.Code(); } diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index d2a6375f5..49c1db42a 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -53,9 +53,6 @@ IAppletResource::IAppletResource(Core::System& system_) }; RegisterHandlers(functions); - auto& kernel = system.Kernel(); - shared_mem = SharedFrom(&kernel.GetHidSharedMem()); - MakeController(HidController::DebugPad); MakeController(HidController::Touchscreen); MakeController(HidController::Mouse); @@ -118,7 +115,7 @@ void IAppletResource::GetSharedMemoryHandle(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(shared_mem.get()); + rb.PushCopyObjects(&system.Kernel().GetHidSharedMem()); } void IAppletResource::UpdateControllers(std::uintptr_t user_data, @@ -130,7 +127,8 @@ void IAppletResource::UpdateControllers(std::uintptr_t user_data, if (should_reload) { controller->OnLoadInputDevices(); } - controller->OnUpdate(core_timing, shared_mem->GetPointer(), SHARED_MEMORY_SIZE); + controller->OnUpdate(core_timing, system.Kernel().GetHidSharedMem().GetPointer(), + SHARED_MEMORY_SIZE); } // If ns_late is higher than the update rate ignore the delay @@ -145,7 +143,7 @@ void IAppletResource::UpdateMotion(std::uintptr_t user_data, std::chrono::nanose auto& core_timing = system.CoreTiming(); controllers[static_cast(HidController::NPad)]->OnMotionUpdate( - core_timing, shared_mem->GetPointer(), SHARED_MEMORY_SIZE); + core_timing, system.Kernel().GetHidSharedMem().GetPointer(), SHARED_MEMORY_SIZE); // If ns_late is higher than the update rate ignore the delay if (ns_late > motion_update_ns) { diff --git a/src/core/hle/service/hid/hid.h b/src/core/hle/service/hid/hid.h index c2bdd39a3..aa3307955 100644 --- a/src/core/hle/service/hid/hid.h +++ b/src/core/hle/service/hid/hid.h @@ -13,10 +13,6 @@ namespace Core::Timing { struct EventType; } -namespace Kernel { -class KSharedMemory; -} - namespace Service::SM { class ServiceManager; } @@ -69,8 +65,6 @@ private: void UpdateControllers(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); void UpdateMotion(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); - std::shared_ptr shared_mem; - std::shared_ptr pad_update_event; std::shared_ptr motion_update_event; diff --git a/src/core/hle/service/hid/irs.cpp b/src/core/hle/service/hid/irs.cpp index b50a51eb7..3c6085990 100644 --- a/src/core/hle/service/hid/irs.cpp +++ b/src/core/hle/service/hid/irs.cpp @@ -37,10 +37,6 @@ IRS::IRS(Core::System& system_) : ServiceFramework{system_, "irs"} { // clang-format on RegisterHandlers(functions); - - auto& kernel = system.Kernel(); - - shared_mem = SharedFrom(&kernel.GetIrsSharedMem()); } void IRS::ActivateIrsensor(Kernel::HLERequestContext& ctx) { @@ -62,7 +58,7 @@ void IRS::GetIrsensorSharedMemoryHandle(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(shared_mem.get()); + rb.PushCopyObjects(&system.Kernel().GetIrsSharedMem()); } void IRS::StopImageProcessor(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/hid/irs.h b/src/core/hle/service/hid/irs.h index b0c8c7168..a1bcb5859 100644 --- a/src/core/hle/service/hid/irs.h +++ b/src/core/hle/service/hid/irs.h @@ -11,10 +11,6 @@ namespace Core { class System; } -namespace Kernel { -class KSharedMemory; -} - namespace Service::HID { class IRS final : public ServiceFramework { @@ -42,7 +38,6 @@ private: void StopImageProcessorAsync(Kernel::HLERequestContext& ctx); void ActivateIrsensorWithFunctionLevel(Kernel::HLERequestContext& ctx); - std::shared_ptr shared_mem; const u32 device_handle{0xABCD}; }; diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp index e6616a3b9..82d847130 100644 --- a/src/core/hle/service/ns/pl_u.cpp +++ b/src/core/hle/service/ns/pl_u.cpp @@ -130,9 +130,6 @@ struct PL_U::Impl { } } - /// Handle to shared memory region designated for a shared font - std::shared_ptr shared_font_mem; - /// Backing memory for the shared font data std::shared_ptr shared_font; @@ -260,14 +257,13 @@ void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) { // Create shared font memory object auto& kernel = system.Kernel(); - impl->shared_font_mem = SharedFrom(&kernel.GetFontSharedMem()); - std::memcpy(impl->shared_font_mem->GetPointer(), impl->shared_font->data(), + std::memcpy(system.Kernel().GetFontSharedMem().GetPointer(), impl->shared_font->data(), impl->shared_font->size()); IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(impl->shared_font_mem.get()); + rb.PushCopyObjects(&system.Kernel().GetFontSharedMem()); } void PL_U::GetSharedFontInOrderOfPriority(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/time/time_sharedmemory.cpp b/src/core/hle/service/time/time_sharedmemory.cpp index 018ce94ed..eb57899f6 100644 --- a/src/core/hle/service/time/time_sharedmemory.cpp +++ b/src/core/hle/service/time/time_sharedmemory.cpp @@ -16,16 +16,11 @@ namespace Service::Time { static constexpr std::size_t SHARED_MEMORY_SIZE{0x1000}; SharedMemory::SharedMemory(Core::System& system) : system(system) { - shared_memory_holder = SharedFrom(&system.Kernel().GetTimeSharedMem()); - std::memset(shared_memory_holder->GetPointer(), 0, SHARED_MEMORY_SIZE); + std::memset(system.Kernel().GetTimeSharedMem().GetPointer(), 0, SHARED_MEMORY_SIZE); } SharedMemory::~SharedMemory() = default; -std::shared_ptr SharedMemory::GetSharedMemoryHolder() const { - return shared_memory_holder; -} - void SharedMemory::SetupStandardSteadyClock(const Common::UUID& clock_source_id, Clock::TimeSpanType current_time_point) { const Clock::TimeSpanType ticks_time_span{Clock::TimeSpanType::FromTicks( @@ -34,22 +29,22 @@ void SharedMemory::SetupStandardSteadyClock(const Common::UUID& clock_source_id, static_cast(current_time_point.nanoseconds - ticks_time_span.nanoseconds), clock_source_id}; shared_memory_format.standard_steady_clock_timepoint.StoreData( - shared_memory_holder->GetPointer(), context); + system.Kernel().GetTimeSharedMem().GetPointer(), context); } void SharedMemory::UpdateLocalSystemClockContext(const Clock::SystemClockContext& context) { shared_memory_format.standard_local_system_clock_context.StoreData( - shared_memory_holder->GetPointer(), context); + system.Kernel().GetTimeSharedMem().GetPointer(), context); } void SharedMemory::UpdateNetworkSystemClockContext(const Clock::SystemClockContext& context) { shared_memory_format.standard_network_system_clock_context.StoreData( - shared_memory_holder->GetPointer(), context); + system.Kernel().GetTimeSharedMem().GetPointer(), context); } void SharedMemory::SetAutomaticCorrectionEnabled(bool is_enabled) { shared_memory_format.standard_user_system_clock_automatic_correction.StoreData( - shared_memory_holder->GetPointer(), is_enabled); + system.Kernel().GetTimeSharedMem().GetPointer(), is_enabled); } } // namespace Service::Time diff --git a/src/core/hle/service/time/time_sharedmemory.h b/src/core/hle/service/time/time_sharedmemory.h index 3bc749114..1ad9a286d 100644 --- a/src/core/hle/service/time/time_sharedmemory.h +++ b/src/core/hle/service/time/time_sharedmemory.h @@ -17,9 +17,6 @@ public: explicit SharedMemory(Core::System& system); ~SharedMemory(); - // Return the shared memory handle - std::shared_ptr GetSharedMemoryHolder() const; - // TODO(ogniK): We have to properly simulate memory barriers, how are we going to do this? template struct MemoryBarrier { @@ -63,7 +60,6 @@ public: void SetAutomaticCorrectionEnabled(bool is_enabled); private: - std::shared_ptr shared_memory_holder; Core::System& system; Format shared_memory_format{}; }; From addc0bf0379e075786048921bede6e089552a6db Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 4 Apr 2021 00:56:09 -0700 Subject: [PATCH 21/82] hle: kernel: Migrate KEvent to KAutoObject. --- src/core/hle/kernel/handle_table.cpp | 1 + src/core/hle/kernel/init/init_slab_setup.cpp | 2 + src/core/hle/kernel/k_event.cpp | 38 +++++++-- src/core/hle/kernel/k_event.h | 23 ++++-- src/core/hle/kernel/object.h | 2 + src/core/hle/kernel/svc.cpp | 9 +- src/core/hle/service/am/am.cpp | 82 +++++++++---------- src/core/hle/service/am/am.h | 18 ++-- src/core/hle/service/am/applets/applets.cpp | 34 ++++---- src/core/hle/service/am/applets/applets.h | 7 +- src/core/hle/service/aoc/aoc_u.cpp | 20 ++--- src/core/hle/service/aoc/aoc_u.h | 3 +- src/core/hle/service/audio/audout_u.cpp | 15 ++-- src/core/hle/service/audio/audren_u.cpp | 41 ++++------ src/core/hle/service/bcat/backend/backend.cpp | 14 ++-- src/core/hle/service/bcat/backend/backend.h | 3 +- src/core/hle/service/btdrv/btdrv.cpp | 11 ++- src/core/hle/service/btm/btm.cpp | 34 ++++---- src/core/hle/service/friend/friend.cpp | 11 ++- src/core/hle/service/hid/controllers/npad.cpp | 5 +- src/core/hle/service/hid/controllers/npad.h | 2 +- src/core/hle/service/nfp/nfp.cpp | 31 +++---- src/core/hle/service/nfp/nfp.h | 3 +- src/core/hle/service/nifm/nifm.cpp | 13 ++- src/core/hle/service/nim/nim.cpp | 16 ++-- .../hle/service/nvdrv/devices/nvhost_ctrl.cpp | 2 +- src/core/hle/service/nvdrv/nvdrv.cpp | 12 ++- src/core/hle/service/nvdrv/nvdrv.h | 2 +- .../hle/service/nvflinger/buffer_queue.cpp | 18 ++-- src/core/hle/service/nvflinger/buffer_queue.h | 3 +- src/core/hle/service/nvflinger/nvflinger.cpp | 4 +- src/core/hle/service/nvflinger/nvflinger.h | 5 +- src/core/hle/service/ptm/psm.cpp | 17 ++-- .../time/standard_user_system_clock_core.cpp | 8 +- .../time/standard_user_system_clock_core.h | 3 +- .../hle/service/vi/display/vi_display.cpp | 11 ++- src/core/hle/service/vi/display/vi_display.h | 12 ++- 37 files changed, 269 insertions(+), 266 deletions(-) diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index 427c6fc1b..58c49460f 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -53,6 +53,7 @@ ResultVal HandleTable::Create(Object* obj) { switch (obj->GetHandleType()) { case HandleType::SharedMemory: case HandleType::Thread: + case HandleType::Event: case HandleType::Process: { Handle handle{}; Add(&handle, reinterpret_cast(obj), {}); diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index eb9c8e2e4..b292f7db2 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -9,6 +9,7 @@ #include "core/core.h" #include "core/hardware_properties.h" #include "core/hle/kernel/init/init_slab_setup.h" +#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" #include "core/hle/kernel/k_shared_memory.h" @@ -25,6 +26,7 @@ namespace Kernel::Init { #define FOREACH_SLAB_TYPE(HANDLER, ...) \ HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ + HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) namespace { diff --git a/src/core/hle/kernel/k_event.cpp b/src/core/hle/kernel/k_event.cpp index bb2fa4ad5..bc4a79cc8 100644 --- a/src/core/hle/kernel/k_event.cpp +++ b/src/core/hle/kernel/k_event.cpp @@ -4,29 +4,53 @@ #include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_readable_event.h" +#include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_writable_event.h" +#include "core/hle/kernel/process.h" namespace Kernel { -KEvent::KEvent(KernelCore& kernel, std::string&& name) : Object{kernel, std::move(name)} {} +KEvent::KEvent(KernelCore& kernel) : KAutoObjectWithSlabHeapAndContainer{kernel} {} KEvent::~KEvent() = default; -std::shared_ptr KEvent::Create(KernelCore& kernel, std::string&& name) { - return std::make_shared(kernel, std::move(name)); -} +void KEvent::Initialize(std::string&& name_) { + // Increment reference count. + // Because reference count is one on creation, this will result + // in a reference count of two. Thus, when both readable and + // writable events are closed this object will be destroyed. + Open(); -void KEvent::Initialize() { // Create our sub events. - readable_event = std::make_shared(kernel, GetName() + ":Readable"); - writable_event = std::make_shared(kernel, GetName() + ":Writable"); + readable_event = std::make_shared(kernel, name_ + ":Readable"); + writable_event = std::make_shared(kernel, name_ + ":Writable"); // Initialize our sub sessions. readable_event->Initialize(this); writable_event->Initialize(this); + // Set our owner process. + owner = kernel.CurrentProcess(); + if (owner) { + owner->Open(); + } + // Mark initialized. + name = std::move(name_); initialized = true; } +void KEvent::Finalize() { + KAutoObjectWithSlabHeapAndContainer::Finalize(); +} + +void KEvent::PostDestroy(uintptr_t arg) { + // Release the event count resource the owner process holds. + Process* owner = reinterpret_cast(arg); + if (owner) { + owner->GetResourceLimit()->Release(LimitableResource::Events, 1); + owner->Close(); + } +} + } // namespace Kernel diff --git a/src/core/hle/kernel/k_event.h b/src/core/hle/kernel/k_event.h index ec6894b16..97ec0ea9c 100644 --- a/src/core/hle/kernel/k_event.h +++ b/src/core/hle/kernel/k_event.h @@ -4,24 +4,34 @@ #pragma once -#include "core/hle/kernel/object.h" +#include "core/hle/kernel/slab_helpers.h" namespace Kernel { class KernelCore; class KReadableEvent; class KWritableEvent; +class Process; + +class KEvent final : public KAutoObjectWithSlabHeapAndContainer { + KERNEL_AUTOOBJECT_TRAITS(KEvent, KAutoObject); -class KEvent final : public Object { public: - explicit KEvent(KernelCore& kernel, std::string&& name); + explicit KEvent(KernelCore& kernel); ~KEvent() override; - static std::shared_ptr Create(KernelCore& kernel, std::string&& name); + void Initialize(std::string&& name); - void Initialize(); + virtual void Finalize() override; - void Finalize() override {} + virtual bool IsInitialized() const override { + return initialized; + } + virtual uintptr_t GetPostDestroyArgument() const override { + return reinterpret_cast(owner); + } + + static void PostDestroy(uintptr_t arg); std::string GetTypeName() const override { return "KEvent"; @@ -51,6 +61,7 @@ public: private: std::shared_ptr readable_event; std::shared_ptr writable_event; + Process* owner{}; bool initialized{}; }; diff --git a/src/core/hle/kernel/object.h b/src/core/hle/kernel/object.h index 5c14aa46f..03443b947 100644 --- a/src/core/hle/kernel/object.h +++ b/src/core/hle/kernel/object.h @@ -71,6 +71,8 @@ protected: private: std::atomic object_id{0}; + +protected: std::string name; }; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 17d63658a..b143a51c7 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -1953,14 +1953,11 @@ static ResultCode CreateEvent(Core::System& system, Handle* out_write, Handle* o HandleTable& handle_table = kernel.CurrentProcess()->GetHandleTable(); // Create a new event. - const auto event = KEvent::Create(kernel, "CreateEvent"); - if (!event) { - LOG_ERROR(Kernel_SVC, "Unable to create new events. Event creation limit reached."); - return ResultOutOfResource; - } + KEvent* event = KEvent::CreateWithKernel(kernel); + R_UNLESS(event != nullptr, ResultOutOfResource); // Initialize the event. - event->Initialize(); + event->Initialize("CreateEvent"); // Add the writable event to the handle table. const auto write_create_result = handle_table.Create(event->GetWritableEvent().get()); diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 7be94446a..3800c65ad 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -253,7 +253,8 @@ IDebugFunctions::IDebugFunctions(Core::System& system_) IDebugFunctions::~IDebugFunctions() = default; ISelfController::ISelfController(Core::System& system_, NVFlinger::NVFlinger& nvflinger_) - : ServiceFramework{system_, "ISelfController"}, nvflinger{nvflinger_} { + : ServiceFramework{system_, "ISelfController"}, nvflinger{nvflinger_}, + launchable_event{system.Kernel()}, accumulated_suspended_tick_changed_event{system.Kernel()} { // clang-format off static const FunctionInfo functions[] = { {0, &ISelfController::Exit, "Exit"}, @@ -306,19 +307,17 @@ ISelfController::ISelfController(Core::System& system_, NVFlinger::NVFlinger& nv RegisterHandlers(functions); - auto& kernel = system.Kernel(); - launchable_event = Kernel::KEvent::Create(kernel, "ISelfController:LaunchableEvent"); - launchable_event->Initialize(); + launchable_event.Initialize("ISelfController:LaunchableEvent"); // This event is created by AM on the first time GetAccumulatedSuspendedTickChangedEvent() is // called. Yuzu can just create it unconditionally, since it doesn't need to support multiple // ISelfControllers. The event is signaled on creation, and on transition from suspended -> not // suspended if the event has previously been created by a call to // GetAccumulatedSuspendedTickChangedEvent. - accumulated_suspended_tick_changed_event = - Kernel::KEvent::Create(kernel, "ISelfController:AccumulatedSuspendedTickChangedEvent"); - accumulated_suspended_tick_changed_event->Initialize(); - accumulated_suspended_tick_changed_event->GetWritableEvent()->Signal(); + + accumulated_suspended_tick_changed_event.Initialize( + "ISelfController:AccumulatedSuspendedTickChangedEvent"); + accumulated_suspended_tick_changed_event.GetWritableEvent()->Signal(); } ISelfController::~ISelfController() = default; @@ -377,11 +376,11 @@ void ISelfController::LeaveFatalSection(Kernel::HLERequestContext& ctx) { void ISelfController::GetLibraryAppletLaunchableEvent(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_AM, "(STUBBED) called"); - launchable_event->GetWritableEvent()->Signal(); + launchable_event.GetWritableEvent()->Signal(); IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(launchable_event->GetReadableEvent()); + rb.PushCopyObjects(launchable_event.GetReadableEvent()); } void ISelfController::SetScreenShotPermission(Kernel::HLERequestContext& ctx) { @@ -560,7 +559,7 @@ void ISelfController::GetAccumulatedSuspendedTickChangedEvent(Kernel::HLERequest IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(accumulated_suspended_tick_changed_event->GetReadableEvent()); + rb.PushCopyObjects(accumulated_suspended_tick_changed_event.GetReadableEvent()); } void ISelfController::SetAlbumImageTakenNotificationEnabled(Kernel::HLERequestContext& ctx) { @@ -578,38 +577,36 @@ void ISelfController::SetAlbumImageTakenNotificationEnabled(Kernel::HLERequestCo rb.Push(RESULT_SUCCESS); } -AppletMessageQueue::AppletMessageQueue(Kernel::KernelCore& kernel) { - on_new_message = Kernel::KEvent::Create(kernel, "AMMessageQueue:OnMessageReceived"); - on_new_message->Initialize(); - on_operation_mode_changed = - Kernel::KEvent::Create(kernel, "AMMessageQueue:OperationModeChanged"); - on_operation_mode_changed->Initialize(); +AppletMessageQueue::AppletMessageQueue(Kernel::KernelCore& kernel) + : on_new_message{kernel}, on_operation_mode_changed{kernel} { + on_new_message.Initialize("AMMessageQueue:OnMessageReceived"); + on_operation_mode_changed.Initialize("AMMessageQueue:OperationModeChanged"); } AppletMessageQueue::~AppletMessageQueue() = default; Kernel::KReadableEvent* AppletMessageQueue::GetMessageReceiveEvent() const { - return on_new_message->GetReadableEvent(); + return on_new_message.GetReadableEvent().get(); } Kernel::KReadableEvent* AppletMessageQueue::GetOperationModeChangedEvent() const { - return on_operation_mode_changed->GetReadableEvent(); + return on_operation_mode_changed.GetReadableEvent().get(); } void AppletMessageQueue::PushMessage(AppletMessage msg) { messages.push(msg); - on_new_message->GetWritableEvent()->Signal(); + on_new_message.GetWritableEvent()->Signal(); } AppletMessageQueue::AppletMessage AppletMessageQueue::PopMessage() { if (messages.empty()) { - on_new_message->GetWritableEvent()->Clear(); + on_new_message.GetWritableEvent()->Clear(); return AppletMessage::NoMessage; } auto msg = messages.front(); messages.pop(); if (messages.empty()) { - on_new_message->GetWritableEvent()->Clear(); + on_new_message.GetWritableEvent()->Clear(); } return msg; } @@ -629,7 +626,7 @@ void AppletMessageQueue::FocusStateChanged() { void AppletMessageQueue::OperationModeChanged() { PushMessage(AppletMessage::OperationModeChanged); PushMessage(AppletMessage::PerformanceModeChanged); - on_operation_mode_changed->GetWritableEvent()->Signal(); + on_operation_mode_changed.GetWritableEvent()->Signal(); } ICommonStateGetter::ICommonStateGetter(Core::System& system_, @@ -1212,16 +1209,16 @@ void ILibraryAppletCreator::CreateTransferMemoryStorage(Kernel::HLERequestContex } auto transfer_mem = - system.CurrentProcess()->GetHandleTable().Get(handle); + system.CurrentProcess()->GetHandleTable().GetObject(handle); - if (transfer_mem == nullptr) { + if (transfer_mem.IsNull()) { LOG_ERROR(Service_AM, "transfer_mem is a nullptr for handle={:08X}", handle); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(RESULT_UNKNOWN); return; } - const u8* const mem_begin = transfer_mem->GetPointer(); + const u8* const mem_begin = system.Memory().GetPointer(transfer_mem->GetSourceAddress()); const u8* const mem_end = mem_begin + transfer_mem->GetSize(); std::vector memory{mem_begin, mem_end}; @@ -1265,7 +1262,9 @@ void ILibraryAppletCreator::CreateHandleStorage(Kernel::HLERequestContext& ctx) } IApplicationFunctions::IApplicationFunctions(Core::System& system_) - : ServiceFramework{system_, "IApplicationFunctions"} { + : ServiceFramework{system_, "IApplicationFunctions"}, gpu_error_detected_event{system.Kernel()}, + friend_invitation_storage_channel_event{system.Kernel()}, + health_warning_disappeared_system_event{system.Kernel()} { // clang-format off static const FunctionInfo functions[] = { {1, &IApplicationFunctions::PopLaunchParameter, "PopLaunchParameter"}, @@ -1334,15 +1333,11 @@ IApplicationFunctions::IApplicationFunctions(Core::System& system_) RegisterHandlers(functions); auto& kernel = system.Kernel(); - gpu_error_detected_event = - Kernel::KEvent::Create(kernel, "IApplicationFunctions:GpuErrorDetectedSystemEvent"); - gpu_error_detected_event->Initialize(); - friend_invitation_storage_channel_event = - Kernel::KEvent::Create(kernel, "IApplicationFunctions:FriendInvitationStorageChannelEvent"); - friend_invitation_storage_channel_event->Initialize(); - health_warning_disappeared_system_event = - Kernel::KEvent::Create(kernel, "IApplicationFunctions:HealthWarningDisappearedSystemEvent"); - health_warning_disappeared_system_event->Initialize(); + gpu_error_detected_event.Initialize("IApplicationFunctions:GpuErrorDetectedSystemEvent"); + friend_invitation_storage_channel_event.Initialize( + "IApplicationFunctions:FriendInvitationStorageChannelEvent"); + health_warning_disappeared_system_event.Initialize( + "IApplicationFunctions:HealthWarningDisappearedSystemEvent"); } IApplicationFunctions::~IApplicationFunctions() = default; @@ -1739,7 +1734,7 @@ void IApplicationFunctions::GetGpuErrorDetectedSystemEvent(Kernel::HLERequestCon IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(gpu_error_detected_event->GetReadableEvent()); + rb.PushCopyObjects(gpu_error_detected_event.GetReadableEvent()); } void IApplicationFunctions::GetFriendInvitationStorageChannelEvent(Kernel::HLERequestContext& ctx) { @@ -1747,7 +1742,7 @@ void IApplicationFunctions::GetFriendInvitationStorageChannelEvent(Kernel::HLERe IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(friend_invitation_storage_channel_event->GetReadableEvent()); + rb.PushCopyObjects(friend_invitation_storage_channel_event.GetReadableEvent()); } void IApplicationFunctions::TryPopFromFriendInvitationStorageChannel( @@ -1763,7 +1758,7 @@ void IApplicationFunctions::GetHealthWarningDisappearedSystemEvent(Kernel::HLERe IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(health_warning_disappeared_system_event->GetReadableEvent()); + rb.PushCopyObjects(health_warning_disappeared_system_event.GetReadableEvent()); } void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger& nvflinger, @@ -1781,7 +1776,8 @@ void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger } IHomeMenuFunctions::IHomeMenuFunctions(Core::System& system_) - : ServiceFramework{system_, "IHomeMenuFunctions"} { + : ServiceFramework{system_, "IHomeMenuFunctions"}, pop_from_general_channel_event{ + system.Kernel()} { // clang-format off static const FunctionInfo functions[] = { {10, &IHomeMenuFunctions::RequestToGetForeground, "RequestToGetForeground"}, @@ -1802,9 +1798,7 @@ IHomeMenuFunctions::IHomeMenuFunctions(Core::System& system_) RegisterHandlers(functions); - pop_from_general_channel_event = - Kernel::KEvent::Create(system.Kernel(), "IHomeMenuFunctions:PopFromGeneralChannelEvent"); - pop_from_general_channel_event->Initialize(); + pop_from_general_channel_event.Initialize("IHomeMenuFunctions:PopFromGeneralChannelEvent"); } IHomeMenuFunctions::~IHomeMenuFunctions() = default; @@ -1821,7 +1815,7 @@ void IHomeMenuFunctions::GetPopFromGeneralChannelEvent(Kernel::HLERequestContext IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(pop_from_general_channel_event->GetReadableEvent()); + rb.PushCopyObjects(pop_from_general_channel_event.GetReadableEvent()); } IGlobalStateController::IGlobalStateController(Core::System& system_) diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index f8daeb437..8f6017c4e 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -8,11 +8,11 @@ #include #include +#include "core/hle/kernel/k_event.h" #include "core/hle/service/service.h" namespace Kernel { class KernelCore; -class KEvent; class TransferMemory; } // namespace Kernel @@ -67,8 +67,8 @@ public: private: std::queue messages; - std::shared_ptr on_new_message; - std::shared_ptr on_operation_mode_changed; + Kernel::KEvent on_new_message; + Kernel::KEvent on_operation_mode_changed; }; class IWindowController final : public ServiceFramework { @@ -156,8 +156,8 @@ private: }; NVFlinger::NVFlinger& nvflinger; - std::shared_ptr launchable_event; - std::shared_ptr accumulated_suspended_tick_changed_event; + Kernel::KEvent launchable_event; + Kernel::KEvent accumulated_suspended_tick_changed_event; u32 idle_time_detection_extension = 0; u64 num_fatal_sections_entered = 0; @@ -300,9 +300,9 @@ private: bool launch_popped_application_specific = false; bool launch_popped_account_preselect = false; s32 previous_program_index{-1}; - std::shared_ptr gpu_error_detected_event; - std::shared_ptr friend_invitation_storage_channel_event; - std::shared_ptr health_warning_disappeared_system_event; + Kernel::KEvent gpu_error_detected_event; + Kernel::KEvent friend_invitation_storage_channel_event; + Kernel::KEvent health_warning_disappeared_system_event; }; class IHomeMenuFunctions final : public ServiceFramework { @@ -314,7 +314,7 @@ private: void RequestToGetForeground(Kernel::HLERequestContext& ctx); void GetPopFromGeneralChannelEvent(Kernel::HLERequestContext& ctx); - std::shared_ptr pop_from_general_channel_event; + Kernel::KEvent pop_from_general_channel_event; }; class IGlobalStateController final : public ServiceFramework { diff --git a/src/core/hle/service/am/applets/applets.cpp b/src/core/hle/service/am/applets/applets.cpp index c093813fe..58bff810d 100644 --- a/src/core/hle/service/am/applets/applets.cpp +++ b/src/core/hle/service/am/applets/applets.cpp @@ -12,7 +12,6 @@ #include "core/frontend/applets/profile_select.h" #include "core/frontend/applets/software_keyboard.h" #include "core/frontend/applets/web_browser.h" -#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/server_session.h" @@ -31,16 +30,11 @@ namespace Service::AM::Applets { AppletDataBroker::AppletDataBroker(Core::System& system_, LibraryAppletMode applet_mode_) - : system{system_}, applet_mode{applet_mode_} { - state_changed_event = - Kernel::KEvent::Create(system.Kernel(), "ILibraryAppletAccessor:StateChangedEvent"); - state_changed_event->Initialize(); - pop_out_data_event = - Kernel::KEvent::Create(system.Kernel(), "ILibraryAppletAccessor:PopDataOutEvent"); - pop_out_data_event->Initialize(); - pop_interactive_out_data_event = Kernel::KEvent::Create( - system.Kernel(), "ILibraryAppletAccessor:PopInteractiveDataOutEvent"); - pop_interactive_out_data_event->Initialize(); + : system{system_}, applet_mode{applet_mode_}, state_changed_event{system.Kernel()}, + pop_out_data_event{system.Kernel()}, pop_interactive_out_data_event{system.Kernel()} { + state_changed_event.Initialize("ILibraryAppletAccessor:StateChangedEvent"); + pop_out_data_event.Initialize("ILibraryAppletAccessor:PopDataOutEvent"); + pop_interactive_out_data_event.Initialize("ILibraryAppletAccessor:PopInteractiveDataOutEvent"); } AppletDataBroker::~AppletDataBroker() = default; @@ -67,7 +61,7 @@ std::shared_ptr AppletDataBroker::PopNormalDataToGame() { auto out = std::move(out_channel.front()); out_channel.pop_front(); - pop_out_data_event->GetWritableEvent()->Clear(); + pop_out_data_event.GetWritableEvent()->Clear(); return out; } @@ -86,7 +80,7 @@ std::shared_ptr AppletDataBroker::PopInteractiveDataToGame() { auto out = std::move(out_interactive_channel.front()); out_interactive_channel.pop_front(); - pop_interactive_out_data_event->GetWritableEvent()->Clear(); + pop_interactive_out_data_event.GetWritableEvent()->Clear(); return out; } @@ -105,7 +99,7 @@ void AppletDataBroker::PushNormalDataFromGame(std::shared_ptr&& storag void AppletDataBroker::PushNormalDataFromApplet(std::shared_ptr&& storage) { out_channel.emplace_back(std::move(storage)); - pop_out_data_event->GetWritableEvent()->Signal(); + pop_out_data_event.GetWritableEvent()->Signal(); } void AppletDataBroker::PushInteractiveDataFromGame(std::shared_ptr&& storage) { @@ -114,11 +108,11 @@ void AppletDataBroker::PushInteractiveDataFromGame(std::shared_ptr&& s void AppletDataBroker::PushInteractiveDataFromApplet(std::shared_ptr&& storage) { out_interactive_channel.emplace_back(std::move(storage)); - pop_interactive_out_data_event->GetWritableEvent()->Signal(); + pop_interactive_out_data_event.GetWritableEvent()->Signal(); } -void AppletDataBroker::SignalStateChanged() const { - state_changed_event->GetWritableEvent()->Signal(); +void AppletDataBroker::SignalStateChanged() { + state_changed_event.GetWritableEvent()->Signal(); switch (applet_mode) { case LibraryAppletMode::AllForeground: @@ -143,15 +137,15 @@ void AppletDataBroker::SignalStateChanged() const { } Kernel::KReadableEvent* AppletDataBroker::GetNormalDataEvent() const { - return pop_out_data_event->GetReadableEvent(); + return pop_out_data_event.GetReadableEvent().get(); } Kernel::KReadableEvent* AppletDataBroker::GetInteractiveDataEvent() const { - return pop_interactive_out_data_event->GetReadableEvent(); + return pop_interactive_out_data_event.GetReadableEvent().get(); } Kernel::KReadableEvent* AppletDataBroker::GetStateChangedEvent() const { - return state_changed_event->GetReadableEvent(); + return state_changed_event.GetReadableEvent().get(); } Applet::Applet(Core::System& system_, LibraryAppletMode applet_mode_) diff --git a/src/core/hle/service/am/applets/applets.h b/src/core/hle/service/am/applets/applets.h index ffde8ced0..327843a98 100644 --- a/src/core/hle/service/am/applets/applets.h +++ b/src/core/hle/service/am/applets/applets.h @@ -8,6 +8,7 @@ #include #include "common/swap.h" +#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/object.h" union ResultCode; @@ -119,13 +120,13 @@ private: // PopInteractiveDataToGame and PushInteractiveDataFromApplet std::deque> out_interactive_channel; - std::shared_ptr state_changed_event; + Kernel::KEvent state_changed_event; // Signaled on PushNormalDataFromApplet - std::shared_ptr pop_out_data_event; + Kernel::KEvent pop_out_data_event; // Signaled on PushInteractiveDataFromApplet - std::shared_ptr pop_interactive_out_data_event; + Kernel::KEvent pop_interactive_out_data_event; }; class Applet { diff --git a/src/core/hle/service/aoc/aoc_u.cpp b/src/core/hle/service/aoc/aoc_u.cpp index 75867e349..12a025610 100644 --- a/src/core/hle/service/aoc/aoc_u.cpp +++ b/src/core/hle/service/aoc/aoc_u.cpp @@ -16,7 +16,6 @@ #include "core/file_sys/patch_manager.h" #include "core/file_sys/registered_cache.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" @@ -50,7 +49,7 @@ static std::vector AccumulateAOCTitleIDs(Core::System& system) { class IPurchaseEventManager final : public ServiceFramework { public: explicit IPurchaseEventManager(Core::System& system_) - : ServiceFramework{system_, "IPurchaseEventManager"} { + : ServiceFramework{system_, "IPurchaseEventManager"}, purchased_event{system.Kernel()} { // clang-format off static const FunctionInfo functions[] = { {0, &IPurchaseEventManager::SetDefaultDeliveryTarget, "SetDefaultDeliveryTarget"}, @@ -63,9 +62,7 @@ public: RegisterHandlers(functions); - purchased_event = - Kernel::KEvent::Create(system.Kernel(), "IPurchaseEventManager:PurchasedEvent"); - purchased_event->Initialize(); + purchased_event.Initialize("IPurchaseEventManager:PurchasedEvent"); } private: @@ -98,14 +95,15 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(purchased_event->GetReadableEvent()); + rb.PushCopyObjects(purchased_event.GetReadableEvent()); } - std::shared_ptr purchased_event; + Kernel::KEvent purchased_event; }; AOC_U::AOC_U(Core::System& system_) - : ServiceFramework{system_, "aoc:u"}, add_on_content{AccumulateAOCTitleIDs(system)} { + : ServiceFramework{system_, "aoc:u"}, add_on_content{AccumulateAOCTitleIDs(system)}, + aoc_change_event{system.Kernel()} { // clang-format off static const FunctionInfo functions[] = { {0, nullptr, "CountAddOnContentByApplicationId"}, @@ -127,9 +125,7 @@ AOC_U::AOC_U(Core::System& system_) RegisterHandlers(functions); - auto& kernel = system.Kernel(); - aoc_change_event = Kernel::KEvent::Create(kernel, "GetAddOnContentListChanged:Event"); - aoc_change_event->Initialize(); + aoc_change_event.Initialize("GetAddOnContentListChanged:Event"); } AOC_U::~AOC_U() = default; @@ -256,7 +252,7 @@ void AOC_U::GetAddOnContentListChangedEvent(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(aoc_change_event->GetReadableEvent()); + rb.PushCopyObjects(aoc_change_event.GetReadableEvent()); } void AOC_U::CreateEcPurchasedEventManager(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/aoc/aoc_u.h b/src/core/hle/service/aoc/aoc_u.h index 1aa23529e..65095baa2 100644 --- a/src/core/hle/service/aoc/aoc_u.h +++ b/src/core/hle/service/aoc/aoc_u.h @@ -4,6 +4,7 @@ #pragma once +#include "core/hle/kernel/k_event.h" #include "core/hle/service/service.h" namespace Core { @@ -31,7 +32,7 @@ private: void CreatePermanentEcPurchasedEventManager(Kernel::HLERequestContext& ctx); std::vector add_on_content; - std::shared_ptr aoc_change_event; + Kernel::KEvent aoc_change_event; }; /// Registers all AOC services with the specified service manager. diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp index 5f51fca9a..4052c8e60 100644 --- a/src/core/hle/service/audio/audout_u.cpp +++ b/src/core/hle/service/audio/audout_u.cpp @@ -43,9 +43,9 @@ class IAudioOut final : public ServiceFramework { public: IAudioOut(Core::System& system_, AudoutParams audio_params_, AudioCore::AudioOut& audio_core_, std::string&& device_name_, std::string&& unique_name) - : ServiceFramework{system_, "IAudioOut"}, audio_core{audio_core_}, - device_name{std::move(device_name_)}, audio_params{audio_params_}, main_memory{ - system.Memory()} { + : ServiceFramework{system_, "IAudioOut"}, audio_core{audio_core_}, device_name{std::move( + device_name_)}, + audio_params{audio_params_}, buffer_event{system.Kernel()}, main_memory{system.Memory()} { // clang-format off static const FunctionInfo functions[] = { {0, &IAudioOut::GetAudioOutState, "GetAudioOutState"}, @@ -67,13 +67,12 @@ public: RegisterHandlers(functions); // This is the event handle used to check if the audio buffer was released - buffer_event = Kernel::KEvent::Create(system.Kernel(), "IAudioOutBufferReleased"); - buffer_event->Initialize(); + buffer_event.Initialize("IAudioOutBufferReleased"); stream = audio_core.OpenStream(system.CoreTiming(), audio_params.sample_rate, audio_params.channel_count, std::move(unique_name), [this] { const auto guard = LockService(); - buffer_event->GetWritableEvent()->Signal(); + buffer_event.GetWritableEvent()->Signal(); }); } @@ -126,7 +125,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(buffer_event->GetReadableEvent()); + rb.PushCopyObjects(buffer_event.GetReadableEvent()); } void AppendAudioOutBufferImpl(Kernel::HLERequestContext& ctx) { @@ -220,7 +219,7 @@ private: [[maybe_unused]] AudoutParams audio_params{}; /// This is the event handle used to check if the audio buffer was released - std::shared_ptr buffer_event; + Kernel::KEvent buffer_event; Core::Memory::Memory& main_memory; }; diff --git a/src/core/hle/service/audio/audren_u.cpp b/src/core/hle/service/audio/audren_u.cpp index 3a48342fd..d573530df 100644 --- a/src/core/hle/service/audio/audren_u.cpp +++ b/src/core/hle/service/audio/audren_u.cpp @@ -30,7 +30,7 @@ public: explicit IAudioRenderer(Core::System& system_, const AudioCommon::AudioRendererParameter& audren_params, const std::size_t instance_number) - : ServiceFramework{system_, "IAudioRenderer"} { + : ServiceFramework{system_, "IAudioRenderer"}, system_event{system.Kernel()} { // clang-format off static const FunctionInfo functions[] = { {0, &IAudioRenderer::GetSampleRate, "GetSampleRate"}, @@ -49,13 +49,12 @@ public: // clang-format on RegisterHandlers(functions); - system_event = Kernel::KEvent::Create(system.Kernel(), "IAudioRenderer:SystemEvent"); - system_event->Initialize(); + system_event.Initialize("IAudioRenderer:SystemEvent"); renderer = std::make_unique( system.CoreTiming(), system.Memory(), audren_params, [this]() { const auto guard = LockService(); - system_event->GetWritableEvent()->Signal(); + system_event.GetWritableEvent()->Signal(); }, instance_number); } @@ -128,7 +127,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(system_event->GetReadableEvent()); + rb.PushCopyObjects(system_event.GetReadableEvent()); } void SetRenderingTimeLimit(Kernel::HLERequestContext& ctx) { @@ -162,7 +161,7 @@ private: rb.Push(ERR_NOT_SUPPORTED); } - std::shared_ptr system_event; + Kernel::KEvent system_event; std::unique_ptr renderer; u32 rendering_time_limit_percent = 100; }; @@ -170,7 +169,9 @@ private: class IAudioDevice final : public ServiceFramework { public: explicit IAudioDevice(Core::System& system_, u32_le revision_num) - : ServiceFramework{system_, "IAudioDevice"}, revision{revision_num} { + : ServiceFramework{system_, "IAudioDevice"}, revision{revision_num}, + buffer_event{system.Kernel()}, audio_input_device_switch_event{system.Kernel()}, + audio_output_device_switch_event{system.Kernel()} { static const FunctionInfo functions[] = { {0, &IAudioDevice::ListAudioDeviceName, "ListAudioDeviceName"}, {1, &IAudioDevice::SetAudioDeviceOutputVolume, "SetAudioDeviceOutputVolume"}, @@ -188,20 +189,14 @@ public: }; RegisterHandlers(functions); - auto& kernel = system.Kernel(); - buffer_event = Kernel::KEvent::Create(kernel, "IAudioOutBufferReleasedEvent"); - buffer_event->Initialize(); + buffer_event.Initialize("IAudioOutBufferReleasedEvent"); // Should be similar to audio_output_device_switch_event - audio_input_device_switch_event = - Kernel::KEvent::Create(kernel, "IAudioDevice:AudioInputDeviceSwitchedEvent"); - audio_input_device_switch_event->Initialize(); + audio_input_device_switch_event.Initialize("IAudioDevice:AudioInputDeviceSwitchedEvent"); // Should only be signalled when an audio output device has been changed, example: speaker // to headset - audio_output_device_switch_event = - Kernel::KEvent::Create(kernel, "IAudioDevice:AudioOutputDeviceSwitchedEvent"); - audio_output_device_switch_event->Initialize(); + audio_output_device_switch_event.Initialize("IAudioDevice:AudioOutputDeviceSwitchedEvent"); } private: @@ -290,11 +285,11 @@ private: void QueryAudioDeviceSystemEvent(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_Audio, "(STUBBED) called"); - buffer_event->GetWritableEvent()->Signal(); + buffer_event.GetWritableEvent()->Signal(); IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(buffer_event->GetReadableEvent()); + rb.PushCopyObjects(buffer_event.GetReadableEvent()); } void GetActiveChannelCount(Kernel::HLERequestContext& ctx) { @@ -311,7 +306,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(audio_input_device_switch_event->GetReadableEvent()); + rb.PushCopyObjects(audio_input_device_switch_event.GetReadableEvent()); } void QueryAudioDeviceOutputEvent(Kernel::HLERequestContext& ctx) { @@ -319,13 +314,13 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(audio_output_device_switch_event->GetReadableEvent()); + rb.PushCopyObjects(audio_output_device_switch_event.GetReadableEvent()); } u32_le revision = 0; - std::shared_ptr buffer_event; - std::shared_ptr audio_input_device_switch_event; - std::shared_ptr audio_output_device_switch_event; + Kernel::KEvent buffer_event; + Kernel::KEvent audio_input_device_switch_event; + Kernel::KEvent audio_output_device_switch_event; }; // namespace Audio diff --git a/src/core/hle/service/bcat/backend/backend.cpp b/src/core/hle/service/bcat/backend/backend.cpp index 7f301fdeb..932e70bfd 100644 --- a/src/core/hle/service/bcat/backend/backend.cpp +++ b/src/core/hle/service/bcat/backend/backend.cpp @@ -5,7 +5,6 @@ #include "common/hex_util.h" #include "common/logging/log.h" #include "core/core.h" -#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_writable_event.h" #include "core/hle/lock.h" @@ -14,14 +13,13 @@ namespace Service::BCAT { ProgressServiceBackend::ProgressServiceBackend(Kernel::KernelCore& kernel, - std::string_view event_name) { - event = Kernel::KEvent::Create(kernel, - "ProgressServiceBackend:UpdateEvent:" + std::string(event_name)); - event->Initialize(); + std::string_view event_name) + : update_event{kernel} { + update_event.Initialize("ProgressServiceBackend:UpdateEvent:" + std::string(event_name)); } std::shared_ptr ProgressServiceBackend::GetEvent() const { - return SharedFrom(event->GetReadableEvent()); + return update_event.GetReadableEvent(); } DeliveryCacheProgressImpl& ProgressServiceBackend::GetImpl() { @@ -89,9 +87,9 @@ void ProgressServiceBackend::FinishDownload(ResultCode result) { void ProgressServiceBackend::SignalUpdate() const { if (need_hle_lock) { std::lock_guard lock(HLE::g_hle_lock); - event->GetWritableEvent()->Signal(); + update_event.GetWritableEvent()->Signal(); } else { - event->GetWritableEvent()->Signal(); + update_event.GetWritableEvent()->Signal(); } } diff --git a/src/core/hle/service/bcat/backend/backend.h b/src/core/hle/service/bcat/backend/backend.h index db585b069..baa4d576a 100644 --- a/src/core/hle/service/bcat/backend/backend.h +++ b/src/core/hle/service/bcat/backend/backend.h @@ -11,6 +11,7 @@ #include "common/common_types.h" #include "core/file_sys/vfs_types.h" +#include "core/hle/kernel/k_event.h" #include "core/hle/result.h" namespace Core { @@ -104,7 +105,7 @@ private: void SignalUpdate() const; DeliveryCacheProgressImpl impl{}; - std::shared_ptr event; + Kernel::KEvent update_event; bool need_hle_lock = false; }; diff --git a/src/core/hle/service/btdrv/btdrv.cpp b/src/core/hle/service/btdrv/btdrv.cpp index af3a5842d..dde276ff4 100644 --- a/src/core/hle/service/btdrv/btdrv.cpp +++ b/src/core/hle/service/btdrv/btdrv.cpp @@ -17,7 +17,8 @@ namespace Service::BtDrv { class Bt final : public ServiceFramework { public: - explicit Bt(Core::System& system_) : ServiceFramework{system_, "bt"} { + explicit Bt(Core::System& system_) + : ServiceFramework{system_, "bt"}, register_event{system.Kernel()} { // clang-format off static const FunctionInfo functions[] = { {0, nullptr, "LeClientReadCharacteristic"}, @@ -34,9 +35,7 @@ public: // clang-format on RegisterHandlers(functions); - auto& kernel = system.Kernel(); - register_event = Kernel::KEvent::Create(kernel, "BT:RegisterEvent"); - register_event->Initialize(); + register_event.Initialize("BT:RegisterEvent"); } private: @@ -45,10 +44,10 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(register_event->GetReadableEvent()); + rb.PushCopyObjects(register_event.GetReadableEvent()); } - std::shared_ptr register_event; + Kernel::KEvent register_event; }; class BtDrv final : public ServiceFramework { diff --git a/src/core/hle/service/btm/btm.cpp b/src/core/hle/service/btm/btm.cpp index d1ebc2388..403296650 100644 --- a/src/core/hle/service/btm/btm.cpp +++ b/src/core/hle/service/btm/btm.cpp @@ -18,7 +18,10 @@ namespace Service::BTM { class IBtmUserCore final : public ServiceFramework { public: - explicit IBtmUserCore(Core::System& system_) : ServiceFramework{system_, "IBtmUserCore"} { + explicit IBtmUserCore(Core::System& system_) + : ServiceFramework{system_, "IBtmUserCore"}, scan_event{system.Kernel()}, + connection_event{system.Kernel()}, service_discovery{system.Kernel()}, + config_event{system.Kernel()} { // clang-format off static const FunctionInfo functions[] = { {0, &IBtmUserCore::AcquireBleScanEvent, "AcquireBleScanEvent"}, @@ -57,15 +60,10 @@ public: // clang-format on RegisterHandlers(functions); - auto& kernel = system.Kernel(); - scan_event = Kernel::KEvent::Create(kernel, "IBtmUserCore:ScanEvent"); - scan_event->Initialize(); - connection_event = Kernel::KEvent::Create(kernel, "IBtmUserCore:ConnectionEvent"); - connection_event->Initialize(); - service_discovery = Kernel::KEvent::Create(kernel, "IBtmUserCore:Discovery"); - service_discovery->Initialize(); - config_event = Kernel::KEvent::Create(kernel, "IBtmUserCore:ConfigEvent"); - config_event->Initialize(); + scan_event.Initialize("IBtmUserCore:ScanEvent"); + connection_event.Initialize("IBtmUserCore:ConnectionEvent"); + service_discovery.Initialize("IBtmUserCore:Discovery"); + config_event.Initialize("IBtmUserCore:ConfigEvent"); } private: @@ -74,7 +72,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(scan_event->GetReadableEvent()); + rb.PushCopyObjects(scan_event.GetReadableEvent()); } void AcquireBleConnectionEvent(Kernel::HLERequestContext& ctx) { @@ -82,7 +80,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(connection_event->GetReadableEvent()); + rb.PushCopyObjects(connection_event.GetReadableEvent()); } void AcquireBleServiceDiscoveryEvent(Kernel::HLERequestContext& ctx) { @@ -90,7 +88,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(service_discovery->GetReadableEvent()); + rb.PushCopyObjects(service_discovery.GetReadableEvent()); } void AcquireBleMtuConfigEvent(Kernel::HLERequestContext& ctx) { @@ -98,13 +96,13 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(config_event->GetReadableEvent()); + rb.PushCopyObjects(config_event.GetReadableEvent()); } - std::shared_ptr scan_event; - std::shared_ptr connection_event; - std::shared_ptr service_discovery; - std::shared_ptr config_event; + Kernel::KEvent scan_event; + Kernel::KEvent connection_event; + Kernel::KEvent service_discovery; + Kernel::KEvent config_event; }; class BTM_USR final : public ServiceFramework { diff --git a/src/core/hle/service/friend/friend.cpp b/src/core/hle/service/friend/friend.cpp index a35979053..526b0f896 100644 --- a/src/core/hle/service/friend/friend.cpp +++ b/src/core/hle/service/friend/friend.cpp @@ -185,7 +185,8 @@ private: class INotificationService final : public ServiceFramework { public: explicit INotificationService(Common::UUID uuid_, Core::System& system_) - : ServiceFramework{system_, "INotificationService"}, uuid{uuid_} { + : ServiceFramework{system_, "INotificationService"}, uuid{uuid_}, notification_event{ + system.Kernel()} { // clang-format off static const FunctionInfo functions[] = { {0, &INotificationService::GetEvent, "GetEvent"}, @@ -196,9 +197,7 @@ public: RegisterHandlers(functions); - notification_event = - Kernel::KEvent::Create(system.Kernel(), "INotificationService:NotifyEvent"); - notification_event->Initialize(); + notification_event.Initialize("INotificationService:NotifyEvent"); } private: @@ -207,7 +206,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(notification_event->GetReadableEvent()); + rb.PushCopyObjects(notification_event.GetReadableEvent()); } void Clear(Kernel::HLERequestContext& ctx) { @@ -273,7 +272,7 @@ private: }; Common::UUID uuid{Common::INVALID_UUID}; - std::shared_ptr notification_event; + Kernel::KEvent notification_event; std::queue notifications; States states{}; }; diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp index 9d07ca09c..7d9debc09 100644 --- a/src/core/hle/service/hid/controllers/npad.cpp +++ b/src/core/hle/service/hid/controllers/npad.cpp @@ -253,9 +253,8 @@ void Controller_NPad::InitNewlyAddedController(std::size_t controller_idx) { void Controller_NPad::OnInit() { auto& kernel = system.Kernel(); for (std::size_t i = 0; i < styleset_changed_events.size(); ++i) { - styleset_changed_events[i] = - Kernel::KEvent::Create(kernel, fmt::format("npad:NpadStyleSetChanged_{}", i)); - styleset_changed_events[i]->Initialize(); + styleset_changed_events[i] = std::make_unique(kernel); + styleset_changed_events[i]->Initialize(fmt::format("npad:NpadStyleSetChanged_{}", i)); } if (!IsControllerActivated()) { diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h index 8c24728b1..515cf7c37 100644 --- a/src/core/hle/service/hid/controllers/npad.h +++ b/src/core/hle/service/hid/controllers/npad.h @@ -573,7 +573,7 @@ private: NpadHandheldActivationMode handheld_activation_mode{NpadHandheldActivationMode::Dual}; NpadCommunicationMode communication_mode{NpadCommunicationMode::Default}; // Each controller should have their own styleset changed event - std::array, 10> styleset_changed_events; + std::array, 10> styleset_changed_events; std::array, 10> last_vibration_timepoints; std::array, 10> latest_vibration_values{}; bool permit_vibration_session_enabled{false}; diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 1446c0bcf..164b8b9cd 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -8,7 +8,6 @@ #include "common/logging/log.h" #include "core/core.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_writable_event.h" @@ -24,10 +23,8 @@ constexpr ResultCode ERR_NO_APPLICATION_AREA(ErrorModule::NFP, 152); Module::Interface::Interface(std::shared_ptr module_, Core::System& system_, const char* name) - : ServiceFramework{system_, name}, module{std::move(module_)} { - auto& kernel = system.Kernel(); - nfc_tag_load = Kernel::KEvent::Create(kernel, "IUser:NFCTagDetected"); - nfc_tag_load->Initialize(); + : ServiceFramework{system_, name}, nfc_tag_load{system.Kernel()}, module{std::move(module_)} { + nfc_tag_load.Initialize("IUser:NFCTagDetected"); } Module::Interface::~Interface() = default; @@ -35,7 +32,8 @@ Module::Interface::~Interface() = default; class IUser final : public ServiceFramework { public: explicit IUser(Module::Interface& nfp_interface_, Core::System& system_) - : ServiceFramework{system_, "NFP::IUser"}, nfp_interface{nfp_interface_} { + : ServiceFramework{system_, "NFP::IUser"}, nfp_interface{nfp_interface_}, + deactivate_event{system.Kernel()}, availability_change_event{system.Kernel()} { static const FunctionInfo functions[] = { {0, &IUser::Initialize, "Initialize"}, {1, &IUser::Finalize, "Finalize"}, @@ -65,11 +63,8 @@ public: }; RegisterHandlers(functions); - auto& kernel = system.Kernel(); - deactivate_event = Kernel::KEvent::Create(kernel, "IUser:DeactivateEvent"); - deactivate_event->Initialize(); - availability_change_event = Kernel::KEvent::Create(kernel, "IUser:AvailabilityChangeEvent"); - availability_change_event->Initialize(); + deactivate_event.Initialize("IUser:DeactivateEvent"); + availability_change_event.Initialize("IUser:AvailabilityChangeEvent"); } private: @@ -167,7 +162,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(deactivate_event->GetReadableEvent()); + rb.PushCopyObjects(deactivate_event.GetReadableEvent()); } void StopDetection(Kernel::HLERequestContext& ctx) { @@ -176,7 +171,7 @@ private: switch (device_state) { case DeviceState::TagFound: case DeviceState::TagNearby: - deactivate_event->GetWritableEvent()->Signal(); + deactivate_event.GetWritableEvent()->Signal(); device_state = DeviceState::Initialized; break; case DeviceState::SearchingForTag: @@ -265,7 +260,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(availability_change_event->GetReadableEvent()); + rb.PushCopyObjects(availability_change_event.GetReadableEvent()); } void GetRegisterInfo(Kernel::HLERequestContext& ctx) { @@ -319,9 +314,9 @@ private: const u32 npad_id{0}; // Player 1 controller State state{State::NonInitialized}; DeviceState device_state{DeviceState::Initialized}; - std::shared_ptr deactivate_event; - std::shared_ptr availability_change_event; const Module::Interface& nfp_interface; + Kernel::KEvent deactivate_event; + Kernel::KEvent availability_change_event; }; void Module::Interface::CreateUserInterface(Kernel::HLERequestContext& ctx) { @@ -339,12 +334,12 @@ bool Module::Interface::LoadAmiibo(const std::vector& buffer) { } std::memcpy(&amiibo, buffer.data(), sizeof(amiibo)); - nfc_tag_load->GetWritableEvent()->Signal(); + nfc_tag_load.GetWritableEvent()->Signal(); return true; } Kernel::KReadableEvent* Module::Interface::GetNFCEvent() const { - return nfc_tag_load->GetReadableEvent(); + return nfc_tag_load.GetReadableEvent().get(); } const Module::Interface::AmiiboFile& Module::Interface::GetAmiiboBuffer() const { diff --git a/src/core/hle/service/nfp/nfp.h b/src/core/hle/service/nfp/nfp.h index 7a97caffb..4bca2192c 100644 --- a/src/core/hle/service/nfp/nfp.h +++ b/src/core/hle/service/nfp/nfp.h @@ -7,6 +7,7 @@ #include #include +#include "core/hle/kernel/k_event.h" #include "core/hle/service/service.h" namespace Kernel { @@ -42,7 +43,7 @@ public: const AmiiboFile& GetAmiiboBuffer() const; private: - std::shared_ptr nfc_tag_load; + Kernel::KEvent nfc_tag_load; AmiiboFile amiibo{}; protected: diff --git a/src/core/hle/service/nifm/nifm.cpp b/src/core/hle/service/nifm/nifm.cpp index 9f110df8e..fe1c25757 100644 --- a/src/core/hle/service/nifm/nifm.cpp +++ b/src/core/hle/service/nifm/nifm.cpp @@ -127,7 +127,8 @@ public: class IRequest final : public ServiceFramework { public: - explicit IRequest(Core::System& system_) : ServiceFramework{system_, "IRequest"} { + explicit IRequest(Core::System& system_) + : ServiceFramework{system_, "IRequest"}, event1{system.Kernel()}, event2{system.Kernel()} { static const FunctionInfo functions[] = { {0, &IRequest::GetRequestState, "GetRequestState"}, {1, &IRequest::GetResult, "GetResult"}, @@ -159,10 +160,8 @@ public: auto& kernel = system.Kernel(); - event1 = Kernel::KEvent::Create(kernel, "IRequest:Event1"); - event1->Initialize(); - event2 = Kernel::KEvent::Create(kernel, "IRequest:Event2"); - event2->Initialize(); + event1.Initialize("IRequest:Event1"); + event2.Initialize("IRequest:Event2"); } private: @@ -198,7 +197,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 2}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(event1->GetReadableEvent(), event2->GetReadableEvent()); + rb.PushCopyObjects(event1.GetReadableEvent(), event2.GetReadableEvent()); } void Cancel(Kernel::HLERequestContext& ctx) { @@ -229,7 +228,7 @@ private: rb.Push(0); } - std::shared_ptr event1, event2; + Kernel::KEvent event1, event2; }; class INetworkProfile final : public ServiceFramework { diff --git a/src/core/hle/service/nim/nim.cpp b/src/core/hle/service/nim/nim.cpp index fee360ab9..6d66ad90b 100644 --- a/src/core/hle/service/nim/nim.cpp +++ b/src/core/hle/service/nim/nim.cpp @@ -300,7 +300,8 @@ class IEnsureNetworkClockAvailabilityService final : public ServiceFramework { public: explicit IEnsureNetworkClockAvailabilityService(Core::System& system_) - : ServiceFramework{system_, "IEnsureNetworkClockAvailabilityService"} { + : ServiceFramework{system_, "IEnsureNetworkClockAvailabilityService"}, + finished_event{system.Kernel()} { static const FunctionInfo functions[] = { {0, &IEnsureNetworkClockAvailabilityService::StartTask, "StartTask"}, {1, &IEnsureNetworkClockAvailabilityService::GetFinishNotificationEvent, @@ -312,19 +313,16 @@ public: }; RegisterHandlers(functions); - auto& kernel = system.Kernel(); - finished_event = - Kernel::KEvent::Create(kernel, "IEnsureNetworkClockAvailabilityService:FinishEvent"); - finished_event->Initialize(); + finished_event.Initialize("IEnsureNetworkClockAvailabilityService:FinishEvent"); } private: - std::shared_ptr finished_event; + Kernel::KEvent finished_event; void StartTask(Kernel::HLERequestContext& ctx) { // No need to connect to the internet, just finish the task straight away. LOG_DEBUG(Service_NIM, "called"); - finished_event->GetWritableEvent()->Signal(); + finished_event.GetWritableEvent()->Signal(); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(RESULT_SUCCESS); } @@ -334,7 +332,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(finished_event->GetReadableEvent()); + rb.PushCopyObjects(finished_event.GetReadableEvent()); } void GetResult(Kernel::HLERequestContext& ctx) { @@ -346,7 +344,7 @@ private: void Cancel(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_NIM, "called"); - finished_event->GetWritableEvent()->Clear(); + finished_event.GetWritableEvent()->Clear(); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(RESULT_SUCCESS); } diff --git a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp index 6f4007294..e99da24ab 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp @@ -102,7 +102,7 @@ NvResult nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector return NvResult::Success; } - auto event = events_interface.events[event_id]; + auto& event = events_interface.events[event_id]; auto& gpu = system.GPU(); // This is mostly to take into account unimplemented features. As synced diff --git a/src/core/hle/service/nvdrv/nvdrv.cpp b/src/core/hle/service/nvdrv/nvdrv.cpp index 6bba9c0b3..74399bcdb 100644 --- a/src/core/hle/service/nvdrv/nvdrv.cpp +++ b/src/core/hle/service/nvdrv/nvdrv.cpp @@ -42,9 +42,8 @@ void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger Module::Module(Core::System& system) : syncpoint_manager{system.GPU()} { auto& kernel = system.Kernel(); for (u32 i = 0; i < MaxNvEvents; i++) { - std::string event_label = fmt::format("NVDRV::NvEvent_{}", i); - events_interface.events[i] = {Kernel::KEvent::Create(kernel, std::move(event_label))}; - events_interface.events[i].event->Initialize(); + events_interface.events[i].event = std::make_unique(kernel); + events_interface.events[i].event->Initialize(fmt::format("NVDRV::NvEvent_{}", i)); events_interface.status[i] = EventState::Free; events_interface.registered[i] = false; } @@ -64,7 +63,12 @@ Module::Module(Core::System& system) : syncpoint_manager{system.GPU()} { std::make_shared(system, nvmap_dev, syncpoint_manager); } -Module::~Module() = default; +Module::~Module() { + for (u32 i = 0; i < MaxNvEvents; i++) { + events_interface.events[i].event->Close(); + events_interface.events[i].event = nullptr; + } +} NvResult Module::VerifyFD(DeviceFD fd) const { if (fd < 0) { diff --git a/src/core/hle/service/nvdrv/nvdrv.h b/src/core/hle/service/nvdrv/nvdrv.h index 53719aadd..76f77cbb1 100644 --- a/src/core/hle/service/nvdrv/nvdrv.h +++ b/src/core/hle/service/nvdrv/nvdrv.h @@ -35,7 +35,7 @@ class nvdevice; /// Represents an Nvidia event struct NvEvent { - std::shared_ptr event; + std::unique_ptr event; Fence fence{}; }; diff --git a/src/core/hle/service/nvflinger/buffer_queue.cpp b/src/core/hle/service/nvflinger/buffer_queue.cpp index f783ae54f..8ddb1f908 100644 --- a/src/core/hle/service/nvflinger/buffer_queue.cpp +++ b/src/core/hle/service/nvflinger/buffer_queue.cpp @@ -7,7 +7,6 @@ #include "common/assert.h" #include "common/logging/log.h" #include "core/core.h" -#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/kernel.h" #include "core/hle/service/nvflinger/buffer_queue.h" @@ -15,9 +14,8 @@ namespace Service::NVFlinger { BufferQueue::BufferQueue(Kernel::KernelCore& kernel, u32 id, u64 layer_id) - : id(id), layer_id(layer_id) { - buffer_wait_event = Kernel::KEvent::Create(kernel, "BufferQueue:WaitEvent"); - buffer_wait_event->Initialize(); + : id(id), layer_id(layer_id), buffer_wait_event{kernel} { + buffer_wait_event.Initialize("BufferQueue:WaitEvent"); } BufferQueue::~BufferQueue() = default; @@ -42,7 +40,7 @@ void BufferQueue::SetPreallocatedBuffer(u32 slot, const IGBPBuffer& igbp_buffer) .multi_fence = {}, }; - buffer_wait_event->GetWritableEvent()->Signal(); + buffer_wait_event.GetWritableEvent()->Signal(); } std::optional> BufferQueue::DequeueBuffer(u32 width, @@ -120,7 +118,7 @@ void BufferQueue::CancelBuffer(u32 slot, const Service::Nvidia::MultiFence& mult } free_buffers_condition.notify_one(); - buffer_wait_event->GetWritableEvent()->Signal(); + buffer_wait_event.GetWritableEvent()->Signal(); } std::optional> BufferQueue::AcquireBuffer() { @@ -155,7 +153,7 @@ void BufferQueue::ReleaseBuffer(u32 slot) { } free_buffers_condition.notify_one(); - buffer_wait_event->GetWritableEvent()->Signal(); + buffer_wait_event.GetWritableEvent()->Signal(); } void BufferQueue::Connect() { @@ -170,7 +168,7 @@ void BufferQueue::Disconnect() { std::unique_lock lock{queue_sequence_mutex}; queue_sequence.clear(); } - buffer_wait_event->GetWritableEvent()->Signal(); + buffer_wait_event.GetWritableEvent()->Signal(); is_connect = false; free_buffers_condition.notify_one(); } @@ -190,11 +188,11 @@ u32 BufferQueue::Query(QueryType type) { } std::shared_ptr BufferQueue::GetWritableBufferWaitEvent() const { - return buffer_wait_event->GetWritableEvent(); + return buffer_wait_event.GetWritableEvent(); } std::shared_ptr BufferQueue::GetBufferWaitEvent() const { - return SharedFrom(buffer_wait_event->GetReadableEvent()); + return buffer_wait_event.GetReadableEvent(); } } // namespace Service::NVFlinger diff --git a/src/core/hle/service/nvflinger/buffer_queue.h b/src/core/hle/service/nvflinger/buffer_queue.h index 9a21c7426..0d2010ad5 100644 --- a/src/core/hle/service/nvflinger/buffer_queue.h +++ b/src/core/hle/service/nvflinger/buffer_queue.h @@ -13,6 +13,7 @@ #include "common/common_funcs.h" #include "common/math_util.h" #include "common/swap.h" +#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/object.h" #include "core/hle/service/nvdrv/nvdata.h" @@ -130,7 +131,7 @@ private: std::list free_buffers; std::array buffers; std::list queue_sequence; - std::shared_ptr buffer_wait_event; + Kernel::KEvent buffer_wait_event; std::mutex free_buffers_mutex; std::condition_variable free_buffers_condition; diff --git a/src/core/hle/service/nvflinger/nvflinger.cpp b/src/core/hle/service/nvflinger/nvflinger.cpp index c43593e7f..c90e4d083 100644 --- a/src/core/hle/service/nvflinger/nvflinger.cpp +++ b/src/core/hle/service/nvflinger/nvflinger.cpp @@ -165,8 +165,8 @@ std::optional NVFlinger::FindBufferQueueId(u64 display_id, u64 layer_id) co return layer->GetBufferQueue().GetId(); } -std::shared_ptr NVFlinger::FindVsyncEvent(u64 display_id) const { - const auto lock_guard = Lock(); +std::shared_ptr NVFlinger::FindVsyncEvent(u64 display_id) { + const auto lock_guard = Lock(); auto* const display = FindDisplay(display_id); if (display == nullptr) { diff --git a/src/core/hle/service/nvflinger/nvflinger.h b/src/core/hle/service/nvflinger/nvflinger.h index 6fe2c7f2a..d51b905c1 100644 --- a/src/core/hle/service/nvflinger/nvflinger.h +++ b/src/core/hle/service/nvflinger/nvflinger.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include #include @@ -72,7 +73,7 @@ public: /// Gets the vsync event for the specified display. /// /// If an invalid display ID is provided, then nullptr is returned. - [[nodiscard]] std::shared_ptr FindVsyncEvent(u64 display_id) const; + [[nodiscard]] std::shared_ptr FindVsyncEvent(u64 display_id); /// Obtains a buffer queue identified by the ID. [[nodiscard]] BufferQueue* FindBufferQueue(u32 id); @@ -106,7 +107,7 @@ private: std::shared_ptr nvdrv; - std::vector displays; + std::list displays; std::vector> buffer_queues; /// Id to use for the next layer that is created, this counter is shared among all displays. diff --git a/src/core/hle/service/ptm/psm.cpp b/src/core/hle/service/ptm/psm.cpp index 26ed52273..22e5d9749 100644 --- a/src/core/hle/service/ptm/psm.cpp +++ b/src/core/hle/service/ptm/psm.cpp @@ -19,7 +19,8 @@ namespace Service::PSM { class IPsmSession final : public ServiceFramework { public: - explicit IPsmSession(Core::System& system_) : ServiceFramework{system_, "IPsmSession"} { + explicit IPsmSession(Core::System& system_) + : ServiceFramework{system_, "IPsmSession"}, state_change_event{system.Kernel()} { // clang-format off static const FunctionInfo functions[] = { {0, &IPsmSession::BindStateChangeEvent, "BindStateChangeEvent"}, @@ -32,28 +33,26 @@ public: RegisterHandlers(functions); - state_change_event = - Kernel::KEvent::Create(system_.Kernel(), "IPsmSession::state_change_event"); - state_change_event->Initialize(); + state_change_event.Initialize("IPsmSession::state_change_event"); } ~IPsmSession() override = default; void SignalChargerTypeChanged() { if (should_signal && should_signal_charger_type) { - state_change_event->GetWritableEvent()->Signal(); + state_change_event.GetWritableEvent()->Signal(); } } void SignalPowerSupplyChanged() { if (should_signal && should_signal_power_supply) { - state_change_event->GetWritableEvent()->Signal(); + state_change_event.GetWritableEvent()->Signal(); } } void SignalBatteryVoltageStateChanged() { if (should_signal && should_signal_battery_voltage) { - state_change_event->GetWritableEvent()->Signal(); + state_change_event.GetWritableEvent()->Signal(); } } @@ -65,7 +64,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(state_change_event->GetReadableEvent()); + rb.PushCopyObjects(state_change_event.GetReadableEvent()); } void UnbindStateChangeEvent(Kernel::HLERequestContext& ctx) { @@ -114,7 +113,7 @@ private: bool should_signal_power_supply{}; bool should_signal_battery_voltage{}; bool should_signal{}; - std::shared_ptr state_change_event; + Kernel::KEvent state_change_event; }; class PSM final : public ServiceFramework { diff --git a/src/core/hle/service/time/standard_user_system_clock_core.cpp b/src/core/hle/service/time/standard_user_system_clock_core.cpp index 3172acc5a..c41bdd48b 100644 --- a/src/core/hle/service/time/standard_user_system_clock_core.cpp +++ b/src/core/hle/service/time/standard_user_system_clock_core.cpp @@ -4,7 +4,6 @@ #include "common/assert.h" #include "core/core.h" -#include "core/hle/kernel/k_event.h" #include "core/hle/service/time/standard_local_system_clock_core.h" #include "core/hle/service/time/standard_network_system_clock_core.h" #include "core/hle/service/time/standard_user_system_clock_core.h" @@ -17,10 +16,9 @@ StandardUserSystemClockCore::StandardUserSystemClockCore( : SystemClockCore(local_system_clock_core.GetSteadyClockCore()), local_system_clock_core{local_system_clock_core}, network_system_clock_core{network_system_clock_core}, auto_correction_enabled{}, - auto_correction_time{SteadyClockTimePoint::GetRandom()}, - auto_correction_event{Kernel::KEvent::Create( - system.Kernel(), "StandardUserSystemClockCore:AutoCorrectionEvent")} { - auto_correction_event->Initialize(); + auto_correction_time{SteadyClockTimePoint::GetRandom()}, auto_correction_event{ + system.Kernel()} { + auto_correction_event.Initialize("StandardUserSystemClockCore:AutoCorrectionEvent"); } ResultCode StandardUserSystemClockCore::SetAutomaticCorrectionEnabled(Core::System& system, diff --git a/src/core/hle/service/time/standard_user_system_clock_core.h b/src/core/hle/service/time/standard_user_system_clock_core.h index 5bc8bf5c2..1bff8a5af 100644 --- a/src/core/hle/service/time/standard_user_system_clock_core.h +++ b/src/core/hle/service/time/standard_user_system_clock_core.h @@ -4,6 +4,7 @@ #pragma once +#include "core/hle/kernel/k_event.h" #include "core/hle/service/time/clock_types.h" #include "core/hle/service/time/system_clock_core.h" @@ -54,7 +55,7 @@ private: StandardNetworkSystemClockCore& network_system_clock_core; bool auto_correction_enabled{}; SteadyClockTimePoint auto_correction_time; - std::shared_ptr auto_correction_event; + Kernel::KEvent auto_correction_event; }; } // namespace Service::Time::Clock diff --git a/src/core/hle/service/vi/display/vi_display.cpp b/src/core/hle/service/vi/display/vi_display.cpp index 9ffa71352..ebaacaa6b 100644 --- a/src/core/hle/service/vi/display/vi_display.cpp +++ b/src/core/hle/service/vi/display/vi_display.cpp @@ -17,10 +17,9 @@ namespace Service::VI { -Display::Display(u64 id, std::string name, Core::System& system) : id{id}, name{std::move(name)} { - auto& kernel = system.Kernel(); - vsync_event = Kernel::KEvent::Create(kernel, fmt::format("Display VSync Event {}", id)); - vsync_event->Initialize(); +Display::Display(u64 id, std::string name, Core::System& system) + : id{id}, name{std::move(name)}, vsync_event{system.Kernel()} { + vsync_event.Initialize(fmt::format("Display VSync Event {}", id)); } Display::~Display() = default; @@ -34,11 +33,11 @@ const Layer& Display::GetLayer(std::size_t index) const { } std::shared_ptr Display::GetVSyncEvent() const { - return SharedFrom(vsync_event->GetReadableEvent()); + return vsync_event.GetReadableEvent(); } void Display::SignalVSyncEvent() { - vsync_event->GetWritableEvent()->Signal(); + vsync_event.GetWritableEvent()->Signal(); } void Display::CreateLayer(u64 layer_id, NVFlinger::BufferQueue& buffer_queue) { diff --git a/src/core/hle/service/vi/display/vi_display.h b/src/core/hle/service/vi/display/vi_display.h index 8340059de..e990d6809 100644 --- a/src/core/hle/service/vi/display/vi_display.h +++ b/src/core/hle/service/vi/display/vi_display.h @@ -8,6 +8,7 @@ #include #include +#include "common/common_funcs.h" #include "common/common_types.h" namespace Kernel { @@ -24,6 +25,9 @@ class Layer; /// Represents a single display type class Display { + NON_COPYABLE(Display); + NON_MOVEABLE(Display); + public: /// Constructs a display with a given unique ID and name. /// @@ -33,12 +37,6 @@ public: Display(u64 id, std::string name, Core::System& system); ~Display(); - Display(const Display&) = delete; - Display& operator=(const Display&) = delete; - - Display(Display&&) = default; - Display& operator=(Display&&) = default; - /// Gets the unique ID assigned to this display. u64 GetID() const { return id; @@ -102,7 +100,7 @@ private: std::string name; std::vector> layers; - std::shared_ptr vsync_event; + Kernel::KEvent vsync_event; }; } // namespace Service::VI From 722195cf704a628aa13c5a178a07dd33c186b952 Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 8 Apr 2021 18:58:38 -0700 Subject: [PATCH 22/82] hle: kernel: Use unique_ptr for suspend and dummy threads. --- src/core/hle/kernel/kernel.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index cac76a6ec..1249a4c96 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -195,9 +195,9 @@ struct KernelCore::Impl { void InitializeSuspendThreads() { for (s32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { - suspend_threads[core_id] = KThread::CreateWithKernel(system.Kernel()); - ASSERT(KThread::InitializeHighPriorityThread(system, suspend_threads[core_id], {}, {}, - core_id) + suspend_threads[core_id] = std::make_unique(system.Kernel()); + ASSERT(KThread::InitializeHighPriorityThread(system, suspend_threads[core_id].get(), {}, + {}, core_id) .IsSuccess()); suspend_threads[core_id]->SetName(fmt::format("SuspendThread:{}", core_id)); } @@ -235,14 +235,14 @@ struct KernelCore::Impl { // Gets the dummy KThread for the caller, allocating a new one if this is the first time KThread* GetHostDummyThread() { auto make_thread = [this]() { - KThread* thread = KThread::CreateWithKernel(system.Kernel()); - ASSERT(KThread::InitializeDummyThread(thread).IsSuccess()); + std::unique_ptr thread = std::make_unique(system.Kernel()); + ASSERT(KThread::InitializeDummyThread(thread.get()).IsSuccess()); thread->SetName(fmt::format("DummyThread:{}", GetHostThreadId())); - return thread; + return std::move(thread); }; thread_local auto thread = make_thread(); - return thread; + return thread.get(); } /// Registers a CPU core thread by allocating a host thread ID for it @@ -661,7 +661,7 @@ struct KernelCore::Impl { // the release of itself std::unique_ptr service_thread_manager; - std::array suspend_threads{}; + std::array, Core::Hardware::NUM_CPU_CORES> suspend_threads; std::array interrupts{}; std::array, Core::Hardware::NUM_CPU_CORES> schedulers{}; From ab704acab80d17f9a8e34dcbb753d60de2a86f86 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 9 Apr 2021 16:56:11 -0700 Subject: [PATCH 23/82] hle: kernel: Ensure all kernel objects with KAutoObject are properly created. --- src/core/hle/kernel/k_scheduler.cpp | 1 + src/core/hle/kernel/kernel.cpp | 7 +++++++ src/core/hle/service/am/am.cpp | 13 +++++++++++++ src/core/hle/service/am/applets/applets.cpp | 5 +++++ src/core/hle/service/aoc/aoc_u.cpp | 2 ++ src/core/hle/service/audio/audout_u.cpp | 1 + src/core/hle/service/audio/audren_u.cpp | 4 ++++ src/core/hle/service/bcat/backend/backend.cpp | 1 + src/core/hle/service/btdrv/btdrv.cpp | 1 + src/core/hle/service/btm/btm.cpp | 5 +++++ src/core/hle/service/friend/friend.cpp | 1 + src/core/hle/service/hid/controllers/npad.cpp | 1 + src/core/hle/service/nfp/nfp.cpp | 4 ++++ src/core/hle/service/nifm/nifm.cpp | 3 +++ src/core/hle/service/nim/nim.cpp | 1 + src/core/hle/service/nvdrv/nvdrv.cpp | 1 + src/core/hle/service/nvflinger/buffer_queue.cpp | 1 + src/core/hle/service/ptm/psm.cpp | 1 + .../time/standard_user_system_clock_core.cpp | 1 + src/core/hle/service/vi/display/vi_display.cpp | 1 + 20 files changed, 55 insertions(+) diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index c048d86a3..09382f7dd 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -793,6 +793,7 @@ void KScheduler::UpdateLastContextSwitchTime(KThread* thread, Process* process) void KScheduler::Initialize() { idle_thread = std::make_unique(system.Kernel()); + KAutoObject::Create(idle_thread.get()); ASSERT(KThread::InitializeIdleThread(system, idle_thread.get(), core_id).IsSuccess()); idle_thread->SetName(fmt::format("IdleThread:{}", core_id)); } diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 1249a4c96..43bce1863 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -196,6 +196,7 @@ struct KernelCore::Impl { void InitializeSuspendThreads() { for (s32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { suspend_threads[core_id] = std::make_unique(system.Kernel()); + KAutoObject::Create(suspend_threads[core_id].get()); ASSERT(KThread::InitializeHighPriorityThread(system, suspend_threads[core_id].get(), {}, {}, core_id) .IsSuccess()); @@ -236,6 +237,7 @@ struct KernelCore::Impl { KThread* GetHostDummyThread() { auto make_thread = [this]() { std::unique_ptr thread = std::make_unique(system.Kernel()); + KAutoObject::Create(thread.get()); ASSERT(KThread::InitializeDummyThread(thread.get()).IsSuccess()); thread->SetName(fmt::format("DummyThread:{}", GetHostThreadId())); return std::move(thread); @@ -580,6 +582,11 @@ struct KernelCore::Impl { irs_shared_mem = std::make_unique(system.Kernel()); time_shared_mem = std::make_unique(system.Kernel()); + KAutoObject::Create(hid_shared_mem.get()); + KAutoObject::Create(font_shared_mem.get()); + KAutoObject::Create(irs_shared_mem.get()); + KAutoObject::Create(time_shared_mem.get()); + hid_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr, {hid_phys_addr, hid_size / PageSize}, KMemoryPermission::None, KMemoryPermission::Read, hid_phys_addr, hid_size, diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 3800c65ad..c1982e996 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -307,6 +307,8 @@ ISelfController::ISelfController(Core::System& system_, NVFlinger::NVFlinger& nv RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(launchable_event)); + launchable_event.Initialize("ISelfController:LaunchableEvent"); // This event is created by AM on the first time GetAccumulatedSuspendedTickChangedEvent() is @@ -315,6 +317,7 @@ ISelfController::ISelfController(Core::System& system_, NVFlinger::NVFlinger& nv // suspended if the event has previously been created by a call to // GetAccumulatedSuspendedTickChangedEvent. + Kernel::KAutoObject::Create(std::addressof(accumulated_suspended_tick_changed_event)); accumulated_suspended_tick_changed_event.Initialize( "ISelfController:AccumulatedSuspendedTickChangedEvent"); accumulated_suspended_tick_changed_event.GetWritableEvent()->Signal(); @@ -579,6 +582,10 @@ void ISelfController::SetAlbumImageTakenNotificationEnabled(Kernel::HLERequestCo AppletMessageQueue::AppletMessageQueue(Kernel::KernelCore& kernel) : on_new_message{kernel}, on_operation_mode_changed{kernel} { + + Kernel::KAutoObject::Create(std::addressof(on_new_message)); + Kernel::KAutoObject::Create(std::addressof(on_operation_mode_changed)); + on_new_message.Initialize("AMMessageQueue:OnMessageReceived"); on_operation_mode_changed.Initialize("AMMessageQueue:OperationModeChanged"); } @@ -1333,6 +1340,11 @@ IApplicationFunctions::IApplicationFunctions(Core::System& system_) RegisterHandlers(functions); auto& kernel = system.Kernel(); + + Kernel::KAutoObject::Create(std::addressof(gpu_error_detected_event)); + Kernel::KAutoObject::Create(std::addressof(friend_invitation_storage_channel_event)); + Kernel::KAutoObject::Create(std::addressof(health_warning_disappeared_system_event)); + gpu_error_detected_event.Initialize("IApplicationFunctions:GpuErrorDetectedSystemEvent"); friend_invitation_storage_channel_event.Initialize( "IApplicationFunctions:FriendInvitationStorageChannelEvent"); @@ -1798,6 +1810,7 @@ IHomeMenuFunctions::IHomeMenuFunctions(Core::System& system_) RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(pop_from_general_channel_event)); pop_from_general_channel_event.Initialize("IHomeMenuFunctions:PopFromGeneralChannelEvent"); } diff --git a/src/core/hle/service/am/applets/applets.cpp b/src/core/hle/service/am/applets/applets.cpp index 58bff810d..6cceff064 100644 --- a/src/core/hle/service/am/applets/applets.cpp +++ b/src/core/hle/service/am/applets/applets.cpp @@ -32,6 +32,11 @@ namespace Service::AM::Applets { AppletDataBroker::AppletDataBroker(Core::System& system_, LibraryAppletMode applet_mode_) : system{system_}, applet_mode{applet_mode_}, state_changed_event{system.Kernel()}, pop_out_data_event{system.Kernel()}, pop_interactive_out_data_event{system.Kernel()} { + + Kernel::KAutoObject::Create(std::addressof(state_changed_event)); + Kernel::KAutoObject::Create(std::addressof(pop_out_data_event)); + Kernel::KAutoObject::Create(std::addressof(pop_interactive_out_data_event)); + state_changed_event.Initialize("ILibraryAppletAccessor:StateChangedEvent"); pop_out_data_event.Initialize("ILibraryAppletAccessor:PopDataOutEvent"); pop_interactive_out_data_event.Initialize("ILibraryAppletAccessor:PopInteractiveDataOutEvent"); diff --git a/src/core/hle/service/aoc/aoc_u.cpp b/src/core/hle/service/aoc/aoc_u.cpp index 12a025610..7d7a8c0ad 100644 --- a/src/core/hle/service/aoc/aoc_u.cpp +++ b/src/core/hle/service/aoc/aoc_u.cpp @@ -62,6 +62,7 @@ public: RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(purchased_event)); purchased_event.Initialize("IPurchaseEventManager:PurchasedEvent"); } @@ -125,6 +126,7 @@ AOC_U::AOC_U(Core::System& system_) RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(aoc_change_event)); aoc_change_event.Initialize("GetAddOnContentListChanged:Event"); } diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp index 4052c8e60..5a7d73b48 100644 --- a/src/core/hle/service/audio/audout_u.cpp +++ b/src/core/hle/service/audio/audout_u.cpp @@ -67,6 +67,7 @@ public: RegisterHandlers(functions); // This is the event handle used to check if the audio buffer was released + Kernel::KAutoObject::Create(std::addressof(buffer_event)); buffer_event.Initialize("IAudioOutBufferReleased"); stream = audio_core.OpenStream(system.CoreTiming(), audio_params.sample_rate, diff --git a/src/core/hle/service/audio/audren_u.cpp b/src/core/hle/service/audio/audren_u.cpp index d573530df..b0a362f52 100644 --- a/src/core/hle/service/audio/audren_u.cpp +++ b/src/core/hle/service/audio/audren_u.cpp @@ -49,6 +49,7 @@ public: // clang-format on RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(system_event)); system_event.Initialize("IAudioRenderer:SystemEvent"); renderer = std::make_unique( system.CoreTiming(), system.Memory(), audren_params, @@ -189,13 +190,16 @@ public: }; RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(buffer_event)); buffer_event.Initialize("IAudioOutBufferReleasedEvent"); // Should be similar to audio_output_device_switch_event + Kernel::KAutoObject::Create(std::addressof(audio_input_device_switch_event)); audio_input_device_switch_event.Initialize("IAudioDevice:AudioInputDeviceSwitchedEvent"); // Should only be signalled when an audio output device has been changed, example: speaker // to headset + Kernel::KAutoObject::Create(std::addressof(audio_output_device_switch_event)); audio_output_device_switch_event.Initialize("IAudioDevice:AudioOutputDeviceSwitchedEvent"); } diff --git a/src/core/hle/service/bcat/backend/backend.cpp b/src/core/hle/service/bcat/backend/backend.cpp index 932e70bfd..8def52b31 100644 --- a/src/core/hle/service/bcat/backend/backend.cpp +++ b/src/core/hle/service/bcat/backend/backend.cpp @@ -15,6 +15,7 @@ namespace Service::BCAT { ProgressServiceBackend::ProgressServiceBackend(Kernel::KernelCore& kernel, std::string_view event_name) : update_event{kernel} { + Kernel::KAutoObject::Create(std::addressof(update_event)); update_event.Initialize("ProgressServiceBackend:UpdateEvent:" + std::string(event_name)); } diff --git a/src/core/hle/service/btdrv/btdrv.cpp b/src/core/hle/service/btdrv/btdrv.cpp index dde276ff4..fd97a822c 100644 --- a/src/core/hle/service/btdrv/btdrv.cpp +++ b/src/core/hle/service/btdrv/btdrv.cpp @@ -35,6 +35,7 @@ public: // clang-format on RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(register_event)); register_event.Initialize("BT:RegisterEvent"); } diff --git a/src/core/hle/service/btm/btm.cpp b/src/core/hle/service/btm/btm.cpp index 403296650..3b5ef69e1 100644 --- a/src/core/hle/service/btm/btm.cpp +++ b/src/core/hle/service/btm/btm.cpp @@ -60,6 +60,11 @@ public: // clang-format on RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(scan_event)); + Kernel::KAutoObject::Create(std::addressof(connection_event)); + Kernel::KAutoObject::Create(std::addressof(service_discovery)); + Kernel::KAutoObject::Create(std::addressof(config_event)); + scan_event.Initialize("IBtmUserCore:ScanEvent"); connection_event.Initialize("IBtmUserCore:ConnectionEvent"); service_discovery.Initialize("IBtmUserCore:Discovery"); diff --git a/src/core/hle/service/friend/friend.cpp b/src/core/hle/service/friend/friend.cpp index 526b0f896..91c202952 100644 --- a/src/core/hle/service/friend/friend.cpp +++ b/src/core/hle/service/friend/friend.cpp @@ -197,6 +197,7 @@ public: RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(notification_event)); notification_event.Initialize("INotificationService:NotifyEvent"); } diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp index 7d9debc09..753875d6e 100644 --- a/src/core/hle/service/hid/controllers/npad.cpp +++ b/src/core/hle/service/hid/controllers/npad.cpp @@ -254,6 +254,7 @@ void Controller_NPad::OnInit() { auto& kernel = system.Kernel(); for (std::size_t i = 0; i < styleset_changed_events.size(); ++i) { styleset_changed_events[i] = std::make_unique(kernel); + Kernel::KAutoObject::Create(styleset_changed_events[i].get()); styleset_changed_events[i]->Initialize(fmt::format("npad:NpadStyleSetChanged_{}", i)); } diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 164b8b9cd..d1cf01390 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -24,6 +24,7 @@ constexpr ResultCode ERR_NO_APPLICATION_AREA(ErrorModule::NFP, 152); Module::Interface::Interface(std::shared_ptr module_, Core::System& system_, const char* name) : ServiceFramework{system_, name}, nfc_tag_load{system.Kernel()}, module{std::move(module_)} { + Kernel::KAutoObject::Create(std::addressof(nfc_tag_load)); nfc_tag_load.Initialize("IUser:NFCTagDetected"); } @@ -63,6 +64,9 @@ public: }; RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(deactivate_event)); + Kernel::KAutoObject::Create(std::addressof(availability_change_event)); + deactivate_event.Initialize("IUser:DeactivateEvent"); availability_change_event.Initialize("IUser:AvailabilityChangeEvent"); } diff --git a/src/core/hle/service/nifm/nifm.cpp b/src/core/hle/service/nifm/nifm.cpp index fe1c25757..c91427b8b 100644 --- a/src/core/hle/service/nifm/nifm.cpp +++ b/src/core/hle/service/nifm/nifm.cpp @@ -160,6 +160,9 @@ public: auto& kernel = system.Kernel(); + Kernel::KAutoObject::Create(std::addressof(event1)); + Kernel::KAutoObject::Create(std::addressof(event2)); + event1.Initialize("IRequest:Event1"); event2.Initialize("IRequest:Event2"); } diff --git a/src/core/hle/service/nim/nim.cpp b/src/core/hle/service/nim/nim.cpp index 6d66ad90b..4a2262562 100644 --- a/src/core/hle/service/nim/nim.cpp +++ b/src/core/hle/service/nim/nim.cpp @@ -313,6 +313,7 @@ public: }; RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(finished_event)); finished_event.Initialize("IEnsureNetworkClockAvailabilityService:FinishEvent"); } diff --git a/src/core/hle/service/nvdrv/nvdrv.cpp b/src/core/hle/service/nvdrv/nvdrv.cpp index 74399bcdb..64bdb0722 100644 --- a/src/core/hle/service/nvdrv/nvdrv.cpp +++ b/src/core/hle/service/nvdrv/nvdrv.cpp @@ -43,6 +43,7 @@ Module::Module(Core::System& system) : syncpoint_manager{system.GPU()} { auto& kernel = system.Kernel(); for (u32 i = 0; i < MaxNvEvents; i++) { events_interface.events[i].event = std::make_unique(kernel); + Kernel::KAutoObject::Create(events_interface.events[i].event.get()); events_interface.events[i].event->Initialize(fmt::format("NVDRV::NvEvent_{}", i)); events_interface.status[i] = EventState::Free; events_interface.registered[i] = false; diff --git a/src/core/hle/service/nvflinger/buffer_queue.cpp b/src/core/hle/service/nvflinger/buffer_queue.cpp index 8ddb1f908..95e5a5e7f 100644 --- a/src/core/hle/service/nvflinger/buffer_queue.cpp +++ b/src/core/hle/service/nvflinger/buffer_queue.cpp @@ -15,6 +15,7 @@ namespace Service::NVFlinger { BufferQueue::BufferQueue(Kernel::KernelCore& kernel, u32 id, u64 layer_id) : id(id), layer_id(layer_id), buffer_wait_event{kernel} { + Kernel::KAutoObject::Create(std::addressof(buffer_wait_event)); buffer_wait_event.Initialize("BufferQueue:WaitEvent"); } diff --git a/src/core/hle/service/ptm/psm.cpp b/src/core/hle/service/ptm/psm.cpp index 22e5d9749..2659ab7d5 100644 --- a/src/core/hle/service/ptm/psm.cpp +++ b/src/core/hle/service/ptm/psm.cpp @@ -33,6 +33,7 @@ public: RegisterHandlers(functions); + Kernel::KAutoObject::Create(std::addressof(state_change_event)); state_change_event.Initialize("IPsmSession::state_change_event"); } diff --git a/src/core/hle/service/time/standard_user_system_clock_core.cpp b/src/core/hle/service/time/standard_user_system_clock_core.cpp index c41bdd48b..7f47b12b8 100644 --- a/src/core/hle/service/time/standard_user_system_clock_core.cpp +++ b/src/core/hle/service/time/standard_user_system_clock_core.cpp @@ -18,6 +18,7 @@ StandardUserSystemClockCore::StandardUserSystemClockCore( network_system_clock_core{network_system_clock_core}, auto_correction_enabled{}, auto_correction_time{SteadyClockTimePoint::GetRandom()}, auto_correction_event{ system.Kernel()} { + Kernel::KAutoObject::Create(std::addressof(auto_correction_event)); auto_correction_event.Initialize("StandardUserSystemClockCore:AutoCorrectionEvent"); } diff --git a/src/core/hle/service/vi/display/vi_display.cpp b/src/core/hle/service/vi/display/vi_display.cpp index ebaacaa6b..7c52b6aa7 100644 --- a/src/core/hle/service/vi/display/vi_display.cpp +++ b/src/core/hle/service/vi/display/vi_display.cpp @@ -19,6 +19,7 @@ namespace Service::VI { Display::Display(u64 id, std::string name, Core::System& system) : id{id}, name{std::move(name)}, vsync_event{system.Kernel()} { + Kernel::KAutoObject::Create(std::addressof(vsync_event)); vsync_event.Initialize(fmt::format("Display VSync Event {}", id)); } From b6156e735cd78d4b7863491ae6bdc63e44404b73 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 9 Apr 2021 22:10:14 -0700 Subject: [PATCH 24/82] hle: kernel: Move slab heap management to KernelCore. --- src/core/hle/kernel/k_condition_variable.cpp | 15 +-- src/core/hle/kernel/k_event.cpp | 4 + src/core/hle/kernel/k_linked_list.h | 15 ++- src/core/hle/kernel/k_scheduler.cpp | 4 +- src/core/hle/kernel/k_slab_heap.h | 1 + src/core/hle/kernel/kernel.h | 36 ++++++- src/core/hle/kernel/slab_helpers.h | 107 ++++++++++--------- 7 files changed, 112 insertions(+), 70 deletions(-) diff --git a/src/core/hle/kernel/k_condition_variable.cpp b/src/core/hle/kernel/k_condition_variable.cpp index 930f78974..72565af05 100644 --- a/src/core/hle/kernel/k_condition_variable.cpp +++ b/src/core/hle/kernel/k_condition_variable.cpp @@ -185,11 +185,11 @@ KThread* KConditionVariable::SignalImpl(KThread* thread) { thread->Wakeup(); } else { // Get the previous owner. - KThread* owner_thread = - kernel.CurrentProcess()->GetHandleTable() - .GetObjectWithoutPseudoHandle( - static_cast(prev_tag & ~Svc::HandleWaitMask)) - .ReleasePointerUnsafe(); + KThread* owner_thread = kernel.CurrentProcess() + ->GetHandleTable() + .GetObjectWithoutPseudoHandle( + static_cast(prev_tag & ~Svc::HandleWaitMask)) + .ReleasePointerUnsafe(); if (owner_thread) { // Add the thread as a waiter on the owner. @@ -214,7 +214,7 @@ void KConditionVariable::Signal(u64 cv_key, s32 count) { // Prepare for signaling. constexpr int MaxThreads = 16; - KLinkedList thread_list; + KLinkedList thread_list{kernel}; std::array thread_array; s32 num_to_close{}; @@ -254,7 +254,8 @@ void KConditionVariable::Signal(u64 cv_key, s32 count) { } // Close threads in the list. - for (auto it = thread_list.begin(); it != thread_list.end(); it = thread_list.erase(it)) { + for (auto it = thread_list.begin(); it != thread_list.end(); + it = thread_list.erase(kernel, it)) { (*it).Close(); } } diff --git a/src/core/hle/kernel/k_event.cpp b/src/core/hle/kernel/k_event.cpp index bc4a79cc8..4020e5325 100644 --- a/src/core/hle/kernel/k_event.cpp +++ b/src/core/hle/kernel/k_event.cpp @@ -21,6 +21,10 @@ void KEvent::Initialize(std::string&& name_) { // writable events are closed this object will be destroyed. Open(); + //// Create our sub events. + //KAutoObject::Create(readable_event.get()); + //KAutoObject::Create(writable_event.get()); + // Create our sub events. readable_event = std::make_shared(kernel, name_ + ":Readable"); writable_event = std::make_shared(kernel, name_ + ":Writable"); diff --git a/src/core/hle/kernel/k_linked_list.h b/src/core/hle/kernel/k_linked_list.h index 8362b6eda..8218bac8d 100644 --- a/src/core/hle/kernel/k_linked_list.h +++ b/src/core/hle/kernel/k_linked_list.h @@ -11,6 +11,8 @@ namespace Kernel { +class KernelCore; + class KLinkedListNode : public boost::intrusive::list_base_hook<>, public KSlabAllocated { private: @@ -118,11 +120,11 @@ public: }; public: - constexpr KLinkedList() : BaseList() {} + constexpr KLinkedList(KernelCore& kernel_) : BaseList(), kernel{kernel_} {} ~KLinkedList() { // Erase all elements. - for (auto it = this->begin(); it != this->end(); it = this->erase(it)) { + for (auto it = this->begin(); it != this->end(); it = this->erase(kernel, it)) { } // Ensure we succeeded. @@ -199,7 +201,7 @@ public: } iterator insert(const_iterator pos, reference ref) { - KLinkedListNode* node = KLinkedListNode::Allocate(); + KLinkedListNode* node = KLinkedListNode::Allocate(kernel); ASSERT(node != nullptr); node->Initialize(std::addressof(ref)); return iterator(BaseList::insert(pos.m_base_it, *node)); @@ -221,13 +223,16 @@ public: this->erase(this->begin()); } - iterator erase(const iterator pos) { + iterator erase(KernelCore& kernel, const iterator pos) { KLinkedListNode* freed_node = std::addressof(*pos.m_base_it); iterator ret = iterator(BaseList::erase(pos.m_base_it)); - KLinkedListNode::Free(freed_node); + KLinkedListNode::Free(kernel, freed_node); return ret; } + +private: + KernelCore& kernel; }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index 09382f7dd..1feda9303 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -617,7 +617,9 @@ KScheduler::KScheduler(Core::System& system, s32 core_id) : system(system), core state.highest_priority_thread = nullptr; } -KScheduler::~KScheduler() = default; +KScheduler::~KScheduler() { + idle_thread->Close(); +} KThread* KScheduler::GetCurrentThread() const { if (auto result = current_thread.load(); result) { diff --git a/src/core/hle/kernel/k_slab_heap.h b/src/core/hle/kernel/k_slab_heap.h index a3948cd27..5ce9a1d7c 100644 --- a/src/core/hle/kernel/k_slab_heap.h +++ b/src/core/hle/kernel/k_slab_heap.h @@ -97,6 +97,7 @@ public: void FreeImpl(void* obj) { // Don't allow freeing an object that wasn't allocated from this heap ASSERT(Contains(reinterpret_cast(obj))); + impl.Free(obj); } diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index b78602f46..855bb590a 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -11,9 +11,10 @@ #include #include "core/arm/cpu_interrupt_handler.h" #include "core/hardware_properties.h" +#include "core/hle/kernel/k_auto_object.h" +#include "core/hle/kernel/k_slab_heap.h" #include "core/hle/kernel/memory_types.h" #include "core/hle/kernel/object.h" -#include "core/hle/kernel/k_auto_object.h" namespace Core { class CPUInterruptHandler; @@ -32,6 +33,8 @@ class ClientPort; class GlobalSchedulerContext; class HandleTable; class KAutoObjectWithListContainer; +class KEvent; +class KLinkedListNode; class KMemoryManager; class KResourceLimit; class KScheduler; @@ -231,9 +234,10 @@ public: /** * Creates an HLE service thread, which are used to execute service routines asynchronously. - * While these are allocated per ServerSession, these need to be owned and managed outside of - * ServerSession to avoid a circular dependency. - * @param name String name for the ServerSession creating this thread, used for debug purposes. + * While these are allocated per ServerSession, these need to be owned and managed outside + * of ServerSession to avoid a circular dependency. + * @param name String name for the ServerSession creating this thread, used for debug + * purposes. * @returns The a weak pointer newly created service thread. */ std::weak_ptr CreateServiceThread(const std::string& name); @@ -252,6 +256,22 @@ public: Core::System& System(); const Core::System& System() const; + /// Gets the slab heap for the specified kernel object type. + template + KSlabHeap& SlabHeap() { + if constexpr (std::is_same_v) { + return slab_heap_Process; + } else if constexpr (std::is_same_v) { + return slab_heap_KThread; + } else if constexpr (std::is_same_v) { + return slab_heap_KEvent; + } else if constexpr (std::is_same_v) { + return slab_heap_KSharedMemory; + } else if constexpr (std::is_same_v) { + return slab_heap_KLinkedListNode; + } + } + private: friend class Object; friend class Process; @@ -277,7 +297,15 @@ private: struct Impl; std::unique_ptr impl; + bool exception_exited{}; + +private: + KSlabHeap slab_heap_Process; + KSlabHeap slab_heap_KThread; + KSlabHeap slab_heap_KEvent; + KSlabHeap slab_heap_KSharedMemory; + KSlabHeap slab_heap_KLinkedListNode; }; } // namespace Kernel diff --git a/src/core/hle/kernel/slab_helpers.h b/src/core/hle/kernel/slab_helpers.h index ae9d097da..4f23ddabf 100644 --- a/src/core/hle/kernel/slab_helpers.h +++ b/src/core/hle/kernel/slab_helpers.h @@ -20,44 +20,44 @@ namespace Kernel { template class KSlabAllocated { -private: - static inline KSlabHeap s_slab_heap; - public: constexpr KSlabAllocated() = default; - size_t GetSlabIndex() const { - return s_slab_heap.GetIndex(static_cast(this)); + size_t GetSlabIndex(KernelCore& kernel) const { + return kernel.SlabHeap().GetIndex(static_cast(this)); } public: - static void InitializeSlabHeap(void* memory, size_t memory_size) { - s_slab_heap.Initialize(memory, memory_size); + static void InitializeSlabHeap(KernelCore& kernel, void* memory, size_t memory_size) { + kernel.SlabHeap().Initialize(memory, memory_size); } - static Derived* Allocate() { - return s_slab_heap.Allocate(); + static Derived* Allocate(KernelCore& kernel) { + return kernel.SlabHeap().Allocate(); } - static void Free(Derived* obj) { - s_slab_heap.Free(obj); + static void Free(KernelCore& kernel, Derived* obj) { + kernel.SlabHeap().Free(obj); } - static size_t GetObjectSize() { - return s_slab_heap.GetObjectSize(); - } - static size_t GetSlabHeapSize() { - return s_slab_heap.GetSlabHeapSize(); - } - static size_t GetPeakIndex() { - return s_slab_heap.GetPeakIndex(); - } - static uintptr_t GetSlabHeapAddress() { - return s_slab_heap.GetSlabHeapAddress(); + static size_t GetObjectSize(KernelCore& kernel) { + return kernel.SlabHeap().GetObjectSize(); } - static size_t GetNumRemaining() { - return s_slab_heap.GetNumRemaining(); + static size_t GetSlabHeapSize(KernelCore& kernel) { + return kernel.SlabHeap().GetSlabHeapSize(); + } + + static size_t GetPeakIndex(KernelCore& kernel) { + return kernel.SlabHeap().GetPeakIndex(); + } + + static uintptr_t GetSlabHeapAddress(KernelCore& kernel) { + return kernel.SlabHeap().GetSlabHeapAddress(); + } + + static size_t GetNumRemaining(KernelCore& kernel) { + return kernel.SlabHeap().GetNumRemaining(); } }; @@ -66,43 +66,38 @@ class KAutoObjectWithSlabHeapAndContainer : public Base { static_assert(std::is_base_of::value); private: - static inline KSlabHeap s_slab_heap; - KernelCore& m_kernel; - -private: - static Derived* Allocate() { - return s_slab_heap.Allocate(); + static Derived* Allocate(KernelCore& kernel) { + return kernel.SlabHeap().Allocate(); } static Derived* AllocateWithKernel(KernelCore& kernel) { - return s_slab_heap.AllocateWithKernel(kernel); + return kernel.SlabHeap().AllocateWithKernel(kernel); } - static void Free(Derived* obj) { - s_slab_heap.Free(obj); + static void Free(KernelCore& kernel, Derived* obj) { + kernel.SlabHeap().Free(obj); } public: class ListAccessor : public KAutoObjectWithListContainer::ListAccessor { public: - ListAccessor() - : KAutoObjectWithListContainer::ListAccessor(m_kernel.ObjectListContainer()) {} + ListAccessor() : KAutoObjectWithListContainer::ListAccessor(kernel.ObjectListContainer()) {} ~ListAccessor() = default; }; public: - KAutoObjectWithSlabHeapAndContainer(KernelCore& kernel) : Base(kernel), m_kernel(kernel) {} + KAutoObjectWithSlabHeapAndContainer(KernelCore& kernel_) : Base(kernel_), kernel(kernel_) {} virtual ~KAutoObjectWithSlabHeapAndContainer() {} virtual void Destroy() override { const bool is_initialized = this->IsInitialized(); uintptr_t arg = 0; if (is_initialized) { - m_kernel.ObjectListContainer().Unregister(this); + kernel.ObjectListContainer().Unregister(this); arg = this->GetPostDestroyArgument(); this->Finalize(); } - Free(static_cast(this)); + Free(kernel, static_cast(this)); if (is_initialized) { Derived::PostDestroy(arg); } @@ -116,12 +111,12 @@ public: } size_t GetSlabIndex() const { - return s_slab_heap.GetObjectIndex(static_cast(this)); + return SlabHeap(kernel).GetObjectIndex(static_cast(this)); } public: static void InitializeSlabHeap(KernelCore& kernel, void* memory, size_t memory_size) { - s_slab_heap.Initialize(memory, memory_size); + kernel.SlabHeap().Initialize(memory, memory_size); kernel.ObjectListContainer().Initialize(); } @@ -145,22 +140,28 @@ public: return kernel.ObjectListContainer().Register(obj); } - static size_t GetObjectSize() { - return s_slab_heap.GetObjectSize(); - } - static size_t GetSlabHeapSize() { - return s_slab_heap.GetSlabHeapSize(); - } - static size_t GetPeakIndex() { - return s_slab_heap.GetPeakIndex(); - } - static uintptr_t GetSlabHeapAddress() { - return s_slab_heap.GetSlabHeapAddress(); + static size_t GetObjectSize(KernelCore& kernel) { + return kernel.SlabHeap().GetObjectSize(); } - static size_t GetNumRemaining() { - return s_slab_heap.GetNumRemaining(); + static size_t GetSlabHeapSize(KernelCore& kernel) { + return kernel.SlabHeap().GetSlabHeapSize(); } + + static size_t GetPeakIndex(KernelCore& kernel) { + return kernel.SlabHeap().GetPeakIndex(); + } + + static uintptr_t GetSlabHeapAddress(KernelCore& kernel) { + return kernel.SlabHeap().GetSlabHeapAddress(); + } + + static size_t GetNumRemaining(KernelCore& kernel) { + return kernel.SlabHeap().GetNumRemaining(); + } + +protected: + KernelCore& kernel; }; } // namespace Kernel From 89edbe8aa20d278d6f2c5ab735163f0d96ff88d2 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 9 Apr 2021 22:42:23 -0700 Subject: [PATCH 25/82] hle: kernel: Refactor several threads/events/sharedmemory to use slab heaps. --- src/core/core.cpp | 2 +- src/core/hle/kernel/k_scheduler.cpp | 16 +++--- src/core/hle/kernel/k_scheduler.h | 4 +- src/core/hle/kernel/kernel.cpp | 50 +++++++++---------- src/core/hle/kernel/process.cpp | 2 +- src/core/hle/kernel/slab_helpers.h | 16 +----- src/core/hle/kernel/svc.cpp | 4 +- src/core/hle/service/hid/controllers/npad.cpp | 8 ++- src/core/hle/service/hid/controllers/npad.h | 5 +- src/core/hle/service/nvdrv/nvdrv.cpp | 3 +- src/core/hle/service/nvdrv/nvdrv.h | 2 +- 11 files changed, 53 insertions(+), 59 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index f050a8d4b..b5bc903cd 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -233,7 +233,7 @@ struct System::Impl { } telemetry_session->AddInitialInfo(*app_loader, fs_controller, *content_provider); - auto main_process = Kernel::Process::CreateWithKernel(system.Kernel()); + auto main_process = Kernel::Process::Create(system.Kernel()); ASSERT(Kernel::Process::Initialize(main_process, system, "main", Kernel::Process::ProcessType::Userland) .IsSuccess()); diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index 1feda9303..38c6b50fa 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -618,14 +618,17 @@ KScheduler::KScheduler(Core::System& system, s32 core_id) : system(system), core } KScheduler::~KScheduler() { - idle_thread->Close(); + if (idle_thread) { + idle_thread->Close(); + idle_thread = nullptr; + } } KThread* KScheduler::GetCurrentThread() const { if (auto result = current_thread.load(); result) { return result; } - return idle_thread.get(); + return idle_thread; } u64 KScheduler::GetLastContextSwitchTicks() const { @@ -710,7 +713,7 @@ void KScheduler::ScheduleImpl() { // We never want to schedule a null thread, so use the idle thread if we don't have a next. if (next_thread == nullptr) { - next_thread = idle_thread.get(); + next_thread = idle_thread; } // If we're not actually switching thread, there's nothing to do. @@ -771,7 +774,7 @@ void KScheduler::SwitchToCurrent() { break; } } - auto thread = next_thread ? next_thread : idle_thread.get(); + auto thread = next_thread ? next_thread : idle_thread; Common::Fiber::YieldTo(switch_fiber, *thread->GetHostContext()); } while (!is_switch_pending()); } @@ -794,9 +797,8 @@ void KScheduler::UpdateLastContextSwitchTime(KThread* thread, Process* process) } void KScheduler::Initialize() { - idle_thread = std::make_unique(system.Kernel()); - KAutoObject::Create(idle_thread.get()); - ASSERT(KThread::InitializeIdleThread(system, idle_thread.get(), core_id).IsSuccess()); + idle_thread = KThread::Create(system.Kernel()); + ASSERT(KThread::InitializeIdleThread(system, idle_thread, core_id).IsSuccess()); idle_thread->SetName(fmt::format("IdleThread:{}", core_id)); } diff --git a/src/core/hle/kernel/k_scheduler.h b/src/core/hle/kernel/k_scheduler.h index 8cb5f6f36..01387b892 100644 --- a/src/core/hle/kernel/k_scheduler.h +++ b/src/core/hle/kernel/k_scheduler.h @@ -51,7 +51,7 @@ public: /// Returns true if the scheduler is idle [[nodiscard]] bool IsIdle() const { - return GetCurrentThread() == idle_thread.get(); + return GetCurrentThread() == idle_thread; } /// Gets the timestamp for the last context switch in ticks. @@ -173,7 +173,7 @@ private: KThread* prev_thread{}; std::atomic current_thread{}; - std::unique_ptr idle_thread; + KThread* idle_thread{}; std::shared_ptr switch_fiber{}; diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 43bce1863..1b7ba39f4 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -102,15 +102,21 @@ struct KernelCore::Impl { next_user_process_id = Process::ProcessIDMin; next_thread_id = 1; - for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) { - if (suspend_threads[i]) { - suspend_threads[i]->Close(); + for (s32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { + if (suspend_threads[core_id]) { + suspend_threads[core_id]->Close(); + suspend_threads[core_id] = nullptr; } + + schedulers[core_id].reset(); } cores.clear(); - current_process = nullptr; + if (current_process) { + current_process->Close(); + current_process = nullptr; + } global_handle_table.Clear(); @@ -195,10 +201,9 @@ struct KernelCore::Impl { void InitializeSuspendThreads() { for (s32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { - suspend_threads[core_id] = std::make_unique(system.Kernel()); - KAutoObject::Create(suspend_threads[core_id].get()); - ASSERT(KThread::InitializeHighPriorityThread(system, suspend_threads[core_id].get(), {}, - {}, core_id) + suspend_threads[core_id] = KThread::Create(system.Kernel()); + ASSERT(KThread::InitializeHighPriorityThread(system, suspend_threads[core_id], {}, {}, + core_id) .IsSuccess()); suspend_threads[core_id]->SetName(fmt::format("SuspendThread:{}", core_id)); } @@ -577,15 +582,10 @@ struct KernelCore::Impl { const PAddr irs_phys_addr{system_pool.GetAddress() + hid_size + font_size}; const PAddr time_phys_addr{system_pool.GetAddress() + hid_size + font_size + irs_size}; - hid_shared_mem = std::make_unique(system.Kernel()); - font_shared_mem = std::make_unique(system.Kernel()); - irs_shared_mem = std::make_unique(system.Kernel()); - time_shared_mem = std::make_unique(system.Kernel()); - - KAutoObject::Create(hid_shared_mem.get()); - KAutoObject::Create(font_shared_mem.get()); - KAutoObject::Create(irs_shared_mem.get()); - KAutoObject::Create(time_shared_mem.get()); + hid_shared_mem = KSharedMemory::Create(system.Kernel()); + font_shared_mem = KSharedMemory::Create(system.Kernel()); + irs_shared_mem = KSharedMemory::Create(system.Kernel()); + time_shared_mem = KSharedMemory::Create(system.Kernel()); hid_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr, {hid_phys_addr, hid_size / PageSize}, KMemoryPermission::None, @@ -656,10 +656,10 @@ struct KernelCore::Impl { std::unique_ptr> user_slab_heap_pages; // Shared memory for services - std::unique_ptr hid_shared_mem; - std::unique_ptr font_shared_mem; - std::unique_ptr irs_shared_mem; - std::unique_ptr time_shared_mem; + Kernel::KSharedMemory* hid_shared_mem{}; + Kernel::KSharedMemory* font_shared_mem{}; + Kernel::KSharedMemory* irs_shared_mem{}; + Kernel::KSharedMemory* time_shared_mem{}; // Threads used for services std::unordered_set> service_threads; @@ -668,7 +668,7 @@ struct KernelCore::Impl { // the release of itself std::unique_ptr service_thread_manager; - std::array, Core::Hardware::NUM_CPU_CORES> suspend_threads; + std::array suspend_threads; std::array interrupts{}; std::array, Core::Hardware::NUM_CPU_CORES> schedulers{}; @@ -938,9 +938,9 @@ void KernelCore::Suspend(bool in_suspention) { { KScopedSchedulerLock lock(*this); const auto state = should_suspend ? ThreadState::Runnable : ThreadState::Waiting; - for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) { - impl->suspend_threads[i]->SetState(state); - impl->suspend_threads[i]->SetWaitReasonForDebugging( + for (s32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { + impl->suspend_threads[core_id]->SetState(state); + impl->suspend_threads[core_id]->SetWaitReasonForDebugging( ThreadWaitReasonForDebugging::Suspended); } } diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index fe4558648..8088c634f 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -41,7 +41,7 @@ void SetupMainThread(Core::System& system, Process& owner_process, u32 priority, const VAddr entry_point = owner_process.PageTable().GetCodeRegionStart(); ASSERT(owner_process.GetResourceLimit()->Reserve(LimitableResource::Threads, 1)); - KThread* thread = KThread::CreateWithKernel(system.Kernel()); + KThread* thread = KThread::Create(system.Kernel()); ASSERT(KThread::InitializeUserThread(system, thread, entry_point, 0, stack_top, priority, owner_process.GetIdealCoreId(), &owner_process) .IsSuccess()); diff --git a/src/core/hle/kernel/slab_helpers.h b/src/core/hle/kernel/slab_helpers.h index 4f23ddabf..66954b6b2 100644 --- a/src/core/hle/kernel/slab_helpers.h +++ b/src/core/hle/kernel/slab_helpers.h @@ -67,10 +67,6 @@ class KAutoObjectWithSlabHeapAndContainer : public Base { private: static Derived* Allocate(KernelCore& kernel) { - return kernel.SlabHeap().Allocate(); - } - - static Derived* AllocateWithKernel(KernelCore& kernel) { return kernel.SlabHeap().AllocateWithKernel(kernel); } @@ -120,16 +116,8 @@ public: kernel.ObjectListContainer().Initialize(); } - static Derived* Create() { - Derived* obj = Allocate(); - if (obj != nullptr) { - KAutoObject::Create(obj); - } - return obj; - } - - static Derived* CreateWithKernel(KernelCore& kernel) { - Derived* obj = AllocateWithKernel(kernel); + static Derived* Create(KernelCore& kernel) { + Derived* obj = Allocate(kernel); if (obj != nullptr) { KAutoObject::Create(obj); } diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index b143a51c7..8050359be 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -1431,7 +1431,7 @@ static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr e } // Create the thread. - KThread* thread = KThread::CreateWithKernel(kernel); + KThread* thread = KThread::Create(kernel); if (!thread) { LOG_ERROR(Kernel_SVC, "Unable to create new threads. Thread creation limit reached."); return ResultOutOfResource; @@ -1953,7 +1953,7 @@ static ResultCode CreateEvent(Core::System& system, Handle* out_write, Handle* o HandleTable& handle_table = kernel.CurrentProcess()->GetHandleTable(); // Create a new event. - KEvent* event = KEvent::CreateWithKernel(kernel); + KEvent* event = KEvent::Create(kernel); R_UNLESS(event != nullptr, ResultOutOfResource); // Initialize the event. diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp index 753875d6e..77768ca7d 100644 --- a/src/core/hle/service/hid/controllers/npad.cpp +++ b/src/core/hle/service/hid/controllers/npad.cpp @@ -253,8 +253,7 @@ void Controller_NPad::InitNewlyAddedController(std::size_t controller_idx) { void Controller_NPad::OnInit() { auto& kernel = system.Kernel(); for (std::size_t i = 0; i < styleset_changed_events.size(); ++i) { - styleset_changed_events[i] = std::make_unique(kernel); - Kernel::KAutoObject::Create(styleset_changed_events[i].get()); + styleset_changed_events[i] = Kernel::KEvent::Create(kernel); styleset_changed_events[i]->Initialize(fmt::format("npad:NpadStyleSetChanged_{}", i)); } @@ -341,6 +340,11 @@ void Controller_NPad::OnRelease() { VibrateControllerAtIndex(npad_idx, device_idx, {}); } } + + for (std::size_t i = 0; i < styleset_changed_events.size(); ++i) { + styleset_changed_events[i]->Close(); + styleset_changed_events[i] = nullptr; + } } void Controller_NPad::RequestPadStateUpdate(u32 npad_id) { diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h index 515cf7c37..b3de272b6 100644 --- a/src/core/hle/service/hid/controllers/npad.h +++ b/src/core/hle/service/hid/controllers/npad.h @@ -573,8 +573,9 @@ private: NpadHandheldActivationMode handheld_activation_mode{NpadHandheldActivationMode::Dual}; NpadCommunicationMode communication_mode{NpadCommunicationMode::Default}; // Each controller should have their own styleset changed event - std::array, 10> styleset_changed_events; - std::array, 10> last_vibration_timepoints; + std::array styleset_changed_events{}; + std::array, 10> + last_vibration_timepoints{}; std::array, 10> latest_vibration_values{}; bool permit_vibration_session_enabled{false}; std::array, 10> vibration_devices_mounted{}; diff --git a/src/core/hle/service/nvdrv/nvdrv.cpp b/src/core/hle/service/nvdrv/nvdrv.cpp index 64bdb0722..2fbf61cd6 100644 --- a/src/core/hle/service/nvdrv/nvdrv.cpp +++ b/src/core/hle/service/nvdrv/nvdrv.cpp @@ -42,8 +42,7 @@ void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger Module::Module(Core::System& system) : syncpoint_manager{system.GPU()} { auto& kernel = system.Kernel(); for (u32 i = 0; i < MaxNvEvents; i++) { - events_interface.events[i].event = std::make_unique(kernel); - Kernel::KAutoObject::Create(events_interface.events[i].event.get()); + events_interface.events[i].event = Kernel::KEvent::Create(kernel); events_interface.events[i].event->Initialize(fmt::format("NVDRV::NvEvent_{}", i)); events_interface.status[i] = EventState::Free; events_interface.registered[i] = false; diff --git a/src/core/hle/service/nvdrv/nvdrv.h b/src/core/hle/service/nvdrv/nvdrv.h index 76f77cbb1..ea5dbbdf9 100644 --- a/src/core/hle/service/nvdrv/nvdrv.h +++ b/src/core/hle/service/nvdrv/nvdrv.h @@ -35,7 +35,7 @@ class nvdevice; /// Represents an Nvidia event struct NvEvent { - std::unique_ptr event; + Kernel::KEvent* event{}; Fence fence{}; }; From cfa7b9256371e689e51ab17fd1e564c556889e1a Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 9 Apr 2021 23:16:13 -0700 Subject: [PATCH 26/82] hle: kernel: Move slab heaps to their own container. --- src/core/hle/kernel/kernel.cpp | 1 + src/core/hle/kernel/kernel.h | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 1b7ba39f4..472c71cf1 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -692,6 +692,7 @@ void KernelCore::SetMulticore(bool is_multicore) { } void KernelCore::Initialize() { + slab_heap_container = std::make_unique(); impl->Initialize(*this); } diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 855bb590a..e494fe9f3 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -260,15 +260,15 @@ public: template KSlabHeap& SlabHeap() { if constexpr (std::is_same_v) { - return slab_heap_Process; + return slab_heap_container->process; } else if constexpr (std::is_same_v) { - return slab_heap_KThread; + return slab_heap_container->thread; } else if constexpr (std::is_same_v) { - return slab_heap_KEvent; + return slab_heap_container->event; } else if constexpr (std::is_same_v) { - return slab_heap_KSharedMemory; + return slab_heap_container->shared_memory; } else if constexpr (std::is_same_v) { - return slab_heap_KLinkedListNode; + return slab_heap_container->linked_list_node; } } @@ -301,11 +301,16 @@ private: bool exception_exited{}; private: - KSlabHeap slab_heap_Process; - KSlabHeap slab_heap_KThread; - KSlabHeap slab_heap_KEvent; - KSlabHeap slab_heap_KSharedMemory; - KSlabHeap slab_heap_KLinkedListNode; + /// Helper to encapsulate all slab heaps in a single heap allocated container + struct SlabHeapContainer { + KSlabHeap process; + KSlabHeap thread; + KSlabHeap event; + KSlabHeap shared_memory; + KSlabHeap linked_list_node; + }; + + std::unique_ptr slab_heap_container; }; } // namespace Kernel From eba3bb9d219283ffc2a7610d925aa157a81b6e4a Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 10 Apr 2021 02:23:17 -0700 Subject: [PATCH 27/82] hle: ipc_helpers: Add methods for copy/move references. --- src/core/hle/ipc_helpers.h | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index 224bee950..8f12616bd 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -217,9 +217,15 @@ public: template void PushMoveObjects(O*... pointers); + template + void PushMoveObjects(O&... pointers); + template void PushCopyObjects(O*... pointers); + template + void PushCopyObjects(O&... pointers); + private: u32 normal_params_size{}; u32 num_handles_to_copy{}; @@ -304,7 +310,15 @@ template inline void ResponseBuilder::PushCopyObjects(O*... pointers) { auto objects = {pointers...}; for (auto& object : objects) { - context->AddCopyObject(std::move(object)); + context->AddCopyObject(object); + } +} + +template +inline void ResponseBuilder::PushCopyObjects(O&... pointers) { + auto objects = {&pointers...}; + for (auto& object : objects) { + context->AddCopyObject(object); } } @@ -312,7 +326,15 @@ template inline void ResponseBuilder::PushMoveObjects(O*... pointers) { auto objects = {pointers...}; for (auto& object : objects) { - context->AddMoveObject(std::move(object)); + context->AddMoveObject(object); + } +} + +template +inline void ResponseBuilder::PushMoveObjects(O&... pointers) { + auto objects = {&pointers...}; + for (auto& object : objects) { + context->AddMoveObject(object); } } From 2e8d6fe9a0c07538397682e1cb25992bfd15676d Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 10 Apr 2021 02:34:26 -0700 Subject: [PATCH 28/82] hle: kernel: Migrate KReadableEvent and KWritableEvent to KAutoObject. --- src/core/hle/kernel/handle_table.cpp | 4 +- src/core/hle/kernel/k_event.cpp | 17 ++--- src/core/hle/kernel/k_event.h | 37 +++++----- src/core/hle/kernel/k_readable_event.cpp | 15 ++-- src/core/hle/kernel/k_readable_event.h | 43 ++++++----- src/core/hle/kernel/k_writable_event.cpp | 18 +++-- src/core/hle/kernel/k_writable_event.h | 34 +++++---- src/core/hle/kernel/kernel.h | 4 ++ src/core/hle/kernel/svc.cpp | 72 +++++++++---------- src/core/hle/service/am/am.cpp | 24 +++---- src/core/hle/service/am/am.h | 4 +- src/core/hle/service/am/applets/applets.cpp | 22 +++--- src/core/hle/service/am/applets/applets.h | 8 +-- src/core/hle/service/audio/audout_u.cpp | 2 +- src/core/hle/service/audio/audren_u.cpp | 4 +- src/core/hle/service/bcat/backend/backend.cpp | 8 +-- src/core/hle/service/bcat/backend/backend.h | 4 +- src/core/hle/service/bcat/module.cpp | 10 ++- src/core/hle/service/hid/controllers/npad.cpp | 9 ++- src/core/hle/service/hid/controllers/npad.h | 2 +- src/core/hle/service/nfp/nfp.cpp | 10 +-- src/core/hle/service/nfp/nfp.h | 2 +- src/core/hle/service/nim/nim.cpp | 4 +- .../hle/service/nvdrv/devices/nvhost_ctrl.cpp | 6 +- src/core/hle/service/nvdrv/interface.cpp | 6 +- src/core/hle/service/nvdrv/nvdrv.cpp | 8 +-- src/core/hle/service/nvdrv/nvdrv.h | 4 +- .../hle/service/nvflinger/buffer_queue.cpp | 12 ++-- src/core/hle/service/nvflinger/buffer_queue.h | 4 +- src/core/hle/service/nvflinger/nvflinger.cpp | 6 +- src/core/hle/service/nvflinger/nvflinger.h | 2 +- src/core/hle/service/ptm/psm.cpp | 6 +- .../hle/service/vi/display/vi_display.cpp | 4 +- src/core/hle/service/vi/display/vi_display.h | 2 +- src/core/hle/service/vi/vi.cpp | 6 +- 35 files changed, 219 insertions(+), 204 deletions(-) diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index 58c49460f..cc3210ef2 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -54,7 +54,9 @@ ResultVal HandleTable::Create(Object* obj) { case HandleType::SharedMemory: case HandleType::Thread: case HandleType::Event: - case HandleType::Process: { + case HandleType::Process: + case HandleType::ReadableEvent: + case HandleType::WritableEvent: { Handle handle{}; Add(&handle, reinterpret_cast(obj), {}); return MakeResult(handle); diff --git a/src/core/hle/kernel/k_event.cpp b/src/core/hle/kernel/k_event.cpp index 4020e5325..fdec0c36f 100644 --- a/src/core/hle/kernel/k_event.cpp +++ b/src/core/hle/kernel/k_event.cpp @@ -3,14 +3,13 @@ // Refer to the license.txt file included. #include "core/hle/kernel/k_event.h" -#include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_resource_limit.h" -#include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/process.h" namespace Kernel { -KEvent::KEvent(KernelCore& kernel) : KAutoObjectWithSlabHeapAndContainer{kernel} {} +KEvent::KEvent(KernelCore& kernel) + : KAutoObjectWithSlabHeapAndContainer{kernel}, readable_event{kernel}, writable_event{kernel} {} KEvent::~KEvent() = default; @@ -21,17 +20,13 @@ void KEvent::Initialize(std::string&& name_) { // writable events are closed this object will be destroyed. Open(); - //// Create our sub events. - //KAutoObject::Create(readable_event.get()); - //KAutoObject::Create(writable_event.get()); - // Create our sub events. - readable_event = std::make_shared(kernel, name_ + ":Readable"); - writable_event = std::make_shared(kernel, name_ + ":Writable"); + KAutoObject::Create(std::addressof(readable_event)); + KAutoObject::Create(std::addressof(writable_event)); // Initialize our sub sessions. - readable_event->Initialize(this); - writable_event->Initialize(this); + readable_event.Initialize(this, name_ + ":Readable"); + writable_event.Initialize(this, name_ + ":Writable"); // Set our owner process. owner = kernel.CurrentProcess(); diff --git a/src/core/hle/kernel/k_event.h b/src/core/hle/kernel/k_event.h index 97ec0ea9c..2c48a0499 100644 --- a/src/core/hle/kernel/k_event.h +++ b/src/core/hle/kernel/k_event.h @@ -4,6 +4,8 @@ #pragma once +#include "core/hle/kernel/k_readable_event.h" +#include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/slab_helpers.h" namespace Kernel { @@ -27,12 +29,27 @@ public: virtual bool IsInitialized() const override { return initialized; } + virtual uintptr_t GetPostDestroyArgument() const override { return reinterpret_cast(owner); } static void PostDestroy(uintptr_t arg); + virtual Process* GetOwner() const override { + return owner; + } + + KReadableEvent& GetReadableEvent() { + return readable_event; + } + + KWritableEvent& GetWritableEvent() { + return writable_event; + } + + // DEPRECATED + std::string GetTypeName() const override { return "KEvent"; } @@ -42,25 +59,9 @@ public: return HANDLE_TYPE; } - KReadableEvent* GetReadableEvent() { - return readable_event.get(); - } - - std::shared_ptr& GetWritableEvent() { - return writable_event; - } - - const std::shared_ptr& GetReadableEvent() const { - return readable_event; - } - - const std::shared_ptr& GetWritableEvent() const { - return writable_event; - } - private: - std::shared_ptr readable_event; - std::shared_ptr writable_event; + KReadableEvent readable_event; + KWritableEvent writable_event; Process* owner{}; bool initialized{}; }; diff --git a/src/core/hle/kernel/k_readable_event.cpp b/src/core/hle/kernel/k_readable_event.cpp index 4b4d34857..8fef4bb00 100644 --- a/src/core/hle/kernel/k_readable_event.cpp +++ b/src/core/hle/kernel/k_readable_event.cpp @@ -2,21 +2,18 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include #include "common/assert.h" -#include "common/common_funcs.h" -#include "common/logging/log.h" +#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/object.h" #include "core/hle/kernel/svc_results.h" namespace Kernel { -KReadableEvent::KReadableEvent(KernelCore& kernel, std::string&& name) - : KSynchronizationObject{kernel, std::move(name)} {} +KReadableEvent::KReadableEvent(KernelCore& kernel) : KSynchronizationObject{kernel} {} + KReadableEvent::~KReadableEvent() = default; bool KReadableEvent::IsSignaled() const { @@ -25,6 +22,12 @@ bool KReadableEvent::IsSignaled() const { return is_signaled; } +void KReadableEvent::Destroy() { + if (parent) { + parent->Close(); + } +} + ResultCode KReadableEvent::Signal() { KScopedSchedulerLock lk{kernel}; diff --git a/src/core/hle/kernel/k_readable_event.h b/src/core/hle/kernel/k_readable_event.h index e6f0fd900..4c22f0584 100644 --- a/src/core/hle/kernel/k_readable_event.h +++ b/src/core/hle/kernel/k_readable_event.h @@ -4,8 +4,10 @@ #pragma once +#include "core/hle/kernel/k_auto_object.h" #include "core/hle/kernel/k_synchronization_object.h" #include "core/hle/kernel/object.h" +#include "core/hle/kernel/slab_helpers.h" #include "core/hle/result.h" namespace Kernel { @@ -13,11 +15,32 @@ namespace Kernel { class KernelCore; class KEvent; -class KReadableEvent final : public KSynchronizationObject { +class KReadableEvent : public KSynchronizationObject { + KERNEL_AUTOOBJECT_TRAITS(KReadableEvent, KSynchronizationObject); + public: - explicit KReadableEvent(KernelCore& kernel, std::string&& name); + explicit KReadableEvent(KernelCore& kernel); ~KReadableEvent() override; + void Initialize(KEvent* parent_, std::string&& name_) { + is_signaled = false; + parent = parent_; + name = std::move(name_); + } + + constexpr KEvent* GetParent() const { + return parent; + } + + virtual bool IsSignaled() const override; + virtual void Destroy() override; + + ResultCode Signal(); + ResultCode Clear(); + ResultCode Reset(); + + // DEPRECATED + std::string GetTypeName() const override { return "KReadableEvent"; } @@ -27,22 +50,6 @@ public: return HANDLE_TYPE; } - KEvent* GetParent() const { - return parent; - } - - void Initialize(KEvent* parent_) { - is_signaled = false; - parent = parent_; - } - - bool IsSignaled() const override; - void Finalize() override {} - - ResultCode Signal(); - ResultCode Clear(); - ResultCode Reset(); - private: bool is_signaled{}; KEvent* parent{}; diff --git a/src/core/hle/kernel/k_writable_event.cpp b/src/core/hle/kernel/k_writable_event.cpp index 25c52edb2..a430e0661 100644 --- a/src/core/hle/kernel/k_writable_event.cpp +++ b/src/core/hle/kernel/k_writable_event.cpp @@ -8,20 +8,28 @@ namespace Kernel { -KWritableEvent::KWritableEvent(KernelCore& kernel, std::string&& name) - : Object{kernel, std::move(name)} {} +KWritableEvent::KWritableEvent(KernelCore& kernel) : KAutoObjectWithSlabHeapAndContainer{kernel} {} + KWritableEvent::~KWritableEvent() = default; -void KWritableEvent::Initialize(KEvent* parent_) { +void KWritableEvent::Initialize(KEvent* parent_, std::string&& name_) { parent = parent_; + name = std::move(name_); + parent->GetReadableEvent().Open(); } ResultCode KWritableEvent::Signal() { - return parent->GetReadableEvent()->Signal(); + return parent->GetReadableEvent().Signal(); } ResultCode KWritableEvent::Clear() { - return parent->GetReadableEvent()->Clear(); + return parent->GetReadableEvent().Clear(); +} + +void KWritableEvent::Destroy() { + // Close our references. + parent->GetReadableEvent().Close(); + parent->Close(); } } // namespace Kernel diff --git a/src/core/hle/kernel/k_writable_event.h b/src/core/hle/kernel/k_writable_event.h index 518f5448d..7cf43f77e 100644 --- a/src/core/hle/kernel/k_writable_event.h +++ b/src/core/hle/kernel/k_writable_event.h @@ -4,7 +4,9 @@ #pragma once +#include "core/hle/kernel/k_auto_object.h" #include "core/hle/kernel/object.h" +#include "core/hle/kernel/slab_helpers.h" #include "core/hle/result.h" namespace Kernel { @@ -12,11 +14,28 @@ namespace Kernel { class KernelCore; class KEvent; -class KWritableEvent final : public Object { +class KWritableEvent final + : public KAutoObjectWithSlabHeapAndContainer { + KERNEL_AUTOOBJECT_TRAITS(KWritableEvent, KAutoObject); + public: - explicit KWritableEvent(KernelCore& kernel, std::string&& name); + explicit KWritableEvent(KernelCore& kernel); ~KWritableEvent() override; + virtual void Destroy() override; + + static void PostDestroy([[maybe_unused]] uintptr_t arg) {} + + void Initialize(KEvent* parent_, std::string&& name_); + ResultCode Signal(); + ResultCode Clear(); + + constexpr KEvent* GetParent() const { + return parent; + } + + // DEPRECATED + std::string GetTypeName() const override { return "KWritableEvent"; } @@ -26,17 +45,6 @@ public: return HANDLE_TYPE; } - void Initialize(KEvent* parent_); - - void Finalize() override {} - - ResultCode Signal(); - ResultCode Clear(); - - KEvent* GetParent() const { - return parent; - } - private: KEvent* parent{}; }; diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index e494fe9f3..850436eb3 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -40,6 +40,7 @@ class KResourceLimit; class KScheduler; class KSharedMemory; class KThread; +class KWritableEvent; class PhysicalCore; class Process; class ServiceThread; @@ -269,6 +270,8 @@ public: return slab_heap_container->shared_memory; } else if constexpr (std::is_same_v) { return slab_heap_container->linked_list_node; + } else if constexpr (std::is_same_v) { + return slab_heap_container->writeable_event; } } @@ -308,6 +311,7 @@ private: KSlabHeap event; KSlabHeap shared_memory; KSlabHeap linked_list_node; + KSlabHeap writeable_event; }; std::unique_ptr slab_heap_container; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 8050359be..c6334f91c 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -1745,16 +1745,16 @@ static ResultCode ResetSignal(Core::System& system, Handle handle) { // Try to reset as readable event. { - auto readable_event = handle_table.Get(handle); - if (readable_event) { + KScopedAutoObject readable_event = handle_table.GetObject(handle); + if (readable_event.IsNotNull()) { return readable_event->Reset(); } } // Try to reset as process. { - auto process = handle_table.Get(handle); - if (process) { + KScopedAutoObject process = handle_table.GetObject(handle); + if (process.IsNotNull()) { return process->Reset(); } } @@ -1885,27 +1885,12 @@ static ResultCode SetThreadCoreMask32(Core::System& system, Handle thread_handle static ResultCode SignalEvent(Core::System& system, Handle event_handle) { LOG_DEBUG(Kernel_SVC, "called, event_handle=0x{:08X}", event_handle); - auto& kernel = system.Kernel(); // Get the current handle table. - const HandleTable& handle_table = kernel.CurrentProcess()->GetHandleTable(); - - // Reserve a new event from the process resource limit. - KScopedResourceReservation event_reservation(kernel.CurrentProcess(), - LimitableResource::Events); - if (!event_reservation.Succeeded()) { - LOG_ERROR(Kernel, "Could not reserve a new event"); - return ResultResourceLimitedExceeded; - } + const HandleTable& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); // Get the writable event. - auto writable_event = handle_table.Get(event_handle); - if (!writable_event) { - LOG_ERROR(Kernel_SVC, "Invalid event handle provided (handle={:08X})", event_handle); - return ResultInvalidHandle; - } - - // Commit the successfuly reservation. - event_reservation.Commit(); + KScopedAutoObject writable_event = handle_table.GetObject(event_handle); + R_UNLESS(writable_event.IsNotNull(), ResultInvalidHandle); return writable_event->Signal(); } @@ -1922,16 +1907,16 @@ static ResultCode ClearEvent(Core::System& system, Handle event_handle) { // Try to clear the writable event. { - auto writable_event = handle_table.Get(event_handle); - if (writable_event) { + KScopedAutoObject writable_event = handle_table.GetObject(event_handle); + if (writable_event.IsNotNull()) { return writable_event->Clear(); } } // Try to clear the readable event. { - auto readable_event = handle_table.Get(event_handle); - if (readable_event) { + KScopedAutoObject readable_event = handle_table.GetObject(event_handle); + if (readable_event.IsNotNull()) { return readable_event->Clear(); } } @@ -1950,7 +1935,12 @@ static ResultCode CreateEvent(Core::System& system, Handle* out_write, Handle* o // Get the kernel reference and handle table. auto& kernel = system.Kernel(); - HandleTable& handle_table = kernel.CurrentProcess()->GetHandleTable(); + auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); + + // Reserve a new event from the process resource limit + KScopedResourceReservation event_reservation(kernel.CurrentProcess(), + LimitableResource::Events); + R_UNLESS(event_reservation.Succeeded(), ResultResourceLimitedExceeded); // Create a new event. KEvent* event = KEvent::Create(kernel); @@ -1959,22 +1949,26 @@ static ResultCode CreateEvent(Core::System& system, Handle* out_write, Handle* o // Initialize the event. event->Initialize("CreateEvent"); - // Add the writable event to the handle table. - const auto write_create_result = handle_table.Create(event->GetWritableEvent().get()); - if (write_create_result.Failed()) { - return write_create_result.Code(); - } - *out_write = *write_create_result; + // Commit the thread reservation. + event_reservation.Commit(); + + // Ensure that we clean up the event (and its only references are handle table) on function end. + SCOPE_EXIT({ + event->GetWritableEvent().Close(); + event->GetReadableEvent().Close(); + }); + + // Register the event. + KEvent::Register(kernel, event); // Add the writable event to the handle table. - auto handle_guard = SCOPE_GUARD({ handle_table.Remove(*write_create_result); }); + R_TRY(handle_table.Add(out_write, std::addressof(event->GetWritableEvent()))); + + // Add the writable event to the handle table. + auto handle_guard = SCOPE_GUARD({ handle_table.Remove(*out_write); }); // Add the readable event to the handle table. - const auto read_create_result = handle_table.Create(event->GetReadableEvent()); - if (read_create_result.Failed()) { - return read_create_result.Code(); - } - *out_read = *read_create_result; + R_TRY(handle_table.Add(out_read, std::addressof(event->GetReadableEvent()))); // We succeeded. handle_guard.Cancel(); diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index c1982e996..937020a6e 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -320,7 +320,7 @@ ISelfController::ISelfController(Core::System& system_, NVFlinger::NVFlinger& nv Kernel::KAutoObject::Create(std::addressof(accumulated_suspended_tick_changed_event)); accumulated_suspended_tick_changed_event.Initialize( "ISelfController:AccumulatedSuspendedTickChangedEvent"); - accumulated_suspended_tick_changed_event.GetWritableEvent()->Signal(); + accumulated_suspended_tick_changed_event.GetWritableEvent().Signal(); } ISelfController::~ISelfController() = default; @@ -379,7 +379,7 @@ void ISelfController::LeaveFatalSection(Kernel::HLERequestContext& ctx) { void ISelfController::GetLibraryAppletLaunchableEvent(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_AM, "(STUBBED) called"); - launchable_event.GetWritableEvent()->Signal(); + launchable_event.GetWritableEvent().Signal(); IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); @@ -592,28 +592,28 @@ AppletMessageQueue::AppletMessageQueue(Kernel::KernelCore& kernel) AppletMessageQueue::~AppletMessageQueue() = default; -Kernel::KReadableEvent* AppletMessageQueue::GetMessageReceiveEvent() const { - return on_new_message.GetReadableEvent().get(); +Kernel::KReadableEvent& AppletMessageQueue::GetMessageReceiveEvent() { + return on_new_message.GetReadableEvent(); } -Kernel::KReadableEvent* AppletMessageQueue::GetOperationModeChangedEvent() const { - return on_operation_mode_changed.GetReadableEvent().get(); +Kernel::KReadableEvent& AppletMessageQueue::GetOperationModeChangedEvent() { + return on_operation_mode_changed.GetReadableEvent(); } void AppletMessageQueue::PushMessage(AppletMessage msg) { messages.push(msg); - on_new_message.GetWritableEvent()->Signal(); + on_new_message.GetWritableEvent().Signal(); } AppletMessageQueue::AppletMessage AppletMessageQueue::PopMessage() { if (messages.empty()) { - on_new_message.GetWritableEvent()->Clear(); + on_new_message.GetWritableEvent().Clear(); return AppletMessage::NoMessage; } auto msg = messages.front(); messages.pop(); if (messages.empty()) { - on_new_message.GetWritableEvent()->Clear(); + on_new_message.GetWritableEvent().Clear(); } return msg; } @@ -633,7 +633,7 @@ void AppletMessageQueue::FocusStateChanged() { void AppletMessageQueue::OperationModeChanged() { PushMessage(AppletMessage::OperationModeChanged); PushMessage(AppletMessage::PerformanceModeChanged); - on_operation_mode_changed.GetWritableEvent()->Signal(); + on_operation_mode_changed.GetWritableEvent().Signal(); } ICommonStateGetter::ICommonStateGetter(Core::System& system_, @@ -930,11 +930,9 @@ private: void GetAppletStateChangedEvent(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_AM, "called"); - const auto event = applet->GetBroker().GetStateChangedEvent(); - IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(event); + rb.PushCopyObjects(applet->GetBroker().GetStateChangedEvent()); } void IsCompleted(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index 8f6017c4e..fbac7b2cf 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -56,8 +56,8 @@ public: explicit AppletMessageQueue(Kernel::KernelCore& kernel); ~AppletMessageQueue(); - Kernel::KReadableEvent* GetMessageReceiveEvent() const; - Kernel::KReadableEvent* GetOperationModeChangedEvent() const; + Kernel::KReadableEvent& GetMessageReceiveEvent(); + Kernel::KReadableEvent& GetOperationModeChangedEvent(); void PushMessage(AppletMessage msg); AppletMessage PopMessage(); std::size_t GetMessageCount() const; diff --git a/src/core/hle/service/am/applets/applets.cpp b/src/core/hle/service/am/applets/applets.cpp index 6cceff064..cf8ad7598 100644 --- a/src/core/hle/service/am/applets/applets.cpp +++ b/src/core/hle/service/am/applets/applets.cpp @@ -66,7 +66,7 @@ std::shared_ptr AppletDataBroker::PopNormalDataToGame() { auto out = std::move(out_channel.front()); out_channel.pop_front(); - pop_out_data_event.GetWritableEvent()->Clear(); + pop_out_data_event.GetWritableEvent().Clear(); return out; } @@ -85,7 +85,7 @@ std::shared_ptr AppletDataBroker::PopInteractiveDataToGame() { auto out = std::move(out_interactive_channel.front()); out_interactive_channel.pop_front(); - pop_interactive_out_data_event.GetWritableEvent()->Clear(); + pop_interactive_out_data_event.GetWritableEvent().Clear(); return out; } @@ -104,7 +104,7 @@ void AppletDataBroker::PushNormalDataFromGame(std::shared_ptr&& storag void AppletDataBroker::PushNormalDataFromApplet(std::shared_ptr&& storage) { out_channel.emplace_back(std::move(storage)); - pop_out_data_event.GetWritableEvent()->Signal(); + pop_out_data_event.GetWritableEvent().Signal(); } void AppletDataBroker::PushInteractiveDataFromGame(std::shared_ptr&& storage) { @@ -113,11 +113,11 @@ void AppletDataBroker::PushInteractiveDataFromGame(std::shared_ptr&& s void AppletDataBroker::PushInteractiveDataFromApplet(std::shared_ptr&& storage) { out_interactive_channel.emplace_back(std::move(storage)); - pop_interactive_out_data_event.GetWritableEvent()->Signal(); + pop_interactive_out_data_event.GetWritableEvent().Signal(); } void AppletDataBroker::SignalStateChanged() { - state_changed_event.GetWritableEvent()->Signal(); + state_changed_event.GetWritableEvent().Signal(); switch (applet_mode) { case LibraryAppletMode::AllForeground: @@ -141,16 +141,16 @@ void AppletDataBroker::SignalStateChanged() { } } -Kernel::KReadableEvent* AppletDataBroker::GetNormalDataEvent() const { - return pop_out_data_event.GetReadableEvent().get(); +Kernel::KReadableEvent& AppletDataBroker::GetNormalDataEvent() { + return pop_out_data_event.GetReadableEvent(); } -Kernel::KReadableEvent* AppletDataBroker::GetInteractiveDataEvent() const { - return pop_interactive_out_data_event.GetReadableEvent().get(); +Kernel::KReadableEvent& AppletDataBroker::GetInteractiveDataEvent() { + return pop_interactive_out_data_event.GetReadableEvent(); } -Kernel::KReadableEvent* AppletDataBroker::GetStateChangedEvent() const { - return state_changed_event.GetReadableEvent().get(); +Kernel::KReadableEvent& AppletDataBroker::GetStateChangedEvent() { + return state_changed_event.GetReadableEvent(); } Applet::Applet(Core::System& system_, LibraryAppletMode applet_mode_) diff --git a/src/core/hle/service/am/applets/applets.h b/src/core/hle/service/am/applets/applets.h index 327843a98..229dc7a1e 100644 --- a/src/core/hle/service/am/applets/applets.h +++ b/src/core/hle/service/am/applets/applets.h @@ -96,11 +96,11 @@ public: void PushInteractiveDataFromGame(std::shared_ptr&& storage); void PushInteractiveDataFromApplet(std::shared_ptr&& storage); - void SignalStateChanged() const; + void SignalStateChanged(); - Kernel::KReadableEvent* GetNormalDataEvent() const; - Kernel::KReadableEvent* GetInteractiveDataEvent() const; - Kernel::KReadableEvent* GetStateChangedEvent() const; + Kernel::KReadableEvent& GetNormalDataEvent(); + Kernel::KReadableEvent& GetInteractiveDataEvent(); + Kernel::KReadableEvent& GetStateChangedEvent(); private: Core::System& system; diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp index 5a7d73b48..e1ae726f5 100644 --- a/src/core/hle/service/audio/audout_u.cpp +++ b/src/core/hle/service/audio/audout_u.cpp @@ -73,7 +73,7 @@ public: stream = audio_core.OpenStream(system.CoreTiming(), audio_params.sample_rate, audio_params.channel_count, std::move(unique_name), [this] { const auto guard = LockService(); - buffer_event.GetWritableEvent()->Signal(); + buffer_event.GetWritableEvent().Signal(); }); } diff --git a/src/core/hle/service/audio/audren_u.cpp b/src/core/hle/service/audio/audren_u.cpp index b0a362f52..513bd3730 100644 --- a/src/core/hle/service/audio/audren_u.cpp +++ b/src/core/hle/service/audio/audren_u.cpp @@ -55,7 +55,7 @@ public: system.CoreTiming(), system.Memory(), audren_params, [this]() { const auto guard = LockService(); - system_event.GetWritableEvent()->Signal(); + system_event.GetWritableEvent().Signal(); }, instance_number); } @@ -289,7 +289,7 @@ private: void QueryAudioDeviceSystemEvent(Kernel::HLERequestContext& ctx) { LOG_WARNING(Service_Audio, "(STUBBED) called"); - buffer_event.GetWritableEvent()->Signal(); + buffer_event.GetWritableEvent().Signal(); IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); diff --git a/src/core/hle/service/bcat/backend/backend.cpp b/src/core/hle/service/bcat/backend/backend.cpp index 8def52b31..0e935bfa6 100644 --- a/src/core/hle/service/bcat/backend/backend.cpp +++ b/src/core/hle/service/bcat/backend/backend.cpp @@ -19,7 +19,7 @@ ProgressServiceBackend::ProgressServiceBackend(Kernel::KernelCore& kernel, update_event.Initialize("ProgressServiceBackend:UpdateEvent:" + std::string(event_name)); } -std::shared_ptr ProgressServiceBackend::GetEvent() const { +Kernel::KReadableEvent& ProgressServiceBackend::GetEvent() { return update_event.GetReadableEvent(); } @@ -85,12 +85,12 @@ void ProgressServiceBackend::FinishDownload(ResultCode result) { SignalUpdate(); } -void ProgressServiceBackend::SignalUpdate() const { +void ProgressServiceBackend::SignalUpdate() { if (need_hle_lock) { std::lock_guard lock(HLE::g_hle_lock); - update_event.GetWritableEvent()->Signal(); + update_event.GetWritableEvent().Signal(); } else { - update_event.GetWritableEvent()->Signal(); + update_event.GetWritableEvent().Signal(); } } diff --git a/src/core/hle/service/bcat/backend/backend.h b/src/core/hle/service/bcat/backend/backend.h index baa4d576a..f591a362a 100644 --- a/src/core/hle/service/bcat/backend/backend.h +++ b/src/core/hle/service/bcat/backend/backend.h @@ -99,10 +99,10 @@ public: private: explicit ProgressServiceBackend(Kernel::KernelCore& kernel, std::string_view event_name); - std::shared_ptr GetEvent() const; + Kernel::KReadableEvent& GetEvent(); DeliveryCacheProgressImpl& GetImpl(); - void SignalUpdate() const; + void SignalUpdate(); DeliveryCacheProgressImpl impl{}; Kernel::KEvent update_event; diff --git a/src/core/hle/service/bcat/module.cpp b/src/core/hle/service/bcat/module.cpp index 8473b716b..05635a2a5 100644 --- a/src/core/hle/service/bcat/module.cpp +++ b/src/core/hle/service/bcat/module.cpp @@ -88,11 +88,9 @@ struct DeliveryCacheDirectoryEntry { class IDeliveryCacheProgressService final : public ServiceFramework { public: - explicit IDeliveryCacheProgressService(Core::System& system_, - std::shared_ptr event_, + explicit IDeliveryCacheProgressService(Core::System& system_, Kernel::KReadableEvent& event_, const DeliveryCacheProgressImpl& impl_) - : ServiceFramework{system_, "IDeliveryCacheProgressService"}, event{std::move(event_)}, - impl{impl_} { + : ServiceFramework{system_, "IDeliveryCacheProgressService"}, event{event_}, impl{impl_} { // clang-format off static const FunctionInfo functions[] = { {0, &IDeliveryCacheProgressService::GetEvent, "GetEvent"}, @@ -109,7 +107,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(event.get()); + rb.PushCopyObjects(event); } void GetImpl(Kernel::HLERequestContext& ctx) { @@ -121,7 +119,7 @@ private: rb.Push(RESULT_SUCCESS); } - std::shared_ptr event; + Kernel::KReadableEvent& event; const DeliveryCacheProgressImpl& impl; }; diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp index 77768ca7d..7acad3798 100644 --- a/src/core/hle/service/hid/controllers/npad.cpp +++ b/src/core/hle/service/hid/controllers/npad.cpp @@ -159,7 +159,7 @@ void Controller_NPad::InitNewlyAddedController(std::size_t controller_idx) { const auto controller_type = connected_controllers[controller_idx].type; auto& controller = shared_memory_entries[controller_idx]; if (controller_type == NPadControllerType::None) { - styleset_changed_events[controller_idx]->GetWritableEvent()->Signal(); + styleset_changed_events[controller_idx]->GetWritableEvent().Signal(); return; } controller.style_set.raw = 0; // Zero out @@ -959,13 +959,12 @@ bool Controller_NPad::IsVibrationDeviceMounted(const DeviceHandle& vibration_dev return vibration_devices_mounted[npad_index][device_index]; } -Kernel::KReadableEvent* Controller_NPad::GetStyleSetChangedEvent(u32 npad_id) const { - const auto& styleset_event = styleset_changed_events[NPadIdToIndex(npad_id)]; - return styleset_event->GetReadableEvent(); +Kernel::KReadableEvent& Controller_NPad::GetStyleSetChangedEvent(u32 npad_id) { + return styleset_changed_events[NPadIdToIndex(npad_id)]->GetReadableEvent(); } void Controller_NPad::SignalStyleSetChangedEvent(u32 npad_id) const { - styleset_changed_events[NPadIdToIndex(npad_id)]->GetWritableEvent()->Signal(); + styleset_changed_events[NPadIdToIndex(npad_id)]->GetWritableEvent().Signal(); } void Controller_NPad::AddNewControllerAt(NPadControllerType controller, std::size_t npad_index) { diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h index b3de272b6..b0f575561 100644 --- a/src/core/hle/service/hid/controllers/npad.h +++ b/src/core/hle/service/hid/controllers/npad.h @@ -199,7 +199,7 @@ public: bool IsVibrationDeviceMounted(const DeviceHandle& vibration_device_handle) const; - Kernel::KReadableEvent* GetStyleSetChangedEvent(u32 npad_id) const; + Kernel::KReadableEvent& GetStyleSetChangedEvent(u32 npad_id); void SignalStyleSetChangedEvent(u32 npad_id) const; // Adds a new controller at an index. diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index d1cf01390..d25b20ab5 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -175,7 +175,7 @@ private: switch (device_state) { case DeviceState::TagFound: case DeviceState::TagNearby: - deactivate_event.GetWritableEvent()->Signal(); + deactivate_event.GetWritableEvent().Signal(); device_state = DeviceState::Initialized; break; case DeviceState::SearchingForTag: @@ -318,7 +318,7 @@ private: const u32 npad_id{0}; // Player 1 controller State state{State::NonInitialized}; DeviceState device_state{DeviceState::Initialized}; - const Module::Interface& nfp_interface; + Module::Interface& nfp_interface; Kernel::KEvent deactivate_event; Kernel::KEvent availability_change_event; }; @@ -338,12 +338,12 @@ bool Module::Interface::LoadAmiibo(const std::vector& buffer) { } std::memcpy(&amiibo, buffer.data(), sizeof(amiibo)); - nfc_tag_load.GetWritableEvent()->Signal(); + nfc_tag_load.GetWritableEvent().Signal(); return true; } -Kernel::KReadableEvent* Module::Interface::GetNFCEvent() const { - return nfc_tag_load.GetReadableEvent().get(); +Kernel::KReadableEvent& Module::Interface::GetNFCEvent() { + return nfc_tag_load.GetReadableEvent(); } const Module::Interface::AmiiboFile& Module::Interface::GetAmiiboBuffer() const { diff --git a/src/core/hle/service/nfp/nfp.h b/src/core/hle/service/nfp/nfp.h index 4bca2192c..5e4e49bc6 100644 --- a/src/core/hle/service/nfp/nfp.h +++ b/src/core/hle/service/nfp/nfp.h @@ -39,7 +39,7 @@ public: void CreateUserInterface(Kernel::HLERequestContext& ctx); bool LoadAmiibo(const std::vector& buffer); - Kernel::KReadableEvent* GetNFCEvent() const; + Kernel::KReadableEvent& GetNFCEvent(); const AmiiboFile& GetAmiiboBuffer() const; private: diff --git a/src/core/hle/service/nim/nim.cpp b/src/core/hle/service/nim/nim.cpp index 4a2262562..420a5a075 100644 --- a/src/core/hle/service/nim/nim.cpp +++ b/src/core/hle/service/nim/nim.cpp @@ -323,7 +323,7 @@ private: void StartTask(Kernel::HLERequestContext& ctx) { // No need to connect to the internet, just finish the task straight away. LOG_DEBUG(Service_NIM, "called"); - finished_event.GetWritableEvent()->Signal(); + finished_event.GetWritableEvent().Signal(); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(RESULT_SUCCESS); } @@ -345,7 +345,7 @@ private: void Cancel(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_NIM, "called"); - finished_event.GetWritableEvent()->Clear(); + finished_event.GetWritableEvent().Clear(); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(RESULT_SUCCESS); } diff --git a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp index e99da24ab..775e76330 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp @@ -108,14 +108,14 @@ NvResult nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector // This is mostly to take into account unimplemented features. As synced // gpu is always synced. if (!gpu.IsAsync()) { - event.event->GetWritableEvent()->Signal(); + event.event->GetWritableEvent().Signal(); return NvResult::Success; } auto lock = gpu.LockSync(); const u32 current_syncpoint_value = event.fence.value; const s32 diff = current_syncpoint_value - params.threshold; if (diff >= 0) { - event.event->GetWritableEvent()->Signal(); + event.event->GetWritableEvent().Signal(); params.value = current_syncpoint_value; std::memcpy(output.data(), ¶ms, sizeof(params)); return NvResult::Success; @@ -142,7 +142,7 @@ NvResult nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector params.value = ((params.syncpt_id & 0xfff) << 16) | 0x10000000; } params.value |= event_id; - event.event->GetWritableEvent()->Clear(); + event.event->GetWritableEvent().Clear(); gpu.RegisterSyncptInterrupt(params.syncpt_id, target_value); std::memcpy(output.data(), ¶ms, sizeof(params)); return NvResult::Timeout; diff --git a/src/core/hle/service/nvdrv/interface.cpp b/src/core/hle/service/nvdrv/interface.cpp index 8e359040f..dc9b9341f 100644 --- a/src/core/hle/service/nvdrv/interface.cpp +++ b/src/core/hle/service/nvdrv/interface.cpp @@ -187,9 +187,9 @@ void NVDRV::QueryEvent(Kernel::HLERequestContext& ctx) { if (event_id < MaxNvEvents) { IPC::ResponseBuilder rb{ctx, 3, 1}; rb.Push(RESULT_SUCCESS); - auto event = nvdrv->GetEvent(event_id); - event->Clear(); - rb.PushCopyObjects(event.get()); + auto& event = nvdrv->GetEvent(event_id); + event.Clear(); + rb.PushCopyObjects(event); rb.PushEnum(NvResult::Success); } else { IPC::ResponseBuilder rb{ctx, 3}; diff --git a/src/core/hle/service/nvdrv/nvdrv.cpp b/src/core/hle/service/nvdrv/nvdrv.cpp index 2fbf61cd6..74796dce1 100644 --- a/src/core/hle/service/nvdrv/nvdrv.cpp +++ b/src/core/hle/service/nvdrv/nvdrv.cpp @@ -176,16 +176,16 @@ void Module::SignalSyncpt(const u32 syncpoint_id, const u32 value) { if (events_interface.assigned_syncpt[i] == syncpoint_id && events_interface.assigned_value[i] == value) { events_interface.LiberateEvent(i); - events_interface.events[i].event->GetWritableEvent()->Signal(); + events_interface.events[i].event->GetWritableEvent().Signal(); } } } -std::shared_ptr Module::GetEvent(const u32 event_id) const { - return SharedFrom(events_interface.events[event_id].event->GetReadableEvent()); +Kernel::KReadableEvent& Module::GetEvent(const u32 event_id) { + return events_interface.events[event_id].event->GetReadableEvent(); } -std::shared_ptr Module::GetEventWriteable(const u32 event_id) const { +Kernel::KWritableEvent& Module::GetEventWriteable(const u32 event_id) { return events_interface.events[event_id].event->GetWritableEvent(); } diff --git a/src/core/hle/service/nvdrv/nvdrv.h b/src/core/hle/service/nvdrv/nvdrv.h index ea5dbbdf9..a43ceb7ae 100644 --- a/src/core/hle/service/nvdrv/nvdrv.h +++ b/src/core/hle/service/nvdrv/nvdrv.h @@ -136,9 +136,9 @@ public: void SignalSyncpt(const u32 syncpoint_id, const u32 value); - std::shared_ptr GetEvent(u32 event_id) const; + Kernel::KReadableEvent& GetEvent(u32 event_id); - std::shared_ptr GetEventWriteable(u32 event_id) const; + Kernel::KWritableEvent& GetEventWriteable(u32 event_id); private: /// Manages syncpoints on the host diff --git a/src/core/hle/service/nvflinger/buffer_queue.cpp b/src/core/hle/service/nvflinger/buffer_queue.cpp index 95e5a5e7f..0b6e7430b 100644 --- a/src/core/hle/service/nvflinger/buffer_queue.cpp +++ b/src/core/hle/service/nvflinger/buffer_queue.cpp @@ -41,7 +41,7 @@ void BufferQueue::SetPreallocatedBuffer(u32 slot, const IGBPBuffer& igbp_buffer) .multi_fence = {}, }; - buffer_wait_event.GetWritableEvent()->Signal(); + buffer_wait_event.GetWritableEvent().Signal(); } std::optional> BufferQueue::DequeueBuffer(u32 width, @@ -119,7 +119,7 @@ void BufferQueue::CancelBuffer(u32 slot, const Service::Nvidia::MultiFence& mult } free_buffers_condition.notify_one(); - buffer_wait_event.GetWritableEvent()->Signal(); + buffer_wait_event.GetWritableEvent().Signal(); } std::optional> BufferQueue::AcquireBuffer() { @@ -154,7 +154,7 @@ void BufferQueue::ReleaseBuffer(u32 slot) { } free_buffers_condition.notify_one(); - buffer_wait_event.GetWritableEvent()->Signal(); + buffer_wait_event.GetWritableEvent().Signal(); } void BufferQueue::Connect() { @@ -169,7 +169,7 @@ void BufferQueue::Disconnect() { std::unique_lock lock{queue_sequence_mutex}; queue_sequence.clear(); } - buffer_wait_event.GetWritableEvent()->Signal(); + buffer_wait_event.GetWritableEvent().Signal(); is_connect = false; free_buffers_condition.notify_one(); } @@ -188,11 +188,11 @@ u32 BufferQueue::Query(QueryType type) { return 0; } -std::shared_ptr BufferQueue::GetWritableBufferWaitEvent() const { +Kernel::KWritableEvent& BufferQueue::GetWritableBufferWaitEvent() { return buffer_wait_event.GetWritableEvent(); } -std::shared_ptr BufferQueue::GetBufferWaitEvent() const { +Kernel::KReadableEvent& BufferQueue::GetBufferWaitEvent() { return buffer_wait_event.GetReadableEvent(); } diff --git a/src/core/hle/service/nvflinger/buffer_queue.h b/src/core/hle/service/nvflinger/buffer_queue.h index 0d2010ad5..044e51d08 100644 --- a/src/core/hle/service/nvflinger/buffer_queue.h +++ b/src/core/hle/service/nvflinger/buffer_queue.h @@ -117,9 +117,9 @@ public: return is_connect; } - std::shared_ptr GetWritableBufferWaitEvent() const; + Kernel::KWritableEvent& GetWritableBufferWaitEvent(); - std::shared_ptr GetBufferWaitEvent() const; + Kernel::KReadableEvent& GetBufferWaitEvent(); private: BufferQueue(const BufferQueue&) = delete; diff --git a/src/core/hle/service/nvflinger/nvflinger.cpp b/src/core/hle/service/nvflinger/nvflinger.cpp index c90e4d083..7fb9133c7 100644 --- a/src/core/hle/service/nvflinger/nvflinger.cpp +++ b/src/core/hle/service/nvflinger/nvflinger.cpp @@ -165,15 +165,15 @@ std::optional NVFlinger::FindBufferQueueId(u64 display_id, u64 layer_id) co return layer->GetBufferQueue().GetId(); } -std::shared_ptr NVFlinger::FindVsyncEvent(u64 display_id) { - const auto lock_guard = Lock(); +Kernel::KReadableEvent* NVFlinger::FindVsyncEvent(u64 display_id) { + const auto lock_guard = Lock(); auto* const display = FindDisplay(display_id); if (display == nullptr) { return nullptr; } - return display->GetVSyncEvent(); + return &display->GetVSyncEvent(); } BufferQueue* NVFlinger::FindBufferQueue(u32 id) { diff --git a/src/core/hle/service/nvflinger/nvflinger.h b/src/core/hle/service/nvflinger/nvflinger.h index d51b905c1..1c0aa8ec4 100644 --- a/src/core/hle/service/nvflinger/nvflinger.h +++ b/src/core/hle/service/nvflinger/nvflinger.h @@ -73,7 +73,7 @@ public: /// Gets the vsync event for the specified display. /// /// If an invalid display ID is provided, then nullptr is returned. - [[nodiscard]] std::shared_ptr FindVsyncEvent(u64 display_id); + [[nodiscard]] Kernel::KReadableEvent* FindVsyncEvent(u64 display_id); /// Obtains a buffer queue identified by the ID. [[nodiscard]] BufferQueue* FindBufferQueue(u32 id); diff --git a/src/core/hle/service/ptm/psm.cpp b/src/core/hle/service/ptm/psm.cpp index 2659ab7d5..bb7af9217 100644 --- a/src/core/hle/service/ptm/psm.cpp +++ b/src/core/hle/service/ptm/psm.cpp @@ -41,19 +41,19 @@ public: void SignalChargerTypeChanged() { if (should_signal && should_signal_charger_type) { - state_change_event.GetWritableEvent()->Signal(); + state_change_event.GetWritableEvent().Signal(); } } void SignalPowerSupplyChanged() { if (should_signal && should_signal_power_supply) { - state_change_event.GetWritableEvent()->Signal(); + state_change_event.GetWritableEvent().Signal(); } } void SignalBatteryVoltageStateChanged() { if (should_signal && should_signal_battery_voltage) { - state_change_event.GetWritableEvent()->Signal(); + state_change_event.GetWritableEvent().Signal(); } } diff --git a/src/core/hle/service/vi/display/vi_display.cpp b/src/core/hle/service/vi/display/vi_display.cpp index 7c52b6aa7..becbd36c1 100644 --- a/src/core/hle/service/vi/display/vi_display.cpp +++ b/src/core/hle/service/vi/display/vi_display.cpp @@ -33,12 +33,12 @@ const Layer& Display::GetLayer(std::size_t index) const { return *layers.at(index); } -std::shared_ptr Display::GetVSyncEvent() const { +Kernel::KReadableEvent& Display::GetVSyncEvent() { return vsync_event.GetReadableEvent(); } void Display::SignalVSyncEvent() { - vsync_event.GetWritableEvent()->Signal(); + vsync_event.GetWritableEvent().Signal(); } void Display::CreateLayer(u64 layer_id, NVFlinger::BufferQueue& buffer_queue) { diff --git a/src/core/hle/service/vi/display/vi_display.h b/src/core/hle/service/vi/display/vi_display.h index e990d6809..a09d982bc 100644 --- a/src/core/hle/service/vi/display/vi_display.h +++ b/src/core/hle/service/vi/display/vi_display.h @@ -59,7 +59,7 @@ public: const Layer& GetLayer(std::size_t index) const; /// Gets the readable vsync event. - std::shared_ptr GetVSyncEvent() const; + Kernel::KReadableEvent& GetVSyncEvent(); /// Signals the internal vsync event. void SignalVSyncEvent(); diff --git a/src/core/hle/service/vi/vi.cpp b/src/core/hle/service/vi/vi.cpp index 6e507bfb8..32e47a43e 100644 --- a/src/core/hle/service/vi/vi.cpp +++ b/src/core/hle/service/vi/vi.cpp @@ -669,12 +669,10 @@ private: LOG_WARNING(Service_VI, "(STUBBED) called id={}, unknown={:08X}", id, unknown); - const auto& buffer_queue = *nv_flinger.FindBufferQueue(id); - // TODO(Subv): Find out what this actually is. IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(buffer_queue.GetBufferWaitEvent().get()); + rb.PushCopyObjects(nv_flinger.FindBufferQueue(id)->GetBufferWaitEvent()); } NVFlinger::NVFlinger& nv_flinger; @@ -1209,7 +1207,7 @@ private: IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(vsync_event.get()); + rb.PushCopyObjects(vsync_event); } void ConvertScalingMode(Kernel::HLERequestContext& ctx) { From c2f6f2ba7a6595abfd6e0e829a556b7424516a2d Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 11 Apr 2021 10:51:09 -0700 Subject: [PATCH 29/82] hle: kernel: KThread: Add missing resource hint release. --- src/core/hle/kernel/k_thread.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp index fbab5ecb7..f798dfd59 100644 --- a/src/core/hle/kernel/k_thread.cpp +++ b/src/core/hle/kernel/k_thread.cpp @@ -981,7 +981,7 @@ void KThread::Exit() { // Release the thread resource hint from parent. if (parent != nullptr) { - // TODO(bunnei): Hint that the resource is about to be released. + parent->GetResourceLimit()->Release(Kernel::LimitableResource::Threads, 0, 1); resource_limit_release_hint = true; } From 269d233a9421e43c2383fe29603b3dfbdaa900e9 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 11 Apr 2021 11:41:48 -0700 Subject: [PATCH 30/82] hle: kernel: svc_results: Update naming.. --- src/core/hle/kernel/client_port.cpp | 2 +- src/core/hle/kernel/client_session.cpp | 2 +- src/core/hle/kernel/handle_table.cpp | 4 +-- src/core/hle/kernel/k_page_table.cpp | 6 ++-- src/core/hle/kernel/process.cpp | 2 +- src/core/hle/kernel/process_capability.cpp | 14 ++++---- src/core/hle/kernel/svc.cpp | 38 +++++++++++----------- src/core/hle/kernel/svc_results.h | 17 +++++----- 8 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src/core/hle/kernel/client_port.cpp b/src/core/hle/kernel/client_port.cpp index 431a90d82..d856b83e3 100644 --- a/src/core/hle/kernel/client_port.cpp +++ b/src/core/hle/kernel/client_port.cpp @@ -21,7 +21,7 @@ std::shared_ptr ClientPort::GetServerPort() const { ResultVal> ClientPort::Connect() { if (active_sessions >= max_sessions) { - return ResultMaxConnectionsReached; + return ResultOutOfSessions; } active_sessions++; diff --git a/src/core/hle/kernel/client_session.cpp b/src/core/hle/kernel/client_session.cpp index 13c55abe4..fa9cad498 100644 --- a/src/core/hle/kernel/client_session.cpp +++ b/src/core/hle/kernel/client_session.cpp @@ -42,7 +42,7 @@ ResultCode ClientSession::SendSyncRequest(KThread* thread, Core::Memory::Memory& Core::Timing::CoreTiming& core_timing) { // Keep ServerSession alive until we're done working with it. if (!parent->Server()) { - return ResultSessionClosedByRemote; + return ResultSessionClosed; } // Signal the server session that new data is available diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index cc3210ef2..adfcd3c5b 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -68,7 +68,7 @@ ResultVal HandleTable::Create(Object* obj) { const u16 slot = next_free_slot; if (slot >= table_size) { LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use."); - return ResultHandleTableFull; + return ResultOutOfHandles; } next_free_slot = generations[slot]; @@ -93,7 +93,7 @@ ResultCode HandleTable::Add(Handle* out_handle, KAutoObject* obj, u16 type) { const u16 slot = next_free_slot; if (slot >= table_size) { LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use."); - return ResultHandleTableFull; + return ResultOutOfHandles; } next_free_slot = generations[slot]; diff --git a/src/core/hle/kernel/k_page_table.cpp b/src/core/hle/kernel/k_page_table.cpp index d09d5ce48..5f60b95cd 100644 --- a/src/core/hle/kernel/k_page_table.cpp +++ b/src/core/hle/kernel/k_page_table.cpp @@ -420,7 +420,7 @@ ResultCode KPageTable::MapPhysicalMemory(VAddr addr, std::size_t size) { remaining_size); if (!memory_reservation.Succeeded()) { LOG_ERROR(Kernel, "Could not reserve remaining {:X} bytes", remaining_size); - return ResultResourceLimitedExceeded; + return ResultLimitReached; } KPageLinkedList page_linked_list; @@ -578,7 +578,7 @@ ResultCode KPageTable::Unmap(VAddr dst_addr, VAddr src_addr, std::size_t size) { AddRegionToPages(dst_addr, num_pages, dst_pages); if (!dst_pages.IsEqual(src_pages)) { - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } { @@ -790,7 +790,7 @@ ResultVal KPageTable::SetHeapSize(std::size_t size) { if (!memory_reservation.Succeeded()) { LOG_ERROR(Kernel, "Could not reserve heap extension of size {:X} bytes", delta); - return ResultResourceLimitedExceeded; + return ResultLimitReached; } KPageLinkedList page_linked_list; diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 8088c634f..678037923 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -276,7 +276,7 @@ ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, if (!memory_reservation.Succeeded()) { LOG_ERROR(Kernel, "Could not reserve process memory requirements of size {:X} bytes", code_size + system_resource_size); - return ResultResourceLimitedExceeded; + return ResultLimitReached; } // Initialize proces address space if (const ResultCode result{ diff --git a/src/core/hle/kernel/process_capability.cpp b/src/core/hle/kernel/process_capability.cpp index 1006ee50c..4ccac0b06 100644 --- a/src/core/hle/kernel/process_capability.cpp +++ b/src/core/hle/kernel/process_capability.cpp @@ -159,7 +159,7 @@ ResultCode ProcessCapabilities::ParseSingleFlagCapability(u32& set_flags, u32& s const auto type = GetCapabilityType(flag); if (type == CapabilityType::Unset) { - return ResultInvalidCapabilityDescriptor; + return ResultInvalidArgument; } // Bail early on ignorable entries, as one would expect, @@ -202,7 +202,7 @@ ResultCode ProcessCapabilities::ParseSingleFlagCapability(u32& set_flags, u32& s } LOG_ERROR(Kernel, "Invalid capability type! type={}", type); - return ResultInvalidCapabilityDescriptor; + return ResultInvalidArgument; } void ProcessCapabilities::Clear() { @@ -225,7 +225,7 @@ ResultCode ProcessCapabilities::HandlePriorityCoreNumFlags(u32 flags) { if (priority_mask != 0 || core_mask != 0) { LOG_ERROR(Kernel, "Core or priority mask are not zero! priority_mask={}, core_mask={}", priority_mask, core_mask); - return ResultInvalidCapabilityDescriptor; + return ResultInvalidArgument; } const u32 core_num_min = (flags >> 16) & 0xFF; @@ -329,7 +329,7 @@ ResultCode ProcessCapabilities::HandleProgramTypeFlags(u32 flags) { const u32 reserved = flags >> 17; if (reserved != 0) { LOG_ERROR(Kernel, "Reserved value is non-zero! reserved={}", reserved); - return ResultReservedValue; + return ResultReservedUsed; } program_type = static_cast((flags >> 14) & 0b111); @@ -349,7 +349,7 @@ ResultCode ProcessCapabilities::HandleKernelVersionFlags(u32 flags) { LOG_ERROR(Kernel, "Kernel version is non zero or flags are too small! major_version={}, flags={}", major_version, flags); - return ResultInvalidCapabilityDescriptor; + return ResultInvalidArgument; } kernel_version = flags; @@ -360,7 +360,7 @@ ResultCode ProcessCapabilities::HandleHandleTableFlags(u32 flags) { const u32 reserved = flags >> 26; if (reserved != 0) { LOG_ERROR(Kernel, "Reserved value is non-zero! reserved={}", reserved); - return ResultReservedValue; + return ResultReservedUsed; } handle_table_size = static_cast((flags >> 16) & 0x3FF); @@ -371,7 +371,7 @@ ResultCode ProcessCapabilities::HandleDebugFlags(u32 flags) { const u32 reserved = flags >> 19; if (reserved != 0) { LOG_ERROR(Kernel, "Reserved value is non-zero! reserved={}", reserved); - return ResultReservedValue; + return ResultReservedUsed; } is_debuggable = (flags & 0x20000) != 0; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index c6334f91c..5ead3a270 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -113,7 +113,7 @@ ResultCode MapUnmapMemorySanityChecks(const KPageTable& manager, VAddr dst_addr, LOG_ERROR(Kernel_SVC, "Destination is not within the stack region, addr=0x{:016X}, size=0x{:016X}", dst_addr, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } if (manager.IsInsideHeapRegion(dst_addr, size)) { @@ -121,7 +121,7 @@ ResultCode MapUnmapMemorySanityChecks(const KPageTable& manager, VAddr dst_addr, "Destination does not fit within the heap region, addr=0x{:016X}, " "size=0x{:016X}", dst_addr, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } if (manager.IsInsideAliasRegion(dst_addr, size)) { @@ -129,7 +129,7 @@ ResultCode MapUnmapMemorySanityChecks(const KPageTable& manager, VAddr dst_addr, "Destination does not fit within the map region, addr=0x{:016X}, " "size=0x{:016X}", dst_addr, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } return RESULT_SUCCESS; @@ -943,7 +943,7 @@ static ResultCode MapPhysicalMemory(Core::System& system, VAddr addr, u64 size) if (!(addr < addr + size)) { LOG_ERROR(Kernel_SVC, "Size causes 64-bit overflow of address"); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } Process* const current_process{system.Kernel().CurrentProcess()}; @@ -958,14 +958,14 @@ static ResultCode MapPhysicalMemory(Core::System& system, VAddr addr, u64 size) LOG_ERROR(Kernel_SVC, "Address is not within the address space, addr=0x{:016X}, size=0x{:016X}", addr, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } if (page_table.IsOutsideAliasRegion(addr, size)) { LOG_ERROR(Kernel_SVC, "Address is not within the alias region, addr=0x{:016X}, size=0x{:016X}", addr, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } return page_table.MapPhysicalMemory(addr, size); @@ -997,7 +997,7 @@ static ResultCode UnmapPhysicalMemory(Core::System& system, VAddr addr, u64 size if (!(addr < addr + size)) { LOG_ERROR(Kernel_SVC, "Size causes 64-bit overflow of address"); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } Process* const current_process{system.Kernel().CurrentProcess()}; @@ -1012,14 +1012,14 @@ static ResultCode UnmapPhysicalMemory(Core::System& system, VAddr addr, u64 size LOG_ERROR(Kernel_SVC, "Address is not within the address space, addr=0x{:016X}, size=0x{:016X}", addr, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } if (page_table.IsOutsideAliasRegion(addr, size)) { LOG_ERROR(Kernel_SVC, "Address is not within the alias region, addr=0x{:016X}, size=0x{:016X}", addr, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } return page_table.UnmapPhysicalMemory(addr, size); @@ -1138,7 +1138,7 @@ static ResultCode MapSharedMemory(Core::System& system, Handle shared_memory_han if ((permission_type | MemoryPermission::Write) != MemoryPermission::ReadWrite) { LOG_ERROR(Kernel_SVC, "Expected Read or ReadWrite permission but got permissions=0x{:08X}", permissions); - return ResultInvalidMemoryPermissions; + return ResultInvalidNewMemoryPermission; } auto* const current_process{system.Kernel().CurrentProcess()}; @@ -1149,7 +1149,7 @@ static ResultCode MapSharedMemory(Core::System& system, Handle shared_memory_han "Addr does not fit within the valid region, addr=0x{:016X}, " "size=0x{:016X}", addr, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } if (page_table.IsInsideHeapRegion(addr, size)) { @@ -1157,7 +1157,7 @@ static ResultCode MapSharedMemory(Core::System& system, Handle shared_memory_han "Addr does not fit within the heap region, addr=0x{:016X}, " "size=0x{:016X}", addr, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } if (page_table.IsInsideAliasRegion(addr, size)) { @@ -1165,7 +1165,7 @@ static ResultCode MapSharedMemory(Core::System& system, Handle shared_memory_han "Address does not fit within the map region, addr=0x{:016X}, " "size=0x{:016X}", addr, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } auto shared_memory{ @@ -1290,7 +1290,7 @@ static ResultCode MapProcessCodeMemory(Core::System& system, Handle process_hand "Destination address range is not within the ASLR region (dst_address=0x{:016X}, " "size=0x{:016X}).", dst_address, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } return page_table.MapProcessCodeMemory(dst_address, src_address, size); @@ -1358,7 +1358,7 @@ static ResultCode UnmapProcessCodeMemory(Core::System& system, Handle process_ha "Destination address range is not within the ASLR region (dst_address=0x{:016X}, " "size=0x{:016X}).", dst_address, size); - return ResultInvalidMemoryRange; + return ResultInvalidMemoryRegion; } return page_table.UnmapProcessCodeMemory(dst_address, src_address, size); @@ -1427,7 +1427,7 @@ static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr e system.CoreTiming().GetGlobalTimeNs().count() + 100000000); if (!thread_reservation.Succeeded()) { LOG_ERROR(Kernel_SVC, "Could not reserve a new thread"); - return ResultResourceLimitedExceeded; + return ResultLimitReached; } // Create the thread. @@ -1795,7 +1795,7 @@ static ResultCode CreateTransferMemory(Core::System& system, Handle* handle, VAd if (perms > MemoryPermission::ReadWrite || perms == MemoryPermission::Write) { LOG_ERROR(Kernel_SVC, "Invalid memory permissions for transfer memory! (perms={:08X})", permissions); - return ResultInvalidMemoryPermissions; + return ResultInvalidNewMemoryPermission; } auto& kernel = system.Kernel(); @@ -1804,7 +1804,7 @@ static ResultCode CreateTransferMemory(Core::System& system, Handle* handle, VAd LimitableResource::TransferMemory); if (!trmem_reservation.Succeeded()) { LOG_ERROR(Kernel_SVC, "Could not reserve a new transfer memory"); - return ResultResourceLimitedExceeded; + return ResultLimitReached; } auto transfer_mem_handle = TransferMemory::Create(kernel, system.Memory(), addr, size, static_cast(perms)); @@ -1940,7 +1940,7 @@ static ResultCode CreateEvent(Core::System& system, Handle* out_write, Handle* o // Reserve a new event from the process resource limit KScopedResourceReservation event_reservation(kernel.CurrentProcess(), LimitableResource::Events); - R_UNLESS(event_reservation.Succeeded(), ResultResourceLimitedExceeded); + R_UNLESS(event_reservation.Succeeded(), ResultLimitReached); // Create a new event. KEvent* event = KEvent::Create(kernel); diff --git a/src/core/hle/kernel/svc_results.h b/src/core/hle/kernel/svc_results.h index a26d9f2c9..cd32acd99 100644 --- a/src/core/hle/kernel/svc_results.h +++ b/src/core/hle/kernel/svc_results.h @@ -10,18 +10,18 @@ namespace Kernel { // Confirmed Switch kernel error codes -constexpr ResultCode ResultMaxConnectionsReached{ErrorModule::Kernel, 7}; -constexpr ResultCode ResultInvalidCapabilityDescriptor{ErrorModule::Kernel, 14}; +constexpr ResultCode ResultOutOfSessions{ErrorModule::Kernel, 7}; +constexpr ResultCode ResultInvalidArgument{ErrorModule::Kernel, 14}; constexpr ResultCode ResultNoSynchronizationObject{ErrorModule::Kernel, 57}; constexpr ResultCode ResultTerminationRequested{ErrorModule::Kernel, 59}; constexpr ResultCode ResultInvalidSize{ErrorModule::Kernel, 101}; constexpr ResultCode ResultInvalidAddress{ErrorModule::Kernel, 102}; constexpr ResultCode ResultOutOfResource{ErrorModule::Kernel, 103}; constexpr ResultCode ResultOutOfMemory{ErrorModule::Kernel, 104}; -constexpr ResultCode ResultHandleTableFull{ErrorModule::Kernel, 105}; +constexpr ResultCode ResultOutOfHandles{ErrorModule::Kernel, 105}; constexpr ResultCode ResultInvalidCurrentMemory{ErrorModule::Kernel, 106}; -constexpr ResultCode ResultInvalidMemoryPermissions{ErrorModule::Kernel, 108}; -constexpr ResultCode ResultInvalidMemoryRange{ErrorModule::Kernel, 110}; +constexpr ResultCode ResultInvalidNewMemoryPermission{ErrorModule::Kernel, 108}; +constexpr ResultCode ResultInvalidMemoryRegion{ErrorModule::Kernel, 110}; constexpr ResultCode ResultInvalidPriority{ErrorModule::Kernel, 112}; constexpr ResultCode ResultInvalidCoreId{ErrorModule::Kernel, 113}; constexpr ResultCode ResultInvalidHandle{ErrorModule::Kernel, 114}; @@ -33,9 +33,10 @@ constexpr ResultCode ResultOutOfRange{ErrorModule::Kernel, 119}; constexpr ResultCode ResultInvalidEnumValue{ErrorModule::Kernel, 120}; constexpr ResultCode ResultNotFound{ErrorModule::Kernel, 121}; constexpr ResultCode ResultBusy{ErrorModule::Kernel, 122}; -constexpr ResultCode ResultSessionClosedByRemote{ErrorModule::Kernel, 123}; +constexpr ResultCode ResultSessionClosed{ErrorModule::Kernel, 123}; constexpr ResultCode ResultInvalidState{ErrorModule::Kernel, 125}; -constexpr ResultCode ResultReservedValue{ErrorModule::Kernel, 126}; -constexpr ResultCode ResultResourceLimitedExceeded{ErrorModule::Kernel, 132}; +constexpr ResultCode ResultReservedUsed{ErrorModule::Kernel, 126}; +constexpr ResultCode ResultLimitReached{ErrorModule::Kernel, 132}; +constexpr ResultCode ResultInvalidId{ErrorModule::Kernel, 519}; } // namespace Kernel From 84bb772003c921518a2ca920f4c4df2c9206844b Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 11 Apr 2021 11:42:22 -0700 Subject: [PATCH 31/82] hle: kernel: KThread: Remove incorrect resource release. --- src/core/hle/kernel/k_thread.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp index f798dfd59..5cc0a0064 100644 --- a/src/core/hle/kernel/k_thread.cpp +++ b/src/core/hle/kernel/k_thread.cpp @@ -255,7 +255,7 @@ void KThread::PostDestroy(uintptr_t arg) { const bool resource_limit_release_hint = (arg & 1); const s64 hint_value = (resource_limit_release_hint ? 0 : 1); if (owner != nullptr) { - owner->GetResourceLimit()->Release(Kernel::LimitableResource::Threads, 1, hint_value); + owner->GetResourceLimit()->Release(LimitableResource::Threads, 1, hint_value); owner->Close(); } } @@ -289,7 +289,6 @@ void KThread::Finalize() { // Decrement the parent process's thread count. if (parent != nullptr) { parent->DecrementThreadCount(); - parent->GetResourceLimit()->Release(LimitableResource::Threads, 1); } // Perform inherited finalization. From 76a0814142b0f46374f1311515c1c870ac5f18e0 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 11 Apr 2021 12:49:18 -0700 Subject: [PATCH 32/82] hle: kernel: svc: Migrate GetProcessId, CancelSynchronization, SetThreadActivity. --- src/core/hle/kernel/svc.cpp | 80 +++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 13 deletions(-) diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 5ead3a270..fae4c7209 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -390,17 +390,43 @@ static ResultCode GetThreadId32(Core::System& system, u32* out_thread_id_low, } /// Gets the ID of the specified process or a specified thread's owning process. -static ResultCode GetProcessId(Core::System& system, u64* process_id, Handle handle) { - __debugbreak(); +static ResultCode GetProcessId(Core::System& system, u64* out_process_id, Handle handle) { + LOG_DEBUG(Kernel_SVC, "called handle=0x{:08X}", handle); + + // Get the object from the handle table. + KScopedAutoObject obj = + system.Kernel().CurrentProcess()->GetHandleTable().GetObject( + static_cast(handle)); + R_UNLESS(obj.IsNotNull(), ResultInvalidHandle); + + // Get the process from the object. + Process* process = nullptr; + if (Process* p = obj->DynamicCast(); p != nullptr) { + // The object is a process, so we can use it directly. + process = p; + } else if (KThread* t = obj->DynamicCast(); t != nullptr) { + // The object is a thread, so we want to use its parent. + process = reinterpret_cast(obj.GetPointerUnsafe())->GetOwnerProcess(); + } else { + // TODO(bunnei): This should also handle debug objects before returning. + UNIMPLEMENTED_MSG("Debug objects not implemented"); + } + + // Make sure the target process exists. + R_UNLESS(process != nullptr, ResultInvalidHandle); + + // Get the process id. + *out_process_id = process->GetId(); + return ResultInvalidHandle; } -static ResultCode GetProcessId32(Core::System& system, u32* process_id_low, u32* process_id_high, - Handle handle) { - u64 process_id{}; - const auto result = GetProcessId(system, &process_id, handle); - *process_id_low = static_cast(process_id); - *process_id_high = static_cast(process_id >> 32); +static ResultCode GetProcessId32(Core::System& system, u32* out_process_id_low, + u32* out_process_id_high, Handle handle) { + u64 out_process_id{}; + const auto result = GetProcessId(system, &out_process_id, handle); + *out_process_id_low = static_cast(out_process_id); + *out_process_id_high = static_cast(out_process_id >> 32); return result; } @@ -468,13 +494,21 @@ static ResultCode WaitSynchronization32(Core::System& system, u32 timeout_low, u } /// Resumes a thread waiting on WaitSynchronization -static ResultCode CancelSynchronization(Core::System& system, Handle thread_handle) { - __debugbreak(); +static ResultCode CancelSynchronization(Core::System& system, Handle handle) { + LOG_TRACE(Kernel_SVC, "called handle=0x{:X}", handle); + + // Get the thread from its handle. + KScopedAutoObject thread = + system.Kernel().CurrentProcess()->GetHandleTable().GetObject( + static_cast(handle)); + + // Cancel the thread's wait. + thread->WaitCancel(); return RESULT_SUCCESS; } -static ResultCode CancelSynchronization32(Core::System& system, Handle thread_handle) { - return CancelSynchronization(system, thread_handle); +static ResultCode CancelSynchronization32(Core::System& system, Handle handle) { + return CancelSynchronization(system, handle); } /// Attempts to locks a mutex @@ -1032,7 +1066,27 @@ static ResultCode UnmapPhysicalMemory32(Core::System& system, u32 addr, u32 size /// Sets the thread activity static ResultCode SetThreadActivity(Core::System& system, Handle thread_handle, ThreadActivity thread_activity) { - __debugbreak(); + LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, activity=0x{:08X}", thread_handle, + thread_activity); + + // Validate the activity. + constexpr auto IsValidThreadActivity = [](ThreadActivity activity) { + return activity == ThreadActivity::Runnable || activity == ThreadActivity::Paused; + }; + R_UNLESS(IsValidThreadActivity(thread_activity), ResultInvalidEnumValue); + + // Get the thread from its handle. + KScopedAutoObject thread = + system.Kernel().CurrentProcess()->GetHandleTable().GetObject(thread_handle); + R_UNLESS(thread.IsNotNull(), ResultInvalidHandle); + + // Check that the activity is being set on a non-current thread for the current process. + R_UNLESS(thread->GetOwnerProcess() == system.Kernel().CurrentProcess(), ResultInvalidHandle); + R_UNLESS(thread.GetPointerUnsafe() != GetCurrentThreadPointer(system.Kernel()), ResultBusy); + + // Set the activity. + R_TRY(thread->SetActivity(thread_activity)); + return RESULT_SUCCESS; } From 2cb6106523a87ae78d3a6b92c34b3b1d2817415e Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 11 Apr 2021 12:49:51 -0700 Subject: [PATCH 33/82] hle: kernel: svc: Migrate GetThreadContext, GetThreadCoreMask. --- src/core/hle/kernel/svc.cpp | 61 +++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index fae4c7209..b83ee3e69 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -1100,7 +1100,55 @@ static ResultCode GetThreadContext(Core::System& system, VAddr out_context, Hand LOG_DEBUG(Kernel_SVC, "called, out_context=0x{:08X}, thread_handle=0x{:X}", out_context, thread_handle); - __debugbreak(); + auto& kernel = system.Kernel(); + + // Get the thread from its handle. + KScopedAutoObject thread = + kernel.CurrentProcess()->GetHandleTable().GetObject(thread_handle); + R_UNLESS(thread.IsNotNull(), ResultInvalidHandle); + + // Require the handle be to a non-current thread in the current process. + const auto* current_process = kernel.CurrentProcess(); + R_UNLESS(current_process == thread->GetOwnerProcess(), ResultInvalidId); + + // Verify that the thread isn't terminated. + R_UNLESS(thread->GetState() != ThreadState::Terminated, ResultTerminationRequested); + + /// Check that the thread is not the current one. + /// NOTE: Nintendo does not check this, and thus the following loop will deadlock. + R_UNLESS(thread.GetPointerUnsafe() != GetCurrentThreadPointer(kernel), ResultInvalidId); + + // Try to get the thread context until the thread isn't current on any core. + while (true) { + KScopedSchedulerLock sl{kernel}; + + // TODO(bunnei): Enforce that thread is suspended for debug here. + + // If the thread's raw state isn't runnable, check if it's current on some core. + if (thread->GetRawState() != ThreadState::Runnable) { + bool current = false; + for (auto i = 0; i < static_cast(Core::Hardware::NUM_CPU_CORES); ++i) { + if (thread.GetPointerUnsafe() == kernel.Scheduler(i).GetCurrentThread()) { + current = true; + } + break; + } + + // If the thread is current, retry until it isn't. + if (current) { + continue; + } + } + + // Get the thread context. + std::vector context; + R_TRY(thread->GetThreadContext3(context)); + + // Copy the thread context to user space. + system.Memory().WriteBlock(out_context, context.data(), context.size()); + + return RESULT_SUCCESS; + } return RESULT_SUCCESS; } @@ -1885,7 +1933,16 @@ static ResultCode CreateTransferMemory32(Core::System& system, Handle* handle, u static ResultCode GetThreadCoreMask(Core::System& system, Handle thread_handle, s32* out_core_id, u64* out_affinity_mask) { - __debugbreak(); + LOG_TRACE(Kernel_SVC, "called, handle=0x{:08X}", thread_handle); + + // Get the thread from its handle. + KScopedAutoObject thread = + system.Kernel().CurrentProcess()->GetHandleTable().GetObject(thread_handle); + R_UNLESS(thread.IsNotNull(), ResultInvalidHandle); + + // Get the core mask. + R_TRY(thread->GetCoreMask(out_core_id, out_affinity_mask)); + return RESULT_SUCCESS; } From 7444963bbb300cff269e410948de7fa577f5ff16 Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 13 Apr 2021 17:48:37 -0700 Subject: [PATCH 34/82] hle: kernel: Migrate KSession, KClientSession, and KServerSession to KAutoObject. --- src/core/CMakeLists.txt | 12 +- src/core/hle/ipc_helpers.h | 12 +- src/core/hle/kernel/client_port.cpp | 14 +-- src/core/hle/kernel/client_port.h | 4 +- src/core/hle/kernel/client_session.cpp | 52 --------- src/core/hle/kernel/handle_table.cpp | 5 +- src/core/hle/kernel/hle_ipc.cpp | 23 ++-- src/core/hle/kernel/hle_ipc.h | 18 ++- src/core/hle/kernel/init/init_slab_setup.cpp | 4 +- src/core/hle/kernel/k_client_session.cpp | 31 +++++ .../{client_session.h => k_client_session.h} | 60 ++++++---- ...erver_session.cpp => k_server_session.cpp} | 65 ++++++----- .../{server_session.h => k_server_session.h} | 73 +++++------- src/core/hle/kernel/k_session.cpp | 67 +++++++++++ src/core/hle/kernel/k_session.h | 108 ++++++++++++++++++ src/core/hle/kernel/kernel.h | 8 ++ src/core/hle/kernel/server_port.cpp | 10 +- src/core/hle/kernel/server_port.h | 8 +- src/core/hle/kernel/service_thread.cpp | 29 +++-- src/core/hle/kernel/service_thread.h | 4 +- src/core/hle/kernel/session.cpp | 41 ------- src/core/hle/kernel/session.h | 64 ----------- src/core/hle/kernel/svc.cpp | 20 ++-- src/core/hle/service/am/applets/applets.cpp | 1 - src/core/hle/service/hid/hid.cpp | 1 - src/core/hle/service/mm/mm_u.cpp | 1 - src/core/hle/service/service.h | 5 +- src/core/hle/service/sm/controller.cpp | 8 +- src/core/hle/service/sm/sm.cpp | 22 ++-- src/core/hle/service/sm/sm.h | 5 +- src/core/hle/service/time/time.cpp | 1 - 31 files changed, 416 insertions(+), 360 deletions(-) delete mode 100644 src/core/hle/kernel/client_session.cpp create mode 100644 src/core/hle/kernel/k_client_session.cpp rename src/core/hle/kernel/{client_session.h => k_client_session.h} (56%) rename src/core/hle/kernel/{server_session.cpp => k_server_session.cpp} (70%) rename src/core/hle/kernel/{server_session.h => k_server_session.h} (70%) create mode 100644 src/core/hle/kernel/k_session.cpp create mode 100644 src/core/hle/kernel/k_session.h delete mode 100644 src/core/hle/kernel/session.cpp delete mode 100644 src/core/hle/kernel/session.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 569b4802e..e30a30823 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -146,8 +146,6 @@ add_library(core STATIC hle/kernel/board/nintendo/nx/secure_monitor.h hle/kernel/client_port.cpp hle/kernel/client_port.h - hle/kernel/client_session.cpp - hle/kernel/client_session.h hle/kernel/code_set.cpp hle/kernel/code_set.h hle/kernel/svc_results.h @@ -170,6 +168,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_session.cpp + hle/kernel/k_client_session.h hle/kernel/k_condition_variable.cpp hle/kernel/k_condition_variable.h hle/kernel/k_event.cpp @@ -205,6 +205,10 @@ add_library(core STATIC hle/kernel/k_scoped_lock.h hle/kernel/k_scoped_resource_reservation.h hle/kernel/k_scoped_scheduler_lock_and_sleep.h + hle/kernel/k_server_session.cpp + hle/kernel/k_server_session.h + hle/kernel/k_session.cpp + hle/kernel/k_session.h hle/kernel/k_shared_memory.cpp hle/kernel/k_shared_memory.h hle/kernel/k_slab_heap.h @@ -233,12 +237,8 @@ add_library(core STATIC hle/kernel/process_capability.h hle/kernel/server_port.cpp hle/kernel/server_port.h - hle/kernel/server_session.cpp - hle/kernel/server_session.h hle/kernel/service_thread.cpp hle/kernel/service_thread.h - hle/kernel/session.cpp - hle/kernel/session.h hle/kernel/slab_helpers.h hle/kernel/svc.cpp hle/kernel/svc.h diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index 8f12616bd..99b7d3d82 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -14,11 +14,9 @@ #include "common/common_types.h" #include "core/hle/ipc.h" #include "core/hle/kernel/client_port.h" -#include "core/hle/kernel/client_session.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_session.h" -#include "core/hle/kernel/session.h" #include "core/hle/result.h" namespace IPC { @@ -137,9 +135,11 @@ public: if (context->Session()->IsDomain()) { context->AddDomainObject(std::move(iface)); } else { - auto [client, server] = Kernel::Session::Create(kernel, iface->GetServiceName()); - context->AddMoveObject(client.get()); - iface->ClientConnected(std::move(client), std::move(server)); + auto* session = Kernel::KSession::Create(kernel); + session->Initialize(iface->GetServiceName()); + + context->AddMoveObject(&session->GetClientSession()); + iface->ClientConnected(session); } } diff --git a/src/core/hle/kernel/client_port.cpp b/src/core/hle/kernel/client_port.cpp index d856b83e3..ce88da1c3 100644 --- a/src/core/hle/kernel/client_port.cpp +++ b/src/core/hle/kernel/client_port.cpp @@ -3,11 +3,10 @@ // Refer to the license.txt file included. #include "core/hle/kernel/client_port.h" -#include "core/hle/kernel/client_session.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/session.h" #include "core/hle/kernel/svc_results.h" namespace Kernel { @@ -19,21 +18,22 @@ std::shared_ptr ClientPort::GetServerPort() const { return server_port; } -ResultVal> ClientPort::Connect() { +ResultVal ClientPort::Connect() { if (active_sessions >= max_sessions) { return ResultOutOfSessions; } active_sessions++; - auto [client, server] = Kernel::Session::Create(kernel, name); + auto* session = Kernel::KSession::Create(kernel); + session->Initialize(name + ":ClientPort"); if (server_port->HasHLEHandler()) { - server_port->GetHLEHandler()->ClientConnected(client, std::move(server)); + server_port->GetHLEHandler()->ClientConnected(session); } else { - server_port->AppendPendingSession(std::move(server)); + server_port->AppendPendingSession(std::addressof(session->GetServerSession())); } - return MakeResult(std::move(client)); + return MakeResult(std::addressof(session->GetClientSession())); } void ClientPort::ConnectionClosed() { diff --git a/src/core/hle/kernel/client_port.h b/src/core/hle/kernel/client_port.h index 77559ebf9..0b20fef40 100644 --- a/src/core/hle/kernel/client_port.h +++ b/src/core/hle/kernel/client_port.h @@ -13,7 +13,7 @@ namespace Kernel { -class ClientSession; +class KClientSession; class KernelCore; class ServerPort; @@ -43,7 +43,7 @@ public: * waiting on it to awake. * @returns ClientSession The client endpoint of the created Session pair, or error code. */ - ResultVal> Connect(); + ResultVal Connect(); /** * Signifies that a previously active connection has been closed, diff --git a/src/core/hle/kernel/client_session.cpp b/src/core/hle/kernel/client_session.cpp deleted file mode 100644 index fa9cad498..000000000 --- a/src/core/hle/kernel/client_session.cpp +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2019 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include "core/hle/kernel/client_session.h" -#include "core/hle/kernel/hle_ipc.h" -#include "core/hle/kernel/k_thread.h" -#include "core/hle/kernel/server_session.h" -#include "core/hle/kernel/session.h" -#include "core/hle/kernel/svc_results.h" -#include "core/hle/result.h" - -namespace Kernel { - -ClientSession::ClientSession(KernelCore& kernel) : KSynchronizationObject{kernel} {} - -ClientSession::~ClientSession() { - // This destructor will be called automatically when the last ClientSession handle is closed by - // the emulated application. - if (parent->Server()) { - parent->Server()->ClientDisconnected(); - } -} - -bool ClientSession::IsSignaled() const { - UNIMPLEMENTED(); - return true; -} - -ResultVal> ClientSession::Create(KernelCore& kernel, - std::shared_ptr parent, - std::string name) { - std::shared_ptr client_session{std::make_shared(kernel)}; - - client_session->name = std::move(name); - client_session->parent = std::move(parent); - - return MakeResult(std::move(client_session)); -} - -ResultCode ClientSession::SendSyncRequest(KThread* thread, Core::Memory::Memory& memory, - Core::Timing::CoreTiming& core_timing) { - // Keep ServerSession alive until we're done working with it. - if (!parent->Server()) { - return ResultSessionClosed; - } - - // Signal the server session that new data is available - return parent->Server()->HandleSyncRequest(std::move(thread), memory, core_timing); -} - -} // namespace Kernel diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index adfcd3c5b..ddb1e6fb2 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -56,7 +56,10 @@ ResultVal HandleTable::Create(Object* obj) { case HandleType::Event: case HandleType::Process: case HandleType::ReadableEvent: - case HandleType::WritableEvent: { + case HandleType::WritableEvent: + case HandleType::ClientSession: + case HandleType::ServerSession: + case HandleType::Session: { Handle handle{}; Add(&handle, reinterpret_cast(obj), {}); return MakeResult(handle); diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index 1e831aaca..d647d9dd3 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -19,12 +19,12 @@ #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h" +#include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/object.h" #include "core/hle/kernel/process.h" -#include "core/hle/kernel/server_session.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/time_manager.h" #include "core/memory.h" @@ -35,24 +35,19 @@ SessionRequestHandler::SessionRequestHandler() = default; SessionRequestHandler::~SessionRequestHandler() = default; -void SessionRequestHandler::ClientConnected(std::shared_ptr client_session, - std::shared_ptr server_session) { - server_session->SetHleHandler(shared_from_this()); - client_sessions.push_back(std::move(client_session)); - server_sessions.push_back(std::move(server_session)); +void SessionRequestHandler::ClientConnected(KSession* session) { + session->GetServerSession().SetHleHandler(shared_from_this()); + sessions.push_back(session); } -void SessionRequestHandler::ClientDisconnected( - const std::shared_ptr& server_session) { - server_session->SetHleHandler(nullptr); - boost::range::remove_erase(server_sessions, server_session); +void SessionRequestHandler::ClientDisconnected(KSession* session) { + session->GetServerSession().SetHleHandler(nullptr); + boost::range::remove_erase(sessions, session); } HLERequestContext::HLERequestContext(KernelCore& kernel_, Core::Memory::Memory& memory_, - std::shared_ptr server_session_, - KThread* thread_) - : server_session(std::move(server_session_)), - thread(thread_), kernel{kernel_}, memory{memory_} { + KServerSession* server_session_, KThread* thread_) + : server_session(server_session_), thread(thread_), kernel{kernel_}, memory{memory_} { cmd_buf[0] = 0; } diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index 74a95bc76..dc5c3b47d 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -39,10 +39,10 @@ class HandleTable; class HLERequestContext; class KernelCore; class Process; -class ClientSession; -class ServerSession; +class KServerSession; class KThread; class KReadableEvent; +class KSession; class KWritableEvent; enum class ThreadWakeupReason; @@ -72,22 +72,20 @@ public: * associated ServerSession alive for the duration of the connection. * @param server_session Owning pointer to the ServerSession associated with the connection. */ - void ClientConnected( - std::shared_ptr client_session, std::shared_ptr server_session); + void ClientConnected(KSession* session); /** * Signals that a client has just disconnected from this HLE handler and releases the * associated ServerSession. * @param server_session ServerSession associated with the connection. */ - void ClientDisconnected(const std::shared_ptr& server_session); + void ClientDisconnected(KSession* session); protected: /// List of sessions that are connected to this handler. /// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list /// for the duration of the connection. - std::vector> client_sessions; - std::vector> server_sessions; + std::vector sessions; }; /** @@ -112,7 +110,7 @@ protected: class HLERequestContext { public: explicit HLERequestContext(KernelCore& kernel, Core::Memory::Memory& memory, - std::shared_ptr session, KThread* thread); + KServerSession* session, KThread* thread); ~HLERequestContext(); /// Returns a pointer to the IPC command buffer for this request. @@ -124,7 +122,7 @@ public: * Returns the session through which this request was made. This can be used as a map key to * access per-client data on services. */ - const std::shared_ptr& Session() const { + Kernel::KServerSession* Session() { return server_session; } @@ -288,7 +286,7 @@ private: void ParseCommandBuffer(const HandleTable& handle_table, u32_le* src_cmdbuf, bool incoming); std::array cmd_buf; - std::shared_ptr server_session; + Kernel::KServerSession* server_session{}; KThread* thread; // TODO(yuriks): Check common usage of this and optimize size accordingly diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index b292f7db2..84d509d52 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -12,6 +12,7 @@ #include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" +#include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_shared_memory.h" #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/k_thread.h" @@ -27,7 +28,8 @@ namespace Kernel::Init { HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ - HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) + HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) \ + HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) namespace { diff --git a/src/core/hle/kernel/k_client_session.cpp b/src/core/hle/kernel/k_client_session.cpp new file mode 100644 index 000000000..0618dc246 --- /dev/null +++ b/src/core/hle/kernel/k_client_session.cpp @@ -0,0 +1,31 @@ +// Copyright 2021 yuzu emulator team +// 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_session.h" +#include "core/hle/kernel/k_server_session.h" +#include "core/hle/kernel/k_session.h" +#include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/svc_results.h" +#include "core/hle/result.h" + +namespace Kernel { + +KClientSession::KClientSession(KernelCore& kernel) : KAutoObjectWithSlabHeapAndContainer{kernel} {} +KClientSession::~KClientSession() = default; + +void KClientSession::Destroy() { + parent->OnClientClosed(); + parent->Close(); +} + +void KClientSession::OnServerClosed() {} + +ResultCode KClientSession::SendSyncRequest(KThread* thread, Core::Memory::Memory& memory, + Core::Timing::CoreTiming& core_timing) { + // Signal the server session that new data is available + return parent->GetServerSession().HandleSyncRequest(thread, memory, core_timing); +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/client_session.h b/src/core/hle/kernel/k_client_session.h similarity index 56% rename from src/core/hle/kernel/client_session.h rename to src/core/hle/kernel/k_client_session.h index 7a1d15d0c..c4b193773 100644 --- a/src/core/hle/kernel/client_session.h +++ b/src/core/hle/kernel/k_client_session.h @@ -1,4 +1,4 @@ -// Copyright 2019 yuzu emulator team +// Copyright 2021 yuzu emulator team // Licensed under GPLv2 or any later version // Refer to the license.txt file included. @@ -7,7 +7,9 @@ #include #include +#include "core/hle/kernel/k_auto_object.h" #include "core/hle/kernel/k_synchronization_object.h" +#include "core/hle/kernel/slab_helpers.h" #include "core/hle/result.h" union ResultCode; @@ -23,15 +25,41 @@ class CoreTiming; namespace Kernel { class KernelCore; -class Session; +class KSession; class KThread; -class ClientSession final : public KSynchronizationObject { -public: - explicit ClientSession(KernelCore& kernel); - ~ClientSession() override; +class KClientSession final + : public KAutoObjectWithSlabHeapAndContainer { + KERNEL_AUTOOBJECT_TRAITS(KClientSession, KAutoObject); - friend class Session; +public: + explicit KClientSession(KernelCore& kernel); + virtual ~KClientSession(); + + void Initialize(KSession* parent_, std::string&& name_) { + // Set member variables. + parent = parent_; + name = std::move(name_); + } + + virtual void Destroy() override; + static void PostDestroy([[maybe_unused]] uintptr_t arg) {} + + constexpr KSession* GetParent() const { + return parent; + } + + ResultCode SendSyncRequest(KThread* thread, Core::Memory::Memory& memory, + Core::Timing::CoreTiming& core_timing); + + void OnServerClosed(); + + // DEPRECATED + + static constexpr HandleType HANDLE_TYPE = HandleType::ClientSession; + HandleType GetHandleType() const override { + return HANDLE_TYPE; + } std::string GetTypeName() const override { return "ClientSession"; @@ -41,25 +69,9 @@ public: return name; } - static constexpr HandleType HANDLE_TYPE = HandleType::ClientSession; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - - ResultCode SendSyncRequest(KThread* thread, Core::Memory::Memory& memory, - Core::Timing::CoreTiming& core_timing); - - bool IsSignaled() const override; - - void Finalize() override {} - private: - static ResultVal> Create(KernelCore& kernel, - std::shared_ptr parent, - std::string name = "Unknown"); - /// The parent session, which links to the server endpoint. - std::shared_ptr parent; + KSession* parent{}; /// Name of the client session (optional) std::string name; diff --git a/src/core/hle/kernel/server_session.cpp b/src/core/hle/kernel/k_server_session.cpp similarity index 70% rename from src/core/hle/kernel/server_session.cpp rename to src/core/hle/kernel/k_server_session.cpp index bb247959c..8cd2c283c 100644 --- a/src/core/hle/kernel/server_session.cpp +++ b/src/core/hle/kernel/k_server_session.cpp @@ -11,43 +11,38 @@ #include "core/core_timing.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/client_port.h" -#include "core/hle/kernel/client_session.h" #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/hle_ipc.h" #include "core/hle/kernel/k_scheduler.h" +#include "core/hle/kernel/k_server_session.h" +#include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" -#include "core/hle/kernel/server_session.h" -#include "core/hle/kernel/session.h" #include "core/memory.h" namespace Kernel { -ServerSession::ServerSession(KernelCore& kernel) : KSynchronizationObject{kernel} {} +KServerSession::KServerSession(KernelCore& kernel) : KSynchronizationObject{kernel} {} -ServerSession::~ServerSession() { +KServerSession::~KServerSession() { kernel.ReleaseServiceThread(service_thread); } -ResultVal> ServerSession::Create(KernelCore& kernel, - std::shared_ptr parent, - std::string name) { - std::shared_ptr session{std::make_shared(kernel)}; - - session->name = std::move(name); - session->parent = std::move(parent); - session->service_thread = kernel.CreateServiceThread(session->name); - - return MakeResult(std::move(session)); +void KServerSession::Initialize(KSession* parent_, std::string&& name_) { + // Set member variables. + parent = parent_; + name = std::move(name_); + service_thread = kernel.CreateServiceThread(name); } -bool ServerSession::IsSignaled() const { - // Closed sessions should never wait, an error will be returned from svcReplyAndReceive. - return !parent->Client(); +void KServerSession::Destroy() { + parent->OnServerClosed(); + + parent->Close(); } -void ServerSession::ClientDisconnected() { +void KServerSession::OnClientClosed() { // We keep a shared pointer to the hle handler to keep it alive throughout // the call to ClientDisconnected, as ClientDisconnected invalidates the // hle_handler member itself during the course of the function executing. @@ -55,19 +50,31 @@ void ServerSession::ClientDisconnected() { if (handler) { // Note that after this returns, this server session's hle_handler is // invalidated (set to null). - handler->ClientDisconnected(SharedFrom(this)); + handler->ClientDisconnected(parent); } } -void ServerSession::AppendDomainRequestHandler(std::shared_ptr handler) { +bool KServerSession::IsSignaled() const { + ASSERT(kernel.GlobalSchedulerContext().IsLocked()); + + // If the client is closed, we're always signaled. + if (parent->IsClientClosed()) { + return true; + } + + // Otherwise, we're signaled if we have a request and aren't handling one. + return false; +} + +void KServerSession::AppendDomainRequestHandler(std::shared_ptr handler) { domain_request_handlers.push_back(std::move(handler)); } -std::size_t ServerSession::NumDomainRequestHandlers() const { +std::size_t KServerSession::NumDomainRequestHandlers() const { return domain_request_handlers.size(); } -ResultCode ServerSession::HandleDomainSyncRequest(Kernel::HLERequestContext& context) { +ResultCode KServerSession::HandleDomainSyncRequest(Kernel::HLERequestContext& context) { if (!context.HasDomainMessageHeader()) { return RESULT_SUCCESS; } @@ -106,21 +113,21 @@ ResultCode ServerSession::HandleDomainSyncRequest(Kernel::HLERequestContext& con return RESULT_SUCCESS; } -ResultCode ServerSession::QueueSyncRequest(KThread* thread, Core::Memory::Memory& memory) { +ResultCode KServerSession::QueueSyncRequest(KThread* thread, Core::Memory::Memory& memory) { u32* cmd_buf{reinterpret_cast(memory.GetPointer(thread->GetTLSAddress()))}; - auto context = std::make_shared(kernel, memory, SharedFrom(this), thread); + auto context = std::make_shared(kernel, memory, this, thread); context->PopulateFromIncomingCommandBuffer(kernel.CurrentProcess()->GetHandleTable(), cmd_buf); if (auto strong_ptr = service_thread.lock()) { - strong_ptr->QueueSyncRequest(*this, std::move(context)); + strong_ptr->QueueSyncRequest(*parent, std::move(context)); return RESULT_SUCCESS; } return RESULT_SUCCESS; } -ResultCode ServerSession::CompleteSyncRequest(HLERequestContext& context) { +ResultCode KServerSession::CompleteSyncRequest(HLERequestContext& context) { ResultCode result = RESULT_SUCCESS; // If the session has been converted to a domain, handle the domain request if (IsDomain() && context.HasDomainMessageHeader()) { @@ -149,8 +156,8 @@ ResultCode ServerSession::CompleteSyncRequest(HLERequestContext& context) { return result; } -ResultCode ServerSession::HandleSyncRequest(KThread* thread, Core::Memory::Memory& memory, - Core::Timing::CoreTiming& core_timing) { +ResultCode KServerSession::HandleSyncRequest(KThread* thread, Core::Memory::Memory& memory, + Core::Timing::CoreTiming& core_timing) { return QueueSyncRequest(thread, memory); } diff --git a/src/core/hle/kernel/server_session.h b/src/core/hle/kernel/k_server_session.h similarity index 70% rename from src/core/hle/kernel/server_session.h rename to src/core/hle/kernel/k_server_session.h index 77ed18c60..ef81c4e30 100644 --- a/src/core/hle/kernel/server_session.h +++ b/src/core/hle/kernel/k_server_session.h @@ -27,55 +27,34 @@ namespace Kernel { class HLERequestContext; class KernelCore; -class Session; +class KSession; class SessionRequestHandler; class KThread; -/** - * Kernel object representing the server endpoint of an IPC session. Sessions are the basic CTR-OS - * primitive for communication between different processes, and are used to implement service calls - * to the various system services. - * - * To make a service call, the client must write the command header and parameters to the buffer - * located at offset 0x80 of the TLS (Thread-Local Storage) area, then execute a SendSyncRequest - * SVC call with its ClientSession handle. The kernel will read the command header, using it to - * marshall the parameters to the process at the server endpoint of the session. - * After the server replies to the request, the response is marshalled back to the caller's - * TLS buffer and control is transferred back to it. - */ -class ServerSession final : public KSynchronizationObject { +class KServerSession final : public KSynchronizationObject { + KERNEL_AUTOOBJECT_TRAITS(KServerSession, KSynchronizationObject); + friend class ServiceThread; public: - explicit ServerSession(KernelCore& kernel); - ~ServerSession() override; + explicit KServerSession(KernelCore& kernel); + virtual ~KServerSession() override; - friend class Session; + virtual void Destroy() override; - static ResultVal> Create(KernelCore& kernel, - std::shared_ptr parent, - std::string name = "Unknown"); + void Initialize(KSession* parent_, std::string&& name_); - std::string GetTypeName() const override { - return "ServerSession"; + constexpr KSession* GetParent() { + return parent; } - std::string GetName() const override { - return name; + constexpr const KSession* GetParent() const { + return parent; } - static constexpr HandleType HANDLE_TYPE = HandleType::ServerSession; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } + virtual bool IsSignaled() const override; - Session* GetParent() { - return parent.get(); - } - - const Session* GetParent() const { - return parent.get(); - } + void OnClientClosed(); /** * Sets the HLE handler for the session. This handler will be called to service IPC requests @@ -98,9 +77,6 @@ public: ResultCode HandleSyncRequest(KThread* thread, Core::Memory::Memory& memory, Core::Timing::CoreTiming& core_timing); - /// Called when a client disconnection occurs. - void ClientDisconnected(); - /// Adds a new domain request handler to the collection of request handlers within /// this ServerSession instance. void AppendDomainRequestHandler(std::shared_ptr handler); @@ -124,9 +100,20 @@ public: convert_to_domain = true; } - bool IsSignaled() const override; + // DEPRECATED - void Finalize() override {} + std::string GetTypeName() const override { + return "ServerSession"; + } + + std::string GetName() const override { + return name; + } + + static constexpr HandleType HANDLE_TYPE = HandleType::ServerSession; + HandleType GetHandleType() const override { + return HANDLE_TYPE; + } private: /// Queues a sync request from the emulated application. @@ -139,9 +126,6 @@ private: /// object handle. ResultCode HandleDomainSyncRequest(Kernel::HLERequestContext& context); - /// The parent session, which links to the client endpoint. - std::shared_ptr parent; - /// This session's HLE request handler (applicable when not a domain) std::shared_ptr hle_handler; @@ -156,6 +140,9 @@ private: /// Thread to dispatch service requests std::weak_ptr service_thread; + + /// KSession that owns this KServerSession + KSession* parent{}; }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_session.cpp b/src/core/hle/kernel/k_session.cpp new file mode 100644 index 000000000..ca1cf18cd --- /dev/null +++ b/src/core/hle/kernel/k_session.cpp @@ -0,0 +1,67 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/assert.h" +#include "core/hle/kernel/k_client_session.h" +#include "core/hle/kernel/k_scoped_resource_reservation.h" +#include "core/hle/kernel/k_server_session.h" +#include "core/hle/kernel/k_session.h" + +namespace Kernel { + +KSession::KSession(KernelCore& kernel) + : KAutoObjectWithSlabHeapAndContainer{kernel}, server{kernel}, client{kernel} {} +KSession::~KSession() = default; + +void KSession::Initialize(std::string&& name_) { + // Increment reference count. + // Because reference count is one on creation, this will result + // in a reference count of two. Thus, when both server and client are closed + // this object will be destroyed. + Open(); + + // Create our sub sessions. + KAutoObject::Create(std::addressof(server)); + KAutoObject::Create(std::addressof(client)); + + // Initialize our sub sessions. + server.Initialize(this, name_ + ":Server"); + client.Initialize(this, name_ + ":Client"); + + // Set state and name. + SetState(State::Normal); + name = std::move(name_); + + // Set our owner process. + process = kernel.CurrentProcess(); + process->Open(); + + // Mark initialized. + initialized = true; +} + +void KSession::Finalize() {} + +void KSession::OnServerClosed() { + if (GetState() == State::Normal) { + SetState(State::ServerClosed); + client.OnServerClosed(); + } +} + +void KSession::OnClientClosed() { + if (GetState() == State::Normal) { + SetState(State::ClientClosed); + server.OnClientClosed(); + } +} + +void KSession::PostDestroy(uintptr_t arg) { + // Release the session count resource the owner process holds. + Process* owner = reinterpret_cast(arg); + owner->GetResourceLimit()->Release(LimitableResource::Sessions, 1); + owner->Close(); +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_session.h b/src/core/hle/kernel/k_session.h new file mode 100644 index 000000000..1d24e80cd --- /dev/null +++ b/src/core/hle/kernel/k_session.h @@ -0,0 +1,108 @@ +// Copyright 2021 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +#include "core/hle/kernel/k_client_session.h" +#include "core/hle/kernel/k_server_session.h" +#include "core/hle/kernel/slab_helpers.h" + +namespace Kernel { + +class KSession final : public KAutoObjectWithSlabHeapAndContainer { + KERNEL_AUTOOBJECT_TRAITS(KSession, KAutoObject); + +private: + enum class State : u8 { + Invalid = 0, + Normal = 1, + ClientClosed = 2, + ServerClosed = 3, + }; + +public: + explicit KSession(KernelCore& kernel); + virtual ~KSession() override; + + void Initialize(std::string&& name_); + + virtual void Finalize() override; + + virtual bool IsInitialized() const override { + return initialized; + } + + virtual uintptr_t GetPostDestroyArgument() const override { + return reinterpret_cast(process); + } + + static void PostDestroy(uintptr_t arg); + + void OnServerClosed(); + + void OnClientClosed(); + + bool IsServerClosed() const { + return this->GetState() != State::Normal; + } + + bool IsClientClosed() const { + return this->GetState() != State::Normal; + } + + KClientSession& GetClientSession() { + return client; + } + + KServerSession& GetServerSession() { + return server; + } + + const KClientSession& GetClientSession() const { + return client; + } + + const KServerSession& GetServerSession() const { + return server; + } + + const ClientPort* GetParent() const { + return port; + } + + // DEPRECATED + + std::string GetName() const override { + return name; + } + + static constexpr HandleType HANDLE_TYPE = HandleType::Session; + HandleType GetHandleType() const override { + return HANDLE_TYPE; + } + +private: + void SetState(State state) { + atomic_state = static_cast(state); + } + + State GetState() const { + return static_cast(atomic_state.load()); + } + +private: + KServerSession server; + KClientSession client; + std::atomic::type> atomic_state{ + static_cast::type>(State::Invalid)}; + ClientPort* port{}; + std::string name; + Process* process{}; + bool initialized{}; +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 850436eb3..ecced1034 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -33,11 +33,13 @@ class ClientPort; class GlobalSchedulerContext; class HandleTable; class KAutoObjectWithListContainer; +class KClientSession; class KEvent; class KLinkedListNode; class KMemoryManager; class KResourceLimit; class KScheduler; +class KSession; class KSharedMemory; class KThread; class KWritableEvent; @@ -272,6 +274,10 @@ public: return slab_heap_container->linked_list_node; } else if constexpr (std::is_same_v) { return slab_heap_container->writeable_event; + } else if constexpr (std::is_same_v) { + return slab_heap_container->client_session; + } else if constexpr (std::is_same_v) { + return slab_heap_container->session; } } @@ -312,6 +318,8 @@ private: KSlabHeap shared_memory; KSlabHeap linked_list_node; KSlabHeap writeable_event; + KSlabHeap client_session; + KSlabHeap session; }; std::unique_ptr slab_heap_container; diff --git a/src/core/hle/kernel/server_port.cpp b/src/core/hle/kernel/server_port.cpp index 5d17346ad..8626b56fd 100644 --- a/src/core/hle/kernel/server_port.cpp +++ b/src/core/hle/kernel/server_port.cpp @@ -5,10 +5,10 @@ #include #include "common/assert.h" #include "core/hle/kernel/client_port.h" +#include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/object.h" #include "core/hle/kernel/server_port.h" -#include "core/hle/kernel/server_session.h" #include "core/hle/kernel/svc_results.h" namespace Kernel { @@ -16,17 +16,17 @@ namespace Kernel { ServerPort::ServerPort(KernelCore& kernel) : KSynchronizationObject{kernel} {} ServerPort::~ServerPort() = default; -ResultVal> ServerPort::Accept() { +ResultVal ServerPort::Accept() { if (pending_sessions.empty()) { return ResultNotFound; } - auto session = std::move(pending_sessions.back()); + auto* session = pending_sessions.back(); pending_sessions.pop_back(); - return MakeResult(std::move(session)); + return MakeResult(session); } -void ServerPort::AppendPendingSession(std::shared_ptr pending_session) { +void ServerPort::AppendPendingSession(KServerSession* pending_session) { pending_sessions.push_back(std::move(pending_session)); if (pending_sessions.size() == 1) { NotifyAvailable(); diff --git a/src/core/hle/kernel/server_port.h b/src/core/hle/kernel/server_port.h index 29b4f2509..eebceaa2a 100644 --- a/src/core/hle/kernel/server_port.h +++ b/src/core/hle/kernel/server_port.h @@ -17,7 +17,7 @@ namespace Kernel { class ClientPort; class KernelCore; -class ServerSession; +class KServerSession; class SessionRequestHandler; class ServerPort final : public KSynchronizationObject { @@ -55,7 +55,7 @@ public: * Accepts a pending incoming connection on this port. If there are no pending sessions, will * return ERR_NO_PENDING_SESSIONS. */ - ResultVal> Accept(); + ResultVal Accept(); /// Whether or not this server port has an HLE handler available. bool HasHLEHandler() const { @@ -77,7 +77,7 @@ public: /// Appends a ServerSession to the collection of ServerSessions /// waiting to be accepted by this port. - void AppendPendingSession(std::shared_ptr pending_session); + void AppendPendingSession(KServerSession* pending_session); bool IsSignaled() const override; @@ -85,7 +85,7 @@ public: private: /// ServerSessions waiting to be accepted by the port - std::vector> pending_sessions; + std::vector pending_sessions; /// This session's HLE request handler template (optional) /// ServerSessions created from this port inherit a reference to this handler. diff --git a/src/core/hle/kernel/service_thread.cpp b/src/core/hle/kernel/service_thread.cpp index ee46f3e21..04be8a502 100644 --- a/src/core/hle/kernel/service_thread.cpp +++ b/src/core/hle/kernel/service_thread.cpp @@ -13,8 +13,8 @@ #include "common/scope_exit.h" #include "common/thread.h" #include "core/core.h" +#include "core/hle/kernel/k_session.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/server_session.h" #include "core/hle/kernel/service_thread.h" #include "core/hle/lock.h" #include "video_core/renderer_base.h" @@ -26,7 +26,7 @@ public: explicit Impl(KernelCore& kernel, std::size_t num_threads, const std::string& name); ~Impl(); - void QueueSyncRequest(ServerSession& session, std::shared_ptr&& context); + void QueueSyncRequest(KSession& session, std::shared_ptr&& context); private: std::vector threads; @@ -69,18 +69,27 @@ ServiceThread::Impl::Impl(KernelCore& kernel, std::size_t num_threads, const std }); } -void ServiceThread::Impl::QueueSyncRequest(ServerSession& session, +void ServiceThread::Impl::QueueSyncRequest(KSession& session, std::shared_ptr&& context) { { std::unique_lock lock{queue_mutex}; - // ServerSession owns the service thread, so we cannot caption a strong pointer here in the - // event that the ServerSession is terminated. - std::weak_ptr weak_ptr{SharedFrom(&session)}; - requests.emplace([weak_ptr, context{std::move(context)}]() { - if (auto strong_ptr = weak_ptr.lock()) { - strong_ptr->CompleteSyncRequest(*context); + // Open a reference to the session to ensure it is not closes while the service request + // completes asynchronously. + session.Open(); + + requests.emplace([session_ptr{&session}, context{std::move(context)}]() { + // Close the reference. + SCOPE_EXIT({ session_ptr->Close(); }); + + // If the session has been closed, we are done. + if (session_ptr->IsServerClosed()) { + return; } + + // Complete the service request. + KScopedAutoObject server_session{&session_ptr->GetServerSession()}; + server_session->CompleteSyncRequest(*context); }); } condition.notify_one(); @@ -102,7 +111,7 @@ ServiceThread::ServiceThread(KernelCore& kernel, std::size_t num_threads, const ServiceThread::~ServiceThread() = default; -void ServiceThread::QueueSyncRequest(ServerSession& session, +void ServiceThread::QueueSyncRequest(KSession& session, std::shared_ptr&& context) { impl->QueueSyncRequest(session, std::move(context)); } diff --git a/src/core/hle/kernel/service_thread.h b/src/core/hle/kernel/service_thread.h index 025ab8fb5..6a7fd7c56 100644 --- a/src/core/hle/kernel/service_thread.h +++ b/src/core/hle/kernel/service_thread.h @@ -11,14 +11,14 @@ namespace Kernel { class HLERequestContext; class KernelCore; -class ServerSession; +class KSession; class ServiceThread final { public: explicit ServiceThread(KernelCore& kernel, std::size_t num_threads, const std::string& name); ~ServiceThread(); - void QueueSyncRequest(ServerSession& session, std::shared_ptr&& context); + void QueueSyncRequest(KSession& session, std::shared_ptr&& context); private: class Impl; diff --git a/src/core/hle/kernel/session.cpp b/src/core/hle/kernel/session.cpp deleted file mode 100644 index 8830d4e91..000000000 --- a/src/core/hle/kernel/session.cpp +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2019 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include "common/assert.h" -#include "core/hle/kernel/client_session.h" -#include "core/hle/kernel/k_scoped_resource_reservation.h" -#include "core/hle/kernel/server_session.h" -#include "core/hle/kernel/session.h" - -namespace Kernel { - -Session::Session(KernelCore& kernel) : KSynchronizationObject{kernel} {} -Session::~Session() { - // Release reserved resource when the Session pair was created. - kernel.GetSystemResourceLimit()->Release(LimitableResource::Sessions, 1); -} - -Session::SessionPair Session::Create(KernelCore& kernel, std::string name) { - // Reserve a new session from the resource limit. - KScopedResourceReservation session_reservation(kernel.GetSystemResourceLimit(), - LimitableResource::Sessions); - ASSERT(session_reservation.Succeeded()); - auto session{std::make_shared(kernel)}; - auto client_session{Kernel::ClientSession::Create(kernel, session, name + "_Client").Unwrap()}; - auto server_session{Kernel::ServerSession::Create(kernel, session, name + "_Server").Unwrap()}; - - session->name = std::move(name); - session->client = client_session; - session->server = server_session; - - session_reservation.Commit(); - return std::make_pair(std::move(client_session), std::move(server_session)); -} - -bool Session::IsSignaled() const { - UNIMPLEMENTED(); - return true; -} - -} // namespace Kernel diff --git a/src/core/hle/kernel/session.h b/src/core/hle/kernel/session.h deleted file mode 100644 index fa3c5651a..000000000 --- a/src/core/hle/kernel/session.h +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2019 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include - -#include "core/hle/kernel/k_synchronization_object.h" - -namespace Kernel { - -class ClientSession; -class ServerSession; - -/** - * Parent structure to link the client and server endpoints of a session with their associated - * client port. - */ -class Session final : public KSynchronizationObject { -public: - explicit Session(KernelCore& kernel); - ~Session() override; - - using SessionPair = std::pair, std::shared_ptr>; - - static SessionPair Create(KernelCore& kernel, std::string name = "Unknown"); - - std::string GetName() const override { - return name; - } - - static constexpr HandleType HANDLE_TYPE = HandleType::Session; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - - bool IsSignaled() const override; - - void Finalize() override {} - - std::shared_ptr Client() { - if (auto result{client.lock()}) { - return result; - } - return {}; - } - - std::shared_ptr Server() { - if (auto result{server.lock()}) { - return result; - } - return {}; - } - -private: - std::string name; - std::weak_ptr client; - std::weak_ptr server; -}; - -} // namespace Kernel diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index b83ee3e69..28c45e8a3 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -22,9 +22,9 @@ #include "core/core_timing_util.h" #include "core/cpu_manager.h" #include "core/hle/kernel/client_port.h" -#include "core/hle/kernel/client_session.h" #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/k_address_arbiter.h" +#include "core/hle/kernel/k_client_session.h" #include "core/hle/kernel/k_condition_variable.h" #include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_memory_block.h" @@ -323,12 +323,12 @@ static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle, auto client_port = it->second; - std::shared_ptr client_session; + KClientSession* client_session{}; CASCADE_RESULT(client_session, client_port->Connect()); // Return the client session auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); - CASCADE_RESULT(*out_handle, handle_table.Create(client_session.get())); + CASCADE_RESULT(*out_handle, handle_table.Create(client_session)); return RESULT_SUCCESS; } @@ -340,16 +340,14 @@ static ResultCode ConnectToNamedPort32(Core::System& system, Handle* out_handle, /// Makes a blocking IPC call to an OS service. static ResultCode SendSyncRequest(Core::System& system, Handle handle) { - auto& kernel = system.Kernel(); - const auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); - auto session = handle_table.Get(handle); - if (!session) { - LOG_ERROR(Kernel_SVC, "called with invalid handle=0x{:08X}", handle); - return ResultInvalidHandle; - } - LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}({})", handle, session->GetName()); + auto& kernel = system.Kernel(); + + KScopedAutoObject session = + kernel.CurrentProcess()->GetHandleTable().GetObject(handle); + R_UNLESS(session.IsNotNull(), ResultInvalidHandle); + auto thread = kernel.CurrentScheduler()->GetCurrentThread(); { KScopedSchedulerLock lock(kernel); diff --git a/src/core/hle/service/am/applets/applets.cpp b/src/core/hle/service/am/applets/applets.cpp index cf8ad7598..ae995df6b 100644 --- a/src/core/hle/service/am/applets/applets.cpp +++ b/src/core/hle/service/am/applets/applets.cpp @@ -14,7 +14,6 @@ #include "core/frontend/applets/web_browser.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_writable_event.h" -#include "core/hle/kernel/server_session.h" #include "core/hle/service/am/am.h" #include "core/hle/service/am/applet_ae.h" #include "core/hle/service/am/applet_oe.h" diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index 49c1db42a..6b3ebeb8f 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -14,7 +14,6 @@ #include "core/hardware_properties.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/client_port.h" -#include "core/hle/kernel/client_session.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_shared_memory.h" #include "core/hle/kernel/k_writable_event.h" diff --git a/src/core/hle/service/mm/mm_u.cpp b/src/core/hle/service/mm/mm_u.cpp index b0cb07d24..c8519e2db 100644 --- a/src/core/hle/service/mm/mm_u.cpp +++ b/src/core/hle/service/mm/mm_u.cpp @@ -4,7 +4,6 @@ #include "common/logging/log.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/client_session.h" #include "core/hle/service/mm/mm_u.h" #include "core/hle/service/sm/sm.h" diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h index 916445517..076f50b0b 100644 --- a/src/core/hle/service/service.h +++ b/src/core/hle/service/service.h @@ -21,11 +21,8 @@ class System; } namespace Kernel { -class ClientPort; -class ServerPort; -class ServerSession; class HLERequestContext; -} // namespace Kernel +} namespace Service { diff --git a/src/core/hle/service/sm/controller.cpp b/src/core/hle/service/sm/controller.cpp index b34fe4bc2..cb397fcc7 100644 --- a/src/core/hle/service/sm/controller.cpp +++ b/src/core/hle/service/sm/controller.cpp @@ -5,9 +5,9 @@ #include "common/assert.h" #include "common/logging/log.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/client_session.h" -#include "core/hle/kernel/server_session.h" -#include "core/hle/kernel/session.h" +#include "core/hle/kernel/k_client_session.h" +#include "core/hle/kernel/k_server_session.h" +#include "core/hle/kernel/k_session.h" #include "core/hle/service/sm/controller.h" namespace Service::SM { @@ -30,7 +30,7 @@ void Controller::CloneCurrentObject(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles}; rb.Push(RESULT_SUCCESS); - rb.PushMoveObjects(ctx.Session()->GetParent()->Client().get()); + rb.PushMoveObjects(ctx.Session()->GetParent()->GetClientSession()); } void Controller::CloneCurrentObjectEx(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 62f7a5358..66e41277f 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -7,7 +7,9 @@ #include "core/core.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/client_port.h" -#include "core/hle/kernel/client_session.h" +#include "core/hle/kernel/k_client_session.h" +#include "core/hle/kernel/k_server_session.h" +#include "core/hle/kernel/k_session.h" #include "core/hle/kernel/server_port.h" #include "core/hle/result.h" #include "core/hle/service/sm/controller.h" @@ -89,13 +91,6 @@ ResultVal> ServiceManager::GetServicePort( return MakeResult(it->second); } -ResultVal> ServiceManager::ConnectToService( - const std::string& name) { - - CASCADE_RESULT(auto client_port, GetServicePort(name)); - return client_port->Connect(); -} - SM::~SM() = default; /** @@ -130,19 +125,20 @@ void SM::GetService(Kernel::HLERequestContext& ctx) { return; } - auto [client, server] = Kernel::Session::Create(kernel, name); + auto* session = Kernel::KSession::Create(kernel); + session->Initialize(std::move(name)); const auto& server_port = client_port.Unwrap()->GetServerPort(); if (server_port->GetHLEHandler()) { - server_port->GetHLEHandler()->ClientConnected(client, server); + server_port->GetHLEHandler()->ClientConnected(session); } else { - server_port->AppendPendingSession(server); + server_port->AppendPendingSession(&session->GetServerSession()); } - LOG_DEBUG(Service_SM, "called service={} -> session={}", name, client->GetObjectId()); + LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetObjectId()); IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles}; rb.Push(RESULT_SUCCESS); - rb.PushMoveObjects(client.get()); + rb.PushMoveObjects(session->GetClientSession()); } void SM::RegisterService(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h index 3f46ae44f..8f6862fa9 100644 --- a/src/core/hle/service/sm/sm.h +++ b/src/core/hle/service/sm/sm.h @@ -22,7 +22,7 @@ class System; namespace Kernel { class ClientPort; -class ClientSession; +class KClientSession; class KernelCore; class ServerPort; class SessionRequestHandler; @@ -59,7 +59,6 @@ public: u32 max_sessions); ResultCode UnregisterService(const std::string& name); ResultVal> GetServicePort(const std::string& name); - ResultVal> ConnectToService(const std::string& name); template T> std::shared_ptr GetService(const std::string& service_name) const { @@ -81,7 +80,7 @@ private: std::weak_ptr sm_interface; std::unique_ptr controller_interface; - /// Map of registered services, retrieved using GetServicePort or ConnectToService. + /// Map of registered services, retrieved using GetServicePort. std::unordered_map> registered_services; /// Kernel context diff --git a/src/core/hle/service/time/time.cpp b/src/core/hle/service/time/time.cpp index 30283f239..413a00ae0 100644 --- a/src/core/hle/service/time/time.cpp +++ b/src/core/hle/service/time/time.cpp @@ -9,7 +9,6 @@ #include "core/hardware_properties.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/client_port.h" -#include "core/hle/kernel/client_session.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/kernel.h" #include "core/hle/service/time/interface.h" From c7d8b7421cd6bdb64410bbb0094ce540f0280c27 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 17 Apr 2021 00:52:53 -0700 Subject: [PATCH 35/82] hle: kernel: Migrate KTransferMemory to KAutoObject. --- src/core/CMakeLists.txt | 4 +- src/core/hle/kernel/handle_table.cpp | 3 +- src/core/hle/kernel/init/init_slab_setup.cpp | 2 + src/core/hle/kernel/k_page_table.h | 4 +- src/core/hle/kernel/k_transfer_memory.cpp | 45 +++++++++ src/core/hle/kernel/k_transfer_memory.h | 82 ++++++++++++++++ src/core/hle/kernel/kernel.h | 4 + src/core/hle/kernel/svc.cpp | 99 ++++++++++---------- src/core/hle/kernel/svc_wrap.h | 12 ++- src/core/hle/service/am/am.cpp | 9 +- src/core/hle/service/am/am.h | 2 +- src/core/hle/service/hid/hid.cpp | 17 ++-- 12 files changed, 212 insertions(+), 71 deletions(-) create mode 100644 src/core/hle/kernel/k_transfer_memory.cpp create mode 100644 src/core/hle/kernel/k_transfer_memory.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index e30a30823..8e3b51ae7 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -221,6 +221,8 @@ add_library(core STATIC hle/kernel/k_thread.h hle/kernel/k_thread_queue.h hle/kernel/k_trace.h + hle/kernel/k_transfer_memory.cpp + hle/kernel/k_transfer_memory.h hle/kernel/k_writable_event.cpp hle/kernel/k_writable_event.h hle/kernel/kernel.cpp @@ -247,8 +249,6 @@ add_library(core STATIC hle/kernel/svc_wrap.h hle/kernel/time_manager.cpp hle/kernel/time_manager.h - hle/kernel/transfer_memory.cpp - hle/kernel/transfer_memory.h hle/lock.cpp hle/lock.h hle/result.h diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index ddb1e6fb2..9291f0a76 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -59,7 +59,8 @@ ResultVal HandleTable::Create(Object* obj) { case HandleType::WritableEvent: case HandleType::ClientSession: case HandleType::ServerSession: - case HandleType::Session: { + case HandleType::Session: + case HandleType::TransferMemory: { Handle handle{}; Add(&handle, reinterpret_cast(obj), {}); return MakeResult(handle); diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index 84d509d52..ce7a24c40 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -14,6 +14,7 @@ #include "core/hle/kernel/k_memory_manager.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_shared_memory.h" +#include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/memory_types.h" @@ -28,6 +29,7 @@ namespace Kernel::Init { HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ + HANDLER(KTransferMemory, (SLAB_COUNT(KTransferMemory)), ##__VA_ARGS__) \ HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) \ HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) diff --git a/src/core/hle/kernel/k_page_table.h b/src/core/hle/kernel/k_page_table.h index 49b824379..80a1586db 100644 --- a/src/core/hle/kernel/k_page_table.h +++ b/src/core/hle/kernel/k_page_table.h @@ -216,8 +216,6 @@ public: constexpr PAddr GetPhysicalAddr(VAddr addr) { return page_table_impl.backing_addr[addr >> PageBits] + addr; } - -private: constexpr bool Contains(VAddr addr) const { return address_space_start <= addr && addr <= address_space_end - 1; } @@ -225,6 +223,8 @@ private: return address_space_start <= addr && addr < addr + size && addr + size - 1 <= address_space_end - 1; } + +private: constexpr bool IsKernel() const { return is_kernel; } diff --git a/src/core/hle/kernel/k_transfer_memory.cpp b/src/core/hle/kernel/k_transfer_memory.cpp new file mode 100644 index 000000000..09c067f95 --- /dev/null +++ b/src/core/hle/kernel/k_transfer_memory.cpp @@ -0,0 +1,45 @@ +// Copyright 2021 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/hle/kernel/k_resource_limit.h" +#include "core/hle/kernel/k_transfer_memory.h" +#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/process.h" + +namespace Kernel { + +KTransferMemory::KTransferMemory(KernelCore& kernel) + : KAutoObjectWithSlabHeapAndContainer{kernel} {} + +KTransferMemory::~KTransferMemory() = default; + +ResultCode KTransferMemory::Initialize(VAddr address_, std::size_t size_, + Svc::MemoryPermission owner_perm_) { + // Set members. + owner = kernel.CurrentProcess(); + + // TODO(bunnei): Lock for transfer memory + + // Set remaining tracking members. + owner->Open(); + owner_perm = owner_perm_; + address = address_; + size = size_; + is_initialized = true; + + return RESULT_SUCCESS; +} + +void KTransferMemory::Finalize() { + // Perform inherited finalization. + KAutoObjectWithSlabHeapAndContainer::Finalize(); +} + +void KTransferMemory::PostDestroy(uintptr_t arg) { + Process* owner = reinterpret_cast(arg); + owner->GetResourceLimit()->Release(LimitableResource::TransferMemory, 1); + owner->Close(); +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_transfer_memory.h b/src/core/hle/kernel/k_transfer_memory.h new file mode 100644 index 000000000..f43725c7f --- /dev/null +++ b/src/core/hle/kernel/k_transfer_memory.h @@ -0,0 +1,82 @@ +// Copyright 2021 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include + +#include "core/hle/kernel/slab_helpers.h" +#include "core/hle/kernel/svc_types.h" +#include "core/hle/result.h" + +union ResultCode; + +namespace Core::Memory { +class Memory; +} + +namespace Kernel { + +class KernelCore; +class Process; + +class KTransferMemory final + : public KAutoObjectWithSlabHeapAndContainer { + KERNEL_AUTOOBJECT_TRAITS(KTransferMemory, KAutoObject); + +public: + explicit KTransferMemory(KernelCore& kernel); + ~KTransferMemory() override; + + static constexpr HandleType HANDLE_TYPE = HandleType::TransferMemory; + + ResultCode Initialize(VAddr address_, std::size_t size_, Svc::MemoryPermission owner_perm_); + + virtual void Finalize() override; + + virtual bool IsInitialized() const override { + return is_initialized; + } + + virtual uintptr_t GetPostDestroyArgument() const override { + return reinterpret_cast(owner); + } + + static void PostDestroy(uintptr_t arg); + + Process* GetOwner() const { + return owner; + } + + VAddr GetSourceAddress() const { + return address; + } + + size_t GetSize() const { + return is_initialized ? size * PageSize : 0; + } + + // DEPRECATED + + std::string GetTypeName() const override { + return "TransferMemory"; + } + + std::string GetName() const override { + return GetTypeName(); + } + + HandleType GetHandleType() const override { + return HANDLE_TYPE; + } + +private: + Process* owner{}; + VAddr address{}; + Svc::MemoryPermission owner_perm{}; + size_t size{}; + bool is_initialized{}; +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index ecced1034..f07f0276e 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -42,6 +42,7 @@ class KScheduler; class KSession; class KSharedMemory; class KThread; +class KTransferMemory; class KWritableEvent; class PhysicalCore; class Process; @@ -278,6 +279,8 @@ public: return slab_heap_container->client_session; } else if constexpr (std::is_same_v) { return slab_heap_container->session; + } else if constexpr (std::is_same_v) { + return slab_heap_container->transfer_memory; } } @@ -320,6 +323,7 @@ private: KSlabHeap writeable_event; KSlabHeap client_session; KSlabHeap session; + KSlabHeap transfer_memory; }; std::unique_ptr slab_heap_container; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 28c45e8a3..0b7eb0740 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -38,6 +38,7 @@ #include "core/hle/kernel/k_shared_memory.h" #include "core/hle/kernel/k_synchronization_object.h" #include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" @@ -47,7 +48,6 @@ #include "core/hle/kernel/svc_types.h" #include "core/hle/kernel/svc_wrap.h" #include "core/hle/kernel/time_manager.h" -#include "core/hle/kernel/transfer_memory.h" #include "core/hle/lock.h" #include "core/hle/result.h" #include "core/hle/service/service.h" @@ -1868,65 +1868,68 @@ static ResultCode ResetSignal32(Core::System& system, Handle handle) { return ResetSignal(system, handle); } +static constexpr bool IsValidTransferMemoryPermission(MemoryPermission perm) { + switch (perm) { + case MemoryPermission::None: + case MemoryPermission::Read: + case MemoryPermission::ReadWrite: + return true; + default: + return false; + } +} + /// Creates a TransferMemory object -static ResultCode CreateTransferMemory(Core::System& system, Handle* handle, VAddr addr, u64 size, - u32 permissions) { - std::lock_guard lock{HLE::g_hle_lock}; - LOG_DEBUG(Kernel_SVC, "called addr=0x{:X}, size=0x{:X}, perms=0x{:08X}", addr, size, - permissions); - - if (!Common::Is4KBAligned(addr)) { - LOG_ERROR(Kernel_SVC, "Address ({:016X}) is not page aligned!", addr); - return ResultInvalidAddress; - } - - if (!Common::Is4KBAligned(size) || size == 0) { - LOG_ERROR(Kernel_SVC, "Size ({:016X}) is not page aligned or equal to zero!", size); - return ResultInvalidAddress; - } - - if (!IsValidAddressRange(addr, size)) { - LOG_ERROR(Kernel_SVC, "Address and size cause overflow! (address={:016X}, size={:016X})", - addr, size); - return ResultInvalidCurrentMemory; - } - - const auto perms{static_cast(permissions)}; - if (perms > MemoryPermission::ReadWrite || perms == MemoryPermission::Write) { - LOG_ERROR(Kernel_SVC, "Invalid memory permissions for transfer memory! (perms={:08X})", - permissions); - return ResultInvalidNewMemoryPermission; - } - +static ResultCode CreateTransferMemory(Core::System& system, Handle* out, VAddr address, u64 size, + MemoryPermission map_perm) { auto& kernel = system.Kernel(); + + // Validate the size. + R_UNLESS(Common::IsAligned(address, PageSize), ResultInvalidAddress); + R_UNLESS(Common::IsAligned(size, PageSize), ResultInvalidSize); + R_UNLESS(size > 0, ResultInvalidSize); + R_UNLESS((address < address + size), ResultInvalidCurrentMemory); + + // Validate the permissions. + R_UNLESS(IsValidTransferMemoryPermission(map_perm), ResultInvalidNewMemoryPermission); + + // Get the current process and handle table. + auto& process = *kernel.CurrentProcess(); + auto& handle_table = process.GetHandleTable(); + // Reserve a new transfer memory from the process resource limit. KScopedResourceReservation trmem_reservation(kernel.CurrentProcess(), LimitableResource::TransferMemory); - if (!trmem_reservation.Succeeded()) { - LOG_ERROR(Kernel_SVC, "Could not reserve a new transfer memory"); - return ResultLimitReached; - } - auto transfer_mem_handle = TransferMemory::Create(kernel, system.Memory(), addr, size, - static_cast(perms)); + R_UNLESS(trmem_reservation.Succeeded(), ResultLimitReached); - if (const auto reserve_result{transfer_mem_handle->Reserve()}; reserve_result.IsError()) { - return reserve_result; - } + // Create the transfer memory. + KTransferMemory* trmem = KTransferMemory::Create(kernel); + R_UNLESS(trmem != nullptr, ResultOutOfResource); - auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); - const auto result{handle_table.Create(transfer_mem_handle.get())}; - if (result.Failed()) { - return result.Code(); - } + // Ensure the only reference is in the handle table when we're done. + SCOPE_EXIT({ trmem->Close(); }); + + // Ensure that the region is in range. + R_UNLESS(process.PageTable().Contains(address, size), ResultInvalidCurrentMemory); + + // Initialize the transfer memory. + R_TRY(trmem->Initialize(address, size, map_perm)); + + // Commit the reservation. trmem_reservation.Commit(); - *handle = *result; + // Register the transfer memory. + KTransferMemory::Register(kernel, trmem); + + // Add the transfer memory to the handle table. + R_TRY(handle_table.Add(out, trmem)); + return RESULT_SUCCESS; } -static ResultCode CreateTransferMemory32(Core::System& system, Handle* handle, u32 addr, u32 size, - u32 permissions) { - return CreateTransferMemory(system, handle, addr, size, permissions); +static ResultCode CreateTransferMemory32(Core::System& system, Handle* out, u32 address, u32 size, + MemoryPermission map_perm) { + return CreateTransferMemory(system, out, address, size, map_perm); } static ResultCode GetThreadCoreMask(Core::System& system, Handle thread_handle, s32* out_core_id, diff --git a/src/core/hle/kernel/svc_wrap.h b/src/core/hle/kernel/svc_wrap.h index 96afd544b..819eadfbb 100644 --- a/src/core/hle/kernel/svc_wrap.h +++ b/src/core/hle/kernel/svc_wrap.h @@ -273,11 +273,12 @@ void SvcWrap64(Core::System& system) { FuncReturn(system, retval); } -template +// Used by CreateTransferMemory +template void SvcWrap64(Core::System& system) { u32 param_1 = 0; const u32 retval = func(system, ¶m_1, Param(system, 1), Param(system, 2), - static_cast(Param(system, 3))) + static_cast(Param(system, 3))) .raw; system.CurrentArmInterface().SetReg(1, param_1); @@ -586,11 +587,12 @@ void SvcWrap32(Core::System& system) { } // Used by CreateTransferMemory32 -template +template void SvcWrap32(Core::System& system) { Handle handle = 0; - const u32 retval = - func(system, &handle, Param32(system, 1), Param32(system, 2), Param32(system, 3)).raw; + const u32 retval = func(system, &handle, Param32(system, 1), Param32(system, 2), + static_cast(Param32(system, 3))) + .raw; system.CurrentArmInterface().SetReg(1, handle); FuncReturn(system, retval); } diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 937020a6e..47d194119 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -16,10 +16,10 @@ #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_readable_event.h" +#include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" -#include "core/hle/kernel/transfer_memory.h" #include "core/hle/service/acc/profile_manager.h" #include "core/hle/service/am/am.h" #include "core/hle/service/am/applet_ae.h" @@ -42,6 +42,7 @@ #include "core/hle/service/set/set.h" #include "core/hle/service/sm/sm.h" #include "core/hle/service/vi/vi.h" +#include "core/memory.h" namespace Service::AM { @@ -1248,16 +1249,16 @@ void ILibraryAppletCreator::CreateHandleStorage(Kernel::HLERequestContext& ctx) } auto transfer_mem = - system.CurrentProcess()->GetHandleTable().Get(handle); + system.CurrentProcess()->GetHandleTable().GetObject(handle); - if (transfer_mem == nullptr) { + if (transfer_mem.IsNull()) { LOG_ERROR(Service_AM, "transfer_mem is a nullptr for handle={:08X}", handle); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(RESULT_UNKNOWN); return; } - const u8* const mem_begin = transfer_mem->GetPointer(); + const u8* const mem_begin = system.Memory().GetPointer(transfer_mem->GetSourceAddress()); const u8* const mem_end = mem_begin + transfer_mem->GetSize(); std::vector memory{mem_begin, mem_end}; diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index fbac7b2cf..184030a8e 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -13,7 +13,7 @@ namespace Kernel { class KernelCore; -class TransferMemory; +class KTransferMemory; } // namespace Kernel namespace Service::NVFlinger { diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index 6b3ebeb8f..d831aa214 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -16,14 +16,15 @@ #include "core/hle/kernel/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" #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/transfer_memory.h" #include "core/hle/service/hid/errors.h" #include "core/hle/service/hid/hid.h" #include "core/hle/service/hid/irs.h" #include "core/hle/service/hid/xcd.h" #include "core/hle/service/service.h" +#include "core/memory.h" #include "core/hle/service/hid/controllers/console_sixaxis.h" #include "core/hle/service/hid/controllers/controller_base.h" @@ -1493,20 +1494,20 @@ void Hid::InitializeSevenSixAxisSensor(Kernel::HLERequestContext& ctx) { ASSERT_MSG(t_mem_1_size == 0x1000, "t_mem_1_size is not 0x1000 bytes"); ASSERT_MSG(t_mem_2_size == 0x7F000, "t_mem_2_size is not 0x7F000 bytes"); - auto t_mem_1 = - system.CurrentProcess()->GetHandleTable().Get(t_mem_1_handle); + auto t_mem_1 = system.CurrentProcess()->GetHandleTable().GetObject( + t_mem_1_handle); - if (t_mem_1 == nullptr) { + if (t_mem_1.IsNull()) { LOG_ERROR(Service_HID, "t_mem_1 is a nullptr for handle=0x{:08X}", t_mem_1_handle); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(RESULT_UNKNOWN); return; } - auto t_mem_2 = - system.CurrentProcess()->GetHandleTable().Get(t_mem_2_handle); + auto t_mem_2 = system.CurrentProcess()->GetHandleTable().GetObject( + t_mem_2_handle); - if (t_mem_2 == nullptr) { + if (t_mem_2.IsNull()) { LOG_ERROR(Service_HID, "t_mem_2 is a nullptr for handle=0x{:08X}", t_mem_2_handle); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(RESULT_UNKNOWN); @@ -1521,7 +1522,7 @@ void Hid::InitializeSevenSixAxisSensor(Kernel::HLERequestContext& ctx) { .ActivateController(); applet_resource->GetController(HidController::ConsoleSixAxisSensor) - .SetTransferMemoryPointer(t_mem_1->GetPointer()); + .SetTransferMemoryPointer(system.Memory().GetPointer(t_mem_1->GetSourceAddress())); LOG_WARNING(Service_HID, "called, t_mem_1_handle=0x{:08X}, t_mem_2_handle=0x{:08X}, " From 126aaeb6d30c4f99c7ee28e38de00d2994bf8853 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 17 Apr 2021 22:21:59 -0700 Subject: [PATCH 36/82] hle: kernel: svc: Use new handle table API for Process. --- src/core/hle/kernel/svc.cpp | 25 ++++++++++++------------- src/core/hle/kernel/svc_wrap.h | 8 +++++--- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 0b7eb0740..790839a4b 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -686,7 +686,7 @@ static void OutputDebugString(Core::System& system, VAddr address, u64 len) { } /// Gets system/memory information for the current process -static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, u64 handle, +static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, Handle handle, u64 info_sub_id) { std::lock_guard lock{HLE::g_hle_lock}; LOG_TRACE(Kernel_SVC, "called info_id=0x{:X}, info_sub_id=0x{:X}, handle=0x{:08X}", info_id, @@ -752,10 +752,9 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, u64 ha return ResultInvalidEnumValue; } - const auto& current_process_handle_table = - system.Kernel().CurrentProcess()->GetHandleTable(); - const auto process = current_process_handle_table.Get(static_cast(handle)); - if (!process) { + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); + KScopedAutoObject process = handle_table.GetObject(handle); + if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Process is not valid! info_id={}, info_sub_id={}, handle={:08X}", info_id, info_sub_id, handle); return ResultInvalidHandle; @@ -1287,8 +1286,8 @@ static ResultCode QueryProcessMemory(Core::System& system, VAddr memory_info_add std::lock_guard lock{HLE::g_hle_lock}; LOG_TRACE(Kernel_SVC, "called process=0x{:08X} address={:X}", process_handle, address); const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - auto process = handle_table.Get(process_handle); - if (!process) { + KScopedAutoObject process = handle_table.GetObject(process_handle); + if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", process_handle); return ResultInvalidHandle; @@ -1369,8 +1368,8 @@ static ResultCode MapProcessCodeMemory(Core::System& system, Handle process_hand } const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - auto process = handle_table.Get(process_handle); - if (!process) { + KScopedAutoObject process = handle_table.GetObject(process_handle); + if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Invalid process handle specified (handle=0x{:08X}).", process_handle); return ResultInvalidHandle; @@ -1437,8 +1436,8 @@ static ResultCode UnmapProcessCodeMemory(Core::System& system, Handle process_ha } const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - auto process = handle_table.Get(process_handle); - if (!process) { + KScopedAutoObject process = handle_table.GetObject(process_handle); + if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Invalid process handle specified (handle=0x{:08X}).", process_handle); return ResultInvalidHandle; @@ -2100,8 +2099,8 @@ static ResultCode GetProcessInfo(Core::System& system, u64* out, Handle process_ }; const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - const auto process = handle_table.Get(process_handle); - if (!process) { + KScopedAutoObject process = handle_table.GetObject(process_handle); + if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", process_handle); return ResultInvalidHandle; diff --git a/src/core/hle/kernel/svc_wrap.h b/src/core/hle/kernel/svc_wrap.h index 819eadfbb..61986bb52 100644 --- a/src/core/hle/kernel/svc_wrap.h +++ b/src/core/hle/kernel/svc_wrap.h @@ -252,11 +252,13 @@ void SvcWrap64(Core::System& system) { .raw); } -template +// Used by GetInfo +template void SvcWrap64(Core::System& system) { u64 param_1 = 0; - const u32 retval = - func(system, ¶m_1, Param(system, 1), Param(system, 2), Param(system, 3)).raw; + const u32 retval = func(system, ¶m_1, Param(system, 1), + static_cast(Param(system, 2)), Param(system, 3)) + .raw; system.CurrentArmInterface().SetReg(1, param_1); FuncReturn(system, retval); From 674122038ad01aae7eb4b6eff604f94fb8864bd4 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 17 Apr 2021 23:38:20 -0700 Subject: [PATCH 37/82] hle: kernel: svc: Migrate WaitSynchronization. --- src/core/hle/kernel/handle_table.h | 55 ++++++++++++++++++++++ src/core/hle/kernel/svc.cpp | 74 ++++++++++-------------------- 2 files changed, 80 insertions(+), 49 deletions(-) diff --git a/src/core/hle/kernel/handle_table.h b/src/core/hle/kernel/handle_table.h index d57188844..d6abdcd47 100644 --- a/src/core/hle/kernel/handle_table.h +++ b/src/core/hle/kernel/handle_table.h @@ -10,6 +10,7 @@ #include "common/common_types.h" #include "core/hle/kernel/k_auto_object.h" +#include "core/hle/kernel/k_spin_lock.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/object.h" #include "core/hle/result.h" @@ -110,6 +111,16 @@ public: return DynamicObjectCast(GetGeneric(handle)); } + template + KAutoObject* GetObjectImpl(Handle handle) const { + if (!IsValid(handle)) { + return nullptr; + } + + auto* obj = objects_new[static_cast(handle >> 15)]; + return obj->DynamicCast(); + } + template KScopedAutoObject GetObject(Handle handle) const { if (handle == CurrentThread) { @@ -148,6 +159,48 @@ public: ResultCode Add(Handle* out_handle, KAutoObject* obj, u16 type); + template + bool GetMultipleObjects(T** out, const Handle* handles, size_t num_handles) const { + // Try to convert and open all the handles. + size_t num_opened; + { + // Lock the table. + KScopedSpinLock lk(lock); + for (num_opened = 0; num_opened < num_handles; num_opened++) { + // Get the current handle. + const auto cur_handle = handles[num_opened]; + + // Get the object for the current handle. + KAutoObject* cur_object = this->GetObjectImpl(cur_handle); + if (cur_object == nullptr) { + break; + } + + // Cast the current object to the desired type. + T* cur_t = cur_object->DynamicCast(); + if (cur_t == nullptr) { + break; + } + + // Open a reference to the current object. + cur_t->Open(); + out[num_opened] = cur_t; + } + } + + // If we converted every object, succeed. + if (num_opened == num_handles) { + return true; + } + + // If we didn't convert entry object, close the ones we opened. + for (size_t i = 0; i < num_opened; i++) { + out[i]->Close(); + } + + return false; + } + private: /// Stores the Object referenced by the handle or null if the slot is empty. std::array, MAX_COUNT> objects; @@ -175,6 +228,8 @@ private: /// Head of the free slots linked list. u16 next_free_slot = 0; + mutable KSpinLock lock; + /// Underlying kernel instance that this handle table operates under. KernelCore& kernel; }; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 790839a4b..9e8184758 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -430,65 +430,41 @@ static ResultCode GetProcessId32(Core::System& system, u32* out_process_id_low, /// Wait for the given handles to synchronize, timeout after the specified nanoseconds static ResultCode WaitSynchronization(Core::System& system, s32* index, VAddr handles_address, - u64 handle_count, s64 nano_seconds) { - LOG_TRACE(Kernel_SVC, "called handles_address=0x{:X}, handle_count={}, nano_seconds={}", - handles_address, handle_count, nano_seconds); + u64 num_handles, s64 nano_seconds) { + LOG_TRACE(Kernel_SVC, "called handles_address=0x{:X}, num_handles={}, nano_seconds={}", + handles_address, num_handles, nano_seconds); - auto& memory = system.Memory(); - if (!memory.IsValidVirtualAddress(handles_address)) { - LOG_ERROR(Kernel_SVC, - "Handle address is not a valid virtual address, handle_address=0x{:016X}", - handles_address); - return ResultInvalidPointer; - } - - static constexpr u64 MaxHandles = 0x40; - - if (handle_count > MaxHandles) { - LOG_ERROR(Kernel_SVC, "Handle count specified is too large, expected {} but got {}", - MaxHandles, handle_count); - return ResultOutOfRange; - } + // Ensure number of handles is valid. + R_UNLESS(0 <= num_handles && num_handles <= ArgumentHandleCountMax, ResultOutOfRange); auto& kernel = system.Kernel(); - std::vector objects(handle_count); + std::vector objs(num_handles); const auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); + Handle* handles = system.Memory().GetPointer(handles_address); - for (u64 i = 0; i < handle_count; ++i) { - const Handle handle = memory.Read32(handles_address + i * sizeof(Handle)); - - bool succeeded{}; - { - auto object = handle_table.Get(handle); - if (object) { - objects[i] = object; - succeeded = true; - } - } - - // TODO(bunnei): WORKAROUND WHILE WE HAVE TWO HANDLE TABLES - if (!succeeded) { - { - auto object = handle_table.GetObject(handle); - - if (object.IsNull()) { - LOG_ERROR(Kernel_SVC, "Object is a nullptr"); - return ResultInvalidHandle; - } - - objects[i] = object.GetPointerUnsafe(); - succeeded = true; - } - } + // Copy user handles. + if (num_handles > 0) { + // Convert the handles to objects. + R_UNLESS(handle_table.GetMultipleObjects(objs.data(), handles, + num_handles), + ResultInvalidHandle); } - return KSynchronizationObject::Wait(kernel, index, objects.data(), - static_cast(objects.size()), nano_seconds); + + // Ensure handles are closed when we're done. + SCOPE_EXIT({ + for (u64 i = 0; i < num_handles; ++i) { + objs[i]->Close(); + } + }); + + return KSynchronizationObject::Wait(kernel, index, objs.data(), static_cast(objs.size()), + nano_seconds); } static ResultCode WaitSynchronization32(Core::System& system, u32 timeout_low, u32 handles_address, - s32 handle_count, u32 timeout_high, s32* index) { + s32 num_handles, u32 timeout_high, s32* index) { const s64 nano_seconds{(static_cast(timeout_high) << 32) | static_cast(timeout_low)}; - return WaitSynchronization(system, index, handles_address, handle_count, nano_seconds); + return WaitSynchronization(system, index, handles_address, num_handles, nano_seconds); } /// Resumes a thread waiting on WaitSynchronization From b57c5a9b54b23a348d7e80e51943f27a54fb8c2f Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 20 Apr 2021 21:28:11 -0700 Subject: [PATCH 38/82] hle: kernel: Migrate KResourceLimit to KAutoObject. --- src/core/hle/kernel/init/init_slab_setup.cpp | 6 +- src/core/hle/kernel/k_resource_limit.cpp | 14 +- src/core/hle/kernel/k_resource_limit.h | 20 ++- .../kernel/k_scoped_resource_reservation.h | 8 +- src/core/hle/kernel/k_shared_memory.cpp | 28 +++- src/core/hle/kernel/k_shared_memory.h | 2 +- src/core/hle/kernel/k_transfer_memory.h | 5 +- src/core/hle/kernel/kernel.cpp | 24 +++- src/core/hle/kernel/kernel.h | 40 +++--- src/core/hle/kernel/process.cpp | 20 ++- src/core/hle/kernel/process.h | 8 +- src/core/hle/kernel/svc.cpp | 127 +++++++++--------- src/core/hle/kernel/svc_wrap.h | 19 ++- 13 files changed, 198 insertions(+), 123 deletions(-) diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index ce7a24c40..a5ddd7344 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -12,11 +12,12 @@ #include "core/hle/kernel/k_event.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" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_shared_memory.h" -#include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/memory_types.h" #include "core/hle/kernel/process.h" #include "core/memory.h" @@ -31,7 +32,8 @@ namespace Kernel::Init { HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ HANDLER(KTransferMemory, (SLAB_COUNT(KTransferMemory)), ##__VA_ARGS__) \ HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) \ - HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) + HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) \ + HANDLER(KResourceLimit, (SLAB_COUNT(KResourceLimit)), ##__VA_ARGS__) namespace { diff --git a/src/core/hle/kernel/k_resource_limit.cpp b/src/core/hle/kernel/k_resource_limit.cpp index d05b34ea3..ad5095bfd 100644 --- a/src/core/hle/kernel/k_resource_limit.cpp +++ b/src/core/hle/kernel/k_resource_limit.cpp @@ -10,10 +10,16 @@ namespace Kernel { constexpr s64 DefaultTimeout = 10000000000; // 10 seconds -KResourceLimit::KResourceLimit(KernelCore& kernel, const Core::Timing::CoreTiming& core_timing_) - : Object{kernel}, lock{kernel}, cond_var{kernel}, core_timing(core_timing_) {} +KResourceLimit::KResourceLimit(KernelCore& kernel) + : KAutoObjectWithSlabHeapAndContainer{kernel}, lock{kernel}, cond_var{kernel} {} KResourceLimit::~KResourceLimit() = default; +void KResourceLimit::Initialize(const Core::Timing::CoreTiming* core_timing_) { + core_timing = core_timing_; +} + +void KResourceLimit::Finalize() {} + s64 KResourceLimit::GetLimitValue(LimitableResource which) const { const auto index = static_cast(which); s64 value{}; @@ -78,7 +84,7 @@ ResultCode KResourceLimit::SetLimitValue(LimitableResource which, s64 value) { } bool KResourceLimit::Reserve(LimitableResource which, s64 value) { - return Reserve(which, value, core_timing.GetGlobalTimeNs().count() + DefaultTimeout); + return Reserve(which, value, core_timing->GetGlobalTimeNs().count() + DefaultTimeout); } bool KResourceLimit::Reserve(LimitableResource which, s64 value, s64 timeout) { @@ -109,7 +115,7 @@ bool KResourceLimit::Reserve(LimitableResource which, s64 value, s64 timeout) { } if (current_hints[index] + value <= limit_values[index] && - (timeout < 0 || core_timing.GetGlobalTimeNs().count() < timeout)) { + (timeout < 0 || core_timing->GetGlobalTimeNs().count() < timeout)) { waiter_count++; cond_var.Wait(&lock, timeout); waiter_count--; diff --git a/src/core/hle/kernel/k_resource_limit.h b/src/core/hle/kernel/k_resource_limit.h index 4542317d0..483c66c33 100644 --- a/src/core/hle/kernel/k_resource_limit.h +++ b/src/core/hle/kernel/k_resource_limit.h @@ -32,10 +32,16 @@ constexpr bool IsValidResourceType(LimitableResource type) { return type < LimitableResource::Count; } -class KResourceLimit final : public Object { +class KResourceLimit final + : public KAutoObjectWithSlabHeapAndContainer { + KERNEL_AUTOOBJECT_TRAITS(KResourceLimit, KAutoObject); + public: - explicit KResourceLimit(KernelCore& kernel, const Core::Timing::CoreTiming& core_timing_); - ~KResourceLimit(); + explicit KResourceLimit(KernelCore& kernel); + virtual ~KResourceLimit(); + + void Initialize(const Core::Timing::CoreTiming* core_timing_); + virtual void Finalize() override; s64 GetLimitValue(LimitableResource which) const; s64 GetCurrentValue(LimitableResource which) const; @@ -49,6 +55,10 @@ public: void Release(LimitableResource which, s64 value); void Release(LimitableResource which, s64 value, s64 hint); + static void PostDestroy([[maybe_unused]] uintptr_t arg) {} + + // DEPRECATED + std::string GetTypeName() const override { return "KResourceLimit"; } @@ -61,8 +71,6 @@ public: return HANDLE_TYPE; } - virtual void Finalize() override {} - private: using ResourceArray = std::array(LimitableResource::Count)>; ResourceArray limit_values{}; @@ -72,6 +80,6 @@ private: mutable KLightLock lock; s32 waiter_count{}; KLightConditionVariable cond_var; - const Core::Timing::CoreTiming& core_timing; + const Core::Timing::CoreTiming* core_timing{}; }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_scoped_resource_reservation.h b/src/core/hle/kernel/k_scoped_resource_reservation.h index c5deca00b..b160587c5 100644 --- a/src/core/hle/kernel/k_scoped_resource_reservation.h +++ b/src/core/hle/kernel/k_scoped_resource_reservation.h @@ -15,8 +15,7 @@ namespace Kernel { class KScopedResourceReservation { public: - explicit KScopedResourceReservation(std::shared_ptr l, LimitableResource r, - s64 v, s64 timeout) + explicit KScopedResourceReservation(KResourceLimit* l, LimitableResource r, s64 v, s64 timeout) : resource_limit(std::move(l)), value(v), resource(r) { if (resource_limit && value) { success = resource_limit->Reserve(resource, value, timeout); @@ -25,8 +24,7 @@ public: } } - explicit KScopedResourceReservation(std::shared_ptr l, LimitableResource r, - s64 v = 1) + explicit KScopedResourceReservation(KResourceLimit* l, LimitableResource r, s64 v = 1) : resource_limit(std::move(l)), value(v), resource(r) { if (resource_limit && value) { success = resource_limit->Reserve(resource, value); @@ -58,7 +56,7 @@ public: } private: - std::shared_ptr resource_limit; + KResourceLimit* resource_limit{}; s64 value; LimitableResource resource; bool success; diff --git a/src/core/hle/kernel/k_shared_memory.cpp b/src/core/hle/kernel/k_shared_memory.cpp index 9e20c2350..e91bc94bd 100644 --- a/src/core/hle/kernel/k_shared_memory.cpp +++ b/src/core/hle/kernel/k_shared_memory.cpp @@ -8,6 +8,7 @@ #include "core/hle/kernel/k_scoped_resource_reservation.h" #include "core/hle/kernel/k_shared_memory.h" #include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/svc_results.h" namespace Kernel { @@ -22,12 +23,7 @@ ResultCode KSharedMemory::Initialize(KernelCore& kernel_, Core::DeviceMemory& de KMemoryPermission owner_permission_, KMemoryPermission user_permission_, PAddr physical_address_, std::size_t size_, std::string name_) { - - resource_limit = kernel_.GetSystemResourceLimit(); - KScopedResourceReservation memory_reservation(resource_limit, LimitableResource::PhysicalMemory, - size_); - ASSERT(memory_reservation.Succeeded()); - + // Set members. owner_process = owner_process_; device_memory = &device_memory_; page_list = std::move(page_list_); @@ -36,9 +32,27 @@ ResultCode KSharedMemory::Initialize(KernelCore& kernel_, Core::DeviceMemory& de physical_address = physical_address_; size = size_; name = name_; + + // Get the resource limit. + KResourceLimit* reslimit = kernel.GetSystemResourceLimit(); + + // Reserve memory for ourselves. + KScopedResourceReservation memory_reservation(reslimit, LimitableResource::PhysicalMemory, + size_); + R_UNLESS(memory_reservation.Succeeded(), ResultLimitReached); + + // Commit our reservation. + memory_reservation.Commit(); + + // Set our resource limit. + resource_limit = reslimit; + resource_limit->Open(); + + // Mark initialized. is_initialized = true; - memory_reservation.Commit(); + // Clear all pages in the memory. + std::memset(device_memory_.GetPointer(physical_address_), 0, size_); return RESULT_SUCCESS; } diff --git a/src/core/hle/kernel/k_shared_memory.h b/src/core/hle/kernel/k_shared_memory.h index aaa773bfc..93153ab20 100644 --- a/src/core/hle/kernel/k_shared_memory.h +++ b/src/core/hle/kernel/k_shared_memory.h @@ -88,7 +88,7 @@ private: KMemoryPermission user_permission{}; PAddr physical_address{}; std::size_t size{}; - std::shared_ptr resource_limit; + KResourceLimit* resource_limit{}; bool is_initialized{}; }; diff --git a/src/core/hle/kernel/k_transfer_memory.h b/src/core/hle/kernel/k_transfer_memory.h index f43725c7f..3c3fa401b 100644 --- a/src/core/hle/kernel/k_transfer_memory.h +++ b/src/core/hle/kernel/k_transfer_memory.h @@ -27,9 +27,7 @@ class KTransferMemory final public: explicit KTransferMemory(KernelCore& kernel); - ~KTransferMemory() override; - - static constexpr HandleType HANDLE_TYPE = HandleType::TransferMemory; + virtual ~KTransferMemory() override; ResultCode Initialize(VAddr address_, std::size_t size_, Svc::MemoryPermission owner_perm_); @@ -67,6 +65,7 @@ public: return GetTypeName(); } + static constexpr HandleType HANDLE_TYPE = HandleType::TransferMemory; HandleType GetHandleType() const override { return HANDLE_TYPE; } diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 472c71cf1..ada993f46 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -74,8 +74,8 @@ struct KernelCore::Impl { Init::InitializeSlabHeaps(system, memory_layout); // Initialize kernel memory and resources. - InitializeMemoryLayout(memory_layout); InitializeSystemResourceLimit(kernel, system.CoreTiming(), memory_layout); + InitializeMemoryLayout(memory_layout); InitializePageSlab(); InitializeSchedulers(); InitializeSuspendThreads(); @@ -126,11 +126,19 @@ struct KernelCore::Impl { exclusive_monitor.reset(); + hid_shared_mem->Close(); hid_shared_mem = nullptr; + + font_shared_mem->Close(); font_shared_mem = nullptr; + + irs_shared_mem->Close(); irs_shared_mem = nullptr; + + time_shared_mem->Close(); time_shared_mem = nullptr; + system_resource_limit->Close(); system_resource_limit = nullptr; // Next host thead ID to use, 0-3 IDs represent core threads, >3 represent others @@ -156,7 +164,9 @@ struct KernelCore::Impl { void InitializeSystemResourceLimit(KernelCore& kernel, const Core::Timing::CoreTiming& core_timing, const KMemoryLayout& memory_layout) { - system_resource_limit = std::make_shared(kernel, core_timing); + system_resource_limit = KResourceLimit::Create(system.Kernel()); + system_resource_limit->Initialize(&core_timing); + const auto [total_size, kernel_size] = memory_layout.GetTotalAndKernelMemorySizes(); // If setting the default system values fails, then something seriously wrong has occurred. @@ -627,11 +637,11 @@ struct KernelCore::Impl { // Lists all processes that exist in the current session. std::vector process_list; - Process* current_process = nullptr; + Process* current_process{}; std::unique_ptr global_scheduler_context; Kernel::TimeManager time_manager; - std::shared_ptr system_resource_limit; + KResourceLimit* system_resource_limit{}; std::shared_ptr preemption_event; @@ -704,7 +714,11 @@ void KernelCore::Shutdown() { impl->Shutdown(); } -std::shared_ptr KernelCore::GetSystemResourceLimit() const { +const KResourceLimit* KernelCore::GetSystemResourceLimit() const { + return impl->system_resource_limit; +} + +KResourceLimit* KernelCore::GetSystemResourceLimit() { return impl->system_resource_limit; } diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index f07f0276e..43e49603b 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -92,7 +92,10 @@ public: void Shutdown(); /// Retrieves a shared pointer to the system resource limit instance. - std::shared_ptr GetSystemResourceLimit() const; + const KResourceLimit* GetSystemResourceLimit() const; + + /// Retrieves a shared pointer to the system resource limit instance. + KResourceLimit* GetSystemResourceLimit(); /// Retrieves a shared pointer to a Thread instance within the thread wakeup handle table. KScopedAutoObject RetrieveThreadFromGlobalHandleTable(Handle handle) const; @@ -263,24 +266,26 @@ public: /// Gets the slab heap for the specified kernel object type. template KSlabHeap& SlabHeap() { - if constexpr (std::is_same_v) { - return slab_heap_container->process; - } else if constexpr (std::is_same_v) { - return slab_heap_container->thread; + if constexpr (std::is_same_v) { + return slab_heap_container->client_session; } else if constexpr (std::is_same_v) { return slab_heap_container->event; - } else if constexpr (std::is_same_v) { - return slab_heap_container->shared_memory; } else if constexpr (std::is_same_v) { return slab_heap_container->linked_list_node; - } else if constexpr (std::is_same_v) { - return slab_heap_container->writeable_event; - } else if constexpr (std::is_same_v) { - return slab_heap_container->client_session; + } else if constexpr (std::is_same_v) { + return slab_heap_container->process; + } else if constexpr (std::is_same_v) { + return slab_heap_container->resource_limit; } else if constexpr (std::is_same_v) { return slab_heap_container->session; + } else if constexpr (std::is_same_v) { + return slab_heap_container->shared_memory; + } else if constexpr (std::is_same_v) { + return slab_heap_container->thread; } else if constexpr (std::is_same_v) { return slab_heap_container->transfer_memory; + } else if constexpr (std::is_same_v) { + return slab_heap_container->writeable_event; } } @@ -315,15 +320,16 @@ private: private: /// Helper to encapsulate all slab heaps in a single heap allocated container struct SlabHeapContainer { - KSlabHeap process; - KSlabHeap thread; - KSlabHeap event; - KSlabHeap shared_memory; - KSlabHeap linked_list_node; - KSlabHeap writeable_event; KSlabHeap client_session; + KSlabHeap event; + KSlabHeap linked_list_node; + KSlabHeap process; + KSlabHeap resource_limit; KSlabHeap session; + KSlabHeap shared_memory; + KSlabHeap thread; KSlabHeap transfer_memory; + KSlabHeap writeable_event; }; std::unique_ptr slab_heap_container; diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 678037923..315640bea 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -138,10 +138,13 @@ ResultCode Process::Initialize(Process* process, Core::System& system, std::stri kernel.AppendNewProcess(process); + // Open a reference to the resource limit. + process->resource_limit->Open(); + return RESULT_SUCCESS; } -std::shared_ptr Process::GetResourceLimit() const { +KResourceLimit* Process::GetResourceLimit() const { return resource_limit; } @@ -166,7 +169,10 @@ u64 Process::GetTotalPhysicalMemoryAvailable() const { const u64 capacity{resource_limit->GetFreeValue(LimitableResource::PhysicalMemory) + page_table->GetTotalHeapSize() + GetSystemResourceSize() + image_size + main_thread_stack_size}; - ASSERT(capacity == kernel.MemoryManager().GetSize(KMemoryManager::Pool::Application)); + if (const auto pool_size = kernel.MemoryManager().GetSize(KMemoryManager::Pool::Application); + capacity != pool_size) { + LOG_WARNING(Kernel, "capacity {} != application pool size {}", capacity, pool_size); + } if (capacity < memory_usage_capacity) { return capacity; } @@ -371,6 +377,16 @@ void Process::PrepareForTermination() { ChangeStatus(ProcessStatus::Exited); } +void Process::Finalize() { + // Release memory to the resource limit. + if (resource_limit != nullptr) { + resource_limit->Close(); + } + + // Perform inherited finalization. + KAutoObjectWithSlabHeapAndContainer::Finalize(); +} + /** * Attempts to find a TLS page that contains a free slot for * use by a thread. diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index df3c7997d..35fe16433 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -171,7 +171,7 @@ public: } /// Gets the resource limit descriptor for this process - std::shared_ptr GetResourceLimit() const; + KResourceLimit* GetResourceLimit() const; /// Gets the ideal CPU core ID for this process u8 GetIdealCoreId() const { @@ -348,9 +348,7 @@ public: static void PostDestroy([[maybe_unused]] uintptr_t arg) {} - virtual void Finalize() override { - UNIMPLEMENTED(); - } + virtual void Finalize(); virtual u64 GetId() const override final { return GetProcessID(); @@ -415,7 +413,7 @@ private: u32 system_resource_size = 0; /// Resource limit descriptor for this process - std::shared_ptr resource_limit; + KResourceLimit* resource_limit{}; /// The ideal CPU core for this process, threads are scheduled on this core by default. u8 ideal_core = 0; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 9e8184758..a78bfd1da 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -153,9 +153,9 @@ ResultVal RetrieveResourceLimitValue(Core::System& system, Handle resource_ const auto* const current_process = system.Kernel().CurrentProcess(); ASSERT(current_process != nullptr); - const auto resource_limit_object = - current_process->GetHandleTable().Get(resource_limit); - if (!resource_limit_object) { + auto resource_limit_object = + current_process->GetHandleTable().GetObject(resource_limit); + if (resource_limit_object.IsNull()) { LOG_ERROR(Kernel_SVC, "Handle to non-existent resource limit instance used. Handle={:08X}", resource_limit); return ResultInvalidHandle; @@ -843,12 +843,10 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, Handle return RESULT_SUCCESS; } - const auto table_result = handle_table.Create(resource_limit.get()); - if (table_result.Failed()) { - return table_result.Code(); - } + Handle handle{}; + R_TRY(handle_table.Add(&handle, resource_limit)); - *result = *table_result; + *result = handle; return RESULT_SUCCESS; } @@ -2093,83 +2091,86 @@ static ResultCode GetProcessInfo(Core::System& system, u64* out, Handle process_ } static ResultCode CreateResourceLimit(Core::System& system, Handle* out_handle) { - std::lock_guard lock{HLE::g_hle_lock}; LOG_DEBUG(Kernel_SVC, "called"); + // Create a new resource limit. auto& kernel = system.Kernel(); - auto resource_limit = std::make_shared(kernel, system.CoreTiming()); + KResourceLimit* resource_limit = KResourceLimit::Create(kernel); + R_UNLESS(resource_limit != nullptr, ResultOutOfResource); - auto* const current_process = kernel.CurrentProcess(); - ASSERT(current_process != nullptr); + // Ensure we don't leak a reference to the limit. + SCOPE_EXIT({ resource_limit->Close(); }); - const auto handle = current_process->GetHandleTable().Create(resource_limit.get()); - if (handle.Failed()) { - return handle.Code(); - } + // Initialize the resource limit. + resource_limit->Initialize(&system.CoreTiming()); + + // Register the limit. + KResourceLimit::Register(kernel, resource_limit); + + // Add the limit to the handle table. + R_TRY(kernel.CurrentProcess()->GetHandleTable().Add(out_handle, resource_limit)); - *out_handle = *handle; return RESULT_SUCCESS; } -static ResultCode GetResourceLimitLimitValue(Core::System& system, u64* out_value, - Handle resource_limit, u32 resource_type) { - LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}", resource_limit, resource_type); +static ResultCode GetResourceLimitLimitValue(Core::System& system, u64* out_limit_value, + Handle resource_limit_handle, + LimitableResource which) { + LOG_DEBUG(Kernel_SVC, "called, resource_limit_handle={:08X}, which={}", resource_limit_handle, + which); - const auto limit_value = RetrieveResourceLimitValue(system, resource_limit, resource_type, - ResourceLimitValueType::LimitValue); - if (limit_value.Failed()) { - return limit_value.Code(); - } + // Validate the resource. + R_UNLESS(IsValidResourceType(which), ResultInvalidEnumValue); + + // Get the resource limit. + auto& kernel = system.Kernel(); + KScopedAutoObject resource_limit = + kernel.CurrentProcess()->GetHandleTable().GetObject(resource_limit_handle); + R_UNLESS(resource_limit.IsNotNull(), ResultInvalidHandle); + + // Get the limit value. + *out_limit_value = resource_limit->GetLimitValue(which); - *out_value = static_cast(*limit_value); return RESULT_SUCCESS; } -static ResultCode GetResourceLimitCurrentValue(Core::System& system, u64* out_value, - Handle resource_limit, u32 resource_type) { - LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}", resource_limit, resource_type); +static ResultCode GetResourceLimitCurrentValue(Core::System& system, u64* out_current_value, + Handle resource_limit_handle, + LimitableResource which) { + LOG_DEBUG(Kernel_SVC, "called, resource_limit_handle={:08X}, which={}", resource_limit_handle, + which); - const auto current_value = RetrieveResourceLimitValue(system, resource_limit, resource_type, - ResourceLimitValueType::CurrentValue); - if (current_value.Failed()) { - return current_value.Code(); - } + // Validate the resource. + R_UNLESS(IsValidResourceType(which), ResultInvalidEnumValue); + + // Get the resource limit. + auto& kernel = system.Kernel(); + KScopedAutoObject resource_limit = + kernel.CurrentProcess()->GetHandleTable().GetObject(resource_limit_handle); + R_UNLESS(resource_limit.IsNotNull(), ResultInvalidHandle); + + // Get the current value. + *out_current_value = resource_limit->GetCurrentValue(which); - *out_value = static_cast(*current_value); return RESULT_SUCCESS; } -static ResultCode SetResourceLimitLimitValue(Core::System& system, Handle resource_limit, - u32 resource_type, u64 value) { - LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}, Value={}", resource_limit, - resource_type, value); +static ResultCode SetResourceLimitLimitValue(Core::System& system, Handle resource_limit_handle, + LimitableResource which, u64 limit_value) { + LOG_DEBUG(Kernel_SVC, "called, resource_limit_handle={:08X}, which={}, limit_value={}", + resource_limit_handle, which, limit_value); - const auto type = static_cast(resource_type); - if (!IsValidResourceType(type)) { - LOG_ERROR(Kernel_SVC, "Invalid resource limit type: '{}'", resource_type); - return ResultInvalidEnumValue; - } + // Validate the resource. + R_UNLESS(IsValidResourceType(which), ResultInvalidEnumValue); - auto* const current_process = system.Kernel().CurrentProcess(); - ASSERT(current_process != nullptr); + // Get the resource limit. + auto& kernel = system.Kernel(); + KScopedAutoObject resource_limit = + kernel.CurrentProcess()->GetHandleTable().GetObject(resource_limit_handle); + R_UNLESS(resource_limit.IsNotNull(), ResultInvalidHandle); - auto resource_limit_object = - current_process->GetHandleTable().Get(resource_limit); - if (!resource_limit_object) { - LOG_ERROR(Kernel_SVC, "Handle to non-existent resource limit instance used. Handle={:08X}", - resource_limit); - return ResultInvalidHandle; - } - - const auto set_result = resource_limit_object->SetLimitValue(type, static_cast(value)); - if (set_result.IsError()) { - LOG_ERROR(Kernel_SVC, - "Attempted to lower resource limit ({}) for category '{}' below its current " - "value ({})", - resource_limit_object->GetLimitValue(type), resource_type, - resource_limit_object->GetCurrentValue(type)); - return set_result; - } + // Set the limit value. + R_TRY(resource_limit->SetLimitValue(which, limit_value)); return RESULT_SUCCESS; } diff --git a/src/core/hle/kernel/svc_wrap.h b/src/core/hle/kernel/svc_wrap.h index 61986bb52..d3df25d0f 100644 --- a/src/core/hle/kernel/svc_wrap.h +++ b/src/core/hle/kernel/svc_wrap.h @@ -154,15 +154,28 @@ void SvcWrap64(Core::System& system) { FuncReturn(system, retval); } +// Used by GetResourceLimitLimitValue. +template +void SvcWrap64(Core::System& system) { + u64 param_1 = 0; + const u32 retval = func(system, ¶m_1, static_cast(Param(system, 1)), + static_cast(Param(system, 2))) + .raw; + + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); +} + template void SvcWrap64(Core::System& system) { FuncReturn(system, func(system, static_cast(Param(system, 0)), Param(system, 1)).raw); } -template +// Used by SetResourceLimitLimitValue +template void SvcWrap64(Core::System& system) { - FuncReturn(system, func(system, static_cast(Param(system, 0)), - static_cast(Param(system, 1)), Param(system, 2)) + FuncReturn(system, func(system, static_cast(Param(system, 0)), + static_cast(Param(system, 1)), Param(system, 2)) .raw); } From aa2844bcf9b2b9bca2ce263270b963ffd13b05e7 Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 20 Apr 2021 22:18:56 -0700 Subject: [PATCH 39/82] hle: kernel: HandleTable: Remove deprecated APIs. --- src/core/hle/kernel/handle_table.cpp | 77 ++++------------------------ src/core/hle/kernel/handle_table.h | 32 ++---------- src/core/hle/kernel/hle_ipc.cpp | 8 +-- src/core/hle/kernel/hle_ipc.h | 10 ++-- src/core/hle/kernel/svc.cpp | 2 +- src/yuzu/debugger/wait_tree.cpp | 2 +- src/yuzu/debugger/wait_tree.h | 8 +-- 7 files changed, 28 insertions(+), 111 deletions(-) diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index 9291f0a76..cd752da4e 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -47,50 +47,6 @@ ResultCode HandleTable::SetSize(s32 handle_table_size) { return RESULT_SUCCESS; } -ResultVal HandleTable::Create(Object* obj) { - DEBUG_ASSERT(obj != nullptr); - - switch (obj->GetHandleType()) { - case HandleType::SharedMemory: - case HandleType::Thread: - case HandleType::Event: - case HandleType::Process: - case HandleType::ReadableEvent: - case HandleType::WritableEvent: - case HandleType::ClientSession: - case HandleType::ServerSession: - case HandleType::Session: - case HandleType::TransferMemory: { - Handle handle{}; - Add(&handle, reinterpret_cast(obj), {}); - return MakeResult(handle); - } - default: - break; - } - - const u16 slot = next_free_slot; - if (slot >= table_size) { - LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use."); - return ResultOutOfHandles; - } - next_free_slot = generations[slot]; - - const u16 generation = next_generation++; - - // Overflow count so it fits in the 15 bits dedicated to the generation in the handle. - // Horizon OS uses zero to represent an invalid handle, so skip to 1. - if (next_generation >= (1 << 15)) { - next_generation = 1; - } - - generations[slot] = generation; - objects[slot] = std::move(SharedFrom(obj)); - - Handle handle = generation | (slot << 15); - return MakeResult(handle); -} - ResultCode HandleTable::Add(Handle* out_handle, KAutoObject* obj, u16 type) { ASSERT(obj != nullptr); @@ -110,7 +66,7 @@ ResultCode HandleTable::Add(Handle* out_handle, KAutoObject* obj, u16 type) { } generations[slot] = generation; - objects_new[slot] = obj; + objects[slot] = obj; obj->Open(); *out_handle = generation | (slot << 15); @@ -119,12 +75,16 @@ ResultCode HandleTable::Add(Handle* out_handle, KAutoObject* obj, u16 type) { } ResultVal HandleTable::Duplicate(Handle handle) { - auto object = GetGeneric(handle); - if (object == nullptr) { + auto object = GetObject(handle); + if (object.IsNull()) { LOG_ERROR(Kernel, "Tried to duplicate invalid handle: {:08X}", handle); return ResultInvalidHandle; } - return Create(object); + + Handle out_handle{}; + R_TRY(Add(&out_handle, object.GetPointerUnsafe())); + + return MakeResult(out_handle); } bool HandleTable::Remove(Handle handle) { @@ -139,12 +99,7 @@ bool HandleTable::Remove(Handle handle) { objects[slot]->Close(); } - if (objects_new[slot]) { - objects_new[slot]->Close(); - } - objects[slot] = nullptr; - objects_new[slot] = nullptr; generations[slot] = next_free_slot; next_free_slot = slot; @@ -155,28 +110,14 @@ bool HandleTable::Remove(Handle handle) { bool HandleTable::IsValid(Handle handle) const { const std::size_t slot = GetSlot(handle); const u16 generation = GetGeneration(handle); - const bool is_object_valid = (objects[slot] != nullptr) || (objects_new[slot] != nullptr); + const bool is_object_valid = (objects[slot] != nullptr); return slot < table_size && is_object_valid && generations[slot] == generation; } -Object* HandleTable::GetGeneric(Handle handle) const { - if (handle == CurrentThread) { - return (kernel.CurrentScheduler()->GetCurrentThread()); - } else if (handle == CurrentProcess) { - return (kernel.CurrentProcess()); - } - - if (!IsValid(handle)) { - return nullptr; - } - return objects[GetSlot(handle)].get(); -} - void HandleTable::Clear() { for (u16 i = 0; i < table_size; ++i) { generations[i] = static_cast(i + 1); objects[i] = nullptr; - objects_new[i] = nullptr; } next_free_slot = 0; } diff --git a/src/core/hle/kernel/handle_table.h b/src/core/hle/kernel/handle_table.h index d6abdcd47..2e0b2d8b8 100644 --- a/src/core/hle/kernel/handle_table.h +++ b/src/core/hle/kernel/handle_table.h @@ -70,13 +70,6 @@ public: */ ResultCode SetSize(s32 handle_table_size); - /** - * Allocates a handle for the given object. - * @return The created Handle or one of the following errors: - * - `ERR_HANDLE_TABLE_FULL`: the maximum number of handles has been exceeded. - */ - ResultVal Create(Object* obj); - /** * Returns a new handle that points to the same object as the passed in handle. * @return The duplicated Handle or one of the following errors: @@ -95,29 +88,13 @@ public: /// Checks if a handle is valid and points to an existing object. bool IsValid(Handle handle) const; - /** - * Looks up a handle. - * @return Pointer to the looked-up object, or `nullptr` if the handle is not valid. - */ - Object* GetGeneric(Handle handle) const; - - /** - * Looks up a handle while verifying its type. - * @return Pointer to the looked-up object, or `nullptr` if the handle is not valid or its - * type differs from the requested one. - */ - template - T* Get(Handle handle) const { - return DynamicObjectCast(GetGeneric(handle)); - } - template KAutoObject* GetObjectImpl(Handle handle) const { if (!IsValid(handle)) { return nullptr; } - auto* obj = objects_new[static_cast(handle >> 15)]; + auto* obj = objects[static_cast(handle >> 15)]; return obj->DynamicCast(); } @@ -133,7 +110,7 @@ public: return nullptr; } - auto* obj = objects_new[static_cast(handle >> 15)]; + auto* obj = objects[static_cast(handle >> 15)]; return obj->DynamicCast(); } @@ -142,7 +119,7 @@ public: if (!IsValid(handle)) { return nullptr; } - auto* obj = objects_new[static_cast(handle >> 15)]; + auto* obj = objects[static_cast(handle >> 15)]; return obj->DynamicCast(); } @@ -203,8 +180,7 @@ public: private: /// Stores the Object referenced by the handle or null if the slot is empty. - std::array, MAX_COUNT> objects; - std::array objects_new{}; + std::array objects{}; /** * The value of `next_generation` when the handle was created, used to check for validity. For diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index d647d9dd3..9e1e63204 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -74,12 +74,12 @@ void HLERequestContext::ParseCommandBuffer(const HandleTable& handle_table, u32_ for (u32 handle = 0; handle < handle_descriptor_header->num_handles_to_copy; ++handle) { const u32 copy_handle{rp.Pop()}; copy_handles.push_back(copy_handle); - copy_objects.push_back(handle_table.GetGeneric(copy_handle)); + copy_objects.push_back(handle_table.GetObject(copy_handle).GetPointerUnsafe()); } for (u32 handle = 0; handle < handle_descriptor_header->num_handles_to_move; ++handle) { const u32 move_handle{rp.Pop()}; move_handles.push_back(move_handle); - move_objects.push_back(handle_table.GetGeneric(move_handle)); + move_objects.push_back(handle_table.GetObject(move_handle).GetPointerUnsafe()); } } else { // For responses we just ignore the handles, they're empty and will be populated when @@ -220,12 +220,12 @@ ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(KThread& thread) { // for specific values in each of these descriptors. for (auto& object : copy_objects) { ASSERT(object != nullptr); - dst_cmdbuf[current_offset++] = handle_table.Create(object).Unwrap(); + R_TRY(handle_table.Add(&dst_cmdbuf[current_offset++], object)); } for (auto& object : move_objects) { ASSERT(object != nullptr); - dst_cmdbuf[current_offset++] = handle_table.Create(object).Unwrap(); + R_TRY(handle_table.Add(&dst_cmdbuf[current_offset++], object)); } } diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index dc5c3b47d..b7484c445 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -16,7 +16,7 @@ #include "common/concepts.h" #include "common/swap.h" #include "core/hle/ipc.h" -#include "core/hle/kernel/object.h" +#include "core/hle/kernel/k_auto_object.h" union ResultCode; @@ -228,11 +228,11 @@ public: return DynamicObjectCast(move_objects.at(index)); } - void AddMoveObject(Object* object) { + void AddMoveObject(KAutoObject* object) { move_objects.emplace_back(object); } - void AddCopyObject(Object* object) { + void AddCopyObject(KAutoObject* object) { copy_objects.emplace_back(object); } @@ -292,8 +292,8 @@ private: // TODO(yuriks): Check common usage of this and optimize size accordingly boost::container::small_vector move_handles; boost::container::small_vector copy_handles; - boost::container::small_vector move_objects; - boost::container::small_vector copy_objects; + boost::container::small_vector move_objects; + boost::container::small_vector copy_objects; boost::container::small_vector, 8> domain_objects; std::optional command_header; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index a78bfd1da..fa85bd631 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -328,7 +328,7 @@ static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle, // Return the client session auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); - CASCADE_RESULT(*out_handle, handle_table.Create(client_session)); + handle_table.Add(out_handle, client_session); return RESULT_SUCCESS; } diff --git a/src/yuzu/debugger/wait_tree.cpp b/src/yuzu/debugger/wait_tree.cpp index f4eeba2c5..317c42631 100644 --- a/src/yuzu/debugger/wait_tree.cpp +++ b/src/yuzu/debugger/wait_tree.cpp @@ -118,7 +118,7 @@ WaitTreeMutexInfo::WaitTreeMutexInfo(VAddr mutex_address, const Kernel::HandleTa : mutex_address(mutex_address) { mutex_value = Core::System::GetInstance().Memory().Read32(mutex_address); owner_handle = static_cast(mutex_value & Kernel::Svc::HandleWaitMask); - owner = SharedFrom(handle_table.Get(owner_handle)); + owner = handle_table.GetObject(owner_handle).GetPointerUnsafe(); } WaitTreeMutexInfo::~WaitTreeMutexInfo() = default; diff --git a/src/yuzu/debugger/wait_tree.h b/src/yuzu/debugger/wait_tree.h index 3da2fdfd2..bf8120a71 100644 --- a/src/yuzu/debugger/wait_tree.h +++ b/src/yuzu/debugger/wait_tree.h @@ -80,10 +80,10 @@ public: std::vector> GetChildren() const override; private: - VAddr mutex_address; - u32 mutex_value; - Kernel::Handle owner_handle; - std::shared_ptr owner; + VAddr mutex_address{}; + u32 mutex_value{}; + Kernel::Handle owner_handle{}; + Kernel::KThread* owner{}; }; class WaitTreeCallstack : public WaitTreeExpandableItem { From 0297448fbc6bf909b0bc061723c38208b9667b66 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 21 Apr 2021 21:43:25 -0700 Subject: [PATCH 40/82] 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 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/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 ce88da1c3..15bf0d4fc 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 0b20fef40..04ee2d664 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 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); + + 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 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" From 7a068641006da739d6af5681a022018785379365 Mon Sep 17 00:00:00 2001 From: bunnei Date: Wed, 21 Apr 2021 21:53:56 -0700 Subject: [PATCH 41/82] hle: kernel: Migrate KServerPort to KAutoObject. --- src/core/CMakeLists.txt | 4 +- src/core/hle/kernel/k_client_port.cpp | 4 +- src/core/hle/kernel/k_client_port.h | 14 +++---- .../{server_port.cpp => k_server_port.cpp} | 30 ++++++++----- .../kernel/{server_port.h => k_server_port.h} | 42 +++++++++++-------- src/core/hle/service/service.cpp | 4 +- src/core/hle/service/sm/sm.cpp | 14 +++---- src/core/hle/service/sm/sm.h | 7 ++-- 8 files changed, 67 insertions(+), 52 deletions(-) rename src/core/hle/kernel/{server_port.cpp => k_server_port.cpp} (55%) rename src/core/hle/kernel/{server_port.h => k_server_port.h} (85%) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index cab1f5ae2..487e6f720 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -205,6 +205,8 @@ add_library(core STATIC hle/kernel/k_scoped_lock.h hle/kernel/k_scoped_resource_reservation.h hle/kernel/k_scoped_scheduler_lock_and_sleep.h + hle/kernel/k_server_port.cpp + hle/kernel/k_server_port.h hle/kernel/k_server_session.cpp hle/kernel/k_server_session.h hle/kernel/k_session.cpp @@ -237,8 +239,6 @@ add_library(core STATIC hle/kernel/process.h hle/kernel/process_capability.cpp hle/kernel/process_capability.h - hle/kernel/server_port.cpp - hle/kernel/server_port.h hle/kernel/service_thread.cpp hle/kernel/service_thread.h hle/kernel/slab_helpers.h diff --git a/src/core/hle/kernel/k_client_port.cpp b/src/core/hle/kernel/k_client_port.cpp index 15bf0d4fc..00e1bbc59 100644 --- a/src/core/hle/kernel/k_client_port.cpp +++ b/src/core/hle/kernel/k_client_port.cpp @@ -4,9 +4,9 @@ #include "core/hle/kernel/hle_ipc.h" #include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_server_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 { @@ -19,7 +19,7 @@ void KClientPort::Initialize(s32 max_sessions_, std::string&& name_) { name = std::move(name_); } -std::shared_ptr KClientPort::GetServerPort() const { +KServerPort* KClientPort::GetServerPort() const { return server_port; } diff --git a/src/core/hle/kernel/k_client_port.h b/src/core/hle/kernel/k_client_port.h index 04ee2d664..60dea4763 100644 --- a/src/core/hle/kernel/k_client_port.h +++ b/src/core/hle/kernel/k_client_port.h @@ -15,7 +15,7 @@ namespace Kernel { class KClientSession; class KernelCore; -class ServerPort; +class KServerPort; class KClientPort final : public KSynchronizationObject { KERNEL_AUTOOBJECT_TRAITS(KClientPort, KSynchronizationObject); @@ -24,11 +24,11 @@ public: explicit KClientPort(KernelCore& kernel); virtual ~KClientPort() override; - friend class ServerPort; + friend class KServerPort; void Initialize(s32 max_sessions_, std::string&& name_); - std::shared_ptr GetServerPort() const; + KServerPort* GetServerPort() const; /** * Creates a new Session pair, adds the created ServerSession to the associated ServerPort's @@ -63,10 +63,10 @@ public: } 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) + KServerPort* server_port{}; ///< ServerPort associated with this client port. + s32 max_sessions{}; ///< Maximum number of simultaneous sessions the port can have + std::atomic num_sessions{}; ///< 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/server_port.cpp b/src/core/hle/kernel/k_server_port.cpp similarity index 55% rename from src/core/hle/kernel/server_port.cpp rename to src/core/hle/kernel/k_server_port.cpp index addaaa5cf..2c51d66db 100644 --- a/src/core/hle/kernel/server_port.cpp +++ b/src/core/hle/kernel/k_server_port.cpp @@ -5,18 +5,23 @@ #include #include "common/assert.h" #include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_server_port.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/object.h" -#include "core/hle/kernel/server_port.h" #include "core/hle/kernel/svc_results.h" namespace Kernel { -ServerPort::ServerPort(KernelCore& kernel) : KSynchronizationObject{kernel} {} -ServerPort::~ServerPort() = default; +KServerPort::KServerPort(KernelCore& kernel) : KSynchronizationObject{kernel} {} +KServerPort::~KServerPort() = default; -ResultVal ServerPort::Accept() { +void KServerPort::Initialize(std::string&& name_) { + // Set member variables. + name = std::move(name_); +} + +ResultVal KServerPort::Accept() { if (pending_sessions.empty()) { return ResultNotFound; } @@ -26,30 +31,35 @@ ResultVal ServerPort::Accept() { return MakeResult(session); } -void ServerPort::AppendPendingSession(KServerSession* pending_session) { +void KServerPort::AppendPendingSession(KServerSession* pending_session) { pending_sessions.push_back(std::move(pending_session)); if (pending_sessions.size() == 1) { NotifyAvailable(); } } -bool ServerPort::IsSignaled() const { +void KServerPort::Destroy() {} + +bool KServerPort::IsSignaled() const { return !pending_sessions.empty(); } -ServerPort::PortPair ServerPort::CreatePortPair(KernelCore& kernel, u32 max_sessions, - std::string name) { - std::shared_ptr server_port = std::make_shared(kernel); +KServerPort::PortPair KServerPort::CreatePortPair(KernelCore& kernel, u32 max_sessions, + std::string name) { + KServerPort* server_port = new KServerPort(kernel); KClientPort* client_port = new KClientPort(kernel); + KAutoObject::Create(server_port); KAutoObject::Create(client_port); + server_port->Initialize(name + "_Server"); client_port->Initialize(max_sessions, name + "_Client"); + client_port->server_port = server_port; server_port->name = name + "_Server"; - return std::make_pair(std::move(server_port), client_port); + return std::make_pair(server_port, client_port); } } // namespace Kernel diff --git a/src/core/hle/kernel/server_port.h b/src/core/hle/kernel/k_server_port.h similarity index 85% rename from src/core/hle/kernel/server_port.h rename to src/core/hle/kernel/k_server_port.h index f7d443df8..13fa54e5e 100644 --- a/src/core/hle/kernel/server_port.h +++ b/src/core/hle/kernel/k_server_port.h @@ -20,13 +20,17 @@ class KernelCore; class KServerSession; class SessionRequestHandler; -class ServerPort final : public KSynchronizationObject { +class KServerPort final : public KSynchronizationObject { + KERNEL_AUTOOBJECT_TRAITS(KServerPort, KSynchronizationObject); + public: - explicit ServerPort(KernelCore& kernel); - ~ServerPort() override; + explicit KServerPort(KernelCore& kernel); + virtual ~KServerPort() override; using HLEHandler = std::shared_ptr; - using PortPair = std::pair, KClientPort*>; + using PortPair = std::pair; + + void Initialize(std::string&& name_); /** * Creates a pair of ServerPort and an associated ClientPort. @@ -39,18 +43,6 @@ public: static PortPair CreatePortPair(KernelCore& kernel, u32 max_sessions, std::string name = "UnknownPort"); - std::string GetTypeName() const override { - return "ServerPort"; - } - std::string GetName() const override { - return name; - } - - static constexpr HandleType HANDLE_TYPE = HandleType::ServerPort; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - /** * Accepts a pending incoming connection on this port. If there are no pending sessions, will * return ERR_NO_PENDING_SESSIONS. @@ -79,9 +71,23 @@ public: /// waiting to be accepted by this port. void AppendPendingSession(KServerSession* pending_session); - bool IsSignaled() const override; + // Overridden virtual functions. + virtual void Destroy() override; + virtual bool IsSignaled() const override; - void Finalize() override {} + // DEPRECATED + + std::string GetTypeName() const override { + return "ServerPort"; + } + std::string GetName() const override { + return name; + } + + static constexpr HandleType HANDLE_TYPE = HandleType::ServerPort; + HandleType GetHandleType() const override { + return HANDLE_TYPE; + } private: /// ServerSessions waiting to be accepted by the port diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index d8ad09b49..a882b3b4e 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -12,10 +12,10 @@ #include "core/hle/ipc.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_server_port.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" -#include "core/hle/kernel/server_port.h" #include "core/hle/service/acc/acc.h" #include "core/hle/service/am/am.h" #include "core/hle/service/aoc/aoc_u.h" @@ -117,7 +117,7 @@ void ServiceFrameworkBase::InstallAsNamedPort(Kernel::KernelCore& kernel) { ASSERT(!port_installed); auto [server_port, client_port] = - Kernel::ServerPort::CreatePortPair(kernel, max_sessions, service_name); + Kernel::KServerPort::CreatePortPair(kernel, max_sessions, service_name); server_port->SetHleHandler(shared_from_this()); kernel.AddNamedPort(service_name, client_port); port_installed = true; diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 39575ce3d..51274bfb1 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -8,9 +8,9 @@ #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_client_session.h" +#include "core/hle/kernel/k_server_port.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_session.h" -#include "core/hle/kernel/server_port.h" #include "core/hle/result.h" #include "core/hle/service/sm/controller.h" #include "core/hle/service/sm/sm.h" @@ -49,8 +49,8 @@ void ServiceManager::InstallInterfaces(std::shared_ptr self, Cor self->controller_interface = std::make_unique(system); } -ResultVal> ServiceManager::RegisterService(std::string name, - u32 max_sessions) { +ResultVal ServiceManager::RegisterService(std::string name, + u32 max_sessions) { CASCADE_CODE(ValidateServiceName(name)); @@ -60,12 +60,12 @@ ResultVal> ServiceManager::RegisterService(s } auto [server_port, client_port] = - Kernel::ServerPort::CreatePortPair(kernel, max_sessions, name); + Kernel::KServerPort::CreatePortPair(kernel, max_sessions, name); client_port->Open(); - registered_services.emplace(std::move(name), std::move(client_port)); - return MakeResult(std::move(server_port)); + registered_services.emplace(std::move(name), client_port); + return MakeResult(server_port); } ResultCode ServiceManager::UnregisterService(const std::string& name) { @@ -172,7 +172,7 @@ void SM::RegisterService(Kernel::HLERequestContext& ctx) { rb.Push(handle.Code()); auto server_port = handle.Unwrap(); - rb.PushMoveObjects(server_port.get()); + rb.PushMoveObjects(server_port); } void SM::UnregisterService(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h index aee9aefec..b0204c4bb 100644 --- a/src/core/hle/service/sm/sm.h +++ b/src/core/hle/service/sm/sm.h @@ -11,8 +11,8 @@ #include "common/concepts.h" #include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_server_port.h" #include "core/hle/kernel/object.h" -#include "core/hle/kernel/server_port.h" #include "core/hle/result.h" #include "core/hle/service/service.h" @@ -24,7 +24,7 @@ namespace Kernel { class KClientPort; class KClientSession; class KernelCore; -class ServerPort; +class KServerPort; class SessionRequestHandler; } // namespace Kernel @@ -55,8 +55,7 @@ public: explicit ServiceManager(Kernel::KernelCore& kernel_); ~ServiceManager(); - ResultVal> RegisterService(std::string name, - u32 max_sessions); + ResultVal RegisterService(std::string name, u32 max_sessions); ResultCode UnregisterService(const std::string& name); ResultVal GetServicePort(const std::string& name); From 626f746971d1d3216a38b20680959df3a1f5f256 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 23 Apr 2021 17:00:15 -0700 Subject: [PATCH 42/82] hle: kernel: Migrate KPort, KClientPort, and KServerPort to KAutoObject. --- src/core/CMakeLists.txt | 2 + src/core/hle/ipc_helpers.h | 4 +- src/core/hle/kernel/hle_ipc.cpp | 10 +- src/core/hle/kernel/hle_ipc.h | 10 +- src/core/hle/kernel/init/init_slab_setup.cpp | 4 +- src/core/hle/kernel/k_client_port.cpp | 123 ++++++++++++++----- src/core/hle/kernel/k_client_port.h | 46 +++---- src/core/hle/kernel/k_event.h | 2 +- src/core/hle/kernel/k_port.cpp | 68 ++++++++++ src/core/hle/kernel/k_port.h | 87 +++++++++++++ src/core/hle/kernel/k_server_port.cpp | 94 ++++++++++---- src/core/hle/kernel/k_server_port.h | 55 ++++----- src/core/hle/kernel/k_server_session.cpp | 2 +- src/core/hle/kernel/k_server_session.h | 5 +- src/core/hle/kernel/k_session.cpp | 18 ++- src/core/hle/kernel/k_session.h | 2 +- src/core/hle/kernel/kernel.h | 6 +- src/core/hle/kernel/svc.cpp | 22 ++-- src/core/hle/kernel/svc_results.h | 1 + src/core/hle/service/service.cpp | 9 +- src/core/hle/service/sm/sm.cpp | 33 ++--- src/core/hle/service/sm/sm.h | 13 +- 22 files changed, 447 insertions(+), 169 deletions(-) create mode 100644 src/core/hle/kernel/k_port.cpp create mode 100644 src/core/hle/kernel/k_port.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 487e6f720..cee6d30f6 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -194,6 +194,8 @@ add_library(core STATIC hle/kernel/k_page_linked_list.h hle/kernel/k_page_table.cpp hle/kernel/k_page_table.h + hle/kernel/k_port.cpp + hle/kernel/k_port.h hle/kernel/k_priority_queue.h hle/kernel/k_readable_event.cpp hle/kernel/k_readable_event.h diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index 18aebf6ea..8128445fd 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -136,10 +136,10 @@ public: context->AddDomainObject(std::move(iface)); } else { auto* session = Kernel::KSession::Create(kernel); - session->Initialize(iface->GetServiceName()); + session->Initialize(nullptr, iface->GetServiceName()); context->AddMoveObject(&session->GetClientSession()); - iface->ClientConnected(session); + iface->ClientConnected(&session->GetServerSession()); } } diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index 9e1e63204..ddff9ce99 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -35,14 +35,12 @@ SessionRequestHandler::SessionRequestHandler() = default; SessionRequestHandler::~SessionRequestHandler() = default; -void SessionRequestHandler::ClientConnected(KSession* session) { - session->GetServerSession().SetHleHandler(shared_from_this()); - sessions.push_back(session); +void SessionRequestHandler::ClientConnected(KServerSession* session) { + session->SetHleHandler(shared_from_this()); } -void SessionRequestHandler::ClientDisconnected(KSession* session) { - session->GetServerSession().SetHleHandler(nullptr); - boost::range::remove_erase(sessions, session); +void SessionRequestHandler::ClientDisconnected(KServerSession* session) { + session->SetHleHandler(nullptr); } HLERequestContext::HLERequestContext(KernelCore& kernel_, Core::Memory::Memory& memory_, diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index b7484c445..d63c730ac 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -72,20 +72,14 @@ public: * associated ServerSession alive for the duration of the connection. * @param server_session Owning pointer to the ServerSession associated with the connection. */ - void ClientConnected(KSession* session); + void ClientConnected(KServerSession* session); /** * Signals that a client has just disconnected from this HLE handler and releases the * associated ServerSession. * @param server_session ServerSession associated with the connection. */ - void ClientDisconnected(KSession* session); - -protected: - /// List of sessions that are connected to this handler. - /// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list - /// for the duration of the connection. - std::vector sessions; + void ClientDisconnected(KServerSession* session); }; /** diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index a5ddd7344..f8c255732 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -12,6 +12,7 @@ #include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" +#include "core/hle/kernel/k_port.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_shared_memory.h" @@ -30,8 +31,9 @@ namespace Kernel::Init { HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ - HANDLER(KTransferMemory, (SLAB_COUNT(KTransferMemory)), ##__VA_ARGS__) \ + HANDLER(KPort, (SLAB_COUNT(KPort)), ##__VA_ARGS__) \ HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) \ + HANDLER(KTransferMemory, (SLAB_COUNT(KTransferMemory)), ##__VA_ARGS__) \ HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) \ HANDLER(KResourceLimit, (SLAB_COUNT(KResourceLimit)), ##__VA_ARGS__) diff --git a/src/core/hle/kernel/k_client_port.cpp b/src/core/hle/kernel/k_client_port.cpp index 00e1bbc59..b6f1d713f 100644 --- a/src/core/hle/kernel/k_client_port.cpp +++ b/src/core/hle/kernel/k_client_port.cpp @@ -1,12 +1,14 @@ -// Copyright 2016 Citra Emulator Project +// Copyright 2021 Citra Emulator Project // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include "common/scope_exit.h" #include "core/hle/kernel/hle_ipc.h" #include "core/hle/kernel/k_client_port.h" -#include "core/hle/kernel/k_server_port.h" +#include "core/hle/kernel/k_port.h" +#include "core/hle/kernel/k_scheduler.h" +#include "core/hle/kernel/k_scoped_resource_reservation.h" #include "core/hle/kernel/k_session.h" -#include "core/hle/kernel/object.h" #include "core/hle/kernel/svc_results.h" namespace Kernel { @@ -14,45 +16,110 @@ namespace Kernel { KClientPort::KClientPort(KernelCore& kernel) : KSynchronizationObject{kernel} {} KClientPort::~KClientPort() = default; -void KClientPort::Initialize(s32 max_sessions_, std::string&& name_) { +void KClientPort::Initialize(KPort* parent_, s32 max_sessions_, std::string&& name_) { + // Set member variables. + num_sessions = 0; + peak_sessions = 0; + parent = parent_; max_sessions = max_sessions_; name = std::move(name_); } -KServerPort* KClientPort::GetServerPort() const { - return server_port; +void KClientPort::OnSessionFinalized() { + KScopedSchedulerLock sl{kernel}; + + const auto prev = num_sessions--; + if (prev == max_sessions) { + this->NotifyAvailable(); + } } -ResultVal KClientPort::Connect() { - if (num_sessions >= max_sessions) { - return ResultOutOfSessions; - } - num_sessions++; +void KClientPort::OnServerClosed() {} - 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())); +bool KClientPort::IsLight() const { + return this->GetParent()->IsLight(); } -void KClientPort::ConnectionClosed() { - if (num_sessions == 0) { - return; - } - - --num_sessions; +bool KClientPort::IsServerClosed() const { + return this->GetParent()->IsServerClosed(); } -void KClientPort::Destroy() {} +void KClientPort::Destroy() { + // Note with our parent that we're closed. + parent->OnClientClosed(); + + // Close our reference to our parent. + parent->Close(); +} bool KClientPort::IsSignaled() const { return num_sessions < max_sessions; } +ResultCode KClientPort::CreateSession(KClientSession** out) { + // Reserve a new session from the resource limit. + KScopedResourceReservation session_reservation(kernel.CurrentProcess()->GetResourceLimit(), + LimitableResource::Sessions); + R_UNLESS(session_reservation.Succeeded(), ResultLimitReached); + + // Update the session counts. + { + // Atomically increment the number of sessions. + s32 new_sessions; + { + const auto max = max_sessions; + auto cur_sessions = num_sessions.load(std::memory_order_acquire); + do { + R_UNLESS(cur_sessions < max, ResultOutOfSessions); + new_sessions = cur_sessions + 1; + } while (!num_sessions.compare_exchange_weak(cur_sessions, new_sessions, + std::memory_order_relaxed)); + } + + // Atomically update the peak session tracking. + { + auto peak = peak_sessions.load(std::memory_order_acquire); + do { + if (peak >= new_sessions) { + break; + } + } while (!peak_sessions.compare_exchange_weak(peak, new_sessions, + std::memory_order_relaxed)); + } + } + + // Create a new session. + KSession* session = KSession::Create(kernel); + if (session == nullptr) { + /* Decrement the session count. */ + const auto prev = num_sessions--; + if (prev == max_sessions) { + this->NotifyAvailable(); + } + + return ResultOutOfResource; + } + + // Initialize the session. + session->Initialize(this, parent->GetName()); + + // Commit the session reservation. + session_reservation.Commit(); + + // Register the session. + KSession::Register(kernel, session); + auto session_guard = SCOPE_GUARD({ + session->GetClientSession().Close(); + session->GetServerSession().Close(); + }); + + // Enqueue the session with our parent. + R_TRY(parent->EnqueueSession(std::addressof(session->GetServerSession()))); + + // We succeeded, so set the output. + session_guard.Cancel(); + *out = std::addressof(session->GetClientSession()); + return RESULT_SUCCESS; +} + } // namespace Kernel diff --git a/src/core/hle/kernel/k_client_port.h b/src/core/hle/kernel/k_client_port.h index 60dea4763..43a17f4a4 100644 --- a/src/core/hle/kernel/k_client_port.h +++ b/src/core/hle/kernel/k_client_port.h @@ -15,7 +15,7 @@ namespace Kernel { class KClientSession; class KernelCore; -class KServerPort; +class KPort; class KClientPort final : public KSynchronizationObject { KERNEL_AUTOOBJECT_TRAITS(KClientPort, KSynchronizationObject); @@ -24,30 +24,33 @@ public: explicit KClientPort(KernelCore& kernel); virtual ~KClientPort() override; - friend class KServerPort; + void Initialize(KPort* parent_, s32 max_sessions_, std::string&& name_); + void OnSessionFinalized(); + void OnServerClosed(); - void Initialize(s32 max_sessions_, std::string&& name_); + constexpr const KPort* GetParent() const { + return parent; + } - KServerPort* GetServerPort() const; + s32 GetNumSessions() const { + return num_sessions; + } + s32 GetPeakSessions() const { + return peak_sessions; + } + s32 GetMaxSessions() const { + return max_sessions; + } - /** - * 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(); + bool IsLight() const; + bool IsServerClosed() const; // Overridden virtual functions. virtual void Destroy() override; virtual bool IsSignaled() const override; + ResultCode CreateSession(KClientSession** out); + // DEPRECATED std::string GetTypeName() const override { @@ -63,10 +66,11 @@ public: } private: - KServerPort* server_port{}; ///< ServerPort associated with this client port. - s32 max_sessions{}; ///< Maximum number of simultaneous sessions the port can have - std::atomic num_sessions{}; ///< Number of currently open sessions to this port - std::string name; ///< Name of client port (optional) + std::atomic num_sessions{}; + std::atomic peak_sessions{}; + s32 max_sessions{}; + KPort* parent{}; + std::string name; }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_event.h b/src/core/hle/kernel/k_event.h index 2c48a0499..45634e401 100644 --- a/src/core/hle/kernel/k_event.h +++ b/src/core/hle/kernel/k_event.h @@ -20,7 +20,7 @@ class KEvent final : public KAutoObjectWithSlabHeapAndContainerClientConnected(session); + } else { + server.EnqueueSession(session); + } + + return RESULT_SUCCESS; +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_port.h b/src/core/hle/kernel/k_port.h new file mode 100644 index 000000000..68c8ed8df --- /dev/null +++ b/src/core/hle/kernel/k_port.h @@ -0,0 +1,87 @@ +// Copyright 2021 yuzu 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_client_port.h" +#include "core/hle/kernel/k_server_port.h" +#include "core/hle/kernel/slab_helpers.h" +#include "core/hle/result.h" + +namespace Kernel { + +class KServerSession; + +class KPort final : public KAutoObjectWithSlabHeapAndContainer { + KERNEL_AUTOOBJECT_TRAITS(KPort, KAutoObject); + +public: + explicit KPort(KernelCore& kernel); + virtual ~KPort(); + + static void PostDestroy([[maybe_unused]] uintptr_t arg) {} + + void Initialize(s32 max_sessions_, bool is_light_, const std::string& name_); + void OnClientClosed(); + void OnServerClosed(); + + bool IsLight() const { + return is_light; + } + + bool IsServerClosed() const; + + ResultCode EnqueueSession(KServerSession* session); + + KClientPort& GetClientPort() { + return client; + } + KServerPort& GetServerPort() { + return server; + } + const KClientPort& GetClientPort() const { + return client; + } + const KServerPort& GetServerPort() const { + return server; + } + + // DEPRECATED + + friend class ServerPort; + std::string GetTypeName() const override { + return "Port"; + } + std::string GetName() const override { + return name; + } + + HandleType GetHandleType() const override { + return {}; + } + + void Finalize() override {} + +private: + enum class State : u8 { + Invalid = 0, + Normal = 1, + ClientClosed = 2, + ServerClosed = 3, + }; + +private: + KServerPort server; + KClientPort client; + State state{State::Invalid}; + bool is_light{}; + + std::string name; ///< Name of client port (optional) +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_server_port.cpp b/src/core/hle/kernel/k_server_port.cpp index 2c51d66db..fcc04abaa 100644 --- a/src/core/hle/kernel/k_server_port.cpp +++ b/src/core/hle/kernel/k_server_port.cpp @@ -1,10 +1,12 @@ -// Copyright 2016 Citra Emulator Project +// Copyright 2021 yuzu emulator team // Licensed under GPLv2 or any later version // Refer to the license.txt file included. #include #include "common/assert.h" #include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_port.h" +#include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_server_port.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_thread.h" @@ -16,50 +18,88 @@ namespace Kernel { KServerPort::KServerPort(KernelCore& kernel) : KSynchronizationObject{kernel} {} KServerPort::~KServerPort() = default; -void KServerPort::Initialize(std::string&& name_) { +void KServerPort::Initialize(KPort* parent_, std::string&& name_) { // Set member variables. + parent = parent_; name = std::move(name_); } -ResultVal KServerPort::Accept() { - if (pending_sessions.empty()) { - return ResultNotFound; - } - - auto* session = pending_sessions.back(); - pending_sessions.pop_back(); - return MakeResult(session); +bool KServerPort::IsLight() const { + return this->GetParent()->IsLight(); } -void KServerPort::AppendPendingSession(KServerSession* pending_session) { - pending_sessions.push_back(std::move(pending_session)); - if (pending_sessions.size() == 1) { - NotifyAvailable(); +void KServerPort::CleanupSessions() { + // Ensure our preconditions are met. + if (this->IsLight()) { + UNIMPLEMENTED(); + } + + // Cleanup the session list. + while (true) { + // Get the last session in the list + KServerSession* session = nullptr; + { + KScopedSchedulerLock sl{kernel}; + if (!session_list.empty()) { + session = std::addressof(session_list.front()); + session_list.pop_front(); + } + } + + // Close the session. + if (session != nullptr) { + session->Close(); + } else { + break; + } } } -void KServerPort::Destroy() {} +void KServerPort::Destroy() { + // Note with our parent that we're closed. + parent->OnServerClosed(); + + // Perform necessary cleanup of our session lists. + this->CleanupSessions(); + + // Close our reference to our parent. + parent->Close(); +} bool KServerPort::IsSignaled() const { - return !pending_sessions.empty(); + if (this->IsLight()) { + UNIMPLEMENTED(); + return false; + } else { + return !session_list.empty(); + } } -KServerPort::PortPair KServerPort::CreatePortPair(KernelCore& kernel, u32 max_sessions, - std::string name) { - KServerPort* server_port = new KServerPort(kernel); - KClientPort* client_port = new KClientPort(kernel); +void KServerPort::EnqueueSession(KServerSession* session) { + ASSERT(!this->IsLight()); - KAutoObject::Create(server_port); - KAutoObject::Create(client_port); + KScopedSchedulerLock sl{kernel}; - server_port->Initialize(name + "_Server"); - client_port->Initialize(max_sessions, name + "_Client"); + // Add the session to our queue. + session_list.push_back(*session); + if (session_list.size() == 1) { + this->NotifyAvailable(); + } +} - client_port->server_port = server_port; +KServerSession* KServerPort::AcceptSession() { + ASSERT(!this->IsLight()); - server_port->name = name + "_Server"; + KScopedSchedulerLock sl{kernel}; - return std::make_pair(server_port, client_port); + // Return the first session in the list. + if (session_list.empty()) { + return nullptr; + } + + KServerSession* session = std::addressof(session_list.front()); + session_list.pop_front(); + return session; } } // namespace Kernel diff --git a/src/core/hle/kernel/k_server_port.h b/src/core/hle/kernel/k_server_port.h index 13fa54e5e..9f45ca3f4 100644 --- a/src/core/hle/kernel/k_server_port.h +++ b/src/core/hle/kernel/k_server_port.h @@ -1,4 +1,4 @@ -// Copyright 2016 Citra Emulator Project +// Copyright 2021 yuzu emulator team // Licensed under GPLv2 or any later version // Refer to the license.txt file included. @@ -8,46 +8,33 @@ #include #include #include + +#include + #include "common/common_types.h" +#include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_synchronization_object.h" -#include "core/hle/kernel/object.h" #include "core/hle/result.h" namespace Kernel { -class KClientPort; class KernelCore; -class KServerSession; +class KPort; class SessionRequestHandler; class KServerPort final : public KSynchronizationObject { KERNEL_AUTOOBJECT_TRAITS(KServerPort, KSynchronizationObject); +private: + using SessionList = boost::intrusive::list; + public: explicit KServerPort(KernelCore& kernel); virtual ~KServerPort() override; using HLEHandler = std::shared_ptr; - using PortPair = std::pair; - void Initialize(std::string&& name_); - - /** - * Creates a pair of ServerPort and an associated ClientPort. - * - * @param kernel The kernel instance to create the port pair under. - * @param max_sessions Maximum number of sessions to the port - * @param name Optional name of the ports - * @return The created port tuple - */ - static PortPair CreatePortPair(KernelCore& kernel, u32 max_sessions, - std::string name = "UnknownPort"); - - /** - * Accepts a pending incoming connection on this port. If there are no pending sessions, will - * return ERR_NO_PENDING_SESSIONS. - */ - ResultVal Accept(); + void Initialize(KPort* parent_, std::string&& name_); /// Whether or not this server port has an HLE handler available. bool HasHLEHandler() const { @@ -67,9 +54,15 @@ public: hle_handler = std::move(hle_handler_); } - /// Appends a ServerSession to the collection of ServerSessions - /// waiting to be accepted by this port. - void AppendPendingSession(KServerSession* pending_session); + void EnqueueSession(KServerSession* pending_session); + + KServerSession* AcceptSession(); + + constexpr const KPort* GetParent() const { + return parent; + } + + bool IsLight() const; // Overridden virtual functions. virtual void Destroy() override; @@ -90,14 +83,12 @@ public: } private: - /// ServerSessions waiting to be accepted by the port - std::vector pending_sessions; + void CleanupSessions(); - /// This session's HLE request handler template (optional) - /// ServerSessions created from this port inherit a reference to this handler. +private: + SessionList session_list; HLEHandler hle_handler; - - /// Name of the port (optional) + KPort* parent{}; std::string name; }; diff --git a/src/core/hle/kernel/k_server_session.cpp b/src/core/hle/kernel/k_server_session.cpp index 46ba7081b..863f9aa5f 100644 --- a/src/core/hle/kernel/k_server_session.cpp +++ b/src/core/hle/kernel/k_server_session.cpp @@ -50,7 +50,7 @@ void KServerSession::OnClientClosed() { if (handler) { // Note that after this returns, this server session's hle_handler is // invalidated (set to null). - handler->ClientDisconnected(parent); + handler->ClientDisconnected(this); } } diff --git a/src/core/hle/kernel/k_server_session.h b/src/core/hle/kernel/k_server_session.h index ef81c4e30..d748754d0 100644 --- a/src/core/hle/kernel/k_server_session.h +++ b/src/core/hle/kernel/k_server_session.h @@ -9,6 +9,8 @@ #include #include +#include + #include "common/threadsafe_queue.h" #include "core/hle/kernel/k_synchronization_object.h" #include "core/hle/kernel/service_thread.h" @@ -31,7 +33,8 @@ class KSession; class SessionRequestHandler; class KThread; -class KServerSession final : public KSynchronizationObject { +class KServerSession final : public KSynchronizationObject, + public boost::intrusive::list_base_hook<> { KERNEL_AUTOOBJECT_TRAITS(KServerSession, KSynchronizationObject); friend class ServiceThread; diff --git a/src/core/hle/kernel/k_session.cpp b/src/core/hle/kernel/k_session.cpp index ca1cf18cd..6f4276189 100644 --- a/src/core/hle/kernel/k_session.cpp +++ b/src/core/hle/kernel/k_session.cpp @@ -1,8 +1,9 @@ -// Copyright 2019 yuzu emulator team +// Copyright 2021 yuzu emulator team // Licensed under GPLv2 or any later version // Refer to the license.txt file included. #include "common/assert.h" +#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_client_session.h" #include "core/hle/kernel/k_scoped_resource_reservation.h" #include "core/hle/kernel/k_server_session.h" @@ -14,7 +15,7 @@ KSession::KSession(KernelCore& kernel) : KAutoObjectWithSlabHeapAndContainer{kernel}, server{kernel}, client{kernel} {} KSession::~KSession() = default; -void KSession::Initialize(std::string&& name_) { +void KSession::Initialize(KClientPort* port_, std::string&& name_) { // Increment reference count. // Because reference count is one on creation, this will result // in a reference count of two. Thus, when both server and client are closed @@ -37,11 +38,22 @@ void KSession::Initialize(std::string&& name_) { process = kernel.CurrentProcess(); process->Open(); + // Set our port. + port = port_; + if (port != nullptr) { + port->Open(); + } + // Mark initialized. initialized = true; } -void KSession::Finalize() {} +void KSession::Finalize() { + if (port != nullptr) { + port->OnSessionFinalized(); + port->Close(); + } +} void KSession::OnServerClosed() { if (GetState() == State::Normal) { diff --git a/src/core/hle/kernel/k_session.h b/src/core/hle/kernel/k_session.h index 6a6fcb588..1597cc608 100644 --- a/src/core/hle/kernel/k_session.h +++ b/src/core/hle/kernel/k_session.h @@ -28,7 +28,7 @@ public: explicit KSession(KernelCore& kernel); virtual ~KSession() override; - void Initialize(std::string&& name_); + void Initialize(KClientPort* port_, std::string&& name_); virtual void Finalize() override; diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 19b3530b4..723be6b51 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -37,6 +37,8 @@ class KClientSession; class KEvent; class KLinkedListNode; class KMemoryManager; +class KPort; +class Process; class KResourceLimit; class KScheduler; class KSession; @@ -45,7 +47,6 @@ class KThread; class KTransferMemory; class KWritableEvent; class PhysicalCore; -class Process; class ServiceThread; class Synchronization; class TimeManager; @@ -272,6 +273,8 @@ public: return slab_heap_container->event; } else if constexpr (std::is_same_v) { return slab_heap_container->linked_list_node; + } else if constexpr (std::is_same_v) { + return slab_heap_container->port; } else if constexpr (std::is_same_v) { return slab_heap_container->process; } else if constexpr (std::is_same_v) { @@ -323,6 +326,7 @@ private: KSlabHeap client_session; KSlabHeap event; KSlabHeap linked_list_node; + KSlabHeap port; KSlabHeap process; KSlabHeap resource_limit; KSlabHeap session; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 8d8d3dd5a..ef8fa98a9 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -293,9 +293,7 @@ static ResultCode UnmapMemory32(Core::System& system, u32 dst_addr, u32 src_addr /// Connect to an OS service given the port name, returns the handle to the port to out static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle, VAddr port_name_address) { - std::lock_guard lock{HLE::g_hle_lock}; auto& memory = system.Memory(); - if (!memory.IsValidVirtualAddress(port_name_address)) { LOG_ERROR(Kernel_SVC, "Port Name Address is not a valid virtual address, port_name_address=0x{:016X}", @@ -314,21 +312,27 @@ static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle, LOG_TRACE(Kernel_SVC, "called port_name={}", port_name); + // Get the current handle table. auto& kernel = system.Kernel(); + auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); + + // Find the client port. const auto it = kernel.FindNamedPort(port_name); if (!kernel.IsValidNamedPort(it)) { LOG_WARNING(Kernel_SVC, "tried to connect to unknown port: {}", port_name); return ResultNotFound; } + auto port = it->second; - auto client_port = it->second; + // Create a session. + KClientSession* session{}; + R_TRY(port->CreateSession(std::addressof(session))); - KClientSession* client_session{}; - CASCADE_RESULT(client_session, client_port->Connect()); + // Register the session in the table, close the extra reference. + handle_table.Add(out_handle, session); + session->Close(); - // Return the client session - auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); - handle_table.Add(out_handle, client_session); + // We succeeded. return RESULT_SUCCESS; } @@ -340,13 +344,13 @@ static ResultCode ConnectToNamedPort32(Core::System& system, Handle* out_handle, /// Makes a blocking IPC call to an OS service. static ResultCode SendSyncRequest(Core::System& system, Handle handle) { - LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}({})", handle, session->GetName()); auto& kernel = system.Kernel(); KScopedAutoObject session = kernel.CurrentProcess()->GetHandleTable().GetObject(handle); R_UNLESS(session.IsNotNull(), ResultInvalidHandle); + LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}({})", handle, session->GetName()); auto thread = kernel.CurrentScheduler()->GetCurrentThread(); { diff --git a/src/core/hle/kernel/svc_results.h b/src/core/hle/kernel/svc_results.h index cd32acd99..53a940723 100644 --- a/src/core/hle/kernel/svc_results.h +++ b/src/core/hle/kernel/svc_results.h @@ -36,6 +36,7 @@ constexpr ResultCode ResultBusy{ErrorModule::Kernel, 122}; constexpr ResultCode ResultSessionClosed{ErrorModule::Kernel, 123}; constexpr ResultCode ResultInvalidState{ErrorModule::Kernel, 125}; constexpr ResultCode ResultReservedUsed{ErrorModule::Kernel, 126}; +constexpr ResultCode ResultPortClosed{ErrorModule::Kernel, 131}; constexpr ResultCode ResultLimitReached{ErrorModule::Kernel, 132}; constexpr ResultCode ResultInvalidId{ErrorModule::Kernel, 519}; diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index a882b3b4e..42e464024 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -116,10 +116,11 @@ void ServiceFrameworkBase::InstallAsNamedPort(Kernel::KernelCore& kernel) { ASSERT(!port_installed); - auto [server_port, client_port] = - Kernel::KServerPort::CreatePortPair(kernel, max_sessions, service_name); - server_port->SetHleHandler(shared_from_this()); - kernel.AddNamedPort(service_name, client_port); + auto* port = Kernel::KPort::Create(kernel); + port->Initialize(max_sessions, false, service_name); + port->GetServerPort().SetHleHandler(shared_from_this()); + kernel.AddNamedPort(service_name, &port->GetClientPort()); + port_installed = true; } diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 51274bfb1..71ab4b6f5 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -8,6 +8,7 @@ #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_client_session.h" +#include "core/hle/kernel/k_port.h" #include "core/hle/kernel/k_server_port.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_session.h" @@ -59,13 +60,12 @@ ResultVal ServiceManager::RegisterService(std::string name return ERR_ALREADY_REGISTERED; } - auto [server_port, client_port] = - Kernel::KServerPort::CreatePortPair(kernel, max_sessions, name); + auto* port = Kernel::KPort::Create(kernel); + port->Initialize(max_sessions, false, name); - client_port->Open(); + registered_services.emplace(std::move(name), port); - registered_services.emplace(std::move(name), client_port); - return MakeResult(server_port); + return MakeResult(&port->GetServerPort()); } ResultCode ServiceManager::UnregisterService(const std::string& name) { @@ -83,7 +83,7 @@ ResultCode ServiceManager::UnregisterService(const std::string& name) { 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); @@ -118,25 +118,26 @@ void SM::GetService(Kernel::HLERequestContext& ctx) { std::string name(name_buf.begin(), end); - auto client_port = service_manager->GetServicePort(name); - if (client_port.Failed()) { + auto result = service_manager->GetServicePort(name); + if (result.Failed()) { IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(client_port.Code()); - LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, client_port.Code().raw); + rb.Push(result.Code()); + LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, result.Code().raw); if (name.length() == 0) return; // LibNX Fix UNIMPLEMENTED(); return; } - auto* session = Kernel::KSession::Create(kernel); - session->Initialize(std::move(name)); + auto* port = result.Unwrap(); - const auto& server_port = client_port.Unwrap()->GetServerPort(); - if (server_port->GetHLEHandler()) { - server_port->GetHLEHandler()->ClientConnected(session); + auto* session = Kernel::KSession::Create(kernel); + session->Initialize(&port->GetClientPort(), std::move(name)); + + if (port->GetServerPort().GetHLEHandler()) { + port->GetServerPort().GetHLEHandler()->ClientConnected(&session->GetServerSession()); } else { - server_port->AppendPendingSession(&session->GetServerSession()); + port->EnqueueSession(&session->GetServerSession()); } LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetObjectId()); diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h index b0204c4bb..af5010c3b 100644 --- a/src/core/hle/service/sm/sm.h +++ b/src/core/hle/service/sm/sm.h @@ -10,9 +10,7 @@ #include #include "common/concepts.h" -#include "core/hle/kernel/k_client_port.h" -#include "core/hle/kernel/k_server_port.h" -#include "core/hle/kernel/object.h" +#include "core/hle/kernel/k_port.h" #include "core/hle/result.h" #include "core/hle/service/service.h" @@ -24,6 +22,7 @@ namespace Kernel { class KClientPort; class KClientSession; class KernelCore; +class KPort; class KServerPort; class SessionRequestHandler; } // namespace Kernel @@ -57,7 +56,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 { @@ -66,11 +65,11 @@ public: LOG_DEBUG(Service, "Can't find service: {}", service_name); return nullptr; } - auto port = service->second->GetServerPort(); + auto* port = service->second; if (port == nullptr) { return nullptr; } - return std::static_pointer_cast(port->GetHLEHandler()); + return std::static_pointer_cast(port->GetServerPort().GetHLEHandler()); } void InvokeControlRequest(Kernel::HLERequestContext& context); @@ -80,7 +79,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; From 39a8dba9a67385b2e246dbf96b22adcb8fc53c03 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 23 Apr 2021 17:00:46 -0700 Subject: [PATCH 43/82] hle: kernel: Cleanup shutdown of persistent kernel objects. --- src/core/hle/kernel/kernel.cpp | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index c939bb903..b2eb51bde 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -129,20 +129,18 @@ struct KernelCore::Impl { exclusive_monitor.reset(); - hid_shared_mem->Close(); - hid_shared_mem = nullptr; - - font_shared_mem->Close(); - font_shared_mem = nullptr; - - irs_shared_mem->Close(); - irs_shared_mem = nullptr; - - time_shared_mem->Close(); - time_shared_mem = nullptr; - - system_resource_limit->Close(); - system_resource_limit = nullptr; + // Cleanup persistent kernel objects + auto CleanupObject = [](KAutoObject* obj) { + if (obj) { + obj->Close(); + obj = nullptr; + } + }; + CleanupObject(hid_shared_mem); + CleanupObject(font_shared_mem); + CleanupObject(irs_shared_mem); + CleanupObject(time_shared_mem); + CleanupObject(system_resource_limit); // Next host thead ID to use, 0-3 IDs represent core threads, >3 represent others next_host_thread_id = Core::Hardware::NUM_CPU_CORES; From 864841eb9eb3af7443aa3672e812e512967ea46e Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 23 Apr 2021 17:04:33 -0700 Subject: [PATCH 44/82] hle: kernel: Do not shutdown twice on emulator close. --- src/core/hle/kernel/kernel.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index b2eb51bde..409bcfaa0 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -694,9 +694,7 @@ struct KernelCore::Impl { }; KernelCore::KernelCore(Core::System& system) : impl{std::make_unique(system, *this)} {} -KernelCore::~KernelCore() { - Shutdown(); -} +KernelCore::~KernelCore() = default; void KernelCore::SetMulticore(bool is_multicore) { impl->SetMulticore(is_multicore); From bf380b858481ef99d7150d322af2c30ac339bcde Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 23 Apr 2021 21:50:04 -0700 Subject: [PATCH 45/82] hle: kernel: Remove deprecated Object class. --- src/core/CMakeLists.txt | 2 - src/core/core.h | 1 - src/core/hle/ipc_helpers.h | 17 ---- src/core/hle/kernel/handle_table.h | 1 - src/core/hle/kernel/hle_ipc.cpp | 1 - src/core/hle/kernel/hle_ipc.h | 10 -- src/core/hle/kernel/k_auto_object.h | 14 ++- src/core/hle/kernel/k_client_port.h | 15 --- src/core/hle/kernel/k_client_session.h | 19 ---- src/core/hle/kernel/k_event.h | 11 --- src/core/hle/kernel/k_port.h | 18 ---- src/core/hle/kernel/k_readable_event.h | 12 --- src/core/hle/kernel/k_resource_limit.h | 15 --- src/core/hle/kernel/k_server_port.cpp | 1 - src/core/hle/kernel/k_server_port.h | 15 --- src/core/hle/kernel/k_server_session.h | 18 ---- src/core/hle/kernel/k_session.cpp | 4 +- src/core/hle/kernel/k_session.h | 14 +-- src/core/hle/kernel/k_shared_memory.h | 13 --- .../hle/kernel/k_synchronization_object.h | 9 -- src/core/hle/kernel/k_thread.cpp | 1 - src/core/hle/kernel/k_thread.h | 15 --- src/core/hle/kernel/k_transfer_memory.h | 15 --- src/core/hle/kernel/k_writable_event.h | 12 --- src/core/hle/kernel/kernel.h | 2 - src/core/hle/kernel/object.cpp | 42 -------- src/core/hle/kernel/object.h | 98 ------------------- src/core/hle/kernel/process.h | 15 --- src/core/hle/kernel/time_manager.h | 2 - src/core/hle/service/am/applets/applets.h | 1 - src/core/hle/service/hid/controllers/npad.h | 1 - src/core/hle/service/hid/irs.h | 1 - src/core/hle/service/nvflinger/buffer_queue.h | 1 - src/core/hle/service/nvflinger/nvflinger.h | 1 - src/core/hle/service/service.h | 1 - src/core/hle/service/sm/controller.cpp | 2 +- src/core/hle/service/sm/sm.cpp | 2 +- src/yuzu/debugger/wait_tree.cpp | 28 +++--- src/yuzu/debugger/wait_tree.h | 7 +- 39 files changed, 34 insertions(+), 423 deletions(-) delete mode 100644 src/core/hle/kernel/object.cpp delete mode 100644 src/core/hle/kernel/object.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index cee6d30f6..4e1387c7e 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -232,8 +232,6 @@ add_library(core STATIC hle/kernel/kernel.cpp hle/kernel/kernel.h hle/kernel/memory_types.h - hle/kernel/object.cpp - hle/kernel/object.h hle/kernel/physical_core.cpp hle/kernel/physical_core.h hle/kernel/physical_memory.h diff --git a/src/core/core.h b/src/core/core.h index f1068d23f..16e191266 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -12,7 +12,6 @@ #include "common/common_types.h" #include "core/file_sys/vfs_types.h" -#include "core/hle/kernel/object.h" namespace Core::Frontend { class EmuWindow; diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index 8128445fd..0906b8cfb 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -16,7 +16,6 @@ #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" namespace IPC { @@ -381,12 +380,6 @@ public: template T PopRaw(); - template - T* GetMoveObject(std::size_t index); - - template - T* GetCopyObject(std::size_t index); - template std::shared_ptr PopIpcInterface() { ASSERT(context->Session()->IsDomain()); @@ -491,14 +484,4 @@ void RequestParser::Pop(First& first_value, Other&... other_values) { Pop(other_values...); } -template -T* RequestParser::GetMoveObject(std::size_t index) { - return context->GetMoveObject(index); -} - -template -T* RequestParser::GetCopyObject(std::size_t index) { - return context->GetCopyObject(index); -} - } // namespace IPC diff --git a/src/core/hle/kernel/handle_table.h b/src/core/hle/kernel/handle_table.h index 2e0b2d8b8..791e303d1 100644 --- a/src/core/hle/kernel/handle_table.h +++ b/src/core/hle/kernel/handle_table.h @@ -12,7 +12,6 @@ #include "core/hle/kernel/k_auto_object.h" #include "core/hle/kernel/k_spin_lock.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/object.h" #include "core/hle/result.h" namespace Kernel { diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index ddff9ce99..a11528f28 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -23,7 +23,6 @@ #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/object.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/time_manager.h" diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index d63c730ac..7f7ab74dd 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -212,16 +212,6 @@ public: return move_handles.at(index); } - template - T* GetCopyObject(std::size_t index) { - return DynamicObjectCast(copy_objects.at(index)); - } - - template - T* GetMoveObject(std::size_t index) { - return DynamicObjectCast(move_objects.at(index)); - } - void AddMoveObject(KAutoObject* object) { move_objects.emplace_back(object); } diff --git a/src/core/hle/kernel/k_auto_object.h b/src/core/hle/kernel/k_auto_object.h index 64c012d44..fd6405a0e 100644 --- a/src/core/hle/kernel/k_auto_object.h +++ b/src/core/hle/kernel/k_auto_object.h @@ -11,13 +11,14 @@ #include "common/common_types.h" #include "common/intrusive_red_black_tree.h" #include "core/hle/kernel/k_class_token.h" -#include "core/hle/kernel/object.h" namespace Kernel { class KernelCore; class Process; +using Handle = u32; + #define KERNEL_AUTOOBJECT_TRAITS(CLASS, BASE_CLASS) \ NON_COPYABLE(CLASS); \ NON_MOVEABLE(CLASS); \ @@ -48,7 +49,7 @@ public: \ private: -class KAutoObject : public Object { +class KAutoObject { protected: class TypeObj { private: @@ -84,16 +85,17 @@ private: KERNEL_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject); private: - std::atomic m_ref_count; + std::atomic m_ref_count{}; protected: KernelCore& kernel; + std::string name; public: static KAutoObject* Create(KAutoObject* ptr); public: - explicit KAutoObject(KernelCore& kernel_) : Object{kernel_}, m_ref_count(0), kernel(kernel_) {} + explicit KAutoObject(KernelCore& kernel_) : kernel(kernel_) {} virtual ~KAutoObject() {} // Destroy is responsible for destroying the auto object's resources when ref_count hits zero. @@ -205,6 +207,10 @@ public: virtual u64 GetId() const { return reinterpret_cast(this); } + + virtual const std::string& GetName() const { + return name; + } }; template diff --git a/src/core/hle/kernel/k_client_port.h b/src/core/hle/kernel/k_client_port.h index 43a17f4a4..f971a8b2c 100644 --- a/src/core/hle/kernel/k_client_port.h +++ b/src/core/hle/kernel/k_client_port.h @@ -51,26 +51,11 @@ public: ResultCode CreateSession(KClientSession** out); - // 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::atomic num_sessions{}; std::atomic peak_sessions{}; s32 max_sessions{}; KPort* parent{}; - std::string name; }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_client_session.h b/src/core/hle/kernel/k_client_session.h index c4b193773..1480597e2 100644 --- a/src/core/hle/kernel/k_client_session.h +++ b/src/core/hle/kernel/k_client_session.h @@ -54,27 +54,8 @@ public: void OnServerClosed(); - // DEPRECATED - - static constexpr HandleType HANDLE_TYPE = HandleType::ClientSession; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - - std::string GetTypeName() const override { - return "ClientSession"; - } - - std::string GetName() const override { - return name; - } - private: - /// The parent session, which links to the server endpoint. KSession* parent{}; - - /// Name of the client session (optional) - std::string name; }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_event.h b/src/core/hle/kernel/k_event.h index 45634e401..f0b89f882 100644 --- a/src/core/hle/kernel/k_event.h +++ b/src/core/hle/kernel/k_event.h @@ -48,17 +48,6 @@ public: return writable_event; } - // DEPRECATED - - std::string GetTypeName() const override { - return "KEvent"; - } - - static constexpr HandleType HANDLE_TYPE = HandleType::Event; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - private: KReadableEvent readable_event; KWritableEvent writable_event; diff --git a/src/core/hle/kernel/k_port.h b/src/core/hle/kernel/k_port.h index 68c8ed8df..f1b2838d8 100644 --- a/src/core/hle/kernel/k_port.h +++ b/src/core/hle/kernel/k_port.h @@ -51,22 +51,6 @@ public: return server; } - // DEPRECATED - - friend class ServerPort; - std::string GetTypeName() const override { - return "Port"; - } - std::string GetName() const override { - return name; - } - - HandleType GetHandleType() const override { - return {}; - } - - void Finalize() override {} - private: enum class State : u8 { Invalid = 0, @@ -80,8 +64,6 @@ private: KClientPort client; State state{State::Invalid}; bool is_light{}; - - std::string name; ///< Name of client port (optional) }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_readable_event.h b/src/core/hle/kernel/k_readable_event.h index 4c22f0584..8514d065a 100644 --- a/src/core/hle/kernel/k_readable_event.h +++ b/src/core/hle/kernel/k_readable_event.h @@ -6,7 +6,6 @@ #include "core/hle/kernel/k_auto_object.h" #include "core/hle/kernel/k_synchronization_object.h" -#include "core/hle/kernel/object.h" #include "core/hle/kernel/slab_helpers.h" #include "core/hle/result.h" @@ -39,17 +38,6 @@ public: ResultCode Clear(); ResultCode Reset(); - // DEPRECATED - - std::string GetTypeName() const override { - return "KReadableEvent"; - } - - static constexpr HandleType HANDLE_TYPE = HandleType::ReadableEvent; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - private: bool is_signaled{}; KEvent* parent{}; diff --git a/src/core/hle/kernel/k_resource_limit.h b/src/core/hle/kernel/k_resource_limit.h index 483c66c33..66ebf32df 100644 --- a/src/core/hle/kernel/k_resource_limit.h +++ b/src/core/hle/kernel/k_resource_limit.h @@ -8,7 +8,6 @@ #include "common/common_types.h" #include "core/hle/kernel/k_light_condition_variable.h" #include "core/hle/kernel/k_light_lock.h" -#include "core/hle/kernel/object.h" union ResultCode; @@ -57,20 +56,6 @@ public: static void PostDestroy([[maybe_unused]] uintptr_t arg) {} - // DEPRECATED - - std::string GetTypeName() const override { - return "KResourceLimit"; - } - std::string GetName() const override { - return GetTypeName(); - } - - static constexpr HandleType HANDLE_TYPE = HandleType::ResourceLimit; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - private: using ResourceArray = std::array(LimitableResource::Count)>; ResourceArray limit_values{}; diff --git a/src/core/hle/kernel/k_server_port.cpp b/src/core/hle/kernel/k_server_port.cpp index fcc04abaa..5e44c48e2 100644 --- a/src/core/hle/kernel/k_server_port.cpp +++ b/src/core/hle/kernel/k_server_port.cpp @@ -10,7 +10,6 @@ #include "core/hle/kernel/k_server_port.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_thread.h" -#include "core/hle/kernel/object.h" #include "core/hle/kernel/svc_results.h" namespace Kernel { diff --git a/src/core/hle/kernel/k_server_port.h b/src/core/hle/kernel/k_server_port.h index 9f45ca3f4..a46fa8f54 100644 --- a/src/core/hle/kernel/k_server_port.h +++ b/src/core/hle/kernel/k_server_port.h @@ -68,20 +68,6 @@ public: virtual void Destroy() override; virtual bool IsSignaled() const override; - // DEPRECATED - - std::string GetTypeName() const override { - return "ServerPort"; - } - std::string GetName() const override { - return name; - } - - static constexpr HandleType HANDLE_TYPE = HandleType::ServerPort; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - private: void CleanupSessions(); @@ -89,7 +75,6 @@ private: SessionList session_list; HLEHandler hle_handler; KPort* parent{}; - std::string name; }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_server_session.h b/src/core/hle/kernel/k_server_session.h index d748754d0..4a54e6634 100644 --- a/src/core/hle/kernel/k_server_session.h +++ b/src/core/hle/kernel/k_server_session.h @@ -103,21 +103,6 @@ public: convert_to_domain = true; } - // DEPRECATED - - std::string GetTypeName() const override { - return "ServerSession"; - } - - std::string GetName() const override { - return name; - } - - static constexpr HandleType HANDLE_TYPE = HandleType::ServerSession; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - private: /// Queues a sync request from the emulated application. ResultCode QueueSyncRequest(KThread* thread, Core::Memory::Memory& memory); @@ -138,9 +123,6 @@ private: /// When set to True, converts the session to a domain at the end of the command bool convert_to_domain{}; - /// The name of this session (optional) - std::string name; - /// Thread to dispatch service requests std::weak_ptr service_thread; diff --git a/src/core/hle/kernel/k_session.cpp b/src/core/hle/kernel/k_session.cpp index 6f4276189..6f5947ce7 100644 --- a/src/core/hle/kernel/k_session.cpp +++ b/src/core/hle/kernel/k_session.cpp @@ -15,7 +15,7 @@ KSession::KSession(KernelCore& kernel) : KAutoObjectWithSlabHeapAndContainer{kernel}, server{kernel}, client{kernel} {} KSession::~KSession() = default; -void KSession::Initialize(KClientPort* port_, std::string&& name_) { +void KSession::Initialize(KClientPort* port_, const std::string& name_) { // Increment reference count. // Because reference count is one on creation, this will result // in a reference count of two. Thus, when both server and client are closed @@ -32,7 +32,7 @@ void KSession::Initialize(KClientPort* port_, std::string&& name_) { // Set state and name. SetState(State::Normal); - name = std::move(name_); + name = name_; // Set our owner process. process = kernel.CurrentProcess(); diff --git a/src/core/hle/kernel/k_session.h b/src/core/hle/kernel/k_session.h index 1597cc608..f29195fa0 100644 --- a/src/core/hle/kernel/k_session.h +++ b/src/core/hle/kernel/k_session.h @@ -28,7 +28,7 @@ public: explicit KSession(KernelCore& kernel); virtual ~KSession() override; - void Initialize(KClientPort* port_, std::string&& name_); + void Initialize(KClientPort* port_, const std::string& name_); virtual void Finalize() override; @@ -74,17 +74,6 @@ public: return port; } - // DEPRECATED - - std::string GetName() const override { - return name; - } - - static constexpr HandleType HANDLE_TYPE = HandleType::Session; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - private: void SetState(State state) { atomic_state = static_cast(state); @@ -100,7 +89,6 @@ private: std::atomic::type> atomic_state{ static_cast::type>(State::Invalid)}; KClientPort* port{}; - std::string name; Process* process{}; bool initialized{}; }; diff --git a/src/core/hle/kernel/k_shared_memory.h b/src/core/hle/kernel/k_shared_memory.h index 93153ab20..9547546a5 100644 --- a/src/core/hle/kernel/k_shared_memory.h +++ b/src/core/hle/kernel/k_shared_memory.h @@ -32,19 +32,6 @@ public: KMemoryPermission owner_permission_, KMemoryPermission user_permission_, PAddr physical_address_, std::size_t size_, std::string name_); - std::string GetTypeName() const override { - return "SharedMemory"; - } - - std::string GetName() const override { - return name; - } - - static constexpr HandleType HANDLE_TYPE = HandleType::SharedMemory; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - /** * Maps a shared memory block to an address in the target process' address space * @param target_process Process on which to map the memory block diff --git a/src/core/hle/kernel/k_synchronization_object.h b/src/core/hle/kernel/k_synchronization_object.h index 5a99dbd46..a41dd1220 100644 --- a/src/core/hle/kernel/k_synchronization_object.h +++ b/src/core/hle/kernel/k_synchronization_object.h @@ -51,13 +51,4 @@ private: ThreadListNode* thread_list_tail{}; }; -// Specialization of DynamicObjectCast for KSynchronizationObjects -template <> -inline KSynchronizationObject* DynamicObjectCast(Object* object) { - if (object != nullptr && object->IsWaitable()) { - return reinterpret_cast(object); - } - return nullptr; -} - } // namespace Kernel diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp index 5cc0a0064..c59f3113c 100644 --- a/src/core/hle/kernel/k_thread.cpp +++ b/src/core/hle/kernel/k_thread.cpp @@ -27,7 +27,6 @@ #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_thread_queue.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/object.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/time_manager.h" diff --git a/src/core/hle/kernel/k_thread.h b/src/core/hle/kernel/k_thread.h index 5c1c17d48..5b943b18b 100644 --- a/src/core/hle/kernel/k_thread.h +++ b/src/core/hle/kernel/k_thread.h @@ -19,7 +19,6 @@ #include "core/hle/kernel/k_light_lock.h" #include "core/hle/kernel/k_spin_lock.h" #include "core/hle/kernel/k_synchronization_object.h" -#include "core/hle/kernel/object.h" #include "core/hle/kernel/slab_helpers.h" #include "core/hle/kernel/svc_common.h" #include "core/hle/kernel/svc_types.h" @@ -120,23 +119,10 @@ public: using ThreadContext64 = Core::ARM_Interface::ThreadContext64; using WaiterList = boost::intrusive::list; - [[nodiscard]] std::string GetName() const override { - return name; - } - void SetName(std::string new_name) { name = std::move(new_name); } - [[nodiscard]] std::string GetTypeName() const override { - return "Thread"; - } - - static constexpr HandleType HANDLE_TYPE = HandleType::Thread; - [[nodiscard]] HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - /** * Gets the thread's current priority * @return The current thread's priority @@ -728,7 +714,6 @@ private: VAddr mutex_wait_address_for_debugging{}; ThreadWaitReasonForDebugging wait_reason_for_debugging{}; ThreadType thread_type_for_debugging{}; - std::string name; public: using ConditionVariableThreadTreeType = ConditionVariableThreadTree; diff --git a/src/core/hle/kernel/k_transfer_memory.h b/src/core/hle/kernel/k_transfer_memory.h index 3c3fa401b..1e4fa9323 100644 --- a/src/core/hle/kernel/k_transfer_memory.h +++ b/src/core/hle/kernel/k_transfer_memory.h @@ -55,21 +55,6 @@ public: return is_initialized ? size * PageSize : 0; } - // DEPRECATED - - std::string GetTypeName() const override { - return "TransferMemory"; - } - - std::string GetName() const override { - return GetTypeName(); - } - - static constexpr HandleType HANDLE_TYPE = HandleType::TransferMemory; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - private: Process* owner{}; VAddr address{}; diff --git a/src/core/hle/kernel/k_writable_event.h b/src/core/hle/kernel/k_writable_event.h index 7cf43f77e..f5e083482 100644 --- a/src/core/hle/kernel/k_writable_event.h +++ b/src/core/hle/kernel/k_writable_event.h @@ -5,7 +5,6 @@ #pragma once #include "core/hle/kernel/k_auto_object.h" -#include "core/hle/kernel/object.h" #include "core/hle/kernel/slab_helpers.h" #include "core/hle/result.h" @@ -34,17 +33,6 @@ public: return parent; } - // DEPRECATED - - std::string GetTypeName() const override { - return "KWritableEvent"; - } - - static constexpr HandleType HANDLE_TYPE = HandleType::WritableEvent; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - private: KEvent* parent{}; }; diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 723be6b51..de7f83423 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -14,7 +14,6 @@ #include "core/hle/kernel/k_auto_object.h" #include "core/hle/kernel/k_slab_heap.h" #include "core/hle/kernel/memory_types.h" -#include "core/hle/kernel/object.h" namespace Core { class CPUInterruptHandler; @@ -293,7 +292,6 @@ public: } private: - friend class Object; friend class Process; friend class KThread; diff --git a/src/core/hle/kernel/object.cpp b/src/core/hle/kernel/object.cpp deleted file mode 100644 index d7f40c403..000000000 --- a/src/core/hle/kernel/object.cpp +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2018 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include "common/assert.h" -#include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/object.h" - -namespace Kernel { - -Object::Object(KernelCore& kernel_) - : kernel{kernel_}, object_id{kernel_.CreateNewObjectID()}, name{"[UNKNOWN KERNEL OBJECT]"} {} -Object::Object(KernelCore& kernel_, std::string&& name_) - : kernel{kernel_}, object_id{kernel_.CreateNewObjectID()}, name{std::move(name_)} {} -Object::~Object() = default; - -bool Object::IsWaitable() const { - switch (GetHandleType()) { - case HandleType::ReadableEvent: - case HandleType::Thread: - case HandleType::Process: - case HandleType::ServerPort: - case HandleType::ServerSession: - return true; - - case HandleType::Unknown: - case HandleType::Event: - case HandleType::WritableEvent: - case HandleType::SharedMemory: - case HandleType::TransferMemory: - case HandleType::ResourceLimit: - case HandleType::ClientPort: - case HandleType::ClientSession: - case HandleType::Session: - return false; - } - - UNREACHABLE(); - return false; -} - -} // namespace Kernel diff --git a/src/core/hle/kernel/object.h b/src/core/hle/kernel/object.h deleted file mode 100644 index 03443b947..000000000 --- a/src/core/hle/kernel/object.h +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2018 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include - -#include "common/common_types.h" - -namespace Kernel { - -class KernelCore; - -using Handle = u32; - -enum class HandleType : u32 { - Unknown, - Event, - WritableEvent, - ReadableEvent, - SharedMemory, - TransferMemory, - Thread, - Process, - ResourceLimit, - ClientPort, - ServerPort, - ClientSession, - ServerSession, - Session, -}; - -class Object : NonCopyable, public std::enable_shared_from_this { -public: - explicit Object(KernelCore& kernel_); - explicit Object(KernelCore& kernel_, std::string&& name_); - virtual ~Object(); - - /// Returns a unique identifier for the object. For debugging purposes only. - u32 GetObjectId() const { - return object_id.load(std::memory_order_relaxed); - } - - virtual std::string GetTypeName() const { - return "[BAD KERNEL OBJECT TYPE]"; - } - virtual std::string GetName() const { - return name; - } - virtual HandleType GetHandleType() const = 0; - - void Close() { - // TODO(bunnei): This is a placeholder to decrement the reference count, which we will use - // when we implement KAutoObject instead of using shared_ptr. - } - - /** - * Check if a thread can wait on the object - * @return True if a thread can wait on the object, otherwise false - */ - bool IsWaitable() const; - - virtual void Finalize() = 0; - -protected: - /// The kernel instance this object was created under. - KernelCore& kernel; - -private: - std::atomic object_id{0}; - -protected: - std::string name; -}; - -template -std::shared_ptr SharedFrom(T* raw) { - if (raw == nullptr) - return nullptr; - return std::static_pointer_cast(raw->shared_from_this()); -} - -/** - * Attempts to downcast the given Object pointer to a pointer to T. - * @return Derived pointer to the object, or `nullptr` if `object` isn't of type T. - */ -template -inline T* DynamicObjectCast(Object* object) { - if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) { - return reinterpret_cast(object); - } - return nullptr; -} - -} // namespace Kernel diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index 35fe16433..b775e1fd0 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -92,18 +92,6 @@ public: static ResultCode Initialize(Process* process, Core::System& system, std::string name, ProcessType type); - std::string GetTypeName() const override { - return "Process"; - } - std::string GetName() const override { - return name; - } - - static constexpr HandleType HANDLE_TYPE = HandleType::Process; - HandleType GetHandleType() const override { - return HANDLE_TYPE; - } - /// Gets a reference to the process' page table. KPageTable& PageTable() { return *page_table; @@ -468,9 +456,6 @@ private: /// Process total image size std::size_t image_size{}; - /// Name of this process - std::string name; - /// Schedule count of this process s64 schedule_count{}; diff --git a/src/core/hle/kernel/time_manager.h b/src/core/hle/kernel/time_manager.h index 0d7f05f30..2d175a9c4 100644 --- a/src/core/hle/kernel/time_manager.h +++ b/src/core/hle/kernel/time_manager.h @@ -8,8 +8,6 @@ #include #include -#include "core/hle/kernel/object.h" - namespace Core { class System; } // namespace Core diff --git a/src/core/hle/service/am/applets/applets.h b/src/core/hle/service/am/applets/applets.h index 229dc7a1e..5c0b4b459 100644 --- a/src/core/hle/service/am/applets/applets.h +++ b/src/core/hle/service/am/applets/applets.h @@ -9,7 +9,6 @@ #include "common/swap.h" #include "core/hle/kernel/k_event.h" -#include "core/hle/kernel/object.h" union ResultCode; diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h index b0f575561..c050c9a44 100644 --- a/src/core/hle/service/hid/controllers/npad.h +++ b/src/core/hle/service/hid/controllers/npad.h @@ -11,7 +11,6 @@ #include "common/quaternion.h" #include "common/settings.h" #include "core/frontend/input.h" -#include "core/hle/kernel/object.h" #include "core/hle/service/hid/controllers/controller_base.h" namespace Kernel { diff --git a/src/core/hle/service/hid/irs.h b/src/core/hle/service/hid/irs.h index a1bcb5859..9bc6462b0 100644 --- a/src/core/hle/service/hid/irs.h +++ b/src/core/hle/service/hid/irs.h @@ -4,7 +4,6 @@ #pragma once -#include "core/hle/kernel/object.h" #include "core/hle/service/service.h" namespace Core { diff --git a/src/core/hle/service/nvflinger/buffer_queue.h b/src/core/hle/service/nvflinger/buffer_queue.h index 044e51d08..4ec0b1506 100644 --- a/src/core/hle/service/nvflinger/buffer_queue.h +++ b/src/core/hle/service/nvflinger/buffer_queue.h @@ -15,7 +15,6 @@ #include "common/swap.h" #include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_readable_event.h" -#include "core/hle/kernel/object.h" #include "core/hle/service/nvdrv/nvdata.h" namespace Kernel { diff --git a/src/core/hle/service/nvflinger/nvflinger.h b/src/core/hle/service/nvflinger/nvflinger.h index 1c0aa8ec4..b0febdaec 100644 --- a/src/core/hle/service/nvflinger/nvflinger.h +++ b/src/core/hle/service/nvflinger/nvflinger.h @@ -15,7 +15,6 @@ #include #include "common/common_types.h" -#include "core/hle/kernel/object.h" namespace Common { class Event; diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h index 076f50b0b..884951428 100644 --- a/src/core/hle/service/service.h +++ b/src/core/hle/service/service.h @@ -11,7 +11,6 @@ #include "common/common_types.h" #include "common/spin_lock.h" #include "core/hle/kernel/hle_ipc.h" -#include "core/hle/kernel/object.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // Namespace Service diff --git a/src/core/hle/service/sm/controller.cpp b/src/core/hle/service/sm/controller.cpp index cb397fcc7..ee026e22f 100644 --- a/src/core/hle/service/sm/controller.cpp +++ b/src/core/hle/service/sm/controller.cpp @@ -14,7 +14,7 @@ namespace Service::SM { void Controller::ConvertCurrentObjectToDomain(Kernel::HLERequestContext& ctx) { ASSERT_MSG(ctx.Session()->IsSession(), "Session is already a domain"); - LOG_DEBUG(Service, "called, server_session={}", ctx.Session()->GetObjectId()); + LOG_DEBUG(Service, "called, server_session={}", ctx.Session()->GetId()); ctx.Session()->ConvertToDomain(); IPC::ResponseBuilder rb{ctx, 3}; diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp index 71ab4b6f5..568effbc9 100644 --- a/src/core/hle/service/sm/sm.cpp +++ b/src/core/hle/service/sm/sm.cpp @@ -140,7 +140,7 @@ void SM::GetService(Kernel::HLERequestContext& ctx) { port->EnqueueSession(&session->GetServerSession()); } - LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetObjectId()); + LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetId()); IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles}; rb.Push(RESULT_SUCCESS); rb.PushMoveObjects(session->GetClientSession()); diff --git a/src/yuzu/debugger/wait_tree.cpp b/src/yuzu/debugger/wait_tree.cpp index 317c42631..3ac4a9e2b 100644 --- a/src/yuzu/debugger/wait_tree.cpp +++ b/src/yuzu/debugger/wait_tree.cpp @@ -13,6 +13,7 @@ #include "core/arm/arm_interface.h" #include "core/core.h" #include "core/hle/kernel/handle_table.h" +#include "core/hle/kernel/k_class_token.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_synchronization_object.h" @@ -183,20 +184,20 @@ bool WaitTreeExpandableItem::IsExpandable() const { } QString WaitTreeSynchronizationObject::GetText() const { - // return tr("[%1]%2 %3") - // .arg(object.GetObjectId()) - // .arg(QString::fromStdString(object.GetTypeName()), - // QString::fromStdString(object.GetName())); - - return tr("UNIMPLEMENTED"); + return tr("[%1] %2 %3") + .arg(object.GetId()) + .arg(QString::fromStdString(object.GetTypeObj().GetName()), + QString::fromStdString(object.GetName())); } std::unique_ptr WaitTreeSynchronizationObject::make( const Kernel::KSynchronizationObject& object) { - switch (object.GetHandleType()) { - case Kernel::HandleType::ReadableEvent: + const auto type = + static_cast(object.GetTypeObj().GetClassToken()); + switch (type) { + case Kernel::KClassTokenGenerator::ObjectType::KReadableEvent: return std::make_unique(static_cast(object)); - case Kernel::HandleType::Thread: + case Kernel::KClassTokenGenerator::ObjectType::KThread: return std::make_unique(static_cast(object)); default: return std::make_unique(object); @@ -206,12 +207,13 @@ std::unique_ptr WaitTreeSynchronizationObject::ma std::vector> WaitTreeSynchronizationObject::GetChildren() const { std::vector> list; - const auto& threads = object.GetWaitingThreadsForDebugging(); + auto threads = object.GetWaitingThreadsForDebugging(); if (threads.empty()) { list.push_back(std::make_unique(tr("waited by no thread"))); } else { - list.push_back(std::make_unique(threads)); + list.push_back(std::make_unique(std::move(threads))); } + return list; } @@ -379,8 +381,8 @@ WaitTreeEvent::WaitTreeEvent(const Kernel::KReadableEvent& object) : WaitTreeSynchronizationObject(object) {} WaitTreeEvent::~WaitTreeEvent() = default; -WaitTreeThreadList::WaitTreeThreadList(const std::vector& list) - : thread_list(list) {} +WaitTreeThreadList::WaitTreeThreadList(std::vector&& list) + : thread_list(std::move(list)) {} WaitTreeThreadList::~WaitTreeThreadList() = default; QString WaitTreeThreadList::GetText() const { diff --git a/src/yuzu/debugger/wait_tree.h b/src/yuzu/debugger/wait_tree.h index bf8120a71..3dd4acab0 100644 --- a/src/yuzu/debugger/wait_tree.h +++ b/src/yuzu/debugger/wait_tree.h @@ -11,8 +11,9 @@ #include #include #include + #include "common/common_types.h" -#include "core/hle/kernel/object.h" +#include "core/hle/kernel/k_auto_object.h" class EmuThread; @@ -149,14 +150,14 @@ public: class WaitTreeThreadList : public WaitTreeExpandableItem { Q_OBJECT public: - explicit WaitTreeThreadList(const std::vector& list); + explicit WaitTreeThreadList(std::vector&& list); ~WaitTreeThreadList() override; QString GetText() const override; std::vector> GetChildren() const override; private: - const std::vector& thread_list; + std::vector thread_list; }; class WaitTreeModel : public QAbstractItemModel { From 2a7eff57a8048933a89c1a8f8d6dced7b5d604f2 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 23 Apr 2021 22:04:28 -0700 Subject: [PATCH 46/82] hle: kernel: Rename Process to KProcess. --- src/core/CMakeLists.txt | 4 +- src/core/arm/dynarmic/arm_dynarmic_64.cpp | 2 +- src/core/core.cpp | 14 ++--- src/core/core.h | 6 +- src/core/file_sys/romfs_factory.cpp | 2 +- src/core/file_sys/savedata_factory.cpp | 2 +- src/core/hle/kernel/handle_table.cpp | 2 +- src/core/hle/kernel/hle_ipc.cpp | 2 +- src/core/hle/kernel/hle_ipc.h | 2 +- src/core/hle/kernel/init/init_slab_setup.cpp | 8 +-- src/core/hle/kernel/init/init_slab_setup.h | 2 +- src/core/hle/kernel/k_auto_object.h | 4 +- .../hle/kernel/k_auto_object_container.cpp | 2 +- src/core/hle/kernel/k_auto_object_container.h | 4 +- src/core/hle/kernel/k_class_token.h | 2 +- src/core/hle/kernel/k_condition_variable.cpp | 2 +- src/core/hle/kernel/k_event.cpp | 4 +- src/core/hle/kernel/k_event.h | 6 +- src/core/hle/kernel/k_page_table.cpp | 2 +- .../hle/kernel/{process.cpp => k_process.cpp} | 62 +++++++++---------- .../hle/kernel/{process.h => k_process.h} | 11 ++-- src/core/hle/kernel/k_scheduler.cpp | 16 ++--- src/core/hle/kernel/k_scheduler.h | 6 +- .../kernel/k_scoped_resource_reservation.h | 6 +- src/core/hle/kernel/k_server_session.cpp | 2 +- src/core/hle/kernel/k_session.cpp | 2 +- src/core/hle/kernel/k_session.h | 2 +- src/core/hle/kernel/k_shared_memory.cpp | 4 +- src/core/hle/kernel/k_shared_memory.h | 8 +-- src/core/hle/kernel/k_thread.cpp | 10 +-- src/core/hle/kernel/k_thread.h | 16 ++--- src/core/hle/kernel/k_transfer_memory.cpp | 4 +- src/core/hle/kernel/k_transfer_memory.h | 6 +- src/core/hle/kernel/kernel.cpp | 26 ++++---- src/core/hle/kernel/kernel.h | 18 +++--- src/core/hle/kernel/svc.cpp | 30 ++++----- src/core/hle/service/acc/acc.cpp | 2 +- src/core/hle/service/am/am.cpp | 2 +- src/core/hle/service/am/applets/error.cpp | 2 +- .../service/am/applets/general_backend.cpp | 2 +- .../hle/service/am/applets/web_browser.cpp | 2 +- src/core/hle/service/aoc/aoc_u.cpp | 2 +- src/core/hle/service/bcat/module.cpp | 2 +- src/core/hle/service/fatal/fatal.cpp | 2 +- .../hle/service/filesystem/filesystem.cpp | 2 +- src/core/hle/service/filesystem/fsp_srv.cpp | 2 +- src/core/hle/service/glue/arp.cpp | 2 +- src/core/hle/service/ldr/ldr.cpp | 8 +-- src/core/hle/service/pctl/module.cpp | 2 +- src/core/hle/service/pm/pm.cpp | 15 ++--- src/core/hle/service/prepo/prepo.cpp | 2 +- src/core/hle/service/service.cpp | 2 +- .../loader/deconstructed_rom_directory.cpp | 4 +- src/core/loader/deconstructed_rom_directory.h | 2 +- src/core/loader/elf.cpp | 4 +- src/core/loader/elf.h | 2 +- src/core/loader/kip.cpp | 4 +- src/core/loader/kip.h | 2 +- src/core/loader/loader.cpp | 2 +- src/core/loader/loader.h | 4 +- src/core/loader/nax.cpp | 4 +- src/core/loader/nax.h | 2 +- src/core/loader/nca.cpp | 4 +- src/core/loader/nca.h | 2 +- src/core/loader/nro.cpp | 8 +-- src/core/loader/nro.h | 6 +- src/core/loader/nso.cpp | 6 +- src/core/loader/nso.h | 6 +- src/core/loader/nsp.cpp | 4 +- src/core/loader/nsp.h | 2 +- src/core/loader/xci.cpp | 4 +- src/core/loader/xci.h | 2 +- src/core/memory.cpp | 34 +++++----- src/core/memory.h | 18 +++--- src/core/memory/cheat_engine.cpp | 2 +- src/core/reporter.cpp | 2 +- src/video_core/memory_manager.cpp | 2 +- .../renderer_opengl/gl_rasterizer.cpp | 2 +- .../renderer_opengl/gl_shader_disk_cache.cpp | 2 +- src/yuzu/bootmanager.cpp | 2 +- src/yuzu/debugger/wait_tree.cpp | 2 +- src/yuzu/main.cpp | 2 +- src/yuzu_cmd/yuzu.cpp | 2 +- 83 files changed, 249 insertions(+), 247 deletions(-) rename src/core/hle/kernel/{process.cpp => k_process.cpp} (90%) rename src/core/hle/kernel/{process.h => k_process.h} (97%) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 4e1387c7e..889a2d2f8 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -197,6 +197,8 @@ add_library(core STATIC hle/kernel/k_port.cpp hle/kernel/k_port.h hle/kernel/k_priority_queue.h + hle/kernel/k_process.cpp + hle/kernel/k_process.h hle/kernel/k_readable_event.cpp hle/kernel/k_readable_event.h hle/kernel/k_resource_limit.cpp @@ -235,8 +237,6 @@ add_library(core STATIC hle/kernel/physical_core.cpp hle/kernel/physical_core.h hle/kernel/physical_memory.h - hle/kernel/process.cpp - hle/kernel/process.h hle/kernel/process_capability.cpp hle/kernel/process_capability.h hle/kernel/service_thread.cpp diff --git a/src/core/arm/dynarmic/arm_dynarmic_64.cpp b/src/core/arm/dynarmic/arm_dynarmic_64.cpp index 4ff72abd8..653bb7a77 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_64.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic_64.cpp @@ -16,8 +16,8 @@ #include "core/core.h" #include "core/core_timing.h" #include "core/hardware_properties.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_scheduler.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc.h" #include "core/memory.h" diff --git a/src/core/core.cpp b/src/core/core.cpp index 4bb96d77d..434bf3262 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -28,11 +28,11 @@ #include "core/file_sys/vfs_real.h" #include "core/hardware_interrupt_manager.h" #include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" -#include "core/hle/kernel/process.h" #include "core/hle/service/am/applets/applets.h" #include "core/hle/service/apm/controller.h" #include "core/hle/service/filesystem/filesystem.h" @@ -233,9 +233,9 @@ struct System::Impl { } telemetry_session->AddInitialInfo(*app_loader, fs_controller, *content_provider); - auto main_process = Kernel::Process::Create(system.Kernel()); - ASSERT(Kernel::Process::Initialize(main_process, system, "main", - Kernel::Process::ProcessType::Userland) + auto main_process = Kernel::KProcess::Create(system.Kernel()); + ASSERT(Kernel::KProcess::Initialize(main_process, system, "main", + Kernel::KProcess::ProcessType::Userland) .IsSuccess()); main_process->Open(); const auto [load_result, load_parameters] = app_loader->Load(*main_process, system); @@ -326,7 +326,7 @@ struct System::Impl { return app_loader->ReadTitle(out); } - void AddGlueRegistrationForProcess(Loader::AppLoader& loader, Kernel::Process& process) { + void AddGlueRegistrationForProcess(Loader::AppLoader& loader, Kernel::KProcess& process) { std::vector nacp_data; FileSys::NACP nacp; if (loader.ReadControlData(nacp) == Loader::ResultStatus::Success) { @@ -517,7 +517,7 @@ const Kernel::GlobalSchedulerContext& System::GlobalSchedulerContext() const { return impl->kernel.GlobalSchedulerContext(); } -Kernel::Process* System::CurrentProcess() { +Kernel::KProcess* System::CurrentProcess() { return impl->kernel.CurrentProcess(); } @@ -529,7 +529,7 @@ const Core::DeviceMemory& System::DeviceMemory() const { return *impl->device_memory; } -const Kernel::Process* System::CurrentProcess() const { +const Kernel::KProcess* System::CurrentProcess() const { return impl->kernel.CurrentProcess(); } diff --git a/src/core/core.h b/src/core/core.h index 16e191266..8b93ba998 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -28,7 +28,7 @@ namespace Kernel { class GlobalSchedulerContext; class KernelCore; class PhysicalCore; -class Process; +class KProcess; class KScheduler; } // namespace Kernel @@ -263,10 +263,10 @@ public: [[nodiscard]] const Core::DeviceMemory& DeviceMemory() const; /// Provides a pointer to the current process - [[nodiscard]] Kernel::Process* CurrentProcess(); + [[nodiscard]] Kernel::KProcess* CurrentProcess(); /// Provides a constant pointer to the current process. - [[nodiscard]] const Kernel::Process* CurrentProcess() const; + [[nodiscard]] const Kernel::KProcess* CurrentProcess() const; /// Provides a reference to the core timing instance. [[nodiscard]] Timing::CoreTiming& CoreTiming(); diff --git a/src/core/file_sys/romfs_factory.cpp b/src/core/file_sys/romfs_factory.cpp index de6ab721d..aa7f3072f 100644 --- a/src/core/file_sys/romfs_factory.cpp +++ b/src/core/file_sys/romfs_factory.cpp @@ -13,7 +13,7 @@ #include "core/file_sys/patch_manager.h" #include "core/file_sys/registered_cache.h" #include "core/file_sys/romfs_factory.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/loader/loader.h" diff --git a/src/core/file_sys/savedata_factory.cpp b/src/core/file_sys/savedata_factory.cpp index fa68af3a8..f973d1d21 100644 --- a/src/core/file_sys/savedata_factory.cpp +++ b/src/core/file_sys/savedata_factory.cpp @@ -9,7 +9,7 @@ #include "core/core.h" #include "core/file_sys/savedata_factory.h" #include "core/file_sys/vfs.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" namespace FileSys { diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index cd752da4e..16c528f5b 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -7,10 +7,10 @@ #include "common/logging/log.h" #include "core/core.h" #include "core/hle/kernel/handle_table.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" namespace Kernel { diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index a11528f28..69190286d 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -16,6 +16,7 @@ #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/hle_ipc.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h" @@ -23,7 +24,6 @@ #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/time_manager.h" #include "core/memory.h" diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index 7f7ab74dd..4b92ba655 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -38,7 +38,7 @@ class Domain; class HandleTable; class HLERequestContext; class KernelCore; -class Process; +class KProcess; class KServerSession; class KThread; class KReadableEvent; diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index f8c255732..04e481a0a 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -13,6 +13,7 @@ #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" #include "core/hle/kernel/k_port.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_shared_memory.h" @@ -20,7 +21,6 @@ #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/memory_types.h" -#include "core/hle/kernel/process.h" #include "core/memory.h" namespace Kernel::Init { @@ -28,7 +28,7 @@ namespace Kernel::Init { #define SLAB_COUNT(CLASS) g_slab_resource_counts.num_##CLASS #define FOREACH_SLAB_TYPE(HANDLER, ...) \ - HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ + HANDLER(KProcess, (SLAB_COUNT(KProcess)), ##__VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ HANDLER(KPort, (SLAB_COUNT(KPort)), ##__VA_ARGS__) \ @@ -48,7 +48,7 @@ enum KSlabType : u32 { #undef DEFINE_SLAB_TYPE_ENUM_MEMBER // Constexpr counts. -constexpr size_t SlabCountProcess = 80; +constexpr size_t SlabCountKProcess = 80; constexpr size_t SlabCountKThread = 800; constexpr size_t SlabCountKEvent = 700; constexpr size_t SlabCountKInterruptEvent = 100; @@ -69,7 +69,7 @@ constexpr size_t SlabCountExtraKThread = 160; // Global to hold our resource counts. KSlabResourceCounts g_slab_resource_counts = { - .num_Process = SlabCountProcess, + .num_KProcess = SlabCountKProcess, .num_KThread = SlabCountKThread, .num_KEvent = SlabCountKEvent, .num_KInterruptEvent = SlabCountKInterruptEvent, diff --git a/src/core/hle/kernel/init/init_slab_setup.h b/src/core/hle/kernel/init/init_slab_setup.h index 8876678b3..6418b97ac 100644 --- a/src/core/hle/kernel/init/init_slab_setup.h +++ b/src/core/hle/kernel/init/init_slab_setup.h @@ -15,7 +15,7 @@ class KMemoryLayout; namespace Kernel::Init { struct KSlabResourceCounts { - size_t num_Process; + size_t num_KProcess; size_t num_KThread; size_t num_KEvent; size_t num_KInterruptEvent; diff --git a/src/core/hle/kernel/k_auto_object.h b/src/core/hle/kernel/k_auto_object.h index fd6405a0e..5a180b7dc 100644 --- a/src/core/hle/kernel/k_auto_object.h +++ b/src/core/hle/kernel/k_auto_object.h @@ -15,7 +15,7 @@ namespace Kernel { class KernelCore; -class Process; +class KProcess; using Handle = u32; @@ -106,7 +106,7 @@ public: // Finalize is responsible for cleaning up resource, but does not destroy the object. virtual void Finalize() {} - virtual Process* GetOwner() const { + virtual KProcess* GetOwner() const { return nullptr; } diff --git a/src/core/hle/kernel/k_auto_object_container.cpp b/src/core/hle/kernel/k_auto_object_container.cpp index 9ba8a54c7..85d03ebe3 100644 --- a/src/core/hle/kernel/k_auto_object_container.cpp +++ b/src/core/hle/kernel/k_auto_object_container.cpp @@ -18,7 +18,7 @@ void KAutoObjectWithListContainer::Unregister(KAutoObjectWithList* obj) { m_object_list.erase(m_object_list.iterator_to(*obj)); } -size_t KAutoObjectWithListContainer::GetOwnedCount(Process* owner) { +size_t KAutoObjectWithListContainer::GetOwnedCount(KProcess* owner) { KScopedLightLock lk(m_lock); size_t count = 0; diff --git a/src/core/hle/kernel/k_auto_object_container.h b/src/core/hle/kernel/k_auto_object_container.h index 4b599b7c3..6d1cd4862 100644 --- a/src/core/hle/kernel/k_auto_object_container.h +++ b/src/core/hle/kernel/k_auto_object_container.h @@ -16,7 +16,7 @@ namespace Kernel { class KernelCore; -class Process; +class KProcess; class KAutoObjectWithListContainer { NON_COPYABLE(KAutoObjectWithListContainer); @@ -66,7 +66,7 @@ public: void Register(KAutoObjectWithList* obj); void Unregister(KAutoObjectWithList* obj); - size_t GetOwnedCount(Process* owner); + size_t GetOwnedCount(KProcess* owner); }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_class_token.h b/src/core/hle/kernel/k_class_token.h index 89b80a341..fb4307cd0 100644 --- a/src/core/hle/kernel/k_class_token.h +++ b/src/core/hle/kernel/k_class_token.h @@ -97,7 +97,7 @@ public: KServerSession, KClientPort, KClientSession, - Process, + KProcess, KResourceLimit, KLightSession, KPort, diff --git a/src/core/hle/kernel/k_condition_variable.cpp b/src/core/hle/kernel/k_condition_variable.cpp index 72565af05..a9738f7ce 100644 --- a/src/core/hle/kernel/k_condition_variable.cpp +++ b/src/core/hle/kernel/k_condition_variable.cpp @@ -8,12 +8,12 @@ #include "core/core.h" #include "core/hle/kernel/k_condition_variable.h" #include "core/hle/kernel/k_linked_list.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h" #include "core/hle/kernel/k_synchronization_object.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_common.h" #include "core/hle/kernel/svc_results.h" #include "core/memory.h" diff --git a/src/core/hle/kernel/k_event.cpp b/src/core/hle/kernel/k_event.cpp index fdec0c36f..986355b78 100644 --- a/src/core/hle/kernel/k_event.cpp +++ b/src/core/hle/kernel/k_event.cpp @@ -3,8 +3,8 @@ // Refer to the license.txt file included. #include "core/hle/kernel/k_event.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" -#include "core/hle/kernel/process.h" namespace Kernel { @@ -45,7 +45,7 @@ void KEvent::Finalize() { void KEvent::PostDestroy(uintptr_t arg) { // Release the event count resource the owner process holds. - Process* owner = reinterpret_cast(arg); + KProcess* owner = reinterpret_cast(arg); if (owner) { owner->GetResourceLimit()->Release(LimitableResource::Events, 1); owner->Close(); diff --git a/src/core/hle/kernel/k_event.h b/src/core/hle/kernel/k_event.h index f0b89f882..4ca869930 100644 --- a/src/core/hle/kernel/k_event.h +++ b/src/core/hle/kernel/k_event.h @@ -13,7 +13,7 @@ namespace Kernel { class KernelCore; class KReadableEvent; class KWritableEvent; -class Process; +class KProcess; class KEvent final : public KAutoObjectWithSlabHeapAndContainer { KERNEL_AUTOOBJECT_TRAITS(KEvent, KAutoObject); @@ -36,7 +36,7 @@ public: static void PostDestroy(uintptr_t arg); - virtual Process* GetOwner() const override { + virtual KProcess* GetOwner() const override { return owner; } @@ -51,7 +51,7 @@ public: private: KReadableEvent readable_event; KWritableEvent writable_event; - Process* owner{}; + KProcess* owner{}; bool initialized{}; }; diff --git a/src/core/hle/kernel/k_page_table.cpp b/src/core/hle/kernel/k_page_table.cpp index 5f60b95cd..2f33cb6c1 100644 --- a/src/core/hle/kernel/k_page_table.cpp +++ b/src/core/hle/kernel/k_page_table.cpp @@ -11,11 +11,11 @@ #include "core/hle/kernel/k_memory_block_manager.h" #include "core/hle/kernel/k_page_linked_list.h" #include "core/hle/kernel/k_page_table.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_scoped_resource_reservation.h" #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/memory.h" diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/k_process.cpp similarity index 90% rename from src/core/hle/kernel/process.cpp rename to src/core/hle/kernel/k_process.cpp index 315640bea..edc3b5175 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/k_process.cpp @@ -17,13 +17,13 @@ #include "core/hle/kernel/code_set.h" #include "core/hle/kernel/k_memory_block_manager.h" #include "core/hle/kernel/k_page_table.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_resource_reservation.h" #include "core/hle/kernel/k_slab_heap.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/lock.h" #include "core/memory.h" @@ -37,7 +37,7 @@ namespace { * @param owner_process The parent process for the main thread * @param priority The priority to give the main thread */ -void SetupMainThread(Core::System& system, Process& owner_process, u32 priority, VAddr stack_top) { +void SetupMainThread(Core::System& system, KProcess& owner_process, u32 priority, VAddr stack_top) { const VAddr entry_point = owner_process.PageTable().GetCodeRegionStart(); ASSERT(owner_process.GetResourceLimit()->Reserve(LimitableResource::Threads, 1)); @@ -117,8 +117,8 @@ private: std::bitset is_slot_used; }; -ResultCode Process::Initialize(Process* process, Core::System& system, std::string name, - ProcessType type) { +ResultCode KProcess::Initialize(KProcess* process, Core::System& system, std::string name, + ProcessType type) { auto& kernel = system.Kernel(); process->name = std::move(name); @@ -144,11 +144,11 @@ ResultCode Process::Initialize(Process* process, Core::System& system, std::stri return RESULT_SUCCESS; } -KResourceLimit* Process::GetResourceLimit() const { +KResourceLimit* KProcess::GetResourceLimit() const { return resource_limit; } -void Process::IncrementThreadCount() { +void KProcess::IncrementThreadCount() { ASSERT(num_threads >= 0); num_created_threads++; @@ -157,7 +157,7 @@ void Process::IncrementThreadCount() { } } -void Process::DecrementThreadCount() { +void KProcess::DecrementThreadCount() { ASSERT(num_threads > 0); if (const auto count = --num_threads; count == 0) { @@ -165,7 +165,7 @@ void Process::DecrementThreadCount() { } } -u64 Process::GetTotalPhysicalMemoryAvailable() const { +u64 KProcess::GetTotalPhysicalMemoryAvailable() const { const u64 capacity{resource_limit->GetFreeValue(LimitableResource::PhysicalMemory) + page_table->GetTotalHeapSize() + GetSystemResourceSize() + image_size + main_thread_stack_size}; @@ -179,20 +179,20 @@ u64 Process::GetTotalPhysicalMemoryAvailable() const { return memory_usage_capacity; } -u64 Process::GetTotalPhysicalMemoryAvailableWithoutSystemResource() const { +u64 KProcess::GetTotalPhysicalMemoryAvailableWithoutSystemResource() const { return GetTotalPhysicalMemoryAvailable() - GetSystemResourceSize(); } -u64 Process::GetTotalPhysicalMemoryUsed() const { +u64 KProcess::GetTotalPhysicalMemoryUsed() const { return image_size + main_thread_stack_size + page_table->GetTotalHeapSize() + GetSystemResourceSize(); } -u64 Process::GetTotalPhysicalMemoryUsedWithoutSystemResource() const { +u64 KProcess::GetTotalPhysicalMemoryUsedWithoutSystemResource() const { return GetTotalPhysicalMemoryUsed() - GetSystemResourceUsage(); } -bool Process::ReleaseUserException(KThread* thread) { +bool KProcess::ReleaseUserException(KThread* thread) { KScopedSchedulerLock sl{kernel}; if (exception_thread == thread) { @@ -217,7 +217,7 @@ bool Process::ReleaseUserException(KThread* thread) { } } -void Process::PinCurrentThread() { +void KProcess::PinCurrentThread() { ASSERT(kernel.GlobalSchedulerContext().IsLocked()); // Get the current thread. @@ -232,7 +232,7 @@ void Process::PinCurrentThread() { KScheduler::SetSchedulerUpdateNeeded(kernel); } -void Process::UnpinCurrentThread() { +void KProcess::UnpinCurrentThread() { ASSERT(kernel.GlobalSchedulerContext().IsLocked()); // Get the current thread. @@ -247,15 +247,15 @@ void Process::UnpinCurrentThread() { KScheduler::SetSchedulerUpdateNeeded(kernel); } -void Process::RegisterThread(const KThread* thread) { +void KProcess::RegisterThread(const KThread* thread) { thread_list.push_back(thread); } -void Process::UnregisterThread(const KThread* thread) { +void KProcess::UnregisterThread(const KThread* thread) { thread_list.remove(thread); } -ResultCode Process::Reset() { +ResultCode KProcess::Reset() { // Lock the process and the scheduler. KScopedLightLock lk(state_lock); KScopedSchedulerLock sl{kernel}; @@ -269,8 +269,8 @@ ResultCode Process::Reset() { return RESULT_SUCCESS; } -ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, - std::size_t code_size) { +ResultCode KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, + std::size_t code_size) { program_id = metadata.GetTitleID(); ideal_core = metadata.GetMainThreadCore(); is_64bit_process = metadata.Is64BitProgram(); @@ -332,7 +332,7 @@ ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, return handle_table.SetSize(capabilities.GetHandleTableSize()); } -void Process::Run(s32 main_thread_priority, u64 stack_size) { +void KProcess::Run(s32 main_thread_priority, u64 stack_size) { AllocateMainThreadStack(stack_size); resource_limit->Reserve(LimitableResource::Threads, 1); resource_limit->Reserve(LimitableResource::PhysicalMemory, main_thread_stack_size); @@ -345,7 +345,7 @@ void Process::Run(s32 main_thread_priority, u64 stack_size) { SetupMainThread(kernel.System(), *this, main_thread_priority, main_thread_stack_top); } -void Process::PrepareForTermination() { +void KProcess::PrepareForTermination() { ChangeStatus(ProcessStatus::Exiting); const auto stop_threads = [this](const std::vector& thread_list) { @@ -377,14 +377,14 @@ void Process::PrepareForTermination() { ChangeStatus(ProcessStatus::Exited); } -void Process::Finalize() { +void KProcess::Finalize() { // Release memory to the resource limit. if (resource_limit != nullptr) { resource_limit->Close(); } // Perform inherited finalization. - KAutoObjectWithSlabHeapAndContainer::Finalize(); + KAutoObjectWithSlabHeapAndContainer::Finalize(); } /** @@ -400,7 +400,7 @@ static auto FindTLSPageWithAvailableSlots(std::vector& tls_pages) { [](const auto& page) { return page.HasAvailableSlots(); }); } -VAddr Process::CreateTLSRegion() { +VAddr KProcess::CreateTLSRegion() { KScopedSchedulerLock lock(kernel); if (auto tls_page_iter{FindTLSPageWithAvailableSlots(tls_pages)}; tls_page_iter != tls_pages.cend()) { @@ -431,7 +431,7 @@ VAddr Process::CreateTLSRegion() { return *reserve_result; } -void Process::FreeTLSRegion(VAddr tls_address) { +void KProcess::FreeTLSRegion(VAddr tls_address) { KScopedSchedulerLock lock(kernel); const VAddr aligned_address = Common::AlignDown(tls_address, Core::Memory::PAGE_SIZE); auto iter = @@ -446,7 +446,7 @@ void Process::FreeTLSRegion(VAddr tls_address) { iter->ReleaseSlot(tls_address); } -void Process::LoadModule(CodeSet code_set, VAddr base_addr) { +void KProcess::LoadModule(CodeSet code_set, VAddr base_addr) { std::lock_guard lock{HLE::g_hle_lock}; const auto ReprotectSegment = [&](const CodeSet::Segment& segment, KMemoryPermission permission) { @@ -461,19 +461,19 @@ void Process::LoadModule(CodeSet code_set, VAddr base_addr) { ReprotectSegment(code_set.DataSegment(), KMemoryPermission::ReadAndWrite); } -bool Process::IsSignaled() const { +bool KProcess::IsSignaled() const { ASSERT(kernel.GlobalSchedulerContext().IsLocked()); return is_signaled; } -Process::Process(KernelCore& kernel) +KProcess::KProcess(KernelCore& kernel) : KAutoObjectWithSlabHeapAndContainer{kernel}, page_table{std::make_unique(kernel.System())}, handle_table{kernel}, address_arbiter{kernel.System()}, condition_var{kernel.System()}, state_lock{kernel} {} -Process::~Process() = default; +KProcess::~KProcess() = default; -void Process::ChangeStatus(ProcessStatus new_status) { +void KProcess::ChangeStatus(ProcessStatus new_status) { if (status == new_status) { return; } @@ -483,7 +483,7 @@ void Process::ChangeStatus(ProcessStatus new_status) { NotifyAvailable(); } -ResultCode Process::AllocateMainThreadStack(std::size_t stack_size) { +ResultCode KProcess::AllocateMainThreadStack(std::size_t stack_size) { ASSERT(stack_size); // The kernel always ensures that the given stack size is page aligned. diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/k_process.h similarity index 97% rename from src/core/hle/kernel/process.h rename to src/core/hle/kernel/k_process.h index b775e1fd0..961c0d9ba 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/k_process.h @@ -62,12 +62,13 @@ enum class ProcessStatus { DebugBreak, }; -class Process final : public KAutoObjectWithSlabHeapAndContainer { - KERNEL_AUTOOBJECT_TRAITS(Process, KSynchronizationObject); +class KProcess final + : public KAutoObjectWithSlabHeapAndContainer { + KERNEL_AUTOOBJECT_TRAITS(KProcess, KSynchronizationObject); public: - explicit Process(KernelCore& kernel); - ~Process() override; + explicit KProcess(KernelCore& kernel); + ~KProcess() override; enum : u64 { /// Lowest allowed process ID for a kernel initial process. @@ -89,7 +90,7 @@ public: static constexpr std::size_t RANDOM_ENTROPY_SIZE = 4; - static ResultCode Initialize(Process* process, Core::System& system, std::string name, + static ResultCode Initialize(KProcess* process, Core::System& system, std::string name, ProcessType type); /// Gets a reference to the process' page table. diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index 38c6b50fa..0115fe6d1 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -15,12 +15,12 @@ #include "core/core.h" #include "core/core_timing.h" #include "core/cpu_manager.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/time_manager.h" namespace Kernel { @@ -71,7 +71,7 @@ u64 KScheduler::UpdateHighestPriorityThread(KThread* highest_thread) { } if (state.should_count_idle) { if (highest_thread != nullptr) { - if (Process* process = highest_thread->GetOwnerProcess(); process != nullptr) { + if (KProcess* process = highest_thread->GetOwnerProcess(); process != nullptr) { process->SetRunningThread(core_id, highest_thread, state.idle_count); } } else { @@ -104,7 +104,7 @@ u64 KScheduler::UpdateHighestPriorityThreadsImpl(KernelCore& kernel) { if (top_thread != nullptr) { // If the thread has no waiters, we need to check if the process has a thread pinned. if (top_thread->GetNumKernelWaiters() == 0) { - if (Process* parent = top_thread->GetOwnerProcess(); parent != nullptr) { + if (KProcess* parent = top_thread->GetOwnerProcess(); parent != nullptr) { if (KThread* pinned = parent->GetPinnedThread(static_cast(core_id)); pinned != nullptr && pinned != top_thread) { // We prefer our parent's pinned thread if possible. However, we also don't @@ -411,7 +411,7 @@ void KScheduler::YieldWithoutCoreMigration(KernelCore& kernel) { // Get the current thread and process. KThread& cur_thread = Kernel::GetCurrentThread(kernel); - Process& cur_process = *kernel.CurrentProcess(); + KProcess& cur_process = *kernel.CurrentProcess(); // If the thread's yield count matches, there's nothing for us to do. if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) { @@ -450,7 +450,7 @@ void KScheduler::YieldWithCoreMigration(KernelCore& kernel) { // Get the current thread and process. KThread& cur_thread = Kernel::GetCurrentThread(kernel); - Process& cur_process = *kernel.CurrentProcess(); + KProcess& cur_process = *kernel.CurrentProcess(); // If the thread's yield count matches, there's nothing for us to do. if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) { @@ -538,7 +538,7 @@ void KScheduler::YieldToAnyThread(KernelCore& kernel) { // Get the current thread and process. KThread& cur_thread = Kernel::GetCurrentThread(kernel); - Process& cur_process = *kernel.CurrentProcess(); + KProcess& cur_process = *kernel.CurrentProcess(); // If the thread's yield count matches, there's nothing for us to do. if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) { @@ -724,7 +724,7 @@ void KScheduler::ScheduleImpl() { current_thread.store(next_thread); - Process* const previous_process = system.Kernel().CurrentProcess(); + KProcess* const previous_process = system.Kernel().CurrentProcess(); UpdateLastContextSwitchTime(previous_thread, previous_process); @@ -780,7 +780,7 @@ void KScheduler::SwitchToCurrent() { } } -void KScheduler::UpdateLastContextSwitchTime(KThread* thread, Process* process) { +void KScheduler::UpdateLastContextSwitchTime(KThread* thread, KProcess* process) { const u64 prev_switch_ticks = last_context_switch_time; const u64 most_recent_switch_ticks = system.CoreTiming().GetCPUTicks(); const u64 update_ticks = most_recent_switch_ticks - prev_switch_ticks; diff --git a/src/core/hle/kernel/k_scheduler.h b/src/core/hle/kernel/k_scheduler.h index 01387b892..282c1863b 100644 --- a/src/core/hle/kernel/k_scheduler.h +++ b/src/core/hle/kernel/k_scheduler.h @@ -24,7 +24,7 @@ class System; namespace Kernel { class KernelCore; -class Process; +class KProcess; class SchedulerLock; class KThread; @@ -165,7 +165,7 @@ private: * most recent tick count retrieved. No special arithmetic is * applied to it. */ - void UpdateLastContextSwitchTime(KThread* thread, Process* process); + void UpdateLastContextSwitchTime(KThread* thread, KProcess* process); static void OnSwitch(void* this_scheduler); void SwitchToCurrent(); @@ -197,7 +197,7 @@ private: class [[nodiscard]] KScopedSchedulerLock : KScopedLock { public: - explicit KScopedSchedulerLock(KernelCore & kernel); + explicit KScopedSchedulerLock(KernelCore& kernel); ~KScopedSchedulerLock(); }; diff --git a/src/core/hle/kernel/k_scoped_resource_reservation.h b/src/core/hle/kernel/k_scoped_resource_reservation.h index b160587c5..07272075d 100644 --- a/src/core/hle/kernel/k_scoped_resource_reservation.h +++ b/src/core/hle/kernel/k_scoped_resource_reservation.h @@ -8,8 +8,8 @@ #pragma once #include "common/common_types.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" -#include "core/hle/kernel/process.h" namespace Kernel { @@ -33,10 +33,10 @@ public: } } - explicit KScopedResourceReservation(const Process* p, LimitableResource r, s64 v, s64 t) + explicit KScopedResourceReservation(const KProcess* p, LimitableResource r, s64 v, s64 t) : KScopedResourceReservation(p->GetResourceLimit(), r, v, t) {} - explicit KScopedResourceReservation(const Process* p, LimitableResource r, s64 v = 1) + explicit KScopedResourceReservation(const KProcess* p, LimitableResource r, s64 v = 1) : KScopedResourceReservation(p->GetResourceLimit(), r, v) {} ~KScopedResourceReservation() noexcept { diff --git a/src/core/hle/kernel/k_server_session.cpp b/src/core/hle/kernel/k_server_session.cpp index 863f9aa5f..3bc259693 100644 --- a/src/core/hle/kernel/k_server_session.cpp +++ b/src/core/hle/kernel/k_server_session.cpp @@ -13,12 +13,12 @@ #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_process.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/memory.h" namespace Kernel { diff --git a/src/core/hle/kernel/k_session.cpp b/src/core/hle/kernel/k_session.cpp index 6f5947ce7..5e629d446 100644 --- a/src/core/hle/kernel/k_session.cpp +++ b/src/core/hle/kernel/k_session.cpp @@ -71,7 +71,7 @@ void KSession::OnClientClosed() { void KSession::PostDestroy(uintptr_t arg) { // Release the session count resource the owner process holds. - Process* owner = reinterpret_cast(arg); + KProcess* owner = reinterpret_cast(arg); owner->GetResourceLimit()->Release(LimitableResource::Sessions, 1); owner->Close(); } diff --git a/src/core/hle/kernel/k_session.h b/src/core/hle/kernel/k_session.h index f29195fa0..d50e21f3f 100644 --- a/src/core/hle/kernel/k_session.h +++ b/src/core/hle/kernel/k_session.h @@ -89,7 +89,7 @@ private: std::atomic::type> atomic_state{ static_cast::type>(State::Invalid)}; KClientPort* port{}; - Process* process{}; + KProcess* process{}; bool initialized{}; }; diff --git a/src/core/hle/kernel/k_shared_memory.cpp b/src/core/hle/kernel/k_shared_memory.cpp index e91bc94bd..f137a182a 100644 --- a/src/core/hle/kernel/k_shared_memory.cpp +++ b/src/core/hle/kernel/k_shared_memory.cpp @@ -19,7 +19,7 @@ KSharedMemory::~KSharedMemory() { } ResultCode KSharedMemory::Initialize(KernelCore& kernel_, Core::DeviceMemory& device_memory_, - Process* owner_process_, KPageLinkedList&& page_list_, + KProcess* owner_process_, KPageLinkedList&& page_list_, KMemoryPermission owner_permission_, KMemoryPermission user_permission_, PAddr physical_address_, std::size_t size_, std::string name_) { @@ -74,7 +74,7 @@ void KSharedMemory::Finalize() { KAutoObjectWithSlabHeapAndContainer::Finalize(); } -ResultCode KSharedMemory::Map(Process& target_process, VAddr address, std::size_t size, +ResultCode KSharedMemory::Map(KProcess& target_process, VAddr address, std::size_t size, KMemoryPermission permissions) { const u64 page_count{(size + PageSize - 1) / PageSize}; diff --git a/src/core/hle/kernel/k_shared_memory.h b/src/core/hle/kernel/k_shared_memory.h index 9547546a5..2d315c916 100644 --- a/src/core/hle/kernel/k_shared_memory.h +++ b/src/core/hle/kernel/k_shared_memory.h @@ -11,7 +11,7 @@ #include "core/device_memory.h" #include "core/hle/kernel/k_memory_block.h" #include "core/hle/kernel/k_page_linked_list.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/slab_helpers.h" #include "core/hle/result.h" @@ -28,7 +28,7 @@ public: ~KSharedMemory() override; ResultCode Initialize(KernelCore& kernel_, Core::DeviceMemory& device_memory_, - Process* owner_process_, KPageLinkedList&& page_list_, + KProcess* owner_process_, KPageLinkedList&& page_list_, KMemoryPermission owner_permission_, KMemoryPermission user_permission_, PAddr physical_address_, std::size_t size_, std::string name_); @@ -39,7 +39,7 @@ public: * @param size Size of the shared memory block to map * @param permissions Memory block map permissions (specified by SVC field) */ - ResultCode Map(Process& target_process, VAddr address, std::size_t size, + ResultCode Map(KProcess& target_process, VAddr address, std::size_t size, KMemoryPermission permissions); /** @@ -69,7 +69,7 @@ public: private: Core::DeviceMemory* device_memory; - Process* owner_process{}; + KProcess* owner_process{}; KPageLinkedList page_list; KMemoryPermission owner_permission{}; KMemoryPermission user_permission{}; diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp index c59f3113c..3de0157ac 100644 --- a/src/core/hle/kernel/k_thread.cpp +++ b/src/core/hle/kernel/k_thread.cpp @@ -21,13 +21,13 @@ #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/k_condition_variable.h" #include "core/hle/kernel/k_memory_layout.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_thread_queue.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/time_manager.h" #include "core/hle/result.h" @@ -65,7 +65,7 @@ KThread::KThread(KernelCore& kernel) KThread::~KThread() = default; ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_stack_top, s32 prio, - s32 virt_core, Process* owner, ThreadType type) { + s32 virt_core, KProcess* owner, ThreadType type) { // Assert parameters are valid. ASSERT((type == ThreadType::Main) || (Svc::HighestThreadPriority <= prio && prio <= Svc::LowestThreadPriority)); @@ -209,7 +209,7 @@ ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_s } ResultCode KThread::InitializeThread(KThread* thread, KThreadFunction func, uintptr_t arg, - VAddr user_stack_top, s32 prio, s32 core, Process* owner, + VAddr user_stack_top, s32 prio, s32 core, KProcess* owner, ThreadType type, std::function&& init_func, void* init_func_parameter) { // Initialize the thread. @@ -242,7 +242,7 @@ ResultCode KThread::InitializeHighPriorityThread(Core::System& system, KThread* ResultCode KThread::InitializeUserThread(Core::System& system, KThread* thread, KThreadFunction func, uintptr_t arg, VAddr user_stack_top, - s32 prio, s32 virt_core, Process* owner) { + s32 prio, s32 virt_core, KProcess* owner) { system.Kernel().GlobalSchedulerContext().AddThread(thread); return InitializeThread(thread, func, arg, user_stack_top, prio, virt_core, owner, ThreadType::User, Core::CpuManager::GetGuestThreadStartFunc(), @@ -250,7 +250,7 @@ ResultCode KThread::InitializeUserThread(Core::System& system, KThread* thread, } void KThread::PostDestroy(uintptr_t arg) { - Process* owner = reinterpret_cast(arg & ~1ULL); + KProcess* owner = reinterpret_cast(arg & ~1ULL); const bool resource_limit_release_hint = (arg & 1); const s64 hint_value = (resource_limit_release_hint ? 0 : 1); if (owner != nullptr) { diff --git a/src/core/hle/kernel/k_thread.h b/src/core/hle/kernel/k_thread.h index 5b943b18b..4145ef56c 100644 --- a/src/core/hle/kernel/k_thread.h +++ b/src/core/hle/kernel/k_thread.h @@ -37,7 +37,7 @@ namespace Kernel { class GlobalSchedulerContext; class KernelCore; -class Process; +class KProcess; class KScheduler; class KThreadQueue; @@ -105,7 +105,7 @@ class KThread final : public KAutoObjectWithSlabHeapAndContainer&& init_func, void* init_func_parameter); @@ -669,7 +669,7 @@ private: std::atomic cpu_time{}; KSynchronizationObject* synced_object{}; VAddr address_key{}; - Process* parent{}; + KProcess* parent{}; VAddr kernel_stack_top{}; u32* light_ipc_data{}; VAddr tls_address{}; diff --git a/src/core/hle/kernel/k_transfer_memory.cpp b/src/core/hle/kernel/k_transfer_memory.cpp index 09c067f95..201617d32 100644 --- a/src/core/hle/kernel/k_transfer_memory.cpp +++ b/src/core/hle/kernel/k_transfer_memory.cpp @@ -2,10 +2,10 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" namespace Kernel { @@ -37,7 +37,7 @@ void KTransferMemory::Finalize() { } void KTransferMemory::PostDestroy(uintptr_t arg) { - Process* owner = reinterpret_cast(arg); + KProcess* owner = reinterpret_cast(arg); owner->GetResourceLimit()->Release(LimitableResource::TransferMemory, 1); owner->Close(); } diff --git a/src/core/hle/kernel/k_transfer_memory.h b/src/core/hle/kernel/k_transfer_memory.h index 1e4fa9323..f56398b9c 100644 --- a/src/core/hle/kernel/k_transfer_memory.h +++ b/src/core/hle/kernel/k_transfer_memory.h @@ -19,7 +19,7 @@ class Memory; namespace Kernel { class KernelCore; -class Process; +class KProcess; class KTransferMemory final : public KAutoObjectWithSlabHeapAndContainer { @@ -43,7 +43,7 @@ public: static void PostDestroy(uintptr_t arg); - Process* GetOwner() const { + KProcess* GetOwner() const { return owner; } @@ -56,7 +56,7 @@ public: } private: - Process* owner{}; + KProcess* owner{}; VAddr address{}; Svc::MemoryPermission owner_perm{}; size_t size{}; diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 409bcfaa0..718525c4c 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -31,6 +31,7 @@ #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_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_shared_memory.h" @@ -38,7 +39,6 @@ #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/service_thread.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/time_manager.h" @@ -98,8 +98,8 @@ struct KernelCore::Impl { service_threads.clear(); next_object_id = 0; - next_kernel_process_id = Process::InitialKIPIDMin; - next_user_process_id = Process::ProcessIDMin; + next_kernel_process_id = KProcess::InitialKIPIDMin; + next_user_process_id = KProcess::ProcessIDMin; next_thread_id = 1; for (s32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { @@ -220,7 +220,7 @@ struct KernelCore::Impl { } } - void MakeCurrentProcess(Process* process) { + void MakeCurrentProcess(KProcess* process) { current_process = process; if (process == nullptr) { return; @@ -632,13 +632,13 @@ struct KernelCore::Impl { } std::atomic next_object_id{0}; - std::atomic next_kernel_process_id{Process::InitialKIPIDMin}; - std::atomic next_user_process_id{Process::ProcessIDMin}; + std::atomic next_kernel_process_id{KProcess::InitialKIPIDMin}; + std::atomic next_user_process_id{KProcess::ProcessIDMin}; std::atomic next_thread_id{1}; // Lists all processes that exist in the current session. - std::vector process_list; - Process* current_process{}; + std::vector process_list; + KProcess* current_process{}; std::unique_ptr global_scheduler_context; Kernel::TimeManager time_manager; @@ -725,23 +725,23 @@ KScopedAutoObject KernelCore::RetrieveThreadFromGlobalHandleTable(Handl return impl->global_handle_table.GetObject(handle); } -void KernelCore::AppendNewProcess(Process* process) { +void KernelCore::AppendNewProcess(KProcess* process) { impl->process_list.push_back(process); } -void KernelCore::MakeCurrentProcess(Process* process) { +void KernelCore::MakeCurrentProcess(KProcess* process) { impl->MakeCurrentProcess(process); } -Process* KernelCore::CurrentProcess() { +KProcess* KernelCore::CurrentProcess() { return impl->current_process; } -const Process* KernelCore::CurrentProcess() const { +const KProcess* KernelCore::CurrentProcess() const { return impl->current_process; } -const std::vector& KernelCore::GetProcessList() const { +const std::vector& KernelCore::GetProcessList() const { return impl->process_list; } diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index de7f83423..0dd9deaeb 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -37,7 +37,7 @@ class KEvent; class KLinkedListNode; class KMemoryManager; class KPort; -class Process; +class KProcess; class KResourceLimit; class KScheduler; class KSession; @@ -101,19 +101,19 @@ public: KScopedAutoObject RetrieveThreadFromGlobalHandleTable(Handle handle) const; /// Adds the given shared pointer to an internal list of active processes. - void AppendNewProcess(Process* process); + void AppendNewProcess(KProcess* process); /// Makes the given process the new current process. - void MakeCurrentProcess(Process* process); + void MakeCurrentProcess(KProcess* process); /// Retrieves a pointer to the current process. - Process* CurrentProcess(); + KProcess* CurrentProcess(); /// Retrieves a const pointer to the current process. - const Process* CurrentProcess() const; + const KProcess* CurrentProcess() const; /// Retrieves the list of processes. - const std::vector& GetProcessList() const; + const std::vector& GetProcessList() const; /// Gets the sole instance of the global scheduler Kernel::GlobalSchedulerContext& GlobalSchedulerContext(); @@ -274,7 +274,7 @@ public: return slab_heap_container->linked_list_node; } else if constexpr (std::is_same_v) { return slab_heap_container->port; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { return slab_heap_container->process; } else if constexpr (std::is_same_v) { return slab_heap_container->resource_limit; @@ -292,7 +292,7 @@ public: } private: - friend class Process; + friend class KProcess; friend class KThread; /// Creates a new object ID, incrementing the internal object ID counter. @@ -325,7 +325,7 @@ private: KSlabHeap event; KSlabHeap linked_list_node; KSlabHeap port; - KSlabHeap process; + KSlabHeap process; KSlabHeap resource_limit; KSlabHeap session; KSlabHeap shared_memory; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index ef8fa98a9..725f16ea4 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -30,6 +30,7 @@ #include "core/hle/kernel/k_memory_block.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_page_table.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_scheduler.h" @@ -42,7 +43,6 @@ #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/svc_types.h" @@ -402,8 +402,8 @@ static ResultCode GetProcessId(Core::System& system, u64* out_process_id, Handle R_UNLESS(obj.IsNotNull(), ResultInvalidHandle); // Get the process from the object. - Process* process = nullptr; - if (Process* p = obj->DynamicCast(); p != nullptr) { + KProcess* process = nullptr; + if (KProcess* p = obj->DynamicCast(); p != nullptr) { // The object is a process, so we can use it directly. process = p; } else if (KThread* t = obj->DynamicCast(); t != nullptr) { @@ -733,7 +733,7 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, Handle } const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - KScopedAutoObject process = handle_table.GetObject(handle); + KScopedAutoObject process = handle_table.GetObject(handle); if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Process is not valid! info_id={}, info_sub_id={}, handle={:08X}", info_id, info_sub_id, handle); @@ -838,7 +838,7 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, Handle return ResultInvalidCombination; } - Process* const current_process = system.Kernel().CurrentProcess(); + KProcess* const current_process = system.Kernel().CurrentProcess(); HandleTable& handle_table = current_process->GetHandleTable(); const auto resource_limit = current_process->GetResourceLimit(); if (!resource_limit) { @@ -861,9 +861,9 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, Handle return ResultInvalidHandle; } - if (info_sub_id >= Process::RANDOM_ENTROPY_SIZE) { + if (info_sub_id >= KProcess::RANDOM_ENTROPY_SIZE) { LOG_ERROR(Kernel_SVC, "Entropy size is out of range, expected {} but got {}", - Process::RANDOM_ENTROPY_SIZE, info_sub_id); + KProcess::RANDOM_ENTROPY_SIZE, info_sub_id); return ResultInvalidCombination; } @@ -955,7 +955,7 @@ static ResultCode MapPhysicalMemory(Core::System& system, VAddr addr, u64 size) return ResultInvalidMemoryRegion; } - Process* const current_process{system.Kernel().CurrentProcess()}; + KProcess* const current_process{system.Kernel().CurrentProcess()}; auto& page_table{current_process->PageTable()}; if (current_process->GetSystemResourceSize() == 0) { @@ -1009,7 +1009,7 @@ static ResultCode UnmapPhysicalMemory(Core::System& system, VAddr addr, u64 size return ResultInvalidMemoryRegion; } - Process* const current_process{system.Kernel().CurrentProcess()}; + KProcess* const current_process{system.Kernel().CurrentProcess()}; auto& page_table{current_process->PageTable()}; if (current_process->GetSystemResourceSize() == 0) { @@ -1153,7 +1153,7 @@ static ResultCode GetThreadPriority32(Core::System& system, u32* out_priority, H /// Sets the priority for the specified thread static ResultCode SetThreadPriority(Core::System& system, Handle thread_handle, u32 priority) { // Get the current process. - Process& process = *system.Kernel().CurrentProcess(); + KProcess& process = *system.Kernel().CurrentProcess(); // Validate the priority. R_UNLESS(HighestThreadPriority <= priority && priority <= LowestThreadPriority, @@ -1264,7 +1264,7 @@ static ResultCode QueryProcessMemory(Core::System& system, VAddr memory_info_add std::lock_guard lock{HLE::g_hle_lock}; LOG_TRACE(Kernel_SVC, "called process=0x{:08X} address={:X}", process_handle, address); const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - KScopedAutoObject process = handle_table.GetObject(process_handle); + KScopedAutoObject process = handle_table.GetObject(process_handle); if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", process_handle); @@ -1346,7 +1346,7 @@ static ResultCode MapProcessCodeMemory(Core::System& system, Handle process_hand } const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - KScopedAutoObject process = handle_table.GetObject(process_handle); + KScopedAutoObject process = handle_table.GetObject(process_handle); if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Invalid process handle specified (handle=0x{:08X}).", process_handle); @@ -1414,7 +1414,7 @@ static ResultCode UnmapProcessCodeMemory(Core::System& system, Handle process_ha } const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - KScopedAutoObject process = handle_table.GetObject(process_handle); + KScopedAutoObject process = handle_table.GetObject(process_handle); if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Invalid process handle specified (handle=0x{:08X}).", process_handle); @@ -1830,7 +1830,7 @@ static ResultCode ResetSignal(Core::System& system, Handle handle) { // Try to reset as process. { - KScopedAutoObject process = handle_table.GetObject(handle); + KScopedAutoObject process = handle_table.GetObject(handle); if (process.IsNotNull()) { return process->Reset(); } @@ -2077,7 +2077,7 @@ static ResultCode GetProcessInfo(Core::System& system, u64* out, Handle process_ }; const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - KScopedAutoObject process = handle_table.GetObject(process_handle); + KScopedAutoObject process = handle_table.GetObject(process_handle); if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", process_handle); diff --git a/src/core/hle/service/acc/acc.cpp b/src/core/hle/service/acc/acc.cpp index 5450dcf0f..49c09a570 100644 --- a/src/core/hle/service/acc/acc.cpp +++ b/src/core/hle/service/acc/acc.cpp @@ -16,8 +16,8 @@ #include "core/file_sys/control_metadata.h" #include "core/file_sys/patch_manager.h" #include "core/hle/ipc_helpers.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/service/acc/acc.h" #include "core/hle/service/acc/acc_aa.h" #include "core/hle/service/acc/acc_su.h" diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 47d194119..e8b82db00 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -15,11 +15,11 @@ #include "core/file_sys/savedata_factory.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/k_event.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/service/acc/profile_manager.h" #include "core/hle/service/am/am.h" #include "core/hle/service/am/applet_ae.h" diff --git a/src/core/hle/service/am/applets/error.cpp b/src/core/hle/service/am/applets/error.cpp index 0dd6ec68e..08348b180 100644 --- a/src/core/hle/service/am/applets/error.cpp +++ b/src/core/hle/service/am/applets/error.cpp @@ -9,7 +9,7 @@ #include "common/string_util.h" #include "core/core.h" #include "core/frontend/applets/error.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/am/am.h" #include "core/hle/service/am/applets/error.h" #include "core/reporter.h" diff --git a/src/core/hle/service/am/applets/general_backend.cpp b/src/core/hle/service/am/applets/general_backend.cpp index b7483261e..e95499edd 100644 --- a/src/core/hle/service/am/applets/general_backend.cpp +++ b/src/core/hle/service/am/applets/general_backend.cpp @@ -9,7 +9,7 @@ #include "common/logging/log.h" #include "core/core.h" #include "core/frontend/applets/general_frontend.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/result.h" #include "core/hle/service/am/am.h" #include "core/hle/service/am/applets/general_backend.h" diff --git a/src/core/hle/service/am/applets/web_browser.cpp b/src/core/hle/service/am/applets/web_browser.cpp index 2404921fd..e5f4a4485 100644 --- a/src/core/hle/service/am/applets/web_browser.cpp +++ b/src/core/hle/service/am/applets/web_browser.cpp @@ -17,7 +17,7 @@ #include "core/file_sys/system_archive/system_archive.h" #include "core/file_sys/vfs_vector.h" #include "core/frontend/applets/web_browser.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/result.h" #include "core/hle/service/am/am.h" #include "core/hle/service/am/applets/web_browser.h" diff --git a/src/core/hle/service/aoc/aoc_u.cpp b/src/core/hle/service/aoc/aoc_u.cpp index 7d7a8c0ad..1863260f1 100644 --- a/src/core/hle/service/aoc/aoc_u.cpp +++ b/src/core/hle/service/aoc/aoc_u.cpp @@ -16,9 +16,9 @@ #include "core/file_sys/patch_manager.h" #include "core/file_sys/registered_cache.h" #include "core/hle/ipc_helpers.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/service/aoc/aoc_u.h" #include "core/loader/loader.h" diff --git a/src/core/hle/service/bcat/module.cpp b/src/core/hle/service/bcat/module.cpp index 05635a2a5..0206cbb6a 100644 --- a/src/core/hle/service/bcat/module.cpp +++ b/src/core/hle/service/bcat/module.cpp @@ -12,9 +12,9 @@ #include "core/core.h" #include "core/file_sys/vfs.h" #include "core/hle/ipc_helpers.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_writable_event.h" -#include "core/hle/kernel/process.h" #include "core/hle/service/bcat/backend/backend.h" #include "core/hle/service/bcat/bcat.h" #include "core/hle/service/bcat/module.h" diff --git a/src/core/hle/service/fatal/fatal.cpp b/src/core/hle/service/fatal/fatal.cpp index 13147472e..432abde76 100644 --- a/src/core/hle/service/fatal/fatal.cpp +++ b/src/core/hle/service/fatal/fatal.cpp @@ -12,7 +12,7 @@ #include "common/swap.h" #include "core/core.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/fatal/fatal.h" #include "core/hle/service/fatal/fatal_p.h" #include "core/hle/service/fatal/fatal_u.h" diff --git a/src/core/hle/service/filesystem/filesystem.cpp b/src/core/hle/service/filesystem/filesystem.cpp index 67b2b3102..67baaee9b 100644 --- a/src/core/hle/service/filesystem/filesystem.cpp +++ b/src/core/hle/service/filesystem/filesystem.cpp @@ -21,7 +21,7 @@ #include "core/file_sys/sdmc_factory.h" #include "core/file_sys/vfs.h" #include "core/file_sys/vfs_offset.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/hle/service/filesystem/fsp_ldr.h" #include "core/hle/service/filesystem/fsp_pr.h" diff --git a/src/core/hle/service/filesystem/fsp_srv.cpp b/src/core/hle/service/filesystem/fsp_srv.cpp index 7dc487e48..92ea27074 100644 --- a/src/core/hle/service/filesystem/fsp_srv.cpp +++ b/src/core/hle/service/filesystem/fsp_srv.cpp @@ -25,7 +25,7 @@ #include "core/file_sys/system_archive/system_archive.h" #include "core/file_sys/vfs.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/hle/service/filesystem/fsp_srv.h" #include "core/reporter.h" diff --git a/src/core/hle/service/glue/arp.cpp b/src/core/hle/service/glue/arp.cpp index 7b1c6677c..6ad62ee5a 100644 --- a/src/core/hle/service/glue/arp.cpp +++ b/src/core/hle/service/glue/arp.cpp @@ -9,8 +9,8 @@ #include "core/file_sys/control_metadata.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/hle_ipc.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/service/glue/arp.h" #include "core/hle/service/glue/errors.h" #include "core/hle/service/glue/manager.h" diff --git a/src/core/hle/service/ldr/ldr.cpp b/src/core/hle/service/ldr/ldr.cpp index c8bc60ad1..563916f29 100644 --- a/src/core/hle/service/ldr/ldr.cpp +++ b/src/core/hle/service/ldr/ldr.cpp @@ -12,8 +12,8 @@ #include "core/core.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/k_page_table.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_system_control.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/service/ldr/ldr.h" #include "core/hle/service/service.h" @@ -321,7 +321,7 @@ public: return addr; } - ResultVal MapProcessCodeMemory(Kernel::Process* process, VAddr baseAddress, + ResultVal MapProcessCodeMemory(Kernel::KProcess* process, VAddr baseAddress, u64 size) const { for (std::size_t retry = 0; retry < MAXIMUM_MAP_RETRIES; retry++) { auto& page_table{process->PageTable()}; @@ -342,7 +342,7 @@ public: return ERROR_INSUFFICIENT_ADDRESS_SPACE; } - ResultVal MapNro(Kernel::Process* process, VAddr nro_addr, std::size_t nro_size, + ResultVal MapNro(Kernel::KProcess* process, VAddr nro_addr, std::size_t nro_size, VAddr bss_addr, std::size_t bss_size, std::size_t size) const { for (std::size_t retry = 0; retry < MAXIMUM_MAP_RETRIES; retry++) { auto& page_table{process->PageTable()}; @@ -378,7 +378,7 @@ public: return ERROR_INSUFFICIENT_ADDRESS_SPACE; } - ResultCode LoadNro(Kernel::Process* process, const NROHeader& nro_header, VAddr nro_addr, + ResultCode LoadNro(Kernel::KProcess* process, const NROHeader& nro_header, VAddr nro_addr, VAddr start) const { const VAddr text_start{start + nro_header.segment_headers[TEXT_INDEX].memory_offset}; const VAddr ro_start{start + nro_header.segment_headers[RO_INDEX].memory_offset}; diff --git a/src/core/hle/service/pctl/module.cpp b/src/core/hle/service/pctl/module.cpp index 9bebe6088..1c3d81143 100644 --- a/src/core/hle/service/pctl/module.cpp +++ b/src/core/hle/service/pctl/module.cpp @@ -7,7 +7,7 @@ #include "core/file_sys/control_metadata.h" #include "core/file_sys/patch_manager.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/pctl/module.h" #include "core/hle/service/pctl/pctl.h" diff --git a/src/core/hle/service/pm/pm.cpp b/src/core/hle/service/pm/pm.cpp index 3a00849e1..f4715935d 100644 --- a/src/core/hle/service/pm/pm.cpp +++ b/src/core/hle/service/pm/pm.cpp @@ -4,8 +4,8 @@ #include "core/core.h" #include "core/hle/ipc_helpers.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/service/pm/pm.h" #include "core/hle/service/service.h" @@ -17,8 +17,9 @@ constexpr ResultCode ERROR_PROCESS_NOT_FOUND{ErrorModule::PM, 1}; constexpr u64 NO_PROCESS_FOUND_PID{0}; -std::optional SearchProcessList(const std::vector& process_list, - std::function predicate) { +std::optional SearchProcessList( + const std::vector& process_list, + std::function predicate) { const auto iter = std::find_if(process_list.begin(), process_list.end(), predicate); if (iter == process_list.end()) { @@ -29,9 +30,9 @@ std::optional SearchProcessList(const std::vector& process_list) { + const std::vector& process_list) { const auto process = SearchProcessList(process_list, [](const auto& process) { - return process->GetProcessID() == Kernel::Process::ProcessIDMin; + return process->GetProcessID() == Kernel::KProcess::ProcessIDMin; }); IPC::ResponseBuilder rb{ctx, 4}; @@ -124,7 +125,7 @@ private: class Info final : public ServiceFramework { public: - explicit Info(Core::System& system_, const std::vector& process_list_) + explicit Info(Core::System& system_, const std::vector& process_list_) : ServiceFramework{system_, "pm:info"}, process_list{process_list_} { static const FunctionInfo functions[] = { {0, &Info::GetTitleId, "GetTitleId"}, @@ -154,7 +155,7 @@ private: rb.Push((*process)->GetTitleID()); } - const std::vector& process_list; + const std::vector& process_list; }; class Shell final : public ServiceFramework { diff --git a/src/core/hle/service/prepo/prepo.cpp b/src/core/hle/service/prepo/prepo.cpp index 449e8d63b..c914f8145 100644 --- a/src/core/hle/service/prepo/prepo.cpp +++ b/src/core/hle/service/prepo/prepo.cpp @@ -6,7 +6,7 @@ #include "common/logging/log.h" #include "core/core.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/acc/profile_manager.h" #include "core/hle/service/prepo/prepo.h" #include "core/hle/service/service.h" diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index 42e464024..00e683c2f 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -12,10 +12,10 @@ #include "core/hle/ipc.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_server_port.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/service/acc/acc.h" #include "core/hle/service/am/am.h" #include "core/hle/service/aoc/aoc_u.h" diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp index fed47ecda..42f023258 100644 --- a/src/core/loader/deconstructed_rom_directory.cpp +++ b/src/core/loader/deconstructed_rom_directory.cpp @@ -13,8 +13,8 @@ #include "core/file_sys/patch_manager.h" #include "core/file_sys/romfs_factory.h" #include "core/hle/kernel/k_page_table.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/loader/deconstructed_rom_directory.h" #include "core/loader/nso.h" @@ -88,7 +88,7 @@ FileType AppLoader_DeconstructedRomDirectory::IdentifyType(const FileSys::Virtua } AppLoader_DeconstructedRomDirectory::LoadResult AppLoader_DeconstructedRomDirectory::Load( - Kernel::Process& process, Core::System& system) { + Kernel::KProcess& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } diff --git a/src/core/loader/deconstructed_rom_directory.h b/src/core/loader/deconstructed_rom_directory.h index 22a4ec5a6..a49a8b001 100644 --- a/src/core/loader/deconstructed_rom_directory.h +++ b/src/core/loader/deconstructed_rom_directory.h @@ -44,7 +44,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process, Core::System& system) override; + LoadResult Load(Kernel::KProcess& process, Core::System& system) override; ResultStatus ReadRomFS(FileSys::VirtualFile& out_dir) override; ResultStatus ReadIcon(std::vector& out_buffer) override; diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 627c18c7e..c062a4259 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -11,7 +11,7 @@ #include "common/logging/log.h" #include "core/hle/kernel/code_set.h" #include "core/hle/kernel/k_page_table.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/loader/elf.h" #include "core/memory.h" @@ -386,7 +386,7 @@ FileType AppLoader_ELF::IdentifyType(const FileSys::VirtualFile& elf_file) { return FileType::Error; } -AppLoader_ELF::LoadResult AppLoader_ELF::Load(Kernel::Process& process, +AppLoader_ELF::LoadResult AppLoader_ELF::Load(Kernel::KProcess& process, [[maybe_unused]] Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h index 2b86c0b49..890299a20 100644 --- a/src/core/loader/elf.h +++ b/src/core/loader/elf.h @@ -32,7 +32,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process, Core::System& system) override; + LoadResult Load(Kernel::KProcess& process, Core::System& system) override; }; } // namespace Loader diff --git a/src/core/loader/kip.cpp b/src/core/loader/kip.cpp index 9b447da2a..3ae9e6e0e 100644 --- a/src/core/loader/kip.cpp +++ b/src/core/loader/kip.cpp @@ -7,7 +7,7 @@ #include "core/file_sys/program_metadata.h" #include "core/hle/kernel/code_set.h" #include "core/hle/kernel/k_page_table.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/loader/kip.h" #include "core/memory.h" @@ -42,7 +42,7 @@ FileType AppLoader_KIP::GetFileType() const { : FileType::Error; } -AppLoader::LoadResult AppLoader_KIP::Load(Kernel::Process& process, +AppLoader::LoadResult AppLoader_KIP::Load(Kernel::KProcess& process, [[maybe_unused]] Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; diff --git a/src/core/loader/kip.h b/src/core/loader/kip.h index 2fe636f01..5f914b4a8 100644 --- a/src/core/loader/kip.h +++ b/src/core/loader/kip.h @@ -32,7 +32,7 @@ public: FileType GetFileType() const override; - LoadResult Load(Kernel::Process& process, Core::System& system) override; + LoadResult Load(Kernel::KProcess& process, Core::System& system) override; private: std::unique_ptr kip; diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index e4f5fd40c..11b2d0837 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -11,7 +11,7 @@ #include "common/logging/log.h" #include "common/string_util.h" #include "core/core.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/loader/deconstructed_rom_directory.h" #include "core/loader/elf.h" #include "core/loader/kip.h" diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index bf6db1ab1..9eac11dec 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h @@ -25,7 +25,7 @@ class NACP; namespace Kernel { struct AddressMapping; -class Process; +class KProcess; } // namespace Kernel namespace Loader { @@ -165,7 +165,7 @@ public: * * @return The status result of the operation. */ - virtual LoadResult Load(Kernel::Process& process, Core::System& system) = 0; + virtual LoadResult Load(Kernel::KProcess& process, Core::System& system) = 0; /** * Get the code (typically .code section) of the application diff --git a/src/core/loader/nax.cpp b/src/core/loader/nax.cpp index f53c3a72c..aceb66414 100644 --- a/src/core/loader/nax.cpp +++ b/src/core/loader/nax.cpp @@ -6,7 +6,7 @@ #include "core/file_sys/content_archive.h" #include "core/file_sys/romfs.h" #include "core/file_sys/xts_archive.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/loader/nax.h" #include "core/loader/nca.h" @@ -41,7 +41,7 @@ FileType AppLoader_NAX::GetFileType() const { return IdentifyTypeImpl(*nax); } -AppLoader_NAX::LoadResult AppLoader_NAX::Load(Kernel::Process& process, Core::System& system) { +AppLoader_NAX::LoadResult AppLoader_NAX::Load(Kernel::KProcess& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } diff --git a/src/core/loader/nax.h b/src/core/loader/nax.h index 68427c1cf..b3a50894f 100644 --- a/src/core/loader/nax.h +++ b/src/core/loader/nax.h @@ -37,7 +37,7 @@ public: FileType GetFileType() const override; - LoadResult Load(Kernel::Process& process, Core::System& system) override; + LoadResult Load(Kernel::KProcess& process, Core::System& system) override; ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; u64 ReadRomFSIVFCOffset() const override; diff --git a/src/core/loader/nca.cpp b/src/core/loader/nca.cpp index 47e7a77a9..418cbf61b 100644 --- a/src/core/loader/nca.cpp +++ b/src/core/loader/nca.cpp @@ -9,7 +9,7 @@ #include "core/core.h" #include "core/file_sys/content_archive.h" #include "core/file_sys/romfs_factory.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/loader/deconstructed_rom_directory.h" #include "core/loader/nca.h" @@ -32,7 +32,7 @@ FileType AppLoader_NCA::IdentifyType(const FileSys::VirtualFile& nca_file) { return FileType::Error; } -AppLoader_NCA::LoadResult AppLoader_NCA::Load(Kernel::Process& process, Core::System& system) { +AppLoader_NCA::LoadResult AppLoader_NCA::Load(Kernel::KProcess& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } diff --git a/src/core/loader/nca.h b/src/core/loader/nca.h index c9792f390..f2ff080bb 100644 --- a/src/core/loader/nca.h +++ b/src/core/loader/nca.h @@ -39,7 +39,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process, Core::System& system) override; + LoadResult Load(Kernel::KProcess& process, Core::System& system) override; ResultStatus ReadRomFS(FileSys::VirtualFile& dir) override; u64 ReadRomFSIVFCOffset() const override; diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index 0597cfa60..ef54fa574 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -17,8 +17,8 @@ #include "core/file_sys/vfs_offset.h" #include "core/hle/kernel/code_set.h" #include "core/hle/kernel/k_page_table.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_thread.h" -#include "core/hle/kernel/process.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/loader/nro.h" #include "core/loader/nso.h" @@ -130,7 +130,7 @@ static constexpr u32 PageAlignSize(u32 size) { return static_cast((size + Core::Memory::PAGE_MASK) & ~Core::Memory::PAGE_MASK); } -static bool LoadNroImpl(Kernel::Process& process, const std::vector& data) { +static bool LoadNroImpl(Kernel::KProcess& process, const std::vector& data) { if (data.size() < sizeof(NroHeader)) { return {}; } @@ -199,11 +199,11 @@ static bool LoadNroImpl(Kernel::Process& process, const std::vector& data) { return true; } -bool AppLoader_NRO::LoadNro(Kernel::Process& process, const FileSys::VfsFile& nro_file) { +bool AppLoader_NRO::LoadNro(Kernel::KProcess& process, const FileSys::VfsFile& nro_file) { return LoadNroImpl(process, nro_file.ReadAllBytes()); } -AppLoader_NRO::LoadResult AppLoader_NRO::Load(Kernel::Process& process, Core::System& system) { +AppLoader_NRO::LoadResult AppLoader_NRO::Load(Kernel::KProcess& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } diff --git a/src/core/loader/nro.h b/src/core/loader/nro.h index 20bbaeb0e..fd453b402 100644 --- a/src/core/loader/nro.h +++ b/src/core/loader/nro.h @@ -19,7 +19,7 @@ class NACP; } namespace Kernel { -class Process; +class KProcess; } namespace Loader { @@ -43,7 +43,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process, Core::System& system) override; + LoadResult Load(Kernel::KProcess& process, Core::System& system) override; ResultStatus ReadIcon(std::vector& buffer) override; ResultStatus ReadProgramId(u64& out_program_id) override; @@ -53,7 +53,7 @@ public: bool IsRomFSUpdatable() const override; private: - bool LoadNro(Kernel::Process& process, const FileSys::VfsFile& nro_file); + bool LoadNro(Kernel::KProcess& process, const FileSys::VfsFile& nro_file); std::vector icon_data; std::unique_ptr nacp; diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index f671afe02..df59412cf 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -17,8 +17,8 @@ #include "core/file_sys/patch_manager.h" #include "core/hle/kernel/code_set.h" #include "core/hle/kernel/k_page_table.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_thread.h" -#include "core/hle/kernel/process.h" #include "core/loader/nso.h" #include "core/memory.h" @@ -71,7 +71,7 @@ FileType AppLoader_NSO::IdentifyType(const FileSys::VirtualFile& in_file) { return FileType::NSO; } -std::optional AppLoader_NSO::LoadModule(Kernel::Process& process, Core::System& system, +std::optional AppLoader_NSO::LoadModule(Kernel::KProcess& process, Core::System& system, const FileSys::VfsFile& nso_file, VAddr load_base, bool should_pass_arguments, bool load_into_process, std::optional pm) { @@ -162,7 +162,7 @@ std::optional AppLoader_NSO::LoadModule(Kernel::Process& process, Core::S return load_base + image_size; } -AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::Process& process, Core::System& system) { +AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::KProcess& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 195149b55..f7b61bc2d 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -17,7 +17,7 @@ class System; } namespace Kernel { -class Process; +class KProcess; } namespace Loader { @@ -86,12 +86,12 @@ public: return IdentifyType(file); } - static std::optional LoadModule(Kernel::Process& process, Core::System& system, + static std::optional LoadModule(Kernel::KProcess& process, Core::System& system, const FileSys::VfsFile& nso_file, VAddr load_base, bool should_pass_arguments, bool load_into_process, std::optional pm = {}); - LoadResult Load(Kernel::Process& process, Core::System& system) override; + LoadResult Load(Kernel::KProcess& process, Core::System& system) override; ResultStatus ReadNSOModules(Modules& out_modules) override; diff --git a/src/core/loader/nsp.cpp b/src/core/loader/nsp.cpp index d7e590f1c..d815a7cd3 100644 --- a/src/core/loader/nsp.cpp +++ b/src/core/loader/nsp.cpp @@ -13,7 +13,7 @@ #include "core/file_sys/patch_manager.h" #include "core/file_sys/registered_cache.h" #include "core/file_sys/submission_package.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/loader/deconstructed_rom_directory.h" #include "core/loader/nca.h" @@ -79,7 +79,7 @@ FileType AppLoader_NSP::IdentifyType(const FileSys::VirtualFile& nsp_file) { return FileType::Error; } -AppLoader_NSP::LoadResult AppLoader_NSP::Load(Kernel::Process& process, Core::System& system) { +AppLoader_NSP::LoadResult AppLoader_NSP::Load(Kernel::KProcess& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } diff --git a/src/core/loader/nsp.h b/src/core/loader/nsp.h index 1660f1b94..644c0ff58 100644 --- a/src/core/loader/nsp.h +++ b/src/core/loader/nsp.h @@ -45,7 +45,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process, Core::System& system) override; + LoadResult Load(Kernel::KProcess& process, Core::System& system) override; ResultStatus ReadRomFS(FileSys::VirtualFile& out_file) override; u64 ReadRomFSIVFCOffset() const override; diff --git a/src/core/loader/xci.cpp b/src/core/loader/xci.cpp index 0125ddf33..635d6ae15 100644 --- a/src/core/loader/xci.cpp +++ b/src/core/loader/xci.cpp @@ -13,7 +13,7 @@ #include "core/file_sys/registered_cache.h" #include "core/file_sys/romfs.h" #include "core/file_sys/submission_package.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/loader/nca.h" #include "core/loader/xci.h" @@ -56,7 +56,7 @@ FileType AppLoader_XCI::IdentifyType(const FileSys::VirtualFile& xci_file) { return FileType::Error; } -AppLoader_XCI::LoadResult AppLoader_XCI::Load(Kernel::Process& process, Core::System& system) { +AppLoader_XCI::LoadResult AppLoader_XCI::Load(Kernel::KProcess& process, Core::System& system) { if (is_loaded) { return {ResultStatus::ErrorAlreadyLoaded, {}}; } diff --git a/src/core/loader/xci.h b/src/core/loader/xci.h index 7ea8179af..708155c30 100644 --- a/src/core/loader/xci.h +++ b/src/core/loader/xci.h @@ -45,7 +45,7 @@ public: return IdentifyType(file); } - LoadResult Load(Kernel::Process& process, Core::System& system) override; + LoadResult Load(Kernel::KProcess& process, Core::System& system) override; ResultStatus ReadRomFS(FileSys::VirtualFile& out_file) override; u64 ReadRomFSIVFCOffset() const override; diff --git a/src/core/memory.cpp b/src/core/memory.cpp index ee4599063..b4c56e1c1 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -17,8 +17,8 @@ #include "core/core.h" #include "core/device_memory.h" #include "core/hle/kernel/k_page_table.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/physical_memory.h" -#include "core/hle/kernel/process.h" #include "core/memory.h" #include "video_core/gpu.h" @@ -30,7 +30,7 @@ namespace Core::Memory { struct Memory::Impl { explicit Impl(Core::System& system_) : system{system_} {} - void SetCurrentPageTable(Kernel::Process& process, u32 core_id) { + void SetCurrentPageTable(Kernel::KProcess& process, u32 core_id) { current_page_table = &process.PageTable().PageTableImpl(); const std::size_t address_space_width = process.PageTable().GetAddressSpaceWidth(); @@ -50,7 +50,7 @@ struct Memory::Impl { MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, 0, Common::PageType::Unmapped); } - bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) const { + bool IsValidVirtualAddress(const Kernel::KProcess& process, const VAddr vaddr) const { const auto& page_table = process.PageTable().PageTableImpl(); const auto [pointer, type] = page_table.pointers[vaddr >> PAGE_BITS].PointerType(); return pointer != nullptr || type == Common::PageType::RasterizerCachedMemory; @@ -194,7 +194,7 @@ struct Memory::Impl { return string; } - void ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer, + void ReadBlock(const Kernel::KProcess& process, const VAddr src_addr, void* dest_buffer, const std::size_t size) { const auto& page_table = process.PageTable().PageTableImpl(); @@ -239,7 +239,7 @@ struct Memory::Impl { } } - void ReadBlockUnsafe(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer, + void ReadBlockUnsafe(const Kernel::KProcess& process, const VAddr src_addr, void* dest_buffer, const std::size_t size) { const auto& page_table = process.PageTable().PageTableImpl(); @@ -291,7 +291,7 @@ struct Memory::Impl { ReadBlockUnsafe(*system.CurrentProcess(), src_addr, dest_buffer, size); } - void WriteBlock(const Kernel::Process& process, const VAddr dest_addr, const void* src_buffer, + void WriteBlock(const Kernel::KProcess& process, const VAddr dest_addr, const void* src_buffer, const std::size_t size) { const auto& page_table = process.PageTable().PageTableImpl(); std::size_t remaining_size = size; @@ -334,7 +334,7 @@ struct Memory::Impl { } } - void WriteBlockUnsafe(const Kernel::Process& process, const VAddr dest_addr, + void WriteBlockUnsafe(const Kernel::KProcess& process, const VAddr dest_addr, const void* src_buffer, const std::size_t size) { const auto& page_table = process.PageTable().PageTableImpl(); std::size_t remaining_size = size; @@ -384,7 +384,7 @@ struct Memory::Impl { WriteBlockUnsafe(*system.CurrentProcess(), dest_addr, src_buffer, size); } - void ZeroBlock(const Kernel::Process& process, const VAddr dest_addr, const std::size_t size) { + void ZeroBlock(const Kernel::KProcess& process, const VAddr dest_addr, const std::size_t size) { const auto& page_table = process.PageTable().PageTableImpl(); std::size_t remaining_size = size; std::size_t page_index = dest_addr >> PAGE_BITS; @@ -429,7 +429,7 @@ struct Memory::Impl { ZeroBlock(*system.CurrentProcess(), dest_addr, size); } - void CopyBlock(const Kernel::Process& process, VAddr dest_addr, VAddr src_addr, + void CopyBlock(const Kernel::KProcess& process, VAddr dest_addr, VAddr src_addr, const std::size_t size) { const auto& page_table = process.PageTable().PageTableImpl(); std::size_t remaining_size = size; @@ -741,7 +741,7 @@ void Memory::Reset() { impl = std::make_unique(system); } -void Memory::SetCurrentPageTable(Kernel::Process& process, u32 core_id) { +void Memory::SetCurrentPageTable(Kernel::KProcess& process, u32 core_id) { impl->SetCurrentPageTable(process, core_id); } @@ -753,7 +753,7 @@ void Memory::UnmapRegion(Common::PageTable& page_table, VAddr base, u64 size) { impl->UnmapRegion(page_table, base, size); } -bool Memory::IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) const { +bool Memory::IsValidVirtualAddress(const Kernel::KProcess& process, const VAddr vaddr) const { return impl->IsValidVirtualAddress(process, vaddr); } @@ -829,7 +829,7 @@ std::string Memory::ReadCString(VAddr vaddr, std::size_t max_length) { return impl->ReadCString(vaddr, max_length); } -void Memory::ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer, +void Memory::ReadBlock(const Kernel::KProcess& process, const VAddr src_addr, void* dest_buffer, const std::size_t size) { impl->ReadBlock(process, src_addr, dest_buffer, size); } @@ -838,7 +838,7 @@ void Memory::ReadBlock(const VAddr src_addr, void* dest_buffer, const std::size_ impl->ReadBlock(src_addr, dest_buffer, size); } -void Memory::ReadBlockUnsafe(const Kernel::Process& process, const VAddr src_addr, +void Memory::ReadBlockUnsafe(const Kernel::KProcess& process, const VAddr src_addr, void* dest_buffer, const std::size_t size) { impl->ReadBlockUnsafe(process, src_addr, dest_buffer, size); } @@ -847,7 +847,7 @@ void Memory::ReadBlockUnsafe(const VAddr src_addr, void* dest_buffer, const std: impl->ReadBlockUnsafe(src_addr, dest_buffer, size); } -void Memory::WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer, +void Memory::WriteBlock(const Kernel::KProcess& process, VAddr dest_addr, const void* src_buffer, std::size_t size) { impl->WriteBlock(process, dest_addr, src_buffer, size); } @@ -856,7 +856,7 @@ void Memory::WriteBlock(const VAddr dest_addr, const void* src_buffer, const std impl->WriteBlock(dest_addr, src_buffer, size); } -void Memory::WriteBlockUnsafe(const Kernel::Process& process, VAddr dest_addr, +void Memory::WriteBlockUnsafe(const Kernel::KProcess& process, VAddr dest_addr, const void* src_buffer, std::size_t size) { impl->WriteBlockUnsafe(process, dest_addr, src_buffer, size); } @@ -866,7 +866,7 @@ void Memory::WriteBlockUnsafe(const VAddr dest_addr, const void* src_buffer, impl->WriteBlockUnsafe(dest_addr, src_buffer, size); } -void Memory::ZeroBlock(const Kernel::Process& process, VAddr dest_addr, std::size_t size) { +void Memory::ZeroBlock(const Kernel::KProcess& process, VAddr dest_addr, std::size_t size) { impl->ZeroBlock(process, dest_addr, size); } @@ -874,7 +874,7 @@ void Memory::ZeroBlock(VAddr dest_addr, std::size_t size) { impl->ZeroBlock(dest_addr, size); } -void Memory::CopyBlock(const Kernel::Process& process, VAddr dest_addr, VAddr src_addr, +void Memory::CopyBlock(const Kernel::KProcess& process, VAddr dest_addr, VAddr src_addr, const std::size_t size) { impl->CopyBlock(process, dest_addr, src_addr, size); } diff --git a/src/core/memory.h b/src/core/memory.h index 9a706a9ac..345fd870d 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -19,7 +19,7 @@ class System; namespace Kernel { class PhysicalMemory; -class Process; +class KProcess; } // namespace Kernel namespace Core::Memory { @@ -68,7 +68,7 @@ public: * * @param process The process to use the page table of. */ - void SetCurrentPageTable(Kernel::Process& process, u32 core_id); + void SetCurrentPageTable(Kernel::KProcess& process, u32 core_id); /** * Maps an allocated buffer onto a region of the emulated process address space. @@ -99,7 +99,7 @@ public: * * @returns True if the given virtual address is valid, false otherwise. */ - bool IsValidVirtualAddress(const Kernel::Process& process, VAddr vaddr) const; + bool IsValidVirtualAddress(const Kernel::KProcess& process, VAddr vaddr) const; /** * Checks whether or not the supplied address is a valid virtual @@ -333,7 +333,7 @@ public: * @post The range [dest_buffer, size) contains the read bytes from the * process' address space. */ - void ReadBlock(const Kernel::Process& process, VAddr src_addr, void* dest_buffer, + void ReadBlock(const Kernel::KProcess& process, VAddr src_addr, void* dest_buffer, std::size_t size); /** @@ -354,7 +354,7 @@ public: * @post The range [dest_buffer, size) contains the read bytes from the * process' address space. */ - void ReadBlockUnsafe(const Kernel::Process& process, VAddr src_addr, void* dest_buffer, + void ReadBlockUnsafe(const Kernel::KProcess& process, VAddr src_addr, void* dest_buffer, std::size_t size); /** @@ -414,7 +414,7 @@ public: * and will mark that region as invalidated to caches that the active * graphics backend may be maintaining over the course of execution. */ - void WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer, + void WriteBlock(const Kernel::KProcess& process, VAddr dest_addr, const void* src_buffer, std::size_t size); /** @@ -434,7 +434,7 @@ public: * will be ignored and an error will be logged. * */ - void WriteBlockUnsafe(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer, + void WriteBlockUnsafe(const Kernel::KProcess& process, VAddr dest_addr, const void* src_buffer, std::size_t size); /** @@ -486,7 +486,7 @@ public: * @post The range [dest_addr, size) within the process' address space is * filled with zeroes. */ - void ZeroBlock(const Kernel::Process& process, VAddr dest_addr, std::size_t size); + void ZeroBlock(const Kernel::KProcess& process, VAddr dest_addr, std::size_t size); /** * Fills the specified address range within the current process' address space with zeroes. @@ -511,7 +511,7 @@ public: * @post The range [dest_addr, size) within the process' address space contains the * same data within the range [src_addr, size). */ - void CopyBlock(const Kernel::Process& process, VAddr dest_addr, VAddr src_addr, + void CopyBlock(const Kernel::KProcess& process, VAddr dest_addr, VAddr src_addr, std::size_t size); /** diff --git a/src/core/memory/cheat_engine.cpp b/src/core/memory/cheat_engine.cpp index 7643b7846..0f5ef7954 100644 --- a/src/core/memory/cheat_engine.cpp +++ b/src/core/memory/cheat_engine.cpp @@ -11,7 +11,7 @@ #include "core/core_timing_util.h" #include "core/hardware_properties.h" #include "core/hle/kernel/k_page_table.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/hid/controllers/npad.h" #include "core/hle/service/hid/hid.h" #include "core/hle/service/sm/sm.h" diff --git a/src/core/reporter.cpp b/src/core/reporter.cpp index 311d4dda8..896add892 100644 --- a/src/core/reporter.cpp +++ b/src/core/reporter.cpp @@ -19,7 +19,7 @@ #include "core/core.h" #include "core/hle/kernel/hle_ipc.h" #include "core/hle/kernel/k_page_table.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/result.h" #include "core/memory.h" #include "core/reporter.h" diff --git a/src/video_core/memory_manager.cpp b/src/video_core/memory_manager.cpp index 4eb71efbd..eb58ac6b6 100644 --- a/src/video_core/memory_manager.cpp +++ b/src/video_core/memory_manager.cpp @@ -7,7 +7,7 @@ #include "common/logging/log.h" #include "core/core.h" #include "core/hle/kernel/k_page_table.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/memory.h" #include "video_core/gpu.h" #include "video_core/memory_manager.h" diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index 0863904e9..a5dbb9adf 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -19,7 +19,7 @@ #include "common/scope_exit.h" #include "common/settings.h" #include "core/core.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/memory.h" #include "video_core/engines/kepler_compute.h" #include "video_core/engines/maxwell_3d.h" diff --git a/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp b/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp index 97fb11ac6..dbcb751cb 100644 --- a/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp +++ b/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp @@ -15,7 +15,7 @@ #include "common/settings.h" #include "common/zstd_compression.h" #include "core/core.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "video_core/engines/shader_type.h" #include "video_core/renderer_opengl/gl_shader_cache.h" #include "video_core/renderer_opengl/gl_shader_disk_cache.h" diff --git a/src/yuzu/bootmanager.cpp b/src/yuzu/bootmanager.cpp index 7ff9491f4..86495803e 100644 --- a/src/yuzu/bootmanager.cpp +++ b/src/yuzu/bootmanager.cpp @@ -32,7 +32,7 @@ #include "common/settings.h" #include "core/core.h" #include "core/frontend/framebuffer_layout.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "input_common/keyboard.h" #include "input_common/main.h" #include "input_common/mouse/mouse_input.h" diff --git a/src/yuzu/debugger/wait_tree.cpp b/src/yuzu/debugger/wait_tree.cpp index 3ac4a9e2b..f040b4c08 100644 --- a/src/yuzu/debugger/wait_tree.cpp +++ b/src/yuzu/debugger/wait_tree.cpp @@ -14,11 +14,11 @@ #include "core/core.h" #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/k_class_token.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_synchronization_object.h" #include "core/hle/kernel/k_thread.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_common.h" #include "core/hle/kernel/svc_types.h" #include "core/memory.h" diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 00d4cfe67..9e72acbf7 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -92,7 +92,7 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual #include "core/file_sys/romfs.h" #include "core/file_sys/savedata_factory.h" #include "core/file_sys/submission_package.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/am/am.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/hle/service/nfp/nfp.h" diff --git a/src/yuzu_cmd/yuzu.cpp b/src/yuzu_cmd/yuzu.cpp index 4871ac3bb..e2812ca61 100644 --- a/src/yuzu_cmd/yuzu.cpp +++ b/src/yuzu_cmd/yuzu.cpp @@ -27,7 +27,7 @@ #include "core/crypto/key_manager.h" #include "core/file_sys/registered_cache.h" #include "core/file_sys/vfs_real.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/loader/loader.h" #include "core/telemetry_session.h" From 0b27c721c994e10200893c3306cdab2184e5143c Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 30 Apr 2021 14:53:22 -0700 Subject: [PATCH 47/82] hle: kernel: Improve MapSharedMemory and implement UnmapSharedMemory. --- src/core/hle/kernel/k_memory_block.h | 4 + src/core/hle/kernel/k_page_table.cpp | 39 ++++++ src/core/hle/kernel/k_page_table.h | 5 +- src/core/hle/kernel/k_process.cpp | 25 ++++ src/core/hle/kernel/k_process.h | 3 + src/core/hle/kernel/k_shared_memory.cpp | 31 +++-- src/core/hle/kernel/k_shared_memory.h | 19 ++- src/core/hle/kernel/kernel.cpp | 24 ++-- src/core/hle/kernel/svc.cpp | 162 +++++++++++++----------- src/core/hle/kernel/svc_wrap.h | 17 ++- 10 files changed, 222 insertions(+), 107 deletions(-) diff --git a/src/core/hle/kernel/k_memory_block.h b/src/core/hle/kernel/k_memory_block.h index c5b9c5e85..a7fdb5fb8 100644 --- a/src/core/hle/kernel/k_memory_block.h +++ b/src/core/hle/kernel/k_memory_block.h @@ -134,6 +134,10 @@ enum class KMemoryPermission : u8 { }; DECLARE_ENUM_FLAG_OPERATORS(KMemoryPermission); +constexpr KMemoryPermission ConvertToKMemoryPermission(Svc::MemoryPermission perm) { + return static_cast(perm); +} + enum class KMemoryAttribute : u8 { None = 0x00, Mask = 0x7F, diff --git a/src/core/hle/kernel/k_page_table.cpp b/src/core/hle/kernel/k_page_table.cpp index 2f33cb6c1..6c5d7d602 100644 --- a/src/core/hle/kernel/k_page_table.cpp +++ b/src/core/hle/kernel/k_page_table.cpp @@ -641,6 +641,45 @@ ResultCode KPageTable::MapPages(VAddr addr, KPageLinkedList& page_linked_list, K return RESULT_SUCCESS; } +ResultCode KPageTable::UnmapPages(VAddr addr, const KPageLinkedList& page_linked_list) { + VAddr cur_addr{addr}; + + for (const auto& node : page_linked_list.Nodes()) { + const std::size_t num_pages{(addr - cur_addr) / PageSize}; + if (const auto result{ + Operate(addr, num_pages, KMemoryPermission::None, OperationType::Unmap)}; + result.IsError()) { + return result; + } + + cur_addr += node.GetNumPages() * PageSize; + } + + return RESULT_SUCCESS; +} + +ResultCode KPageTable::UnmapPages(VAddr addr, KPageLinkedList& page_linked_list, + KMemoryState state) { + std::lock_guard lock{page_table_lock}; + + const std::size_t num_pages{page_linked_list.GetNumPages()}; + const std::size_t size{num_pages * PageSize}; + + if (!CanContain(addr, size, state)) { + return ResultInvalidCurrentMemory; + } + + if (IsRegionMapped(addr, num_pages * PageSize)) { + return ResultInvalidCurrentMemory; + } + + CASCADE_CODE(UnmapPages(addr, page_linked_list)); + + block_manager->Update(addr, num_pages, state, KMemoryPermission::None); + + return RESULT_SUCCESS; +} + ResultCode KPageTable::SetCodeMemoryPermission(VAddr addr, std::size_t size, KMemoryPermission perm) { diff --git a/src/core/hle/kernel/k_page_table.h b/src/core/hle/kernel/k_page_table.h index 80a1586db..baec7c260 100644 --- a/src/core/hle/kernel/k_page_table.h +++ b/src/core/hle/kernel/k_page_table.h @@ -40,6 +40,7 @@ public: ResultCode Unmap(VAddr dst_addr, VAddr src_addr, std::size_t size); ResultCode MapPages(VAddr addr, KPageLinkedList& page_linked_list, KMemoryState state, KMemoryPermission perm); + ResultCode UnmapPages(VAddr addr, KPageLinkedList& page_linked_list, KMemoryState state); ResultCode SetCodeMemoryPermission(VAddr addr, std::size_t size, KMemoryPermission perm); KMemoryInfo QueryInfo(VAddr addr); ResultCode ReserveTransferMemory(VAddr addr, std::size_t size, KMemoryPermission perm); @@ -63,6 +64,8 @@ public: return page_table_impl; } + constexpr bool CanContain(VAddr addr, std::size_t size, KMemoryState state) const; + private: enum class OperationType : u32 { Map, @@ -79,6 +82,7 @@ private: ResultCode InitializeMemoryLayout(VAddr start, VAddr end); ResultCode MapPages(VAddr addr, const KPageLinkedList& page_linked_list, KMemoryPermission perm); + ResultCode UnmapPages(VAddr addr, const KPageLinkedList& page_linked_list); void MapPhysicalMemory(KPageLinkedList& page_linked_list, VAddr start, VAddr end); bool IsRegionMapped(VAddr address, u64 size); bool IsRegionContiguous(VAddr addr, u64 size) const; @@ -92,7 +96,6 @@ private: OperationType operation, PAddr map_addr = 0); constexpr VAddr GetRegionAddress(KMemoryState state) const; constexpr std::size_t GetRegionSize(KMemoryState state) const; - constexpr bool CanContain(VAddr addr, std::size_t size, KMemoryState state) const; constexpr ResultCode CheckMemoryState(const KMemoryInfo& info, KMemoryState state_mask, KMemoryState state, KMemoryPermission perm_mask, diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp index edc3b5175..e542b1f07 100644 --- a/src/core/hle/kernel/k_process.cpp +++ b/src/core/hle/kernel/k_process.cpp @@ -21,6 +21,7 @@ #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_resource_reservation.h" +#include "core/hle/kernel/k_shared_memory.h" #include "core/hle/kernel/k_slab_heap.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" @@ -247,6 +248,30 @@ void KProcess::UnpinCurrentThread() { KScheduler::SetSchedulerUpdateNeeded(kernel); } +ResultCode KProcess::AddSharedMemory(KSharedMemory* shmem, [[maybe_unused]] VAddr address, + [[maybe_unused]] size_t size) { + // Lock ourselves, to prevent concurrent access. + KScopedLightLock lk(state_lock); + + // TODO(bunnei): Manage KSharedMemoryInfo list here. + + // Open a reference to the shared memory. + shmem->Open(); + + return RESULT_SUCCESS; +} + +void KProcess::RemoveSharedMemory(KSharedMemory* shmem, [[maybe_unused]] VAddr address, + [[maybe_unused]] size_t size) { + // Lock ourselves, to prevent concurrent access. + KScopedLightLock lk(state_lock); + + // TODO(bunnei): Manage KSharedMemoryInfo list here. + + // Close a reference to the shared memory. + shmem->Close(); +} + void KProcess::RegisterThread(const KThread* thread) { thread_list.push_back(thread); } diff --git a/src/core/hle/kernel/k_process.h b/src/core/hle/kernel/k_process.h index 961c0d9ba..5c54c6360 100644 --- a/src/core/hle/kernel/k_process.h +++ b/src/core/hle/kernel/k_process.h @@ -352,6 +352,9 @@ public: return state_lock; } + ResultCode AddSharedMemory(KSharedMemory* shmem, VAddr address, size_t size); + void RemoveSharedMemory(KSharedMemory* shmem, VAddr address, size_t size); + /////////////////////////////////////////////////////////////////////////////////////////////// // Thread-local storage management diff --git a/src/core/hle/kernel/k_shared_memory.cpp b/src/core/hle/kernel/k_shared_memory.cpp index f137a182a..1da57a4c3 100644 --- a/src/core/hle/kernel/k_shared_memory.cpp +++ b/src/core/hle/kernel/k_shared_memory.cpp @@ -20,9 +20,10 @@ KSharedMemory::~KSharedMemory() { ResultCode KSharedMemory::Initialize(KernelCore& kernel_, Core::DeviceMemory& device_memory_, KProcess* owner_process_, KPageLinkedList&& page_list_, - KMemoryPermission owner_permission_, - KMemoryPermission user_permission_, PAddr physical_address_, - std::size_t size_, std::string name_) { + Svc::MemoryPermission owner_permission_, + Svc::MemoryPermission user_permission_, + PAddr physical_address_, std::size_t size_, + std::string name_) { // Set members. owner_process = owner_process_; device_memory = &device_memory_; @@ -58,14 +59,6 @@ ResultCode KSharedMemory::Initialize(KernelCore& kernel_, Core::DeviceMemory& de } void KSharedMemory::Finalize() { - ///* Get the number of pages. */ - // const size_t num_pages = m_page_group.GetNumPages(); - // const size_t size = num_pages * PageSize; - - ///* Close and finalize the page group. */ - // m_page_group.Close(); - // m_page_group.Finalize(); - // Release the memory reservation. resource_limit->Release(LimitableResource::PhysicalMemory, size); resource_limit->Close(); @@ -75,14 +68,14 @@ void KSharedMemory::Finalize() { } ResultCode KSharedMemory::Map(KProcess& target_process, VAddr address, std::size_t size, - KMemoryPermission permissions) { + Svc::MemoryPermission permissions) { const u64 page_count{(size + PageSize - 1) / PageSize}; if (page_list.GetNumPages() != page_count) { UNIMPLEMENTED_MSG("Page count does not match"); } - const KMemoryPermission expected = + const Svc::MemoryPermission expected = &target_process == owner_process ? owner_permission : user_permission; if (permissions != expected) { @@ -90,7 +83,17 @@ ResultCode KSharedMemory::Map(KProcess& target_process, VAddr address, std::size } return target_process.PageTable().MapPages(address, page_list, KMemoryState::Shared, - permissions); + ConvertToKMemoryPermission(permissions)); +} + +ResultCode KSharedMemory::Unmap(KProcess& target_process, VAddr address, std::size_t size) { + const u64 page_count{(size + PageSize - 1) / PageSize}; + + if (page_list.GetNumPages() != page_count) { + UNIMPLEMENTED_MSG("Page count does not match"); + } + + return target_process.PageTable().UnmapPages(address, page_list, KMemoryState::Shared); } } // namespace Kernel diff --git a/src/core/hle/kernel/k_shared_memory.h b/src/core/hle/kernel/k_shared_memory.h index 2d315c916..28939c93c 100644 --- a/src/core/hle/kernel/k_shared_memory.h +++ b/src/core/hle/kernel/k_shared_memory.h @@ -29,8 +29,9 @@ public: ResultCode Initialize(KernelCore& kernel_, Core::DeviceMemory& device_memory_, KProcess* owner_process_, KPageLinkedList&& page_list_, - KMemoryPermission owner_permission_, KMemoryPermission user_permission_, - PAddr physical_address_, std::size_t size_, std::string name_); + Svc::MemoryPermission owner_permission_, + Svc::MemoryPermission user_permission_, PAddr physical_address_, + std::size_t size_, std::string name_); /** * Maps a shared memory block to an address in the target process' address space @@ -40,7 +41,15 @@ public: * @param permissions Memory block map permissions (specified by SVC field) */ ResultCode Map(KProcess& target_process, VAddr address, std::size_t size, - KMemoryPermission permissions); + Svc::MemoryPermission permissions); + + /** + * Unmaps a shared memory block from an address in the target process' address space + * @param target_process Process on which to unmap the memory block + * @param address Address in system memory to unmap shared memory block + * @param size Size of the shared memory block to unmap + */ + ResultCode Unmap(KProcess& target_process, VAddr address, std::size_t size); /** * Gets a pointer to the shared memory block @@ -71,8 +80,8 @@ private: Core::DeviceMemory* device_memory; KProcess* owner_process{}; KPageLinkedList page_list; - KMemoryPermission owner_permission{}; - KMemoryPermission user_permission{}; + Svc::MemoryPermission owner_permission{}; + Svc::MemoryPermission user_permission{}; PAddr physical_address{}; std::size_t size{}; KResourceLimit* resource_limit{}; diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 718525c4c..f64e07081 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -599,21 +599,21 @@ struct KernelCore::Impl { time_shared_mem = KSharedMemory::Create(system.Kernel()); hid_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr, - {hid_phys_addr, hid_size / PageSize}, KMemoryPermission::None, - KMemoryPermission::Read, hid_phys_addr, hid_size, - "HID:SharedMemory"); + {hid_phys_addr, hid_size / PageSize}, + Svc::MemoryPermission::None, Svc::MemoryPermission::Read, + hid_phys_addr, hid_size, "HID:SharedMemory"); font_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr, - {font_phys_addr, font_size / PageSize}, KMemoryPermission::None, - KMemoryPermission::Read, font_phys_addr, font_size, - "Font:SharedMemory"); + {font_phys_addr, font_size / PageSize}, + Svc::MemoryPermission::None, Svc::MemoryPermission::Read, + font_phys_addr, font_size, "Font:SharedMemory"); irs_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr, - {irs_phys_addr, irs_size / PageSize}, KMemoryPermission::None, - KMemoryPermission::Read, irs_phys_addr, irs_size, - "IRS:SharedMemory"); + {irs_phys_addr, irs_size / PageSize}, + Svc::MemoryPermission::None, Svc::MemoryPermission::Read, + irs_phys_addr, irs_size, "IRS:SharedMemory"); time_shared_mem->Initialize(system.Kernel(), system.DeviceMemory(), nullptr, - {time_phys_addr, time_size / PageSize}, KMemoryPermission::None, - KMemoryPermission::Read, time_phys_addr, time_size, - "Time:SharedMemory"); + {time_phys_addr, time_size / PageSize}, + Svc::MemoryPermission::None, Svc::MemoryPermission::Read, + time_phys_addr, time_size, "Time:SharedMemory"); } void InitializePageSlab() { diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 725f16ea4..156c565b0 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -1183,79 +1183,97 @@ static u32 GetCurrentProcessorNumber32(Core::System& system) { return GetCurrentProcessorNumber(system); } -static ResultCode MapSharedMemory(Core::System& system, Handle shared_memory_handle, VAddr addr, - u64 size, u32 permissions) { - std::lock_guard lock{HLE::g_hle_lock}; - LOG_TRACE(Kernel_SVC, - "called, shared_memory_handle=0x{:X}, addr=0x{:X}, size=0x{:X}, permissions=0x{:08X}", - shared_memory_handle, addr, size, permissions); - - if (!Common::Is4KBAligned(addr)) { - LOG_ERROR(Kernel_SVC, "Address is not aligned to 4KB, addr=0x{:016X}", addr); - return ResultInvalidAddress; +constexpr bool IsValidSharedMemoryPermission(Svc::MemoryPermission perm) { + switch (perm) { + case Svc::MemoryPermission::Read: + case Svc::MemoryPermission::ReadWrite: + return true; + default: + return false; } - - if (size == 0) { - LOG_ERROR(Kernel_SVC, "Size is 0"); - return ResultInvalidSize; - } - - if (!Common::Is4KBAligned(size)) { - LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, size=0x{:016X}", size); - return ResultInvalidSize; - } - - if (!IsValidAddressRange(addr, size)) { - LOG_ERROR(Kernel_SVC, "Region is not a valid address range, addr=0x{:016X}, size=0x{:016X}", - addr, size); - return ResultInvalidCurrentMemory; - } - - const auto permission_type = static_cast(permissions); - if ((permission_type | MemoryPermission::Write) != MemoryPermission::ReadWrite) { - LOG_ERROR(Kernel_SVC, "Expected Read or ReadWrite permission but got permissions=0x{:08X}", - permissions); - return ResultInvalidNewMemoryPermission; - } - - auto* const current_process{system.Kernel().CurrentProcess()}; - auto& page_table{current_process->PageTable()}; - - if (page_table.IsInvalidRegion(addr, size)) { - LOG_ERROR(Kernel_SVC, - "Addr does not fit within the valid region, addr=0x{:016X}, " - "size=0x{:016X}", - addr, size); - return ResultInvalidMemoryRegion; - } - - if (page_table.IsInsideHeapRegion(addr, size)) { - LOG_ERROR(Kernel_SVC, - "Addr does not fit within the heap region, addr=0x{:016X}, " - "size=0x{:016X}", - addr, size); - return ResultInvalidMemoryRegion; - } - - if (page_table.IsInsideAliasRegion(addr, size)) { - LOG_ERROR(Kernel_SVC, - "Address does not fit within the map region, addr=0x{:016X}, " - "size=0x{:016X}", - addr, size); - return ResultInvalidMemoryRegion; - } - - auto shared_memory{ - current_process->GetHandleTable().GetObject(shared_memory_handle)}; - R_UNLESS(shared_memory.IsNotNull(), ResultInvalidHandle); - - return shared_memory->Map(*current_process, addr, size, - static_cast(permission_type)); } -static ResultCode MapSharedMemory32(Core::System& system, Handle shared_memory_handle, u32 addr, - u32 size, u32 permissions) { - return MapSharedMemory(system, shared_memory_handle, addr, size, permissions); +constexpr bool IsValidRemoteSharedMemoryPermission(Svc::MemoryPermission perm) { + return IsValidSharedMemoryPermission(perm) || perm == Svc::MemoryPermission::DontCare; +} + +static ResultCode MapSharedMemory(Core::System& system, Handle shmem_handle, VAddr address, + u64 size, Svc::MemoryPermission map_perm) { + LOG_TRACE(Kernel_SVC, + "called, shared_memory_handle=0x{:X}, addr=0x{:X}, size=0x{:X}, permissions=0x{:08X}", + shmem_handle, address, size, map_perm); + + // Validate the address/size. + R_UNLESS(Common::IsAligned(address, PageSize), ResultInvalidAddress); + R_UNLESS(Common::IsAligned(size, PageSize), ResultInvalidSize); + R_UNLESS(size > 0, ResultInvalidSize); + R_UNLESS((address < address + size), ResultInvalidCurrentMemory); + + // Validate the permission. + R_UNLESS(IsValidSharedMemoryPermission(map_perm), ResultInvalidNewMemoryPermission); + + // Get the current process. + auto& process = *system.Kernel().CurrentProcess(); + auto& page_table = process.PageTable(); + + // Get the shared memory. + KScopedAutoObject shmem = process.GetHandleTable().GetObject(shmem_handle); + R_UNLESS(shmem.IsNotNull(), ResultInvalidHandle); + + // Verify that the mapping is in range. + R_UNLESS(page_table.CanContain(address, size, KMemoryState::Shared), ResultInvalidMemoryRegion); + + // Add the shared memory to the process. + R_TRY(process.AddSharedMemory(shmem.GetPointerUnsafe(), address, size)); + + // Ensure that we clean up the shared memory if we fail to map it. + auto guard = + SCOPE_GUARD({ process.RemoveSharedMemory(shmem.GetPointerUnsafe(), address, size); }); + + // Map the shared memory. + R_TRY(shmem->Map(process, address, size, map_perm)); + + // We succeeded. + guard.Cancel(); + return RESULT_SUCCESS; +} + +static ResultCode MapSharedMemory32(Core::System& system, Handle shmem_handle, u32 address, + u32 size, Svc::MemoryPermission map_perm) { + return MapSharedMemory(system, shmem_handle, address, size, map_perm); +} + +static ResultCode UnmapSharedMemory(Core::System& system, Handle shmem_handle, VAddr address, + u64 size) { + // Validate the address/size. + R_UNLESS(Common::IsAligned(address, PageSize), ResultInvalidAddress); + R_UNLESS(Common::IsAligned(size, PageSize), ResultInvalidSize); + R_UNLESS(size > 0, ResultInvalidSize); + R_UNLESS((address < address + size), ResultInvalidCurrentMemory); + + // Get the current process. + auto& process = *system.Kernel().CurrentProcess(); + auto& page_table = process.PageTable(); + + // Get the shared memory. + KScopedAutoObject shmem = process.GetHandleTable().GetObject(shmem_handle); + R_UNLESS(shmem.IsNotNull(), ResultInvalidHandle); + + // Verify that the mapping is in range. + R_UNLESS(page_table.CanContain(address, size, KMemoryState::Shared), ResultInvalidMemoryRegion); + + // Unmap the shared memory. + R_TRY(shmem->Unmap(process, address, size)); + + // Remove the shared memory from the process. + process.RemoveSharedMemory(shmem.GetPointerUnsafe(), address, size); + + return RESULT_SUCCESS; +} + +static ResultCode UnmapSharedMemory32(Core::System& system, Handle shmem_handle, u32 address, + u32 size) { + return UnmapSharedMemory(system, shmem_handle, address, size); } static ResultCode QueryProcessMemory(Core::System& system, VAddr memory_info_address, @@ -2297,7 +2315,7 @@ static const FunctionDef SVC_Table_32[] = { {0x11, SvcWrap32, "SignalEvent32"}, {0x12, SvcWrap32, "ClearEvent32"}, {0x13, SvcWrap32, "MapSharedMemory32"}, - {0x14, nullptr, "UnmapSharedMemory32"}, + {0x14, SvcWrap32, "UnmapSharedMemory32"}, {0x15, SvcWrap32, "CreateTransferMemory32"}, {0x16, SvcWrap32, "CloseHandle32"}, {0x17, SvcWrap32, "ResetSignal32"}, @@ -2492,7 +2510,7 @@ static const FunctionDef SVC_Table_64[] = { {0x11, SvcWrap64, "SignalEvent"}, {0x12, SvcWrap64, "ClearEvent"}, {0x13, SvcWrap64, "MapSharedMemory"}, - {0x14, nullptr, "UnmapSharedMemory"}, + {0x14, SvcWrap64, "UnmapSharedMemory"}, {0x15, SvcWrap64, "CreateTransferMemory"}, {0x16, SvcWrap64, "CloseHandle"}, {0x17, SvcWrap64, "ResetSignal"}, diff --git a/src/core/hle/kernel/svc_wrap.h b/src/core/hle/kernel/svc_wrap.h index d3df25d0f..0968987af 100644 --- a/src/core/hle/kernel/svc_wrap.h +++ b/src/core/hle/kernel/svc_wrap.h @@ -232,10 +232,11 @@ void SvcWrap64(Core::System& system) { func(system, Param(system, 0), Param(system, 1), static_cast(Param(system, 2))).raw); } -template +// Used by MapSharedMemory +template void SvcWrap64(Core::System& system) { - FuncReturn(system, func(system, static_cast(Param(system, 0)), Param(system, 1), - Param(system, 2), static_cast(Param(system, 3))) + FuncReturn(system, func(system, static_cast(Param(system, 0)), Param(system, 1), + Param(system, 2), static_cast(Param(system, 3))) .raw); } @@ -553,6 +554,16 @@ void SvcWrap32(Core::System& system) { FuncReturn(system, retval); } +// Used by MapSharedMemory32 +template +void SvcWrap32(Core::System& system) { + const u32 retval = + func(system, static_cast(Param(system, 0)), static_cast(Param(system, 1)), static_cast(Param(system, 2)), + static_cast(Param(system, 3))) + .raw; + FuncReturn(system, retval); +} + // Used by SetThreadCoreMask32 template void SvcWrap32(Core::System& system) { From 8f5052a514706eb8a15d4f2d80573b1291f37b69 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 23 Apr 2021 22:11:03 -0700 Subject: [PATCH 48/82] hle: kernel: KClassToken: Ensure class tokens are correct. --- src/core/hle/kernel/k_class_token.cpp | 128 +++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/src/core/hle/kernel/k_class_token.cpp b/src/core/hle/kernel/k_class_token.cpp index ec2f8ccad..beb8a2a05 100644 --- a/src/core/hle/kernel/k_class_token.cpp +++ b/src/core/hle/kernel/k_class_token.cpp @@ -2,6 +2,132 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include "core/hle/kernel/k_auto_object.h" #include "core/hle/kernel/k_class_token.h" +#include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_client_session.h" +#include "core/hle/kernel/k_event.h" +#include "core/hle/kernel/k_port.h" +#include "core/hle/kernel/k_process.h" +#include "core/hle/kernel/k_readable_event.h" +#include "core/hle/kernel/k_resource_limit.h" +#include "core/hle/kernel/k_server_port.h" +#include "core/hle/kernel/k_server_session.h" +#include "core/hle/kernel/k_session.h" +#include "core/hle/kernel/k_shared_memory.h" +#include "core/hle/kernel/k_synchronization_object.h" +#include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/k_transfer_memory.h" +#include "core/hle/kernel/k_writable_event.h" -namespace Kernel {} // namespace Kernel +namespace Kernel { + +// Ensure that we generate correct class tokens for all types. + +// Ensure that the absolute token values are correct. +static_assert(ClassToken == 0b00000000'00000000); +static_assert(ClassToken == 0b00000000'00000001); +static_assert(ClassToken == 0b00000000'00000011); +// static_assert(ClassToken == 0b00000111'00000011); +// static_assert(ClassToken == 0b00001011'00000001); +static_assert(ClassToken == 0b00010011'00000001); +static_assert(ClassToken == 0b00100011'00000001); +static_assert(ClassToken == 0b01000011'00000001); +static_assert(ClassToken == 0b10000011'00000001); +static_assert(ClassToken == 0b00001101'00000000); +static_assert(ClassToken == 0b00010101'00000001); +static_assert(ClassToken == 0b00100101'00000000); +// static_assert(ClassToken == 0b01000101'00000000); +static_assert(ClassToken == 0b10000101'00000000); +static_assert(ClassToken == 0b00011001'00000000); +static_assert(ClassToken == 0b00101001'00000000); +static_assert(ClassToken == 0b01001001'00000000); +static_assert(ClassToken == 0b10001001'00000000); +// static_assert(ClassToken == 0b00110001'00000000); +// static_assert(ClassToken == 0b01010001'00000000); +static_assert(ClassToken == 0b10010001'00000000); +// static_assert(ClassToken == 0b01100001'00000000); +// static_assert(ClassToken == 0b10100001'00000000); +// static_assert(ClassToken == 0b11000001'00000000); + +// Ensure that the token hierarchy is correct. + +// Base classes +static_assert(ClassToken == (0b00000000)); +static_assert(ClassToken == (0b00000001 | ClassToken)); +static_assert(ClassToken == (0b00000010 | ClassToken)); + +// Final classes +// static_assert(ClassToken == ((0b00000111 << 8) | ClassToken)); +// static_assert(ClassToken == ((0b00001011 << 8) | ClassToken)); +static_assert(ClassToken == ((0b00010011 << 8) | ClassToken)); +static_assert(ClassToken == ((0b00100011 << 8) | ClassToken)); +static_assert(ClassToken == + ((0b01000011 << 8) | ClassToken)); +static_assert(ClassToken == ((0b10000011 << 8) | ClassToken)); +static_assert(ClassToken == ((0b00001101 << 8) | ClassToken)); +static_assert(ClassToken == ((0b00010101 << 8) | ClassToken)); +static_assert(ClassToken == ((0b00100101 << 8) | ClassToken)); +// static_assert(ClassToken == ((0b01000101 << 8) | ClassToken)); +static_assert(ClassToken == ((0b10000101 << 8) | ClassToken)); +static_assert(ClassToken == ((0b00011001 << 8) | ClassToken)); +static_assert(ClassToken == ((0b00101001 << 8) | ClassToken)); +static_assert(ClassToken == ((0b01001001 << 8) | ClassToken)); +static_assert(ClassToken == ((0b10001001 << 8) | ClassToken)); +// static_assert(ClassToken == ((0b00110001 << 8) | ClassToken)); +// static_assert(ClassToken == ((0b01010001 << 8) | ClassToken)); +static_assert(ClassToken == ((0b10010001 << 8) | ClassToken)); +// static_assert(ClassToken == ((0b01100001 << 8) | ClassToken)); +// static_assert(ClassToken == ((0b10100001 << 8) | ClassToken)); +// static_assert(ClassToken == ((0b11000001 << 8) | ClassToken)); + +// Ensure that the token hierarchy reflects the class hierarchy. + +// Base classes. +static_assert(!std::is_final::value && + std::is_base_of::value); +static_assert(!std::is_final::value && + std::is_base_of::value); + +// Final classes +// static_assert(std::is_final::value && +// std::is_base_of::value); +// static_assert(std::is_final::value && +// std::is_base_of::value); +static_assert(std::is_final::value && + std::is_base_of::value); +static_assert(std::is_final::value && + std::is_base_of::value); +static_assert(std::is_final::value && + std::is_base_of::value); +static_assert(std::is_final::value && + std::is_base_of::value); +static_assert(std::is_final::value && + std::is_base_of::value); +static_assert(std::is_final::value && + std::is_base_of::value); +static_assert(std::is_final::value && + std::is_base_of::value); +// static_assert(std::is_final::value && +// std::is_base_of::value); +static_assert(std::is_final::value && std::is_base_of::value); +static_assert(std::is_final::value && std::is_base_of::value); +static_assert(std::is_final::value && + std::is_base_of::value); +static_assert(std::is_final::value && std::is_base_of::value); +static_assert(std::is_final::value && + std::is_base_of::value); +// static_assert(std::is_final::value && +// std::is_base_of::value); +// static_assert(std::is_final::value && +// std::is_base_of::value); +static_assert(std::is_final::value && + std::is_base_of::value); +// static_assert(std::is_final::value && +// std::is_base_of::value); +// static_assert(std::is_final::value && +// std::is_base_of::value); +// static_assert(std::is_final::value && +// std::is_base_of::value); + +} // namespace Kernel From 4b03e6e776e6421c2b2c290b0822b9e5a8556a4c Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 24 Apr 2021 02:40:31 -0700 Subject: [PATCH 49/82] hle: kernel: Migrate to KHandleTable. --- src/core/CMakeLists.txt | 4 +- src/core/hle/kernel/handle_table.cpp | 125 ------- src/core/hle/kernel/handle_table.h | 212 ------------ src/core/hle/kernel/hle_ipc.cpp | 6 +- src/core/hle/kernel/hle_ipc.h | 7 +- src/core/hle/kernel/k_auto_object.h | 2 - src/core/hle/kernel/k_condition_variable.cpp | 2 +- src/core/hle/kernel/k_handle_table.cpp | 135 ++++++++ src/core/hle/kernel/k_handle_table.h | 309 ++++++++++++++++++ src/core/hle/kernel/k_process.cpp | 2 +- src/core/hle/kernel/k_process.h | 8 +- .../k_scoped_scheduler_lock_and_sleep.h | 4 +- src/core/hle/kernel/k_server_session.cpp | 2 +- src/core/hle/kernel/k_thread.cpp | 2 +- src/core/hle/kernel/kernel.cpp | 20 +- src/core/hle/kernel/kernel.h | 7 +- src/core/hle/kernel/process_capability.cpp | 4 +- src/core/hle/kernel/svc.cpp | 8 +- src/core/hle/kernel/svc_common.h | 15 + src/core/hle/kernel/time_manager.cpp | 1 - src/yuzu/debugger/wait_tree.cpp | 4 +- src/yuzu/debugger/wait_tree.h | 5 +- 22 files changed, 503 insertions(+), 381 deletions(-) delete mode 100644 src/core/hle/kernel/handle_table.cpp delete mode 100644 src/core/hle/kernel/handle_table.h create mode 100644 src/core/hle/kernel/k_handle_table.cpp create mode 100644 src/core/hle/kernel/k_handle_table.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 889a2d2f8..83da30418 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -149,8 +149,6 @@ add_library(core STATIC hle/kernel/svc_results.h hle/kernel/global_scheduler_context.cpp hle/kernel/global_scheduler_context.h - hle/kernel/handle_table.cpp - hle/kernel/handle_table.h hle/kernel/hle_ipc.cpp hle/kernel/hle_ipc.h hle/kernel/init/init_slab_setup.cpp @@ -174,6 +172,8 @@ add_library(core STATIC hle/kernel/k_condition_variable.h hle/kernel/k_event.cpp hle/kernel/k_event.h + hle/kernel/k_handle_table.cpp + hle/kernel/k_handle_table.h hle/kernel/k_light_condition_variable.h hle/kernel/k_light_lock.cpp hle/kernel/k_light_lock.h diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp deleted file mode 100644 index 16c528f5b..000000000 --- a/src/core/hle/kernel/handle_table.cpp +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright 2014 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include -#include "common/assert.h" -#include "common/logging/log.h" -#include "core/core.h" -#include "core/hle/kernel/handle_table.h" -#include "core/hle/kernel/k_process.h" -#include "core/hle/kernel/k_scheduler.h" -#include "core/hle/kernel/k_thread.h" -#include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/svc_results.h" - -namespace Kernel { -namespace { -constexpr u16 GetSlot(Handle handle) { - return static_cast(handle >> 15); -} - -constexpr u16 GetGeneration(Handle handle) { - return static_cast(handle & 0x7FFF); -} -} // Anonymous namespace - -HandleTable::HandleTable(KernelCore& kernel) : kernel{kernel} { - Clear(); -} - -HandleTable::~HandleTable() = default; - -ResultCode HandleTable::SetSize(s32 handle_table_size) { - if (static_cast(handle_table_size) > MAX_COUNT) { - LOG_ERROR(Kernel, "Handle table size {} is greater than {}", handle_table_size, MAX_COUNT); - return ResultOutOfMemory; - } - - // Values less than or equal to zero indicate to use the maximum allowable - // size for the handle table in the actual kernel, so we ignore the given - // value in that case, since we assume this by default unless this function - // is called. - if (handle_table_size > 0) { - table_size = static_cast(handle_table_size); - } - - return RESULT_SUCCESS; -} - -ResultCode HandleTable::Add(Handle* out_handle, KAutoObject* obj, u16 type) { - ASSERT(obj != nullptr); - - const u16 slot = next_free_slot; - if (slot >= table_size) { - LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use."); - return ResultOutOfHandles; - } - next_free_slot = generations[slot]; - - const u16 generation = next_generation++; - - // Overflow count so it fits in the 15 bits dedicated to the generation in the handle. - // Horizon OS uses zero to represent an invalid handle, so skip to 1. - if (next_generation >= (1 << 15)) { - next_generation = 1; - } - - generations[slot] = generation; - objects[slot] = obj; - obj->Open(); - - *out_handle = generation | (slot << 15); - - return RESULT_SUCCESS; -} - -ResultVal HandleTable::Duplicate(Handle handle) { - auto object = GetObject(handle); - if (object.IsNull()) { - LOG_ERROR(Kernel, "Tried to duplicate invalid handle: {:08X}", handle); - return ResultInvalidHandle; - } - - Handle out_handle{}; - R_TRY(Add(&out_handle, object.GetPointerUnsafe())); - - return MakeResult(out_handle); -} - -bool HandleTable::Remove(Handle handle) { - if (!IsValid(handle)) { - LOG_ERROR(Kernel, "Handle is not valid! handle={:08X}", handle); - return {}; - } - - const u16 slot = GetSlot(handle); - - if (objects[slot]) { - objects[slot]->Close(); - } - - objects[slot] = nullptr; - - generations[slot] = next_free_slot; - next_free_slot = slot; - - return true; -} - -bool HandleTable::IsValid(Handle handle) const { - const std::size_t slot = GetSlot(handle); - const u16 generation = GetGeneration(handle); - const bool is_object_valid = (objects[slot] != nullptr); - return slot < table_size && is_object_valid && generations[slot] == generation; -} - -void HandleTable::Clear() { - for (u16 i = 0; i < table_size; ++i) { - generations[i] = static_cast(i + 1); - objects[i] = nullptr; - } - next_free_slot = 0; -} - -} // namespace Kernel diff --git a/src/core/hle/kernel/handle_table.h b/src/core/hle/kernel/handle_table.h deleted file mode 100644 index 791e303d1..000000000 --- a/src/core/hle/kernel/handle_table.h +++ /dev/null @@ -1,212 +0,0 @@ -// Copyright 2014 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include - -#include "common/common_types.h" -#include "core/hle/kernel/k_auto_object.h" -#include "core/hle/kernel/k_spin_lock.h" -#include "core/hle/kernel/kernel.h" -#include "core/hle/result.h" - -namespace Kernel { - -class KernelCore; - -enum KernelHandle : Handle { - InvalidHandle = 0, - CurrentThread = 0xFFFF8000, - CurrentProcess = 0xFFFF8001, -}; - -/** - * This class allows the creation of Handles, which are references to objects that can be tested - * for validity and looked up. Here they are used to pass references to kernel objects to/from the - * emulated process. it has been designed so that it follows the same handle format and has - * approximately the same restrictions as the handle manager in the CTR-OS. - * - * Handles contain two sub-fields: a slot index (bits 31:15) and a generation value (bits 14:0). - * The slot index is used to index into the arrays in this class to access the data corresponding - * to the Handle. - * - * To prevent accidental use of a freed Handle whose slot has already been reused, a global counter - * is kept and incremented every time a Handle is created. This is the Handle's "generation". The - * value of the counter is stored into the Handle as well as in the handle table (in the - * "generations" array). When looking up a handle, the Handle's generation must match with the - * value stored on the class, otherwise the Handle is considered invalid. - * - * To find free slots when allocating a Handle without needing to scan the entire object array, the - * generations field of unallocated slots is re-purposed as a linked list of indices to free slots. - * When a Handle is created, an index is popped off the list and used for the new Handle. When it - * is destroyed, it is again pushed onto the list to be re-used by the next allocation. It is - * likely that this allocation strategy differs from the one used in CTR-OS, but this hasn't been - * verified and isn't likely to cause any problems. - */ -class HandleTable final : NonCopyable { -public: - /// This is the maximum limit of handles allowed per process in Horizon - static constexpr std::size_t MAX_COUNT = 1024; - - explicit HandleTable(KernelCore& kernel); - ~HandleTable(); - - /** - * Sets the number of handles that may be in use at one time - * for this handle table. - * - * @param handle_table_size The desired size to limit the handle table to. - * - * @returns an error code indicating if initialization was successful. - * If initialization was not successful, then ERR_OUT_OF_MEMORY - * will be returned. - * - * @pre handle_table_size must be within the range [0, 1024] - */ - ResultCode SetSize(s32 handle_table_size); - - /** - * Returns a new handle that points to the same object as the passed in handle. - * @return The duplicated Handle or one of the following errors: - * - `ERR_INVALID_HANDLE`: an invalid handle was passed in. - * - Any errors returned by `Create()`. - */ - ResultVal Duplicate(Handle handle); - - /** - * Closes a handle, removing it from the table and decreasing the object's ref-count. - * @return `RESULT_SUCCESS` or one of the following errors: - * - `ERR_INVALID_HANDLE`: an invalid handle was passed in. - */ - bool Remove(Handle handle); - - /// Checks if a handle is valid and points to an existing object. - bool IsValid(Handle handle) const; - - template - KAutoObject* GetObjectImpl(Handle handle) const { - if (!IsValid(handle)) { - return nullptr; - } - - auto* obj = objects[static_cast(handle >> 15)]; - return obj->DynamicCast(); - } - - template - KScopedAutoObject GetObject(Handle handle) const { - if (handle == CurrentThread) { - return kernel.CurrentScheduler()->GetCurrentThread()->DynamicCast(); - } else if (handle == CurrentProcess) { - return kernel.CurrentProcess()->DynamicCast(); - } - - if (!IsValid(handle)) { - return nullptr; - } - - auto* obj = objects[static_cast(handle >> 15)]; - return obj->DynamicCast(); - } - - template - KScopedAutoObject GetObjectWithoutPseudoHandle(Handle handle) const { - if (!IsValid(handle)) { - return nullptr; - } - auto* obj = objects[static_cast(handle >> 15)]; - return obj->DynamicCast(); - } - - /// Closes all handles held in this table. - void Clear(); - - // NEW IMPL - - template - ResultCode Add(Handle* out_handle, T* obj) { - static_assert(std::is_base_of::value); - return this->Add(out_handle, obj, obj->GetTypeObj().GetClassToken()); - } - - ResultCode Add(Handle* out_handle, KAutoObject* obj, u16 type); - - template - bool GetMultipleObjects(T** out, const Handle* handles, size_t num_handles) const { - // Try to convert and open all the handles. - size_t num_opened; - { - // Lock the table. - KScopedSpinLock lk(lock); - for (num_opened = 0; num_opened < num_handles; num_opened++) { - // Get the current handle. - const auto cur_handle = handles[num_opened]; - - // Get the object for the current handle. - KAutoObject* cur_object = this->GetObjectImpl(cur_handle); - if (cur_object == nullptr) { - break; - } - - // Cast the current object to the desired type. - T* cur_t = cur_object->DynamicCast(); - if (cur_t == nullptr) { - break; - } - - // Open a reference to the current object. - cur_t->Open(); - out[num_opened] = cur_t; - } - } - - // If we converted every object, succeed. - if (num_opened == num_handles) { - return true; - } - - // If we didn't convert entry object, close the ones we opened. - for (size_t i = 0; i < num_opened; i++) { - out[i]->Close(); - } - - return false; - } - -private: - /// Stores the Object referenced by the handle or null if the slot is empty. - std::array objects{}; - - /** - * The value of `next_generation` when the handle was created, used to check for validity. For - * empty slots, contains the index of the next free slot in the list. - */ - std::array generations; - - /** - * The limited size of the handle table. This can be specified by process - * capabilities in order to restrict the overall number of handles that - * can be created in a process instance - */ - u16 table_size = static_cast(MAX_COUNT); - - /** - * Global counter of the number of created handles. Stored in `generations` when a handle is - * created, and wraps around to 1 when it hits 0x8000. - */ - u16 next_generation = 1; - - /// Head of the free slots linked list. - u16 next_free_slot = 0; - - mutable KSpinLock lock; - - /// Underlying kernel instance that this handle table operates under. - KernelCore& kernel; -}; - -} // namespace Kernel diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index 69190286d..b505d20a6 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -14,8 +14,8 @@ #include "common/common_types.h" #include "common/logging/log.h" #include "core/hle/ipc_helpers.h" -#include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/hle_ipc.h" +#include "core/hle/kernel/k_handle_table.h" #include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_scheduler.h" @@ -50,7 +50,7 @@ HLERequestContext::HLERequestContext(KernelCore& kernel_, Core::Memory::Memory& HLERequestContext::~HLERequestContext() = default; -void HLERequestContext::ParseCommandBuffer(const HandleTable& handle_table, u32_le* src_cmdbuf, +void HLERequestContext::ParseCommandBuffer(const KHandleTable& handle_table, u32_le* src_cmdbuf, bool incoming) { IPC::RequestParser rp(src_cmdbuf); command_header = rp.PopRaw(); @@ -163,7 +163,7 @@ void HLERequestContext::ParseCommandBuffer(const HandleTable& handle_table, u32_ rp.Skip(1, false); // The command is actually an u64, but we don't use the high part. } -ResultCode HLERequestContext::PopulateFromIncomingCommandBuffer(const HandleTable& handle_table, +ResultCode HLERequestContext::PopulateFromIncomingCommandBuffer(const KHandleTable& handle_table, u32_le* src_cmdbuf) { ParseCommandBuffer(handle_table, src_cmdbuf, true); if (command_header->type == IPC::CommandType::Close) { diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index 4b92ba655..fa031c121 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -17,6 +17,7 @@ #include "common/swap.h" #include "core/hle/ipc.h" #include "core/hle/kernel/k_auto_object.h" +#include "core/hle/kernel/svc_common.h" union ResultCode; @@ -35,9 +36,9 @@ class ServiceFrameworkBase; namespace Kernel { class Domain; -class HandleTable; class HLERequestContext; class KernelCore; +class KHandleTable; class KProcess; class KServerSession; class KThread; @@ -121,7 +122,7 @@ public: } /// Populates this context with data from the requesting process/thread. - ResultCode PopulateFromIncomingCommandBuffer(const HandleTable& handle_table, + ResultCode PopulateFromIncomingCommandBuffer(const KHandleTable& handle_table, u32_le* src_cmdbuf); /// Writes data from this context back to the requesting process/thread. @@ -267,7 +268,7 @@ public: private: friend class IPC::ResponseBuilder; - void ParseCommandBuffer(const HandleTable& handle_table, u32_le* src_cmdbuf, bool incoming); + void ParseCommandBuffer(const KHandleTable& handle_table, u32_le* src_cmdbuf, bool incoming); std::array cmd_buf; Kernel::KServerSession* server_session{}; diff --git a/src/core/hle/kernel/k_auto_object.h b/src/core/hle/kernel/k_auto_object.h index 5a180b7dc..32aaf9fc5 100644 --- a/src/core/hle/kernel/k_auto_object.h +++ b/src/core/hle/kernel/k_auto_object.h @@ -17,8 +17,6 @@ namespace Kernel { class KernelCore; class KProcess; -using Handle = u32; - #define KERNEL_AUTOOBJECT_TRAITS(CLASS, BASE_CLASS) \ NON_COPYABLE(CLASS); \ NON_MOVEABLE(CLASS); \ diff --git a/src/core/hle/kernel/k_condition_variable.cpp b/src/core/hle/kernel/k_condition_variable.cpp index a9738f7ce..f51cf3e7b 100644 --- a/src/core/hle/kernel/k_condition_variable.cpp +++ b/src/core/hle/kernel/k_condition_variable.cpp @@ -179,7 +179,7 @@ KThread* KConditionVariable::SignalImpl(KThread* thread) { KThread* thread_to_close = nullptr; if (can_access) { - if (prev_tag == InvalidHandle) { + if (prev_tag == Svc::InvalidHandle) { // If nobody held the lock previously, we're all good. thread->SetSyncedObject(nullptr, RESULT_SUCCESS); thread->Wakeup(); diff --git a/src/core/hle/kernel/k_handle_table.cpp b/src/core/hle/kernel/k_handle_table.cpp new file mode 100644 index 000000000..0378447f6 --- /dev/null +++ b/src/core/hle/kernel/k_handle_table.cpp @@ -0,0 +1,135 @@ +// Copyright 2021 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/hle/kernel/k_handle_table.h" + +namespace Kernel { + +KHandleTable::KHandleTable(KernelCore& kernel_) : kernel{kernel_} {} +KHandleTable ::~KHandleTable() = default; + +ResultCode KHandleTable::Finalize() { + // Get the table and clear our record of it. + u16 saved_table_size = 0; + { + KScopedSpinLock lk(m_lock); + + std::swap(m_table_size, saved_table_size); + } + + // Close and free all entries. + for (size_t i = 0; i < saved_table_size; i++) { + if (KAutoObject* obj = m_objects[i]; obj != nullptr) { + obj->Close(); + } + } + + return RESULT_SUCCESS; +} + +bool KHandleTable::Remove(Handle handle) { + // Don't allow removal of a pseudo-handle. + if (Svc::IsPseudoHandle(handle)) { + return false; + } + + // Handles must not have reserved bits set. + const auto handle_pack = HandlePack(handle); + if (handle_pack.reserved != 0) { + return false; + } + + // Find the object and free the entry. + KAutoObject* obj = nullptr; + { + KScopedSpinLock lk(m_lock); + + if (this->IsValidHandle(handle)) { + const auto index = handle_pack.index; + + obj = m_objects[index]; + this->FreeEntry(index); + } else { + return false; + } + } + + // Close the object. + obj->Close(); + return true; +} + +ResultCode KHandleTable::Add(Handle* out_handle, KAutoObject* obj, u16 type) { + KScopedSpinLock lk(m_lock); + + // Never exceed our capacity. + R_UNLESS(m_count < m_table_size, ResultOutOfHandles); + + // Allocate entry, set output handle. + { + const auto linear_id = this->AllocateLinearId(); + const auto index = this->AllocateEntry(); + + m_entry_infos[index].info = {.linear_id = linear_id, .type = type}; + m_objects[index] = obj; + + obj->Open(); + + *out_handle = EncodeHandle(static_cast(index), linear_id); + } + + return RESULT_SUCCESS; +} + +ResultCode KHandleTable::Reserve(Handle* out_handle) { + KScopedSpinLock lk(m_lock); + + // Never exceed our capacity. + R_UNLESS(m_count < m_table_size, ResultOutOfHandles); + + *out_handle = EncodeHandle(static_cast(this->AllocateEntry()), this->AllocateLinearId()); + return RESULT_SUCCESS; +} + +void KHandleTable::Unreserve(Handle handle) { + KScopedSpinLock lk(m_lock); + + // Unpack the handle. + const auto handle_pack = HandlePack(handle); + const auto index = handle_pack.index; + const auto linear_id = handle_pack.linear_id; + const auto reserved = handle_pack.reserved; + ASSERT(reserved == 0); + ASSERT(linear_id != 0); + + if (index < m_table_size) { + // NOTE: This code does not check the linear id. + ASSERT(m_objects[index] == nullptr); + this->FreeEntry(index); + } +} + +void KHandleTable::Register(Handle handle, KAutoObject* obj, u16 type) { + KScopedSpinLock lk(m_lock); + + // Unpack the handle. + const auto handle_pack = HandlePack(handle); + const auto index = handle_pack.index; + const auto linear_id = handle_pack.linear_id; + const auto reserved = handle_pack.reserved; + ASSERT(reserved == 0); + ASSERT(linear_id != 0); + + if (index < m_table_size) { + // Set the entry. + ASSERT(m_objects[index] == nullptr); + + m_entry_infos[index].info = {.linear_id = static_cast(linear_id), .type = type}; + m_objects[index] = obj; + + obj->Open(); + } +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_handle_table.h b/src/core/hle/kernel/k_handle_table.h new file mode 100644 index 000000000..e38ad0fd9 --- /dev/null +++ b/src/core/hle/kernel/k_handle_table.h @@ -0,0 +1,309 @@ +// Copyright 2021 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include + +#include "common/assert.h" +#include "common/bit_field.h" +#include "common/bit_util.h" +#include "common/common_types.h" +#include "core/hle/kernel/k_auto_object.h" +#include "core/hle/kernel/k_spin_lock.h" +#include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/svc_common.h" +#include "core/hle/kernel/svc_results.h" +#include "core/hle/result.h" + +namespace Kernel { + +class KernelCore; + +class KHandleTable { + NON_COPYABLE(KHandleTable); + NON_MOVEABLE(KHandleTable); + +public: + static constexpr size_t MaxTableSize = 1024; + +private: + union HandlePack { + u32 raw; + BitField<0, 15, u32> index; + BitField<15, 15, u32> linear_id; + BitField<30, 2, u32> reserved; + }; + + static constexpr u16 MinLinearId = 1; + static constexpr u16 MaxLinearId = 0x7FFF; + + static constexpr Handle EncodeHandle(u16 index, u16 linear_id) { + HandlePack handle{}; + handle.index.Assign(index); + handle.linear_id.Assign(linear_id); + handle.reserved.Assign(0); + return handle.raw; + } + + union EntryInfo { + struct { + u16 linear_id; + u16 type; + } info; + s32 next_free_index; + + constexpr u16 GetLinearId() const { + return info.linear_id; + } + constexpr u16 GetType() const { + return info.type; + } + constexpr s32 GetNextFreeIndex() const { + return next_free_index; + } + }; + +private: + std::array m_entry_infos{}; + std::array m_objects{}; + s32 m_free_head_index{-1}; + u16 m_table_size{}; + u16 m_max_count{}; + u16 m_next_linear_id{MinLinearId}; + u16 m_count{}; + mutable KSpinLock m_lock; + +public: + explicit KHandleTable(KernelCore& kernel_); + ~KHandleTable(); + + constexpr ResultCode Initialize(s32 size) { + R_UNLESS(size <= static_cast(MaxTableSize), ResultOutOfMemory); + + // Initialize all fields. + m_max_count = 0; + m_table_size = static_cast((size <= 0) ? MaxTableSize : size); + m_next_linear_id = MinLinearId; + m_count = 0; + m_free_head_index = -1; + + // Free all entries. + for (s32 i = 0; i < static_cast(m_table_size); ++i) { + m_objects[i] = nullptr; + m_entry_infos[i].next_free_index = i - 1; + m_free_head_index = i; + } + + return RESULT_SUCCESS; + } + + constexpr size_t GetTableSize() const { + return m_table_size; + } + constexpr size_t GetCount() const { + return m_count; + } + constexpr size_t GetMaxCount() const { + return m_max_count; + } + + ResultCode Finalize(); + bool Remove(Handle handle); + + template + KScopedAutoObject GetObjectWithoutPseudoHandle(Handle handle) const { + // Lock and look up in table. + KScopedSpinLock lk(m_lock); + + if constexpr (std::is_same::value) { + return this->GetObjectImpl(handle); + } else { + if (auto* obj = this->GetObjectImpl(handle); obj != nullptr) { + return obj->DynamicCast(); + } else { + return nullptr; + } + } + } + + template + KScopedAutoObject GetObject(Handle handle) const { + // Handle pseudo-handles. + if constexpr (std::derived_from) { + if (handle == Svc::PseudoHandle::CurrentProcess) { + auto* const cur_process = kernel.CurrentProcess(); + ASSERT(cur_process != nullptr); + return cur_process; + } + } else if constexpr (std::derived_from) { + if (handle == Svc::PseudoHandle::CurrentThread) { + auto* const cur_thread = GetCurrentThreadPointer(kernel); + ASSERT(cur_thread != nullptr); + return cur_thread; + } + } + + return this->template GetObjectWithoutPseudoHandle(handle); + } + + ResultCode Reserve(Handle* out_handle); + void Unreserve(Handle handle); + + template + ResultCode Add(Handle* out_handle, T* obj) { + static_assert(std::is_base_of::value); + return this->Add(out_handle, obj, obj->GetTypeObj().GetClassToken()); + } + + template + void Register(Handle handle, T* obj) { + static_assert(std::is_base_of::value); + return this->Register(handle, obj, obj->GetTypeObj().GetClassToken()); + } + + template + bool GetMultipleObjects(T** out, const Handle* handles, size_t num_handles) const { + // Try to convert and open all the handles. + size_t num_opened; + { + // Lock the table. + KScopedSpinLock lk(m_lock); + for (num_opened = 0; num_opened < num_handles; num_opened++) { + // Get the current handle. + const auto cur_handle = handles[num_opened]; + + // Get the object for the current handle. + KAutoObject* cur_object = this->GetObjectImpl(cur_handle); + if (cur_object == nullptr) { + break; + } + + // Cast the current object to the desired type. + T* cur_t = cur_object->DynamicCast(); + if (cur_t == nullptr) { + break; + } + + // Open a reference to the current object. + cur_t->Open(); + out[num_opened] = cur_t; + } + } + + // If we converted every object, succeed. + if (num_opened == num_handles) { + return true; + } + + // If we didn't convert entry object, close the ones we opened. + for (size_t i = 0; i < num_opened; i++) { + out[i]->Close(); + } + + return false; + } + +private: + ResultCode Add(Handle* out_handle, KAutoObject* obj, u16 type); + void Register(Handle handle, KAutoObject* obj, u16 type); + + constexpr s32 AllocateEntry() { + ASSERT(m_count < m_table_size); + + const auto index = m_free_head_index; + + m_free_head_index = m_entry_infos[index].GetNextFreeIndex(); + + m_max_count = std::max(m_max_count, ++m_count); + + return index; + } + + constexpr void FreeEntry(s32 index) { + ASSERT(m_count > 0); + + m_objects[index] = nullptr; + m_entry_infos[index].next_free_index = m_free_head_index; + + m_free_head_index = index; + + --m_count; + } + + constexpr u16 AllocateLinearId() { + const u16 id = m_next_linear_id++; + if (m_next_linear_id > MaxLinearId) { + m_next_linear_id = MinLinearId; + } + return id; + } + + constexpr bool IsValidHandle(Handle handle) const { + // Unpack the handle. + const auto handle_pack = HandlePack(handle); + const auto raw_value = handle_pack.raw; + const auto index = handle_pack.index; + const auto linear_id = handle_pack.linear_id; + const auto reserved = handle_pack.reserved; + ASSERT(reserved == 0); + + // Validate our indexing information. + if (raw_value == 0) { + return false; + } + if (linear_id == 0) { + return false; + } + if (index >= m_table_size) { + return false; + } + + // Check that there's an object, and our serial id is correct. + if (m_objects[index] == nullptr) { + return false; + } + if (m_entry_infos[index].GetLinearId() != linear_id) { + return false; + } + + return true; + } + + constexpr KAutoObject* GetObjectImpl(Handle handle) const { + // Handles must not have reserved bits set. + const auto handle_pack = HandlePack(handle); + if (handle_pack.reserved != 0) { + return nullptr; + } + + if (this->IsValidHandle(handle)) { + return m_objects[handle_pack.index]; + } else { + return nullptr; + } + } + + constexpr KAutoObject* GetObjectByIndexImpl(Handle* out_handle, size_t index) const { + + // Index must be in bounds. + if (index >= m_table_size) { + return nullptr; + } + + // Ensure entry has an object. + if (KAutoObject* obj = m_objects[index]; obj != nullptr) { + *out_handle = EncodeHandle(static_cast(index), m_entry_infos[index].GetLinearId()); + return obj; + } else { + return nullptr; + } + } + +private: + KernelCore& kernel; +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp index e542b1f07..174318180 100644 --- a/src/core/hle/kernel/k_process.cpp +++ b/src/core/hle/kernel/k_process.cpp @@ -354,7 +354,7 @@ ResultCode KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, tls_region_address = CreateTLSRegion(); memory_reservation.Commit(); - return handle_table.SetSize(capabilities.GetHandleTableSize()); + return handle_table.Initialize(capabilities.GetHandleTableSize()); } void KProcess::Run(s32 main_thread_priority, u64 stack_size) { diff --git a/src/core/hle/kernel/k_process.h b/src/core/hle/kernel/k_process.h index 5c54c6360..62ab26b05 100644 --- a/src/core/hle/kernel/k_process.h +++ b/src/core/hle/kernel/k_process.h @@ -11,10 +11,10 @@ #include #include #include "common/common_types.h" -#include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/k_address_arbiter.h" #include "core/hle/kernel/k_auto_object.h" #include "core/hle/kernel/k_condition_variable.h" +#include "core/hle/kernel/k_handle_table.h" #include "core/hle/kernel/k_synchronization_object.h" #include "core/hle/kernel/process_capability.h" #include "core/hle/kernel/slab_helpers.h" @@ -104,12 +104,12 @@ public: } /// Gets a reference to the process' handle table. - HandleTable& GetHandleTable() { + KHandleTable& GetHandleTable() { return handle_table; } /// Gets a const reference to the process' handle table. - const HandleTable& GetHandleTable() const { + const KHandleTable& GetHandleTable() const { return handle_table; } @@ -429,7 +429,7 @@ private: u64 total_process_running_time_ticks = 0; /// Per-process handle table for storing created object handles in. - HandleTable handle_table; + KHandleTable handle_table; /// Per-process address arbiter. KAddressArbiter address_arbiter; diff --git a/src/core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h b/src/core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h index ebecf0c77..1bfbbcfe2 100644 --- a/src/core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h +++ b/src/core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h @@ -8,7 +8,7 @@ #pragma once #include "common/common_types.h" -#include "core/hle/kernel/handle_table.h" +#include "core/hle/kernel/k_handle_table.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/time_manager.h" @@ -17,7 +17,7 @@ namespace Kernel { class [[nodiscard]] KScopedSchedulerLockAndSleep { public: - explicit KScopedSchedulerLockAndSleep(KernelCore & kernel, KThread * t, s64 timeout) + explicit KScopedSchedulerLockAndSleep(KernelCore& kernel, KThread* t, s64 timeout) : kernel(kernel), thread(t), timeout_tick(timeout) { // Lock the scheduler. kernel.GlobalSchedulerContext().scheduler_lock.Lock(); diff --git a/src/core/hle/kernel/k_server_session.cpp b/src/core/hle/kernel/k_server_session.cpp index 3bc259693..c8acaa453 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/handle_table.h" #include "core/hle/kernel/hle_ipc.h" #include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_handle_table.h" #include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_server_session.h" diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp index 3de0157ac..ef6dfeeca 100644 --- a/src/core/hle/kernel/k_thread.cpp +++ b/src/core/hle/kernel/k_thread.cpp @@ -18,8 +18,8 @@ #include "core/core.h" #include "core/cpu_manager.h" #include "core/hardware_properties.h" -#include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/k_condition_variable.h" +#include "core/hle/kernel/k_handle_table.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index f64e07081..825fab694 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/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_handle_table.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" #include "core/hle/kernel/k_process.h" @@ -52,8 +52,7 @@ namespace Kernel { struct KernelCore::Impl { explicit Impl(Core::System& system, KernelCore& kernel) - : time_manager{system}, global_handle_table{kernel}, - object_list_container{kernel}, system{system} {} + : time_manager{system}, object_list_container{kernel}, system{system} {} void SetMulticore(bool is_multicore) { this->is_multicore = is_multicore; @@ -61,6 +60,7 @@ struct KernelCore::Impl { void Initialize(KernelCore& kernel) { global_scheduler_context = std::make_unique(kernel); + global_handle_table = std::make_unique(kernel); service_thread_manager = std::make_unique(1, "yuzu:ServiceThreadManager"); @@ -118,7 +118,7 @@ struct KernelCore::Impl { current_process = nullptr; } - global_handle_table.Clear(); + global_handle_table.reset(); preemption_event = nullptr; @@ -648,7 +648,7 @@ struct KernelCore::Impl { // This is the kernel's handle table or supervisor handle table which // stores all the objects in place. - HandleTable global_handle_table; + std::unique_ptr global_handle_table; KAutoObjectWithListContainer object_list_container; @@ -722,7 +722,7 @@ KResourceLimit* KernelCore::GetSystemResourceLimit() { } KScopedAutoObject KernelCore::RetrieveThreadFromGlobalHandleTable(Handle handle) const { - return impl->global_handle_table.GetObject(handle); + return impl->global_handle_table->GetObject(handle); } void KernelCore::AppendNewProcess(KProcess* process) { @@ -876,12 +876,12 @@ u64 KernelCore::CreateNewUserProcessID() { return impl->next_user_process_id++; } -Kernel::HandleTable& KernelCore::GlobalHandleTable() { - return impl->global_handle_table; +KHandleTable& KernelCore::GlobalHandleTable() { + return *impl->global_handle_table; } -const Kernel::HandleTable& KernelCore::GlobalHandleTable() const { - return impl->global_handle_table; +const KHandleTable& KernelCore::GlobalHandleTable() const { + return *impl->global_handle_table; } void KernelCore::RegisterCoreThread(std::size_t core_id) { diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 0dd9deaeb..7c46aa997 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -14,6 +14,7 @@ #include "core/hle/kernel/k_auto_object.h" #include "core/hle/kernel/k_slab_heap.h" #include "core/hle/kernel/memory_types.h" +#include "core/hle/kernel/svc_common.h" namespace Core { class CPUInterruptHandler; @@ -30,10 +31,10 @@ namespace Kernel { class KClientPort; class GlobalSchedulerContext; -class HandleTable; class KAutoObjectWithListContainer; class KClientSession; class KEvent; +class KHandleTable; class KLinkedListNode; class KMemoryManager; class KPort; @@ -308,10 +309,10 @@ private: u64 CreateNewThreadID(); /// Provides a reference to the global handle table. - Kernel::HandleTable& GlobalHandleTable(); + KHandleTable& GlobalHandleTable(); /// Provides a const reference to the global handle table. - const Kernel::HandleTable& GlobalHandleTable() const; + const KHandleTable& GlobalHandleTable() const; struct Impl; std::unique_ptr impl; diff --git a/src/core/hle/kernel/process_capability.cpp b/src/core/hle/kernel/process_capability.cpp index 4ccac0b06..fcb8b1ea5 100644 --- a/src/core/hle/kernel/process_capability.cpp +++ b/src/core/hle/kernel/process_capability.cpp @@ -6,7 +6,7 @@ #include "common/bit_util.h" #include "common/logging/log.h" -#include "core/hle/kernel/handle_table.h" +#include "core/hle/kernel/k_handle_table.h" #include "core/hle/kernel/k_page_table.h" #include "core/hle/kernel/process_capability.h" #include "core/hle/kernel/svc_results.h" @@ -99,7 +99,7 @@ void ProcessCapabilities::InitializeForMetadatalessProcess() { interrupt_capabilities.set(); // Allow using the maximum possible amount of handles - handle_table_size = static_cast(HandleTable::MAX_COUNT); + handle_table_size = static_cast(KHandleTable::MaxTableSize); // Allow all debugging capabilities. is_debuggable = true; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 156c565b0..d3293a1fe 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -21,12 +21,12 @@ #include "core/core_timing.h" #include "core/core_timing_util.h" #include "core/cpu_manager.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" +#include "core/hle/kernel/k_handle_table.h" #include "core/hle/kernel/k_memory_block.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_page_table.h" @@ -839,10 +839,10 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, Handle } KProcess* const current_process = system.Kernel().CurrentProcess(); - HandleTable& handle_table = current_process->GetHandleTable(); + KHandleTable& handle_table = current_process->GetHandleTable(); const auto resource_limit = current_process->GetResourceLimit(); if (!resource_limit) { - *result = KernelHandle::InvalidHandle; + *result = Svc::InvalidHandle; // Yes, the kernel considers this a successful operation. return RESULT_SUCCESS; } @@ -1993,7 +1993,7 @@ static ResultCode SignalEvent(Core::System& system, Handle event_handle) { LOG_DEBUG(Kernel_SVC, "called, event_handle=0x{:08X}", event_handle); // Get the current handle table. - const HandleTable& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); + const KHandleTable& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); // Get the writable event. KScopedAutoObject writable_event = handle_table.GetObject(event_handle); diff --git a/src/core/hle/kernel/svc_common.h b/src/core/hle/kernel/svc_common.h index 4af049551..8a8f347b5 100644 --- a/src/core/hle/kernel/svc_common.h +++ b/src/core/hle/kernel/svc_common.h @@ -6,9 +6,24 @@ #include "common/common_types.h" +namespace Kernel { +using Handle = u32; +} + namespace Kernel::Svc { constexpr s32 ArgumentHandleCountMax = 0x40; constexpr u32 HandleWaitMask{1u << 30}; +constexpr inline Handle InvalidHandle = Handle(0); + +enum PseudoHandle : Handle { + CurrentThread = 0xFFFF8000, + CurrentProcess = 0xFFFF8001, +}; + +constexpr bool IsPseudoHandle(const Handle& handle) { + return handle == PseudoHandle::CurrentProcess || handle == PseudoHandle::CurrentThread; +} + } // namespace Kernel::Svc diff --git a/src/core/hle/kernel/time_manager.cpp b/src/core/hle/kernel/time_manager.cpp index 59ebfc51f..ae9b4be2f 100644 --- a/src/core/hle/kernel/time_manager.cpp +++ b/src/core/hle/kernel/time_manager.cpp @@ -6,7 +6,6 @@ #include "core/core.h" #include "core/core_timing.h" #include "core/core_timing_util.h" -#include "core/hle/kernel/handle_table.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/yuzu/debugger/wait_tree.cpp b/src/yuzu/debugger/wait_tree.cpp index f040b4c08..bdfda6c54 100644 --- a/src/yuzu/debugger/wait_tree.cpp +++ b/src/yuzu/debugger/wait_tree.cpp @@ -12,8 +12,8 @@ #include "common/assert.h" #include "core/arm/arm_interface.h" #include "core/core.h" -#include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/k_class_token.h" +#include "core/hle/kernel/k_handle_table.h" #include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_scheduler.h" @@ -115,7 +115,7 @@ QString WaitTreeText::GetText() const { return text; } -WaitTreeMutexInfo::WaitTreeMutexInfo(VAddr mutex_address, const Kernel::HandleTable& handle_table) +WaitTreeMutexInfo::WaitTreeMutexInfo(VAddr mutex_address, const Kernel::KHandleTable& handle_table) : mutex_address(mutex_address) { mutex_value = Core::System::GetInstance().Memory().Read32(mutex_address); owner_handle = static_cast(mutex_value & Kernel::Svc::HandleWaitMask); diff --git a/src/yuzu/debugger/wait_tree.h b/src/yuzu/debugger/wait_tree.h index 3dd4acab0..d450345df 100644 --- a/src/yuzu/debugger/wait_tree.h +++ b/src/yuzu/debugger/wait_tree.h @@ -14,11 +14,12 @@ #include "common/common_types.h" #include "core/hle/kernel/k_auto_object.h" +#include "core/hle/kernel/svc_common.h" class EmuThread; namespace Kernel { -class HandleTable; +class KHandleTable; class KReadableEvent; class KSynchronizationObject; class KThread; @@ -74,7 +75,7 @@ public: class WaitTreeMutexInfo : public WaitTreeExpandableItem { Q_OBJECT public: - explicit WaitTreeMutexInfo(VAddr mutex_address, const Kernel::HandleTable& handle_table); + explicit WaitTreeMutexInfo(VAddr mutex_address, const Kernel::KHandleTable& handle_table); ~WaitTreeMutexInfo() override; QString GetText() const override; From 7866eb03bb2e84917ba3930efed6c6b52035f368 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 24 Apr 2021 02:54:16 -0700 Subject: [PATCH 50/82] hle: kernel: svc: ConnectToNamedPort: Use KHandleTable::Reserve. --- src/core/hle/kernel/svc.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index d3293a1fe..80d70a816 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -291,8 +291,7 @@ static ResultCode UnmapMemory32(Core::System& system, u32 dst_addr, u32 src_addr } /// Connect to an OS service given the port name, returns the handle to the port to out -static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle, - VAddr port_name_address) { +static ResultCode ConnectToNamedPort(Core::System& system, Handle* out, VAddr port_name_address) { auto& memory = system.Memory(); if (!memory.IsValidVirtualAddress(port_name_address)) { LOG_ERROR(Kernel_SVC, @@ -324,15 +323,21 @@ static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle, } auto port = it->second; + // Reserve a handle for the port. + // NOTE: Nintendo really does write directly to the output handle here. + R_TRY(handle_table.Reserve(out)); + auto handle_guard = SCOPE_GUARD({ handle_table.Unreserve(*out); }); + // Create a session. KClientSession* session{}; R_TRY(port->CreateSession(std::addressof(session))); // Register the session in the table, close the extra reference. - handle_table.Add(out_handle, session); + handle_table.Register(*out, session); session->Close(); // We succeeded. + handle_guard.Cancel(); return RESULT_SUCCESS; } From caa11748c6929dad3d386f3f8ef8b36158871cad Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:17:11 -0700 Subject: [PATCH 51/82] fixup! hle: kernel: Improve MapSharedMemory and implement UnmapSharedMemory. --- src/core/hle/kernel/svc_wrap.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/hle/kernel/svc_wrap.h b/src/core/hle/kernel/svc_wrap.h index 0968987af..913b16494 100644 --- a/src/core/hle/kernel/svc_wrap.h +++ b/src/core/hle/kernel/svc_wrap.h @@ -557,10 +557,10 @@ void SvcWrap32(Core::System& system) { // Used by MapSharedMemory32 template void SvcWrap32(Core::System& system) { - const u32 retval = - func(system, static_cast(Param(system, 0)), static_cast(Param(system, 1)), static_cast(Param(system, 2)), + const u32 retval = func(system, static_cast(Param(system, 0)), + static_cast(Param(system, 1)), static_cast(Param(system, 2)), static_cast(Param(system, 3))) - .raw; + .raw; FuncReturn(system, retval); } From 57f80c74b662bd6337edc162eff8808cf7001dcf Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:17:45 -0700 Subject: [PATCH 52/82] fixup! hle: kernel: Migrate to KHandleTable. --- src/core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h b/src/core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h index 1bfbbcfe2..b5d405744 100644 --- a/src/core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h +++ b/src/core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h @@ -17,7 +17,7 @@ namespace Kernel { class [[nodiscard]] KScopedSchedulerLockAndSleep { public: - explicit KScopedSchedulerLockAndSleep(KernelCore& kernel, KThread* t, s64 timeout) + explicit KScopedSchedulerLockAndSleep(KernelCore & kernel, KThread * t, s64 timeout) : kernel(kernel), thread(t), timeout_tick(timeout) { // Lock the scheduler. kernel.GlobalSchedulerContext().scheduler_lock.Lock(); From 0536004d9104b2d237f26c42388ec3784abb8c46 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:18:38 -0700 Subject: [PATCH 53/82] fixup! hle: kernel: Rename Process to KProcess. --- src/core/hle/kernel/k_scheduler.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/k_scheduler.h b/src/core/hle/kernel/k_scheduler.h index 282c1863b..b789a64a4 100644 --- a/src/core/hle/kernel/k_scheduler.h +++ b/src/core/hle/kernel/k_scheduler.h @@ -197,7 +197,7 @@ private: class [[nodiscard]] KScopedSchedulerLock : KScopedLock { public: - explicit KScopedSchedulerLock(KernelCore& kernel); + explicit KScopedSchedulerLock(KernelCore & kernel); ~KScopedSchedulerLock(); }; From d3c166d4d5fddb0c19c0219a3efdc85907ebce31 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:22:14 -0700 Subject: [PATCH 54/82] common: Rename NON_COPYABLE/NON_MOVABLE with YUZU_ prefix. --- src/common/common_funcs.h | 4 ++-- src/core/hle/kernel/k_auto_object.h | 6 +++--- src/core/hle/kernel/k_auto_object_container.h | 4 ++-- src/core/hle/kernel/k_handle_table.h | 4 ++-- src/core/hle/service/vi/display/vi_display.h | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index 19bb021e0..17d1ee86b 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h @@ -108,11 +108,11 @@ __declspec(dllimport) void __stdcall DebugBreak(void); } \ } -#define NON_COPYABLE(cls) \ +#define YUZU_NON_COPYABLE(cls) \ cls(const cls&) = delete; \ cls& operator=(const cls&) = delete -#define NON_MOVEABLE(cls) \ +#define YUZU_NON_MOVEABLE(cls) \ cls(cls&&) = delete; \ cls& operator=(cls&&) = delete diff --git a/src/core/hle/kernel/k_auto_object.h b/src/core/hle/kernel/k_auto_object.h index 32aaf9fc5..e99986c68 100644 --- a/src/core/hle/kernel/k_auto_object.h +++ b/src/core/hle/kernel/k_auto_object.h @@ -18,8 +18,8 @@ class KernelCore; class KProcess; #define KERNEL_AUTOOBJECT_TRAITS(CLASS, BASE_CLASS) \ - NON_COPYABLE(CLASS); \ - NON_MOVEABLE(CLASS); \ + YUZU_NON_COPYABLE(CLASS); \ + YUZU_NON_MOVEABLE(CLASS); \ \ private: \ friend class ::Kernel::KClassTokenGenerator; \ @@ -213,7 +213,7 @@ public: template class KScopedAutoObject { - NON_COPYABLE(KScopedAutoObject); + YUZU_NON_COPYABLE(KScopedAutoObject); private: template diff --git a/src/core/hle/kernel/k_auto_object_container.h b/src/core/hle/kernel/k_auto_object_container.h index 6d1cd4862..8609f7a8d 100644 --- a/src/core/hle/kernel/k_auto_object_container.h +++ b/src/core/hle/kernel/k_auto_object_container.h @@ -19,8 +19,8 @@ class KernelCore; class KProcess; class KAutoObjectWithListContainer { - NON_COPYABLE(KAutoObjectWithListContainer); - NON_MOVEABLE(KAutoObjectWithListContainer); + YUZU_NON_COPYABLE(KAutoObjectWithListContainer); + YUZU_NON_MOVEABLE(KAutoObjectWithListContainer); public: using ListType = Common::IntrusiveRedBlackTreeMemberTraits< diff --git a/src/core/hle/kernel/k_handle_table.h b/src/core/hle/kernel/k_handle_table.h index e38ad0fd9..74afb91b1 100644 --- a/src/core/hle/kernel/k_handle_table.h +++ b/src/core/hle/kernel/k_handle_table.h @@ -23,8 +23,8 @@ namespace Kernel { class KernelCore; class KHandleTable { - NON_COPYABLE(KHandleTable); - NON_MOVEABLE(KHandleTable); + YUZU_NON_COPYABLE(KHandleTable); + YUZU_NON_MOVEABLE(KHandleTable); public: static constexpr size_t MaxTableSize = 1024; diff --git a/src/core/hle/service/vi/display/vi_display.h b/src/core/hle/service/vi/display/vi_display.h index a09d982bc..388ce6083 100644 --- a/src/core/hle/service/vi/display/vi_display.h +++ b/src/core/hle/service/vi/display/vi_display.h @@ -25,8 +25,8 @@ class Layer; /// Represents a single display type class Display { - NON_COPYABLE(Display); - NON_MOVEABLE(Display); + YUZU_NON_COPYABLE(Display); + YUZU_NON_MOVEABLE(Display); public: /// Constructs a display with a given unique ID and name. From 91d865795933136fa57fe7fdceffddd1b6c49084 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:25:51 -0700 Subject: [PATCH 55/82] fixup! hle: kernel: Add initial impl. of slab setup. --- src/core/hle/kernel/init/init_slab_setup.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index 04e481a0a..3aa76ee33 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -146,21 +146,21 @@ void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) { VAddr address = memory_layout.GetSlabRegionAddress(); // Initialize slab type array to be in sorted order. - KSlabType slab_types[KSlabType_Count]; - for (size_t i = 0; i < Common::Size(slab_types); i++) { + std::array slab_types; + for (size_t i = 0; i < slab_types.size(); i++) { slab_types[i] = static_cast(i); } // N shuffles the slab type array with the following simple algorithm. - for (size_t i = 0; i < Common::Size(slab_types); i++) { - const size_t rnd = KSystemControl::GenerateRandomRange(0, Common::Size(slab_types) - 1); + for (size_t i = 0; i < slab_types.size(); i++) { + const size_t rnd = KSystemControl::GenerateRandomRange(0, slab_types.size() - 1); std::swap(slab_types[i], slab_types[rnd]); } // Create an array to represent the gaps between the slabs. const size_t total_gap_size = CalculateSlabHeapGapSize(); - size_t slab_gaps[Common::Size(slab_types)]; - for (size_t i = 0; i < Common::Size(slab_gaps); i++) { + std::array slab_gaps; + for (size_t i = 0; i < slab_gaps.size(); i++) { // Note: This is an off-by-one error from Nintendo's intention, because GenerateRandomRange // is inclusive. However, Nintendo also has the off-by-one error, and it's "harmless", so we // will include it ourselves. @@ -169,13 +169,13 @@ void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) { // Sort the array, so that we can treat differences between values as offsets to the starts of // slabs. - for (size_t i = 1; i < Common::Size(slab_gaps); i++) { + for (size_t i = 1; i < slab_gaps.size(); i++) { for (size_t j = i; j > 0 && slab_gaps[j - 1] > slab_gaps[j]; j--) { std::swap(slab_gaps[j], slab_gaps[j - 1]); } } - for (size_t i = 0; i < Common::Size(slab_types); i++) { + for (size_t i = 0; i < slab_types.size(); i++) { // Add the random gap to the address. address += (i == 0) ? slab_gaps[0] : slab_gaps[i] - slab_gaps[i - 1]; From 9bae3992e613ef93df45d025021f87aa75e486dc Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:27:13 -0700 Subject: [PATCH 56/82] fixup! hle: kernel: Add initial impl. of KAutoObject. --- src/core/hle/kernel/k_auto_object.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/k_auto_object.h b/src/core/hle/kernel/k_auto_object.h index e99986c68..452325f73 100644 --- a/src/core/hle/kernel/k_auto_object.h +++ b/src/core/hle/kernel/k_auto_object.h @@ -41,7 +41,7 @@ public: virtual TypeObj GetTypeObj() const { \ return GetStaticTypeObj(); \ } \ - virtual const char* GetTypeName() { \ + virtual const char* GetTypeName() const { \ return GetStaticTypeName(); \ } \ \ From 25538db150a90f01356c238e65548ccf3e797cd8 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:36:21 -0700 Subject: [PATCH 57/82] fixup! hle: kernel: Add initial impl. of KAutoObject. --- src/core/hle/kernel/k_auto_object.h | 92 ++++++++++++++--------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/src/core/hle/kernel/k_auto_object.h b/src/core/hle/kernel/k_auto_object.h index 452325f73..765e46670 100644 --- a/src/core/hle/kernel/k_auto_object.h +++ b/src/core/hle/kernel/k_auto_object.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include "common/assert.h" #include "common/common_funcs.h" @@ -45,19 +46,15 @@ public: return GetStaticTypeName(); \ } \ \ -private: +private: \ + constexpr bool operator!=(const TypeObj& rhs) class KAutoObject { protected: class TypeObj { - private: - const char* m_name; - ClassTokenType m_class_token; - public: constexpr explicit TypeObj(const char* n, ClassTokenType tok) - : m_name(n), m_class_token(tok) { // ... - } + : m_name(n), m_class_token(tok) {} constexpr const char* GetName() const { return m_name; @@ -66,35 +63,31 @@ protected: return m_class_token; } - constexpr bool operator==(const TypeObj& rhs) { + constexpr bool operator==(const TypeObj& rhs) const { return this->GetClassToken() == rhs.GetClassToken(); } - constexpr bool operator!=(const TypeObj& rhs) { + constexpr bool operator!=(const TypeObj& rhs) const { return this->GetClassToken() != rhs.GetClassToken(); } - constexpr bool IsDerivedFrom(const TypeObj& rhs) { + constexpr bool IsDerivedFrom(const TypeObj& rhs) const { return (this->GetClassToken() | rhs.GetClassToken()) == this->GetClassToken(); } + + private: + const char* m_name; + ClassTokenType m_class_token; }; private: KERNEL_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject); -private: - std::atomic m_ref_count{}; - -protected: - KernelCore& kernel; - std::string name; - -public: - static KAutoObject* Create(KAutoObject* ptr); - public: explicit KAutoObject(KernelCore& kernel_) : kernel(kernel_) {} - virtual ~KAutoObject() {} + virtual ~KAutoObject() = default; + + static KAutoObject* Create(KAutoObject* ptr); // Destroy is responsible for destroying the auto object's resources when ref_count hits zero. virtual void Destroy() { @@ -122,8 +115,8 @@ public: template Derived DynamicCast() { - static_assert(std::is_pointer::value); - using DerivedType = typename std::remove_pointer::type; + static_assert(std::is_pointer_v); + using DerivedType = std::remove_pointer_t; if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) { return static_cast(this); @@ -134,8 +127,8 @@ public: template const Derived DynamicCast() const { - static_assert(std::is_pointer::value); - using DerivedType = typename std::remove_pointer::type; + static_assert(std::is_pointer_v); + using DerivedType = std::remove_pointer_t; if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) { return static_cast(this); @@ -171,20 +164,18 @@ public: this->Destroy(); } } + +protected: + KernelCore& kernel; + std::string name; + +private: + std::atomic m_ref_count{}; }; class KAutoObjectWithListContainer; class KAutoObjectWithList : public KAutoObject { -private: - friend class KAutoObjectWithListContainer; - -private: - Common::IntrusiveRedBlackTreeNode list_node; - -protected: - KernelCore& kernel; - public: explicit KAutoObjectWithList(KernelCore& kernel_) : KAutoObject(kernel_), kernel(kernel_) {} @@ -209,24 +200,21 @@ public: virtual const std::string& GetName() const { return name; } + +private: + friend class KAutoObjectWithListContainer; + +private: + Common::IntrusiveRedBlackTreeNode list_node; + +protected: + KernelCore& kernel; }; template class KScopedAutoObject { YUZU_NON_COPYABLE(KScopedAutoObject); -private: - template - friend class KScopedAutoObject; - -private: - T* m_obj{}; - -private: - constexpr void Swap(KScopedAutoObject& rhs) { - std::swap(m_obj, rhs.m_obj); - } - public: constexpr KScopedAutoObject() = default; @@ -301,6 +289,18 @@ public: constexpr bool IsNotNull() const { return m_obj != nullptr; } + +private: + template + friend class KScopedAutoObject; + +private: + T* m_obj{}; + +private: + constexpr void Swap(KScopedAutoObject& rhs) noexcept { + std::swap(m_obj, rhs.m_obj); + } }; } // namespace Kernel From 51aa5a5364dffb7d8d278a66e69f190aef05c666 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:36:48 -0700 Subject: [PATCH 58/82] fixup! hle: kernel: Add initial impl. of KAutoObjectWithListContainer. --- src/core/hle/kernel/k_auto_object_container.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/core/hle/kernel/k_auto_object_container.cpp b/src/core/hle/kernel/k_auto_object_container.cpp index 85d03ebe3..fc0c28874 100644 --- a/src/core/hle/kernel/k_auto_object_container.cpp +++ b/src/core/hle/kernel/k_auto_object_container.cpp @@ -21,15 +21,8 @@ void KAutoObjectWithListContainer::Unregister(KAutoObjectWithList* obj) { size_t KAutoObjectWithListContainer::GetOwnedCount(KProcess* owner) { KScopedLightLock lk(m_lock); - size_t count = 0; - - for (auto& obj : m_object_list) { - if (obj.GetOwner() == owner) { - count++; - } - } - - return count; + return std::count_if(m_object_list.begin(), m_object_list.end(), + [&](const auto& obj) { return obj.GetOwner() == owner; }); } } // namespace Kernel From 4356361faf9b3fb87d55535e2e8300a8a4134a1d Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:38:23 -0700 Subject: [PATCH 59/82] fixup! hle: kernel: Add initial impl. of KAutoObjectWithListContainer. --- src/core/hle/kernel/k_auto_object_container.h | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/core/hle/kernel/k_auto_object_container.h b/src/core/hle/kernel/k_auto_object_container.h index 8609f7a8d..ff40cf5a7 100644 --- a/src/core/hle/kernel/k_auto_object_container.h +++ b/src/core/hle/kernel/k_auto_object_container.h @@ -28,16 +28,11 @@ public: public: class ListAccessor : public KScopedLightLock { - private: - ListType& m_list; - public: explicit ListAccessor(KAutoObjectWithListContainer* container) - : KScopedLightLock(container->m_lock), m_list(container->m_object_list) { /* ... */ - } + : KScopedLightLock(container->m_lock), m_list(container->m_object_list) {} explicit ListAccessor(KAutoObjectWithListContainer& container) - : KScopedLightLock(container.m_lock), m_list(container.m_object_list) { /* ... */ - } + : KScopedLightLock(container.m_lock), m_list(container.m_object_list) {} typename ListType::iterator begin() const { return m_list.begin(); @@ -50,14 +45,13 @@ public: typename ListType::iterator find(typename ListType::const_reference ref) const { return m_list.find(ref); } + + private: + ListType& m_list; }; friend class ListAccessor; -private: - KLightLock m_lock; - ListType m_object_list; - public: KAutoObjectWithListContainer(KernelCore& kernel) : m_lock(kernel), m_object_list() {} @@ -67,6 +61,10 @@ public: void Register(KAutoObjectWithList* obj); void Unregister(KAutoObjectWithList* obj); size_t GetOwnedCount(KProcess* owner); + +private: + KLightLock m_lock; + ListType m_object_list; }; } // namespace Kernel From d23f9f75fff3b65ce5b3feae873df9726e099e93 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:42:44 -0700 Subject: [PATCH 60/82] fixup! hle: kernel: Migrate to KHandleTable. --- src/core/hle/kernel/k_handle_table.h | 118 +++++++++++++-------------- 1 file changed, 58 insertions(+), 60 deletions(-) diff --git a/src/core/hle/kernel/k_handle_table.h b/src/core/hle/kernel/k_handle_table.h index 74afb91b1..adce899c3 100644 --- a/src/core/hle/kernel/k_handle_table.h +++ b/src/core/hle/kernel/k_handle_table.h @@ -29,58 +29,11 @@ class KHandleTable { public: static constexpr size_t MaxTableSize = 1024; -private: - union HandlePack { - u32 raw; - BitField<0, 15, u32> index; - BitField<15, 15, u32> linear_id; - BitField<30, 2, u32> reserved; - }; - - static constexpr u16 MinLinearId = 1; - static constexpr u16 MaxLinearId = 0x7FFF; - - static constexpr Handle EncodeHandle(u16 index, u16 linear_id) { - HandlePack handle{}; - handle.index.Assign(index); - handle.linear_id.Assign(linear_id); - handle.reserved.Assign(0); - return handle.raw; - } - - union EntryInfo { - struct { - u16 linear_id; - u16 type; - } info; - s32 next_free_index; - - constexpr u16 GetLinearId() const { - return info.linear_id; - } - constexpr u16 GetType() const { - return info.type; - } - constexpr s32 GetNextFreeIndex() const { - return next_free_index; - } - }; - -private: - std::array m_entry_infos{}; - std::array m_objects{}; - s32 m_free_head_index{-1}; - u16 m_table_size{}; - u16 m_max_count{}; - u16 m_next_linear_id{MinLinearId}; - u16 m_count{}; - mutable KSpinLock m_lock; - public: explicit KHandleTable(KernelCore& kernel_); ~KHandleTable(); - constexpr ResultCode Initialize(s32 size) { + ResultCode Initialize(s32 size) { R_UNLESS(size <= static_cast(MaxTableSize), ResultOutOfMemory); // Initialize all fields. @@ -100,13 +53,13 @@ public: return RESULT_SUCCESS; } - constexpr size_t GetTableSize() const { + size_t GetTableSize() const { return m_table_size; } - constexpr size_t GetCount() const { + size_t GetCount() const { return m_count; } - constexpr size_t GetMaxCount() const { + size_t GetMaxCount() const { return m_max_count; } @@ -118,7 +71,7 @@ public: // Lock and look up in table. KScopedSpinLock lk(m_lock); - if constexpr (std::is_same::value) { + if constexpr (std::is_same_v) { return this->GetObjectImpl(handle); } else { if (auto* obj = this->GetObjectImpl(handle); obj != nullptr) { @@ -154,13 +107,13 @@ public: template ResultCode Add(Handle* out_handle, T* obj) { - static_assert(std::is_base_of::value); + static_assert(std::is_base_o_vf); return this->Add(out_handle, obj, obj->GetTypeObj().GetClassToken()); } template void Register(Handle handle, T* obj) { - static_assert(std::is_base_of::value); + static_assert(std::is_base_of_v); return this->Register(handle, obj, obj->GetTypeObj().GetClassToken()); } @@ -210,7 +163,7 @@ private: ResultCode Add(Handle* out_handle, KAutoObject* obj, u16 type); void Register(Handle handle, KAutoObject* obj, u16 type); - constexpr s32 AllocateEntry() { + s32 AllocateEntry() { ASSERT(m_count < m_table_size); const auto index = m_free_head_index; @@ -222,7 +175,7 @@ private: return index; } - constexpr void FreeEntry(s32 index) { + void FreeEntry(s32 index) { ASSERT(m_count > 0); m_objects[index] = nullptr; @@ -233,7 +186,7 @@ private: --m_count; } - constexpr u16 AllocateLinearId() { + u16 AllocateLinearId() { const u16 id = m_next_linear_id++; if (m_next_linear_id > MaxLinearId) { m_next_linear_id = MinLinearId; @@ -241,7 +194,7 @@ private: return id; } - constexpr bool IsValidHandle(Handle handle) const { + bool IsValidHandle(Handle handle) const { // Unpack the handle. const auto handle_pack = HandlePack(handle); const auto raw_value = handle_pack.raw; @@ -272,7 +225,7 @@ private: return true; } - constexpr KAutoObject* GetObjectImpl(Handle handle) const { + KAutoObject* GetObjectImpl(Handle handle) const { // Handles must not have reserved bits set. const auto handle_pack = HandlePack(handle); if (handle_pack.reserved != 0) { @@ -286,7 +239,7 @@ private: } } - constexpr KAutoObject* GetObjectByIndexImpl(Handle* out_handle, size_t index) const { + KAutoObject* GetObjectByIndexImpl(Handle* out_handle, size_t index) const { // Index must be in bounds. if (index >= m_table_size) { @@ -303,6 +256,51 @@ private: } private: + union HandlePack { + u32 raw; + BitField<0, 15, u32> index; + BitField<15, 15, u32> linear_id; + BitField<30, 2, u32> reserved; + }; + + static constexpr u16 MinLinearId = 1; + static constexpr u16 MaxLinearId = 0x7FFF; + + static constexpr Handle EncodeHandle(u16 index, u16 linear_id) { + HandlePack handle{}; + handle.index.Assign(index); + handle.linear_id.Assign(linear_id); + handle.reserved.Assign(0); + return handle.raw; + } + + union EntryInfo { + struct { + u16 linear_id; + u16 type; + } info; + s32 next_free_index; + + constexpr u16 GetLinearId() const { + return info.linear_id; + } + constexpr u16 GetType() const { + return info.type; + } + constexpr s32 GetNextFreeIndex() const { + return next_free_index; + } + }; + +private: + std::array m_entry_infos{}; + std::array m_objects{}; + s32 m_free_head_index{-1}; + u16 m_table_size{}; + u16 m_max_count{}; + u16 m_next_linear_id{MinLinearId}; + u16 m_count{}; + mutable KSpinLock m_lock; KernelCore& kernel; }; From 50d2dc3b514887510ffb012478c049ee2d1b9e75 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:43:00 -0700 Subject: [PATCH 61/82] fixup! hle: kernel: Migrate KPort, KClientPort, and KServerPort to KAutoObject. --- src/core/hle/kernel/k_client_port.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/k_client_port.h b/src/core/hle/kernel/k_client_port.h index f971a8b2c..ec1d7e12e 100644 --- a/src/core/hle/kernel/k_client_port.h +++ b/src/core/hle/kernel/k_client_port.h @@ -28,7 +28,7 @@ public: void OnSessionFinalized(); void OnServerClosed(); - constexpr const KPort* GetParent() const { + const KPort* GetParent() const { return parent; } From 1b074b898450af28ba1f96e0cb3bf4a9c5687b5d Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:43:15 -0700 Subject: [PATCH 62/82] fixup! hle: kernel: Migrate KSession, KClientSession, and KServerSession to KAutoObject. --- src/core/hle/kernel/k_client_session.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/k_client_session.h b/src/core/hle/kernel/k_client_session.h index 1480597e2..6476a588b 100644 --- a/src/core/hle/kernel/k_client_session.h +++ b/src/core/hle/kernel/k_client_session.h @@ -45,7 +45,7 @@ public: virtual void Destroy() override; static void PostDestroy([[maybe_unused]] uintptr_t arg) {} - constexpr KSession* GetParent() const { + KSession* GetParent() const { return parent; } From f6d45b747e37ed1871d9155fbf2d3d5099e1c1b8 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:48:41 -0700 Subject: [PATCH 63/82] fixup! hle: kernel: Migrate KSession, KClientSession, and KServerSession to KAutoObject. --- src/core/hle/kernel/k_server_session.h | 4 ++-- src/core/hle/kernel/k_session.cpp | 24 +++++++++++++++--------- src/core/hle/kernel/k_session.h | 22 +++++++++++----------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/core/hle/kernel/k_server_session.h b/src/core/hle/kernel/k_server_session.h index 4a54e6634..77095bb85 100644 --- a/src/core/hle/kernel/k_server_session.h +++ b/src/core/hle/kernel/k_server_session.h @@ -47,11 +47,11 @@ public: void Initialize(KSession* parent_, std::string&& name_); - constexpr KSession* GetParent() { + KSession* GetParent() { return parent; } - constexpr const KSession* GetParent() const { + const KSession* GetParent() const { return parent; } diff --git a/src/core/hle/kernel/k_session.cpp b/src/core/hle/kernel/k_session.cpp index 5e629d446..7b0bc177d 100644 --- a/src/core/hle/kernel/k_session.cpp +++ b/src/core/hle/kernel/k_session.cpp @@ -49,24 +49,30 @@ void KSession::Initialize(KClientPort* port_, const std::string& name_) { } void KSession::Finalize() { - if (port != nullptr) { - port->OnSessionFinalized(); - port->Close(); + if (port == nullptr) { + return; } + + port->OnSessionFinalized(); + port->Close(); } void KSession::OnServerClosed() { - if (GetState() == State::Normal) { - SetState(State::ServerClosed); - client.OnServerClosed(); + if (GetState() != State::Normal) { + return; } + + SetState(State::ServerClosed); + client.OnServerClosed(); } void KSession::OnClientClosed() { - if (GetState() == State::Normal) { - SetState(State::ClientClosed); - server.OnClientClosed(); + if (GetState() != State::Normal) { + return; } + + SetState(State::ClientClosed); + server.OnClientClosed(); } void KSession::PostDestroy(uintptr_t arg) { diff --git a/src/core/hle/kernel/k_session.h b/src/core/hle/kernel/k_session.h index d50e21f3f..4321b7885 100644 --- a/src/core/hle/kernel/k_session.h +++ b/src/core/hle/kernel/k_session.h @@ -16,14 +16,6 @@ namespace Kernel { class KSession final : public KAutoObjectWithSlabHeapAndContainer { KERNEL_AUTOOBJECT_TRAITS(KSession, KAutoObject); -private: - enum class State : u8 { - Invalid = 0, - Normal = 1, - ClientClosed = 2, - ServerClosed = 3, - }; - public: explicit KSession(KernelCore& kernel); virtual ~KSession() override; @@ -74,20 +66,28 @@ public: return port; } +private: + enum class State : u8 { + Invalid = 0, + Normal = 1, + ClientClosed = 2, + ServerClosed = 3, + }; + private: void SetState(State state) { atomic_state = static_cast(state); } State GetState() const { - return static_cast(atomic_state.load()); + return static_cast(atomic_state.load(std::memory_order_relaxed)); } private: KServerSession server; KClientSession client; - std::atomic::type> atomic_state{ - static_cast::type>(State::Invalid)}; + std::atomic> atomic_state{ + static_cast>(State::Invalid)}; KClientPort* port{}; KProcess* process{}; bool initialized{}; From 34abe4a90554af20f8fcceb9effa9bc6f5bb0587 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:48:58 -0700 Subject: [PATCH 64/82] fixup! hle: kernel: Migrate KPort, KClientPort, and KServerPort to KAutoObject. --- src/core/hle/kernel/k_server_port.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/k_server_port.h b/src/core/hle/kernel/k_server_port.h index a46fa8f54..558c8ed4d 100644 --- a/src/core/hle/kernel/k_server_port.h +++ b/src/core/hle/kernel/k_server_port.h @@ -58,7 +58,7 @@ public: KServerSession* AcceptSession(); - constexpr const KPort* GetParent() const { + const KPort* GetParent() const { return parent; } From 2cdc7142b0a3489205bdb0e0c7862aeec2a98c5a Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:50:35 -0700 Subject: [PATCH 65/82] fixup! hle: kernel: Migrate to KHandleTable. --- src/core/hle/kernel/k_handle_table.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/k_handle_table.h b/src/core/hle/kernel/k_handle_table.h index adce899c3..8a08bdbdc 100644 --- a/src/core/hle/kernel/k_handle_table.h +++ b/src/core/hle/kernel/k_handle_table.h @@ -107,7 +107,7 @@ public: template ResultCode Add(Handle* out_handle, T* obj) { - static_assert(std::is_base_o_vf); + static_assert(std::is_base_of_v); return this->Add(out_handle, obj, obj->GetTypeObj().GetClassToken()); } From 9beb239634735dc026f55b20d31763cdc295d15c Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:51:11 -0700 Subject: [PATCH 66/82] fixup! hle: kernel: Add initial impl. of KLinkedList. --- src/core/hle/kernel/k_linked_list.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/core/hle/kernel/k_linked_list.h b/src/core/hle/kernel/k_linked_list.h index 8218bac8d..500f44685 100644 --- a/src/core/hle/kernel/k_linked_list.h +++ b/src/core/hle/kernel/k_linked_list.h @@ -15,19 +15,20 @@ class KernelCore; class KLinkedListNode : public boost::intrusive::list_base_hook<>, public KSlabAllocated { -private: - void* m_item; public: - KLinkedListNode() : m_item(nullptr) {} + KLinkedListNode() = default; - constexpr void Initialize(void* it) { + void Initialize(void* it) { m_item = it; } - constexpr void* GetItem() const { + void* GetItem() const { return m_item; } + +private: + void* m_item = nullptr; }; template @@ -61,13 +62,9 @@ public: using iterator_category = std::bidirectional_iterator_tag; using value_type = typename KLinkedList::value_type; using difference_type = typename KLinkedList::difference_type; - using pointer = typename std::conditional::type; - using reference = typename std::conditional::type; - - private: - BaseIterator m_base_it; + using pointer = std::conditional_t; + using reference = + std::conditional_t; public: explicit Iterator(BaseIterator it) : m_base_it(it) {} @@ -117,6 +114,9 @@ public: operator Iterator() const { return Iterator(m_base_it); } + + private: + BaseIterator m_base_it; }; public: From 703d7aaab6cafa1926f85a77f5fdb9c72811aa6a Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:53:37 -0700 Subject: [PATCH 67/82] fixup! hle: kernel: Migrate to KHandleTable. --- src/core/hle/kernel/svc_common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/svc_common.h b/src/core/hle/kernel/svc_common.h index 8a8f347b5..60ea2c405 100644 --- a/src/core/hle/kernel/svc_common.h +++ b/src/core/hle/kernel/svc_common.h @@ -22,7 +22,7 @@ enum PseudoHandle : Handle { CurrentProcess = 0xFFFF8001, }; -constexpr bool IsPseudoHandle(const Handle& handle) { +constexpr bool IsPseudoHandle(Handle handle) { return handle == PseudoHandle::CurrentProcess || handle == PseudoHandle::CurrentThread; } From 9434603450088e9d155dcb0f36435470c925e271 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:53:56 -0700 Subject: [PATCH 68/82] fixup! hle: kernel: Migrate KReadableEvent and KWritableEvent to KAutoObject. --- src/core/hle/kernel/k_readable_event.h | 2 +- src/core/hle/kernel/k_writable_event.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/hle/kernel/k_readable_event.h b/src/core/hle/kernel/k_readable_event.h index 8514d065a..1783ef0b8 100644 --- a/src/core/hle/kernel/k_readable_event.h +++ b/src/core/hle/kernel/k_readable_event.h @@ -27,7 +27,7 @@ public: name = std::move(name_); } - constexpr KEvent* GetParent() const { + KEvent* GetParent() const { return parent; } diff --git a/src/core/hle/kernel/k_writable_event.h b/src/core/hle/kernel/k_writable_event.h index f5e083482..154d2382c 100644 --- a/src/core/hle/kernel/k_writable_event.h +++ b/src/core/hle/kernel/k_writable_event.h @@ -29,7 +29,7 @@ public: ResultCode Signal(); ResultCode Clear(); - constexpr KEvent* GetParent() const { + KEvent* GetParent() const { return parent; } From 27a6ef64fd06b7fd5de8d3dc9a3939368593b808 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 14:16:26 -0700 Subject: [PATCH 69/82] fixup! common: intrusive_red_black_tree: Disable static_assert that will not evaluate as constant on MSVC. --- src/common/intrusive_red_black_tree.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/common/intrusive_red_black_tree.h b/src/common/intrusive_red_black_tree.h index 15f972054..1f696fe80 100644 --- a/src/common/intrusive_red_black_tree.h +++ b/src/common/intrusive_red_black_tree.h @@ -509,11 +509,6 @@ private: private: static constexpr TypedStorage DerivedStorage = {}; - -#ifndef _MSC_VER - // TODO(bunnei): Enable on MSVC once this can be const evaluated by the compiler - static_assert(GetParent(GetNode(GetPointer(DerivedStorage))) == GetPointer(DerivedStorage)); -#endif }; template > From e02785be83dd3d02a8fc7aebc13337546f04bab8 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 14:32:36 -0700 Subject: [PATCH 70/82] common: parent_of_member: Fix build for OffsetOf(). --- src/common/parent_of_member.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/common/parent_of_member.h b/src/common/parent_of_member.h index d9a14529d..e0f8ab5c8 100644 --- a/src/common/parent_of_member.h +++ b/src/common/parent_of_member.h @@ -133,27 +133,27 @@ template using GetMemberType = typename GetMemberPointerTraits::Member; template > -static inline std::ptrdiff_t OffsetOf = [] { +constexpr std::ptrdiff_t OffsetOf() { using DeducedParentType = GetParentType; using MemberType = GetMemberType; static_assert(std::is_base_of::value || std::is_same::value); return OffsetOfCalculator::OffsetOf(MemberPtr); -}(); +}; } // namespace impl template > constexpr RealParentType& GetParentReference(impl::GetMemberType* member) { - std::ptrdiff_t Offset = impl::OffsetOf; + std::ptrdiff_t Offset = impl::OffsetOf(); return *static_cast( static_cast(static_cast(static_cast(member)) - Offset)); } template > constexpr RealParentType const& GetParentReference(impl::GetMemberType const* member) { - std::ptrdiff_t Offset = impl::OffsetOf; + std::ptrdiff_t Offset = impl::OffsetOf(); return *static_cast(static_cast( static_cast(static_cast(member)) - Offset)); } From ad5a5ef43f91229895d6ce93b66cfdfc1cdbd261 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 21:42:11 -0700 Subject: [PATCH 71/82] fixup! hle: kernel: Migrate more of KThread to KAutoObject. --- src/core/hle/kernel/slab_helpers.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/core/hle/kernel/slab_helpers.h b/src/core/hle/kernel/slab_helpers.h index 66954b6b2..0c5995db0 100644 --- a/src/core/hle/kernel/slab_helpers.h +++ b/src/core/hle/kernel/slab_helpers.h @@ -74,13 +74,6 @@ private: kernel.SlabHeap().Free(obj); } -public: - class ListAccessor : public KAutoObjectWithListContainer::ListAccessor { - public: - ListAccessor() : KAutoObjectWithListContainer::ListAccessor(kernel.ObjectListContainer()) {} - ~ListAccessor() = default; - }; - public: KAutoObjectWithSlabHeapAndContainer(KernelCore& kernel_) : Base(kernel_), kernel(kernel_) {} virtual ~KAutoObjectWithSlabHeapAndContainer() {} From 1e983b19df056183cbc729132704e0101ea8a86e Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 22:07:29 -0700 Subject: [PATCH 72/82] fixup! hle: kernel: Migrate to KHandleTable. --- src/core/hle/kernel/k_handle_table.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/hle/kernel/k_handle_table.h b/src/core/hle/kernel/k_handle_table.h index 8a08bdbdc..ba9dd061d 100644 --- a/src/core/hle/kernel/k_handle_table.h +++ b/src/core/hle/kernel/k_handle_table.h @@ -257,6 +257,9 @@ private: private: union HandlePack { + HandlePack() = default; + HandlePack(Handle handle) : raw{static_cast(handle)} {} + u32 raw; BitField<0, 15, u32> index; BitField<15, 15, u32> linear_id; From f23760b1e1b4dc703dffaa245b7b469141935342 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 22:30:42 -0700 Subject: [PATCH 73/82] fixup! hle: kernel: Add initial impl. of slab setup. --- src/core/hle/kernel/init/init_slab_setup.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index 3aa76ee33..2dd792e71 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -117,10 +117,6 @@ void InitializeSlabResourceCounts() { } } -size_t CalculateSlabHeapGapSize() { - return KernelSlabHeapGapsSize; -} - size_t CalculateTotalSlabHeapSize() { size_t size = 0; @@ -136,7 +132,7 @@ size_t CalculateTotalSlabHeapSize() { #undef ADD_SLAB_SIZE // Add the reserved size. - size += CalculateSlabHeapGapSize(); + size += KernelSlabHeapGapsSize; return size; } @@ -158,7 +154,7 @@ void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) { } // Create an array to represent the gaps between the slabs. - const size_t total_gap_size = CalculateSlabHeapGapSize(); + const size_t total_gap_size = KernelSlabHeapGapsSize; std::array slab_gaps; for (size_t i = 0; i < slab_gaps.size(); i++) { // Note: This is an off-by-one error from Nintendo's intention, because GenerateRandomRange From da22def511ab164d4d33a4d4e83a9ccf65a8b47d Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 23:24:51 -0700 Subject: [PATCH 74/82] hle: kernel: Fix un/sign mismatch errors with NUM_CPU_CORES. --- src/core/hle/kernel/kernel.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 825fab694..5ebd47e49 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -102,7 +102,7 @@ struct KernelCore::Impl { next_user_process_id = KProcess::ProcessIDMin; next_thread_id = 1; - for (s32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { + for (u32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { if (suspend_threads[core_id]) { suspend_threads[core_id]->Close(); suspend_threads[core_id] = nullptr; @@ -211,7 +211,7 @@ struct KernelCore::Impl { } void InitializeSuspendThreads() { - for (s32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { + for (u32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { suspend_threads[core_id] = KThread::Create(system.Kernel()); ASSERT(KThread::InitializeHighPriorityThread(system, suspend_threads[core_id], {}, {}, core_id) @@ -953,7 +953,7 @@ void KernelCore::Suspend(bool in_suspention) { { KScopedSchedulerLock lock(*this); const auto state = should_suspend ? ThreadState::Runnable : ThreadState::Waiting; - for (s32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { + for (u32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { impl->suspend_threads[core_id]->SetState(state); impl->suspend_threads[core_id]->SetWaitReasonForDebugging( ThreadWaitReasonForDebugging::Suspended); From eae107d0e9764ac2773a20e744c17201cfe2f814 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 2 May 2021 00:44:28 -0700 Subject: [PATCH 75/82] kernel: svc: Remove unused RetrieveResourceLimitValue function. --- src/core/hle/kernel/svc.cpp | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 80d70a816..52011be9c 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -141,38 +141,6 @@ enum class ResourceLimitValueType { PeakValue, }; -ResultVal RetrieveResourceLimitValue(Core::System& system, Handle resource_limit, - u32 resource_type, ResourceLimitValueType value_type) { - std::lock_guard lock{HLE::g_hle_lock}; - const auto type = static_cast(resource_type); - if (!IsValidResourceType(type)) { - LOG_ERROR(Kernel_SVC, "Invalid resource limit type: '{}'", resource_type); - return ResultInvalidEnumValue; - } - - const auto* const current_process = system.Kernel().CurrentProcess(); - ASSERT(current_process != nullptr); - - auto resource_limit_object = - current_process->GetHandleTable().GetObject(resource_limit); - if (resource_limit_object.IsNull()) { - LOG_ERROR(Kernel_SVC, "Handle to non-existent resource limit instance used. Handle={:08X}", - resource_limit); - return ResultInvalidHandle; - } - - switch (value_type) { - case ResourceLimitValueType::CurrentValue: - return MakeResult(resource_limit_object->GetCurrentValue(type)); - case ResourceLimitValueType::LimitValue: - return MakeResult(resource_limit_object->GetLimitValue(type)); - case ResourceLimitValueType::PeakValue: - return MakeResult(resource_limit_object->GetPeakValue(type)); - default: - LOG_ERROR(Kernel_SVC, "Invalid resource value_type: '{}'", value_type); - return ResultInvalidEnumValue; - } -} } // Anonymous namespace /// Set the process heap to a given Size. It can both extend and shrink the heap. From 9f812215286a0757a3d4e15131c9103d41a5327b Mon Sep 17 00:00:00 2001 From: bunnei Date: Mon, 3 May 2021 18:36:01 -0700 Subject: [PATCH 76/82] fixup! hle: kernel: Ensure all kernel objects with KAutoObject are properly created. --- src/core/hle/service/am/am.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index e8b82db00..408e441dc 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -1338,8 +1338,6 @@ IApplicationFunctions::IApplicationFunctions(Core::System& system_) RegisterHandlers(functions); - auto& kernel = system.Kernel(); - Kernel::KAutoObject::Create(std::addressof(gpu_error_detected_event)); Kernel::KAutoObject::Create(std::addressof(friend_invitation_storage_channel_event)); Kernel::KAutoObject::Create(std::addressof(health_warning_disappeared_system_event)); From 510f71d87191e598cf2067c89d16103b04079157 Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 4 May 2021 19:50:14 -0700 Subject: [PATCH 77/82] fixup! hle: kernel: Ensure all kernel objects with KAutoObject are properly created. --- src/core/hle/service/nifm/nifm.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/core/hle/service/nifm/nifm.cpp b/src/core/hle/service/nifm/nifm.cpp index c91427b8b..94ef3983a 100644 --- a/src/core/hle/service/nifm/nifm.cpp +++ b/src/core/hle/service/nifm/nifm.cpp @@ -158,8 +158,6 @@ public: }; RegisterHandlers(functions); - auto& kernel = system.Kernel(); - Kernel::KAutoObject::Create(std::addressof(event1)); Kernel::KAutoObject::Create(std::addressof(event2)); From a488b86e97df2bf188938f01fb484aa420298fef Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 4 May 2021 20:53:45 -0700 Subject: [PATCH 78/82] fixup! common: bit_util: Add BIT macro. --- src/common/bit_util.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/common/bit_util.h b/src/common/bit_util.h index 683f09158..64520ca4e 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -44,6 +44,4 @@ template return static_cast(log2_f + static_cast((value ^ (1ULL << log2_f)) != 0ULL)); } -#define BIT(n) (1U << (n)) - } // namespace Common From 2c4615f3a65c10ae50e1c6bc2e389b0f5f14e3fa Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 4 May 2021 20:55:13 -0700 Subject: [PATCH 79/82] fixup! hle: kernel: Migrate more of KThread to KAutoObject. --- src/core/hle/kernel/k_class_token.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/k_class_token.h b/src/core/hle/kernel/k_class_token.h index fb4307cd0..c28db49ec 100644 --- a/src/core/hle/kernel/k_class_token.h +++ b/src/core/hle/kernel/k_class_token.h @@ -38,7 +38,7 @@ public: private: template - static constexpr inline TokenBaseType BaseClassToken = BIT(Index); + static constexpr inline TokenBaseType BaseClassToken = 1U << Index; template static constexpr inline TokenBaseType FinalClassToken = [] { From d2c4dbde9e5b09ae85d6901e6c7b8424d2b36893 Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 4 May 2021 21:28:16 -0700 Subject: [PATCH 80/82] fixup! hle: kernel: Migrate KSharedMemory to KAutoObject. --- src/core/hle/service/ns/pl_u.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp index 82d847130..e14acce58 100644 --- a/src/core/hle/service/ns/pl_u.cpp +++ b/src/core/hle/service/ns/pl_u.cpp @@ -258,12 +258,12 @@ void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) { // Create shared font memory object auto& kernel = system.Kernel(); - std::memcpy(system.Kernel().GetFontSharedMem().GetPointer(), impl->shared_font->data(), + std::memcpy(kernel.GetFontSharedMem().GetPointer(), impl->shared_font->data(), impl->shared_font->size()); IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(&system.Kernel().GetFontSharedMem()); + rb.PushCopyObjects(&kernel.GetFontSharedMem()); } void PL_U::GetSharedFontInOrderOfPriority(Kernel::HLERequestContext& ctx) { From b805ee653f3d178ed2b4b2e0a403bb0ab61dad8b Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 4 May 2021 21:35:42 -0700 Subject: [PATCH 81/82] hle: kernel: Move slab resource counts to Kernel. --- src/core/hle/kernel/init/init_slab_setup.cpp | 54 ++++++++++---------- src/core/hle/kernel/init/init_slab_setup.h | 9 ++-- src/core/hle/kernel/kernel.cpp | 12 ++++- src/core/hle/kernel/kernel.h | 10 ++++ 4 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index 2dd792e71..69ae405e6 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -25,7 +25,7 @@ namespace Kernel::Init { -#define SLAB_COUNT(CLASS) g_slab_resource_counts.num_##CLASS +#define SLAB_COUNT(CLASS) kernel.SlabResourceCounts().num_##CLASS #define FOREACH_SLAB_TYPE(HANDLER, ...) \ HANDLER(KProcess, (SLAB_COUNT(KProcess)), ##__VA_ARGS__) \ @@ -67,26 +67,6 @@ constexpr size_t SlabCountKBeta = 6; constexpr size_t SlabCountExtraKThread = 160; -// Global to hold our resource counts. -KSlabResourceCounts g_slab_resource_counts = { - .num_KProcess = SlabCountKProcess, - .num_KThread = SlabCountKThread, - .num_KEvent = SlabCountKEvent, - .num_KInterruptEvent = SlabCountKInterruptEvent, - .num_KPort = SlabCountKPort, - .num_KSharedMemory = SlabCountKSharedMemory, - .num_KTransferMemory = SlabCountKTransferMemory, - .num_KCodeMemory = SlabCountKCodeMemory, - .num_KDeviceAddressSpace = SlabCountKDeviceAddressSpace, - .num_KSession = SlabCountKSession, - .num_KLightSession = SlabCountKLightSession, - .num_KObjectName = SlabCountKObjectName, - .num_KResourceLimit = SlabCountKResourceLimit, - .num_KDebug = SlabCountKDebug, - .num_KAlpha = SlabCountKAlpha, - .num_KBeta = SlabCountKBeta, -}; - template VAddr InitializeSlabHeap(Core::System& system, KMemoryLayout& memory_layout, VAddr address, size_t num_objects) { @@ -105,19 +85,35 @@ VAddr InitializeSlabHeap(Core::System& system, KMemoryLayout& memory_layout, VAd } // namespace -const KSlabResourceCounts& GetSlabResourceCounts() { - return g_slab_resource_counts; +KSlabResourceCounts KSlabResourceCounts::CreateDefault() { + return { + .num_KProcess = SlabCountKProcess, + .num_KThread = SlabCountKThread, + .num_KEvent = SlabCountKEvent, + .num_KInterruptEvent = SlabCountKInterruptEvent, + .num_KPort = SlabCountKPort, + .num_KSharedMemory = SlabCountKSharedMemory, + .num_KTransferMemory = SlabCountKTransferMemory, + .num_KCodeMemory = SlabCountKCodeMemory, + .num_KDeviceAddressSpace = SlabCountKDeviceAddressSpace, + .num_KSession = SlabCountKSession, + .num_KLightSession = SlabCountKLightSession, + .num_KObjectName = SlabCountKObjectName, + .num_KResourceLimit = SlabCountKResourceLimit, + .num_KDebug = SlabCountKDebug, + .num_KAlpha = SlabCountKAlpha, + .num_KBeta = SlabCountKBeta, + }; } -void InitializeSlabResourceCounts() { - // Note: Nintendo initializes all fields here, but we initialize all constants at compile-time. - +void InitializeSlabResourceCounts(KernelCore& kernel) { + kernel.SlabResourceCounts() = KSlabResourceCounts::CreateDefault(); if (KSystemControl::Init::ShouldIncreaseThreadResourceLimit()) { - g_slab_resource_counts.num_KThread += SlabCountExtraKThread; + kernel.SlabResourceCounts().num_KThread += SlabCountExtraKThread; } } -size_t CalculateTotalSlabHeapSize() { +size_t CalculateTotalSlabHeapSize(const KernelCore& kernel) { size_t size = 0; #define ADD_SLAB_SIZE(NAME, COUNT, ...) \ @@ -138,6 +134,8 @@ size_t CalculateTotalSlabHeapSize() { } void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) { + auto& kernel = system.Kernel(); + // Get the start of the slab region, since that's where we'll be working. VAddr address = memory_layout.GetSlabRegionAddress(); diff --git a/src/core/hle/kernel/init/init_slab_setup.h b/src/core/hle/kernel/init/init_slab_setup.h index 6418b97ac..a8f7e0918 100644 --- a/src/core/hle/kernel/init/init_slab_setup.h +++ b/src/core/hle/kernel/init/init_slab_setup.h @@ -9,12 +9,15 @@ class System; } // namespace Core namespace Kernel { +class KernelCore; class KMemoryLayout; } // namespace Kernel namespace Kernel::Init { struct KSlabResourceCounts { + static KSlabResourceCounts CreateDefault(); + size_t num_KProcess; size_t num_KThread; size_t num_KEvent; @@ -33,10 +36,8 @@ struct KSlabResourceCounts { size_t num_KBeta; }; -void InitializeSlabResourceCounts(); -const KSlabResourceCounts& GetSlabResourceCounts(); - -size_t CalculateTotalSlabHeapSize(); +void InitializeSlabResourceCounts(KernelCore& kernel); +size_t CalculateTotalSlabHeapSize(const KernelCore& kernel); void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout); } // namespace Kernel::Init diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 5ebd47e49..32bbf2d9b 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -69,6 +69,7 @@ struct KernelCore::Impl { InitializePhysicalCores(); // Derive the initial memory layout from the emulated board + Init::InitializeSlabResourceCounts(kernel); KMemoryLayout memory_layout; DeriveInitialMemoryLayout(memory_layout); Init::InitializeSlabHeaps(system, memory_layout); @@ -395,7 +396,7 @@ struct KernelCore::Impl { // Determine the size of the slab region. const size_t slab_region_size = - Common::AlignUp(Init::CalculateTotalSlabHeapSize(), PageSize); + Common::AlignUp(Init::CalculateTotalSlabHeapSize(system.Kernel()), PageSize); ASSERT(slab_region_size <= resource_region_size); // Setup the slab region. @@ -642,6 +643,7 @@ struct KernelCore::Impl { std::unique_ptr global_scheduler_context; Kernel::TimeManager time_manager; + Init::KSlabResourceCounts slab_resource_counts{}; KResourceLimit* system_resource_limit{}; std::shared_ptr preemption_event; @@ -995,6 +997,14 @@ void KernelCore::ReleaseServiceThread(std::weak_ptr servi }); } +Init::KSlabResourceCounts& KernelCore::SlabResourceCounts() { + return impl->slab_resource_counts; +} + +const Init::KSlabResourceCounts& KernelCore::SlabResourceCounts() const { + return impl->slab_resource_counts; +} + bool KernelCore::IsPhantomModeForSingleCore() const { return impl->IsPhantomModeForSingleCore(); } diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 7c46aa997..51aaccbc7 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -51,6 +51,10 @@ class ServiceThread; class Synchronization; class TimeManager; +namespace Init { +struct KSlabResourceCounts; +} + template class KSlabHeap; @@ -292,6 +296,12 @@ public: } } + /// Gets the current slab resource counts. + Init::KSlabResourceCounts& SlabResourceCounts(); + + /// Gets the current slab resource counts. + const Init::KSlabResourceCounts& SlabResourceCounts() const; + private: friend class KProcess; friend class KThread; From d57b12193b12f7b6e3565d29f7bc3d7584632768 Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 4 May 2021 22:14:06 -0700 Subject: [PATCH 82/82] hle: kernel: KPageTable: CanContain should not be constexpr. --- src/core/hle/kernel/k_page_table.cpp | 2 +- src/core/hle/kernel/k_page_table.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/hle/kernel/k_page_table.cpp b/src/core/hle/kernel/k_page_table.cpp index 6c5d7d602..d4ce98ee3 100644 --- a/src/core/hle/kernel/k_page_table.cpp +++ b/src/core/hle/kernel/k_page_table.cpp @@ -1106,7 +1106,7 @@ constexpr std::size_t KPageTable::GetRegionSize(KMemoryState state) const { } } -constexpr bool KPageTable::CanContain(VAddr addr, std::size_t size, KMemoryState state) const { +bool KPageTable::CanContain(VAddr addr, std::size_t size, KMemoryState state) const { const VAddr end{addr + size}; const VAddr last{end - 1}; const VAddr region_start{GetRegionAddress(state)}; diff --git a/src/core/hle/kernel/k_page_table.h b/src/core/hle/kernel/k_page_table.h index baec7c260..8c2cc03eb 100644 --- a/src/core/hle/kernel/k_page_table.h +++ b/src/core/hle/kernel/k_page_table.h @@ -64,7 +64,7 @@ public: return page_table_impl; } - constexpr bool CanContain(VAddr addr, std::size_t size, KMemoryState state) const; + bool CanContain(VAddr addr, std::size_t size, KMemoryState state) const; private: enum class OperationType : u32 {