diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 16:38:20 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 16:38:20 -0500 |
| commit | c116c3fa28983444cd47feea0461e1e7eaac83b5 (patch) | |
| tree | f2f4821b744b678f0cf1ce98638e981ea737b0c6 /runtime/shims/file_system.cpp | |
| parent | 5866d6974e86fc2d39e6ce0691af63b354f6c8b9 (diff) | |
| parent | 24d8af66af151f024b0b520fb397d899924f5486 (diff) | |
Merge main into t-drive-overlay
docs/boot.md: main added LEONIDA_WRITE_DIR / LEONIDA_SAVE_DIR to the
--overlay bullet and a new "Under Wine on Linux" section; keep both and
the T: paragraph of this branch after main's sentences.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/file_system.cpp')
| -rw-r--r-- | runtime/shims/file_system.cpp | 73 |
1 files changed, 55 insertions, 18 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index dd7fd77c9..b3cc6b219 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp | |||
| @@ -43,6 +43,7 @@ | |||
| 43 | #include "file_system.h" | 43 | #include "file_system.h" |
| 44 | #include "shims.h" | 44 | #include "shims.h" |
| 45 | #include "startup_trace.h" | 45 | #include "startup_trace.h" |
| 46 | #include "../src/title_page_watch.h" | ||
| 46 | 47 | ||
| 47 | namespace rt::fs { | 48 | namespace rt::fs { |
| 48 | namespace { | 49 | namespace { |
| @@ -798,6 +799,24 @@ void note_open_failure(const char* api, LPCWSTR name, DWORD error, const void* c | |||
| 798 | rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller)))); | 799 | rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller)))); |
| 799 | } | 800 | } |
| 800 | 801 | ||
| 802 | // LOCAL DIAGNOSTIC: gta6rt --trace FILE also records the first 600 successful opens ("file_open": path, access, | ||
| 803 | // caller), so the startup trace shows which file the title was working on when it stopped making progress. | ||
| 804 | void note_open(const char* api, LPCWSTR name, DWORD access, const void* caller) { | ||
| 805 | if (!rt::startup_trace::enabled()) return; | ||
| 806 | static std::atomic<int> left{600}; | ||
| 807 | if (left.fetch_sub(1, std::memory_order_relaxed) <= 0) return; | ||
| 808 | std::string utf8; | ||
| 809 | if (const int n = WideCharToMultiByte(CP_UTF8, 0, name, -1, nullptr, 0, nullptr, nullptr); n > 1) { | ||
| 810 | utf8.resize(static_cast<size_t>(n) - 1); | ||
| 811 | WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8.data(), n, nullptr, nullptr); | ||
| 812 | } | ||
| 813 | rt::startup_trace::event( | ||
| 814 | "file_open", | ||
| 815 | std::string("\"api\":\"") + api + "\",\"path\":" + rt::startup_trace::quote(utf8) + ",\"access\":" + | ||
| 816 | std::to_string(access) + ",\"caller\":" + | ||
| 817 | rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller)))); | ||
| 818 | } | ||
| 819 | |||
| 801 | // Reads one byte of every 4 KiB page of [buffer, buffer + size) from user mode, so that pages committed on first | 820 | // Reads one byte of every 4 KiB page of [buffer, buffer + size) from user mode, so that pages committed on first |
| 802 | // touch (xmem's physical-page views: a reserve-only section committed by its vectored handler) are committed before | 821 | // touch (xmem's physical-page views: a reserve-only section committed by its vectored handler) are committed before |
| 803 | // the kernel writes into them. A page that is not valid at all is left for the caller's ReadFile to refuse. No C++ | 822 | // the kernel writes into them. A page that is not valid at all is left for the caller's ReadFile to refuse. No C++ |
| @@ -818,21 +837,6 @@ bool touch_pages(void* buffer, DWORD size) { | |||
| 818 | return all; | 837 | return all; |
| 819 | } | 838 | } |
| 820 | 839 | ||
| 821 | // ReadFile with one retry for a destination the kernel cannot write yet. The retail title reads into title memory | ||
| 822 | // that xmem maps from physical pages committed on first touch; the kernel's write raises no user-mode fault, so | ||
| 823 | // ReadFile fails with ERROR_NOACCESS (998), and the title's read loop sleeps 1 ms and retries forever (measured on | ||
| 824 | // 1.0.2.1, native Windows). Touching the pages from user mode commits them; the read is then retried once. | ||
| 825 | BOOL WINAPI read_file(HANDLE file, LPVOID buffer, DWORD size, LPDWORD read, LPOVERLAPPED overlapped) { | ||
| 826 | if (ReadFile(file, buffer, size, read, overlapped)) return TRUE; | ||
| 827 | if (GetLastError() != ERROR_NOACCESS || !buffer || !size) return FALSE; | ||
| 828 | static std::atomic<bool> logged{false}; | ||
| 829 | if (!logged.exchange(true)) | ||
| 830 | std::fprintf(stderr, "[rt] fs: ReadFile into uncommitted title memory (ERROR_NOACCESS): committing the " | ||
| 831 | "destination pages and retrying (logged once)\n"); | ||
| 832 | touch_pages(buffer, size); | ||
| 833 | return ReadFile(file, buffer, size, read, overlapped); | ||
| 834 | } | ||
| 835 | |||
| 836 | HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attributes, | 840 | HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attributes, |
| 837 | DWORD disposition, DWORD flags, HANDLE template_file) { | 841 | DWORD disposition, DWORD flags, HANDLE template_file) { |
| 838 | if (!name) { | 842 | if (!name) { |
| @@ -850,6 +854,8 @@ HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_ | |||
| 850 | const DWORD error = GetLastError(); | 854 | const DWORD error = GetLastError(); |
| 851 | note_open_failure("CreateFileW", name, error, _ReturnAddress()); | 855 | note_open_failure("CreateFileW", name, error, _ReturnAddress()); |
| 852 | SetLastError(error); | 856 | SetLastError(error); |
| 857 | } else { | ||
| 858 | note_open("CreateFileW", name, access, _ReturnAddress()); | ||
| 853 | } | 859 | } |
| 854 | return h; | 860 | return h; |
| 855 | } | 861 | } |
| @@ -881,6 +887,8 @@ HANDLE WINAPI create_file2(LPCWSTR name, DWORD access, DWORD share, DWORD dispos | |||
| 881 | const DWORD error = GetLastError(); | 887 | const DWORD error = GetLastError(); |
| 882 | note_open_failure("CreateFile2", name, error, _ReturnAddress()); | 888 | note_open_failure("CreateFile2", name, error, _ReturnAddress()); |
| 883 | SetLastError(error); | 889 | SetLastError(error); |
| 890 | } else { | ||
| 891 | note_open("CreateFile2", name, access, _ReturnAddress()); | ||
| 884 | } | 892 | } |
| 885 | return h; | 893 | return h; |
| 886 | } | 894 | } |
| @@ -1527,14 +1535,40 @@ BOOL WINAPI set_file_attributes_a(LPCSTR name, DWORD attributes) { | |||
| 1527 | namespace { | 1535 | namespace { |
| 1528 | using namespace rt::fs; | 1536 | using namespace rt::fs; |
| 1529 | 1537 | ||
| 1538 | // Reads into title memory. The D3D12 layer keeps some of the title's pages read-only between submissions to see | ||
| 1539 | // which ones the title writes (slot-133 shadow buffers); the kernel fails a read into such a page with | ||
| 1540 | // ERROR_NOACCESS instead of faulting, so the destination is pinned and opened first (title_page_watch.h). | ||
| 1541 | // | ||
| 1542 | // One more retry for a destination the kernel still cannot write. The retail title reads into title memory that | ||
| 1543 | // xmem maps from physical pages committed on first touch; the kernel's write raises no user-mode fault, so ReadFile | ||
| 1544 | // fails with ERROR_NOACCESS (998), and the title's read loop sleeps 1 ms and retries forever (measured on 1.0.2.1, | ||
| 1545 | // native Windows). Touching the pages from user mode commits them; the read is then retried once. | ||
| 1546 | BOOL WINAPI read_file(HANDLE file, LPVOID buffer, DWORD bytes, LPDWORD read, LPOVERLAPPED overlapped) { | ||
| 1547 | if (leonida::kernel_writes::read_file(file, buffer, bytes, read, overlapped)) return TRUE; | ||
| 1548 | if (GetLastError() != ERROR_NOACCESS || !buffer || !bytes) return FALSE; | ||
| 1549 | static std::atomic<bool> logged{false}; | ||
| 1550 | if (!logged.exchange(true)) | ||
| 1551 | std::fprintf(stderr, "[rt] fs: ReadFile into uncommitted title memory (ERROR_NOACCESS): committing the " | ||
| 1552 | "destination pages and retrying (logged once)\n"); | ||
| 1553 | touch_pages(buffer, bytes); | ||
| 1554 | return leonida::kernel_writes::read_file(file, buffer, bytes, read, overlapped); | ||
| 1555 | } | ||
| 1556 | BOOL WINAPI read_file_ex(HANDLE file, LPVOID buffer, DWORD bytes, LPOVERLAPPED overlapped, | ||
| 1557 | LPOVERLAPPED_COMPLETION_ROUTINE routine) { | ||
| 1558 | return leonida::kernel_writes::read_file_ex(file, buffer, bytes, overlapped, routine); | ||
| 1559 | } | ||
| 1560 | BOOL WINAPI read_file_scatter(HANDLE file, FILE_SEGMENT_ELEMENT segments[], DWORD bytes, LPDWORD reserved, | ||
| 1561 | LPOVERLAPPED overlapped) { | ||
| 1562 | return leonida::kernel_writes::read_file_scatter(file, segments, bytes, reserved, overlapped); | ||
| 1563 | } | ||
| 1564 | |||
| 1530 | // CreateFile, the FindFirstFile family, the attribute queries and DeleteFile arrive through kernel32/kernelbase | 1565 | // CreateFile, the FindFirstFile family, the attribute queries and DeleteFile arrive through kernel32/kernelbase |
| 1531 | // imports and through the api sets a GDK title is linked against (file-l1-1-0 for the find and attribute calls, | 1566 | // imports and through the api sets a GDK title is linked against (file-l1-1-0 for the find and attribute calls, |
| 1532 | // file-l1-2-0 and file-l2-1-0 for CreateFile). | 1567 | // file-l1-2-0 and file-l2-1-0 for CreateFile; ReadFile and its variants are in file-l1-1-0). |
| 1533 | #define FILE_API(dll) \ | 1568 | #define FILE_API(dll) \ |
| 1534 | {dll, "CreateFileA", reinterpret_cast<void*>(&create_file_a)}, \ | 1569 | {dll, "CreateFileA", reinterpret_cast<void*>(&create_file_a)}, \ |
| 1535 | {dll, "CreateFileW", reinterpret_cast<void*>(&create_file_w)}, \ | 1570 | {dll, "CreateFileW", reinterpret_cast<void*>(&create_file_w)}, \ |
| 1536 | {dll, "CreateFile2", reinterpret_cast<void*>(&create_file2)}, \ | 1571 | {dll, "CreateFile2", reinterpret_cast<void*>(&create_file2)}, \ |
| 1537 | {dll, "ReadFile", reinterpret_cast<void*>(&read_file)}, \ | ||
| 1538 | {dll, "FindFirstFileA", reinterpret_cast<void*>(&find_first_file_a)}, \ | 1572 | {dll, "FindFirstFileA", reinterpret_cast<void*>(&find_first_file_a)}, \ |
| 1539 | {dll, "FindFirstFileW", reinterpret_cast<void*>(&find_first_file_w)}, \ | 1573 | {dll, "FindFirstFileW", reinterpret_cast<void*>(&find_first_file_w)}, \ |
| 1540 | {dll, "FindFirstFileExA", reinterpret_cast<void*>(&find_first_file_ex_a)}, \ | 1574 | {dll, "FindFirstFileExA", reinterpret_cast<void*>(&find_first_file_ex_a)}, \ |
| @@ -1564,7 +1598,10 @@ using namespace rt::fs; | |||
| 1564 | {dll, "CopyFileExW", reinterpret_cast<void*>(©_file_ex_w)}, \ | 1598 | {dll, "CopyFileExW", reinterpret_cast<void*>(©_file_ex_w)}, \ |
| 1565 | {dll, "CopyFile2", reinterpret_cast<void*>(©_file2)}, \ | 1599 | {dll, "CopyFile2", reinterpret_cast<void*>(©_file2)}, \ |
| 1566 | {dll, "SetFileAttributesA", reinterpret_cast<void*>(&set_file_attributes_a)}, \ | 1600 | {dll, "SetFileAttributesA", reinterpret_cast<void*>(&set_file_attributes_a)}, \ |
| 1567 | {dll, "SetFileAttributesW", reinterpret_cast<void*>(&set_file_attributes_w)} | 1601 | {dll, "SetFileAttributesW", reinterpret_cast<void*>(&set_file_attributes_w)}, \ |
| 1602 | {dll, "ReadFile", reinterpret_cast<void*>(&read_file)}, \ | ||
| 1603 | {dll, "ReadFileEx", reinterpret_cast<void*>(&read_file_ex)}, \ | ||
| 1604 | {dll, "ReadFileScatter", reinterpret_cast<void*>(&read_file_scatter)} | ||
| 1568 | } // namespace | 1605 | } // namespace |
| 1569 | 1606 | ||
| 1570 | SHIM_TABLE(file_system, | 1607 | SHIM_TABLE(file_system, |