2019-12-13 10:21:43 +01:00
|
|
|
/*
|
2020-01-24 11:10:40 +01:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-12-13 10:21:43 +01: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-01-29 23:26:24 +01:00
|
|
|
void KInterruptTaskManager::TaskQueue::Enqueue(KInterruptTask *task) {
|
2020-12-18 02:18:47 +01:00
|
|
|
MESOSPHERE_ASSERT(task != m_head);
|
|
|
|
MESOSPHERE_ASSERT(task != m_tail);
|
2020-07-19 05:03:27 +02:00
|
|
|
MESOSPHERE_AUDIT(task->GetNextTask() == nullptr);
|
2020-02-10 11:26:00 +01:00
|
|
|
|
2020-01-29 23:26:24 +01:00
|
|
|
/* Insert the task into the queue. */
|
2020-12-18 02:18:47 +01:00
|
|
|
if (m_tail != nullptr) {
|
|
|
|
m_tail->SetNextTask(task);
|
2020-01-29 23:26:24 +01:00
|
|
|
} else {
|
2020-12-18 02:18:47 +01:00
|
|
|
m_head = task;
|
2020-01-29 23:26:24 +01:00
|
|
|
}
|
2019-12-13 10:21:43 +01:00
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
m_tail = task;
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
/* Set the next task for auditing. */
|
|
|
|
#if defined (MESOSPHERE_BUILD_FOR_AUDITING)
|
|
|
|
task->SetNextTask(GetDummyInterruptTask());
|
|
|
|
#endif
|
2019-12-13 10:21:43 +01:00
|
|
|
}
|
|
|
|
|
2020-01-29 23:26:24 +01:00
|
|
|
void KInterruptTaskManager::TaskQueue::Dequeue() {
|
2020-12-18 02:18:47 +01:00
|
|
|
MESOSPHERE_ASSERT(m_head != nullptr);
|
|
|
|
MESOSPHERE_ASSERT(m_tail != nullptr);
|
|
|
|
MESOSPHERE_AUDIT(m_tail->GetNextTask() == GetDummyInterruptTask());
|
2020-02-10 11:26:00 +01:00
|
|
|
|
|
|
|
/* Pop the task from the front of the queue. */
|
2020-12-18 02:18:47 +01:00
|
|
|
KInterruptTask *old_head = m_head;
|
2020-07-19 05:03:27 +02:00
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
if (m_head == m_tail) {
|
|
|
|
m_head = nullptr;
|
|
|
|
m_tail = nullptr;
|
2020-01-29 23:26:24 +01:00
|
|
|
} else {
|
2020-12-18 02:18:47 +01:00
|
|
|
m_head = m_head->GetNextTask();
|
2020-01-29 23:26:24 +01:00
|
|
|
}
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
#if defined (MESOSPHERE_BUILD_FOR_AUDITING)
|
|
|
|
old_head->SetNextTask(nullptr);
|
2020-07-28 00:22:40 +02:00
|
|
|
#else
|
|
|
|
AMS_UNUSED(old_head);
|
2020-07-19 05:03:27 +02:00
|
|
|
#endif
|
2019-12-13 10:21:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 11:26:00 +01:00
|
|
|
void KInterruptTaskManager::ThreadFunction(uintptr_t arg) {
|
|
|
|
reinterpret_cast<KInterruptTaskManager *>(arg)->ThreadFunctionImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KInterruptTaskManager::ThreadFunctionImpl() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
/* Get a task. */
|
|
|
|
KInterruptTask *task = nullptr;
|
|
|
|
{
|
|
|
|
KScopedInterruptDisable di;
|
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
task = m_task_queue.GetHead();
|
2020-02-10 11:26:00 +01:00
|
|
|
if (task == nullptr) {
|
2020-12-18 02:18:47 +01:00
|
|
|
m_thread->SetState(KThread::ThreadState_Waiting);
|
2020-02-10 11:26:00 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
m_task_queue.Dequeue();
|
2020-02-10 11:26:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the task. */
|
|
|
|
task->DoTask();
|
2021-04-07 22:38:51 +02:00
|
|
|
|
|
|
|
/* Destroy any objects we may need to close. */
|
|
|
|
m_thread->DestroyClosedObjects();
|
2020-02-10 11:26:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KInterruptTaskManager::Initialize() {
|
|
|
|
/* Reserve a thread from the system limit. */
|
|
|
|
MESOSPHERE_ABORT_UNLESS(Kernel::GetSystemResourceLimit().Reserve(ams::svc::LimitableResource_ThreadCountMax, 1));
|
|
|
|
|
|
|
|
/* Create and initialize the thread. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_thread = KThread::Create();
|
|
|
|
MESOSPHERE_ABORT_UNLESS(m_thread != nullptr);
|
|
|
|
MESOSPHERE_R_ABORT_UNLESS(KThread::InitializeHighPriorityThread(m_thread, ThreadFunction, reinterpret_cast<uintptr_t>(this)));
|
|
|
|
KThread::Register(m_thread);
|
2020-02-10 11:26:00 +01:00
|
|
|
|
|
|
|
/* Run the thread. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_thread->Run();
|
2020-02-10 11:26:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void KInterruptTaskManager::EnqueueTask(KInterruptTask *task) {
|
|
|
|
MESOSPHERE_ASSERT(!KInterruptManager::AreInterruptsEnabled());
|
|
|
|
|
2020-02-21 22:05:16 +01:00
|
|
|
/* Enqueue the task and signal the scheduler. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_task_queue.Enqueue(task);
|
2020-07-18 07:10:50 +02:00
|
|
|
Kernel::GetScheduler().SetInterruptTaskRunnable();
|
2020-02-10 11:26:00 +01:00
|
|
|
}
|
|
|
|
|
2019-12-13 10:21:43 +01:00
|
|
|
}
|