2016-07-01 14:01:06 +01:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2016 MerryMage
|
|
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
|
|
* General Public License version 2 or any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
#include <xbyak.h>
|
|
|
|
|
|
|
|
#include "backend_x64/abi.h"
|
2016-08-07 18:08:48 +01:00
|
|
|
#include "backend_x64/block_of_code.h"
|
2016-07-01 14:01:06 +01:00
|
|
|
#include "backend_x64/jitstate.h"
|
2016-08-24 20:07:08 +01:00
|
|
|
#include "common/assert.h"
|
2016-09-01 00:06:40 +01:00
|
|
|
#include "dynarmic/callbacks.h"
|
2016-07-01 14:01:06 +01:00
|
|
|
|
|
|
|
namespace Dynarmic {
|
|
|
|
namespace BackendX64 {
|
|
|
|
|
2016-09-01 00:06:40 +01:00
|
|
|
BlockOfCode::BlockOfCode(UserCallbacks cb) : Xbyak::CodeGenerator(128 * 1024 * 1024), cb(cb) {
|
2016-09-01 09:47:09 +01:00
|
|
|
ClearCache();
|
2016-08-07 18:08:48 +01:00
|
|
|
}
|
|
|
|
|
2016-09-01 09:47:09 +01:00
|
|
|
void BlockOfCode::ClearCache() {
|
2016-08-25 02:59:42 +01:00
|
|
|
consts.~Consts();
|
|
|
|
new (&consts) Consts();
|
2016-08-24 20:07:08 +01:00
|
|
|
reset();
|
2016-08-06 17:12:40 +01:00
|
|
|
GenConstants();
|
2016-07-01 14:01:06 +01:00
|
|
|
GenRunCode();
|
2016-08-13 00:10:23 +01:00
|
|
|
GenReturnFromRunCode();
|
2016-09-01 00:06:40 +01:00
|
|
|
GenMemoryAccessors();
|
2016-07-01 14:01:06 +01:00
|
|
|
}
|
|
|
|
|
2016-08-07 18:08:48 +01:00
|
|
|
size_t BlockOfCode::RunCode(JitState* jit_state, CodePtr basic_block, size_t cycles_to_run) const {
|
2016-07-18 10:28:17 +01:00
|
|
|
constexpr size_t max_cycles_to_run = static_cast<size_t>(std::numeric_limits<decltype(jit_state->cycles_remaining)>::max());
|
|
|
|
ASSERT(cycles_to_run <= max_cycles_to_run);
|
2016-07-01 14:01:06 +01:00
|
|
|
|
|
|
|
jit_state->cycles_remaining = cycles_to_run;
|
|
|
|
run_code(jit_state, basic_block);
|
|
|
|
return cycles_to_run - jit_state->cycles_remaining; // Return number of cycles actually run.
|
|
|
|
}
|
|
|
|
|
2016-08-07 22:47:17 +01:00
|
|
|
void BlockOfCode::ReturnFromRunCode(bool MXCSR_switch) {
|
2016-08-24 20:07:08 +01:00
|
|
|
jmp(MXCSR_switch ? return_from_run_code : return_from_run_code_without_mxcsr_switch);
|
2016-08-07 18:08:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockOfCode::GenConstants() {
|
2016-08-24 20:07:08 +01:00
|
|
|
align();
|
2016-08-25 02:59:42 +01:00
|
|
|
L(consts.FloatNegativeZero32);
|
2016-08-24 20:07:08 +01:00
|
|
|
dd(0x80000000u);
|
|
|
|
|
|
|
|
align();
|
2016-08-25 02:59:42 +01:00
|
|
|
L(consts.FloatNaN32);
|
2016-08-24 20:07:08 +01:00
|
|
|
dd(0x7fc00000u);
|
|
|
|
|
|
|
|
align();
|
2016-08-25 02:59:42 +01:00
|
|
|
L(consts.FloatNonSignMask32);
|
2016-08-24 20:07:08 +01:00
|
|
|
dq(0x7fffffffu);
|
|
|
|
|
|
|
|
align();
|
2016-08-25 02:59:42 +01:00
|
|
|
L(consts.FloatNegativeZero64);
|
2016-08-24 20:07:08 +01:00
|
|
|
dq(0x8000000000000000u);
|
|
|
|
|
|
|
|
align();
|
2016-08-25 02:59:42 +01:00
|
|
|
L(consts.FloatNaN64);
|
2016-08-24 20:07:08 +01:00
|
|
|
dq(0x7ff8000000000000u);
|
|
|
|
|
|
|
|
align();
|
2016-08-25 02:59:42 +01:00
|
|
|
L(consts.FloatNonSignMask64);
|
2016-08-24 20:07:08 +01:00
|
|
|
dq(0x7fffffffffffffffu);
|
|
|
|
|
|
|
|
align();
|
2016-08-25 02:59:42 +01:00
|
|
|
L(consts.FloatPenultimatePositiveDenormal64);
|
2016-08-24 20:07:08 +01:00
|
|
|
dq(0x000ffffffffffffeu);
|
|
|
|
|
|
|
|
align();
|
2016-08-25 02:59:42 +01:00
|
|
|
L(consts.FloatMinS32);
|
2016-08-24 20:07:08 +01:00
|
|
|
dq(0xc1e0000000000000u); // -2147483648 as a double
|
|
|
|
|
|
|
|
align();
|
2016-08-25 02:59:42 +01:00
|
|
|
L(consts.FloatMaxS32);
|
2016-08-24 20:07:08 +01:00
|
|
|
dq(0x41dfffffffc00000u); // 2147483647 as a double
|
|
|
|
|
|
|
|
align();
|
2016-08-25 02:59:42 +01:00
|
|
|
L(consts.FloatPositiveZero32);
|
|
|
|
L(consts.FloatPositiveZero64);
|
|
|
|
L(consts.FloatMinU32);
|
2016-08-24 20:07:08 +01:00
|
|
|
dq(0x0000000000000000u); // 0 as a double
|
|
|
|
|
|
|
|
align();
|
2016-08-25 02:59:42 +01:00
|
|
|
L(consts.FloatMaxU32);
|
2016-08-24 20:07:08 +01:00
|
|
|
dq(0x41efffffffe00000u); // 4294967295 as a double
|
|
|
|
|
|
|
|
align();
|
2016-08-06 17:12:40 +01:00
|
|
|
}
|
|
|
|
|
2016-08-07 18:08:48 +01:00
|
|
|
void BlockOfCode::GenRunCode() {
|
2016-08-24 20:07:08 +01:00
|
|
|
align();
|
|
|
|
run_code = getCurr<RunCodeFuncType>();
|
2016-07-01 14:01:06 +01:00
|
|
|
|
|
|
|
// This serves two purposes:
|
|
|
|
// 1. It saves all the registers we as a callee need to save.
|
|
|
|
// 2. It aligns the stack so that the code the JIT emits can assume
|
|
|
|
// that the stack is appropriately aligned for CALLs.
|
2016-08-24 20:07:08 +01:00
|
|
|
ABI_PushCalleeSaveRegistersAndAdjustStack(this);
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
mov(r15, ABI_PARAM1);
|
2016-08-07 22:47:17 +01:00
|
|
|
SwitchMxcsrOnEntry();
|
2016-08-24 20:07:08 +01:00
|
|
|
jmp(ABI_PARAM2);
|
2016-08-07 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
2016-08-13 00:10:23 +01:00
|
|
|
void BlockOfCode::GenReturnFromRunCode() {
|
2016-08-24 20:07:08 +01:00
|
|
|
return_from_run_code = getCurr<const void*>();
|
2016-08-13 00:10:23 +01:00
|
|
|
|
|
|
|
SwitchMxcsrOnExit();
|
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
return_from_run_code_without_mxcsr_switch = getCurr<const void*>();
|
2016-08-13 00:10:23 +01:00
|
|
|
|
2016-08-24 20:07:08 +01:00
|
|
|
ABI_PopCalleeSaveRegistersAndAdjustStack(this);
|
|
|
|
ret();
|
2016-08-13 00:10:23 +01:00
|
|
|
}
|
|
|
|
|
2016-09-01 00:06:40 +01:00
|
|
|
void BlockOfCode::GenMemoryAccessors() {
|
|
|
|
align();
|
|
|
|
read_memory_8 = getCurr<const void*>();
|
|
|
|
ABI_PushCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
CallFunction(cb.MemoryRead8);
|
|
|
|
ABI_PopCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
ret();
|
|
|
|
|
|
|
|
align();
|
|
|
|
read_memory_16 = getCurr<const void*>();
|
|
|
|
ABI_PushCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
CallFunction(cb.MemoryRead16);
|
|
|
|
ABI_PopCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
ret();
|
|
|
|
|
|
|
|
align();
|
|
|
|
read_memory_32 = getCurr<const void*>();
|
|
|
|
ABI_PushCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
CallFunction(cb.MemoryRead32);
|
|
|
|
ABI_PopCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
ret();
|
|
|
|
|
|
|
|
align();
|
|
|
|
read_memory_64 = getCurr<const void*>();
|
|
|
|
ABI_PushCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
CallFunction(cb.MemoryRead64);
|
|
|
|
ABI_PopCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
ret();
|
|
|
|
|
|
|
|
align();
|
|
|
|
write_memory_8 = getCurr<const void*>();
|
|
|
|
ABI_PushCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
CallFunction(cb.MemoryWrite8);
|
|
|
|
ABI_PopCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
ret();
|
|
|
|
|
|
|
|
align();
|
|
|
|
write_memory_16 = getCurr<const void*>();
|
|
|
|
ABI_PushCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
CallFunction(cb.MemoryWrite16);
|
|
|
|
ABI_PopCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
ret();
|
|
|
|
|
|
|
|
align();
|
|
|
|
write_memory_32 = getCurr<const void*>();
|
|
|
|
ABI_PushCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
CallFunction(cb.MemoryWrite32);
|
|
|
|
ABI_PopCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
ret();
|
|
|
|
|
|
|
|
align();
|
|
|
|
write_memory_64 = getCurr<const void*>();
|
|
|
|
ABI_PushCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
CallFunction(cb.MemoryWrite64);
|
|
|
|
ABI_PopCallerSaveRegistersAndAdjustStack(this);
|
|
|
|
ret();
|
|
|
|
}
|
|
|
|
|
2016-08-07 22:47:17 +01:00
|
|
|
void BlockOfCode::SwitchMxcsrOnEntry() {
|
2016-08-24 20:07:08 +01:00
|
|
|
stmxcsr(dword[r15 + offsetof(JitState, save_host_MXCSR)]);
|
|
|
|
ldmxcsr(dword[r15 + offsetof(JitState, guest_MXCSR)]);
|
2016-08-07 22:47:17 +01:00
|
|
|
}
|
2016-07-01 14:01:06 +01:00
|
|
|
|
2016-08-07 22:47:17 +01:00
|
|
|
void BlockOfCode::SwitchMxcsrOnExit() {
|
2016-08-24 20:07:08 +01:00
|
|
|
stmxcsr(dword[r15 + offsetof(JitState, guest_MXCSR)]);
|
|
|
|
ldmxcsr(dword[r15 + offsetof(JitState, save_host_MXCSR)]);
|
|
|
|
}
|
|
|
|
|
2016-08-26 13:46:19 +01:00
|
|
|
void BlockOfCode::nop(size_t size) {
|
|
|
|
switch (size) {
|
|
|
|
case 0:
|
|
|
|
return;
|
|
|
|
case 1:
|
|
|
|
db(0x90);
|
|
|
|
return;
|
|
|
|
case 2:
|
|
|
|
db(0x66); db(0x90);
|
|
|
|
return;
|
|
|
|
case 3:
|
|
|
|
db(0x0f); db(0x1f); db(0x00);
|
|
|
|
return;
|
|
|
|
case 4:
|
|
|
|
db(0x0f); db(0x1f); db(0x40); db(0x00);
|
|
|
|
return;
|
|
|
|
case 5:
|
|
|
|
db(0x0f); db(0x1f); db(0x44); db(0x00); db(0x00);
|
|
|
|
return;
|
|
|
|
case 6:
|
|
|
|
db(0x66); db(0x0f); db(0x1f); db(0x44); db(0x00); db(0x00);
|
|
|
|
return;
|
|
|
|
case 7:
|
|
|
|
db(0x0f); db(0x1f); db(0x80); db(0x00); db(0x00); db(0x00); db(0x00);
|
|
|
|
return;
|
|
|
|
case 8:
|
|
|
|
db(0x0f); db(0x1f); db(0x84); db(0x00); db(0x00); db(0x00); db(0x00); db(0x00);
|
|
|
|
return;
|
|
|
|
case 9:
|
|
|
|
db(0x66); db(0x0f); db(0x1f); db(0x84); db(0x00); db(0x00); db(0x00); db(0x00); db(0x00);
|
|
|
|
return;
|
|
|
|
case 10:
|
|
|
|
default:
|
|
|
|
db(0x66); db(0x2e); db(0x0f); db(0x1f); db(0x84); db(0x00); db(0x00); db(0x00); db(0x00); db(0x00);
|
|
|
|
nop(size - 10);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 11:30:57 +01:00
|
|
|
void BlockOfCode::SetCodePtr(CodePtr code_ptr) {
|
2016-08-24 20:07:08 +01:00
|
|
|
// The "size" defines where top_, the insertion point, is.
|
2016-09-04 11:30:57 +01:00
|
|
|
size_t required_size = reinterpret_cast<const u8*>(code_ptr) - getCode();
|
2016-08-24 20:07:08 +01:00
|
|
|
setSize(required_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockOfCode::EnsurePatchLocationSize(CodePtr begin, size_t size) {
|
|
|
|
size_t current_size = getCurr<const u8*>() - reinterpret_cast<const u8*>(begin);
|
|
|
|
ASSERT(current_size <= size);
|
|
|
|
nop(size - current_size);
|
2016-07-01 14:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace BackendX64
|
|
|
|
} // namespace Dynarmic
|