Files
parallel-gx1/Source/Core/Common/MemoryUtil.cpp
T
LibretroAdmin f7d5095739 Core, InputCommon, VideoCommon: remove the remaining locks
Every std::mutex, lock_guard, unique_lock, scoped_lock,
condition_variable, call_once and semaphore left in Source/Core is
replaced by a lock-free design:

- Input: the device list, control bindings, stick calibration and
  default device are published snapshots read under a quiesce; a
  rebind requested while another thread is rebinding is handed to
  that thread instead of waited for.
- WiimoteReal: the pool thread owns the slots and runs commands from
  an MPSC queue; the active Wiimotes are atomic slots.
- EXI/SI/IOS devices: socket and packet traffic goes through SPSC
  rings or MPSC handoffs owned by one thread; USB scanning publishes
  its device list, and libusb transfers drain through a CallGate.
- Memory cards: the flush thread applies change records queued by
  the CPU thread.
- Custom assets: loads are scheduled as a published batch with an
  atomic cursor, results come back over an MPSC stack, and busy
  assets are skipped and retried.
- D3D state and GL program caches use insert-only atomic maps
  (Common/AtomicHashMap.h).
- Logging appends with O_APPEND / FILE_APPEND_DATA; FatFs reports
  busy instead of waiting; per-socket WSAStartup/WSACleanup.

Deleted as unused by the libretro core: the profiler, the FIFO
recorder, the volume verifier, the touch InputOverrider and the
GameCube adapter's libusb path.
2026-11-05 20:11:31 +01:00

446 lines
12 KiB
C++

// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Common/MemoryUtil.h"
#include <cstddef>
#include <cstdlib>
#include <retro_atomic.h>
#include "Common/CommonFuncs.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
#ifdef _WIN32
#include <windows.h>
#include "Common/StringUtil.h"
#else
#include <stdio.h>
#include <sys/mman.h>
#if defined(_M_ARM_64) && defined(__APPLE__)
#include <pthread.h>
#endif
#if defined(__APPLE__) && defined(__aarch64__)
#include <mach/mach.h>
#endif
#if defined __APPLE__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
#include <sys/sysctl.h>
#elif defined __HAIKU__
#include <OS.h>
#else
#include <sys/sysinfo.h>
#endif
#endif
#ifdef IPHONEOS
#include "Common/JITMemoryTracker.h"
#ifdef __LIBRETRO__
#include <algorithm>
#include <vector>
#include "DolphinLibretro/Common/Globals.h"
static void* s_last_exec_rw = nullptr;
static unsigned s_last_exec_mode = 0;
// Regions the frontend allocated for us. Whether they are dual-mapped says
// nothing about who owns them, so ownership is recorded rather than inferred.
// Slots claimed and released with a CAS; a handful of regions at most.
static retro_atomic_ptr_t s_frontend_regions[64];
#endif
#endif
#ifdef IPHONEOS
namespace Common
{
static JitType g_jit_type = JitType::Legacy;
void SetJitType(JitType type)
{
g_jit_type = type;
}
} // namespace Common
#endif
namespace Common
{
// This is purposely not a full wrapper for virtualalloc/mmap, but it
// provides exactly the primitive operations that Dolphin needs.
#ifdef IPHONEOS
static JITMemoryTracker g_jit_memory_tracker;
#endif
void* AllocateExecutableMemory(size_t size)
{
#if defined(IPHONEOS) && defined(__LIBRETRO__)
{
struct retro_exec_mem_alloc alloc = {};
alloc.version = 1;
alloc.size = size;
if (Libretro::environ_cb &&
Libretro::environ_cb(RETRO_ENVIRONMENT_EXEC_MEM_ALLOC, &alloc) &&
alloc.mode != RETRO_EXEC_MEM_MODE_UNAVAILABLE &&
alloc.rx != nullptr)
{
s_last_exec_rw = alloc.rw;
s_last_exec_mode = alloc.mode;
SetJitType(alloc.mode == RETRO_EXEC_MEM_MODE_DUAL_MAP ? JitType::LuckTXM :
JitType::Legacy);
for (auto& slot : s_frontend_regions)
{
if (!retro_atomic_load_acquire_ptr(&slot) && retro_atomic_cas_ptr(&slot, nullptr, alloc.rx))
break;
}
g_jit_memory_tracker.RegisterJITRegion(alloc.rx, size);
return alloc.rx;
}
}
#endif
#if defined(_WIN32)
void* ptr = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
#else
int map_flags = MAP_ANON | MAP_PRIVATE;
#if defined(__APPLE__) && !defined(IPHONEOS)
map_flags |= MAP_JIT;
#endif
int map_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
void* ptr = mmap(nullptr, size, map_prot, map_flags, -1, 0);
if (ptr == MAP_FAILED)
ptr = nullptr;
#endif
if (ptr == nullptr)
PanicAlertFmt("Failed to allocate executable memory: {}", LastStrerrorString());
#ifdef IPHONEOS
g_jit_memory_tracker.RegisterJITRegion(ptr, size);
#endif
return ptr;
}
#ifndef IPHONEOS
// This function is used to provide a counter for the JITPageWrite*Execute*
// functions to enable nesting. The static variable is wrapped in a a function
// to allow those functions to be called inside of the constructor of a static
// variable portably.
//
// The variable is thread_local as the W^X mode is specific to each running thread.
static int& JITPageWriteNestCounter()
{
static thread_local int nest_counter = 0;
return nest_counter;
}
// Certain platforms (Mac OS on ARM) enforce that a single thread can only have write or
// execute permissions to pages at any given point of time. The two below functions
// are used to toggle between having write permissions or execute permissions.
//
// The default state of these allocations in Dolphin is for them to be executable,
// but not writeable. So, functions that are updating these pages should wrap their
// writes like below:
// JITPageWriteEnableExecuteDisable();
// PrepareInstructionStreamForJIT();
// JITPageWriteDisableExecuteEnable();
// These functions can be nested, in which case execution will only be enabled
// after the call to the JITPageWriteDisableExecuteEnable from the top most
// nesting level. Example:
// [JIT page is in execute mode for the thread]
// JITPageWriteEnableExecuteDisable();
// [JIT page is in write mode for the thread]
// JITPageWriteEnableExecuteDisable();
// [JIT page is in write mode for the thread]
// JITPageWriteDisableExecuteEnable();
// [JIT page is in write mode for the thread]
// JITPageWriteDisableExecuteEnable();
// [JIT page is in execute mode for the thread]
// Allows a thread to write to executable memory, but not execute the data.
void JITPageWriteEnableExecuteDisable()
{
#if defined(_M_ARM_64) && defined(__APPLE__) && !defined(IPHONEOS)
if (JITPageWriteNestCounter() == 0)
{
pthread_jit_write_protect_np(0);
}
#endif
JITPageWriteNestCounter()++;
}
// Allows a thread to execute memory allocated for execution, but not write to it.
void JITPageWriteDisableExecuteEnable()
{
JITPageWriteNestCounter()--;
// Sanity check the NestCounter to identify underflow
// This can indicate the calls to JITPageWriteDisableExecuteEnable()
// are not matched with previous calls to JITPageWriteEnableExecuteDisable()
if (JITPageWriteNestCounter() < 0)
PanicAlertFmt("JITPageWriteNestCounter() underflowed");
#if defined(_M_ARM_64) && defined(__APPLE__) && !defined(IPHONEOS)
if (JITPageWriteNestCounter() == 0)
{
pthread_jit_write_protect_np(1);
}
#endif
}
#else
void JITPageWriteEnableExecuteDisable(void* ptr)
{
if (g_jit_type == JitType::Legacy)
g_jit_memory_tracker.JITRegionWriteEnableExecuteDisable(ptr);
}
void JITPageWriteDisableExecuteEnable(void* ptr)
{
if (g_jit_type == JitType::Legacy)
g_jit_memory_tracker.JITRegionWriteDisableExecuteEnable(ptr);
}
#endif
void* AllocateMemoryPages(size_t size)
{
#ifdef _WIN32
void* ptr = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE);
#else
void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (ptr == MAP_FAILED)
ptr = nullptr;
#endif
if (ptr == nullptr)
PanicAlertFmt("Failed to allocate raw memory");
return ptr;
}
void* AllocateAlignedMemory(size_t size, size_t alignment)
{
#ifdef _WIN32
void* ptr = _aligned_malloc(size, alignment);
#else
void* ptr = nullptr;
if (posix_memalign(&ptr, alignment, size) != 0)
ERROR_LOG_FMT(MEMMAP, "Failed to allocate aligned memory");
#endif
if (ptr == nullptr)
PanicAlertFmt("Failed to allocate aligned memory");
return ptr;
}
bool FreeMemoryPages(void* ptr, size_t size)
{
if (ptr)
{
#ifdef _WIN32
if (!VirtualFree(ptr, 0, MEM_RELEASE))
{
PanicAlertFmt("FreeMemoryPages failed!\nVirtualFree: {}", GetLastErrorString());
return false;
}
#else
if (munmap(ptr, size) != 0)
{
PanicAlertFmt("FreeMemoryPages failed!\nmunmap: {}", LastStrerrorString());
return false;
}
#endif
#ifdef IPHONEOS
g_jit_memory_tracker.UnregisterJITRegion(ptr);
#endif
}
return true;
}
void FreeAlignedMemory(void* ptr)
{
if (ptr)
{
#ifdef _WIN32
_aligned_free(ptr);
#else
free(ptr);
#endif
}
}
bool ReadProtectMemory(void* ptr, size_t size)
{
#ifdef _WIN32
DWORD oldValue;
if (!VirtualProtect(ptr, size, PAGE_NOACCESS, &oldValue))
{
PanicAlertFmt("ReadProtectMemory failed!\nVirtualProtect: {}", GetLastErrorString());
return false;
}
#else
if (mprotect(ptr, size, PROT_NONE) != 0)
{
PanicAlertFmt("ReadProtectMemory failed!\nmprotect: {}", LastStrerrorString());
return false;
}
#endif
return true;
}
bool WriteProtectMemory(void* ptr, size_t size, bool allowExecute)
{
#ifdef _WIN32
DWORD oldValue;
if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue))
{
PanicAlertFmt("WriteProtectMemory failed!\nVirtualProtect: {}", GetLastErrorString());
return false;
}
#elif !(defined(_M_ARM_64) && defined(__APPLE__) && !defined(IPHONEOS))
// MacOS 11.2 on ARM does not allow for changing the access permissions of pages
// that were marked executable, instead it uses the protections offered by MAP_JIT
// for write protection.
if (mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ) != 0)
{
PanicAlertFmt("WriteProtectMemory failed!\nmprotect: {}", LastStrerrorString());
return false;
}
#endif
return true;
}
bool UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute)
{
#ifdef _WIN32
DWORD oldValue;
if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue))
{
PanicAlertFmt("UnWriteProtectMemory failed!\nVirtualProtect: {}", GetLastErrorString());
return false;
}
#elif !(defined(_M_ARM_64) && defined(__APPLE__) && !defined(IPHONEOS))
// MacOS 11.2 on ARM does not allow for changing the access permissions of pages
// that were marked executable, instead it uses the protections offered by MAP_JIT
// for write protection.
if (mprotect(ptr, size,
allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ) != 0)
{
PanicAlertFmt("UnWriteProtectMemory failed!\nmprotect: {}", LastStrerrorString());
return false;
}
#endif
return true;
}
size_t MemPhysical()
{
#ifdef _WIN32
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
return memInfo.ullTotalPhys;
#elif defined __APPLE__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
int mib[2];
size_t physical_memory;
mib[0] = CTL_HW;
#ifdef __APPLE__
mib[1] = HW_MEMSIZE;
#elif defined __FreeBSD__
mib[1] = HW_REALMEM;
#elif defined __OpenBSD__ || defined __NetBSD__
mib[1] = HW_PHYSMEM64;
#endif
size_t length = sizeof(size_t);
sysctl(mib, 2, &physical_memory, &length, nullptr, 0);
return physical_memory;
#elif defined __HAIKU__
system_info sysinfo;
get_system_info(&sysinfo);
return static_cast<size_t>(sysinfo.max_pages * B_PAGE_SIZE);
#else
struct sysinfo memInfo;
sysinfo(&memInfo);
return (size_t)memInfo.totalram * memInfo.mem_unit;
#endif
}
#if defined(__APPLE__) && defined(__aarch64__)
ptrdiff_t AllocateWritableRegionAndGetDiff(void* rx_ptr, size_t size)
{
#if defined(IPHONEOS) && defined(__LIBRETRO__)
if (s_last_exec_rw && s_last_exec_mode == RETRO_EXEC_MEM_MODE_DUAL_MAP)
{
ptrdiff_t diff = static_cast<u8*>(s_last_exec_rw) - static_cast<u8*>(rx_ptr);
s_last_exec_rw = nullptr;
return diff;
}
return 0;
#endif
vm_address_t rw_region = 0;
vm_prot_t cur_protection = 0;
vm_prot_t max_protection = 0;
kern_return_t retval =
vm_remap(mach_task_self(), &rw_region, size, 0, true, mach_task_self(),
(vm_address_t)rx_ptr, false, &cur_protection, &max_protection,
VM_INHERIT_DEFAULT);
if (retval != KERN_SUCCESS)
{
PanicAlertFmt("AllocateWritableRegionAndGetDiff failed! vm_remap returned {0:#x}", retval);
return 0;
}
u8* rw_ptr = reinterpret_cast<u8*>(rw_region);
if (mprotect(rw_ptr, size, PROT_READ | PROT_WRITE) != 0)
{
PanicAlertFmt("AllocateWritableRegionAndGetDiff failed! mprotect returned {}",
LastStrerrorString());
return 0;
}
return rw_ptr - static_cast<u8*>(rx_ptr);
}
void FreeWritableRegion(void* rx_ptr, size_t size, ptrdiff_t diff)
{
#if defined(IPHONEOS) && defined(__LIBRETRO__)
// Frontend owns the writable region; nothing to do here.
// FreeExecutableMemory will tell the frontend to free everything.
#else
if (diff == 0)
return;
u8* rw_ptr = static_cast<u8*>(rx_ptr) + diff;
vm_deallocate(mach_task_self(), reinterpret_cast<vm_address_t>(rw_ptr), size);
#endif
}
void FreeExecutableMemory(void* ptr, size_t size)
{
if (!ptr)
return;
#ifdef IPHONEOS
g_jit_memory_tracker.UnregisterJITRegion(ptr);
#endif
#if defined(IPHONEOS) && defined(__LIBRETRO__)
for (auto& slot : s_frontend_regions)
{
if (retro_atomic_cas_ptr(&slot, ptr, nullptr))
{
struct retro_exec_mem_free f = {};
f.rx = ptr;
if (Libretro::environ_cb)
Libretro::environ_cb(RETRO_ENVIRONMENT_EXEC_MEM_FREE, &f);
return;
}
}
#endif
munmap(ptr, size);
}
#endif
} // namespace Common