diff --git a/Source/Tests/FEXLoader.cpp b/Source/Tests/FEXLoader.cpp index 2279a2049..0311b3c95 100644 --- a/Source/Tests/FEXLoader.cpp +++ b/Source/Tests/FEXLoader.cpp @@ -10,7 +10,8 @@ $end_info$ #include "Common/Config.h" #include "ELFCodeLoader.h" #include "ELFCodeLoader2.h" -#include "Tests/LinuxSyscalls/Syscalls.h" +#include "Tests/LinuxSyscalls/x32/Syscalls.h" +#include "Tests/LinuxSyscalls/x64/Syscalls.h" #include "Tests/LinuxSyscalls/SignalDelegator.h" #include @@ -232,7 +233,13 @@ int main(int argc, char **argv, char **const envp) { return -ENOEXEC; } - + + uint32_t KernelVersion = FEX::HLE::SyscallHandler::CalculateHostKernelVersion(); + if (KernelVersion < FEX::HLE::SyscallHandler::KernelVersion(4, 17)) { + // We require 4.17 minimum for MAP_FIXED_NOREPLACE + LogMan::Msg::E("FEXLoader requires kernel 4.17 minimum. Expect problems."); + } + ELFCodeLoader2 Loader{Program, LDPath(), Args, ParsedArgs, envp, &Environment}; //FEX::HarnessHelper::ELFCodeLoader Loader{Program, LDPath(), Args, ParsedArgs, envp, &Environment}; @@ -241,38 +248,48 @@ int main(int argc, char **argv, char **const envp) { return -ENOEXEC; } + FEXCore::Config::Set(FEXCore::Config::CONFIG_APP_FILENAME, std::filesystem::canonical(Program)); + FEXCore::Config::Set(FEXCore::Config::CONFIG_IS64BIT_MODE, Loader.Is64BitMode() ? "1" : "0"); - if (!Loader.MapMemory([](void *addr, size_t length, int prot, int flags, int fd, off_t offset) { + + FEX::HLE::x32::MemAllocator *Allocator = nullptr; + + if (Loader.Is64BitMode()) { + if (!Loader.MapMemory([](void *addr, size_t length, int prot, int flags, int fd, off_t offset) { return mmap(addr, length, prot, flags, fd, offset); }, [](void *addr, size_t length) { return munmap(addr, length); })) { - // failed to map - return -ENOEXEC; + // failed to map + return -ENOEXEC; + } + } else { + Allocator = new FEX::HLE::x32::MemAllocator(); + if (!Loader.MapMemory([Allocator](void *addr, size_t length, int prot, int flags, int fd, off_t offset) { + return Allocator->mmap(addr, length, prot, flags, fd, offset); + }, [Allocator](void *addr, size_t length) { + return Allocator->munmap(addr, length); + })) { + // failed to map + return -ENOEXEC; + } } - - FEXCore::Config::Set(FEXCore::Config::CONFIG_APP_FILENAME, std::filesystem::canonical(Program)); - FEXCore::Config::Set(FEXCore::Config::CONFIG_IS64BIT_MODE, Loader.Is64BitMode() ? "1" : "0"); - - uint32_t KernelVersion = FEX::HLE::SyscallHandler::CalculateHostKernelVersion(); - if (KernelVersion < FEX::HLE::SyscallHandler::KernelVersion(4, 17) && - !Loader.Is64BitMode()) { - // We require 4.17 minimum for MAP_FIXED_NOREPLACE on 32bit ELFs - LogMan::Msg::E("FEXLoader requires kernel 4.17 minimum. Expect problems."); - } - + FEXCore::Context::InitializeStaticTables(Loader.Is64BitMode() ? FEXCore::Context::MODE_64BIT : FEXCore::Context::MODE_32BIT); auto CTX = FEXCore::Context::CreateNewContext(); FEXCore::Context::InitializeContext(CTX); std::unique_ptr SignalDelegation = std::make_unique(); + std::unique_ptr SyscallHandler{ - FEX::HLE::CreateHandler( - Loader.Is64BitMode() ? FEXCore::Context::OperatingMode::MODE_64BIT : FEXCore::Context::OperatingMode::MODE_32BIT, - CTX, - SignalDelegation.get(), - &Loader)}; + Loader.Is64BitMode() ? + FEX::HLE::x64::CreateHandler(CTX, SignalDelegation.get()) : + FEX::HLE::x32::CreateHandler(CTX, SignalDelegation.get(), Allocator) + }; + + SyscallHandler->SetCodeLoader(&Loader); + auto BRKInfo = Loader.GetBRKInfo(); //fprintf(stderr, "BRK %lX - %ld\n", BRKInfo.Base, BRKInfo.Size); diff --git a/Source/Tests/LinuxSyscalls/Syscalls.cpp b/Source/Tests/LinuxSyscalls/Syscalls.cpp index 1d08231de..8b2d1df3a 100644 --- a/Source/Tests/LinuxSyscalls/Syscalls.cpp +++ b/Source/Tests/LinuxSyscalls/Syscalls.cpp @@ -314,21 +314,4 @@ uint64_t UnimplementedSyscallSafe(FEXCore::Core::CpuStateFrame *Frame, uint64_t return -ENOSYS; } -FEX::HLE::SyscallHandler *CreateHandler(FEXCore::Context::OperatingMode Mode, - FEXCore::Context::Context *ctx, - FEX::HLE::SignalDelegator *_SignalDelegation, - FEXCore::CodeLoader *Loader) { - - FEX::HLE::SyscallHandler *Result{}; - if (Mode == FEXCore::Context::MODE_64BIT) { - Result = FEX::HLE::x64::CreateHandler(ctx, _SignalDelegation); - } - else { - Result = FEX::HLE::x32::CreateHandler(ctx, _SignalDelegation); - } - - Result->SetCodeLoader(Loader); - return Result; -} - } diff --git a/Source/Tests/LinuxSyscalls/Syscalls.h b/Source/Tests/LinuxSyscalls/Syscalls.h index e5e7898b4..69f49810c 100644 --- a/Source/Tests/LinuxSyscalls/Syscalls.h +++ b/Source/Tests/LinuxSyscalls/Syscalls.h @@ -146,11 +146,6 @@ private: }; -FEX::HLE::SyscallHandler *CreateHandler(FEXCore::Context::OperatingMode Mode, - FEXCore::Context::Context *ctx, - FEX::HLE::SignalDelegator *_SignalDelegation, - FEXCore::CodeLoader *Loader - ); uint64_t HandleSyscall(SyscallHandler *Handler, FEXCore::Core::CpuStateFrame *Frame, FEXCore::HLE::SyscallArguments *Args); #define SYSCALL_ERRNO() do { if (Result == -1) return -errno; return Result; } while(0) diff --git a/Source/Tests/LinuxSyscalls/x32/Syscalls.cpp b/Source/Tests/LinuxSyscalls/x32/Syscalls.cpp index 0a7a1372d..85eac38b5 100644 --- a/Source/Tests/LinuxSyscalls/x32/Syscalls.cpp +++ b/Source/Tests/LinuxSyscalls/x32/Syscalls.cpp @@ -527,9 +527,9 @@ uint64_t MemAllocator::shmdt(const void* shmaddr) { }); } - x32SyscallHandler::x32SyscallHandler(FEXCore::Context::Context *ctx, FEX::HLE::SignalDelegator *_SignalDelegation) + x32SyscallHandler::x32SyscallHandler(FEXCore::Context::Context *ctx, FEX::HLE::SignalDelegator *_SignalDelegation, MemAllocator *Allocator) : SyscallHandler {ctx, _SignalDelegation} { - AllocHandler = std::make_unique(); + AllocHandler.reset(Allocator); OSABI = FEXCore::HLE::SyscallOSABI::OS_LINUX32; RegisterSyscallHandlers(); } @@ -607,8 +607,8 @@ uint64_t MemAllocator::shmdt(const void* shmaddr) { #endif } - FEX::HLE::SyscallHandler *CreateHandler(FEXCore::Context::Context *ctx, FEX::HLE::SignalDelegator *_SignalDelegation) { - return new x32SyscallHandler(ctx, _SignalDelegation); + FEX::HLE::SyscallHandler *CreateHandler(FEXCore::Context::Context *ctx, FEX::HLE::SignalDelegator *_SignalDelegation, MemAllocator *Allocator) { + return new x32SyscallHandler(ctx, _SignalDelegation, Allocator); } } diff --git a/Source/Tests/LinuxSyscalls/x32/Syscalls.h b/Source/Tests/LinuxSyscalls/x32/Syscalls.h index 873b48685..144cdfd04 100644 --- a/Source/Tests/LinuxSyscalls/x32/Syscalls.h +++ b/Source/Tests/LinuxSyscalls/x32/Syscalls.h @@ -97,7 +97,7 @@ private: class x32SyscallHandler final : public FEX::HLE::SyscallHandler { public: - x32SyscallHandler(FEXCore::Context::Context *ctx, FEX::HLE::SignalDelegator *_SignalDelegation); + x32SyscallHandler(FEXCore::Context::Context *ctx, FEX::HLE::SignalDelegator *_SignalDelegation, MemAllocator *Allocator); FEX::HLE::x32::MemAllocator *GetAllocator() { return AllocHandler.get(); } @@ -106,7 +106,7 @@ private: std::unique_ptr AllocHandler{}; }; -FEX::HLE::SyscallHandler *CreateHandler(FEXCore::Context::Context *ctx, FEX::HLE::SignalDelegator *_SignalDelegation); +FEX::HLE::SyscallHandler *CreateHandler(FEXCore::Context::Context *ctx, FEX::HLE::SignalDelegator *_SignalDelegation, MemAllocator *Allocator); void RegisterSyscallInternal(int SyscallNumber, #ifdef DEBUG_STRACE const std::string& TraceFormatString,