1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-18 09:56:40 +00:00
Atmosphere/stratosphere/loader/source/ldr_capabilities.cpp

429 lines
19 KiB
C++
Raw Normal View History

2019-06-26 23:46:19 +01:00
/*
* Copyright (c) Atmosphère-NX
2019-06-26 23:46:19 +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 <stratosphere.hpp>
2019-06-26 23:46:19 +01:00
#include "ldr_capabilities.hpp"
namespace ams::ldr {
2019-06-26 23:46:19 +01:00
namespace {
/* Types. */
enum class CapabilityId {
KernelFlags = 3,
SyscallMask = 4,
MapRange = 6,
MapPage = 7,
MapRegion = 10,
2019-06-26 23:46:19 +01:00
InterruptPair = 11,
ApplicationType = 13,
KernelVersion = 14,
HandleTable = 15,
DebugFlags = 16,
Empty = 32,
};
2020-01-02 11:13:40 +00:00
template<size_t Index, size_t Count, typename T = u32>
using CapabilityField = util::BitPack32::Field<Index, Count, T>;
2019-06-26 23:46:19 +01:00
2020-01-02 11:13:40 +00:00
#define DEFINE_CAPABILITY_FIELD(name, prev, ...) \
using name = CapabilityField<prev::Next, __VA_ARGS__>; \
constexpr ALWAYS_INLINE typename name::Type Get##name() const { return this->Get<name>(); }
2019-06-26 23:46:19 +01:00
2020-01-02 11:13:40 +00:00
constexpr ALWAYS_INLINE CapabilityId GetCapabilityId(util::BitPack32 cap) {
return static_cast<CapabilityId>(util::CountTrailingZeros<u32>(~cap.value));
2020-01-02 11:13:40 +00:00
}
2019-06-26 23:46:19 +01:00
constexpr inline util::BitPack32 EmptyCapability = {~u32{}};
static_assert(GetCapabilityId(EmptyCapability) == CapabilityId::Empty);
2019-06-26 23:46:19 +01:00
#define CAPABILITY_CLASS_NAME(id) Capability##id
2020-01-02 11:13:40 +00:00
#define DEFINE_CAPABILITY_CLASS(id, member_functions) \
class CAPABILITY_CLASS_NAME(id) { \
public: \
static constexpr CapabilityId Id = CapabilityId::id; \
using IdBits = CapabilityField<0, static_cast<size_t>(Id) + 1>; \
static constexpr u32 IdBitsValue = (static_cast<u32>(1) << static_cast<size_t>(Id)) - 1; \
private: \
2021-10-10 08:14:06 +01:00
util::BitPack32 m_value; \
2020-01-02 11:13:40 +00:00
private: \
template<typename FieldType> \
2021-10-10 08:14:06 +01:00
constexpr ALWAYS_INLINE typename FieldType::Type Get() const { return m_value.Get<FieldType>(); } \
2020-01-02 11:13:40 +00:00
template<typename FieldType> \
2021-10-10 08:14:06 +01:00
constexpr ALWAYS_INLINE void Set(typename FieldType::Type fv) { m_value.Set<FieldType>(fv); } \
constexpr ALWAYS_INLINE u32 GetValue() const { return m_value.value; } \
2020-01-02 11:13:40 +00:00
public: \
2021-10-10 08:14:06 +01:00
constexpr ALWAYS_INLINE CAPABILITY_CLASS_NAME(id)(util::BitPack32 v) : m_value{v} { /* ... */ } \
2020-01-02 11:13:40 +00:00
\
static constexpr CAPABILITY_CLASS_NAME(id) Decode(util::BitPack32 v) { return CAPABILITY_CLASS_NAME(id)(v); } \
\
member_functions \
}; \
static_assert(std::is_trivially_destructible<CAPABILITY_CLASS_NAME(id)>::value)
2019-06-26 23:46:19 +01:00
/* Class definitions. */
DEFINE_CAPABILITY_CLASS(KernelFlags,
2020-01-02 11:13:40 +00:00
DEFINE_CAPABILITY_FIELD(MaximumThreadPriority, IdBits, 6);
DEFINE_CAPABILITY_FIELD(MinimumThreadPriority, MaximumThreadPriority, 6);
DEFINE_CAPABILITY_FIELD(MinimumCoreId, MinimumThreadPriority, 8);
DEFINE_CAPABILITY_FIELD(MaximumCoreId, MinimumCoreId, 8);
2019-06-26 23:46:19 +01:00
2020-01-02 11:13:40 +00:00
bool IsValid(const util::BitPack32 *kac, size_t kac_count) const {
2019-06-26 23:46:19 +01:00
for (size_t i = 0; i < kac_count; i++) {
2020-01-02 11:13:40 +00:00
if (GetCapabilityId(kac[i]) == Id) {
2019-06-28 01:37:33 +01:00
const auto restriction = Decode(kac[i]);
2019-06-26 23:46:19 +01:00
2019-06-28 01:37:33 +01:00
if (this->GetMinimumThreadPriority() < restriction.GetMinimumThreadPriority() ||
this->GetMaximumThreadPriority() > restriction.GetMaximumThreadPriority() ||
2019-06-26 23:46:19 +01:00
this->GetMinimumThreadPriority() > this->GetMaximumThreadPriority()) {
return false;
}
2019-06-28 01:37:33 +01:00
if (this->GetMinimumCoreId() < restriction.GetMinimumCoreId() ||
this->GetMaximumCoreId() > restriction.GetMaximumCoreId() ||
2019-06-26 23:46:19 +01:00
this->GetMinimumCoreId() > this->GetMaximumCoreId()) {
return false;
}
return true;
}
}
return false;
}
);
DEFINE_CAPABILITY_CLASS(SyscallMask,
2020-01-02 11:13:40 +00:00
DEFINE_CAPABILITY_FIELD(Mask, IdBits, 24);
DEFINE_CAPABILITY_FIELD(Index, Mask, 3);
2019-06-26 23:46:19 +01:00
2020-01-02 11:13:40 +00:00
bool IsValid(const util::BitPack32 *kac, size_t kac_count) const {
2019-06-26 23:46:19 +01:00
for (size_t i = 0; i < kac_count; i++) {
2020-01-02 11:13:40 +00:00
if (GetCapabilityId(kac[i]) == Id) {
2019-06-28 01:37:33 +01:00
const auto restriction = Decode(kac[i]);
2019-06-26 23:46:19 +01:00
2019-06-28 01:37:33 +01:00
if (this->GetIndex() == restriction.GetIndex() && this->GetMask() == restriction.GetMask()) {
2019-06-26 23:46:19 +01:00
return true;
}
}
}
return false;
}
);
DEFINE_CAPABILITY_CLASS(MapRange,
2020-01-02 11:13:40 +00:00
DEFINE_CAPABILITY_FIELD(AddressSize, IdBits, 24);
DEFINE_CAPABILITY_FIELD(Flag, AddressSize, 1, bool);
2019-06-26 23:46:19 +01:00
static constexpr size_t SizeMax = 0x100000;
2020-01-02 11:13:40 +00:00
bool IsValid(const util::BitPack32 next_cap, const util::BitPack32 *kac, size_t kac_count) const {
if (GetCapabilityId(next_cap) != Id) {
2019-06-26 23:46:19 +01:00
return false;
}
const auto next = Decode(next_cap);
const u32 start = this->GetAddressSize();
const u32 size = next.GetAddressSize();
const u32 end = start + size;
if (size >= SizeMax) {
return false;
}
for (size_t i = 0; i < kac_count; i++) {
2020-01-02 11:13:40 +00:00
if (GetCapabilityId(kac[i]) == Id) {
2019-06-28 01:37:33 +01:00
const auto restriction = Decode(kac[i++]);
2020-01-02 11:13:40 +00:00
if (i >= kac_count || GetCapabilityId(kac[i]) != Id) {
2019-06-26 23:46:19 +01:00
return false;
}
2019-06-28 01:37:33 +01:00
const auto restriction_next = Decode(kac[i]);
const u32 restriction_start = restriction.GetAddressSize();
const u32 restriction_size = restriction_next.GetAddressSize();
const u32 restriction_end = restriction_start + restriction_size;
2019-06-26 23:46:19 +01:00
2019-06-28 01:37:33 +01:00
if (restriction_size >= SizeMax) {
2019-06-26 23:46:19 +01:00
continue;
}
2019-06-28 01:37:33 +01:00
if (this->GetFlag() == restriction.GetFlag() && next.GetFlag() == restriction_next.GetFlag()) {
if (restriction_start <= start && start <= restriction_end && end <= restriction_end) {
2019-06-26 23:46:19 +01:00
return true;
}
}
}
}
return false;
}
);
DEFINE_CAPABILITY_CLASS(MapPage,
2020-01-02 11:13:40 +00:00
DEFINE_CAPABILITY_FIELD(Address, IdBits, 24);
2019-06-26 23:46:19 +01:00
2020-01-02 11:13:40 +00:00
bool IsValid(const util::BitPack32 *kac, size_t kac_count) const {
2019-06-26 23:46:19 +01:00
for (size_t i = 0; i < kac_count; i++) {
2020-01-02 11:13:40 +00:00
if (GetCapabilityId(kac[i]) == Id) {
2019-06-28 01:37:33 +01:00
const auto restriction = Decode(kac[i]);
2019-06-26 23:46:19 +01:00
2019-06-28 01:37:33 +01:00
if (this->GetValue() == restriction.GetValue()) {
2019-06-26 23:46:19 +01:00
return true;
}
}
}
return false;
}
);
enum class MemoryRegionType : u32 {
2022-04-18 04:11:05 +01:00
NoMapping = 0,
KernelTraceBuffer = 1,
OnMemoryBootImage = 2,
DTB = 3,
};
DEFINE_CAPABILITY_CLASS(MapRegion,
DEFINE_CAPABILITY_FIELD(Region0, IdBits, 6, MemoryRegionType);
DEFINE_CAPABILITY_FIELD(ReadOnly0, Region0, 1, bool);
DEFINE_CAPABILITY_FIELD(Region1, ReadOnly0, 6, MemoryRegionType);
DEFINE_CAPABILITY_FIELD(ReadOnly1, Region1, 1, bool);
DEFINE_CAPABILITY_FIELD(Region2, ReadOnly1, 6, MemoryRegionType);
DEFINE_CAPABILITY_FIELD(ReadOnly2, Region2, 1, bool);
2022-04-18 04:11:05 +01:00
static bool IsValidRegionType(const util::BitPack32 *kac, size_t kac_count, MemoryRegionType region_type, bool is_read_only) {
if (region_type != MemoryRegionType::NoMapping) {
for (size_t i = 0; i < kac_count; i++) {
if (GetCapabilityId(kac[i]) == Id) {
const auto restriction = Decode(kac[i]);
2022-04-18 04:11:05 +01:00
if ((restriction.GetRegion0() == region_type && (is_read_only || !restriction.GetReadOnly0())) ||
(restriction.GetRegion1() == region_type && (is_read_only || !restriction.GetReadOnly1())) ||
(restriction.GetRegion2() == region_type && (is_read_only || !restriction.GetReadOnly2())))
{
return true;
}
}
}
2022-04-18 04:11:05 +01:00
return false;
} else {
return true;
}
2022-04-18 04:11:05 +01:00
}
bool IsValid(const util::BitPack32 *kac, size_t kac_count) const {
return IsValidRegionType(kac, kac_count, this->GetRegion0(), this->GetReadOnly0()) &&
IsValidRegionType(kac, kac_count, this->GetRegion1(), this->GetReadOnly1()) &&
IsValidRegionType(kac, kac_count, this->GetRegion2(), this->GetReadOnly2());
}
);
2019-06-26 23:46:19 +01:00
DEFINE_CAPABILITY_CLASS(InterruptPair,
2020-01-02 11:13:40 +00:00
DEFINE_CAPABILITY_FIELD(InterruptId0, IdBits, 10);
DEFINE_CAPABILITY_FIELD(InterruptId1, InterruptId0, 10);
2019-06-26 23:46:19 +01:00
static constexpr u32 EmptyInterruptId = 0x3FF;
2020-01-02 11:13:40 +00:00
bool IsSingleIdValid(const u32 id, const util::BitPack32 *kac, size_t kac_count) const {
2019-06-26 23:46:19 +01:00
for (size_t i = 0; i < kac_count; i++) {
2020-01-02 11:13:40 +00:00
if (GetCapabilityId(kac[i]) == Id) {
2019-06-28 01:37:33 +01:00
const auto restriction = Decode(kac[i]);
2019-06-26 23:46:19 +01:00
2019-06-28 01:37:33 +01:00
if (restriction.GetInterruptId0() == EmptyInterruptId && restriction.GetInterruptId1() == EmptyInterruptId) {
2019-06-26 23:46:19 +01:00
return true;
}
2019-06-28 01:37:33 +01:00
if (restriction.GetInterruptId0() == id || restriction.GetInterruptId1() == id) {
2019-06-26 23:46:19 +01:00
return true;
}
}
}
return false;
}
2020-01-02 11:13:40 +00:00
bool IsValid(const util::BitPack32 *kac, size_t kac_count) const {
2019-06-26 23:46:19 +01:00
return IsSingleIdValid(this->GetInterruptId0(), kac, kac_count) && IsSingleIdValid(this->GetInterruptId1(), kac, kac_count);
}
);
DEFINE_CAPABILITY_CLASS(ApplicationType,
2020-01-02 11:13:40 +00:00
DEFINE_CAPABILITY_FIELD(ApplicationType, IdBits, 3);
2019-06-26 23:46:19 +01:00
2020-01-02 11:13:40 +00:00
bool IsValid(const util::BitPack32 *kac, size_t kac_count) const {
2019-06-26 23:46:19 +01:00
for (size_t i = 0; i < kac_count; i++) {
2020-01-02 11:13:40 +00:00
if (GetCapabilityId(kac[i]) == Id) {
2019-06-28 01:37:33 +01:00
const auto restriction = Decode(kac[i]);
2019-06-26 23:46:19 +01:00
2019-06-28 01:37:33 +01:00
return restriction.GetValue() == this->GetValue();
2019-06-26 23:46:19 +01:00
}
}
return false;
}
2020-01-02 11:13:40 +00:00
static constexpr util::BitPack32 Encode(u32 app_type) {
util::BitPack32 encoded{IdBitsValue};
2020-01-02 11:13:40 +00:00
encoded.Set<ApplicationType>(app_type);
return encoded;
2019-06-26 23:46:19 +01:00
}
);
DEFINE_CAPABILITY_CLASS(KernelVersion,
2020-01-02 11:13:40 +00:00
DEFINE_CAPABILITY_FIELD(MinorVersion, IdBits, 4);
DEFINE_CAPABILITY_FIELD(MajorVersion, MinorVersion, 13);
2019-06-26 23:46:19 +01:00
2020-01-02 11:13:40 +00:00
bool IsValid(const util::BitPack32 *kac, size_t kac_count) const {
2019-06-26 23:46:19 +01:00
for (size_t i = 0; i < kac_count; i++) {
2020-01-02 11:13:40 +00:00
if (GetCapabilityId(kac[i]) == Id) {
2019-06-28 01:37:33 +01:00
const auto restriction = Decode(kac[i]);
2019-06-26 23:46:19 +01:00
2019-06-28 01:37:33 +01:00
return restriction.GetValue() == this->GetValue();
2019-06-26 23:46:19 +01:00
}
}
return false;
}
);
DEFINE_CAPABILITY_CLASS(HandleTable,
2020-01-02 11:13:40 +00:00
DEFINE_CAPABILITY_FIELD(Size, IdBits, 10);
2019-06-26 23:46:19 +01:00
2020-01-02 11:13:40 +00:00
bool IsValid(const util::BitPack32 *kac, size_t kac_count) const {
2019-06-26 23:46:19 +01:00
for (size_t i = 0; i < kac_count; i++) {
2020-01-02 11:13:40 +00:00
if (GetCapabilityId(kac[i]) == Id) {
2019-06-28 01:37:33 +01:00
const auto restriction = Decode(kac[i]);
2019-06-26 23:46:19 +01:00
2019-06-28 01:37:33 +01:00
return this->GetSize() <= restriction.GetSize();
2019-06-26 23:46:19 +01:00
}
}
return false;
}
);
DEFINE_CAPABILITY_CLASS(DebugFlags,
2020-01-02 11:13:40 +00:00
DEFINE_CAPABILITY_FIELD(AllowDebug, IdBits, 1, bool);
DEFINE_CAPABILITY_FIELD(ForceDebug, AllowDebug, 1, bool);
2019-06-26 23:46:19 +01:00
2020-01-02 11:13:40 +00:00
bool IsValid(const util::BitPack32 *kac, size_t kac_count) const {
2019-06-26 23:46:19 +01:00
for (size_t i = 0; i < kac_count; i++) {
2020-01-02 11:13:40 +00:00
if (GetCapabilityId(kac[i]) == Id) {
2019-06-28 01:37:33 +01:00
const auto restriction = Decode(kac[i]);
2019-06-26 23:46:19 +01:00
2019-06-28 01:37:33 +01:00
return (restriction.GetValue() & this->GetValue()) == this->GetValue();
2019-06-26 23:46:19 +01:00
}
}
return false;
}
2020-01-02 11:13:40 +00:00
static constexpr util::BitPack32 Encode(bool allow_debug, bool force_debug) {
util::BitPack32 encoded{IdBitsValue};
2020-01-02 11:13:40 +00:00
encoded.Set<AllowDebug>(allow_debug);
encoded.Set<ForceDebug>(force_debug);
return encoded;
2019-06-26 23:46:19 +01:00
}
);
}
/* Capabilities API. */
Result TestCapability(const util::BitPack32 *kacd, size_t kacd_count, const util::BitPack32 *kac, size_t kac_count) {
for (size_t i = 0; i < kac_count; i++) {
const auto cap = kac[i];
const auto id = GetCapabilityId(cap);
2019-06-26 23:46:19 +01:00
#define VALIDATE_CASE(id) \
case CapabilityId::id: \
R_UNLESS(Capability##id::Decode(cap).IsValid(kacd, kacd_count), ldr::ResultInvalidCapability##id()); \
2019-06-26 23:46:19 +01:00
break
switch (id) {
VALIDATE_CASE(KernelFlags);
VALIDATE_CASE(SyscallMask);
VALIDATE_CASE(MapPage);
VALIDATE_CASE(MapRegion);
2019-06-26 23:46:19 +01:00
VALIDATE_CASE(InterruptPair);
VALIDATE_CASE(ApplicationType);
VALIDATE_CASE(KernelVersion);
VALIDATE_CASE(HandleTable);
VALIDATE_CASE(DebugFlags);
case CapabilityId::MapRange:
{
/* Map Range needs extra logic because there it involves two sequential caps. */
i++;
R_UNLESS(i < kac_count, ldr::ResultInvalidCapabilityMapRange());
R_UNLESS(CapabilityMapRange::Decode(cap).IsValid(kac[i], kacd, kacd_count), ldr::ResultInvalidCapabilityMapRange());
2019-06-26 23:46:19 +01:00
}
break;
default:
R_UNLESS(id == CapabilityId::Empty, ldr::ResultUnknownCapability());
2019-06-26 23:46:19 +01:00
break;
}
#undef VALIDATE_CASE
}
2022-03-26 07:14:36 +00:00
R_SUCCEED();
2019-06-26 23:46:19 +01:00
}
u16 MakeProgramInfoFlag(const util::BitPack32 *kac, size_t count) {
2019-06-26 23:46:19 +01:00
u16 flags = 0;
for (size_t i = 0; i < count; ++i) {
const auto cap = kac[i];
2019-06-26 23:46:19 +01:00
switch (GetCapabilityId(cap)) {
2019-06-26 23:46:19 +01:00
case CapabilityId::ApplicationType:
{
const auto app_type = CapabilityApplicationType::Decode(cap).GetApplicationType() & ProgramInfoFlag_ApplicationTypeMask;
2019-06-26 23:46:19 +01:00
if (app_type != ProgramInfoFlag_InvalidType) {
flags |= app_type;
}
}
break;
case CapabilityId::DebugFlags:
if (CapabilityDebugFlags::Decode(cap).GetAllowDebug()) {
2019-06-26 23:46:19 +01:00
flags |= ProgramInfoFlag_AllowDebug;
}
break;
default:
break;
}
}
return flags;
}
void UpdateProgramInfoFlag(u16 flags, util::BitPack32 *kac, size_t count) {
for (size_t i = 0; i < count; ++i) {
const auto cap = kac[i];
switch (GetCapabilityId(cap)) {
2019-06-26 23:46:19 +01:00
case CapabilityId::ApplicationType:
kac[i] = CapabilityApplicationType::Encode(flags & ProgramInfoFlag_ApplicationTypeMask);
2019-06-26 23:46:19 +01:00
break;
case CapabilityId::DebugFlags:
kac[i] = CapabilityDebugFlags::Encode((flags & ProgramInfoFlag_AllowDebug) != 0, CapabilityDebugFlags::Decode(cap).GetForceDebug());
2019-06-26 23:46:19 +01:00
break;
default:
break;
}
}
}
void PreProcessCapability(util::BitPack32 *kac, size_t count) {
for (size_t i = 0; i < count; ++i) {
const auto cap = kac[i];
switch (GetCapabilityId(cap)) {
/* NOTE: Currently, there is no pre-processing necessary. */
default:
break;
}
}
}
2019-06-26 23:46:19 +01:00
}