2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
2013-10-19 16:58:02 -06:00
|
|
|
#include <set>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <string>
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2017-04-14 04:53:32 -06:00
|
|
|
#include "Common/CommonFuncs.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/MemArena.h"
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/StringUtil.h"
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2009-01-15 19:58:34 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2008-09-17 02:08:22 -06:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
2013-02-26 12:49:00 -07:00
|
|
|
#ifdef ANDROID
|
2020-07-16 10:42:15 -06:00
|
|
|
#include <dlfcn.h>
|
2013-02-26 12:49:00 -07:00
|
|
|
#include <linux/ashmem.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
2008-07-12 11:40:22 -06:00
|
|
|
#endif
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2018-05-15 15:28:41 -06:00
|
|
|
namespace Common
|
|
|
|
{
|
2013-02-26 12:49:00 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
#define ASHMEM_DEVICE "/dev/ashmem"
|
|
|
|
|
2016-01-21 12:46:25 -07:00
|
|
|
static int AshmemCreateFileMapping(const char* name, size_t size)
|
2013-02-26 12:49:00 -07:00
|
|
|
{
|
2020-07-16 10:42:15 -06:00
|
|
|
// ASharedMemory path - works on API >= 26 and falls through on API < 26:
|
|
|
|
|
|
|
|
// We can't call ASharedMemory_create the normal way without increasing the
|
|
|
|
// minimum version requirement to API 26, so we use dlopen/dlsym instead
|
|
|
|
static void* libandroid = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
|
|
|
|
static auto shared_memory_create =
|
|
|
|
reinterpret_cast<int (*)(const char*, size_t)>(dlsym(libandroid, "ASharedMemory_create"));
|
|
|
|
if (shared_memory_create)
|
|
|
|
return shared_memory_create(name, size);
|
|
|
|
|
|
|
|
// /dev/ashmem path - works on API < 29:
|
|
|
|
|
2013-02-26 12:49:00 -07:00
|
|
|
int fd, ret;
|
|
|
|
fd = open(ASHMEM_DEVICE, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-10-28 23:09:01 -06:00
|
|
|
// We don't really care if we can't set the name, it is optional
|
2014-02-23 15:03:39 -07:00
|
|
|
ioctl(fd, ASHMEM_SET_NAME, name);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-02-26 12:49:00 -07:00
|
|
|
ret = ioctl(fd, ASHMEM_SET_SIZE, size);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
2020-10-23 12:41:30 -06:00
|
|
|
NOTICE_LOG_FMT(MEMMAP, "Ashmem returned error: {:#010x}", ret);
|
2013-02-26 12:49:00 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2014-11-01 14:30:14 -06:00
|
|
|
void MemArena::GrabSHMSegment(size_t size)
|
2008-07-12 11:40:22 -06:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2017-10-11 09:17:25 -06:00
|
|
|
const std::string name = "dolphin-emu." + std::to_string(GetCurrentProcessId());
|
2017-09-30 12:59:07 -06:00
|
|
|
hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0,
|
2017-10-11 09:17:25 -06:00
|
|
|
static_cast<DWORD>(size), UTF8ToTStr(name).c_str());
|
2013-02-26 12:49:00 -07:00
|
|
|
#elif defined(ANDROID)
|
2017-10-11 09:17:25 -06:00
|
|
|
fd = AshmemCreateFileMapping(("dolphin-emu." + std::to_string(getpid())).c_str(), size);
|
2013-02-26 12:49:00 -07:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
NOTICE_LOG_FMT(MEMMAP, "Ashmem allocation failed");
|
2013-02-26 12:49:00 -07:00
|
|
|
return;
|
|
|
|
}
|
2008-07-12 11:40:22 -06:00
|
|
|
#else
|
2017-10-11 09:17:25 -06:00
|
|
|
const std::string file_name = "/dolphin-emu." + std::to_string(getpid());
|
|
|
|
fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
|
|
|
|
if (fd == -1)
|
Fix an idiotic race condition when starting games in multiple Dolphin instances at the same time on Unix.
MemArena mmaps the emulated memory from a file in order to get the same
mapping at multiple addresses. A file which, formerly, was located at a
static filename: it was unlinked after creation, but the open did not
use O_EXCL, so if two instances started up on the same system at just
the right time, they would get the same memory. Naturally, this caused
extremely mysterious crashes, but only in Netplay, where the game is
automatically started when the client receives a broadcast from the
server, so races are actually quite likely.
And switch to shm_open, because it fits the bill better and avoids any
issues with using /tmp.
2013-12-09 22:27:20 -07:00
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
ERROR_LOG_FMT(MEMMAP, "shm_open failed: {}", strerror(errno));
|
2017-10-11 09:17:25 -06:00
|
|
|
return;
|
Fix an idiotic race condition when starting games in multiple Dolphin instances at the same time on Unix.
MemArena mmaps the emulated memory from a file in order to get the same
mapping at multiple addresses. A file which, formerly, was located at a
static filename: it was unlinked after creation, but the open did not
use O_EXCL, so if two instances started up on the same system at just
the right time, they would get the same memory. Naturally, this caused
extremely mysterious crashes, but only in Netplay, where the game is
automatically started when the client receives a broadcast from the
server, so races are actually quite likely.
And switch to shm_open, because it fits the bill better and avoids any
issues with using /tmp.
2013-12-09 22:27:20 -07:00
|
|
|
}
|
2017-10-11 09:17:25 -06:00
|
|
|
shm_unlink(file_name.c_str());
|
2013-01-31 14:29:29 -07:00
|
|
|
if (ftruncate(fd, size) < 0)
|
2020-10-23 12:41:30 -06:00
|
|
|
ERROR_LOG_FMT(MEMMAP, "Failed to allocate low memory space");
|
2008-07-12 11:40:22 -06:00
|
|
|
#endif
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2014-11-01 14:30:14 -06:00
|
|
|
void MemArena::ReleaseSHMSegment()
|
2008-07-12 11:40:22 -06:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
CloseHandle(hMemoryMapping);
|
|
|
|
hMemoryMapping = 0;
|
|
|
|
#else
|
|
|
|
close(fd);
|
|
|
|
#endif
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2016-01-21 13:16:51 -07:00
|
|
|
void* MemArena::CreateView(s64 offset, size_t size, void* base)
|
2008-07-12 11:40:22 -06:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2009-01-19 14:42:24 -07:00
|
|
|
return MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base);
|
2008-07-12 11:40:22 -06:00
|
|
|
#else
|
2012-07-11 21:54:50 -06:00
|
|
|
void* retval = mmap(base, size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED | ((base == nullptr) ? 0 : MAP_FIXED), fd, offset);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2012-07-11 21:54:50 -06:00
|
|
|
if (retval == MAP_FAILED)
|
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
NOTICE_LOG_FMT(MEMMAP, "mmap failed");
|
2012-07-11 21:54:50 -06:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
2008-07-12 11:40:22 -06:00
|
|
|
#endif
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2008-07-12 11:40:22 -06:00
|
|
|
void MemArena::ReleaseView(void* view, size_t size)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
UnmapViewOfFile(view);
|
|
|
|
#else
|
|
|
|
munmap(view, size);
|
|
|
|
#endif
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2015-01-17 14:17:36 -07:00
|
|
|
u8* MemArena::FindMemoryBase()
|
2008-07-12 11:40:22 -06:00
|
|
|
{
|
2017-04-14 04:53:32 -06:00
|
|
|
#if _ARCH_32
|
|
|
|
const size_t memory_size = 0x31000000;
|
|
|
|
#else
|
|
|
|
const size_t memory_size = 0x400000000;
|
|
|
|
#endif
|
|
|
|
|
2008-07-12 11:40:22 -06:00
|
|
|
#ifdef _WIN32
|
2017-04-14 04:53:32 -06:00
|
|
|
u8* base = static_cast<u8*>(VirtualAlloc(nullptr, memory_size, MEM_RESERVE, PAGE_READWRITE));
|
|
|
|
if (!base)
|
|
|
|
{
|
2020-12-02 11:17:27 -07:00
|
|
|
PanicAlertFmt("Failed to map enough memory space: {}", GetLastErrorString());
|
2017-04-14 04:53:32 -06:00
|
|
|
return nullptr;
|
|
|
|
}
|
2008-07-12 11:40:22 -06:00
|
|
|
VirtualFree(base, 0, MEM_RELEASE);
|
2008-09-17 01:58:17 -06:00
|
|
|
return base;
|
2008-07-12 11:40:22 -06:00
|
|
|
#else
|
2013-03-24 20:06:34 -06:00
|
|
|
#ifdef ANDROID
|
2013-09-02 03:10:21 -06:00
|
|
|
// Android 4.3 changed how mmap works.
|
|
|
|
// if we map it private and then munmap it, we can't use the base returned.
|
|
|
|
// This may be due to changes in them support a full SELinux implementation.
|
2013-09-29 19:53:32 -06:00
|
|
|
const int flags = MAP_ANON | MAP_SHARED;
|
2013-03-24 20:06:34 -06:00
|
|
|
#else
|
2013-09-02 03:10:21 -06:00
|
|
|
const int flags = MAP_ANON | MAP_PRIVATE;
|
2013-03-24 20:06:34 -06:00
|
|
|
#endif
|
2017-04-14 04:53:32 -06:00
|
|
|
void* base = mmap(nullptr, memory_size, PROT_NONE, flags, -1, 0);
|
2014-08-30 14:14:56 -06:00
|
|
|
if (base == MAP_FAILED)
|
|
|
|
{
|
2020-12-02 11:17:27 -07:00
|
|
|
PanicAlertFmt("Failed to map enough memory space: {}", LastStrerrorString());
|
2017-04-14 04:53:32 -06:00
|
|
|
return nullptr;
|
2008-09-17 02:08:22 -06:00
|
|
|
}
|
2017-04-14 04:53:32 -06:00
|
|
|
munmap(base, memory_size);
|
2008-09-17 01:58:17 -06:00
|
|
|
return static_cast<u8*>(base);
|
|
|
|
#endif
|
2008-07-12 11:40:22 -06:00
|
|
|
}
|
2018-05-15 15:28:41 -06:00
|
|
|
|
|
|
|
} // namespace Common
|