upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/shadow_keep.h
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/shadow_keep.h')
-rw-r--r--runtime/src/shadow_keep.h72
1 files changed, 72 insertions, 0 deletions
diff --git a/runtime/src/shadow_keep.h b/runtime/src/shadow_keep.h
new file mode 100644
index 000000000..a59752b49
--- /dev/null
+++ b/runtime/src/shadow_keep.h
@@ -0,0 +1,72 @@
1// What a shadow copy of the title's pages keeps of the GPU's writes (LEONIDA_SHADOW_KEEP, shims/
2// d3d12_vulkan_device.cpp ShadowPages). Some pages are copied at every refresh without a sign that the title wrote
3// them (mapped at two addresses, unguarded, reopened with the 64 KiB block around a written page, write-watched or
4// pinned): copied whole, their unchanged bytes overwrote what a shader had stored in the copy since. Here the title's
5// bytes are compared, per 256-byte piece of the title's address space, with a 64-bit sum of those copied last, and
6// only a piece that differs is copied. Portable: no Windows headers, no Vulkan.
7#pragma once
8
9#include <algorithm>
10#include <cstddef>
11#include <cstdint>
12#include <cstring>
13
14namespace leonida::shadow_keep {
15
16inline constexpr std::size_t kPiece = 256;
17
18// The sum of `n` bytes, never 0 (0 is "not known": the piece is copied at its next look).
19// Four independent lanes over 32 bytes a step, so the multiplies do not wait on each other: about the speed of a
20// copy (one chained lane read about 3.6 GB/s on an RTX 5080 machine's CPU, a copy about 18 GB/s).
21inline std::uint64_t sum_of(const std::uint8_t* bytes, std::size_t n) {
22 constexpr std::uint64_t k = 0xff51afd7ed558ccdull;
23 const auto rotl = [](std::uint64_t x, int r) { return (x << r) | (x >> (64 - r)); };
24 std::uint64_t a0 = 0x9e3779b97f4a7c15ull ^ n, a1 = 0xc2b2ae3d27d4eb4full, a2 = 0x165667b19e3779f9ull,
25 a3 = 0x27d4eb2f165667c5ull;
26 std::size_t i = 0;
27 for (; i + 32 <= n; i += 32) {
28 std::uint64_t v[4];
29 std::memcpy(v, bytes + i, 32);
30 a0 = rotl(a0 ^ v[0], 31) * k;
31 a1 = rotl(a1 ^ v[1], 31) * k;
32 a2 = rotl(a2 ^ v[2], 31) * k;
33 a3 = rotl(a3 ^ v[3], 31) * k;
34 }
35 for (; i < n; i += 8) {
36 std::uint64_t v = 0;
37 std::memcpy(&v, bytes + i, n - i < 8 ? n - i : 8);
38 a0 = rotl(a0 ^ v, 31) * k;
39 }
40 std::uint64_t h = a0 ^ rotl(a1, 17) ^ rotl(a2, 29) ^ rotl(a3, 47);
41 h ^= h >> 33;
42 h *= 0xc4ceb9fe1a85ec53ull;
43 h ^= h >> 29;
44 return h | 1; // 0: not known
45}
46
47// `bytes` holds the title's bytes [va, va + n) as read now. sums[k] is the sum of the piece at base + k * kPiece
48// (base kPiece-aligned) as last copied. Each piece, cut at kPiece boundaries of the address (the first and last may be
49// partial), whose bytes differ from its sum takes the new sum and is handed to copy(from, to, source).
50template <class Copy>
51void copy_changed(const std::uint8_t* bytes, std::uintptr_t va, std::size_t n, std::uint64_t* sums,
52 std::uintptr_t base, Copy&& copy) {
53 for (std::size_t off = 0; off < n;) {
54 const std::size_t piece = std::min<std::size_t>(n - off, kPiece - ((va + off) & (kPiece - 1)));
55 std::uint64_t& known = sums[(va + off - base) / kPiece];
56 const std::uint64_t sum = sum_of(bytes + off, piece);
57 if (sum != known) {
58 known = sum;
59 copy(va + off, va + off + piece, bytes + off);
60 }
61 off += piece;
62 }
63}
64
65// The pieces over [from, to) are not known any more (the copies were changed another way, as zeroed): each is copied
66// at its next look.
67inline void forget(std::uint64_t* sums, std::uintptr_t base, std::uintptr_t from, std::uintptr_t to) {
68 if (from >= to) return;
69 std::fill(sums + (from - base) / kPiece, sums + (to - base + kPiece - 1) / kPiece, std::uint64_t(0));
70}
71
72} // namespace leonida::shadow_keep