diff options
Diffstat (limited to 'runtime/shims/file_system.cpp')
| -rw-r--r-- | runtime/shims/file_system.cpp | 88 |
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 | ||
| 45 | namespace rt::fs { | 46 | namespace rt::fs { |
| 46 | namespace { | 47 | namespace { |
| @@ -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 | ||
| 359 | DWORD 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. |
| 359 | bool empty_in_view(const Roots& r, const std::vector<std::wstring>& parts) { | 364 | bool 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. | ||
| 711 | void 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. | ||
| 728 | BOOL 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 | |||
| 739 | HANDLE WINAPI create_file_w(LPCWSTR name, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attributes, | 747 | HANDLE 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) { | |||
| 1433 | namespace { | 1442 | namespace { |
| 1434 | using namespace rt::fs; | 1443 | using 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. | ||
| 1453 | BOOL 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 | } | ||
| 1463 | BOOL 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 | } | ||
| 1467 | BOOL 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*>(©_file_ex_w)}, \ | 1505 | {dll, "CopyFileExW", reinterpret_cast<void*>(©_file_ex_w)}, \ |
| 1471 | {dll, "CopyFile2", reinterpret_cast<void*>(©_file2)}, \ | 1506 | {dll, "CopyFile2", reinterpret_cast<void*>(©_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 | ||
| 1476 | SHIM_TABLE(file_system, | 1514 | SHIM_TABLE(file_system, |