From 5eca82a6f2049bc10843e9bfaeb5e8963fc071de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Tue, 16 Feb 2021 12:30:29 +0100 Subject: [PATCH] IOS/ES: Allow various utility functions to return timing info --- Source/Core/Core/IOS/ES/ES.cpp | 4 +- Source/Core/Core/IOS/ES/ES.h | 10 ++--- Source/Core/Core/IOS/ES/Formats.cpp | 25 +++++++----- Source/Core/Core/IOS/ES/Formats.h | 12 +++++- Source/Core/Core/IOS/ES/NandUtils.cpp | 45 ++++++++++----------- Source/Core/Core/IOS/ES/TitleContents.cpp | 2 +- Source/Core/Core/IOS/ES/TitleManagement.cpp | 6 +-- Source/Core/Core/IOS/FS/FileSystemProxy.cpp | 33 ++++++++++----- Source/Core/Core/IOS/FS/FileSystemProxy.h | 9 +++++ Source/Core/Core/IOS/IOS.cpp | 5 +++ Source/Core/Core/IOS/IOS.h | 2 + 11 files changed, 98 insertions(+), 55 deletions(-) diff --git a/Source/Core/Core/IOS/ES/ES.cpp b/Source/Core/Core/IOS/ES/ES.cpp index 8be562c84b..5ce32591c8 100644 --- a/Source/Core/Core/IOS/ES/ES.cpp +++ b/Source/Core/Core/IOS/ES/ES.cpp @@ -161,7 +161,7 @@ IPCReply ESDevice::GetTitleId(const IOCtlVRequest& request) static bool UpdateUIDAndGID(Kernel& kernel, const ES::TMDReader& tmd) { - ES::UIDSys uid_sys{kernel.GetFS()}; + ES::UIDSys uid_sys{kernel.GetFSDevice()}; const u64 title_id = tmd.GetTitleId(); const u32 uid = uid_sys.GetOrInsertUIDForTitle(title_id); if (uid == 0) @@ -177,7 +177,7 @@ static bool UpdateUIDAndGID(Kernel& kernel, const ES::TMDReader& tmd) static ReturnCode CheckIsAllowedToSetUID(Kernel& kernel, const u32 caller_uid, const ES::TMDReader& active_tmd) { - ES::UIDSys uid_map{kernel.GetFS()}; + ES::UIDSys uid_map{kernel.GetFSDevice()}; const u32 system_menu_uid = uid_map.GetOrInsertUIDForTitle(Titles::SYSTEM_MENU); if (!system_menu_uid) return ES_SHORT_READ; diff --git a/Source/Core/Core/IOS/ES/ES.h b/Source/Core/Core/IOS/ES/ES.h index c4636cab17..4e2d0fa8d9 100644 --- a/Source/Core/Core/IOS/ES/ES.h +++ b/Source/Core/Core/IOS/ES/ES.h @@ -87,8 +87,8 @@ public: No = false, }; - ES::TMDReader FindImportTMD(u64 title_id) const; - ES::TMDReader FindInstalledTMD(u64 title_id) const; + ES::TMDReader FindImportTMD(u64 title_id, Ticks ticks = {}) const; + ES::TMDReader FindInstalledTMD(u64 title_id, Ticks ticks = {}) const; ES::TicketReader FindSignedTicket(u64 title_id) const; // Get installed titles (in /title) without checking for TMDs at all. @@ -364,9 +364,9 @@ private: void FinishStaleImport(u64 title_id); void FinishAllStaleImports(); - std::string GetContentPath(u64 title_id, const ES::Content& content, - const ES::SharedContentMap& map) const; - std::string GetContentPath(u64 title_id, const ES::Content& content) const; + std::string GetContentPath(u64 title_id, const ES::Content& content, Ticks ticks = {}) const; + + static constexpr u64 IPC_OVERHEAD_TICKS = 2700_tbticks; struct OpenedContent { diff --git a/Source/Core/Core/IOS/ES/Formats.cpp b/Source/Core/Core/IOS/ES/Formats.cpp index fcda458126..441747a7d9 100644 --- a/Source/Core/Core/IOS/ES/Formats.cpp +++ b/Source/Core/Core/IOS/ES/Formats.cpp @@ -27,6 +27,7 @@ #include "Core/CommonTitles.h" #include "Core/IOS/Device.h" #include "Core/IOS/FS/FileSystem.h" +#include "Core/IOS/FS/FileSystemProxy.h" #include "Core/IOS/IOS.h" #include "Core/IOS/IOSC.h" #include "Core/IOS/Uids.h" @@ -521,17 +522,21 @@ struct SharedContentMap::Entry }; constexpr char CONTENT_MAP_PATH[] = "/shared1/content.map"; -SharedContentMap::SharedContentMap(std::shared_ptr fs) : m_fs{fs} +SharedContentMap::SharedContentMap(std::shared_ptr fs) + : m_fs_device{fs}, m_fs{fs->GetFS()} { static_assert(sizeof(Entry) == 28, "SharedContentMap::Entry has the wrong size"); Entry entry; - const auto file = fs->OpenFile(PID_KERNEL, PID_KERNEL, CONTENT_MAP_PATH, HLE::FS::Mode::Read); - while (file && file->Read(&entry, 1)) + s64 fd = fs->Open(PID_KERNEL, PID_KERNEL, CONTENT_MAP_PATH, HLE::FS::Mode::Read, {}, &m_ticks); + if (fd < 0) + return; + while (fs->Read(fd, &entry, 1, &m_ticks) == sizeof(entry)) { m_entries.push_back(entry); m_last_id++; } + fs->Close(fd, &m_ticks); } SharedContentMap::~SharedContentMap() = default; @@ -600,32 +605,34 @@ bool SharedContentMap::WriteEntries() const HLE::FS::ResultCode::Success; } -static std::pair ReadUidSysEntry(const HLE::FS::FileHandle& file) +static std::pair ReadUidSysEntry(HLE::FSDevice& fs, u64 fd, u64* ticks) { u64 title_id = 0; - if (!file.Read(&title_id, 1)) + if (fs.Read(fd, &title_id, 1, ticks) != sizeof(title_id)) return {}; u32 uid = 0; - if (!file.Read(&uid, 1)) + if (fs.Read(fd, &uid, 1, ticks) != sizeof(uid)) return {}; return {Common::swap32(uid), Common::swap64(title_id)}; } constexpr char UID_MAP_PATH[] = "/sys/uid.sys"; -UIDSys::UIDSys(std::shared_ptr fs) : m_fs{fs} +UIDSys::UIDSys(std::shared_ptr fs) : m_fs_device{fs}, m_fs{fs->GetFS()} { - if (const auto file = fs->OpenFile(PID_KERNEL, PID_KERNEL, UID_MAP_PATH, HLE::FS::Mode::Read)) + s64 fd = fs->Open(PID_KERNEL, PID_KERNEL, UID_MAP_PATH, HLE::FS::Mode::Read, {}, &m_ticks); + if (fd >= 0) { while (true) { - std::pair entry = ReadUidSysEntry(*file); + std::pair entry = ReadUidSysEntry(*fs, fd, &m_ticks); if (!entry.first && !entry.second) break; m_entries.insert(std::move(entry)); } + fs->Close(fd, &m_ticks); } if (m_entries.empty()) diff --git a/Source/Core/Core/IOS/ES/Formats.h b/Source/Core/Core/IOS/ES/Formats.h index 2ab6cfe6b4..1b1fe6996e 100644 --- a/Source/Core/Core/IOS/ES/Formats.h +++ b/Source/Core/Core/IOS/ES/Formats.h @@ -260,7 +260,7 @@ public: class SharedContentMap final { public: - explicit SharedContentMap(std::shared_ptr fs); + explicit SharedContentMap(std::shared_ptr fs); ~SharedContentMap(); std::optional GetFilenameFromSHA1(const std::array& sha1) const; @@ -268,27 +268,35 @@ public: bool DeleteSharedContent(const std::array& sha1); std::vector> GetHashes() const; + u64 GetTicks() const { return m_ticks; } + private: bool WriteEntries() const; struct Entry; u32 m_last_id = 0; std::vector m_entries; + std::shared_ptr m_fs_device; std::shared_ptr m_fs; + u64 m_ticks = 0; }; class UIDSys final { public: - explicit UIDSys(std::shared_ptr fs); + explicit UIDSys(std::shared_ptr fs); u32 GetUIDFromTitle(u64 title_id) const; u32 GetOrInsertUIDForTitle(u64 title_id); u32 GetNextUID() const; + u64 GetTicks() const { return m_ticks; } + private: + std::shared_ptr m_fs_device; std::shared_ptr m_fs; std::map m_entries; + u64 m_ticks = 0; }; class CertReader final : public SignedBlobReader diff --git a/Source/Core/Core/IOS/ES/NandUtils.cpp b/Source/Core/Core/IOS/ES/NandUtils.cpp index 44fb46f471..88cc040552 100644 --- a/Source/Core/Core/IOS/ES/NandUtils.cpp +++ b/Source/Core/Core/IOS/ES/NandUtils.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -17,35 +16,38 @@ #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" #include "Common/NandPaths.h" +#include "Common/ScopeGuard.h" #include "Common/StringUtil.h" #include "Core/IOS/ES/ES.h" #include "Core/IOS/ES/Formats.h" +#include "Core/IOS/FS/FileSystemProxy.h" #include "Core/IOS/Uids.h" namespace IOS::HLE { -static ES::TMDReader FindTMD(FS::FileSystem* fs, u64 title_id, const std::string& tmd_path) +static ES::TMDReader FindTMD(FSDevice& fs, const std::string& tmd_path, Ticks ticks) { - const auto file = fs->OpenFile(PID_KERNEL, PID_KERNEL, tmd_path, FS::Mode::Read); - if (!file) + const s64 fd = fs.Open(PID_KERNEL, PID_KERNEL, tmd_path, FS::Mode::Read, {}, ticks); + if (fd < 0) return {}; + Common::ScopeGuard guard{[&] { fs.Close(fd, ticks); }}; - std::vector tmd_bytes(file->GetStatus()->size); - if (!file->Read(tmd_bytes.data(), tmd_bytes.size())) + std::vector tmd_bytes(fs.GetFileStatus(fd, ticks)->size); + if (!fs.Read(fd, tmd_bytes.data(), tmd_bytes.size(), ticks)) return {}; return ES::TMDReader{std::move(tmd_bytes)}; } -ES::TMDReader ESDevice::FindImportTMD(u64 title_id) const +ES::TMDReader ESDevice::FindImportTMD(u64 title_id, Ticks ticks) const { - return FindTMD(m_ios.GetFS().get(), title_id, - Common::GetImportTitlePath(title_id) + "/content/title.tmd"); + return FindTMD(*m_ios.GetFSDevice(), Common::GetImportTitlePath(title_id) + "/content/title.tmd", + ticks); } -ES::TMDReader ESDevice::FindInstalledTMD(u64 title_id) const +ES::TMDReader ESDevice::FindInstalledTMD(u64 title_id, Ticks ticks) const { - return FindTMD(m_ios.GetFS().get(), title_id, Common::GetTMDFileName(title_id)); + return FindTMD(*m_ios.GetFSDevice(), Common::GetTMDFileName(title_id), ticks); } ES::TicketReader ESDevice::FindSignedTicket(u64 title_id) const @@ -171,16 +173,15 @@ ESDevice::GetStoredContentsFromTMD(const ES::TMDReader& tmd, if (!tmd.IsValid()) return {}; - const ES::SharedContentMap map{m_ios.GetFS()}; const std::vector contents = tmd.GetContents(); std::vector stored_contents; std::copy_if(contents.begin(), contents.end(), std::back_inserter(stored_contents), - [this, &tmd, &map, check_content_hashes](const ES::Content& content) { + [this, &tmd, check_content_hashes](const ES::Content& content) { const auto fs = m_ios.GetFS(); - const std::string path = GetContentPath(tmd.GetTitleId(), content, map); + const std::string path = GetContentPath(tmd.GetTitleId(), content); if (path.empty()) return false; @@ -217,7 +218,7 @@ u32 ESDevice::GetSharedContentsCount() const std::vector> ESDevice::GetSharedContents() const { - const ES::SharedContentMap map{m_ios.GetFS()}; + const ES::SharedContentMap map{m_ios.GetFSDevice()}; return map.GetHashes(); } @@ -267,7 +268,7 @@ bool ESDevice::CreateTitleDirectories(u64 title_id, u16 group_id) const return false; } - ES::UIDSys uid_sys{fs}; + ES::UIDSys uid_sys{m_ios.GetFSDevice()}; const u32 uid = uid_sys.GetOrInsertUIDForTitle(title_id); if (fs->SetMetadata(0, data_dir, uid, group_id, 0, data_dir_modes) != FS::ResultCode::Success) { @@ -382,16 +383,14 @@ void ESDevice::FinishAllStaleImports() } std::string ESDevice::GetContentPath(const u64 title_id, const ES::Content& content, - const ES::SharedContentMap& content_map) const + Ticks ticks) const { if (content.IsShared()) + { + ES::SharedContentMap content_map{m_ios.GetFSDevice()}; + ticks.Add(content_map.GetTicks()); return content_map.GetFilenameFromSHA1(content.sha1).value_or(""); + } return fmt::format("{}/{:08x}.app", Common::GetTitleContentPath(title_id), content.id); } - -std::string ESDevice::GetContentPath(const u64 title_id, const ES::Content& content) const -{ - ES::SharedContentMap map{m_ios.GetFS()}; - return GetContentPath(title_id, content, map); -} } // namespace IOS::HLE diff --git a/Source/Core/Core/IOS/ES/TitleContents.cpp b/Source/Core/Core/IOS/ES/TitleContents.cpp index 30dd6c08c5..547df70eaf 100644 --- a/Source/Core/Core/IOS/ES/TitleContents.cpp +++ b/Source/Core/Core/IOS/ES/TitleContents.cpp @@ -78,7 +78,7 @@ IPCReply ESDevice::OpenActiveTitleContent(u32 caller_uid, const IOCtlVRequest& r if (!m_title_context.active) return IPCReply(ES_EINVAL); - ES::UIDSys uid_map{m_ios.GetFS()}; + ES::UIDSys uid_map{m_ios.GetFSDevice()}; const u32 uid = uid_map.GetOrInsertUIDForTitle(m_title_context.tmd.GetTitleId()); if (caller_uid != 0 && caller_uid != uid) return IPCReply(ES_EACCES); diff --git a/Source/Core/Core/IOS/ES/TitleManagement.cpp b/Source/Core/Core/IOS/ES/TitleManagement.cpp index 09b543f1db..df9c6ec9c2 100644 --- a/Source/Core/Core/IOS/ES/TitleManagement.cpp +++ b/Source/Core/Core/IOS/ES/TitleManagement.cpp @@ -394,7 +394,7 @@ ReturnCode ESDevice::ImportContentEnd(Context& context, u32 content_fd) std::string content_path; if (content_info.IsShared()) { - ES::SharedContentMap shared_content{fs}; + ES::SharedContentMap shared_content{m_ios.GetFSDevice()}; content_path = shared_content.AddSharedContent(content_info.sha1); } else @@ -441,7 +441,7 @@ static bool HasAllRequiredContents(Kernel& ios, const ES::TMDReader& tmd) { const u64 title_id = tmd.GetTitleId(); const std::vector contents = tmd.GetContents(); - const ES::SharedContentMap shared_content_map{ios.GetFS()}; + const ES::SharedContentMap shared_content_map{ios.GetFSDevice()}; return std::all_of(contents.cbegin(), contents.cend(), [&](const ES::Content& content) { if (content.IsOptional()) return true; @@ -818,7 +818,7 @@ IPCReply ESDevice::ExportTitleDone(Context& context, const IOCtlVRequest& reques ReturnCode ESDevice::DeleteSharedContent(const std::array& sha1) const { - ES::SharedContentMap map{m_ios.GetFS()}; + ES::SharedContentMap map{m_ios.GetFSDevice()}; const auto content_path = map.GetFilenameFromSHA1(sha1); if (!content_path) return ES_EINVAL; diff --git a/Source/Core/Core/IOS/FS/FileSystemProxy.cpp b/Source/Core/Core/IOS/FS/FileSystemProxy.cpp index dd7ae5ceb8..7bd33e44dd 100644 --- a/Source/Core/Core/IOS/FS/FileSystemProxy.cpp +++ b/Source/Core/Core/IOS/FS/FileSystemProxy.cpp @@ -661,19 +661,32 @@ IPCReply FSDevice::SetFileVersionControl(const Handle& handle, const IOCtlReques IPCReply FSDevice::GetFileStats(const Handle& handle, const IOCtlRequest& request) { - if (request.buffer_out_size < 8 || handle.fs_fd == INVALID_FD) + if (request.buffer_out_size < 8) return GetFSReply(ConvertResult(ResultCode::Invalid)); - const Result status = m_ios.GetFS()->GetFileStatus(handle.fs_fd); - LogResult(status, "GetFileStatus({})", handle.name.data()); - if (!status) - return IPCReply(ConvertResult(status.Error())); + return MakeIPCReply([&](Ticks ticks) { + const Result status = GetFileStatus(request.fd, ticks); + if (!status) + return ConvertResult(status.Error()); - ISFSFileStats out; - out.size = status->size; - out.seek_position = status->offset; - Memory::CopyToEmu(request.buffer_out, &out, sizeof(out)); - return IPCReply(IPC_SUCCESS); + ISFSFileStats out; + out.size = status->size; + out.seek_position = status->offset; + Memory::CopyToEmu(request.buffer_out, &out, sizeof(out)); + return IPC_SUCCESS; + }); +} + +FS::Result FSDevice::GetFileStatus(u64 fd, Ticks ticks) +{ + ticks.AddTimeBaseTicks(GetIPCOverheadTicks()); + const auto& handle = m_fd_map[fd]; + if (handle.fs_fd == INVALID_FD) + return ResultCode::Invalid; + + auto status = m_ios.GetFS()->GetFileStatus(handle.fs_fd); + LogResult(status, "GetFileStatus({})", handle.name.data()); + return status; } IPCReply FSDevice::GetUsage(const Handle& handle, const IOCtlVRequest& request) diff --git a/Source/Core/Core/IOS/FS/FileSystemProxy.h b/Source/Core/Core/IOS/FS/FileSystemProxy.h index a2a1c20428..101dbdffc2 100644 --- a/Source/Core/Core/IOS/FS/FileSystemProxy.h +++ b/Source/Core/Core/IOS/FS/FileSystemProxy.h @@ -35,6 +35,15 @@ public: s32 Write(u64 fd, const u8* data, u32 size, std::optional ipc_buffer_addr = {}, Ticks ticks = {}); s32 Seek(u64 fd, u32 offset, FS::SeekMode mode, Ticks ticks = {}); + FS::Result GetFileStatus(u64 fd, Ticks ticks = {}); + + template + s32 Read(u64 fd, T* data, size_t count, Ticks ticks = {}) + { + return Read(fd, reinterpret_cast(data), static_cast(sizeof(T) * count), {}, ticks); + } + + std::shared_ptr GetFS() const { return m_ios.GetFS(); } void DoState(PointerWrap& p) override; diff --git a/Source/Core/Core/IOS/IOS.cpp b/Source/Core/Core/IOS/IOS.cpp index db0c3593f8..50b586e343 100644 --- a/Source/Core/Core/IOS/IOS.cpp +++ b/Source/Core/Core/IOS/IOS.cpp @@ -285,6 +285,11 @@ std::shared_ptr Kernel::GetFS() return m_fs; } +std::shared_ptr Kernel::GetFSDevice() +{ + return std::static_pointer_cast(m_device_map.at("/dev/fs")); +} + std::shared_ptr Kernel::GetES() { return std::static_pointer_cast(m_device_map.at("/dev/es")); diff --git a/Source/Core/Core/IOS/IOS.h b/Source/Core/Core/IOS/IOS.h index 15bcc1cccf..11220f1194 100644 --- a/Source/Core/Core/IOS/IOS.h +++ b/Source/Core/Core/IOS/IOS.h @@ -30,6 +30,7 @@ class FileSystem; class Device; class ESDevice; +class FSDevice; struct Request; struct OpenRequest; @@ -116,6 +117,7 @@ public: // These are *always* part of the IOS kernel and always available. // They are also the only available resource managers even before loading any module. std::shared_ptr GetFS(); + std::shared_ptr GetFSDevice(); std::shared_ptr GetES(); void SDIO_EventNotify();