Merge pull request #4996 from bunnei/use-4jits
Kernel: Refactor to use 4-instances of Dynarmic & various cleanups and improvements
This commit is contained in:
commit
69aaad9b96
|
@ -5,18 +5,20 @@
|
||||||
#include "common/assert.h"
|
#include "common/assert.h"
|
||||||
#include "common/fiber.h"
|
#include "common/fiber.h"
|
||||||
#include "common/spin_lock.h"
|
#include "common/spin_lock.h"
|
||||||
|
#include "common/virtual_buffer.h"
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(WIN32)
|
|
||||||
#include <windows.h>
|
|
||||||
#else
|
|
||||||
#include <boost/context/detail/fcontext.hpp>
|
#include <boost/context/detail/fcontext.hpp>
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
|
||||||
constexpr std::size_t default_stack_size = 256 * 1024; // 256kb
|
constexpr std::size_t default_stack_size = 256 * 1024;
|
||||||
|
|
||||||
struct Fiber::FiberImpl {
|
struct Fiber::FiberImpl {
|
||||||
|
FiberImpl() : stack{default_stack_size}, rewind_stack{default_stack_size} {}
|
||||||
|
|
||||||
|
VirtualBuffer<u8> stack;
|
||||||
|
VirtualBuffer<u8> rewind_stack;
|
||||||
|
|
||||||
SpinLock guard{};
|
SpinLock guard{};
|
||||||
std::function<void(void*)> entry_point;
|
std::function<void(void*)> entry_point;
|
||||||
std::function<void(void*)> rewind_point;
|
std::function<void(void*)> rewind_point;
|
||||||
|
@ -26,17 +28,10 @@ struct Fiber::FiberImpl {
|
||||||
bool is_thread_fiber{};
|
bool is_thread_fiber{};
|
||||||
bool released{};
|
bool released{};
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(WIN32)
|
u8* stack_limit{};
|
||||||
LPVOID handle = nullptr;
|
u8* rewind_stack_limit{};
|
||||||
LPVOID rewind_handle = nullptr;
|
boost::context::detail::fcontext_t context{};
|
||||||
#else
|
boost::context::detail::fcontext_t rewind_context{};
|
||||||
alignas(64) std::array<u8, default_stack_size> stack;
|
|
||||||
alignas(64) std::array<u8, default_stack_size> rewind_stack;
|
|
||||||
u8* stack_limit;
|
|
||||||
u8* rewind_stack_limit;
|
|
||||||
boost::context::detail::fcontext_t context;
|
|
||||||
boost::context::detail::fcontext_t rewind_context;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void Fiber::SetStartParameter(void* new_parameter) {
|
void Fiber::SetStartParameter(void* new_parameter) {
|
||||||
|
@ -48,95 +43,6 @@ void Fiber::SetRewindPoint(std::function<void(void*)>&& rewind_func, void* rewin
|
||||||
impl->rewind_parameter = rewind_param;
|
impl->rewind_parameter = rewind_param;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(WIN32)
|
|
||||||
|
|
||||||
void Fiber::Start() {
|
|
||||||
ASSERT(impl->previous_fiber != nullptr);
|
|
||||||
impl->previous_fiber->impl->guard.unlock();
|
|
||||||
impl->previous_fiber.reset();
|
|
||||||
impl->entry_point(impl->start_parameter);
|
|
||||||
UNREACHABLE();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Fiber::OnRewind() {
|
|
||||||
ASSERT(impl->handle != nullptr);
|
|
||||||
DeleteFiber(impl->handle);
|
|
||||||
impl->handle = impl->rewind_handle;
|
|
||||||
impl->rewind_handle = nullptr;
|
|
||||||
impl->rewind_point(impl->rewind_parameter);
|
|
||||||
UNREACHABLE();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Fiber::FiberStartFunc(void* fiber_parameter) {
|
|
||||||
auto* fiber = static_cast<Fiber*>(fiber_parameter);
|
|
||||||
fiber->Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Fiber::RewindStartFunc(void* fiber_parameter) {
|
|
||||||
auto* fiber = static_cast<Fiber*>(fiber_parameter);
|
|
||||||
fiber->OnRewind();
|
|
||||||
}
|
|
||||||
|
|
||||||
Fiber::Fiber(std::function<void(void*)>&& entry_point_func, void* start_parameter)
|
|
||||||
: impl{std::make_unique<FiberImpl>()} {
|
|
||||||
impl->entry_point = std::move(entry_point_func);
|
|
||||||
impl->start_parameter = start_parameter;
|
|
||||||
impl->handle = CreateFiber(default_stack_size, &FiberStartFunc, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
Fiber::Fiber() : impl{std::make_unique<FiberImpl>()} {}
|
|
||||||
|
|
||||||
Fiber::~Fiber() {
|
|
||||||
if (impl->released) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Make sure the Fiber is not being used
|
|
||||||
const bool locked = impl->guard.try_lock();
|
|
||||||
ASSERT_MSG(locked, "Destroying a fiber that's still running");
|
|
||||||
if (locked) {
|
|
||||||
impl->guard.unlock();
|
|
||||||
}
|
|
||||||
DeleteFiber(impl->handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Fiber::Exit() {
|
|
||||||
ASSERT_MSG(impl->is_thread_fiber, "Exitting non main thread fiber");
|
|
||||||
if (!impl->is_thread_fiber) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ConvertFiberToThread();
|
|
||||||
impl->guard.unlock();
|
|
||||||
impl->released = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Fiber::Rewind() {
|
|
||||||
ASSERT(impl->rewind_point);
|
|
||||||
ASSERT(impl->rewind_handle == nullptr);
|
|
||||||
impl->rewind_handle = CreateFiber(default_stack_size, &RewindStartFunc, this);
|
|
||||||
SwitchToFiber(impl->rewind_handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Fiber::YieldTo(std::shared_ptr<Fiber> from, std::shared_ptr<Fiber> to) {
|
|
||||||
ASSERT_MSG(from != nullptr, "Yielding fiber is null!");
|
|
||||||
ASSERT_MSG(to != nullptr, "Next fiber is null!");
|
|
||||||
to->impl->guard.lock();
|
|
||||||
to->impl->previous_fiber = from;
|
|
||||||
SwitchToFiber(to->impl->handle);
|
|
||||||
ASSERT(from->impl->previous_fiber != nullptr);
|
|
||||||
from->impl->previous_fiber->impl->guard.unlock();
|
|
||||||
from->impl->previous_fiber.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Fiber> Fiber::ThreadToFiber() {
|
|
||||||
std::shared_ptr<Fiber> fiber = std::shared_ptr<Fiber>{new Fiber()};
|
|
||||||
fiber->impl->guard.lock();
|
|
||||||
fiber->impl->handle = ConvertThreadToFiber(nullptr);
|
|
||||||
fiber->impl->is_thread_fiber = true;
|
|
||||||
return fiber;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
void Fiber::Start(boost::context::detail::transfer_t& transfer) {
|
void Fiber::Start(boost::context::detail::transfer_t& transfer) {
|
||||||
ASSERT(impl->previous_fiber != nullptr);
|
ASSERT(impl->previous_fiber != nullptr);
|
||||||
impl->previous_fiber->impl->context = transfer.fctx;
|
impl->previous_fiber->impl->context = transfer.fctx;
|
||||||
|
@ -229,5 +135,4 @@ std::shared_ptr<Fiber> Fiber::ThreadToFiber() {
|
||||||
return fiber;
|
return fiber;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
} // namespace Common
|
} // namespace Common
|
||||||
|
|
|
@ -7,11 +7,9 @@
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#if !defined(_WIN32) && !defined(WIN32)
|
|
||||||
namespace boost::context::detail {
|
namespace boost::context::detail {
|
||||||
struct transfer_t;
|
struct transfer_t;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
|
||||||
|
@ -59,17 +57,10 @@ public:
|
||||||
private:
|
private:
|
||||||
Fiber();
|
Fiber();
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(WIN32)
|
|
||||||
void OnRewind();
|
|
||||||
void Start();
|
|
||||||
static void FiberStartFunc(void* fiber_parameter);
|
|
||||||
static void RewindStartFunc(void* fiber_parameter);
|
|
||||||
#else
|
|
||||||
void OnRewind(boost::context::detail::transfer_t& transfer);
|
void OnRewind(boost::context::detail::transfer_t& transfer);
|
||||||
void Start(boost::context::detail::transfer_t& transfer);
|
void Start(boost::context::detail::transfer_t& transfer);
|
||||||
static void FiberStartFunc(boost::context::detail::transfer_t transfer);
|
static void FiberStartFunc(boost::context::detail::transfer_t transfer);
|
||||||
static void RewindStartFunc(boost::context::detail::transfer_t transfer);
|
static void RewindStartFunc(boost::context::detail::transfer_t transfer);
|
||||||
#endif
|
|
||||||
|
|
||||||
struct FiberImpl;
|
struct FiberImpl;
|
||||||
std::unique_ptr<FiberImpl> impl;
|
std::unique_ptr<FiberImpl> impl;
|
||||||
|
|
|
@ -64,15 +64,25 @@ public:
|
||||||
/// Step CPU by one instruction
|
/// Step CPU by one instruction
|
||||||
virtual void Step() = 0;
|
virtual void Step() = 0;
|
||||||
|
|
||||||
|
/// Exits execution from a callback, the callback must rewind the stack
|
||||||
|
virtual void ExceptionalExit() = 0;
|
||||||
|
|
||||||
/// Clear all instruction cache
|
/// Clear all instruction cache
|
||||||
virtual void ClearInstructionCache() = 0;
|
virtual void ClearInstructionCache() = 0;
|
||||||
|
|
||||||
/// Notifies CPU emulation that the current page table has changed.
|
/**
|
||||||
///
|
* Clear instruction cache range
|
||||||
/// @param new_page_table The new page table.
|
* @param addr Start address of the cache range to clear
|
||||||
/// @param new_address_space_size_in_bits The new usable size of the address space in bits.
|
* @param size Size of the cache range to clear, starting at addr
|
||||||
/// This can be either 32, 36, or 39 on official software.
|
*/
|
||||||
///
|
virtual void InvalidateCacheRange(VAddr addr, std::size_t size) = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies CPU emulation that the current page table has changed.
|
||||||
|
* @param new_page_table The new page table.
|
||||||
|
* @param new_address_space_size_in_bits The new usable size of the address space in bits.
|
||||||
|
* This can be either 32, 36, or 39 on official software.
|
||||||
|
*/
|
||||||
virtual void PageTableChanged(Common::PageTable& new_page_table,
|
virtual void PageTableChanged(Common::PageTable& new_page_table,
|
||||||
std::size_t new_address_space_size_in_bits) = 0;
|
std::size_t new_address_space_size_in_bits) = 0;
|
||||||
|
|
||||||
|
|
|
@ -189,6 +189,10 @@ void ARM_Dynarmic_32::Run() {
|
||||||
jit->Run();
|
jit->Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ARM_Dynarmic_32::ExceptionalExit() {
|
||||||
|
jit->ExceptionalExit();
|
||||||
|
}
|
||||||
|
|
||||||
void ARM_Dynarmic_32::Step() {
|
void ARM_Dynarmic_32::Step() {
|
||||||
jit->Step();
|
jit->Step();
|
||||||
}
|
}
|
||||||
|
@ -282,6 +286,13 @@ void ARM_Dynarmic_32::ClearInstructionCache() {
|
||||||
jit->ClearCache();
|
jit->ClearCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ARM_Dynarmic_32::InvalidateCacheRange(VAddr addr, std::size_t size) {
|
||||||
|
if (!jit) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
jit->InvalidateCacheRange(static_cast<u32>(addr), size);
|
||||||
|
}
|
||||||
|
|
||||||
void ARM_Dynarmic_32::ClearExclusiveState() {
|
void ARM_Dynarmic_32::ClearExclusiveState() {
|
||||||
jit->ClearExclusiveState();
|
jit->ClearExclusiveState();
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ public:
|
||||||
u32 GetPSTATE() const override;
|
u32 GetPSTATE() const override;
|
||||||
void SetPSTATE(u32 pstate) override;
|
void SetPSTATE(u32 pstate) override;
|
||||||
void Run() override;
|
void Run() override;
|
||||||
|
void ExceptionalExit() override;
|
||||||
void Step() override;
|
void Step() override;
|
||||||
VAddr GetTlsAddress() const override;
|
VAddr GetTlsAddress() const override;
|
||||||
void SetTlsAddress(VAddr address) override;
|
void SetTlsAddress(VAddr address) override;
|
||||||
|
@ -58,6 +59,7 @@ public:
|
||||||
void ClearExclusiveState() override;
|
void ClearExclusiveState() override;
|
||||||
|
|
||||||
void ClearInstructionCache() override;
|
void ClearInstructionCache() override;
|
||||||
|
void InvalidateCacheRange(VAddr addr, std::size_t size) override;
|
||||||
void PageTableChanged(Common::PageTable& new_page_table,
|
void PageTableChanged(Common::PageTable& new_page_table,
|
||||||
std::size_t new_address_space_size_in_bits) override;
|
std::size_t new_address_space_size_in_bits) override;
|
||||||
|
|
||||||
|
|
|
@ -220,6 +220,10 @@ void ARM_Dynarmic_64::Run() {
|
||||||
jit->Run();
|
jit->Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ARM_Dynarmic_64::ExceptionalExit() {
|
||||||
|
jit->ExceptionalExit();
|
||||||
|
}
|
||||||
|
|
||||||
void ARM_Dynarmic_64::Step() {
|
void ARM_Dynarmic_64::Step() {
|
||||||
cb->InterpreterFallback(jit->GetPC(), 1);
|
cb->InterpreterFallback(jit->GetPC(), 1);
|
||||||
}
|
}
|
||||||
|
@ -318,6 +322,13 @@ void ARM_Dynarmic_64::ClearInstructionCache() {
|
||||||
jit->ClearCache();
|
jit->ClearCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ARM_Dynarmic_64::InvalidateCacheRange(VAddr addr, std::size_t size) {
|
||||||
|
if (!jit) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
jit->InvalidateCacheRange(addr, size);
|
||||||
|
}
|
||||||
|
|
||||||
void ARM_Dynarmic_64::ClearExclusiveState() {
|
void ARM_Dynarmic_64::ClearExclusiveState() {
|
||||||
jit->ClearExclusiveState();
|
jit->ClearExclusiveState();
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ public:
|
||||||
void SetPSTATE(u32 pstate) override;
|
void SetPSTATE(u32 pstate) override;
|
||||||
void Run() override;
|
void Run() override;
|
||||||
void Step() override;
|
void Step() override;
|
||||||
|
void ExceptionalExit() override;
|
||||||
VAddr GetTlsAddress() const override;
|
VAddr GetTlsAddress() const override;
|
||||||
void SetTlsAddress(VAddr address) override;
|
void SetTlsAddress(VAddr address) override;
|
||||||
void SetTPIDR_EL0(u64 value) override;
|
void SetTPIDR_EL0(u64 value) override;
|
||||||
|
@ -55,6 +56,7 @@ public:
|
||||||
void ClearExclusiveState() override;
|
void ClearExclusiveState() override;
|
||||||
|
|
||||||
void ClearInstructionCache() override;
|
void ClearInstructionCache() override;
|
||||||
|
void InvalidateCacheRange(VAddr addr, std::size_t size) override;
|
||||||
void PageTableChanged(Common::PageTable& new_page_table,
|
void PageTableChanged(Common::PageTable& new_page_table,
|
||||||
std::size_t new_address_space_size_in_bits) override;
|
std::size_t new_address_space_size_in_bits) override;
|
||||||
|
|
||||||
|
|
|
@ -245,6 +245,7 @@ struct System::Impl {
|
||||||
}
|
}
|
||||||
AddGlueRegistrationForProcess(*app_loader, *main_process);
|
AddGlueRegistrationForProcess(*app_loader, *main_process);
|
||||||
kernel.MakeCurrentProcess(main_process.get());
|
kernel.MakeCurrentProcess(main_process.get());
|
||||||
|
kernel.InitializeCores();
|
||||||
|
|
||||||
// Initialize cheat engine
|
// Initialize cheat engine
|
||||||
if (cheat_engine) {
|
if (cheat_engine) {
|
||||||
|
@ -456,6 +457,10 @@ void System::InvalidateCpuInstructionCaches() {
|
||||||
impl->kernel.InvalidateAllInstructionCaches();
|
impl->kernel.InvalidateAllInstructionCaches();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void System::InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size) {
|
||||||
|
impl->kernel.InvalidateCpuInstructionCacheRange(addr, size);
|
||||||
|
}
|
||||||
|
|
||||||
void System::Shutdown() {
|
void System::Shutdown() {
|
||||||
impl->Shutdown();
|
impl->Shutdown();
|
||||||
}
|
}
|
||||||
|
@ -490,11 +495,11 @@ const TelemetrySession& System::TelemetrySession() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
ARM_Interface& System::CurrentArmInterface() {
|
ARM_Interface& System::CurrentArmInterface() {
|
||||||
return impl->kernel.CurrentScheduler().GetCurrentThread()->ArmInterface();
|
return impl->kernel.CurrentPhysicalCore().ArmInterface();
|
||||||
}
|
}
|
||||||
|
|
||||||
const ARM_Interface& System::CurrentArmInterface() const {
|
const ARM_Interface& System::CurrentArmInterface() const {
|
||||||
return impl->kernel.CurrentScheduler().GetCurrentThread()->ArmInterface();
|
return impl->kernel.CurrentPhysicalCore().ArmInterface();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t System::CurrentCoreIndex() const {
|
std::size_t System::CurrentCoreIndex() const {
|
||||||
|
@ -554,15 +559,11 @@ const Kernel::Process* System::CurrentProcess() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
ARM_Interface& System::ArmInterface(std::size_t core_index) {
|
ARM_Interface& System::ArmInterface(std::size_t core_index) {
|
||||||
auto* thread = impl->kernel.Scheduler(core_index).GetCurrentThread();
|
return impl->kernel.PhysicalCore(core_index).ArmInterface();
|
||||||
ASSERT(thread && !thread->IsHLEThread());
|
|
||||||
return thread->ArmInterface();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ARM_Interface& System::ArmInterface(std::size_t core_index) const {
|
const ARM_Interface& System::ArmInterface(std::size_t core_index) const {
|
||||||
auto* thread = impl->kernel.Scheduler(core_index).GetCurrentThread();
|
return impl->kernel.PhysicalCore(core_index).ArmInterface();
|
||||||
ASSERT(thread && !thread->IsHLEThread());
|
|
||||||
return thread->ArmInterface();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ExclusiveMonitor& System::Monitor() {
|
ExclusiveMonitor& System::Monitor() {
|
||||||
|
|
|
@ -166,6 +166,8 @@ public:
|
||||||
*/
|
*/
|
||||||
void InvalidateCpuInstructionCaches();
|
void InvalidateCpuInstructionCaches();
|
||||||
|
|
||||||
|
void InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size);
|
||||||
|
|
||||||
/// Shutdown the emulated system.
|
/// Shutdown the emulated system.
|
||||||
void Shutdown();
|
void Shutdown();
|
||||||
|
|
||||||
|
|
|
@ -113,22 +113,23 @@ void CpuManager::MultiCoreRunGuestThread() {
|
||||||
auto& sched = kernel.CurrentScheduler();
|
auto& sched = kernel.CurrentScheduler();
|
||||||
sched.OnThreadStart();
|
sched.OnThreadStart();
|
||||||
}
|
}
|
||||||
|
auto* thread = kernel.CurrentScheduler().GetCurrentThread();
|
||||||
|
auto& host_context = thread->GetHostContext();
|
||||||
|
host_context->SetRewindPoint(GuestRewindFunction, this);
|
||||||
MultiCoreRunGuestLoop();
|
MultiCoreRunGuestLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CpuManager::MultiCoreRunGuestLoop() {
|
void CpuManager::MultiCoreRunGuestLoop() {
|
||||||
auto& kernel = system.Kernel();
|
auto& kernel = system.Kernel();
|
||||||
auto* thread = kernel.CurrentScheduler().GetCurrentThread();
|
|
||||||
while (true) {
|
while (true) {
|
||||||
auto* physical_core = &kernel.CurrentPhysicalCore();
|
auto* physical_core = &kernel.CurrentPhysicalCore();
|
||||||
auto& arm_interface = thread->ArmInterface();
|
|
||||||
system.EnterDynarmicProfile();
|
system.EnterDynarmicProfile();
|
||||||
while (!physical_core->IsInterrupted()) {
|
while (!physical_core->IsInterrupted()) {
|
||||||
arm_interface.Run();
|
physical_core->Run();
|
||||||
physical_core = &kernel.CurrentPhysicalCore();
|
physical_core = &kernel.CurrentPhysicalCore();
|
||||||
}
|
}
|
||||||
system.ExitDynarmicProfile();
|
system.ExitDynarmicProfile();
|
||||||
arm_interface.ClearExclusiveState();
|
|
||||||
auto& scheduler = kernel.CurrentScheduler();
|
auto& scheduler = kernel.CurrentScheduler();
|
||||||
scheduler.TryDoContextSwitch();
|
scheduler.TryDoContextSwitch();
|
||||||
}
|
}
|
||||||
|
@ -209,6 +210,9 @@ void CpuManager::SingleCoreRunGuestThread() {
|
||||||
auto& sched = kernel.CurrentScheduler();
|
auto& sched = kernel.CurrentScheduler();
|
||||||
sched.OnThreadStart();
|
sched.OnThreadStart();
|
||||||
}
|
}
|
||||||
|
auto* thread = kernel.CurrentScheduler().GetCurrentThread();
|
||||||
|
auto& host_context = thread->GetHostContext();
|
||||||
|
host_context->SetRewindPoint(GuestRewindFunction, this);
|
||||||
SingleCoreRunGuestLoop();
|
SingleCoreRunGuestLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,17 +221,15 @@ void CpuManager::SingleCoreRunGuestLoop() {
|
||||||
auto* thread = kernel.CurrentScheduler().GetCurrentThread();
|
auto* thread = kernel.CurrentScheduler().GetCurrentThread();
|
||||||
while (true) {
|
while (true) {
|
||||||
auto* physical_core = &kernel.CurrentPhysicalCore();
|
auto* physical_core = &kernel.CurrentPhysicalCore();
|
||||||
auto& arm_interface = thread->ArmInterface();
|
|
||||||
system.EnterDynarmicProfile();
|
system.EnterDynarmicProfile();
|
||||||
if (!physical_core->IsInterrupted()) {
|
if (!physical_core->IsInterrupted()) {
|
||||||
arm_interface.Run();
|
physical_core->Run();
|
||||||
physical_core = &kernel.CurrentPhysicalCore();
|
physical_core = &kernel.CurrentPhysicalCore();
|
||||||
}
|
}
|
||||||
system.ExitDynarmicProfile();
|
system.ExitDynarmicProfile();
|
||||||
thread->SetPhantomMode(true);
|
thread->SetPhantomMode(true);
|
||||||
system.CoreTiming().Advance();
|
system.CoreTiming().Advance();
|
||||||
thread->SetPhantomMode(false);
|
thread->SetPhantomMode(false);
|
||||||
arm_interface.ClearExclusiveState();
|
|
||||||
PreemptSingleCore();
|
PreemptSingleCore();
|
||||||
auto& scheduler = kernel.Scheduler(current_core);
|
auto& scheduler = kernel.Scheduler(current_core);
|
||||||
scheduler.TryDoContextSwitch();
|
scheduler.TryDoContextSwitch();
|
||||||
|
|
|
@ -275,12 +275,6 @@ ResultCode AddressArbiter::WaitForAddressIfEqual(VAddr address, s32 value, s64 t
|
||||||
return current_thread->GetSignalingResult();
|
return current_thread->GetSignalingResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddressArbiter::HandleWakeupThread(std::shared_ptr<Thread> thread) {
|
|
||||||
ASSERT(thread->GetStatus() == ThreadStatus::WaitArb);
|
|
||||||
RemoveThread(thread);
|
|
||||||
thread->SetArbiterWaitAddress(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddressArbiter::InsertThread(std::shared_ptr<Thread> thread) {
|
void AddressArbiter::InsertThread(std::shared_ptr<Thread> thread) {
|
||||||
const VAddr arb_addr = thread->GetArbiterWaitAddress();
|
const VAddr arb_addr = thread->GetArbiterWaitAddress();
|
||||||
std::list<std::shared_ptr<Thread>>& thread_list = arb_threads[arb_addr];
|
std::list<std::shared_ptr<Thread>>& thread_list = arb_threads[arb_addr];
|
||||||
|
|
|
@ -50,9 +50,6 @@ public:
|
||||||
/// Waits on an address with a particular arbitration type.
|
/// Waits on an address with a particular arbitration type.
|
||||||
ResultCode WaitForAddress(VAddr address, ArbitrationType type, s32 value, s64 timeout_ns);
|
ResultCode WaitForAddress(VAddr address, ArbitrationType type, s32 value, s64 timeout_ns);
|
||||||
|
|
||||||
/// Removes a thread from the container and resets its address arbiter adress to 0
|
|
||||||
void HandleWakeupThread(std::shared_ptr<Thread> thread);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Signals an address being waited on.
|
/// Signals an address being waited on.
|
||||||
ResultCode SignalToAddressOnly(VAddr address, s32 num_to_wake);
|
ResultCode SignalToAddressOnly(VAddr address, s32 num_to_wake);
|
||||||
|
|
|
@ -68,6 +68,12 @@ struct KernelCore::Impl {
|
||||||
InitializeSuspendThreads();
|
InitializeSuspendThreads();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InitializeCores() {
|
||||||
|
for (auto& core : cores) {
|
||||||
|
core.Initialize(current_process->Is64BitProcess());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Shutdown() {
|
void Shutdown() {
|
||||||
next_object_id = 0;
|
next_object_id = 0;
|
||||||
next_kernel_process_id = Process::InitialKIPIDMin;
|
next_kernel_process_id = Process::InitialKIPIDMin;
|
||||||
|
@ -116,7 +122,7 @@ struct KernelCore::Impl {
|
||||||
Core::MakeExclusiveMonitor(system.Memory(), Core::Hardware::NUM_CPU_CORES);
|
Core::MakeExclusiveMonitor(system.Memory(), Core::Hardware::NUM_CPU_CORES);
|
||||||
for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
|
for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
|
||||||
schedulers[i] = std::make_unique<Kernel::Scheduler>(system, i);
|
schedulers[i] = std::make_unique<Kernel::Scheduler>(system, i);
|
||||||
cores.emplace_back(system, i, *schedulers[i], interrupts[i]);
|
cores.emplace_back(i, system, *schedulers[i], interrupts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,6 +187,7 @@ struct KernelCore::Impl {
|
||||||
if (process == nullptr) {
|
if (process == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const u32 core_id = GetCurrentHostThreadID();
|
const u32 core_id = GetCurrentHostThreadID();
|
||||||
if (core_id < Core::Hardware::NUM_CPU_CORES) {
|
if (core_id < Core::Hardware::NUM_CPU_CORES) {
|
||||||
system.Memory().SetCurrentPageTable(*process, core_id);
|
system.Memory().SetCurrentPageTable(*process, core_id);
|
||||||
|
@ -372,6 +379,10 @@ void KernelCore::Initialize() {
|
||||||
impl->Initialize(*this);
|
impl->Initialize(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void KernelCore::InitializeCores() {
|
||||||
|
impl->InitializeCores();
|
||||||
|
}
|
||||||
|
|
||||||
void KernelCore::Shutdown() {
|
void KernelCore::Shutdown() {
|
||||||
impl->Shutdown();
|
impl->Shutdown();
|
||||||
}
|
}
|
||||||
|
@ -486,12 +497,17 @@ const Core::ExclusiveMonitor& KernelCore::GetExclusiveMonitor() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void KernelCore::InvalidateAllInstructionCaches() {
|
void KernelCore::InvalidateAllInstructionCaches() {
|
||||||
auto& threads = GlobalScheduler().GetThreadList();
|
for (auto& physical_core : impl->cores) {
|
||||||
for (auto& thread : threads) {
|
physical_core.ArmInterface().ClearInstructionCache();
|
||||||
if (!thread->IsHLEThread()) {
|
|
||||||
auto& arm_interface = thread->ArmInterface();
|
|
||||||
arm_interface.ClearInstructionCache();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void KernelCore::InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size) {
|
||||||
|
for (auto& physical_core : impl->cores) {
|
||||||
|
if (!physical_core.IsInitialized()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
physical_core.ArmInterface().InvalidateCacheRange(addr, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,6 +74,9 @@ public:
|
||||||
/// Resets the kernel to a clean slate for use.
|
/// Resets the kernel to a clean slate for use.
|
||||||
void Initialize();
|
void Initialize();
|
||||||
|
|
||||||
|
/// Initializes the CPU cores.
|
||||||
|
void InitializeCores();
|
||||||
|
|
||||||
/// Clears all resources in use by the kernel instance.
|
/// Clears all resources in use by the kernel instance.
|
||||||
void Shutdown();
|
void Shutdown();
|
||||||
|
|
||||||
|
@ -153,6 +156,8 @@ public:
|
||||||
|
|
||||||
void InvalidateAllInstructionCaches();
|
void InvalidateAllInstructionCaches();
|
||||||
|
|
||||||
|
void InvalidateCpuInstructionCacheRange(VAddr addr, std::size_t size);
|
||||||
|
|
||||||
/// Adds a port to the named port table
|
/// Adds a port to the named port table
|
||||||
void AddNamedPort(std::string name, std::shared_ptr<ClientPort> port);
|
void AddNamedPort(std::string name, std::shared_ptr<ClientPort> port);
|
||||||
|
|
||||||
|
|
|
@ -670,6 +670,11 @@ ResultCode PageTable::SetCodeMemoryPermission(VAddr addr, std::size_t size, Memo
|
||||||
return RESULT_SUCCESS;
|
return RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((prev_perm & MemoryPermission::Execute) != (perm & MemoryPermission::Execute)) {
|
||||||
|
// Memory execution state is changing, invalidate CPU cache range
|
||||||
|
system.InvalidateCpuInstructionCacheRange(addr, size);
|
||||||
|
}
|
||||||
|
|
||||||
const std::size_t num_pages{size / PageSize};
|
const std::size_t num_pages{size / PageSize};
|
||||||
const OperationType operation{(perm & MemoryPermission::Execute) != MemoryPermission::None
|
const OperationType operation{(perm & MemoryPermission::Execute) != MemoryPermission::None
|
||||||
? OperationType::ChangePermissionsAndRefresh
|
? OperationType::ChangePermissionsAndRefresh
|
||||||
|
|
|
@ -4,21 +4,43 @@
|
||||||
|
|
||||||
#include "common/spin_lock.h"
|
#include "common/spin_lock.h"
|
||||||
#include "core/arm/cpu_interrupt_handler.h"
|
#include "core/arm/cpu_interrupt_handler.h"
|
||||||
|
#include "core/arm/dynarmic/arm_dynarmic_32.h"
|
||||||
|
#include "core/arm/dynarmic/arm_dynarmic_64.h"
|
||||||
#include "core/core.h"
|
#include "core/core.h"
|
||||||
|
#include "core/hle/kernel/kernel.h"
|
||||||
#include "core/hle/kernel/physical_core.h"
|
#include "core/hle/kernel/physical_core.h"
|
||||||
#include "core/hle/kernel/scheduler.h"
|
#include "core/hle/kernel/scheduler.h"
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
PhysicalCore::PhysicalCore(Core::System& system, std::size_t id, Kernel::Scheduler& scheduler,
|
PhysicalCore::PhysicalCore(std::size_t core_index, Core::System& system,
|
||||||
Core::CPUInterruptHandler& interrupt_handler)
|
Kernel::Scheduler& scheduler, Core::CPUInterrupts& interrupts)
|
||||||
: interrupt_handler{interrupt_handler},
|
: core_index{core_index}, system{system}, scheduler{scheduler},
|
||||||
core_index{id}, scheduler{scheduler}, guard{std::make_unique<Common::SpinLock>()} {}
|
interrupts{interrupts}, guard{std::make_unique<Common::SpinLock>()} {}
|
||||||
|
|
||||||
PhysicalCore::~PhysicalCore() = default;
|
PhysicalCore::~PhysicalCore() = default;
|
||||||
|
|
||||||
|
void PhysicalCore::Initialize([[maybe_unused]] bool is_64_bit) {
|
||||||
|
#ifdef ARCHITECTURE_x86_64
|
||||||
|
auto& kernel = system.Kernel();
|
||||||
|
if (is_64_bit) {
|
||||||
|
arm_interface = std::make_unique<Core::ARM_Dynarmic_64>(
|
||||||
|
system, interrupts, kernel.IsMulticore(), kernel.GetExclusiveMonitor(), core_index);
|
||||||
|
} else {
|
||||||
|
arm_interface = std::make_unique<Core::ARM_Dynarmic_32>(
|
||||||
|
system, interrupts, kernel.IsMulticore(), kernel.GetExclusiveMonitor(), core_index);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#error Platform not supported yet.
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicalCore::Run() {
|
||||||
|
arm_interface->Run();
|
||||||
|
}
|
||||||
|
|
||||||
void PhysicalCore::Idle() {
|
void PhysicalCore::Idle() {
|
||||||
interrupt_handler.AwaitInterrupt();
|
interrupts[core_index].AwaitInterrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicalCore::Shutdown() {
|
void PhysicalCore::Shutdown() {
|
||||||
|
@ -26,18 +48,18 @@ void PhysicalCore::Shutdown() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PhysicalCore::IsInterrupted() const {
|
bool PhysicalCore::IsInterrupted() const {
|
||||||
return interrupt_handler.IsInterrupted();
|
return interrupts[core_index].IsInterrupted();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicalCore::Interrupt() {
|
void PhysicalCore::Interrupt() {
|
||||||
guard->lock();
|
guard->lock();
|
||||||
interrupt_handler.SetInterrupt(true);
|
interrupts[core_index].SetInterrupt(true);
|
||||||
guard->unlock();
|
guard->unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicalCore::ClearInterrupt() {
|
void PhysicalCore::ClearInterrupt() {
|
||||||
guard->lock();
|
guard->lock();
|
||||||
interrupt_handler.SetInterrupt(false);
|
interrupts[core_index].SetInterrupt(false);
|
||||||
guard->unlock();
|
guard->unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,12 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include "core/arm/arm_interface.h"
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
class SpinLock;
|
class SpinLock;
|
||||||
}
|
}
|
||||||
|
@ -16,7 +19,6 @@ class Scheduler;
|
||||||
} // namespace Kernel
|
} // namespace Kernel
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
class ARM_Interface;
|
|
||||||
class CPUInterruptHandler;
|
class CPUInterruptHandler;
|
||||||
class ExclusiveMonitor;
|
class ExclusiveMonitor;
|
||||||
class System;
|
class System;
|
||||||
|
@ -26,8 +28,8 @@ namespace Kernel {
|
||||||
|
|
||||||
class PhysicalCore {
|
class PhysicalCore {
|
||||||
public:
|
public:
|
||||||
PhysicalCore(Core::System& system, std::size_t id, Kernel::Scheduler& scheduler,
|
PhysicalCore(std::size_t core_index, Core::System& system, Kernel::Scheduler& scheduler,
|
||||||
Core::CPUInterruptHandler& interrupt_handler);
|
Core::CPUInterrupts& interrupts);
|
||||||
~PhysicalCore();
|
~PhysicalCore();
|
||||||
|
|
||||||
PhysicalCore(const PhysicalCore&) = delete;
|
PhysicalCore(const PhysicalCore&) = delete;
|
||||||
|
@ -36,7 +38,14 @@ public:
|
||||||
PhysicalCore(PhysicalCore&&) = default;
|
PhysicalCore(PhysicalCore&&) = default;
|
||||||
PhysicalCore& operator=(PhysicalCore&&) = default;
|
PhysicalCore& operator=(PhysicalCore&&) = default;
|
||||||
|
|
||||||
|
/// Initialize the core for the specified parameters.
|
||||||
|
void Initialize(bool is_64_bit);
|
||||||
|
|
||||||
|
/// Execute current jit state
|
||||||
|
void Run();
|
||||||
|
|
||||||
void Idle();
|
void Idle();
|
||||||
|
|
||||||
/// Interrupt this physical core.
|
/// Interrupt this physical core.
|
||||||
void Interrupt();
|
void Interrupt();
|
||||||
|
|
||||||
|
@ -49,6 +58,18 @@ public:
|
||||||
// Shutdown this physical core.
|
// Shutdown this physical core.
|
||||||
void Shutdown();
|
void Shutdown();
|
||||||
|
|
||||||
|
bool IsInitialized() const {
|
||||||
|
return arm_interface != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Core::ARM_Interface& ArmInterface() {
|
||||||
|
return *arm_interface;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Core::ARM_Interface& ArmInterface() const {
|
||||||
|
return *arm_interface;
|
||||||
|
}
|
||||||
|
|
||||||
bool IsMainCore() const {
|
bool IsMainCore() const {
|
||||||
return core_index == 0;
|
return core_index == 0;
|
||||||
}
|
}
|
||||||
|
@ -70,10 +91,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Core::CPUInterruptHandler& interrupt_handler;
|
const std::size_t core_index;
|
||||||
std::size_t core_index;
|
Core::System& system;
|
||||||
Kernel::Scheduler& scheduler;
|
Kernel::Scheduler& scheduler;
|
||||||
|
Core::CPUInterrupts& interrupts;
|
||||||
std::unique_ptr<Common::SpinLock> guard;
|
std::unique_ptr<Common::SpinLock> guard;
|
||||||
|
std::unique_ptr<Core::ARM_Interface> arm_interface;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Kernel
|
} // namespace Kernel
|
||||||
|
|
|
@ -618,14 +618,16 @@ void Scheduler::OnThreadStart() {
|
||||||
SwitchContextStep2();
|
SwitchContextStep2();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scheduler::Unload() {
|
void Scheduler::Unload(Thread* thread) {
|
||||||
Thread* thread = current_thread.get();
|
|
||||||
if (thread) {
|
if (thread) {
|
||||||
thread->SetContinuousOnSVC(false);
|
|
||||||
thread->last_running_ticks = system.CoreTiming().GetCPUTicks();
|
thread->last_running_ticks = system.CoreTiming().GetCPUTicks();
|
||||||
thread->SetIsRunning(false);
|
thread->SetIsRunning(false);
|
||||||
|
if (thread->IsContinuousOnSVC() && !thread->IsHLEThread()) {
|
||||||
|
system.ArmInterface(core_id).ExceptionalExit();
|
||||||
|
thread->SetContinuousOnSVC(false);
|
||||||
|
}
|
||||||
if (!thread->IsHLEThread() && !thread->HasExited()) {
|
if (!thread->IsHLEThread() && !thread->HasExited()) {
|
||||||
Core::ARM_Interface& cpu_core = thread->ArmInterface();
|
Core::ARM_Interface& cpu_core = system.ArmInterface(core_id);
|
||||||
cpu_core.SaveContext(thread->GetContext32());
|
cpu_core.SaveContext(thread->GetContext32());
|
||||||
cpu_core.SaveContext(thread->GetContext64());
|
cpu_core.SaveContext(thread->GetContext64());
|
||||||
// Save the TPIDR_EL0 system register in case it was modified.
|
// Save the TPIDR_EL0 system register in case it was modified.
|
||||||
|
@ -636,8 +638,11 @@ void Scheduler::Unload() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scheduler::Reload() {
|
void Scheduler::Unload() {
|
||||||
Thread* thread = current_thread.get();
|
Unload(current_thread.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Scheduler::Reload(Thread* thread) {
|
||||||
if (thread) {
|
if (thread) {
|
||||||
ASSERT_MSG(thread->GetSchedulingStatus() == ThreadSchedStatus::Runnable,
|
ASSERT_MSG(thread->GetSchedulingStatus() == ThreadSchedStatus::Runnable,
|
||||||
"Thread must be runnable.");
|
"Thread must be runnable.");
|
||||||
|
@ -652,42 +657,23 @@ void Scheduler::Reload() {
|
||||||
system.Kernel().MakeCurrentProcess(thread_owner_process);
|
system.Kernel().MakeCurrentProcess(thread_owner_process);
|
||||||
}
|
}
|
||||||
if (!thread->IsHLEThread()) {
|
if (!thread->IsHLEThread()) {
|
||||||
Core::ARM_Interface& cpu_core = thread->ArmInterface();
|
Core::ARM_Interface& cpu_core = system.ArmInterface(core_id);
|
||||||
cpu_core.LoadContext(thread->GetContext32());
|
cpu_core.LoadContext(thread->GetContext32());
|
||||||
cpu_core.LoadContext(thread->GetContext64());
|
cpu_core.LoadContext(thread->GetContext64());
|
||||||
cpu_core.SetTlsAddress(thread->GetTLSAddress());
|
cpu_core.SetTlsAddress(thread->GetTLSAddress());
|
||||||
cpu_core.SetTPIDR_EL0(thread->GetTPIDR_EL0());
|
cpu_core.SetTPIDR_EL0(thread->GetTPIDR_EL0());
|
||||||
cpu_core.ChangeProcessorID(this->core_id);
|
|
||||||
cpu_core.ClearExclusiveState();
|
cpu_core.ClearExclusiveState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Scheduler::Reload() {
|
||||||
|
Reload(current_thread.get());
|
||||||
|
}
|
||||||
|
|
||||||
void Scheduler::SwitchContextStep2() {
|
void Scheduler::SwitchContextStep2() {
|
||||||
// Load context of new thread
|
// Load context of new thread
|
||||||
if (selected_thread) {
|
Reload(selected_thread.get());
|
||||||
ASSERT_MSG(selected_thread->GetSchedulingStatus() == ThreadSchedStatus::Runnable,
|
|
||||||
"Thread must be runnable.");
|
|
||||||
|
|
||||||
// Cancel any outstanding wakeup events for this thread
|
|
||||||
selected_thread->SetIsRunning(true);
|
|
||||||
selected_thread->last_running_ticks = system.CoreTiming().GetCPUTicks();
|
|
||||||
selected_thread->SetWasRunning(false);
|
|
||||||
|
|
||||||
auto* const thread_owner_process = current_thread->GetOwnerProcess();
|
|
||||||
if (thread_owner_process != nullptr) {
|
|
||||||
system.Kernel().MakeCurrentProcess(thread_owner_process);
|
|
||||||
}
|
|
||||||
if (!selected_thread->IsHLEThread()) {
|
|
||||||
Core::ARM_Interface& cpu_core = selected_thread->ArmInterface();
|
|
||||||
cpu_core.LoadContext(selected_thread->GetContext32());
|
|
||||||
cpu_core.LoadContext(selected_thread->GetContext64());
|
|
||||||
cpu_core.SetTlsAddress(selected_thread->GetTLSAddress());
|
|
||||||
cpu_core.SetTPIDR_EL0(selected_thread->GetTPIDR_EL0());
|
|
||||||
cpu_core.ChangeProcessorID(this->core_id);
|
|
||||||
cpu_core.ClearExclusiveState();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TryDoContextSwitch();
|
TryDoContextSwitch();
|
||||||
}
|
}
|
||||||
|
@ -711,23 +697,7 @@ void Scheduler::SwitchContext() {
|
||||||
UpdateLastContextSwitchTime(previous_thread, previous_process);
|
UpdateLastContextSwitchTime(previous_thread, previous_process);
|
||||||
|
|
||||||
// Save context for previous thread
|
// Save context for previous thread
|
||||||
if (previous_thread) {
|
Unload(previous_thread);
|
||||||
if (new_thread != nullptr && new_thread->IsSuspendThread()) {
|
|
||||||
previous_thread->SetWasRunning(true);
|
|
||||||
}
|
|
||||||
previous_thread->SetContinuousOnSVC(false);
|
|
||||||
previous_thread->last_running_ticks = system.CoreTiming().GetCPUTicks();
|
|
||||||
previous_thread->SetIsRunning(false);
|
|
||||||
if (!previous_thread->IsHLEThread() && !previous_thread->HasExited()) {
|
|
||||||
Core::ARM_Interface& cpu_core = previous_thread->ArmInterface();
|
|
||||||
cpu_core.SaveContext(previous_thread->GetContext32());
|
|
||||||
cpu_core.SaveContext(previous_thread->GetContext64());
|
|
||||||
// Save the TPIDR_EL0 system register in case it was modified.
|
|
||||||
previous_thread->SetTPIDR_EL0(cpu_core.GetTPIDR_EL0());
|
|
||||||
cpu_core.ClearExclusiveState();
|
|
||||||
}
|
|
||||||
previous_thread->context_guard.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Common::Fiber>* old_context;
|
std::shared_ptr<Common::Fiber>* old_context;
|
||||||
if (previous_thread != nullptr) {
|
if (previous_thread != nullptr) {
|
||||||
|
|
|
@ -212,8 +212,10 @@ public:
|
||||||
|
|
||||||
/// The next two are for SingleCore Only.
|
/// The next two are for SingleCore Only.
|
||||||
/// Unload current thread before preempting core.
|
/// Unload current thread before preempting core.
|
||||||
|
void Unload(Thread* thread);
|
||||||
void Unload();
|
void Unload();
|
||||||
/// Reload current thread after core preemption.
|
/// Reload current thread after core preemption.
|
||||||
|
void Reload(Thread* thread);
|
||||||
void Reload();
|
void Reload();
|
||||||
|
|
||||||
/// Gets the current running thread
|
/// Gets the current running thread
|
||||||
|
|
|
@ -2639,6 +2639,9 @@ void Call(Core::System& system, u32 immediate) {
|
||||||
auto& kernel = system.Kernel();
|
auto& kernel = system.Kernel();
|
||||||
kernel.EnterSVCProfile();
|
kernel.EnterSVCProfile();
|
||||||
|
|
||||||
|
auto* thread = system.CurrentScheduler().GetCurrentThread();
|
||||||
|
thread->SetContinuousOnSVC(true);
|
||||||
|
|
||||||
const FunctionDef* info = system.CurrentProcess()->Is64BitProcess() ? GetSVCInfo64(immediate)
|
const FunctionDef* info = system.CurrentProcess()->Is64BitProcess() ? GetSVCInfo64(immediate)
|
||||||
: GetSVCInfo32(immediate);
|
: GetSVCInfo32(immediate);
|
||||||
if (info) {
|
if (info) {
|
||||||
|
@ -2652,6 +2655,12 @@ void Call(Core::System& system, u32 immediate) {
|
||||||
}
|
}
|
||||||
|
|
||||||
kernel.ExitSVCProfile();
|
kernel.ExitSVCProfile();
|
||||||
|
|
||||||
|
if (!thread->IsContinuousOnSVC()) {
|
||||||
|
auto* host_context = thread->GetHostContext().get();
|
||||||
|
host_context->Rewind();
|
||||||
|
}
|
||||||
|
|
||||||
system.EnterDynarmicProfile();
|
system.EnterDynarmicProfile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -56,7 +57,7 @@ public:
|
||||||
void ClearWaitingThreads();
|
void ClearWaitingThreads();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool is_signaled{}; // Tells if this sync object is signalled;
|
std::atomic_bool is_signaled{}; // Tells if this sync object is signaled
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Threads waiting for this object to become available
|
/// Threads waiting for this object to become available
|
||||||
|
|
|
@ -12,7 +12,6 @@
|
||||||
#include "common/fiber.h"
|
#include "common/fiber.h"
|
||||||
#include "common/logging/log.h"
|
#include "common/logging/log.h"
|
||||||
#include "common/thread_queue_list.h"
|
#include "common/thread_queue_list.h"
|
||||||
#include "core/arm/arm_interface.h"
|
|
||||||
#include "core/core.h"
|
#include "core/core.h"
|
||||||
#include "core/cpu_manager.h"
|
#include "core/cpu_manager.h"
|
||||||
#include "core/hardware_properties.h"
|
#include "core/hardware_properties.h"
|
||||||
|
@ -62,7 +61,6 @@ void Thread::Stop() {
|
||||||
// Mark the TLS slot in the thread's page as free.
|
// Mark the TLS slot in the thread's page as free.
|
||||||
owner_process->FreeTLSRegion(tls_address);
|
owner_process->FreeTLSRegion(tls_address);
|
||||||
}
|
}
|
||||||
arm_interface.reset();
|
|
||||||
has_exited = true;
|
has_exited = true;
|
||||||
}
|
}
|
||||||
global_handle = 0;
|
global_handle = 0;
|
||||||
|
@ -90,10 +88,6 @@ void Thread::ResumeFromWait() {
|
||||||
// before actually resuming. We can ignore subsequent wakeups if the thread status has
|
// before actually resuming. We can ignore subsequent wakeups if the thread status has
|
||||||
// already been set to ThreadStatus::Ready.
|
// already been set to ThreadStatus::Ready.
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case ThreadStatus::Running:
|
|
||||||
DEBUG_ASSERT_MSG(false, "Thread with object id {} has already resumed.", GetObjectId());
|
|
||||||
return;
|
|
||||||
case ThreadStatus::Dead:
|
case ThreadStatus::Dead:
|
||||||
// This should never happen, as threads must complete before being stopped.
|
// This should never happen, as threads must complete before being stopped.
|
||||||
DEBUG_ASSERT_MSG(false, "Thread with object id {} cannot be resumed because it's DEAD.",
|
DEBUG_ASSERT_MSG(false, "Thread with object id {} cannot be resumed because it's DEAD.",
|
||||||
|
@ -217,22 +211,9 @@ ResultVal<std::shared_ptr<Thread>> Thread::Create(Core::System& system, ThreadTy
|
||||||
thread->tls_address = 0;
|
thread->tls_address = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
thread->arm_interface.reset();
|
// TODO(peachum): move to ScheduleThread() when scheduler is added so selected core is used
|
||||||
|
// to initialize the context
|
||||||
if ((type_flags & THREADTYPE_HLE) == 0) {
|
if ((type_flags & THREADTYPE_HLE) == 0) {
|
||||||
#ifdef ARCHITECTURE_x86_64
|
|
||||||
if (owner_process && !owner_process->Is64BitProcess()) {
|
|
||||||
thread->arm_interface = std::make_unique<Core::ARM_Dynarmic_32>(
|
|
||||||
system, kernel.Interrupts(), kernel.IsMulticore(), kernel.GetExclusiveMonitor(),
|
|
||||||
processor_id);
|
|
||||||
} else {
|
|
||||||
thread->arm_interface = std::make_unique<Core::ARM_Dynarmic_64>(
|
|
||||||
system, kernel.Interrupts(), kernel.IsMulticore(), kernel.GetExclusiveMonitor(),
|
|
||||||
processor_id);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
#error Platform not supported yet.
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ResetThreadContext32(thread->context_32, static_cast<u32>(stack_top),
|
ResetThreadContext32(thread->context_32, static_cast<u32>(stack_top),
|
||||||
static_cast<u32>(entry_point), static_cast<u32>(arg));
|
static_cast<u32>(entry_point), static_cast<u32>(arg));
|
||||||
ResetThreadContext64(thread->context_64, stack_top, entry_point, arg);
|
ResetThreadContext64(thread->context_64, stack_top, entry_point, arg);
|
||||||
|
@ -268,14 +249,6 @@ VAddr Thread::GetCommandBufferAddress() const {
|
||||||
return GetTLSAddress() + command_header_offset;
|
return GetTLSAddress() + command_header_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
Core::ARM_Interface& Thread::ArmInterface() {
|
|
||||||
return *arm_interface;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Core::ARM_Interface& Thread::ArmInterface() const {
|
|
||||||
return *arm_interface;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Thread::SetStatus(ThreadStatus new_status) {
|
void Thread::SetStatus(ThreadStatus new_status) {
|
||||||
if (new_status == status) {
|
if (new_status == status) {
|
||||||
return;
|
return;
|
||||||
|
@ -283,7 +256,6 @@ void Thread::SetStatus(ThreadStatus new_status) {
|
||||||
|
|
||||||
switch (new_status) {
|
switch (new_status) {
|
||||||
case ThreadStatus::Ready:
|
case ThreadStatus::Ready:
|
||||||
case ThreadStatus::Running:
|
|
||||||
SetSchedulingStatus(ThreadSchedStatus::Runnable);
|
SetSchedulingStatus(ThreadSchedStatus::Runnable);
|
||||||
break;
|
break;
|
||||||
case ThreadStatus::Dormant:
|
case ThreadStatus::Dormant:
|
||||||
|
|
|
@ -72,7 +72,6 @@ enum ThreadProcessorId : s32 {
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class ThreadStatus {
|
enum class ThreadStatus {
|
||||||
Running, ///< Currently running
|
|
||||||
Ready, ///< Ready to run
|
Ready, ///< Ready to run
|
||||||
Paused, ///< Paused by SetThreadActivity or debug
|
Paused, ///< Paused by SetThreadActivity or debug
|
||||||
WaitHLEEvent, ///< Waiting for hle event to finish
|
WaitHLEEvent, ///< Waiting for hle event to finish
|
||||||
|
@ -248,10 +247,6 @@ public:
|
||||||
|
|
||||||
void SetSynchronizationResults(SynchronizationObject* object, ResultCode result);
|
void SetSynchronizationResults(SynchronizationObject* object, ResultCode result);
|
||||||
|
|
||||||
Core::ARM_Interface& ArmInterface();
|
|
||||||
|
|
||||||
const Core::ARM_Interface& ArmInterface() const;
|
|
||||||
|
|
||||||
SynchronizationObject* GetSignalingObject() const {
|
SynchronizationObject* GetSignalingObject() const {
|
||||||
return signaling_object;
|
return signaling_object;
|
||||||
}
|
}
|
||||||
|
@ -586,7 +581,6 @@ private:
|
||||||
Common::SpinLock context_guard{};
|
Common::SpinLock context_guard{};
|
||||||
ThreadContext32 context_32{};
|
ThreadContext32 context_32{};
|
||||||
ThreadContext64 context_64{};
|
ThreadContext64 context_64{};
|
||||||
std::unique_ptr<Core::ARM_Interface> arm_interface{};
|
|
||||||
std::shared_ptr<Common::Fiber> host_context{};
|
std::shared_ptr<Common::Fiber> host_context{};
|
||||||
|
|
||||||
u64 thread_id = 0;
|
u64 thread_id = 0;
|
||||||
|
|
|
@ -24,11 +24,15 @@ TimeManager::TimeManager(Core::System& system_) : system{system_} {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto thread = this->system.Kernel().RetrieveThreadFromGlobalHandleTable(proper_handle);
|
auto thread = this->system.Kernel().RetrieveThreadFromGlobalHandleTable(proper_handle);
|
||||||
|
if (thread) {
|
||||||
|
// Thread can be null if process has exited
|
||||||
thread->OnWakeUp();
|
thread->OnWakeUp();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimeManager::ScheduleTimeEvent(Handle& event_handle, Thread* timetask, s64 nanoseconds) {
|
void TimeManager::ScheduleTimeEvent(Handle& event_handle, Thread* timetask, s64 nanoseconds) {
|
||||||
|
std::lock_guard lock{mutex};
|
||||||
event_handle = timetask->GetGlobalHandle();
|
event_handle = timetask->GetGlobalHandle();
|
||||||
if (nanoseconds > 0) {
|
if (nanoseconds > 0) {
|
||||||
ASSERT(timetask);
|
ASSERT(timetask);
|
||||||
|
@ -43,6 +47,7 @@ void TimeManager::ScheduleTimeEvent(Handle& event_handle, Thread* timetask, s64
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimeManager::UnscheduleTimeEvent(Handle event_handle) {
|
void TimeManager::UnscheduleTimeEvent(Handle event_handle) {
|
||||||
|
std::lock_guard lock{mutex};
|
||||||
if (event_handle == InvalidHandle) {
|
if (event_handle == InvalidHandle) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -51,7 +56,7 @@ void TimeManager::UnscheduleTimeEvent(Handle event_handle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimeManager::CancelTimeEvent(Thread* time_task) {
|
void TimeManager::CancelTimeEvent(Thread* time_task) {
|
||||||
Handle event_handle = time_task->GetGlobalHandle();
|
const Handle event_handle = time_task->GetGlobalHandle();
|
||||||
UnscheduleTimeEvent(event_handle);
|
UnscheduleTimeEvent(event_handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "core/hle/kernel/object.h"
|
#include "core/hle/kernel/object.h"
|
||||||
|
@ -42,6 +43,7 @@ private:
|
||||||
Core::System& system;
|
Core::System& system;
|
||||||
std::shared_ptr<Core::Timing::EventType> time_manager_event_type;
|
std::shared_ptr<Core::Timing::EventType> time_manager_event_type;
|
||||||
std::unordered_map<Handle, bool> cancelled_events;
|
std::unordered_map<Handle, bool> cancelled_events;
|
||||||
|
std::mutex mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Kernel
|
} // namespace Kernel
|
||||||
|
|
|
@ -527,9 +527,6 @@ public:
|
||||||
header.segment_headers[RO_INDEX].memory_size,
|
header.segment_headers[RO_INDEX].memory_size,
|
||||||
header.segment_headers[DATA_INDEX].memory_size, nro_address});
|
header.segment_headers[DATA_INDEX].memory_size, nro_address});
|
||||||
|
|
||||||
// Invalidate JIT caches for the newly mapped process code
|
|
||||||
system.InvalidateCpuInstructionCaches();
|
|
||||||
|
|
||||||
IPC::ResponseBuilder rb{ctx, 4};
|
IPC::ResponseBuilder rb{ctx, 4};
|
||||||
rb.Push(RESULT_SUCCESS);
|
rb.Push(RESULT_SUCCESS);
|
||||||
rb.Push(*map_result);
|
rb.Push(*map_result);
|
||||||
|
@ -590,8 +587,6 @@ public:
|
||||||
|
|
||||||
const auto result{UnmapNro(iter->second)};
|
const auto result{UnmapNro(iter->second)};
|
||||||
|
|
||||||
system.InvalidateCpuInstructionCaches();
|
|
||||||
|
|
||||||
nro.erase(iter);
|
nro.erase(iter);
|
||||||
|
|
||||||
IPC::ResponseBuilder rb{ctx, 2};
|
IPC::ResponseBuilder rb{ctx, 2};
|
||||||
|
|
|
@ -24,7 +24,6 @@
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
constexpr std::array<std::array<Qt::GlobalColor, 2>, 10> WaitTreeColors{{
|
constexpr std::array<std::array<Qt::GlobalColor, 2>, 10> WaitTreeColors{{
|
||||||
{Qt::GlobalColor::darkGreen, Qt::GlobalColor::green},
|
|
||||||
{Qt::GlobalColor::darkGreen, Qt::GlobalColor::green},
|
{Qt::GlobalColor::darkGreen, Qt::GlobalColor::green},
|
||||||
{Qt::GlobalColor::darkBlue, Qt::GlobalColor::cyan},
|
{Qt::GlobalColor::darkBlue, Qt::GlobalColor::cyan},
|
||||||
{Qt::GlobalColor::lightGray, Qt::GlobalColor::lightGray},
|
{Qt::GlobalColor::lightGray, Qt::GlobalColor::lightGray},
|
||||||
|
@ -239,9 +238,6 @@ QString WaitTreeThread::GetText() const {
|
||||||
const auto& thread = static_cast<const Kernel::Thread&>(object);
|
const auto& thread = static_cast<const Kernel::Thread&>(object);
|
||||||
QString status;
|
QString status;
|
||||||
switch (thread.GetStatus()) {
|
switch (thread.GetStatus()) {
|
||||||
case Kernel::ThreadStatus::Running:
|
|
||||||
status = tr("running");
|
|
||||||
break;
|
|
||||||
case Kernel::ThreadStatus::Ready:
|
case Kernel::ThreadStatus::Ready:
|
||||||
if (!thread.IsPaused()) {
|
if (!thread.IsPaused()) {
|
||||||
if (thread.WasRunning()) {
|
if (thread.WasRunning()) {
|
||||||
|
@ -298,34 +294,32 @@ QColor WaitTreeThread::GetColor() const {
|
||||||
|
|
||||||
const auto& thread = static_cast<const Kernel::Thread&>(object);
|
const auto& thread = static_cast<const Kernel::Thread&>(object);
|
||||||
switch (thread.GetStatus()) {
|
switch (thread.GetStatus()) {
|
||||||
case Kernel::ThreadStatus::Running:
|
|
||||||
return QColor(WaitTreeColors[0][color_index]);
|
|
||||||
case Kernel::ThreadStatus::Ready:
|
case Kernel::ThreadStatus::Ready:
|
||||||
if (!thread.IsPaused()) {
|
if (!thread.IsPaused()) {
|
||||||
if (thread.WasRunning()) {
|
if (thread.WasRunning()) {
|
||||||
|
return QColor(WaitTreeColors[0][color_index]);
|
||||||
|
} else {
|
||||||
return QColor(WaitTreeColors[1][color_index]);
|
return QColor(WaitTreeColors[1][color_index]);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return QColor(WaitTreeColors[2][color_index]);
|
return QColor(WaitTreeColors[2][color_index]);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return QColor(WaitTreeColors[3][color_index]);
|
|
||||||
}
|
|
||||||
case Kernel::ThreadStatus::Paused:
|
case Kernel::ThreadStatus::Paused:
|
||||||
return QColor(WaitTreeColors[4][color_index]);
|
return QColor(WaitTreeColors[3][color_index]);
|
||||||
case Kernel::ThreadStatus::WaitHLEEvent:
|
case Kernel::ThreadStatus::WaitHLEEvent:
|
||||||
case Kernel::ThreadStatus::WaitIPC:
|
case Kernel::ThreadStatus::WaitIPC:
|
||||||
return QColor(WaitTreeColors[5][color_index]);
|
return QColor(WaitTreeColors[4][color_index]);
|
||||||
case Kernel::ThreadStatus::WaitSleep:
|
case Kernel::ThreadStatus::WaitSleep:
|
||||||
return QColor(WaitTreeColors[6][color_index]);
|
return QColor(WaitTreeColors[5][color_index]);
|
||||||
case Kernel::ThreadStatus::WaitSynch:
|
case Kernel::ThreadStatus::WaitSynch:
|
||||||
case Kernel::ThreadStatus::WaitMutex:
|
case Kernel::ThreadStatus::WaitMutex:
|
||||||
case Kernel::ThreadStatus::WaitCondVar:
|
case Kernel::ThreadStatus::WaitCondVar:
|
||||||
case Kernel::ThreadStatus::WaitArb:
|
case Kernel::ThreadStatus::WaitArb:
|
||||||
return QColor(WaitTreeColors[7][color_index]);
|
return QColor(WaitTreeColors[6][color_index]);
|
||||||
case Kernel::ThreadStatus::Dormant:
|
case Kernel::ThreadStatus::Dormant:
|
||||||
return QColor(WaitTreeColors[8][color_index]);
|
return QColor(WaitTreeColors[7][color_index]);
|
||||||
case Kernel::ThreadStatus::Dead:
|
case Kernel::ThreadStatus::Dead:
|
||||||
return QColor(WaitTreeColors[9][color_index]);
|
return QColor(WaitTreeColors[8][color_index]);
|
||||||
default:
|
default:
|
||||||
return WaitTreeItem::GetColor();
|
return WaitTreeItem::GetColor();
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue