1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-23 12:22:08 +00:00
Atmosphere/libraries/libmesosphere/source/svc/kern_svc_resource_limit.cpp

151 lines
6.6 KiB
C++
Raw Normal View History

/*
* Copyright (c) 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::svc {
/* ============================= Common ============================= */
namespace {
2020-07-21 08:02:05 +01:00
constexpr bool IsValidLimitableResource(ams::svc::LimitableResource which) {
return which < ams::svc::LimitableResource_Count;
}
2020-07-21 08:02:05 +01:00
Result GetResourceLimitLimitValue(int64_t *out_limit_value, ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which) {
/* Validate the resource. */
R_UNLESS(IsValidLimitableResource(which), svc::ResultInvalidEnumValue());
/* Get the resource limit. */
KScopedAutoObject resource_limit = GetCurrentProcess().GetHandleTable().GetObject<KResourceLimit>(resource_limit_handle);
R_UNLESS(resource_limit.IsNotNull(), svc::ResultInvalidHandle());
/* Get the limit value. */
*out_limit_value = resource_limit->GetLimitValue(which);
R_SUCCEED();
2020-07-21 08:02:05 +01:00
}
Result GetResourceLimitCurrentValue(int64_t *out_current_value, ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which) {
/* Validate the resource. */
R_UNLESS(IsValidLimitableResource(which), svc::ResultInvalidEnumValue());
/* Get the resource limit. */
KScopedAutoObject resource_limit = GetCurrentProcess().GetHandleTable().GetObject<KResourceLimit>(resource_limit_handle);
R_UNLESS(resource_limit.IsNotNull(), svc::ResultInvalidHandle());
/* Get the current value. */
*out_current_value = resource_limit->GetCurrentValue(which);
R_SUCCEED();
2020-07-21 08:02:05 +01:00
}
2020-12-01 14:07:18 +00:00
Result GetResourceLimitPeakValue(int64_t *out_peak_value, ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which) {
/* Validate the resource. */
R_UNLESS(IsValidLimitableResource(which), svc::ResultInvalidEnumValue());
/* Get the resource limit. */
KScopedAutoObject resource_limit = GetCurrentProcess().GetHandleTable().GetObject<KResourceLimit>(resource_limit_handle);
R_UNLESS(resource_limit.IsNotNull(), svc::ResultInvalidHandle());
/* Get the peak value. */
2020-12-02 03:29:17 +00:00
*out_peak_value = resource_limit->GetPeakValue(which);
2020-12-01 14:07:18 +00:00
R_SUCCEED();
2020-12-01 14:07:18 +00:00
}
2020-07-21 08:02:05 +01:00
Result CreateResourceLimit(ams::svc::Handle *out_handle) {
/* Create a new resource limit. */
KResourceLimit *resource_limit = KResourceLimit::Create();
R_UNLESS(resource_limit != nullptr, svc::ResultOutOfResource());
/* Ensure we don't leak a reference to the limit. */
ON_SCOPE_EXIT { resource_limit->Close(); };
/* Initialize the resource limit. */
resource_limit->Initialize();
/* Register the limit. */
KResourceLimit::Register(resource_limit);
2020-07-21 08:02:05 +01:00
/* Add the limit to the handle table. */
R_TRY(GetCurrentProcess().GetHandleTable().Add(out_handle, resource_limit));
R_SUCCEED();
2020-07-21 08:02:05 +01:00
}
Result SetResourceLimitLimitValue(ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which, int64_t limit_value) {
/* Validate the resource. */
R_UNLESS(IsValidLimitableResource(which), svc::ResultInvalidEnumValue());
/* Get the resource limit. */
KScopedAutoObject resource_limit = GetCurrentProcess().GetHandleTable().GetObject<KResourceLimit>(resource_limit_handle);
R_UNLESS(resource_limit.IsNotNull(), svc::ResultInvalidHandle());
/* Set the limit value. */
R_TRY(resource_limit->SetLimitValue(which, limit_value));
R_SUCCEED();
2020-07-21 08:02:05 +01:00
}
}
/* ============================= 64 ABI ============================= */
Result GetResourceLimitLimitValue64(int64_t *out_limit_value, ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which) {
R_RETURN(GetResourceLimitLimitValue(out_limit_value, resource_limit_handle, which));
}
Result GetResourceLimitCurrentValue64(int64_t *out_current_value, ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which) {
R_RETURN(GetResourceLimitCurrentValue(out_current_value, resource_limit_handle, which));
}
2020-12-01 14:07:18 +00:00
Result GetResourceLimitPeakValue64(int64_t *out_peak_value, ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which) {
R_RETURN(GetResourceLimitPeakValue(out_peak_value, resource_limit_handle, which));
2020-12-01 14:07:18 +00:00
}
Result CreateResourceLimit64(ams::svc::Handle *out_handle) {
R_RETURN(CreateResourceLimit(out_handle));
}
Result SetResourceLimitLimitValue64(ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which, int64_t limit_value) {
R_RETURN(SetResourceLimitLimitValue(resource_limit_handle, which, limit_value));
}
/* ============================= 64From32 ABI ============================= */
Result GetResourceLimitLimitValue64From32(int64_t *out_limit_value, ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which) {
R_RETURN(GetResourceLimitLimitValue(out_limit_value, resource_limit_handle, which));
}
Result GetResourceLimitCurrentValue64From32(int64_t *out_current_value, ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which) {
R_RETURN(GetResourceLimitCurrentValue(out_current_value, resource_limit_handle, which));
}
2020-12-01 14:07:18 +00:00
Result GetResourceLimitPeakValue64From32(int64_t *out_peak_value, ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which) {
R_RETURN(GetResourceLimitPeakValue(out_peak_value, resource_limit_handle, which));
2020-12-01 14:07:18 +00:00
}
Result CreateResourceLimit64From32(ams::svc::Handle *out_handle) {
R_RETURN(CreateResourceLimit(out_handle));
}
Result SetResourceLimitLimitValue64From32(ams::svc::Handle resource_limit_handle, ams::svc::LimitableResource which, int64_t limit_value) {
R_RETURN(SetResourceLimitLimitValue(resource_limit_handle, which, limit_value));
}
2020-02-08 19:56:13 +00:00
}