2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

hle: kernel: k_memory_region: Refactor to simplify code.

This commit is contained in:
bunnei 2021-03-20 00:36:19 -07:00
parent edbc505e52
commit 80688362cf
2 changed files with 89 additions and 83 deletions

View file

@ -24,7 +24,7 @@ public:
: address(address_), last_address(last_address_), pair_address(pair_address_), : address(address_), last_address(last_address_), pair_address(pair_address_),
attributes(attributes_), type_id(type_id_) {} attributes(attributes_), type_id(type_id_) {}
constexpr KMemoryRegion(u64 address_, u64 last_address_, u32 attributes_, u32 type_id_) constexpr KMemoryRegion(u64 address_, u64 last_address_, u32 attributes_, u32 type_id_)
: KMemoryRegion(address_, last_address_, std::numeric_limits<uintptr_t>::max(), attributes_, : KMemoryRegion(address_, last_address_, std::numeric_limits<u64>::max(), attributes_,
type_id_) {} type_id_) {}
static constexpr int Compare(const KMemoryRegion& lhs, const KMemoryRegion& rhs) { 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 { constexpr u64 GetAddress() const {
return address; return address;
} }
@ -50,11 +60,11 @@ public:
} }
constexpr u64 GetEndAddress() const { constexpr u64 GetEndAddress() const {
return GetLastAddress() + 1; return this->GetLastAddress() + 1;
} }
constexpr std::size_t GetSize() const { constexpr size_t GetSize() const {
return GetEndAddress() - GetAddress(); return this->GetEndAddress() - this->GetAddress();
} }
constexpr u32 GetAttributes() const { constexpr u32 GetAttributes() const {
@ -70,7 +80,7 @@ public:
type_id = type; type_id = type;
} }
constexpr bool Contains(uintptr_t address) const { constexpr bool Contains(u64 address) const {
ASSERT(this->GetEndAddress() != 0); ASSERT(this->GetEndAddress() != 0);
return this->GetAddress() <= address && address <= this->GetLastAddress(); return this->GetAddress() <= address && address <= this->GetLastAddress();
} }
@ -80,7 +90,7 @@ public:
} }
constexpr bool HasTypeAttribute(KMemoryRegionAttr attr) const { constexpr bool HasTypeAttribute(KMemoryRegionAttr attr) const {
return (this->GetType() | static_cast<u32>(attr)) == this->GetType(); return (this->GetType() | attr) == this->GetType();
} }
constexpr bool CanDerive(u32 type) const { constexpr bool CanDerive(u32 type) const {
@ -92,12 +102,12 @@ public:
} }
constexpr void SetTypeAttribute(KMemoryRegionAttr attr) { constexpr void SetTypeAttribute(KMemoryRegionAttr attr) {
type_id |= static_cast<u32>(attr); type_id |= attr;
} }
private: private:
const u64 address{}; u64 address{};
const u64 last_address{}; u64 last_address{};
u64 pair_address{}; u64 pair_address{};
u32 attributes{}; u32 attributes{};
u32 type_id{}; 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) { for (auto it = this->cbegin(); it != this->cend(); ++it) {
if (it->GetType() == type_id) { if (it->GetType() == static_cast<u32>(type_id)) {
return std::addressof(*it); return std::addressof(*it);
} }
} }
@ -184,7 +194,7 @@ public:
return nullptr; 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++) { for (auto it = this->cbegin(); it != this->cend(); it++) {
if (it->IsDerivedFrom(type_id)) { if (it->IsDerivedFrom(type_id)) {
return std::addressof(*it); return std::addressof(*it);
@ -193,7 +203,7 @@ public:
return nullptr; return nullptr;
} }
const KMemoryRegion* FindLastDerived(u32 type_id) const { const KMemoryRegion* FindLastDerived(KMemoryRegionType type_id) const {
const KMemoryRegion* region = nullptr; const KMemoryRegion* region = nullptr;
for (auto it = this->begin(); it != this->end(); it++) { for (auto it = this->begin(); it != this->end(); it++) {
if (it->IsDerivedFrom(type_id)) { if (it->IsDerivedFrom(type_id)) {
@ -203,7 +213,7 @@ public:
return region; return region;
} }
DerivedRegionExtents GetDerivedRegionExtents(u32 type_id) const { DerivedRegionExtents GetDerivedRegionExtents(KMemoryRegionType type_id) const {
DerivedRegionExtents extents; DerivedRegionExtents extents;
ASSERT(extents.first_region == nullptr); ASSERT(extents.first_region == nullptr);
@ -224,6 +234,10 @@ public:
return extents; return extents;
} }
DerivedRegionExtents GetDerivedRegionExtents(KMemoryRegionAttr type_id) const {
return GetDerivedRegionExtents(static_cast<KMemoryRegionType>(type_id));
}
public: public:
void InsertDirectly(u64 address, u64 last_address, u32 attr = 0, u32 type_id = 0); 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); bool Insert(u64 address, size_t size, u32 type_id, u32 new_attr = 0, u32 old_attr = 0);

View file

@ -8,17 +8,20 @@
#include "common/common_funcs.h" #include "common/common_funcs.h"
#include "common/common_types.h" #include "common/common_types.h"
#define ARCH_ARM64
#define BOARD_NINTENDO_NX
namespace Kernel { namespace Kernel {
enum class KMemoryRegionType : u32 {}; enum KMemoryRegionType : u32 {};
enum class KMemoryRegionAttr : typename std::underlying_type<KMemoryRegionType>::type { enum KMemoryRegionAttr : typename std::underlying_type<KMemoryRegionType>::type {
CarveoutProtected = 0x04000000, KMemoryRegionAttr_CarveoutProtected = 0x04000000,
DidKernelMap = 0x08000000, KMemoryRegionAttr_DidKernelMap = 0x08000000,
ShouldKernelMap = 0x10000000, KMemoryRegionAttr_ShouldKernelMap = 0x10000000,
UserReadOnly = 0x20000000, KMemoryRegionAttr_UserReadOnly = 0x20000000,
NoUserMap = 0x40000000, KMemoryRegionAttr_NoUserMap = 0x40000000,
LinearMapped = 0x80000000, KMemoryRegionAttr_LinearMapped = 0x80000000,
}; };
DECLARE_ENUM_FLAG_OPERATORS(KMemoryRegionAttr); DECLARE_ENUM_FLAG_OPERATORS(KMemoryRegionAttr);
@ -150,17 +153,15 @@ static_assert(KMemoryRegionType_Dram.GetValue() == 0x2);
constexpr auto KMemoryRegionType_DramKernelBase = constexpr auto KMemoryRegionType_DramKernelBase =
KMemoryRegionType_Dram.DeriveSparse(0, 3, 0) KMemoryRegionType_Dram.DeriveSparse(0, 3, 0)
.SetAttribute(KMemoryRegionAttr::NoUserMap) .SetAttribute(KMemoryRegionAttr_NoUserMap)
.SetAttribute(KMemoryRegionAttr::CarveoutProtected); .SetAttribute(KMemoryRegionAttr_CarveoutProtected);
constexpr auto KMemoryRegionType_DramReservedBase = KMemoryRegionType_Dram.DeriveSparse(0, 3, 1); constexpr auto KMemoryRegionType_DramReservedBase = KMemoryRegionType_Dram.DeriveSparse(0, 3, 1);
constexpr auto KMemoryRegionType_DramHeapBase = constexpr auto KMemoryRegionType_DramHeapBase =
KMemoryRegionType_Dram.DeriveSparse(0, 3, 2).SetAttribute(KMemoryRegionAttr::LinearMapped); KMemoryRegionType_Dram.DeriveSparse(0, 3, 2).SetAttribute(KMemoryRegionAttr_LinearMapped);
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramKernelBase.GetValue()) == static_assert(KMemoryRegionType_DramKernelBase.GetValue() ==
(static_cast<KMemoryRegionAttr>(0xE) | KMemoryRegionAttr::CarveoutProtected | (0xE | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap));
KMemoryRegionAttr::NoUserMap));
static_assert(KMemoryRegionType_DramReservedBase.GetValue() == (0x16)); static_assert(KMemoryRegionType_DramReservedBase.GetValue() == (0x16));
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramHeapBase.GetValue()) == static_assert(KMemoryRegionType_DramHeapBase.GetValue() == (0x26 | KMemoryRegionAttr_LinearMapped));
(static_cast<KMemoryRegionAttr>(0x26) | KMemoryRegionAttr::LinearMapped));
constexpr auto KMemoryRegionType_DramKernelCode = constexpr auto KMemoryRegionType_DramKernelCode =
KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 0); KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 0);
@ -168,78 +169,69 @@ constexpr auto KMemoryRegionType_DramKernelSlab =
KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 1); KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 1);
constexpr auto KMemoryRegionType_DramKernelPtHeap = constexpr auto KMemoryRegionType_DramKernelPtHeap =
KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 2).SetAttribute( KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 2).SetAttribute(
KMemoryRegionAttr::LinearMapped); KMemoryRegionAttr_LinearMapped);
constexpr auto KMemoryRegionType_DramKernelInitPt = constexpr auto KMemoryRegionType_DramKernelInitPt =
KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 3).SetAttribute( KMemoryRegionType_DramKernelBase.DeriveSparse(0, 4, 3).SetAttribute(
KMemoryRegionAttr::LinearMapped); KMemoryRegionAttr_LinearMapped);
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramKernelCode.GetValue()) == static_assert(KMemoryRegionType_DramKernelCode.GetValue() ==
(static_cast<KMemoryRegionAttr>(0xCE) | KMemoryRegionAttr::CarveoutProtected | (0xCE | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap));
KMemoryRegionAttr::NoUserMap)); static_assert(KMemoryRegionType_DramKernelSlab.GetValue() ==
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramKernelSlab.GetValue()) == (0x14E | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap));
(static_cast<KMemoryRegionAttr>(0x14E) | KMemoryRegionAttr::CarveoutProtected | static_assert(KMemoryRegionType_DramKernelPtHeap.GetValue() ==
KMemoryRegionAttr::NoUserMap)); (0x24E | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap |
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramKernelPtHeap.GetValue()) == KMemoryRegionAttr_LinearMapped));
(static_cast<KMemoryRegionAttr>(0x24E) | KMemoryRegionAttr::CarveoutProtected | static_assert(KMemoryRegionType_DramKernelInitPt.GetValue() ==
KMemoryRegionAttr::NoUserMap | KMemoryRegionAttr::LinearMapped)); (0x44E | KMemoryRegionAttr_CarveoutProtected | KMemoryRegionAttr_NoUserMap |
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramKernelInitPt.GetValue()) == KMemoryRegionAttr_LinearMapped));
(static_cast<KMemoryRegionAttr>(0x44E) | KMemoryRegionAttr::CarveoutProtected |
KMemoryRegionAttr::NoUserMap | KMemoryRegionAttr::LinearMapped));
constexpr auto KMemoryRegionType_DramReservedEarly = constexpr auto KMemoryRegionType_DramReservedEarly =
KMemoryRegionType_DramReservedBase.DeriveAttribute(KMemoryRegionAttr::NoUserMap); KMemoryRegionType_DramReservedBase.DeriveAttribute(KMemoryRegionAttr_NoUserMap);
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramReservedEarly.GetValue()) == static_assert(KMemoryRegionType_DramReservedEarly.GetValue() ==
(static_cast<KMemoryRegionAttr>(0x16) | KMemoryRegionAttr::NoUserMap)); (0x16 | KMemoryRegionAttr_NoUserMap));
constexpr auto KMemoryRegionType_KernelTraceBuffer = constexpr auto KMemoryRegionType_KernelTraceBuffer =
KMemoryRegionType_DramReservedBase.DeriveSparse(0, 3, 0) KMemoryRegionType_DramReservedBase.DeriveSparse(0, 3, 0)
.SetAttribute(KMemoryRegionAttr::LinearMapped) .SetAttribute(KMemoryRegionAttr_LinearMapped)
.SetAttribute(KMemoryRegionAttr::UserReadOnly); .SetAttribute(KMemoryRegionAttr_UserReadOnly);
constexpr auto KMemoryRegionType_OnMemoryBootImage = constexpr auto KMemoryRegionType_OnMemoryBootImage =
KMemoryRegionType_DramReservedBase.DeriveSparse(0, 3, 1); KMemoryRegionType_DramReservedBase.DeriveSparse(0, 3, 1);
constexpr auto KMemoryRegionType_DTB = KMemoryRegionType_DramReservedBase.DeriveSparse(0, 3, 2); constexpr auto KMemoryRegionType_DTB = KMemoryRegionType_DramReservedBase.DeriveSparse(0, 3, 2);
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_KernelTraceBuffer.GetValue()) == static_assert(KMemoryRegionType_KernelTraceBuffer.GetValue() ==
(static_cast<KMemoryRegionAttr>(0xD6) | KMemoryRegionAttr::LinearMapped | (0xD6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_UserReadOnly));
KMemoryRegionAttr::UserReadOnly));
static_assert(KMemoryRegionType_OnMemoryBootImage.GetValue() == 0x156); static_assert(KMemoryRegionType_OnMemoryBootImage.GetValue() == 0x156);
static_assert(KMemoryRegionType_DTB.GetValue() == 0x256); static_assert(KMemoryRegionType_DTB.GetValue() == 0x256);
constexpr auto KMemoryRegionType_DramPoolPartition = constexpr auto KMemoryRegionType_DramPoolPartition =
KMemoryRegionType_DramHeapBase.DeriveAttribute(KMemoryRegionAttr::NoUserMap); KMemoryRegionType_DramHeapBase.DeriveAttribute(KMemoryRegionAttr_NoUserMap);
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramPoolPartition.GetValue()) == static_assert(KMemoryRegionType_DramPoolPartition.GetValue() ==
(static_cast<KMemoryRegionAttr>(0x26) | KMemoryRegionAttr::LinearMapped | (0x26 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap));
KMemoryRegionAttr::NoUserMap));
constexpr auto KMemoryRegionType_DramPoolManagement = constexpr auto KMemoryRegionType_DramPoolManagement =
KMemoryRegionType_DramPoolPartition.DeriveTransition(0, 2).DeriveTransition().SetAttribute( KMemoryRegionType_DramPoolPartition.DeriveTransition(0, 2).DeriveTransition().SetAttribute(
KMemoryRegionAttr::CarveoutProtected); KMemoryRegionAttr_CarveoutProtected);
constexpr auto KMemoryRegionType_DramUserPool = constexpr auto KMemoryRegionType_DramUserPool =
KMemoryRegionType_DramPoolPartition.DeriveTransition(1, 2).DeriveTransition(); KMemoryRegionType_DramPoolPartition.DeriveTransition(1, 2).DeriveTransition();
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramPoolManagement.GetValue()) == static_assert(KMemoryRegionType_DramPoolManagement.GetValue() ==
(static_cast<KMemoryRegionAttr>(0x166) | KMemoryRegionAttr::LinearMapped | (0x166 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap |
KMemoryRegionAttr::NoUserMap | KMemoryRegionAttr::CarveoutProtected)); KMemoryRegionAttr_CarveoutProtected));
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramUserPool.GetValue()) == static_assert(KMemoryRegionType_DramUserPool.GetValue() ==
(static_cast<KMemoryRegionAttr>(0x1A6) | KMemoryRegionAttr::LinearMapped | (0x1A6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap));
KMemoryRegionAttr::NoUserMap));
constexpr auto KMemoryRegionType_DramApplicationPool = KMemoryRegionType_DramUserPool.Derive(4, 0); constexpr auto KMemoryRegionType_DramApplicationPool = KMemoryRegionType_DramUserPool.Derive(4, 0);
constexpr auto KMemoryRegionType_DramAppletPool = KMemoryRegionType_DramUserPool.Derive(4, 1); constexpr auto KMemoryRegionType_DramAppletPool = KMemoryRegionType_DramUserPool.Derive(4, 1);
constexpr auto KMemoryRegionType_DramSystemNonSecurePool = constexpr auto KMemoryRegionType_DramSystemNonSecurePool =
KMemoryRegionType_DramUserPool.Derive(4, 2); KMemoryRegionType_DramUserPool.Derive(4, 2);
constexpr auto KMemoryRegionType_DramSystemPool = constexpr auto KMemoryRegionType_DramSystemPool =
KMemoryRegionType_DramUserPool.Derive(4, 3).SetAttribute(KMemoryRegionAttr::CarveoutProtected); KMemoryRegionType_DramUserPool.Derive(4, 3).SetAttribute(KMemoryRegionAttr_CarveoutProtected);
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramApplicationPool.GetValue()) == static_assert(KMemoryRegionType_DramApplicationPool.GetValue() ==
(static_cast<KMemoryRegionAttr>(0x7A6) | KMemoryRegionAttr::LinearMapped | (0x7A6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap));
KMemoryRegionAttr::NoUserMap)); static_assert(KMemoryRegionType_DramAppletPool.GetValue() ==
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramAppletPool.GetValue()) == (0xBA6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap));
(static_cast<KMemoryRegionAttr>(0xBA6) | KMemoryRegionAttr::LinearMapped | static_assert(KMemoryRegionType_DramSystemNonSecurePool.GetValue() ==
KMemoryRegionAttr::NoUserMap)); (0xDA6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap));
static_assert( static_assert(KMemoryRegionType_DramSystemPool.GetValue() ==
static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramSystemNonSecurePool.GetValue()) == (0x13A6 | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_NoUserMap |
(static_cast<KMemoryRegionAttr>(0xDA6) | KMemoryRegionAttr::LinearMapped | KMemoryRegionAttr_CarveoutProtected));
KMemoryRegionAttr::NoUserMap));
static_assert(static_cast<KMemoryRegionAttr>(KMemoryRegionType_DramSystemPool.GetValue()) ==
(static_cast<KMemoryRegionAttr>(0x13A6) | KMemoryRegionAttr::LinearMapped |
KMemoryRegionAttr::NoUserMap | KMemoryRegionAttr::CarveoutProtected));
constexpr auto KMemoryRegionType_VirtualDramHeapBase = KMemoryRegionType_Dram.DeriveSparse(1, 3, 0); constexpr auto KMemoryRegionType_VirtualDramHeapBase = KMemoryRegionType_Dram.DeriveSparse(1, 3, 0);
constexpr auto KMemoryRegionType_VirtualDramKernelPtHeap = constexpr auto KMemoryRegionType_VirtualDramKernelPtHeap =
@ -284,18 +276,18 @@ constexpr auto KMemoryRegionType_BoardDeviceBase =
static_assert(KMemoryRegionType_ArchDeviceBase.GetValue() == 0x5); static_assert(KMemoryRegionType_ArchDeviceBase.GetValue() == 0x5);
static_assert(KMemoryRegionType_BoardDeviceBase.GetValue() == 0x5); static_assert(KMemoryRegionType_BoardDeviceBase.GetValue() == 0x5);
#if defined(ATMOSPHERE_ARCH_ARM64) #if defined(ARCH_ARM64)
#include <mesosphere/arch/arm64/kern_k_memory_region_device_types.inc> #include "core/hle/kernel/arch/arm64/k_memory_region_device_types.inc"
#elif defined(ATMOSPHERE_ARCH_ARM) #elif defined(ARCH_ARM)
#include <mesosphere/arch/arm/kern_k_memory_region_device_types.inc> #error "Unimplemented""
#else #else
// Default to no architecture devices. // Default to no architecture devices.
constexpr auto NumArchitectureDeviceRegions = 0; constexpr auto NumArchitectureDeviceRegions = 0;
#endif #endif
static_assert(NumArchitectureDeviceRegions >= 0); static_assert(NumArchitectureDeviceRegions >= 0);
#if defined(ATMOSPHERE_BOARD_NINTENDO_NX) #if defined(BOARD_NINTENDO_NX)
#include <mesosphere/board/nintendo/nx/kern_k_memory_region_device_types.inc> #include "core/hle/kernel/board/nintendo/nx/k_memory_region_device_types.inc"
#else #else
// Default to no board devices. // Default to no board devices.
constexpr auto NumBoardDeviceRegions = 0; constexpr auto NumBoardDeviceRegions = 0;