2020-07-15 18:15:49 +02:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-07-15 18:15:49 +02: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 {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool ReadFromUser(s32 *out, KProcessAddress address) {
|
|
|
|
return UserspaceAccess::CopyMemoryFromUserSize32Bit(out, GetVoidPointer(address));
|
|
|
|
}
|
|
|
|
|
2020-07-17 04:06:48 +02:00
|
|
|
ALWAYS_INLINE bool DecrementIfLessThan(s32 *out, KProcessAddress address, s32 value) {
|
2023-02-21 18:58:02 +01:00
|
|
|
/* NOTE: If scheduler lock is not held here, interrupt disable is required. */
|
|
|
|
/* KScopedInterruptDisable di; */
|
|
|
|
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
|
2020-07-17 04:06:48 +02:00
|
|
|
|
|
|
|
if (!cpu::CanAccessAtomic(address)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UserspaceAccess::DecrementIfLessThanAtomic(out, GetPointer<s32>(address), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool UpdateIfEqual(s32 *out, KProcessAddress address, s32 value, s32 new_value) {
|
2023-02-21 18:58:02 +01:00
|
|
|
/* NOTE: If scheduler lock is not held here, interrupt disable is required. */
|
|
|
|
/* KScopedInterruptDisable di; */
|
|
|
|
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
|
2020-07-17 04:06:48 +02:00
|
|
|
|
|
|
|
if (!cpu::CanAccessAtomic(address)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return UserspaceAccess::UpdateIfEqualAtomic(out, GetPointer<s32>(address), value, new_value);
|
|
|
|
}
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
class ThreadQueueImplForKAddressArbiter final : public KThreadQueue {
|
|
|
|
private:
|
|
|
|
KAddressArbiter::ThreadTree *m_tree;
|
|
|
|
public:
|
|
|
|
constexpr ThreadQueueImplForKAddressArbiter(KAddressArbiter::ThreadTree *t) : KThreadQueue(), m_tree(t) { /* ... */ }
|
|
|
|
|
|
|
|
virtual 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. */
|
|
|
|
if (waiting_thread->IsWaitingForAddressArbiter()) {
|
|
|
|
m_tree->erase(m_tree->iterator_to(*waiting_thread));
|
|
|
|
waiting_thread->ClearAddressArbiter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invoke the base cancel wait handler. */
|
|
|
|
KThreadQueue::CancelWait(waiting_thread, wait_result, cancel_timer_task);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-15 18:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KAddressArbiter::Signal(uintptr_t addr, s32 count) {
|
2020-07-17 04:06:48 +02:00
|
|
|
/* Perform signaling. */
|
|
|
|
s32 num_waiters = 0;
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
2021-04-21 01:56:33 +02:00
|
|
|
auto it = m_tree.nfind_key({ addr, -1 });
|
2020-12-18 02:18:47 +01:00
|
|
|
while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) && (it->GetAddressArbiterKey() == addr)) {
|
2021-09-19 19:11:56 +02:00
|
|
|
/* End the thread's wait. */
|
2020-07-17 04:06:48 +02:00
|
|
|
KThread *target_thread = std::addressof(*it);
|
2021-09-19 19:11:56 +02:00
|
|
|
target_thread->EndWait(ResultSuccess());
|
2020-07-17 04:06:48 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
MESOSPHERE_ASSERT(target_thread->IsWaitingForAddressArbiter());
|
|
|
|
target_thread->ClearAddressArbiter();
|
2020-07-17 04:06:48 +02:00
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
it = m_tree.erase(it);
|
2020-07-17 04:06:48 +02:00
|
|
|
++num_waiters;
|
|
|
|
}
|
|
|
|
}
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-15 18:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KAddressArbiter::SignalAndIncrementIfEqual(uintptr_t addr, s32 value, s32 count) {
|
2020-07-17 04:06:48 +02:00
|
|
|
/* Perform signaling. */
|
|
|
|
s32 num_waiters = 0;
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Check the userspace value. */
|
|
|
|
s32 user_value;
|
|
|
|
R_UNLESS(UpdateIfEqual(std::addressof(user_value), addr, value, value + 1), svc::ResultInvalidCurrentMemory());
|
|
|
|
R_UNLESS(user_value == value, svc::ResultInvalidState());
|
|
|
|
|
2021-04-21 01:56:33 +02:00
|
|
|
auto it = m_tree.nfind_key({ addr, -1 });
|
2020-12-18 02:18:47 +01:00
|
|
|
while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) && (it->GetAddressArbiterKey() == addr)) {
|
2021-09-19 19:11:56 +02:00
|
|
|
/* End the thread's wait. */
|
2020-07-17 04:06:48 +02:00
|
|
|
KThread *target_thread = std::addressof(*it);
|
2021-09-19 19:11:56 +02:00
|
|
|
target_thread->EndWait(ResultSuccess());
|
2020-07-17 04:06:48 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
MESOSPHERE_ASSERT(target_thread->IsWaitingForAddressArbiter());
|
|
|
|
target_thread->ClearAddressArbiter();
|
2020-07-17 04:06:48 +02:00
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
it = m_tree.erase(it);
|
2020-07-17 04:06:48 +02:00
|
|
|
++num_waiters;
|
|
|
|
}
|
|
|
|
}
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-15 18:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KAddressArbiter::SignalAndModifyByWaitingCountIfEqual(uintptr_t addr, s32 value, s32 count) {
|
2020-07-17 04:06:48 +02:00
|
|
|
/* Perform signaling. */
|
|
|
|
s32 num_waiters = 0;
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
2021-04-21 01:56:33 +02:00
|
|
|
auto it = m_tree.nfind_key({ addr, -1 });
|
2020-07-17 04:06:48 +02:00
|
|
|
/* Determine the updated value. */
|
|
|
|
s32 new_value;
|
2021-07-23 00:56:43 +02:00
|
|
|
if (count <= 0) {
|
|
|
|
if ((it != m_tree.end()) && (it->GetAddressArbiterKey() == addr)) {
|
2022-06-08 08:40:32 +02:00
|
|
|
new_value = value - 1;
|
2020-07-17 04:06:48 +02:00
|
|
|
} else {
|
2021-07-23 00:56:43 +02:00
|
|
|
new_value = value + 1;
|
2020-07-17 04:06:48 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-23 00:56:43 +02:00
|
|
|
if ((it != m_tree.end()) && (it->GetAddressArbiterKey() == addr)) {
|
2020-10-12 10:05:30 +02:00
|
|
|
auto tmp_it = it;
|
|
|
|
s32 tmp_num_waiters = 0;
|
2021-07-23 00:56:43 +02:00
|
|
|
while ((++tmp_it != m_tree.end()) && (tmp_it->GetAddressArbiterKey() == addr)) {
|
2022-06-08 08:49:31 +02:00
|
|
|
if ((++tmp_num_waiters) >= count) {
|
2021-07-23 00:56:43 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-10-12 10:05:30 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 00:56:43 +02:00
|
|
|
if (tmp_num_waiters < count) {
|
2020-10-12 10:05:30 +02:00
|
|
|
new_value = value - 1;
|
|
|
|
} else {
|
|
|
|
new_value = value;
|
|
|
|
}
|
2021-07-23 00:56:43 +02:00
|
|
|
} else {
|
|
|
|
new_value = value + 1;
|
2020-07-17 04:06:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the userspace value. */
|
|
|
|
s32 user_value;
|
|
|
|
bool succeeded;
|
|
|
|
if (value != new_value) {
|
|
|
|
succeeded = UpdateIfEqual(std::addressof(user_value), addr, value, new_value);
|
|
|
|
} else {
|
|
|
|
succeeded = ReadFromUser(std::addressof(user_value), addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_UNLESS(succeeded, svc::ResultInvalidCurrentMemory());
|
|
|
|
R_UNLESS(user_value == value, svc::ResultInvalidState());
|
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) && (it->GetAddressArbiterKey() == addr)) {
|
2021-09-19 19:11:56 +02:00
|
|
|
/* End the thread's wait. */
|
2020-07-17 04:06:48 +02:00
|
|
|
KThread *target_thread = std::addressof(*it);
|
2021-09-19 19:11:56 +02:00
|
|
|
target_thread->EndWait(ResultSuccess());
|
2020-07-17 04:06:48 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
MESOSPHERE_ASSERT(target_thread->IsWaitingForAddressArbiter());
|
|
|
|
target_thread->ClearAddressArbiter();
|
2020-07-17 04:06:48 +02:00
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
it = m_tree.erase(it);
|
2020-07-17 04:06:48 +02:00
|
|
|
++num_waiters;
|
|
|
|
}
|
|
|
|
}
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-15 18:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KAddressArbiter::WaitIfLessThan(uintptr_t addr, s32 value, bool decrement, s64 timeout) {
|
2020-07-17 04:06:48 +02:00
|
|
|
/* Prepare to wait. */
|
|
|
|
KThread *cur_thread = GetCurrentThreadPointer();
|
|
|
|
KHardwareTimer *timer;
|
2021-09-19 19:11:56 +02:00
|
|
|
ThreadQueueImplForKAddressArbiter wait_queue(std::addressof(m_tree));
|
2020-07-17 04:06:48 +02: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 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultTerminationRequested());
|
2020-07-17 04:06:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the value from userspace. */
|
|
|
|
s32 user_value;
|
|
|
|
bool succeeded;
|
|
|
|
if (decrement) {
|
|
|
|
succeeded = DecrementIfLessThan(std::addressof(user_value), addr, value);
|
|
|
|
} else {
|
|
|
|
succeeded = ReadFromUser(std::addressof(user_value), addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!succeeded) {
|
|
|
|
slp.CancelSleep();
|
2022-02-14 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultInvalidCurrentMemory());
|
2020-07-17 04:06:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the value is less than the specified one. */
|
|
|
|
if (user_value >= value) {
|
|
|
|
slp.CancelSleep();
|
2022-02-14 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultInvalidState());
|
2020-07-17 04:06:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the timeout is non-zero. */
|
|
|
|
if (timeout == 0) {
|
|
|
|
slp.CancelSleep();
|
2022-02-14 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultTimedOut());
|
2020-07-17 04:06:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the arbiter. */
|
2020-12-18 02:18:47 +01:00
|
|
|
cur_thread->SetAddressArbiter(std::addressof(m_tree), addr);
|
|
|
|
m_tree.insert(*cur_thread);
|
2020-07-17 04:06:48 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Wait for the thread to finish. */
|
|
|
|
wait_queue.SetHardwareTimer(timer);
|
|
|
|
cur_thread->BeginWait(std::addressof(wait_queue));
|
2020-07-17 04:06:48 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Get the wait result. */
|
2022-02-14 23:45:32 +01:00
|
|
|
R_RETURN(cur_thread->GetWaitResult());
|
2020-07-15 18:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KAddressArbiter::WaitIfEqual(uintptr_t addr, s32 value, s64 timeout) {
|
|
|
|
/* Prepare to wait. */
|
|
|
|
KThread *cur_thread = GetCurrentThreadPointer();
|
|
|
|
KHardwareTimer *timer;
|
2021-09-19 19:11:56 +02:00
|
|
|
ThreadQueueImplForKAddressArbiter wait_queue(std::addressof(m_tree));
|
2020-07-15 18:15:49 +02: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 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultTerminationRequested());
|
2020-07-15 18:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the value from userspace. */
|
|
|
|
s32 user_value;
|
|
|
|
if (!ReadFromUser(std::addressof(user_value), addr)) {
|
|
|
|
slp.CancelSleep();
|
2022-02-14 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultInvalidCurrentMemory());
|
2020-07-15 18:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the value is equal. */
|
|
|
|
if (value != user_value) {
|
|
|
|
slp.CancelSleep();
|
2022-02-14 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultInvalidState());
|
2020-07-15 18:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the timeout is non-zero. */
|
|
|
|
if (timeout == 0) {
|
|
|
|
slp.CancelSleep();
|
2022-02-14 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultTimedOut());
|
2020-07-15 18:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the arbiter. */
|
2020-12-18 02:18:47 +01:00
|
|
|
cur_thread->SetAddressArbiter(std::addressof(m_tree), addr);
|
|
|
|
m_tree.insert(*cur_thread);
|
2020-07-15 18:15:49 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Wait for the thread to finish. */
|
|
|
|
wait_queue.SetHardwareTimer(timer);
|
|
|
|
cur_thread->BeginWait(std::addressof(wait_queue));
|
2020-07-15 18:15:49 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Get the wait result. */
|
2022-02-14 23:45:32 +01:00
|
|
|
R_RETURN(cur_thread->GetWaitResult());
|
2020-07-15 18:15:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|