From 7ed5dd0d62347054d5a03a24354cd43bec1184ba Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 12 Feb 2021 15:47:05 -0800 Subject: hle: kernel: Migrate AddressSpaceInfo to KAddressSpaceInfo. --- src/core/CMakeLists.txt | 4 +- src/core/hle/kernel/k_address_space_info.cpp | 117 +++++++++++++++++++++ src/core/hle/kernel/k_address_space_info.h | 31 ++++++ src/core/hle/kernel/memory/address_space_info.cpp | 119 ---------------------- src/core/hle/kernel/memory/address_space_info.h | 34 ------- src/core/hle/kernel/memory/page_table.cpp | 34 +++---- 6 files changed, 167 insertions(+), 172 deletions(-) create mode 100644 src/core/hle/kernel/k_address_space_info.cpp create mode 100644 src/core/hle/kernel/k_address_space_info.h delete mode 100644 src/core/hle/kernel/memory/address_space_info.cpp delete mode 100644 src/core/hle/kernel/memory/address_space_info.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 8aad9cb41..442618e90 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -156,6 +156,8 @@ add_library(core STATIC hle/kernel/hle_ipc.h hle/kernel/k_address_arbiter.cpp hle/kernel/k_address_arbiter.h + hle/kernel/k_address_space_info.cpp + hle/kernel/k_address_space_info.h hle/kernel/k_affinity_mask.h hle/kernel/k_condition_variable.cpp hle/kernel/k_condition_variable.h @@ -191,8 +193,6 @@ add_library(core STATIC hle/kernel/k_writable_event.h hle/kernel/kernel.cpp hle/kernel/kernel.h - hle/kernel/memory/address_space_info.cpp - hle/kernel/memory/address_space_info.h hle/kernel/memory/memory_block.h hle/kernel/memory/memory_block_manager.cpp hle/kernel/memory/memory_block_manager.h diff --git a/src/core/hle/kernel/k_address_space_info.cpp b/src/core/hle/kernel/k_address_space_info.cpp new file mode 100644 index 000000000..24944d15b --- /dev/null +++ b/src/core/hle/kernel/k_address_space_info.cpp @@ -0,0 +1,117 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include + +#include "common/assert.h" +#include "core/hle/kernel/k_address_space_info.h" + +namespace Kernel { + +namespace { + +enum : u64 { + Size_1_MB = 0x100000, + Size_2_MB = 2 * Size_1_MB, + Size_128_MB = 128 * Size_1_MB, + Size_1_GB = 0x40000000, + Size_2_GB = 2 * Size_1_GB, + Size_4_GB = 4 * Size_1_GB, + Size_6_GB = 6 * Size_1_GB, + Size_64_GB = 64 * Size_1_GB, + Size_512_GB = 512 * Size_1_GB, + Invalid = std::numeric_limits::max(), +}; + +// clang-format off +constexpr std::array AddressSpaceInfos{{ + { .bit_width = 32, .address = Size_2_MB , .size = Size_1_GB - Size_2_MB , .type = KAddressSpaceInfo::Type::MapSmall, }, + { .bit_width = 32, .address = Size_1_GB , .size = Size_4_GB - Size_1_GB , .type = KAddressSpaceInfo::Type::MapLarge, }, + { .bit_width = 32, .address = Invalid , .size = Size_1_GB , .type = KAddressSpaceInfo::Type::Heap, }, + { .bit_width = 32, .address = Invalid , .size = Size_1_GB , .type = KAddressSpaceInfo::Type::Alias, }, + { .bit_width = 36, .address = Size_128_MB, .size = Size_2_GB - Size_128_MB, .type = KAddressSpaceInfo::Type::MapSmall, }, + { .bit_width = 36, .address = Size_2_GB , .size = Size_64_GB - Size_2_GB , .type = KAddressSpaceInfo::Type::MapLarge, }, + { .bit_width = 36, .address = Invalid , .size = Size_6_GB , .type = KAddressSpaceInfo::Type::Heap, }, + { .bit_width = 36, .address = Invalid , .size = Size_6_GB , .type = KAddressSpaceInfo::Type::Alias, }, + { .bit_width = 39, .address = Size_128_MB, .size = Size_512_GB - Size_128_MB, .type = KAddressSpaceInfo::Type::Map39Bit, }, + { .bit_width = 39, .address = Invalid , .size = Size_64_GB , .type = KAddressSpaceInfo::Type::MapSmall }, + { .bit_width = 39, .address = Invalid , .size = Size_6_GB , .type = KAddressSpaceInfo::Type::Heap, }, + { .bit_width = 39, .address = Invalid , .size = Size_64_GB , .type = KAddressSpaceInfo::Type::Alias, }, + { .bit_width = 39, .address = Invalid , .size = Size_2_GB , .type = KAddressSpaceInfo::Type::Stack, }, +}}; +// clang-format on + +constexpr bool IsAllowedIndexForAddress(std::size_t index) { + return index < AddressSpaceInfos.size() && AddressSpaceInfos[index].address != Invalid; +} + +using IndexArray = + std::array(KAddressSpaceInfo::Type::Count)>; + +constexpr IndexArray AddressSpaceIndices32Bit{ + 0, 1, 0, 2, 0, 3, +}; + +constexpr IndexArray AddressSpaceIndices36Bit{ + 4, 5, 4, 6, 4, 7, +}; + +constexpr IndexArray AddressSpaceIndices39Bit{ + 9, 8, 8, 10, 12, 11, +}; + +constexpr bool IsAllowed32BitType(KAddressSpaceInfo::Type type) { + return type < KAddressSpaceInfo::Type::Count && type != KAddressSpaceInfo::Type::Map39Bit && + type != KAddressSpaceInfo::Type::Stack; +} + +constexpr bool IsAllowed36BitType(KAddressSpaceInfo::Type type) { + return type < KAddressSpaceInfo::Type::Count && type != KAddressSpaceInfo::Type::Map39Bit && + type != KAddressSpaceInfo::Type::Stack; +} + +constexpr bool IsAllowed39BitType(KAddressSpaceInfo::Type type) { + return type < KAddressSpaceInfo::Type::Count && type != KAddressSpaceInfo::Type::MapLarge; +} + +} // namespace + +u64 KAddressSpaceInfo::GetAddressSpaceStart(std::size_t width, Type type) { + const std::size_t index{static_cast(type)}; + switch (width) { + case 32: + ASSERT(IsAllowed32BitType(type)); + ASSERT(IsAllowedIndexForAddress(AddressSpaceIndices32Bit[index])); + return AddressSpaceInfos[AddressSpaceIndices32Bit[index]].address; + case 36: + ASSERT(IsAllowed36BitType(type)); + ASSERT(IsAllowedIndexForAddress(AddressSpaceIndices36Bit[index])); + return AddressSpaceInfos[AddressSpaceIndices36Bit[index]].address; + case 39: + ASSERT(IsAllowed39BitType(type)); + ASSERT(IsAllowedIndexForAddress(AddressSpaceIndices39Bit[index])); + return AddressSpaceInfos[AddressSpaceIndices39Bit[index]].address; + } + UNREACHABLE(); + return 0; +} + +std::size_t KAddressSpaceInfo::GetAddressSpaceSize(std::size_t width, Type type) { + const std::size_t index{static_cast(type)}; + switch (width) { + case 32: + ASSERT(IsAllowed32BitType(type)); + return AddressSpaceInfos[AddressSpaceIndices32Bit[index]].size; + case 36: + ASSERT(IsAllowed36BitType(type)); + return AddressSpaceInfos[AddressSpaceIndices36Bit[index]].size; + case 39: + ASSERT(IsAllowed39BitType(type)); + return AddressSpaceInfos[AddressSpaceIndices39Bit[index]].size; + } + UNREACHABLE(); + return 0; +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_address_space_info.h b/src/core/hle/kernel/k_address_space_info.h new file mode 100644 index 000000000..06f31c6d5 --- /dev/null +++ b/src/core/hle/kernel/k_address_space_info.h @@ -0,0 +1,31 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +namespace Kernel { + +struct KAddressSpaceInfo final { + enum class Type : u32 { + MapSmall = 0, + MapLarge = 1, + Map39Bit = 2, + Heap = 3, + Stack = 4, + Alias = 5, + Count, + }; + + static u64 GetAddressSpaceStart(std::size_t width, Type type); + static std::size_t GetAddressSpaceSize(std::size_t width, Type type); + + const std::size_t bit_width{}; + const std::size_t address{}; + const std::size_t size{}; + const Type type{}; +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/memory/address_space_info.cpp b/src/core/hle/kernel/memory/address_space_info.cpp deleted file mode 100644 index 6cf43ba24..000000000 --- a/src/core/hle/kernel/memory/address_space_info.cpp +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright 2020 yuzu Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -// This file references various implementation details from Atmosphere, an open-source firmware for -// the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX. - -#include - -#include "common/assert.h" -#include "core/hle/kernel/memory/address_space_info.h" - -namespace Kernel::Memory { - -namespace { - -enum : u64 { - Size_1_MB = 0x100000, - Size_2_MB = 2 * Size_1_MB, - Size_128_MB = 128 * Size_1_MB, - Size_1_GB = 0x40000000, - Size_2_GB = 2 * Size_1_GB, - Size_4_GB = 4 * Size_1_GB, - Size_6_GB = 6 * Size_1_GB, - Size_64_GB = 64 * Size_1_GB, - Size_512_GB = 512 * Size_1_GB, - Invalid = std::numeric_limits::max(), -}; - -// clang-format off -constexpr std::array AddressSpaceInfos{{ - { .bit_width = 32, .address = Size_2_MB , .size = Size_1_GB - Size_2_MB , .type = AddressSpaceInfo::Type::Is32Bit, }, - { .bit_width = 32, .address = Size_1_GB , .size = Size_4_GB - Size_1_GB , .type = AddressSpaceInfo::Type::Small64Bit, }, - { .bit_width = 32, .address = Invalid , .size = Size_1_GB , .type = AddressSpaceInfo::Type::Heap, }, - { .bit_width = 32, .address = Invalid , .size = Size_1_GB , .type = AddressSpaceInfo::Type::Alias, }, - { .bit_width = 36, .address = Size_128_MB, .size = Size_2_GB - Size_128_MB, .type = AddressSpaceInfo::Type::Is32Bit, }, - { .bit_width = 36, .address = Size_2_GB , .size = Size_64_GB - Size_2_GB , .type = AddressSpaceInfo::Type::Small64Bit, }, - { .bit_width = 36, .address = Invalid , .size = Size_6_GB , .type = AddressSpaceInfo::Type::Heap, }, - { .bit_width = 36, .address = Invalid , .size = Size_6_GB , .type = AddressSpaceInfo::Type::Alias, }, - { .bit_width = 39, .address = Size_128_MB, .size = Size_512_GB - Size_128_MB, .type = AddressSpaceInfo::Type::Large64Bit, }, - { .bit_width = 39, .address = Invalid , .size = Size_64_GB , .type = AddressSpaceInfo::Type::Is32Bit }, - { .bit_width = 39, .address = Invalid , .size = Size_6_GB , .type = AddressSpaceInfo::Type::Heap, }, - { .bit_width = 39, .address = Invalid , .size = Size_64_GB , .type = AddressSpaceInfo::Type::Alias, }, - { .bit_width = 39, .address = Invalid , .size = Size_2_GB , .type = AddressSpaceInfo::Type::Stack, }, -}}; -// clang-format on - -constexpr bool IsAllowedIndexForAddress(std::size_t index) { - return index < AddressSpaceInfos.size() && AddressSpaceInfos[index].address != Invalid; -} - -using IndexArray = std::array(AddressSpaceInfo::Type::Count)>; - -constexpr IndexArray AddressSpaceIndices32Bit{ - 0, 1, 0, 2, 0, 3, -}; - -constexpr IndexArray AddressSpaceIndices36Bit{ - 4, 5, 4, 6, 4, 7, -}; - -constexpr IndexArray AddressSpaceIndices39Bit{ - 9, 8, 8, 10, 12, 11, -}; - -constexpr bool IsAllowed32BitType(AddressSpaceInfo::Type type) { - return type < AddressSpaceInfo::Type::Count && type != AddressSpaceInfo::Type::Large64Bit && - type != AddressSpaceInfo::Type::Stack; -} - -constexpr bool IsAllowed36BitType(AddressSpaceInfo::Type type) { - return type < AddressSpaceInfo::Type::Count && type != AddressSpaceInfo::Type::Large64Bit && - type != AddressSpaceInfo::Type::Stack; -} - -constexpr bool IsAllowed39BitType(AddressSpaceInfo::Type type) { - return type < AddressSpaceInfo::Type::Count && type != AddressSpaceInfo::Type::Small64Bit; -} - -} // namespace - -u64 AddressSpaceInfo::GetAddressSpaceStart(std::size_t width, Type type) { - const std::size_t index{static_cast(type)}; - switch (width) { - case 32: - ASSERT(IsAllowed32BitType(type)); - ASSERT(IsAllowedIndexForAddress(AddressSpaceIndices32Bit[index])); - return AddressSpaceInfos[AddressSpaceIndices32Bit[index]].address; - case 36: - ASSERT(IsAllowed36BitType(type)); - ASSERT(IsAllowedIndexForAddress(AddressSpaceIndices36Bit[index])); - return AddressSpaceInfos[AddressSpaceIndices36Bit[index]].address; - case 39: - ASSERT(IsAllowed39BitType(type)); - ASSERT(IsAllowedIndexForAddress(AddressSpaceIndices39Bit[index])); - return AddressSpaceInfos[AddressSpaceIndices39Bit[index]].address; - } - UNREACHABLE(); - return 0; -} - -std::size_t AddressSpaceInfo::GetAddressSpaceSize(std::size_t width, Type type) { - const std::size_t index{static_cast(type)}; - switch (width) { - case 32: - ASSERT(IsAllowed32BitType(type)); - return AddressSpaceInfos[AddressSpaceIndices32Bit[index]].size; - case 36: - ASSERT(IsAllowed36BitType(type)); - return AddressSpaceInfos[AddressSpaceIndices36Bit[index]].size; - case 39: - ASSERT(IsAllowed39BitType(type)); - return AddressSpaceInfos[AddressSpaceIndices39Bit[index]].size; - } - UNREACHABLE(); - return 0; -} - -} // namespace Kernel::Memory diff --git a/src/core/hle/kernel/memory/address_space_info.h b/src/core/hle/kernel/memory/address_space_info.h deleted file mode 100644 index a4e6e91e5..000000000 --- a/src/core/hle/kernel/memory/address_space_info.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2020 yuzu Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -// This file references various implementation details from Atmosphere, an open-source firmware for -// the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX. - -#pragma once - -#include "common/common_types.h" - -namespace Kernel::Memory { - -struct AddressSpaceInfo final { - enum class Type : u32 { - Is32Bit = 0, - Small64Bit = 1, - Large64Bit = 2, - Heap = 3, - Stack = 4, - Alias = 5, - Count, - }; - - static u64 GetAddressSpaceStart(std::size_t width, Type type); - static std::size_t GetAddressSpaceSize(std::size_t width, Type type); - - const std::size_t bit_width{}; - const std::size_t address{}; - const std::size_t size{}; - const Type type{}; -}; - -} // namespace Kernel::Memory diff --git a/src/core/hle/kernel/memory/page_table.cpp b/src/core/hle/kernel/memory/page_table.cpp index 034d43ecd..02a17a695 100644 --- a/src/core/hle/kernel/memory/page_table.cpp +++ b/src/core/hle/kernel/memory/page_table.cpp @@ -6,11 +6,11 @@ #include "common/assert.h" #include "common/scope_exit.h" #include "core/core.h" +#include "core/hle/kernel/k_address_space_info.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/memory/address_space_info.h" #include "core/hle/kernel/memory/memory_block.h" #include "core/hle/kernel/memory/memory_block_manager.h" #include "core/hle/kernel/memory/page_linked_list.h" @@ -64,19 +64,19 @@ ResultCode PageTable::InitializeForProcess(FileSys::ProgramAddressSpaceType as_t bool enable_aslr, VAddr code_addr, std::size_t code_size, Memory::MemoryManager::Pool pool) { - const auto GetSpaceStart = [this](AddressSpaceInfo::Type type) { - return AddressSpaceInfo::GetAddressSpaceStart(address_space_width, type); + const auto GetSpaceStart = [this](KAddressSpaceInfo::Type type) { + return KAddressSpaceInfo::GetAddressSpaceStart(address_space_width, type); }; - const auto GetSpaceSize = [this](AddressSpaceInfo::Type type) { - return AddressSpaceInfo::GetAddressSpaceSize(address_space_width, type); + const auto GetSpaceSize = [this](KAddressSpaceInfo::Type type) { + return KAddressSpaceInfo::GetAddressSpaceSize(address_space_width, type); }; // Set our width and heap/alias sizes address_space_width = GetAddressSpaceWidthFromType(as_type); const VAddr start = 0; const VAddr end{1ULL << address_space_width}; - std::size_t alias_region_size{GetSpaceSize(AddressSpaceInfo::Type::Alias)}; - std::size_t heap_region_size{GetSpaceSize(AddressSpaceInfo::Type::Heap)}; + std::size_t alias_region_size{GetSpaceSize(KAddressSpaceInfo::Type::Alias)}; + std::size_t heap_region_size{GetSpaceSize(KAddressSpaceInfo::Type::Heap)}; ASSERT(start <= code_addr); ASSERT(code_addr < code_addr + code_size); @@ -96,12 +96,12 @@ ResultCode PageTable::InitializeForProcess(FileSys::ProgramAddressSpaceType as_t std::size_t kernel_map_region_size{}; if (address_space_width == 39) { - alias_region_size = GetSpaceSize(AddressSpaceInfo::Type::Alias); - heap_region_size = GetSpaceSize(AddressSpaceInfo::Type::Heap); - stack_region_size = GetSpaceSize(AddressSpaceInfo::Type::Stack); - kernel_map_region_size = GetSpaceSize(AddressSpaceInfo::Type::Is32Bit); - code_region_start = GetSpaceStart(AddressSpaceInfo::Type::Large64Bit); - code_region_end = code_region_start + GetSpaceSize(AddressSpaceInfo::Type::Large64Bit); + alias_region_size = GetSpaceSize(KAddressSpaceInfo::Type::Alias); + heap_region_size = GetSpaceSize(KAddressSpaceInfo::Type::Heap); + stack_region_size = GetSpaceSize(KAddressSpaceInfo::Type::Stack); + kernel_map_region_size = GetSpaceSize(KAddressSpaceInfo::Type::MapSmall); + code_region_start = GetSpaceStart(KAddressSpaceInfo::Type::Map39Bit); + code_region_end = code_region_start + GetSpaceSize(KAddressSpaceInfo::Type::Map39Bit); alias_code_region_start = code_region_start; alias_code_region_end = code_region_end; process_code_start = Common::AlignDown(code_addr, RegionAlignment); @@ -109,12 +109,12 @@ ResultCode PageTable::InitializeForProcess(FileSys::ProgramAddressSpaceType as_t } else { stack_region_size = 0; kernel_map_region_size = 0; - code_region_start = GetSpaceStart(AddressSpaceInfo::Type::Is32Bit); - code_region_end = code_region_start + GetSpaceSize(AddressSpaceInfo::Type::Is32Bit); + code_region_start = GetSpaceStart(KAddressSpaceInfo::Type::MapSmall); + code_region_end = code_region_start + GetSpaceSize(KAddressSpaceInfo::Type::MapSmall); stack_region_start = code_region_start; alias_code_region_start = code_region_start; - alias_code_region_end = GetSpaceStart(AddressSpaceInfo::Type::Small64Bit) + - GetSpaceSize(AddressSpaceInfo::Type::Small64Bit); + alias_code_region_end = GetSpaceStart(KAddressSpaceInfo::Type::MapLarge) + + GetSpaceSize(KAddressSpaceInfo::Type::MapLarge); stack_region_end = code_region_end; kernel_map_region_start = code_region_start; kernel_map_region_end = code_region_end; -- cgit v1.2.3