2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2021-12-10 03:22:16 +01:00
|
|
|
#include "Common/MemArena.h"
|
|
|
|
|
2021-06-21 01:17:07 +02:00
|
|
|
#include <cerrno>
|
2014-02-20 04:11:52 +01:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
2021-06-21 01:17:07 +02:00
|
|
|
#include <cstring>
|
2013-10-20 00:58:02 +02:00
|
|
|
#include <set>
|
2014-02-20 04:11:52 +01:00
|
|
|
#include <string>
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2023-05-11 01:51:43 +02:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2021-06-21 01:17:07 +02:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/ashmem.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-09-02 04:01:32 +02:00
|
|
|
#include "Common/Assert.h"
|
2017-04-14 12:53:32 +02:00
|
|
|
#include "Common/CommonFuncs.h"
|
2014-09-08 03:06:58 +02:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-09-26 23:13:07 +02:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "Common/StringUtil.h"
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2018-05-15 23:28:41 +02:00
|
|
|
namespace Common
|
|
|
|
{
|
2013-02-26 20:49:00 +01:00
|
|
|
#define ASHMEM_DEVICE "/dev/ashmem"
|
|
|
|
|
2016-01-21 20:46:25 +01:00
|
|
|
static int AshmemCreateFileMapping(const char* name, size_t size)
|
2013-02-26 20:49:00 +01:00
|
|
|
{
|
2020-07-16 18:42:15 +02: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 20:49:00 +01:00
|
|
|
int fd, ret;
|
|
|
|
fd = open(ASHMEM_DEVICE, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-10-29 06:09:01 +01:00
|
|
|
// We don't really care if we can't set the name, it is optional
|
2014-02-23 23:03:39 +01:00
|
|
|
ioctl(fd, ASHMEM_SET_NAME, name);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-02-26 20:49:00 +01:00
|
|
|
ret = ioctl(fd, ASHMEM_SET_SIZE, size);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
2020-10-23 20:41:30 +02:00
|
|
|
NOTICE_LOG_FMT(MEMMAP, "Ashmem returned error: {:#010x}", ret);
|
2013-02-26 20:49:00 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2021-06-21 02:13:00 +02:00
|
|
|
MemArena::MemArena() = default;
|
|
|
|
MemArena::~MemArena() = default;
|
|
|
|
|
2023-05-11 01:51:43 +02:00
|
|
|
void MemArena::GrabSHMSegment(size_t size, std::string_view base_name)
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2023-05-11 01:51:43 +02:00
|
|
|
const std::string name = fmt::format("{}.{}", base_name, getpid());
|
|
|
|
m_shm_fd = AshmemCreateFileMapping(name.c_str(), size);
|
2023-05-11 02:41:40 +02:00
|
|
|
if (m_shm_fd < 0)
|
2020-10-23 20:41:30 +02:00
|
|
|
NOTICE_LOG_FMT(MEMMAP, "Ashmem allocation failed");
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2014-11-01 21:30:14 +01:00
|
|
|
void MemArena::ReleaseSHMSegment()
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2023-05-11 02:41:40 +02:00
|
|
|
close(m_shm_fd);
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2021-06-21 02:13:00 +02:00
|
|
|
void* MemArena::CreateView(s64 offset, size_t size)
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2023-05-11 02:41:40 +02:00
|
|
|
void* retval = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, m_shm_fd, offset);
|
|
|
|
if (retval == MAP_FAILED)
|
|
|
|
{
|
|
|
|
NOTICE_LOG_FMT(MEMMAP, "mmap failed");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2008-07-12 19:40:22 +02:00
|
|
|
void MemArena::ReleaseView(void* view, size_t size)
|
|
|
|
{
|
2023-05-11 02:41:40 +02:00
|
|
|
munmap(view, size);
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2021-06-21 02:13:00 +02:00
|
|
|
u8* MemArena::ReserveMemoryRegion(size_t memory_size)
|
2008-07-12 19:40:22 +02:00
|
|
|
{
|
2013-09-02 11:10:21 +02:00
|
|
|
// Android 4.3 changed how mmap works.
|
|
|
|
// if we map it private and then munmap it, we can't use the base returned.
|
2021-06-21 01:17:07 +02:00
|
|
|
// This may be due to changes in them to support a full SELinux implementation.
|
2013-09-30 03:53:32 +02:00
|
|
|
const int flags = MAP_ANON | MAP_SHARED;
|
2017-04-14 12:53:32 +02:00
|
|
|
void* base = mmap(nullptr, memory_size, PROT_NONE, flags, -1, 0);
|
2014-08-30 22:14:56 +02:00
|
|
|
if (base == MAP_FAILED)
|
|
|
|
{
|
2020-12-02 19:17:27 +01:00
|
|
|
PanicAlertFmt("Failed to map enough memory space: {}", LastStrerrorString());
|
2017-04-14 12:53:32 +02:00
|
|
|
return nullptr;
|
2008-09-17 10:08:22 +02:00
|
|
|
}
|
2023-05-11 02:41:40 +02:00
|
|
|
m_reserved_region = base;
|
|
|
|
m_reserved_region_size = memory_size;
|
2008-09-17 09:58:17 +02:00
|
|
|
return static_cast<u8*>(base);
|
2008-07-12 19:40:22 +02:00
|
|
|
}
|
2021-06-21 02:13:00 +02:00
|
|
|
|
|
|
|
void MemArena::ReleaseMemoryRegion()
|
|
|
|
{
|
2023-05-11 02:41:40 +02:00
|
|
|
if (m_reserved_region)
|
|
|
|
{
|
|
|
|
munmap(m_reserved_region, m_reserved_region_size);
|
|
|
|
m_reserved_region = nullptr;
|
|
|
|
}
|
2021-06-21 02:13:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void* MemArena::MapInMemoryRegion(s64 offset, size_t size, void* base)
|
|
|
|
{
|
2023-05-11 02:41:40 +02:00
|
|
|
void* retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, m_shm_fd, offset);
|
2021-06-21 02:13:00 +02:00
|
|
|
if (retval == MAP_FAILED)
|
|
|
|
{
|
|
|
|
NOTICE_LOG_FMT(MEMMAP, "mmap failed");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemArena::UnmapFromMemoryRegion(void* view, size_t size)
|
|
|
|
{
|
2023-05-11 02:41:40 +02:00
|
|
|
void* retval = mmap(view, size, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
|
|
|
|
if (retval == MAP_FAILED)
|
|
|
|
NOTICE_LOG_FMT(MEMMAP, "mmap failed");
|
2021-06-21 02:13:00 +02:00
|
|
|
}
|
2023-09-02 04:01:32 +02:00
|
|
|
|
|
|
|
LazyMemoryRegion::LazyMemoryRegion() = default;
|
|
|
|
|
|
|
|
LazyMemoryRegion::~LazyMemoryRegion()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void* LazyMemoryRegion::Create(size_t size)
|
|
|
|
{
|
|
|
|
ASSERT(!m_memory);
|
|
|
|
|
2023-09-07 14:41:24 +02:00
|
|
|
if (size == 0)
|
|
|
|
return nullptr;
|
|
|
|
|
2023-09-02 04:01:32 +02:00
|
|
|
void* memory = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2023-09-07 13:59:33 +02:00
|
|
|
if (memory == MAP_FAILED)
|
2023-09-02 04:01:32 +02:00
|
|
|
{
|
|
|
|
NOTICE_LOG_FMT(MEMMAP, "Memory allocation of {} bytes failed.", size);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_memory = memory;
|
|
|
|
m_size = size;
|
|
|
|
|
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LazyMemoryRegion::Clear()
|
|
|
|
{
|
|
|
|
ASSERT(m_memory);
|
|
|
|
|
2023-09-07 04:00:22 +02:00
|
|
|
void* new_memory = mmap(m_memory, m_size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
|
|
|
|
ASSERT(new_memory == m_memory);
|
2023-09-02 04:01:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LazyMemoryRegion::Release()
|
|
|
|
{
|
|
|
|
if (m_memory)
|
|
|
|
{
|
|
|
|
munmap(m_memory, m_size);
|
|
|
|
m_memory = nullptr;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:28:41 +02:00
|
|
|
} // namespace Common
|