From 1c99a2e7fb2b640900166f3806ed43df990bb696 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sun, 8 Nov 2020 01:56:35 +0300 Subject: [PATCH] vm: add map_self() method to utils::shm Add complementary unmap_self() method. Move VirtualMemory to util/vm.hpp Minor associated include cleanup. Move asm.h to util/asm.hpp --- Utilities/JIT.cpp | 2 +- Utilities/Thread.cpp | 1 + Utilities/typemap.h | 1 - rpcs3/Emu/CMakeLists.txt | 2 +- rpcs3/Emu/Cell/PPUAnalyser.cpp | 2 +- rpcs3/Emu/Cell/PPUInterpreter.cpp | 5 ++-- rpcs3/Emu/Cell/PPUModule.cpp | 3 +-- rpcs3/Emu/Cell/PPUThread.cpp | 3 +-- rpcs3/Emu/Cell/SPUASMJITRecompiler.cpp | 3 ++- rpcs3/Emu/Cell/SPUInterpreter.cpp | 3 ++- rpcs3/Emu/Cell/SPUThread.cpp | 3 ++- rpcs3/Emu/Cell/lv2/sys_memory.cpp | 3 ++- rpcs3/Emu/Cell/lv2/sys_mmapper.cpp | 3 ++- rpcs3/Emu/Cell/lv2/sys_mmapper.h | 5 ++++ rpcs3/Emu/Cell/lv2/sys_time.cpp | 3 ++- rpcs3/Emu/Memory/vm.cpp | 16 +++++++------ rpcs3/Emu/Memory/vm.h | 6 ++++- rpcs3/Emu/RSX/RSXThread.cpp | 3 ++- rpcs3/Emu/RSX/rsx_cache.h | 4 +++- rpcs3/emucore.vcxproj | 5 ++-- rpcs3/emucore.vcxproj.filters | 7 ++++-- Utilities/asm.h => rpcs3/util/asm.hpp | 18 +++++++++++--- .../VirtualMemory.h => rpcs3/util/vm.hpp | 15 +++++++++++- .../util/vm_native.cpp | 24 ++++++++++++++++++- 24 files changed, 105 insertions(+), 35 deletions(-) rename Utilities/asm.h => rpcs3/util/asm.hpp (93%) rename Utilities/VirtualMemory.h => rpcs3/util/vm.hpp (85%) rename Utilities/VirtualMemory.cpp => rpcs3/util/vm_native.cpp (96%) diff --git a/Utilities/JIT.cpp b/Utilities/JIT.cpp index fd3d52581a..f5fefb88b8 100644 --- a/Utilities/JIT.cpp +++ b/Utilities/JIT.cpp @@ -5,7 +5,7 @@ #include "util/logs.hpp" #include "mutex.h" #include "sysinfo.h" -#include "VirtualMemory.h" +#include "util/vm.hpp" #include #include diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index 8d193fee51..8c6e6599e4 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -73,6 +73,7 @@ #endif #include "sync.h" +#include "util/vm.hpp" #include "util/logs.hpp" #include "Emu/Memory/vm_locking.h" diff --git a/Utilities/typemap.h b/Utilities/typemap.h index 15a0f7e5d9..082ed633d8 100644 --- a/Utilities/typemap.h +++ b/Utilities/typemap.h @@ -4,7 +4,6 @@ #include "mutex.h" #include "util/atomic.hpp" #include "util/typeindices.hpp" -#include "VirtualMemory.h" #include namespace utils diff --git a/rpcs3/Emu/CMakeLists.txt b/rpcs3/Emu/CMakeLists.txt index f02e072a61..f51f625bb7 100644 --- a/rpcs3/Emu/CMakeLists.txt +++ b/rpcs3/Emu/CMakeLists.txt @@ -39,6 +39,7 @@ target_sources(rpcs3_emu PRIVATE ../util/logs.cpp ../util/yaml.cpp ../util/cereal.cpp + ../util/vm_native.cpp ../../Utilities/bin_patch.cpp ../../Utilities/cheat_info.cpp ../../Utilities/cond.cpp @@ -54,7 +55,6 @@ target_sources(rpcs3_emu PRIVATE ../../Utilities/sysinfo.cpp ../../Utilities/Thread.cpp ../../Utilities/version.cpp - ../../Utilities/VirtualMemory.cpp ) target_include_directories(rpcs3_emu PUBLIC "${CMAKE_SOURCE_DIR}") diff --git a/rpcs3/Emu/Cell/PPUAnalyser.cpp b/rpcs3/Emu/Cell/PPUAnalyser.cpp index d3a674c57f..7a0d77b414 100644 --- a/rpcs3/Emu/Cell/PPUAnalyser.cpp +++ b/rpcs3/Emu/Cell/PPUAnalyser.cpp @@ -6,7 +6,7 @@ #include #include "util/yaml.hpp" -#include "Utilities/asm.h" +#include "util/asm.hpp" LOG_CHANNEL(ppu_validator); diff --git a/rpcs3/Emu/Cell/PPUInterpreter.cpp b/rpcs3/Emu/Cell/PPUInterpreter.cpp index f08246d871..7c5c7324d7 100644 --- a/rpcs3/Emu/Cell/PPUInterpreter.cpp +++ b/rpcs3/Emu/Cell/PPUInterpreter.cpp @@ -4,7 +4,6 @@ #include "Emu/Memory/vm_reservation.h" #include "Emu/system_config.h" #include "PPUThread.h" -#include "Utilities/asm.h" #include "Utilities/sysinfo.h" #include "Emu/Cell/Common.h" @@ -12,6 +11,8 @@ #include #include +#include "util/asm.hpp" + #if !defined(_MSC_VER) && defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wold-style-cast" @@ -119,7 +120,7 @@ FORCE_INLINE auto ppu_feed_data(ppu_thread& ppu, u64 addr) const std::byte* src; u32 size; u32 offs = 0; - + if (raddr / 128 == addr / 128) src = &ppu.rdata[addr & 127], size = std::min(128 - (addr % 128), sizeof(T)); else diff --git a/rpcs3/Emu/Cell/PPUModule.cpp b/rpcs3/Emu/Cell/PPUModule.cpp index fc377d2006..2b9fbe8a45 100644 --- a/rpcs3/Emu/Cell/PPUModule.cpp +++ b/rpcs3/Emu/Cell/PPUModule.cpp @@ -1,7 +1,6 @@ #include "stdafx.h" #include "Emu/Cell/PPUModule.h" -#include "Utilities/VirtualMemory.h" #include "Utilities/bin_patch.h" #include "Utilities/StrUtil.h" #include "Crypto/sha1.h" @@ -747,7 +746,7 @@ static void ppu_check_patch_spu_images(const ppu_segment& seg) if (!name.empty()) { fmt::append(dump, "\n\tSPUNAME: '%s'", name); - } + } } } diff --git a/rpcs3/Emu/Cell/PPUThread.cpp b/rpcs3/Emu/Cell/PPUThread.cpp index 9e1ab60f86..3855d6e831 100644 --- a/rpcs3/Emu/Cell/PPUThread.cpp +++ b/rpcs3/Emu/Cell/PPUThread.cpp @@ -1,5 +1,4 @@ #include "stdafx.h" -#include "Utilities/VirtualMemory.h" #include "Utilities/sysinfo.h" #include "Utilities/JIT.h" #include "Crypto/sha1.h" @@ -64,7 +63,7 @@ #include #include #include -#include +#include "util/vm.hpp" const bool s_use_ssse3 = utils::has_ssse3(); diff --git a/rpcs3/Emu/Cell/SPUASMJITRecompiler.cpp b/rpcs3/Emu/Cell/SPUASMJITRecompiler.cpp index dae3d33e5d..ede44ac5c7 100644 --- a/rpcs3/Emu/Cell/SPUASMJITRecompiler.cpp +++ b/rpcs3/Emu/Cell/SPUASMJITRecompiler.cpp @@ -8,10 +8,11 @@ #include "SPUThread.h" #include "SPUInterpreter.h" #include "Utilities/sysinfo.h" -#include "Utilities/asm.h" #include "PPUAnalyser.h" #include "Crypto/sha1.h" +#include "util/asm.hpp" + #include #include #include diff --git a/rpcs3/Emu/Cell/SPUInterpreter.cpp b/rpcs3/Emu/Cell/SPUInterpreter.cpp index 629a55dbf0..0ef65ffcd2 100644 --- a/rpcs3/Emu/Cell/SPUInterpreter.cpp +++ b/rpcs3/Emu/Cell/SPUInterpreter.cpp @@ -3,10 +3,11 @@ #include "Utilities/JIT.h" #include "Utilities/sysinfo.h" -#include "Utilities/asm.h" #include "SPUThread.h" #include "Emu/Cell/Common.h" +#include "util/asm.hpp" + #include #include diff --git a/rpcs3/Emu/Cell/SPUThread.cpp b/rpcs3/Emu/Cell/SPUThread.cpp index 0d67a46411..935262ee22 100644 --- a/rpcs3/Emu/Cell/SPUThread.cpp +++ b/rpcs3/Emu/Cell/SPUThread.cpp @@ -1,6 +1,5 @@ #include "stdafx.h" #include "Utilities/JIT.h" -#include "Utilities/asm.h" #include "Utilities/date_time.h" #include "Utilities/sysinfo.h" #include "Emu/Memory/vm.h" @@ -31,6 +30,8 @@ #include #include #include +#include "util/vm.hpp" +#include "util/asm.hpp" using spu_rdata_t = decltype(spu_thread::rdata); diff --git a/rpcs3/Emu/Cell/lv2/sys_memory.cpp b/rpcs3/Emu/Cell/lv2/sys_memory.cpp index 54a13df94c..a6a4284033 100644 --- a/rpcs3/Emu/Cell/lv2/sys_memory.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_memory.cpp @@ -1,12 +1,13 @@ #include "stdafx.h" #include "sys_memory.h" -#include "Utilities/VirtualMemory.h" #include "Emu/Memory/vm_locking.h" #include "Emu/CPU/CPUThread.h" #include "Emu/Cell/ErrorCodes.h" #include "Emu/IdManager.h" +#include "util/vm.hpp" + LOG_CHANNEL(sys_memory); // diff --git a/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp b/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp index 1949dab61c..d0ba560c5a 100644 --- a/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp @@ -5,11 +5,12 @@ #include "sys_ppu_thread.h" #include "Emu/Cell/lv2/sys_event.h" #include "Emu/Memory/vm_var.h" -#include "Utilities/VirtualMemory.h" #include "sys_memory.h" #include "sys_sync.h" #include "sys_process.h" +#include "util/vm.hpp" + LOG_CHANNEL(sys_mmapper); lv2_memory::lv2_memory(u32 size, u32 align, u64 flags, lv2_memory_container* ct) diff --git a/rpcs3/Emu/Cell/lv2/sys_mmapper.h b/rpcs3/Emu/Cell/lv2/sys_mmapper.h index e3d1fe093b..6850c325e4 100644 --- a/rpcs3/Emu/Cell/lv2/sys_mmapper.h +++ b/rpcs3/Emu/Cell/lv2/sys_mmapper.h @@ -8,6 +8,11 @@ struct lv2_memory_container; +namespace utils +{ + class shm; +} + struct lv2_memory : lv2_obj { static const u32 id_base = 0x08000000; diff --git a/rpcs3/Emu/Cell/lv2/sys_time.cpp b/rpcs3/Emu/Cell/lv2/sys_time.cpp index d221b90698..703c5e57e9 100644 --- a/rpcs3/Emu/Cell/lv2/sys_time.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_time.cpp @@ -3,7 +3,8 @@ #include "Emu/system_config.h" #include "Emu/Cell/ErrorCodes.h" -#include "Utilities/asm.h" + +#include "util/asm.hpp" #ifdef _WIN32 diff --git a/rpcs3/Emu/Memory/vm.cpp b/rpcs3/Emu/Memory/vm.cpp index b373fe66b8..317dfd1902 100644 --- a/rpcs3/Emu/Memory/vm.cpp +++ b/rpcs3/Emu/Memory/vm.cpp @@ -7,7 +7,6 @@ #include "Utilities/mutex.h" #include "Utilities/Thread.h" -#include "Utilities/VirtualMemory.h" #include "Utilities/address_range.h" #include "Emu/CPU/CPUThread.h" #include "Emu/Cell/lv2/sys_memory.h" @@ -18,6 +17,9 @@ #include #include +#include "util/vm.hpp" +#include "util/asm.hpp" + LOG_CHANNEL(vm_log, "VM"); namespace vm @@ -301,9 +303,9 @@ namespace vm // Block or signal new range locks g_range_lock = addr | u64{size} << 32 | flags; - _mm_prefetch(g_range_lock_set + 0, _MM_HINT_T0); - _mm_prefetch(g_range_lock_set + 2, _MM_HINT_T0); - _mm_prefetch(g_range_lock_set + 4, _MM_HINT_T0); + utils::prefetch_read(g_range_lock_set + 0); + utils::prefetch_read(g_range_lock_set + 2); + utils::prefetch_read(g_range_lock_set + 4); const auto range = utils::address_range::start_length(addr, size); @@ -497,9 +499,9 @@ namespace vm g_range_lock = addr | range_locked; - _mm_prefetch(g_range_lock_set + 0, _MM_HINT_T0); - _mm_prefetch(g_range_lock_set + 2, _MM_HINT_T0); - _mm_prefetch(g_range_lock_set + 4, _MM_HINT_T0); + utils::prefetch_read(g_range_lock_set + 0); + utils::prefetch_read(g_range_lock_set + 2); + utils::prefetch_read(g_range_lock_set + 4); const auto range = utils::address_range::start_length(addr, 128); diff --git a/rpcs3/Emu/Memory/vm.h b/rpcs3/Emu/Memory/vm.h index fbb9ab5728..16a9ab09cd 100644 --- a/rpcs3/Emu/Memory/vm.h +++ b/rpcs3/Emu/Memory/vm.h @@ -3,10 +3,14 @@ #include #include #include "Utilities/types.h" -#include "Utilities/VirtualMemory.h" #include "Utilities/StrFmt.h" #include "Utilities/BEType.h" +namespace utils +{ + class shm; +} + namespace vm { extern u8* const g_base_addr; diff --git a/rpcs3/Emu/RSX/RSXThread.cpp b/rpcs3/Emu/RSX/RSXThread.cpp index 55036fdff8..03de591b66 100644 --- a/rpcs3/Emu/RSX/RSXThread.cpp +++ b/rpcs3/Emu/RSX/RSXThread.cpp @@ -14,11 +14,12 @@ #include "Overlays/overlay_perf_metrics.h" #include "Utilities/date_time.h" #include "Utilities/span.h" -#include "Utilities/asm.h" #include "Utilities/StrUtil.h" #include +#include "util/asm.hpp" + #include #include #include diff --git a/rpcs3/Emu/RSX/rsx_cache.h b/rpcs3/Emu/RSX/rsx_cache.h index 446315d60c..2ee7bf6ae3 100644 --- a/rpcs3/Emu/RSX/rsx_cache.h +++ b/rpcs3/Emu/RSX/rsx_cache.h @@ -1,5 +1,4 @@ #pragma once -#include "Utilities/VirtualMemory.h" #include "Utilities/hash.h" #include "Utilities/File.h" #include "Utilities/lockless.h" @@ -15,6 +14,9 @@ #include #include + +#include "util/vm.hpp" + namespace rsx { enum protection_policy diff --git a/rpcs3/emucore.vcxproj b/rpcs3/emucore.vcxproj index 24184cfeb0..f6c75108aa 100644 --- a/rpcs3/emucore.vcxproj +++ b/rpcs3/emucore.vcxproj @@ -174,7 +174,7 @@ - + NotUsing @@ -550,7 +550,8 @@ - + + diff --git a/rpcs3/emucore.vcxproj.filters b/rpcs3/emucore.vcxproj.filters index 56e222e385..3b80f08550 100644 --- a/rpcs3/emucore.vcxproj.filters +++ b/rpcs3/emucore.vcxproj.filters @@ -188,7 +188,7 @@ Emu\GPU\RSX\Common - + Utilities @@ -1165,7 +1165,10 @@ Emu\Cell - + + Utilities + + Utilities diff --git a/Utilities/asm.h b/rpcs3/util/asm.hpp similarity index 93% rename from Utilities/asm.h rename to rpcs3/util/asm.hpp index e56a2a121c..86f7f363cd 100644 --- a/Utilities/asm.h +++ b/rpcs3/util/asm.hpp @@ -1,6 +1,6 @@ #pragma once -#include "types.h" +#include "Utilities/types.h" extern bool g_use_rtm; extern u64 g_rtm_tx_limit1; @@ -67,10 +67,17 @@ namespace utils } }; - -// Rotate helpers #if defined(__GNUG__) + inline void prefetch_read(const void* ptr) + { +#if __has_builtin(__builtin_prefetch) + return __builtin_prefetch(ptr); +#else + __asm__ volatile ("prefetcht0 0(%[ptr])" : : [ptr] "r" (ptr)); +#endif + } + inline u8 rol8(u8 x, u8 n) { #if __has_builtin(__builtin_rotateleft8) @@ -200,6 +207,11 @@ namespace utils } #elif defined(_MSC_VER) + inline void prefetch_read(const void* ptr) + { + return _mm_prefetch(reinterpret_cast(ptr), _MM_HINT_T0); + } + inline u8 rol8(u8 x, u8 n) { return _rotl8(x, n); diff --git a/Utilities/VirtualMemory.h b/rpcs3/util/vm.hpp similarity index 85% rename from Utilities/VirtualMemory.h rename to rpcs3/util/vm.hpp index e830713a7a..0dcc02d574 100644 --- a/Utilities/VirtualMemory.h +++ b/rpcs3/util/vm.hpp @@ -1,6 +1,6 @@ #pragma once -#include "types.h" +#include "Utilities/types.h" namespace utils { @@ -49,6 +49,7 @@ namespace utils #endif u32 m_size; u32 m_flags; + void* m_ptr; public: explicit shm(u32 size, u32 flags = 0); @@ -65,12 +66,24 @@ namespace utils // Map shared memory over reserved memory region, which is unsafe (non-atomic) under Win32 u8* map_critical(void* ptr, protection prot = protection::rw); + // Map shared memory into its own storage (not mapped by default) + u8* map_self(protection prot = protection::rw); + // Unmap shared memory void unmap(void* ptr) const; // Unmap shared memory, undoing map_critical void unmap_critical(void* ptr); + // Unmap shared memory, undoing map_self() + void unmap_self(); + + // Get memory mapped by map_self() + u8* get() const + { + return reinterpret_cast(m_ptr); + } + u32 size() const { return m_size; diff --git a/Utilities/VirtualMemory.cpp b/rpcs3/util/vm_native.cpp similarity index 96% rename from Utilities/VirtualMemory.cpp rename to rpcs3/util/vm_native.cpp index e3e496ce78..e4dd50999d 100644 --- a/Utilities/VirtualMemory.cpp +++ b/rpcs3/util/vm_native.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" #include "util/logs.hpp" -#include "VirtualMemory.h" +#include "util/vm.hpp" #ifdef _WIN32 #include #else @@ -183,6 +183,7 @@ namespace utils shm::shm(u32 size, u32 flags) : m_size(::align(size, 0x10000)) , m_flags(flags) + , m_ptr(0) { #ifdef _WIN32 m_handle = ::CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, 0, m_size, NULL); @@ -211,6 +212,8 @@ namespace utils shm::~shm() { + this->unmap_self(); + #ifdef _WIN32 ::CloseHandle(m_handle); #else @@ -284,6 +287,16 @@ namespace utils return this->map(target, prot); } + u8* shm::map_self(protection prot) + { + if (!m_ptr) + { + m_ptr = this->map(nullptr, prot); + } + + return static_cast(m_ptr); + } + void shm::unmap(void* ptr) const { #ifdef _WIN32 @@ -325,4 +338,13 @@ namespace utils } #endif } + + void shm::unmap_self() + { + if (m_ptr) + { + this->unmap(m_ptr); + m_ptr = nullptr; + } + } }