diff --git a/tools/fex/patches/0027-fixed-guest-memory-above-host-address-limit.patch b/tools/fex/patches/0027-fixed-guest-memory-above-host-address-limit.patch index 77a2b680b4..8c20a9d10e 100644 --- a/tools/fex/patches/0027-fixed-guest-memory-above-host-address-limit.patch +++ b/tools/fex/patches/0027-fixed-guest-memory-above-host-address-limit.patch @@ -39,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..b2d94fa +index 0000000..ffd97ef --- /dev/null +++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp -@@ -0,0 +1,1208 @@ +@@ -0,0 +1,1312 @@ +#include "LinuxSyscalls/GuestHighMemory.h" +#include "LinuxSyscalls/Syscalls.h" +#include "LinuxSyscalls/SignalDelegator.h" @@ -78,6 +78,7 @@ index 0000000..b2d94fa + std::atomic Size; + std::atomic Host; + std::atomic Code; ++ std::atomic Prot; + }; + + struct RegionView { @@ -86,6 +87,7 @@ index 0000000..b2d94fa + uint64_t Host; + uint32_t Index; + bool Code; ++ uint8_t Prot; + }; + + struct HostRange { @@ -125,6 +127,7 @@ index 0000000..b2d94fa + Out->Size = Size; + Out->Host = Regions[i].Host.load(std::memory_order_relaxed); + Out->Code = Regions[i].Code.load(std::memory_order_relaxed); ++ Out->Prot = Regions[i].Prot.load(std::memory_order_relaxed); + Out->Index = i; + return true; + } @@ -198,7 +201,7 @@ index 0000000..b2d94fa + 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 (Apply(reinterpret_cast(Host), OverlapEnd - OverlapStart) != 0) { ++ if (Apply(reinterpret_cast(Host), OverlapEnd - OverlapStart, i) != 0) { + return -errno; + } + Covered += OverlapEnd - OverlapStart; @@ -222,28 +225,30 @@ index 0000000..b2d94fa + return 0; + } + -+ void SetEntry(uint32_t i, uint64_t Guest, uint64_t Size, uint64_t Host, bool Code) { ++ void SetEntry(uint32_t i, uint64_t Guest, uint64_t Size, uint64_t Host, bool Code, uint8_t Prot) { + 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); ++ Regions[i].Prot.store(Prot, 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)); ++ Regions[Last].Host.load(std::memory_order_relaxed), Regions[Last].Code.load(std::memory_order_relaxed), ++ Regions[Last].Prot.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) { ++ bool AddEntry(uint64_t Guest, uint64_t Size, uint64_t Host, bool Code, uint8_t Prot) { + const auto Entries = Count.load(std::memory_order_relaxed); + if (Entries >= MaxRegions) { + return false; + } -+ SetEntry(Entries, Guest, Size, Host, Code); ++ SetEntry(Entries, Guest, Size, Host, Code, Prot); + Count.store(Entries + 1, std::memory_order_release); + return true; + } @@ -310,6 +315,7 @@ index 0000000..b2d94fa + 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 Prot = Regions[i].Prot.load(std::memory_order_relaxed); + const auto RegionEnd = Guest + Size; + if (RegionEnd <= Address || Guest >= End) { + ++i; @@ -324,20 +330,61 @@ index 0000000..b2d94fa + continue; + } + if (OverlapStart == Guest) { -+ SetEntry(i, OverlapEnd, RegionEnd - OverlapEnd, Host + (OverlapEnd - Guest), Code); ++ SetEntry(i, OverlapEnd, RegionEnd - OverlapEnd, Host + (OverlapEnd - Guest), Code, Prot); + } else if (OverlapEnd == RegionEnd) { -+ SetEntry(i, Guest, OverlapStart - Guest, Host, Code); ++ SetEntry(i, Guest, OverlapStart - Guest, Host, Code, Prot); + } else { -+ if (!AddEntry(OverlapEnd, RegionEnd - OverlapEnd, Host + (OverlapEnd - Guest), Code)) { ++ if (!AddEntry(OverlapEnd, RegionEnd - OverlapEnd, Host + (OverlapEnd - Guest), Code, Prot)) { + return -ENOMEM; + } -+ SetEntry(i, Guest, OverlapStart - Guest, Host, Code); ++ SetEntry(i, Guest, OverlapStart - Guest, Host, Code, Prot); + } + ++i; + } + return 0; + } + ++ constexpr uint8_t TrackedProt(int Prot) { ++ return Prot & (PROT_READ | PROT_WRITE | PROT_EXEC); ++ } ++ ++ int SplitLocked(uint64_t Address, uint64_t Length) { ++ const auto Size = FEXCore::AlignUp(Length, FEXCore::Utils::FEX_PAGE_SIZE); ++ if (Size == 0) { ++ return 0; ++ } ++ const uint64_t Boundaries[] = {Address, Address + std::min(Size, TaskTop - Address)}; ++ uint32_t Needed = 0; ++ const auto Entries = Count.load(std::memory_order_relaxed); ++ for (const auto Boundary : Boundaries) { ++ for (uint32_t i = 0; i < Entries; ++i) { ++ const auto Guest = Regions[i].Guest.load(std::memory_order_relaxed); ++ if (Boundary > Guest && Boundary - Guest < Regions[i].Size.load(std::memory_order_relaxed)) { ++ ++Needed; ++ } ++ } ++ } ++ if (Entries + Needed > MaxRegions) { ++ return -ENOMEM; ++ } ++ for (const auto Boundary : Boundaries) { ++ for (uint32_t i = 0; i < Count.load(std::memory_order_relaxed); ++i) { ++ const auto Guest = Regions[i].Guest.load(std::memory_order_relaxed); ++ const auto RegionSize = Regions[i].Size.load(std::memory_order_relaxed); ++ if (Boundary <= Guest || Boundary - Guest >= RegionSize) { ++ continue; ++ } ++ const auto Host = Regions[i].Host.load(std::memory_order_relaxed); ++ const auto Code = Regions[i].Code.load(std::memory_order_relaxed); ++ const auto Prot = Regions[i].Prot.load(std::memory_order_relaxed); ++ AddEntry(Boundary, Guest + RegionSize - Boundary, Host + (Boundary - Guest), Code, Prot); ++ SetEntry(i, Guest, Boundary - Guest, Host, Code, Prot); ++ break; ++ } ++ } ++ return 0; ++ } ++ + constexpr uint32_t RtField(uint32_t Insn) { + return Insn & 0x1f; + } @@ -387,6 +434,11 @@ index 0000000..b2d94fa + return (Insn & (1U << 21)) != 0; + } + ++ constexpr bool ExclusiveReachable(uint32_t Insn, uint8_t Prot) { ++ const uint8_t Required = IsExclusiveLoad(Insn) ? PROT_READ : (PROT_READ | PROT_WRITE); ++ return (Prot & Required) == Required; ++ } ++ + constexpr uint32_t ExclusiveElementSize(uint32_t Insn) { + return 1U << (Insn >> 30); + } @@ -524,10 +576,13 @@ index 0000000..b2d94fa + ReceivedMessage, + }; + ++ constexpr uint8_t BitmapElements = 0xff; ++ + struct Layout { + uint8_t Pointers; + uint32_t Lengths; + VectorKind Vector {VectorKind::None}; ++ uint64_t ElementSizes {}; + }; + + struct LayoutEntry { @@ -547,6 +602,37 @@ index 0000000..b2d94fa + return (Arguments.Lengths >> (Pointer * 4)) & 0xf; + } + ++ constexpr uint64_t Each(uint32_t Pointer, uint8_t Size) { ++ return static_cast(Size) << (Pointer * 8); ++ } ++ ++ constexpr uint8_t ElementSize(const Layout& Arguments, uint32_t Pointer) { ++ return (Arguments.ElementSizes >> (Pointer * 8)) & 0xff; ++ } ++ ++ constexpr uint64_t PollFdSize = 8; ++ constexpr uint64_t EpollEventSize = 12; ++ constexpr uint64_t MmsgHeaderSize = 64; ++ constexpr uint64_t MaxElements = 1ULL << 32; ++ ++ bool ByteLength(const Layout& Arguments, uint32_t Pointer, uint64_t Count, uint64_t* Bytes) { ++ const auto Size = ElementSize(Arguments, Pointer); ++ if (Size == 0) { ++ *Bytes = Count; ++ return true; ++ } ++ if (Size == BitmapElements) { ++ const auto Elements = static_cast(Count); ++ *Bytes = (static_cast(Elements) + 63) / 64 * 8; ++ return true; ++ } ++ if (Count >= MaxElements) { ++ return false; ++ } ++ *Bytes = Count * Size; ++ return true; ++ } ++ + constexpr LayoutEntry LayoutEntries[] = { + {x64::SYSCALL_x64_read, {Ptr(1), Sized(1, 2)}}, + {x64::SYSCALL_x64_write, {Ptr(1), Sized(1, 2)}}, @@ -554,7 +640,7 @@ index 0000000..b2d94fa + {x64::SYSCALL_x64_stat, {Ptr(0) | Ptr(1)}}, + {x64::SYSCALL_x64_fstat, {Ptr(1)}}, + {x64::SYSCALL_x64_lstat, {Ptr(0) | Ptr(1)}}, -+ {x64::SYSCALL_x64_poll, {Ptr(0)}}, ++ {x64::SYSCALL_x64_poll, {Ptr(0), Sized(0, 1), VectorKind::None, Each(0, PollFdSize)}}, + {x64::SYSCALL_x64_rt_sigaction, {Ptr(1) | Ptr(2)}}, + {x64::SYSCALL_x64_rt_sigprocmask, {Ptr(1) | Ptr(2)}}, + {x64::SYSCALL_x64_ioctl, {Ptr(2)}}, @@ -564,7 +650,7 @@ index 0000000..b2d94fa + {x64::SYSCALL_x64_writev, {0, 0, VectorKind::Io}}, + {x64::SYSCALL_x64_access, {Ptr(0)}}, + {x64::SYSCALL_x64_pipe, {Ptr(0)}}, -+ {x64::SYSCALL_x64_select, {Ptr(1) | Ptr(2) | Ptr(3) | Ptr(4)}}, ++ {x64::SYSCALL_x64_select, {Ptr(1) | Ptr(2) | Ptr(3) | Ptr(4), Sized(1, 0) | Sized(2, 0) | Sized(3, 0), VectorKind::None, Each(1, BitmapElements) | Each(2, BitmapElements) | Each(3, BitmapElements)}}, + {x64::SYSCALL_x64_msync, {Ptr(0), Sized(0, 1)}}, + {x64::SYSCALL_x64_mincore, {Ptr(0) | Ptr(2), Sized(0, 1)}}, + {x64::SYSCALL_x64_shmctl, {Ptr(2)}}, @@ -693,7 +779,7 @@ index 0000000..b2d94fa + {x64::SYSCALL_x64_clock_gettime, {Ptr(1)}}, + {x64::SYSCALL_x64_clock_getres, {Ptr(1)}}, + {x64::SYSCALL_x64_clock_nanosleep, {Ptr(2) | Ptr(3)}}, -+ {x64::SYSCALL_x64_epoll_wait, {Ptr(1)}}, ++ {x64::SYSCALL_x64_epoll_wait, {Ptr(1), Sized(1, 2), VectorKind::None, Each(1, EpollEventSize)}}, + {x64::SYSCALL_x64_epoll_ctl, {Ptr(3)}}, + {x64::SYSCALL_x64_utimes, {Ptr(0) | Ptr(1)}}, + {x64::SYSCALL_x64_mbind, {Ptr(0) | Ptr(3), Sized(0, 1)}}, @@ -725,14 +811,14 @@ index 0000000..b2d94fa + {x64::SYSCALL_x64_readlinkat, {Ptr(1) | Ptr(2), Sized(2, 3)}}, + {x64::SYSCALL_x64_fchmodat, {Ptr(1)}}, + {x64::SYSCALL_x64_faccessat, {Ptr(1)}}, -+ {x64::SYSCALL_x64_pselect6, {Ptr(1) | Ptr(2) | Ptr(3) | Ptr(4) | Ptr(5)}}, -+ {x64::SYSCALL_x64_ppoll, {Ptr(0) | Ptr(2) | Ptr(3)}}, ++ {x64::SYSCALL_x64_pselect6, {Ptr(1) | Ptr(2) | Ptr(3) | Ptr(4) | Ptr(5), Sized(1, 0) | Sized(2, 0) | Sized(3, 0), VectorKind::None, Each(1, BitmapElements) | Each(2, BitmapElements) | Each(3, BitmapElements)}}, ++ {x64::SYSCALL_x64_ppoll, {Ptr(0) | Ptr(2) | Ptr(3), Sized(0, 1) | Sized(3, 4), VectorKind::None, Each(0, PollFdSize)}}, + {x64::SYSCALL_x64_get_robust_list, {Ptr(1) | Ptr(2)}}, + {x64::SYSCALL_x64_splice, {Ptr(1) | Ptr(3)}}, + {x64::SYSCALL_x64_vmsplice, {0, 0, VectorKind::Io}}, + {x64::SYSCALL_x64_move_pages, {Ptr(2) | Ptr(3) | Ptr(4)}}, + {x64::SYSCALL_x64_utimensat, {Ptr(1) | Ptr(2)}}, -+ {x64::SYSCALL_x64_epoll_pwait, {Ptr(1) | Ptr(4)}}, ++ {x64::SYSCALL_x64_epoll_pwait, {Ptr(1) | Ptr(4), Sized(1, 2) | Sized(4, 5), VectorKind::None, Each(1, EpollEventSize)}}, + {x64::SYSCALL_x64_signalfd, {Ptr(1)}}, + {x64::SYSCALL_x64_timerfd_settime, {Ptr(2) | Ptr(3)}}, + {x64::SYSCALL_x64_timerfd_gettime, {Ptr(1)}}, @@ -743,13 +829,13 @@ index 0000000..b2d94fa + {x64::SYSCALL_x64_pwritev, {0, 0, VectorKind::Io}}, + {x64::SYSCALL_x64_rt_tgsigqueueinfo, {Ptr(3)}}, + {x64::SYSCALL_x64_perf_event_open, {Ptr(0)}}, -+ {x64::SYSCALL_x64_recvmmsg, {Ptr(1) | Ptr(4)}}, ++ {x64::SYSCALL_x64_recvmmsg, {Ptr(1) | Ptr(4), Sized(1, 2), VectorKind::None, Each(1, MmsgHeaderSize)}}, + {x64::SYSCALL_x64_fanotify_mark, {Ptr(4)}}, + {x64::SYSCALL_x64_prlimit_64, {Ptr(2) | Ptr(3)}}, + {x64::SYSCALL_x64_name_to_handle_at, {Ptr(1) | Ptr(2) | Ptr(3)}}, + {x64::SYSCALL_x64_open_by_handle_at, {Ptr(1)}}, + {x64::SYSCALL_x64_clock_adjtime, {Ptr(1)}}, -+ {x64::SYSCALL_x64_sendmmsg, {Ptr(1)}}, ++ {x64::SYSCALL_x64_sendmmsg, {Ptr(1), Sized(1, 2), VectorKind::None, Each(1, MmsgHeaderSize)}}, + {x64::SYSCALL_x64_getcpu, {Ptr(0) | Ptr(1) | Ptr(2)}}, + {x64::SYSCALL_x64_kcmp, {Ptr(4)}}, + {x64::SYSCALL_x64_finit_module, {Ptr(1)}}, @@ -782,7 +868,7 @@ index 0000000..b2d94fa + {x64::SYSCALL_x64_openat2, {Ptr(1) | Ptr(2), Sized(2, 3)}}, + {x64::SYSCALL_x64_faccessat2, {Ptr(1)}}, + {x64::SYSCALL_x64_process_madvise, {Ptr(1)}}, -+ {x64::SYSCALL_x64_epoll_pwait2, {Ptr(1) | Ptr(3) | Ptr(4)}}, ++ {x64::SYSCALL_x64_epoll_pwait2, {Ptr(1) | Ptr(3) | Ptr(4), Sized(1, 2) | Sized(4, 5), VectorKind::None, Each(1, EpollEventSize)}}, + {x64::SYSCALL_x64_mount_setattr, {Ptr(1) | Ptr(3), Sized(3, 4)}}, + {x64::SYSCALL_x64_quotactl_fd, {Ptr(3)}}, + {x64::SYSCALL_x64_landlock_create_ruleset, {Ptr(0), Sized(0, 1)}}, @@ -985,7 +1071,7 @@ index 0000000..b2d94fa + } else { + Result = UnmapLocked(Address, Size); + } -+ if (Result == 0 && !AddEntry(Address, Size, reinterpret_cast(Host), false)) { ++ if (Result == 0 && !AddEntry(Address, Size, reinterpret_cast(Host), false, TrackedProt(Prot))) { + Result = -ENOMEM; + } + EndWrite(); @@ -1011,12 +1097,27 @@ index 0000000..b2d94fa +} + +uint64_t Protect(uint64_t Address, uint64_t Length, int Prot) { -+ return ApplyToRegionsLocked(Address, Length, -ENOMEM, [Prot](void* Host, uint64_t Size) { return ::mprotect(Host, Size, Prot); }); ++ BeginWrite(); ++ uint64_t Result = ApplyToRegionsLocked(Address, Length, -ENOMEM, [](void*, uint64_t, uint32_t) { return 0; }); ++ if (Result == 0) { ++ Result = SplitLocked(Address, Length); ++ } ++ if (Result == 0) { ++ Result = ApplyToRegionsLocked(Address, Length, -ENOMEM, [Prot](void* Host, uint64_t Size, uint32_t i) { ++ const int Applied = ::mprotect(Host, Size, Prot); ++ if (Applied == 0) { ++ Regions[i].Prot.store(TrackedProt(Prot), std::memory_order_relaxed); ++ } ++ return Applied; ++ }); ++ } ++ EndWrite(); ++ return Result; +} + +uint64_t Advise(uint64_t Address, uint64_t Length, int Advice, bool* Applied) { + *Applied = false; -+ return ApplyToRegionsLocked(Address, Length, -EINVAL, [Advice, Applied](void* Host, uint64_t Size) { ++ return ApplyToRegionsLocked(Address, Length, -EINVAL, [Advice, Applied](void* Host, uint64_t Size, uint32_t) { + const int Result = ::madvise(Host, Size, Advice); + if (Result == 0) { + *Applied = true; @@ -1093,7 +1194,10 @@ index 0000000..b2d94fa + continue; + } + const auto LengthArg = LengthArgument(Arguments, i); -+ const uint64_t Length = LengthArg != 0 && LengthArg <= NumArgs ? Args[LengthArg - 1] : 0; ++ uint64_t Length = 0; ++ if (LengthArg != 0 && LengthArg <= NumArgs && !ByteLength(Arguments, i, Args[LengthArg - 1], &Length)) { ++ return -EFAULT; ++ } + if (!TranslateRange(Args[i], Length, &Args[i])) { + return -EFAULT; + } @@ -1181,7 +1285,7 @@ index 0000000..b2d94fa + 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 || ++ if (Address < View.Guest || Address - View.Guest > View.Size - Size || !ExclusiveReachable(Insn, View.Prot) || + !EmulateExclusive(Trap.Monitor, Insn, View.Host + (Address - View.Guest), UContext)) { + return false; + } diff --git a/tools/tests/test_fex_guest_high_memory.py b/tools/tests/test_fex_guest_high_memory.py index 32d436d74f..9b4ff36d3c 100644 --- a/tools/tests/test_fex_guest_high_memory.py +++ b/tools/tests/test_fex_guest_high_memory.py @@ -93,11 +93,11 @@ int main() { const uint64_t Single = 0x7f0000000000ULL; const uint64_t Split = 0x7e0000000000ULL; const uint64_t Joined = 0x7d0000000000ULL; - AddEntry(Single, Page, Base, false); - AddEntry(Split, Page, Base + 2 * Page, false); - AddEntry(Split + Page, Page, Base + 4 * Page, false); - AddEntry(Joined, Page, Base + 5 * Page, false); - AddEntry(Joined + Page, Page, Base + 6 * Page, false); + AddEntry(Single, Page, Base, false, PROT_READ | PROT_WRITE); + AddEntry(Split, Page, Base + 2 * Page, false, PROT_READ | PROT_WRITE); + AddEntry(Split + Page, Page, Base + 4 * Page, false, PROT_READ | PROT_WRITE); + AddEntry(Joined, Page, Base + 5 * Page, false, PROT_READ | PROT_WRITE); + AddEntry(Joined + Page, Page, Base + 6 * Page, false, PROT_READ | PROT_WRITE); ArgumentStorage Storage; uint64_t Read[6] = {3, Single + 16, 32, 0, 0, 0}; @@ -128,6 +128,25 @@ int main() { uint64_t Write[6] = {3, Split + Page - 96, 96, 0, 0, 0}; Show("fits_result", Call(Sys::SYSCALL_x64_write, Write, &Storage)); + + uint64_t PollApart[6] = {Split + Page - 96, 25, 0, 0, 0, 0}; + Show("poll_split_result", Call(Sys::SYSCALL_x64_poll, PollApart, &Storage)); + uint64_t PollFits[6] = {Split + Page - 96, 12, 0, 0, 0, 0}; + Show("poll_fits_result", Call(Sys::SYSCALL_x64_poll, PollFits, &Storage)); + uint64_t PollJoined[6] = {Joined + Page - 96, 25, 0, 0, 0, 0}; + Show("poll_joined_result", Call(Sys::SYSCALL_x64_poll, PollJoined, &Storage)); + uint64_t PollHuge[6] = {Single, 1ULL << 61, 0, 0, 0, 0}; + Show("poll_huge_result", Call(Sys::SYSCALL_x64_poll, PollHuge, &Storage)); + uint64_t EpollApart[6] = {3, Split + Page - 96, 9, 0, 0, 0}; + Show("epoll_split_result", Call(Sys::SYSCALL_x64_epoll_wait, EpollApart, &Storage)); + uint64_t EpollFits[6] = {3, Split + Page - 96, 8, 0, 0, 0}; + Show("epoll_fits_result", Call(Sys::SYSCALL_x64_epoll_wait, EpollFits, &Storage)); + uint64_t SelectApart[6] = {1024, Split + Page - 64, 0, 0, 0, 0}; + Show("select_split_result", Call(Sys::SYSCALL_x64_select, SelectApart, &Storage)); + uint64_t SelectFits[6] = {512, Split + Page - 64, 0, 0, 0, 0}; + Show("select_fits_result", Call(Sys::SYSCALL_x64_select, SelectFits, &Storage)); + uint64_t SelectWide[6] = {(1ULL << 32) + 1, Single, 0, 0, 0, 0}; + Show("select_wide_result", Call(Sys::SYSCALL_x64_select, SelectWide, &Storage)); Show("fits_buffer", Write[1] - Base); char LowBuffer[16]; @@ -183,7 +202,7 @@ int main() { Show("others_allowed", Allowed); std::vector> Applied; - auto Record = [&](void* Host, uint64_t Size) { + auto Record = [&](void* Host, uint64_t Size, uint32_t) { Applied.emplace_back(reinterpret_cast(Host), Size); return 0; }; @@ -208,6 +227,27 @@ int main() { Show("protect_split", Protect(Split, 2 * Page, PROT_READ | PROT_WRITE)); Show("protect_zero", Protect(Single, 0, PROT_READ | PROT_WRITE)); Show("protect_wrap", Protect(Single, UINT64_MAX - 16, PROT_READ | PROT_WRITE)); + + auto* Guarded = static_cast(mmap(nullptr, 3 * Page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + const uint64_t Guard = 0x7b0000000000ULL; + RegionView Part {}; + AddEntry(Guard, 3 * Page, reinterpret_cast(Guarded), false, PROT_READ | PROT_WRITE); + Show("guard_protect", Protect(Guard + Page, Page, PROT_READ)); + FindLocked(Guard, &Part); + Show("guard_low_size", Part.Size); + Show("guard_low_prot", Part.Prot); + FindLocked(Guard + Page, &Part); + Show("guard_mid_size", Part.Size); + Show("guard_mid_host", Part.Host - reinterpret_cast(Guarded)); + Show("guard_mid_prot", Part.Prot); + FindLocked(Guard + 2 * Page, &Part); + Show("guard_high_size", Part.Size); + Show("guard_high_prot", Part.Prot); + Show("guard_store_readonly", ExclusiveReachable(0xc8027c20, PROT_READ)); + Show("guard_load_readonly", ExclusiveReachable(0xc85f7c20, PROT_READ)); + Show("guard_store_writable", ExclusiveReachable(0xc8027c20, PROT_READ | PROT_WRITE)); + Show("guard_load_none", ExclusiveReachable(0xc85f7c20, PROT_NONE)); + Show("guard_pair_store_readonly", ExclusiveReachable(0xc8220c20, PROT_READ)); Show("discards", DiscardsContents(MADV_DONTNEED) + DiscardsContents(MADV_FREE) + DiscardsContents(MADV_REMOVE) + DiscardsContents(MADV_DONTNEED_LOCKED)); Show("keeps", DiscardsContents(MADV_WILLNEED) + DiscardsContents(MADV_NORMAL) + DiscardsContents(MADV_HUGEPAGE)); @@ -216,7 +256,7 @@ int main() { const uint64_t Retire = 0x7c0000000000ULL; unsigned char Resident = 0; RegionView View {}; - AddEntry(Retire, 2 * Page, reinterpret_cast(Spare), false); + AddEntry(Retire, 2 * Page, reinterpret_cast(Spare), false, PROT_READ | PROT_WRITE); MunmapFailures = 1; Show("retire_unmap", UnmapLocked(Retire, Page)); Show("retire_pending", RetiredCount); @@ -328,6 +368,10 @@ def harness(): block(source, "void ReleaseRetiredLocked("), block(source, "void UnmapHost("), block(source, "int UnmapLocked("), + block(source, "void BeginWrite("), + block(source, "void EndWrite("), + block(source, "constexpr uint8_t TrackedProt("), + block(source, "int SplitLocked("), span(source, " bool FindLocked(", " uint64_t NeighbourHint("), span(source, " constexpr uint32_t RtField(", " constexpr bool IsLoadStore("), block(source, "constexpr bool IsExclusive("), @@ -403,6 +447,32 @@ class GuestHighMemoryTest(unittest.TestCase): self.assertEqual(self.value("fits_result"), 0) self.assertEqual(self.value("fits_buffer"), 3 * 4096 - 96) + def test_element_counts_size_their_buffers(self): + self.assertEqual(self.value("poll_split_result"), self.efault()) + self.assertEqual(self.value("poll_fits_result"), 0) + self.assertEqual(self.value("poll_joined_result"), 0) + self.assertEqual(self.value("poll_huge_result"), self.efault()) + self.assertEqual(self.value("epoll_split_result"), self.efault()) + self.assertEqual(self.value("epoll_fits_result"), 0) + self.assertEqual(self.value("select_split_result"), self.efault()) + self.assertEqual(self.value("select_fits_result"), 0) + self.assertEqual(self.value("select_wide_result"), 0) + + def test_exclusive_access_respects_guest_protection(self): + self.assertEqual(self.value("guard_protect"), 0) + self.assertEqual(self.value("guard_low_size"), 4096) + self.assertEqual(self.value("guard_low_prot"), 3) + self.assertEqual(self.value("guard_mid_size"), 4096) + self.assertEqual(self.value("guard_mid_host"), 4096) + self.assertEqual(self.value("guard_mid_prot"), 1) + self.assertEqual(self.value("guard_high_size"), 4096) + self.assertEqual(self.value("guard_high_prot"), 3) + self.assertEqual(self.value("guard_store_readonly"), 0) + self.assertEqual(self.value("guard_load_readonly"), 1) + self.assertEqual(self.value("guard_store_writable"), 1) + self.assertEqual(self.value("guard_load_none"), 0) + self.assertEqual(self.value("guard_pair_store_readonly"), 0) + def test_io_vectors_get_a_translated_host_copy(self): self.assertEqual(self.value("readv_result"), 0) self.assertEqual(self.value("readv_copied"), 1)