mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
Emulate exclusive pairs on high guest memory and release failed host unmaps later
This commit is contained in:
1 parent
46e36d95a3
commit
7217dcc99a
4 files changed
+297
-60
No files matched your search
@@ -29,8 +29,8 @@ 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/0027`). FEX claims such mappings only on
|
||||
hosts with LSE atomics; elsewhere the map fails as in stock FEX. `tools/fex/probes/guest-high-memory.c`
|
||||
`0x7001_0000_0000`, which FEX serves from emulated guest pages (`tools/fex/patches/0027`). Exclusive load and store pairs on that
|
||||
memory are emulated as a compare-and-swap, so hosts without LSE atomics work too. `tools/fex/probes/guest-high-memory.c`
|
||||
checks loads, stores, atomics, iovec system calls, `madvise` and code written through a high alias. 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
|
||||
|
||||
@@ -12,40 +12,19 @@ writes to code in them invalidate translated blocks.
|
||||
|
||||
Builds on StonkGuy's anti-cheat emulation from https://github.com/StonkGuy/FEX (eac-2611, MIT).
|
||||
---
|
||||
.../Tools/FEXInterpreter/FEXInterpreter.cpp | 2 +
|
||||
Source/Tools/LinuxEmulation/CMakeLists.txt | 1 +
|
||||
.../LinuxSyscalls/GuestHighMemory.cpp | 1051 +++++++++++++++++
|
||||
.../LinuxSyscalls/GuestHighMemory.h | 43 +
|
||||
.../LinuxSyscalls/GuestHighMemory.cpp | 1208 +++++++++++++++++
|
||||
.../LinuxSyscalls/GuestHighMemory.h | 42 +
|
||||
.../LinuxSyscalls/SignalDelegator.cpp | 5 +
|
||||
.../LinuxEmulation/LinuxSyscalls/Syscalls.cpp | 52 +-
|
||||
.../LinuxSyscalls/Syscalls/Memory.cpp | 30 +
|
||||
.../LinuxSyscalls/SyscallsSMCTracking.cpp | 61 +-
|
||||
.../LinuxSyscalls/ThreadManager.cpp | 2 +
|
||||
.../LinuxSyscalls/ThreadManager.h | 12 +
|
||||
10 files changed, 1229 insertions(+), 30 deletions(-)
|
||||
.../LinuxSyscalls/ThreadManager.h | 19 +
|
||||
9 files changed, 1390 insertions(+), 30 deletions(-)
|
||||
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
|
||||
|
||||
diff --git a/Source/Tools/FEXInterpreter/FEXInterpreter.cpp b/Source/Tools/FEXInterpreter/FEXInterpreter.cpp
|
||||
index 79f47aa..684e9fe 100644
|
||||
--- a/Source/Tools/FEXInterpreter/FEXInterpreter.cpp
|
||||
+++ b/Source/Tools/FEXInterpreter/FEXInterpreter.cpp
|
||||
@@ -16,6 +16,7 @@ $end_info$
|
||||
#include "ELFCodeLoader.h"
|
||||
#include "VDSO_Emulation.h"
|
||||
#include "LinuxSyscalls/GdbServer.h"
|
||||
+#include "LinuxSyscalls/GuestHighMemory.h"
|
||||
#include "LinuxSyscalls/LinuxAllocator.h"
|
||||
#include "LinuxSyscalls/Syscalls.h"
|
||||
#include "LinuxSyscalls/Utils/Threads.h"
|
||||
@@ -552,6 +553,7 @@ int main(int argc, char** argv, char** const envp) {
|
||||
{
|
||||
auto HostFeatures = FEX::FetchHostFeatures(Loader.Is64BitMode());
|
||||
CTX = FEXCore::Context::Context::CreateNewContext(HostFeatures);
|
||||
+ FEX::HLE::GuestHighMemory::SetHostAtomics(HostFeatures.SupportsAtomics);
|
||||
SupportsAVX = HostFeatures.SupportsAVX();
|
||||
SupportsSVE256 = HostFeatures.SupportsSVE256();
|
||||
}
|
||||
diff --git a/Source/Tools/LinuxEmulation/CMakeLists.txt b/Source/Tools/LinuxEmulation/CMakeLists.txt
|
||||
index 3f02104..acf4c4c 100644
|
||||
--- a/Source/Tools/LinuxEmulation/CMakeLists.txt
|
||||
@@ -60,10 +39,10 @@ index 3f02104..acf4c4c 100644
|
||||
LinuxSyscalls/SyscallsVMATracking.cpp
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
new file mode 100644
|
||||
index 0000000..72a79f6
|
||||
index 0000000..b2d94fa
|
||||
--- /dev/null
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
@@ -0,0 +1,1051 @@
|
||||
@@ -0,0 +1,1208 @@
|
||||
+#include "LinuxSyscalls/GuestHighMemory.h"
|
||||
+#include "LinuxSyscalls/Syscalls.h"
|
||||
+#include "LinuxSyscalls/SignalDelegator.h"
|
||||
@@ -109,10 +88,16 @@ index 0000000..72a79f6
|
||||
+ bool Code;
|
||||
+ };
|
||||
+
|
||||
+ struct HostRange {
|
||||
+ uint64_t Start;
|
||||
+ uint64_t Size;
|
||||
+ };
|
||||
+
|
||||
+ Region Regions[MaxRegions];
|
||||
+ std::atomic<uint32_t> Count {0};
|
||||
+ HostRange Retired[MaxRegions];
|
||||
+ uint32_t RetiredCount {};
|
||||
+ std::atomic<uint32_t> Sequence {0};
|
||||
+ bool HostAtomics {};
|
||||
+
|
||||
+ uint64_t HostLimit() {
|
||||
+ static const uint64_t Limit = [] {
|
||||
@@ -282,7 +267,39 @@ index 0000000..72a79f6
|
||||
+ return After <= MaxRegions;
|
||||
+ }
|
||||
+
|
||||
+ void ReleaseRetiredLocked() {
|
||||
+ uint32_t i = 0;
|
||||
+ while (i < RetiredCount) {
|
||||
+ if (::munmap(reinterpret_cast<void*>(Retired[i].Start), Retired[i].Size) == 0) {
|
||||
+ Retired[i] = Retired[--RetiredCount];
|
||||
+ } else {
|
||||
+ ++i;
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ void UnmapHost(uint64_t Start, uint64_t Size) {
|
||||
+ if (::munmap(reinterpret_cast<void*>(Start), Size) == 0) {
|
||||
+ return;
|
||||
+ }
|
||||
+ ::madvise(reinterpret_cast<void*>(Start), Size, MADV_DONTNEED);
|
||||
+ for (uint32_t i = 0; i < RetiredCount; ++i) {
|
||||
+ if (Retired[i].Start + Retired[i].Size == Start) {
|
||||
+ Retired[i].Size += Size;
|
||||
+ return;
|
||||
+ }
|
||||
+ if (Start + Size == Retired[i].Start) {
|
||||
+ Retired[i] = {Start, Retired[i].Size + Size};
|
||||
+ return;
|
||||
+ }
|
||||
+ }
|
||||
+ if (RetiredCount < MaxRegions) {
|
||||
+ Retired[RetiredCount++] = {Start, Size};
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ int UnmapLocked(uint64_t Address, uint64_t Length) {
|
||||
+ ReleaseRetiredLocked();
|
||||
+ if (!HasRoomLocked(Address, Length, 0)) {
|
||||
+ return -ENOMEM;
|
||||
+ }
|
||||
@@ -300,10 +317,7 @@ index 0000000..72a79f6
|
||||
+ }
|
||||
+ const auto OverlapStart = std::max(Guest, Address);
|
||||
+ const auto OverlapEnd = std::min(RegionEnd, End);
|
||||
+ auto* HostStart = reinterpret_cast<void*>(Host + (OverlapStart - Guest));
|
||||
+ if (::munmap(HostStart, OverlapEnd - OverlapStart) != 0) {
|
||||
+ ::madvise(HostStart, OverlapEnd - OverlapStart, MADV_DONTNEED);
|
||||
+ }
|
||||
+ UnmapHost(Host + (OverlapStart - Guest), OverlapEnd - OverlapStart);
|
||||
+
|
||||
+ if (OverlapStart == Guest && OverlapEnd == RegionEnd) {
|
||||
+ RemoveEntry(i);
|
||||
@@ -365,6 +379,121 @@ index 0000000..72a79f6
|
||||
+ return (Insn & (1U << 22)) == 0;
|
||||
+ }
|
||||
+
|
||||
+ constexpr bool IsExclusiveLoad(uint32_t Insn) {
|
||||
+ return (Insn & (1U << 22)) != 0;
|
||||
+ }
|
||||
+
|
||||
+ constexpr bool IsExclusivePair(uint32_t Insn) {
|
||||
+ return (Insn & (1U << 21)) != 0;
|
||||
+ }
|
||||
+
|
||||
+ constexpr uint32_t ExclusiveElementSize(uint32_t Insn) {
|
||||
+ return 1U << (Insn >> 30);
|
||||
+ }
|
||||
+
|
||||
+ uint64_t ReadReg(void* UContext, uint32_t Reg) {
|
||||
+ return Reg == 31 ? 0 : ArchHelpers::Context::GetArmReg(UContext, Reg);
|
||||
+ }
|
||||
+
|
||||
+ void WriteReg(void* UContext, uint32_t Reg, uint64_t Value) {
|
||||
+ if (Reg != 31) {
|
||||
+ ArchHelpers::Context::SetArmReg(UContext, Reg, Value);
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ uint64_t ElementMask(uint32_t Size) {
|
||||
+ return Size == 8 ? ~0ULL : (1ULL << (Size * 8)) - 1;
|
||||
+ }
|
||||
+
|
||||
+ uint64_t LoadElement(uint64_t Host, uint32_t Size) {
|
||||
+ switch (Size) {
|
||||
+ case 1: return __atomic_load_n(reinterpret_cast<const uint8_t*>(Host), __ATOMIC_SEQ_CST);
|
||||
+ case 2: return __atomic_load_n(reinterpret_cast<const uint16_t*>(Host), __ATOMIC_SEQ_CST);
|
||||
+ case 4: return __atomic_load_n(reinterpret_cast<const uint32_t*>(Host), __ATOMIC_SEQ_CST);
|
||||
+ default: return __atomic_load_n(reinterpret_cast<const uint64_t*>(Host), __ATOMIC_SEQ_CST);
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ bool CompareExchange(uint64_t Host, uint32_t Size, uint64_t ExpectedLow, uint64_t ExpectedHigh, uint64_t Low, uint64_t High) {
|
||||
+ switch (Size) {
|
||||
+ case 1: {
|
||||
+ auto Expected = static_cast<uint8_t>(ExpectedLow);
|
||||
+ return __atomic_compare_exchange_n(reinterpret_cast<uint8_t*>(Host), &Expected, static_cast<uint8_t>(Low), false,
|
||||
+ __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
|
||||
+ }
|
||||
+ case 2: {
|
||||
+ auto Expected = static_cast<uint16_t>(ExpectedLow);
|
||||
+ return __atomic_compare_exchange_n(reinterpret_cast<uint16_t*>(Host), &Expected, static_cast<uint16_t>(Low), false,
|
||||
+ __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
|
||||
+ }
|
||||
+ case 4: {
|
||||
+ auto Expected = static_cast<uint32_t>(ExpectedLow);
|
||||
+ return __atomic_compare_exchange_n(reinterpret_cast<uint32_t*>(Host), &Expected, static_cast<uint32_t>(Low), false,
|
||||
+ __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
|
||||
+ }
|
||||
+ case 8: {
|
||||
+ auto Expected = ExpectedLow;
|
||||
+ return __atomic_compare_exchange_n(reinterpret_cast<uint64_t*>(Host), &Expected, Low, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
|
||||
+ }
|
||||
+ default: {
|
||||
+ auto Expected = (static_cast<unsigned __int128>(ExpectedHigh) << 64) | ExpectedLow;
|
||||
+ const auto Desired = (static_cast<unsigned __int128>(High) << 64) | Low;
|
||||
+ return __atomic_compare_exchange_n(reinterpret_cast<unsigned __int128*>(Host), &Expected, Desired, false, __ATOMIC_SEQ_CST,
|
||||
+ __ATOMIC_SEQ_CST);
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ template<typename MonitorType>
|
||||
+ bool EmulateExclusive(MonitorType& Monitor, uint32_t Insn, uint64_t Host, void* UContext) {
|
||||
+ const auto Element = ExclusiveElementSize(Insn);
|
||||
+ const bool Pair = IsExclusivePair(Insn);
|
||||
+ const auto Size = Pair ? Element * 2 : Element;
|
||||
+ if (Host & (Size - 1)) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ if (IsExclusiveLoad(Insn)) {
|
||||
+ uint64_t Low {};
|
||||
+ uint64_t High {};
|
||||
+ if (Pair && Element == 8) {
|
||||
+ Low = LoadElement(Host, 8);
|
||||
+ High = LoadElement(Host + 8, 8);
|
||||
+ WriteReg(UContext, RtField(Insn), Low);
|
||||
+ WriteReg(UContext, Rt2Field(Insn), High);
|
||||
+ } else if (Pair) {
|
||||
+ Low = LoadElement(Host, 8);
|
||||
+ WriteReg(UContext, RtField(Insn), Low & 0xffffffffULL);
|
||||
+ WriteReg(UContext, Rt2Field(Insn), Low >> 32);
|
||||
+ } else {
|
||||
+ Low = LoadElement(Host, Element);
|
||||
+ WriteReg(UContext, RtField(Insn), Low);
|
||||
+ }
|
||||
+ Monitor.Host = Host;
|
||||
+ Monitor.Size = Size;
|
||||
+ Monitor.Low = Low;
|
||||
+ Monitor.High = High;
|
||||
+ Monitor.Valid = true;
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ uint64_t Low {};
|
||||
+ uint64_t High {};
|
||||
+ if (Pair && Element == 8) {
|
||||
+ Low = ReadReg(UContext, RtField(Insn));
|
||||
+ High = ReadReg(UContext, Rt2Field(Insn));
|
||||
+ } else if (Pair) {
|
||||
+ Low = (ReadReg(UContext, RtField(Insn)) & 0xffffffffULL) | (ReadReg(UContext, Rt2Field(Insn)) << 32);
|
||||
+ } else {
|
||||
+ Low = ReadReg(UContext, RtField(Insn)) & ElementMask(Element);
|
||||
+ }
|
||||
+ const bool Stored = Monitor.Valid && Monitor.Host == Host && Monitor.Size == Size &&
|
||||
+ CompareExchange(Host, Size, Monitor.Low, Monitor.High, Low, High);
|
||||
+ Monitor.Valid = false;
|
||||
+ WriteReg(UContext, RsField(Insn), Stored ? 0 : 1);
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ 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) {
|
||||
@@ -824,12 +953,8 @@ index 0000000..72a79f6
|
||||
+ return Address >= HostLimit() && Address < TaskTop;
|
||||
+}
|
||||
+
|
||||
+void SetHostAtomics(bool Supported) {
|
||||
+ HostAtomics = Supported;
|
||||
+}
|
||||
+
|
||||
+bool ClaimsMapping(uint64_t Address, uint64_t Length, int Flags) {
|
||||
+ if (!HostAtomics || !(Flags & MAP_FIXED) || (Flags & MAP_FIXED_NOREPLACE) || Length == 0) {
|
||||
+ if (!(Flags & MAP_FIXED) || (Flags & MAP_FIXED_NOREPLACE) || Length == 0) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ if (Address & (FEXCore::Utils::FEX_PAGE_SIZE - 1)) {
|
||||
@@ -1023,7 +1148,7 @@ index 0000000..72a79f6
|
||||
+ }
|
||||
+
|
||||
+ const auto Insn = *reinterpret_cast<const uint32_t*>(PC);
|
||||
+ if (!IsLoadStore(Insn) || IsLiteralLoad(Insn) || IsExclusive(Insn) || RnField(Insn) == 31) {
|
||||
+ if (!IsLoadStore(Insn) || IsLiteralLoad(Insn) || RnField(Insn) == 31) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
@@ -1053,6 +1178,17 @@ index 0000000..72a79f6
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ if (IsExclusive(Insn)) {
|
||||
+ const auto Address = ArchHelpers::Context::GetArmReg(UContext, RnField(Insn));
|
||||
+ const auto Size = IsExclusivePair(Insn) ? 2 * ExclusiveElementSize(Insn) : ExclusiveElementSize(Insn);
|
||||
+ if (Address < View.Guest || Address - View.Guest > View.Size - Size ||
|
||||
+ !EmulateExclusive(Trap.Monitor, Insn, View.Host + (Address - View.Guest), UContext)) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ ArchHelpers::Context::SetPc(UContext, PC + 4);
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ const int Scratch = PickScratch(Insn);
|
||||
+ if (Scratch < 0) {
|
||||
+ return false;
|
||||
@@ -1117,10 +1253,10 @@ index 0000000..72a79f6
|
||||
+} // 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 0000000..b744a6d
|
||||
index 0000000..30c2db4
|
||||
--- /dev/null
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
|
||||
@@ -0,0 +1,43 @@
|
||||
@@ -0,0 +1,42 @@
|
||||
+#pragma once
|
||||
+
|
||||
+#include <FEXCore/fextl/vector.h>
|
||||
@@ -1146,7 +1282,6 @@ index 0000000..b744a6d
|
||||
+ uint64_t ReceivedHeader {};
|
||||
+};
|
||||
+
|
||||
+void SetHostAtomics(bool Supported);
|
||||
+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);
|
||||
@@ -1495,10 +1630,10 @@ index e13cb1c..49df38c 100644
|
||||
FEX::HLE::_SyscallHandler->SeccompEmulator.FreeSeccompFilters(Thread);
|
||||
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
|
||||
index 657de6c..16ef192 100644
|
||||
index 657de6c..26040c6 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 {
|
||||
@@ -103,6 +103,25 @@ struct ThreadStateObject : public FEXCore::Allocator::FEXAllocOperators {
|
||||
bool Enabled;
|
||||
} SyscallUserDispatch {};
|
||||
|
||||
@@ -1512,6 +1647,13 @@ index 657de6c..16ef192 100644
|
||||
+ uint32_t Scratch;
|
||||
+ uint32_t Base;
|
||||
+ bool Active;
|
||||
+ struct {
|
||||
+ uint64_t Host;
|
||||
+ uint64_t Low;
|
||||
+ uint64_t High;
|
||||
+ uint32_t Size;
|
||||
+ bool Valid;
|
||||
+ } Monitor;
|
||||
+ } HighMemoryTrap {};
|
||||
+
|
||||
uint64_t JITStackSP {};
|
||||
|
||||
@@ -62,11 +62,12 @@ int main(void) {
|
||||
ssize_t w = writev(p[1], out, 2);
|
||||
memset(high + 12288, 0, 64);
|
||||
struct iovec in[2] = {{high + 12288, 3}, {high + 12291, 8}};
|
||||
ssize_t r = readv(p[0], in, 2);
|
||||
ssize_t r = w == 11 ? readv(p[0], in, 2) : -1;
|
||||
check("writev-readv", w == 11 && r == 11 && memcmp(high + 12288, "hello world", 11) == 0);
|
||||
|
||||
check("write", write(p[1], "abcdef", 6) == 6);
|
||||
check("read", read(p[0], high + 20000, 6) == 6 && memcmp(high + 20000, "abcdef", 6) == 0);
|
||||
int wrote = write(p[1], "abcdef", 6) == 6;
|
||||
check("write", wrote);
|
||||
check("read", wrote && read(p[0], high + 20000, 6) == 6 && memcmp(high + 20000, "abcdef", 6) == 0);
|
||||
|
||||
high[30000] = 7;
|
||||
check("madvise-dontneed", madvise(high + 28672, 4096, MADV_DONTNEED) == 0 && high[30000] == 0);
|
||||
|
||||
@@ -43,6 +43,20 @@ namespace Allocator { inline uint64_t GetHostVABits() { return 39; } }
|
||||
"""
|
||||
|
||||
STUBS = r"""
|
||||
static int MunmapFailures;
|
||||
static int TestMunmap(void* Address, size_t Size) {
|
||||
if (MunmapFailures > 0) {
|
||||
--MunmapFailures;
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
return munmap(Address, Size);
|
||||
}
|
||||
#define munmap TestMunmap
|
||||
namespace ArchHelpers::Context {
|
||||
inline uint64_t GetArmReg(void* UContext, uint32_t Reg) { return static_cast<uint64_t*>(UContext)[Reg]; }
|
||||
inline void SetArmReg(void* UContext, uint32_t Reg, uint64_t Value) { static_cast<uint64_t*>(UContext)[Reg] = Value; }
|
||||
}
|
||||
namespace FEX::HLE {
|
||||
inline bool HasSyscallError(uint64_t Result) { return Result >= 0xFFFF'FFFF'FFFF'0001ULL; }
|
||||
namespace FaultSafeUserMemAccess {
|
||||
@@ -68,8 +82,6 @@ int main() {
|
||||
Show("contains_task_top", Contains(TaskTop));
|
||||
Show("contains_host_limit", Contains(1ULL << 39));
|
||||
Show("contains_below_limit", Contains((1ULL << 39) - Page));
|
||||
Show("claims_without_atomics", ClaimsMapping(High, Page, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED));
|
||||
SetHostAtomics(true);
|
||||
Show("claims_fixed", ClaimsMapping(High, Page, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED));
|
||||
Show("claims_noreplace", ClaimsMapping(High, Page, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE));
|
||||
Show("claims_low", ClaimsMapping(Low, Page, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED));
|
||||
@@ -200,8 +212,65 @@ int main() {
|
||||
DiscardsContents(MADV_DONTNEED_LOCKED));
|
||||
Show("keeps", DiscardsContents(MADV_WILLNEED) + DiscardsContents(MADV_NORMAL) + DiscardsContents(MADV_HUGEPAGE));
|
||||
|
||||
SetHostAtomics(false);
|
||||
Show("claims_after_disable", ClaimsMapping(High, Page, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED));
|
||||
auto* Spare = static_cast<uint8_t*>(mmap(nullptr, 2 * Page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
|
||||
const uint64_t Retire = 0x7c0000000000ULL;
|
||||
unsigned char Resident = 0;
|
||||
RegionView View {};
|
||||
AddEntry(Retire, 2 * Page, reinterpret_cast<uint64_t>(Spare), false);
|
||||
MunmapFailures = 1;
|
||||
Show("retire_unmap", UnmapLocked(Retire, Page));
|
||||
Show("retire_pending", RetiredCount);
|
||||
Show("retire_untracked", !FindLocked(Retire, &View));
|
||||
Show("retire_host_held", mincore(Spare, Page, &Resident) == 0);
|
||||
Show("retire_second", UnmapLocked(Retire + Page, Page));
|
||||
Show("retire_released", RetiredCount);
|
||||
Show("retire_host_gone", mincore(Spare, Page, &Resident) != 0 && errno == ENOMEM);
|
||||
Show("retire_rest_gone", mincore(Spare + Page, Page, &Resident) != 0 && errno == ENOMEM);
|
||||
|
||||
struct {
|
||||
uint64_t Host;
|
||||
uint64_t Low;
|
||||
uint64_t High;
|
||||
uint32_t Size;
|
||||
bool Valid;
|
||||
} Monitor {};
|
||||
uint64_t Context[32] {};
|
||||
alignas(16) uint64_t Cell[2] = {5, 0};
|
||||
const auto CellHost = reinterpret_cast<uint64_t>(Cell);
|
||||
EmulateExclusive(Monitor, 0xc85f7c20, CellHost, Context);
|
||||
Show("ldxr_value", Context[0]);
|
||||
Context[0] = 9;
|
||||
EmulateExclusive(Monitor, 0xc8027c20, CellHost, Context);
|
||||
Show("stxr_status", Context[2]);
|
||||
Show("stxr_memory", Cell[0]);
|
||||
EmulateExclusive(Monitor, 0xc85f7c20, CellHost, Context);
|
||||
Cell[0] = 11;
|
||||
Context[0] = 12;
|
||||
EmulateExclusive(Monitor, 0xc8027c20, CellHost, Context);
|
||||
Show("contended_status", Context[2]);
|
||||
Show("contended_memory", Cell[0]);
|
||||
Context[0] = 13;
|
||||
EmulateExclusive(Monitor, 0xc8027c20, CellHost, Context);
|
||||
Show("unarmed_status", Context[2]);
|
||||
Show("unarmed_memory", Cell[0]);
|
||||
Cell[0] = 1;
|
||||
Cell[1] = 2;
|
||||
EmulateExclusive(Monitor, 0xc87f0c20, CellHost, Context);
|
||||
Show("ldxp_low", Context[0]);
|
||||
Show("ldxp_high", Context[3]);
|
||||
Context[0] = 7;
|
||||
Context[3] = 8;
|
||||
EmulateExclusive(Monitor, 0xc8220c20, CellHost, Context);
|
||||
Show("stxp_status", Context[2]);
|
||||
Show("stxp_memory", Cell[0] * 16 + Cell[1]);
|
||||
Cell[0] = 0x1122334455667788ULL;
|
||||
EmulateExclusive(Monitor, 0x085f7c20, CellHost, Context);
|
||||
Show("ldxrb_value", Context[0]);
|
||||
Context[0] = 0x1ab;
|
||||
EmulateExclusive(Monitor, 0x0802fc20, CellHost, Context);
|
||||
Show("stlxrb_status", Context[2]);
|
||||
Show("stlxrb_memory", Cell[0]);
|
||||
Show("misaligned", EmulateExclusive(Monitor, 0xc85f7c20, CellHost + 4, Context));
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
@@ -254,12 +323,18 @@ def harness():
|
||||
span(source, " constexpr uint64_t TaskTop", " void BeginWrite()"),
|
||||
block(source, "void SetEntry("),
|
||||
block(source, "bool AddEntry("),
|
||||
block(source, "void RemoveEntry("),
|
||||
block(source, "bool HasRoomLocked("),
|
||||
block(source, "void ReleaseRetiredLocked("),
|
||||
block(source, "void UnmapHost("),
|
||||
block(source, "int UnmapLocked("),
|
||||
span(source, " bool FindLocked(", " uint64_t NeighbourHint("),
|
||||
span(source, " constexpr uint32_t RtField(", " constexpr bool IsLoadStore("),
|
||||
block(source, "constexpr bool IsExclusive("),
|
||||
span(source, " constexpr bool IsExclusiveLoad(", " int PickScratch("),
|
||||
span(source, " constexpr uint64_t MaxIoVectors", "} // namespace"),
|
||||
])
|
||||
public = "\n".join(block(source, signature) for signature in (
|
||||
"void SetHostAtomics(",
|
||||
"bool Contains(",
|
||||
"bool ClaimsMapping(",
|
||||
"uint64_t Protect(",
|
||||
@@ -286,7 +361,7 @@ class GuestHighMemoryTest(unittest.TestCase):
|
||||
source = Path(tmp) / "high.cpp"
|
||||
binary = Path(tmp) / "high"
|
||||
source.write_text(harness())
|
||||
subprocess.run([compiler, "-std=c++20", "-o", str(binary), str(source)], check=True)
|
||||
subprocess.run([compiler, "-std=c++20", "-o", str(binary), str(source), "-latomic"], check=True)
|
||||
output = subprocess.run([str(binary)], capture_output=True, text=True, check=True).stdout
|
||||
cls.results = dict(line.split() for line in output.splitlines())
|
||||
|
||||
@@ -305,8 +380,6 @@ class GuestHighMemoryTest(unittest.TestCase):
|
||||
self.assertEqual(self.value("contains_below_limit"), 0)
|
||||
|
||||
def test_only_fixed_mappings_above_the_host_limit_are_relocated(self):
|
||||
self.assertEqual(self.value("claims_without_atomics"), 0)
|
||||
self.assertEqual(self.value("claims_after_disable"), 0)
|
||||
self.assertEqual(
|
||||
[self.value(name) for name in ("claims_fixed", "claims_noreplace", "claims_low", "claims_hint", "claims_task_top")],
|
||||
[1, 0, 0, 0, 0],
|
||||
@@ -353,16 +426,27 @@ class GuestHighMemoryTest(unittest.TestCase):
|
||||
text = PATCH.read_text()
|
||||
self.assertRegex(text, r"\n\+ Buf = reinterpret_cast<const void\*>\(FEX::HLE::GuestHighMemory::ToGuest\(")
|
||||
|
||||
def test_hosts_without_atomics_keep_the_old_mapping_path(self):
|
||||
def test_exclusive_pairs_become_compare_and_swap(self):
|
||||
self.assertEqual(self.value("ldxr_value"), 5)
|
||||
self.assertEqual([self.value("stxr_status"), self.value("stxr_memory")], [0, 9])
|
||||
self.assertEqual([self.value("contended_status"), self.value("contended_memory")], [1, 11])
|
||||
self.assertEqual([self.value("unarmed_status"), self.value("unarmed_memory")], [1, 11])
|
||||
self.assertEqual([self.value("ldxp_low"), self.value("ldxp_high")], [1, 2])
|
||||
self.assertEqual([self.value("stxp_status"), self.value("stxp_memory")], [0, 7 * 16 + 8])
|
||||
self.assertEqual(self.value("ldxrb_value"), 0x88)
|
||||
self.assertEqual([self.value("stlxrb_status"), self.value("stlxrb_memory")], [0, 0x11223344556677AB])
|
||||
self.assertEqual(self.value("misaligned"), 0)
|
||||
|
||||
def test_every_host_relocates_high_mappings(self):
|
||||
text = PATCH.read_text()
|
||||
self.assertRegex(text, r"\n\+ FEX::HLE::GuestHighMemory::SetHostAtomics\(HostFeatures\.SupportsAtomics\);")
|
||||
self.assertRegex(text, r"\n\+ if \(!HostAtomics \|\| !\(Flags & MAP_FIXED\)")
|
||||
self.assertNotIn("SetHostAtomics", text)
|
||||
self.assertNotIn("HostAtomics", text)
|
||||
|
||||
def test_exclusive_accesses_are_never_single_stepped(self):
|
||||
self.assertEqual(self.value("exclusive_refused"), 11)
|
||||
self.assertEqual(self.value("others_allowed"), 11)
|
||||
text = PATCH.read_text()
|
||||
self.assertRegex(text, r"\n\+ if \(!IsLoadStore\(Insn\) \|\| IsLiteralLoad\(Insn\) \|\| IsExclusive\(Insn\)")
|
||||
self.assertRegex(text, r"\n\+ if \(IsExclusive\(Insn\)\) \{\n(?:\+.*\n){3}\+ !EmulateExclusive\(")
|
||||
|
||||
def test_high_code_writes_invalidate_every_alias(self):
|
||||
text = PATCH.read_text()
|
||||
@@ -397,6 +481,16 @@ class GuestHighMemoryTest(unittest.TestCase):
|
||||
text = PATCH.read_text()
|
||||
self.assertRegex(text, r"\n\+ if \(Applied && FEX::HLE::GuestHighMemory::DiscardsContents\(advice\)\) \{")
|
||||
|
||||
def test_failed_host_unmap_is_released_later(self):
|
||||
self.assertEqual(self.value("retire_unmap"), 0)
|
||||
self.assertEqual(self.value("retire_pending"), 1)
|
||||
self.assertEqual(self.value("retire_untracked"), 1)
|
||||
self.assertEqual(self.value("retire_host_held"), 1)
|
||||
self.assertEqual(self.value("retire_second"), 0)
|
||||
self.assertEqual(self.value("retire_released"), 0)
|
||||
self.assertEqual(self.value("retire_host_gone"), 1)
|
||||
self.assertEqual(self.value("retire_rest_gone"), 1)
|
||||
|
||||
def test_no_replace_probes_reach_the_host(self):
|
||||
text = PATCH.read_text()
|
||||
self.assertIsNone(re.search(r"^\+.*Flags & \(MAP_FIXED \| MAP_FIXED_NOREPLACE\)", text, re.M))
|
||||
|
||||
Reference in new issue
Block a user