From f7a008d77f8aa47baf4c874c508b38af4965a145 Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 11 Feb 2021 18:55:22 -0800 Subject: hle: kernel: KSystemControl does not belong in Memory namespace. --- src/core/CMakeLists.txt | 4 +-- src/core/hle/kernel/k_page_bitmap.h | 4 +-- src/core/hle/kernel/k_system_control.cpp | 42 +++++++++++++++++++++++++++ src/core/hle/kernel/k_system_control.h | 19 ++++++++++++ src/core/hle/kernel/memory/page_table.cpp | 11 +++---- src/core/hle/kernel/memory/system_control.cpp | 41 -------------------------- src/core/hle/kernel/memory/system_control.h | 14 --------- src/core/hle/service/ldr/ldr.cpp | 4 +-- 8 files changed, 73 insertions(+), 66 deletions(-) create mode 100644 src/core/hle/kernel/k_system_control.cpp create mode 100644 src/core/hle/kernel/k_system_control.h delete mode 100644 src/core/hle/kernel/memory/system_control.cpp delete mode 100644 src/core/hle/kernel/memory/system_control.h (limited to 'src/core') diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 6604bc2c5..8aad9cb41 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -182,6 +182,8 @@ add_library(core STATIC hle/kernel/k_spin_lock.h hle/kernel/k_synchronization_object.cpp hle/kernel/k_synchronization_object.h + hle/kernel/k_system_control.cpp + hle/kernel/k_system_control.h hle/kernel/k_thread.cpp hle/kernel/k_thread.h hle/kernel/k_thread_queue.h @@ -204,8 +206,6 @@ add_library(core STATIC hle/kernel/memory/page_table.cpp hle/kernel/memory/page_table.h hle/kernel/memory/slab_heap.h - hle/kernel/memory/system_control.cpp - hle/kernel/memory/system_control.h hle/kernel/object.cpp hle/kernel/object.h hle/kernel/physical_core.cpp diff --git a/src/core/hle/kernel/k_page_bitmap.h b/src/core/hle/kernel/k_page_bitmap.h index da2d20032..c75d667c9 100644 --- a/src/core/hle/kernel/k_page_bitmap.h +++ b/src/core/hle/kernel/k_page_bitmap.h @@ -12,7 +12,7 @@ #include "common/bit_util.h" #include "common/common_types.h" #include "common/tiny_mt.h" -#include "core/hle/kernel/memory/system_control.h" +#include "core/hle/kernel/k_system_control.h" namespace Kernel { @@ -43,7 +43,7 @@ private: public: RandomBitGenerator() { - rng.Initialize(static_cast(Memory::SystemControl::GenerateRandomU64())); + rng.Initialize(static_cast(KSystemControl::GenerateRandomU64())); } std::size_t SelectRandomBit(u64 bitmap) { diff --git a/src/core/hle/kernel/k_system_control.cpp b/src/core/hle/kernel/k_system_control.cpp new file mode 100644 index 000000000..aa1682f69 --- /dev/null +++ b/src/core/hle/kernel/k_system_control.cpp @@ -0,0 +1,42 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include + +#include "core/hle/kernel/k_system_control.h" + +namespace Kernel { + +namespace { +template +u64 GenerateUniformRange(u64 min, u64 max, F f) { + // Handle the case where the difference is too large to represent. + if (max == std::numeric_limits::max() && min == std::numeric_limits::min()) { + return f(); + } + + // Iterate until we get a value in range. + const u64 range_size = ((max + 1) - min); + const u64 effective_max = (std::numeric_limits::max() / range_size) * range_size; + while (true) { + if (const u64 rnd = f(); rnd < effective_max) { + return min + (rnd % range_size); + } + } +} + +} // Anonymous namespace + +u64 KSystemControl::GenerateRandomU64() { + static std::random_device device; + static std::mt19937 gen(device()); + static std::uniform_int_distribution distribution(1, std::numeric_limits::max()); + return distribution(gen); +} + +u64 KSystemControl::GenerateRandomRange(u64 min, u64 max) { + return GenerateUniformRange(min, max, GenerateRandomU64); +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_system_control.h b/src/core/hle/kernel/k_system_control.h new file mode 100644 index 000000000..1d5b64ffa --- /dev/null +++ b/src/core/hle/kernel/k_system_control.h @@ -0,0 +1,19 @@ +// Copyright 2021 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 { + +class KSystemControl { +public: + KSystemControl() = default; + + static u64 GenerateRandomRange(u64 min, u64 max); + static u64 GenerateRandomU64(); +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/memory/page_table.cpp b/src/core/hle/kernel/memory/page_table.cpp index 00ed9b881..034d43ecd 100644 --- a/src/core/hle/kernel/memory/page_table.cpp +++ b/src/core/hle/kernel/memory/page_table.cpp @@ -6,14 +6,15 @@ #include "common/assert.h" #include "common/scope_exit.h" #include "core/core.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" #include "core/hle/kernel/memory/page_table.h" -#include "core/hle/kernel/memory/system_control.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/memory.h" @@ -149,13 +150,13 @@ ResultCode PageTable::InitializeForProcess(FileSys::ProgramAddressSpaceType as_t // Determine random placements for each region std::size_t alias_rnd{}, heap_rnd{}, stack_rnd{}, kmap_rnd{}; if (enable_aslr) { - alias_rnd = SystemControl::GenerateRandomRange(0, remaining_size / RegionAlignment) * + alias_rnd = KSystemControl::GenerateRandomRange(0, remaining_size / RegionAlignment) * RegionAlignment; - heap_rnd = SystemControl::GenerateRandomRange(0, remaining_size / RegionAlignment) * + heap_rnd = KSystemControl::GenerateRandomRange(0, remaining_size / RegionAlignment) * RegionAlignment; - stack_rnd = SystemControl::GenerateRandomRange(0, remaining_size / RegionAlignment) * + stack_rnd = KSystemControl::GenerateRandomRange(0, remaining_size / RegionAlignment) * RegionAlignment; - kmap_rnd = SystemControl::GenerateRandomRange(0, remaining_size / RegionAlignment) * + kmap_rnd = KSystemControl::GenerateRandomRange(0, remaining_size / RegionAlignment) * RegionAlignment; } diff --git a/src/core/hle/kernel/memory/system_control.cpp b/src/core/hle/kernel/memory/system_control.cpp deleted file mode 100644 index e855696ad..000000000 --- a/src/core/hle/kernel/memory/system_control.cpp +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2020 yuzu Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include - -#include "core/hle/kernel/memory/system_control.h" - -namespace Kernel::Memory::SystemControl { -namespace { -template -u64 GenerateUniformRange(u64 min, u64 max, F f) { - // Handle the case where the difference is too large to represent. - if (max == std::numeric_limits::max() && min == std::numeric_limits::min()) { - return f(); - } - - // Iterate until we get a value in range. - const u64 range_size = ((max + 1) - min); - const u64 effective_max = (std::numeric_limits::max() / range_size) * range_size; - while (true) { - if (const u64 rnd = f(); rnd < effective_max) { - return min + (rnd % range_size); - } - } -} - -} // Anonymous namespace - -u64 GenerateRandomU64() { - static std::random_device device; - static std::mt19937 gen(device()); - static std::uniform_int_distribution distribution(1, std::numeric_limits::max()); - return distribution(gen); -} - -u64 GenerateRandomRange(u64 min, u64 max) { - return GenerateUniformRange(min, max, GenerateRandomU64); -} - -} // namespace Kernel::Memory::SystemControl diff --git a/src/core/hle/kernel/memory/system_control.h b/src/core/hle/kernel/memory/system_control.h deleted file mode 100644 index a01b6b014..000000000 --- a/src/core/hle/kernel/memory/system_control.h +++ /dev/null @@ -1,14 +0,0 @@ -// 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::Memory::SystemControl { - -u64 GenerateRandomRange(u64 min, u64 max); -u64 GenerateRandomU64(); - -} // namespace Kernel::Memory::SystemControl diff --git a/src/core/hle/service/ldr/ldr.cpp b/src/core/hle/service/ldr/ldr.cpp index c724d2554..d3cd25ff8 100644 --- a/src/core/hle/service/ldr/ldr.cpp +++ b/src/core/hle/service/ldr/ldr.cpp @@ -11,8 +11,8 @@ #include "common/scope_exit.h" #include "core/core.h" #include "core/hle/ipc_helpers.h" +#include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/memory/page_table.h" -#include "core/hle/kernel/memory/system_control.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/service/ldr/ldr.h" @@ -315,7 +315,7 @@ public: Kernel::Memory::PageBits}; do { addr = page_table.GetAliasCodeRegionStart() + - (Kernel::Memory::SystemControl::GenerateRandomRange(0, end_pages) + (Kernel::KSystemControl::GenerateRandomRange(0, end_pages) << Kernel::Memory::PageBits); } while (!page_table.IsInsideAddressSpace(addr, size) || page_table.IsInsideHeapRegion(addr, size) || -- cgit v1.2.3