kern: add KAddressArbiter::WaitIfEqual

This commit is contained in:
Michael Scire 2020-07-15 09:15:49 -07:00 committed by SciresM
parent 01a7606f95
commit a0cc22302c
8 changed files with 275 additions and 44 deletions

View file

@ -0,0 +1,108 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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 {
constinit KThread g_arbiter_compare_thread;
ALWAYS_INLINE bool ReadFromUser(s32 *out, KProcessAddress address) {
return UserspaceAccess::CopyMemoryFromUserSize32Bit(out, GetVoidPointer(address));
}
}
Result KAddressArbiter::Signal(uintptr_t addr, s32 count) {
MESOSPHERE_UNIMPLEMENTED();
}
Result KAddressArbiter::SignalAndIncrementIfEqual(uintptr_t addr, s32 value, s32 count) {
MESOSPHERE_UNIMPLEMENTED();
}
Result KAddressArbiter::SignalAndModifyByWaitingCountIfEqual(uintptr_t addr, s32 value, s32 count) {
MESOSPHERE_UNIMPLEMENTED();
}
Result KAddressArbiter::WaitIfLessThan(uintptr_t addr, s32 value, bool decrement, s64 timeout) {
MESOSPHERE_UNIMPLEMENTED();
}
Result KAddressArbiter::WaitIfEqual(uintptr_t addr, s32 value, s64 timeout) {
/* Prepare to wait. */
KThread *cur_thread = GetCurrentThreadPointer();
KHardwareTimer *timer;
{
KScopedSchedulerLockAndSleep slp(std::addressof(timer), cur_thread, timeout);
/* Check that the thread isn't terminating. */
if (cur_thread->IsTerminationRequested()) {
slp.CancelSleep();
return svc::ResultTerminationRequested();
}
/* Set the synced object. */
cur_thread->SetSyncedObject(nullptr, ams::svc::ResultTimedOut());
/* Read the value from userspace. */
s32 user_value;
if (!ReadFromUser(std::addressof(user_value), addr)) {
slp.CancelSleep();
return svc::ResultInvalidCurrentMemory();
}
/* Check that the value is equal. */
if (value != user_value) {
slp.CancelSleep();
return svc::ResultInvalidState();
}
/* Check that the timeout is non-zero. */
if (timeout == 0) {
slp.CancelSleep();
return svc::ResultTimedOut();
}
/* Set the arbiter. */
cur_thread->SetAddressArbiter(std::addressof(this->tree), addr);
this->tree.insert(*cur_thread);
cur_thread->SetState(KThread::ThreadState_Waiting);
}
/* Cancel the timer wait. */
if (timer != nullptr) {
timer->CancelTask(cur_thread);
}
/* Remove from the address arbiter. */
{
KScopedSchedulerLock sl;
if (cur_thread->IsWaitingForAddressArbiter()) {
this->tree.erase(this->tree.iterator_to(*cur_thread));
cur_thread->ClearAddressArbiter();
}
}
/* Get the result. */
KSynchronizationObject *dummy;
return cur_thread->GetWaitResult(std::addressof(dummy));
}
}

View file

@ -146,7 +146,7 @@ namespace ams::kern {
}
it = this->tree.erase(it);
target_thread->ClearConditionVariable();
target_thread->ClearConditionVariableTree();
++num_waiters;
}
}

View file

@ -94,7 +94,7 @@ namespace ams::kern {
/* Set parent and condvar tree. */
this->parent = nullptr;
this->cond_var = nullptr;
this->condvar_tree = nullptr;
/* Set sync booleans. */
this->signaled = false;
@ -519,8 +519,8 @@ namespace ams::kern {
}
/* Ensure we don't violate condition variable red black tree invariants. */
if (auto *cond_var = thread->GetConditionVariable(); cond_var != nullptr) {
cond_var->BeforeUpdatePriority(thread);
if (auto *cv_tree = thread->GetConditionVariableTree(); cv_tree != nullptr) {
BeforeUpdatePriority(cv_tree, thread);
}
/* Change the priority. */
@ -528,8 +528,8 @@ namespace ams::kern {
thread->SetPriority(new_priority);
/* Restore the condition variable, if relevant. */
if (auto *cond_var = thread->GetConditionVariable(); cond_var != nullptr) {
cond_var->AfterUpdatePriority(thread);
if (auto *cv_tree = thread->GetConditionVariableTree(); cv_tree != nullptr) {
AfterUpdatePriority(cv_tree, thread);
}
/* Update the scheduler. */

View file

@ -21,28 +21,86 @@ namespace ams::kern::svc {
namespace {
constexpr bool IsKernelAddress(uintptr_t address) {
return KernelVirtualAddressSpaceBase <= address && address < KernelVirtualAddressSpaceEnd;
}
constexpr bool IsValidSignalType(ams::svc::SignalType type) {
switch (type) {
case ams::svc::SignalType_Signal:
case ams::svc::SignalType_SignalAndIncrementIfEqual:
case ams::svc::SignalType_SignalAndModifyByWaitingCountIfEqual:
return true;
default:
return false;
}
}
constexpr bool IsValidArbitrationType(ams::svc::ArbitrationType type) {
switch (type) {
case ams::svc::ArbitrationType_WaitIfLessThan:
case ams::svc::ArbitrationType_DecrementAndWaitIfLessThan:
case ams::svc::ArbitrationType_WaitIfEqual:
return true;
default:
return false;
}
}
Result WaitForAddress(uintptr_t address, ams::svc::ArbitrationType arb_type, int32_t value, int64_t timeout_ns) {
/* Validate input. */
R_UNLESS(AMS_LIKELY(!IsKernelAddress(address)), svc::ResultInvalidCurrentMemory());
R_UNLESS(util::IsAligned(address, sizeof(int32_t)), svc::ResultInvalidAddress());
R_UNLESS(IsValidArbitrationType(arb_type), svc::ResultInvalidEnumValue());
/* Convert timeout from nanoseconds to ticks. */
s64 timeout;
if (timeout_ns > 0) {
const ams::svc::Tick offset_tick(TimeSpan::FromNanoSeconds(timeout_ns));
if (AMS_LIKELY(offset_tick > 0)) {
timeout = KHardwareTimer::GetTick() + offset_tick + 2;
if (AMS_UNLIKELY(timeout <= 0)) {
timeout = std::numeric_limits<s64>::max();
}
} else {
timeout = std::numeric_limits<s64>::max();
}
} else {
timeout = timeout_ns;
}
return GetCurrentProcess().WaitAddressArbiter(address, arb_type, value, timeout);
}
Result SignalToAddress(uintptr_t address, ams::svc::SignalType signal_type, int32_t value, int32_t count) {
/* Validate input. */
R_UNLESS(AMS_LIKELY(!IsKernelAddress(address)), svc::ResultInvalidCurrentMemory());
R_UNLESS(util::IsAligned(address, sizeof(int32_t)), svc::ResultInvalidAddress());
R_UNLESS(IsValidSignalType(signal_type), svc::ResultInvalidEnumValue());
return GetCurrentProcess().SignalAddressArbiter(address, signal_type, value, count);
}
}
/* ============================= 64 ABI ============================= */
Result WaitForAddress64(ams::svc::Address address, ams::svc::ArbitrationType arb_type, int32_t value, int64_t timeout_ns) {
MESOSPHERE_PANIC("Stubbed SvcWaitForAddress64 was called.");
return WaitForAddress(address, arb_type, value, timeout_ns);
}
Result SignalToAddress64(ams::svc::Address address, ams::svc::SignalType signal_type, int32_t value, int32_t count) {
MESOSPHERE_PANIC("Stubbed SvcSignalToAddress64 was called.");
return SignalToAddress(address, signal_type, value, count);
}
/* ============================= 64From32 ABI ============================= */
Result WaitForAddress64From32(ams::svc::Address address, ams::svc::ArbitrationType arb_type, int32_t value, int64_t timeout_ns) {
MESOSPHERE_PANIC("Stubbed SvcWaitForAddress64From32 was called.");
return WaitForAddress(address, arb_type, value, timeout_ns);
}
Result SignalToAddress64From32(ams::svc::Address address, ams::svc::SignalType signal_type, int32_t value, int32_t count) {
MESOSPHERE_PANIC("Stubbed SvcSignalToAddress64From32 was called.");
return SignalToAddress(address, signal_type, value, count);
}
}