Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/RosettaLibs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ add_subdirectory(${CMAKE_SOURCE_DIR}/tools/rosetta/launcher ${CMAKE_BINARY_DIR}/

if(BUILD_TESTING)
add_subdirectory(${ANYPS5_ROSETTA_LIBS_ROOT}/tests/direct_memory_darwin ${CMAKE_BINARY_DIR}/rosetta/tests/direct_memory_darwin)
add_subdirectory(${ANYPS5_ROSETTA_LIBS_ROOT}/tests/gpu_shared_memory ${CMAKE_BINARY_DIR}/rosetta/tests/gpu_shared_memory)
add_subdirectory(${ANYPS5_ROSETTA_LIBS_ROOT}/tests/pad_input_script ${CMAKE_BINARY_DIR}/rosetta/tests/pad_input_script)
anyps5_rosetta_graphics_tests()
add_subdirectory(${ANYPS5_ROSETTA_LIBS_ROOT}/tests/rosetta_direct_input ${CMAKE_BINARY_DIR}/rosetta/tests/direct_input)
Expand Down
16 changes: 15 additions & 1 deletion core/libs/prx/libSceAgcDriver/Execution/Metal/DirectDriver.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "prx/libSceAgcDriver/Execution/include/Pm4.hpp"
#include "prx/libSceAgcDriver/Graphics/Metal/MetalGuestMemory.hpp"
#include "prx/libc/include/GuestAllocations.hpp"
#include "prx/libc/include/GuestArena.hpp"
#include "prx/libc/include/GpuSharedMemory.hpp"
#import <Foundation/Foundation.h>
#import <Metal/Metal.h>
#include <dlfcn.h>
Expand Down Expand Up @@ -56,11 +58,19 @@ bool HostAccessible(std::uint64_t address, std::size_t bytes, bool writable) {
// Command words outside the GPU-visible guest allocations (VideoOut's flip packet, a DCB in CPU-only
// or host memory) are copied here before submission. The driver captures submitted words during
// the call, so one buffer under a lock is enough. It starts at StagingBytes and grows to the
// largest such buffer, up to MaxStagingBytes (the capture's own limit is larger).
// largest such buffer, up to MaxStagingBytes (the capture's own limit is larger). Under
// ANYPS5_GPU_SHARED_MEMORY the buffer comes from the slid, shm-backed guest arena instead, so a
// native arm64 server can read the staged words at the address this process registered.
constexpr std::size_t StagingBytes = 64 * 1024;
constexpr std::size_t MaxStagingBytes = 64 * 1024 * 1024;
constexpr std::size_t StagingPageBytes = 0x4000;

std::byte* MapStaging(std::size_t bytes) {
if (GpuSharedMemory::GpuSharedMemoryEnabled_nid_postfix() && GuestArena::GuestArenaAvailable_nid_postfix()) {
void* address = GuestArena::GuestArenaAllocate_nid_postfix(bytes, StagingPageBytes);
GuestArena::GuestArenaCommit_nid_postfix(address, bytes, 0x04, StagingPageBytes);
return static_cast<std::byte*>(address);
}
void* mapping = ::mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (mapping == MAP_FAILED) throw std::runtime_error("Rosetta direct path: cannot map the command staging buffer");
return static_cast<std::byte*>(mapping);
Expand Down Expand Up @@ -269,6 +279,8 @@ void Configure(DirectState& state, MetalDriver& driver, const std::shared_ptr<co
}
state.applied = ranges;
state.configured = true;
if (GpuSharedMemory::GpuSharedMemoryEnabled_nid_postfix() && !GpuSharedMemory::GpuSharedMemoryPublish_nid_postfix())
std::fprintf(stderr, "[gpu] the shared-memory manifest could not be published\n");
}

}
Expand Down Expand Up @@ -377,6 +389,8 @@ void DirectSubmit(const Packet& packet, std::uint32_t queue, const char* call) {
} else if (ranges != state.applied) {
driver.ReplaceBorrowedRanges(*ranges, ++state.driverGeneration);
state.applied = ranges;
if (GpuSharedMemory::GpuSharedMemoryEnabled_nid_postfix() && !GpuSharedMemory::GpuSharedMemoryPublish_nid_postfix())
std::fprintf(stderr, "[gpu] the shared-memory manifest could not be published\n");
}
return driver;
}
Expand Down
41 changes: 30 additions & 11 deletions core/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Move driver memory outside the reserved arena interval

When ANYPS5_GPU_SHARED_MEMORY=1, this slides DriverMemoryAddress from 0xfe0000000 to 0x8fe0000000, but the raw address lies inside [GuestLayout::SystemReservedStart, GuestLayout::SystemReservedEnd). SeedDarwinUsedRanges() has already inserted that entire slid interval into the arena's _used map, so this commit calls MarkUsedForCommit() 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 👍 / 👎.

Comment on lines +63 to +65

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep the FS table at its fixed guest address

When ANYPS5_GPU_SHARED_MEMORY=1, allocating this block at the arena's next free address moves the FS table away from 0xfe0040000, but table-call shaders still embed that address unchanged: ReadTableCallees() dereferences the shader's constant pointer, and ComputeFsTableCalls.cpp explicitly verifies the table at 0xfe0040000. Such shaders will therefore fault while reading the descriptor instead of reaching the initialized table. Back the fixed address with shareable memory, or add a consistent relocation for every reference to it, rather than assigning an arbitrary arena address.

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);
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ target_sources(libc PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/StandardStreams.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/GuestAllocations.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/GuestArena.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/GpuSharedMemory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/CreationMask.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/HostEntropy.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/GuestStacks.cpp
Expand Down
36 changes: 36 additions & 0 deletions core/libs/prx/libc/include/GpuSharedMemory.hpp
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
10 changes: 10 additions & 0 deletions core/libs/prx/libc/include/GuestArena.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ using SharedBackingWriter = bool (*)(std::uintptr_t address, const void* source,

extern "C" {

// The slide is read from ANYPS5_GUEST_BASE before the arena is first used; this overrides it for
// a caller that already knows the value. Both must be page aligned.
void GuestArenaSetOffset_nid_postfix(std::uint64_t offset);
// Moves a raw guest address by the arena slide when the whole range lies in the raw PS5
// application area, so a fixed mapping request and the slid arena agree on the address. Returns
// false when no slide applies.
bool GuestArenaSlideAddress_nid_postfix(std::uintptr_t* address, std::size_t bytes);
// The slide the arena was placed with, so a memory mapping layer can move a raw guest address by
// the same amount.
std::uint64_t GuestArenaSlide_nid_postfix();
bool GuestArenaAvailable_nid_postfix();
bool GuestArenaContains_nid_postfix(const void* pointer, std::size_t bytes);
void* GuestArenaAllocate_nid_postfix(std::size_t bytes, std::size_t alignment);
Expand Down
34 changes: 34 additions & 0 deletions core/libs/prx/libc/include/GuestLayout.hpp
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
Loading