Files
mandy/runtime/src/guarded_copy.h
Jan Trencansky 70ea06a50b runtime: Round 10 WS2 — shared Cell::known_* cache, TitleHeap changed() scoping, 64-tile bridging, fused AVX2 copy_and_sum_256 & filter/collect cuts
- Move title_memory_map::changed() into TitleHeap::hooks.commit/decommit and !from_mirror_heap in free_title_block()
- Share Cell::known_* across CellGuard & TitleTextureCells (no PAGE_READONLY no-guard cache; MEM_RESERVE intra-pass in TitleTextureCells, cross-pass in CellGuard; seed on xmem::map_view)
- Persist TitleTextureCells VQ cache across outer batches; bridge across run_already_armed_ tiles with uniform restore[0..15] up to kMaxBridgeGap=64
- Persist 1024-entry local_mirror per ConsoleHeap*, flush Phase A/B disjointly, handle !base V#s in Phase A, skip redundant WC BAR writes for waiting T#s
- Fused SEH-safe AVX2/SSE2 guarded::copy_and_sum_256(), kMaxWorkers=8, single-reader title_write_watch::ask() fast path preserving mark() entries

AI-assisted: Antigravity (Gemini 4 Neon).
2026-11-28 05:38:11 -05:00

69 lines
3.8 KiB
C++

#pragma once
// Copies and reads made inside a __try frame that must turn an access violation into an error (the shadow and
// title-texture copies of the D3D12 layer, DirectStorage request copies, the kernel-write retry).
//
// The faulting access has to happen in a function of ours, not inlined into the frame with the __try:
// - Under Wine an access violation raised inside the CRT's memcpy or memmove (Wine's ucrtbase) never reaches the
// __except of the frame that called it, and the process ends ("Unhandled page fault ... starting debugger").
// guarded_copy_test --crt shows it: the same fault raised in a function of ours is caught. A Linux boot ended that way
// in the slot-133 shadow refresh, reading a title page the title was remapping (CopyPageMappingsBatchX).
// - clang-cl, without /EHa, catches only what a call inside the __try raises, not an inline load or store.
// So the bytes move here: never inlined, and no_builtin keeps the compiler from turning a loop back into a call to
// memcpy or memmove.
#include "shadow_keep.h"
#include <cstddef>
#include <cstdint>
#include <intrin.h>
// clang-cl: no_builtin keeps the loops below from becoming CRT calls. MSVC has no such attribute (and accepts no
// __attribute__ at all); it does not turn __movsb or a loop over volatile bytes into memcpy/memmove, so nothing is needed.
#if defined(__clang__)
#define LEONIDA_GUARDED_NO_BUILTIN __attribute__((no_builtin))
#else
#define LEONIDA_GUARDED_NO_BUILTIN
#endif
namespace leonida::guarded {
// Forward copy, REP MOVSB: the source and destination do not overlap, or the destination is below the source.
__declspec(noinline) LEONIDA_GUARDED_NO_BUILTIN inline void copy(void* to, const void* from, std::size_t bytes) {
__movsb(static_cast<unsigned char*>(to), static_cast<const unsigned char*>(from), bytes);
}
// memmove: forward when that is safe, else from the end down, byte by byte (rare: only an overlapping move upwards).
__declspec(noinline) LEONIDA_GUARDED_NO_BUILTIN inline void move(void* to, const void* from, std::size_t bytes) {
auto* d = static_cast<volatile unsigned char*>(to);
const auto* s = static_cast<const volatile unsigned char*>(from);
const auto dst = reinterpret_cast<std::uintptr_t>(to), src = reinterpret_cast<std::uintptr_t>(from);
if (dst <= src || dst >= src + bytes) {
__movsb(static_cast<unsigned char*>(to), static_cast<const unsigned char*>(from), bytes);
return;
}
for (std::size_t i = bytes; i-- > 0;) d[i] = s[i];
}
// An aligned 32- or 64-bit store in one locked access (XCHG): a thread polling the value, or reading it with one
// load, sees the old value or the new one, never half of each (a REP MOVSB may store byte by byte).
__declspec(noinline) inline void store32(void* to, std::uint32_t value) {
_InterlockedExchange(static_cast<volatile long*>(to), static_cast<long>(value));
}
__declspec(noinline) inline void store64(void* to, std::uint64_t value) {
_InterlockedExchange64(static_cast<volatile long long*>(to), static_cast<long long>(value));
}
// One word read: for a __try that only asks whether memory still holds what it did (an OVERLAPPED the title may have
// freed).
__declspec(noinline) inline std::uintptr_t read_word(const void* at) {
return *static_cast<const volatile std::uintptr_t*>(at);
}
// Fused copy + 256-byte piece hashing for a __try frame: never inlined and no_builtin so clang-cl (/EHsc) and Wine
// unwind any access violation on `from` into the caller's __except instead of a CRT memcpy.
// Intentionally NOT noexcept (SEH under /EHsc).
__declspec(noinline) LEONIDA_GUARDED_NO_BUILTIN inline void copy_and_sum_256(
std::uint8_t* to, const std::uint8_t* from, std::size_t pieces, std::uint64_t* out) {
leonida::shadow_keep::copy_and_sum_256(to, from, pieces, out);
}
} // namespace leonida::guarded