2020-07-10 02:17:30 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-07-10 02:17:30 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
2020-07-17 02:06:48 +00:00
|
|
|
namespace {
|
2020-07-10 02:17:30 +00:00
|
|
|
|
2020-07-10 03:45:26 +00:00
|
|
|
ALWAYS_INLINE bool ReadFromUser(u32 *out, KProcessAddress address) {
|
|
|
|
return UserspaceAccess::CopyMemoryFromUserSize32Bit(out, GetVoidPointer(address));
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool WriteToUser(KProcessAddress address, const u32 *p) {
|
|
|
|
return UserspaceAccess::CopyMemoryToUserSize32Bit(GetVoidPointer(address), p);
|
|
|
|
}
|
|
|
|
|
2020-07-22 10:29:27 +00:00
|
|
|
ALWAYS_INLINE bool UpdateLockAtomic(u32 *out, KProcessAddress address, u32 if_zero, u32 new_orr_mask) {
|
|
|
|
return UserspaceAccess::UpdateLockAtomic(out, GetPointer<u32>(address), if_zero, new_orr_mask);
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
class ThreadQueueImplForKConditionVariableWaitForAddress final : public KThreadQueue {
|
|
|
|
public:
|
|
|
|
constexpr ThreadQueueImplForKConditionVariableWaitForAddress() : KThreadQueue() { /* ... */ }
|
|
|
|
|
|
|
|
virtual void CancelWait(KThread *waiting_thread, Result wait_result, bool cancel_timer_task) override {
|
|
|
|
/* Remove the thread as a waiter from its owner. */
|
|
|
|
waiting_thread->GetLockOwner()->RemoveWaiter(waiting_thread);
|
|
|
|
|
|
|
|
/* Invoke the base cancel wait handler. */
|
|
|
|
KThreadQueue::CancelWait(waiting_thread, wait_result, cancel_timer_task);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadQueueImplForKConditionVariableWaitConditionVariable final : public KThreadQueue {
|
|
|
|
private:
|
|
|
|
KConditionVariable::ThreadTree *m_tree;
|
|
|
|
public:
|
|
|
|
constexpr ThreadQueueImplForKConditionVariableWaitConditionVariable(KConditionVariable::ThreadTree *t) : KThreadQueue(), m_tree(t) { /* ... */ }
|
|
|
|
|
|
|
|
virtual void CancelWait(KThread *waiting_thread, Result wait_result, bool cancel_timer_task) override {
|
|
|
|
/* Remove the thread as a waiter from its owner. */
|
|
|
|
if (KThread *owner = waiting_thread->GetLockOwner(); owner != nullptr) {
|
|
|
|
owner->RemoveWaiter(waiting_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the thread is waiting on a condvar, remove it from the tree. */
|
|
|
|
if (waiting_thread->IsWaitingForConditionVariable()) {
|
|
|
|
m_tree->erase(m_tree->iterator_to(*waiting_thread));
|
|
|
|
waiting_thread->ClearConditionVariable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invoke the base cancel wait handler. */
|
|
|
|
KThreadQueue::CancelWait(waiting_thread, wait_result, cancel_timer_task);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-10 02:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KConditionVariable::SignalToAddress(KProcessAddress addr) {
|
2021-09-19 17:11:56 +00:00
|
|
|
KThread *owner_thread = GetCurrentThreadPointer();
|
2020-07-10 03:45:26 +00:00
|
|
|
|
|
|
|
/* Signal the address. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Remove waiter thread. */
|
2023-02-21 20:15:01 +00:00
|
|
|
bool has_waiters;
|
|
|
|
KThread * const next_owner_thread = owner_thread->RemoveWaiterByKey(std::addressof(has_waiters), addr);
|
2020-07-10 03:45:26 +00:00
|
|
|
|
|
|
|
/* Determine the next tag. */
|
|
|
|
u32 next_value = 0;
|
2021-09-19 17:11:56 +00:00
|
|
|
if (next_owner_thread != nullptr) {
|
2020-07-10 03:45:26 +00:00
|
|
|
next_value = next_owner_thread->GetAddressKeyValue();
|
2023-02-21 20:15:01 +00:00
|
|
|
if (has_waiters) {
|
2020-07-10 03:45:26 +00:00
|
|
|
next_value |= ams::svc::HandleWaitMask;
|
|
|
|
}
|
2023-02-21 17:42:24 +00:00
|
|
|
}
|
2020-07-10 03:45:26 +00:00
|
|
|
|
2023-02-21 17:42:24 +00:00
|
|
|
/* Synchronize memory before proceeding. */
|
|
|
|
cpu::DataMemoryBarrierInnerShareable();
|
2020-07-10 03:45:26 +00:00
|
|
|
|
2023-02-21 17:42:24 +00:00
|
|
|
/* Write the value to userspace. */
|
|
|
|
Result result;
|
|
|
|
if (AMS_LIKELY(WriteToUser(addr, std::addressof(next_value)))) {
|
|
|
|
result = ResultSuccess();
|
2021-09-19 17:11:56 +00:00
|
|
|
} else {
|
2023-02-21 17:42:24 +00:00
|
|
|
result = svc::ResultInvalidCurrentMemory();
|
|
|
|
}
|
2021-09-19 17:11:56 +00:00
|
|
|
|
2023-02-21 17:42:24 +00:00
|
|
|
/* If necessary, signal the next owner thread. */
|
|
|
|
if (next_owner_thread != nullptr) {
|
|
|
|
next_owner_thread->EndWait(result);
|
2020-07-10 03:45:26 +00:00
|
|
|
}
|
2023-02-21 17:42:24 +00:00
|
|
|
|
|
|
|
R_RETURN(result);
|
2020-07-10 03:45:26 +00:00
|
|
|
}
|
2020-07-10 02:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KConditionVariable::WaitForAddress(ams::svc::Handle handle, KProcessAddress addr, u32 value) {
|
2021-09-19 17:11:56 +00:00
|
|
|
KThread *cur_thread = GetCurrentThreadPointer();
|
|
|
|
ThreadQueueImplForKConditionVariableWaitForAddress wait_queue;
|
2020-07-10 03:45:26 +00:00
|
|
|
|
|
|
|
/* Wait for the address. */
|
2021-10-19 08:19:31 +00:00
|
|
|
KThread *owner_thread;
|
2020-07-10 03:45:26 +00:00
|
|
|
{
|
2021-09-19 17:11:56 +00:00
|
|
|
KScopedSchedulerLock sl;
|
2020-07-10 03:45:26 +00:00
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
/* Check if the thread should terminate. */
|
|
|
|
R_UNLESS(!cur_thread->IsTerminationRequested(), svc::ResultTerminationRequested());
|
2020-07-10 03:45:26 +00:00
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
/* Read the tag from userspace. */
|
|
|
|
u32 test_tag;
|
|
|
|
R_UNLESS(ReadFromUser(std::addressof(test_tag), addr), svc::ResultInvalidCurrentMemory());
|
2020-07-10 03:45:26 +00:00
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
/* If the tag isn't the handle (with wait mask), we're done. */
|
|
|
|
R_SUCCEED_IF(test_tag != (handle | ams::svc::HandleWaitMask));
|
2020-07-10 03:45:26 +00:00
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
/* Get the lock owner thread. */
|
2021-10-19 08:19:31 +00:00
|
|
|
owner_thread = GetCurrentProcess().GetHandleTable().GetObjectWithoutPseudoHandle<KThread>(handle).ReleasePointerUnsafe();
|
|
|
|
R_UNLESS(owner_thread != nullptr, svc::ResultInvalidHandle());
|
2020-07-10 03:45:26 +00:00
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
/* Update the lock. */
|
|
|
|
cur_thread->SetAddressKey(addr, value);
|
|
|
|
owner_thread->AddWaiter(cur_thread);
|
2020-07-10 03:45:26 +00:00
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
/* Begin waiting. */
|
|
|
|
cur_thread->BeginWait(std::addressof(wait_queue));
|
2020-07-10 03:45:26 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 08:19:31 +00:00
|
|
|
/* Close our reference to the owner thread, now that the wait is over. */
|
|
|
|
owner_thread->Close();
|
|
|
|
|
2020-07-10 03:45:26 +00:00
|
|
|
/* Get the wait result. */
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(cur_thread->GetWaitResult());
|
2020-07-10 02:17:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:33:33 +00:00
|
|
|
void KConditionVariable::SignalImpl(KThread *thread) {
|
2020-07-22 10:29:27 +00:00
|
|
|
/* Check pre-conditions. */
|
|
|
|
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
|
|
|
|
|
|
|
|
/* Update the tag. */
|
|
|
|
KProcessAddress address = thread->GetAddressKey();
|
|
|
|
u32 own_tag = thread->GetAddressKeyValue();
|
|
|
|
|
|
|
|
u32 prev_tag;
|
|
|
|
bool can_access;
|
|
|
|
{
|
2023-02-21 17:58:02 +00:00
|
|
|
/* NOTE: If scheduler lock is not held here, interrupt disable is required. */
|
|
|
|
/* KScopedInterruptDisable di; */
|
2020-07-22 10:29:27 +00:00
|
|
|
|
|
|
|
can_access = cpu::CanAccessAtomic(address);
|
|
|
|
if (AMS_LIKELY(can_access)) {
|
2021-07-13 08:40:16 +00:00
|
|
|
can_access = UpdateLockAtomic(std::addressof(prev_tag), address, own_tag, ams::svc::HandleWaitMask);
|
2020-07-22 10:29:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AMS_LIKELY(can_access)) {
|
|
|
|
if (prev_tag == ams::svc::InvalidHandle) {
|
|
|
|
/* If nobody held the lock previously, we're all good. */
|
2021-09-19 17:11:56 +00:00
|
|
|
thread->EndWait(ResultSuccess());
|
2020-07-22 10:29:27 +00:00
|
|
|
} else {
|
|
|
|
/* Get the previous owner. */
|
|
|
|
KThread *owner_thread = GetCurrentProcess().GetHandleTable().GetObjectWithoutPseudoHandle<KThread>(static_cast<ams::svc::Handle>(prev_tag & ~ams::svc::HandleWaitMask))
|
|
|
|
.ReleasePointerUnsafe();
|
|
|
|
if (AMS_LIKELY(owner_thread != nullptr)) {
|
|
|
|
/* Add the thread as a waiter on the owner. */
|
|
|
|
owner_thread->AddWaiter(thread);
|
2021-04-07 06:33:33 +00:00
|
|
|
owner_thread->Close();
|
2020-07-22 10:29:27 +00:00
|
|
|
} else {
|
|
|
|
/* The lock was tagged with a thread that doesn't exist. */
|
2021-09-19 17:11:56 +00:00
|
|
|
thread->EndWait(svc::ResultInvalidState());
|
2020-07-22 10:29:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* If the address wasn't accessible, note so. */
|
2021-09-19 17:11:56 +00:00
|
|
|
thread->EndWait(svc::ResultInvalidCurrentMemory());
|
2020-07-22 10:29:27 +00:00
|
|
|
}
|
2020-07-10 02:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KConditionVariable::Signal(uintptr_t cv_key, s32 count) {
|
|
|
|
/* Perform signaling. */
|
|
|
|
int num_waiters = 0;
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
2021-04-20 23:56:33 +00:00
|
|
|
auto it = m_tree.nfind_key({ cv_key, -1 });
|
2020-12-18 01:18:47 +00:00
|
|
|
while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) && (it->GetConditionVariableKey() == cv_key)) {
|
2020-07-10 02:17:30 +00:00
|
|
|
KThread *target_thread = std::addressof(*it);
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
it = m_tree.erase(it);
|
2020-07-19 03:03:27 +00:00
|
|
|
target_thread->ClearConditionVariable();
|
2023-02-21 20:15:01 +00:00
|
|
|
|
|
|
|
this->SignalImpl(target_thread);
|
|
|
|
|
2020-07-10 02:17:30 +00:00
|
|
|
++num_waiters;
|
|
|
|
}
|
2020-12-02 09:28:21 +00:00
|
|
|
|
|
|
|
/* If we have no waiters, clear the has waiter flag. */
|
2020-12-18 01:18:47 +00:00
|
|
|
if (it == m_tree.end() || it->GetConditionVariableKey() != cv_key) {
|
2020-12-02 09:28:21 +00:00
|
|
|
const u32 has_waiter_flag = 0;
|
|
|
|
WriteToUser(cv_key, std::addressof(has_waiter_flag));
|
|
|
|
}
|
2020-07-10 02:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result KConditionVariable::Wait(KProcessAddress addr, uintptr_t key, u32 value, s64 timeout) {
|
2020-07-19 03:03:27 +00:00
|
|
|
/* Prepare to wait. */
|
|
|
|
KThread *cur_thread = GetCurrentThreadPointer();
|
|
|
|
KHardwareTimer *timer;
|
2021-09-19 17:11:56 +00:00
|
|
|
ThreadQueueImplForKConditionVariableWaitConditionVariable wait_queue(std::addressof(m_tree));
|
2020-07-19 03:03:27 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
KScopedSchedulerLockAndSleep slp(std::addressof(timer), cur_thread, timeout);
|
|
|
|
|
|
|
|
/* Check that the thread isn't terminating. */
|
|
|
|
if (cur_thread->IsTerminationRequested()) {
|
|
|
|
slp.CancelSleep();
|
2022-02-14 22:45:32 +00:00
|
|
|
R_THROW(svc::ResultTerminationRequested());
|
2020-07-19 03:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the value and process for the next owner. */
|
|
|
|
{
|
|
|
|
/* Remove waiter thread. */
|
2023-02-21 20:15:01 +00:00
|
|
|
bool has_waiters;
|
|
|
|
KThread *next_owner_thread = cur_thread->RemoveWaiterByKey(std::addressof(has_waiters), GetInteger(addr));
|
2020-07-19 03:03:27 +00:00
|
|
|
|
|
|
|
/* Update for the next owner thread. */
|
|
|
|
u32 next_value = 0;
|
|
|
|
if (next_owner_thread != nullptr) {
|
|
|
|
/* Get the next tag value. */
|
|
|
|
next_value = next_owner_thread->GetAddressKeyValue();
|
2023-02-21 20:15:01 +00:00
|
|
|
if (has_waiters) {
|
2020-07-19 03:03:27 +00:00
|
|
|
next_value |= ams::svc::HandleWaitMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wake up the next owner. */
|
2021-09-19 17:11:56 +00:00
|
|
|
next_owner_thread->EndWait(ResultSuccess());
|
2020-07-19 03:03:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 09:28:21 +00:00
|
|
|
/* Write to the cv key. */
|
|
|
|
{
|
|
|
|
const u32 has_waiter_flag = 1;
|
|
|
|
WriteToUser(key, std::addressof(has_waiter_flag));
|
2021-10-26 00:38:50 +00:00
|
|
|
cpu::DataMemoryBarrierInnerShareable();
|
2020-12-02 09:28:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 03:03:27 +00:00
|
|
|
/* Write the value to userspace. */
|
|
|
|
if (!WriteToUser(addr, std::addressof(next_value))) {
|
|
|
|
slp.CancelSleep();
|
2022-02-14 22:45:32 +00:00
|
|
|
R_THROW(svc::ResultInvalidCurrentMemory());
|
2020-07-19 03:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
/* If timeout is zero, time out. */
|
|
|
|
R_UNLESS(timeout != 0, svc::ResultTimedOut());
|
2020-07-19 03:03:27 +00:00
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
/* Update condition variable tracking. */
|
|
|
|
cur_thread->SetConditionVariable(std::addressof(m_tree), addr, key, value);
|
|
|
|
m_tree.insert(*cur_thread);
|
2020-07-19 03:03:27 +00:00
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
/* Begin waiting. */
|
|
|
|
wait_queue.SetHardwareTimer(timer);
|
|
|
|
cur_thread->BeginWait(std::addressof(wait_queue));
|
2021-04-07 17:19:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
/* Get the wait result. */
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(cur_thread->GetWaitResult());
|
2020-07-10 02:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|