svc: Implement svcLockMutex.
This commit is contained in:
parent
d0046b120a
commit
b9950cd4b0
|
@ -7,8 +7,10 @@
|
||||||
#include <boost/range/algorithm_ext/erase.hpp>
|
#include <boost/range/algorithm_ext/erase.hpp>
|
||||||
#include "common/assert.h"
|
#include "common/assert.h"
|
||||||
#include "core/core.h"
|
#include "core/core.h"
|
||||||
|
#include "core/hle/kernel/handle_table.h"
|
||||||
#include "core/hle/kernel/kernel.h"
|
#include "core/hle/kernel/kernel.h"
|
||||||
#include "core/hle/kernel/mutex.h"
|
#include "core/hle/kernel/mutex.h"
|
||||||
|
#include "core/hle/kernel/object_address_table.h"
|
||||||
#include "core/hle/kernel/thread.h"
|
#include "core/hle/kernel/thread.h"
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
@ -25,17 +27,25 @@ void ReleaseThreadMutexes(Thread* thread) {
|
||||||
Mutex::Mutex() {}
|
Mutex::Mutex() {}
|
||||||
Mutex::~Mutex() {}
|
Mutex::~Mutex() {}
|
||||||
|
|
||||||
SharedPtr<Mutex> Mutex::Create(bool initial_locked, VAddr addr, std::string name) {
|
SharedPtr<Mutex> Mutex::Create(SharedPtr<Kernel::Thread> holding_thread, VAddr guest_addr,
|
||||||
|
std::string name) {
|
||||||
SharedPtr<Mutex> mutex(new Mutex);
|
SharedPtr<Mutex> mutex(new Mutex);
|
||||||
|
|
||||||
mutex->lock_count = 0;
|
mutex->lock_count = 0;
|
||||||
mutex->addr = addr;
|
mutex->guest_addr = guest_addr;
|
||||||
mutex->name = std::move(name);
|
mutex->name = std::move(name);
|
||||||
mutex->holding_thread = nullptr;
|
mutex->holding_thread = nullptr;
|
||||||
|
|
||||||
// Acquire mutex with current thread if initialized as locked
|
// If mutex was initialized with a holding thread, acquire it by the holding thread
|
||||||
if (initial_locked)
|
if (holding_thread) {
|
||||||
mutex->Acquire(GetCurrentThread());
|
mutex->Acquire(holding_thread.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mutexes are referenced by guest address, so track this in the kernel
|
||||||
|
g_object_address_table.Insert(guest_addr, mutex);
|
||||||
|
|
||||||
|
// Verify that the created mutex matches the guest state for the mutex
|
||||||
|
mutex->VerifyGuestState();
|
||||||
|
|
||||||
return mutex;
|
return mutex;
|
||||||
}
|
}
|
||||||
|
@ -53,38 +63,60 @@ void Mutex::Acquire(Thread* thread) {
|
||||||
thread->held_mutexes.insert(this);
|
thread->held_mutexes.insert(this);
|
||||||
holding_thread = thread;
|
holding_thread = thread;
|
||||||
thread->UpdatePriority();
|
thread->UpdatePriority();
|
||||||
|
UpdateGuestState();
|
||||||
Core::System::GetInstance().PrepareReschedule();
|
Core::System::GetInstance().PrepareReschedule();
|
||||||
}
|
}
|
||||||
|
|
||||||
lock_count++;
|
lock_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mutex::Release() {
|
ResultCode Mutex::Release(Thread* thread) {
|
||||||
// Only release if the mutex is held
|
// We can only release the mutex if it's held by the calling thread.
|
||||||
if (lock_count > 0) {
|
if (thread != holding_thread) {
|
||||||
lock_count--;
|
if (holding_thread) {
|
||||||
|
LOG_ERROR(
|
||||||
// Yield to the next thread only if we've fully released the mutex
|
Kernel,
|
||||||
if (lock_count == 0) {
|
"Tried to release a mutex (owned by thread id %u) from a different thread id %u",
|
||||||
holding_thread->held_mutexes.erase(this);
|
holding_thread->thread_id, thread->thread_id);
|
||||||
holding_thread->UpdatePriority();
|
|
||||||
holding_thread = nullptr;
|
|
||||||
WakeupAllWaitingThreads();
|
|
||||||
Core::System::GetInstance().PrepareReschedule();
|
|
||||||
}
|
}
|
||||||
|
// TODO(bunnei): Use correct error code
|
||||||
|
return ResultCode(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Note: It should not be possible for the situation where the mutex has a holding thread with a
|
||||||
|
// zero lock count to occur. The real kernel still checks for this, so we do too.
|
||||||
|
if (lock_count <= 0) {
|
||||||
|
// TODO(bunnei): Use correct error code
|
||||||
|
return ResultCode(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
lock_count--;
|
||||||
|
|
||||||
|
// Yield to the next thread only if we've fully released the mutex
|
||||||
|
if (lock_count == 0) {
|
||||||
|
holding_thread->held_mutexes.erase(this);
|
||||||
|
holding_thread->UpdatePriority();
|
||||||
|
holding_thread = nullptr;
|
||||||
|
WakeupAllWaitingThreads();
|
||||||
|
UpdateGuestState();
|
||||||
|
Core::System::GetInstance().PrepareReschedule();
|
||||||
|
}
|
||||||
|
|
||||||
|
return RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mutex::AddWaitingThread(SharedPtr<Thread> thread) {
|
void Mutex::AddWaitingThread(SharedPtr<Thread> thread) {
|
||||||
WaitObject::AddWaitingThread(thread);
|
WaitObject::AddWaitingThread(thread);
|
||||||
thread->pending_mutexes.insert(this);
|
thread->pending_mutexes.insert(this);
|
||||||
UpdatePriority();
|
UpdatePriority();
|
||||||
|
UpdateGuestState();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mutex::RemoveWaitingThread(Thread* thread) {
|
void Mutex::RemoveWaitingThread(Thread* thread) {
|
||||||
WaitObject::RemoveWaitingThread(thread);
|
WaitObject::RemoveWaitingThread(thread);
|
||||||
thread->pending_mutexes.erase(this);
|
thread->pending_mutexes.erase(this);
|
||||||
UpdatePriority();
|
UpdatePriority();
|
||||||
|
UpdateGuestState();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mutex::UpdatePriority() {
|
void Mutex::UpdatePriority() {
|
||||||
|
@ -103,4 +135,17 @@ void Mutex::UpdatePriority() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
void Mutex::UpdateGuestState() {
|
||||||
|
GuestState guest_state{Memory::Read32(guest_addr)};
|
||||||
|
guest_state.has_waiters.Assign(!GetWaitingThreads().empty());
|
||||||
|
guest_state.holding_thread_handle.Assign(holding_thread ? holding_thread->guest_handle : 0);
|
||||||
|
Memory::Write32(guest_addr, guest_state.raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mutex::VerifyGuestState() {
|
||||||
|
GuestState guest_state{Memory::Read32(guest_addr)};
|
||||||
|
ASSERT(guest_state.has_waiters == !GetWaitingThreads().empty());
|
||||||
|
ASSERT(guest_state.holding_thread_handle == holding_thread->guest_handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Kernel
|
||||||
|
|
|
@ -6,8 +6,10 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
|
#include "common/swap.h"
|
||||||
#include "core/hle/kernel/kernel.h"
|
#include "core/hle/kernel/kernel.h"
|
||||||
#include "core/hle/kernel/wait_object.h"
|
#include "core/hle/kernel/wait_object.h"
|
||||||
|
#include "core/hle/result.h"
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
|
@ -17,11 +19,15 @@ class Mutex final : public WaitObject {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Creates a mutex.
|
* Creates a mutex.
|
||||||
* @param initial_locked Specifies if the mutex should be locked initially
|
* @param holding_thread Specifies a thread already holding the mutex. If not nullptr, this
|
||||||
|
* thread will acquire the mutex.
|
||||||
|
* @param guest_addr Address of the object tracking the mutex in guest memory. If specified,
|
||||||
|
* this mutex will update the guest object when its state changes.
|
||||||
* @param name Optional name of mutex
|
* @param name Optional name of mutex
|
||||||
* @return Pointer to new Mutex object
|
* @return Pointer to new Mutex object
|
||||||
*/
|
*/
|
||||||
static SharedPtr<Mutex> Create(bool initial_locked, VAddr addr, std::string name = "Unknown");
|
static SharedPtr<Mutex> Create(SharedPtr<Kernel::Thread> holding_thread, VAddr guest_addr = 0,
|
||||||
|
std::string name = "Unknown");
|
||||||
|
|
||||||
std::string GetTypeName() const override {
|
std::string GetTypeName() const override {
|
||||||
return "Mutex";
|
return "Mutex";
|
||||||
|
@ -39,7 +45,7 @@ public:
|
||||||
u32 priority; ///< The priority of the mutex, used for priority inheritance.
|
u32 priority; ///< The priority of the mutex, used for priority inheritance.
|
||||||
std::string name; ///< Name of mutex (optional)
|
std::string name; ///< Name of mutex (optional)
|
||||||
SharedPtr<Thread> holding_thread; ///< Thread that has acquired the mutex
|
SharedPtr<Thread> holding_thread; ///< Thread that has acquired the mutex
|
||||||
VAddr addr;
|
VAddr guest_addr; ///< Address of the guest mutex value
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Elevate the mutex priority to the best priority
|
* Elevate the mutex priority to the best priority
|
||||||
|
@ -53,11 +59,32 @@ public:
|
||||||
void AddWaitingThread(SharedPtr<Thread> thread) override;
|
void AddWaitingThread(SharedPtr<Thread> thread) override;
|
||||||
void RemoveWaitingThread(Thread* thread) override;
|
void RemoveWaitingThread(Thread* thread) override;
|
||||||
|
|
||||||
void Release();
|
/**
|
||||||
|
* Attempts to release the mutex from the specified thread.
|
||||||
|
* @param thread Thread that wants to release the mutex.
|
||||||
|
* @returns The result code of the operation.
|
||||||
|
*/
|
||||||
|
ResultCode Release(Thread* thread);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex();
|
Mutex();
|
||||||
~Mutex() override;
|
~Mutex() override;
|
||||||
|
|
||||||
|
/// Object in guest memory used to track the mutex state
|
||||||
|
union GuestState {
|
||||||
|
u32_le raw;
|
||||||
|
/// Handle of the thread that currently holds the mutex, 0 if available
|
||||||
|
BitField<0, 30, u32_le> holding_thread_handle;
|
||||||
|
/// 1 when there are threads waiting for this mutex, otherwise 0
|
||||||
|
BitField<30, 1, u32_le> has_waiters;
|
||||||
|
};
|
||||||
|
static_assert(sizeof(GuestState) == 4, "GuestState size is incorrect");
|
||||||
|
|
||||||
|
/// Updates the state of the object tracking this mutex in guest memory
|
||||||
|
void UpdateGuestState();
|
||||||
|
|
||||||
|
/// Verifies the state of the object tracking this mutex in guest memory
|
||||||
|
void VerifyGuestState();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,4 +93,4 @@ private:
|
||||||
*/
|
*/
|
||||||
void ReleaseThreadMutexes(Thread* thread);
|
void ReleaseThreadMutexes(Thread* thread);
|
||||||
|
|
||||||
} // namespace
|
} // namespace Kernel
|
||||||
|
|
|
@ -122,6 +122,44 @@ static ResultCode GetProcessId(u32* process_id, Kernel::Handle process_handle) {
|
||||||
return RESULT_SUCCESS;
|
return RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Attempts to locks a mutex, creating it if it does not already exist
|
||||||
|
static ResultCode LockMutex(Handle holding_thread_handle, VAddr mutex_addr,
|
||||||
|
Handle requesting_thread_handle) {
|
||||||
|
LOG_TRACE(Kernel_SVC,
|
||||||
|
"called holding_thread_handle=0x%08X, mutex_addr=0x%llx, "
|
||||||
|
"requesting_current_thread_handle=0x%08X",
|
||||||
|
holding_thread_handle, mutex_addr, requesting_thread_handle);
|
||||||
|
|
||||||
|
SharedPtr<Kernel::Thread> holding_thread =
|
||||||
|
Kernel::g_handle_table.Get<Kernel::Thread>(holding_thread_handle);
|
||||||
|
SharedPtr<Kernel::Thread> requesting_thread =
|
||||||
|
Kernel::g_handle_table.Get<Kernel::Thread>(requesting_thread_handle);
|
||||||
|
|
||||||
|
ASSERT(holding_thread);
|
||||||
|
ASSERT(requesting_thread);
|
||||||
|
|
||||||
|
SharedPtr<Kernel::Mutex> mutex = Kernel::g_object_address_table.Get<Kernel::Mutex>(mutex_addr);
|
||||||
|
if (!mutex) {
|
||||||
|
// Create a new mutex for the specified address if one does not already exist
|
||||||
|
mutex = Kernel::Mutex::Create(holding_thread, mutex_addr);
|
||||||
|
mutex->name = Common::StringFromFormat("mutex-%llx", mutex_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mutex->ShouldWait(requesting_thread.get())) {
|
||||||
|
// If we cannot lock the mutex, then put the thread too sleep and trigger a reschedule
|
||||||
|
requesting_thread->wait_objects = {mutex};
|
||||||
|
mutex->AddWaitingThread(requesting_thread);
|
||||||
|
requesting_thread->status = THREADSTATUS_WAIT_SYNCH_ANY;
|
||||||
|
|
||||||
|
Core::System::GetInstance().PrepareReschedule();
|
||||||
|
} else {
|
||||||
|
// The mutex is available, lock it
|
||||||
|
mutex->Acquire(requesting_thread.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
return RESULT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
/// Break program execution
|
/// Break program execution
|
||||||
static void Break(u64 unk_0, u64 unk_1, u64 unk_2) {
|
static void Break(u64 unk_0, u64 unk_1, u64 unk_2) {
|
||||||
LOG_CRITICAL(Debug_Emulated, "Emulated program broke execution!");
|
LOG_CRITICAL(Debug_Emulated, "Emulated program broke execution!");
|
||||||
|
@ -371,8 +409,8 @@ static const FunctionDef SVC_Table[] = {
|
||||||
{0x17, nullptr, "svcResetSignal"},
|
{0x17, nullptr, "svcResetSignal"},
|
||||||
{0x18, nullptr, "svcWaitSynchronization"},
|
{0x18, nullptr, "svcWaitSynchronization"},
|
||||||
{0x19, nullptr, "svcCancelSynchronization"},
|
{0x19, nullptr, "svcCancelSynchronization"},
|
||||||
{0x1A, nullptr, "svcLockMutex"},
|
|
||||||
{0x1B, nullptr, "svcUnlockMutex"},
|
{0x1B, nullptr, "svcUnlockMutex"},
|
||||||
|
{0x1A, HLE::Wrap<LockMutex>, "svcLockMutex"},
|
||||||
{0x1C, nullptr, "svcWaitProcessWideKeyAtomic"},
|
{0x1C, nullptr, "svcWaitProcessWideKeyAtomic"},
|
||||||
{0x1D, HLE::Wrap<SignalProcessWideKey>, "svcSignalProcessWideKey"},
|
{0x1D, HLE::Wrap<SignalProcessWideKey>, "svcSignalProcessWideKey"},
|
||||||
{0x1E, nullptr, "svcGetSystemTick"},
|
{0x1E, nullptr, "svcGetSystemTick"},
|
||||||
|
|
Reference in New Issue