CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/MemoryUtil.cpp
Views: 1401
// Copyright (C) 2003 Dolphin Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official SVN repository and contact information can be found at15// http://code.google.com/p/dolphin-emu/1617#include "ppsspp_config.h"1819#if !PPSSPP_PLATFORM(SWITCH)20#include <cstring>21#include <cstdlib>2223#include "Common/CommonTypes.h"24#include "Common/Log.h"25#include "Common/MemoryUtil.h"26#include "Common/StringUtils.h"27#include "Common/SysError.h"28#include "Common/Data/Text/Parsers.h"2930#ifdef _WIN3231#include "Common/CommonWindows.h"32#else33#include <errno.h>34#include <stdio.h>35#endif3637#ifdef __APPLE__38#include <sys/types.h>39#include <sys/mman.h>40#include <mach/vm_param.h>41#endif4243#ifndef _WIN3244#include <unistd.h>45#endif46static int hint_location;47#ifdef __APPLE__48#define MEM_PAGE_SIZE (PAGE_SIZE)49#elif defined(_WIN32)50static SYSTEM_INFO sys_info;51#define MEM_PAGE_SIZE (uintptr_t)(sys_info.dwPageSize)52#else53#define MEM_PAGE_SIZE (getpagesize())54#endif5556#define MEM_PAGE_MASK ((MEM_PAGE_SIZE)-1)57#define ppsspp_round_page(x) ((((uintptr_t)(x)) + MEM_PAGE_MASK) & ~(MEM_PAGE_MASK))5859#ifdef _WIN3260// Win32 memory protection flags are odd...61static uint32_t ConvertProtFlagsWin32(uint32_t flags) {62uint32_t protect = 0;63switch (flags) {64case 0: protect = PAGE_NOACCESS; break;65case MEM_PROT_READ: protect = PAGE_READONLY; break;66case MEM_PROT_WRITE: protect = PAGE_READWRITE; break; // Can't set write-only67case MEM_PROT_EXEC: protect = PAGE_EXECUTE; break;68case MEM_PROT_READ | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READ; break;69case MEM_PROT_WRITE | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READWRITE; break; // Can't set write-only70case MEM_PROT_READ | MEM_PROT_WRITE: protect = PAGE_READWRITE; break;71case MEM_PROT_READ | MEM_PROT_WRITE | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READWRITE; break;72}73return protect;74}7576#else7778static uint32_t ConvertProtFlagsUnix(uint32_t flags) {79uint32_t protect = 0;80if (flags & MEM_PROT_READ)81protect |= PROT_READ;82if (flags & MEM_PROT_WRITE)83protect |= PROT_WRITE;84if (flags & MEM_PROT_EXEC)85protect |= PROT_EXEC;86return protect;87}8889#endif9091#if defined(_WIN32) && PPSSPP_ARCH(AMD64)92static uintptr_t last_executable_addr;93static void *SearchForFreeMem(size_t size) {94if (!last_executable_addr)95last_executable_addr = (uintptr_t) &hint_location - sys_info.dwPageSize;96last_executable_addr -= size;9798MEMORY_BASIC_INFORMATION info;99while (VirtualQuery((void *)last_executable_addr, &info, sizeof(info)) == sizeof(info)) {100// went too far, unusable for executable memory101if (last_executable_addr + 0x80000000 < (uintptr_t) &hint_location)102return NULL;103104uintptr_t end = last_executable_addr + size;105if (info.State != MEM_FREE)106{107last_executable_addr = (uintptr_t) info.AllocationBase - size;108continue;109}110111if ((uintptr_t)info.BaseAddress + (uintptr_t)info.RegionSize >= end &&112(uintptr_t)info.BaseAddress <= last_executable_addr)113return (void *)last_executable_addr;114115last_executable_addr -= size;116}117118return NULL;119}120#endif121122#if PPSSPP_PLATFORM(WINDOWS)123124// This is purposely not a full wrapper for virtualalloc/mmap, but it125// provides exactly the primitive operations that PPSSPP needs.126void *AllocateExecutableMemory(size_t size) {127void *ptr = nullptr;128DWORD prot = PAGE_EXECUTE_READWRITE;129if (PlatformIsWXExclusive())130prot = PAGE_READWRITE;131if (sys_info.dwPageSize == 0)132GetSystemInfo(&sys_info);133#if PPSSPP_ARCH(AMD64)134if ((uintptr_t)&hint_location > 0xFFFFFFFFULL) {135size_t aligned_size = ppsspp_round_page(size);136#if 1 // Turn off to hunt for RIP bugs on x86-64.137ptr = SearchForFreeMem(aligned_size);138if (!ptr) {139// Let's try again, from the top.140// When we deallocate, this doesn't change, so we eventually run out of space.141last_executable_addr = 0;142ptr = SearchForFreeMem(aligned_size);143}144#endif145if (ptr) {146ptr = VirtualAlloc(ptr, aligned_size, MEM_RESERVE | MEM_COMMIT, prot);147} else {148WARN_LOG(Log::Common, "Unable to find nearby executable memory for jit. Proceeding with far memory.");149// Can still run, thanks to "RipAccessible".150ptr = VirtualAlloc(nullptr, aligned_size, MEM_RESERVE | MEM_COMMIT, prot);151}152}153else154#endif155{156#if PPSSPP_PLATFORM(UWP)157ptr = VirtualAllocFromApp(0, size, MEM_RESERVE | MEM_COMMIT, prot);158#else159ptr = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, prot);160#endif161}162if (!ptr) {163ERROR_LOG(Log::MemMap, "Failed to allocate executable memory (%d)", (int)size);164}165return ptr;166}167168#else // Non-Windows platforms169170void *AllocateExecutableMemory(size_t size) {171static char *map_hint = nullptr;172173#if PPSSPP_ARCH(AMD64)174// Try to request one that is close to our memory location if we're in high memory.175// We use a dummy global variable to give us a good location to start from.176// TODO: Should we also do this for ARM64?177if (!map_hint) {178if ((uintptr_t) &hint_location > 0xFFFFFFFFULL)179map_hint = (char*)ppsspp_round_page(&hint_location) - 0x20000000; // 0.5gb lower than our approximate location180else181map_hint = (char*)0x20000000; // 0.5GB mark in memory182} else if ((uintptr_t) map_hint > 0xFFFFFFFFULL) {183map_hint -= ppsspp_round_page(size); /* round down to the next page if we're in high memory */184}185#endif186187int prot = PROT_READ | PROT_WRITE | PROT_EXEC;188if (PlatformIsWXExclusive())189prot = PROT_READ | PROT_WRITE; // POST_EXEC is added later in this case.190191void* ptr = mmap(map_hint, size, prot, MAP_ANON | MAP_PRIVATE, -1, 0);192193if (ptr == MAP_FAILED) {194ptr = nullptr;195ERROR_LOG(Log::MemMap, "Failed to allocate executable memory (%d) errno=%d", (int)size, errno);196}197198#if PPSSPP_ARCH(AMD64)199else if ((uintptr_t)map_hint <= 0xFFFFFFFF) {200// Round up if we're below 32-bit mark, probably allocating sequentially.201map_hint += ppsspp_round_page(size);202203// If we moved ahead too far, skip backwards and recalculate.204// When we free, we keep moving forward and eventually move too far.205if ((uintptr_t)map_hint - (uintptr_t) &hint_location >= 0x70000000) {206map_hint = 0;207}208}209#endif210211return ptr;212}213214#endif // non-windows215216void *AllocateMemoryPages(size_t size, uint32_t memProtFlags) {217#ifdef _WIN32218if (sys_info.dwPageSize == 0)219GetSystemInfo(&sys_info);220uint32_t protect = ConvertProtFlagsWin32(memProtFlags);221// Make sure to do this after GetSystemInfo().222size = ppsspp_round_page(size);223#if PPSSPP_PLATFORM(UWP)224void* ptr = VirtualAllocFromApp(0, size, MEM_COMMIT, protect);225#else226void* ptr = VirtualAlloc(0, size, MEM_COMMIT, protect);227#endif228if (!ptr) {229ERROR_LOG(Log::MemMap, "Failed to allocate raw memory pages");230return nullptr;231}232#else233size = ppsspp_round_page(size);234uint32_t protect = ConvertProtFlagsUnix(memProtFlags);235void *ptr = mmap(0, size, protect, MAP_ANON | MAP_PRIVATE, -1, 0);236if (ptr == MAP_FAILED) {237ERROR_LOG(Log::MemMap, "Failed to allocate raw memory pages: errno=%d", errno);238return nullptr;239}240#endif241242// printf("Mapped memory at %p (size %ld)\n", ptr,243// (unsigned long)size);244return ptr;245}246247void *AllocateAlignedMemory(size_t size, size_t alignment) {248#ifdef _WIN32249void* ptr = _aligned_malloc(size, alignment);250#else251void* ptr = NULL;252#ifdef __ANDROID__253ptr = memalign(alignment, size);254#else255if (posix_memalign(&ptr, alignment, size) != 0) {256ptr = nullptr;257}258#endif259#endif260char temp[32];261NiceSizeFormat(size, temp, sizeof(temp));262_assert_msg_(ptr != nullptr, "Failed to allocate aligned memory of size %s (%llu)", temp, (unsigned long long)size);263return ptr;264}265266void FreeMemoryPages(void *ptr, size_t size) {267if (!ptr)268return;269uintptr_t page_size = GetMemoryProtectPageSize();270size = (size + page_size - 1) & (~(page_size - 1));271#ifdef _WIN32272if (!VirtualFree(ptr, 0, MEM_RELEASE)) {273ERROR_LOG(Log::MemMap, "FreeMemoryPages failed!\n%s", GetLastErrorMsg().c_str());274}275#else276munmap(ptr, size);277#endif278}279280void FreeExecutableMemory(void *ptr, size_t size) {281FreeMemoryPages(ptr, size);282}283284void FreeAlignedMemory(void* ptr) {285if (!ptr)286return;287#ifdef _WIN32288_aligned_free(ptr);289#else290free(ptr);291#endif292}293294bool PlatformIsWXExclusive() {295// Needed on platforms that disable W^X pages for security. Even without block linking, still should be much faster than IR JIT.296// This might also come in useful for UWP (Universal Windows Platform) if I'm understanding things correctly.297#if PPSSPP_PLATFORM(IOS) || PPSSPP_PLATFORM(UWP) || defined(__OpenBSD__)298return true;299#elif PPSSPP_PLATFORM(MAC) && PPSSPP_ARCH(ARM64)300return true;301#else302// Returning true here lets you test the W^X path on Windows and other non-W^X platforms.303return false;304#endif305}306307bool ProtectMemoryPages(const void* ptr, size_t size, uint32_t memProtFlags) {308VERBOSE_LOG(Log::JIT, "ProtectMemoryPages: %p (%d) : r%d w%d x%d", ptr, (int)size,309(memProtFlags & MEM_PROT_READ) != 0, (memProtFlags & MEM_PROT_WRITE) != 0, (memProtFlags & MEM_PROT_EXEC) != 0);310311if (PlatformIsWXExclusive()) {312if ((memProtFlags & (MEM_PROT_WRITE | MEM_PROT_EXEC)) == (MEM_PROT_WRITE | MEM_PROT_EXEC)) {313_assert_msg_(false, "Bad memory protect flags %d: W^X is in effect, can't both write and exec", memProtFlags);314}315}316// Note - VirtualProtect will affect the full pages containing the requested range.317// mprotect does not seem to, at least not on Android unless I made a mistake somewhere, so we manually round.318#ifdef _WIN32319uint32_t protect = ConvertProtFlagsWin32(memProtFlags);320321#if PPSSPP_PLATFORM(UWP)322DWORD oldValue;323if (!VirtualProtectFromApp((void *)ptr, size, protect, &oldValue)) {324ERROR_LOG(Log::MemMap, "WriteProtectMemory failed!\n%s", GetLastErrorMsg().c_str());325return false;326}327#else328DWORD oldValue;329if (!VirtualProtect((void *)ptr, size, protect, &oldValue)) {330ERROR_LOG(Log::MemMap, "WriteProtectMemory failed!\n%s", GetLastErrorMsg().c_str());331return false;332}333#endif334return true;335#else336uint32_t protect = ConvertProtFlagsUnix(memProtFlags);337uintptr_t page_size = GetMemoryProtectPageSize();338339uintptr_t start = (uintptr_t)ptr;340uintptr_t end = (uintptr_t)ptr + size;341start &= ~(page_size - 1);342end = (end + page_size - 1) & ~(page_size - 1);343int retval = mprotect((void *)start, end - start, protect);344if (retval != 0) {345ERROR_LOG(Log::MemMap, "mprotect failed (%p)! errno=%d (%s)", (void *)start, errno, strerror(errno));346return false;347}348return true;349#endif350}351352int GetMemoryProtectPageSize() {353#ifdef _WIN32354if (sys_info.dwPageSize == 0)355GetSystemInfo(&sys_info);356return sys_info.dwPageSize;357#endif358return MEM_PAGE_SIZE;359}360#endif // !PPSSPP_PLATFORM(SWITCH)361362363