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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "../PrecompiledHeader.h"
|
2010-11-15 14:05:02 +00:00
|
|
|
#include "PageFaultSource.h"
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2010-11-16 05:32:31 +00:00
|
|
|
#include <wx/thread.h>
|
|
|
|
|
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>
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2010-11-15 14:05:02 +00:00
|
|
|
extern void SignalExit(int sig);
|
|
|
|
|
|
|
|
static const uptr m_pagemask = getpagesize()-1;
|
|
|
|
|
|
|
|
// Linux implementation of SIGSEGV handler. Bind it using sigaction().
|
|
|
|
static void SysPageFaultSignalFilter( int signal, siginfo_t *siginfo, void * )
|
|
|
|
{
|
|
|
|
// [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.
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2010-11-15 14:05:02 +00:00
|
|
|
// Note: Use of stdio functions isn't safe here. Avoid console logs,
|
|
|
|
// assertions, file logs, or just about anything else useful.
|
|
|
|
|
2011-08-12 02:31:49 +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...
|
|
|
|
Threading::ScopedLock lock(PageFault_Mutex);
|
|
|
|
|
2010-11-15 14:05:02 +00:00
|
|
|
Source_PageFault->Dispatch( PageFaultInfo( (uptr)siginfo->si_addr & ~m_pagemask ) );
|
|
|
|
|
|
|
|
// resumes execution right where we left off (re-executes instruction that
|
|
|
|
// caused the SIGSEGV).
|
|
|
|
if (Source_PageFault->WasHandled()) return;
|
|
|
|
|
|
|
|
if (!wxThread::IsMain())
|
|
|
|
{
|
|
|
|
pxFailRel(pxsFmt("Unhandled page fault @ 0x%08x", siginfo->si_addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad mojo! Completely invalid address.
|
|
|
|
// Instigate a trap if we're in a debugger, and if not then do a SIGKILL.
|
|
|
|
|
|
|
|
wxTrap();
|
|
|
|
if (!IsDebugBuild) raise( SIGKILL );
|
|
|
|
}
|
|
|
|
|
|
|
|
void _platform_InstallSignalHandler()
|
|
|
|
{
|
|
|
|
Console.WriteLn("Installing POSIX SIGSEGV handler...");
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_SIGINFO;
|
|
|
|
sa.sa_sigaction = SysPageFaultSignalFilter;
|
|
|
|
sigaction(SIGSEGV, &sa, NULL);
|
|
|
|
}
|
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
static __ri void PageSizeAssertionTest( size_t size )
|
2009-11-16 00:40:09 +00:00
|
|
|
{
|
2010-10-27 23:58:10 +00:00
|
|
|
pxAssertMsg( (__pagesize == getpagesize()), pxsFmt(
|
2010-10-22 16:23:52 +00:00
|
|
|
"Internal system error: Operating system pagesize does not match compiled pagesize.\n\t"
|
|
|
|
L"\tOS Page Size: 0x%x (%d), Compiled Page Size: 0x%x (%u)",
|
|
|
|
getpagesize(), getpagesize(), __pagesize, __pagesize )
|
|
|
|
);
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
pxAssertDev( (size & (__pagesize-1)) == 0, pxsFmt(
|
|
|
|
L"Memory block size must be a multiple of the target platform's page size.\n"
|
|
|
|
L"\tPage Size: 0x%x (%u), Block Size: 0x%x (%u)",
|
|
|
|
__pagesize, __pagesize, size, size )
|
|
|
|
);
|
|
|
|
}
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2010-11-05 01:33:01 +00:00
|
|
|
// returns FALSE if the mprotect call fails with an ENOMEM.
|
|
|
|
// Raises assertions on other types of POSIX errors (since those typically reflect invalid object
|
|
|
|
// or memory states).
|
|
|
|
static bool _memprotect( void* baseaddr, size_t size, const PageProtectionMode& mode )
|
|
|
|
{
|
|
|
|
PageSizeAssertionTest(size);
|
|
|
|
|
|
|
|
uint lnxmode = 0;
|
|
|
|
|
|
|
|
if (mode.CanWrite()) lnxmode |= PROT_WRITE;
|
|
|
|
if (mode.CanRead()) lnxmode |= PROT_READ;
|
|
|
|
if (mode.CanExecute()) lnxmode |= PROT_EXEC | PROT_READ;
|
|
|
|
|
|
|
|
const int result = mprotect( baseaddr, size, lnxmode );
|
|
|
|
|
|
|
|
if (result == 0) return true;
|
2010-11-17 03:18:36 +00:00
|
|
|
|
2010-11-05 01:33:01 +00:00
|
|
|
switch(errno)
|
|
|
|
{
|
|
|
|
case EINVAL:
|
|
|
|
pxFailDev(pxsFmt(L"mprotect returned EINVAL @ 0x%08X -> 0x%08X (mode=%s)",
|
|
|
|
baseaddr, (uptr)baseaddr+size, mode.ToString().c_str())
|
|
|
|
);
|
|
|
|
break;
|
2010-11-17 03:18:36 +00:00
|
|
|
|
2010-11-05 01:33:01 +00:00
|
|
|
case EACCES:
|
|
|
|
pxFailDev(pxsFmt(L"mprotect returned EACCES @ 0x%08X -> 0x%08X (mode=%s)",
|
|
|
|
baseaddr, (uptr)baseaddr+size, mode.ToString().c_str())
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENOMEM:
|
|
|
|
// caller handles assertion or exception, or whatever.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-03 14:18:30 +00:00
|
|
|
void* HostSys::MmapReservePtr(void* base, size_t size)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
|
|
|
PageSizeAssertionTest(size);
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
// On linux a reserve-without-commit is performed by using mmap on a read-only
|
|
|
|
// or anonymous source, with PROT_NONE (no-access) permission. Since the mapping
|
|
|
|
// is completely inaccessible, the OS will simply reserve it and will not put it
|
|
|
|
// against the commit table.
|
2010-11-03 14:18:30 +00:00
|
|
|
return mmap(base, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2010-11-05 01:33:01 +00:00
|
|
|
bool HostSys::MmapCommitPtr(void* base, size_t size, const PageProtectionMode& mode)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
|
|
|
// In linux, reserved memory is automatically committed when its permissions are
|
2010-11-03 14:18:30 +00:00
|
|
|
// changed to something other than PROT_NONE. If the user is committing memory
|
|
|
|
// as PROT_NONE, then just ignore this call (memory will be committed automatically
|
|
|
|
// later when the user changes permissions to something useful via calls to MemProtect).
|
|
|
|
|
2010-11-06 05:21:06 +00:00
|
|
|
if (mode.IsNone()) return false;
|
2010-11-05 01:33:01 +00:00
|
|
|
|
|
|
|
if (_memprotect( base, size, mode )) return true;
|
|
|
|
|
|
|
|
if (!pxDoOutOfMemory) return false;
|
|
|
|
pxDoOutOfMemory(size);
|
|
|
|
return _memprotect( base, size, mode );
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2010-11-03 14:18:30 +00:00
|
|
|
void HostSys::MmapResetPtr(void* base, size_t size)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
|
|
|
// On linux the only way to reset the memory is to unmap and remap it as PROT_NONE.
|
|
|
|
// That forces linux to unload all committed pages and start from scratch.
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
// FIXME: Ideally this code would have some threading lock on it to prevent any other
|
|
|
|
// malloc/free code in the current process from interfering with the operation, but I
|
|
|
|
// can't think of any good way to do that. (generally it shouldn't be a problem in
|
|
|
|
// PCSX2 anyway, since MmapReset is only called when the ps2vm is suspended; so that
|
|
|
|
// pretty well stops all PCSX2 threads anyway).
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2010-10-22 16:23:52 +00:00
|
|
|
Munmap(base, size);
|
2010-11-27 16:47:43 +00:00
|
|
|
void* result = MmapReservePtr(base, size);
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2010-11-28 07:23:24 +00:00
|
|
|
pxAssertRel ((uptr)result == (uptr)base, pxsFmt(
|
2010-10-22 16:23:52 +00:00
|
|
|
"Virtual memory decommit failed: memory at 0x%08X -> 0x%08X could not be remapped. "
|
2010-11-16 05:32:31 +00:00
|
|
|
"This is likely caused by multi-thread memory contention.", base, (uptr)base+size
|
2010-10-22 16:23:52 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2010-11-03 14:18:30 +00:00
|
|
|
void* HostSys::MmapReserve(uptr base, size_t size)
|
|
|
|
{
|
|
|
|
return MmapReservePtr((void*)base, size);
|
|
|
|
}
|
|
|
|
|
2010-11-05 01:33:01 +00:00
|
|
|
bool HostSys::MmapCommit(uptr base, size_t size, const PageProtectionMode& mode)
|
2010-11-03 14:18:30 +00:00
|
|
|
{
|
2010-11-05 01:33:01 +00:00
|
|
|
return MmapCommitPtr( (void*)base, size, mode );
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HostSys::MmapReset(uptr base, size_t size)
|
|
|
|
{
|
|
|
|
MmapResetPtr((void*)base, size);
|
|
|
|
}
|
|
|
|
|
2010-10-27 23:58:10 +00:00
|
|
|
void* HostSys::Mmap(uptr base, size_t size)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
|
|
|
PageSizeAssertionTest(size);
|
|
|
|
|
|
|
|
// MAP_ANONYMOUS - means we have no associated file handle (or device).
|
|
|
|
|
2010-10-27 23:58:10 +00:00
|
|
|
return mmap((void*)base, size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2010-10-27 23:58:10 +00:00
|
|
|
void HostSys::Munmap(uptr base, size_t size)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2010-11-17 03:18:36 +00:00
|
|
|
if (!base) return;
|
2010-10-27 23:58:10 +00:00
|
|
|
munmap((void*)base, size);
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HostSys::MemProtect( void* baseaddr, size_t size, const PageProtectionMode& mode )
|
|
|
|
{
|
2010-11-05 01:33:01 +00:00
|
|
|
if (!_memprotect(baseaddr, size, mode))
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2010-11-06 05:21:06 +00:00
|
|
|
throw Exception::OutOfMemory( L"MemProtect" )
|
2010-11-05 01:33:01 +00:00
|
|
|
.SetDiagMsg(pxsFmt( L"mprotect failed @ 0x%08X -> 0x%08X (mode=%s)",
|
|
|
|
baseaddr, (uptr)baseaddr+size, mode.ToString().c_str()
|
|
|
|
)
|
|
|
|
);
|
2009-11-16 00:40:09 +00:00
|
|
|
}
|
|
|
|
}
|