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
|
|
|
|
2021-12-09 19:22:16 -07:00
|
|
|
#include "Common/MemArena.h"
|
|
|
|
|
2021-06-20 17:17:07 -06:00
|
|
|
#include <cerrno>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
2021-06-20 17:17:07 -06:00
|
|
|
#include <cstring>
|
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
|
|
|
|
2023-05-10 17:51:43 -06:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2021-06-20 17:17:07 -06:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/ashmem.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-09-01 20:01:32 -06:00
|
|
|
#include "Common/Assert.h"
|
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"
|
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/StringUtil.h"
|
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
|
|
|
#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;
|
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2021-06-20 18:13:00 -06:00
|
|
|
MemArena::MemArena() = default;
|
|
|
|
MemArena::~MemArena() = default;
|
|
|
|
|
2023-05-10 17:51:43 -06:00
|
|
|
void MemArena::GrabSHMSegment(size_t size, std::string_view base_name)
|
2008-07-12 11:40:22 -06:00
|
|
|
{
|
2023-05-10 17:51:43 -06:00
|
|
|
const std::string name = fmt::format("{}.{}", base_name, getpid());
|
|
|
|
m_shm_fd = AshmemCreateFileMapping(name.c_str(), size);
|
2023-05-10 18:41:40 -06:00
|
|
|
if (m_shm_fd < 0)
|
2020-10-23 12:41:30 -06:00
|
|
|
NOTICE_LOG_FMT(MEMMAP, "Ashmem allocation failed");
|
2008-07-12 11:40:22 -06:00
|
|
|
}
|
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
|
|
|
{
|
2023-05-10 18:41:40 -06:00
|
|
|
close(m_shm_fd);
|
2008-07-12 11:40:22 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2021-06-20 18:13:00 -06:00
|
|
|
void* MemArena::CreateView(s64 offset, size_t size)
|
2008-07-12 11:40:22 -06:00
|
|
|
{
|
2023-05-10 18:41:40 -06: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 11:40:22 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2008-07-12 11:40:22 -06:00
|
|
|
void MemArena::ReleaseView(void* view, size_t size)
|
|
|
|
{
|
2023-05-10 18:41:40 -06:00
|
|
|
munmap(view, size);
|
2008-07-12 11:40:22 -06:00
|
|
|
}
|
2008-12-07 22:30:24 -07:00
|
|
|
|
2021-06-20 18:13:00 -06:00
|
|
|
u8* MemArena::ReserveMemoryRegion(size_t memory_size)
|
2008-07-12 11:40:22 -06:00
|
|
|
{
|
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.
|
2021-06-20 17:17:07 -06:00
|
|
|
// This may be due to changes in them to support a full SELinux implementation.
|
2013-09-29 19:53:32 -06:00
|
|
|
const int flags = MAP_ANON | MAP_SHARED;
|
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
|
|
|
}
|
2023-05-10 18:41:40 -06:00
|
|
|
m_reserved_region = base;
|
|
|
|
m_reserved_region_size = memory_size;
|
2008-09-17 01:58:17 -06:00
|
|
|
return static_cast<u8*>(base);
|
2008-07-12 11:40:22 -06:00
|
|
|
}
|
2021-06-20 18:13:00 -06:00
|
|
|
|
|
|
|
void MemArena::ReleaseMemoryRegion()
|
|
|
|
{
|
2023-05-10 18:41:40 -06:00
|
|
|
if (m_reserved_region)
|
|
|
|
{
|
|
|
|
munmap(m_reserved_region, m_reserved_region_size);
|
|
|
|
m_reserved_region = nullptr;
|
|
|
|
}
|
2021-06-20 18:13:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void* MemArena::MapInMemoryRegion(s64 offset, size_t size, void* base)
|
|
|
|
{
|
2023-05-10 18:41:40 -06:00
|
|
|
void* retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, m_shm_fd, offset);
|
2021-06-20 18:13:00 -06: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-10 18:41:40 -06: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-20 18:13:00 -06:00
|
|
|
}
|
2023-09-01 20:01:32 -06:00
|
|
|
|
|
|
|
LazyMemoryRegion::LazyMemoryRegion() = default;
|
|
|
|
|
|
|
|
LazyMemoryRegion::~LazyMemoryRegion()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void* LazyMemoryRegion::Create(size_t size)
|
|
|
|
{
|
|
|
|
ASSERT(!m_memory);
|
|
|
|
|
2023-09-07 06:41:24 -06:00
|
|
|
if (size == 0)
|
|
|
|
return nullptr;
|
|
|
|
|
2023-09-01 20:01:32 -06:00
|
|
|
void* memory = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2023-09-07 05:59:33 -06:00
|
|
|
if (memory == MAP_FAILED)
|
2023-09-01 20:01:32 -06: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-06 20:00:22 -06: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-01 20:01:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void LazyMemoryRegion::Release()
|
|
|
|
{
|
|
|
|
if (m_memory)
|
|
|
|
{
|
|
|
|
munmap(m_memory, m_size);
|
|
|
|
m_memory = nullptr;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 15:28:41 -06:00
|
|
|
} // namespace Common
|