Skip to content

Commit

Permalink
core: Remove some usages of global system instace by PLGLDR port
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelthegreat authored and Gamer64ytb committed Sep 1, 2024
1 parent 5927943 commit 21c6e2b
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 39 deletions.
16 changes: 8 additions & 8 deletions src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ System::ResultStatus System::Load(Frontend::EmuWindow& emu_window, const std::st
restore_deliver_arg.reset();
}
if (restore_plugin_context.has_value()) {
if (auto plg_ldr = Service::PLGLDR::GetService(*this)) {
if (auto plg_ldr = Service::PLGLDR::GetService(*kernel)) {
plg_ldr->SetPluginLoaderContext(restore_plugin_context.value());
}
restore_plugin_context.reset();
Expand Down Expand Up @@ -539,7 +539,7 @@ System::ResultStatus System::Init(Frontend::EmuWindow& emu_window,
gpu->SetInterruptHandler(
[gsp](Service::GSP::InterruptId interrupt_id) { gsp->SignalInterrupt(interrupt_id); });

auto plg_ldr = Service::PLGLDR::GetService(*this);
auto plg_ldr = Service::PLGLDR::GetService(*kernel);
if (plg_ldr) {
plg_ldr->SetEnabled(Settings::values.plugin_loader_enabled.GetValue());
plg_ldr->SetAllowGameChangeState(Settings::values.allow_plugin_loader.GetValue());
Expand Down Expand Up @@ -580,10 +580,6 @@ const Kernel::KernelSystem& System::Kernel() const {
return *kernel;
}

bool System::KernelRunning() {
return kernel != nullptr;
}

Timing& System::CoreTiming() {
return *timing;
}
Expand Down Expand Up @@ -707,7 +703,7 @@ void System::Reset() {
if (auto apt = Service::APT::GetModule(*this)) {
restore_deliver_arg = apt->GetAppletManager()->ReceiveDeliverArg();
}
if (auto plg_ldr = Service::PLGLDR::GetService(*this)) {
if (auto plg_ldr = Service::PLGLDR::GetService(*kernel)) {
restore_plugin_context = plg_ldr->GetPluginLoaderContext();
}

Expand Down Expand Up @@ -767,7 +763,11 @@ void System::ApplySettings() {
Service::MIC::ReloadMic(*this);
}

auto plg_ldr = Service::PLGLDR::GetService(*this);
if (!kernel) {
return;
}

auto plg_ldr = Service::PLGLDR::GetService(*kernel);
if (plg_ldr) {
plg_ldr->SetEnabled(Settings::values.plugin_loader_enabled.GetValue());
plg_ldr->SetAllowGameChangeState(Settings::values.allow_plugin_loader.GetValue());
Expand Down
3 changes: 0 additions & 3 deletions src/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,6 @@ class System {
/// Gets a const reference to the kernel
[[nodiscard]] const Kernel::KernelSystem& Kernel() const;

/// Get kernel is running
[[nodiscard]] bool KernelRunning();

/// Gets a reference to the timing system
[[nodiscard]] Timing& CoreTiming();

Expand Down
6 changes: 3 additions & 3 deletions src/core/hle/kernel/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void Process::Run(s32 main_thread_priority, u32 stack_size) {
kernel.HandleSpecialMapping(vm_manager, mapping);
}

auto plgldr = Service::PLGLDR::GetService(Core::System::GetInstance());
auto plgldr = Service::PLGLDR::GetService(kernel);
if (plgldr) {
plgldr->OnProcessRun(*this, kernel);
}
Expand All @@ -261,7 +261,7 @@ void Process::Run(s32 main_thread_priority, u32 stack_size) {
}

void Process::Exit() {
auto plgldr = Service::PLGLDR::GetService(Core::System::GetInstance());
auto plgldr = Service::PLGLDR::GetService(kernel);
if (plgldr) {
plgldr->OnProcessExit(*this, kernel);
}
Expand Down Expand Up @@ -627,7 +627,7 @@ void Process::FreeAllMemory() {
}

Kernel::Process::Process(KernelSystem& kernel)
: Object(kernel), handle_table(kernel), vm_manager(kernel.memory, *this), kernel(kernel) {
: Object(kernel), handle_table(kernel), vm_manager(kernel, *this), kernel(kernel) {
kernel.memory.RegisterPageTable(vm_manager.page_table);
}
Kernel::Process::~Process() {
Expand Down
2 changes: 1 addition & 1 deletion src/core/hle/kernel/svc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,7 @@ Result SVC::ControlProcess(Handle process_handle, u32 process_OP, u32 varg2, u32
return ResultSuccess;
}
case ControlProcessOP::PROCESSOP_GET_ON_MEMORY_CHANGE_EVENT: {
auto plgldr = Service::PLGLDR::GetService(system);
auto plgldr = Service::PLGLDR::GetService(kernel);
R_UNLESS(plgldr, ResultNotFound);

ResultVal<Handle> out = plgldr->GetMemoryChangedHandle(kernel);
Expand Down
12 changes: 6 additions & 6 deletions src/core/hle/kernel/vm_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ void VirtualMemoryArea::serialize(Archive& ar, const unsigned int) {
}
SERIALIZE_IMPL(VirtualMemoryArea)

VMManager::VMManager(Memory::MemorySystem& memory, Kernel::Process& proc)
: page_table(std::make_shared<Memory::PageTable>()), memory(memory), process(proc) {
VMManager::VMManager(Kernel::KernelSystem& kernel, Kernel::Process& proc)
: page_table(std::make_shared<Memory::PageTable>()), kernel(kernel), process(proc) {
Reset();
}

Expand Down Expand Up @@ -365,16 +365,16 @@ VMManager::VMAIter VMManager::MergeAdjacent(VMAIter iter) {
void VMManager::UpdatePageTableForVMA(const VirtualMemoryArea& vma) {
switch (vma.type) {
case VMAType::Free:
memory.UnmapRegion(*page_table, vma.base, vma.size);
kernel.memory.UnmapRegion(*page_table, vma.base, vma.size);
break;
case VMAType::BackingMemory:
memory.MapMemoryRegion(*page_table, vma.base, vma.size, vma.backing_memory);
kernel.memory.MapMemoryRegion(*page_table, vma.base, vma.size, vma.backing_memory);
break;
}

auto plgldr = Service::PLGLDR::GetService(Core::System::GetInstance());
auto plgldr = Service::PLGLDR::GetService(kernel);
if (plgldr)
plgldr->OnMemoryChanged(process, Core::System::GetInstance().Kernel());
plgldr->OnMemoryChanged(process, kernel);
}

ResultVal<std::vector<std::pair<MemoryRef, u32>>> VMManager::GetBackingBlocksForRange(VAddr address,
Expand Down
6 changes: 4 additions & 2 deletions src/core/hle/kernel/vm_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace Kernel {

class KernelSystem;

enum class VMAType : u8 {
/// VMA represents an unmapped region of the address space.
Free,
Expand Down Expand Up @@ -111,7 +113,7 @@ class VMManager final {
std::map<VAddr, VirtualMemoryArea> vma_map;
using VMAHandle = decltype(vma_map)::const_iterator;

explicit VMManager(Memory::MemorySystem& memory, Kernel::Process& proc);
explicit VMManager(Kernel::KernelSystem& kernel, Kernel::Process& proc);
~VMManager();

/// Clears the address space map, re-initializing with a single free area.
Expand Down Expand Up @@ -221,7 +223,7 @@ class VMManager final {
/// Updates the pages corresponding to this VMA so they match the VMA's attributes.
void UpdatePageTableForVMA(const VirtualMemoryArea& vma);

Memory::MemorySystem& memory;
Kernel::KernelSystem& kernel;
Kernel::Process& process;

// When locked, ChangeMemoryState calls will be ignored, other modification calls will hit an
Expand Down
2 changes: 1 addition & 1 deletion src/core/hle/service/apt/ns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ std::shared_ptr<Kernel::Process> LaunchTitle(Core::System& system, FS::MediaType
return nullptr;
}

auto plg_ldr = Service::PLGLDR::GetService(system);
auto plg_ldr = Service::PLGLDR::GetService(system.Kernel());
if (plg_ldr) {
const auto& plg_context = plg_ldr->GetPluginLoaderContext();
if (plg_context.is_enabled && plg_context.use_user_load_parameters &&
Expand Down
11 changes: 5 additions & 6 deletions src/core/hle/service/plgldr/plgldr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,12 @@ void PLG_LDR::GetPluginPath(Kernel::HLERequestContext& ctx) {
rb.PushMappedBuffer(path);
}

std::shared_ptr<PLG_LDR> GetService(Core::System& system) {
if (!system.KernelRunning())
std::shared_ptr<PLG_LDR> GetService(Kernel::KernelSystem& kernel) {
const auto it = kernel.named_ports.find("plg:ldr");
if (it == kernel.named_ports.end()) {
return nullptr;
auto it = system.Kernel().named_ports.find("plg:ldr");
if (it != system.Kernel().named_ports.end())
return std::static_pointer_cast<PLG_LDR>(it->second->GetServerPort()->hle_handler);
return nullptr;
}
return std::static_pointer_cast<PLG_LDR>(it->second->GetServerPort()->hle_handler);
}

void InstallInterfaces(Core::System& system) {
Expand Down
6 changes: 5 additions & 1 deletion src/core/hle/service/plgldr/plgldr.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ namespace Core {
class System;
}

namespace Kernel {
class KernelSystem;
}

namespace Service::PLGLDR {

class PLG_LDR final : public ServiceFramework<PLG_LDR> {
Expand Down Expand Up @@ -134,7 +138,7 @@ class PLG_LDR final : public ServiceFramework<PLG_LDR> {
friend class boost::serialization::access;
};

std::shared_ptr<PLG_LDR> GetService(Core::System& system);
std::shared_ptr<PLG_LDR> GetService(Kernel::KernelSystem& kernel);

void InstallInterfaces(Core::System& system);

Expand Down
6 changes: 3 additions & 3 deletions src/core/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class MemorySystem::Impl {
return {vram_mem, addr - VRAM_VADDR};
}
if (addr >= PLUGIN_3GX_FB_VADDR && addr < PLUGIN_3GX_FB_VADDR_END) {
auto plg_ldr = Service::PLGLDR::GetService(system);
auto plg_ldr = Service::PLGLDR::GetService(system.Kernel());
if (plg_ldr) {
return {fcram_mem,
addr - PLUGIN_3GX_FB_VADDR + plg_ldr->GetPluginFBAddr() - FCRAM_PADDR};
Expand Down Expand Up @@ -309,7 +309,7 @@ class MemorySystem::Impl {
CheckRegion(LINEAR_HEAP_VADDR, LINEAR_HEAP_VADDR_END, FCRAM_PADDR);
CheckRegion(NEW_LINEAR_HEAP_VADDR, NEW_LINEAR_HEAP_VADDR_END, FCRAM_PADDR);
CheckRegion(VRAM_VADDR, VRAM_VADDR_END, VRAM_PADDR);
auto plg_ldr = Service::PLGLDR::GetService(system);
auto plg_ldr = Service::PLGLDR::GetService(system.Kernel());
if (plg_ldr && plg_ldr->GetPluginFBAddr()) {
CheckRegion(PLUGIN_3GX_FB_VADDR, PLUGIN_3GX_FB_VADDR_END, plg_ldr->GetPluginFBAddr());
}
Expand Down Expand Up @@ -695,7 +695,7 @@ std::vector<VAddr> MemorySystem::PhysicalToVirtualAddressForRasterizer(PAddr add
return {addr - VRAM_PADDR + VRAM_VADDR};
}
// NOTE: Order matters here.
auto plg_ldr = Service::PLGLDR::GetService(impl->system);
auto plg_ldr = Service::PLGLDR::GetService(impl->system.Kernel());
if (plg_ldr) {
auto fb_addr = plg_ldr->GetPluginFBAddr();
if (addr >= fb_addr && addr < fb_addr + PLUGIN_3GX_FB_SIZE) {
Expand Down
8 changes: 4 additions & 4 deletions src/tests/core/memory/vm_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TEST_CASE("Memory Basics", "[kernel][memory]") {
Kernel::Process process(kernel);
SECTION("mapping memory") {
// Because of the PageTable, Kernel::VMManager is too big to be created on the stack.
auto manager = std::make_unique<Kernel::VMManager>(memory, process);
auto manager = std::make_unique<Kernel::VMManager>(kernel, process);
auto result =
manager->MapBackingMemory(Memory::HEAP_VADDR, block, static_cast<u32>(block.GetSize()),
Kernel::MemoryState::Private);
Expand All @@ -38,7 +38,7 @@ TEST_CASE("Memory Basics", "[kernel][memory]") {

SECTION("unmapping memory") {
// Because of the PageTable, Kernel::VMManager is too big to be created on the stack.
auto manager = std::make_unique<Kernel::VMManager>(memory, process);
auto manager = std::make_unique<Kernel::VMManager>(kernel, process);
auto result =
manager->MapBackingMemory(Memory::HEAP_VADDR, block, static_cast<u32>(block.GetSize()),
Kernel::MemoryState::Private);
Expand All @@ -55,7 +55,7 @@ TEST_CASE("Memory Basics", "[kernel][memory]") {

SECTION("changing memory permissions") {
// Because of the PageTable, Kernel::VMManager is too big to be created on the stack.
auto manager = std::make_unique<Kernel::VMManager>(memory, process);
auto manager = std::make_unique<Kernel::VMManager>(kernel, process);
auto result =
manager->MapBackingMemory(Memory::HEAP_VADDR, block, static_cast<u32>(block.GetSize()),
Kernel::MemoryState::Private);
Expand All @@ -75,7 +75,7 @@ TEST_CASE("Memory Basics", "[kernel][memory]") {

SECTION("changing memory state") {
// Because of the PageTable, Kernel::VMManager is too big to be created on the stack.
auto manager = std::make_unique<Kernel::VMManager>(memory, process);
auto manager = std::make_unique<Kernel::VMManager>(kernel, process);
auto result =
manager->MapBackingMemory(Memory::HEAP_VADDR, block, static_cast<u32>(block.GetSize()),
Kernel::MemoryState::Private);
Expand Down
2 changes: 1 addition & 1 deletion src/video_core/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ PAddr GPU::VirtualToPhysicalAddress(VAddr addr) {
return addr - Memory::NEW_LINEAR_HEAP_VADDR + Memory::FCRAM_PADDR;
}
if (addr >= Memory::PLUGIN_3GX_FB_VADDR && addr <= Memory::PLUGIN_3GX_FB_VADDR_END) {
auto plg_ldr = Service::PLGLDR::GetService(impl->system);
auto plg_ldr = Service::PLGLDR::GetService(impl->system.Kernel());
if (plg_ldr) {
return addr - Memory::PLUGIN_3GX_FB_VADDR + plg_ldr->GetPluginFBAddr();
}
Expand Down

0 comments on commit 21c6e2b

Please sign in to comment.