2020-03-14 06:12:47 +00:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-03-14 06:12:47 +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 {
|
|
|
|
|
|
|
|
void KClientPort::Initialize(KPort *parent, s32 max_sessions) {
|
|
|
|
/* Set member variables. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_num_sessions = 0;
|
|
|
|
m_peak_sessions = 0;
|
|
|
|
m_parent = parent;
|
|
|
|
m_max_sessions = max_sessions;
|
2020-03-14 06:12:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 22:49:51 +01:00
|
|
|
void KClientPort::OnSessionFinalized() {
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
2021-10-20 19:02:17 +01:00
|
|
|
if (const auto prev = m_num_sessions--; prev == m_max_sessions) {
|
2020-07-09 22:49:51 +01:00
|
|
|
this->NotifyAvailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KClientPort::OnServerClosed() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
}
|
|
|
|
|
2020-03-14 06:12:47 +00:00
|
|
|
bool KClientPort::IsLight() const {
|
|
|
|
return this->GetParent()->IsLight();
|
|
|
|
}
|
|
|
|
|
2021-04-07 22:45:38 +01:00
|
|
|
bool KClientPort::IsServerClosed() const {
|
|
|
|
return this->GetParent()->IsServerClosed();
|
|
|
|
}
|
|
|
|
|
2020-03-14 06:12:47 +00:00
|
|
|
void KClientPort::Destroy() {
|
|
|
|
/* Note with our parent that we're closed. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_parent->OnClientClosed();
|
2020-03-14 06:12:47 +00:00
|
|
|
|
|
|
|
/* Close our reference to our parent. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_parent->Close();
|
2020-03-14 06:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool KClientPort::IsSignaled() const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2021-10-19 23:24:15 +01:00
|
|
|
return m_num_sessions.Load() < m_max_sessions;
|
2020-03-14 06:12:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 22:49:51 +01:00
|
|
|
Result KClientPort::CreateSession(KClientSession **out) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2021-09-18 06:01:58 +01:00
|
|
|
/* Declare the session we're going to allocate. */
|
|
|
|
KSession *session;
|
|
|
|
|
2020-07-09 22:49:51 +01:00
|
|
|
/* Reserve a new session from the resource limit. */
|
|
|
|
KScopedResourceReservation session_reservation(GetCurrentProcessPointer(), ams::svc::LimitableResource_SessionCountMax);
|
2021-09-18 06:01:58 +01:00
|
|
|
if (session_reservation.Succeeded()) {
|
|
|
|
/* Allocate a session normally. */
|
|
|
|
session = KSession::Create();
|
|
|
|
} else {
|
|
|
|
/* We couldn't reserve a session. Check that we support dynamically expanding the resource limit. */
|
|
|
|
R_UNLESS(GetCurrentProcess().GetResourceLimit() == std::addressof(Kernel::GetSystemResourceLimit()), svc::ResultLimitReached());
|
|
|
|
R_UNLESS(KTargetSystem::IsDynamicResourceLimitsEnabled(), svc::ResultLimitReached());
|
|
|
|
|
|
|
|
/* Try to allocate a session from unused slab memory. */
|
|
|
|
session = KSession::CreateFromUnusedSlabMemory();
|
|
|
|
R_UNLESS(session != nullptr, svc::ResultLimitReached());
|
2022-02-14 22:45:32 +00:00
|
|
|
ON_RESULT_FAILURE { session->Close(); };
|
2021-09-18 06:01:58 +01:00
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
/* We want to add two KSessionRequests to the heap, to prevent request exhaustion. */
|
|
|
|
for (size_t i = 0; i < 2; ++i) {
|
|
|
|
KSessionRequest *request = KSessionRequest::CreateFromUnusedSlabMemory();
|
|
|
|
R_UNLESS(request != nullptr, svc::ResultLimitReached());
|
2021-09-18 06:01:58 +01:00
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
request->Close();
|
2021-09-18 06:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We successfully allocated a session, so add the object we allocated to the resource limit. */
|
|
|
|
Kernel::GetSystemResourceLimit().Add(ams::svc::LimitableResource_SessionCountMax, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that we successfully created a session. */
|
|
|
|
R_UNLESS(session != nullptr, svc::ResultOutOfResource());
|
|
|
|
|
2020-07-09 22:49:51 +01:00
|
|
|
/* Update the session counts. */
|
|
|
|
{
|
2022-02-14 22:45:32 +00:00
|
|
|
ON_RESULT_FAILURE { session->Close(); };
|
|
|
|
|
2020-07-09 22:49:51 +01:00
|
|
|
/* Atomically increment the number of sessions. */
|
|
|
|
s32 new_sessions;
|
|
|
|
{
|
2020-12-18 01:18:47 +00:00
|
|
|
const auto max = m_max_sessions;
|
2021-10-19 23:24:15 +01:00
|
|
|
auto cur_sessions = m_num_sessions.Load();
|
2020-07-09 22:49:51 +01:00
|
|
|
do {
|
|
|
|
R_UNLESS(cur_sessions < max, svc::ResultOutOfSessions());
|
|
|
|
new_sessions = cur_sessions + 1;
|
2021-10-19 23:24:15 +01:00
|
|
|
} while (!m_num_sessions.CompareExchangeWeak<std::memory_order_relaxed>(cur_sessions, new_sessions));
|
2020-07-09 22:49:51 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Atomically update the peak session tracking. */
|
|
|
|
{
|
2021-10-19 23:24:15 +01:00
|
|
|
auto peak = m_peak_sessions.Load();
|
2020-07-09 22:49:51 +01:00
|
|
|
do {
|
|
|
|
if (peak >= new_sessions) {
|
|
|
|
break;
|
|
|
|
}
|
2021-10-19 23:24:15 +01:00
|
|
|
} while (!m_peak_sessions.CompareExchangeWeak<std::memory_order_relaxed>(peak, new_sessions));
|
2020-07-09 22:49:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the session. */
|
2020-12-18 01:18:47 +00:00
|
|
|
session->Initialize(this, m_parent->GetName());
|
2020-07-09 22:49:51 +01:00
|
|
|
|
|
|
|
/* Commit the session reservation. */
|
|
|
|
session_reservation.Commit();
|
|
|
|
|
|
|
|
/* Register the session. */
|
|
|
|
KSession::Register(session);
|
2022-02-14 22:45:32 +00:00
|
|
|
ON_RESULT_FAILURE {
|
2020-07-09 22:49:51 +01:00
|
|
|
session->GetClientSession().Close();
|
|
|
|
session->GetServerSession().Close();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Enqueue the session with our parent. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_TRY(m_parent->EnqueueSession(std::addressof(session->GetServerSession())));
|
2020-07-09 22:49:51 +01:00
|
|
|
|
|
|
|
/* We succeeded, so set the output. */
|
|
|
|
*out = std::addressof(session->GetClientSession());
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-07-14 10:24:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KClientPort::CreateLightSession(KLightClientSession **out) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2021-09-18 06:01:58 +01:00
|
|
|
/* Declare the session we're going to allocate. */
|
|
|
|
KLightSession *session;
|
|
|
|
|
2020-07-14 10:24:26 +01:00
|
|
|
/* Reserve a new session from the resource limit. */
|
|
|
|
KScopedResourceReservation session_reservation(GetCurrentProcessPointer(), ams::svc::LimitableResource_SessionCountMax);
|
2021-09-18 06:01:58 +01:00
|
|
|
if (session_reservation.Succeeded()) {
|
|
|
|
/* Allocate a session normally. */
|
|
|
|
session = KLightSession::Create();
|
|
|
|
} else {
|
|
|
|
/* We couldn't reserve a session. Check that we support dynamically expanding the resource limit. */
|
|
|
|
R_UNLESS(GetCurrentProcess().GetResourceLimit() == std::addressof(Kernel::GetSystemResourceLimit()), svc::ResultLimitReached());
|
|
|
|
R_UNLESS(KTargetSystem::IsDynamicResourceLimitsEnabled(), svc::ResultLimitReached());
|
|
|
|
|
|
|
|
/* Try to allocate a session from unused slab memory. */
|
|
|
|
session = KLightSession::CreateFromUnusedSlabMemory();
|
|
|
|
R_UNLESS(session != nullptr, svc::ResultLimitReached());
|
|
|
|
|
|
|
|
/* We successfully allocated a session, so add the object we allocated to the resource limit. */
|
|
|
|
Kernel::GetSystemResourceLimit().Add(ams::svc::LimitableResource_SessionCountMax, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that we successfully created a session. */
|
|
|
|
R_UNLESS(session != nullptr, svc::ResultOutOfResource());
|
2020-07-14 10:24:26 +01:00
|
|
|
|
|
|
|
/* Update the session counts. */
|
|
|
|
{
|
2022-02-14 22:45:32 +00:00
|
|
|
ON_RESULT_FAILURE { session->Close(); };
|
|
|
|
|
2020-07-14 10:24:26 +01:00
|
|
|
/* Atomically increment the number of sessions. */
|
|
|
|
s32 new_sessions;
|
|
|
|
{
|
2020-12-18 01:18:47 +00:00
|
|
|
const auto max = m_max_sessions;
|
2021-10-19 23:24:15 +01:00
|
|
|
auto cur_sessions = m_num_sessions.Load();
|
2020-07-14 10:24:26 +01:00
|
|
|
do {
|
|
|
|
R_UNLESS(cur_sessions < max, svc::ResultOutOfSessions());
|
|
|
|
new_sessions = cur_sessions + 1;
|
2021-10-19 23:24:15 +01:00
|
|
|
} while (!m_num_sessions.CompareExchangeWeak<std::memory_order_relaxed>(cur_sessions, new_sessions));
|
2020-07-14 10:24:26 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Atomically update the peak session tracking. */
|
|
|
|
{
|
2021-10-19 23:24:15 +01:00
|
|
|
auto peak = m_peak_sessions.Load();
|
2020-07-14 10:24:26 +01:00
|
|
|
do {
|
|
|
|
if (peak >= new_sessions) {
|
|
|
|
break;
|
|
|
|
}
|
2021-10-19 23:24:15 +01:00
|
|
|
} while (!m_peak_sessions.CompareExchangeWeak<std::memory_order_relaxed>(peak, new_sessions));
|
2020-07-14 10:24:26 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-09 22:49:51 +01:00
|
|
|
|
2020-07-14 10:24:26 +01:00
|
|
|
/* Initialize the session. */
|
2020-12-18 01:18:47 +00:00
|
|
|
session->Initialize(this, m_parent->GetName());
|
2020-07-14 10:24:26 +01:00
|
|
|
|
|
|
|
/* Commit the session reservation. */
|
|
|
|
session_reservation.Commit();
|
|
|
|
|
|
|
|
/* Register the session. */
|
|
|
|
KLightSession::Register(session);
|
2022-02-14 22:45:32 +00:00
|
|
|
ON_RESULT_FAILURE {
|
2020-07-14 10:24:26 +01:00
|
|
|
session->GetClientSession().Close();
|
|
|
|
session->GetServerSession().Close();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Enqueue the session with our parent. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_TRY(m_parent->EnqueueSession(std::addressof(session->GetServerSession())));
|
2020-07-14 10:24:26 +01:00
|
|
|
|
|
|
|
/* We succeeded, so set the output. */
|
|
|
|
*out = std::addressof(session->GetClientSession());
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-07-09 22:49:51 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 06:12:47 +00:00
|
|
|
}
|