From 69f16ba50e3c52a17405670b976ac4ba63f58021 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 6 Oct 2019 13:02:23 -0400 Subject: [PATCH] hle/service: Replace global system instance calls with instance-based ones Migrates the HLE service code off the use of directly accessing the global system instance where trivially able to do so. This removes all usages of Core::CurrentProcess from the service code, only 8 occurrences of this function exist elsewhere. There's still quite a bit of "System::GetInstance()" being used, however this was able to replace a few instances. --- src/core/core.cpp | 7 ++-- src/core/core.h | 6 ++-- src/core/file_sys/romfs_factory.cpp | 4 +-- src/core/file_sys/romfs_factory.h | 2 +- src/core/hle/service/am/am.cpp | 6 ++-- src/core/hle/service/bcat/bcat.cpp | 5 +-- src/core/hle/service/bcat/bcat.h | 8 +++-- src/core/hle/service/bcat/module.cpp | 34 +++++++++++-------- src/core/hle/service/bcat/module.h | 11 ++++-- src/core/hle/service/fatal/fatal.cpp | 2 +- .../hle/service/filesystem/filesystem.cpp | 20 +++++------ src/core/hle/service/filesystem/filesystem.h | 8 ++++- src/core/hle/service/ldr/ldr.cpp | 6 ++-- src/core/hle/service/ns/pl_u.cpp | 8 ++--- 14 files changed, 76 insertions(+), 51 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 75a7ffb973..a58ceb703d 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -111,7 +111,8 @@ FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs, } struct System::Impl { explicit Impl(System& system) - : kernel{system}, cpu_core_manager{system}, applet_manager{system}, reporter{system} {} + : kernel{system}, fs_controller{system}, cpu_core_manager{system}, + applet_manager{system}, reporter{system} {} Cpu& CurrentCpuCore() { return cpu_core_manager.GetCurrentCore(); @@ -641,11 +642,11 @@ bool System::GetExitLock() const { return impl->exit_lock; } -void System::SetCurrentProcessBuildID(std::array id) { +void System::SetCurrentProcessBuildID(const CurrentBuildProcessID& id) { impl->build_id = id; } -const std::array& System::GetCurrentProcessBuildID() const { +const System::CurrentBuildProcessID& System::GetCurrentProcessBuildID() const { return impl->build_id; } diff --git a/src/core/core.h b/src/core/core.h index f49b7fbf9f..fecfdb9594 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -98,6 +98,8 @@ FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs, class System { public: + using CurrentBuildProcessID = std::array; + System(const System&) = delete; System& operator=(const System&) = delete; @@ -330,9 +332,9 @@ public: bool GetExitLock() const; - void SetCurrentProcessBuildID(std::array id); + void SetCurrentProcessBuildID(const CurrentBuildProcessID& id); - const std::array& GetCurrentProcessBuildID() const; + const CurrentBuildProcessID& GetCurrentProcessBuildID() const; private: System(); diff --git a/src/core/file_sys/romfs_factory.cpp b/src/core/file_sys/romfs_factory.cpp index 84cd4684c5..4bd2e6183f 100644 --- a/src/core/file_sys/romfs_factory.cpp +++ b/src/core/file_sys/romfs_factory.cpp @@ -35,11 +35,11 @@ void RomFSFactory::SetPackedUpdate(VirtualFile update_raw) { this->update_raw = std::move(update_raw); } -ResultVal RomFSFactory::OpenCurrentProcess() const { +ResultVal RomFSFactory::OpenCurrentProcess(u64 current_process_title_id) const { if (!updatable) return MakeResult(file); - const PatchManager patch_manager(Core::CurrentProcess()->GetTitleID()); + const PatchManager patch_manager(current_process_title_id); return MakeResult( patch_manager.PatchRomFS(file, ivfc_offset, ContentRecordType::Program, update_raw)); } diff --git a/src/core/file_sys/romfs_factory.h b/src/core/file_sys/romfs_factory.h index da63a313ad..c5d40285c9 100644 --- a/src/core/file_sys/romfs_factory.h +++ b/src/core/file_sys/romfs_factory.h @@ -33,7 +33,7 @@ public: ~RomFSFactory(); void SetPackedUpdate(VirtualFile update_raw); - ResultVal OpenCurrentProcess() const; + ResultVal OpenCurrentProcess(u64 current_process_title_id) const; ResultVal Open(u64 title_id, StorageId storage, ContentRecordType type) const; private: diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 34409e0c37..941ebc93a6 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -1142,12 +1142,12 @@ void IApplicationFunctions::PopLaunchParameter(Kernel::HLERequestContext& ctx) { if (kind == LaunchParameterKind::ApplicationSpecific && !launch_popped_application_specific) { const auto backend = BCAT::CreateBackendFromSettings( [this](u64 tid) { return system.GetFileSystemController().GetBCATDirectory(tid); }); - const auto build_id_full = Core::System::GetInstance().GetCurrentProcessBuildID(); + const auto build_id_full = system.GetCurrentProcessBuildID(); u64 build_id{}; std::memcpy(&build_id, build_id_full.data(), sizeof(u64)); const auto data = - backend->GetLaunchParameter({Core::CurrentProcess()->GetTitleID(), build_id}); + backend->GetLaunchParameter({system.CurrentProcess()->GetTitleID(), build_id}); if (data.has_value()) { IPC::ResponseBuilder rb{ctx, 2, 0, 1}; @@ -1200,7 +1200,7 @@ void IApplicationFunctions::EnsureSaveData(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_AM, "called, uid={:016X}{:016X}", user_id[1], user_id[0]); FileSys::SaveDataDescriptor descriptor{}; - descriptor.title_id = Core::CurrentProcess()->GetTitleID(); + descriptor.title_id = system.CurrentProcess()->GetTitleID(); descriptor.user_id = user_id; descriptor.type = FileSys::SaveDataType::SaveData; const auto res = system.GetFileSystemController().CreateSaveData( diff --git a/src/core/hle/service/bcat/bcat.cpp b/src/core/hle/service/bcat/bcat.cpp index c2f9464240..8bb2528c9e 100644 --- a/src/core/hle/service/bcat/bcat.cpp +++ b/src/core/hle/service/bcat/bcat.cpp @@ -6,8 +6,9 @@ namespace Service::BCAT { -BCAT::BCAT(std::shared_ptr module, FileSystem::FileSystemController& fsc, const char* name) - : Module::Interface(std::move(module), fsc, name) { +BCAT::BCAT(Core::System& system, std::shared_ptr module, + FileSystem::FileSystemController& fsc, const char* name) + : Interface(system, std::move(module), fsc, name) { // clang-format off static const FunctionInfo functions[] = { {0, &BCAT::CreateBcatService, "CreateBcatService"}, diff --git a/src/core/hle/service/bcat/bcat.h b/src/core/hle/service/bcat/bcat.h index 8130736588..6354465fcb 100644 --- a/src/core/hle/service/bcat/bcat.h +++ b/src/core/hle/service/bcat/bcat.h @@ -6,12 +6,16 @@ #include "core/hle/service/bcat/module.h" +namespace Core { +class System; +} + namespace Service::BCAT { class BCAT final : public Module::Interface { public: - explicit BCAT(std::shared_ptr module, FileSystem::FileSystemController& fsc, - const char* name); + explicit BCAT(Core::System& system, std::shared_ptr module, + FileSystem::FileSystemController& fsc, const char* name); ~BCAT() override; }; diff --git a/src/core/hle/service/bcat/module.cpp b/src/core/hle/service/bcat/module.cpp index 4c01bcd990..8931fb3855 100644 --- a/src/core/hle/service/bcat/module.cpp +++ b/src/core/hle/service/bcat/module.cpp @@ -35,8 +35,7 @@ using BCATDigest = std::array; namespace { -u64 GetCurrentBuildID() { - const auto& id = Core::System::GetInstance().GetCurrentProcessBuildID(); +u64 GetCurrentBuildID(const Core::System::CurrentBuildProcessID& id) { u64 out{}; std::memcpy(&out, id.data(), sizeof(u64)); return out; @@ -125,7 +124,8 @@ private: class IBcatService final : public ServiceFramework { public: - IBcatService(Backend& backend) : ServiceFramework("IBcatService"), backend(backend) { + explicit IBcatService(Core::System& system_, Backend& backend_) + : ServiceFramework("IBcatService"), system{system_}, backend{backend_} { // clang-format off static const FunctionInfo functions[] = { {10100, &IBcatService::RequestSyncDeliveryCache, "RequestSyncDeliveryCache"}, @@ -163,7 +163,8 @@ private: void RequestSyncDeliveryCache(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_BCAT, "called"); - backend.Synchronize({Core::CurrentProcess()->GetTitleID(), GetCurrentBuildID()}, + backend.Synchronize({system.CurrentProcess()->GetTitleID(), + GetCurrentBuildID(system.GetCurrentProcessBuildID())}, progress.at(static_cast(SyncType::Normal))); IPC::ResponseBuilder rb{ctx, 2, 0, 1}; @@ -179,7 +180,8 @@ private: LOG_DEBUG(Service_BCAT, "called, name={}", name); - backend.SynchronizeDirectory({Core::CurrentProcess()->GetTitleID(), GetCurrentBuildID()}, + backend.SynchronizeDirectory({system.CurrentProcess()->GetTitleID(), + GetCurrentBuildID(system.GetCurrentProcessBuildID())}, name, progress.at(static_cast(SyncType::Directory))); @@ -244,6 +246,7 @@ private: rb.Push(RESULT_SUCCESS); } + Core::System& system; Backend& backend; std::array(SyncType::Count)> progress{ @@ -257,7 +260,7 @@ void Module::Interface::CreateBcatService(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(RESULT_SUCCESS); - rb.PushIpcInterface(*backend); + rb.PushIpcInterface(system, *backend); } class IDeliveryCacheFileService final : public ServiceFramework { @@ -539,7 +542,7 @@ void Module::Interface::CreateDeliveryCacheStorageService(Kernel::HLERequestCont IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(RESULT_SUCCESS); rb.PushIpcInterface( - fsc.GetBCATDirectory(Core::CurrentProcess()->GetTitleID())); + fsc.GetBCATDirectory(system.CurrentProcess()->GetTitleID())); } void Module::Interface::CreateDeliveryCacheStorageServiceWithApplicationId( @@ -565,22 +568,23 @@ std::unique_ptr CreateBackendFromSettings(DirectoryGetter getter) { return std::make_unique(std::move(getter)); } -Module::Interface::Interface(std::shared_ptr module, FileSystem::FileSystemController& fsc, - const char* name) - : ServiceFramework(name), fsc(fsc), module(std::move(module)), - backend(CreateBackendFromSettings([&fsc](u64 tid) { return fsc.GetBCATDirectory(tid); })) {} +Module::Interface::Interface(Core::System& system_, std::shared_ptr module_, + FileSystem::FileSystemController& fsc_, const char* name) + : ServiceFramework(name), fsc{fsc_}, module{std::move(module_)}, + backend{CreateBackendFromSettings([&fsc_](u64 tid) { return fsc_.GetBCATDirectory(tid); })}, + system{system_} {} Module::Interface::~Interface() = default; void InstallInterfaces(Core::System& system) { auto module = std::make_shared(); - std::make_shared(module, system.GetFileSystemController(), "bcat:a") + std::make_shared(system, module, system.GetFileSystemController(), "bcat:a") ->InstallAsService(system.ServiceManager()); - std::make_shared(module, system.GetFileSystemController(), "bcat:m") + std::make_shared(system, module, system.GetFileSystemController(), "bcat:m") ->InstallAsService(system.ServiceManager()); - std::make_shared(module, system.GetFileSystemController(), "bcat:u") + std::make_shared(system, module, system.GetFileSystemController(), "bcat:u") ->InstallAsService(system.ServiceManager()); - std::make_shared(module, system.GetFileSystemController(), "bcat:s") + std::make_shared(system, module, system.GetFileSystemController(), "bcat:s") ->InstallAsService(system.ServiceManager()); } diff --git a/src/core/hle/service/bcat/module.h b/src/core/hle/service/bcat/module.h index 27469926aa..e4ba23ba0f 100644 --- a/src/core/hle/service/bcat/module.h +++ b/src/core/hle/service/bcat/module.h @@ -6,6 +6,10 @@ #include "core/hle/service/service.h" +namespace Core { +class System; +} + namespace Service { namespace FileSystem { @@ -20,8 +24,8 @@ class Module final { public: class Interface : public ServiceFramework { public: - explicit Interface(std::shared_ptr module, FileSystem::FileSystemController& fsc, - const char* name); + explicit Interface(Core::System& system_, std::shared_ptr module_, + FileSystem::FileSystemController& fsc_, const char* name); ~Interface() override; void CreateBcatService(Kernel::HLERequestContext& ctx); @@ -33,6 +37,9 @@ public: std::shared_ptr module; std::unique_ptr backend; + + private: + Core::System& system; }; }; diff --git a/src/core/hle/service/fatal/fatal.cpp b/src/core/hle/service/fatal/fatal.cpp index b2ebf62401..2546d7595f 100644 --- a/src/core/hle/service/fatal/fatal.cpp +++ b/src/core/hle/service/fatal/fatal.cpp @@ -66,7 +66,7 @@ enum class FatalType : u32 { static void GenerateErrorReport(Core::System& system, ResultCode error_code, const FatalInfo& info) { - const auto title_id = Core::CurrentProcess()->GetTitleID(); + const auto title_id = system.CurrentProcess()->GetTitleID(); std::string crash_report = fmt::format( "Yuzu {}-{} crash report\n" "Title ID: {:016x}\n" diff --git a/src/core/hle/service/filesystem/filesystem.cpp b/src/core/hle/service/filesystem/filesystem.cpp index 7fa4e820be..11e5c56b7a 100644 --- a/src/core/hle/service/filesystem/filesystem.cpp +++ b/src/core/hle/service/filesystem/filesystem.cpp @@ -241,7 +241,7 @@ ResultVal VfsDirectoryServiceWrapper::GetEntryType( return FileSys::ERROR_PATH_NOT_FOUND; } -FileSystemController::FileSystemController() = default; +FileSystemController::FileSystemController(Core::System& system_) : system{system_} {} FileSystemController::~FileSystemController() = default; @@ -290,7 +290,7 @@ ResultVal FileSystemController::OpenRomFSCurrentProcess() return ResultCode(-1); } - return romfs_factory->OpenCurrentProcess(); + return romfs_factory->OpenCurrentProcess(system.CurrentProcess()->GetTitleID()); } ResultVal FileSystemController::OpenRomFS( @@ -447,10 +447,10 @@ FileSys::SaveDataSize FileSystemController::ReadSaveDataSize(FileSys::SaveDataTy FileSys::SaveDataSize new_size{SUFFICIENT_SAVE_DATA_SIZE, SUFFICIENT_SAVE_DATA_SIZE}; FileSys::NACP nacp; - const auto res = Core::System::GetInstance().GetAppLoader().ReadControlData(nacp); + const auto res = system.GetAppLoader().ReadControlData(nacp); if (res != Loader::ResultStatus::Success) { - FileSys::PatchManager pm{Core::CurrentProcess()->GetTitleID()}; + FileSys::PatchManager pm{system.CurrentProcess()->GetTitleID()}; auto [nacp_unique, discard] = pm.GetControlMetadata(); if (nacp_unique != nullptr) { @@ -702,10 +702,10 @@ void FileSystemController::CreateFactories(FileSys::VfsFilesystem& vfs, bool ove if (bis_factory == nullptr) { bis_factory = std::make_unique(nand_directory, load_directory, dump_directory); - Core::System::GetInstance().RegisterContentProvider( - FileSys::ContentProviderUnionSlot::SysNAND, bis_factory->GetSystemNANDContents()); - Core::System::GetInstance().RegisterContentProvider( - FileSys::ContentProviderUnionSlot::UserNAND, bis_factory->GetUserNANDContents()); + system.RegisterContentProvider(FileSys::ContentProviderUnionSlot::SysNAND, + bis_factory->GetSystemNANDContents()); + system.RegisterContentProvider(FileSys::ContentProviderUnionSlot::UserNAND, + bis_factory->GetUserNANDContents()); } if (save_data_factory == nullptr) { @@ -714,8 +714,8 @@ void FileSystemController::CreateFactories(FileSys::VfsFilesystem& vfs, bool ove if (sdmc_factory == nullptr) { sdmc_factory = std::make_unique(std::move(sd_directory)); - Core::System::GetInstance().RegisterContentProvider(FileSys::ContentProviderUnionSlot::SDMC, - sdmc_factory->GetSDMCContents()); + system.RegisterContentProvider(FileSys::ContentProviderUnionSlot::SDMC, + sdmc_factory->GetSDMCContents()); } } diff --git a/src/core/hle/service/filesystem/filesystem.h b/src/core/hle/service/filesystem/filesystem.h index e6b49d8a20..1b0a6a9496 100644 --- a/src/core/hle/service/filesystem/filesystem.h +++ b/src/core/hle/service/filesystem/filesystem.h @@ -10,6 +10,10 @@ #include "core/file_sys/vfs.h" #include "core/hle/result.h" +namespace Core { +class System; +} + namespace FileSys { class BISFactory; class RegisteredCache; @@ -52,7 +56,7 @@ enum class ImageDirectoryId : u32 { class FileSystemController { public: - FileSystemController(); + explicit FileSystemController(Core::System& system_); ~FileSystemController(); ResultCode RegisterRomFS(std::unique_ptr&& factory); @@ -125,6 +129,8 @@ private: std::unique_ptr gamecard; std::unique_ptr gamecard_registered; std::unique_ptr gamecard_placeholder; + + Core::System& system; }; void InstallInterfaces(Core::System& system); diff --git a/src/core/hle/service/ldr/ldr.cpp b/src/core/hle/service/ldr/ldr.cpp index 3164ca26e9..499376bfc4 100644 --- a/src/core/hle/service/ldr/ldr.cpp +++ b/src/core/hle/service/ldr/ldr.cpp @@ -163,7 +163,7 @@ public: return; } - if (Core::CurrentProcess()->GetTitleID() != header.title_id) { + if (system.CurrentProcess()->GetTitleID() != header.title_id) { LOG_ERROR(Service_LDR, "Attempting to load NRR with title ID other than current process. (actual " "{:016X})!", @@ -327,7 +327,7 @@ public: } // Load NRO as new executable module - auto* process = Core::CurrentProcess(); + auto* process = system.CurrentProcess(); auto& vm_manager = process->VMManager(); auto map_address = vm_manager.FindFreeRegion(nro_size + bss_size); @@ -411,7 +411,7 @@ public: return; } - auto& vm_manager = Core::CurrentProcess()->VMManager(); + auto& vm_manager = system.CurrentProcess()->VMManager(); const auto& nro_info = iter->second; // Unmap the mirrored memory diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp index 7dcdb4a075..f645352378 100644 --- a/src/core/hle/service/ns/pl_u.cpp +++ b/src/core/hle/service/ns/pl_u.cpp @@ -324,14 +324,14 @@ void PL_U::GetSharedMemoryAddressOffset(Kernel::HLERequestContext& ctx) { void PL_U::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) { // Map backing memory for the font data LOG_DEBUG(Service_NS, "called"); - Core::CurrentProcess()->VMManager().MapMemoryBlock(SHARED_FONT_MEM_VADDR, impl->shared_font, 0, - SHARED_FONT_MEM_SIZE, - Kernel::MemoryState::Shared); + system.CurrentProcess()->VMManager().MapMemoryBlock(SHARED_FONT_MEM_VADDR, impl->shared_font, 0, + SHARED_FONT_MEM_SIZE, + Kernel::MemoryState::Shared); // Create shared font memory object auto& kernel = system.Kernel(); impl->shared_font_mem = Kernel::SharedMemory::Create( - kernel, Core::CurrentProcess(), SHARED_FONT_MEM_SIZE, Kernel::MemoryPermission::ReadWrite, + kernel, system.CurrentProcess(), SHARED_FONT_MEM_SIZE, Kernel::MemoryPermission::ReadWrite, Kernel::MemoryPermission::Read, SHARED_FONT_MEM_VADDR, Kernel::MemoryRegion::BASE, "PL_U:shared_font_mem");