mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
Compare commits
34
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e4026481ec | ||
|
|
67e46ce419 | ||
|
|
b160011cd9 | ||
|
|
75eba86428 | ||
|
|
d06af47b1c | ||
|
|
1f34d90955 | ||
|
|
046f02368f | ||
|
|
22484faef4 | ||
|
|
fff1831daa | ||
|
|
ed9e87dec5 | ||
|
|
730fe7e7c6 | ||
|
|
e691375eea | ||
|
|
fb9d584394 | ||
|
|
960594b54e | ||
|
|
8773a80cd5 | ||
|
|
015eface3a | ||
|
|
2e5b987292 | ||
|
|
36b62a0707 | ||
|
|
e482c2968e | ||
|
|
138a018f10 | ||
|
|
0086c789e0 | ||
|
|
b6986d884e | ||
|
|
f7fedffe9c | ||
|
|
225a0a85aa | ||
|
|
a3ef731d07 | ||
|
|
8e652b8bed | ||
|
|
f31f7d73f1 | ||
|
|
fa6b0f8c44 | ||
|
|
561d0126e6 | ||
|
|
654080557c | ||
|
|
d105cbc6ec | ||
|
|
a766120f1a | ||
|
|
d36f19db74 | ||
|
|
5a64a05b70 |
No files matched your search
@@ -68,6 +68,7 @@ DroidDeck is [GPL-3.0](LICENSE). Steam and Proton belong to Valve Corporation. V
|
||||
- Runtime, shim, input and controller work build on WinNative and Bannerlator by maxjivi05.
|
||||
- mmap ring buffer controller input by ribbit384 ([WinNative #345](https://github.com/WinNative-Emu/WinNative/pull/345)).
|
||||
- [FEX](https://github.com/FEX-Emu/FEX) (MIT) runs x86 code. DroidDeck's build puts the platform name under `AT_PLATFORM` so [sharun](https://github.com/VHSgunzo/sharun) AppImages by VHSgunzo (MIT) start through its [userland-execve](https://crates.io/crates/userland-execve) loader (MIT), and its GL thunk hands X errors to the program's own error handler. FEX's code disk cache was checked in its source. The fix for host stack left behind when a program leaves a signal handler without returning follows the approach suggested in [FEX-Emu/FEX#2487](https://github.com/FEX-Emu/FEX/issues/2487). The host stack fix for system calls that end a block is a backport of Ryan Houdek's [FEX-Emu/FEX#6122](https://github.com/FEX-Emu/FEX/pull/6122).
|
||||
- Anti-cheat support under FEX is the work of [StonkGuy](https://github.com/StonkGuy): the x86-64 ptrace, seccomp and `/proc` emulation from his [FEX fork](https://github.com/StonkGuy/FEX) (`eac-2611` branch, MIT) is carried as build patch 0020 with his authorship, patches 0021 to 0026 build on it, and his `eac-tests` regression suite verifies them.
|
||||
- x86-64 programs on Wayland reach the GPU through FEX's Wayland client, wayland-egl and EGL thunks. The guest `wl_list` and `wl_array` helpers follow [libwayland](https://gitlab.freedesktop.org/wayland/wayland)'s `src/wayland-util.c` (MIT). The EGL extension table comes from the [Khronos EGL registry](https://github.com/KhronosGroup/EGL-Registry) (Apache-2.0) and Mesa's `egl_other.xml` (MIT).
|
||||
- x86 AppImages, and arm64 ones whose runtime can't unpack on Android, are unpacked with [uruntime](https://github.com/VHSgunzo/uruntime) by VHSgunzo (MIT), shipped unmodified with its licence.
|
||||
- DwarFS AppImages are unpacked without proot by `dwarfsextract` from [DwarFS](https://github.com/mhx/dwarfs) by Marcus Holland-Moritz (MIT), the release binary uruntime embeds, taken out of its self-extracting wrapper and shipped with its licence.
|
||||
|
||||
@@ -329,16 +329,28 @@ static constexpr int kWatchedFds = 1 << 16;
|
||||
static constexpr int kWatchedBits = sizeof(unsigned long) * 8;
|
||||
static std::atomic<unsigned long> watched_fds[kWatchedFds / kWatchedBits];
|
||||
static std::atomic<bool> watched_beyond{false};
|
||||
static std::atomic<int> watched_count{0};
|
||||
|
||||
static void watch_fd(int fd) {
|
||||
if (fd < 0) return;
|
||||
if (fd >= kWatchedFds) watched_beyond.store(true, std::memory_order_release);
|
||||
else watched_fds[fd / kWatchedBits].fetch_or(1UL << (fd % kWatchedBits), std::memory_order_release);
|
||||
if (fd >= kWatchedFds) {
|
||||
watched_beyond.store(true, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
const unsigned long bit = 1UL << (fd % kWatchedBits);
|
||||
if (!(watched_fds[fd / kWatchedBits].fetch_or(bit, std::memory_order_acq_rel) & bit))
|
||||
watched_count.fetch_add(1, std::memory_order_acq_rel);
|
||||
}
|
||||
|
||||
static void unwatch_fd(int fd) {
|
||||
if (fd >= 0 && fd < kWatchedFds)
|
||||
watched_fds[fd / kWatchedBits].fetch_and(~(1UL << (fd % kWatchedBits)), std::memory_order_release);
|
||||
if (fd < 0 || fd >= kWatchedFds) return;
|
||||
const unsigned long bit = 1UL << (fd % kWatchedBits);
|
||||
if (watched_fds[fd / kWatchedBits].fetch_and(~bit, std::memory_order_acq_rel) & bit)
|
||||
watched_count.fetch_sub(1, std::memory_order_acq_rel);
|
||||
}
|
||||
|
||||
static bool any_watched() {
|
||||
return watched_count.load(std::memory_order_acquire) > 0 || watched_beyond.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
static bool watched(int fd) {
|
||||
@@ -4269,7 +4281,7 @@ static int poll_fake(struct pollfd *fds, nfds_t nfds, int timeout,
|
||||
const auto my_poll = next_symbol<decltype(&::poll)>(Next::poll);
|
||||
const auto my_ppoll = next_symbol<decltype(&::ppoll)>(Next::ppoll);
|
||||
|
||||
nfds_t first = 0;
|
||||
nfds_t first = any_watched() ? 0 : nfds;
|
||||
while (first < nfds && !watched(fds[first].fd)) first++;
|
||||
bool has_fake_fds = false;
|
||||
std::vector<struct pollfd> real_fds;
|
||||
@@ -4372,12 +4384,12 @@ EXPORT int poll(struct pollfd *fds, nfds_t nfds, int timeout) {
|
||||
EXPORT int ppoll(struct pollfd *fds, nfds_t nfds,
|
||||
const struct timespec *timeout, const sigset_t *sigmask) {
|
||||
const auto my_ppoll = next_symbol<decltype(&::ppoll)>(Next::ppoll);
|
||||
if (timeout && (timeout->tv_sec < 0 || timeout->tv_nsec < 0 || timeout->tv_nsec >= 1000000000L)) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
for (nfds_t i = 0; i < nfds; ++i) {
|
||||
for (nfds_t i = 0; any_watched() && i < nfds; ++i) {
|
||||
if (!is_fake_input_fd(fds[i].fd)) continue;
|
||||
if (timeout && (timeout->tv_sec < 0 || timeout->tv_nsec < 0 || timeout->tv_nsec >= 1000000000L)) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (!sigmask)
|
||||
return poll_fake(fds, nfds, static_cast<int>(timespec_to_ms(timeout)), nullptr);
|
||||
sigset_t all_signals, original_mask;
|
||||
@@ -4410,6 +4422,8 @@ EXPORT int __ppoll_chk(struct pollfd *fds, nfds_t nfds, const struct timespec *t
|
||||
EXPORT int select(int nfds, fd_set *readfds, fd_set *writefds,
|
||||
fd_set *exceptfds, struct timeval *timeout) {
|
||||
const auto my_select = next_symbol<decltype(&::select)>(Next::select);
|
||||
if (!any_watched())
|
||||
return my_select ? my_select(nfds, readfds, writefds, exceptfds, timeout) : -1;
|
||||
|
||||
fd_set original_readfds;
|
||||
fd_set original_writefds;
|
||||
|
||||
@@ -66,11 +66,14 @@ object ProtonExtras {
|
||||
|
||||
internal fun installedIn(dir: File, tool: Tool): List<String> =
|
||||
dir.listFiles()
|
||||
?.filter { it.isDirectory && it.name.startsWith(tool.prefix) && usable(it) }
|
||||
?.filter { it.isDirectory && it.name.startsWith(tool.prefix) && !x86_64Build(it) && usable(it) }
|
||||
?.map { it.name }
|
||||
?.sortedWith { a, b -> compareVersions(a, b) }
|
||||
?: emptyList()
|
||||
|
||||
internal fun x86_64Build(dir: File): Boolean =
|
||||
File(dir, "files/lib/wine/x86_64-unix").isDirectory && !File(dir, "files/bin-arm64").isDirectory
|
||||
|
||||
private fun usable(dir: File): Boolean {
|
||||
val manifest = runCatching { File(dir, "toolmanifest.vdf").readText() }.getOrNull() ?: return false
|
||||
val command = Regex("\\\"commandline\\\"\\s+\\\"/?([^\\\" ]+)").find(manifest)?.groupValues?.getOrNull(1) ?: return false
|
||||
|
||||
@@ -181,6 +181,17 @@ class ProtonExtrasTest {
|
||||
assertEquals(emptyList<String>(), ProtonExtras.installedIn(File(tools, "missing"), tool("ge")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun x86_64BuildsAreNotTakenForTheArm64Tool() {
|
||||
val tools = temp.newFolder("compatibilitytools.d")
|
||||
for (name in listOf("DroidDeck-Proton 11-7.5", "DroidDeck-Proton-11-14.2-x86_64")) installTool(tools, name)
|
||||
File(tools, "DroidDeck-Proton-11-14.2-x86_64/files/lib/wine/x86_64-unix").mkdirs()
|
||||
File(tools, "DroidDeck-Proton 11-7.5/files/lib/wine/x86_64-unix").mkdirs()
|
||||
File(tools, "DroidDeck-Proton 11-7.5/files/bin-arm64").mkdirs()
|
||||
assertEquals(listOf("DroidDeck-Proton 11-7.5"), ProtonExtras.installedIn(tools, tool("droiddeck")))
|
||||
assertTrue(ProtonExtras.x86_64Build(File(tools, "DroidDeck-Proton-11-14.2-x86_64")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun interruptedBuildsAreNotReportedAsInstalled() {
|
||||
val tools = temp.newFolder("compatibilitytools.d")
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# Easy Anti-Cheat and BattlEye
|
||||
|
||||
On a Steam Deck, Proton loads Valve's anti-cheat runtimes (Proton EasyAntiCheat Runtime, app 1826330, and
|
||||
Proton BattlEye Runtime, app 1161040). Their bridge libraries are x86 only, so the ARM64 DroidDeck-Proton
|
||||
cannot load them. Games that need them run an x86-64 DroidDeck-Proton under FEX instead.
|
||||
|
||||
## Routing
|
||||
|
||||
The Proton launchers (`steam-compatibility`: the built-in DroidDeck-Proton launcher and the wrapper put around
|
||||
every tool in `compatibilitytools.d`) check each `run` and `waitforexitandrun` before any ARM64 setup:
|
||||
|
||||
- **Condition.** The game folder (`STEAM_COMPAT_INSTALL_PATH`, three levels deep) holds one of
|
||||
`EasyAntiCheat/Settings.json`, `easyanticheat_x64.so`, `BEClient_x64.so` or `BEService_x64.exe`. No
|
||||
title, app ID or device list is involved.
|
||||
- **Tool.** The newest `compatibilitytools.d/DroidDeck-Proton*` whose layout is x86-64
|
||||
(`files/lib/wine/x86_64-unix`, no `files/bin-arm64`).
|
||||
- **Runtime.** `PROTON_EAC_RUNTIME` or `PROTON_BATTLEYE_RUNTIME` when it already points at a folder with the
|
||||
bridge libraries, otherwise the runtime's folder in any Steam library.
|
||||
- **Launch.** `droiddeck-fex proton <tool>/proton <verb> <args>`: FEX with the Steam Linux Runtime rootfs,
|
||||
keeping the game's own `STEAM_COMPAT_DATA_PATH` prefix.
|
||||
|
||||
Without the x86-64 tool or the runtime files the launch stays on ARM64 and the launcher logs why.
|
||||
`DROIDDECK_ANTICHEAT=off` in the game's launch options keeps it on ARM64. Picking the x86-64 tool for a
|
||||
game in Steam always runs it under FEX.
|
||||
|
||||
## Why the x86-64 build needs its own Wine patches
|
||||
|
||||
Android kernels use 39-bit user address spaces. Easy Anti-Cheat's launcher maps fixed memory at
|
||||
`0x7001_0000_0000`, which FEX serves from emulated guest pages (`tools/fex/patches/0026`). The x86-64 Wine
|
||||
in DroidDeck-Proton carries `ntdll-fixed-allocations-above-host-address-limit.patch` (fixed mappings above
|
||||
the host limit go to the emulator) and `ntdll-keep-libc-in-native-syscall-dispatch-range.patch` (the
|
||||
re-armed syscall user dispatch range keeps libc native). Stock x86-64 Proton stops after "Hack native
|
||||
syscalls" on the same game.
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,132 @@
|
||||
From: maxjivi05 <maxjividen@gmail.com>
|
||||
Subject: [PATCH] List only guest mappings in /proc/<pid>/maps on hosts below 47 address bits
|
||||
|
||||
Builds on StonkGuy's /proc emulation from https://github.com/StonkGuy/FEX (eac-2611, MIT).
|
||||
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
index 1b2396b01..156e3964c 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
@@ -17,7 +17,11 @@ $end_info$
|
||||
#include <FEXCore/Config/Config.h>
|
||||
#include <FEXCore/Core/Context.h>
|
||||
#include <FEXCore/Core/CPUID.h>
|
||||
+#include <FEXCore/Utils/Allocator.h>
|
||||
#include <FEXCore/Utils/LogManager.h>
|
||||
+#include <FEXCore/Utils/SignalScopeGuards.h>
|
||||
+#include <FEXCore/fextl/unordered_map.h>
|
||||
+#include <FEXCore/fextl/vector.h>
|
||||
#include <FEXCore/fextl/fmt.h>
|
||||
#include <FEXCore/fextl/string.h>
|
||||
#include <FEXCore/fextl/sstream.h>
|
||||
@@ -25,9 +29,13 @@ $end_info$
|
||||
|
||||
#include <git_version.h>
|
||||
|
||||
+#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
+#include <elf.h>
|
||||
+#include <mutex>
|
||||
+#include <shared_mutex>
|
||||
#include <fcntl.h>
|
||||
#include <filesystem>
|
||||
#include <ostream>
|
||||
@@ -608,6 +616,37 @@ EmulatedFDManager::EmulatedFDManager(FEXCore::Context::Context* ctx)
|
||||
|
||||
EmulatedFDManager::~EmulatedFDManager() {}
|
||||
|
||||
+static bool IsSelfProcPath(const char* Path) {
|
||||
+ const char* P = Path + 6;
|
||||
+ if (!isdigit(*P)) {
|
||||
+ return true;
|
||||
+ }
|
||||
+ return strtol(P, nullptr, 10) == ::getpid();
|
||||
+}
|
||||
+
|
||||
+static bool IsHostArchModule(const char* Path, fextl::unordered_map<fextl::string, bool>& Seen) {
|
||||
+ auto Known = Seen.find(Path);
|
||||
+ if (Known != Seen.end()) {
|
||||
+ return Known->second;
|
||||
+ }
|
||||
+ bool Host = false;
|
||||
+ int FD = ::open(Path, O_RDONLY | O_CLOEXEC);
|
||||
+ if (FD >= 0) {
|
||||
+ Elf64_Ehdr Header {};
|
||||
+ if (::pread(FD, &Header, sizeof(Header), 0) >= static_cast<ssize_t>(offsetof(Elf64_Ehdr, e_version)) &&
|
||||
+ memcmp(Header.e_ident, ELFMAG, SELFMAG) == 0) {
|
||||
+#if defined(__aarch64__)
|
||||
+ Host = Header.e_machine == EM_AARCH64;
|
||||
+#else
|
||||
+ Host = Header.e_machine != EM_X86_64 && Header.e_machine != EM_386;
|
||||
+#endif
|
||||
+ }
|
||||
+ ::close(FD);
|
||||
+ }
|
||||
+ Seen.emplace(Path, Host);
|
||||
+ return Host;
|
||||
+}
|
||||
+
|
||||
static bool IsProcMapsPath(const char* Path) {
|
||||
if (strncmp(Path, "/proc/", 6) != 0) {
|
||||
return false;
|
||||
@@ -660,14 +699,56 @@ int32_t EmulatedFDManager::ProcMaps(FEXCore::Context::Context* ctx, int32_t fd,
|
||||
::close(HostFD);
|
||||
|
||||
constexpr uint64_t X86_64TaskSizeMax = (1ULL << 47) - 4096;
|
||||
+ const bool HostSharesGuestRange = FEXCore::Allocator::GetHostVABits() < 47;
|
||||
+ const bool Self = IsSelfProcPath(pathname);
|
||||
+ fextl::vector<std::pair<uint64_t, uint64_t>> GuestRanges;
|
||||
+ if (HostSharesGuestRange && Self && FEX::HLE::_SyscallHandler) {
|
||||
+ auto lk = FEXCore::MaskSignalsAndLockMutex<std::shared_lock>(FEX::HLE::_SyscallHandler->VMATracking.Mutex);
|
||||
+ GuestRanges.reserve(FEX::HLE::_SyscallHandler->VMATracking.VMAs.size());
|
||||
+ for (const auto& [Base, VMA] : FEX::HLE::_SyscallHandler->VMATracking.VMAs) {
|
||||
+ if (!GuestRanges.empty() && GuestRanges.back().second >= Base) {
|
||||
+ GuestRanges.back().second = std::max(GuestRanges.back().second, Base + VMA.Length);
|
||||
+ } else {
|
||||
+ GuestRanges.emplace_back(Base, Base + VMA.Length);
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ auto IsGuestRange = [&GuestRanges](uint64_t Start, uint64_t End) {
|
||||
+ auto It = std::upper_bound(GuestRanges.begin(), GuestRanges.end(), Start,
|
||||
+ [](uint64_t Value, const std::pair<uint64_t, uint64_t>& Range) { return Value < Range.first; });
|
||||
+ if (It != GuestRanges.begin() && std::prev(It)->second > Start) {
|
||||
+ return true;
|
||||
+ }
|
||||
+ return It != GuestRanges.end() && It->first < End;
|
||||
+ };
|
||||
+ fextl::unordered_map<fextl::string, bool> ModuleArch;
|
||||
fextl::string Out;
|
||||
Out.reserve(In.size());
|
||||
size_t Pos = 0;
|
||||
while (Pos < In.size()) {
|
||||
size_t Eol = In.find('\n', Pos);
|
||||
const size_t End = Eol == fextl::string::npos ? In.size() : Eol + 1;
|
||||
- const uint64_t Start = strtoull(In.c_str() + Pos, nullptr, 16);
|
||||
- if (Start < X86_64TaskSizeMax) {
|
||||
+ char* Cursor {};
|
||||
+ const uint64_t Start = strtoull(In.c_str() + Pos, &Cursor, 16);
|
||||
+ const uint64_t Stop = *Cursor == '-' ? strtoull(Cursor + 1, nullptr, 16) : Start;
|
||||
+ bool Keep = Start < X86_64TaskSizeMax;
|
||||
+ if (Keep && HostSharesGuestRange) {
|
||||
+ if (Self) {
|
||||
+ Keep = IsGuestRange(Start, Stop);
|
||||
+ } else {
|
||||
+ const size_t LineEnd = Eol == fextl::string::npos ? In.size() : Eol;
|
||||
+ size_t Name = In.find_first_of("/[", Pos);
|
||||
+ if (Name != fextl::string::npos && Name < LineEnd) {
|
||||
+ fextl::string Path(In, Name, LineEnd - Name);
|
||||
+ if (Path.starts_with("[anon:FEX")) {
|
||||
+ Keep = false;
|
||||
+ } else if (Path[0] == '/') {
|
||||
+ Keep = !IsHostArchModule(Path.c_str(), ModuleArch);
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ if (Keep) {
|
||||
Out.append(In, Pos, End - Pos);
|
||||
}
|
||||
Pos = End;
|
||||
@@ -0,0 +1,53 @@
|
||||
From 597bfab6ff068e0f6c08f63d6940486446967b0d Mon Sep 17 00:00:00 2001
|
||||
From: maxjivi05 <maxjividen@gmail.com>
|
||||
Date: Wed, 2 Dec 2026 18:21:23 -0500
|
||||
Subject: [PATCH] Report the calling thread's seccomp mode in /proc/self/status
|
||||
|
||||
Builds on StonkGuy's anti-cheat emulation from https://github.com/StonkGuy/FEX (eac-2611, MIT).
|
||||
|
||||
---
|
||||
.../LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp | 6 +++++-
|
||||
.../Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h | 10 ++++++++++
|
||||
2 files changed, 15 insertions(+), 1 deletion(-)
|
||||
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
index 156e3964c..c1a91d122 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
@@ -796,7 +796,11 @@ int32_t EmulatedFDManager::ProcStatus(FEXCore::Context::Context* ctx, int32_t fd
|
||||
Buf[Len] = '\0';
|
||||
|
||||
uint32_t SeccompMode = SECCOMP_MODE_DISABLED;
|
||||
- if (auto* Thread = FEX::HLE::ThreadManager::GetCurrentThreadObject()) {
|
||||
+ auto* Thread = FEX::HLE::_SyscallHandler ? FEX::HLE::_SyscallHandler->TM.FindThreadByTID(::gettid()) : nullptr;
|
||||
+ if (!Thread) {
|
||||
+ Thread = FEX::HLE::ThreadManager::GetCurrentThreadObject();
|
||||
+ }
|
||||
+ if (Thread) {
|
||||
SeccompMode = Thread->SeccompMode;
|
||||
}
|
||||
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
|
||||
index c5bfe3c25..657de6ceb 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
|
||||
@@ -289,6 +289,16 @@ public:
|
||||
return &Threads;
|
||||
}
|
||||
|
||||
+ FEX::HLE::ThreadStateObject* FindThreadByTID(uint32_t TID) {
|
||||
+ std::lock_guard lk(ThreadCreationMutex);
|
||||
+ for (auto* Thread : Threads) {
|
||||
+ if (Thread->ThreadInfo.TID == TID) {
|
||||
+ return Thread;
|
||||
+ }
|
||||
+ }
|
||||
+ return nullptr;
|
||||
+ }
|
||||
+
|
||||
private:
|
||||
struct InvalidationStats {
|
||||
explicit InvalidationStats(FEXCore::Core::InternalThreadState* Thread)
|
||||
--
|
||||
2.43.0
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
From ab6a01db9e93e7f143b9a3da5b40dcb160b960f3 Mon Sep 17 00:00:00 2001
|
||||
From: maxjivi05 <maxjividen@gmail.com>
|
||||
Date: Wed, 2 Dec 2026 18:25:20 -0500
|
||||
Subject: [PATCH] Read FEX's own /proc/self/status with a direct openat system
|
||||
call
|
||||
|
||||
Builds on StonkGuy's anti-cheat emulation from https://github.com/StonkGuy/FEX (eac-2611, MIT).
|
||||
|
||||
---
|
||||
FEXHeaderUtils/FEXHeaderUtils/Syscalls.h | 30 +++++++++++++++++++
|
||||
.../EmulatedFiles/EmulatedFiles.cpp | 3 +-
|
||||
.../LinuxSyscalls/PtraceEmu.cpp | 3 +-
|
||||
3 files changed, 34 insertions(+), 2 deletions(-)
|
||||
|
||||
diff --git a/FEXHeaderUtils/FEXHeaderUtils/Syscalls.h b/FEXHeaderUtils/FEXHeaderUtils/Syscalls.h
|
||||
index dae3d3bcc..c0f3eb3ed 100644
|
||||
--- a/FEXHeaderUtils/FEXHeaderUtils/Syscalls.h
|
||||
+++ b/FEXHeaderUtils/FEXHeaderUtils/Syscalls.h
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <FEXCore/Utils/LogManager.h>
|
||||
|
||||
#include <cstdint>
|
||||
+#include <errno.h>
|
||||
#include <sched.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
@@ -123,4 +124,33 @@ inline int32_t gettid() {
|
||||
|
||||
#endif
|
||||
|
||||
+#ifndef _WIN32
|
||||
+inline int32_t openat_kernel(int dirfd, const char* pathname, int flags) {
|
||||
+#if defined(__aarch64__)
|
||||
+ register long x0 asm("x0") = dirfd;
|
||||
+ register long x1 asm("x1") = reinterpret_cast<long>(pathname);
|
||||
+ register long x2 asm("x2") = flags;
|
||||
+ register long x3 asm("x3") = 0;
|
||||
+ register long x8 asm("x8") = SYS_openat;
|
||||
+ asm volatile("svc #0" : "+r"(x0) : "r"(x1), "r"(x2), "r"(x3), "r"(x8) : "memory");
|
||||
+ if (x0 < 0) {
|
||||
+ errno = static_cast<int>(-x0);
|
||||
+ return -1;
|
||||
+ }
|
||||
+ return static_cast<int32_t>(x0);
|
||||
+#elif defined(__x86_64__)
|
||||
+ long ret;
|
||||
+ register long r10 asm("r10") = 0;
|
||||
+ asm volatile("syscall" : "=a"(ret) : "a"(SYS_openat), "D"(dirfd), "S"(pathname), "d"(flags), "r"(r10) : "rcx", "r11", "memory");
|
||||
+ if (ret < 0) {
|
||||
+ errno = static_cast<int>(-ret);
|
||||
+ return -1;
|
||||
+ }
|
||||
+ return static_cast<int32_t>(ret);
|
||||
+#else
|
||||
+ return static_cast<int32_t>(::syscall(SYS_openat, dirfd, pathname, flags));
|
||||
+#endif
|
||||
+}
|
||||
+#endif
|
||||
+
|
||||
} // namespace FHU::Syscalls
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
index c1a91d122..49adaaa66 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
@@ -14,6 +14,7 @@ $end_info$
|
||||
#include "LinuxSyscalls/Syscalls.h"
|
||||
#include "LinuxSyscalls/EmulatedFiles/EmulatedFiles.h"
|
||||
|
||||
+#include <FEXHeaderUtils/Syscalls.h>
|
||||
#include <FEXCore/Config/Config.h>
|
||||
#include <FEXCore/Core/Context.h>
|
||||
#include <FEXCore/Core/CPUID.h>
|
||||
@@ -784,7 +785,7 @@ int32_t EmulatedFDManager::ProcStatus(FEXCore::Context::Context* ctx, int32_t fd
|
||||
char Buf[8192];
|
||||
ssize_t Len = 0;
|
||||
{
|
||||
- int HostFD = ::open("/proc/self/status", O_RDONLY | O_CLOEXEC);
|
||||
+ int HostFD = FHU::Syscalls::openat_kernel(AT_FDCWD, "/proc/self/status", O_RDONLY | O_CLOEXEC);
|
||||
if (HostFD >= 0) {
|
||||
Len = ::read(HostFD, Buf, sizeof(Buf) - 1);
|
||||
::close(HostFD);
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
|
||||
index ca4c86063..49ece24a3 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "LinuxSyscalls/PtraceEmu.h"
|
||||
#include "LinuxSyscalls/Syscalls.h"
|
||||
|
||||
+#include <FEXHeaderUtils/Syscalls.h>
|
||||
#include <FEXCore/Core/CoreState.h>
|
||||
#include <FEXCore/Core/X86Enums.h>
|
||||
|
||||
@@ -133,7 +134,7 @@ namespace {
|
||||
}
|
||||
|
||||
bool HasHostTracer() {
|
||||
- const int fd = ::open("/proc/self/status", O_RDONLY | O_CLOEXEC);
|
||||
+ const int fd = FHU::Syscalls::openat_kernel(AT_FDCWD, "/proc/self/status", O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
return true;
|
||||
}
|
||||
--
|
||||
2.43.0
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
From 38ee3ef08d9bfd8312fccff688290572da82ef2a Mon Sep 17 00:00:00 2001
|
||||
From: maxjivi05 <maxjividen@gmail.com>
|
||||
Date: Wed, 2 Dec 2026 18:41:52 -0500
|
||||
Subject: [PATCH] Hand the tracee's ptrace state across exec
|
||||
|
||||
Builds on StonkGuy's anti-cheat emulation from https://github.com/StonkGuy/FEX (eac-2611, MIT).
|
||||
|
||||
---
|
||||
.../LinuxSyscalls/PtraceEmu.cpp | 54 ++++++++++++++++++-
|
||||
1 file changed, 53 insertions(+), 1 deletion(-)
|
||||
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
|
||||
index 49ece24a3..62acdb0da 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
|
||||
@@ -266,6 +266,38 @@ namespace {
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
+ bool AdoptionTried = false;
|
||||
+
|
||||
+ void AdoptSharedPage() {
|
||||
+ if (AdoptionTried || Current != &StaticState || !IsMainThread()) {
|
||||
+ return;
|
||||
+ }
|
||||
+ AdoptionTried = true;
|
||||
+ const int fd = OpenSidecar("state", ::getpid(), O_RDWR);
|
||||
+ if (fd < 0) {
|
||||
+ return;
|
||||
+ }
|
||||
+ if (!HasHostTracer()) {
|
||||
+ ::close(fd);
|
||||
+ return;
|
||||
+ }
|
||||
+ void* Page = ::mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
+ ::close(fd);
|
||||
+ if (Page == MAP_FAILED) {
|
||||
+ return;
|
||||
+ }
|
||||
+ auto* S = static_cast<SharedState*>(Page);
|
||||
+ if (S->Magic != SharedState::MagicValue) {
|
||||
+ ::munmap(Page, 4096);
|
||||
+ return;
|
||||
+ }
|
||||
+ S->Pending = 0;
|
||||
+ S->DirtyMask = 0;
|
||||
+ S->SignalNumber = 0;
|
||||
+ S->FlushLen = 0;
|
||||
+ Current = S;
|
||||
+ }
|
||||
+
|
||||
struct Remote {
|
||||
uint8_t* Mapped = nullptr;
|
||||
uintptr_t StaticAddr = 0;
|
||||
@@ -332,6 +364,22 @@ namespace {
|
||||
return &Remotes.emplace(Pid, std::move(R)).first->second;
|
||||
}
|
||||
|
||||
+ bool KeepAcrossExec(pid_t Pid) {
|
||||
+ {
|
||||
+ std::lock_guard lk(RemotesLock);
|
||||
+ auto It = Remotes.find(Pid);
|
||||
+ if (It != Remotes.end()) {
|
||||
+ return It->second.Mapped != nullptr;
|
||||
+ }
|
||||
+ }
|
||||
+ const int fd = OpenSidecar("state", Pid, O_RDONLY);
|
||||
+ if (fd < 0) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ ::close(fd);
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
void Forget(pid_t Pid, bool Exited) {
|
||||
std::lock_guard lk(RemotesLock);
|
||||
auto It = Remotes.find(Pid);
|
||||
@@ -470,10 +518,12 @@ namespace {
|
||||
|
||||
|
||||
bool IsAttached() {
|
||||
+ AdoptSharedPage();
|
||||
return Current->Attached != 0;
|
||||
}
|
||||
|
||||
bool WantsSyscallStops() {
|
||||
+ AdoptSharedPage();
|
||||
return Current->SyscallStops && IsMainThread();
|
||||
}
|
||||
|
||||
@@ -610,7 +660,9 @@ bool FixupWaitStatus(pid_t Pid, int* Status) {
|
||||
|
||||
if ((St >> 16) == X86_PTRACE_EVENT_EXEC) {
|
||||
CaptureExecPath(Pid);
|
||||
- Forget(Pid, false);
|
||||
+ if (!KeepAcrossExec(Pid)) {
|
||||
+ Forget(Pid, false);
|
||||
+ }
|
||||
}
|
||||
|
||||
if (WSTOPSIG(St) == SIGSTOP) {
|
||||
--
|
||||
2.43.0
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
From 834075bdee10fb0a1c7a7f499a0bb97162370b5d Mon Sep 17 00:00:00 2001
|
||||
From: maxjivi05 <maxjividen@gmail.com>
|
||||
Date: Wed, 2 Dec 2026 18:58:13 -0500
|
||||
Subject: [PATCH] Serve emulated proc files by the requested path when the host
|
||||
returns a memfd
|
||||
|
||||
Builds on StonkGuy's anti-cheat emulation from https://github.com/StonkGuy/FEX (eac-2611, MIT).
|
||||
---
|
||||
.../LinuxEmulation/LinuxSyscalls/FileManagement.cpp | 11 +++++++----
|
||||
.../LinuxEmulation/LinuxSyscalls/FileManagement.h | 2 +-
|
||||
2 files changed, 8 insertions(+), 5 deletions(-)
|
||||
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/FileManagement.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/FileManagement.cpp
|
||||
index 62b6a2d27..b76e96e58 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/FileManagement.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/FileManagement.cpp
|
||||
@@ -785,7 +785,7 @@ static int OpenatWithoutOpenat2(int DirFD, const char* Pathname, int Flags, uint
|
||||
return ::syscall(SYSCALL_DEF(openat), DirFD, Resolved, Flags, Mode);
|
||||
}
|
||||
|
||||
-bool FileManager::ReplaceEmuFd(int fd, int flags, uint32_t mode) {
|
||||
+bool FileManager::ReplaceEmuFd(int fd, int flags, uint32_t mode, const char* RequestedPath) {
|
||||
char Tmp[PATH_MAX + 1];
|
||||
|
||||
if (fd < 0) {
|
||||
@@ -801,6 +801,9 @@ bool FileManager::ReplaceEmuFd(int fd, int flags, uint32_t mode) {
|
||||
|
||||
// And try to open via EmuFD
|
||||
auto EmuFd = EmuFD.Open(Tmp, flags, mode);
|
||||
+ if (EmuFd == -1 && RequestedPath && RequestedPath[0] == '/' && strncmp(Tmp, "/memfd:", 7) == 0) {
|
||||
+ EmuFd = EmuFD.Open(RequestedPath, flags, mode);
|
||||
+ }
|
||||
if (EmuFd == -1) {
|
||||
return false;
|
||||
}
|
||||
@@ -841,7 +844,7 @@ uint64_t FileManager::Open(const char* pathname, int flags, uint32_t mode) {
|
||||
fd = ::open(SelfPath, flags, mode);
|
||||
}
|
||||
|
||||
- ReplaceEmuFd(fd, flags, mode);
|
||||
+ ReplaceEmuFd(fd, flags, mode, SelfPath);
|
||||
} else {
|
||||
fd = ::open(SelfPath, flags, mode);
|
||||
}
|
||||
@@ -1135,7 +1138,7 @@ uint64_t FileManager::Openat([[maybe_unused]] int dirfs, const char* pathname, i
|
||||
fd = ::syscall(SYSCALL_DEF(openat), dirfs, SelfPath, flags, mode);
|
||||
}
|
||||
|
||||
- ReplaceEmuFd(fd, flags, mode);
|
||||
+ ReplaceEmuFd(fd, flags, mode, SelfPath);
|
||||
} else {
|
||||
fd = ::syscall(SYSCALL_DEF(openat), dirfs, SelfPath, flags, mode);
|
||||
}
|
||||
@@ -1173,7 +1176,7 @@ uint64_t FileManager::Openat2(int dirfs, const char* pathname, FEX::HLE::open_ho
|
||||
fd = ::syscall(SYSCALL_DEF(openat2), dirfs, SelfPath, how, usize);
|
||||
}
|
||||
|
||||
- ReplaceEmuFd(fd, how->flags, how->mode);
|
||||
+ ReplaceEmuFd(fd, how->flags, how->mode, SelfPath);
|
||||
} else {
|
||||
fd = ::syscall(SYSCALL_DEF(openat2), dirfs, SelfPath, how, usize);
|
||||
}
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/FileManagement.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/FileManagement.h
|
||||
index b11f096f7..a86539972 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/FileManagement.h
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/FileManagement.h
|
||||
@@ -102,7 +102,7 @@ public:
|
||||
fextl::string GetEmulatedPath(const char* pathname, bool FollowSymlink = false) const;
|
||||
fextl::string GetHostPath(fextl::string& Path, bool AliasedOnly) const;
|
||||
|
||||
- bool ReplaceEmuFd(int fd, int flags, uint32_t mode);
|
||||
+ bool ReplaceEmuFd(int fd, int flags, uint32_t mode, const char* RequestedPath);
|
||||
|
||||
#if defined(ASSERTIONS_ENABLED) && ASSERTIONS_ENABLED
|
||||
void TrackFEXFD(int FD) noexcept {
|
||||
--
|
||||
2.43.0
|
||||
|
||||
@@ -0,0 +1,781 @@
|
||||
From 4e237204720bf57c8e4b7dae1ff74b5e341763e6 Mon Sep 17 00:00:00 2001
|
||||
From: maxjivi05 <maxjividen@gmail.com>
|
||||
Date: Wed, 2 Dec 2026 20:01:44 -0500
|
||||
Subject: [PATCH] Map fixed guest memory above a small host address space
|
||||
|
||||
Hosts with 39-bit or 42-bit user address spaces cannot place x86-64
|
||||
mappings that ask for a fixed address above that limit. Such mappings
|
||||
are now backed by host memory at any address, and guest accesses to
|
||||
them are redirected through a per-thread single-instruction trampoline.
|
||||
Syscall arguments that point into these mappings are translated, and
|
||||
writes to code in them invalidate translated blocks.
|
||||
|
||||
Builds on StonkGuy's anti-cheat emulation from https://github.com/StonkGuy/FEX (eac-2611, MIT).
|
||||
---
|
||||
Source/Tools/LinuxEmulation/CMakeLists.txt | 1 +
|
||||
.../LinuxSyscalls/GuestHighMemory.cpp | 439 ++++++++++++++++++
|
||||
.../LinuxSyscalls/GuestHighMemory.h | 26 ++
|
||||
.../LinuxSyscalls/SignalDelegator.cpp | 5 +
|
||||
.../LinuxEmulation/LinuxSyscalls/Syscalls.cpp | 30 +-
|
||||
.../LinuxSyscalls/SyscallsSMCTracking.cpp | 58 ++-
|
||||
.../LinuxSyscalls/ThreadManager.cpp | 2 +
|
||||
.../LinuxSyscalls/ThreadManager.h | 12 +
|
||||
8 files changed, 551 insertions(+), 22 deletions(-)
|
||||
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
|
||||
|
||||
diff --git a/Source/Tools/LinuxEmulation/CMakeLists.txt b/Source/Tools/LinuxEmulation/CMakeLists.txt
|
||||
index 3f02104ea..acf4c4c13 100644
|
||||
--- a/Source/Tools/LinuxEmulation/CMakeLists.txt
|
||||
+++ b/Source/Tools/LinuxEmulation/CMakeLists.txt
|
||||
@@ -18,6 +18,7 @@ add_library(LinuxEmulation STATIC
|
||||
LinuxSyscalls/Seccomp/BPFEmitter.cpp
|
||||
LinuxSyscalls/Seccomp/Dumper.cpp
|
||||
LinuxSyscalls/SignalDelegator.cpp
|
||||
+ LinuxSyscalls/GuestHighMemory.cpp
|
||||
LinuxSyscalls/Syscalls.cpp
|
||||
LinuxSyscalls/SyscallsSMCTracking.cpp
|
||||
LinuxSyscalls/SyscallsVMATracking.cpp
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
new file mode 100644
|
||||
index 000000000..a097f97fa
|
||||
--- /dev/null
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
@@ -0,0 +1,439 @@
|
||||
+#include "LinuxSyscalls/GuestHighMemory.h"
|
||||
+#include "LinuxSyscalls/Syscalls.h"
|
||||
+#include "LinuxSyscalls/SignalDelegator.h"
|
||||
+#include "LinuxSyscalls/ThreadManager.h"
|
||||
+#include "ArchHelpers/MContext.h"
|
||||
+
|
||||
+#include <FEXCore/Debug/InternalThreadState.h>
|
||||
+#include <FEXCore/Utils/Allocator.h>
|
||||
+#include <FEXCore/Utils/AllocatorHooks.h>
|
||||
+#include <FEXCore/Utils/MathUtils.h>
|
||||
+#include <FEXCore/Utils/TypeDefines.h>
|
||||
+
|
||||
+#include <algorithm>
|
||||
+#include <atomic>
|
||||
+#include <cerrno>
|
||||
+#include <csignal>
|
||||
+#include <cstring>
|
||||
+#include <sys/mman.h>
|
||||
+
|
||||
+namespace FEX::HLE::GuestHighMemory {
|
||||
+namespace {
|
||||
+ constexpr uint64_t TaskTop = (1ULL << 47) - FEXCore::Utils::FEX_PAGE_SIZE;
|
||||
+ constexpr uint32_t MaxRegions = 1024;
|
||||
+
|
||||
+ struct Region {
|
||||
+ std::atomic<uint64_t> Guest;
|
||||
+ std::atomic<uint64_t> Size;
|
||||
+ std::atomic<uint64_t> Host;
|
||||
+ std::atomic<bool> Code;
|
||||
+ };
|
||||
+
|
||||
+ struct RegionView {
|
||||
+ uint64_t Guest;
|
||||
+ uint64_t Size;
|
||||
+ uint64_t Host;
|
||||
+ uint32_t Index;
|
||||
+ bool Code;
|
||||
+ };
|
||||
+
|
||||
+ Region Regions[MaxRegions];
|
||||
+ std::atomic<uint32_t> Count {0};
|
||||
+ std::atomic<uint32_t> Sequence {0};
|
||||
+
|
||||
+ uint64_t HostLimit() {
|
||||
+ static const uint64_t Limit = [] {
|
||||
+ const auto Bits = FEXCore::Allocator::GetHostVABits();
|
||||
+ return Bits >= 47 ? TaskTop : (1ULL << Bits);
|
||||
+ }();
|
||||
+ return Limit;
|
||||
+ }
|
||||
+
|
||||
+ void BeginWrite() {
|
||||
+ Sequence.fetch_add(1, std::memory_order_acq_rel);
|
||||
+ }
|
||||
+
|
||||
+ void EndWrite() {
|
||||
+ Sequence.fetch_add(1, std::memory_order_release);
|
||||
+ }
|
||||
+
|
||||
+ bool Lookup(uint64_t Address, RegionView* Out) {
|
||||
+ for (;;) {
|
||||
+ const auto Before = Sequence.load(std::memory_order_acquire);
|
||||
+ if (Before & 1) {
|
||||
+ continue;
|
||||
+ }
|
||||
+ bool Found = false;
|
||||
+ const auto Entries = Count.load(std::memory_order_acquire);
|
||||
+ for (uint32_t i = 0; i < Entries; ++i) {
|
||||
+ const auto Guest = Regions[i].Guest.load(std::memory_order_relaxed);
|
||||
+ const auto Size = Regions[i].Size.load(std::memory_order_relaxed);
|
||||
+ if (Address >= Guest && Address - Guest < Size) {
|
||||
+ Out->Guest = Guest;
|
||||
+ Out->Size = Size;
|
||||
+ Out->Host = Regions[i].Host.load(std::memory_order_relaxed);
|
||||
+ Out->Code = Regions[i].Code.load(std::memory_order_relaxed);
|
||||
+ Out->Index = i;
|
||||
+ Found = true;
|
||||
+ break;
|
||||
+ }
|
||||
+ }
|
||||
+ std::atomic_thread_fence(std::memory_order_acquire);
|
||||
+ if (Sequence.load(std::memory_order_relaxed) == Before) {
|
||||
+ return Found;
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ void SetEntry(uint32_t i, uint64_t Guest, uint64_t Size, uint64_t Host, bool Code) {
|
||||
+ Regions[i].Guest.store(Guest, std::memory_order_relaxed);
|
||||
+ Regions[i].Size.store(Size, std::memory_order_relaxed);
|
||||
+ Regions[i].Host.store(Host, std::memory_order_relaxed);
|
||||
+ Regions[i].Code.store(Code, std::memory_order_relaxed);
|
||||
+ }
|
||||
+
|
||||
+ void RemoveEntry(uint32_t i) {
|
||||
+ const auto Last = Count.load(std::memory_order_relaxed) - 1;
|
||||
+ if (i != Last) {
|
||||
+ SetEntry(i, Regions[Last].Guest.load(std::memory_order_relaxed), Regions[Last].Size.load(std::memory_order_relaxed),
|
||||
+ Regions[Last].Host.load(std::memory_order_relaxed), Regions[Last].Code.load(std::memory_order_relaxed));
|
||||
+ }
|
||||
+ Count.store(Last, std::memory_order_release);
|
||||
+ }
|
||||
+
|
||||
+ bool AddEntry(uint64_t Guest, uint64_t Size, uint64_t Host, bool Code) {
|
||||
+ const auto Entries = Count.load(std::memory_order_relaxed);
|
||||
+ if (Entries >= MaxRegions) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ SetEntry(Entries, Guest, Size, Host, Code);
|
||||
+ Count.store(Entries + 1, std::memory_order_release);
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ bool UnmapLocked(uint64_t Address, uint64_t Length) {
|
||||
+ const auto End = Address + Length;
|
||||
+ uint32_t i = 0;
|
||||
+ while (i < Count.load(std::memory_order_relaxed)) {
|
||||
+ const auto Guest = Regions[i].Guest.load(std::memory_order_relaxed);
|
||||
+ const auto Size = Regions[i].Size.load(std::memory_order_relaxed);
|
||||
+ const auto Host = Regions[i].Host.load(std::memory_order_relaxed);
|
||||
+ const auto Code = Regions[i].Code.load(std::memory_order_relaxed);
|
||||
+ const auto RegionEnd = Guest + Size;
|
||||
+ if (RegionEnd <= Address || Guest >= End) {
|
||||
+ ++i;
|
||||
+ continue;
|
||||
+ }
|
||||
+ const auto OverlapStart = std::max(Guest, Address);
|
||||
+ const auto OverlapEnd = std::min(RegionEnd, End);
|
||||
+ ::munmap(reinterpret_cast<void*>(Host + (OverlapStart - Guest)), OverlapEnd - OverlapStart);
|
||||
+
|
||||
+ if (OverlapStart == Guest && OverlapEnd == RegionEnd) {
|
||||
+ RemoveEntry(i);
|
||||
+ continue;
|
||||
+ }
|
||||
+ if (OverlapStart == Guest) {
|
||||
+ SetEntry(i, OverlapEnd, RegionEnd - OverlapEnd, Host + (OverlapEnd - Guest), Code);
|
||||
+ } else if (OverlapEnd == RegionEnd) {
|
||||
+ SetEntry(i, Guest, OverlapStart - Guest, Host, Code);
|
||||
+ } else {
|
||||
+ if (!AddEntry(OverlapEnd, RegionEnd - OverlapEnd, Host + (OverlapEnd - Guest), Code)) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ SetEntry(i, Guest, OverlapStart - Guest, Host, Code);
|
||||
+ }
|
||||
+ ++i;
|
||||
+ }
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ constexpr uint32_t RtField(uint32_t Insn) {
|
||||
+ return Insn & 0x1f;
|
||||
+ }
|
||||
+
|
||||
+ constexpr uint32_t RnField(uint32_t Insn) {
|
||||
+ return (Insn >> 5) & 0x1f;
|
||||
+ }
|
||||
+
|
||||
+ constexpr uint32_t Rt2Field(uint32_t Insn) {
|
||||
+ return (Insn >> 10) & 0x1f;
|
||||
+ }
|
||||
+
|
||||
+ constexpr uint32_t RsField(uint32_t Insn) {
|
||||
+ return (Insn >> 16) & 0x1f;
|
||||
+ }
|
||||
+
|
||||
+ constexpr bool IsLoadStore(uint32_t Insn) {
|
||||
+ return (Insn & 0x0a000000) == 0x08000000;
|
||||
+ }
|
||||
+
|
||||
+ constexpr bool IsLiteralLoad(uint32_t Insn) {
|
||||
+ return (Insn & 0x3b000000) == 0x18000000;
|
||||
+ }
|
||||
+
|
||||
+ constexpr bool IsWrite(uint32_t Insn) {
|
||||
+ if ((Insn & 0x3b200c00) == 0x38200000) {
|
||||
+ return ((Insn >> 12) & 0xf) != 0xc;
|
||||
+ }
|
||||
+ if ((Insn & 0x3f200000) == 0x08200000 && Rt2Field(Insn) == 0x1f) {
|
||||
+ return true;
|
||||
+ }
|
||||
+ return (Insn & (1U << 22)) == 0;
|
||||
+ }
|
||||
+
|
||||
+ int PickScratch(uint32_t Insn) {
|
||||
+ constexpr int Candidates[] = {16, 17, 9, 10, 11, 12, 13, 14, 15, 8, 7, 6, 5, 4, 3, 2};
|
||||
+ for (auto Reg : Candidates) {
|
||||
+ const auto R = static_cast<uint32_t>(Reg);
|
||||
+ if (R != RtField(Insn) && R != RnField(Insn) && R != Rt2Field(Insn) && R != RsField(Insn)) {
|
||||
+ return Reg;
|
||||
+ }
|
||||
+ }
|
||||
+ return -1;
|
||||
+ }
|
||||
+
|
||||
+ constexpr uint64_t SynchronousSignalMask() {
|
||||
+ uint64_t Mask = 0;
|
||||
+ for (int Sig : {SIGSEGV, SIGBUS, SIGILL, SIGTRAP, SIGFPE}) {
|
||||
+ Mask |= 1ULL << (Sig - 1);
|
||||
+ }
|
||||
+ return Mask;
|
||||
+ }
|
||||
+
|
||||
+ constexpr uint32_t TrapInstruction = 0xd4200000 | (0xf3e << 5);
|
||||
+} // namespace
|
||||
+
|
||||
+bool Contains(uint64_t Address) {
|
||||
+ return Address >= HostLimit() && Address < TaskTop;
|
||||
+}
|
||||
+
|
||||
+bool ClaimsMapping(uint64_t Address, uint64_t Length, int Flags) {
|
||||
+ if (!(Flags & MAP_FIXED) || (Flags & MAP_FIXED_NOREPLACE) || Length == 0) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ if (Address & (FEXCore::Utils::FEX_PAGE_SIZE - 1)) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ const auto Size = FEXCore::AlignUp(Length, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+ return Contains(Address) && Size <= TaskTop - Address;
|
||||
+}
|
||||
+
|
||||
+uint64_t Map(uint64_t Address, uint64_t Length, int Prot, int Flags, int FD, off_t Offset) {
|
||||
+ const auto Size = FEXCore::AlignUp(Length, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+ const int HostFlags = Flags & ~(MAP_FIXED | MAP_FIXED_NOREPLACE);
|
||||
+ void* Host = ::mmap(nullptr, Size, Prot, HostFlags, FD, Offset);
|
||||
+ if (Host == MAP_FAILED) {
|
||||
+ return -errno;
|
||||
+ }
|
||||
+
|
||||
+ BeginWrite();
|
||||
+ bool Ok = UnmapLocked(Address, Size) && AddEntry(Address, Size, reinterpret_cast<uint64_t>(Host), false);
|
||||
+ EndWrite();
|
||||
+ if (!Ok) {
|
||||
+ ::munmap(Host, Size);
|
||||
+ return -ENOMEM;
|
||||
+ }
|
||||
+ return Address;
|
||||
+}
|
||||
+
|
||||
+uint64_t Unmap(uint64_t Address, uint64_t Length) {
|
||||
+ if (Address & (FEXCore::Utils::FEX_PAGE_SIZE - 1)) {
|
||||
+ return -EINVAL;
|
||||
+ }
|
||||
+ const auto Size = FEXCore::AlignUp(Length, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+ if (Size == 0 || Size > TaskTop - Address) {
|
||||
+ return -EINVAL;
|
||||
+ }
|
||||
+ BeginWrite();
|
||||
+ bool Ok = UnmapLocked(Address, Size);
|
||||
+ EndWrite();
|
||||
+ return Ok ? 0 : -ENOMEM;
|
||||
+}
|
||||
+
|
||||
+uint64_t Protect(uint64_t Address, uint64_t Length, int Prot) {
|
||||
+ if (Address & (FEXCore::Utils::FEX_PAGE_SIZE - 1)) {
|
||||
+ return -EINVAL;
|
||||
+ }
|
||||
+ const auto Size = FEXCore::AlignUp(Length, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+ if (Size == 0) {
|
||||
+ return 0;
|
||||
+ }
|
||||
+ const auto End = Address + Size;
|
||||
+ uint64_t Covered = 0;
|
||||
+ const auto Entries = Count.load(std::memory_order_acquire);
|
||||
+ for (uint32_t i = 0; i < Entries; ++i) {
|
||||
+ const auto Guest = Regions[i].Guest.load(std::memory_order_relaxed);
|
||||
+ const auto RegionEnd = Guest + Regions[i].Size.load(std::memory_order_relaxed);
|
||||
+ if (RegionEnd <= Address || Guest >= End) {
|
||||
+ continue;
|
||||
+ }
|
||||
+ const auto OverlapStart = std::max(Guest, Address);
|
||||
+ const auto OverlapEnd = std::min(RegionEnd, End);
|
||||
+ const auto Host = Regions[i].Host.load(std::memory_order_relaxed) + (OverlapStart - Guest);
|
||||
+ if (::mprotect(reinterpret_cast<void*>(Host), OverlapEnd - OverlapStart, Prot) != 0) {
|
||||
+ return -errno;
|
||||
+ }
|
||||
+ Covered += OverlapEnd - OverlapStart;
|
||||
+ }
|
||||
+ return Covered == Size ? 0 : -ENOMEM;
|
||||
+}
|
||||
+
|
||||
+uint64_t ToHost(uint64_t Address) {
|
||||
+ RegionView View;
|
||||
+ if (!Contains(Address) || !Lookup(Address, &View)) {
|
||||
+ return Address;
|
||||
+ }
|
||||
+ return View.Host + (Address - View.Guest);
|
||||
+}
|
||||
+
|
||||
+void MarkCode(uint64_t Address, uint64_t Length) {
|
||||
+ const auto End = Address + Length;
|
||||
+ const auto Entries = Count.load(std::memory_order_acquire);
|
||||
+ for (uint32_t i = 0; i < Entries; ++i) {
|
||||
+ const auto Guest = Regions[i].Guest.load(std::memory_order_relaxed);
|
||||
+ const auto RegionEnd = Guest + Regions[i].Size.load(std::memory_order_relaxed);
|
||||
+ if (RegionEnd > Address && Guest < End) {
|
||||
+ Regions[i].Code.store(true, std::memory_order_relaxed);
|
||||
+ }
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+void TranslateSyscallArguments(uint64_t Syscall, uint64_t* Args, uint32_t NumArgs) {
|
||||
+ if (Count.load(std::memory_order_relaxed) == 0) [[likely]] {
|
||||
+ return;
|
||||
+ }
|
||||
+ switch (Syscall) {
|
||||
+ case 9:
|
||||
+ case 10:
|
||||
+ case 11:
|
||||
+ case 12:
|
||||
+ case 15:
|
||||
+ case 25:
|
||||
+ case 30:
|
||||
+ case 56:
|
||||
+ case 67:
|
||||
+ case 101:
|
||||
+ case 157:
|
||||
+ case 158:
|
||||
+ case 218:
|
||||
+ case 273:
|
||||
+ case 310:
|
||||
+ case 311:
|
||||
+ case 435: return;
|
||||
+ default: break;
|
||||
+ }
|
||||
+ for (uint32_t i = 0; i < NumArgs; ++i) {
|
||||
+ Args[i] = ToHost(Args[i]);
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+bool HandleFault(FEXCore::Core::InternalThreadState* Thread, int Signal, void* Info, void* UContext) {
|
||||
+ auto* SigInfo = static_cast<siginfo_t*>(Info);
|
||||
+ const auto FaultAddress = reinterpret_cast<uint64_t>(SigInfo->si_addr);
|
||||
+ auto ThreadObject = FEX::HLE::ThreadManager::GetStateObjectFromFEXCoreThread(Thread);
|
||||
+ auto& Trap = ThreadObject->HighMemoryTrap;
|
||||
+ const auto PC = ArchHelpers::Context::GetPc(UContext);
|
||||
+
|
||||
+ if (Trap.Active && PC == reinterpret_cast<uint64_t>(Trap.Code)) {
|
||||
+ ArchHelpers::Context::SetArmReg(UContext, Trap.Scratch, Trap.SavedScratch);
|
||||
+ ArchHelpers::Context::SetArmReg(UContext, Trap.Base, Trap.BaseBefore);
|
||||
+ ArchHelpers::Context::SetPc(UContext, Trap.ResumePC - 4);
|
||||
+ memcpy(&ArchHelpers::Context::GetUContext(UContext)->uc_sigmask, &Trap.SavedMask, sizeof(Trap.SavedMask));
|
||||
+ SigInfo->si_addr = reinterpret_cast<void*>(FaultAddress - Trap.Delta);
|
||||
+ Trap.Active = false;
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ if (!Contains(FaultAddress) || Trap.Active) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ RegionView View;
|
||||
+ if (!Lookup(FaultAddress, &View)) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ const auto Insn = *reinterpret_cast<const uint32_t*>(PC);
|
||||
+ if (!IsLoadStore(Insn) || IsLiteralLoad(Insn) || RnField(Insn) == 31) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ if (View.Code && IsWrite(Insn)) {
|
||||
+ const auto FaultBase = FEXCore::AlignDown(FaultAddress, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+ FEX::HLE::_SyscallHandler->TM.InvalidateGuestCodeRange(Thread, FaultBase, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+
|
||||
+ constexpr uint64_t MaxStoreSize = 64;
|
||||
+ const uint64_t StoreWindowStart = FaultAddress - MaxStoreSize;
|
||||
+ auto CTX = Thread->CTX;
|
||||
+ if (CTX->IsAddressInCodeBuffer(Thread, PC) && !CTX->IsCurrentBlockSingleInst(Thread) &&
|
||||
+ CTX->IsAddressInCurrentBlock(Thread, StoreWindowStart, 2 * MaxStoreSize)) {
|
||||
+ ThreadObject->SignalInfo.Delegator->SpillSRA(Thread, UContext, Thread->CurrentFrame->InSyscallInfo & 0xFFFF);
|
||||
+ const auto& Config = ThreadObject->SignalInfo.Delegator->GetConfig();
|
||||
+ ArchHelpers::Context::SetPc(UContext, Config.AbsoluteLoopTopAddressFillSRA);
|
||||
+ ArchHelpers::Context::SetArmReg(UContext, 1, 1);
|
||||
+ return true;
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ const int Scratch = PickScratch(Insn);
|
||||
+ if (Scratch < 0) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ if (!Trap.Code) {
|
||||
+ void* Page = FEXCore::Allocator::VirtualAlloc(FEXCore::Utils::FEX_PAGE_SIZE, true);
|
||||
+ if (Page == MAP_FAILED || Page == nullptr) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ Trap.Code = static_cast<uint8_t*>(Page);
|
||||
+ }
|
||||
+
|
||||
+ const auto Base = RnField(Insn);
|
||||
+ auto* Code = reinterpret_cast<uint32_t*>(Trap.Code);
|
||||
+ Code[0] = (Insn & ~(0x1fU << 5)) | (static_cast<uint32_t>(Scratch) << 5);
|
||||
+ Code[1] = TrapInstruction;
|
||||
+ __builtin___clear_cache(reinterpret_cast<char*>(Code), reinterpret_cast<char*>(Code + 2));
|
||||
+
|
||||
+ Trap.Scratch = Scratch;
|
||||
+ Trap.Base = Base;
|
||||
+ Trap.SavedScratch = ArchHelpers::Context::GetArmReg(UContext, Scratch);
|
||||
+ Trap.BaseBefore = ArchHelpers::Context::GetArmReg(UContext, Base);
|
||||
+ Trap.Delta = View.Host - View.Guest;
|
||||
+ Trap.ResumePC = PC + 4;
|
||||
+
|
||||
+ auto* Mask = &ArchHelpers::Context::GetUContext(UContext)->uc_sigmask;
|
||||
+ memcpy(&Trap.SavedMask, Mask, sizeof(Trap.SavedMask));
|
||||
+ const uint64_t Blocked = ~SynchronousSignalMask();
|
||||
+ memcpy(Mask, &Blocked, sizeof(Blocked));
|
||||
+
|
||||
+ ArchHelpers::Context::SetArmReg(UContext, Scratch, Trap.BaseBefore + Trap.Delta);
|
||||
+ ArchHelpers::Context::SetPc(UContext, reinterpret_cast<uint64_t>(Code));
|
||||
+ Trap.Active = true;
|
||||
+ return true;
|
||||
+}
|
||||
+
|
||||
+bool HandleTrap(FEXCore::Core::InternalThreadState* Thread, int Signal, void* Info, void* UContext) {
|
||||
+ auto ThreadObject = FEX::HLE::ThreadManager::GetStateObjectFromFEXCoreThread(Thread);
|
||||
+ auto& Trap = ThreadObject->HighMemoryTrap;
|
||||
+ if (!Trap.Active || ArchHelpers::Context::GetPc(UContext) != reinterpret_cast<uint64_t>(Trap.Code) + 4) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ const auto ScratchAfter = ArchHelpers::Context::GetArmReg(UContext, Trap.Scratch);
|
||||
+ if (ScratchAfter != Trap.BaseBefore + Trap.Delta) {
|
||||
+ ArchHelpers::Context::SetArmReg(UContext, Trap.Base, ScratchAfter - Trap.Delta);
|
||||
+ }
|
||||
+ ArchHelpers::Context::SetArmReg(UContext, Trap.Scratch, Trap.SavedScratch);
|
||||
+ memcpy(&ArchHelpers::Context::GetUContext(UContext)->uc_sigmask, &Trap.SavedMask, sizeof(Trap.SavedMask));
|
||||
+ ArchHelpers::Context::SetPc(UContext, Trap.ResumePC);
|
||||
+ Trap.Active = false;
|
||||
+ return true;
|
||||
+}
|
||||
+
|
||||
+void ReleaseThread(FEX::HLE::ThreadStateObject* Thread) {
|
||||
+ if (Thread->HighMemoryTrap.Code) {
|
||||
+ FEXCore::Allocator::munmap(Thread->HighMemoryTrap.Code, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+ Thread->HighMemoryTrap.Code = nullptr;
|
||||
+ }
|
||||
+}
|
||||
+} // namespace FEX::HLE::GuestHighMemory
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
|
||||
new file mode 100644
|
||||
index 000000000..326b1a37d
|
||||
--- /dev/null
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
|
||||
@@ -0,0 +1,26 @@
|
||||
+#pragma once
|
||||
+
|
||||
+#include <cstdint>
|
||||
+#include <sys/types.h>
|
||||
+
|
||||
+namespace FEXCore::Core {
|
||||
+struct InternalThreadState;
|
||||
+}
|
||||
+
|
||||
+namespace FEX::HLE {
|
||||
+struct ThreadStateObject;
|
||||
+}
|
||||
+
|
||||
+namespace FEX::HLE::GuestHighMemory {
|
||||
+bool Contains(uint64_t Address);
|
||||
+bool ClaimsMapping(uint64_t Address, uint64_t Length, int Flags);
|
||||
+uint64_t Map(uint64_t Address, uint64_t Length, int Prot, int Flags, int FD, off_t Offset);
|
||||
+uint64_t Unmap(uint64_t Address, uint64_t Length);
|
||||
+uint64_t Protect(uint64_t Address, uint64_t Length, int Prot);
|
||||
+uint64_t ToHost(uint64_t Address);
|
||||
+void MarkCode(uint64_t Address, uint64_t Length);
|
||||
+void TranslateSyscallArguments(uint64_t Syscall, uint64_t* Args, uint32_t Count);
|
||||
+bool HandleFault(FEXCore::Core::InternalThreadState* Thread, int Signal, void* Info, void* UContext);
|
||||
+bool HandleTrap(FEXCore::Core::InternalThreadState* Thread, int Signal, void* Info, void* UContext);
|
||||
+void ReleaseThread(FEX::HLE::ThreadStateObject* Thread);
|
||||
+} // namespace FEX::HLE::GuestHighMemory
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
|
||||
index efe49dbf1..e8723e8f1 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
|
||||
@@ -7,6 +7,7 @@ $end_info$
|
||||
*/
|
||||
|
||||
#include "LinuxSyscalls/SignalDelegator.h"
|
||||
+#include "LinuxSyscalls/GuestHighMemory.h"
|
||||
#include "LinuxSyscalls/Syscalls.h"
|
||||
#include "LinuxSyscalls/PtraceEmu.h"
|
||||
#include "LinuxSyscalls/SignalTrace.h"
|
||||
@@ -748,6 +749,10 @@ uint64_t SignalDelegator::GetNewSigMask(int Signal) const {
|
||||
}
|
||||
|
||||
bool SignalDelegator::HandleFrontendSIGSEGV(FEXCore::Core::InternalThreadState* Thread, int Signal, void* Info, void* UContext) {
|
||||
+ if (FEX::HLE::GuestHighMemory::HandleFault(Thread, Signal, Info, UContext)) {
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
auto SigInfo = *static_cast<siginfo_t*>(Info);
|
||||
|
||||
if (FaultSafeUserMemAccess::TryHandleSafeFault(Signal, SigInfo, UContext)) {
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp
|
||||
index 607ab0b97..078faedac 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp
|
||||
@@ -16,6 +16,7 @@ $end_info$
|
||||
#include "LinuxSyscalls/LinuxAllocator.h"
|
||||
#include "LinuxSyscalls/SignalDelegator.h"
|
||||
#include "LinuxSyscalls/PtraceEmu.h"
|
||||
+#include "LinuxSyscalls/GuestHighMemory.h"
|
||||
#include <limits.h>
|
||||
#include "LinuxSyscalls/Syscalls.h"
|
||||
#include "LinuxSyscalls/Syscalls/Thread.h"
|
||||
@@ -820,6 +821,7 @@ SyscallHandler::SyscallHandler(FEXCore::Context::Context* _CTX, FEX::HLE::Signal
|
||||
|
||||
SignalDelegation->RegisterHostSignalHandler(SIGSEGV, HandleSegfault, true);
|
||||
|
||||
+ SignalDelegation->RegisterHostSignalHandler(SIGTRAP, FEX::HLE::GuestHighMemory::HandleTrap, true);
|
||||
SignalDelegation->RegisterHostSignalHandler(
|
||||
SIGTRAP, [](FEXCore::Core::InternalThreadState*, int, void*, void*) { return false; }, true);
|
||||
|
||||
@@ -894,24 +896,22 @@ void SyscallHandler::HandleSyscallImpl(FEXCore::Core::CpuStateFrame* Frame, uint
|
||||
}
|
||||
|
||||
auto& Def = Definitions[SyscallNum];
|
||||
+ uint64_t A[7] {};
|
||||
+ for (unsigned i = 1; i < 7; ++i) {
|
||||
+ A[i] = GetArg(Is64Bit, Frame, i);
|
||||
+ }
|
||||
+ if constexpr (Is64Bit) {
|
||||
+ FEX::HLE::GuestHighMemory::TranslateSyscallArguments(SyscallNum, &A[1], 6);
|
||||
+ }
|
||||
uint64_t Result {};
|
||||
switch (Def.NumArgs) {
|
||||
case 0: Result = std::invoke(Def.Ptr0, Frame); break;
|
||||
- case 1: Result = std::invoke(Def.Ptr1, Frame, GetArg(Is64Bit, Frame, 1)); break;
|
||||
- case 2: Result = std::invoke(Def.Ptr2, Frame, GetArg(Is64Bit, Frame, 1), GetArg(Is64Bit, Frame, 2)); break;
|
||||
- case 3: Result = std::invoke(Def.Ptr3, Frame, GetArg(Is64Bit, Frame, 1), GetArg(Is64Bit, Frame, 2), GetArg(Is64Bit, Frame, 3)); break;
|
||||
- case 4:
|
||||
- Result =
|
||||
- std::invoke(Def.Ptr4, Frame, GetArg(Is64Bit, Frame, 1), GetArg(Is64Bit, Frame, 2), GetArg(Is64Bit, Frame, 3), GetArg(Is64Bit, Frame, 4));
|
||||
- break;
|
||||
- case 5:
|
||||
- Result = std::invoke(Def.Ptr5, Frame, GetArg(Is64Bit, Frame, 1), GetArg(Is64Bit, Frame, 2), GetArg(Is64Bit, Frame, 3),
|
||||
- GetArg(Is64Bit, Frame, 4), GetArg(Is64Bit, Frame, 5));
|
||||
- break;
|
||||
- case 6:
|
||||
- Result = std::invoke(Def.Ptr6, Frame, GetArg(Is64Bit, Frame, 1), GetArg(Is64Bit, Frame, 2), GetArg(Is64Bit, Frame, 3),
|
||||
- GetArg(Is64Bit, Frame, 4), GetArg(Is64Bit, Frame, 5), GetArg(Is64Bit, Frame, 6));
|
||||
- break;
|
||||
+ case 1: Result = std::invoke(Def.Ptr1, Frame, A[1]); break;
|
||||
+ case 2: Result = std::invoke(Def.Ptr2, Frame, A[1], A[2]); break;
|
||||
+ case 3: Result = std::invoke(Def.Ptr3, Frame, A[1], A[2], A[3]); break;
|
||||
+ case 4: Result = std::invoke(Def.Ptr4, Frame, A[1], A[2], A[3], A[4]); break;
|
||||
+ case 5: Result = std::invoke(Def.Ptr5, Frame, A[1], A[2], A[3], A[4], A[5]); break;
|
||||
+ case 6: Result = std::invoke(Def.Ptr6, Frame, A[1], A[2], A[3], A[4], A[5], A[6]); break;
|
||||
// for missing syscalls
|
||||
case 255: Result = std::invoke(Def.Ptr1, Frame, GetArg(Is64Bit, Frame, 0)); break;
|
||||
default:
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SyscallsSMCTracking.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/SyscallsSMCTracking.cpp
|
||||
index f9e85a26f..bebdfd300 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SyscallsSMCTracking.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SyscallsSMCTracking.cpp
|
||||
@@ -21,6 +21,7 @@ $end_info$
|
||||
|
||||
#include "LinuxSyscalls/Syscalls.h"
|
||||
#include "LinuxSyscalls/SignalDelegator.h"
|
||||
+#include "LinuxSyscalls/GuestHighMemory.h"
|
||||
|
||||
#include <FEXCore/Debug/InternalThreadState.h>
|
||||
#include <FEXCore/Utils/LogManager.h>
|
||||
@@ -45,6 +46,9 @@ static void HandleSegfaultForCodeCacheFinalization(FEXCore::Core::InternalThread
|
||||
void SyscallHandler::InvalidateWrittenCodePage(FEXCore::Core::InternalThreadState* Thread, uint64_t FaultBase, uint64_t EntryBase,
|
||||
const FEX::HLE::VMATracking::VMAEntry& Mapping) {
|
||||
auto UnprotectRegionCallback = [](uintptr_t Start, uintptr_t Length) {
|
||||
+ if (FEX::HLE::GuestHighMemory::Contains(Start)) {
|
||||
+ return;
|
||||
+ }
|
||||
auto rv = mprotect((void*)Start, Length, PROT_READ | PROT_WRITE);
|
||||
LogMan::Throw::AFmt(rv == 0, "mprotect({}, {}) failed", Start, Length);
|
||||
};
|
||||
@@ -93,6 +97,10 @@ bool SyscallHandler::HandleSegfault(FEXCore::Core::InternalThreadState* Thread,
|
||||
}
|
||||
}
|
||||
|
||||
+ if (FEX::HLE::GuestHighMemory::Contains(FaultAddress)) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
{
|
||||
// Can't use the deferred signal lock in the SIGSEGV handler.
|
||||
auto lk = FEXCore::MaskSignalsAndLockMutex<std::shared_lock>(_SyscallHandler->VMATracking.Mutex);
|
||||
@@ -176,6 +184,9 @@ void SyscallHandler::PrepareGuestBufferForKernelWrite(FEXCore::Core::InternalThr
|
||||
}
|
||||
} else {
|
||||
TM.InvalidateGuestCodeRange(Thread, From, To - From, [](uintptr_t Start, uintptr_t Length) {
|
||||
+ if (FEX::HLE::GuestHighMemory::Contains(Start)) {
|
||||
+ return;
|
||||
+ }
|
||||
auto rv = mprotect(reinterpret_cast<void*>(Start), Length, PROT_READ | PROT_WRITE);
|
||||
LogMan::Throw::AFmt(rv == 0, "mprotect({}, {}) failed", Start, Length);
|
||||
});
|
||||
@@ -227,7 +238,9 @@ void SyscallHandler::MarkGuestExecutableRange(FEXCore::Core::InternalThreadState
|
||||
const auto ProtectBase = std::max(MapBase, Base);
|
||||
const auto ProtectSize = std::min(MapTop, Top) - ProtectBase;
|
||||
|
||||
- if (Mapping->second.Flags.Shared) {
|
||||
+ if (FEX::HLE::GuestHighMemory::Contains(ProtectBase)) {
|
||||
+ FEX::HLE::GuestHighMemory::MarkCode(ProtectBase, ProtectSize);
|
||||
+ } else if (Mapping->second.Flags.Shared) {
|
||||
LOGMAN_THROW_A_FMT(Mapping->second.Resource, "VMA tracking error");
|
||||
|
||||
const auto OffsetBase = ProtectBase - Mapping->first + Mapping->second.Offset;
|
||||
@@ -245,6 +258,10 @@ void SyscallHandler::MarkGuestExecutableRange(FEXCore::Core::InternalThreadState
|
||||
|
||||
const auto MirroredBase = std::max(VMAOffsetBase, OffsetBase);
|
||||
const auto MirroredSize = std::min(OffsetTop, VMAOffsetTop) - MirroredBase;
|
||||
+ if (FEX::HLE::GuestHighMemory::Contains(MirroredBase - VMAOffsetBase + VMABase)) {
|
||||
+ FEX::HLE::GuestHighMemory::MarkCode(MirroredBase - VMAOffsetBase + VMABase, MirroredSize);
|
||||
+ continue;
|
||||
+ }
|
||||
|
||||
auto rv = mprotect((void*)(MirroredBase - VMAOffsetBase + VMABase), MirroredSize, PROT_READ);
|
||||
LogMan::Throw::AFmt(rv == 0, "mprotect({}, {}) failed", MirroredBase, MirroredSize);
|
||||
@@ -293,6 +310,10 @@ void SyscallHandler::ProtectWritableAliasOfCode(uint64_t Base, uint64_t Length)
|
||||
|
||||
const auto OverlapBase = std::max(VMA->Offset, OffsetBase);
|
||||
const auto OverlapTop = std::min(VMA->Offset + VMA->Length, OffsetTop);
|
||||
+ if (FEX::HLE::GuestHighMemory::Contains(MapBase + (OverlapBase - OffsetBase))) {
|
||||
+ FEX::HLE::GuestHighMemory::MarkCode(MapBase + (OverlapBase - OffsetBase), OverlapTop - OverlapBase);
|
||||
+ continue;
|
||||
+ }
|
||||
auto rv = mprotect((void*)(MapBase + (OverlapBase - OffsetBase)), OverlapTop - OverlapBase, PROT_READ);
|
||||
LogMan::Throw::AFmt(rv == 0, "mprotect({}, {}) failed", OverlapBase, OverlapTop - OverlapBase);
|
||||
}
|
||||
@@ -429,6 +450,11 @@ void* SyscallHandler::GuestMmap(bool Is64Bit, FEXCore::Core::InternalThreadState
|
||||
return reinterpret_cast<void*>(Result);
|
||||
}
|
||||
LOGMAN_THROW_A_FMT(Is64Bit || (Result >> 32) == 0 || (Result >> 32) == 0xFFFFFFFF, "values must fit to 32 bits");
|
||||
+ } else if (FEX::HLE::GuestHighMemory::ClaimsMapping(reinterpret_cast<uint64_t>(addr), length, flags)) {
|
||||
+ Result = FEX::HLE::GuestHighMemory::Map(reinterpret_cast<uint64_t>(addr), length, prot, flags, fd, offset);
|
||||
+ if (FEX::HLE::HasSyscallError(Result)) {
|
||||
+ return reinterpret_cast<void*>(Result);
|
||||
+ }
|
||||
} else {
|
||||
Result = reinterpret_cast<uint64_t>(::mmap(reinterpret_cast<void*>(addr), length, prot, flags, fd, offset));
|
||||
if (Result == ~0ULL) {
|
||||
@@ -483,9 +509,16 @@ uint64_t SyscallHandler::GuestMunmap(bool Is64Bit, FEXCore::Core::InternalThread
|
||||
if (Addr > X86_64TaskSizeMax || Size > X86_64TaskSizeMax - Addr || (length != 0 && Size == 0)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
- Result = ::munmap(addr, length);
|
||||
- if (Result == -1) {
|
||||
- return -errno;
|
||||
+ if (FEX::HLE::GuestHighMemory::Contains(Addr)) {
|
||||
+ Result = FEX::HLE::GuestHighMemory::Unmap(Addr, length);
|
||||
+ if (FEX::HLE::HasSyscallError(Result)) {
|
||||
+ return Result;
|
||||
+ }
|
||||
+ } else {
|
||||
+ Result = ::munmap(addr, length);
|
||||
+ if (Result == -1) {
|
||||
+ return -errno;
|
||||
+ }
|
||||
}
|
||||
if (reinterpret_cast<uintptr_t>(addr) < 0x1'0000'0000ULL) {
|
||||
Get32BitAllocator()->TrackUnmapped(addr, length);
|
||||
@@ -511,6 +544,10 @@ uint64_t SyscallHandler::GuestMremap(bool Is64Bit, FEXCore::Core::InternalThread
|
||||
{
|
||||
auto lk = FEXCore::GuardSignalDeferringSection(VMATracking.Mutex, Thread);
|
||||
if (Is64Bit) {
|
||||
+ if (FEX::HLE::GuestHighMemory::Contains(reinterpret_cast<uint64_t>(old_address)) ||
|
||||
+ ((flags & MREMAP_FIXED) && FEX::HLE::GuestHighMemory::Contains(reinterpret_cast<uint64_t>(new_address)))) {
|
||||
+ return -EINVAL;
|
||||
+ }
|
||||
Result = reinterpret_cast<uint64_t>(::mremap(old_address, old_size, new_size, flags, new_address));
|
||||
if (Result == -1) {
|
||||
return -errno;
|
||||
@@ -602,9 +639,16 @@ uint64_t SyscallHandler::GuestMprotect(FEXCore::Core::InternalThreadState* Threa
|
||||
}
|
||||
}
|
||||
|
||||
- Result = ::mprotect(addr, len, prot);
|
||||
- if (Result == -1) {
|
||||
- return -errno;
|
||||
+ if (FEX::HLE::GuestHighMemory::Contains(reinterpret_cast<uint64_t>(addr))) {
|
||||
+ Result = FEX::HLE::GuestHighMemory::Protect(reinterpret_cast<uint64_t>(addr), len, prot);
|
||||
+ if (FEX::HLE::HasSyscallError(Result)) {
|
||||
+ return Result;
|
||||
+ }
|
||||
+ } else {
|
||||
+ Result = ::mprotect(addr, len, prot);
|
||||
+ if (Result == -1) {
|
||||
+ return -errno;
|
||||
+ }
|
||||
}
|
||||
|
||||
TrackMprotect(Thread, addr, len, prot);
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp
|
||||
index e13cb1ca9..49df38c30 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "LinuxSyscalls/ThreadManager.h"
|
||||
+#include "LinuxSyscalls/GuestHighMemory.h"
|
||||
#include "LinuxSyscalls/SamplingProfiler.h"
|
||||
|
||||
#include "LinuxSyscalls/Syscalls.h"
|
||||
@@ -330,6 +331,7 @@ void ThreadManager::HandleThreadDeletion(FEX::HLE::ThreadStateObject* Thread, bo
|
||||
FEXCore::Allocator::munmap(Thread->ldt_entries, Thread->ldt_entry_count * FEX::HLE::SyscallHandler::LDT_ENTRY_SIZE);
|
||||
}
|
||||
|
||||
+ FEX::HLE::GuestHighMemory::ReleaseThread(Thread);
|
||||
CTX->DestroyThread(Thread->Thread);
|
||||
FEX::HLE::_SyscallHandler->SeccompEmulator.FreeSeccompFilters(Thread);
|
||||
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
|
||||
index 657de6ceb..16ef19209 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
|
||||
@@ -103,6 +103,18 @@ struct ThreadStateObject : public FEXCore::Allocator::FEXAllocOperators {
|
||||
bool Enabled;
|
||||
} SyscallUserDispatch {};
|
||||
|
||||
+ struct {
|
||||
+ uint8_t* Code;
|
||||
+ uint64_t ResumePC;
|
||||
+ uint64_t SavedScratch;
|
||||
+ uint64_t BaseBefore;
|
||||
+ uint64_t Delta;
|
||||
+ uint64_t SavedMask;
|
||||
+ uint32_t Scratch;
|
||||
+ uint32_t Base;
|
||||
+ bool Active;
|
||||
+ } HighMemoryTrap {};
|
||||
+
|
||||
uint64_t JITStackSP {};
|
||||
|
||||
struct TrapSignalFrame {
|
||||
--
|
||||
2.43.0
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
From 0510284085165d39ef0eabb83e7604fe1bb74c76 Mon Sep 17 00:00:00 2001
|
||||
From: maxjivi05 <maxjividen@gmail.com>
|
||||
Date: Thu, 3 Dec 2026 03:26:13 -0500
|
||||
Subject: [PATCH] Show the guest command line when the argument area cannot be
|
||||
remapped
|
||||
|
||||
Kernels built without checkpoint/restore support refuse PR_SET_MM_MAP,
|
||||
so /proc/pid/cmdline kept showing the FEX invocation instead of the
|
||||
guest program and the argv rewrites it makes. For 64-bit guests whose
|
||||
arguments fit, the guest argument strings now live in the original
|
||||
argument area and argv points there, which needs no privilege.
|
||||
---
|
||||
Source/Tools/FEXInterpreter/ELFCodeLoader.h | 23 ++++++++++++++++++++-
|
||||
1 file changed, 22 insertions(+), 1 deletion(-)
|
||||
|
||||
diff --git a/Source/Tools/FEXInterpreter/ELFCodeLoader.h b/Source/Tools/FEXInterpreter/ELFCodeLoader.h
|
||||
index cebf1f5..f21d755 100644
|
||||
--- a/Source/Tools/FEXInterpreter/ELFCodeLoader.h
|
||||
+++ b/Source/Tools/FEXInterpreter/ELFCodeLoader.h
|
||||
@@ -855,13 +855,34 @@ public:
|
||||
|
||||
int r = prctl(PR_SET_MM, PR_SET_MM_MAP, &map, sizeof(map), 0L);
|
||||
if (r != 0) {
|
||||
- LogMan::Msg::EFmt("Failed to remap /proc/pid/cmdline data (prctl failed: result {}, errno {})", r, errno);
|
||||
+ int Error = errno;
|
||||
+ if (Is64BitMode() && MoveArgumentDataToHostArea(NewArgStart, ArgSize)) {
|
||||
+ return;
|
||||
+ }
|
||||
+ LogMan::Msg::EFmt("Failed to remap /proc/pid/cmdline data (prctl failed: result {}, errno {})", r, Error);
|
||||
}
|
||||
} else {
|
||||
LogMan::Msg::EFmt("Failed to remap /proc/pid/cmdline data (GetCurrentMap failed)");
|
||||
}
|
||||
}
|
||||
|
||||
+ bool MoveArgumentDataToHostArea(uintptr_t GuestArgStart, uint64_t ArgSize) {
|
||||
+ struct prctl_mm_map map {};
|
||||
+ if (!GetCurrentMap(map) || map.arg_end <= map.arg_start || ArgSize > map.arg_end - map.arg_start) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ auto HostArgs = reinterpret_cast<uint8_t*>(map.arg_start);
|
||||
+ memmove(HostArgs, reinterpret_cast<const void*>(GuestArgStart), ArgSize);
|
||||
+ memset(HostArgs + ArgSize, 0, map.arg_end - map.arg_start - ArgSize);
|
||||
+
|
||||
+ auto ArgumentPointers = reinterpret_cast<uint64_t*>(StackPointer + sizeof(uint64_t));
|
||||
+ for (size_t i = 0; i < ApplicationArgs.size(); ++i) {
|
||||
+ ArgumentPointers[i] = ArgumentPointers[i] - GuestArgStart + map.arg_start;
|
||||
+ }
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
// Setups the stack initial data (argv, envp, auxv)
|
||||
void SetupStack() {
|
||||
StackPointer += StackSize();
|
||||
--
|
||||
2.43.0
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
From 394866b0f36e85147de39d37e3bda15f29e1332a Mon Sep 17 00:00:00 2001
|
||||
From: maxjivi05 <maxjividen@gmail.com>
|
||||
Date: Thu, 3 Dec 2026 03:38:07 -0500
|
||||
Subject: [PATCH] Diagnostic: mounts without hidepid
|
||||
|
||||
---
|
||||
.../EmulatedFiles/EmulatedFiles.cpp | 57 +++++++++++++++++++
|
||||
1 file changed, 57 insertions(+)
|
||||
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
index 49adaaa..3ec43e1 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp
|
||||
@@ -608,6 +608,63 @@ EmulatedFDManager::EmulatedFDManager(FEXCore::Context::Context* ctx)
|
||||
FDReadCreators[procStatus] = &EmulatedFDManager::ProcStatus;
|
||||
FDReadCreators["/proc/self/status"] = &EmulatedFDManager::ProcStatus;
|
||||
|
||||
+ if (getenv("DD_DIAG_MOUNTS")) {
|
||||
+ auto Mounts = [](FEXCore::Context::Context* ctx, int32_t fd, const char* pathname, int32_t flags, mode_t mode) -> int32_t {
|
||||
+ int Host = ::open("/proc/self/mounts", O_RDONLY | O_CLOEXEC);
|
||||
+ if (Host < 0) {
|
||||
+ return -1;
|
||||
+ }
|
||||
+ fextl::string Data;
|
||||
+ char Buf[4096];
|
||||
+ ssize_t n;
|
||||
+ while ((n = ::read(Host, Buf, sizeof(Buf))) > 0) {
|
||||
+ Data.append(Buf, n);
|
||||
+ }
|
||||
+ ::close(Host);
|
||||
+ for (const char* Opt : {",gid=3009,hidepid=invisible", ",hidepid=invisible", ",hidepid=2"}) {
|
||||
+ size_t Pos;
|
||||
+ while ((Pos = Data.find(Opt)) != fextl::string::npos) {
|
||||
+ Data.erase(Pos, strlen(Opt));
|
||||
+ }
|
||||
+ }
|
||||
+ int FD = GenTmpFD(pathname, flags);
|
||||
+ write(FD, Data.data(), Data.size());
|
||||
+ lseek(FD, 0, SEEK_SET);
|
||||
+ SealTmpFD(FD);
|
||||
+ return FD;
|
||||
+ };
|
||||
+ FDReadCreators["/proc/mounts"] = Mounts;
|
||||
+ FDReadCreators["/proc/self/mounts"] = Mounts;
|
||||
+ FDReadCreators[fextl::fmt::format("/proc/{}/mounts", getpid())] = Mounts;
|
||||
+ auto Maps = [](FEXCore::Context::Context* ctx, int32_t fd, const char* pathname, int32_t flags, mode_t mode) -> int32_t {
|
||||
+ int Host = ::open("/proc/self/maps", O_RDONLY | O_CLOEXEC);
|
||||
+ if (Host < 0) {
|
||||
+ return -1;
|
||||
+ }
|
||||
+ fextl::string Data;
|
||||
+ char Buf[4096];
|
||||
+ ssize_t n;
|
||||
+ while ((n = ::read(Host, Buf, sizeof(Buf))) > 0) {
|
||||
+ Data.append(Buf, n);
|
||||
+ }
|
||||
+ ::close(Host);
|
||||
+ const char* Prefix = getenv("DD_DIAG_MAPS_PREFIX");
|
||||
+ if (Prefix && *Prefix) {
|
||||
+ size_t Pos;
|
||||
+ while ((Pos = Data.find(Prefix)) != fextl::string::npos) {
|
||||
+ Data.erase(Pos, strlen(Prefix));
|
||||
+ }
|
||||
+ }
|
||||
+ int FD = GenTmpFD(pathname, flags);
|
||||
+ write(FD, Data.data(), Data.size());
|
||||
+ lseek(FD, 0, SEEK_SET);
|
||||
+ SealTmpFD(FD);
|
||||
+ return FD;
|
||||
+ };
|
||||
+ FDReadCreators["/proc/self/maps"] = Maps;
|
||||
+ FDReadCreators[fextl::fmt::format("/proc/{}/maps", getpid())] = Maps;
|
||||
+ }
|
||||
+
|
||||
if (ThreadsConfig > 1) {
|
||||
cpus_online = fextl::fmt::format("0-{}", ThreadsConfig - 1);
|
||||
} else {
|
||||
--
|
||||
2.43.0
|
||||
|
||||
@@ -0,0 +1,394 @@
|
||||
diff --git a/Source/Tools/FEXInterpreter/FEXInterpreter.cpp b/Source/Tools/FEXInterpreter/FEXInterpreter.cpp
|
||||
index 79f47aa..3358491 100644
|
||||
--- a/Source/Tools/FEXInterpreter/FEXInterpreter.cpp
|
||||
+++ b/Source/Tools/FEXInterpreter/FEXInterpreter.cpp
|
||||
@@ -6,6 +6,7 @@ desc: Glues the ELF loader, FEXCore and LinuxSyscalls to launch an elf under fex
|
||||
$end_info$
|
||||
*/
|
||||
|
||||
+#include <cstdio>
|
||||
#include "Common/ArgumentLoader.h"
|
||||
#include "Common/FEXServerClient.h"
|
||||
#include "Common/Config.h"
|
||||
@@ -384,6 +385,17 @@ static int StealFEXFDFromEnv(const char* Env) {
|
||||
}
|
||||
|
||||
int main(int argc, char** argv, char** const envp) {
|
||||
+ {
|
||||
+ long N = 0, Bad = -1;
|
||||
+ for (; envp[N]; ++N) {
|
||||
+ if (Bad < 0) {
|
||||
+ for (const unsigned char* C = reinterpret_cast<const unsigned char*>(envp[N]); *C; ++C) {
|
||||
+ if (*C < 0x20 || *C > 0x7e) { Bad = N; break; }
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ ::dprintf(2, "DDENV %d main n=%ld bad=%ld envp=%p sp~%p\n", ::getpid(), N, Bad, (void*)envp, (void*)&N);
|
||||
+ }
|
||||
auto SBRKPointer = FEX::SBRKAllocations::DisableSBRKAllocations();
|
||||
FEXCore::Allocator::GLIBCScopedFault GLIBFaultScope;
|
||||
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
|
||||
index 62acdb0..53a7b44 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.cpp
|
||||
@@ -27,6 +27,7 @@
|
||||
|
||||
namespace FEX::HLE::PtraceEmu {
|
||||
namespace {
|
||||
+#define DDPT(fmt, ...) ::dprintf(2, "DDPT %d/%ld " fmt "\n", ::getpid(), (long)::syscall(SYS_gettid), ##__VA_ARGS__)
|
||||
#ifndef NT_X86_XSTATE
|
||||
constexpr uintptr_t NT_X86_XSTATE = 0x202;
|
||||
#endif
|
||||
@@ -180,6 +181,7 @@ namespace {
|
||||
}
|
||||
|
||||
void Stop(uint64_t Nr, const uint64_t Args[6], bool Entry, uint64_t Ret, const uint64_t* Regs) {
|
||||
+ DDPT("stop nr=%lld entry=%d rip=%#llx", (long long)Nr, Entry, (unsigned long long)Regs[Rip]);
|
||||
SharedState& S = *Current;
|
||||
if (!HasHostTracer()) {
|
||||
S.SyscallStops = 0;
|
||||
@@ -212,6 +214,7 @@ namespace {
|
||||
}
|
||||
|
||||
const uint64_t Dirty = S.DirtyMask;
|
||||
+ if (Dirty) DDPT("apply dirty=%#llx entry=%d rip=%#llx rax=%#llx orax=%#llx rdi=%#llx rsi=%#llx rdx=%#llx r10=%#llx rsp=%#llx", (unsigned long long)Dirty, Entry, (unsigned long long)S.Regs[Rip], (unsigned long long)S.Regs[10], (unsigned long long)S.Regs[15], (unsigned long long)S.Regs[14], (unsigned long long)S.Regs[13], (unsigned long long)S.Regs[12], (unsigned long long)S.Regs[7], (unsigned long long)S.Regs[19]);
|
||||
if (!Dirty) {
|
||||
return NoOverride;
|
||||
}
|
||||
@@ -574,13 +577,17 @@ void ApplyPendingFlush(FEXCore::Core::CpuStateFrame* Frame) {
|
||||
}
|
||||
}
|
||||
|
||||
+bool DDTrace = false;
|
||||
uint64_t TakePendingRip() {
|
||||
const uint64_t Rip = PendingRip;
|
||||
PendingRip = 0;
|
||||
+ if (Rip) DDTrace = true;
|
||||
+ if (Rip) DDPT("take rip=%#llx", (unsigned long long)Rip);
|
||||
return Rip;
|
||||
}
|
||||
|
||||
bool TryGuestSignalStop(FEXCore::Core::CpuStateFrame* Frame, int Signal) {
|
||||
+ if (Current->Attached) DDPT("sigstop sig=%d rip=%#llx rax=%#llx", Signal, (unsigned long long)Frame->State.rip, (unsigned long long)Frame->State.gregs[FEXCore::X86State::REG_RAX]);
|
||||
if (Signal != SIGTRAP && Signal != SIGSEGV) {
|
||||
return false;
|
||||
}
|
||||
@@ -631,6 +638,7 @@ bool FixupWaitStatus(pid_t Pid, int* Status) {
|
||||
return false;
|
||||
}
|
||||
const int St = *Status;
|
||||
+ DDPT("wait pid=%d st=%#x", Pid, St);
|
||||
|
||||
if (WIFEXITED(St) || WIFSIGNALED(St)) {
|
||||
Forget(Pid, true);
|
||||
@@ -677,6 +685,8 @@ bool FixupWaitStatus(pid_t Pid, int* Status) {
|
||||
}
|
||||
|
||||
bool HandleRequest(int Request, pid_t Pid, void* Addr, void* Data, uint64_t* Result) {
|
||||
+ DDPT("req %d pid=%d addr=%p data=%p", Request, Pid, Addr, Data);
|
||||
+ if (Request == 13 && Data) DDPT("setregs rip=%#llx rcx=%#llx", (unsigned long long)static_cast<const uint64_t*>(Data)[Rip], (unsigned long long)static_cast<const uint64_t*>(Data)[11]);
|
||||
switch (Request) {
|
||||
case PTRACE_TRACEME:
|
||||
*Result = HostPtrace(Request, Pid, Addr, Data);
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.h
|
||||
index 5919497..34685be 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.h
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/PtraceEmu.h
|
||||
@@ -45,6 +45,7 @@ void OnForkedChild(bool SharesAddressSpace);
|
||||
|
||||
void ApplyPendingFlush(FEXCore::Core::CpuStateFrame* Frame);
|
||||
|
||||
+extern bool DDTrace;
|
||||
uint64_t TakePendingRip();
|
||||
|
||||
bool TryGuestSignalStop(FEXCore::Core::CpuStateFrame* Frame, int Signal);
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/Seccomp/SeccompEmulator.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/Seccomp/SeccompEmulator.cpp
|
||||
index d2626fd..1dc85de 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/Seccomp/SeccompEmulator.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/Seccomp/SeccompEmulator.cpp
|
||||
@@ -5,6 +5,8 @@ tags: LinuxSyscalls|syscalls-shared
|
||||
$end_info$
|
||||
*/
|
||||
|
||||
+#include <stdio.h>
|
||||
+#include <unistd.h>
|
||||
#include "LinuxSyscalls/Seccomp/BPFEmitter.h"
|
||||
#include "LinuxSyscalls/Seccomp/SeccompEmulator.h"
|
||||
|
||||
@@ -132,6 +134,7 @@ $end_info$
|
||||
|
||||
namespace FEX::HLE {
|
||||
uint64_t SeccompEmulator::Handle(FEXCore::Core::CpuStateFrame* Frame, uint32_t Op, uint32_t flags, void* arg) {
|
||||
+ ::dprintf(2, "DDPT %d seccomp op=%u flags=%#x\n", ::getpid(), Op, flags);
|
||||
// If seccomp isn't enabled then say so.
|
||||
if (!NeedsSeccomp) {
|
||||
return -EINVAL;
|
||||
@@ -474,6 +477,7 @@ SeccompEmulator::ExecuteFilter(FEXCore::Core::CpuStateFrame* Frame, uint64_t JIT
|
||||
Filename(), Signal, Arch, Args->Argument[0], RIP, SeccompResult);
|
||||
}
|
||||
|
||||
+ if (ActionMasked != SECCOMP_RET_ALLOW) ::dprintf(2, "DDPT %d seccomp action=%#x nr=%llu rip=%#llx\n", ::getpid(), (unsigned)SeccompResult, (unsigned long long)Args->Argument[0], (unsigned long long)RIP);
|
||||
switch (ActionMasked) {
|
||||
// Unknown actions behave like RET_KILL_PROCESS.
|
||||
default:
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
|
||||
index e8723e8..a3cc742 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
|
||||
@@ -1,3 +1,6 @@
|
||||
+#include <stdio.h>
|
||||
+#include <sys/syscall.h>
|
||||
+#include <unistd.h>
|
||||
// SPDX-License-Identifier: MIT
|
||||
/*
|
||||
$info$
|
||||
@@ -920,6 +923,8 @@ void SignalDelegator::HandleGuestSignal(FEX::HLE::ThreadStateObject* ThreadObjec
|
||||
ERROR_AND_DIE_FMT("Unhandled guest exception");
|
||||
}
|
||||
|
||||
+ ::dprintf(2, "DDPT %d/%d fatal sig=%d code=%d addr=%p rip=%#llx guestdfl=%d\n", ::getpid(), (int)::syscall(SYS_gettid), Signal, SigInfo.si_code, SigInfo.si_addr,
|
||||
+ (unsigned long long)ThreadObject->Thread->CurrentFrame->State.rip, Handler.GuestAction.sigaction_handler.handler == SIG_DFL);
|
||||
// Unhandled crash
|
||||
// Call back in to the previous handler
|
||||
if (Handler.OldAction.sa_flags & SA_SIGINFO) {
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp
|
||||
index 078faed..c9a9ec8 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp
|
||||
@@ -62,6 +62,9 @@ $end_info$
|
||||
#include <system_error>
|
||||
#include <syscall.h>
|
||||
#include <sys/mman.h>
|
||||
+#include <dirent.h>
|
||||
+#include <fcntl.h>
|
||||
+#include <string.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
@@ -199,7 +202,91 @@ static fextl::string GetShebangInterpFilename(const fextl::string& Filename) {
|
||||
return Interp;
|
||||
}
|
||||
|
||||
+static uint64_t ExecveHandlerImpl(FEXCore::Core::CpuStateFrame* Frame, const char* pathname, char* const* argv, char* const* envp, ExecveAtArgs Args);
|
||||
+
|
||||
+static void DDMapLine(const char* Tag, uintptr_t A) {
|
||||
+ static char Buf[1 << 20];
|
||||
+ int M = ::open("/proc/self/maps", O_RDONLY | O_CLOEXEC);
|
||||
+ if (M < 0) return;
|
||||
+ ssize_t R = 0, K;
|
||||
+ while ((K = ::read(M, Buf + R, sizeof(Buf) - 1 - R)) > 0) R += K;
|
||||
+ ::close(M);
|
||||
+ if (R <= 0) return;
|
||||
+ Buf[R] = 0;
|
||||
+ char* Prev = nullptr;
|
||||
+ for (char* L = Buf; L && *L;) {
|
||||
+ char* N = strchr(L, '\n');
|
||||
+ if (N) *N = 0;
|
||||
+ unsigned long Lo, Hi;
|
||||
+ if (sscanf(L, "%lx-%lx", &Lo, &Hi) == 2 && A >= Lo && A < Hi) {
|
||||
+ ::dprintf(2, "DDMAP %d %s %lx in [%s] prev=[%s]\n", ::getpid(), Tag, (unsigned long)A, L, Prev ? Prev : "");
|
||||
+ return;
|
||||
+ }
|
||||
+ Prev = L;
|
||||
+ L = N ? N + 1 : nullptr;
|
||||
+ }
|
||||
+ ::dprintf(2, "DDMAP %d %s %lx unmapped\n", ::getpid(), Tag, (unsigned long)A);
|
||||
+}
|
||||
+
|
||||
+static char* const* DDProbeEnv(char* const* E) {
|
||||
+ int Dummy = 0;
|
||||
+ uintptr_t Lo = ~0UL, Hi = 0;
|
||||
+ long N = 0;
|
||||
+ for (; E && E[N]; ++N) {
|
||||
+ uintptr_t A = reinterpret_cast<uintptr_t>(E[N]);
|
||||
+ if (A < Lo) Lo = A;
|
||||
+ if (A > Hi) Hi = A;
|
||||
+ }
|
||||
+ int Tasks = 0;
|
||||
+ if (DIR* D = ::opendir("/proc/self/task")) {
|
||||
+ while (struct dirent* Ent = ::readdir(D)) if (Ent->d_name[0] != '.') ++Tasks;
|
||||
+ ::closedir(D);
|
||||
+ }
|
||||
+ ::dprintf(2, "DDPROBE %d n=%ld lo=%lx hi=%lx arr=%p sp=%p tasks=%d\n", ::getpid(), N, (unsigned long)Lo, (unsigned long)Hi, (void*)E, (void*)&Dummy, Tasks);
|
||||
+ DDMapLine("arr", reinterpret_cast<uintptr_t>(E));
|
||||
+ DDMapLine("strlo", Lo);
|
||||
+ DDMapLine("strhi", Hi);
|
||||
+ DDMapLine("sp", reinterpret_cast<uintptr_t>(&Dummy));
|
||||
+ const char* Copy = getenv("DD_ENVCOPY");
|
||||
+ if (!E || !Copy || Copy[0] != '1') return E;
|
||||
+ size_t Bytes = (N + 1) * sizeof(char*);
|
||||
+ for (long i = 0; i < N; ++i) Bytes += strlen(E[i]) + 1;
|
||||
+ void* Mem = ::mmap(nullptr, Bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
+ if (Mem == MAP_FAILED) return E;
|
||||
+ char** Out = static_cast<char**>(Mem);
|
||||
+ char* W = reinterpret_cast<char*>(Out + N + 1);
|
||||
+ for (long i = 0; i < N; ++i) {
|
||||
+ size_t L = strlen(E[i]) + 1;
|
||||
+ memcpy(W, E[i], L);
|
||||
+ Out[i] = W;
|
||||
+ W += L;
|
||||
+ }
|
||||
+ Out[N] = nullptr;
|
||||
+ ::dprintf(2, "DDPROBE %d copied to %p\n", ::getpid(), Mem);
|
||||
+ return Out;
|
||||
+}
|
||||
+
|
||||
+static void DDCheckEnv(const char* Tag, char* const* E) {
|
||||
+ if (!E) { ::dprintf(2, "DDENV %d %s null\n", ::getpid(), Tag); return; }
|
||||
+ long N = 0, Bad = -1;
|
||||
+ for (; E[N]; ++N) {
|
||||
+ if (Bad < 0) {
|
||||
+ for (const unsigned char* C = reinterpret_cast<const unsigned char*>(E[N]); *C; ++C) {
|
||||
+ if (*C < 0x20 || *C > 0x7e) { Bad = N; break; }
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ ::dprintf(2, "DDENV %d %s n=%ld bad=%ld envp=%p e0=%p\n", ::getpid(), Tag, N, Bad, (void*)E, N ? (void*)E[0] : nullptr);
|
||||
+}
|
||||
uint64_t ExecveHandler(FEXCore::Core::CpuStateFrame* Frame, const char* pathname, char* const* argv, char* const* envp, ExecveAtArgs Args) {
|
||||
+ ::dprintf(2, "DDPT %d execve path=[%s] dirfd=%d flags=%#x a0=[%s] a1=[%s] a2=[%s] a3=[%s]\n", ::getpid(), pathname, Args.dirfd, Args.flags,
|
||||
+ argv && argv[0] ? argv[0] : "", argv && argv[0] && argv[1] ? argv[1] : "", argv && argv[0] && argv[1] && argv[2] ? argv[2] : "", argv && argv[0] && argv[1] && argv[2] && argv[3] ? argv[3] : "");
|
||||
+ DDCheckEnv("guest", envp);
|
||||
+ uint64_t R = ExecveHandlerImpl(Frame, pathname, argv, envp, Args);
|
||||
+ ::dprintf(2, "DDPT %d execve result=%ld\n", ::getpid(), (long)R);
|
||||
+ return R;
|
||||
+}
|
||||
+static uint64_t ExecveHandlerImpl(FEXCore::Core::CpuStateFrame* Frame, const char* pathname, char* const* argv, char* const* envp, ExecveAtArgs Args) {
|
||||
auto SyscallHandler = FEX::HLE::_SyscallHandler;
|
||||
Frame->Thread->CTX->FlushAndCloseCodeMap();
|
||||
|
||||
@@ -417,6 +504,7 @@ uint64_t ExecveHandler(FEXCore::Core::CpuStateFrame* Frame, const char* pathname
|
||||
}
|
||||
|
||||
if (IsBinfmtCompatible || IsOtherELF || IsForeignShebang) {
|
||||
+ DDCheckEnv("host-direct", EnvpPtr);
|
||||
Result = ::syscall(SYS_execveat, Args.dirfd, Filename.c_str(), argv, EnvpPtr, Args.flags);
|
||||
CloseSeccompFD();
|
||||
CloseFDExecFD();
|
||||
@@ -465,7 +553,27 @@ uint64_t ExecveHandler(FEXCore::Core::CpuStateFrame* Frame, const char* pathname
|
||||
ExecveArgs.emplace_back(nullptr);
|
||||
}
|
||||
|
||||
- Result = ::syscall(SYS_execveat, Args.dirfd, "/proc/self/exe", const_cast<char* const*>(ExecveArgs.data()), EnvpPtr, Args.flags);
|
||||
+ DDCheckEnv("host-loader", EnvpPtr);
|
||||
+ EnvpPtr = DDProbeEnv(EnvpPtr);
|
||||
+ const char* DDMode = getenv("DD_EXECMODE");
|
||||
+ if (DDMode && DDMode[0] == '3') {
|
||||
+ long R3 = ::syscall(SYS_execveat, AT_FDCWD, "/nonexistent-dd-probe", const_cast<char* const*>(ExecveArgs.data()), EnvpPtr, 0);
|
||||
+ ::dprintf(2, "DDPROBE %d failing exec %ld errno %d\n", ::getpid(), R3, errno);
|
||||
+ DDCheckEnv("after-fail", EnvpPtr);
|
||||
+ }
|
||||
+ if (DDMode && DDMode[0] == '2') {
|
||||
+ register long X0 asm("x0") = Args.dirfd;
|
||||
+ register long X1 asm("x1") = reinterpret_cast<long>("/proc/self/exe");
|
||||
+ register long X2 asm("x2") = reinterpret_cast<long>(ExecveArgs.data());
|
||||
+ register long X3 asm("x3") = reinterpret_cast<long>(EnvpPtr);
|
||||
+ register long X4 asm("x4") = Args.flags;
|
||||
+ register long X8 asm("x8") = SYS_execveat;
|
||||
+ asm volatile("svc #0" : "+r"(X0) : "r"(X1), "r"(X2), "r"(X3), "r"(X4), "r"(X8) : "memory");
|
||||
+ Result = X0;
|
||||
+ } else {
|
||||
+ Result = ::syscall(SYS_execveat, Args.dirfd, "/proc/self/exe", const_cast<char* const*>(ExecveArgs.data()), EnvpPtr, Args.flags);
|
||||
+ }
|
||||
+ ::dprintf(2, "DDPROBE %d execveat returned %ld errno %d\n", ::getpid(), (long)Result, errno);
|
||||
CloseSeccompFD();
|
||||
CloseFDExecFD();
|
||||
|
||||
@@ -903,6 +1011,10 @@ void SyscallHandler::HandleSyscallImpl(FEXCore::Core::CpuStateFrame* Frame, uint
|
||||
if constexpr (Is64Bit) {
|
||||
FEX::HLE::GuestHighMemory::TranslateSyscallArguments(SyscallNum, &A[1], 6);
|
||||
}
|
||||
+ if (PtraceEmu::DDTrace) {
|
||||
+ ::dprintf(2, "DDSC %d/%ld nr=%llu a1=%#llx a2=%#llx a3=%#llx\n", ::getpid(), ::syscall(SYS_gettid), (unsigned long long)SyscallNum, (unsigned long long)A[1],
|
||||
+ (unsigned long long)A[2], (unsigned long long)A[3]);
|
||||
+ }
|
||||
uint64_t Result {};
|
||||
switch (Def.NumArgs) {
|
||||
case 0: Result = std::invoke(Def.Ptr0, Frame); break;
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Passthrough.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Passthrough.cpp
|
||||
index 602e175..de49917 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Passthrough.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Passthrough.cpp
|
||||
@@ -13,6 +13,7 @@ $end_info$
|
||||
#include "LinuxSyscalls/SignalTrace.h"
|
||||
|
||||
#include <errno.h>
|
||||
+#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
@@ -303,6 +304,7 @@ static void RegisterCommon(FEX::HLE::SyscallHandler* Handler) {
|
||||
if (SigTrace::Enabled()) {
|
||||
SigTrace::Log(::syscall(SYS_gettid), SigTrace::Kill, sig, pid, 0);
|
||||
}
|
||||
+ ::dprintf(2, "DDPT %d/%ld kill pid=%d sig=%d\n", ::getpid(), ::syscall(SYS_gettid), pid, sig);
|
||||
uint64_t Result = ::syscall(SYSCALL_DEF(kill), pid, sig);
|
||||
SYSCALL_ERRNO();
|
||||
});
|
||||
@@ -372,6 +374,7 @@ static void RegisterCommon(FEX::HLE::SyscallHandler* Handler) {
|
||||
if (SigTrace::Enabled()) {
|
||||
SigTrace::Log(::syscall(SYS_gettid), SigTrace::Kill, sig, tid, 0);
|
||||
}
|
||||
+ ::dprintf(2, "DDPT %d/%ld tkill tid=%d sig=%d\n", ::getpid(), ::syscall(SYS_gettid), tid, sig);
|
||||
uint64_t Result = ::syscall(SYSCALL_DEF(tkill), tid, sig);
|
||||
SYSCALL_ERRNO();
|
||||
});
|
||||
@@ -388,6 +391,7 @@ static void RegisterCommon(FEX::HLE::SyscallHandler* Handler) {
|
||||
if (SigTrace::Enabled()) {
|
||||
SigTrace::Log(::syscall(SYS_gettid), SigTrace::Kill, sig, tid, tgid);
|
||||
}
|
||||
+ if (tgid != ::getpid() || (sig != 10 && sig != 12 && sig < 32)) ::dprintf(2, "DDPT %d/%ld tgkill tgid=%d tid=%d sig=%d\n", ::getpid(), ::syscall(SYS_gettid), tgid, tid, sig);
|
||||
uint64_t Result = ::syscall(SYSCALL_DEF(tgkill), tgid, tid, sig);
|
||||
SYSCALL_ERRNO();
|
||||
});
|
||||
@@ -582,7 +586,11 @@ namespace x64 {
|
||||
REGISTER_SYSCALL_IMPL_X64(preadv2, SyscallPassthrough6<SYSCALL_DEF(preadv2)>);
|
||||
REGISTER_SYSCALL_IMPL_X64(pwritev2, SyscallPassthrough6<SYSCALL_DEF(pwritev2)>);
|
||||
REGISTER_SYSCALL_IMPL_X64(io_pgetevents, SyscallPassthrough6<SYSCALL_DEF(io_pgetevents)>);
|
||||
- REGISTER_SYSCALL_IMPL_X64(pidfd_send_signal, SyscallPassthrough4<SYSCALL_DEF(pidfd_send_signal)>);
|
||||
+ REGISTER_SYSCALL_IMPL_X64(pidfd_send_signal, [](FEXCore::Core::CpuStateFrame* Frame, int pidfd, int sig, void* info, unsigned int flags) -> uint64_t {
|
||||
+ ::dprintf(2, "DDPT %d/%ld pidfd_send_signal fd=%d sig=%d\n", ::getpid(), ::syscall(SYS_gettid), pidfd, sig);
|
||||
+ uint64_t Result = ::syscall(SYSCALL_DEF(pidfd_send_signal), pidfd, sig, info, flags);
|
||||
+ SYSCALL_ERRNO();
|
||||
+ });
|
||||
REGISTER_SYSCALL_IMPL_X64(process_madvise, SyscallPassthrough5<SYSCALL_DEF(process_madvise)>);
|
||||
REGISTER_SYSCALL_IMPL_X64(fadvise64, SyscallPassthrough4<SYSCALL_DEF(fadvise64)>);
|
||||
if (Handler->IsHostKernelVersionAtLeast(6, 5, 0)) {
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Thread.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Thread.cpp
|
||||
index de6daa9..34d110d 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Thread.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Thread.cpp
|
||||
@@ -5,6 +5,8 @@ tags: LinuxSyscalls|syscalls-shared
|
||||
$end_info$
|
||||
*/
|
||||
|
||||
+#include <stdio.h>
|
||||
+#include <unistd.h>
|
||||
#include "CodeLoader.h"
|
||||
|
||||
#include "LinuxSyscalls/SignalDelegator.h"
|
||||
@@ -456,6 +458,7 @@ void RegisterThread(FEX::HLE::SyscallHandler* Handler) {
|
||||
}));
|
||||
|
||||
REGISTER_SYSCALL_IMPL(exit, [](FEXCore::Core::CpuStateFrame* Frame, int status) -> uint64_t {
|
||||
+ ::dprintf(2, "DDPT %d/%ld exit status=%d\n", ::getpid(), ::syscall(SYS_gettid), status);
|
||||
// TLS/DTV teardown is something FEX can't control. Disable glibc checking when we leave a pthread.
|
||||
// Since this thread is hard stopping, we can't track the TLS/DTV teardown in FEX's thread handling.
|
||||
FEXCore::Allocator::YesIKnowImNotSupposedToUseTheGlibcAllocator::HardDisable();
|
||||
@@ -487,6 +490,7 @@ void RegisterThread(FEX::HLE::SyscallHandler* Handler) {
|
||||
[](FEXCore::Core::CpuStateFrame* Frame, int option, unsigned long arg2, unsigned long arg3, unsigned long arg4,
|
||||
unsigned long arg5) -> uint64_t {
|
||||
uint64_t Result {};
|
||||
+ ::dprintf(2, "DDPT %d/%ld prctl op=%d a2=%#lx a3=%#lx\n", ::getpid(), ::syscall(SYS_gettid), option, arg2, arg3);
|
||||
#ifndef PR_GET_AUXV
|
||||
#define PR_GET_AUXV 0x41555856
|
||||
#endif
|
||||
@@ -599,6 +603,7 @@ void RegisterThread(FEX::HLE::SyscallHandler* Handler) {
|
||||
});
|
||||
|
||||
REGISTER_SYSCALL_IMPL(exit_group, [](FEXCore::Core::CpuStateFrame* Frame, int status) -> uint64_t {
|
||||
+ ::dprintf(2, "DDPT %d exit_group status=%d rip=%#llx\n", ::getpid(), status, (unsigned long long)Frame->State.rip);
|
||||
Frame->Thread->CTX->FlushAndCloseCodeMap();
|
||||
|
||||
// Save telemetry if we're exiting.
|
||||
@@ -0,0 +1,54 @@
|
||||
From 18e2978254eaed75e7742859f501632313da968f Mon Sep 17 00:00:00 2001
|
||||
From: maxjivi05 <maxjividen@gmail.com>
|
||||
Date: Thu, 3 Dec 2026 06:51:24 -0500
|
||||
Subject: [PATCH] Keep the guest stack clear of the host stack growth reserve
|
||||
|
||||
---
|
||||
Source/Tools/FEXInterpreter/ELFCodeLoader.h | 19 +++++++++++++++++++
|
||||
1 file changed, 19 insertions(+)
|
||||
|
||||
diff --git a/Source/Tools/FEXInterpreter/ELFCodeLoader.h b/Source/Tools/FEXInterpreter/ELFCodeLoader.h
|
||||
index f21d755..6d21573 100644
|
||||
--- a/Source/Tools/FEXInterpreter/ELFCodeLoader.h
|
||||
+++ b/Source/Tools/FEXInterpreter/ELFCodeLoader.h
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <sys/personality.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/random.h>
|
||||
+#include <sys/resource.h>
|
||||
|
||||
#define PAGE_START(x) ((x) & ~(uintptr_t)(4095))
|
||||
#define PAGE_OFFSET(x) ((x) & 4095)
|
||||
@@ -505,7 +506,12 @@ public:
|
||||
auto PageSize = sysconf(_SC_PAGESIZE);
|
||||
PageSize = PageSize > 0 ? PageSize : FEXCore::Utils::FEX_PAGE_SIZE;
|
||||
|
||||
+ const auto [HostStackLow, HostStackHigh] = HostStackReservation(PageSize);
|
||||
+
|
||||
do {
|
||||
+ if (StackHint < HostStackHigh && StackHint + FULL_STACK_SIZE > HostStackLow && HostStackLow > FULL_STACK_SIZE) {
|
||||
+ StackHint = (HostStackLow - FULL_STACK_SIZE) & ~static_cast<uint64_t>(PageSize - 1);
|
||||
+ }
|
||||
// Allocate the base of the full 128MB stack range.
|
||||
StackPointerBase = Handler->GuestMmap(Thread, reinterpret_cast<void*>(StackHint), FULL_STACK_SIZE, PROT_NONE,
|
||||
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK | MAP_GROWSDOWN | MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0);
|
||||
@@ -793,6 +799,19 @@ public:
|
||||
*AuxTabSize = sizeof(AuxType) * AuxVariables.size();
|
||||
}
|
||||
|
||||
+ static std::pair<uint64_t, uint64_t> HostStackReservation(uint64_t PageSize) {
|
||||
+ struct prctl_mm_map map {};
|
||||
+ uint64_t Top = GetCurrentMap(map) && map.env_end ? map.env_end : reinterpret_cast<uint64_t>(__builtin_frame_address(0));
|
||||
+ Top = (Top + PageSize - 1) & ~(PageSize - 1);
|
||||
+
|
||||
+ constexpr uint64_t MinGap = 128ULL * 1024 * 1024;
|
||||
+ const uint64_t MaxGap = std::max(Top / 6 * 5, MinGap);
|
||||
+ struct rlimit Limit {};
|
||||
+ uint64_t Gap = getrlimit(RLIMIT_STACK, &Limit) == 0 ? static_cast<uint64_t>(Limit.rlim_cur) : MinGap;
|
||||
+ Gap = std::clamp(Gap, MinGap, MaxGap) + 256 * PageSize;
|
||||
+ return {Top > Gap ? Top - Gap : 0, Top};
|
||||
+ }
|
||||
+
|
||||
// Get the current memory map from /proc/self/stat
|
||||
static bool GetCurrentMap(struct prctl_mm_map& map) {
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/Seccomp/SeccompEmulator.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/Seccomp/SeccompEmulator.cpp
|
||||
index 1dc85de..dc0b3a1 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/Seccomp/SeccompEmulator.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/Seccomp/SeccompEmulator.cpp
|
||||
@@ -6,6 +6,7 @@ $end_info$
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
+#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
#include "LinuxSyscalls/Seccomp/BPFEmitter.h"
|
||||
#include "LinuxSyscalls/Seccomp/SeccompEmulator.h"
|
||||
@@ -365,7 +366,16 @@ SeccompEmulator::ExecuteSyscallUserDispatch(FEXCore::Core::CpuStateFrame* Frame,
|
||||
|
||||
if (SUD.Selector) {
|
||||
uint8_t State {};
|
||||
- if (FaultSafeUserMemAccess::CopyFromUser(&State, reinterpret_cast<const void*>(SUD.Selector), sizeof(State))) {
|
||||
+ const bool SelFault = FaultSafeUserMemAccess::CopyFromUser(&State, reinterpret_cast<const void*>(SUD.Selector), sizeof(State));
|
||||
+ if (SelFault || State != 0) ::dprintf(2, "DDSUD %d/%ld nr=%lu rip=%#lx off=%#lx len=%#lx sel=%#lx fault=%d state=%u\n", ::getpid(), ::syscall(SYS_gettid), SyscallNumber, PostSyscallRIP, SUD.Offset, SUD.Length, SUD.Selector, SelFault, State);
|
||||
+ if (SelFault || State != 0) {
|
||||
+ uint64_t Stk[48] {};
|
||||
+ const uint64_t Rsp = Frame->State.gregs[FEXCore::X86State::REG_RSP];
|
||||
+ FaultSafeUserMemAccess::CopyFromUser(Stk, reinterpret_cast<const void*>(Rsp), sizeof(Stk));
|
||||
+ ::dprintf(2, "DDSUDCTX %d/%ld mask=%#lx rsp=%#lx rbp=%#lx rdi=%#lx rsi=%#lx\n", ::getpid(), ::syscall(SYS_gettid), static_cast<unsigned long>(Thread->SignalInfo.CurrentSignalMask.Val), Rsp, Frame->State.gregs[FEXCore::X86State::REG_RBP], Frame->State.gregs[FEXCore::X86State::REG_RDI], Frame->State.gregs[FEXCore::X86State::REG_RSI]);
|
||||
+ for (int i = 0; i < 48; i += 6) ::dprintf(2, "DDSTK %d +%#x %#lx %#lx %#lx %#lx %#lx %#lx\n", ::getpid(), i * 8, Stk[i], Stk[i+1], Stk[i+2], Stk[i+3], Stk[i+4], Stk[i+5]);
|
||||
+ }
|
||||
+ if (SelFault) {
|
||||
ForceExitSignal(Thread, SIGSEGV);
|
||||
return {true, SyscallNumber};
|
||||
}
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
|
||||
index a3cc742..8857e4a 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
|
||||
@@ -334,6 +334,7 @@ void SignalDelegator::RestoreThreadState(FEXCore::Core::InternalThreadState* Thr
|
||||
auto* GuestUContext = reinterpret_cast<FEXCore::x86_64::ucontext_t*>(Context->UContextLocation);
|
||||
NewGuestMask = GuestUContext->uc_sigmask.val[0] & ~((1ULL << (SIGKILL - 1)) | (1ULL << (SIGSTOP - 1)));
|
||||
}
|
||||
+ if (NewGuestMask & (1ULL << (SIGSYS - 1))) ::dprintf(2, "DDMSKRET %d/%ld new=%#lx saved=%#lx uctx=%#lx rsp=%#lx rip=%#lx\n", ::getpid(), ::syscall(SYS_gettid), static_cast<unsigned long>(NewGuestMask), static_cast<unsigned long>(Context->GuestSignalMask), static_cast<unsigned long>(Context->UContextLocation), static_cast<unsigned long>(Thread->CurrentFrame->State.gregs[FEXCore::X86State::REG_RSP]), static_cast<unsigned long>(Thread->CurrentFrame->State.rip));
|
||||
ThreadObject->SignalInfo.CurrentSignalMask.Val = NewGuestMask;
|
||||
CheckForPendingSignals(ThreadObject);
|
||||
}
|
||||
@@ -903,6 +904,7 @@ void SignalDelegator::HandleGuestSignal(FEX::HLE::ThreadStateObject* ThreadObjec
|
||||
GuestNew |= 1ULL << (Signal - 1);
|
||||
}
|
||||
GuestNew &= ~((1ULL << (SIGKILL - 1)) | (1ULL << (SIGSTOP - 1)));
|
||||
+ if (Signal != SIGSYS && (GuestNew & (1ULL << (SIGSYS - 1)))) ::dprintf(2, "DDMSKDLV %d/%ld sig=%d new=%#lx samask=%#lx\n", ::getpid(), ::syscall(SYS_gettid), Signal, static_cast<unsigned long>(GuestNew), static_cast<unsigned long>(Handler.GuestAction.sa_mask.Val));
|
||||
ThreadObject->SignalInfo.CurrentSignalMask.Val = GuestNew;
|
||||
NewMask = GuestNew;
|
||||
for (size_t i = 0; i < MAX_SIGNALS; ++i) {
|
||||
@@ -1126,6 +1128,7 @@ void SignalDelegator::PrepareForcedGuestSignal(FEX::HLE::ThreadStateObject* Thre
|
||||
GuestSigAction Action {};
|
||||
RegisterGuestSignalHandler(Signal, nullptr, &Action);
|
||||
const bool Ignored = Action.sigaction_handler.handler == SIG_IGN;
|
||||
+ ::dprintf(2, "DDFORCE %d/%ld sig=%d blocked=%d ignored=%d handler=%p mask=%#lx\n", ::getpid(), ::syscall(SYS_gettid), Signal, Blocked, Ignored, reinterpret_cast<void*>(Action.sigaction_handler.handler), static_cast<unsigned long>(Thread->SignalInfo.CurrentSignalMask.Val));
|
||||
|
||||
if (!Blocked && !Ignored) {
|
||||
return;
|
||||
@@ -1511,6 +1514,7 @@ uint64_t SignalDelegator::GuestSigProcMask(FEX::HLE::ThreadStateObject* Thread,
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
+ if (Thread->SignalInfo.CurrentSignalMask.Val & (1ULL << (SIGSYS - 1))) ::dprintf(2, "DDMSKPROC %d/%ld how=%d set=%#lx new=%#lx\n", ::getpid(), ::syscall(SYS_gettid), how, static_cast<unsigned long>(*set), static_cast<unsigned long>(Thread->SignalInfo.CurrentSignalMask.Val));
|
||||
uint64_t HostMask = Thread->SignalInfo.CurrentSignalMask.Val;
|
||||
// Now actually set the host mask
|
||||
// This will hide from the guest that we are not actually setting all of the masks it wants
|
||||
@@ -664,7 +664,7 @@ def data_dir(env):
|
||||
return DATA_DIR
|
||||
|
||||
|
||||
def guest_environment(fex, env):
|
||||
def guest_environment(fex, env, keep_compat=False):
|
||||
result = dict(env)
|
||||
data = data_dir(env)
|
||||
for directory in (CONFIG_DIR, data, CACHE_DIR):
|
||||
@@ -672,7 +672,7 @@ def guest_environment(fex, env):
|
||||
result["FEX_APP_CONFIG_LOCATION"] = CONFIG_DIR + "/"
|
||||
result["FEX_APP_DATA_LOCATION"] = data + "/"
|
||||
result["FEX_APP_CACHE_LOCATION"] = CACHE_DIR + "/"
|
||||
if result.get("STEAM_COMPAT_DATA_PATH"):
|
||||
if result.get("STEAM_COMPAT_DATA_PATH") and not keep_compat:
|
||||
short = data if data != DATA_DIR else "/tmp/fex"
|
||||
os.makedirs(short, exist_ok=True)
|
||||
result["STEAM_COMPAT_DATA_PATH"] = short
|
||||
@@ -750,9 +750,12 @@ def vulkan_thunk_loads(fex, folder):
|
||||
|
||||
|
||||
def required_runtime(program):
|
||||
text = read_text(os.path.join(os.path.dirname(program), "toolmanifest.vdf")) or ""
|
||||
match = re.search(r'"require_tool_appid"\s+"(\d+)"', text)
|
||||
return match.group(1) if match and match.group(1) in RUNTIMES else SNIPER_APPID
|
||||
folder = os.path.dirname(program)
|
||||
for name in ("toolmanifest.vdf.droiddeck-orig", "toolmanifest.vdf"):
|
||||
match = re.search(r'"require_tool_appid"\s+"(\d+)"', read_text(os.path.join(folder, name)) or "")
|
||||
if match and match.group(1) in RUNTIMES:
|
||||
return match.group(1)
|
||||
return SNIPER_APPID
|
||||
|
||||
|
||||
def resolve(program, env):
|
||||
@@ -761,7 +764,7 @@ def resolve(program, env):
|
||||
return shutil.which(program, path=env.get("PATH")) or program
|
||||
|
||||
|
||||
def launch(command, env, mode, decide=None):
|
||||
def launch(command, env, mode, decide=None, keep_compat=False):
|
||||
program = resolve(command[0], env)
|
||||
use = mode == "on" or (mode == "auto" and needs_fex(decide or program))
|
||||
if not use:
|
||||
@@ -769,7 +772,7 @@ def launch(command, env, mode, decide=None):
|
||||
fex = prepare(required_runtime(program))
|
||||
if fex is None:
|
||||
return 126
|
||||
guest = guest_environment(fex, env)
|
||||
guest = guest_environment(fex, env, keep_compat)
|
||||
x86_64 = elf_arch(program) != "i386"
|
||||
thunks = "on" if "FEX_THUNKCONFIG" in guest else "off"
|
||||
folder = decide if decide and os.path.isdir(decide) else os.path.dirname(program)
|
||||
@@ -963,7 +966,7 @@ def parse_run(args):
|
||||
|
||||
def main(argv):
|
||||
if not argv:
|
||||
say("usage: droiddeck-fex steam VERB CMD... | run [--mode auto|on|off] [--for PATH] [--] CMD... | needs PATH | extract IMAGE | prepare | status")
|
||||
say("usage: droiddeck-fex steam VERB CMD... | proton PROTON VERB CMD... | run [--mode auto|on|off] [--for PATH] [--] CMD... | needs PATH | extract IMAGE | prepare | status")
|
||||
return 2
|
||||
action, args = argv[0], list(argv[1:])
|
||||
if action == "steam":
|
||||
@@ -975,6 +978,10 @@ def main(argv):
|
||||
if not command:
|
||||
return 2
|
||||
return launch(command, game_environment(dict(os.environ), command), os.environ.get("DROIDDECK_FEX", "auto"))
|
||||
if action == "proton":
|
||||
if len(args) < 2:
|
||||
return 2
|
||||
return launch(args, game_environment(dict(os.environ), args[2:] or args), "on", keep_compat=True)
|
||||
if action == "run":
|
||||
try:
|
||||
mode, decide, command = parse_run(args)
|
||||
|
||||
@@ -318,12 +318,59 @@ dd_vacdiag() {
|
||||
}
|
||||
"""
|
||||
|
||||
DD_ANTICHEAT_SETUP = """
|
||||
dd_anticheat_runtime() {
|
||||
local current=$1 name=$2 library
|
||||
if [ -n "$current" ] && [ -n "$(find "$current" -name '*.so' -print -quit 2>/dev/null)" ]; then
|
||||
printf '%s' "$current"; return 0
|
||||
fi
|
||||
while IFS= read -r library; do
|
||||
[ -n "$(find "$library/steamapps/common/$name" -name '*.so' -print -quit 2>/dev/null)" ] || continue
|
||||
printf '%s' "$library/steamapps/common/$name"; return 0
|
||||
done < <(printf '%s\\n' "$DD_AC_ROOT" /mnt/droiddeck-sd; sed -n 's/^[[:space:]]*"path"[[:space:]]*"\\(.*\\)"[[:space:]]*$/\\1/p' "$DD_AC_ROOT/steamapps/libraryfolders.vdf" 2>/dev/null)
|
||||
return 1
|
||||
}
|
||||
|
||||
dd_x86_64_proton() {
|
||||
[ -d "$1/files/lib/wine/x86_64-unix" ] && [ ! -d "$1/files/bin-arm64" ]
|
||||
}
|
||||
|
||||
dd_anticheat() {
|
||||
case ${verb:-} in waitforexitandrun | run) ;; *) return 0 ;; esac
|
||||
case ${DROIDDECK_ANTICHEAT:-auto} in off | 0 | false) return 0 ;; esac
|
||||
local game=${STEAM_COMPAT_INSTALL_PATH:-} found tool runtime
|
||||
[ -d "$game" ] || return 0
|
||||
found=$(find "$game" -maxdepth 3 \\( -ipath '*/easyanticheat/settings.json' -o -iname 'easyanticheat_x64.so' \\
|
||||
-o -iname 'beclient_x64.so' -o -iname 'beservice_x64.exe' \\) -print -quit 2>/dev/null)
|
||||
[ -n "$found" ] || return 0
|
||||
DD_AC_ROOT=${STEAM_COMPAT_CLIENT_INSTALL_PATH:-${XDG_DATA_HOME:-$HOME/.local/share}/Steam}
|
||||
tool=$(cd "$DD_AC_ROOT/compatibilitytools.d" 2>/dev/null && for d in DroidDeck-Proton*; do
|
||||
[ -x "$d/proton" ] && dd_x86_64_proton "$d" && printf '%s/%s\\n' "$PWD" "$d"
|
||||
done | sort -V | tail -n 1)
|
||||
if [ -z "$tool" ]; then
|
||||
echo "droiddeck-proton: anti-cheat files in ${found#$game/}; no x86-64 DroidDeck-Proton is installed, so this launch stays on ARM64" >&2
|
||||
return 0
|
||||
fi
|
||||
case ${found,,} in
|
||||
*easyanticheat*) runtime=$(dd_anticheat_runtime "${PROTON_EAC_RUNTIME:-}" "Proton EasyAntiCheat Runtime") && export PROTON_EAC_RUNTIME="$runtime" ;;
|
||||
*) runtime=$(dd_anticheat_runtime "${PROTON_BATTLEYE_RUNTIME:-}" "Proton BattlEye Runtime") && export PROTON_BATTLEYE_RUNTIME="$runtime" ;;
|
||||
esac
|
||||
if [ -z "$runtime" ]; then
|
||||
echo "droiddeck-proton: anti-cheat files in ${found#$game/}; its Proton runtime is not installed, so this launch stays on ARM64" >&2
|
||||
return 0
|
||||
fi
|
||||
dd_game_cpus
|
||||
echo "droiddeck-proton: anti-cheat files in ${found#$game/}; running ${tool##*/} under FEX" >&2
|
||||
exec ${DD_TASKSET:-} /usr/local/bin/droiddeck-fex proton "$tool/proton" "$verb" "$@"
|
||||
}
|
||||
"""
|
||||
|
||||
# DroidDeck-Bionic runs only without proot. With proot on, a launch Steam sends to a DroidDeck-Bionic tool goes to the
|
||||
# newest DroidDeck-Proton's wrapper before anything else runs, as if that Proton had been picked; the picks stay as set.
|
||||
DD_BIONIC_PROOT_FALLBACK = """
|
||||
if [ -f "$here/bionic/launch.sh" ] && [ -z "${DDNATV_ROOT:-}" ]; then
|
||||
dd_fallback=$(cd "$here/.." 2>/dev/null && for d in DroidDeck-Proton*; do
|
||||
[ -x "$d/droiddeck-proton-wrap" ] && [ ! -e "$d/bionic/launch.sh" ] || continue
|
||||
[ -x "$d/droiddeck-proton-wrap" ] && [ ! -e "$d/bionic/launch.sh" ] && ! dd_x86_64_proton "$d" || continue
|
||||
v=${d#DroidDeck-Proton}; printf '%s\\t%s/%s\\n' "${v#[ -]}" "$PWD" "$d"
|
||||
done | sort -V | tail -n 1 | cut -f 2-)
|
||||
if [ -n "$dd_fallback" ]; then
|
||||
@@ -461,6 +508,7 @@ for name in %s; do
|
||||
[ -n "$depot" ] && break
|
||||
done
|
||||
if [ -n "$depot" ]; then
|
||||
dd_anticheat "$@"
|
||||
dd_components "$depot"
|
||||
dd_directaudio "$depot" "$@"
|
||||
dd_game_cpus
|
||||
@@ -481,7 +529,7 @@ LAUNCHER_TEMPLATE = LAUNCHER_SH
|
||||
def launcher_sh(sources=SOURCES):
|
||||
return (LAUNCHER_TEMPLATE % " ".join('"%s"' % n for n in sources)).replace(
|
||||
"verb=$1; shift", "verb=$1; shift\n" + DD_DRIVER_QUERY_SKIP + DD_DESKTOP_GPU_SETUP + DD_DIRECTAUDIO_SETUP + DD_GAME_CPUS_SETUP + DD_IDTECH3_SETUP
|
||||
+ DD_VACDIAG_SETUP + DD_COMPONENTS_SETUP + DD_LIBPATH_SETUP + DD_STORE_SETUP, 1)
|
||||
+ DD_VACDIAG_SETUP + DD_COMPONENTS_SETUP + DD_LIBPATH_SETUP + DD_STORE_SETUP + DD_ANTICHEAT_SETUP, 1)
|
||||
|
||||
|
||||
LAUNCHER_SH = launcher_sh()
|
||||
@@ -516,6 +564,11 @@ if [ -n "${PROOT_FP_KEY:-}" ] && [ -f "$audit" ]; then
|
||||
*) LD_AUDIT="$audit${LD_AUDIT:+:$LD_AUDIT}"; export LD_AUDIT ;;
|
||||
esac
|
||||
fi
|
||||
if dd_x86_64_proton "$here"; then
|
||||
dd_game_cpus
|
||||
exec ${DD_TASKSET:-} /usr/local/bin/droiddeck-fex proton "$here/%s" "$verb" "$@"
|
||||
fi
|
||||
dd_anticheat "$@"
|
||||
dd_components "$here"
|
||||
dd_directaudio "$here" "$@"
|
||||
dd_game_cpus
|
||||
@@ -530,9 +583,9 @@ dd_run ${DD_TASKSET:-} /usr/local/bin/droiddeck-game-env "$DD_STORE_PROTON" "$ve
|
||||
|
||||
|
||||
def extra_wrapper_sh(entry_point):
|
||||
return (EXTRA_WRAPPER_SH % entry_point).replace(
|
||||
return (EXTRA_WRAPPER_SH % (entry_point, entry_point)).replace(
|
||||
'here=$(dirname "$0")',
|
||||
'here=$(dirname "$0")\n' + DD_BIONIC_PROOT_FALLBACK + DD_DRIVER_QUERY_SKIP + DD_DESKTOP_GPU_SETUP + DD_DIRECTAUDIO_SETUP + DD_GAME_CPUS_SETUP
|
||||
'here=$(dirname "$0")\n' + DD_ANTICHEAT_SETUP + DD_BIONIC_PROOT_FALLBACK + DD_DRIVER_QUERY_SKIP + DD_DESKTOP_GPU_SETUP + DD_DIRECTAUDIO_SETUP + DD_GAME_CPUS_SETUP
|
||||
+ DD_IDTECH3_SETUP + DD_VACDIAG_SETUP + DD_COMPONENTS_SETUP + DD_LIBPATH_SETUP + DD_STORE_SETUP, 1)
|
||||
|
||||
|
||||
|
||||
@@ -957,8 +957,9 @@ __attribute__((visibility("hidden"))) void dd_vdec_closed(int fd) {
|
||||
|
||||
__attribute__((visibility("hidden"))) int dd_vdec_poll(struct pollfd *fds, nfds_t n, int timeout_ms,
|
||||
int (*real)(struct pollfd *, nfds_t, int), int *ret) {
|
||||
if (n != 1 || vd_find(fds[0].fd) == NULL) return 0;
|
||||
if (n != 1 || __atomic_load_n(&vd_open_count, __ATOMIC_ACQUIRE) == 0) return 0;
|
||||
struct vdec *v = vd_find(fds[0].fd);
|
||||
if (v == NULL) return 0;
|
||||
pthread_mutex_lock(&vd_lock);
|
||||
short ready = (v->n_cap_done ? POLLIN | POLLRDNORM : 0) | (v->n_out_done ? POLLOUT | POLLWRNORM : 0);
|
||||
pthread_mutex_unlock(&vd_lock);
|
||||
|
||||
@@ -374,6 +374,7 @@ static int ddnatv_execve(const char *filename, char *const argv[], char *const e
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
if (is_proc_self_exe(filename) && g_real_exe[0] != '\0') filename = g_real_exe;
|
||||
if (depth == 0 && !in_exec_rehearsal()) ddh_try(filename, argv, envp);
|
||||
|
||||
// Scripts get the path exactly as the caller wrote it ("./x.sh" stays "./x.sh" in $0),
|
||||
|
||||
@@ -660,6 +660,19 @@ long syscall(long number, ...) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_execve
|
||||
if (number == __NR_execve) {
|
||||
return (long)execve((const char *)arg1, (char *const *)arg2, (char *const *)arg3);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_execveat
|
||||
if (number == __NR_execveat && arg2 && ((const char *)arg2)[0] != '\0' &&
|
||||
(((const char *)arg2)[0] == '/' || (int)arg1 == AT_FDCWD) && (arg5 & ~(unsigned long)AT_EMPTY_PATH) == 0) {
|
||||
return (long)execve((const char *)arg2, (char *const *)arg3, (char *const *)arg4);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __NR_clone3
|
||||
if (number == __NR_clone3) {
|
||||
errno = ENOSYS;
|
||||
|
||||
@@ -10,6 +10,12 @@
|
||||
#define SYS_SECCOMP 1
|
||||
#endif
|
||||
|
||||
#if defined(__aarch64__)
|
||||
#define DDNATV_HOST_AUDIT_ARCH 0xC00000B7U
|
||||
#else
|
||||
#define DDNATV_HOST_AUDIT_ARCH 0x40000028U
|
||||
#endif
|
||||
|
||||
static int (*get_real_sigaction(void))(int, const struct sigaction *, struct sigaction *);
|
||||
|
||||
// A program's own SIGSYS handler, for the traps of a seccomp filter it installed itself (Firefox's
|
||||
@@ -57,7 +63,13 @@ static long sigsys_id_change(int nr, const unsigned long long *regs, long ret_va
|
||||
// Intercept SECCOMP blocked syscalls (SIGSYS), resolve sandbox/landlock gracefully, and advance PC
|
||||
static void ddnatv_sigsys_handler(int sig, siginfo_t *info, void *ctx) {
|
||||
if (!ctx || !info) return;
|
||||
if (info->si_code != SYS_SECCOMP) {
|
||||
int host_trap = info->si_code == SYS_SECCOMP && info->si_errno == 0 && info->si_arch == DDNATV_HOST_AUDIT_ARCH;
|
||||
if (!host_trap && g_app_sigsys_set) {
|
||||
if (g_app_sigsys.sa_flags & SA_SIGINFO) g_app_sigsys.sa_sigaction(sig, info, ctx);
|
||||
else g_app_sigsys.sa_handler(sig);
|
||||
return;
|
||||
}
|
||||
if (!host_trap) {
|
||||
int (*real_sig)(int, const struct sigaction *, struct sigaction *) = get_real_sigaction();
|
||||
if (real_sig) {
|
||||
struct sigaction sa_dfl;
|
||||
@@ -69,11 +81,6 @@ static void ddnatv_sigsys_handler(int sig, siginfo_t *info, void *ctx) {
|
||||
raise(sig);
|
||||
return;
|
||||
}
|
||||
if (info->si_errno != 0 && g_app_sigsys_set) {
|
||||
if (g_app_sigsys.sa_flags & SA_SIGINFO) g_app_sigsys.sa_sigaction(sig, info, ctx);
|
||||
else g_app_sigsys.sa_handler(sig);
|
||||
return;
|
||||
}
|
||||
ucontext_t *uctx = (ucontext_t *)ctx;
|
||||
|
||||
int sys_nr = info->si_syscall;
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import runpy
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
|
||||
COMPAT = runpy.run_path(str(BIN / "steam-compatibility"), run_name="steam_compatibility")
|
||||
SETUP = COMPAT["DD_ANTICHEAT_SETUP"]
|
||||
|
||||
|
||||
class AntiCheatRoutingTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(temp.cleanup)
|
||||
self.root = Path(temp.name)
|
||||
self.steam = self.root / "Steam"
|
||||
self.common = self.steam / "steamapps" / "common"
|
||||
self.tools = self.steam / "compatibilitytools.d"
|
||||
self.game = self.common / "Some Game"
|
||||
self.game.mkdir(parents=True)
|
||||
self.tools.mkdir(parents=True)
|
||||
self.record = self.root / "record.json"
|
||||
self.fex = self.root / "droiddeck-fex"
|
||||
self.fex.write_text("#!/bin/bash\npython3 -c 'import json,os,sys; json.dump({\"argv\": sys.argv[2:], "
|
||||
"\"eac\": os.environ.get(\"PROTON_EAC_RUNTIME\"), \"be\": os.environ.get(\"PROTON_BATTLEYE_RUNTIME\")}, "
|
||||
"open(sys.argv[1], \"w\"))' \"$DD_RECORD\" \"$@\"\n")
|
||||
self.fex.chmod(0o755)
|
||||
|
||||
def tool(self, name, x86_64=True):
|
||||
tool = self.tools / name
|
||||
(tool / "files" / "lib" / "wine" / ("x86_64-unix" if x86_64 else "aarch64-unix")).mkdir(parents=True)
|
||||
if not x86_64:
|
||||
(tool / "files" / "bin-arm64").mkdir()
|
||||
(tool / "proton").write_text("#!/bin/sh\n")
|
||||
(tool / "proton").chmod(0o755)
|
||||
return tool
|
||||
|
||||
def runtime(self, name, library):
|
||||
folder = self.common / name / "v2" / "lib64"
|
||||
folder.mkdir(parents=True)
|
||||
(folder / library).write_text("")
|
||||
return self.common / name
|
||||
|
||||
def file(self, relative):
|
||||
path = self.game / relative
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text("{}")
|
||||
|
||||
def launch(self, verb="waitforexitandrun", extra=None):
|
||||
script = SETUP.replace("/usr/local/bin/droiddeck-fex", str(self.fex)) + (
|
||||
"dd_game_cpus() { :; }\n"
|
||||
"verb=$1; shift\n"
|
||||
"dd_anticheat \"$@\"\n"
|
||||
"echo stayed\n")
|
||||
env = {"PATH": os.environ["PATH"], "HOME": str(self.root), "DD_RECORD": str(self.record),
|
||||
"STEAM_COMPAT_INSTALL_PATH": str(self.game), "STEAM_COMPAT_CLIENT_INSTALL_PATH": str(self.steam)}
|
||||
env.update(extra or {})
|
||||
result = subprocess.run(["bash", "-c", script, "launcher", verb, str(self.game / "game.exe")],
|
||||
env=env, capture_output=True, text=True, timeout=30)
|
||||
routed = json.loads(self.record.read_text()) if self.record.exists() else None
|
||||
return result, routed
|
||||
|
||||
def test_eos_easy_anti_cheat_runs_the_newest_x86_64_build_under_fex(self):
|
||||
self.file("EasyAntiCheat/Settings.json")
|
||||
self.tool("DroidDeck-Proton-11.0-1-x86_64")
|
||||
newest = self.tool("DroidDeck-Proton-11.0-10-x86_64")
|
||||
self.tool("DroidDeck-Proton-11.0-20", x86_64=False)
|
||||
runtime = self.runtime("Proton EasyAntiCheat Runtime", "easyanticheat_x64.so")
|
||||
result, routed = self.launch()
|
||||
self.assertNotIn("stayed", result.stdout)
|
||||
self.assertEqual(routed["argv"], ["proton", str(newest / "proton"), "waitforexitandrun", str(self.game / "game.exe")])
|
||||
self.assertEqual(routed["eac"], str(runtime))
|
||||
self.assertIn("running DroidDeck-Proton-11.0-10-x86_64 under FEX", result.stderr)
|
||||
|
||||
def test_battleye_sets_its_own_runtime(self):
|
||||
self.file("BattlEye/BEClient_x64.so")
|
||||
tool = self.tool("DroidDeck-Proton-11.0-1-x86_64")
|
||||
runtime = self.runtime("Proton BattlEye Runtime", "beclient_x64.so")
|
||||
_, routed = self.launch()
|
||||
self.assertEqual((routed["argv"][1], routed["be"], routed["eac"]), (str(tool / "proton"), str(runtime), None))
|
||||
|
||||
def test_a_runtime_already_set_with_files_is_kept(self):
|
||||
self.file("EasyAntiCheat/Settings.json")
|
||||
self.tool("DroidDeck-Proton-11.0-1-x86_64")
|
||||
own = self.root / "own-eac"
|
||||
(own / "v2").mkdir(parents=True)
|
||||
(own / "v2" / "easyanticheat_x64.so").write_text("")
|
||||
_, routed = self.launch(extra={"PROTON_EAC_RUNTIME": str(own)})
|
||||
self.assertEqual(routed["eac"], str(own))
|
||||
|
||||
def test_games_without_anti_cheat_files_stay_on_arm64(self):
|
||||
self.file("Data/Settings.json")
|
||||
self.tool("DroidDeck-Proton-11.0-1-x86_64")
|
||||
self.runtime("Proton EasyAntiCheat Runtime", "easyanticheat_x64.so")
|
||||
result, routed = self.launch()
|
||||
self.assertIsNone(routed)
|
||||
self.assertIn("stayed", result.stdout)
|
||||
self.assertEqual(result.stderr, "")
|
||||
|
||||
def test_without_an_x86_64_build_the_launch_stays_on_arm64(self):
|
||||
self.file("EasyAntiCheat/Settings.json")
|
||||
self.tool("DroidDeck-Proton-11.0-20", x86_64=False)
|
||||
self.runtime("Proton EasyAntiCheat Runtime", "easyanticheat_x64.so")
|
||||
result, routed = self.launch()
|
||||
self.assertIsNone(routed)
|
||||
self.assertIn("stayed", result.stdout)
|
||||
self.assertIn("no x86-64 DroidDeck-Proton is installed", result.stderr)
|
||||
|
||||
def test_without_the_runtime_files_the_launch_stays_on_arm64(self):
|
||||
self.file("EasyAntiCheat/Settings.json")
|
||||
self.tool("DroidDeck-Proton-11.0-1-x86_64")
|
||||
(self.common / "Proton EasyAntiCheat Runtime").mkdir(parents=True)
|
||||
result, routed = self.launch()
|
||||
self.assertIsNone(routed)
|
||||
self.assertIn("its Proton runtime is not installed", result.stderr)
|
||||
|
||||
def test_the_user_can_turn_routing_off(self):
|
||||
self.file("EasyAntiCheat/Settings.json")
|
||||
self.tool("DroidDeck-Proton-11.0-1-x86_64")
|
||||
self.runtime("Proton EasyAntiCheat Runtime", "easyanticheat_x64.so")
|
||||
result, routed = self.launch(extra={"DROIDDECK_ANTICHEAT": "off"})
|
||||
self.assertIsNone(routed)
|
||||
self.assertIn("stayed", result.stdout)
|
||||
|
||||
def test_other_verbs_are_left_alone(self):
|
||||
self.file("EasyAntiCheat/Settings.json")
|
||||
self.tool("DroidDeck-Proton-11.0-1-x86_64")
|
||||
self.runtime("Proton EasyAntiCheat Runtime", "easyanticheat_x64.so")
|
||||
result, routed = self.launch(verb="getcompatpath")
|
||||
self.assertIsNone(routed)
|
||||
self.assertIn("stayed", result.stdout)
|
||||
|
||||
def test_launchers_check_before_arm64_setup_and_x86_64_tools_run_under_fex(self):
|
||||
launcher = COMPAT["LAUNCHER_SH"]
|
||||
self.assertLess(launcher.index('dd_anticheat "$@"'), launcher.index('dd_components "$depot"'))
|
||||
wrapper = COMPAT["extra_wrapper_sh"]("proton")
|
||||
self.assertLess(wrapper.index("dd_x86_64_proton() {"), wrapper.index("dd_fallback="))
|
||||
self.assertLess(wrapper.index('droiddeck-fex proton "$here/proton"'), wrapper.index('dd_components "$here"'))
|
||||
self.assertIn('! dd_x86_64_proton "$d"', wrapper)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1235,6 +1235,29 @@ class LaunchTest(FexTestCase):
|
||||
self.assertEqual(emulated.call_args[0][2]["FEX_ROOTFS"], str(platform / "files"))
|
||||
self.assertEqual(FEX["required_runtime"](str(self.proton("1391110"))), "1628350")
|
||||
|
||||
def test_an_adopted_proton_keeps_the_runtime_of_its_original_manifest(self):
|
||||
proton = self.proton("4183110")
|
||||
folder = proton.parent
|
||||
os.rename(folder / "toolmanifest.vdf", folder / "toolmanifest.vdf.droiddeck-orig")
|
||||
self.write(folder / "toolmanifest.vdf", '"manifest"\n{\n "commandline" "/droiddeck-proton-wrap %verb%"\n}\n')
|
||||
self.assertEqual(FEX["required_runtime"](str(proton)), "4183110")
|
||||
os.unlink(folder / "toolmanifest.vdf.droiddeck-orig")
|
||||
self.assertEqual(FEX["required_runtime"](str(proton)), "1628350")
|
||||
|
||||
def test_proton_action_keeps_the_prefix_and_runs_under_fex(self):
|
||||
self.fex_tool()
|
||||
self.runtime()
|
||||
proton = self.proton("1628350")
|
||||
prefix = str(self.tmp / "compatdata" / "730")
|
||||
env = {"STEAM_COMPAT_DATA_PATH": prefix}
|
||||
with mock.patch.dict(os.environ, env, clear=False), mock.patch("os.execve") as emulated:
|
||||
self.quiet(FEX["main"], ["proton", str(proton), "waitforexitandrun", "game.exe"])
|
||||
command, guest = emulated.call_args[0][1], emulated.call_args[0][2]
|
||||
self.assertEqual(command[1:], [str(proton), "waitforexitandrun", "game.exe"])
|
||||
self.assertEqual(guest["STEAM_COMPAT_DATA_PATH"], prefix)
|
||||
self.assertNotEqual(FEX["guest_environment"]({"portable": True}, env)["STEAM_COMPAT_DATA_PATH"], prefix)
|
||||
self.assertEqual(FEX["main"](["proton", str(proton)]), 2)
|
||||
|
||||
def test_native_programs_run_directly(self):
|
||||
program = self.write(self.tmp / "native", elf(183), 0o755)
|
||||
with mock.patch("os.execvpe") as native, mock.patch("os.execve") as emulated:
|
||||
|
||||
Reference in new issue
Block a user