2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2017-03-03 21:48:05 +00:00
|
|
|
#include "ppsspp_config.h"
|
2017-02-28 00:47:13 +00:00
|
|
|
|
2023-06-26 14:17:45 +00:00
|
|
|
#if !PPSSPP_PLATFORM(SWITCH)
|
2020-08-15 10:25:39 +00:00
|
|
|
#include <cstring>
|
2020-10-14 21:49:44 +00:00
|
|
|
#include <cstdlib>
|
2017-05-26 13:39:27 +00:00
|
|
|
|
2022-01-30 23:49:02 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2020-07-19 18:32:15 +00:00
|
|
|
#include "Common/Log.h"
|
|
|
|
#include "Common/MemoryUtil.h"
|
|
|
|
#include "Common/StringUtils.h"
|
2020-09-29 13:50:16 +00:00
|
|
|
#include "Common/SysError.h"
|
2023-09-11 15:45:54 +00:00
|
|
|
#include "Common/Data/Text/Parsers.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2020-07-19 18:32:15 +00:00
|
|
|
#include "Common/CommonWindows.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
2024-10-10 09:55:07 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
2012-11-25 11:25:07 +00:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
2016-08-28 16:41:15 +00:00
|
|
|
#include <mach/vm_param.h>
|
2012-11-25 11:25:07 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-05 22:45:19 +00:00
|
|
|
#ifndef _WIN32
|
2012-11-01 15:19:01 +00:00
|
|
|
#include <unistd.h>
|
2014-08-05 22:45:19 +00:00
|
|
|
#endif
|
2015-10-10 16:07:34 +00:00
|
|
|
static int hint_location;
|
2012-11-25 11:25:07 +00:00
|
|
|
#ifdef __APPLE__
|
2016-08-28 16:35:57 +00:00
|
|
|
#define MEM_PAGE_SIZE (PAGE_SIZE)
|
2014-08-05 22:45:19 +00:00
|
|
|
#elif defined(_WIN32)
|
|
|
|
static SYSTEM_INFO sys_info;
|
2016-08-28 16:35:57 +00:00
|
|
|
#define MEM_PAGE_SIZE (uintptr_t)(sys_info.dwPageSize)
|
2012-11-25 11:25:07 +00:00
|
|
|
#else
|
2016-08-28 16:35:57 +00:00
|
|
|
#define MEM_PAGE_SIZE (getpagesize())
|
2012-11-25 11:25:07 +00:00
|
|
|
#endif
|
2016-08-28 16:35:57 +00:00
|
|
|
|
|
|
|
#define MEM_PAGE_MASK ((MEM_PAGE_SIZE)-1)
|
2020-03-09 04:23:48 +00:00
|
|
|
#define ppsspp_round_page(x) ((((uintptr_t)(x)) + MEM_PAGE_MASK) & ~(MEM_PAGE_MASK))
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2016-08-28 10:28:17 +00:00
|
|
|
#ifdef _WIN32
|
2020-07-19 18:32:15 +00:00
|
|
|
// Win32 memory protection flags are odd...
|
2017-01-26 08:49:26 +00:00
|
|
|
static uint32_t ConvertProtFlagsWin32(uint32_t flags) {
|
2016-08-28 10:28:17 +00:00
|
|
|
uint32_t protect = 0;
|
|
|
|
switch (flags) {
|
|
|
|
case 0: protect = PAGE_NOACCESS; break;
|
|
|
|
case MEM_PROT_READ: protect = PAGE_READONLY; break;
|
|
|
|
case MEM_PROT_WRITE: protect = PAGE_READWRITE; break; // Can't set write-only
|
|
|
|
case MEM_PROT_EXEC: protect = PAGE_EXECUTE; break;
|
|
|
|
case MEM_PROT_READ | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READ; break;
|
|
|
|
case MEM_PROT_WRITE | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READWRITE; break; // Can't set write-only
|
|
|
|
case MEM_PROT_READ | MEM_PROT_WRITE: protect = PAGE_READWRITE; break;
|
|
|
|
case MEM_PROT_READ | MEM_PROT_WRITE | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READWRITE; break;
|
|
|
|
}
|
|
|
|
return protect;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-01-26 08:49:26 +00:00
|
|
|
static uint32_t ConvertProtFlagsUnix(uint32_t flags) {
|
2016-08-28 10:28:17 +00:00
|
|
|
uint32_t protect = 0;
|
|
|
|
if (flags & MEM_PROT_READ)
|
|
|
|
protect |= PROT_READ;
|
|
|
|
if (flags & MEM_PROT_WRITE)
|
|
|
|
protect |= PROT_WRITE;
|
|
|
|
if (flags & MEM_PROT_EXEC)
|
|
|
|
protect |= PROT_EXEC;
|
|
|
|
return protect;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-03-03 05:49:21 +00:00
|
|
|
#if defined(_WIN32) && PPSSPP_ARCH(AMD64)
|
2015-10-10 16:07:34 +00:00
|
|
|
static uintptr_t last_executable_addr;
|
2016-08-28 10:09:01 +00:00
|
|
|
static void *SearchForFreeMem(size_t size) {
|
2015-10-10 16:07:34 +00:00
|
|
|
if (!last_executable_addr)
|
|
|
|
last_executable_addr = (uintptr_t) &hint_location - sys_info.dwPageSize;
|
|
|
|
last_executable_addr -= size;
|
2014-08-05 22:45:19 +00:00
|
|
|
|
|
|
|
MEMORY_BASIC_INFORMATION info;
|
2016-08-28 10:09:01 +00:00
|
|
|
while (VirtualQuery((void *)last_executable_addr, &info, sizeof(info)) == sizeof(info)) {
|
2014-08-05 22:45:19 +00:00
|
|
|
// went too far, unusable for executable memory
|
2015-10-10 16:07:34 +00:00
|
|
|
if (last_executable_addr + 0x80000000 < (uintptr_t) &hint_location)
|
2014-08-05 22:45:19 +00:00
|
|
|
return NULL;
|
|
|
|
|
2015-10-10 16:07:34 +00:00
|
|
|
uintptr_t end = last_executable_addr + size;
|
2014-08-05 22:45:19 +00:00
|
|
|
if (info.State != MEM_FREE)
|
|
|
|
{
|
2015-10-10 16:07:34 +00:00
|
|
|
last_executable_addr = (uintptr_t) info.AllocationBase - size;
|
2014-08-05 22:45:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((uintptr_t)info.BaseAddress + (uintptr_t)info.RegionSize >= end &&
|
2015-10-10 16:07:34 +00:00
|
|
|
(uintptr_t)info.BaseAddress <= last_executable_addr)
|
|
|
|
return (void *)last_executable_addr;
|
2014-08-05 22:45:19 +00:00
|
|
|
|
2015-10-10 16:07:34 +00:00
|
|
|
last_executable_addr -= size;
|
2014-08-05 22:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-24 09:36:44 +00:00
|
|
|
#if PPSSPP_PLATFORM(WINDOWS)
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// This is purposely not a full wrapper for virtualalloc/mmap, but it
|
2013-11-10 16:55:45 +00:00
|
|
|
// provides exactly the primitive operations that PPSSPP needs.
|
2016-08-28 10:28:17 +00:00
|
|
|
void *AllocateExecutableMemory(size_t size) {
|
2017-07-07 13:04:04 +00:00
|
|
|
void *ptr = nullptr;
|
2017-01-26 08:49:26 +00:00
|
|
|
DWORD prot = PAGE_EXECUTE_READWRITE;
|
|
|
|
if (PlatformIsWXExclusive())
|
|
|
|
prot = PAGE_READWRITE;
|
2017-03-05 01:21:25 +00:00
|
|
|
if (sys_info.dwPageSize == 0)
|
|
|
|
GetSystemInfo(&sys_info);
|
2021-03-03 05:49:21 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64)
|
2016-08-28 10:28:17 +00:00
|
|
|
if ((uintptr_t)&hint_location > 0xFFFFFFFFULL) {
|
2020-03-09 04:23:48 +00:00
|
|
|
size_t aligned_size = ppsspp_round_page(size);
|
2017-07-07 13:12:57 +00:00
|
|
|
#if 1 // Turn off to hunt for RIP bugs on x86-64.
|
2015-10-10 16:07:34 +00:00
|
|
|
ptr = SearchForFreeMem(aligned_size);
|
|
|
|
if (!ptr) {
|
|
|
|
// Let's try again, from the top.
|
|
|
|
// When we deallocate, this doesn't change, so we eventually run out of space.
|
|
|
|
last_executable_addr = 0;
|
|
|
|
ptr = SearchForFreeMem(aligned_size);
|
|
|
|
}
|
2017-07-07 13:12:57 +00:00
|
|
|
#endif
|
2015-10-10 16:07:34 +00:00
|
|
|
if (ptr) {
|
2017-01-26 08:49:26 +00:00
|
|
|
ptr = VirtualAlloc(ptr, aligned_size, MEM_RESERVE | MEM_COMMIT, prot);
|
2015-10-10 16:07:34 +00:00
|
|
|
} else {
|
2024-07-14 12:42:59 +00:00
|
|
|
WARN_LOG(Log::Common, "Unable to find nearby executable memory for jit. Proceeding with far memory.");
|
2017-07-07 13:04:04 +00:00
|
|
|
// Can still run, thanks to "RipAccessible".
|
|
|
|
ptr = VirtualAlloc(nullptr, aligned_size, MEM_RESERVE | MEM_COMMIT, prot);
|
2015-10-10 16:07:34 +00:00
|
|
|
}
|
2014-08-05 22:45:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2017-01-26 08:49:26 +00:00
|
|
|
{
|
2017-03-05 01:21:25 +00:00
|
|
|
#if PPSSPP_PLATFORM(UWP)
|
|
|
|
ptr = VirtualAllocFromApp(0, size, MEM_RESERVE | MEM_COMMIT, prot);
|
|
|
|
#else
|
2017-01-26 08:49:26 +00:00
|
|
|
ptr = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, prot);
|
2017-03-05 01:21:25 +00:00
|
|
|
#endif
|
2017-01-26 08:49:26 +00:00
|
|
|
}
|
2023-04-24 09:36:44 +00:00
|
|
|
if (!ptr) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::MemMap, "Failed to allocate executable memory (%d)", (int)size);
|
2023-04-24 09:36:44 +00:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // Non-Windows platforms
|
|
|
|
|
|
|
|
void *AllocateExecutableMemory(size_t size) {
|
|
|
|
static char *map_hint = nullptr;
|
|
|
|
|
2021-03-03 05:49:21 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64)
|
2014-08-05 22:45:19 +00:00
|
|
|
// Try to request one that is close to our memory location if we're in high memory.
|
|
|
|
// We use a dummy global variable to give us a good location to start from.
|
2023-04-24 09:36:44 +00:00
|
|
|
// TODO: Should we also do this for ARM64?
|
2016-08-28 12:52:08 +00:00
|
|
|
if (!map_hint) {
|
2014-08-07 01:06:49 +00:00
|
|
|
if ((uintptr_t) &hint_location > 0xFFFFFFFFULL)
|
2020-03-09 04:23:48 +00:00
|
|
|
map_hint = (char*)ppsspp_round_page(&hint_location) - 0x20000000; // 0.5gb lower than our approximate location
|
2014-08-07 01:06:49 +00:00
|
|
|
else
|
|
|
|
map_hint = (char*)0x20000000; // 0.5GB mark in memory
|
2020-01-04 20:54:25 +00:00
|
|
|
} else if ((uintptr_t) map_hint > 0xFFFFFFFFULL) {
|
2020-03-09 04:23:48 +00:00
|
|
|
map_hint -= ppsspp_round_page(size); /* round down to the next page if we're in high memory */
|
2014-08-07 01:06:49 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
2017-01-25 17:35:09 +00:00
|
|
|
|
|
|
|
int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
|
|
|
|
if (PlatformIsWXExclusive())
|
|
|
|
prot = PROT_READ | PROT_WRITE; // POST_EXEC is added later in this case.
|
|
|
|
|
2020-01-04 20:54:25 +00:00
|
|
|
void* ptr = mmap(map_hint, size, prot, MAP_ANON | MAP_PRIVATE, -1, 0);
|
2015-10-10 16:07:34 +00:00
|
|
|
|
2023-04-24 09:36:44 +00:00
|
|
|
if (ptr == MAP_FAILED) {
|
2015-10-10 16:07:34 +00:00
|
|
|
ptr = nullptr;
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::MemMap, "Failed to allocate executable memory (%d) errno=%d", (int)size, errno);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2020-01-04 20:54:25 +00:00
|
|
|
|
2023-04-24 09:36:44 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64)
|
2016-08-28 12:52:08 +00:00
|
|
|
else if ((uintptr_t)map_hint <= 0xFFFFFFFF) {
|
2015-10-10 16:07:34 +00:00
|
|
|
// Round up if we're below 32-bit mark, probably allocating sequentially.
|
2020-03-09 04:23:48 +00:00
|
|
|
map_hint += ppsspp_round_page(size);
|
2016-01-02 21:24:58 +00:00
|
|
|
|
|
|
|
// If we moved ahead too far, skip backwards and recalculate.
|
|
|
|
// When we free, we keep moving forward and eventually move too far.
|
|
|
|
if ((uintptr_t)map_hint - (uintptr_t) &hint_location >= 0x70000000) {
|
|
|
|
map_hint = 0;
|
|
|
|
}
|
2014-08-07 01:06:49 +00:00
|
|
|
}
|
2014-08-07 02:02:45 +00:00
|
|
|
#endif
|
2023-04-24 09:36:44 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2023-04-24 09:36:44 +00:00
|
|
|
#endif // non-windows
|
|
|
|
|
2016-08-28 10:28:17 +00:00
|
|
|
void *AllocateMemoryPages(size_t size, uint32_t memProtFlags) {
|
2017-03-05 10:25:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-03-05 01:21:25 +00:00
|
|
|
if (sys_info.dwPageSize == 0)
|
|
|
|
GetSystemInfo(&sys_info);
|
2016-08-28 10:28:17 +00:00
|
|
|
uint32_t protect = ConvertProtFlagsWin32(memProtFlags);
|
2023-03-16 04:29:08 +00:00
|
|
|
// Make sure to do this after GetSystemInfo().
|
|
|
|
size = ppsspp_round_page(size);
|
2017-03-05 01:21:25 +00:00
|
|
|
#if PPSSPP_PLATFORM(UWP)
|
|
|
|
void* ptr = VirtualAllocFromApp(0, size, MEM_COMMIT, protect);
|
|
|
|
#else
|
2016-08-28 10:28:17 +00:00
|
|
|
void* ptr = VirtualAlloc(0, size, MEM_COMMIT, protect);
|
2017-03-05 01:21:25 +00:00
|
|
|
#endif
|
2020-07-19 18:32:15 +00:00
|
|
|
if (!ptr) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::MemMap, "Failed to allocate raw memory pages");
|
2020-07-19 18:32:15 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
2023-03-16 04:29:08 +00:00
|
|
|
size = ppsspp_round_page(size);
|
2016-08-28 10:28:17 +00:00
|
|
|
uint32_t protect = ConvertProtFlagsUnix(memProtFlags);
|
2017-01-26 08:49:26 +00:00
|
|
|
void *ptr = mmap(0, size, protect, MAP_ANON | MAP_PRIVATE, -1, 0);
|
2017-01-25 18:11:33 +00:00
|
|
|
if (ptr == MAP_FAILED) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::MemMap, "Failed to allocate raw memory pages: errno=%d", errno);
|
2017-01-25 18:11:33 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// printf("Mapped memory at %p (size %ld)\n", ptr,
|
|
|
|
// (unsigned long)size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2016-08-28 10:09:01 +00:00
|
|
|
void *AllocateAlignedMemory(size_t size, size_t alignment) {
|
2012-11-01 15:19:01 +00:00
|
|
|
#ifdef _WIN32
|
2022-04-23 20:52:28 +00:00
|
|
|
void* ptr = _aligned_malloc(size, alignment);
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
|
|
|
void* ptr = NULL;
|
2016-10-12 09:13:16 +00:00
|
|
|
#ifdef __ANDROID__
|
2012-12-13 03:15:20 +00:00
|
|
|
ptr = memalign(alignment, size);
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
2020-07-19 18:32:15 +00:00
|
|
|
if (posix_memalign(&ptr, alignment, size) != 0) {
|
|
|
|
ptr = nullptr;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2016-08-28 10:09:01 +00:00
|
|
|
void FreeMemoryPages(void *ptr, size_t size) {
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
2017-01-25 18:11:33 +00:00
|
|
|
uintptr_t page_size = GetMemoryProtectPageSize();
|
|
|
|
size = (size + page_size - 1) & (~(page_size - 1));
|
2012-11-01 15:19:01 +00:00
|
|
|
#ifdef _WIN32
|
2020-07-19 18:32:15 +00:00
|
|
|
if (!VirtualFree(ptr, 0, MEM_RELEASE)) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::MemMap, "FreeMemoryPages failed!\n%s", GetLastErrorMsg().c_str());
|
2020-07-19 18:32:15 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
2016-08-28 10:09:01 +00:00
|
|
|
munmap(ptr, size);
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-04-24 09:42:27 +00:00
|
|
|
void FreeExecutableMemory(void *ptr, size_t size) {
|
|
|
|
FreeMemoryPages(ptr, size);
|
|
|
|
}
|
|
|
|
|
2016-08-28 10:09:01 +00:00
|
|
|
void FreeAlignedMemory(void* ptr) {
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
2012-11-01 15:19:01 +00:00
|
|
|
#ifdef _WIN32
|
2016-08-28 10:09:01 +00:00
|
|
|
_aligned_free(ptr);
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
2016-08-28 10:09:01 +00:00
|
|
|
free(ptr);
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-08-28 10:09:01 +00:00
|
|
|
bool PlatformIsWXExclusive() {
|
2017-11-06 00:06:09 +00:00
|
|
|
// Needed on platforms that disable W^X pages for security. Even without block linking, still should be much faster than IR JIT.
|
2016-08-28 16:07:54 +00:00
|
|
|
// This might also come in useful for UWP (Universal Windows Platform) if I'm understanding things correctly.
|
2021-03-03 04:57:25 +00:00
|
|
|
#if PPSSPP_PLATFORM(IOS) || PPSSPP_PLATFORM(UWP) || defined(__OpenBSD__)
|
2016-08-28 12:52:08 +00:00
|
|
|
return true;
|
2020-12-01 23:12:44 +00:00
|
|
|
#elif PPSSPP_PLATFORM(MAC) && PPSSPP_ARCH(ARM64)
|
|
|
|
return true;
|
2016-08-28 12:52:08 +00:00
|
|
|
#else
|
|
|
|
// Returning true here lets you test the W^X path on Windows and other non-W^X platforms.
|
2016-08-28 10:09:01 +00:00
|
|
|
return false;
|
2016-08-28 12:52:08 +00:00
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 17:35:09 +00:00
|
|
|
bool ProtectMemoryPages(const void* ptr, size_t size, uint32_t memProtFlags) {
|
2024-07-14 12:42:59 +00:00
|
|
|
VERBOSE_LOG(Log::JIT, "ProtectMemoryPages: %p (%d) : r%d w%d x%d", ptr, (int)size,
|
2017-01-26 08:49:26 +00:00
|
|
|
(memProtFlags & MEM_PROT_READ) != 0, (memProtFlags & MEM_PROT_WRITE) != 0, (memProtFlags & MEM_PROT_EXEC) != 0);
|
2016-08-28 12:52:08 +00:00
|
|
|
|
2016-08-28 11:35:27 +00:00
|
|
|
if (PlatformIsWXExclusive()) {
|
2017-01-26 08:49:26 +00:00
|
|
|
if ((memProtFlags & (MEM_PROT_WRITE | MEM_PROT_EXEC)) == (MEM_PROT_WRITE | MEM_PROT_EXEC)) {
|
2020-07-19 15:52:41 +00:00
|
|
|
_assert_msg_(false, "Bad memory protect flags %d: W^X is in effect, can't both write and exec", memProtFlags);
|
2017-01-26 08:49:26 +00:00
|
|
|
}
|
2016-08-28 11:35:27 +00:00
|
|
|
}
|
2016-08-28 17:00:13 +00:00
|
|
|
// Note - VirtualProtect will affect the full pages containing the requested range.
|
2016-08-28 16:10:26 +00:00
|
|
|
// mprotect does not seem to, at least not on Android unless I made a mistake somewhere, so we manually round.
|
2012-11-01 15:19:01 +00:00
|
|
|
#ifdef _WIN32
|
2016-08-28 10:28:17 +00:00
|
|
|
uint32_t protect = ConvertProtFlagsWin32(memProtFlags);
|
2017-02-28 00:47:13 +00:00
|
|
|
|
|
|
|
#if PPSSPP_PLATFORM(UWP)
|
|
|
|
DWORD oldValue;
|
|
|
|
if (!VirtualProtectFromApp((void *)ptr, size, protect, &oldValue)) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::MemMap, "WriteProtectMemory failed!\n%s", GetLastErrorMsg().c_str());
|
2017-02-28 00:47:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
2012-11-01 15:19:01 +00:00
|
|
|
DWORD oldValue;
|
2017-01-25 19:10:17 +00:00
|
|
|
if (!VirtualProtect((void *)ptr, size, protect, &oldValue)) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::MemMap, "WriteProtectMemory failed!\n%s", GetLastErrorMsg().c_str());
|
2017-01-25 19:10:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-28 00:47:13 +00:00
|
|
|
#endif
|
2017-01-25 19:10:17 +00:00
|
|
|
return true;
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
2016-08-28 10:28:17 +00:00
|
|
|
uint32_t protect = ConvertProtFlagsUnix(memProtFlags);
|
2017-01-25 17:35:09 +00:00
|
|
|
uintptr_t page_size = GetMemoryProtectPageSize();
|
2016-08-28 12:52:08 +00:00
|
|
|
|
|
|
|
uintptr_t start = (uintptr_t)ptr;
|
|
|
|
uintptr_t end = (uintptr_t)ptr + size;
|
|
|
|
start &= ~(page_size - 1);
|
|
|
|
end = (end + page_size - 1) & ~(page_size - 1);
|
2017-01-25 17:35:09 +00:00
|
|
|
int retval = mprotect((void *)start, end - start, protect);
|
2017-01-25 18:11:33 +00:00
|
|
|
if (retval != 0) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::MemMap, "mprotect failed (%p)! errno=%d (%s)", (void *)start, errno, strerror(errno));
|
2017-01-25 17:35:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
2016-08-28 11:35:27 +00:00
|
|
|
}
|
|
|
|
|
2016-08-28 16:07:54 +00:00
|
|
|
int GetMemoryProtectPageSize() {
|
2016-08-28 11:35:27 +00:00
|
|
|
#ifdef _WIN32
|
2016-08-28 16:35:57 +00:00
|
|
|
if (sys_info.dwPageSize == 0)
|
|
|
|
GetSystemInfo(&sys_info);
|
2016-08-28 16:44:41 +00:00
|
|
|
return sys_info.dwPageSize;
|
2016-08-28 11:35:27 +00:00
|
|
|
#endif
|
2016-08-28 16:35:57 +00:00
|
|
|
return MEM_PAGE_SIZE;
|
2016-08-28 16:44:41 +00:00
|
|
|
}
|
2023-06-26 14:17:45 +00:00
|
|
|
#endif // !PPSSPP_PLATFORM(SWITCH)
|