mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-03 03:25:39 +00:00
Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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
|
||||
|
||||
@@ -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
|
||||
@@ -761,7 +761,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 +769,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 +963,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 +975,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,20 @@ 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_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