2019-01-26 21:15:45 +01:00
|
|
|
#include "types.h"
|
2018-05-14 22:06:17 +02:00
|
|
|
#include "JIT.h"
|
2019-01-26 21:15:45 +01:00
|
|
|
#include "StrFmt.h"
|
|
|
|
#include "File.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
#include "sysinfo.h"
|
|
|
|
#include "VirtualMemory.h"
|
2018-09-19 18:26:11 +02:00
|
|
|
#include <immintrin.h>
|
2018-05-14 22:06:17 +02:00
|
|
|
|
2019-01-26 21:15:45 +01:00
|
|
|
// Memory manager mutex
|
|
|
|
shared_mutex s_mutex2;
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
#define CAN_OVERCOMMIT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static u8* get_jit_memory()
|
|
|
|
{
|
|
|
|
// Reserve 2G memory (magic static)
|
|
|
|
static void* const s_memory2 = []() -> void*
|
|
|
|
{
|
|
|
|
void* ptr = utils::memory_reserve(0x80000000);
|
|
|
|
|
|
|
|
#ifdef CAN_OVERCOMMIT
|
|
|
|
utils::memory_commit(ptr, 0x80000000);
|
|
|
|
utils::memory_protect(ptr, 0x40000000, utils::protection::wx);
|
|
|
|
#endif
|
|
|
|
return ptr;
|
|
|
|
}();
|
|
|
|
|
|
|
|
return static_cast<u8*>(s_memory2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocation counters (1G code, 1G data subranges)
|
|
|
|
static atomic_t<u64> s_code_pos{0}, s_data_pos{0};
|
|
|
|
|
|
|
|
// Snapshot of code generated before main()
|
|
|
|
static std::vector<u8> s_code_init, s_data_init;
|
|
|
|
|
|
|
|
template <atomic_t<u64>& Ctr, uint Off, utils::protection Prot>
|
|
|
|
static u8* add_jit_memory(std::size_t size, uint align)
|
|
|
|
{
|
|
|
|
// Select subrange
|
|
|
|
u8* pointer = get_jit_memory() + Off;
|
|
|
|
|
|
|
|
if (UNLIKELY(!size && !align))
|
|
|
|
{
|
|
|
|
// Return subrange info
|
|
|
|
return pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef CAN_OVERCOMMIT
|
|
|
|
std::lock_guard lock(s_mutex2);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
u64 olda, newa;
|
|
|
|
|
|
|
|
// Simple allocation by incrementing pointer to the next free data
|
|
|
|
const u64 pos = Ctr.atomic_op([&](u64& ctr) -> u64
|
|
|
|
{
|
|
|
|
const u64 _pos = ::align(ctr, align);
|
|
|
|
const u64 _new = ::align(_pos + size, align);
|
|
|
|
|
|
|
|
if (UNLIKELY(_new > 0x40000000))
|
|
|
|
{
|
2019-03-18 21:01:16 +01:00
|
|
|
// Sorry, we failed, and further attempts should fail too.
|
|
|
|
ctr = 0x40000000;
|
2019-01-26 21:15:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the necessity to commit more memory
|
|
|
|
olda = ::align(ctr, 0x10000);
|
|
|
|
newa = ::align(_new, 0x10000);
|
|
|
|
|
|
|
|
ctr = _new;
|
|
|
|
return _pos;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (UNLIKELY(pos == -1))
|
|
|
|
{
|
2019-03-18 21:01:16 +01:00
|
|
|
LOG_WARNING(GENERAL, "JIT: Out of memory (size=0x%x, align=0x%x, off=0x%x)", size, align, Off);
|
2019-01-26 21:15:45 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UNLIKELY(olda != newa))
|
|
|
|
{
|
|
|
|
#ifdef CAN_OVERCOMMIT
|
|
|
|
// TODO: possibly madvise
|
|
|
|
#else
|
|
|
|
// Commit more memory
|
|
|
|
utils::memory_commit(pointer + olda, newa - olda, Prot);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return pointer + pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
jit_runtime::jit_runtime()
|
|
|
|
: HostRuntime()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
jit_runtime::~jit_runtime()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
asmjit::Error jit_runtime::_add(void** dst, asmjit::CodeHolder* code) noexcept
|
|
|
|
{
|
|
|
|
std::size_t codeSize = code->getCodeSize();
|
|
|
|
if (UNLIKELY(!codeSize))
|
|
|
|
{
|
|
|
|
*dst = nullptr;
|
|
|
|
return asmjit::kErrorNoCodeGenerated;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* p = jit_runtime::alloc(codeSize, 16);
|
|
|
|
if (UNLIKELY(!p))
|
|
|
|
{
|
|
|
|
*dst = nullptr;
|
|
|
|
return asmjit::kErrorNoVirtualMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t relocSize = code->relocate(p);
|
|
|
|
if (UNLIKELY(!relocSize))
|
|
|
|
{
|
|
|
|
*dst = nullptr;
|
|
|
|
return asmjit::kErrorInvalidState;
|
|
|
|
}
|
|
|
|
|
|
|
|
flush(p, relocSize);
|
|
|
|
*dst = p;
|
|
|
|
|
|
|
|
return asmjit::kErrorOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
asmjit::Error jit_runtime::_release(void* ptr) noexcept
|
|
|
|
{
|
|
|
|
return asmjit::kErrorOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* jit_runtime::alloc(std::size_t size, uint align, bool exec) noexcept
|
|
|
|
{
|
|
|
|
if (exec)
|
|
|
|
{
|
|
|
|
return add_jit_memory<s_code_pos, 0x0, utils::protection::wx>(size, align);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return add_jit_memory<s_data_pos, 0x40000000, utils::protection::rw>(size, align);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void jit_runtime::initialize()
|
|
|
|
{
|
|
|
|
if (!s_code_init.empty() || !s_data_init.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create code/data snapshot
|
|
|
|
s_code_init.resize(s_code_pos);
|
|
|
|
std::memcpy(s_code_init.data(), alloc(0, 0, true), s_code_pos);
|
|
|
|
s_data_init.resize(s_data_pos);
|
|
|
|
std::memcpy(s_data_init.data(), alloc(0, 0, false), s_data_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void jit_runtime::finalize() noexcept
|
|
|
|
{
|
|
|
|
// Reset JIT memory
|
|
|
|
#ifdef CAN_OVERCOMMIT
|
|
|
|
utils::memory_reset(get_jit_memory(), 0x80000000);
|
|
|
|
utils::memory_protect(get_jit_memory(), 0x40000000, utils::protection::wx);
|
|
|
|
#else
|
|
|
|
utils::memory_decommit(get_jit_memory(), 0x80000000);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
s_code_pos = 0;
|
|
|
|
s_data_pos = 0;
|
|
|
|
|
|
|
|
// Restore code/data snapshot
|
|
|
|
std::memcpy(alloc(s_code_init.size(), 1, true), s_code_init.data(), s_code_init.size());
|
|
|
|
std::memcpy(alloc(s_data_init.size(), 1, false), s_data_init.data(), s_data_init.size());
|
|
|
|
}
|
|
|
|
|
2019-03-18 21:01:16 +01:00
|
|
|
asmjit::JitRuntime& asmjit::get_global_runtime()
|
2018-05-14 22:06:17 +02:00
|
|
|
{
|
|
|
|
// Magic static
|
2019-03-18 21:01:16 +01:00
|
|
|
static asmjit::JitRuntime g_rt;
|
2018-05-14 22:06:17 +02:00
|
|
|
return g_rt;
|
|
|
|
}
|
|
|
|
|
2019-06-06 20:32:35 +02:00
|
|
|
void asmjit::build_transaction_enter(asmjit::X86Assembler& c, asmjit::Label fallback, const asmjit::X86Gp& ctr, uint less_than)
|
2018-05-14 22:06:17 +02:00
|
|
|
{
|
|
|
|
Label fall = c.newLabel();
|
|
|
|
Label begin = c.newLabel();
|
|
|
|
c.jmp(begin);
|
|
|
|
c.bind(fall);
|
2019-06-06 20:32:35 +02:00
|
|
|
|
|
|
|
if (less_than < 65)
|
|
|
|
{
|
|
|
|
c.add(ctr, 1);
|
|
|
|
c.test(x86::eax, _XABORT_RETRY);
|
|
|
|
c.jz(fallback);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-12 23:02:44 +02:00
|
|
|
// Don't repeat on explicit XABORT instruction (workaround)
|
|
|
|
c.test(x86::eax, _XABORT_EXPLICIT);
|
|
|
|
c.jnz(fallback);
|
|
|
|
|
2019-06-06 20:32:35 +02:00
|
|
|
// Count an attempt without RETRY flag as 65 normal attempts and continue
|
2019-10-12 23:02:44 +02:00
|
|
|
c.push(x86::rax);
|
2019-06-06 20:32:35 +02:00
|
|
|
c.not_(x86::eax);
|
|
|
|
c.and_(x86::eax, _XABORT_RETRY);
|
|
|
|
c.shl(x86::eax, 5);
|
|
|
|
c.add(x86::eax, 1); // eax = RETRY ? 1 : 65
|
|
|
|
c.add(ctr, x86::rax);
|
2019-10-12 23:02:44 +02:00
|
|
|
c.pop(x86::rax);
|
2019-06-06 20:32:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c.cmp(ctr, less_than);
|
|
|
|
c.jae(fallback);
|
2018-05-14 22:06:17 +02:00
|
|
|
c.align(kAlignCode, 16);
|
|
|
|
c.bind(begin);
|
|
|
|
c.xbegin(fall);
|
|
|
|
}
|
|
|
|
|
|
|
|
void asmjit::build_transaction_abort(asmjit::X86Assembler& c, unsigned char code)
|
|
|
|
{
|
|
|
|
c.db(0xc6);
|
|
|
|
c.db(0xf8);
|
|
|
|
c.db(code);
|
|
|
|
}
|
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
#ifdef LLVM_AVAILABLE
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <set>
|
|
|
|
#include <array>
|
2017-06-22 23:52:09 +02:00
|
|
|
#include <deque>
|
2016-06-22 15:37:51 +02:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 0)
|
|
|
|
#endif
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
|
|
|
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
|
|
|
|
#include "llvm/ExecutionEngine/JITEventListener.h"
|
2017-06-22 23:52:09 +02:00
|
|
|
#include "llvm/ExecutionEngine/ObjectCache.h"
|
2016-06-22 15:37:51 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
Fixes from FreeBSD package (#3765)
* Thread: unbreak on BSDs after dbc9bdfe02ae
Utilities/Thread.cpp:1920:2: error: unknown type name 'cpu_set_t'; did you mean 'cpusetid_t'?
cpu_set_t cs;
^~~~~~~~~
cpusetid_t
/usr/include/sys/types.h:84:22: note: 'cpusetid_t' declared here
typedef __cpusetid_t cpusetid_t;
^
Utilities/Thread.cpp:1921:2: error: use of undeclared identifier 'CPU_ZERO'
CPU_ZERO(&cs);
^
Utilities/Thread.cpp:1922:2: error: use of undeclared identifier 'CPU_SET'
CPU_SET(core, &cs);
^
Utilities/Thread.cpp:1923:48: error: unknown type name 'cpu_set_t'; did you mean 'cpusetid_t'?
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cs);
^~~~~~~~~
cpusetid_t
* JIT: use MAP_32BIT on Linux and FreeBSD
Unless RLIMIT_DATA is low enough FreeBSD by default reserves lower 2Gb
for brk(2) style heap, ignoring mmap(2) address hint requested by RPCS3.
Passing MAP_32BIT fixes the following crash
Assertion failed: ((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) || (Type == ELF::R_X86_64_32S && ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))), function resolveX86_64Relocation, file /usr/ports/devel/llvm40/work/llvm-4.0.1.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp, line 287.
* build: unbreak -DVULKAN_PREBUILT with system glslang on Unix
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:4:10: fatal error: '../../../../Vulkan/glslang/SPIRV/GlslangToSpv.h' file not found
#include "../../../../Vulkan/glslang/SPIRV/GlslangToSpv.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rpcs3/CMakeFiles/rpcs3.dir/Emu/RSX/VK/VKCommonDecompiler.cpp.o: In function `vk::compile_glsl_to_spv(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, glsl::program_domain, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >&)':
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x50e): undefined reference to `glslang::TProgram::TProgram()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x51d): undefined reference to `glslang::TShader::TShader(EShLanguage)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x542): undefined reference to `glslang::TShader::setStrings(char const* const*, int)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x581): undefined reference to `glslang::TShader::parse(TBuiltInResource const*, int, EProfile, bool, bool, EShMessages, glslang::TShader::Includer&)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x5d6): undefined reference to `glslang::TProgram::link(EShMessages)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x5f1): undefined reference to `glslang::GlslangToSpv(glslang::TIntermediate const&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >&, glslang::SpvOptions*)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x5ff): undefined reference to `glslang::TShader::getInfoLog()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x61a): undefined reference to `glslang::TShader::getInfoDebugLog()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x630): undefined reference to `glslang::TShader::~TShader()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x63c): undefined reference to `glslang::TProgram::~TProgram()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x6d2): undefined reference to `glslang::TShader::~TShader()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x6de): undefined reference to `glslang::TProgram::~TProgram()'
rpcs3/CMakeFiles/rpcs3.dir/Emu/RSX/VK/VKCommonDecompiler.cpp.o: In function `vk::initialize_compiler_context()':
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x6f5): undefined reference to `glslang::InitializeProcess()'
rpcs3/CMakeFiles/rpcs3.dir/Emu/RSX/VK/VKCommonDecompiler.cpp.o: In function `vk::finalize_compiler_context()':
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x856): undefined reference to `glslang::FinalizeProcess()'
* build/msvc: add missing glslang include directory after 6bb3f1b4d75c
"c:\projects\rpcs3\rpcs3\VKGSRender.vcxproj" (default target) (15) ->
(ClCompile target) ->
Emu\RSX\VK\VKCommonDecompiler.cpp(4): fatal error C1083: Cannot open include file: 'SPIRV/GlslangToSpv.h': No such file or directory [c:\projects\rpcs3\rpcs3\VKGSRender.vcxproj]
2017-11-20 22:56:25 +01:00
|
|
|
#else
|
|
|
|
#include <sys/mman.h>
|
2016-06-22 15:37:51 +02:00
|
|
|
#endif
|
|
|
|
|
2017-06-24 17:36:49 +02:00
|
|
|
// Memory manager mutex
|
|
|
|
shared_mutex s_mutex;
|
2016-06-22 15:37:51 +02:00
|
|
|
|
|
|
|
// Size of virtual memory area reserved: 512 MB
|
|
|
|
static const u64 s_memory_size = 0x20000000;
|
|
|
|
|
|
|
|
// Try to reserve a portion of virtual memory in the first 2 GB address space beforehand, if possible.
|
|
|
|
static void* const s_memory = []() -> void*
|
|
|
|
{
|
2017-06-24 17:36:49 +02:00
|
|
|
llvm::InitializeNativeTarget();
|
|
|
|
llvm::InitializeNativeTargetAsmPrinter();
|
2019-05-18 19:33:22 +02:00
|
|
|
llvm::InitializeNativeTargetAsmParser();
|
2017-06-24 17:36:49 +02:00
|
|
|
LLVMLinkInMCJIT();
|
|
|
|
|
Fixes from FreeBSD package (#3765)
* Thread: unbreak on BSDs after dbc9bdfe02ae
Utilities/Thread.cpp:1920:2: error: unknown type name 'cpu_set_t'; did you mean 'cpusetid_t'?
cpu_set_t cs;
^~~~~~~~~
cpusetid_t
/usr/include/sys/types.h:84:22: note: 'cpusetid_t' declared here
typedef __cpusetid_t cpusetid_t;
^
Utilities/Thread.cpp:1921:2: error: use of undeclared identifier 'CPU_ZERO'
CPU_ZERO(&cs);
^
Utilities/Thread.cpp:1922:2: error: use of undeclared identifier 'CPU_SET'
CPU_SET(core, &cs);
^
Utilities/Thread.cpp:1923:48: error: unknown type name 'cpu_set_t'; did you mean 'cpusetid_t'?
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cs);
^~~~~~~~~
cpusetid_t
* JIT: use MAP_32BIT on Linux and FreeBSD
Unless RLIMIT_DATA is low enough FreeBSD by default reserves lower 2Gb
for brk(2) style heap, ignoring mmap(2) address hint requested by RPCS3.
Passing MAP_32BIT fixes the following crash
Assertion failed: ((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) || (Type == ELF::R_X86_64_32S && ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))), function resolveX86_64Relocation, file /usr/ports/devel/llvm40/work/llvm-4.0.1.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp, line 287.
* build: unbreak -DVULKAN_PREBUILT with system glslang on Unix
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:4:10: fatal error: '../../../../Vulkan/glslang/SPIRV/GlslangToSpv.h' file not found
#include "../../../../Vulkan/glslang/SPIRV/GlslangToSpv.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rpcs3/CMakeFiles/rpcs3.dir/Emu/RSX/VK/VKCommonDecompiler.cpp.o: In function `vk::compile_glsl_to_spv(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, glsl::program_domain, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >&)':
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x50e): undefined reference to `glslang::TProgram::TProgram()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x51d): undefined reference to `glslang::TShader::TShader(EShLanguage)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x542): undefined reference to `glslang::TShader::setStrings(char const* const*, int)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x581): undefined reference to `glslang::TShader::parse(TBuiltInResource const*, int, EProfile, bool, bool, EShMessages, glslang::TShader::Includer&)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x5d6): undefined reference to `glslang::TProgram::link(EShMessages)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x5f1): undefined reference to `glslang::GlslangToSpv(glslang::TIntermediate const&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >&, glslang::SpvOptions*)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x5ff): undefined reference to `glslang::TShader::getInfoLog()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x61a): undefined reference to `glslang::TShader::getInfoDebugLog()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x630): undefined reference to `glslang::TShader::~TShader()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x63c): undefined reference to `glslang::TProgram::~TProgram()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x6d2): undefined reference to `glslang::TShader::~TShader()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x6de): undefined reference to `glslang::TProgram::~TProgram()'
rpcs3/CMakeFiles/rpcs3.dir/Emu/RSX/VK/VKCommonDecompiler.cpp.o: In function `vk::initialize_compiler_context()':
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x6f5): undefined reference to `glslang::InitializeProcess()'
rpcs3/CMakeFiles/rpcs3.dir/Emu/RSX/VK/VKCommonDecompiler.cpp.o: In function `vk::finalize_compiler_context()':
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x856): undefined reference to `glslang::FinalizeProcess()'
* build/msvc: add missing glslang include directory after 6bb3f1b4d75c
"c:\projects\rpcs3\rpcs3\VKGSRender.vcxproj" (default target) (15) ->
(ClCompile target) ->
Emu\RSX\VK\VKCommonDecompiler.cpp(4): fatal error C1083: Cannot open include file: 'SPIRV/GlslangToSpv.h': No such file or directory [c:\projects\rpcs3\rpcs3\VKGSRender.vcxproj]
2017-11-20 22:56:25 +01:00
|
|
|
#ifdef MAP_32BIT
|
|
|
|
auto ptr = ::mmap(nullptr, s_memory_size, PROT_NONE, MAP_ANON | MAP_PRIVATE | MAP_32BIT, -1, 0);
|
|
|
|
if (ptr != MAP_FAILED)
|
|
|
|
return ptr;
|
|
|
|
#else
|
2017-03-19 13:53:48 +01:00
|
|
|
for (u64 addr = 0x10000000; addr <= 0x80000000 - s_memory_size; addr += 0x1000000)
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2017-03-19 13:53:48 +01:00
|
|
|
if (auto ptr = utils::memory_reserve(s_memory_size, (void*)addr))
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2017-03-19 13:53:48 +01:00
|
|
|
return ptr;
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
|
|
|
}
|
Fixes from FreeBSD package (#3765)
* Thread: unbreak on BSDs after dbc9bdfe02ae
Utilities/Thread.cpp:1920:2: error: unknown type name 'cpu_set_t'; did you mean 'cpusetid_t'?
cpu_set_t cs;
^~~~~~~~~
cpusetid_t
/usr/include/sys/types.h:84:22: note: 'cpusetid_t' declared here
typedef __cpusetid_t cpusetid_t;
^
Utilities/Thread.cpp:1921:2: error: use of undeclared identifier 'CPU_ZERO'
CPU_ZERO(&cs);
^
Utilities/Thread.cpp:1922:2: error: use of undeclared identifier 'CPU_SET'
CPU_SET(core, &cs);
^
Utilities/Thread.cpp:1923:48: error: unknown type name 'cpu_set_t'; did you mean 'cpusetid_t'?
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cs);
^~~~~~~~~
cpusetid_t
* JIT: use MAP_32BIT on Linux and FreeBSD
Unless RLIMIT_DATA is low enough FreeBSD by default reserves lower 2Gb
for brk(2) style heap, ignoring mmap(2) address hint requested by RPCS3.
Passing MAP_32BIT fixes the following crash
Assertion failed: ((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) || (Type == ELF::R_X86_64_32S && ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))), function resolveX86_64Relocation, file /usr/ports/devel/llvm40/work/llvm-4.0.1.src/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp, line 287.
* build: unbreak -DVULKAN_PREBUILT with system glslang on Unix
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:4:10: fatal error: '../../../../Vulkan/glslang/SPIRV/GlslangToSpv.h' file not found
#include "../../../../Vulkan/glslang/SPIRV/GlslangToSpv.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rpcs3/CMakeFiles/rpcs3.dir/Emu/RSX/VK/VKCommonDecompiler.cpp.o: In function `vk::compile_glsl_to_spv(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, glsl::program_domain, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >&)':
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x50e): undefined reference to `glslang::TProgram::TProgram()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x51d): undefined reference to `glslang::TShader::TShader(EShLanguage)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x542): undefined reference to `glslang::TShader::setStrings(char const* const*, int)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x581): undefined reference to `glslang::TShader::parse(TBuiltInResource const*, int, EProfile, bool, bool, EShMessages, glslang::TShader::Includer&)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x5d6): undefined reference to `glslang::TProgram::link(EShMessages)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x5f1): undefined reference to `glslang::GlslangToSpv(glslang::TIntermediate const&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >&, glslang::SpvOptions*)'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x5ff): undefined reference to `glslang::TShader::getInfoLog()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x61a): undefined reference to `glslang::TShader::getInfoDebugLog()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x630): undefined reference to `glslang::TShader::~TShader()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x63c): undefined reference to `glslang::TProgram::~TProgram()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x6d2): undefined reference to `glslang::TShader::~TShader()'
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x6de): undefined reference to `glslang::TProgram::~TProgram()'
rpcs3/CMakeFiles/rpcs3.dir/Emu/RSX/VK/VKCommonDecompiler.cpp.o: In function `vk::initialize_compiler_context()':
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x6f5): undefined reference to `glslang::InitializeProcess()'
rpcs3/CMakeFiles/rpcs3.dir/Emu/RSX/VK/VKCommonDecompiler.cpp.o: In function `vk::finalize_compiler_context()':
rpcs3/Emu/RSX/VK/VKCommonDecompiler.cpp:(.text+0x856): undefined reference to `glslang::FinalizeProcess()'
* build/msvc: add missing glslang include directory after 6bb3f1b4d75c
"c:\projects\rpcs3\rpcs3\VKGSRender.vcxproj" (default target) (15) ->
(ClCompile target) ->
Emu\RSX\VK\VKCommonDecompiler.cpp(4): fatal error C1083: Cannot open include file: 'SPIRV/GlslangToSpv.h': No such file or directory [c:\projects\rpcs3\rpcs3\VKGSRender.vcxproj]
2017-11-20 22:56:25 +01:00
|
|
|
#endif
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2017-03-19 13:53:48 +01:00
|
|
|
return utils::memory_reserve(s_memory_size);
|
2016-06-22 15:37:51 +02:00
|
|
|
}();
|
|
|
|
|
2017-06-24 17:36:49 +02:00
|
|
|
static void* s_next = s_memory;
|
2016-08-10 12:09:11 +02:00
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
#ifdef _WIN32
|
2017-06-22 23:52:09 +02:00
|
|
|
static std::deque<std::vector<RUNTIME_FUNCTION>> s_unwater;
|
2017-02-26 16:56:31 +01:00
|
|
|
static std::vector<std::vector<RUNTIME_FUNCTION>> s_unwind; // .pdata
|
2018-01-01 08:40:57 +01:00
|
|
|
#else
|
2018-05-01 12:20:36 +02:00
|
|
|
static std::deque<std::pair<u8*, std::size_t>> s_unfire;
|
2016-06-22 15:37:51 +02:00
|
|
|
#endif
|
|
|
|
|
2017-06-24 17:36:49 +02:00
|
|
|
// Reset memory manager
|
|
|
|
extern void jit_finalize()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
for (auto&& unwind : s_unwind)
|
|
|
|
{
|
|
|
|
if (!RtlDeleteFunctionTable(unwind.data()))
|
|
|
|
{
|
|
|
|
LOG_FATAL(GENERAL, "RtlDeleteFunctionTable() failed! Error %u", GetLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s_unwind.clear();
|
|
|
|
#else
|
2018-01-01 08:40:57 +01:00
|
|
|
for (auto&& t : s_unfire)
|
|
|
|
{
|
2018-05-01 12:20:36 +02:00
|
|
|
llvm::RTDyldMemoryManager::deregisterEHFramesInProcess(t.first, t.second);
|
2018-01-01 08:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s_unfire.clear();
|
2017-06-24 17:36:49 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
utils::memory_decommit(s_memory, s_memory_size);
|
|
|
|
|
|
|
|
s_next = s_memory;
|
|
|
|
}
|
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
// Helper class
|
2017-06-24 17:36:49 +02:00
|
|
|
struct MemoryManager : llvm::RTDyldMemoryManager
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2017-08-08 00:05:59 +02:00
|
|
|
std::unordered_map<std::string, u64>& m_link;
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2017-06-24 17:36:49 +02:00
|
|
|
std::array<u8, 16>* m_tramps{};
|
|
|
|
|
|
|
|
u8* m_code_addr{}; // TODO
|
2017-03-19 13:53:48 +01:00
|
|
|
|
2017-08-08 00:05:59 +02:00
|
|
|
MemoryManager(std::unordered_map<std::string, u64>& table)
|
2017-02-26 16:56:31 +01:00
|
|
|
: m_link(table)
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[[noreturn]] static void null()
|
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
fmt::throw_exception("Null function" HERE);
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
llvm::JITSymbol findSymbol(const std::string& name) override
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2017-03-19 13:53:48 +01:00
|
|
|
auto& addr = m_link[name];
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2017-03-19 13:53:48 +01:00
|
|
|
// Find function address
|
|
|
|
if (!addr)
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2017-03-19 13:53:48 +01:00
|
|
|
addr = RTDyldMemoryManager::getSymbolAddress(name);
|
|
|
|
|
|
|
|
if (addr)
|
|
|
|
{
|
|
|
|
LOG_WARNING(GENERAL, "LLVM: Symbol requested: %s -> 0x%016llx", name, addr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_ERROR(GENERAL, "LLVM: Linkage failed: %s", name);
|
|
|
|
addr = (u64)null;
|
|
|
|
}
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 13:53:48 +01:00
|
|
|
// Verify address for small code model
|
|
|
|
if ((u64)s_memory > 0x80000000 - s_memory_size ? (u64)addr - (u64)s_memory >= s_memory_size : addr >= 0x80000000)
|
2017-03-11 17:49:32 +01:00
|
|
|
{
|
2017-06-24 17:36:49 +02:00
|
|
|
// Lock memory manager
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(s_mutex);
|
2017-06-24 17:36:49 +02:00
|
|
|
|
2017-03-19 13:53:48 +01:00
|
|
|
// Allocate memory for trampolines
|
|
|
|
if (!m_tramps)
|
|
|
|
{
|
2017-06-22 23:52:09 +02:00
|
|
|
m_tramps = reinterpret_cast<decltype(m_tramps)>(s_next);
|
|
|
|
utils::memory_commit(s_next, 4096, utils::protection::wx);
|
|
|
|
s_next = (u8*)((u64)s_next + 4096);
|
2017-03-19 13:53:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a trampoline
|
|
|
|
auto& data = *m_tramps++;
|
|
|
|
data[0x0] = 0xff; // JMP [rip+2]
|
|
|
|
data[0x1] = 0x25;
|
|
|
|
data[0x2] = 0x02;
|
|
|
|
data[0x3] = 0x00;
|
|
|
|
data[0x4] = 0x00;
|
|
|
|
data[0x5] = 0x00;
|
|
|
|
data[0x6] = 0x48; // MOV rax, imm64 (not executed)
|
|
|
|
data[0x7] = 0xb8;
|
|
|
|
std::memcpy(data.data() + 8, &addr, 8);
|
|
|
|
addr = (u64)&data;
|
|
|
|
|
|
|
|
// Reset pointer (memory page exhausted)
|
|
|
|
if (((u64)m_tramps % 4096) == 0)
|
|
|
|
{
|
|
|
|
m_tramps = nullptr;
|
|
|
|
}
|
2017-03-11 17:49:32 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
return {addr, llvm::JITSymbolFlags::Exported};
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 22:51:00 +01:00
|
|
|
u8* allocateCodeSection(std::uintptr_t size, uint align, uint sec_id, llvm::StringRef sec_name) override
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2017-06-24 17:36:49 +02:00
|
|
|
// Lock memory manager
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(s_mutex);
|
2017-06-24 17:36:49 +02:00
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
// Simple allocation
|
2017-06-22 23:52:09 +02:00
|
|
|
const u64 next = ::align((u64)s_next + size, 4096);
|
2016-06-22 15:37:51 +02:00
|
|
|
|
|
|
|
if (next > (u64)s_memory + s_memory_size)
|
|
|
|
{
|
|
|
|
LOG_FATAL(GENERAL, "LLVM: Out of memory (size=0x%llx, aligned 0x%x)", size, align);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
utils::memory_commit(s_next, size, utils::protection::wx);
|
2017-06-24 17:36:49 +02:00
|
|
|
m_code_addr = (u8*)s_next;
|
2016-08-10 12:09:11 +02:00
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
LOG_NOTICE(GENERAL, "LLVM: Code section %u '%s' allocated -> %p (size=0x%llx, aligned 0x%x)", sec_id, sec_name.data(), s_next, size, align);
|
|
|
|
return (u8*)std::exchange(s_next, (void*)next);
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 22:51:00 +01:00
|
|
|
u8* allocateDataSection(std::uintptr_t size, uint align, uint sec_id, llvm::StringRef sec_name, bool is_ro) override
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2017-06-24 17:36:49 +02:00
|
|
|
// Lock memory manager
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(s_mutex);
|
2017-06-24 17:36:49 +02:00
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
// Simple allocation
|
2017-06-22 23:52:09 +02:00
|
|
|
const u64 next = ::align((u64)s_next + size, 4096);
|
2016-06-22 15:37:51 +02:00
|
|
|
|
|
|
|
if (next > (u64)s_memory + s_memory_size)
|
|
|
|
{
|
|
|
|
LOG_FATAL(GENERAL, "LLVM: Out of memory (size=0x%llx, aligned 0x%x)", size, align);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-10 12:09:11 +02:00
|
|
|
if (!is_ro)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
utils::memory_commit(s_next, size);
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
LOG_NOTICE(GENERAL, "LLVM: Data section %u '%s' allocated -> %p (size=0x%llx, aligned 0x%x, %s)", sec_id, sec_name.data(), s_next, size, align, is_ro ? "ro" : "rw");
|
|
|
|
return (u8*)std::exchange(s_next, (void*)next);
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 22:51:00 +01:00
|
|
|
bool finalizeMemory(std::string* = nullptr) override
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2017-06-24 17:36:49 +02:00
|
|
|
// Lock memory manager
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(s_mutex);
|
2017-06-24 17:36:49 +02:00
|
|
|
|
2016-08-10 12:09:11 +02:00
|
|
|
// TODO: make only read-only sections read-only
|
2017-02-26 16:56:31 +01:00
|
|
|
//#ifdef _WIN32
|
|
|
|
// DWORD op;
|
|
|
|
// VirtualProtect(s_memory, (u64)m_next - (u64)s_memory, PAGE_READONLY, &op);
|
|
|
|
// VirtualProtect(s_code_addr, s_code_size, PAGE_EXECUTE_READ, &op);
|
|
|
|
//#else
|
|
|
|
// ::mprotect(s_memory, (u64)m_next - (u64)s_memory, PROT_READ);
|
|
|
|
// ::mprotect(s_code_addr, s_code_size, PROT_READ | PROT_EXEC);
|
|
|
|
//#endif
|
2016-06-22 15:37:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:51:00 +01:00
|
|
|
void registerEHFrames(u8* addr, u64 load_addr, std::size_t size) override
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2017-03-20 14:23:50 +01:00
|
|
|
#ifdef _WIN32
|
2017-06-24 17:36:49 +02:00
|
|
|
// Lock memory manager
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(s_mutex);
|
2017-06-24 17:36:49 +02:00
|
|
|
|
2017-03-20 14:23:50 +01:00
|
|
|
// Use s_memory as a BASE, compute the difference
|
|
|
|
const u64 unwind_diff = (u64)addr - (u64)s_memory;
|
|
|
|
|
|
|
|
// Fix RUNTIME_FUNCTION records (.pdata section)
|
2017-06-22 23:52:09 +02:00
|
|
|
auto pdata = std::move(s_unwater.front());
|
|
|
|
s_unwater.pop_front();
|
2017-03-20 14:23:50 +01:00
|
|
|
|
|
|
|
for (auto& rf : pdata)
|
|
|
|
{
|
2017-06-22 23:52:09 +02:00
|
|
|
rf.UnwindData += static_cast<DWORD>(unwind_diff);
|
2017-03-20 14:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register .xdata UNWIND_INFO structs
|
|
|
|
if (!RtlAddFunctionTable(pdata.data(), (DWORD)pdata.size(), (u64)s_memory))
|
|
|
|
{
|
|
|
|
LOG_ERROR(GENERAL, "RtlAddFunctionTable() failed! Error %u", GetLastError());
|
|
|
|
}
|
2017-06-22 23:52:09 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
s_unwind.emplace_back(std::move(pdata));
|
|
|
|
}
|
2018-01-01 08:40:57 +01:00
|
|
|
#else
|
2018-05-01 12:20:36 +02:00
|
|
|
s_unfire.push_front(std::make_pair(addr, size));
|
2017-03-20 14:23:50 +01:00
|
|
|
#endif
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2019-05-05 15:28:41 +02:00
|
|
|
return RTDyldMemoryManager::registerEHFramesInProcess(addr, size);
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 12:20:36 +02:00
|
|
|
void deregisterEHFrames() override
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-10 14:46:01 +02:00
|
|
|
// Simple memory manager
|
|
|
|
struct MemoryManager2 : llvm::RTDyldMemoryManager
|
|
|
|
{
|
|
|
|
MemoryManager2() = default;
|
|
|
|
|
|
|
|
~MemoryManager2() override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* allocateCodeSection(std::uintptr_t size, uint align, uint sec_id, llvm::StringRef sec_name) override
|
|
|
|
{
|
2019-01-26 21:15:45 +01:00
|
|
|
return jit_runtime::alloc(size, align, true);
|
2018-06-10 14:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u8* allocateDataSection(std::uintptr_t size, uint align, uint sec_id, llvm::StringRef sec_name, bool is_ro) override
|
|
|
|
{
|
2019-01-26 21:15:45 +01:00
|
|
|
return jit_runtime::alloc(size, align, false);
|
2018-06-10 14:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool finalizeMemory(std::string* = nullptr) override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-18 17:24:55 +01:00
|
|
|
|
|
|
|
void registerEHFrames(u8* addr, u64 load_addr, std::size_t size) override
|
|
|
|
{
|
2019-05-05 15:28:41 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
RTDyldMemoryManager::registerEHFramesInProcess(addr, size);
|
|
|
|
s_unfire.push_front(std::make_pair(addr, size));
|
|
|
|
#endif
|
2019-03-18 17:24:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void deregisterEHFrames() override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Simple memory manager. I promise there will be no MemoryManager4.
|
|
|
|
struct MemoryManager3 : llvm::RTDyldMemoryManager
|
|
|
|
{
|
|
|
|
std::vector<std::pair<u8*, std::size_t>> allocs;
|
|
|
|
|
|
|
|
MemoryManager3() = default;
|
|
|
|
|
|
|
|
~MemoryManager3() override
|
|
|
|
{
|
|
|
|
for (auto& a : allocs)
|
|
|
|
{
|
|
|
|
utils::memory_release(a.first, a.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* allocateCodeSection(std::uintptr_t size, uint align, uint sec_id, llvm::StringRef sec_name) override
|
|
|
|
{
|
|
|
|
u8* r = static_cast<u8*>(utils::memory_reserve(size));
|
|
|
|
utils::memory_commit(r, size, utils::protection::wx);
|
|
|
|
allocs.emplace_back(r, size);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* allocateDataSection(std::uintptr_t size, uint align, uint sec_id, llvm::StringRef sec_name, bool is_ro) override
|
|
|
|
{
|
|
|
|
u8* r = static_cast<u8*>(utils::memory_reserve(size));
|
|
|
|
utils::memory_commit(r, size);
|
|
|
|
allocs.emplace_back(r, size);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool finalizeMemory(std::string* = nullptr) override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerEHFrames(u8* addr, u64 load_addr, std::size_t size) override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void deregisterEHFrames() override
|
|
|
|
{
|
|
|
|
}
|
2018-06-10 14:46:01 +02:00
|
|
|
};
|
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
// Helper class
|
2017-06-24 17:36:49 +02:00
|
|
|
struct EventListener : llvm::JITEventListener
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2017-06-24 17:36:49 +02:00
|
|
|
MemoryManager& m_mem;
|
|
|
|
|
|
|
|
EventListener(MemoryManager& mem)
|
|
|
|
: m_mem(mem)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-29 14:35:00 +01:00
|
|
|
void notifyObjectLoaded(ObjectKey K, const llvm::object::ObjectFile& obj, const llvm::RuntimeDyld::LoadedObjectInfo& inf) override
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2017-03-20 14:23:50 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
for (auto it = obj.section_begin(), end = obj.section_end(); it != end; ++it)
|
|
|
|
{
|
|
|
|
llvm::StringRef name;
|
2019-10-23 12:09:57 +02:00
|
|
|
name = it->getName().get();
|
2017-03-20 14:23:50 +01:00
|
|
|
|
|
|
|
if (name == ".pdata")
|
|
|
|
{
|
|
|
|
llvm::StringRef data;
|
2019-10-23 12:09:57 +02:00
|
|
|
data = it->getContents().get();
|
2017-03-20 14:23:50 +01:00
|
|
|
|
|
|
|
std::vector<RUNTIME_FUNCTION> rfs(data.size() / sizeof(RUNTIME_FUNCTION));
|
|
|
|
|
|
|
|
auto offsets = reinterpret_cast<DWORD*>(rfs.data());
|
|
|
|
|
|
|
|
// Initialize .pdata section using relocation info
|
|
|
|
for (auto ri = it->relocation_begin(), end = it->relocation_end(); ri != end; ++ri)
|
|
|
|
{
|
|
|
|
if (ri->getType() == 3 /*R_X86_64_GOT32*/)
|
|
|
|
{
|
|
|
|
const u64 value = *reinterpret_cast<const DWORD*>(data.data() + ri->getOffset());
|
|
|
|
offsets[ri->getOffset() / sizeof(DWORD)] = static_cast<DWORD>(value + ri->getSymbol()->getAddress().get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-24 17:36:49 +02:00
|
|
|
// Lock memory manager
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(s_mutex);
|
2017-06-24 17:36:49 +02:00
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
// Use s_memory as a BASE, compute the difference
|
2017-06-24 17:36:49 +02:00
|
|
|
const u64 code_diff = (u64)m_mem.m_code_addr - (u64)s_memory;
|
2017-06-22 23:52:09 +02:00
|
|
|
|
|
|
|
// Fix RUNTIME_FUNCTION records (.pdata section)
|
|
|
|
for (auto& rf : rfs)
|
|
|
|
{
|
|
|
|
rf.BeginAddress += static_cast<DWORD>(code_diff);
|
|
|
|
rf.EndAddress += static_cast<DWORD>(code_diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
s_unwater.emplace_back(std::move(rfs));
|
2017-03-20 14:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
// Helper class
|
|
|
|
class ObjectCache final : public llvm::ObjectCache
|
|
|
|
{
|
|
|
|
const std::string& m_path;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ObjectCache(const std::string& path)
|
|
|
|
: m_path(path)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ObjectCache() override = default;
|
|
|
|
|
|
|
|
void notifyObjectCompiled(const llvm::Module* module, llvm::MemoryBufferRef obj) override
|
|
|
|
{
|
|
|
|
std::string name = m_path;
|
|
|
|
name.append(module->getName());
|
|
|
|
fs::file(name, fs::rewrite).write(obj.getBufferStart(), obj.getBufferSize());
|
2018-06-10 14:46:01 +02:00
|
|
|
LOG_NOTICE(GENERAL, "LLVM: Created module: %s", module->getName().data());
|
2017-06-22 23:52:09 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 11:20:40 +02:00
|
|
|
static std::unique_ptr<llvm::MemoryBuffer> load(const std::string& path)
|
2017-06-22 23:52:09 +02:00
|
|
|
{
|
2017-07-15 11:20:40 +02:00
|
|
|
if (fs::file cached{path, fs::read})
|
2017-06-22 23:52:09 +02:00
|
|
|
{
|
2018-05-01 12:20:36 +02:00
|
|
|
auto buf = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(cached.size());
|
|
|
|
cached.read(buf->getBufferStart(), buf->getBufferSize());
|
2018-09-05 22:52:34 +02:00
|
|
|
return buf;
|
2017-06-22 23:52:09 +02:00
|
|
|
}
|
2017-07-15 11:20:40 +02:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module* module) override
|
|
|
|
{
|
|
|
|
std::string path = m_path;
|
|
|
|
path.append(module->getName());
|
|
|
|
|
|
|
|
if (auto buf = load(path))
|
2017-06-22 23:52:09 +02:00
|
|
|
{
|
2018-06-10 14:46:01 +02:00
|
|
|
LOG_NOTICE(GENERAL, "LLVM: Loaded module: %s", module->getName().data());
|
2017-07-15 11:20:40 +02:00
|
|
|
return buf;
|
2017-06-22 23:52:09 +02:00
|
|
|
}
|
2017-07-15 11:20:40 +02:00
|
|
|
|
|
|
|
return nullptr;
|
2017-06-22 23:52:09 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-17 18:41:35 +01:00
|
|
|
std::string jit_compiler::cpu(const std::string& _cpu)
|
2016-06-22 15:37:51 +02:00
|
|
|
{
|
2018-03-17 18:41:35 +01:00
|
|
|
std::string m_cpu = _cpu;
|
|
|
|
|
2017-03-14 13:23:07 +01:00
|
|
|
if (m_cpu.empty())
|
|
|
|
{
|
|
|
|
m_cpu = llvm::sys::getHostCPUName();
|
2017-07-18 14:21:29 +02:00
|
|
|
|
|
|
|
if (m_cpu == "sandybridge" ||
|
|
|
|
m_cpu == "ivybridge" ||
|
|
|
|
m_cpu == "haswell" ||
|
|
|
|
m_cpu == "broadwell" ||
|
|
|
|
m_cpu == "skylake" ||
|
|
|
|
m_cpu == "skylake-avx512" ||
|
2019-03-05 19:46:58 +01:00
|
|
|
m_cpu == "cascadelake" ||
|
2018-05-01 12:20:36 +02:00
|
|
|
m_cpu == "cannonlake" ||
|
2019-02-28 22:20:04 +01:00
|
|
|
m_cpu == "icelake" ||
|
|
|
|
m_cpu == "icelake-client" ||
|
|
|
|
m_cpu == "icelake-server")
|
2017-07-18 14:21:29 +02:00
|
|
|
{
|
2018-05-01 12:20:36 +02:00
|
|
|
// Downgrade if AVX is not supported by some chips
|
2017-07-18 14:21:29 +02:00
|
|
|
if (!utils::has_avx())
|
|
|
|
{
|
|
|
|
m_cpu = "nehalem";
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 12:20:36 +02:00
|
|
|
|
|
|
|
if (m_cpu == "skylake-avx512" ||
|
2019-03-05 19:46:58 +01:00
|
|
|
m_cpu == "cascadelake" ||
|
2018-05-01 12:20:36 +02:00
|
|
|
m_cpu == "cannonlake" ||
|
2019-02-28 22:20:04 +01:00
|
|
|
m_cpu == "icelake" ||
|
|
|
|
m_cpu == "icelake-client" ||
|
|
|
|
m_cpu == "icelake-server")
|
2018-05-01 12:20:36 +02:00
|
|
|
{
|
|
|
|
// Downgrade if AVX-512 is disabled or not supported
|
|
|
|
if (!utils::has_512())
|
|
|
|
{
|
|
|
|
m_cpu = "skylake";
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 13:23:07 +01:00
|
|
|
}
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2018-03-17 18:41:35 +01:00
|
|
|
return m_cpu;
|
|
|
|
}
|
|
|
|
|
2019-03-18 17:24:55 +01:00
|
|
|
jit_compiler::jit_compiler(const std::unordered_map<std::string, u64>& _link, const std::string& _cpu, u32 flags)
|
2018-03-17 18:41:35 +01:00
|
|
|
: m_link(_link)
|
|
|
|
, m_cpu(cpu(_cpu))
|
|
|
|
{
|
2017-02-26 16:56:31 +01:00
|
|
|
std::string result;
|
|
|
|
|
2019-10-15 16:43:33 +02:00
|
|
|
auto null_mod = std::make_unique<llvm::Module> ("null_", m_context);
|
|
|
|
|
2017-06-24 17:36:49 +02:00
|
|
|
if (m_link.empty())
|
|
|
|
{
|
2019-03-18 17:24:55 +01:00
|
|
|
std::unique_ptr<llvm::RTDyldMemoryManager> mem;
|
|
|
|
|
|
|
|
if (flags & 0x1)
|
|
|
|
{
|
|
|
|
mem = std::make_unique<MemoryManager3>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mem = std::make_unique<MemoryManager2>();
|
2019-10-15 16:43:33 +02:00
|
|
|
null_mod->setTargetTriple(llvm::Triple::normalize("x86_64-unknown-linux-gnu"));
|
2019-03-18 17:24:55 +01:00
|
|
|
}
|
|
|
|
|
2017-06-29 16:25:39 +02:00
|
|
|
// Auxiliary JIT (does not use custom memory manager, only writes the objects)
|
2019-10-15 16:43:33 +02:00
|
|
|
m_engine.reset(llvm::EngineBuilder(std::move(null_mod))
|
2017-06-24 17:36:49 +02:00
|
|
|
.setErrorStr(&result)
|
2018-05-12 21:56:18 +02:00
|
|
|
.setEngineKind(llvm::EngineKind::JIT)
|
2019-03-18 17:24:55 +01:00
|
|
|
.setMCJITMemoryManager(std::move(mem))
|
2017-06-24 17:36:49 +02:00
|
|
|
.setOptLevel(llvm::CodeGenOpt::Aggressive)
|
2019-03-18 17:24:55 +01:00
|
|
|
.setCodeModel(flags & 0x2 ? llvm::CodeModel::Large : llvm::CodeModel::Small)
|
2017-06-24 17:36:49 +02:00
|
|
|
.setMCPU(m_cpu)
|
|
|
|
.create());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-29 16:25:39 +02:00
|
|
|
// Primary JIT
|
2017-06-24 17:36:49 +02:00
|
|
|
auto mem = std::make_unique<MemoryManager>(m_link);
|
|
|
|
m_jit_el = std::make_unique<EventListener>(*mem);
|
|
|
|
|
2019-10-15 16:43:33 +02:00
|
|
|
m_engine.reset(llvm::EngineBuilder(std::move(null_mod))
|
2017-06-24 17:36:49 +02:00
|
|
|
.setErrorStr(&result)
|
2018-05-12 21:56:18 +02:00
|
|
|
.setEngineKind(llvm::EngineKind::JIT)
|
2017-06-24 17:36:49 +02:00
|
|
|
.setMCJITMemoryManager(std::move(mem))
|
|
|
|
.setOptLevel(llvm::CodeGenOpt::Aggressive)
|
2019-03-18 17:24:55 +01:00
|
|
|
.setCodeModel(flags & 0x2 ? llvm::CodeModel::Large : llvm::CodeModel::Small)
|
2017-06-24 17:36:49 +02:00
|
|
|
.setMCPU(m_cpu)
|
|
|
|
.create());
|
|
|
|
|
|
|
|
if (m_engine)
|
|
|
|
{
|
|
|
|
m_engine->RegisterJITEventListener(m_jit_el.get());
|
|
|
|
}
|
|
|
|
}
|
2016-06-22 15:37:51 +02:00
|
|
|
|
|
|
|
if (!m_engine)
|
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
fmt::throw_exception("LLVM: Failed to create ExecutionEngine: %s", result);
|
2016-06-22 15:37:51 +02:00
|
|
|
}
|
2017-06-24 17:36:49 +02:00
|
|
|
}
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2017-06-24 17:36:49 +02:00
|
|
|
jit_compiler::~jit_compiler()
|
|
|
|
{
|
2017-02-26 16:56:31 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
void jit_compiler::add(std::unique_ptr<llvm::Module> module, const std::string& path)
|
2017-02-26 16:56:31 +01:00
|
|
|
{
|
2017-06-22 23:52:09 +02:00
|
|
|
ObjectCache cache{path};
|
|
|
|
m_engine->setObjectCache(&cache);
|
2017-02-26 16:56:31 +01:00
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
const auto ptr = module.get();
|
2017-02-26 16:56:31 +01:00
|
|
|
m_engine->addModule(std::move(module));
|
2017-06-22 23:52:09 +02:00
|
|
|
m_engine->generateCodeForModule(ptr);
|
|
|
|
m_engine->setObjectCache(nullptr);
|
2017-02-26 16:56:31 +01:00
|
|
|
|
2018-05-01 12:20:36 +02:00
|
|
|
for (auto& func : ptr->functions())
|
|
|
|
{
|
|
|
|
// Delete IR to lower memory consumption
|
|
|
|
func.deleteBody();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void jit_compiler::add(std::unique_ptr<llvm::Module> module)
|
|
|
|
{
|
|
|
|
const auto ptr = module.get();
|
|
|
|
m_engine->addModule(std::move(module));
|
|
|
|
m_engine->generateCodeForModule(ptr);
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
for (auto& func : ptr->functions())
|
2017-02-26 16:56:31 +01:00
|
|
|
{
|
2017-06-22 23:52:09 +02:00
|
|
|
// Delete IR to lower memory consumption
|
|
|
|
func.deleteBody();
|
2017-02-26 16:56:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 11:20:40 +02:00
|
|
|
void jit_compiler::add(const std::string& path)
|
|
|
|
{
|
2019-10-20 21:42:59 +02:00
|
|
|
auto cache = ObjectCache::load(path);
|
|
|
|
|
|
|
|
if (auto object_file = llvm::object::ObjectFile::createObjectFile(*cache))
|
|
|
|
{
|
|
|
|
m_engine->addObjectFile( std::move(*object_file) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_ERROR(GENERAL, "ObjectCache: Adding failed: %s", path);
|
|
|
|
}
|
2017-07-15 11:20:40 +02:00
|
|
|
}
|
|
|
|
|
2017-06-24 17:36:49 +02:00
|
|
|
void jit_compiler::fin()
|
2017-02-26 16:56:31 +01:00
|
|
|
{
|
2016-06-22 15:37:51 +02:00
|
|
|
m_engine->finalizeObject();
|
2017-06-22 23:52:09 +02:00
|
|
|
}
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2017-06-29 16:25:39 +02:00
|
|
|
u64 jit_compiler::get(const std::string& name)
|
2017-06-22 23:52:09 +02:00
|
|
|
{
|
2017-06-29 16:25:39 +02:00
|
|
|
return m_engine->getGlobalValueAddress(name);
|
2017-06-24 17:36:49 +02:00
|
|
|
}
|
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
#endif
|