From 837cca331c902413ca28110c76de2bf4cb1bb9b3 Mon Sep 17 00:00:00 2001 From: DH Date: Sun, 3 Nov 2024 18:40:23 +0300 Subject: [PATCH] vm: protect: allow unaligned addresses --- rpcsx/vm.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/rpcsx/vm.cpp b/rpcsx/vm.cpp index e0363c682..22ac77df6 100644 --- a/rpcsx/vm.cpp +++ b/rpcsx/vm.cpp @@ -1003,9 +1003,12 @@ bool vm::protect(void *addr, std::uint64_t size, std::int32_t prot) { std::println("vm::protect(addr = {}, len = {}, prot = {})", addr, size, mapProtToString(prot)); - size = rx::alignUp(size, kPageSize); - auto pages = (size + (kPageSize - 1)) >> kPageShift; auto address = reinterpret_cast(addr); + auto endAddress = address + size; + address = rx::alignDown(address, kPageSize); + endAddress = rx::alignUp(endAddress, kPageSize); + size = endAddress - address; + auto pages = size >> kPageShift; if (address < kMinAddress || address >= kMaxAddress || size > kMaxAddress || address > kMaxAddress - size) { std::println(stderr, "Memory error: protect out of memory"); @@ -1037,7 +1040,8 @@ bool vm::protect(void *addr, std::uint64_t size, std::int32_t prot) { } else if (prot >> 4) { std::println(stderr, "ignoring mapping {:x}-{:x}", address, address + size); } - return ::mprotect(addr, size, prot & kMapProtCpuAll) == 0; + return ::mprotect(std::bit_cast(address), size, + prot & kMapProtCpuAll) == 0; } static std::int32_t getPageProtectionImpl(std::uint64_t address) {