Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEXCore: Moves SignalThread/SignalEvent to Frontend #4176

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions FEXCore/include/FEXCore/Core/SignalDelegator.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ namespace FEXCore {
namespace Core {
struct InternalThreadState;

enum class SignalEvent {
Nothing, // If the guest uses our signal we need to know it was errant on our end
Pause,
Stop,
Return,
ReturnRT,
};

enum SignalNumber {
#ifndef _WIN32
FAULT_SIGSEGV = SIGSEGV,
Expand Down Expand Up @@ -78,14 +70,6 @@ class SignalDelegator {
return Config;
}

/**
* @brief Signals a thread with a specific core event.
*
* @param Thread Which thread to signal.
* @param Event Which event to signal the event with.
*/
virtual void SignalThread(FEXCore::Core::InternalThreadState* Thread, Core::SignalEvent Event) = 0;

protected:
SignalDelegatorConfig Config;
};
Expand Down
1 change: 0 additions & 1 deletion FEXCore/include/FEXCore/Debug/InternalThreadState.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ struct InternalThreadState : public FEXCore::Allocator::FEXAllocOperators {
} RunningEvents;

FEXCore::Context::Context* CTX;
std::atomic<SignalEvent> SignalReason {SignalEvent::Nothing};

NonMovableUniquePtr<FEXCore::Threads::Thread> ExecutionThread;
bool StartPaused {false};
Expand Down
2 changes: 0 additions & 2 deletions Source/Tools/CommonTools/DummyHandlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class DummySyscallHandler : public FEXCore::HLE::SyscallHandler, public FEXCore:

class DummySignalDelegator final : public FEXCore::SignalDelegator, public FEXCore::Allocator::FEXAllocOperators {
public:
void SignalThread(FEXCore::Core::InternalThreadState* Thread, FEXCore::Core::SignalEvent Event) override {}

FEXCore::Core::InternalThreadState* GetBackingTLSThread() {
return GetTLSThread();
}
Expand Down
26 changes: 13 additions & 13 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,11 @@ bool SignalDelegator::HandleSIGILL(FEXCore::Core::InternalThreadState* Thread, i
}

bool SignalDelegator::HandleSignalPause(FEXCore::Core::InternalThreadState* Thread, int Signal, void* info, void* ucontext) {
FEXCore::Core::SignalEvent SignalReason = Thread->SignalReason.load();
auto ThreadObject = FEX::HLE::ThreadManager::GetStateObjectFromFEXCoreThread(Thread);
SignalEvent SignalReason = ThreadObject->SignalReason.load();
auto Frame = Thread->CurrentFrame;

if (SignalReason == FEXCore::Core::SignalEvent::Pause) {
if (SignalReason == SignalEvent::Pause) {
// Store our thread state so we can come back to this
StoreThreadState(Thread, Signal, ucontext);

Expand All @@ -446,11 +447,11 @@ bool SignalDelegator::HandleSignalPause(FEXCore::Core::InternalThreadState* Thre
// We use this to track if it is safe to clear cache
++Thread->CurrentFrame->SignalHandlerRefCounter;

Thread->SignalReason.store(FEXCore::Core::SignalEvent::Nothing);
ThreadObject->SignalReason.store(SignalEvent::Nothing);
return true;
}

if (SignalReason == FEXCore::Core::SignalEvent::Stop) {
if (SignalReason == SignalEvent::Stop) {
// Our thread is stopping
// We don't care about anything at this point
// Set the stack to our starting location when we entered the core and get out safely
Expand Down Expand Up @@ -479,31 +480,30 @@ bool SignalDelegator::HandleSignalPause(FEXCore::Core::InternalThreadState* Thre
FEX::HLE::_SyscallHandler->TM.IncrementIdleRefCount();
}

Thread->SignalReason.store(FEXCore::Core::SignalEvent::Nothing);
ThreadObject->SignalReason.store(SignalEvent::Nothing);
return true;
}

if (SignalReason == FEXCore::Core::SignalEvent::Return || SignalReason == FEXCore::Core::SignalEvent::ReturnRT) {
RestoreThreadState(Thread, ucontext,
SignalReason == FEXCore::Core::SignalEvent::ReturnRT ? RestoreType::TYPE_REALTIME : RestoreType::TYPE_NONREALTIME);
if (SignalReason == SignalEvent::Return || SignalReason == SignalEvent::ReturnRT) {
RestoreThreadState(Thread, ucontext, SignalReason == SignalEvent::ReturnRT ? RestoreType::TYPE_REALTIME : RestoreType::TYPE_NONREALTIME);

// Ref count our faults
// We use this to track if it is safe to clear cache
--Thread->CurrentFrame->SignalHandlerRefCounter;

Thread->SignalReason.store(FEXCore::Core::SignalEvent::Nothing);
ThreadObject->SignalReason.store(SignalEvent::Nothing);
return true;
}
return false;
}

void SignalDelegator::SignalThread(FEXCore::Core::InternalThreadState* Thread, FEXCore::Core::SignalEvent Event) {
auto ThreadObject = static_cast<const FEX::HLE::ThreadStateObject*>(Thread->FrontendPtr);
if (Event == FEXCore::Core::SignalEvent::Pause && Thread->RunningEvents.Running.load() == false) {
void SignalDelegator::SignalThread(FEXCore::Core::InternalThreadState* Thread, SignalEvent Event) {
auto ThreadObject = FEX::HLE::ThreadManager::GetStateObjectFromFEXCoreThread(Thread);
if (Event == SignalEvent::Pause && Thread->RunningEvents.Running.load() == false) {
// Skip signaling a thread if it is already paused.
return;
}
Thread->SignalReason.store(Event);
ThreadObject->SignalReason.store(Event);
FHU::Syscalls::tgkill(ThreadObject->ThreadInfo.PID, ThreadObject->ThreadInfo.TID, SignalDelegator::SIGNAL_FOR_PAUSE);
}

Expand Down
8 changes: 7 additions & 1 deletion Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ class SignalDelegator final : public FEXCore::SignalDelegator, public FEXCore::A
FEX_UNREACHABLE;
}

void SignalThread(FEXCore::Core::InternalThreadState* Thread, FEXCore::Core::SignalEvent Event) override;
/**
* @brief Signals a thread with a specific core event.
*
* @param Thread Which thread to signal.
* @param Event Which event to signal the event with.
*/
void SignalThread(FEXCore::Core::InternalThreadState* Thread, SignalEvent Event);

FEXCore::ArchHelpers::Arm64::UnalignedHandlerType GetUnalignedHandlerType() const {
return UnalignedHandlerType;
Expand Down
6 changes: 3 additions & 3 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void ThreadManager::DestroyThread(FEX::HLE::ThreadStateObject* Thread, bool Need

void ThreadManager::StopThread(FEX::HLE::ThreadStateObject* Thread) {
if (Thread->Thread->RunningEvents.Running.exchange(false)) {
SignalDelegation->SignalThread(Thread->Thread, FEXCore::Core::SignalEvent::Stop);
SignalDelegation->SignalThread(Thread->Thread, SignalEvent::Stop);
}
}

Expand Down Expand Up @@ -74,7 +74,7 @@ void ThreadManager::NotifyPause() {
// Tell all the threads that they should pause
std::lock_guard lk(ThreadCreationMutex);
for (auto& Thread : Threads) {
SignalDelegation->SignalThread(Thread->Thread, FEXCore::Core::SignalEvent::Pause);
SignalDelegation->SignalThread(Thread->Thread, SignalEvent::Pause);
}
}

Expand All @@ -87,7 +87,7 @@ void ThreadManager::Run() {
// Spin up all the threads
std::lock_guard lk(ThreadCreationMutex);
for (auto& Thread : Threads) {
Thread->Thread->SignalReason.store(FEXCore::Core::SignalEvent::Return);
Thread->SignalReason.store(SignalEvent::Return);
}

for (auto& Thread : Threads) {
Expand Down
11 changes: 11 additions & 0 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ namespace FEX::HLE {
class SyscallHandler;
class SignalDelegator;

enum class SignalEvent : uint32_t {
Nothing, // If the guest uses our signal we need to know it was errant on our end
Pause,
Stop,
Return,
ReturnRT,
};

struct ThreadStateObject : public FEXCore::Allocator::FEXAllocOperators {
FEXCore::Core::InternalThreadState* Thread;

Expand Down Expand Up @@ -58,6 +66,9 @@ struct ThreadStateObject : public FEXCore::Allocator::FEXAllocOperators {

// personality emulation.
uint32_t persona {};

// Thread signaling information
std::atomic<SignalEvent> SignalReason {SignalEvent::Nothing};
};

class ThreadManager final {
Expand Down
Loading