2020-07-14 11:24:26 +02:00
|
|
|
/*
|
|
|
|
* 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 {
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr u64 InvalidThreadId = -1ull;
|
|
|
|
|
|
|
|
class ThreadQueueImplForKLightServerSessionRequest final : public KThreadQueue {
|
|
|
|
private:
|
|
|
|
KThread::WaiterList *m_wait_list;
|
|
|
|
public:
|
|
|
|
constexpr ThreadQueueImplForKLightServerSessionRequest(KThread::WaiterList *wl) : KThreadQueue(), m_wait_list(wl) { /* ... */ }
|
|
|
|
|
|
|
|
virtual void EndWait(KThread *waiting_thread, Result wait_result) override {
|
|
|
|
/* Remove the thread from our wait list. */
|
|
|
|
m_wait_list->erase(m_wait_list->iterator_to(*waiting_thread));
|
|
|
|
|
|
|
|
/* Invoke the base end wait handler. */
|
|
|
|
KThreadQueue::EndWait(waiting_thread, wait_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CancelWait(KThread *waiting_thread, Result wait_result, bool cancel_timer_task) override {
|
|
|
|
/* Remove the thread from our wait list. */
|
|
|
|
m_wait_list->erase(m_wait_list->iterator_to(*waiting_thread));
|
|
|
|
|
|
|
|
/* Invoke the base cancel wait handler. */
|
|
|
|
KThreadQueue::CancelWait(waiting_thread, wait_result, cancel_timer_task);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadQueueImplForKLightServerSessionReceive final : public KThreadQueue {
|
|
|
|
private:
|
|
|
|
KThread **m_server_thread;
|
|
|
|
public:
|
|
|
|
constexpr ThreadQueueImplForKLightServerSessionReceive(KThread **st) : KThreadQueue(), m_server_thread(st) { /* ... */ }
|
|
|
|
|
|
|
|
virtual void EndWait(KThread *waiting_thread, Result wait_result) override {
|
|
|
|
/* Clear the server thread. */
|
|
|
|
*m_server_thread = nullptr;
|
|
|
|
|
|
|
|
/* Set the waiting thread as not cancelable. */
|
|
|
|
waiting_thread->ClearCancellable();
|
|
|
|
|
|
|
|
/* Invoke the base end wait handler. */
|
|
|
|
KThreadQueue::EndWait(waiting_thread, wait_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CancelWait(KThread *waiting_thread, Result wait_result, bool cancel_timer_task) override {
|
|
|
|
/* Clear the server thread. */
|
|
|
|
*m_server_thread = nullptr;
|
|
|
|
|
|
|
|
/* Set the waiting thread as not cancelable. */
|
|
|
|
waiting_thread->ClearCancellable();
|
|
|
|
|
|
|
|
/* Invoke the base cancel wait handler. */
|
|
|
|
KThreadQueue::CancelWait(waiting_thread, wait_result, cancel_timer_task);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-14 11:24:26 +02:00
|
|
|
void KLightServerSession::Destroy() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
this->CleanupRequests();
|
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
m_parent->OnServerClosed();
|
2020-07-14 11:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KLightServerSession::OnClientClosed() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-07-29 12:57:40 +02:00
|
|
|
|
|
|
|
this->CleanupRequests();
|
2020-07-14 11:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KLightServerSession::OnRequest(KThread *request_thread) {
|
2020-07-29 12:57:40 +02:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
ThreadQueueImplForKLightServerSessionRequest wait_queue(std::addressof(m_request_list));
|
|
|
|
|
|
|
|
/* Send the request. */
|
|
|
|
{
|
|
|
|
/* Lock the scheduler. */
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Check that the server isn't closed. */
|
|
|
|
R_UNLESS(!m_parent->IsServerClosed(), svc::ResultSessionClosed());
|
|
|
|
|
|
|
|
/* Check that the request thread isn't terminating. */
|
|
|
|
R_UNLESS(!request_thread->IsTerminationRequested(), svc::ResultTerminationRequested());
|
2020-07-29 12:57:40 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Add the request thread to our list. */
|
|
|
|
m_request_list.push_back(*request_thread);
|
2020-07-29 12:57:40 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Begin waiting on the request. */
|
|
|
|
request_thread->BeginWait(std::addressof(wait_queue));
|
|
|
|
|
|
|
|
/* If we have a server thread, end its wait. */
|
|
|
|
if (m_server_thread != nullptr) {
|
|
|
|
m_server_thread->EndWait(ResultSuccess());
|
|
|
|
}
|
2020-07-29 12:57:40 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* NOTE: Nintendo returns GetCurrentThread().GetWaitResult() here. */
|
|
|
|
/* This is technically incorrect, although it doesn't cause problems in practice */
|
|
|
|
/* because this is only ever called with request_thread = GetCurrentThreadPointer(). */
|
|
|
|
return request_thread->GetWaitResult();
|
2020-07-14 11:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KLightServerSession::ReplyAndReceive(u32 *data) {
|
2020-07-29 12:57:40 +02:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Set the server context. */
|
2021-09-19 19:11:56 +02:00
|
|
|
GetCurrentThread().SetLightSessionData(data);
|
2020-07-29 12:57:40 +02:00
|
|
|
|
|
|
|
/* Reply, if we need to. */
|
|
|
|
if (data[0] & KLightSession::ReplyFlag) {
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Check that we're open. */
|
2020-12-18 02:18:47 +01:00
|
|
|
R_UNLESS(!m_parent->IsClientClosed(), svc::ResultSessionClosed());
|
|
|
|
R_UNLESS(!m_parent->IsServerClosed(), svc::ResultSessionClosed());
|
2020-07-29 12:57:40 +02:00
|
|
|
|
|
|
|
/* Check that we have a request to reply to. */
|
2020-12-18 02:18:47 +01:00
|
|
|
R_UNLESS(m_current_request != nullptr, svc::ResultInvalidState());
|
2020-07-29 12:57:40 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Check that the server thread id is correct. */
|
|
|
|
R_UNLESS(m_server_thread_id == GetCurrentThread().GetId(), svc::ResultInvalidState());
|
2020-07-29 12:57:40 +02:00
|
|
|
|
|
|
|
/* If we can reply, do so. */
|
2020-12-18 02:18:47 +01:00
|
|
|
if (!m_current_request->IsTerminationRequested()) {
|
2021-09-19 19:11:56 +02:00
|
|
|
std::memcpy(m_current_request->GetLightSessionData(), GetCurrentThread().GetLightSessionData(), KLightSession::DataSize);
|
|
|
|
m_current_request->EndWait(ResultSuccess());
|
2020-07-29 12:57:40 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Close our current request. */
|
|
|
|
m_current_request->Close();
|
|
|
|
|
2020-07-29 12:57:40 +02:00
|
|
|
/* Clear our current request. */
|
2021-09-19 19:11:56 +02:00
|
|
|
m_current_request = nullptr;
|
|
|
|
m_server_thread_id = InvalidThreadId;
|
2020-07-29 12:57:40 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Close any pending objects before we wait. */
|
|
|
|
GetCurrentThread().DestroyClosedObjects();
|
|
|
|
|
|
|
|
/* Create the wait queue for our receive. */
|
|
|
|
ThreadQueueImplForKLightServerSessionReceive wait_queue(std::addressof(m_server_thread));
|
2020-07-29 12:57:40 +02:00
|
|
|
|
|
|
|
/* Receive. */
|
|
|
|
while (true) {
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Try to receive a request. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
2020-07-29 12:57:40 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Check that we aren't already receiving. */
|
|
|
|
R_UNLESS(m_server_thread == nullptr, svc::ResultInvalidState());
|
|
|
|
R_UNLESS(m_server_thread_id == InvalidThreadId, svc::ResultInvalidState());
|
2020-07-29 12:57:40 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Check that we're open. */
|
|
|
|
R_UNLESS(!m_parent->IsClientClosed(), svc::ResultSessionClosed());
|
|
|
|
R_UNLESS(!m_parent->IsServerClosed(), svc::ResultSessionClosed());
|
2020-07-29 12:57:40 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Check that we're not terminating. */
|
|
|
|
R_UNLESS(!GetCurrentThread().IsTerminationRequested(), svc::ResultTerminationRequested());
|
|
|
|
|
|
|
|
/* If we have a request available, use it. */
|
|
|
|
if (auto head = m_request_list.begin(); head != m_request_list.end()) {
|
|
|
|
/* Set our current request. */
|
|
|
|
m_current_request = std::addressof(*head);
|
|
|
|
m_current_request->Open();
|
|
|
|
|
|
|
|
/* Set our server thread id. */
|
|
|
|
m_server_thread_id = GetCurrentThread().GetId();
|
|
|
|
|
|
|
|
/* Copy the client request data. */
|
|
|
|
std::memcpy(GetCurrentThread().GetLightSessionData(), m_current_request->GetLightSessionData(), KLightSession::DataSize);
|
|
|
|
|
|
|
|
/* We successfully received. */
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
2020-07-29 12:57:40 +02:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* We need to wait for a request to come in. */
|
2020-07-29 12:57:40 +02:00
|
|
|
|
|
|
|
/* Check if we were cancelled. */
|
2021-09-19 19:11:56 +02:00
|
|
|
if (GetCurrentThread().IsWaitCancelled()) {
|
|
|
|
GetCurrentThread().ClearWaitCancelled();
|
2020-07-29 12:57:40 +02:00
|
|
|
return svc::ResultCancelled();
|
|
|
|
}
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Mark ourselves as cancellable. */
|
|
|
|
GetCurrentThread().SetCancellable();
|
|
|
|
|
|
|
|
/* Wait for a request to come in. */
|
|
|
|
m_server_thread = GetCurrentThreadPointer();
|
|
|
|
GetCurrentThread().BeginWait(std::addressof(wait_queue));
|
2020-07-29 12:57:40 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* We waited to receive a request; if our wait failed, return the failing result. */
|
|
|
|
R_TRY(GetCurrentThread().GetWaitResult());
|
|
|
|
}
|
2020-07-14 11:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KLightServerSession::CleanupRequests() {
|
2020-07-29 12:57:40 +02:00
|
|
|
/* Cleanup all pending requests. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Handle the current request. */
|
2020-12-18 02:18:47 +01:00
|
|
|
if (m_current_request != nullptr) {
|
2020-07-29 12:57:40 +02:00
|
|
|
/* Reply to the current request. */
|
2020-12-18 02:18:47 +01:00
|
|
|
if (!m_current_request->IsTerminationRequested()) {
|
2021-09-19 19:11:56 +02:00
|
|
|
m_current_request->EndWait(svc::ResultSessionClosed());
|
2020-07-29 12:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear our current request. */
|
2021-09-19 19:11:56 +02:00
|
|
|
m_current_request->Close();
|
|
|
|
m_current_request = nullptr;
|
|
|
|
m_server_thread_id = InvalidThreadId;
|
2020-07-29 12:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reply to all other requests. */
|
2021-09-19 19:11:56 +02:00
|
|
|
for (auto &thread : m_request_list) {
|
|
|
|
thread.EndWait(svc::ResultSessionClosed());
|
2020-07-29 12:57:40 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Wait up our server thread, if we have one. */
|
|
|
|
if (m_server_thread != nullptr) {
|
|
|
|
m_server_thread->EndWait(svc::ResultSessionClosed());
|
2020-07-29 12:57:40 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-14 11:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|