Repository navigation
feat(memory): back GPU-visible guest memory with shareable memory behind a flag #580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
52a2a63
e56f5a6
21459ef
7747091
0f03030
bdf4a11
8ec2c32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| #include "prx/libSceAgcDriver/Execution/include/ProfileOutput.hpp" | ||
| #include "prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp" | ||
| #include "prx/libc/include/GuestAllocations.hpp" | ||
| #include "prx/libc/include/GpuSharedMemory.hpp" | ||
| #include "prx/libc/include/GuestArena.hpp" | ||
| #include <cstdint> | ||
| #include <cstdio> | ||
|
|
@@ -52,25 +53,43 @@ constexpr std::uint32_t ScalarEndProgram = 0xbf810000u; | |
| constexpr std::uint64_t FsTableDword3 = 0x5204u; | ||
|
|
||
| void MapDriverMemory() { | ||
| void* mapped; | ||
| // The FS table and its routine block are GPU-visible, so under the GPU-process flag they come | ||
| // from the slid, shm-backed guest arena; the raw address is inside the PS5 system reservation, | ||
| // whose slid image is reserved too, so the block is allocated rather than slid. Every address | ||
| // written into it follows the mapped base. | ||
| std::uint64_t base = DriverMemoryAddress; | ||
| void* mapped = nullptr; | ||
| #if defined(__APPLE__) | ||
| if (GpuSharedMemory::GpuSharedMemoryEnabled_nid_postfix() && GuestArena::GuestArenaAvailable_nid_postfix()) { | ||
| base = reinterpret_cast<std::uint64_t>(GuestArena::GuestArenaAllocate_nid_postfix(DriverMemoryBytes, 0x1000)); | ||
| GuestArena::GuestArenaCommit_nid_postfix(reinterpret_cast<void*>(base), DriverMemoryBytes, 0x04, DriverMemoryBytes); | ||
|
Comment on lines
+63
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| mapped = reinterpret_cast<void*>(base); | ||
| } | ||
| #endif | ||
| if (mapped == nullptr) { | ||
| GuestArena::GuestArenaSlideAddress_nid_postfix(&base, DriverMemoryBytes); | ||
| #ifdef _WIN32 | ||
| mapped = reinterpret_cast<void*>(DriverMemoryAddress); | ||
| GuestArena::GuestArenaCommit_nid_postfix(mapped, DriverMemoryBytes, PAGE_READWRITE, DriverMemoryBytes); | ||
| mapped = reinterpret_cast<void*>(base); | ||
| GuestArena::GuestArenaCommit_nid_postfix(mapped, DriverMemoryBytes, PAGE_READWRITE, DriverMemoryBytes); | ||
| #else | ||
| mapped = mmap(reinterpret_cast<void*>(DriverMemoryAddress), DriverMemoryBytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0); | ||
| if (mapped == MAP_FAILED) throw std::system_error(errno, std::generic_category(), "map the AGC driver memory"); | ||
| mapped = mmap(reinterpret_cast<void*>(base), DriverMemoryBytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0); | ||
| if (mapped == MAP_FAILED) throw std::system_error(errno, std::generic_category(), "map the AGC driver memory"); | ||
| #endif | ||
| if (reinterpret_cast<std::uint64_t>(mapped) != DriverMemoryAddress) throw std::runtime_error("the AGC driver memory was not mapped at its fixed address"); | ||
| auto* routineTable = reinterpret_cast<std::uint64_t*>(FsRoutineTableAddress); | ||
| } | ||
| if (reinterpret_cast<std::uint64_t>(mapped) != base) throw std::runtime_error("the AGC driver memory was not mapped at its fixed address"); | ||
| const auto tableAddress = base + (FsTableAddress - DriverMemoryAddress); | ||
| const auto routineTableAddress = base + (FsRoutineTableAddress - DriverMemoryAddress); | ||
| const auto routineAddress = base + (FsRoutineAddress - DriverMemoryAddress); | ||
| auto* routineTable = reinterpret_cast<std::uint64_t*>(routineTableAddress); | ||
| for (std::uint64_t record = 0; record < FsRoutineRecords; ++record) { | ||
| routineTable[record * 2u] = FsRoutineAddress; | ||
| routineTable[record * 2u] = routineAddress; | ||
| routineTable[record * 2u + 1u] = 0u; | ||
| } | ||
| auto* routine = reinterpret_cast<std::uint32_t*>(FsRoutineAddress); | ||
| auto* routine = reinterpret_cast<std::uint32_t*>(routineAddress); | ||
| routine[0] = ScalarSetpcB64LinkS14; | ||
| routine[1] = ScalarEndProgram; | ||
| auto* slot = reinterpret_cast<std::uint64_t*>(FsTableAddress); | ||
| slot[0] = (FsRoutineTableAddress & 0xffffffffffffull) | 16ull << 48u; | ||
| auto* slot = reinterpret_cast<std::uint64_t*>(tableAddress); | ||
| slot[0] = (routineTableAddress & 0xffffffffffffull) | 16ull << 48u; | ||
| slot[1] = FsTableDword3 << 32u | FsRoutineRecords; | ||
| GuestAllocations::Mutation mutation; | ||
| mutation.Add(mapped, DriverMemoryBytes, true, true, true); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #ifndef CORE_LIBS_PRX_LIBC_INCLUDE_GPUSHAREDMEMORY_HPP | ||
| #define CORE_LIBS_PRX_LIBC_INCLUDE_GPUSHAREDMEMORY_HPP | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| // Registers the guest memory a separate native arm64 process has to map (a GPU driver server) and | ||
| // publishes, for each region, the shm object and offset that backs it. Enabled by | ||
| // ANYPS5_GPU_SHARED_MEMORY=1; the manifest is written to the path in | ||
| // ANYPS5_GPU_SHARED_MEMORY_MANIFEST on demand and at process exit. See docs/dev/debt/libc.md. | ||
| namespace GpuSharedMemory { | ||
|
|
||
| inline constexpr const char* EnableEnvironment = "ANYPS5_GPU_SHARED_MEMORY"; | ||
| inline constexpr const char* ManifestEnvironment = "ANYPS5_GPU_SHARED_MEMORY_MANIFEST"; | ||
| inline constexpr const char* ObjectPrefix = "/aps5-gpu-"; | ||
| inline constexpr std::size_t MaximumObjectName = 32; | ||
|
|
||
| extern "C" { | ||
|
|
||
| bool GpuSharedMemoryEnabled_nid_postfix(); | ||
| // Creates a shm object of `bytes` named ObjectPrefix + <pid> + "-" + suffix, records it for the | ||
| // exit-time shm_unlink and writes its name (at most MaximumObjectName bytes, terminator included) | ||
| // into `name`. Returns the descriptor, or -1. | ||
| int GpuSharedMemoryCreateObject_nid_postfix(const char* suffix, std::uint64_t bytes, char* name, std::size_t nameCapacity); | ||
| // Records an already-created object for the exit-time shm_unlink, so a reader can open it by name. | ||
| void GpuSharedMemoryAdoptObject_nid_postfix(const char* name); | ||
| bool GpuSharedMemoryRegister_nid_postfix(std::uint64_t address, std::uint64_t bytes, const char* object, std::uint64_t offset); | ||
| void GpuSharedMemoryUnregister_nid_postfix(std::uint64_t address, std::uint64_t bytes); | ||
| std::size_t GpuSharedMemoryRegionCount_nid_postfix(); | ||
| bool GpuSharedMemoryPublish_nid_postfix(); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #ifndef CORE_LIBS_PRX_LIBC_INCLUDE_GUESTLAYOUT_HPP | ||
| #define CORE_LIBS_PRX_LIBC_INCLUDE_GUESTLAYOUT_HPP | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| // The guest address space, shared by the native AOT compiler (core/aot/guest_layout.py mirrors | ||
| // these values), the native runtime and the HLE libs. A native arm64 process cannot map the PS5 | ||
| // guest at its raw addresses (macOS refuses fixed mappings in the 64-512 GiB hole), so the whole | ||
| // space is slid by one offset; the compiler's guest_base, the runtime's image mapping and the | ||
| // guest arena all use it. test_guest_layout parses this header and fails if the Python mirror | ||
| // drifts. | ||
| namespace GuestLayout { | ||
|
|
||
| // Raw PS5 addresses, as the title's own libc and libkernel see them. | ||
| inline constexpr std::uint64_t ApplicationAreaStart = 0x0000000200000000ull; // 8 GiB | ||
| inline constexpr std::uint64_t SystemReservedStart = 0x00000007FFFFC000ull; | ||
| inline constexpr std::uint64_t SystemReservedEnd = 0x0000001000000000ull; // 64 GiB | ||
| inline constexpr std::uint64_t ApplicationAreaEnd = 0x000000FC00000000ull; // 1008 GiB | ||
|
|
||
| // macOS refuses fixed mappings here, natively and under Rosetta (DirectMemory.cpp relocates them | ||
| // per request on the Rosetta path, where the guest arena is not used). | ||
| inline constexpr std::uint64_t MacOsRefusedStart = 0x0000001000000000ull; // 64 GiB | ||
| inline constexpr std::uint64_t MacOsRefusedEnd = 0x0000008000000000ull; // 512 GiB | ||
|
|
||
| // The slide the native arm64 build places the whole guest address space at. Sliding by 512 GiB | ||
| // puts the application area at [520 GiB, 1520 GiB], entirely above the hole. | ||
| inline constexpr std::uint64_t NativeSlide = 0x0000008000000000ull; // 512 GiB | ||
|
|
||
| // The environment variable the native launcher exports so the HLE arena agrees with the compiler. | ||
| inline constexpr const char* SlideEnvironment = "ANYPS5_GUEST_BASE"; | ||
|
|
||
| } | ||
|
|
||
| #endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
ANYPS5_GPU_SHARED_MEMORY=1, this slidesDriverMemoryAddressfrom0xfe0000000to0x8fe0000000, but the raw address lies inside[GuestLayout::SystemReservedStart, GuestLayout::SystemReservedEnd).SeedDarwinUsedRanges()has already inserted that entire slid interval into the arena's_usedmap, so this commit callsMarkUsedForCommit()and throws an "overlaps a used range" exception whenever the AGC driver is initialized. Fresh evidence after the earlier driver-memory finding is this new shared commit path, which now prevents initialization rather than leaving the block private; place the internal block in an allocatable arena range or explicitly carve it out of the reservation.Useful? React with 👍 / 👎.