From 80688362cf90055cfdee8bcd5a5c0cfd2c57b67f Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 20 Mar 2021 00:36:19 -0700 Subject: [PATCH] hle: kernel: k_memory_region: Refactor to simplify code. --- src/core/hle/kernel/k_memory_region.h | 42 ++++--- src/core/hle/kernel/k_memory_region_type.h | 130 ++++++++++----------- 2 files changed, 89 insertions(+), 83 deletions(-) diff --git a/src/core/hle/kernel/k_memory_region.h b/src/core/hle/kernel/k_memory_region.h index afa89011c9..1d4fcde6fb 100644 --- a/src/core/hle/kernel/k_memory_region.h +++ b/src/core/hle/kernel/k_memory_region.h @@ -24,7 +24,7 @@ public: : address(address_), last_address(last_address_), pair_address(pair_address_), attributes(attributes_), type_id(type_id_) {} constexpr KMemoryRegion(u64 address_, u64 last_address_, u32 attributes_, u32 type_id_) - : KMemoryRegion(address_, last_address_, std::numeric_limits::max(), attributes_, + : KMemoryRegion(address_, last_address_, std::numeric_limits::max(), attributes_, type_id_) {} static constexpr int Compare(const KMemoryRegion& lhs, const KMemoryRegion& rhs) { @@ -37,6 +37,16 @@ public: } } +private: + constexpr void Reset(u64 a, u64 la, u64 p, u32 r, u32 t) { + address = a; + pair_address = p; + last_address = la; + attributes = r; + type_id = t; + } + +public: constexpr u64 GetAddress() const { return address; } @@ -50,11 +60,11 @@ public: } constexpr u64 GetEndAddress() const { - return GetLastAddress() + 1; + return this->GetLastAddress() + 1; } - constexpr std::size_t GetSize() const { - return GetEndAddress() - GetAddress(); + constexpr size_t GetSize() const { + return this->GetEndAddress() - this->GetAddress(); } constexpr u32 GetAttributes() const { @@ -70,7 +80,7 @@ public: type_id = type; } - constexpr bool Contains(uintptr_t address) const { + constexpr bool Contains(u64 address) const { ASSERT(this->GetEndAddress() != 0); return this->GetAddress() <= address && address <= this->GetLastAddress(); } @@ -80,7 +90,7 @@ public: } constexpr bool HasTypeAttribute(KMemoryRegionAttr attr) const { - return (this->GetType() | static_cast(attr)) == this->GetType(); + return (this->GetType() | attr) == this->GetType(); } constexpr bool CanDerive(u32 type) const { @@ -92,12 +102,12 @@ public: } constexpr void SetTypeAttribute(KMemoryRegionAttr attr) { - type_id |= static_cast(attr); + type_id |= attr; } private: - const u64 address{}; - const u64 last_address{}; + u64 address{}; + u64 last_address{}; u64 pair_address{}; u32 attributes{}; u32 type_id{}; @@ -166,9 +176,9 @@ public: } } - const KMemoryRegion* FindByType(u32 type_id) const { + const KMemoryRegion* FindByType(KMemoryRegionType type_id) const { for (auto it = this->cbegin(); it != this->cend(); ++it) { - if (it->GetType() == type_id) { + if (it->GetType() == static_cast(type_id)) { return std::addressof(*it); } } @@ -184,7 +194,7 @@ public: return nullptr; } - const KMemoryRegion* FindFirstDerived(u32 type_id) const { + const KMemoryRegion* FindFirstDerived(KMemoryRegionType type_id) const { for (auto it = this->cbegin(); it != this->cend(); it++) { if (it->IsDerivedFrom(type_id)) { return std::addressof(*it); @@ -193,7 +203,7 @@ public: return nullptr; } - const KMemoryRegion* FindLastDerived(u32 type_id) const { + const KMemoryRegion* FindLastDerived(KMemoryRegionType type_id) const { const KMemoryRegion* region = nullptr; for (auto it = this->begin(); it != this->end(); it++) { if (it->IsDerivedFrom(type_id)) { @@ -203,7 +213,7 @@ public: return region; } - DerivedRegionExtents GetDerivedRegionExtents(u32 type_id) const { + DerivedRegionExtents GetDerivedRegionExtents(KMemoryRegionType type_id) const { DerivedRegionExtents extents; ASSERT(extents.first_region == nullptr); @@ -224,6 +234,10 @@ public: return extents; } + DerivedRegionExtents GetDerivedRegionExtents(KMemoryRegionAttr type_id) const { + return GetDerivedRegionExtents(static_cast(type_id)); + } + public: void InsertDirectly(u64 address, u64 last_address, u32 attr = 0, u32 type_id = 0); bool Insert(u64 address, size_t size, u32 type_id, u32 new_attr = 0, u32 old_attr = 0); diff --git a/src/core/hle/kernel/k_memory_region_type.h b/src/core/hle/kernel/k_memory_region_type.h index 243e2fd3d5..d83e80793d 100644 --- a/src/core/hle/kernel/k_memory_region_type.h +++ b/src/core/hle/kernel/k_memory_region_type.h @@ -8,17 +8,20 @@ #include "common/common_funcs.h" #include "common/common_types.h" +#define ARCH_ARM64 +#define BOARD_NINTENDO_NX + namespace Kernel { -enum class KMemoryRegionType : u32 {}; +enum KMemoryRegionType : u32 {}; -enum class KMemoryRegionAttr : typename std::underlying_type::type { - CarveoutProtected = 0x04000000, - DidKernelMap = 0x08000000, - ShouldKernelMap = 0x10000000, - UserReadOnly = 0x20000000, - NoUserMap = 0x40000000, - LinearMapped = 0x80000000, +enum KMemoryRegionAttr : typename std::underlying_type::type { + KMemoryRegionAttr_CarveoutProtected = 0x04000000, + KMemoryRegionAttr_DidKernelMap = 0x08000000, + KMemoryRegionAttr_ShouldKernelMap = 0x10000000, + KMemoryRegionAttr_UserReadOnly = 0x20000000, + KMemoryRegionAttr_NoUserMap = 0x40000000, + KMemoryRegionAttr_LinearMapped = 0x80000000, }; DECLARE_ENUM_FLAG_OPERATORS(KMemoryRegionAttr); @@ -150,17 +153,15 @@ static_assert(KMemoryRegionType_Dram.GetValue() == 0x2); constexpr auto KMemoryRegionType_DramKernelBase = KMemoryRegionType_Dram.DeriveSparse(0, 3, 0) - .SetAttribute(KMemoryRegionAttr::NoUserMap) - .SetAttribute(KMemoryRegionAttr::CarveoutProtected); + .SetAttribute(KMemoryRegionAttr_NoUserMap) + .SetAttribute(KMemoryRegionAttr_CarveoutProtected); constexpr auto KMemoryRegionType_DramReservedBase = KMemoryRegionType_Dram.DeriveSparse(0, 3, 1); constexpr auto KMemoryRegionType_DramHeapBase = - KMemoryRegionType_Dram.DeriveSparse(0, 3, 2).SetAttribute(KMemoryRegionAttr::LinearMapped); -static_assert(static_cast(KMemoryRegionType_DramKernelBase.GetValue()) == - (static_cast(0xE) | KMemoryRegionAttr::CarveoutProtected | - KMemoryRegionAttr::NoUserMap)); + KMemoryRegionType_Dram.DeriveSparse(0, 3, 2).SetAttribute(KMemoryRegionAttr_LinearMapped); +static_assert(KMemoryRegionType_DramKernelBase.GetValue() == + (0xE | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap)); static_assert(KMemoryRegionType_DramReservedBase.GetValue() == (0x16)); -static_assert(static_cast(KMemoryRegionType_DramHeapBase.GetValue()) == - (static_cast(0x26) | KMemoryRegionAttr::LinearMapped)); +static_assert(KMemoryRegionType_DramHeapBase.GetValue() == (0x26 | KMemoryRegionAttr_LinearMapped)); constexpr auto KMemoryRegionType_DramKernelCode = KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 0); @@ -168,78 +169,69 @@ constexpr auto KMemoryRegionType_DramKernelSlab = KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 1); constexpr auto KMemoryRegionType_DramKernelPtHeap = KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 2).SetAttribute( - KMemoryRegionAttr::LinearMapped); + KMemoryRegionAttr_LinearMapped); constexpr auto KMemoryRegionType_DramKernelInitPt = KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 3).SetAttribute( - KMemoryRegionAttr::LinearMapped); -static_assert(static_cast(KMemoryRegionType_DramKernelCode.GetValue()) == - (static_cast(0xCE) | KMemoryRegionAttr::CarveoutProtected | - KMemoryRegionAttr::NoUserMap)); -static_assert(static_cast(KMemoryRegionType_DramKernelSlab.GetValue()) == - (static_cast(0x14E) | KMemoryRegionAttr::CarveoutProtected | - KMemoryRegionAttr::NoUserMap)); -static_assert(static_cast(KMemoryRegionType_DramKernelPtHeap.GetValue()) == - (static_cast(0x24E) | KMemoryRegionAttr::CarveoutProtected | - KMemoryRegionAttr::NoUserMap | KMemoryRegionAttr::LinearMapped)); -static_assert(static_cast(KMemoryRegionType_DramKernelInitPt.GetValue()) == - (static_cast(0x44E) | KMemoryRegionAttr::CarveoutProtected | - KMemoryRegionAttr::NoUserMap | KMemoryRegionAttr::LinearMapped)); + KMemoryRegionAttr_LinearMapped); +static_assert(KMemoryRegionType_DramKernelCode.GetValue() == + (0xCE | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap)); +static_assert(KMemoryRegionType_DramKernelSlab.GetValue() == + (0x14E | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap)); +static_assert(KMemoryRegionType_DramKernelPtHeap.GetValue() == + (0x24E | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap | + KMemoryRegionAttr_LinearMapped)); +static_assert(KMemoryRegionType_DramKernelInitPt.GetValue() == + (0x44E | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap | + KMemoryRegionAttr_LinearMapped)); constexpr auto KMemoryRegionType_DramReservedEarly = - KMemoryRegionType_DramReservedBase.DeriveAttribute(KMemoryRegionAttr::NoUserMap); -static_assert(static_cast(KMemoryRegionType_DramReservedEarly.GetValue()) == - (static_cast(0x16) | KMemoryRegionAttr::NoUserMap)); + KMemoryRegionType_DramReservedBase.DeriveAttribute(KMemoryRegionAttr_NoUserMap); +static_assert(KMemoryRegionType_DramReservedEarly.GetValue() == + (0x16 | KMemoryRegionAttr_NoUserMap)); constexpr auto KMemoryRegionType_KernelTraceBuffer = KMemoryRegionType_DramReservedBase.DeriveSparse(0, 3, 0) - .SetAttribute(KMemoryRegionAttr::LinearMapped) - .SetAttribute(KMemoryRegionAttr::UserReadOnly); + .SetAttribute(KMemoryRegionAttr_LinearMapped) + .SetAttribute(KMemoryRegionAttr_UserReadOnly); constexpr auto KMemoryRegionType_OnMemoryBootImage = KMemoryRegionType_DramReservedBase.DeriveSparse(0, 3, 1); constexpr auto KMemoryRegionType_DTB = KMemoryRegionType_DramReservedBase.DeriveSparse(0, 3, 2); -static_assert(static_cast(KMemoryRegionType_KernelTraceBuffer.GetValue()) == - (static_cast(0xD6) | KMemoryRegionAttr::LinearMapped | - KMemoryRegionAttr::UserReadOnly)); +static_assert(KMemoryRegionType_KernelTraceBuffer.GetValue() == + (0xD6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_UserReadOnly)); static_assert(KMemoryRegionType_OnMemoryBootImage.GetValue() == 0x156); static_assert(KMemoryRegionType_DTB.GetValue() == 0x256); constexpr auto KMemoryRegionType_DramPoolPartition = - KMemoryRegionType_DramHeapBase.DeriveAttribute(KMemoryRegionAttr::NoUserMap); -static_assert(static_cast(KMemoryRegionType_DramPoolPartition.GetValue()) == - (static_cast(0x26) | KMemoryRegionAttr::LinearMapped | - KMemoryRegionAttr::NoUserMap)); + KMemoryRegionType_DramHeapBase.DeriveAttribute(KMemoryRegionAttr_NoUserMap); +static_assert(KMemoryRegionType_DramPoolPartition.GetValue() == + (0x26 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap)); constexpr auto KMemoryRegionType_DramPoolManagement = KMemoryRegionType_DramPoolPartition.DeriveTransition(0, 2).DeriveTransition().SetAttribute( - KMemoryRegionAttr::CarveoutProtected); + KMemoryRegionAttr_CarveoutProtected); constexpr auto KMemoryRegionType_DramUserPool = KMemoryRegionType_DramPoolPartition.DeriveTransition(1, 2).DeriveTransition(); -static_assert(static_cast(KMemoryRegionType_DramPoolManagement.GetValue()) == - (static_cast(0x166) | KMemoryRegionAttr::LinearMapped | - KMemoryRegionAttr::NoUserMap | KMemoryRegionAttr::CarveoutProtected)); -static_assert(static_cast(KMemoryRegionType_DramUserPool.GetValue()) == - (static_cast(0x1A6) | KMemoryRegionAttr::LinearMapped | - KMemoryRegionAttr::NoUserMap)); +static_assert(KMemoryRegionType_DramPoolManagement.GetValue() == + (0x166 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap | + KMemoryRegionAttr_CarveoutProtected)); +static_assert(KMemoryRegionType_DramUserPool.GetValue() == + (0x1A6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap)); constexpr auto KMemoryRegionType_DramApplicationPool = KMemoryRegionType_DramUserPool.Derive(4, 0); constexpr auto KMemoryRegionType_DramAppletPool = KMemoryRegionType_DramUserPool.Derive(4, 1); constexpr auto KMemoryRegionType_DramSystemNonSecurePool = KMemoryRegionType_DramUserPool.Derive(4, 2); constexpr auto KMemoryRegionType_DramSystemPool = - KMemoryRegionType_DramUserPool.Derive(4, 3).SetAttribute(KMemoryRegionAttr::CarveoutProtected); -static_assert(static_cast(KMemoryRegionType_DramApplicationPool.GetValue()) == - (static_cast(0x7A6) | KMemoryRegionAttr::LinearMapped | - KMemoryRegionAttr::NoUserMap)); -static_assert(static_cast(KMemoryRegionType_DramAppletPool.GetValue()) == - (static_cast(0xBA6) | KMemoryRegionAttr::LinearMapped | - KMemoryRegionAttr::NoUserMap)); -static_assert( - static_cast(KMemoryRegionType_DramSystemNonSecurePool.GetValue()) == - (static_cast(0xDA6) | KMemoryRegionAttr::LinearMapped | - KMemoryRegionAttr::NoUserMap)); -static_assert(static_cast(KMemoryRegionType_DramSystemPool.GetValue()) == - (static_cast(0x13A6) | KMemoryRegionAttr::LinearMapped | - KMemoryRegionAttr::NoUserMap | KMemoryRegionAttr::CarveoutProtected)); + KMemoryRegionType_DramUserPool.Derive(4, 3).SetAttribute(KMemoryRegionAttr_CarveoutProtected); +static_assert(KMemoryRegionType_DramApplicationPool.GetValue() == + (0x7A6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap)); +static_assert(KMemoryRegionType_DramAppletPool.GetValue() == + (0xBA6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap)); +static_assert(KMemoryRegionType_DramSystemNonSecurePool.GetValue() == + (0xDA6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap)); +static_assert(KMemoryRegionType_DramSystemPool.GetValue() == + (0x13A6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap | + KMemoryRegionAttr_CarveoutProtected)); constexpr auto KMemoryRegionType_VirtualDramHeapBase = KMemoryRegionType_Dram.DeriveSparse(1, 3, 0); constexpr auto KMemoryRegionType_VirtualDramKernelPtHeap = @@ -284,18 +276,18 @@ constexpr auto KMemoryRegionType_BoardDeviceBase = static_assert(KMemoryRegionType_ArchDeviceBase.GetValue() == 0x5); static_assert(KMemoryRegionType_BoardDeviceBase.GetValue() == 0x5); -#if defined(ATMOSPHERE_ARCH_ARM64) -#include -#elif defined(ATMOSPHERE_ARCH_ARM) -#include +#if defined(ARCH_ARM64) +#include "core/hle/kernel/arch/arm64/k_memory_region_device_types.inc" +#elif defined(ARCH_ARM) +#error "Unimplemented"" #else // Default to no architecture devices. constexpr auto NumArchitectureDeviceRegions = 0; #endif static_assert(NumArchitectureDeviceRegions >= 0); -#if defined(ATMOSPHERE_BOARD_NINTENDO_NX) -#include +#if defined(BOARD_NINTENDO_NX) +#include "core/hle/kernel/board/nintendo/nx/k_memory_region_device_types.inc" #else // Default to no board devices. constexpr auto NumBoardDeviceRegions = 0;