2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:58:25 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program 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 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2021-03-03 05:49:21 +00:00
|
|
|
#include "ppsspp_config.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
#include "MemoryUtil.h"
|
|
|
|
#include "ABI.h"
|
|
|
|
#include "Thunk.h"
|
|
|
|
|
|
|
|
#define THUNK_ARENA_SIZE 1024*1024*1
|
|
|
|
|
2017-08-30 23:14:51 +00:00
|
|
|
namespace {
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2021-03-03 05:49:21 +00:00
|
|
|
#if !PPSSPP_ARCH(AMD64)
|
2017-08-30 23:14:51 +00:00
|
|
|
alignas(32) static u8 saved_fp_state[16 * 4 * 4];
|
|
|
|
alignas(32) static u8 saved_gpr_state[16 * 8];
|
2012-11-01 15:19:01 +00:00
|
|
|
static u16 saved_mxcsr;
|
2014-02-15 06:59:57 +00:00
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
using namespace Gen;
|
|
|
|
|
|
|
|
void ThunkManager::Init()
|
|
|
|
{
|
2021-03-03 05:49:21 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64)
|
2014-02-17 05:05:43 +00:00
|
|
|
// Account for the return address and "home space" on Windows (which needs to be at the bottom.)
|
|
|
|
const int stackOffset = ThunkStackOffset();
|
2014-02-15 06:59:57 +00:00
|
|
|
int stackPosition;
|
|
|
|
#endif
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
AllocCodeSpace(THUNK_ARENA_SIZE);
|
2016-08-28 12:52:08 +00:00
|
|
|
BeginWrite();
|
2012-11-01 15:19:01 +00:00
|
|
|
save_regs = GetCodePtr();
|
2021-03-03 05:49:21 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64)
|
2014-02-15 06:59:57 +00:00
|
|
|
for (int i = 2; i < ABI_GetNumXMMRegs(); i++)
|
|
|
|
MOVAPS(MDisp(RSP, stackOffset + (i - 2) * 16), (X64Reg)(XMM0 + i));
|
|
|
|
stackPosition = (ABI_GetNumXMMRegs() - 2) * 2;
|
|
|
|
STMXCSR(MDisp(RSP, stackOffset + (stackPosition++ * 8)));
|
|
|
|
MOV(64, MDisp(RSP, stackOffset + (stackPosition++ * 8)), R(RCX));
|
|
|
|
MOV(64, MDisp(RSP, stackOffset + (stackPosition++ * 8)), R(RDX));
|
|
|
|
MOV(64, MDisp(RSP, stackOffset + (stackPosition++ * 8)), R(R8) );
|
|
|
|
MOV(64, MDisp(RSP, stackOffset + (stackPosition++ * 8)), R(R9) );
|
|
|
|
MOV(64, MDisp(RSP, stackOffset + (stackPosition++ * 8)), R(R10));
|
|
|
|
MOV(64, MDisp(RSP, stackOffset + (stackPosition++ * 8)), R(R11));
|
2012-11-01 15:19:01 +00:00
|
|
|
#ifndef _WIN32
|
2014-02-15 06:59:57 +00:00
|
|
|
MOV(64, MDisp(RSP, stackOffset + (stackPosition++ * 8)), R(RSI));
|
|
|
|
MOV(64, MDisp(RSP, stackOffset + (stackPosition++ * 8)), R(RDI));
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
2014-02-15 06:59:57 +00:00
|
|
|
MOV(64, MDisp(RSP, stackOffset + (stackPosition++ * 8)), R(RBX));
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
2014-02-15 06:59:57 +00:00
|
|
|
for (int i = 2; i < ABI_GetNumXMMRegs(); i++)
|
|
|
|
MOVAPS(M(saved_fp_state + i * 16), (X64Reg)(XMM0 + i));
|
|
|
|
STMXCSR(M(&saved_mxcsr));
|
2012-11-01 15:19:01 +00:00
|
|
|
MOV(32, M(saved_gpr_state + 0 ), R(RCX));
|
|
|
|
MOV(32, M(saved_gpr_state + 4 ), R(RDX));
|
|
|
|
#endif
|
|
|
|
RET();
|
2014-02-15 06:59:57 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
load_regs = GetCodePtr();
|
2021-03-03 05:49:21 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64)
|
2014-02-15 06:59:57 +00:00
|
|
|
for (int i = 2; i < ABI_GetNumXMMRegs(); i++)
|
|
|
|
MOVAPS((X64Reg)(XMM0 + i), MDisp(RSP, stackOffset + (i - 2) * 16));
|
|
|
|
stackPosition = (ABI_GetNumXMMRegs() - 2) * 2;
|
|
|
|
LDMXCSR(MDisp(RSP, stackOffset + (stackPosition++ * 8)));
|
|
|
|
MOV(64, R(RCX), MDisp(RSP, stackOffset + (stackPosition++ * 8)));
|
|
|
|
MOV(64, R(RDX), MDisp(RSP, stackOffset + (stackPosition++ * 8)));
|
|
|
|
MOV(64, R(R8) , MDisp(RSP, stackOffset + (stackPosition++ * 8)));
|
|
|
|
MOV(64, R(R9) , MDisp(RSP, stackOffset + (stackPosition++ * 8)));
|
|
|
|
MOV(64, R(R10), MDisp(RSP, stackOffset + (stackPosition++ * 8)));
|
|
|
|
MOV(64, R(R11), MDisp(RSP, stackOffset + (stackPosition++ * 8)));
|
2012-11-01 15:19:01 +00:00
|
|
|
#ifndef _WIN32
|
2014-02-15 06:59:57 +00:00
|
|
|
MOV(64, R(RSI), MDisp(RSP, stackOffset + (stackPosition++ * 8)));
|
|
|
|
MOV(64, R(RDI), MDisp(RSP, stackOffset + (stackPosition++ * 8)));
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
2014-02-15 06:59:57 +00:00
|
|
|
MOV(64, R(RBX), MDisp(RSP, stackOffset + (stackPosition++ * 8)));
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
2014-02-15 06:59:57 +00:00
|
|
|
LDMXCSR(M(&saved_mxcsr));
|
|
|
|
for (int i = 2; i < ABI_GetNumXMMRegs(); i++)
|
|
|
|
MOVAPS((X64Reg)(XMM0 + i), M(saved_fp_state + i * 16));
|
2012-11-01 15:19:01 +00:00
|
|
|
MOV(32, R(RCX), M(saved_gpr_state + 0 ));
|
|
|
|
MOV(32, R(RDX), M(saved_gpr_state + 4 ));
|
|
|
|
#endif
|
|
|
|
RET();
|
2016-08-28 12:52:08 +00:00
|
|
|
EndWrite();
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThunkManager::Reset()
|
|
|
|
{
|
|
|
|
thunks.clear();
|
2017-05-26 13:39:27 +00:00
|
|
|
ResetCodePtr(0);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThunkManager::Shutdown()
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
FreeCodeSpace();
|
|
|
|
}
|
|
|
|
|
2014-02-15 06:59:57 +00:00
|
|
|
int ThunkManager::ThunkBytesNeeded()
|
|
|
|
{
|
|
|
|
int space = (ABI_GetNumXMMRegs() - 2) * 16;
|
2021-03-03 05:49:21 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64)
|
2014-02-15 06:59:57 +00:00
|
|
|
// MXCSR
|
|
|
|
space += 8;
|
|
|
|
space += 7 * 8;
|
|
|
|
#ifndef _WIN32
|
|
|
|
space += 2 * 8;
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
// MXCSR
|
|
|
|
space += 4;
|
|
|
|
space += 2 * 4;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Round up to the nearest 16 just in case.
|
|
|
|
return (space + 15) & ~15;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ThunkManager::ThunkStackOffset()
|
|
|
|
{
|
2021-03-03 05:49:21 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64)
|
2014-02-15 06:59:57 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return 0x28;
|
|
|
|
#else
|
|
|
|
return 0x8;
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-08-28 12:52:08 +00:00
|
|
|
const void *ThunkManager::ProtectFunction(const void *function, int num_params) {
|
2014-01-18 17:57:13 +00:00
|
|
|
std::map<const void *, const u8 *>::iterator iter;
|
2012-11-01 15:19:01 +00:00
|
|
|
iter = thunks.find(function);
|
|
|
|
if (iter != thunks.end())
|
2014-01-18 17:57:13 +00:00
|
|
|
return (const void *)iter->second;
|
2020-07-19 17:49:20 +00:00
|
|
|
|
|
|
|
_assert_msg_(region != nullptr, "Can't protect functions before the emu is started.");
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2016-08-28 12:52:08 +00:00
|
|
|
BeginWrite();
|
2012-11-01 15:19:01 +00:00
|
|
|
const u8 *call_point = GetCodePtr();
|
2014-02-24 00:56:27 +00:00
|
|
|
Enter(this, true);
|
2014-02-17 03:18:08 +00:00
|
|
|
|
2021-03-03 05:49:21 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64)
|
2014-01-18 17:57:13 +00:00
|
|
|
ABI_CallFunction(function);
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
|
|
|
// Since parameters are in the previous stack frame, not in registers, this takes some
|
|
|
|
// trickery : we simply re-push the parameters. might not be optimal, but that doesn't really
|
|
|
|
// matter.
|
|
|
|
ABI_AlignStack(num_params * 4);
|
|
|
|
unsigned int alignedSize = ABI_GetAlignedFrameSize(num_params * 4);
|
|
|
|
for (int i = 0; i < num_params; i++) {
|
|
|
|
// ESP is changing, so we do not need i
|
|
|
|
PUSH(32, MDisp(ESP, alignedSize - 4));
|
|
|
|
}
|
|
|
|
CALL(function);
|
|
|
|
ABI_RestoreStack(num_params * 4);
|
|
|
|
#endif
|
|
|
|
|
2014-02-24 00:56:27 +00:00
|
|
|
Leave(this, true);
|
2014-02-17 03:18:08 +00:00
|
|
|
RET();
|
2016-08-28 12:52:08 +00:00
|
|
|
EndWrite();
|
2014-02-17 03:18:08 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
thunks[function] = call_point;
|
2014-01-18 17:57:13 +00:00
|
|
|
return (const void *)call_point;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2014-02-17 03:18:08 +00:00
|
|
|
|
2014-02-24 00:56:27 +00:00
|
|
|
void ThunkManager::Enter(ThunkEmitter *emit, bool withinCall)
|
2014-02-17 03:18:08 +00:00
|
|
|
{
|
2021-03-03 05:49:21 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64)
|
2014-02-17 03:18:08 +00:00
|
|
|
// Make sure to align stack.
|
2014-02-24 00:56:27 +00:00
|
|
|
emit->SUB(64, R(ESP), Imm32(ThunkStackOffset() + ThunkBytesNeeded() + (withinCall ? 0 : 8)));
|
2014-02-17 03:18:08 +00:00
|
|
|
emit->ABI_CallFunction(save_regs);
|
|
|
|
#else
|
|
|
|
emit->CALL((const void *)save_regs);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-24 00:56:27 +00:00
|
|
|
void ThunkManager::Leave(ThunkEmitter *emit, bool withinCall)
|
2014-02-17 03:18:08 +00:00
|
|
|
{
|
2021-03-03 05:49:21 +00:00
|
|
|
#if PPSSPP_ARCH(AMD64)
|
2014-02-17 03:18:08 +00:00
|
|
|
emit->ABI_CallFunction(load_regs);
|
2014-02-24 00:56:27 +00:00
|
|
|
emit->ADD(64, R(ESP), Imm32(ThunkStackOffset() + ThunkBytesNeeded() + (withinCall ? 0 : 8)));
|
2014-02-17 03:18:08 +00:00
|
|
|
#else
|
|
|
|
emit->CALL((void*)load_regs);
|
|
|
|
#endif
|
|
|
|
}
|