upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/file_system.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 20:26:00 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 20:26:00 -0500
commit154e4ec034902deffa4b89b2cbca9a949f38baec (patch)
tree1b2ef5656de42c172291bc6009efa4c510650815 /runtime/shims/file_system.cpp
parenteb6a08cd359d98b5777e70d54d0ae8a75879495a (diff)
parent8a242efebf823aeb23c72d17c3d666daf0bbd3e2 (diff)
Merge the #984 result (main + RT-PROTON-STORY-LOAD) into #939
The base is 8a242efeb, the merge of main and #984. 68 files conflicted; they were resolved in seven groups by one rule: the same change in two forms keeps the base's form, everything only #939 has is kept, and where the two solve one problem in two ways the base's is the frame and #939's plugs into it. A, the Xbox proxy (d3d12_xs_device.cpp/.h, d3d12_xs.cpp, d3d12_xs_pso_stream.cpp/.h, d3d12_xs_alloc_size.h, d3d12_xs_desc_clean.h, device_proxy_test.cpp, queue_proxy_test.cpp): the base's files with #939's additions ported in. Device slot 57 asks #939's NativeGraphicsFactory first and falls back to the base's proxy_create_graphics_pipeline_from_native_code on E_NOTIMPL. Kept from #939: the providers (texture lease, raw texture view, resource by address, constant view, descriptor mapping, depth-stencil UAV, texture pages), the title-backed descriptor mirror, slot 13's feature answers, GPU labels on list slots 71/72. Dropped: #939's duplicates of the boot ladder routes, its fallback window, its slot 133 stand-in. A2 (xs_proxy_mock_test.cpp): the base's test with #939's slot 70 raw-buffer case; presentation cases follow #939's rule that a device which ever had a handler has no queue fallback. B1, the Vulkan device (d3d12_vulkan_device.cpp, d3d12_com.cpp/.h, shader_library.h, queue_families.h, pipeline_cache_file.h, startup_trace.h): one lazy-pipeline mechanism, the base's; #939's pending_base_compile and first_use_backend_pipelines dropped, prepare_on_bind and cold_graphics_compile kept. create_texture keeps the base's signature; #939's deferred backing is folded into the base's placed-memory binder. #939's quad lists as triangle indices. B2, the D3D12 tests (d3d12_existing_heap, a8_dimensions, pipeline_cache_seed, title_va, rect_list, shader_library, loader_tls_index0, draw, compute, xmem_placed, shader_library_corpus): the base's tests where both carry the same case, #939's cases added (rebound tables, linear texture map, large vertex batch, executable shaders, default A8 views). C, the backend (vulkan_replay.h, vulkan_backend.h, vulkan_bindless.h, vulkan_scratch.h, vulkan_present.h, rect_list_geometry.h, vk_test_device.h, vk_pipeline_check.cpp, vk_store_test.cpp, bindless_sizing_test.cpp, replay_texture_barrier_test.cpp): the base's prepare_draw and lazy compute, its #1055 release rule and its rectangle generator; #939's rectangle variants, GPU quad index expansion, 1D/3D and depth-stencil UAVs, the label fence on the base's submit. D, memory, services, presentation (xmem.cpp/.h, xg_xs.cpp, xg_xs_test.cpp, title_va_alias.h and its test, main.cpp, frame_pacer.h, xbox_present.cpp/.h; xgameruntime_offline.cpp for one comment): the base's xmem with #939's label hooks, file-backed sections and pin_mapped_data; both texture computers in xg_xs.cpp, the base's answering first; --first-run-ui kept without host speech in its profile. E, build, tools, docs (runtime/CMakeLists.txt, make_game.py, tools/rdna2spirv.py, tools/shader_disasm.py, scripts, tests, docs; docs/README.md and tests/test_exe_shaders_stage.py beyond the conflicts): the base's v_dot4 decode, #281's disassembler form and tests/semantic/test_fspx_dot4_op_sel_disasm.py dropped; the base's executable-shader step with #939's cache fix; 366 registered tests. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/file_system.cpp')
-rw-r--r--runtime/shims/file_system.cpp88
1 files changed, 63 insertions, 25 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp
index 75f5e9810..dad6f37a8 100644
--- a/runtime/shims/file_system.cpp
+++ b/runtime/shims/file_system.cpp
@@ -41,6 +41,7 @@
41#include "file_system.h" 41#include "file_system.h"
42#include "shims.h" 42#include "shims.h"
43#include "startup_trace.h" 43#include "startup_trace.h"
44#include "../src/title_page_watch.h"
44 45
45namespace rt::fs { 46namespace rt::fs {
46namespace { 47namespace {
@@ -355,6 +356,10 @@ std::vector<std::wstring> parent_of(const std::vector<std::wstring>& parts) {
355 return parts.empty() ? parts : std::vector<std::wstring>(parts.begin(), parts.end() - 1); 356 return parts.empty() ? parts : std::vector<std::wstring>(parts.begin(), parts.end() - 1);
356} 357}
357 358
359DWORD missing_error(const Roots& r, const std::vector<std::wstring>& parts) {
360 return is_folder(r, parent_of(parts)) ? ERROR_FILE_NOT_FOUND : ERROR_PATH_NOT_FOUND;
361}
362
358// Whether a folder has nothing in the title's view: no overlay entry and no package entry that is not deleted. 363// Whether a folder has nothing in the title's view: no overlay entry and no package entry that is not deleted.
359bool empty_in_view(const Roots& r, const std::vector<std::wstring>& parts) { 364bool empty_in_view(const Roots& r, const std::vector<std::wstring>& parts) {
360 std::error_code ec; 365 std::error_code ec;
@@ -701,6 +706,24 @@ void note_open_failure(const char* api, LPCWSTR name, DWORD error, const void* c
701 rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller)))); 706 rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller))));
702} 707}
703 708
709// LOCAL DIAGNOSTIC: gta6rt --trace FILE also records the first 600 successful opens ("file_open": path, access,
710// caller), so the startup trace shows which file the title was working on when it stopped making progress.
711void note_open(const char* api, LPCWSTR name, DWORD access, const void* caller) {
712 if (!rt::startup_trace::enabled()) return;
713 static std::atomic<int> left{600};
714 if (left.fetch_sub(1, std::memory_order_relaxed) <= 0) return;
715 std::string utf8;
716 if (const int n = WideCharToMultiByte(CP_UTF8, 0, name, -1, nullptr, 0, nullptr, nullptr); n > 1) {
717 utf8.resize(static_cast<size_t>(n) - 1);
718 WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8.data(), n, nullptr, nullptr);
719 }
720 rt::startup_trace::event(
721 "file_open",
722 std::string("\"api\":\"") + api + "\",\"path\":" + rt::startup_trace::quote(utf8) + ",\"access\":" +
723 std::to_string(access) + ",\"caller\":" +
724 rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller))));
725}
726
704// Reads one byte of every 4 KiB page of [buffer, buffer + size) from user mode, so that pages committed on first 727// Reads one byte of every 4 KiB page of [buffer, buffer + size) from user mode, so that pages committed on first
705// touch (xmem's physical-page views: a reserve-only section committed by its vectored handler) are committed before 728// touch (xmem's physical-page views: a reserve-only section committed by its vectored handler) are committed before
706// the kernel writes into them. A page that is not valid at all is left for the caller's ReadFile to refuse. No C++ 729// the kernel writes into them. A page that is not valid at all is left for the caller's ReadFile to refuse. No C++
@@ -721,21 +744,6 @@ bool touch_pages(void* buffer, DWORD size) {
721 return all; 744 return all;
722} 745}
723 746
724// ReadFile with one retry for a destination the kernel cannot write yet. The retail title reads into title memory
725// that xmem maps from physical pages committed on first touch; the kernel's write raises no user-mode fault, so
726// ReadFile fails with ERROR_NOACCESS (998), and the title's read loop sleeps 1 ms and retries forever (measured on
727// 1.0.2.1, native Windows). Touching the pages from user mode commits them; the read is then retried once.
728BOOL WINAPI read_file(HANDLE file, LPVOID buffer, DWORD size, LPDWORD read, LPOVERLAPPED overlapped) {
729 if (ReadFile(file, buffer, size, read, overlapped)) return TRUE;
730 if (GetLastError() != ERROR_NOACCESS || !buffer || !size) return FALSE;
731 static std::atomic<bool> logged{false};
732 if (!logged.exchange(true))
733 std::fprintf(stderr, "[rt] fs: ReadFile into uncommitted title memory (ERROR_NOACCESS): committing the "
734 "destination pages and retrying (logged once)\n");
735 touch_pages(buffer, size);
736 return ReadFile(file, buffer, size, read, overlapped);
737}
738
739HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attributes, 747HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attributes,
740 DWORD disposition, DWORD flags, HANDLE template_file) { 748 DWORD disposition, DWORD flags, HANDLE template_file) {
741 if (!name) { 749 if (!name) {
@@ -753,6 +761,8 @@ HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_
753 const DWORD error = GetLastError(); 761 const DWORD error = GetLastError();
754 note_open_failure("CreateFileW", name, error, _ReturnAddress()); 762 note_open_failure("CreateFileW", name, error, _ReturnAddress());
755 SetLastError(error); 763 SetLastError(error);
764 } else {
765 note_open("CreateFileW", name, access, _ReturnAddress());
756 } 766 }
757 return h; 767 return h;
758} 768}
@@ -784,6 +794,8 @@ HANDLE WINAPI create_file2(LPCWSTR name, DWORD access, DWORD share, DWORD dispos
784 const DWORD error = GetLastError(); 794 const DWORD error = GetLastError();
785 note_open_failure("CreateFile2", name, error, _ReturnAddress()); 795 note_open_failure("CreateFile2", name, error, _ReturnAddress());
786 SetLastError(error); 796 SetLastError(error);
797 } else {
798 note_open("CreateFile2", name, access, _ReturnAddress());
787 } 799 }
788 return h; 800 return h;
789} 801}
@@ -1024,10 +1036,7 @@ BOOL WINAPI delete_file_w(LPCWSTR name) {
1024 removed = true; 1036 removed = true;
1025 } 1037 }
1026 } 1038 }
1027 if (!removed) { 1039 if (!removed) return fail_bool(missing_error(r, parts));
1028 SetLastError(ERROR_FILE_NOT_FOUND);
1029 return FALSE;
1030 }
1031 return TRUE; 1040 return TRUE;
1032} 1041}
1033 1042
@@ -1116,7 +1125,7 @@ BOOL WINAPI remove_directory_w(LPCWSTR name) {
1116 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); 1125 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
1117 if (drive_root(name, parts)) return fail_bool(ERROR_ACCESS_DENIED); // "GAME:" and "T:" themselves stay 1126 if (drive_root(name, parts)) return fail_bool(ERROR_ACCESS_DENIED); // "GAME:" and "T:" themselves stay
1118 const View v = view(r, parts); 1127 const View v = view(r, parts);
1119 if (v.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND); 1128 if (v.kind == Kind::None) return fail_bool(missing_error(r, parts));
1120 if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY); 1129 if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY);
1121 if (!empty_in_view(r, parts)) return fail_bool(ERROR_DIR_NOT_EMPTY); 1130 if (!empty_in_view(r, parts)) return fail_bool(ERROR_DIR_NOT_EMPTY);
1122 1131
@@ -1153,7 +1162,7 @@ BOOL WINAPI move_file_ex_w(LPCWSTR from, LPCWSTR to, DWORD flags) {
1153 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); 1162 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
1154 if (drive_root(from, src) || drive_root(to, dst)) return fail_bool(ERROR_ACCESS_DENIED); 1163 if (drive_root(from, src) || drive_root(to, dst)) return fail_bool(ERROR_ACCESS_DENIED);
1155 const View from_view = view(r, src); 1164 const View from_view = view(r, src);
1156 if (from_view.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND); 1165 if (from_view.kind == Kind::None) return fail_bool(missing_error(r, src));
1157 const std::filesystem::path overlay_from = disk_path(r.overlay, src), overlay_to = disk_path(r.overlay, dst); 1166 const std::filesystem::path overlay_from = disk_path(r.overlay, src), overlay_to = disk_path(r.overlay, dst);
1158 if (same_entry(src, dst)) { 1167 if (same_entry(src, dst)) {
1159 // The destination is the source, so it is not "already there". The host succeeds at a move onto the same 1168 // The destination is the source, so it is not "already there". The host succeeds at a move onto the same
@@ -1404,7 +1413,7 @@ BOOL WINAPI set_file_attributes_w(LPCWSTR name, DWORD attributes) {
1404 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); 1413 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
1405 if (parts.empty()) return fail_bool(ERROR_ACCESS_DENIED); 1414 if (parts.empty()) return fail_bool(ERROR_ACCESS_DENIED);
1406 const View v = view(r, parts); 1415 const View v = view(r, parts);
1407 if (v.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND); 1416 if (v.kind == Kind::None) return fail_bool(missing_error(r, parts));
1408 1417
1409 // The attributes belong to the overlay copy: a package file is copied up and a package folder mirrored first, 1418 // The attributes belong to the overlay copy: a package file is copied up and a package folder mirrored first,
1410 // so the package keeps its own attributes and reads, which prefer the overlay, see the new ones. 1419 // so the package keeps its own attributes and reads, which prefer the overlay, see the new ones.
@@ -1433,14 +1442,40 @@ BOOL WINAPI set_file_attributes_a(LPCSTR name, DWORD attributes) {
1433namespace { 1442namespace {
1434using namespace rt::fs; 1443using namespace rt::fs;
1435 1444
1445// Reads into title memory. The D3D12 layer keeps some of the title's pages read-only between submissions to see
1446// which ones the title writes (slot-133 shadow buffers); the kernel fails a read into such a page with
1447// ERROR_NOACCESS instead of faulting, so the destination is pinned and opened first (title_page_watch.h).
1448//
1449// One more retry for a destination the kernel still cannot write. The retail title reads into title memory that
1450// xmem maps from physical pages committed on first touch; the kernel's write raises no user-mode fault, so ReadFile
1451// fails with ERROR_NOACCESS (998), and the title's read loop sleeps 1 ms and retries forever (measured on 1.0.2.1,
1452// native Windows). Touching the pages from user mode commits them; the read is then retried once.
1453BOOL WINAPI read_file(HANDLE file, LPVOID buffer, DWORD bytes, LPDWORD read, LPOVERLAPPED overlapped) {
1454 if (leonida::kernel_writes::read_file(file, buffer, bytes, read, overlapped)) return TRUE;
1455 if (GetLastError() != ERROR_NOACCESS || !buffer || !bytes) return FALSE;
1456 static std::atomic<bool> logged{false};
1457 if (!logged.exchange(true))
1458 std::fprintf(stderr, "[rt] fs: ReadFile into uncommitted title memory (ERROR_NOACCESS): committing the "
1459 "destination pages and retrying (logged once)\n");
1460 touch_pages(buffer, bytes);
1461 return leonida::kernel_writes::read_file(file, buffer, bytes, read, overlapped);
1462}
1463BOOL WINAPI read_file_ex(HANDLE file, LPVOID buffer, DWORD bytes, LPOVERLAPPED overlapped,
1464 LPOVERLAPPED_COMPLETION_ROUTINE routine) {
1465 return leonida::kernel_writes::read_file_ex(file, buffer, bytes, overlapped, routine);
1466}
1467BOOL WINAPI read_file_scatter(HANDLE file, FILE_SEGMENT_ELEMENT segments[], DWORD bytes, LPDWORD reserved,
1468 LPOVERLAPPED overlapped) {
1469 return leonida::kernel_writes::read_file_scatter(file, segments, bytes, reserved, overlapped);
1470}
1471
1436// CreateFile, the FindFirstFile family, the attribute queries and DeleteFile arrive through kernel32/kernelbase 1472// CreateFile, the FindFirstFile family, the attribute queries and DeleteFile arrive through kernel32/kernelbase
1437// imports and through the api sets a GDK title is linked against (file-l1-1-0 for the find and attribute calls, 1473// imports and through the api sets a GDK title is linked against (file-l1-1-0 for the find and attribute calls,
1438// file-l1-2-0 and file-l2-1-0 for CreateFile). 1474// file-l1-2-0 and file-l2-1-0 for CreateFile; ReadFile and its variants are in file-l1-1-0).
1439#define FILE_API(dll) \ 1475#define FILE_API(dll) \
1440 {dll, "CreateFileA", reinterpret_cast<void*>(&create_file_a)}, \ 1476 {dll, "CreateFileA", reinterpret_cast<void*>(&create_file_a)}, \
1441 {dll, "CreateFileW", reinterpret_cast<void*>(&create_file_w)}, \ 1477 {dll, "CreateFileW", reinterpret_cast<void*>(&create_file_w)}, \
1442 {dll, "CreateFile2", reinterpret_cast<void*>(&create_file2)}, \ 1478 {dll, "CreateFile2", reinterpret_cast<void*>(&create_file2)}, \
1443 {dll, "ReadFile", reinterpret_cast<void*>(&read_file)}, \
1444 {dll, "FindFirstFileA", reinterpret_cast<void*>(&find_first_file_a)}, \ 1479 {dll, "FindFirstFileA", reinterpret_cast<void*>(&find_first_file_a)}, \
1445 {dll, "FindFirstFileW", reinterpret_cast<void*>(&find_first_file_w)}, \ 1480 {dll, "FindFirstFileW", reinterpret_cast<void*>(&find_first_file_w)}, \
1446 {dll, "FindFirstFileExA", reinterpret_cast<void*>(&find_first_file_ex_a)}, \ 1481 {dll, "FindFirstFileExA", reinterpret_cast<void*>(&find_first_file_ex_a)}, \
@@ -1470,7 +1505,10 @@ using namespace rt::fs;
1470 {dll, "CopyFileExW", reinterpret_cast<void*>(&copy_file_ex_w)}, \ 1505 {dll, "CopyFileExW", reinterpret_cast<void*>(&copy_file_ex_w)}, \
1471 {dll, "CopyFile2", reinterpret_cast<void*>(&copy_file2)}, \ 1506 {dll, "CopyFile2", reinterpret_cast<void*>(&copy_file2)}, \
1472 {dll, "SetFileAttributesA", reinterpret_cast<void*>(&set_file_attributes_a)}, \ 1507 {dll, "SetFileAttributesA", reinterpret_cast<void*>(&set_file_attributes_a)}, \
1473 {dll, "SetFileAttributesW", reinterpret_cast<void*>(&set_file_attributes_w)} 1508 {dll, "SetFileAttributesW", reinterpret_cast<void*>(&set_file_attributes_w)}, \
1509 {dll, "ReadFile", reinterpret_cast<void*>(&read_file)}, \
1510 {dll, "ReadFileEx", reinterpret_cast<void*>(&read_file_ex)}, \
1511 {dll, "ReadFileScatter", reinterpret_cast<void*>(&read_file_scatter)}
1474} // namespace 1512} // namespace
1475 1513
1476SHIM_TABLE(file_system, 1514SHIM_TABLE(file_system,