core: Eliminate more uses of Core::System::GetInstance(). (#7313)

This commit is contained in:
Steveice10 2024-01-05 12:07:28 -08:00 committed by GitHub
parent 8e2037b3ff
commit f2ee9baec7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 416 additions and 387 deletions

View file

@ -94,7 +94,7 @@ static u32 TranslateAddr(u32 addr, const THREEloadinfo* loadinfo, u32* offsets)
using Kernel::CodeSet;
static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr,
static THREEDSX_Error Load3DSXFile(Core::System& system, FileUtil::IOFile& file, u32 base_addr,
std::shared_ptr<CodeSet>* out_codeset) {
if (!file.IsOpen())
return ERROR_FILE;
@ -222,7 +222,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr,
}
// Create the CodeSet
std::shared_ptr<CodeSet> code_set = Core::System::GetInstance().Kernel().CreateCodeSet("", 0);
std::shared_ptr<CodeSet> code_set = system.Kernel().CreateCodeSet("", 0);
code_set->CodeSegment().offset = loadinfo.seg_ptrs[0] - program_image.data();
code_set->CodeSegment().addr = loadinfo.seg_addrs[0];
@ -268,25 +268,24 @@ ResultStatus AppLoader_THREEDSX::Load(std::shared_ptr<Kernel::Process>& process)
return ResultStatus::Error;
std::shared_ptr<CodeSet> codeset;
if (Load3DSXFile(file, Memory::PROCESS_IMAGE_VADDR, &codeset) != ERROR_NONE)
if (Load3DSXFile(system, file, Memory::PROCESS_IMAGE_VADDR, &codeset) != ERROR_NONE)
return ResultStatus::Error;
codeset->name = filename;
process = Core::System::GetInstance().Kernel().CreateProcess(std::move(codeset));
process = system.Kernel().CreateProcess(std::move(codeset));
process->Set3dsxKernelCaps();
// Attach the default resource limit (APPLICATION) to the process
process->resource_limit = Core::System::GetInstance().Kernel().ResourceLimit().GetForCategory(
Kernel::ResourceLimitCategory::Application);
process->resource_limit =
system.Kernel().ResourceLimit().GetForCategory(Kernel::ResourceLimitCategory::Application);
// On real HW this is done with FS:Reg, but we can be lazy
auto fs_user =
Core::System::GetInstance().ServiceManager().GetService<Service::FS::FS_USER>("fs:USER");
auto fs_user = system.ServiceManager().GetService<Service::FS::FS_USER>("fs:USER");
fs_user->RegisterProgramInfo(process->GetObjectId(), process->codeset->program_id, filepath);
process->Run(48, Kernel::DEFAULT_STACK_SIZE);
Core::System::GetInstance().ArchiveManager().RegisterSelfNCCH(*this);
system.ArchiveManager().RegisterSelfNCCH(*this);
is_loaded = true;
return ResultStatus::Success;

View file

@ -14,9 +14,9 @@ namespace Loader {
/// Loads an 3DSX file
class AppLoader_THREEDSX final : public AppLoader {
public:
AppLoader_THREEDSX(FileUtil::IOFile&& file, const std::string& filename,
AppLoader_THREEDSX(Core::System& system_, FileUtil::IOFile&& file, const std::string& filename,
const std::string& filepath)
: AppLoader(std::move(file)), filename(std::move(filename)), filepath(filepath) {}
: AppLoader(system_, std::move(file)), filename(std::move(filename)), filepath(filepath) {}
/**
* Returns the type of the file

View file

@ -199,7 +199,7 @@ public:
u32 GetFlags() const {
return (u32)(header->e_flags);
}
std::shared_ptr<CodeSet> LoadInto(u32 vaddr);
std::shared_ptr<CodeSet> LoadInto(Core::System& system, u32 vaddr);
int GetNumSegments() const {
return (int)(header->e_phnum);
@ -262,7 +262,7 @@ const char* ElfReader::GetSectionName(int section) const {
return nullptr;
}
std::shared_ptr<CodeSet> ElfReader::LoadInto(u32 vaddr) {
std::shared_ptr<CodeSet> ElfReader::LoadInto(Core::System& system, u32 vaddr) {
LOG_DEBUG(Loader, "String section: {}", header->e_shstrndx);
// Should we relocate?
@ -290,7 +290,7 @@ std::shared_ptr<CodeSet> ElfReader::LoadInto(u32 vaddr) {
std::vector<u8> program_image(total_image_size);
std::size_t current_image_position = 0;
std::shared_ptr<CodeSet> codeset = Core::System::GetInstance().Kernel().CreateCodeSet("", 0);
std::shared_ptr<CodeSet> codeset = system.Kernel().CreateCodeSet("", 0);
for (unsigned int i = 0; i < header->e_phnum; ++i) {
Elf32_Phdr* p = &segments[i];
@ -380,15 +380,15 @@ ResultStatus AppLoader_ELF::Load(std::shared_ptr<Kernel::Process>& process) {
return ResultStatus::Error;
ElfReader elf_reader(&buffer[0]);
std::shared_ptr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR);
std::shared_ptr<CodeSet> codeset = elf_reader.LoadInto(system, Memory::PROCESS_IMAGE_VADDR);
codeset->name = filename;
process = Core::System::GetInstance().Kernel().CreateProcess(std::move(codeset));
process = system.Kernel().CreateProcess(std::move(codeset));
process->Set3dsxKernelCaps();
// Attach the default resource limit (APPLICATION) to the process
process->resource_limit = Core::System::GetInstance().Kernel().ResourceLimit().GetForCategory(
Kernel::ResourceLimitCategory::Application);
process->resource_limit =
system.Kernel().ResourceLimit().GetForCategory(Kernel::ResourceLimitCategory::Application);
process->Run(48, Kernel::DEFAULT_STACK_SIZE);

View file

@ -14,8 +14,8 @@ namespace Loader {
/// Loads an ELF/AXF file
class AppLoader_ELF final : public AppLoader {
public:
AppLoader_ELF(FileUtil::IOFile&& file, std::string filename)
: AppLoader(std::move(file)), filename(std::move(filename)) {}
AppLoader_ELF(Core::System& system_, FileUtil::IOFile&& file, std::string filename)
: AppLoader(system_, std::move(file)), filename(std::move(filename)) {}
/**
* Returns the type of the file

View file

@ -6,6 +6,7 @@
#include <string>
#include "common/logging/log.h"
#include "common/string_util.h"
#include "core/core.h"
#include "core/hle/kernel/process.h"
#include "core/loader/3dsx.h"
#include "core/loader/elf.h"
@ -89,23 +90,23 @@ const char* GetFileTypeString(FileType type) {
* @param filepath the file full path (with name)
* @return std::unique_ptr<AppLoader> a pointer to a loader object; nullptr for unsupported type
*/
static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileType type,
const std::string& filename,
static std::unique_ptr<AppLoader> GetFileLoader(Core::System& system, FileUtil::IOFile&& file,
FileType type, const std::string& filename,
const std::string& filepath) {
switch (type) {
// 3DSX file format.
case FileType::THREEDSX:
return std::make_unique<AppLoader_THREEDSX>(std::move(file), filename, filepath);
return std::make_unique<AppLoader_THREEDSX>(system, std::move(file), filename, filepath);
// Standard ELF file format.
case FileType::ELF:
return std::make_unique<AppLoader_ELF>(std::move(file), filename);
return std::make_unique<AppLoader_ELF>(system, std::move(file), filename);
// NCCH/NCSD container formats.
case FileType::CXI:
case FileType::CCI:
return std::make_unique<AppLoader_NCCH>(std::move(file), filepath);
return std::make_unique<AppLoader_NCCH>(system, std::move(file), filepath);
default:
return nullptr;
@ -133,7 +134,8 @@ std::unique_ptr<AppLoader> GetLoader(const std::string& filename) {
LOG_DEBUG(Loader, "Loading file {} as {}...", filename, GetFileTypeString(type));
return GetFileLoader(std::move(file), type, filename_filename, filename);
auto& system = Core::System::GetInstance();
return GetFileLoader(system, std::move(file), type, filename_filename, filename);
}
} // namespace Loader

View file

@ -82,7 +82,8 @@ constexpr u32 MakeMagic(char a, char b, char c, char d) {
/// Interface for loading an application
class AppLoader : NonCopyable {
public:
explicit AppLoader(FileUtil::IOFile&& file) : file(std::move(file)) {}
explicit AppLoader(Core::System& system_, FileUtil::IOFile&& file)
: system(system_), file(std::move(file)) {}
virtual ~AppLoader() {}
/**
@ -253,6 +254,7 @@ public:
}
protected:
Core::System& system;
FileUtil::IOFile file;
bool is_loaded = false;
};

View file

@ -113,8 +113,7 @@ ResultStatus AppLoader_NCCH::LoadExec(std::shared_ptr<Kernel::Process>& process)
std::string process_name = Common::StringFromFixedZeroTerminatedBuffer(
(const char*)overlay_ncch->exheader_header.codeset_info.name, 8);
std::shared_ptr<CodeSet> codeset =
Core::System::GetInstance().Kernel().CreateCodeSet(process_name, program_id);
std::shared_ptr<CodeSet> codeset = system.Kernel().CreateCodeSet(process_name, program_id);
codeset->CodeSegment().offset = 0;
codeset->CodeSegment().addr = overlay_ncch->exheader_header.codeset_info.text.address;
@ -148,7 +147,6 @@ ResultStatus AppLoader_NCCH::LoadExec(std::shared_ptr<Kernel::Process>& process)
codeset->entrypoint = codeset->CodeSegment().addr;
codeset->memory = std::move(code);
auto& system = Core::System::GetInstance();
process = system.Kernel().CreateProcess(std::move(codeset));
// Attach a resource limit to the process based on the resource limit category
@ -276,7 +274,6 @@ ResultStatus AppLoader_NCCH::Load(std::shared_ptr<Kernel::Process>& process) {
overlay_ncch = &update_ncch;
}
auto& system = Core::System::GetInstance();
system.TelemetrySession().AddField(Common::Telemetry::FieldType::Session, "ProgramId",
program_id);

View file

@ -15,8 +15,8 @@ namespace Loader {
/// Loads an NCCH file (e.g. from a CCI, or the first NCCH in a CXI)
class AppLoader_NCCH final : public AppLoader {
public:
AppLoader_NCCH(FileUtil::IOFile&& file, const std::string& filepath)
: AppLoader(std::move(file)), base_ncch(filepath), overlay_ncch(&base_ncch),
AppLoader_NCCH(Core::System& system_, FileUtil::IOFile&& file, const std::string& filepath)
: AppLoader(system_, std::move(file)), base_ncch(filepath), overlay_ncch(&base_ncch),
filepath(filepath) {}
/**