diff options
Diffstat (limited to '')
27 files changed, 832 insertions, 482 deletions
diff --git a/src/core/arm/dynarmic/arm_dynarmic.cpp b/src/core/arm/dynarmic/arm_dynarmic.cpp index 49145911b..dc96e35d5 100644 --- a/src/core/arm/dynarmic/arm_dynarmic.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic.cpp @@ -14,6 +14,7 @@ #include "core/core_timing.h" #include "core/core_timing_util.h" #include "core/gdbstub/gdbstub.h" +#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/svc.h" #include "core/hle/kernel/vm_manager.h" @@ -99,7 +100,7 @@ public: } void CallSVC(u32 swi) override { - Kernel::CallSVC(swi); + Kernel::CallSVC(parent.system, swi); } void AddTicks(u64 ticks) override { @@ -112,14 +113,14 @@ public: // Always execute at least one tick. amortized_ticks = std::max<u64>(amortized_ticks, 1); - parent.core_timing.AddTicks(amortized_ticks); + parent.system.CoreTiming().AddTicks(amortized_ticks); num_interpreted_instructions = 0; } u64 GetTicksRemaining() override { - return std::max(parent.core_timing.GetDowncount(), 0); + return std::max(parent.system.CoreTiming().GetDowncount(), 0); } u64 GetCNTPCT() override { - return Timing::CpuCyclesToClockCycles(parent.core_timing.GetTicks()); + return Timing::CpuCyclesToClockCycles(parent.system.CoreTiming().GetTicks()); } ARM_Dynarmic& parent; @@ -129,7 +130,7 @@ public: }; std::unique_ptr<Dynarmic::A64::Jit> ARM_Dynarmic::MakeJit() const { - auto* current_process = Core::CurrentProcess(); + auto* current_process = system.Kernel().CurrentProcess(); auto** const page_table = current_process->VMManager().page_table.pointers.data(); Dynarmic::A64::UserConfig config; @@ -171,10 +172,10 @@ void ARM_Dynarmic::Step() { cb->InterpreterFallback(jit->GetPC(), 1); } -ARM_Dynarmic::ARM_Dynarmic(Timing::CoreTiming& core_timing, ExclusiveMonitor& exclusive_monitor, +ARM_Dynarmic::ARM_Dynarmic(System& system, ExclusiveMonitor& exclusive_monitor, std::size_t core_index) - : cb(std::make_unique<ARM_Dynarmic_Callbacks>(*this)), inner_unicorn{core_timing}, - core_index{core_index}, core_timing{core_timing}, + : cb(std::make_unique<ARM_Dynarmic_Callbacks>(*this)), inner_unicorn{system}, + core_index{core_index}, system{system}, exclusive_monitor{dynamic_cast<DynarmicExclusiveMonitor&>(exclusive_monitor)} { ThreadContext ctx{}; inner_unicorn.SaveContext(ctx); diff --git a/src/core/arm/dynarmic/arm_dynarmic.h b/src/core/arm/dynarmic/arm_dynarmic.h index d867c2a50..c1db254e8 100644 --- a/src/core/arm/dynarmic/arm_dynarmic.h +++ b/src/core/arm/dynarmic/arm_dynarmic.h @@ -12,19 +12,15 @@ #include "core/arm/exclusive_monitor.h" #include "core/arm/unicorn/arm_unicorn.h" -namespace Core::Timing { -class CoreTiming; -} - namespace Core { class ARM_Dynarmic_Callbacks; class DynarmicExclusiveMonitor; +class System; class ARM_Dynarmic final : public ARM_Interface { public: - ARM_Dynarmic(Timing::CoreTiming& core_timing, ExclusiveMonitor& exclusive_monitor, - std::size_t core_index); + ARM_Dynarmic(System& system, ExclusiveMonitor& exclusive_monitor, std::size_t core_index); ~ARM_Dynarmic() override; void MapBackingMemory(VAddr address, std::size_t size, u8* memory, @@ -63,7 +59,7 @@ private: ARM_Unicorn inner_unicorn; std::size_t core_index; - Timing::CoreTiming& core_timing; + System& system; DynarmicExclusiveMonitor& exclusive_monitor; }; diff --git a/src/core/arm/unicorn/arm_unicorn.cpp b/src/core/arm/unicorn/arm_unicorn.cpp index 27309280c..4e07fe8b5 100644 --- a/src/core/arm/unicorn/arm_unicorn.cpp +++ b/src/core/arm/unicorn/arm_unicorn.cpp @@ -10,7 +10,6 @@ #include "core/core.h" #include "core/core_timing.h" #include "core/hle/kernel/svc.h" -#include "core/memory.h" namespace Core { @@ -49,20 +48,6 @@ static void CodeHook(uc_engine* uc, uint64_t address, uint32_t size, void* user_ } } -static void InterruptHook(uc_engine* uc, u32 intNo, void* user_data) { - u32 esr{}; - CHECKED(uc_reg_read(uc, UC_ARM64_REG_ESR, &esr)); - - auto ec = esr >> 26; - auto iss = esr & 0xFFFFFF; - - switch (ec) { - case 0x15: // SVC - Kernel::CallSVC(iss); - break; - } -} - static bool UnmappedMemoryHook(uc_engine* uc, uc_mem_type type, u64 addr, int size, u64 value, void* user_data) { ARM_Interface::ThreadContext ctx{}; @@ -72,7 +57,7 @@ static bool UnmappedMemoryHook(uc_engine* uc, uc_mem_type type, u64 addr, int si return {}; } -ARM_Unicorn::ARM_Unicorn(Timing::CoreTiming& core_timing) : core_timing{core_timing} { +ARM_Unicorn::ARM_Unicorn(System& system) : system{system} { CHECKED(uc_open(UC_ARCH_ARM64, UC_MODE_ARM, &uc)); auto fpv = 3 << 20; @@ -177,7 +162,7 @@ void ARM_Unicorn::Run() { if (GDBStub::IsServerEnabled()) { ExecuteInstructions(std::max(4000000, 0)); } else { - ExecuteInstructions(std::max(core_timing.GetDowncount(), 0)); + ExecuteInstructions(std::max(system.CoreTiming().GetDowncount(), 0)); } } @@ -190,7 +175,7 @@ MICROPROFILE_DEFINE(ARM_Jit_Unicorn, "ARM JIT", "Unicorn", MP_RGB(255, 64, 64)); void ARM_Unicorn::ExecuteInstructions(int num_instructions) { MICROPROFILE_SCOPE(ARM_Jit_Unicorn); CHECKED(uc_emu_start(uc, GetPC(), 1ULL << 63, 0, num_instructions)); - core_timing.AddTicks(num_instructions); + system.CoreTiming().AddTicks(num_instructions); if (GDBStub::IsServerEnabled()) { if (last_bkpt_hit && last_bkpt.type == GDBStub::BreakpointType::Execute) { uc_reg_write(uc, UC_ARM64_REG_PC, &last_bkpt.address); @@ -273,4 +258,20 @@ void ARM_Unicorn::RecordBreak(GDBStub::BreakpointAddress bkpt) { last_bkpt_hit = true; } +void ARM_Unicorn::InterruptHook(uc_engine* uc, u32 int_no, void* user_data) { + u32 esr{}; + CHECKED(uc_reg_read(uc, UC_ARM64_REG_ESR, &esr)); + + const auto ec = esr >> 26; + const auto iss = esr & 0xFFFFFF; + + auto* const arm_instance = static_cast<ARM_Unicorn*>(user_data); + + switch (ec) { + case 0x15: // SVC + Kernel::CallSVC(arm_instance->system, iss); + break; + } +} + } // namespace Core diff --git a/src/core/arm/unicorn/arm_unicorn.h b/src/core/arm/unicorn/arm_unicorn.h index 1e44f0736..209fc16ad 100644 --- a/src/core/arm/unicorn/arm_unicorn.h +++ b/src/core/arm/unicorn/arm_unicorn.h @@ -9,15 +9,13 @@ #include "core/arm/arm_interface.h" #include "core/gdbstub/gdbstub.h" -namespace Core::Timing { -class CoreTiming; -} - namespace Core { +class System; + class ARM_Unicorn final : public ARM_Interface { public: - explicit ARM_Unicorn(Timing::CoreTiming& core_timing); + explicit ARM_Unicorn(System& system); ~ARM_Unicorn() override; void MapBackingMemory(VAddr address, std::size_t size, u8* memory, @@ -47,8 +45,10 @@ public: void RecordBreak(GDBStub::BreakpointAddress bkpt); private: + static void InterruptHook(uc_engine* uc, u32 int_no, void* user_data); + uc_engine* uc{}; - Timing::CoreTiming& core_timing; + System& system; GDBStub::BreakpointAddress last_bkpt{}; bool last_bkpt_hit = false; }; diff --git a/src/core/core_cpu.cpp b/src/core/core_cpu.cpp index e75741db0..ba63c3e61 100644 --- a/src/core/core_cpu.cpp +++ b/src/core/core_cpu.cpp @@ -55,13 +55,13 @@ Cpu::Cpu(System& system, ExclusiveMonitor& exclusive_monitor, CpuBarrier& cpu_ba : cpu_barrier{cpu_barrier}, core_timing{system.CoreTiming()}, core_index{core_index} { if (Settings::values.use_cpu_jit) { #ifdef ARCHITECTURE_x86_64 - arm_interface = std::make_unique<ARM_Dynarmic>(core_timing, exclusive_monitor, core_index); + arm_interface = std::make_unique<ARM_Dynarmic>(system, exclusive_monitor, core_index); #else - arm_interface = std::make_unique<ARM_Unicorn>(); + arm_interface = std::make_unique<ARM_Unicorn>(system); LOG_WARNING(Core, "CPU JIT requested, but Dynarmic not available"); #endif } else { - arm_interface = std::make_unique<ARM_Unicorn>(core_timing); + arm_interface = std::make_unique<ARM_Unicorn>(system); } scheduler = std::make_unique<Kernel::Scheduler>(system, *arm_interface); diff --git a/src/core/frontend/emu_window.h b/src/core/frontend/emu_window.h index d0bcb4660..70a522556 100644 --- a/src/core/frontend/emu_window.h +++ b/src/core/frontend/emu_window.h @@ -13,6 +13,23 @@ namespace Core::Frontend { /** + * Represents a graphics context that can be used for background computation or drawing. If the + * graphics backend doesn't require the context, then the implementation of these methods can be + * stubs + */ +class GraphicsContext { +public: + /// Makes the graphics context current for the caller thread + virtual void MakeCurrent() = 0; + + /// Releases (dunno if this is the "right" word) the context from the caller thread + virtual void DoneCurrent() = 0; + + /// Swap buffers to display the next frame + virtual void SwapBuffers() = 0; +}; + +/** * Abstraction class used to provide an interface between emulation code and the frontend * (e.g. SDL, QGLWidget, GLFW, etc...). * @@ -30,7 +47,7 @@ namespace Core::Frontend { * - DO NOT TREAT THIS CLASS AS A GUI TOOLKIT ABSTRACTION LAYER. That's not what it is. Please * re-read the upper points again and think about it if you don't see this. */ -class EmuWindow { +class EmuWindow : public GraphicsContext { public: /// Data structure to store emuwindow configuration struct WindowConfig { @@ -40,17 +57,21 @@ public: std::pair<unsigned, unsigned> min_client_area_size; }; - /// Swap buffers to display the next frame - virtual void SwapBuffers() = 0; - /// Polls window events virtual void PollEvents() = 0; - /// Makes the graphics context current for the caller thread - virtual void MakeCurrent() = 0; - - /// Releases (dunno if this is the "right" word) the GLFW context from the caller thread - virtual void DoneCurrent() = 0; + /** + * Returns a GraphicsContext that the frontend provides that is shared with the emu window. This + * context can be used from other threads for background graphics computation. If the frontend + * is using a graphics backend that doesn't need anything specific to run on a different thread, + * then it can use a stubbed implemenation for GraphicsContext. + * + * If the return value is null, then the core should assume that the frontend cannot provide a + * Shared Context + */ + virtual std::unique_ptr<GraphicsContext> CreateSharedContext() const { + return nullptr; + } /** * Signal that a touch pressed event has occurred (e.g. mouse click pressed) diff --git a/src/core/hle/kernel/client_port.h b/src/core/hle/kernel/client_port.h index 6cd607206..4921ad4f0 100644 --- a/src/core/hle/kernel/client_port.h +++ b/src/core/hle/kernel/client_port.h @@ -25,7 +25,7 @@ public: return name; } - static const HandleType HANDLE_TYPE = HandleType::ClientPort; + static constexpr HandleType HANDLE_TYPE = HandleType::ClientPort; HandleType GetHandleType() const override { return HANDLE_TYPE; } diff --git a/src/core/hle/kernel/client_session.h b/src/core/hle/kernel/client_session.h index b1f39aad7..09cdff588 100644 --- a/src/core/hle/kernel/client_session.h +++ b/src/core/hle/kernel/client_session.h @@ -29,7 +29,7 @@ public: return name; } - static const HandleType HANDLE_TYPE = HandleType::ClientSession; + static constexpr HandleType HANDLE_TYPE = HandleType::ClientSession; HandleType GetHandleType() const override { return HANDLE_TYPE; } diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index f060f2a3b..dda52f4c0 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -85,7 +85,7 @@ public: return name; } - static const HandleType HANDLE_TYPE = HandleType::Process; + static constexpr HandleType HANDLE_TYPE = HandleType::Process; HandleType GetHandleType() const override { return HANDLE_TYPE; } diff --git a/src/core/hle/kernel/readable_event.h b/src/core/hle/kernel/readable_event.h index 2eb9dcbb7..84215f572 100644 --- a/src/core/hle/kernel/readable_event.h +++ b/src/core/hle/kernel/readable_event.h @@ -31,7 +31,7 @@ public: return reset_type; } - static const HandleType HANDLE_TYPE = HandleType::ReadableEvent; + static constexpr HandleType HANDLE_TYPE = HandleType::ReadableEvent; HandleType GetHandleType() const override { return HANDLE_TYPE; } diff --git a/src/core/hle/kernel/resource_limit.h b/src/core/hle/kernel/resource_limit.h index 70e09858a..2613a6bb5 100644 --- a/src/core/hle/kernel/resource_limit.h +++ b/src/core/hle/kernel/resource_limit.h @@ -41,7 +41,7 @@ public: return GetTypeName(); } - static const HandleType HANDLE_TYPE = HandleType::ResourceLimit; + static constexpr HandleType HANDLE_TYPE = HandleType::ResourceLimit; HandleType GetHandleType() const override { return HANDLE_TYPE; } diff --git a/src/core/hle/kernel/server_port.h b/src/core/hle/kernel/server_port.h index fef573b71..dc88a1ebd 100644 --- a/src/core/hle/kernel/server_port.h +++ b/src/core/hle/kernel/server_port.h @@ -43,7 +43,7 @@ public: return name; } - static const HandleType HANDLE_TYPE = HandleType::ServerPort; + static constexpr HandleType HANDLE_TYPE = HandleType::ServerPort; HandleType GetHandleType() const override { return HANDLE_TYPE; } diff --git a/src/core/hle/kernel/server_session.cpp b/src/core/hle/kernel/server_session.cpp index a6b2cf06a..696a82cd9 100644 --- a/src/core/hle/kernel/server_session.cpp +++ b/src/core/hle/kernel/server_session.cpp @@ -28,11 +28,9 @@ ServerSession::~ServerSession() { // the emulated application. // Decrease the port's connection count. - if (parent->port) + if (parent->port) { parent->port->ConnectionClosed(); - - // TODO(Subv): Wake up all the ClientSession's waiting threads and set - // the SendSyncRequest result to 0xC920181A. + } parent->server = nullptr; } @@ -74,9 +72,6 @@ void ServerSession::ClientDisconnected() { handler->ClientDisconnected(this); } - // TODO(Subv): Force a wake up of all the ServerSession's waiting threads and set - // their WaitSynchronization result to 0xC920181A. - // Clean up the list of client threads with pending requests, they are unneeded now that the // client endpoint is closed. pending_requesting_threads.clear(); diff --git a/src/core/hle/kernel/server_session.h b/src/core/hle/kernel/server_session.h index 09b835ff8..738df30f8 100644 --- a/src/core/hle/kernel/server_session.h +++ b/src/core/hle/kernel/server_session.h @@ -46,7 +46,7 @@ public: return name; } - static const HandleType HANDLE_TYPE = HandleType::ServerSession; + static constexpr HandleType HANDLE_TYPE = HandleType::ServerSession; HandleType GetHandleType() const override { return HANDLE_TYPE; } diff --git a/src/core/hle/kernel/shared_memory.h b/src/core/hle/kernel/shared_memory.h index 37e18c443..c2b6155e1 100644 --- a/src/core/hle/kernel/shared_memory.h +++ b/src/core/hle/kernel/shared_memory.h @@ -76,7 +76,7 @@ public: return name; } - static const HandleType HANDLE_TYPE = HandleType::SharedMemory; + static constexpr HandleType HANDLE_TYPE = HandleType::SharedMemory; HandleType GetHandleType() const override { return HANDLE_TYPE; } diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 2fd07ab34..d48a2203a 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -131,16 +131,15 @@ enum class ResourceLimitValueType { LimitValue, }; -ResultVal<s64> RetrieveResourceLimitValue(Handle resource_limit, u32 resource_type, - ResourceLimitValueType value_type) { +ResultVal<s64> RetrieveResourceLimitValue(Core::System& system, Handle resource_limit, + u32 resource_type, ResourceLimitValueType value_type) { const auto type = static_cast<ResourceType>(resource_type); if (!IsValidResourceType(type)) { LOG_ERROR(Kernel_SVC, "Invalid resource limit type: '{}'", resource_type); return ERR_INVALID_ENUM_VALUE; } - const auto& kernel = Core::System::GetInstance().Kernel(); - const auto* const current_process = kernel.CurrentProcess(); + const auto* const current_process = system.Kernel().CurrentProcess(); ASSERT(current_process != nullptr); const auto resource_limit_object = @@ -160,7 +159,7 @@ ResultVal<s64> RetrieveResourceLimitValue(Handle resource_limit, u32 resource_ty } // Anonymous namespace /// Set the process heap to a given Size. It can both extend and shrink the heap. -static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) { +static ResultCode SetHeapSize(Core::System& system, VAddr* heap_addr, u64 heap_size) { LOG_TRACE(Kernel_SVC, "called, heap_size=0x{:X}", heap_size); // Size must be a multiple of 0x200000 (2MB) and be equal to or less than 8GB. @@ -175,7 +174,7 @@ static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) { return ERR_INVALID_SIZE; } - auto& vm_manager = Core::System::GetInstance().Kernel().CurrentProcess()->VMManager(); + auto& vm_manager = system.Kernel().CurrentProcess()->VMManager(); const auto alloc_result = vm_manager.SetHeapSize(heap_size); if (alloc_result.Failed()) { return alloc_result.Code(); @@ -185,7 +184,7 @@ static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) { return RESULT_SUCCESS; } -static ResultCode SetMemoryPermission(VAddr addr, u64 size, u32 prot) { +static ResultCode SetMemoryPermission(Core::System& system, VAddr addr, u64 size, u32 prot) { LOG_TRACE(Kernel_SVC, "called, addr=0x{:X}, size=0x{:X}, prot=0x{:X}", addr, size, prot); if (!Common::Is4KBAligned(addr)) { @@ -217,7 +216,7 @@ static ResultCode SetMemoryPermission(VAddr addr, u64 size, u32 prot) { return ERR_INVALID_MEMORY_PERMISSIONS; } - auto* const current_process = Core::CurrentProcess(); + auto* const current_process = system.Kernel().CurrentProcess(); auto& vm_manager = current_process->VMManager(); if (!vm_manager.IsWithinAddressSpace(addr, size)) { @@ -242,7 +241,8 @@ static ResultCode SetMemoryPermission(VAddr addr, u64 size, u32 prot) { return vm_manager.ReprotectRange(addr, size, converted_permissions); } -static ResultCode SetMemoryAttribute(VAddr address, u64 size, u32 mask, u32 attribute) { +static ResultCode SetMemoryAttribute(Core::System& system, VAddr address, u64 size, u32 mask, + u32 attribute) { LOG_DEBUG(Kernel_SVC, "called, address=0x{:016X}, size=0x{:X}, mask=0x{:08X}, attribute=0x{:08X}", address, size, mask, attribute); @@ -280,7 +280,7 @@ static ResultCode SetMemoryAttribute(VAddr address, u64 size, u32 mask, u32 attr return ERR_INVALID_COMBINATION; } - auto& vm_manager = Core::CurrentProcess()->VMManager(); + auto& vm_manager = system.Kernel().CurrentProcess()->VMManager(); if (!vm_manager.IsWithinAddressSpace(address, size)) { LOG_ERROR(Kernel_SVC, "Given address (0x{:016X}) is outside the bounds of the address space.", address); @@ -291,11 +291,11 @@ static ResultCode SetMemoryAttribute(VAddr address, u64 size, u32 mask, u32 attr } /// Maps a memory range into a different range. -static ResultCode MapMemory(VAddr dst_addr, VAddr src_addr, u64 size) { +static ResultCode MapMemory(Core::System& system, VAddr dst_addr, VAddr src_addr, u64 size) { LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr, src_addr, size); - auto& vm_manager = Core::CurrentProcess()->VMManager(); + auto& vm_manager = system.Kernel().CurrentProcess()->VMManager(); const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size); if (result.IsError()) { @@ -306,11 +306,11 @@ static ResultCode MapMemory(VAddr dst_addr, VAddr src_addr, u64 size) { } /// Unmaps a region that was previously mapped with svcMapMemory -static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) { +static ResultCode UnmapMemory(Core::System& system, VAddr dst_addr, VAddr src_addr, u64 size) { LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr, src_addr, size); - auto& vm_manager = Core::CurrentProcess()->VMManager(); + auto& vm_manager = system.Kernel().CurrentProcess()->VMManager(); const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size); if (result.IsError()) { @@ -321,7 +321,8 @@ static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) { } /// Connect to an OS service given the port name, returns the handle to the port to out -static ResultCode ConnectToNamedPort(Handle* out_handle, VAddr port_name_address) { +static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle, + VAddr port_name_address) { if (!Memory::IsValidVirtualAddress(port_name_address)) { LOG_ERROR(Kernel_SVC, "Port Name Address is not a valid virtual address, port_name_address=0x{:016X}", @@ -340,8 +341,8 @@ static ResultCode ConnectToNamedPort(Handle* out_handle, VAddr port_name_address LOG_TRACE(Kernel_SVC, "called port_name={}", port_name); - auto& kernel = Core::System::GetInstance().Kernel(); - auto it = kernel.FindNamedPort(port_name); + auto& kernel = system.Kernel(); + const auto it = kernel.FindNamedPort(port_name); if (!kernel.IsValidNamedPort(it)) { LOG_WARNING(Kernel_SVC, "tried to connect to unknown port: {}", port_name); return ERR_NOT_FOUND; @@ -353,14 +354,14 @@ static ResultCode ConnectToNamedPort(Handle* out_handle, VAddr port_name_address CASCADE_RESULT(client_session, client_port->Connect()); // Return the client session - auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); CASCADE_RESULT(*out_handle, handle_table.Create(client_session)); return RESULT_SUCCESS; } /// Makes a blocking IPC call to an OS service. -static ResultCode SendSyncRequest(Handle handle) { - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); +static ResultCode SendSyncRequest(Core::System& system, Handle handle) { + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); SharedPtr<ClientSession> session = handle_table.Get<ClientSession>(handle); if (!session) { LOG_ERROR(Kernel_SVC, "called with invalid handle=0x{:08X}", handle); @@ -369,18 +370,18 @@ static ResultCode SendSyncRequest(Handle handle) { LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}({})", handle, session->GetName()); - Core::System::GetInstance().PrepareReschedule(); + system.PrepareReschedule(); // TODO(Subv): svcSendSyncRequest should put the caller thread to sleep while the server // responds and cause a reschedule. - return session->SendSyncRequest(GetCurrentThread()); + return session->SendSyncRequest(system.CurrentScheduler().GetCurrentThread()); } /// Get the ID for the specified thread. -static ResultCode GetThreadId(u64* thread_id, Handle thread_handle) { +static ResultCode GetThreadId(Core::System& system, u64* thread_id, Handle thread_handle) { LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle); if (!thread) { LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", thread_handle); @@ -392,10 +393,10 @@ static ResultCode GetThreadId(u64* thread_id, Handle thread_handle) { } /// Gets the ID of the specified process or a specified thread's owning process. -static ResultCode GetProcessId(u64* process_id, Handle handle) { +static ResultCode GetProcessId(Core::System& system, u64* process_id, Handle handle) { LOG_DEBUG(Kernel_SVC, "called handle=0x{:08X}", handle); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); const SharedPtr<Process> process = handle_table.Get<Process>(handle); if (process) { *process_id = process->GetProcessID(); @@ -437,8 +438,8 @@ static bool DefaultThreadWakeupCallback(ThreadWakeupReason reason, SharedPtr<Thr }; /// Wait for the given handles to synchronize, timeout after the specified nanoseconds -static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 handle_count, - s64 nano_seconds) { +static ResultCode WaitSynchronization(Core::System& system, Handle* index, VAddr handles_address, + u64 handle_count, s64 nano_seconds) { LOG_TRACE(Kernel_SVC, "called handles_address=0x{:X}, handle_count={}, nano_seconds={}", handles_address, handle_count, nano_seconds); @@ -457,11 +458,11 @@ static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 return ERR_OUT_OF_RANGE; } - auto* const thread = GetCurrentThread(); + auto* const thread = system.CurrentScheduler().GetCurrentThread(); using ObjectPtr = Thread::ThreadWaitObjects::value_type; Thread::ThreadWaitObjects objects(handle_count); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); for (u64 i = 0; i < handle_count; ++i) { const Handle handle = Memory::Read32(handles_address + i * sizeof(Handle)); @@ -507,16 +508,16 @@ static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 thread->WakeAfterDelay(nano_seconds); thread->SetWakeupCallback(DefaultThreadWakeupCallback); - Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule(); + system.CpuCore(thread->GetProcessorID()).PrepareReschedule(); return RESULT_TIMEOUT; } /// Resumes a thread waiting on WaitSynchronization -static ResultCode CancelSynchronization(Handle thread_handle) { +static ResultCode CancelSynchronization(Core::System& system, Handle thread_handle) { LOG_TRACE(Kernel_SVC, "called thread=0x{:X}", thread_handle); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle); if (!thread) { LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}", @@ -531,8 +532,8 @@ static ResultCode CancelSynchronization(Handle thread_handle) { } /// Attempts to locks a mutex, creating it if it does not already exist -static ResultCode ArbitrateLock(Handle holding_thread_handle, VAddr mutex_addr, - Handle requesting_thread_handle) { +static ResultCode ArbitrateLock(Core::System& system, Handle holding_thread_handle, + VAddr mutex_addr, Handle requesting_thread_handle) { LOG_TRACE(Kernel_SVC, "called holding_thread_handle=0x{:08X}, mutex_addr=0x{:X}, " "requesting_current_thread_handle=0x{:08X}", @@ -549,13 +550,13 @@ static ResultCode ArbitrateLock(Handle holding_thread_handle, VAddr mutex_addr, return ERR_INVALID_ADDRESS; } - auto* const current_process = Core::System::GetInstance().Kernel().CurrentProcess(); + auto* const current_process = system.Kernel().CurrentProcess(); return current_process->GetMutex().TryAcquire(mutex_addr, holding_thread_handle, requesting_thread_handle); } /// Unlock a mutex -static ResultCode ArbitrateUnlock(VAddr mutex_addr) { +static ResultCode ArbitrateUnlock(Core::System& system, VAddr mutex_addr) { LOG_TRACE(Kernel_SVC, "called mutex_addr=0x{:X}", mutex_addr); if (Memory::IsKernelVirtualAddress(mutex_addr)) { @@ -569,7 +570,7 @@ static ResultCode ArbitrateUnlock(VAddr mutex_addr) { return ERR_INVALID_ADDRESS; } - auto* const current_process = Core::System::GetInstance().Kernel().CurrentProcess(); + auto* const current_process = system.Kernel().CurrentProcess(); return current_process->GetMutex().Release(mutex_addr); } @@ -592,7 +593,7 @@ struct BreakReason { }; /// Break program execution -static void Break(u32 reason, u64 info1, u64 info2) { +static void Break(Core::System& system, u32 reason, u64 info1, u64 info2) { BreakReason break_reason{reason}; bool has_dumped_buffer{}; @@ -670,22 +671,24 @@ static void Break(u32 reason, u64 info1, u64 info2) { Debug_Emulated, "Emulated program broke execution! reason=0x{:016X}, info1=0x{:016X}, info2=0x{:016X}", reason, info1, info2); + handle_debug_buffer(info1, info2); - Core::System::GetInstance() - .ArmInterface(static_cast<std::size_t>(GetCurrentThread()->GetProcessorID())) - .LogBacktrace(); + + auto* const current_thread = system.CurrentScheduler().GetCurrentThread(); + const auto thread_processor_id = current_thread->GetProcessorID(); + system.ArmInterface(static_cast<std::size_t>(thread_processor_id)).LogBacktrace(); ASSERT(false); - Core::CurrentProcess()->PrepareForTermination(); + system.Kernel().CurrentProcess()->PrepareForTermination(); // Kill the current thread - GetCurrentThread()->Stop(); - Core::System::GetInstance().PrepareReschedule(); + current_thread->Stop(); + system.PrepareReschedule(); } } /// Used to output a message on a debug hardware unit - does nothing on a retail unit -static void OutputDebugString(VAddr address, u64 len) { +static void OutputDebugString([[maybe_unused]] Core::System& system, VAddr address, u64 len) { if (len == 0) { return; } @@ -696,7 +699,8 @@ static void OutputDebugString(VAddr address, u64 len) { } /// Gets system/memory information for the current process -static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) { +static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, u64 handle, + u64 info_sub_id) { LOG_TRACE(Kernel_SVC, "called info_id=0x{:X}, info_sub_id=0x{:X}, handle=0x{:08X}", info_id, info_sub_id, handle); @@ -754,7 +758,8 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) return ERR_INVALID_ENUM_VALUE; } - const auto& current_process_handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& current_process_handle_table = + system.Kernel().CurrentProcess()->GetHandleTable(); const auto process = current_process_handle_table.Get<Process>(static_cast<Handle>(handle)); if (!process) { return ERR_INVALID_HANDLE; @@ -844,7 +849,7 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) return ERR_INVALID_COMBINATION; } - Process* const current_process = Core::CurrentProcess(); + Process* const current_process = system.Kernel().CurrentProcess(); HandleTable& handle_table = current_process->GetHandleTable(); const auto resource_limit = current_process->GetResourceLimit(); if (!resource_limit) { @@ -875,7 +880,7 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) return ERR_INVALID_COMBINATION; } - *result = Core::CurrentProcess()->GetRandomEntropy(info_sub_id); + *result = system.Kernel().CurrentProcess()->GetRandomEntropy(info_sub_id); return RESULT_SUCCESS; case GetInfoType::PrivilegedProcessId: @@ -892,15 +897,14 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) return ERR_INVALID_COMBINATION; } - const auto thread = - Core::CurrentProcess()->GetHandleTable().Get<Thread>(static_cast<Handle>(handle)); + const auto thread = system.Kernel().CurrentProcess()->GetHandleTable().Get<Thread>( + static_cast<Handle>(handle)); if (!thread) { LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", static_cast<Handle>(handle)); return ERR_INVALID_HANDLE; } - const auto& system = Core::System::GetInstance(); const auto& core_timing = system.CoreTiming(); const auto& scheduler = system.CurrentScheduler(); const auto* const current_thread = scheduler.GetCurrentThread(); @@ -927,13 +931,13 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) } /// Sets the thread activity -static ResultCode SetThreadActivity(Handle handle, u32 activity) { +static ResultCode SetThreadActivity(Core::System& system, Handle handle, u32 activity) { LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, activity=0x{:08X}", handle, activity); if (activity > static_cast<u32>(ThreadActivity::Paused)) { return ERR_INVALID_ENUM_VALUE; } - const auto* current_process = Core::CurrentProcess(); + const auto* current_process = system.Kernel().CurrentProcess(); const SharedPtr<Thread> thread = current_process->GetHandleTable().Get<Thread>(handle); if (!thread) { LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle); @@ -950,7 +954,7 @@ static ResultCode SetThreadActivity(Handle handle, u32 activity) { return ERR_INVALID_HANDLE; } - if (thread == GetCurrentThread()) { + if (thread == system.CurrentScheduler().GetCurrentThread()) { LOG_ERROR(Kernel_SVC, "The thread handle specified is the current running thread"); return ERR_BUSY; } @@ -960,10 +964,10 @@ static ResultCode SetThreadActivity(Handle handle, u32 activity) { } /// Gets the thread context -static ResultCode GetThreadContext(VAddr thread_context, Handle handle) { +static ResultCode GetThreadContext(Core::System& system, VAddr thread_context, Handle handle) { LOG_DEBUG(Kernel_SVC, "called, context=0x{:08X}, thread=0x{:X}", thread_context, handle); - const auto* current_process = Core::CurrentProcess(); + const auto* current_process = system.Kernel().CurrentProcess(); const SharedPtr<Thread> thread = current_process->GetHandleTable().Get<Thread>(handle); if (!thread) { LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle); @@ -980,7 +984,7 @@ static ResultCode GetThreadContext(VAddr thread_context, Handle handle) { return ERR_INVALID_HANDLE; } - if (thread == GetCurrentThread()) { + if (thread == system.CurrentScheduler().GetCurrentThread()) { LOG_ERROR(Kernel_SVC, "The thread handle specified is the current running thread"); return ERR_BUSY; } @@ -1001,10 +1005,10 @@ static ResultCode GetThreadContext(VAddr thread_context, Handle handle) { } /// Gets the priority for the specified thread -static ResultCode GetThreadPriority(u32* priority, Handle handle) { +static ResultCode GetThreadPriority(Core::System& system, u32* priority, Handle handle) { LOG_TRACE(Kernel_SVC, "called"); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); const SharedPtr<Thread> thread = handle_table.Get<Thread>(handle); if (!thread) { LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle); @@ -1016,7 +1020,7 @@ static ResultCode GetThreadPriority(u32* priority, Handle handle) { } /// Sets the priority for the specified thread -static ResultCode SetThreadPriority(Handle handle, u32 priority) { +static ResultCode SetThreadPriority(Core::System& system, Handle handle, u32 priority) { LOG_TRACE(Kernel_SVC, "called"); if (priority > THREADPRIO_LOWEST) { @@ -1027,7 +1031,7 @@ static ResultCode SetThreadPriority(Handle handle, u32 priority) { return ERR_INVALID_THREAD_PRIORITY; } - const auto* const current_process = Core::CurrentProcess(); + const auto* const current_process = system.Kernel().CurrentProcess(); SharedPtr<Thread> thread = current_process->GetHandleTable().Get<Thread>(handle); if (!thread) { @@ -1037,18 +1041,18 @@ static ResultCode SetThreadPriority(Handle handle, u32 priority) { thread->SetPriority(priority); - Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule(); + system.CpuCore(thread->GetProcessorID()).PrepareReschedule(); return RESULT_SUCCESS; } /// Get which CPU core is executing the current thread -static u32 GetCurrentProcessorNumber() { +static u32 GetCurrentProcessorNumber(Core::System& system) { LOG_TRACE(Kernel_SVC, "called"); - return GetCurrentThread()->GetProcessorID(); + return system.CurrentScheduler().GetCurrentThread()->GetProcessorID(); } -static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size, - u32 permissions) { +static ResultCode MapSharedMemory(Core::System& system, Handle shared_memory_handle, VAddr addr, + u64 size, u32 permissions) { LOG_TRACE(Kernel_SVC, "called, shared_memory_handle=0x{:X}, addr=0x{:X}, size=0x{:X}, permissions=0x{:08X}", shared_memory_handle, addr, size, permissions); @@ -1082,7 +1086,7 @@ static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 s return ERR_INVALID_MEMORY_PERMISSIONS; } - auto* const current_process = Core::CurrentProcess(); + auto* const current_process = system.Kernel().CurrentProcess(); auto shared_memory = current_process->GetHandleTable().Get<SharedMemory>(shared_memory_handle); if (!shared_memory) { LOG_ERROR(Kernel_SVC, "Shared memory does not exist, shared_memory_handle=0x{:08X}", @@ -1100,7 +1104,8 @@ static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 s return shared_memory->Map(*current_process, addr, permissions_type, MemoryPermission::DontCare); } -static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size) { +static ResultCode UnmapSharedMemory(Core::System& system, Handle shared_memory_handle, VAddr addr, + u64 size) { LOG_WARNING(Kernel_SVC, "called, shared_memory_handle=0x{:08X}, addr=0x{:X}, size=0x{:X}", shared_memory_handle, addr, size); @@ -1125,7 +1130,7 @@ static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 return ERR_INVALID_ADDRESS_STATE; } - auto* const current_process = Core::CurrentProcess(); + auto* const current_process = system.Kernel().CurrentProcess(); auto shared_memory = current_process->GetHandleTable().Get<SharedMemory>(shared_memory_handle); if (!shared_memory) { LOG_ERROR(Kernel_SVC, "Shared memory does not exist, shared_memory_handle=0x{:08X}", @@ -1143,10 +1148,11 @@ static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 return shared_memory->Unmap(*current_process, addr, size); } -static ResultCode QueryProcessMemory(VAddr memory_info_address, VAddr page_info_address, - Handle process_handle, VAddr address) { +static ResultCode QueryProcessMemory(Core::System& system, VAddr memory_info_address, + VAddr page_info_address, Handle process_handle, + VAddr address) { LOG_TRACE(Kernel_SVC, "called process=0x{:08X} address={:X}", process_handle, address); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); SharedPtr<Process> process = handle_table.Get<Process>(process_handle); if (!process) { LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", @@ -1172,20 +1178,156 @@ static ResultCode QueryProcessMemory(VAddr memory_info_address, VAddr page_info_ return RESULT_SUCCESS; } -static ResultCode QueryMemory(VAddr memory_info_address, VAddr page_info_address, - VAddr query_address) { +static ResultCode QueryMemory(Core::System& system, VAddr memory_info_address, + VAddr page_info_address, VAddr query_address) { LOG_TRACE(Kernel_SVC, "called, memory_info_address=0x{:016X}, page_info_address=0x{:016X}, " "query_address=0x{:016X}", memory_info_address, page_info_address, query_address); - return QueryProcessMemory(memory_info_address, page_info_address, CurrentProcess, + return QueryProcessMemory(system, memory_info_address, page_info_address, CurrentProcess, query_address); } +static ResultCode MapProcessCodeMemory(Core::System& system, Handle process_handle, u64 dst_address, + u64 src_address, u64 size) { + LOG_DEBUG(Kernel_SVC, + "called. process_handle=0x{:08X}, dst_address=0x{:016X}, " + "src_address=0x{:016X}, size=0x{:016X}", + process_handle, dst_address, src_address, size); + + if (!Common::Is4KBAligned(src_address)) { + LOG_ERROR(Kernel_SVC, "src_address is not page-aligned (src_address=0x{:016X}).", + src_address); + return ERR_INVALID_ADDRESS; + } + + if (!Common::Is4KBAligned(dst_address)) { + LOG_ERROR(Kernel_SVC, "dst_address is not page-aligned (dst_address=0x{:016X}).", + dst_address); + return ERR_INVALID_ADDRESS; + } + + if (size == 0 || !Common::Is4KBAligned(size)) { + LOG_ERROR(Kernel_SVC, "Size is zero or not page-aligned (size=0x{:016X})", size); + return ERR_INVALID_SIZE; + } + + if (!IsValidAddressRange(dst_address, size)) { + LOG_ERROR(Kernel_SVC, + "Destination address range overflows the address space (dst_address=0x{:016X}, " + "size=0x{:016X}).", + dst_address, size); + return ERR_INVALID_ADDRESS_STATE; + } + + if (!IsValidAddressRange(src_address, size)) { + LOG_ERROR(Kernel_SVC, + "Source address range overflows the address space (src_address=0x{:016X}, " + "size=0x{:016X}).", + src_address, size); + return ERR_INVALID_ADDRESS_STATE; + } + + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); + auto process = handle_table.Get<Process>(process_handle); + if (!process) { + LOG_ERROR(Kernel_SVC, "Invalid process handle specified (handle=0x{:08X}).", + process_handle); + return ERR_INVALID_HANDLE; + } + + auto& vm_manager = process->VMManager(); + if (!vm_manager.IsWithinAddressSpace(src_address, size)) { + LOG_ERROR(Kernel_SVC, + "Source address range is not within the address space (src_address=0x{:016X}, " + "size=0x{:016X}).", + src_address, size); + return ERR_INVALID_ADDRESS_STATE; + } + + if (!vm_manager.IsWithinASLRRegion(dst_address, size)) { + LOG_ERROR(Kernel_SVC, + "Destination address range is not within the ASLR region (dst_address=0x{:016X}, " + "size=0x{:016X}).", + dst_address, size); + return ERR_INVALID_MEMORY_RANGE; + } + + return vm_manager.MapCodeMemory(dst_address, src_address, size); +} + +ResultCode UnmapProcessCodeMemory(Core::System& system, Handle process_handle, u64 dst_address, + u64 src_address, u64 size) { + LOG_DEBUG(Kernel_SVC, + "called. process_handle=0x{:08X}, dst_address=0x{:016X}, src_address=0x{:016X}, " + "size=0x{:016X}", + process_handle, dst_address, src_address, size); + + if (!Common::Is4KBAligned(dst_address)) { + LOG_ERROR(Kernel_SVC, "dst_address is not page-aligned (dst_address=0x{:016X}).", + dst_address); + return ERR_INVALID_ADDRESS; + } + + if (!Common::Is4KBAligned(src_address)) { + LOG_ERROR(Kernel_SVC, "src_address is not page-aligned (src_address=0x{:016X}).", + src_address); + return ERR_INVALID_ADDRESS; + } + + if (size == 0 || Common::Is4KBAligned(size)) { + LOG_ERROR(Kernel_SVC, "Size is zero or not page-aligned (size=0x{:016X}).", size); + return ERR_INVALID_SIZE; + } + + if (!IsValidAddressRange(dst_address, size)) { + LOG_ERROR(Kernel_SVC, + "Destination address range overflows the address space (dst_address=0x{:016X}, " + "size=0x{:016X}).", + dst_address, size); + return ERR_INVALID_ADDRESS_STATE; + } + + if (!IsValidAddressRange(src_address, size)) { + LOG_ERROR(Kernel_SVC, + "Source address range overflows the address space (src_address=0x{:016X}, " + "size=0x{:016X}).", + src_address, size); + return ERR_INVALID_ADDRESS_STATE; + } + + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); + auto process = handle_table.Get<Process>(process_handle); + if (!process) { + LOG_ERROR(Kernel_SVC, "Invalid process handle specified (handle=0x{:08X}).", + process_handle); + return ERR_INVALID_HANDLE; + } + + auto& vm_manager = process->VMManager(); + if (!vm_manager.IsWithinAddressSpace(src_address, size)) { + LOG_ERROR(Kernel_SVC, + "Source address range is not within the address space (src_address=0x{:016X}, " + "size=0x{:016X}).", + src_address, size); + return ERR_INVALID_ADDRESS_STATE; + } + + if (!vm_manager.IsWithinASLRRegion(dst_address, size)) { + LOG_ERROR(Kernel_SVC, + "Destination address range is not within the ASLR region (dst_address=0x{:016X}, " + "size=0x{:016X}).", + dst_address, size); + return ERR_INVALID_MEMORY_RANGE; + } + + return vm_manager.UnmapCodeMemory(dst_address, src_address, size); +} + /// Exits the current process -static void ExitProcess() { - auto* current_process = Core::CurrentProcess(); +static void ExitProcess(Core::System& system) { + auto* current_process = system.Kernel().CurrentProcess(); LOG_INFO(Kernel_SVC, "Process {} exiting", current_process->GetProcessID()); ASSERT_MSG(current_process->GetStatus() == ProcessStatus::Running, @@ -1194,20 +1336,20 @@ static void ExitProcess() { current_process->PrepareForTermination(); // Kill the current thread - GetCurrentThread()->Stop(); + system.CurrentScheduler().GetCurrentThread()->Stop(); - Core::System::GetInstance().PrepareReschedule(); + system.PrepareReschedule(); } /// Creates a new thread -static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, VAddr stack_top, - u32 priority, s32 processor_id) { +static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr entry_point, u64 arg, + VAddr stack_top, u32 priority, s32 processor_id) { LOG_TRACE(Kernel_SVC, "called entrypoint=0x{:08X}, arg=0x{:08X}, stacktop=0x{:08X}, " "threadpriority=0x{:08X}, processorid=0x{:08X} : created handle=0x{:08X}", entry_point, arg, stack_top, priority, processor_id, *out_handle); - auto* const current_process = Core::CurrentProcess(); + auto* const current_process = system.Kernel().CurrentProcess(); if (processor_id == THREADPROCESSORID_IDEAL) { // Set the target CPU to the one specified by the process. @@ -1239,7 +1381,7 @@ static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, V } const std::string name = fmt::format("thread-{:X}", entry_point); - auto& kernel = Core::System::GetInstance().Kernel(); + auto& kernel = system.Kernel(); CASCADE_RESULT(SharedPtr<Thread> thread, Thread::Create(kernel, name, entry_point, priority, arg, processor_id, stack_top, *current_process)); @@ -1253,16 +1395,16 @@ static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, V thread->SetGuestHandle(*new_guest_handle); *out_handle = *new_guest_handle; - Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule(); + system.CpuCore(thread->GetProcessorID()).PrepareReschedule(); return RESULT_SUCCESS; } /// Starts the thread for the provided handle -static ResultCode StartThread(Handle thread_handle) { +static ResultCode StartThread(Core::System& system, Handle thread_handle) { LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle); if (!thread) { LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}", @@ -1275,16 +1417,14 @@ static ResultCode StartThread(Handle thread_handle) { thread->ResumeFromWait(); if (thread->GetStatus() == ThreadStatus::Ready) { - Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule(); + system.CpuCore(thread->GetProcessorID()).PrepareReschedule(); } return RESULT_SUCCESS; } /// Called when a thread exits -static void ExitThread() { - auto& system = Core::System::GetInstance(); - +static void ExitThread(Core::System& system) { LOG_TRACE(Kernel_SVC, "called, pc=0x{:08X}", system.CurrentArmInterface().GetPC()); auto* const current_thread = system.CurrentScheduler().GetCurrentThread(); @@ -1294,7 +1434,7 @@ static void ExitThread() { } /// Sleep the current thread -static void SleepThread(s64 nanoseconds) { +static void SleepThread(Core::System& system, s64 nanoseconds) { LOG_TRACE(Kernel_SVC, "called nanoseconds={}", nanoseconds); enum class SleepType : s64 { @@ -1303,7 +1443,6 @@ static void SleepThread(s64 nanoseconds) { YieldAndWaitForLoadBalancing = -2, }; - auto& system = Core::System::GetInstance(); auto& scheduler = system.CurrentScheduler(); auto* const current_thread = scheduler.GetCurrentThread(); @@ -1332,8 +1471,9 @@ static void SleepThread(s64 nanoseconds) { } /// Wait process wide key atomic -static ResultCode WaitProcessWideKeyAtomic(VAddr mutex_addr, VAddr condition_variable_addr, - Handle thread_handle, s64 nano_seconds) { +static ResultCode WaitProcessWideKeyAtomic(Core::System& system, VAddr mutex_addr, + VAddr condition_variable_addr, Handle thread_handle, + s64 nano_seconds) { LOG_TRACE( Kernel_SVC, "called mutex_addr={:X}, condition_variable_addr={:X}, thread_handle=0x{:08X}, timeout={}", @@ -1353,7 +1493,7 @@ static ResultCode WaitProcessWideKeyAtomic(VAddr mutex_addr, VAddr condition_var return ERR_INVALID_ADDRESS; } - auto* const current_process = Core::System::GetInstance().Kernel().CurrentProcess(); + auto* const current_process = system.Kernel().CurrentProcess(); const auto& handle_table = current_process->GetHandleTable(); SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle); ASSERT(thread); @@ -1363,7 +1503,7 @@ static ResultCode WaitProcessWideKeyAtomic(VAddr mutex_addr, VAddr condition_var return release_result; } - SharedPtr<Thread> current_thread = GetCurrentThread(); + SharedPtr<Thread> current_thread = system.CurrentScheduler().GetCurrentThread(); current_thread->SetCondVarWaitAddress(condition_variable_addr); current_thread->SetMutexWaitAddress(mutex_addr); current_thread->SetWaitHandle(thread_handle); @@ -1374,19 +1514,20 @@ static ResultCode WaitProcessWideKeyAtomic(VAddr mutex_addr, VAddr condition_var // Note: Deliberately don't attempt to inherit the lock owner's priority. - Core::System::GetInstance().CpuCore(current_thread->GetProcessorID()).PrepareReschedule(); + system.CpuCore(current_thread->GetProcessorID()).PrepareReschedule(); return RESULT_SUCCESS; } /// Signal process wide key -static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target) { +static ResultCode SignalProcessWideKey(Core::System& system, VAddr condition_variable_addr, + s32 target) { LOG_TRACE(Kernel_SVC, "called, condition_variable_addr=0x{:X}, target=0x{:08X}", condition_variable_addr, target); - const auto RetrieveWaitingThreads = [](std::size_t core_index, - std::vector<SharedPtr<Thread>>& waiting_threads, - VAddr condvar_addr) { - const auto& scheduler = Core::System::GetInstance().Scheduler(core_index); + const auto RetrieveWaitingThreads = [&system](std::size_t core_index, + std::vector<SharedPtr<Thread>>& waiting_threads, + VAddr condvar_addr) { + const auto& scheduler = system.Scheduler(core_index); const auto& thread_list = scheduler.GetThreadList(); for (const auto& thread : thread_list) { @@ -1425,9 +1566,8 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target // liberate Cond Var Thread. thread->SetCondVarWaitAddress(0); - std::size_t current_core = Core::System::GetInstance().CurrentCoreIndex(); - - auto& monitor = Core::System::GetInstance().Monitor(); + const std::size_t current_core = system.CurrentCoreIndex(); + auto& monitor = system.Monitor(); // Atomically read the value of the mutex. u32 mutex_val = 0; @@ -1456,7 +1596,7 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target thread->SetLockOwner(nullptr); thread->SetMutexWaitAddress(0); thread->SetWaitHandle(0); - Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule(); + system.CpuCore(thread->GetProcessorID()).PrepareReschedule(); } else { // Atomically signal that the mutex now has a waiting thread. do { @@ -1472,7 +1612,7 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target // The mutex is already owned by some other thread, make this thread wait on it. const Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); auto owner = handle_table.Get<Thread>(owner_handle); ASSERT(owner); ASSERT(thread->GetStatus() == ThreadStatus::WaitCondVar); @@ -1487,14 +1627,17 @@ static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target } // Wait for an address (via Address Arbiter) -static ResultCode WaitForAddress(VAddr address, u32 type, s32 value, s64 timeout) { +static ResultCode WaitForAddress(Core::System& system, VAddr address, u32 type, s32 value, + s64 timeout) { LOG_WARNING(Kernel_SVC, "called, address=0x{:X}, type=0x{:X}, value=0x{:X}, timeout={}", address, type, value, timeout); + // If the passed address is a kernel virtual address, return invalid memory state. if (Memory::IsKernelVirtualAddress(address)) { LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address, address={:016X}", address); return ERR_INVALID_ADDRESS_STATE; } + // If the address is not properly aligned to 4 bytes, return invalid address. if (!Common::IsWordAligned(address)) { LOG_ERROR(Kernel_SVC, "Address is not word aligned, address={:016X}", address); @@ -1502,20 +1645,22 @@ static ResultCode WaitForAddress(VAddr address, u32 type, s32 value, s64 timeout } const auto arbitration_type = static_cast<AddressArbiter::ArbitrationType>(type); - auto& address_arbiter = - Core::System::GetInstance().Kernel().CurrentProcess()->GetAddressArbiter(); + auto& address_arbiter = system.Kernel().CurrentProcess()->GetAddressArbiter(); return address_arbiter.WaitForAddress(address, arbitration_type, value, timeout); } // Signals to an address (via Address Arbiter) -static ResultCode SignalToAddress(VAddr address, u32 type, s32 value, s32 num_to_wake) { +static ResultCode SignalToAddress(Core::System& system, VAddr address, u32 type, s32 value, + s32 num_to_wake) { LOG_WARNING(Kernel_SVC, "called, address=0x{:X}, type=0x{:X}, value=0x{:X}, num_to_wake=0x{:X}", address, type, value, num_to_wake); + // If the passed address is a kernel virtual address, return invalid memory state. if (Memory::IsKernelVirtualAddress(address)) { LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address, address={:016X}", address); return ERR_INVALID_ADDRESS_STATE; } + // If the address is not properly aligned to 4 bytes, return invalid address. if (!Common::IsWordAligned(address)) { LOG_ERROR(Kernel_SVC, "Address is not word aligned, address={:016X}", address); @@ -1523,16 +1668,15 @@ static ResultCode SignalToAddress(VAddr address, u32 type, s32 value, s32 num_to } const auto signal_type = static_cast<AddressArbiter::SignalType>(type); - auto& address_arbiter = - Core::System::GetInstance().Kernel().CurrentProcess()->GetAddressArbiter(); + auto& address_arbiter = system.Kernel().CurrentProcess()->GetAddressArbiter(); return address_arbiter.SignalToAddress(address, signal_type, value, num_to_wake); } /// This returns the total CPU ticks elapsed since the CPU was powered-on -static u64 GetSystemTick() { +static u64 GetSystemTick(Core::System& system) { LOG_TRACE(Kernel_SVC, "called"); - auto& core_timing = Core::System::GetInstance().CoreTiming(); + auto& core_timing = system.CoreTiming(); const u64 result{core_timing.GetTicks()}; // Advance time to defeat dumb games that busy-wait for the frame to end. @@ -1542,18 +1686,18 @@ static u64 GetSystemTick() { } /// Close a handle -static ResultCode CloseHandle(Handle handle) { +static ResultCode CloseHandle(Core::System& system, Handle handle) { LOG_TRACE(Kernel_SVC, "Closing handle 0x{:08X}", handle); - auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); return handle_table.Close(handle); } /// Clears the signaled state of an event or process. -static ResultCode ResetSignal(Handle handle) { +static ResultCode ResetSignal(Core::System& system, Handle handle) { LOG_DEBUG(Kernel_SVC, "called handle 0x{:08X}", handle); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); auto event = handle_table.Get<ReadableEvent>(handle); if (event) { @@ -1570,7 +1714,8 @@ static ResultCode ResetSignal(Handle handle) { } /// Creates a TransferMemory object -static ResultCode CreateTransferMemory(Handle* handle, VAddr addr, u64 size, u32 permissions) { +static ResultCode CreateTransferMemory(Core::System& system, Handle* handle, VAddr addr, u64 size, + u32 permissions) { LOG_DEBUG(Kernel_SVC, "called addr=0x{:X}, size=0x{:X}, perms=0x{:08X}", addr, size, permissions); @@ -1598,7 +1743,7 @@ static ResultCode CreateTransferMemory(Handle* handle, VAddr addr, u64 size, u32 return ERR_INVALID_MEMORY_PERMISSIONS; } - auto& kernel = Core::System::GetInstance().Kernel(); + auto& kernel = system.Kernel(); auto transfer_mem_handle = TransferMemory::Create(kernel, addr, size, perms); auto& handle_table = kernel.CurrentProcess()->GetHandleTable(); @@ -1611,7 +1756,8 @@ static ResultCode CreateTransferMemory(Handle* handle, VAddr addr, u64 size, u32 return RESULT_SUCCESS; } -static ResultCode MapTransferMemory(Handle handle, VAddr address, u64 size, u32 permission_raw) { +static ResultCode MapTransferMemory(Core::System& system, Handle handle, VAddr address, u64 size, + u32 permission_raw) { LOG_DEBUG(Kernel_SVC, "called. handle=0x{:08X}, address=0x{:016X}, size=0x{:016X}, permissions=0x{:08X}", handle, address, size, permission_raw); @@ -1645,7 +1791,7 @@ static ResultCode MapTransferMemory(Handle handle, VAddr address, u64 size, u32 return ERR_INVALID_STATE; } - const auto& kernel = Core::System::GetInstance().Kernel(); + const auto& kernel = system.Kernel(); const auto* const current_process = kernel.CurrentProcess(); const auto& handle_table = current_process->GetHandleTable(); @@ -1667,7 +1813,8 @@ static ResultCode MapTransferMemory(Handle handle, VAddr address, u64 size, u32 return transfer_memory->MapMemory(address, size, permissions); } -static ResultCode UnmapTransferMemory(Handle handle, VAddr address, u64 size) { +static ResultCode UnmapTransferMemory(Core::System& system, Handle handle, VAddr address, + u64 size) { LOG_DEBUG(Kernel_SVC, "called. handle=0x{:08X}, address=0x{:016X}, size=0x{:016X}", handle, address, size); @@ -1692,7 +1839,7 @@ static ResultCode UnmapTransferMemory(Handle handle, VAddr address, u64 size) { return ERR_INVALID_ADDRESS_STATE; } - const auto& kernel = Core::System::GetInstance().Kernel(); + const auto& kernel = system.Kernel(); const auto* const current_process = kernel.CurrentProcess(); const auto& handle_table = current_process->GetHandleTable(); @@ -1714,10 +1861,11 @@ static ResultCode UnmapTransferMemory(Handle handle, VAddr address, u64 size) { return transfer_memory->UnmapMemory(address, size); } -static ResultCode GetThreadCoreMask(Handle thread_handle, u32* core, u64* mask) { +static ResultCode GetThreadCoreMask(Core::System& system, Handle thread_handle, u32* core, + u64* mask) { LOG_TRACE(Kernel_SVC, "called, handle=0x{:08X}", thread_handle); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle); if (!thread) { LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}", @@ -1731,11 +1879,12 @@ static ResultCode GetThreadCoreMask(Handle thread_handle, u32* core, u64* mask) return RESULT_SUCCESS; } -static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) { +static ResultCode SetThreadCoreMask(Core::System& system, Handle thread_handle, u32 core, + u64 mask) { LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, mask=0x{:016X}, core=0x{:X}", thread_handle, mask, core); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle); if (!thread) { LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}", @@ -1780,8 +1929,8 @@ static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) { return RESULT_SUCCESS; } -static ResultCode CreateSharedMemory(Handle* handle, u64 size, u32 local_permissions, - u32 remote_permissions) { +static ResultCode CreateSharedMemory(Core::System& system, Handle* handle, u64 size, + u32 local_permissions, u32 remote_permissions) { LOG_TRACE(Kernel_SVC, "called, size=0x{:X}, localPerms=0x{:08X}, remotePerms=0x{:08X}", size, local_permissions, remote_permissions); if (size == 0) { @@ -1817,7 +1966,7 @@ static ResultCode CreateSharedMemory(Handle* handle, u64 size, u32 local_permiss return ERR_INVALID_MEMORY_PERMISSIONS; } - auto& kernel = Core::System::GetInstance().Kernel(); + auto& kernel = system.Kernel(); auto process = kernel.CurrentProcess(); auto& handle_table = process->GetHandleTable(); auto shared_mem_handle = SharedMemory::Create(kernel, process, size, local_perms, remote_perms); @@ -1826,10 +1975,10 @@ static ResultCode CreateSharedMemory(Handle* handle, u64 size, u32 local_permiss return RESULT_SUCCESS; } -static ResultCode CreateEvent(Handle* write_handle, Handle* read_handle) { +static ResultCode CreateEvent(Core::System& system, Handle* write_handle, Handle* read_handle) { LOG_DEBUG(Kernel_SVC, "called"); - auto& kernel = Core::System::GetInstance().Kernel(); + auto& kernel = system.Kernel(); const auto [readable_event, writable_event] = WritableEvent::CreateEventPair(kernel, ResetType::Sticky, "CreateEvent"); @@ -1854,10 +2003,10 @@ static ResultCode CreateEvent(Handle* write_handle, Handle* read_handle) { return RESULT_SUCCESS; } -static ResultCode ClearEvent(Handle handle) { +static ResultCode ClearEvent(Core::System& system, Handle handle) { LOG_TRACE(Kernel_SVC, "called, event=0x{:08X}", handle); - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); auto writable_event = handle_table.Get<WritableEvent>(handle); if (writable_event) { @@ -1875,10 +2024,10 @@ static ResultCode ClearEvent(Handle handle) { return ERR_INVALID_HANDLE; } -static ResultCode SignalEvent(Handle handle) { +static ResultCode SignalEvent(Core::System& system, Handle handle) { LOG_DEBUG(Kernel_SVC, "called. Handle=0x{:08X}", handle); - HandleTable& handle_table = Core::CurrentProcess()->GetHandleTable(); + HandleTable& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); auto writable_event = handle_table.Get<WritableEvent>(handle); if (!writable_event) { @@ -1890,7 +2039,7 @@ static ResultCode SignalEvent(Handle handle) { return RESULT_SUCCESS; } -static ResultCode GetProcessInfo(u64* out, Handle process_handle, u32 type) { +static ResultCode GetProcessInfo(Core::System& system, u64* out, Handle process_handle, u32 type) { LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, type=0x{:X}", process_handle, type); // This function currently only allows retrieving a process' status. @@ -1898,7 +2047,7 @@ static ResultCode GetProcessInfo(u64* out, Handle process_handle, u32 type) { Status, }; - const auto& handle_table = Core::CurrentProcess()->GetHandleTable(); + const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); const auto process = handle_table.Get<Process>(process_handle); if (!process) { LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", @@ -1916,10 +2065,10 @@ static ResultCode GetProcessInfo(u64* out, Handle process_handle, u32 type) { return RESULT_SUCCESS; } -static ResultCode CreateResourceLimit(Handle* out_handle) { +static ResultCode CreateResourceLimit(Core::System& system, Handle* out_handle) { LOG_DEBUG(Kernel_SVC, "called"); - auto& kernel = Core::System::GetInstance().Kernel(); + auto& kernel = system.Kernel(); auto resource_limit = ResourceLimit::Create(kernel); auto* const current_process = kernel.CurrentProcess(); @@ -1934,11 +2083,11 @@ static ResultCode CreateResourceLimit(Handle* out_handle) { return RESULT_SUCCESS; } -static ResultCode GetResourceLimitLimitValue(u64* out_value, Handle resource_limit, - u32 resource_type) { +static ResultCode GetResourceLimitLimitValue(Core::System& system, u64* out_value, + Handle resource_limit, u32 resource_type) { LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}", resource_limit, resource_type); - const auto limit_value = RetrieveResourceLimitValue(resource_limit, resource_type, + const auto limit_value = RetrieveResourceLimitValue(system, resource_limit, resource_type, ResourceLimitValueType::LimitValue); if (limit_value.Failed()) { return limit_value.Code(); @@ -1948,11 +2097,11 @@ static ResultCode GetResourceLimitLimitValue(u64* out_value, Handle resource_lim return RESULT_SUCCESS; } -static ResultCode GetResourceLimitCurrentValue(u64* out_value, Handle resource_limit, - u32 resource_type) { +static ResultCode GetResourceLimitCurrentValue(Core::System& system, u64* out_value, + Handle resource_limit, u32 resource_type) { LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}", resource_limit, resource_type); - const auto current_value = RetrieveResourceLimitValue(resource_limit, resource_type, + const auto current_value = RetrieveResourceLimitValue(system, resource_limit, resource_type, ResourceLimitValueType::CurrentValue); if (current_value.Failed()) { return current_value.Code(); @@ -1962,7 +2111,8 @@ static ResultCode GetResourceLimitCurrentValue(u64* out_value, Handle resource_l return RESULT_SUCCESS; } -static ResultCode SetResourceLimitLimitValue(Handle resource_limit, u32 resource_type, u64 value) { +static ResultCode SetResourceLimitLimitValue(Core::System& system, Handle resource_limit, + u32 resource_type, u64 value) { LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}, Value={}", resource_limit, resource_type, value); @@ -1972,8 +2122,7 @@ static ResultCode SetResourceLimitLimitValue(Handle resource_limit, u32 resource return ERR_INVALID_ENUM_VALUE; } - auto& kernel = Core::System::GetInstance().Kernel(); - auto* const current_process = kernel.CurrentProcess(); + auto* const current_process = system.Kernel().CurrentProcess(); ASSERT(current_process != nullptr); auto resource_limit_object = @@ -1997,8 +2146,8 @@ static ResultCode SetResourceLimitLimitValue(Handle resource_limit, u32 resource return RESULT_SUCCESS; } -static ResultCode GetProcessList(u32* out_num_processes, VAddr out_process_ids, - u32 out_process_ids_size) { +static ResultCode GetProcessList(Core::System& system, u32* out_num_processes, + VAddr out_process_ids, u32 out_process_ids_size) { LOG_DEBUG(Kernel_SVC, "called. out_process_ids=0x{:016X}, out_process_ids_size={}", out_process_ids, out_process_ids_size); @@ -2010,7 +2159,7 @@ static ResultCode GetProcessList(u32* out_num_processes, VAddr out_process_ids, return ERR_OUT_OF_RANGE; } - const auto& kernel = Core::System::GetInstance().Kernel(); + const auto& kernel = system.Kernel(); const auto& vm_manager = kernel.CurrentProcess()->VMManager(); const auto total_copy_size = out_process_ids_size * sizeof(u64); @@ -2034,8 +2183,8 @@ static ResultCode GetProcessList(u32* out_num_processes, VAddr out_process_ids, return RESULT_SUCCESS; } -ResultCode GetThreadList(u32* out_num_threads, VAddr out_thread_ids, u32 out_thread_ids_size, - Handle debug_handle) { +ResultCode GetThreadList(Core::System& system, u32* out_num_threads, VAddr out_thread_ids, + u32 out_thread_ids_size, Handle debug_handle) { // TODO: Handle this case when debug events are supported. UNIMPLEMENTED_IF(debug_handle != InvalidHandle); @@ -2049,7 +2198,7 @@ ResultCode GetThreadList(u32* out_num_threads, VAddr out_thread_ids, u32 out_thr return ERR_OUT_OF_RANGE; } - const auto* const current_process = Core::System::GetInstance().Kernel().CurrentProcess(); + const auto* const current_process = system.Kernel().CurrentProcess(); const auto& vm_manager = current_process->VMManager(); const auto total_copy_size = out_thread_ids_size * sizeof(u64); @@ -2076,7 +2225,7 @@ ResultCode GetThreadList(u32* out_num_threads, VAddr out_thread_ids, u32 out_thr namespace { struct FunctionDef { - using Func = void(); + using Func = void(Core::System&); u32 id; Func* func; @@ -2204,8 +2353,8 @@ static const FunctionDef SVC_Table[] = { {0x74, nullptr, "MapProcessMemory"}, {0x75, nullptr, "UnmapProcessMemory"}, {0x76, SvcWrap<QueryProcessMemory>, "QueryProcessMemory"}, - {0x77, nullptr, "MapProcessCodeMemory"}, - {0x78, nullptr, "UnmapProcessCodeMemory"}, + {0x77, SvcWrap<MapProcessCodeMemory>, "MapProcessCodeMemory"}, + {0x78, SvcWrap<UnmapProcessCodeMemory>, "UnmapProcessCodeMemory"}, {0x79, nullptr, "CreateProcess"}, {0x7A, nullptr, "StartProcess"}, {0x7B, nullptr, "TerminateProcess"}, @@ -2225,7 +2374,7 @@ static const FunctionDef* GetSVCInfo(u32 func_num) { MICROPROFILE_DEFINE(Kernel_SVC, "Kernel", "SVC", MP_RGB(70, 200, 70)); -void CallSVC(u32 immediate) { +void CallSVC(Core::System& system, u32 immediate) { MICROPROFILE_SCOPE(Kernel_SVC); // Lock the global kernel mutex when we enter the kernel HLE. @@ -2234,7 +2383,7 @@ void CallSVC(u32 immediate) { const FunctionDef* info = GetSVCInfo(immediate); if (info) { if (info->func) { - info->func(); + info->func(system); } else { LOG_CRITICAL(Kernel_SVC, "Unimplemented SVC function {}(..)", info->name); } diff --git a/src/core/hle/kernel/svc.h b/src/core/hle/kernel/svc.h index c37ae0f98..c5539ac1c 100644 --- a/src/core/hle/kernel/svc.h +++ b/src/core/hle/kernel/svc.h @@ -6,8 +6,12 @@ #include "common/common_types.h" +namespace Core { +class System; +} + namespace Kernel { -void CallSVC(u32 immediate); +void CallSVC(Core::System& system, u32 immediate); } // namespace Kernel diff --git a/src/core/hle/kernel/svc_wrap.h b/src/core/hle/kernel/svc_wrap.h index b3733680f..865473c6f 100644 --- a/src/core/hle/kernel/svc_wrap.h +++ b/src/core/hle/kernel/svc_wrap.h @@ -11,278 +11,319 @@ namespace Kernel { -static inline u64 Param(int n) { - return Core::CurrentArmInterface().GetReg(n); +static inline u64 Param(const Core::System& system, int n) { + return system.CurrentArmInterface().GetReg(n); } /** * HLE a function return from the current ARM userland process - * @param res Result to return + * @param system System context + * @param result Result to return */ -static inline void FuncReturn(u64 res) { - Core::CurrentArmInterface().SetReg(0, res); +static inline void FuncReturn(Core::System& system, u64 result) { + system.CurrentArmInterface().SetReg(0, result); } //////////////////////////////////////////////////////////////////////////////////////////////////// // Function wrappers that return type ResultCode -template <ResultCode func(u64)> -void SvcWrap() { - FuncReturn(func(Param(0)).raw); +template <ResultCode func(Core::System&, u64)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, Param(system, 0)).raw); } -template <ResultCode func(u32)> -void SvcWrap() { - FuncReturn(func(static_cast<u32>(Param(0))).raw); +template <ResultCode func(Core::System&, u32)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, static_cast<u32>(Param(system, 0))).raw); } -template <ResultCode func(u32, u32)> -void SvcWrap() { - FuncReturn(func(static_cast<u32>(Param(0)), static_cast<u32>(Param(1))).raw); +template <ResultCode func(Core::System&, u32, u32)> +void SvcWrap(Core::System& system) { + FuncReturn( + system, + func(system, static_cast<u32>(Param(system, 0)), static_cast<u32>(Param(system, 1))).raw); +} + +template <ResultCode func(Core::System&, u32, u64, u64, u64)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, static_cast<u32>(Param(system, 0)), Param(system, 1), + Param(system, 2), Param(system, 3)) + .raw); } -template <ResultCode func(u32*)> -void SvcWrap() { +template <ResultCode func(Core::System&, u32*)> +void SvcWrap(Core::System& system) { u32 param = 0; - const u32 retval = func(¶m).raw; - Core::CurrentArmInterface().SetReg(1, param); - FuncReturn(retval); + const u32 retval = func(system, ¶m).raw; + system.CurrentArmInterface().SetReg(1, param); + FuncReturn(system, retval); } -template <ResultCode func(u32*, u32)> -void SvcWrap() { +template <ResultCode func(Core::System&, u32*, u32)> +void SvcWrap(Core::System& system) { u32 param_1 = 0; - u32 retval = func(¶m_1, static_cast<u32>(Param(1))).raw; - Core::CurrentArmInterface().SetReg(1, param_1); - FuncReturn(retval); + const u32 retval = func(system, ¶m_1, static_cast<u32>(Param(system, 1))).raw; + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); } -template <ResultCode func(u32*, u32*)> -void SvcWrap() { +template <ResultCode func(Core::System&, u32*, u32*)> +void SvcWrap(Core::System& system) { u32 param_1 = 0; u32 param_2 = 0; - const u32 retval = func(¶m_1, ¶m_2).raw; + const u32 retval = func(system, ¶m_1, ¶m_2).raw; - auto& arm_interface = Core::CurrentArmInterface(); + auto& arm_interface = system.CurrentArmInterface(); arm_interface.SetReg(1, param_1); arm_interface.SetReg(2, param_2); - FuncReturn(retval); + FuncReturn(system, retval); } -template <ResultCode func(u32*, u64)> -void SvcWrap() { +template <ResultCode func(Core::System&, u32*, u64)> +void SvcWrap(Core::System& system) { u32 param_1 = 0; - const u32 retval = func(¶m_1, Param(1)).raw; - Core::CurrentArmInterface().SetReg(1, param_1); - FuncReturn(retval); + const u32 retval = func(system, ¶m_1, Param(system, 1)).raw; + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); } -template <ResultCode func(u32*, u64, u32)> -void SvcWrap() { +template <ResultCode func(Core::System&, u32*, u64, u32)> +void SvcWrap(Core::System& system) { u32 param_1 = 0; - const u32 retval = func(¶m_1, Param(1), static_cast<u32>(Param(2))).raw; - Core::CurrentArmInterface().SetReg(1, param_1); - FuncReturn(retval); + const u32 retval = + func(system, ¶m_1, Param(system, 1), static_cast<u32>(Param(system, 2))).raw; + + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); } -template <ResultCode func(u64*, u32)> -void SvcWrap() { +template <ResultCode func(Core::System&, u64*, u32)> +void SvcWrap(Core::System& system) { u64 param_1 = 0; - const u32 retval = func(¶m_1, static_cast<u32>(Param(1))).raw; - Core::CurrentArmInterface().SetReg(1, param_1); - FuncReturn(retval); + const u32 retval = func(system, ¶m_1, static_cast<u32>(Param(system, 1))).raw; + + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); } -template <ResultCode func(u64, s32)> -void SvcWrap() { - FuncReturn(func(Param(0), static_cast<s32>(Param(1))).raw); +template <ResultCode func(Core::System&, u64, s32)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, Param(system, 0), static_cast<s32>(Param(system, 1))).raw); } -template <ResultCode func(u64, u32)> -void SvcWrap() { - FuncReturn(func(Param(0), static_cast<u32>(Param(1))).raw); +template <ResultCode func(Core::System&, u64, u32)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, Param(system, 0), static_cast<u32>(Param(system, 1))).raw); } -template <ResultCode func(u64*, u64)> -void SvcWrap() { +template <ResultCode func(Core::System&, u64*, u64)> +void SvcWrap(Core::System& system) { u64 param_1 = 0; - u32 retval = func(¶m_1, Param(1)).raw; - Core::CurrentArmInterface().SetReg(1, param_1); - FuncReturn(retval); + const u32 retval = func(system, ¶m_1, Param(system, 1)).raw; + + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); } -template <ResultCode func(u64*, u32, u32)> -void SvcWrap() { +template <ResultCode func(Core::System&, u64*, u32, u32)> +void SvcWrap(Core::System& system) { u64 param_1 = 0; - u32 retval = func(¶m_1, static_cast<u32>(Param(1)), static_cast<u32>(Param(2))).raw; - Core::CurrentArmInterface().SetReg(1, param_1); - FuncReturn(retval); + const u32 retval = func(system, ¶m_1, static_cast<u32>(Param(system, 1)), + static_cast<u32>(Param(system, 2))) + .raw; + + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); } -template <ResultCode func(u32, u64)> -void SvcWrap() { - FuncReturn(func(static_cast<u32>(Param(0)), Param(1)).raw); +template <ResultCode func(Core::System&, u32, u64)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, static_cast<u32>(Param(system, 0)), Param(system, 1)).raw); } -template <ResultCode func(u32, u32, u64)> -void SvcWrap() { - FuncReturn(func(static_cast<u32>(Param(0)), static_cast<u32>(Param(1)), Param(2)).raw); +template <ResultCode func(Core::System&, u32, u32, u64)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, static_cast<u32>(Param(system, 0)), + static_cast<u32>(Param(system, 1)), Param(system, 2)) + .raw); } -template <ResultCode func(u32, u32*, u64*)> -void SvcWrap() { +template <ResultCode func(Core::System&, u32, u32*, u64*)> +void SvcWrap(Core::System& system) { u32 param_1 = 0; u64 param_2 = 0; - ResultCode retval = func(static_cast<u32>(Param(2)), ¶m_1, ¶m_2); - Core::CurrentArmInterface().SetReg(1, param_1); - Core::CurrentArmInterface().SetReg(2, param_2); - FuncReturn(retval.raw); -} + const ResultCode retval = func(system, static_cast<u32>(Param(system, 2)), ¶m_1, ¶m_2); -template <ResultCode func(u64, u64, u32, u32)> -void SvcWrap() { - FuncReturn( - func(Param(0), Param(1), static_cast<u32>(Param(2)), static_cast<u32>(Param(3))).raw); + system.CurrentArmInterface().SetReg(1, param_1); + system.CurrentArmInterface().SetReg(2, param_2); + FuncReturn(system, retval.raw); } -template <ResultCode func(u64, u64, u32, u64)> -void SvcWrap() { - FuncReturn(func(Param(0), Param(1), static_cast<u32>(Param(2)), Param(3)).raw); +template <ResultCode func(Core::System&, u64, u64, u32, u32)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, Param(system, 0), Param(system, 1), + static_cast<u32>(Param(system, 2)), static_cast<u32>(Param(system, 3))) + .raw); } -template <ResultCode func(u32, u64, u32)> -void SvcWrap() { - FuncReturn(func(static_cast<u32>(Param(0)), Param(1), static_cast<u32>(Param(2))).raw); +template <ResultCode func(Core::System&, u64, u64, u32, u64)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, Param(system, 0), Param(system, 1), + static_cast<u32>(Param(system, 2)), Param(system, 3)) + .raw); } -template <ResultCode func(u64, u64, u64)> -void SvcWrap() { - FuncReturn(func(Param(0), Param(1), Param(2)).raw); +template <ResultCode func(Core::System&, u32, u64, u32)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, static_cast<u32>(Param(system, 0)), Param(system, 1), + static_cast<u32>(Param(system, 2))) + .raw); } -template <ResultCode func(u64, u64, u32)> -void SvcWrap() { - FuncReturn(func(Param(0), Param(1), static_cast<u32>(Param(2))).raw); +template <ResultCode func(Core::System&, u64, u64, u64)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, Param(system, 0), Param(system, 1), Param(system, 2)).raw); } -template <ResultCode func(u32, u64, u64, u32)> -void SvcWrap() { +template <ResultCode func(Core::System&, u64, u64, u32)> +void SvcWrap(Core::System& system) { FuncReturn( - func(static_cast<u32>(Param(0)), Param(1), Param(2), static_cast<u32>(Param(3))).raw); + system, + func(system, Param(system, 0), Param(system, 1), static_cast<u32>(Param(system, 2))).raw); } -template <ResultCode func(u32, u64, u64)> -void SvcWrap() { - FuncReturn(func(static_cast<u32>(Param(0)), Param(1), Param(2)).raw); +template <ResultCode func(Core::System&, u32, u64, u64, u32)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, static_cast<u32>(Param(system, 0)), Param(system, 1), + Param(system, 2), static_cast<u32>(Param(system, 3))) + .raw); } -template <ResultCode func(u32*, u64, u64, s64)> -void SvcWrap() { +template <ResultCode func(Core::System&, u32, u64, u64)> +void SvcWrap(Core::System& system) { + FuncReturn( + system, + func(system, static_cast<u32>(Param(system, 0)), Param(system, 1), Param(system, 2)).raw); +} + +template <ResultCode func(Core::System&, u32*, u64, u64, s64)> +void SvcWrap(Core::System& system) { u32 param_1 = 0; - ResultCode retval = - func(¶m_1, Param(1), static_cast<u32>(Param(2)), static_cast<s64>(Param(3))); - Core::CurrentArmInterface().SetReg(1, param_1); - FuncReturn(retval.raw); + const u32 retval = func(system, ¶m_1, Param(system, 1), static_cast<u32>(Param(system, 2)), + static_cast<s64>(Param(system, 3))) + .raw; + + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); } -template <ResultCode func(u64, u64, u32, s64)> -void SvcWrap() { - FuncReturn( - func(Param(0), Param(1), static_cast<u32>(Param(2)), static_cast<s64>(Param(3))).raw); +template <ResultCode func(Core::System&, u64, u64, u32, s64)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, Param(system, 0), Param(system, 1), + static_cast<u32>(Param(system, 2)), static_cast<s64>(Param(system, 3))) + .raw); } -template <ResultCode func(u64*, u64, u64, u64)> -void SvcWrap() { +template <ResultCode func(Core::System&, u64*, u64, u64, u64)> +void SvcWrap(Core::System& system) { u64 param_1 = 0; - u32 retval = func(¶m_1, Param(1), Param(2), Param(3)).raw; - Core::CurrentArmInterface().SetReg(1, param_1); - FuncReturn(retval); + const u32 retval = + func(system, ¶m_1, Param(system, 1), Param(system, 2), Param(system, 3)).raw; + + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); } -template <ResultCode func(u32*, u64, u64, u64, u32, s32)> -void SvcWrap() { +template <ResultCode func(Core::System&, u32*, u64, u64, u64, u32, s32)> +void SvcWrap(Core::System& system) { u32 param_1 = 0; - u32 retval = func(¶m_1, Param(1), Param(2), Param(3), static_cast<u32>(Param(4)), - static_cast<s32>(Param(5))) - .raw; - Core::CurrentArmInterface().SetReg(1, param_1); - FuncReturn(retval); + const u32 retval = func(system, ¶m_1, Param(system, 1), Param(system, 2), Param(system, 3), + static_cast<u32>(Param(system, 4)), static_cast<s32>(Param(system, 5))) + .raw; + + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); } -template <ResultCode func(u32*, u64, u64, u32)> -void SvcWrap() { +template <ResultCode func(Core::System&, u32*, u64, u64, u32)> +void SvcWrap(Core::System& system) { u32 param_1 = 0; - u32 retval = func(¶m_1, Param(1), Param(2), static_cast<u32>(Param(3))).raw; - Core::CurrentArmInterface().SetReg(1, param_1); - FuncReturn(retval); + const u32 retval = func(system, ¶m_1, Param(system, 1), Param(system, 2), + static_cast<u32>(Param(system, 3))) + .raw; + + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); } -template <ResultCode func(Handle*, u64, u32, u32)> -void SvcWrap() { +template <ResultCode func(Core::System&, Handle*, u64, u32, u32)> +void SvcWrap(Core::System& system) { u32 param_1 = 0; - u32 retval = - func(¶m_1, Param(1), static_cast<u32>(Param(2)), static_cast<u32>(Param(3))).raw; - Core::CurrentArmInterface().SetReg(1, param_1); - FuncReturn(retval); + const u32 retval = func(system, ¶m_1, Param(system, 1), static_cast<u32>(Param(system, 2)), + static_cast<u32>(Param(system, 3))) + .raw; + + system.CurrentArmInterface().SetReg(1, param_1); + FuncReturn(system, retval); } -template <ResultCode func(u64, u32, s32, s64)> -void SvcWrap() { - FuncReturn(func(Param(0), static_cast<u32>(Param(1)), static_cast<s32>(Param(2)), - static_cast<s64>(Param(3))) - .raw); +template <ResultCode func(Core::System&, u64, u32, s32, s64)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, Param(system, 0), static_cast<u32>(Param(system, 1)), + static_cast<s32>(Param(system, 2)), static_cast<s64>(Param(system, 3))) + .raw); } -template <ResultCode func(u64, u32, s32, s32)> -void SvcWrap() { - FuncReturn(func(Param(0), static_cast<u32>(Param(1)), static_cast<s32>(Param(2)), - static_cast<s32>(Param(3))) - .raw); +template <ResultCode func(Core::System&, u64, u32, s32, s32)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system, Param(system, 0), static_cast<u32>(Param(system, 1)), + static_cast<s32>(Param(system, 2)), static_cast<s32>(Param(system, 3))) + .raw); } //////////////////////////////////////////////////////////////////////////////////////////////////// // Function wrappers that return type u32 -template <u32 func()> -void SvcWrap() { - FuncReturn(func()); +template <u32 func(Core::System&)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system)); } //////////////////////////////////////////////////////////////////////////////////////////////////// // Function wrappers that return type u64 -template <u64 func()> -void SvcWrap() { - FuncReturn(func()); +template <u64 func(Core::System&)> +void SvcWrap(Core::System& system) { + FuncReturn(system, func(system)); } //////////////////////////////////////////////////////////////////////////////////////////////////// /// Function wrappers that return type void -template <void func()> -void SvcWrap() { - func(); +template <void func(Core::System&)> +void SvcWrap(Core::System& system) { + func(system); } -template <void func(s64)> -void SvcWrap() { - func(static_cast<s64>(Param(0))); +template <void func(Core::System&, s64)> +void SvcWrap(Core::System& system) { + func(system, static_cast<s64>(Param(system, 0))); } -template <void func(u64, u64 len)> -void SvcWrap() { - func(Param(0), Param(1)); +template <void func(Core::System&, u64, u64)> +void SvcWrap(Core::System& system) { + func(system, Param(system, 0), Param(system, 1)); } -template <void func(u64, u64, u64)> -void SvcWrap() { - func(Param(0), Param(1), Param(2)); +template <void func(Core::System&, u64, u64, u64)> +void SvcWrap(Core::System& system) { + func(system, Param(system, 0), Param(system, 1), Param(system, 2)); } -template <void func(u32, u64, u64)> -void SvcWrap() { - func(static_cast<u32>(Param(0)), Param(1), Param(2)); +template <void func(Core::System&, u32, u64, u64)> +void SvcWrap(Core::System& system) { + func(system, static_cast<u32>(Param(system, 0)), Param(system, 1), Param(system, 2)); } } // namespace Kernel diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 1b891f632..ca52267b2 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -220,11 +220,6 @@ void Thread::SetPriority(u32 priority) { UpdatePriority(); } -void Thread::BoostPriority(u32 priority) { - scheduler->SetThreadPriority(this, priority); - current_priority = priority; -} - void Thread::SetWaitSynchronizationResult(ResultCode result) { context.cpu_registers[0] = result.raw; } diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 73e5d1bb4..32026d7f0 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -106,7 +106,7 @@ public: return "Thread"; } - static const HandleType HANDLE_TYPE = HandleType::Thread; + static constexpr HandleType HANDLE_TYPE = HandleType::Thread; HandleType GetHandleType() const override { return HANDLE_TYPE; } @@ -136,12 +136,6 @@ public: */ void SetPriority(u32 priority); - /** - * Temporarily boosts the thread's priority until the next time it is scheduled - * @param priority The new priority - */ - void BoostPriority(u32 priority); - /// Adds a thread to the list of threads that are waiting for a lock held by this thread. void AddMutexWaiter(SharedPtr<Thread> thread); diff --git a/src/core/hle/kernel/vm_manager.cpp b/src/core/hle/kernel/vm_manager.cpp index ec0a480ce..f0c0c12fc 100644 --- a/src/core/hle/kernel/vm_manager.cpp +++ b/src/core/hle/kernel/vm_manager.cpp @@ -302,6 +302,86 @@ ResultVal<VAddr> VMManager::SetHeapSize(u64 size) { return MakeResult<VAddr>(heap_region_base); } +ResultCode VMManager::MapCodeMemory(VAddr dst_address, VAddr src_address, u64 size) { + constexpr auto ignore_attribute = MemoryAttribute::LockedForIPC | MemoryAttribute::DeviceMapped; + const auto src_check_result = CheckRangeState( + src_address, size, MemoryState::All, MemoryState::Heap, VMAPermission::All, + VMAPermission::ReadWrite, MemoryAttribute::Mask, MemoryAttribute::None, ignore_attribute); + + if (src_check_result.Failed()) { + return src_check_result.Code(); + } + + const auto mirror_result = + MirrorMemory(dst_address, src_address, size, MemoryState::ModuleCode); + if (mirror_result.IsError()) { + return mirror_result; + } + + // Ensure we lock the source memory region. + const auto src_vma_result = CarveVMARange(src_address, size); + if (src_vma_result.Failed()) { + return src_vma_result.Code(); + } + auto src_vma_iter = *src_vma_result; + src_vma_iter->second.attribute = MemoryAttribute::Locked; + Reprotect(src_vma_iter, VMAPermission::Read); + + // The destination memory region is fine as is, however we need to make it read-only. + return ReprotectRange(dst_address, size, VMAPermission::Read); +} + +ResultCode VMManager::UnmapCodeMemory(VAddr dst_address, VAddr src_address, u64 size) { + constexpr auto ignore_attribute = MemoryAttribute::LockedForIPC | MemoryAttribute::DeviceMapped; + const auto src_check_result = CheckRangeState( + src_address, size, MemoryState::All, MemoryState::Heap, VMAPermission::None, + VMAPermission::None, MemoryAttribute::Mask, MemoryAttribute::Locked, ignore_attribute); + + if (src_check_result.Failed()) { + return src_check_result.Code(); + } + + // Yes, the kernel only checks the first page of the region. + const auto dst_check_result = + CheckRangeState(dst_address, Memory::PAGE_SIZE, MemoryState::FlagModule, + MemoryState::FlagModule, VMAPermission::None, VMAPermission::None, + MemoryAttribute::Mask, MemoryAttribute::None, ignore_attribute); + + if (dst_check_result.Failed()) { + return dst_check_result.Code(); + } + + const auto dst_memory_state = std::get<MemoryState>(*dst_check_result); + const auto dst_contiguous_check_result = CheckRangeState( + dst_address, size, MemoryState::All, dst_memory_state, VMAPermission::None, + VMAPermission::None, MemoryAttribute::Mask, MemoryAttribute::None, ignore_attribute); + + if (dst_contiguous_check_result.Failed()) { + return dst_contiguous_check_result.Code(); + } + + const auto unmap_result = UnmapRange(dst_address, size); + if (unmap_result.IsError()) { + return unmap_result; + } + + // With the mirrored portion unmapped, restore the original region's traits. + const auto src_vma_result = CarveVMARange(src_address, size); + if (src_vma_result.Failed()) { + return src_vma_result.Code(); + } + auto src_vma_iter = *src_vma_result; + src_vma_iter->second.state = MemoryState::Heap; + src_vma_iter->second.attribute = MemoryAttribute::None; + Reprotect(src_vma_iter, VMAPermission::ReadWrite); + + if (dst_memory_state == MemoryState::ModuleCode) { + Core::System::GetInstance().InvalidateCpuInstructionCaches(); + } + + return unmap_result; +} + MemoryInfo VMManager::QueryMemory(VAddr address) const { const auto vma = FindVMA(address); MemoryInfo memory_info{}; diff --git a/src/core/hle/kernel/vm_manager.h b/src/core/hle/kernel/vm_manager.h index 6f484b7bf..288eb9450 100644 --- a/src/core/hle/kernel/vm_manager.h +++ b/src/core/hle/kernel/vm_manager.h @@ -43,6 +43,9 @@ enum class VMAPermission : u8 { ReadExecute = Read | Execute, WriteExecute = Write | Execute, ReadWriteExecute = Read | Write | Execute, + + // Used as a wildcard when checking permissions across memory ranges + All = 0xFF, }; constexpr VMAPermission operator|(VMAPermission lhs, VMAPermission rhs) { @@ -152,6 +155,9 @@ enum class MemoryState : u32 { FlagUncached = 1U << 24, FlagCodeMemory = 1U << 25, + // Wildcard used in range checking to indicate all states. + All = 0xFFFFFFFF, + // Convenience flag sets to reduce repetition IPCFlags = FlagIPC0 | FlagIPC3 | FlagIPC1, @@ -415,6 +421,49 @@ public: /// ResultVal<VAddr> SetHeapSize(u64 size); + /// Maps a region of memory as code memory. + /// + /// @param dst_address The base address of the region to create the aliasing memory region. + /// @param src_address The base address of the region to be aliased. + /// @param size The total amount of memory to map in bytes. + /// + /// @pre Both memory regions lie within the actual addressable address space. + /// + /// @post After this function finishes execution, assuming success, then the address range + /// [dst_address, dst_address+size) will alias the memory region, + /// [src_address, src_address+size). + /// <p> + /// What this also entails is as follows: + /// 1. The aliased region gains the Locked memory attribute. + /// 2. The aliased region becomes read-only. + /// 3. The aliasing region becomes read-only. + /// 4. The aliasing region is created with a memory state of MemoryState::CodeModule. + /// + ResultCode MapCodeMemory(VAddr dst_address, VAddr src_address, u64 size); + + /// Unmaps a region of memory designated as code module memory. + /// + /// @param dst_address The base address of the memory region aliasing the source memory region. + /// @param src_address The base address of the memory region being aliased. + /// @param size The size of the memory region to unmap in bytes. + /// + /// @pre Both memory ranges lie within the actual addressable address space. + /// + /// @pre The memory region being unmapped has been previously been mapped + /// by a call to MapCodeMemory. + /// + /// @post After execution of the function, if successful. the aliasing memory region + /// will be unmapped and the aliased region will have various traits about it + /// restored to what they were prior to the original mapping call preceding + /// this function call. + /// <p> + /// What this also entails is as follows: + /// 1. The state of the memory region will now indicate a general heap region. + /// 2. All memory attributes for the memory region are cleared. + /// 3. Memory permissions for the region are restored to user read/write. + /// + ResultCode UnmapCodeMemory(VAddr dst_address, VAddr src_address, u64 size); + /// Queries the memory manager for information about the given address. /// /// @param address The address to query the memory manager about for information. diff --git a/src/core/hle/kernel/writable_event.h b/src/core/hle/kernel/writable_event.h index c9068dd3d..d00c92a6b 100644 --- a/src/core/hle/kernel/writable_event.h +++ b/src/core/hle/kernel/writable_event.h @@ -37,7 +37,7 @@ public: return name; } - static const HandleType HANDLE_TYPE = HandleType::WritableEvent; + static constexpr HandleType HANDLE_TYPE = HandleType::WritableEvent; HandleType GetHandleType() const override { return HANDLE_TYPE; } diff --git a/src/core/hle/service/filesystem/fsp_srv.cpp b/src/core/hle/service/filesystem/fsp_srv.cpp index 65d2c2802..e7df8fd98 100644 --- a/src/core/hle/service/filesystem/fsp_srv.cpp +++ b/src/core/hle/service/filesystem/fsp_srv.cpp @@ -115,11 +115,12 @@ private: void Read(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const u64 unk = rp.Pop<u64>(); + const u64 option = rp.Pop<u64>(); const s64 offset = rp.Pop<s64>(); const s64 length = rp.Pop<s64>(); - LOG_DEBUG(Service_FS, "called, offset=0x{:X}, length={}", offset, length); + LOG_DEBUG(Service_FS, "called, option={}, offset=0x{:X}, length={}", option, offset, + length); // Error checking if (length < 0) { @@ -148,11 +149,12 @@ private: void Write(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const u64 unk = rp.Pop<u64>(); + const u64 option = rp.Pop<u64>(); const s64 offset = rp.Pop<s64>(); const s64 length = rp.Pop<s64>(); - LOG_DEBUG(Service_FS, "called, offset=0x{:X}, length={}", offset, length); + LOG_DEBUG(Service_FS, "called, option={}, offset=0x{:X}, length={}", option, offset, + length); // Error checking if (length < 0) { @@ -250,10 +252,7 @@ private: u64 next_entry_index = 0; void Read(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const u64 unk = rp.Pop<u64>(); - - LOG_DEBUG(Service_FS, "called, unk=0x{:X}", unk); + LOG_DEBUG(Service_FS, "called."); // Calculate how many entries we can fit in the output buffer const u64 count_entries = ctx.GetWriteBufferSize() / sizeof(FileSys::Entry); diff --git a/src/core/hle/service/ldr/ldr.cpp b/src/core/hle/service/ldr/ldr.cpp index d6fb9524c..5af925515 100644 --- a/src/core/hle/service/ldr/ldr.cpp +++ b/src/core/hle/service/ldr/ldr.cpp @@ -94,12 +94,18 @@ public: } void LoadNrr(Kernel::HLERequestContext& ctx) { + struct Parameters { + u64_le process_id; + u64_le nrr_address; + u64_le nrr_size; + }; + IPC::RequestParser rp{ctx}; - rp.Skip(2, false); - const VAddr nrr_addr{rp.Pop<VAddr>()}; - const u64 nrr_size{rp.Pop<u64>()}; - LOG_DEBUG(Service_LDR, "called with nrr_addr={:016X}, nrr_size={:016X}", nrr_addr, - nrr_size); + const auto [process_id, nrr_address, nrr_size] = rp.PopRaw<Parameters>(); + + LOG_DEBUG(Service_LDR, + "called with process_id={:016X}, nrr_address={:016X}, nrr_size={:016X}", + process_id, nrr_address, nrr_size); if (!initialized) { LOG_ERROR(Service_LDR, "LDR:RO not initialized before use!"); @@ -117,24 +123,26 @@ public: } // NRR Address does not fall on 0x1000 byte boundary - if (!Common::Is4KBAligned(nrr_addr)) { - LOG_ERROR(Service_LDR, "NRR Address has invalid alignment (actual {:016X})!", nrr_addr); + if (!Common::Is4KBAligned(nrr_address)) { + LOG_ERROR(Service_LDR, "NRR Address has invalid alignment (actual {:016X})!", + nrr_address); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ERROR_INVALID_ALIGNMENT); return; } // NRR Size is zero or causes overflow - if (nrr_addr + nrr_size <= nrr_addr || nrr_size == 0 || !Common::Is4KBAligned(nrr_size)) { + if (nrr_address + nrr_size <= nrr_address || nrr_size == 0 || + !Common::Is4KBAligned(nrr_size)) { LOG_ERROR(Service_LDR, "NRR Size is invalid! (nrr_address={:016X}, nrr_size={:016X})", - nrr_addr, nrr_size); + nrr_address, nrr_size); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ERROR_INVALID_SIZE); return; } // Read NRR data from memory std::vector<u8> nrr_data(nrr_size); - Memory::ReadBlock(nrr_addr, nrr_data.data(), nrr_size); + Memory::ReadBlock(nrr_address, nrr_data.data(), nrr_size); NRRHeader header; std::memcpy(&header, nrr_data.data(), sizeof(NRRHeader)); @@ -175,7 +183,7 @@ public: hashes.emplace_back(hash); } - nrr.insert_or_assign(nrr_addr, std::move(hashes)); + nrr.insert_or_assign(nrr_address, std::move(hashes)); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(RESULT_SUCCESS); @@ -189,23 +197,30 @@ public: return; } + struct Parameters { + u64_le process_id; + u64_le nrr_address; + }; + IPC::RequestParser rp{ctx}; - rp.Skip(2, false); - const auto nrr_addr{rp.Pop<VAddr>()}; - LOG_DEBUG(Service_LDR, "called with nrr_addr={:016X}", nrr_addr); + const auto [process_id, nrr_address] = rp.PopRaw<Parameters>(); + + LOG_DEBUG(Service_LDR, "called with process_id={:016X}, nrr_addr={:016X}", process_id, + nrr_address); - if (!Common::Is4KBAligned(nrr_addr)) { - LOG_ERROR(Service_LDR, "NRR Address has invalid alignment (actual {:016X})!", nrr_addr); + if (!Common::Is4KBAligned(nrr_address)) { + LOG_ERROR(Service_LDR, "NRR Address has invalid alignment (actual {:016X})!", + nrr_address); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ERROR_INVALID_ALIGNMENT); return; } - const auto iter = nrr.find(nrr_addr); + const auto iter = nrr.find(nrr_address); if (iter == nrr.end()) { LOG_ERROR(Service_LDR, "Attempting to unload NRR which has not been loaded! (addr={:016X})", - nrr_addr); + nrr_address); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ERROR_INVALID_NRR_ADDRESS); return; @@ -217,16 +232,22 @@ public: } void LoadNro(Kernel::HLERequestContext& ctx) { + struct Parameters { + u64_le process_id; + u64_le image_address; + u64_le image_size; + u64_le bss_address; + u64_le bss_size; + }; + IPC::RequestParser rp{ctx}; - rp.Skip(2, false); - const VAddr nro_addr{rp.Pop<VAddr>()}; - const u64 nro_size{rp.Pop<u64>()}; - const VAddr bss_addr{rp.Pop<VAddr>()}; - const u64 bss_size{rp.Pop<u64>()}; - LOG_DEBUG( - Service_LDR, - "called with nro_addr={:016X}, nro_size={:016X}, bss_addr={:016X}, bss_size={:016X}", - nro_addr, nro_size, bss_addr, bss_size); + const auto [process_id, nro_address, nro_size, bss_address, bss_size] = + rp.PopRaw<Parameters>(); + + LOG_DEBUG(Service_LDR, + "called with pid={:016X}, nro_addr={:016X}, nro_size={:016X}, bss_addr={:016X}, " + "bss_size={:016X}", + process_id, nro_address, nro_size, bss_address, bss_size); if (!initialized) { LOG_ERROR(Service_LDR, "LDR:RO not initialized before use!"); @@ -244,8 +265,9 @@ public: } // NRO Address does not fall on 0x1000 byte boundary - if (!Common::Is4KBAligned(nro_addr)) { - LOG_ERROR(Service_LDR, "NRO Address has invalid alignment (actual {:016X})!", nro_addr); + if (!Common::Is4KBAligned(nro_address)) { + LOG_ERROR(Service_LDR, "NRO Address has invalid alignment (actual {:016X})!", + nro_address); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ERROR_INVALID_ALIGNMENT); return; @@ -253,15 +275,15 @@ public: // NRO Size or BSS Size is zero or causes overflow const auto nro_size_valid = - nro_size != 0 && nro_addr + nro_size > nro_addr && Common::Is4KBAligned(nro_size); - const auto bss_size_valid = - nro_size + bss_size >= nro_size && (bss_size == 0 || bss_addr + bss_size > bss_addr); + nro_size != 0 && nro_address + nro_size > nro_address && Common::Is4KBAligned(nro_size); + const auto bss_size_valid = nro_size + bss_size >= nro_size && + (bss_size == 0 || bss_address + bss_size > bss_address); if (!nro_size_valid || !bss_size_valid) { LOG_ERROR(Service_LDR, "NRO Size or BSS Size is invalid! (nro_address={:016X}, nro_size={:016X}, " "bss_address={:016X}, bss_size={:016X})", - nro_addr, nro_size, bss_addr, bss_size); + nro_address, nro_size, bss_address, bss_size); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ERROR_INVALID_SIZE); return; @@ -269,7 +291,7 @@ public: // Read NRO data from memory std::vector<u8> nro_data(nro_size); - Memory::ReadBlock(nro_addr, nro_data.data(), nro_size); + Memory::ReadBlock(nro_address, nro_data.data(), nro_size); SHA256Hash hash{}; mbedtls_sha256(nro_data.data(), nro_data.size(), hash.data(), 0); @@ -319,17 +341,18 @@ public: return; } - ASSERT(vm_manager - .MirrorMemory(*map_address, nro_addr, nro_size, Kernel::MemoryState::ModuleCode) - .IsSuccess()); - ASSERT(vm_manager.UnmapRange(nro_addr, nro_size).IsSuccess()); + ASSERT( + vm_manager + .MirrorMemory(*map_address, nro_address, nro_size, Kernel::MemoryState::ModuleCode) + .IsSuccess()); + ASSERT(vm_manager.UnmapRange(nro_address, nro_size).IsSuccess()); if (bss_size > 0) { ASSERT(vm_manager - .MirrorMemory(*map_address + nro_size, bss_addr, bss_size, + .MirrorMemory(*map_address + nro_size, bss_address, bss_size, Kernel::MemoryState::ModuleCode) .IsSuccess()); - ASSERT(vm_manager.UnmapRange(bss_addr, bss_size).IsSuccess()); + ASSERT(vm_manager.UnmapRange(bss_address, bss_size).IsSuccess()); } vm_manager.ReprotectRange(*map_address, header.text_size, @@ -349,13 +372,6 @@ public: } void UnloadNro(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - rp.Skip(2, false); - const VAddr mapped_addr{rp.PopRaw<VAddr>()}; - const VAddr heap_addr{rp.PopRaw<VAddr>()}; - LOG_DEBUG(Service_LDR, "called with mapped_addr={:016X}, heap_addr={:016X}", mapped_addr, - heap_addr); - if (!initialized) { LOG_ERROR(Service_LDR, "LDR:RO not initialized before use!"); IPC::ResponseBuilder rb{ctx, 2}; @@ -363,22 +379,30 @@ public: return; } - if (!Common::Is4KBAligned(mapped_addr) || !Common::Is4KBAligned(heap_addr)) { - LOG_ERROR(Service_LDR, - "NRO/BSS Address has invalid alignment (actual nro_addr={:016X}, " - "bss_addr={:016X})!", - mapped_addr, heap_addr); + struct Parameters { + u64_le process_id; + u64_le nro_address; + }; + + IPC::RequestParser rp{ctx}; + const auto [process_id, nro_address] = rp.PopRaw<Parameters>(); + LOG_DEBUG(Service_LDR, "called with process_id={:016X}, nro_address=0x{:016X}", process_id, + nro_address); + + if (!Common::Is4KBAligned(nro_address)) { + LOG_ERROR(Service_LDR, "NRO address has invalid alignment (nro_address=0x{:016X})", + nro_address); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ERROR_INVALID_ALIGNMENT); return; } - const auto iter = nro.find(mapped_addr); + const auto iter = nro.find(nro_address); if (iter == nro.end()) { LOG_ERROR(Service_LDR, - "The NRO attempting to unmap was not mapped or has an invalid address " - "(actual {:016X})!", - mapped_addr); + "The NRO attempting to be unmapped was not mapped or has an invalid address " + "(nro_address=0x{:016X})!", + nro_address); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ERROR_INVALID_NRO_ADDRESS); return; @@ -387,10 +411,7 @@ public: auto& vm_manager = Core::CurrentProcess()->VMManager(); const auto& nro_size = iter->second.size; - ASSERT(vm_manager - .MirrorMemory(heap_addr, mapped_addr, nro_size, Kernel::MemoryState::ModuleCode) - .IsSuccess()); - ASSERT(vm_manager.UnmapRange(mapped_addr, nro_size).IsSuccess()); + ASSERT(vm_manager.UnmapRange(nro_address, nro_size).IsSuccess()); Core::System::GetInstance().InvalidateCpuInstructionCaches(); @@ -460,11 +481,10 @@ private: std::map<VAddr, NROInfo> nro; std::map<VAddr, std::vector<SHA256Hash>> nrr; - bool IsValidNROHash(const SHA256Hash& hash) { - return std::any_of( - nrr.begin(), nrr.end(), [&hash](const std::pair<VAddr, std::vector<SHA256Hash>>& p) { - return std::find(p.second.begin(), p.second.end(), hash) != p.second.end(); - }); + bool IsValidNROHash(const SHA256Hash& hash) const { + return std::any_of(nrr.begin(), nrr.end(), [&hash](const auto& p) { + return std::find(p.second.begin(), p.second.end(), hash) != p.second.end(); + }); } static bool IsValidNRO(const NROHeader& header, u64 nro_size, u64 bss_size) { diff --git a/src/core/hle/service/nvflinger/nvflinger.cpp b/src/core/hle/service/nvflinger/nvflinger.cpp index c7f5bbf28..3c5c53e24 100644 --- a/src/core/hle/service/nvflinger/nvflinger.cpp +++ b/src/core/hle/service/nvflinger/nvflinger.cpp @@ -21,12 +21,13 @@ #include "core/hle/service/vi/display/vi_display.h" #include "core/hle/service/vi/layer/vi_layer.h" #include "core/perf_stats.h" +#include "core/settings.h" #include "video_core/renderer_base.h" namespace Service::NVFlinger { -constexpr std::size_t SCREEN_REFRESH_RATE = 60; -constexpr s64 frame_ticks = static_cast<s64>(Core::Timing::BASE_CLOCK_RATE / SCREEN_REFRESH_RATE); +constexpr s64 frame_ticks = static_cast<s64>(Core::Timing::BASE_CLOCK_RATE / 60); +constexpr s64 frame_ticks_30fps = static_cast<s64>(Core::Timing::BASE_CLOCK_RATE / 30); NVFlinger::NVFlinger(Core::Timing::CoreTiming& core_timing) : core_timing{core_timing} { displays.emplace_back(0, "Default"); @@ -36,13 +37,15 @@ NVFlinger::NVFlinger(Core::Timing::CoreTiming& core_timing) : core_timing{core_t displays.emplace_back(4, "Null"); // Schedule the screen composition events - composition_event = - core_timing.RegisterEvent("ScreenComposition", [this](u64 userdata, s64 cycles_late) { + const auto ticks = Settings::values.force_30fps_mode ? frame_ticks_30fps : frame_ticks; + + composition_event = core_timing.RegisterEvent( + "ScreenComposition", [this, ticks](u64 userdata, s64 cycles_late) { Compose(); - this->core_timing.ScheduleEvent(frame_ticks - cycles_late, composition_event); + this->core_timing.ScheduleEvent(ticks - cycles_late, composition_event); }); - core_timing.ScheduleEvent(frame_ticks, composition_event); + core_timing.ScheduleEvent(ticks, composition_event); } NVFlinger::~NVFlinger() { @@ -62,6 +65,7 @@ std::optional<u64> NVFlinger::OpenDisplay(std::string_view name) { const auto itr = std::find_if(displays.begin(), displays.end(), [&](const VI::Display& display) { return display.GetName() == name; }); + if (itr == displays.end()) { return {}; } diff --git a/src/core/settings.h b/src/core/settings.h index d543eb32f..b84390745 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -393,6 +393,7 @@ struct Values { bool use_disk_shader_cache; bool use_accurate_gpu_emulation; bool use_asynchronous_gpu_emulation; + bool force_30fps_mode; float bg_red; float bg_green; |