summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_address_arbiter.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/core/hle/kernel/k_address_arbiter.cpp205
1 files changed, 111 insertions, 94 deletions
diff --git a/src/core/hle/kernel/k_address_arbiter.cpp b/src/core/hle/kernel/k_address_arbiter.cpp
index a442a3b98..78d43d729 100644
--- a/src/core/hle/kernel/k_address_arbiter.cpp
+++ b/src/core/hle/kernel/k_address_arbiter.cpp
@@ -8,46 +8,57 @@
#include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h"
#include "core/hle/kernel/k_thread.h"
#include "core/hle/kernel/k_thread_queue.h"
+#include "core/hle/kernel/k_typed_address.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/svc_results.h"
#include "core/memory.h"
namespace Kernel {
-KAddressArbiter::KAddressArbiter(Core::System& system_)
- : system{system_}, kernel{system.Kernel()} {}
+KAddressArbiter::KAddressArbiter(Core::System& system)
+ : m_system{system}, m_kernel{system.Kernel()} {}
KAddressArbiter::~KAddressArbiter() = default;
namespace {
-bool ReadFromUser(Core::System& system, s32* out, VAddr address) {
- *out = system.Memory().Read32(address);
+bool ReadFromUser(KernelCore& kernel, s32* out, KProcessAddress address) {
+ *out = GetCurrentMemory(kernel).Read32(GetInteger(address));
return true;
}
-bool DecrementIfLessThan(Core::System& system, s32* out, VAddr address, s32 value) {
+bool DecrementIfLessThan(Core::System& system, s32* out, KProcessAddress address, s32 value) {
auto& monitor = system.Monitor();
const auto current_core = system.Kernel().CurrentPhysicalCoreIndex();
- // TODO(bunnei): We should disable interrupts here via KScopedInterruptDisable.
+ // NOTE: If scheduler lock is not held here, interrupt disable is required.
+ // KScopedInterruptDisable di;
+
// TODO(bunnei): We should call CanAccessAtomic(..) here.
- // Load the value from the address.
- const s32 current_value = static_cast<s32>(monitor.ExclusiveRead32(current_core, address));
+ s32 current_value{};
+
+ while (true) {
+ // Load the value from the address.
+ current_value =
+ static_cast<s32>(monitor.ExclusiveRead32(current_core, GetInteger(address)));
+
+ // Compare it to the desired one.
+ if (current_value < value) {
+ // If less than, we want to try to decrement.
+ const s32 decrement_value = current_value - 1;
- // Compare it to the desired one.
- if (current_value < value) {
- // If less than, we want to try to decrement.
- const s32 decrement_value = current_value - 1;
+ // Decrement and try to store.
+ if (monitor.ExclusiveWrite32(current_core, GetInteger(address),
+ static_cast<u32>(decrement_value))) {
+ break;
+ }
- // Decrement and try to store.
- if (!monitor.ExclusiveWrite32(current_core, address, static_cast<u32>(decrement_value))) {
// If we failed to store, try again.
- DecrementIfLessThan(system, out, address, value);
+ } else {
+ // Otherwise, clear our exclusive hold and finish
+ monitor.ClearExclusive(current_core);
+ break;
}
- } else {
- // Otherwise, clear our exclusive hold and finish
- monitor.ClearExclusive(current_core);
}
// We're done.
@@ -55,28 +66,39 @@ bool DecrementIfLessThan(Core::System& system, s32* out, VAddr address, s32 valu
return true;
}
-bool UpdateIfEqual(Core::System& system, s32* out, VAddr address, s32 value, s32 new_value) {
+bool UpdateIfEqual(Core::System& system, s32* out, KProcessAddress address, s32 value,
+ s32 new_value) {
auto& monitor = system.Monitor();
const auto current_core = system.Kernel().CurrentPhysicalCoreIndex();
- // TODO(bunnei): We should disable interrupts here via KScopedInterruptDisable.
+ // NOTE: If scheduler lock is not held here, interrupt disable is required.
+ // KScopedInterruptDisable di;
+
// TODO(bunnei): We should call CanAccessAtomic(..) here.
- // Load the value from the address.
- const s32 current_value = static_cast<s32>(monitor.ExclusiveRead32(current_core, address));
+ s32 current_value{};
- // Compare it to the desired one.
- if (current_value == value) {
- // If equal, we want to try to write the new value.
+ // Load the value from the address.
+ while (true) {
+ current_value =
+ static_cast<s32>(monitor.ExclusiveRead32(current_core, GetInteger(address)));
+
+ // Compare it to the desired one.
+ if (current_value == value) {
+ // If equal, we want to try to write the new value.
+
+ // Try to store.
+ if (monitor.ExclusiveWrite32(current_core, GetInteger(address),
+ static_cast<u32>(new_value))) {
+ break;
+ }
- // Try to store.
- if (!monitor.ExclusiveWrite32(current_core, address, static_cast<u32>(new_value))) {
// If we failed to store, try again.
- UpdateIfEqual(system, out, address, value, new_value);
+ } else {
+ // Otherwise, clear our exclusive hold and finish.
+ monitor.ClearExclusive(current_core);
+ break;
}
- } else {
- // Otherwise, clear our exclusive hold and finish.
- monitor.ClearExclusive(current_core);
}
// We're done.
@@ -86,8 +108,8 @@ bool UpdateIfEqual(Core::System& system, s32* out, VAddr address, s32 value, s32
class ThreadQueueImplForKAddressArbiter final : public KThreadQueue {
public:
- explicit ThreadQueueImplForKAddressArbiter(KernelCore& kernel_, KAddressArbiter::ThreadTree* t)
- : KThreadQueue(kernel_), m_tree(t) {}
+ explicit ThreadQueueImplForKAddressArbiter(KernelCore& kernel, KAddressArbiter::ThreadTree* t)
+ : KThreadQueue(kernel), m_tree(t) {}
void CancelWait(KThread* waiting_thread, Result wait_result, bool cancel_timer_task) override {
// If the thread is waiting on an address arbiter, remove it from the tree.
@@ -101,19 +123,19 @@ public:
}
private:
- KAddressArbiter::ThreadTree* m_tree;
+ KAddressArbiter::ThreadTree* m_tree{};
};
} // namespace
-Result KAddressArbiter::Signal(VAddr addr, s32 count) {
+Result KAddressArbiter::Signal(uint64_t addr, s32 count) {
// Perform signaling.
s32 num_waiters{};
{
- KScopedSchedulerLock sl(kernel);
+ KScopedSchedulerLock sl(m_kernel);
- auto it = thread_tree.nfind_key({addr, -1});
- while ((it != thread_tree.end()) && (count <= 0 || num_waiters < count) &&
+ auto it = m_tree.nfind_key({addr, -1});
+ while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) &&
(it->GetAddressArbiterKey() == addr)) {
// End the thread's wait.
KThread* target_thread = std::addressof(*it);
@@ -122,31 +144,27 @@ Result KAddressArbiter::Signal(VAddr addr, s32 count) {
ASSERT(target_thread->IsWaitingForAddressArbiter());
target_thread->ClearAddressArbiter();
- it = thread_tree.erase(it);
+ it = m_tree.erase(it);
++num_waiters;
}
}
- return ResultSuccess;
+ R_SUCCEED();
}
-Result KAddressArbiter::SignalAndIncrementIfEqual(VAddr addr, s32 value, s32 count) {
+Result KAddressArbiter::SignalAndIncrementIfEqual(uint64_t addr, s32 value, s32 count) {
// Perform signaling.
s32 num_waiters{};
{
- KScopedSchedulerLock sl(kernel);
+ KScopedSchedulerLock sl(m_kernel);
// Check the userspace value.
s32 user_value{};
- if (!UpdateIfEqual(system, &user_value, addr, value, value + 1)) {
- LOG_ERROR(Kernel, "Invalid current memory!");
- return ResultInvalidCurrentMemory;
- }
- if (user_value != value) {
- return ResultInvalidState;
- }
+ R_UNLESS(UpdateIfEqual(m_system, std::addressof(user_value), addr, value, value + 1),
+ ResultInvalidCurrentMemory);
+ R_UNLESS(user_value == value, ResultInvalidState);
- auto it = thread_tree.nfind_key({addr, -1});
- while ((it != thread_tree.end()) && (count <= 0 || num_waiters < count) &&
+ auto it = m_tree.nfind_key({addr, -1});
+ while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) &&
(it->GetAddressArbiterKey() == addr)) {
// End the thread's wait.
KThread* target_thread = std::addressof(*it);
@@ -155,33 +173,33 @@ Result KAddressArbiter::SignalAndIncrementIfEqual(VAddr addr, s32 value, s32 cou
ASSERT(target_thread->IsWaitingForAddressArbiter());
target_thread->ClearAddressArbiter();
- it = thread_tree.erase(it);
+ it = m_tree.erase(it);
++num_waiters;
}
}
- return ResultSuccess;
+ R_SUCCEED();
}
-Result KAddressArbiter::SignalAndModifyByWaitingCountIfEqual(VAddr addr, s32 value, s32 count) {
+Result KAddressArbiter::SignalAndModifyByWaitingCountIfEqual(uint64_t addr, s32 value, s32 count) {
// Perform signaling.
s32 num_waiters{};
{
- [[maybe_unused]] const KScopedSchedulerLock sl(kernel);
+ KScopedSchedulerLock sl(m_kernel);
- auto it = thread_tree.nfind_key({addr, -1});
+ auto it = m_tree.nfind_key({addr, -1});
// Determine the updated value.
s32 new_value{};
if (count <= 0) {
- if (it != thread_tree.end() && it->GetAddressArbiterKey() == addr) {
+ if (it != m_tree.end() && it->GetAddressArbiterKey() == addr) {
new_value = value - 2;
} else {
new_value = value + 1;
}
} else {
- if (it != thread_tree.end() && it->GetAddressArbiterKey() == addr) {
+ if (it != m_tree.end() && it->GetAddressArbiterKey() == addr) {
auto tmp_it = it;
s32 tmp_num_waiters{};
- while (++tmp_it != thread_tree.end() && tmp_it->GetAddressArbiterKey() == addr) {
+ while (++tmp_it != m_tree.end() && tmp_it->GetAddressArbiterKey() == addr) {
if (tmp_num_waiters++ >= count) {
break;
}
@@ -201,20 +219,15 @@ Result KAddressArbiter::SignalAndModifyByWaitingCountIfEqual(VAddr addr, s32 val
s32 user_value{};
bool succeeded{};
if (value != new_value) {
- succeeded = UpdateIfEqual(system, &user_value, addr, value, new_value);
+ succeeded = UpdateIfEqual(m_system, std::addressof(user_value), addr, value, new_value);
} else {
- succeeded = ReadFromUser(system, &user_value, addr);
+ succeeded = ReadFromUser(m_kernel, std::addressof(user_value), addr);
}
- if (!succeeded) {
- LOG_ERROR(Kernel, "Invalid current memory!");
- return ResultInvalidCurrentMemory;
- }
- if (user_value != value) {
- return ResultInvalidState;
- }
+ R_UNLESS(succeeded, ResultInvalidCurrentMemory);
+ R_UNLESS(user_value == value, ResultInvalidState);
- while ((it != thread_tree.end()) && (count <= 0 || num_waiters < count) &&
+ while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) &&
(it->GetAddressArbiterKey() == addr)) {
// End the thread's wait.
KThread* target_thread = std::addressof(*it);
@@ -223,58 +236,60 @@ Result KAddressArbiter::SignalAndModifyByWaitingCountIfEqual(VAddr addr, s32 val
ASSERT(target_thread->IsWaitingForAddressArbiter());
target_thread->ClearAddressArbiter();
- it = thread_tree.erase(it);
+ it = m_tree.erase(it);
++num_waiters;
}
}
- return ResultSuccess;
+ R_SUCCEED();
}
-Result KAddressArbiter::WaitIfLessThan(VAddr addr, s32 value, bool decrement, s64 timeout) {
+Result KAddressArbiter::WaitIfLessThan(uint64_t addr, s32 value, bool decrement, s64 timeout) {
// Prepare to wait.
- KThread* cur_thread = GetCurrentThreadPointer(kernel);
- ThreadQueueImplForKAddressArbiter wait_queue(kernel, std::addressof(thread_tree));
+ KThread* cur_thread = GetCurrentThreadPointer(m_kernel);
+ KHardwareTimer* timer{};
+ ThreadQueueImplForKAddressArbiter wait_queue(m_kernel, std::addressof(m_tree));
{
- KScopedSchedulerLockAndSleep slp{kernel, cur_thread, timeout};
+ KScopedSchedulerLockAndSleep slp{m_kernel, std::addressof(timer), cur_thread, timeout};
// Check that the thread isn't terminating.
if (cur_thread->IsTerminationRequested()) {
slp.CancelSleep();
- return ResultTerminationRequested;
+ R_THROW(ResultTerminationRequested);
}
// Read the value from userspace.
s32 user_value{};
bool succeeded{};
if (decrement) {
- succeeded = DecrementIfLessThan(system, &user_value, addr, value);
+ succeeded = DecrementIfLessThan(m_system, std::addressof(user_value), addr, value);
} else {
- succeeded = ReadFromUser(system, &user_value, addr);
+ succeeded = ReadFromUser(m_kernel, std::addressof(user_value), addr);
}
if (!succeeded) {
slp.CancelSleep();
- return ResultInvalidCurrentMemory;
+ R_THROW(ResultInvalidCurrentMemory);
}
// Check that the value is less than the specified one.
if (user_value >= value) {
slp.CancelSleep();
- return ResultInvalidState;
+ R_THROW(ResultInvalidState);
}
// Check that the timeout is non-zero.
if (timeout == 0) {
slp.CancelSleep();
- return ResultTimedOut;
+ R_THROW(ResultTimedOut);
}
// Set the arbiter.
- cur_thread->SetAddressArbiter(&thread_tree, addr);
- thread_tree.insert(*cur_thread);
+ cur_thread->SetAddressArbiter(std::addressof(m_tree), addr);
+ m_tree.insert(*cur_thread);
// Wait for the thread to finish.
+ wait_queue.SetHardwareTimer(timer);
cur_thread->BeginWait(std::addressof(wait_queue));
cur_thread->SetWaitReasonForDebugging(ThreadWaitReasonForDebugging::Arbitration);
}
@@ -283,44 +298,46 @@ Result KAddressArbiter::WaitIfLessThan(VAddr addr, s32 value, bool decrement, s6
return cur_thread->GetWaitResult();
}
-Result KAddressArbiter::WaitIfEqual(VAddr addr, s32 value, s64 timeout) {
+Result KAddressArbiter::WaitIfEqual(uint64_t addr, s32 value, s64 timeout) {
// Prepare to wait.
- KThread* cur_thread = GetCurrentThreadPointer(kernel);
- ThreadQueueImplForKAddressArbiter wait_queue(kernel, std::addressof(thread_tree));
+ KThread* cur_thread = GetCurrentThreadPointer(m_kernel);
+ KHardwareTimer* timer{};
+ ThreadQueueImplForKAddressArbiter wait_queue(m_kernel, std::addressof(m_tree));
{
- KScopedSchedulerLockAndSleep slp{kernel, cur_thread, timeout};
+ KScopedSchedulerLockAndSleep slp{m_kernel, std::addressof(timer), cur_thread, timeout};
// Check that the thread isn't terminating.
if (cur_thread->IsTerminationRequested()) {
slp.CancelSleep();
- return ResultTerminationRequested;
+ R_THROW(ResultTerminationRequested);
}
// Read the value from userspace.
s32 user_value{};
- if (!ReadFromUser(system, &user_value, addr)) {
+ if (!ReadFromUser(m_kernel, std::addressof(user_value), addr)) {
slp.CancelSleep();
- return ResultInvalidCurrentMemory;
+ R_THROW(ResultInvalidCurrentMemory);
}
// Check that the value is equal.
if (value != user_value) {
slp.CancelSleep();
- return ResultInvalidState;
+ R_THROW(ResultInvalidState);
}
// Check that the timeout is non-zero.
if (timeout == 0) {
slp.CancelSleep();
- return ResultTimedOut;
+ R_THROW(ResultTimedOut);
}
// Set the arbiter.
- cur_thread->SetAddressArbiter(&thread_tree, addr);
- thread_tree.insert(*cur_thread);
+ cur_thread->SetAddressArbiter(std::addressof(m_tree), addr);
+ m_tree.insert(*cur_thread);
// Wait for the thread to finish.
+ wait_queue.SetHardwareTimer(timer);
cur_thread->BeginWait(std::addressof(wait_queue));
cur_thread->SetWaitReasonForDebugging(ThreadWaitReasonForDebugging::Arbitration);
}