2009-11-16 00:40:09 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2009-11-16 00:40:09 +00:00
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 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 for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-09-03 10:43:33 +00:00
|
|
|
#if !defined(_WIN32)
|
2022-05-18 13:27:23 +00:00
|
|
|
#include <cstdio>
|
2009-11-16 00:40:09 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <signal.h>
|
2010-10-27 23:58:10 +00:00
|
|
|
#include <errno.h>
|
2022-10-12 13:57:53 +00:00
|
|
|
#include <fcntl.h>
|
2012-03-17 11:21:51 +00:00
|
|
|
#include <unistd.h>
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "fmt/core.h"
|
|
|
|
|
2022-10-29 03:39:19 +00:00
|
|
|
#include "common/Align.h"
|
2021-09-01 20:31:46 +00:00
|
|
|
#include "common/PageFaultSource.h"
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/Assertions.h"
|
2022-05-09 10:11:30 +00:00
|
|
|
#include "common/Console.h"
|
|
|
|
#include "common/Exceptions.h"
|
2021-09-01 20:31:46 +00:00
|
|
|
|
2015-11-17 17:38:26 +00:00
|
|
|
// Apple uses the MAP_ANON define instead of MAP_ANONYMOUS, but they mean
|
|
|
|
// the same thing.
|
|
|
|
#if defined(__APPLE__) && !defined(MAP_ANONYMOUS)
|
2016-11-12 15:28:37 +00:00
|
|
|
#define MAP_ANONYMOUS MAP_ANON
|
2015-11-17 17:38:26 +00:00
|
|
|
#endif
|
|
|
|
|
2022-10-29 03:39:19 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifndef __APPLE__
|
|
|
|
#include <ucontext.h>
|
|
|
|
#endif
|
|
|
|
|
2010-11-15 14:05:02 +00:00
|
|
|
extern void SignalExit(int sig);
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
static const uptr m_pagemask = getpagesize() - 1;
|
2010-11-15 14:05:02 +00:00
|
|
|
|
2022-10-29 03:39:19 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
static struct sigaction s_old_sigbus_action;
|
2022-11-19 13:18:58 +00:00
|
|
|
#else
|
|
|
|
static struct sigaction s_old_sigsegv_action;
|
2022-10-29 03:39:19 +00:00
|
|
|
#endif
|
|
|
|
|
2010-11-15 14:05:02 +00:00
|
|
|
// Linux implementation of SIGSEGV handler. Bind it using sigaction().
|
2022-10-29 03:39:19 +00:00
|
|
|
static void SysPageFaultSignalFilter(int signal, siginfo_t* siginfo, void* ctx)
|
2010-11-15 14:05:02 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
// [TODO] : Add a thread ID filter to the Linux Signal handler here.
|
|
|
|
// Rationale: On windows, the __try/__except model allows per-thread specific behavior
|
|
|
|
// for page fault handling. On linux, there is a single signal handler for the whole
|
|
|
|
// process, but the handler is executed by the thread that caused the exception.
|
2010-11-15 14:05:02 +00:00
|
|
|
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// Stdio Usage note: SIGSEGV handling is a synchronous in-thread signal. It is done
|
|
|
|
// from the context of the current thread and stackframe. So long as the thread is not
|
|
|
|
// the main/ui thread, use of the px assertion system should be safe. Use of stdio should
|
|
|
|
// be safe even on the main thread.
|
|
|
|
// (in other words, stdio limitations only really apply to process-level asynchronous
|
|
|
|
// signals)
|
2010-11-17 03:18:36 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// Note: Use of stdio functions isn't safe here. Avoid console logs,
|
|
|
|
// assertions, file logs, or just about anything else useful.
|
2010-11-15 14:05:02 +00:00
|
|
|
|
2022-10-29 03:39:19 +00:00
|
|
|
#if defined(__APPLE__) && defined(__x86_64__)
|
|
|
|
void* const exception_pc = reinterpret_cast<void*>(static_cast<ucontext_t*>(ctx)->uc_mcontext->__ss.__rip);
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
void* const exception_pc = reinterpret_cast<void*>(static_cast<ucontext_t*>(ctx)->uc_mcontext.gregs[REG_RIP]);
|
|
|
|
#else
|
|
|
|
void* const exception_pc = nullptr;
|
|
|
|
#endif
|
2011-08-12 02:31:49 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// Note: This signal can be accessed by the EE or MTVU thread
|
|
|
|
// Source_PageFault is a global variable with its own state information
|
|
|
|
// so for now we lock this exception code unless someone can fix this better...
|
2022-05-09 10:11:30 +00:00
|
|
|
std::unique_lock lock(PageFault_Mutex);
|
2011-08-12 02:31:49 +00:00
|
|
|
|
2022-10-29 03:39:19 +00:00
|
|
|
Source_PageFault->Dispatch(PageFaultInfo((uptr)exception_pc, (uptr)siginfo->si_addr & ~m_pagemask));
|
2010-11-15 14:05:02 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
// resumes execution right where we left off (re-executes instruction that
|
|
|
|
// caused the SIGSEGV).
|
|
|
|
if (Source_PageFault->WasHandled())
|
|
|
|
return;
|
2010-11-15 14:05:02 +00:00
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
std::fprintf(stderr, "Unhandled page fault @ 0x%08x", siginfo->si_addr);
|
|
|
|
pxFailRel("Unhandled page fault");
|
2010-11-15 14:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _platform_InstallSignalHandler()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
Console.WriteLn("Installing POSIX SIGSEGV handler...");
|
|
|
|
struct sigaction sa;
|
2010-11-15 14:05:02 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_SIGINFO;
|
|
|
|
sa.sa_sigaction = SysPageFaultSignalFilter;
|
2022-10-29 03:39:19 +00:00
|
|
|
#if defined(__APPLE__)
|
2021-09-06 18:28:26 +00:00
|
|
|
// MacOS uses SIGBUS for memory permission violations
|
2022-10-29 03:39:19 +00:00
|
|
|
sigaction(SIGBUS, &sa, &s_old_sigbus_action);
|
2020-05-24 06:19:47 +00:00
|
|
|
#else
|
2022-10-29 03:39:19 +00:00
|
|
|
sigaction(SIGSEGV, &sa, &s_old_sigsegv_action);
|
2020-05-24 06:19:47 +00:00
|
|
|
#endif
|
2010-11-15 14:05:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
static __ri uint LinuxProt(const PageProtectionMode& mode)
|
2010-11-05 01:33:01 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
u32 lnxmode = 0;
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
if (mode.CanWrite())
|
|
|
|
lnxmode |= PROT_WRITE;
|
|
|
|
if (mode.CanRead())
|
|
|
|
lnxmode |= PROT_READ;
|
|
|
|
if (mode.CanExecute())
|
|
|
|
lnxmode |= PROT_EXEC | PROT_READ;
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
return lnxmode;
|
2010-11-05 01:33:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void* HostSys::Mmap(void* base, size_t size, const PageProtectionMode& mode)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2022-05-18 13:27:23 +00:00
|
|
|
pxAssertDev((size & (__pagesize - 1)) == 0, "Size is page aligned");
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
if (mode.IsNone())
|
|
|
|
return nullptr;
|
2022-05-19 14:01:56 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
const u32 prot = LinuxProt(mode);
|
2022-05-19 14:01:56 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
u32 flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
|
|
|
if (base)
|
|
|
|
flags |= MAP_FIXED;
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void* res = mmap(base, size, prot, flags, -1, 0);
|
|
|
|
if (res == MAP_FAILED)
|
|
|
|
return nullptr;
|
2010-11-03 14:18:30 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
return res;
|
|
|
|
}
|
2010-11-05 01:33:01 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void HostSys::Munmap(void* base, size_t size)
|
|
|
|
{
|
|
|
|
if (!base)
|
|
|
|
return;
|
2010-11-05 01:33:01 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
munmap((void*)base, size);
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void HostSys::MemProtect(void* baseaddr, size_t size, const PageProtectionMode& mode)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2022-05-18 13:27:23 +00:00
|
|
|
pxAssertDev((size & (__pagesize - 1)) == 0, "Size is page aligned");
|
2016-11-12 15:28:37 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
const u32 lnxmode = LinuxProt(mode);
|
2016-11-12 15:28:37 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
const int result = mprotect(baseaddr, size, lnxmode);
|
|
|
|
if (result != 0)
|
|
|
|
pxFail("mprotect() failed");
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
std::string HostSys::GetFileMappingName(const char* prefix)
|
2010-11-03 14:18:30 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
const unsigned pid = static_cast<unsigned>(getpid());
|
2022-10-27 14:51:37 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
// FreeBSD's shm_open(3) requires name to be absolute
|
|
|
|
return fmt::format("/tmp/{}_{}", prefix, pid);
|
|
|
|
#else
|
2022-10-12 13:57:53 +00:00
|
|
|
return fmt::format("{}_{}", prefix, pid);
|
2022-10-27 14:51:37 +00:00
|
|
|
#endif
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void* HostSys::CreateSharedMemory(const char* name, size_t size)
|
2010-11-03 14:18:30 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
const int fd = shm_open(name, O_CREAT | O_EXCL | O_RDWR, 0600);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
std::fprintf(stderr, "shm_open failed: %d\n", errno);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're not going to be opening this mapping in other processes, so remove the file
|
|
|
|
shm_unlink(name);
|
|
|
|
|
|
|
|
// ensure it's the correct size
|
2022-10-27 14:56:17 +00:00
|
|
|
#if !defined(__APPLE__) && !defined(__FreeBSD__)
|
2022-10-12 13:57:53 +00:00
|
|
|
if (ftruncate64(fd, static_cast<off64_t>(size)) < 0)
|
|
|
|
#else
|
|
|
|
if (ftruncate(fd, static_cast<off_t>(size)) < 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
std::fprintf(stderr, "ftruncate64(%zu) failed: %d\n", size, errno);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<void*>(static_cast<intptr_t>(fd));
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void HostSys::DestroySharedMemory(void* ptr)
|
2010-11-03 14:18:30 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
close(static_cast<int>(reinterpret_cast<intptr_t>(ptr)));
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void* HostSys::MapSharedMemory(void* handle, size_t offset, void* baseaddr, size_t size, const PageProtectionMode& mode)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
const uint lnxmode = LinuxProt(mode);
|
2010-10-22 16:23:52 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
const int flags = (baseaddr != nullptr) ? (MAP_SHARED | MAP_FIXED) : MAP_SHARED;
|
|
|
|
void* ptr = mmap(baseaddr, size, lnxmode, flags, static_cast<int>(reinterpret_cast<intptr_t>(handle)), static_cast<off_t>(offset));
|
|
|
|
if (ptr == MAP_FAILED)
|
|
|
|
return nullptr;
|
2010-10-22 16:23:52 +00:00
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
return ptr;
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:57:53 +00:00
|
|
|
void HostSys::UnmapSharedMemory(void* baseaddr, size_t size)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2022-10-12 13:57:53 +00:00
|
|
|
if (mmap(baseaddr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) == MAP_FAILED)
|
|
|
|
pxFailRel("Failed to unmap shared memory");
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 03:39:19 +00:00
|
|
|
SharedMemoryMappingArea::SharedMemoryMappingArea(u8* base_ptr, size_t size, size_t num_pages)
|
|
|
|
: m_base_ptr(base_ptr)
|
|
|
|
, m_size(size)
|
|
|
|
, m_num_pages(num_pages)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedMemoryMappingArea::~SharedMemoryMappingArea()
|
|
|
|
{
|
|
|
|
pxAssertRel(m_num_mappings == 0, "No mappings left");
|
|
|
|
|
|
|
|
if (munmap(m_base_ptr, m_size) != 0)
|
|
|
|
pxFailRel("Failed to release shared memory area");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<SharedMemoryMappingArea> SharedMemoryMappingArea::Create(size_t size)
|
|
|
|
{
|
|
|
|
pxAssertRel(Common::IsAlignedPow2(size, __pagesize), "Size is page aligned");
|
|
|
|
|
|
|
|
void* alloc = mmap(nullptr, size, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
|
|
|
if (alloc == MAP_FAILED)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return std::unique_ptr<SharedMemoryMappingArea>(new SharedMemoryMappingArea(static_cast<u8*>(alloc), size, size / __pagesize));
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* SharedMemoryMappingArea::Map(void* file_handle, size_t file_offset, void* map_base, size_t map_size, const PageProtectionMode& mode)
|
|
|
|
{
|
|
|
|
pxAssert(static_cast<u8*>(map_base) >= m_base_ptr && static_cast<u8*>(map_base) < (m_base_ptr + m_size));
|
|
|
|
|
|
|
|
const uint lnxmode = LinuxProt(mode);
|
|
|
|
void* const ptr = mmap(map_base, map_size, lnxmode, MAP_SHARED | MAP_FIXED,
|
|
|
|
static_cast<int>(reinterpret_cast<intptr_t>(file_handle)), static_cast<off_t>(file_offset));
|
|
|
|
if (ptr == MAP_FAILED)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
m_num_mappings++;
|
|
|
|
return static_cast<u8*>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SharedMemoryMappingArea::Unmap(void* map_base, size_t map_size)
|
|
|
|
{
|
|
|
|
pxAssert(static_cast<u8*>(map_base) >= m_base_ptr && static_cast<u8*>(map_base) < (m_base_ptr + m_size));
|
|
|
|
|
|
|
|
if (mmap(map_base, map_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0) == MAP_FAILED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_num_mappings--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-01 20:31:46 +00:00
|
|
|
#endif
|