From 0c7f7638892e2808bae19f1e5444111082d4c3f3 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Tue, 2 Feb 2016 00:52:49 +0300 Subject: [PATCH] Partial commit: Memory --- rpcs3/Emu/Memory/Memory.cpp | 4 +- rpcs3/Emu/Memory/vm.cpp | 68 +++-- rpcs3/Emu/Memory/vm.h | 67 +++-- rpcs3/Emu/Memory/vm_ptr.h | 532 ++++++++++++++++-------------------- rpcs3/Emu/Memory/vm_ref.h | 104 +++---- rpcs3/Emu/Memory/vm_var.h | 254 +++++------------ 6 files changed, 422 insertions(+), 607 deletions(-) diff --git a/rpcs3/Emu/Memory/Memory.cpp b/rpcs3/Emu/Memory/Memory.cpp index d9ca368f29..3f25183067 100644 --- a/rpcs3/Emu/Memory/Memory.cpp +++ b/rpcs3/Emu/Memory/Memory.cpp @@ -18,7 +18,7 @@ bool VirtualMemoryBlock::IsInMyRange(const u32 addr, const u32 size) u32 VirtualMemoryBlock::Map(u32 realaddr, u32 size) { - assert(size); + Expects(size); for (u32 addr = m_range_start; addr <= m_range_start + m_range_size - 1 - GetReservedAmount() - size;) { @@ -48,7 +48,7 @@ u32 VirtualMemoryBlock::Map(u32 realaddr, u32 size) bool VirtualMemoryBlock::Map(u32 realaddr, u32 size, u32 addr) { - assert(size); + Expects(size); if (!IsInMyRange(addr, size)) { diff --git a/rpcs3/Emu/Memory/vm.cpp b/rpcs3/Emu/Memory/vm.cpp index e5bd00b22d..3986166b12 100644 --- a/rpcs3/Emu/Memory/vm.cpp +++ b/rpcs3/Emu/Memory/vm.cpp @@ -24,6 +24,8 @@ namespace vm { + thread_local u64 g_tls_fault_count{}; + template struct mapped_ptr_deleter { void operator ()(void* ptr) @@ -93,7 +95,7 @@ namespace vm class reservation_mutex_t { - std::atomic m_lock{ false }; + atomic_t m_lock{ false }; std::thread::id m_owner{}; std::condition_variable m_cv; @@ -164,7 +166,7 @@ namespace vm std::mutex g_waiter_list_mutex; - waiter_t* _add_waiter(named_thread_t& thread, u32 addr, u32 size) + waiter_t* _add_waiter(named_thread& thread, u32 addr, u32 size) { std::lock_guard lock(g_waiter_list_mutex); @@ -255,7 +257,7 @@ namespace vm return true; } - waiter_lock_t::waiter_lock_t(named_thread_t& thread, u32 addr, u32 size) + waiter_lock_t::waiter_lock_t(named_thread& thread, u32 addr, u32 size) : m_waiter(_add_waiter(thread, addr, size)) , m_lock(thread.mutex, std::adopt_lock) // must be locked in _add_waiter { @@ -312,6 +314,12 @@ namespace vm } } + access_violation::access_violation(u64 addr, const char* cause) + : std::runtime_error(fmt::exception("Access violation %s address 0x%llx", cause, addr)) + { + g_tls_fault_count &= ~(1ull << 63); + } + void notify_at(u32 addr, u32 size) { const u64 align = 0x80000000ull >> cntlz32(size); @@ -353,7 +361,7 @@ namespace vm void start() { // start notification thread - thread_ctrl::spawn(PURE_EXPR("vm::start thread"s), []() + thread_ctrl::spawn("vm::start thread", []() { while (!Emu.IsStopped()) { @@ -654,8 +662,8 @@ namespace vm { _reservation_break(i * 4096); - const u8 f1 = g_pages[i]._or(flags_set & ~flags_inv) & (page_writable | page_readable); - g_pages[i]._and_not(flags_clear & ~flags_inv); + const u8 f1 = g_pages[i].fetch_or(flags_set & ~flags_inv) & (page_writable | page_readable); + g_pages[i].fetch_and(~(flags_clear & ~flags_inv)); const u8 f2 = (g_pages[i] ^= flags_inv) & (page_writable | page_readable); if (f1 != f2) @@ -1065,13 +1073,13 @@ namespace vm u32 stack_push(u32 size, u32 align_v) { - if (auto cpu = get_current_cpu_thread()) switch (cpu->get_type()) + if (auto cpu = get_current_cpu_thread()) switch (cpu->type) { - case CPU_THREAD_PPU: + case cpu_type::ppu: { PPUThread& context = static_cast(*cpu); - const u32 old_pos = VM_CAST(context.GPR[1]); + const u32 old_pos = vm::cast(context.GPR[1], HERE); context.GPR[1] -= align(size + 4, 8); // room minimal possible size context.GPR[1] &= ~(align_v - 1); // fix stack alignment @@ -1083,12 +1091,12 @@ namespace vm { const u32 addr = static_cast(context.GPR[1]); vm::ps3::_ref>(addr + size) = old_pos; + std::memset(vm::base(addr), 0, size); return addr; } } - case CPU_THREAD_SPU: - case CPU_THREAD_RAW_SPU: + case cpu_type::spu: { SPUThread& context = static_cast(*cpu); @@ -1108,9 +1116,9 @@ namespace vm } } - case CPU_THREAD_ARMv7: + case cpu_type::arm: { - ARMv7Context& context = static_cast(*cpu); + ARMv7Thread& context = static_cast(*cpu); const u32 old_pos = context.SP; context.SP -= align(size + 4, 4); // room minimal possible size @@ -1129,63 +1137,63 @@ namespace vm default: { - throw EXCEPTION("Invalid thread type (%d)", cpu->get_type()); + throw EXCEPTION("Invalid thread type (%u)", cpu->type); } } throw EXCEPTION("Invalid thread"); } - void stack_pop(u32 addr, u32 size) + void stack_pop_verbose(u32 addr, u32 size) noexcept { - if (auto cpu = get_current_cpu_thread()) switch (cpu->get_type()) + if (auto cpu = get_current_cpu_thread()) switch (cpu->type) { - case CPU_THREAD_PPU: + case cpu_type::ppu: { PPUThread& context = static_cast(*cpu); if (context.GPR[1] != addr) { - throw EXCEPTION("Stack inconsistency (addr=0x%x, SP=0x%llx, size=0x%x)", addr, context.GPR[1], size); + LOG_ERROR(MEMORY, "Stack inconsistency (addr=0x%x, SP=0x%llx, size=0x%x)", addr, context.GPR[1], size); + return; } context.GPR[1] = vm::ps3::_ref>(context.GPR[1] + size); return; } - case CPU_THREAD_SPU: - case CPU_THREAD_RAW_SPU: + case cpu_type::spu: { SPUThread& context = static_cast(*cpu); if (context.gpr[1]._u32[3] + context.offset != addr) { - throw EXCEPTION("Stack inconsistency (addr=0x%x, SP=LS:0x%05x, size=0x%x)", addr, context.gpr[1]._u32[3], size); + LOG_ERROR(MEMORY, "Stack inconsistency (addr=0x%x, SP=LS:0x%05x, size=0x%x)", addr, context.gpr[1]._u32[3], size); + return; } context.gpr[1]._u32[3] = vm::ps3::_ref>(context.gpr[1]._u32[3] + context.offset + size); return; } - case CPU_THREAD_ARMv7: + case cpu_type::arm: { - ARMv7Context& context = static_cast(*cpu); + ARMv7Thread& context = static_cast(*cpu); if (context.SP != addr) { - throw EXCEPTION("Stack inconsistency (addr=0x%x, SP=0x%x, size=0x%x)", addr, context.SP, size); + LOG_ERROR(MEMORY, "Stack inconsistency (addr=0x%x, SP=0x%x, size=0x%x)", addr, context.SP, size); + return; } context.SP = vm::psv::_ref>(context.SP + size); return; } - - default: - { - throw EXCEPTION("Invalid thread type (%d)", cpu->get_type()); - } } + } - throw EXCEPTION("Invalid thread"); + [[noreturn]] void throw_access_violation(u64 addr, const char* cause) + { + throw access_violation(addr, cause); } } diff --git a/rpcs3/Emu/Memory/vm.h b/rpcs3/Emu/Memory/vm.h index 06cb3a78f8..7b61048b54 100644 --- a/rpcs3/Emu/Memory/vm.h +++ b/rpcs3/Emu/Memory/vm.h @@ -1,6 +1,7 @@ #pragma once #include "Utilities/Thread.h" +#include namespace vm { @@ -30,28 +31,31 @@ namespace vm page_allocated = (1 << 7), }; + struct access_violation : std::runtime_error + { + access_violation(u64 addr, const char* cause); + }; + + [[noreturn]] void throw_access_violation(u64 addr, const char* cause); + struct waiter_t { u32 addr = 0; u32 mask = ~0; - named_thread_t* thread = nullptr; + named_thread* thread = nullptr; std::function pred; waiter_t() = default; - waiter_t* reset(u32 addr, u32 size, named_thread_t& thread) + waiter_t* reset(u32 addr, u32 size, named_thread& thread) { this->addr = addr; this->mask = ~(size - 1); this->thread = &thread; // must be null at this point - if (pred) - { - throw EXCEPTION("Unexpected"); - } - + Ensures(!pred); return this; } @@ -64,7 +68,7 @@ namespace vm std::unique_lock m_lock; public: - waiter_lock_t(named_thread_t& thread, u32 addr, u32 size); + waiter_lock_t(named_thread& thread, u32 addr, u32 size); waiter_t* operator ->() const { @@ -77,7 +81,8 @@ namespace vm }; // Wait until pred() returns true, addr must be aligned to size which must be a power of 2, pred() may be called by any thread - template auto wait_op(named_thread_t& thread, u32 addr, u32 size, F pred, Args&&... args) -> decltype(static_cast(pred(args...))) + template + auto wait_op(named_thread& thread, u32 addr, u32 size, F pred, Args&&... args) -> decltype(static_cast(pred(args...))) { // return immediately if condition passed (optimistic case) if (pred(args...)) return; @@ -207,7 +212,7 @@ namespace vm return res; } - throw EXCEPTION("Not a virtual memory pointer (%p)", real_ptr); + throw fmt::exception("Not a virtual memory pointer (%p)", real_ptr); } // Convert pointer-to-member to a vm address compatible offset @@ -216,38 +221,43 @@ namespace vm return static_cast(reinterpret_cast(&reinterpret_cast(reinterpret_cast(0ull)->*member_ptr))); } - template struct cast_ptr + template + struct cast_impl { - static_assert(std::is_same::value, "Unsupported VM_CAST() type"); + static_assert(std::is_same::value, "vm::cast() error: unsupported type"); }; - template<> struct cast_ptr + template<> + struct cast_impl { - static u32 cast(const u32 addr, const char* file, int line, const char* func) + static u32 cast(const u32& addr, const char* loc) { return addr; } }; - template<> struct cast_ptr + template<> + struct cast_impl { - static u32 cast(const u64 addr, const char* file, int line, const char* func) + static u32 cast(const u64& addr, const char* loc) { - return static_cast(addr) == addr ? static_cast(addr) : throw fmt::exception(file, line, func, "VM_CAST failed (addr=0x%llx)", addr); + return fmt::narrow("Memory address out of range: 0x%llx%s", addr, loc); } }; - template struct cast_ptr> + template + struct cast_impl> { - static u32 cast(const se_t& addr, const char* file, int line, const char* func) + static u32 cast(const se_t& addr, const char* loc) { - return cast_ptr::cast(addr, file, line, func); + return cast_impl::cast(addr, loc); } }; - template u32 impl_cast(const T& addr, const char* file, int line, const char* func) + template + u32 cast(const T& addr, const char* loc) { - return cast_ptr::cast(addr, file, line, func); + return cast_impl::cast(addr, loc); } // Convert specified PS3/PSV virtual memory address to a pointer for common access @@ -392,12 +402,10 @@ namespace vm } void close(); -} -#include "vm_ptr.h" + u32 stack_push(u32 size, u32 align_v); + void stack_pop_verbose(u32 addr, u32 size) noexcept; -namespace vm -{ class stack { u32 m_begin; @@ -418,21 +426,20 @@ namespace vm u32 alloc_new_page() { - assert(m_position + m_page_size < (int)m_size); + Expects(m_position + m_page_size < (int)m_size); m_position += (int)m_page_size; return m_begin + m_position; } u32 dealloc_new_page() { - assert(m_position - m_page_size > 0); + Expects(m_position - m_page_size > 0); m_position -= (int)m_page_size; return m_begin + m_position; } }; - u32 stack_push(u32 size, u32 align_v); - void stack_pop(u32 addr, u32 size); + extern thread_local u64 g_tls_fault_count; } #include "vm_var.h" diff --git a/rpcs3/Emu/Memory/vm_ptr.h b/rpcs3/Emu/Memory/vm_ptr.h index 9b853b4302..f43e9e0df8 100644 --- a/rpcs3/Emu/Memory/vm_ptr.h +++ b/rpcs3/Emu/Memory/vm_ptr.h @@ -7,14 +7,12 @@ class ARMv7Thread; namespace vm { - // helper SFINAE type for vm::_ptr_base comparison operators (enables comparison between equal types and between any type and void*) - template using if_comparable_t = std::enable_if_t< - std::is_void::value || - std::is_void::value || - std::is_same, std::remove_cv_t>::value, - RT>; + // SFINAE helper type for vm::_ptr_base comparison operators (enables comparison between equal types and between any type and void*) + template + using if_comparable_t = std::enable_if_t::value || std::is_void::value || std::is_same, std::remove_cv_t>::value, RT>; - template class _ptr_base + template + class _ptr_base { AT m_addr; @@ -37,84 +35,85 @@ namespace vm return m_addr; } - // get vm pointer to a struct member - template> _ptr_base ptr(MT T2::*const mptr) const + void set(addr_type addr) { - return{ VM_CAST(m_addr) + get_offset(mptr), vm::addr }; + this->m_addr = addr; } - // get vm pointer to a struct member with array subscription - template, typename = if_comparable_t> _ptr_base ptr(MT T2::*const mptr, u32 index) const - { - return{ VM_CAST(m_addr) + get_offset(mptr) + SIZE_32(ET) * index, vm::addr }; - } - - // get vm reference to a struct member - template> _ref_base ref(MT T2::*const mptr) const - { - return{ VM_CAST(m_addr) + get_offset(mptr), vm::addr }; - } - - // get vm reference to a struct member with array subscription - template, typename = if_comparable_t> _ref_base ref(MT T2::*const mptr, u32 index) const - { - return{ VM_CAST(m_addr) + get_offset(mptr) + SIZE_32(ET) * index, vm::addr }; - } - - // get vm reference - _ref_base ref() const - { - return{ VM_CAST(m_addr), vm::addr }; - } - - /*[[deprecated("Use constructor instead")]]*/ void set(addr_type value) - { - m_addr = value; - } - - /*[[deprecated("Use constructor instead")]]*/ static _ptr_base make(addr_type addr) + static constexpr _ptr_base make(addr_type addr) { return{ addr, vm::addr }; } + // Enable only the conversions which are originally possible between pointer types + template::value>> + operator _ptr_base() const + { + return{ vm::cast(m_addr, HERE), vm::addr }; + } + + explicit constexpr operator bool() const + { + return m_addr != 0; + } + + // Get vm pointer to a struct member + template> + _ptr_base ptr(MT T2::*const mptr) const + { + return{ vm::cast(m_addr, HERE) + get_offset(mptr), vm::addr }; + } + + // Get vm pointer to a struct member with array subscription + template, typename = if_comparable_t> + _ptr_base ptr(MT T2::*const mptr, u32 index) const + { + return{ vm::cast(m_addr, HERE) + get_offset(mptr) + SIZE_32(ET) * index, vm::addr }; + } + + // Get vm reference to a struct member + template> + _ref_base ref(MT T2::*const mptr) const + { + return{ vm::cast(m_addr, HERE) + get_offset(mptr), vm::addr }; + } + + // Get vm reference to a struct member with array subscription + template, typename = if_comparable_t> + _ref_base ref(MT T2::*const mptr, u32 index) const + { + return{ vm::cast(m_addr, HERE) + get_offset(mptr) + SIZE_32(ET) * index, vm::addr }; + } + + // Get vm reference + _ref_base ref() const + { + return{ vm::cast(m_addr, HERE), vm::addr }; + } + T* get_ptr() const { - return static_cast(vm::base(VM_CAST(m_addr))); + return static_cast(vm::base(vm::cast(m_addr, HERE))); } T* get_ptr_priv() const { - return static_cast(vm::base_priv(VM_CAST(m_addr))); + return static_cast(vm::base_priv(vm::cast(m_addr, HERE))); } T* operator ->() const { - static_assert(!std::is_void::value, "vm::_ptr_base<> error: operator-> is not available for void pointers"); - return get_ptr(); } + std::add_lvalue_reference_t operator *() const + { + return *static_cast(vm::base(vm::cast(m_addr, HERE))); + } + std::add_lvalue_reference_t operator [](u32 index) const { - static_assert(!std::is_void::value, "vm::_ptr_base<> error: operator[] is not available for void pointers"); - - return *static_cast(vm::base(VM_CAST(m_addr) + SIZE_32(T) * index)); - } - - // enable only the conversions which are originally possible between pointer types - template::value>> operator _ptr_base() const - { - return{ VM_CAST(m_addr), vm::addr }; - } - - //template::value>> explicit operator T2*() const - //{ - // return get_ptr(); - //} - - explicit operator bool() const - { - return m_addr != 0; + return *static_cast(vm::base(vm::cast(m_addr, HERE) + SIZE_32(T) * index)); } // Test address for arbitrary alignment: (addr & (align - 1)) == 0 @@ -126,8 +125,6 @@ namespace vm // Test address alignment using alignof(T) bool aligned() const { - static_assert(!std::is_void::value, "vm::_ptr_base<> error: aligned() is not available for void pointers"); - return aligned(ALIGN_32(T)); } @@ -137,62 +134,74 @@ namespace vm return !aligned(align); } - // pointer increment (postfix) + _ptr_base operator +() const + { + return{ vm::cast(m_addr, HERE), vm::addr }; + } + + _ptr_base operator +(u32 count) const + { + return{ vm::cast(m_addr, HERE) + count * SIZE_32(T), vm::addr }; + } + + _ptr_base operator -(u32 count) const + { + return{ vm::cast(m_addr, HERE) - count * SIZE_32(T), vm::addr }; + } + + friend _ptr_base operator +(u32 count, const _ptr_base& ptr) + { + return{ vm::cast(ptr.m_addr, HERE) + count * SIZE_32(T), vm::addr }; + } + + // Pointer difference operator + template + std::enable_if_t::value && std::is_same::value, s32> operator -(const _ptr_base& right) const + { + return static_cast(vm::cast(m_addr, HERE) - vm::cast(right.m_addr, HERE)) / SIZE_32(T); + } + _ptr_base operator ++(int) { - static_assert(!std::is_void::value, "vm::_ptr_base<> error: operator++ is not available for void pointers"); - const addr_type result = m_addr; - m_addr = VM_CAST(m_addr) + SIZE_32(T); + m_addr = vm::cast(m_addr, HERE) + SIZE_32(T); return{ result, vm::addr }; } - // pointer increment (prefix) _ptr_base& operator ++() { - static_assert(!std::is_void::value, "vm::_ptr_base<> error: operator++ is not available for void pointers"); - - m_addr = VM_CAST(m_addr) + SIZE_32(T); + m_addr = vm::cast(m_addr, HERE) + SIZE_32(T); return *this; } - // pointer decrement (postfix) _ptr_base operator --(int) { - static_assert(!std::is_void::value, "vm::_ptr_base<> error: operator-- is not available for void pointers"); - const addr_type result = m_addr; - m_addr = VM_CAST(m_addr) - SIZE_32(T); + m_addr = vm::cast(m_addr, HERE) - SIZE_32(T); return{ result, vm::addr }; } - // pointer decrement (prefix) _ptr_base& operator --() { - static_assert(!std::is_void::value, "vm::_ptr_base<> error: operator-- is not available for void pointers"); - - m_addr = VM_CAST(m_addr) - SIZE_32(T); + m_addr = vm::cast(m_addr, HERE) - SIZE_32(T); return *this; } _ptr_base& operator +=(s32 count) { - static_assert(!std::is_void::value, "vm::_ptr_base<> error: operator+= is not available for void pointers"); - - m_addr = VM_CAST(m_addr) + count * SIZE_32(T); + m_addr = vm::cast(m_addr, HERE) + count * SIZE_32(T); return *this; } _ptr_base& operator -=(s32 count) { - static_assert(!std::is_void::value, "vm::_ptr_base<> error: operator-= is not available for void pointers"); - - m_addr = VM_CAST(m_addr) - count * SIZE_32(T); + m_addr = vm::cast(m_addr, HERE) - count * SIZE_32(T); return *this; } }; - template class _ptr_base + template + class _ptr_base { AT m_addr; @@ -211,38 +220,43 @@ namespace vm return m_addr; } - /*[[deprecated("Use constructor instead")]]*/ void set(addr_type value) + void set(addr_type addr) { - m_addr = value; + m_addr = addr; } - /*[[deprecated("Use constructor instead")]]*/ static _ptr_base make(addr_type addr) + static constexpr _ptr_base make(addr_type addr) { return{ addr, vm::addr }; } - // defined in CB_FUNC.h, passing context is mandatory - RT operator()(PPUThread& CPU, T... args) const; - - // defined in ARMv7Callback.h, passing context is mandatory - RT operator()(ARMv7Thread& context, T... args) const; - - // conversion to another function pointer - template operator _ptr_base() const + // Conversion to another function pointer + template + operator _ptr_base() const { - return{ VM_CAST(m_addr), vm::addr }; + return{ vm::cast(m_addr, HERE), vm::addr }; } - explicit operator bool() const + explicit constexpr operator bool() const { return m_addr != 0; } + + _ptr_base operator +() const + { + return{ vm::cast(m_addr, HERE), vm::addr }; + } + + // Callback; defined in PPUCallback.h, passing context is mandatory + RT operator()(PPUThread& ppu, T... args) const; + + // Callback; defined in ARMv7Callback.h, passing context is mandatory + RT operator()(ARMv7Thread& cpu, T... args) const; }; - template class _ptr_base + template + class _ptr_base { - AT m_addr; - static_assert(!sizeof(AT), "vm::_ptr_base<> error: use RT(T...) format for functions instead of RT(*)(T...)"); }; @@ -266,19 +280,19 @@ namespace vm namespace ps3 { - // default pointer for PS3 HLE functions (Native endianness pointer to BE data) + // Default pointer type for PS3 HLE functions (Native endianness pointer to BE data) template using ptr = ptrb; - // default pointer to pointer for PS3 HLE functions (Native endianness pointer to BE pointer to BE data) + // Default pointer to pointer type for PS3 HLE functions (Native endianness pointer to BE pointer to BE data) template using pptr = ptr, AT>; - // default pointer for PS3 HLE structures (BE pointer to BE data) + // Default pointer type for PS3 HLE structures (BE pointer to BE data) template using bptr = bptrb; - // default pointer to pointer for PS3 HLE structures (BE pointer to BE pointer to BE data) + // Default pointer to pointer type for PS3 HLE structures (BE pointer to BE pointer to BE data) template using bpptr = bptr, AT>; - // native endianness pointer to const BE data + // Native endianness pointer to const BE data template using cptr = ptr; // BE pointer to const BE data @@ -287,34 +301,36 @@ namespace vm template using cpptr = pptr; template using bcpptr = bpptr; - // perform static_cast (for example, vm::ptr to vm::ptr) - template*>(std::declval()))> inline _ptr_base> static_ptr_cast(const _ptr_base& other) + // Perform static_cast (for example, vm::ptr to vm::ptr) + template*>(std::declval()))> + inline _ptr_base> static_ptr_cast(const _ptr_base& other) { - return{ VM_CAST(other.addr()), vm::addr }; + return{ vm::cast(other.addr(), HERE), vm::addr }; } - // perform const_cast (for example, vm::cptr to vm::ptr) - template*>(std::declval()))> inline _ptr_base> const_ptr_cast(const _ptr_base& other) + // Perform const_cast (for example, vm::cptr to vm::ptr) + template*>(std::declval()))> + inline _ptr_base> const_ptr_cast(const _ptr_base& other) { - return{ VM_CAST(other.addr()), vm::addr }; + return{ vm::cast(other.addr(), HERE), vm::addr }; } } namespace psv { - // default pointer for PSV HLE functions (Native endianness pointer to LE data) + // Default pointer type for PSV HLE functions (Native endianness pointer to LE data) template using ptr = ptrl; - // default pointer to pointer for PSV HLE functions (Native endianness pointer to LE pointer to LE data) + // Default pointer to pointer type for PSV HLE functions (Native endianness pointer to LE pointer to LE data) template using pptr = ptr>; - // default pointer for PSV HLE structures (LE pointer to LE data) + // Default pointer type for PSV HLE structures (LE pointer to LE data) template using lptr = lptrl; - // default pointer to pointer for PSV HLE structures (LE pointer to LE pointer to LE data) + // Default pointer to pointer type for PSV HLE structures (LE pointer to LE pointer to LE data) template using lpptr = lptr>; - // native endianness pointer to const LE data + // Native endianness pointer to const LE data template using cptr = ptr; // LE pointer to const LE data @@ -323,198 +339,168 @@ namespace vm template using cpptr = pptr; template using lcpptr = lpptr; - // perform static_cast (for example, vm::ptr to vm::ptr) - template*>(std::declval()))> inline _ptr_base> static_ptr_cast(const _ptr_base& other) + // Perform static_cast (for example, vm::ptr to vm::ptr) + template*>(std::declval()))> + inline _ptr_base> static_ptr_cast(const _ptr_base& other) { - return{ VM_CAST(other.addr()), vm::addr }; + return{ vm::cast(other.addr(), HERE), vm::addr }; } - // perform const_cast (for example, vm::cptr to vm::ptr) - template*>(std::declval()))> inline _ptr_base> const_ptr_cast(const _ptr_base& other) + // Perform const_cast (for example, vm::cptr to vm::ptr) + template*>(std::declval()))> + inline _ptr_base> const_ptr_cast(const _ptr_base& other) { - return{ VM_CAST(other.addr()), vm::addr }; + return{ vm::cast(other.addr(), HERE), vm::addr }; } } struct null_t { - template operator _ptr_base() const + template + constexpr operator _ptr_base() const { - return{ 0, vm::addr }; + return _ptr_base{ 0, vm::addr }; + } + + template + friend constexpr bool operator ==(const null_t&, const _ptr_base& ptr) + { + return !ptr; + } + + template + friend constexpr bool operator ==(const _ptr_base& ptr, const null_t&) + { + return !ptr; + } + + template + friend constexpr bool operator !=(const null_t&, const _ptr_base& ptr) + { + return ptr.operator bool(); + } + + template + friend constexpr bool operator !=(const _ptr_base& ptr, const null_t&) + { + return ptr.operator bool(); + } + + template + friend constexpr bool operator <(const null_t&, const _ptr_base& ptr) + { + return ptr.operator bool(); + } + + template + friend constexpr bool operator <(const _ptr_base&, const null_t&) + { + return false; + } + + template + friend constexpr bool operator <=(const null_t&, const _ptr_base&) + { + return true; + } + + template + friend constexpr bool operator <=(const _ptr_base& ptr, const null_t&) + { + return !ptr.operator bool(); + } + + template + friend constexpr bool operator >(const null_t&, const _ptr_base&) + { + return false; + } + + template + friend constexpr bool operator >(const _ptr_base& ptr, const null_t&) + { + return ptr.operator bool(); + } + + template + friend constexpr bool operator >=(const null_t&, const _ptr_base& ptr) + { + return !ptr; + } + + template + friend constexpr bool operator >=(const _ptr_base&, const null_t&) + { + return true; } }; - // vm::null is convertible to any vm::ptr type as null pointer in virtual memory + // Null pointer convertible to any vm::ptr* type static null_t null; // Call wait_op() for specified vm pointer - template inline auto wait_op(named_thread_t& thread, const _ptr_base& ptr, F pred, Args&&... args) -> decltype(static_cast(pred(args...))) + template + static inline auto wait_op(named_thread& thread, const _ptr_base& ptr, F pred, Args&&... args) -> decltype(static_cast(pred(args...))) { return wait_op(thread, ptr.addr(), SIZE_32(T), std::move(pred), std::forward(args)...); } // Call notify_at() for specified vm pointer - template inline void notify_at(const vm::_ptr_base& ptr) + template + inline void notify_at(const vm::_ptr_base& ptr) { return notify_at(ptr.addr(), SIZE_32(T)); } } -// unary plus operator for vm::_ptr_base (always available) -template inline vm::_ptr_base operator +(const vm::_ptr_base& ptr) -{ - return ptr; -} - -// indirection operator for vm::_ptr_base -template inline std::enable_if_t::value, T&> operator *(const vm::_ptr_base& ptr) -{ - return *ptr.get_ptr(); -} - -// addition operator for vm::_ptr_base (pointer + integer) -template inline std::enable_if_t::value, vm::_ptr_base> operator +(const vm::_ptr_base& ptr, u32 count) -{ - return{ VM_CAST(ptr.addr()) + count * SIZE_32(T), vm::addr }; -} - -// addition operator for vm::_ptr_base (integer + pointer) -template inline std::enable_if_t::value, vm::_ptr_base> operator +(u32 count, const vm::_ptr_base& ptr) -{ - return{ VM_CAST(ptr.addr()) + count * SIZE_32(T), vm::addr }; -} - -// subtraction operator for vm::_ptr_base (pointer - integer) -template inline std::enable_if_t::value, vm::_ptr_base> operator -(const vm::_ptr_base& ptr, u32 count) -{ - return{ VM_CAST(ptr.addr()) - count * SIZE_32(T), vm::addr }; -} - -// pointer difference operator for vm::_ptr_base -template inline std::enable_if_t< - std::is_object::value && - std::is_object::value && - std::is_same, std::remove_cv_t>::value, - s32> operator -(const vm::_ptr_base& left, const vm::_ptr_base& right) -{ - return static_cast(VM_CAST(left.addr()) - VM_CAST(right.addr())) / SIZE_32(T1); -} - -// comparison operator for vm::_ptr_base (pointer1 == pointer2) -template inline vm::if_comparable_t operator ==(const vm::_ptr_base& left, const vm::_ptr_base& right) +template +inline vm::if_comparable_t operator ==(const vm::_ptr_base& left, const vm::_ptr_base& right) { return left.addr() == right.addr(); } -template inline bool operator ==(const vm::null_t&, const vm::_ptr_base& ptr) -{ - return !ptr.operator bool(); -} - -template inline bool operator ==(const vm::_ptr_base& ptr, const vm::null_t&) -{ - return !ptr.operator bool(); -} - -// comparison operator for vm::_ptr_base (pointer1 != pointer2) -template inline vm::if_comparable_t operator !=(const vm::_ptr_base& left, const vm::_ptr_base& right) +template +inline vm::if_comparable_t operator !=(const vm::_ptr_base& left, const vm::_ptr_base& right) { return left.addr() != right.addr(); } -template inline bool operator !=(const vm::null_t&, const vm::_ptr_base& ptr) -{ - return ptr.operator bool(); -} - -template inline bool operator !=(const vm::_ptr_base& ptr, const vm::null_t&) -{ - return ptr.operator bool(); -} - -// comparison operator for vm::_ptr_base (pointer1 < pointer2) -template inline vm::if_comparable_t operator <(const vm::_ptr_base& left, const vm::_ptr_base& right) +template +inline vm::if_comparable_t operator <(const vm::_ptr_base& left, const vm::_ptr_base& right) { return left.addr() < right.addr(); } -template inline bool operator <(const vm::null_t&, const vm::_ptr_base& ptr) -{ - return ptr.operator bool(); -} - -template inline bool operator <(const vm::_ptr_base&, const vm::null_t&) -{ - return false; -} - -// comparison operator for vm::_ptr_base (pointer1 <= pointer2) -template inline vm::if_comparable_t operator <=(const vm::_ptr_base& left, const vm::_ptr_base& right) +template +inline vm::if_comparable_t operator <=(const vm::_ptr_base& left, const vm::_ptr_base& right) { return left.addr() <= right.addr(); } -template inline bool operator <=(const vm::null_t&, const vm::_ptr_base&) -{ - return true; -} - -template inline bool operator <=(const vm::_ptr_base& ptr, const vm::null_t&) -{ - return !ptr.operator bool(); -} - -// comparison operator for vm::_ptr_base (pointer1 > pointer2) -template inline vm::if_comparable_t operator >(const vm::_ptr_base& left, const vm::_ptr_base& right) +template +inline vm::if_comparable_t operator >(const vm::_ptr_base& left, const vm::_ptr_base& right) { return left.addr() > right.addr(); } -template inline bool operator >(const vm::null_t&, const vm::_ptr_base&) -{ - return false; -} - -template inline bool operator >(const vm::_ptr_base& ptr, const vm::null_t&) -{ - return ptr.operator bool(); -} - -// comparison operator for vm::_ptr_base (pointer1 >= pointer2) -template inline vm::if_comparable_t operator >=(const vm::_ptr_base& left, const vm::_ptr_base& right) +template +inline vm::if_comparable_t operator >=(const vm::_ptr_base& left, const vm::_ptr_base& right) { return left.addr() >= right.addr(); } -template inline bool operator >=(const vm::null_t&, const vm::_ptr_base& ptr) -{ - return !ptr.operator bool(); -} - -template inline bool operator >=(const vm::_ptr_base&, const vm::null_t&) -{ - return true; -} - -// external specialization for to_se<> (change AT endianness to BE/LE) - -template struct to_se, Se> +// Change AT endianness to BE/LE +template +struct to_se, Se> { using type = vm::_ptr_base::type>; }; -// external specialization for to_ne<> (change AT endianness to native) - -template struct to_ne> -{ - using type = vm::_ptr_base::type>; -}; - namespace fmt { - // external specialization for fmt::format function - - template struct unveil, false> + // Format pointer + template + struct unveil, void> { using result_type = typename unveil::result_type; @@ -524,37 +510,3 @@ namespace fmt } }; } - -// external specializations for PPU GPR (SC_FUNC.h, CB_FUNC.h) - -template struct cast_ppu_gpr; - -template struct cast_ppu_gpr, false> -{ - static inline u64 to_gpr(const vm::_ptr_base& value) - { - return cast_ppu_gpr::value>::to_gpr(value.addr()); - } - - static inline vm::_ptr_base from_gpr(const u64 reg) - { - return{ cast_ppu_gpr::value>::from_gpr(reg), vm::addr }; - } -}; - -// external specializations for ARMv7 GPR - -template struct cast_armv7_gpr; - -template struct cast_armv7_gpr, false> -{ - static inline u32 to_gpr(const vm::_ptr_base& value) - { - return cast_armv7_gpr::value>::to_gpr(value.addr()); - } - - static inline vm::_ptr_base from_gpr(const u32 reg) - { - return{ cast_armv7_gpr::value>::from_gpr(reg), vm::addr }; - } -}; diff --git a/rpcs3/Emu/Memory/vm_ref.h b/rpcs3/Emu/Memory/vm_ref.h index 2c8f3e1fb2..9b5d1bcc23 100644 --- a/rpcs3/Emu/Memory/vm_ref.h +++ b/rpcs3/Emu/Memory/vm_ref.h @@ -5,9 +5,11 @@ namespace vm // Tag which allows to construct vm objects from the address value static struct addr_tag_t {} constexpr addr{}; - template class _ptr_base; + template + class _ptr_base; - template class _ref_base + template + class _ref_base { AT m_addr; @@ -36,16 +38,16 @@ namespace vm T& get_ref() const { - return *static_cast(vm::base(VM_CAST(m_addr))); + return *static_cast(vm::base(vm::cast(m_addr, HERE))); } // convert to vm pointer vm::_ptr_base ptr() const { - return{ VM_CAST(m_addr), vm::addr }; + return{ vm::cast(m_addr, HERE), vm::addr }; } - operator to_ne_t() const + operator simple_t() const { return get_ref(); } @@ -60,7 +62,7 @@ namespace vm return get_ref() = right.get_ref(); } - T& operator =(const T& right) const + T& operator =(const simple_t& right) const { return get_ref() = right; } @@ -85,52 +87,62 @@ namespace vm return --get_ref(); } - template decltype(auto) operator +=(const T2& right) + template + decltype(auto) operator +=(const T2& right) { return get_ref() += right; } - template decltype(auto) operator -=(const T2& right) + template + decltype(auto) operator -=(const T2& right) { return get_ref() -= right; } - template decltype(auto) operator *=(const T2& right) + template + decltype(auto) operator *=(const T2& right) { return get_ref() *= right; } - template decltype(auto) operator /=(const T2& right) + template + decltype(auto) operator /=(const T2& right) { return get_ref() /= right; } - template decltype(auto) operator %=(const T2& right) + template + decltype(auto) operator %=(const T2& right) { return get_ref() %= right; } - template decltype(auto) operator &=(const T2& right) + template + decltype(auto) operator &=(const T2& right) { return get_ref() &= right; } - template decltype(auto) operator |=(const T2& right) + template + decltype(auto) operator |=(const T2& right) { return get_ref() |= right; } - template decltype(auto) operator ^=(const T2& right) + template + decltype(auto) operator ^=(const T2& right) { return get_ref() ^= right; } - template decltype(auto) operator <<=(const T2& right) + template + decltype(auto) operator <<=(const T2& right) { return get_ref() <<= right; } - template decltype(auto) operator >>=(const T2& right) + template + decltype(auto) operator >>=(const T2& right) { return get_ref() >>= right; } @@ -173,65 +185,19 @@ namespace vm } } -// external specialization for to_se<> (change AT's endianness to BE/LE) - -template struct to_se, Se> +// Change AT endianness to BE/LE +template +struct to_se, Se> { using type = vm::_ref_base::type>; }; -// external specialization for to_ne<> (change AT's endianness to native) - -template struct to_ne> -{ - using type = vm::_ref_base::type>; -}; - namespace fmt { - // external specialization for fmt::format function - - template struct unveil, false> + // Forbid formatting + template + struct unveil, void> { - using result_type = typename unveil::result_type; - - static inline result_type get_value(const vm::_ref_base& arg) - { - return unveil::get_value(arg.addr()); - } + static_assert(!sizeof(T), "vm::_ref_base<>: ambiguous format argument"); }; } - -// external specializations for PPU GPR (SC_FUNC.h, CB_FUNC.h) - -template struct cast_ppu_gpr; - -template struct cast_ppu_gpr, false> -{ - static inline u64 to_gpr(const vm::_ref_base& value) - { - return cast_ppu_gpr::value>::to_gpr(value.addr()); - } - - static inline vm::_ref_base from_gpr(const u64 reg) - { - return{ cast_ppu_gpr::value>::from_gpr(reg), vm::addr }; - } -}; - -// external specializations for ARMv7 GPR - -template struct cast_armv7_gpr; - -template struct cast_armv7_gpr, false> -{ - static inline u32 to_gpr(const vm::_ref_base& value) - { - return cast_armv7_gpr::value>::to_gpr(value.addr()); - } - - static inline vm::_ref_base from_gpr(const u32 reg) - { - return{ cast_armv7_gpr::value>::from_gpr(reg), vm::addr }; - } -}; diff --git a/rpcs3/Emu/Memory/vm_var.h b/rpcs3/Emu/Memory/vm_var.h index 7cfa8e414e..4420d164b9 100644 --- a/rpcs3/Emu/Memory/vm_var.h +++ b/rpcs3/Emu/Memory/vm_var.h @@ -1,268 +1,150 @@ #pragma once +#include "vm_ptr.h" + namespace vm { - template class page_alloc_t + template + struct page_allocator { - u32 m_addr; - - public: static inline u32 alloc(u32 size, u32 align) { return vm::alloc(size, Location, std::max(align, 4096)); } - static inline void dealloc(u32 addr, u32 size) noexcept + static inline void dealloc(u32 addr, u32 size = 0) noexcept { return vm::dealloc_verbose_nothrow(addr, Location); } - - page_alloc_t() - : m_addr(0) - { - } - - page_alloc_t(u32 size, u32 align) - : m_addr(alloc(size, align)) - { - } - - page_alloc_t(page_alloc_t&& other) - : m_addr(other.m_addr) - { - other.m_addr = 0; - } - - ~page_alloc_t() - { - if (m_addr) - { - dealloc(m_addr, 0); - } - } - - page_alloc_t& operator =(page_alloc_t&& other) - { - std::swap(m_addr, other.m_addr); - - return *this; - } - - u32 get_addr() const - { - return m_addr; - } }; - class stack_alloc_t + struct stack_allocator { - u32 m_addr; - u32 m_size; - - public: static inline u32 alloc(u32 size, u32 align) { return vm::stack_push(size, align); } - static inline void dealloc(u32 addr, u32 size) + static inline void dealloc(u32 addr, u32 size) noexcept { - if (!std::uncaught_exception()) // Don't call during stack unwinding - { - vm::stack_pop(addr, size); - } - } - - stack_alloc_t(u32 size, u32 align) - : m_addr(alloc(size, align)) - , m_size(size) - { - } - - ~stack_alloc_t() noexcept(false) // Allow exceptions - { - dealloc(m_addr, m_size); - } - - stack_alloc_t(const stack_alloc_t&) = delete; // Delete copy/move constructors and copy/move operators - - u32 get_addr() const - { - return m_addr; + vm::stack_pop_verbose(addr, size); } }; - // _var_base prototype (T - data type, A - allocation traits) - template class _var_base; - - // _var_base general specialization (single object of type T) - template class _var_base final : public _ptr_base + // Variable general specialization + template + class _var_base final : public _ptr_base { using pointer = _ptr_base; public: - // Call the constructor with specified arguments - template::value>> _var_base(Args&&... args) + _var_base() : pointer(A::alloc(SIZE_32(T), ALIGN_32(T)), vm::addr) { -#include "restore_new.h" - new(pointer::get_ptr()) T(std::forward(args)...); -#include "define_new_memleakdetect.h" } - _var_base(const _var_base&) = delete; // Delete copy/move constructors and copy/move operators - - ~_var_base() noexcept(noexcept(std::declval().~T()) && noexcept(A::dealloc(0, 0))) + _var_base(const T& right) + : _var_base() { - // Call the destructor - pointer::get_ptr()->~T(); - - // Deallocate memory - A::dealloc(pointer::addr(), SIZE_32(T)); + std::memcpy(pointer::get_ptr(), &right, sizeof(T)); } - // Remove operator [] - std::add_lvalue_reference_t operator [](u32 index) const = delete; + _var_base(_var_base&& right) + : pointer(right) + { + reinterpret_cast(static_cast(right)) = 0; + } + + ~_var_base() + { + if (pointer::addr()) A::dealloc(pointer::addr(), SIZE_32(T)); + } }; - // _var_base unknown length array specialization - template class _var_base final : public _ptr_base + // Dynamic length array variable + template + class _var_base final : public _ptr_base { using pointer = _ptr_base; - u32 m_count; + u32 m_size; public: - // Call the default constructor for each element _var_base(u32 count) : pointer(A::alloc(SIZE_32(T) * count, ALIGN_32(T)), vm::addr) - , m_count(count) + , m_size(SIZE_32(T) * count) { -#include "restore_new.h" - new(pointer::get_ptr()) T[count](); -#include "define_new_memleakdetect.h" } - // Call the constructor for each element using [it, it + count) - template _var_base(u32 count, T2 it) - : pointer(A::alloc(SIZE_32(T) * count, ALIGN_32(T)), vm::addr) - , m_count(count) + _var_base(_var_base&& right) + : pointer(right) { -#include "restore_new.h" - for (u32 i = 0; i < m_count; i++, it++) new(pointer::get_ptr() + i) T(*it); -#include "define_new_memleakdetect.h" + reinterpret_cast(static_cast(right)) = 0; } - _var_base(const _var_base&) = delete; // Delete copy/move constructors and copy/move operators - - ~_var_base() noexcept(noexcept(std::declval().~T()) && noexcept(A::dealloc(0, 0))) + ~_var_base() { - // Call the destructor for each element - for (u32 i = m_count - 1; ~i; i--) pointer::operator [](i).~T(); - - // Deallocate memory - A::dealloc(pointer::addr(), SIZE_32(T) * m_count); + if (pointer::addr()) A::dealloc(pointer::addr(), m_size); } + // Remove operator -> + T* operator ->() const = delete; + u32 get_count() const { - return m_count; + return m_size / SIZE_32(T); } - - std::add_lvalue_reference_t at(u32 index) const - { - if (index >= m_count) throw EXCEPTION("Out of range (0x%x >= 0x%x)", index, m_count); - - return pointer::operator [](index); - } - - // Remove operator -> - T* operator ->() const = delete; - }; - - // _var_base fixed length array specialization - template class _var_base final : public _ptr_base - { - using pointer = _ptr_base; - - public: - // Call the default constructor for each element - _var_base() - : pointer(A::alloc(SIZE_32(T) * N, ALIGN_32(T)), vm::addr) - { -#include "restore_new.h" - new(pointer::get_ptr()) T[N](); -#include "define_new_memleakdetect.h" - } - - // Call the constructor for each element using array - template _var_base(const T2(&array)[N]) - : pointer(A::alloc(SIZE_32(T) * N, ALIGN_32(T)), vm::addr) - { -#include "restore_new.h" - for (u32 i = 0; i < N; i++) new(pointer::get_ptr() + i) T(array[i]); -#include "define_new_memleakdetect.h" - } - - _var_base(const _var_base&) = delete; // Delete copy/move constructors and copy/move operators - - ~_var_base() noexcept(noexcept(std::declval().~T()) && noexcept(A::dealloc(0, 0))) - { - // Call the destructor for each element - for (u32 i = N - 1; ~i; i--) pointer::operator [](i).~T(); - - // Deallocate memory - A::dealloc(pointer::addr(), SIZE_32(T) * N); - } - - constexpr u32 get_count() const - { - return N; - } - - std::add_lvalue_reference_t at(u32 index) const - { - if (index >= N) throw EXCEPTION("Out of range (0x%x > 0x%x)", index, N); - - return pointer::operator [](index); - } - - // Remove operator -> - T* operator ->() const = delete; }; // LE variable - template using varl = _var_base, A>; + template using varl = _var_base, A>; // BE variable - template using varb = _var_base, A>; + template using varb = _var_base, A>; namespace ps3 { // BE variable - template using var = varb; + template using var = varb; - // BE variable initialized from value - template inline varb make_var(const T& value) + // Make BE variable initialized from value + template inline auto make_var(const T& value) { - return{ value }; + varb var(value); + return var; } + + // Global HLE variable + template + struct gvar : ptr + { + }; } namespace psv { // LE variable - template using var = varl; + template using var = varl; - // LE variable initialized from value - template inline varl make_var(const T& value) + // Make LE variable initialized from value + template inline auto make_var(const T& value) { - return{ value }; + varl var(value); + return var; } + + // Global HLE variable + template + struct gvar : ptr + { + }; } - static _var_base make_str(const std::string& str) + // Make char[] variable initialized from std::string + static auto make_str(const std::string& str) { - return{ size32(str) + 1, str.data() }; + _var_base var(size32(str) + 1); + std::memcpy(var.get_ptr(), str.c_str(), str.size() + 1); + return var; } }