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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ std::uint64_t ReadLabel(std::uint64_t address, std::size_t bytes);
// scratch heaps): uncommitted pages read as zeros, are not compared, and are never stored.
void ReadCommitted(std::uint64_t address, std::span<std::byte> destination);
void ReadCommittedUnsynced(std::uint64_t address, std::span<std::byte> destination);
void ReadCommittedCheckedUnsynced(std::uint64_t address, std::span<std::byte> destination);
bool EqualsCommitted(std::uint64_t address, std::span<const std::byte> bytes);
// EqualsCommitted without the flush hook: the caller has decided that the recorded GPU work
// writing the range need not be waited for (the dispatch-cache validation in Driver.cpp).
Expand Down Expand Up @@ -304,7 +305,9 @@ unsigned long long CodeOffset(const void* address);
// calls FlushGpuWrites itself.
void SetFlushHook(void (*hook)(std::uint64_t address, std::size_t bytes));
auto CurrentFlushHook() -> void (*)(std::uint64_t address, std::size_t bytes);
void SetReadGuard(void (*guard)(std::uint64_t address, std::size_t bytes));
using ReadGuard = void (*)(std::uint64_t address, std::size_t bytes);
void SetReadGuard(ReadGuard guard);
void SetReadGuards(ReadGuard access, ReadGuard observation);
void GuardRead(std::uint64_t address, std::size_t bytes);
void FlushGpuWrites(std::uint64_t address, std::size_t bytes);
void PrepareHostWrite(std::uintptr_t address, std::size_t bytes);
Expand Down
50 changes: 47 additions & 3 deletions core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3982,7 +3982,8 @@ unsigned long long CodeOffset(const void* address) {

namespace {
std::atomic<void (*)(std::uint64_t, std::size_t)> flushHook{nullptr};
std::atomic<void (*)(std::uint64_t, std::size_t)> readGuard{nullptr};
std::atomic<ReadGuard> readGuard{nullptr};
std::atomic<ReadGuard> observationGuard{nullptr};

void PrepareHostRead(std::uintptr_t address, std::size_t bytes) {
if (const auto guard = readGuard.load(std::memory_order_acquire)) guard(address, bytes);
Expand All @@ -3999,8 +4000,15 @@ auto CurrentFlushHook() -> void (*)(std::uint64_t, std::size_t) {
return flushHook.load(std::memory_order_acquire);
}

void SetReadGuard(void (*guard)(std::uint64_t address, std::size_t bytes)) {
readGuard.store(guard, std::memory_order_release);
void SetReadGuard(ReadGuard guard) {
SetReadGuards(guard, guard);
}

void SetReadGuards(ReadGuard access, ReadGuard observation) {
static std::mutex registration;
const std::lock_guard lock(registration);
observationGuard.store(observation, std::memory_order_release);
readGuard.store(access, std::memory_order_release);
GuestArena::GuestArenaSetHostReadHook_nid_no_patch(&PrepareHostRead);
}

Expand Down Expand Up @@ -4317,6 +4325,42 @@ void ReadCommittedUnsynced(std::uint64_t address, std::span<std::byte> destinati
copyCommitted(address, destination);
}

void ReadCommittedCheckedUnsynced(std::uint64_t address, std::span<std::byte> destination) {
if (destination.empty()) return;
require(address <= std::numeric_limits<std::uintptr_t>::max() && destination.size() <= std::numeric_limits<std::uintptr_t>::max() - address, "address range overflow");
if (const auto guard = observationGuard.load(std::memory_order_acquire)) guard(address, destination.size());
const TimedAccess timed(CounterRead, destination.size());
CountReadCaller(__builtin_return_address(0));
#if defined(_WIN32) || defined(__linux__) || defined(__APPLE__)
const auto copy = [](std::uint64_t source, std::span<std::byte> out) {
#ifdef _WIN32
return readPages(out.data(), source, out.size());
#elif defined(__APPLE__)
mach_vm_size_t copied = 0;
const auto result = mach_vm_read_overwrite(mach_task_self(), source, out.size(), reinterpret_cast<mach_vm_address_t>(out.data()), &copied);
if (result == KERN_SUCCESS) {
require(copied == out.size(), "short committed guest memory read");
return true;
}
require(result == KERN_INVALID_ADDRESS || result == KERN_PROTECTION_FAILURE, "cannot read committed guest memory");
return false;
#else
return CopyMapped(source, out) == Compare::Equal;
#endif
};
if (copy(address, destination)) return;
for (std::size_t offset = 0; offset < destination.size();) {
const auto cursor = address + offset;
const auto length = std::min<std::size_t>(destination.size() - offset, PageBytes - cursor % PageBytes);
const auto part = destination.subspan(offset, length);
if (!copy(cursor, part)) std::memset(part.data(), 0, part.size());
offset += length;
}
#else
copyCommitted(address, destination);
#endif
}

bool EqualsCommitted(std::uint64_t address, std::span<const std::byte> bytes) {
if (bytes.empty()) return true;
FlushGpuWrites(address, bytes.size());
Expand Down
33 changes: 31 additions & 2 deletions core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <bit>
#include <chrono>
#include <cstddef>
#include <deque>
#include <functional>
#include <map>
#include <memory>
Expand Down Expand Up @@ -302,6 +303,27 @@ class UnitFlags {
// either at once (WriteBack) or deferred: MarkDirty keeps them on the GPU until something reads that
// memory (FlushPending, through the GuestMemory flush hook), the image is refreshed after a CPU write,
// or it leaves the cache. Dispatches reusing the image meanwhile skip the round trip entirely.
class TextureWriteBackObserver : public std::enable_shared_from_this<TextureWriteBackObserver> {
public:
using Sink = std::function<void(std::uint64_t, std::span<const std::byte>)>;
using Report = std::function<void(const Sink&)>;
explicit TextureWriteBackObserver(Sink sink) : sink(std::move(sink)) {}
void Record(Recorder* recorder, Report report);
void Store(std::uint64_t address, std::span<const std::byte> bytes);
bool Await();

private:
struct Write {
Recorder* recorder;
std::weak_ptr<Report> scheduled;
Report ready;
};
void drain();
Sink sink;
std::deque<Write> writes;
bool failed = false;
};

class StorageTexture : public std::enable_shared_from_this<StorageTexture> {
public:
struct CreationHandOver {
Expand Down Expand Up @@ -619,6 +641,12 @@ class StorageTexture : public std::enable_shared_from_this<StorageTexture> {
DccKeys UploadedKeys() const { return uploadedKeys; }
DccKeys FilledKeys() const { return filledKeys; }
bool GuestSnapshotValid() const { return originalHolds(descriptor.baseAddress, static_cast<std::size_t>(guestBytes)); }
void ShareWriteBackObserver(std::shared_ptr<TextureWriteBackObserver> observer) { writeBackObserver = std::move(observer); }
void SetWriteBackObserver(TextureWriteBackObserver::Sink sink) { writeBackObserver = sink ? std::make_shared<TextureWriteBackObserver>(std::move(sink)) : nullptr; }
bool AwaitWriteBackSnapshot() {
const auto observer = writeBackObserver;
return !observer || observer->Await();
}
DccKeyProof& KeyProof() const { return keyProof; }
DccRangeProof& TargetKeyProof() const { return targetKeyProof; }
DccKeys ProvedKeys() const;
Expand Down Expand Up @@ -719,8 +747,8 @@ class StorageTexture : public std::enable_shared_from_this<StorageTexture> {
// the batch completed (`savedTicket` names that write-back per unit; anything that replaces or
// drops a unit's saved bytes first clears it).
std::vector<std::uint32_t> prepareStoredSave(const std::vector<bool>& layers, const std::vector<bool>& skippedLayer);
void commitStoredSave(const std::vector<std::uint32_t>& units);
void applyStoredSave(std::uint64_t ticket, const std::vector<std::uint32_t>& units);
void commitStoredSave(const std::vector<std::uint32_t>& units, const std::vector<std::pair<std::uint64_t, std::uint64_t>>& stored);
void applyStoredSave(std::uint64_t ticket, const std::vector<std::uint32_t>& units, const std::shared_ptr<Buffer>& bytes);
void cancelStoredSave(std::uint32_t unit);
void cancelStoredSaves();
void releaseUnheldOriginal();
Expand Down Expand Up @@ -1043,6 +1071,7 @@ class StorageTexture : public std::enable_shared_from_this<StorageTexture> {
std::atomic<bool> anyBaselineDue{false};
std::vector<std::uint64_t> rebaseTicket;
std::uint64_t lastRebaseTicket = 0;
std::shared_ptr<TextureWriteBackObserver> writeBackObserver;
// The storage cache let the image go (Flush): a lookup makes a new image of the surface, so
// no fill may put results into this one (guarded by the live-image registry lock).
bool released = false;
Expand Down
Loading
Loading