2009-09-08 12:08:10 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +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.
|
2009-04-27 02:04:31 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* 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.
|
2009-04-27 02:04:31 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2009-04-27 02:04:31 +00:00
|
|
|
*/
|
|
|
|
|
2021-09-02 16:36:36 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
2021-09-01 20:31:46 +00:00
|
|
|
#include "common/RedtapeWindows.h"
|
|
|
|
#include "common/PageFaultSource.h"
|
2022-05-09 10:11:30 +00:00
|
|
|
#include "common/Console.h"
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/Exceptions.h"
|
|
|
|
#include "common/StringUtil.h"
|
|
|
|
#include "common/AlignedMalloc.h"
|
2009-04-27 02:04:31 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
static long DoSysPageFaultExceptionFilter(EXCEPTION_POINTERS* eps)
|
2010-11-15 14:05:02 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (eps->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
|
|
|
|
// Note: This exception 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);
|
2021-09-06 18:28:26 +00:00
|
|
|
Source_PageFault->Dispatch(PageFaultInfo((uptr)eps->ExceptionRecord->ExceptionInformation[1]));
|
|
|
|
return Source_PageFault->WasHandled() ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
|
2010-11-15 14:05:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
long __stdcall SysPageFaultExceptionFilter(EXCEPTION_POINTERS* eps)
|
2012-04-07 15:35:53 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
// Prevent recursive exception filtering by catching the exception from the filter here.
|
|
|
|
// In the event that the filter causes an access violation (happened during shutdown
|
|
|
|
// because Source_PageFault was deallocated), this will allow the debugger to catch the
|
|
|
|
// exception.
|
|
|
|
// TODO: find a reliable way to debug the filter itself, I've come up with a few ways that
|
|
|
|
// work but I don't fully understand why some do and some don't.
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
return DoSysPageFaultExceptionFilter(eps);
|
|
|
|
}
|
|
|
|
__except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
|
|
|
|
{
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
2012-04-07 15:35:53 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 14:05:02 +00:00
|
|
|
void _platform_InstallSignalHandler()
|
|
|
|
{
|
2020-07-02 02:14:37 +00:00
|
|
|
#ifdef _WIN64 // We don't handle SEH properly on Win64 so use a vectored exception handler instead
|
2021-09-06 18:28:26 +00:00
|
|
|
AddVectoredExceptionHandler(true, SysPageFaultExceptionFilter);
|
2020-07-02 02:14:37 +00:00
|
|
|
#endif
|
2010-11-15 14:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
static DWORD ConvertToWinApi(const PageProtectionMode& mode)
|
2010-11-03 14:18:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
DWORD winmode = PAGE_NOACCESS;
|
|
|
|
|
|
|
|
// Windows has some really bizarre memory protection enumeration that uses bitwise
|
|
|
|
// numbering (like flags) but is in fact not a flag value. *Someone* from the early
|
|
|
|
// microsoft days wasn't a very good coder, me thinks. --air
|
|
|
|
|
|
|
|
if (mode.CanExecute())
|
|
|
|
{
|
|
|
|
winmode = mode.CanWrite() ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ;
|
|
|
|
}
|
|
|
|
else if (mode.CanRead())
|
|
|
|
{
|
|
|
|
winmode = mode.CanWrite() ? PAGE_READWRITE : PAGE_READONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return winmode;
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
void* HostSys::MmapReservePtr(void* base, size_t size)
|
2009-04-27 02:04:31 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS);
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
bool HostSys::MmapCommitPtr(void* base, size_t size, const PageProtectionMode& mode)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
void* result = VirtualAlloc(base, size, MEM_COMMIT, ConvertToWinApi(mode));
|
|
|
|
if (result)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const DWORD errcode = GetLastError();
|
|
|
|
if (errcode == ERROR_COMMITMENT_MINIMUM)
|
|
|
|
{
|
|
|
|
Console.Warning("(MmapCommit) Received windows error %u {Virtual Memory Minimum Too Low}.", ERROR_COMMITMENT_MINIMUM);
|
|
|
|
Sleep(1000); // Cut windows some time to rework its memory...
|
|
|
|
}
|
|
|
|
else if (errcode != ERROR_NOT_ENOUGH_MEMORY && errcode != ERROR_OUTOFMEMORY)
|
|
|
|
{
|
2022-05-18 13:27:23 +00:00
|
|
|
pxFailDev(("VirtualAlloc COMMIT failed: " + Exception::WinApiError().GetMsgFromWindows()).c_str());
|
2021-09-06 18:28:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
return false;
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
void HostSys::MmapResetPtr(void* base, size_t size)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
VirtualFree(base, size, MEM_DECOMMIT);
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2010-11-03 14:18:30 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
void* HostSys::MmapReserve(uptr base, size_t size)
|
2010-11-03 14:18:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return MmapReservePtr((void*)base, size);
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
bool HostSys::MmapCommit(uptr base, size_t size, const PageProtectionMode& mode)
|
2010-11-03 14:18:30 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return MmapCommitPtr((void*)base, size, mode);
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HostSys::MmapReset(uptr base, size_t size)
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
MmapResetPtr((void*)base, size);
|
2010-11-03 14:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
void* HostSys::Mmap(uptr base, size_t size)
|
2010-10-22 16:23:52 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
return VirtualAlloc((void*)base, size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HostSys::Munmap(uptr base, size_t size)
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (!base)
|
|
|
|
return;
|
|
|
|
//VirtualFree((void*)base, size, MEM_DECOMMIT);
|
|
|
|
VirtualFree((void*)base, 0, MEM_RELEASE);
|
2010-10-22 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:28:26 +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
|
|
|
pxAssert((size & (__pagesize - 1)) == 0);
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
DWORD OldProtect; // enjoy my uselessness, yo!
|
|
|
|
if (!VirtualProtect(baseaddr, size, ConvertToWinApi(mode), &OldProtect))
|
|
|
|
{
|
|
|
|
Exception::WinApiError apiError;
|
|
|
|
|
|
|
|
apiError.SetDiagMsg(
|
2022-05-18 13:27:23 +00:00
|
|
|
StringUtil::StdStringFromFormat("VirtualProtect failed @ 0x%08X -> 0x%08X (mode=%s)",
|
2021-09-06 18:28:26 +00:00
|
|
|
baseaddr, (uptr)baseaddr + size, mode.ToString().c_str()));
|
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
pxFailDev(apiError.FormatDiagnosticMessage().c_str());
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2009-04-27 02:04:31 +00:00
|
|
|
}
|
2021-09-02 16:36:36 +00:00
|
|
|
#endif
|