kern: implement capabilities parsing

This commit is contained in:
Michael Scire 2020-02-19 04:55:00 -08:00
parent 0534ddd37a
commit 905ce0eeea
9 changed files with 483 additions and 3 deletions

View file

@ -213,6 +213,11 @@ namespace ams::kern::arch::arm64 {
this->gicc->eoir = irq;
}
bool IsInterruptDefined(s32 irq) {
const s32 num_interrupts = std::min(32 + 32 * (this->gicd->typer & 0x1F), static_cast<u32>(NumInterrupts));
return (0 <= irq && irq < num_interrupts);
}
/* TODO: Implement more KInterruptController functionality. */
public:
static constexpr ALWAYS_INLINE bool IsSoftware(s32 id) {

View file

@ -67,6 +67,10 @@ namespace ams::kern::arch::arm64 {
NOINLINE void Initialize(s32 core_id);
NOINLINE void Finalize(s32 core_id);
bool IsInterruptDefined(s32 irq) {
return this->interrupt_controller.IsInterruptDefined(irq);
}
NOINLINE Result BindHandler(KInterruptHandler *handler, s32 irq, s32 core_id, s32 priority, bool manual_clear, bool level);
NOINLINE Result UnbindHandler(s32 irq, s32 core);

View file

@ -31,6 +31,18 @@ namespace ams::kern::arch::arm64 {
void Finalize() { this->page_table.Finalize(); }
Result MapIo(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm) {
return this->page_table.MapIo(phys_addr, size, perm);
}
Result MapStatic(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm) {
return this->page_table.MapStatic(phys_addr, size, perm);
}
Result MapRegion(KMemoryRegionType region_type, KMemoryPermission perm) {
return this->page_table.MapRegion(region_type, perm);
}
Result MapPageGroup(KProcessAddress addr, const KPageGroup &pg, KMemoryState state, KMemoryPermission perm) {
return this->page_table.MapPageGroup(addr, pg, state, perm);
}

View file

@ -24,6 +24,180 @@ namespace ams::kern {
private:
static constexpr size_t SvcFlagCount = svc::NumSupervisorCalls / BITSIZEOF(u8);
static constexpr size_t IrqFlagCount = /* TODO */0x80;
enum class CapabilityType : u32 {
CorePriority = (1u << 3) - 1,
SyscallMask = (1u << 4) - 1,
MapRange = (1u << 6) - 1,
MapIoPage = (1u << 7) - 1,
MapRegion = (1u << 10) - 1,
InterruptPair = (1u << 11) - 1,
ProgramType = (1u << 13) - 1,
KernelVersion = (1u << 14) - 1,
HandleTable = (1u << 15) - 1,
DebugFlags = (1u << 16) - 1,
Invalid = 0u,
Padding = ~0u,
};
using RawCapabilityValue = util::BitPack32::Field<0, BITSIZEOF(util::BitPack32), u32>;
static constexpr CapabilityType GetCapabilityType(const util::BitPack32 cap) {
const u32 value = cap.Get<RawCapabilityValue>();
return static_cast<CapabilityType>((~value & (value + 1)) - 1);
}
static constexpr u32 GetCapabilityFlag(CapabilityType type) {
return static_cast<u32>(type) + 1;
}
static constexpr u32 CountTrailingZero(u32 flag) {
for (u32 i = 0; i < BITSIZEOF(u32); i++) {
if (flag & (1u << i)) {
return i;
}
}
return BITSIZEOF(u32);
}
static constexpr u32 GetCapabilityId(CapabilityType type) {
const u32 flag = GetCapabilityFlag(type);
if (true /* C++20: std::is_constant_evaluated() */) {
return CountTrailingZero(flag);
} else {
return static_cast<u32>(__builtin_ctz(flag));
}
}
template<size_t Index, size_t Count, typename T = u32>
using Field = util::BitPack32::Field<Index, Count, T>;
#define DEFINE_FIELD(name, prev, ...) using name = Field<prev::Next, __VA_ARGS__>
template<CapabilityType Type>
static constexpr inline u32 CapabilityFlag = []() -> u32 {
return static_cast<u32>(Type) + 1;
}();
template<CapabilityType Type>
static constexpr inline u32 CapabilityId = []() -> u32 {
const u32 flag = static_cast<u32>(Type) + 1;
if (true /* C++20: std::is_constant_evaluated() */) {
for (u32 i = 0; i < BITSIZEOF(u32); i++) {
if (flag & (1u << i)) {
return i;
}
}
return BITSIZEOF(u32);
} else {
return __builtin_ctz(flag);
}
}();
struct CorePriority {
using IdBits = Field<0, CapabilityId<CapabilityType::CorePriority> + 1>;
DEFINE_FIELD(LowestThreadPriority, IdBits, 6);
DEFINE_FIELD(HighestThreadPriority, LowestThreadPriority, 6);
DEFINE_FIELD(MinimumCoreId, HighestThreadPriority, 8);
DEFINE_FIELD(MaximumCoreId, MinimumCoreId, 8);
};
struct SyscallMask {
using IdBits = Field<0, CapabilityId<CapabilityType::SyscallMask> + 1>;
DEFINE_FIELD(Mask, IdBits, 24);
DEFINE_FIELD(Index, Mask, 3);
};
static constexpr u64 PhysicalMapAllowedMask = (1ul << 36) - 1;
struct MapRange {
using IdBits = Field<0, CapabilityId<CapabilityType::MapRange> + 1>;
DEFINE_FIELD(Address, IdBits, 24);
DEFINE_FIELD(ReadOnly, Address, 1, bool);
};
struct MapRangeSize {
using IdBits = Field<0, CapabilityId<CapabilityType::MapRange> + 1>;
DEFINE_FIELD(Pages, IdBits, 20);
DEFINE_FIELD(Reserved, Pages, 4);
DEFINE_FIELD(Normal, Reserved, 1, bool);
};
struct MapIoPage {
using IdBits = Field<0, CapabilityId<CapabilityType::MapIoPage> + 1>;
DEFINE_FIELD(Address, IdBits, 24);
};
enum class RegionType : u32 {
None = 0,
KernelTraceBuffer = 1,
OnMemoryBootImage = 2,
DTB = 3,
};
struct MapRegion {
using IdBits = Field<0, CapabilityId<CapabilityType::MapRegion> + 1>;
DEFINE_FIELD(Region0, IdBits, 6, RegionType);
DEFINE_FIELD(ReadOnly0, Region0, 1, bool);
DEFINE_FIELD(Region1, ReadOnly0, 6, RegionType);
DEFINE_FIELD(ReadOnly1, Region1, 1, bool);
DEFINE_FIELD(Region2, ReadOnly1, 6, RegionType);
DEFINE_FIELD(ReadOnly2, Region2, 1, bool);
};
static const u32 PaddingInterruptId = 0x3FF;
struct InterruptPair {
using IdBits = Field<0, CapabilityId<CapabilityType::InterruptPair> + 1>;
DEFINE_FIELD(InterruptId0, IdBits, 10);
DEFINE_FIELD(InterruptId1, InterruptId0, 10);
};
struct ProgramType {
using IdBits = Field<0, CapabilityId<CapabilityType::ProgramType> + 1>;
DEFINE_FIELD(Type, IdBits, 3);
DEFINE_FIELD(Reserved, Type, 15);
};
struct KernelVersion {
using IdBits = Field<0, CapabilityId<CapabilityType::KernelVersion> + 1>;
DEFINE_FIELD(MinorVersion, IdBits, 4);
DEFINE_FIELD(MajorVersion, MinorVersion, 13);
};
struct HandleTable {
using IdBits = Field<0, CapabilityId<CapabilityType::HandleTable> + 1>;
DEFINE_FIELD(Size, IdBits, 10);
DEFINE_FIELD(Reserved, Size, 6);
};
struct DebugFlags {
using IdBits = Field<0, CapabilityId<CapabilityType::HandleTable> + 1>;
DEFINE_FIELD(AllowDebug, IdBits, 1, bool);
DEFINE_FIELD(ForceDebug, AllowDebug, 1, bool);
DEFINE_FIELD(Reserved, ForceDebug, 13);
};
#undef DEFINE_FIELD
static constexpr u32 InitializeOnceFlags = CapabilityFlag<CapabilityType::CorePriority> |
CapabilityFlag<CapabilityType::ProgramType> |
CapabilityFlag<CapabilityType::KernelVersion> |
CapabilityFlag<CapabilityType::HandleTable> |
CapabilityFlag<CapabilityType::DebugFlags>;
private:
u8 svc_access_flags[SvcFlagCount]{};
u8 irq_access_flags[IrqFlagCount]{};
@ -33,6 +207,40 @@ namespace ams::kern {
s32 handle_table_size{};
util::BitPack32 intended_kernel_version;
u32 program_type{};
private:
bool SetSvcAllowed(u32 id) {
constexpr size_t BitsPerWord = BITSIZEOF(this->svc_access_flags[0]);
if (id < BITSIZEOF(this->svc_access_flags)) {
this->svc_access_flags[id / BitsPerWord] = (1ul << (id % BitsPerWord));
return true;
} else {
return false;
}
}
bool SetInterruptAllowed(u32 id) {
constexpr size_t BitsPerWord = BITSIZEOF(this->irq_access_flags[0]);
if (id < BITSIZEOF(this->irq_access_flags)) {
this->irq_access_flags[id / BitsPerWord] = (1ul << (id % BitsPerWord));
return true;
} else {
return false;
}
}
Result SetCorePriorityCapability(const util::BitPack32 cap);
Result SetSyscallMaskCapability(const util::BitPack32 cap, u32 &set_svc);
Result MapRange(const util::BitPack32 cap, const util::BitPack32 size_cap, KProcessPageTable *page_table);
Result MapIoPage(const util::BitPack32 cap, KProcessPageTable *page_table);
Result MapRegion(const util::BitPack32 cap, KProcessPageTable *page_table);
Result SetInterruptPairCapability(const util::BitPack32 cap);
Result SetProgramTypeCapability(const util::BitPack32 cap);
Result SetKernelVersionCapability(const util::BitPack32 cap);
Result SetHandleTableCapability(const util::BitPack32 cap);
Result SetDebugFlagsCapability(const util::BitPack32 cap);
Result SetCapability(const util::BitPack32 cap, u32 &set_flags, u32 &set_svc, KProcessPageTable *page_table);
Result SetCapabilities(const u32 *caps, s32 num_caps, KProcessPageTable *page_table);
public:
constexpr KCapabilities() : debug_capabilities(0), intended_kernel_version(0) { /* ... */ }

View file

@ -240,6 +240,10 @@ namespace ams::kern {
return this->GetImpl().GetPhysicalAddress(out, virt_addr);
}
Result MapIo(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm);
Result MapStatic(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm);
Result MapRegion(KMemoryRegionType region_type, KMemoryPermission perm);
Result MapPages(KProcessAddress *out_addr, size_t num_pages, size_t alignment, KPhysicalAddress phys_addr, KProcessAddress region_start, size_t region_num_pages, KMemoryState state, KMemoryPermission perm) {
return this->MapPages(out_addr, num_pages, alignment, phys_addr, true, region_start, region_num_pages, state, perm);
}

View file

@ -55,7 +55,7 @@ namespace ams::kern::svc {
/* 123 */ using ::ams::svc::ResultSessionClosed;
/* 124 */ using ::ams::svc::ResultNotHandled;
/* 125 */ using ::ams::svc::ResultInvalidState;
/* 126 */ using ::ams::svc::ResultReservedValue;
/* 126 */ using ::ams::svc::ResultReservedUsed;
/* 127 */ using ::ams::svc::ResultNotSupported;
/* 128 */ using ::ams::svc::ResultDebug;
/* 129 */ using ::ams::svc::ResultThreadNotOwned;

View file

@ -18,7 +18,242 @@
namespace ams::kern {
Result KCapabilities::Initialize(const u32 *caps, s32 num_caps, KProcessPageTable *page_table) {
MESOSPHERE_TODO_IMPLEMENT();
/* We're initializing an initial process. */
/* Most fields have already been cleared by our constructor. */
/* Initial processes may run on all cores. */
this->core_mask = (1ul << cpu::NumCores) - 1;
/* Initial processes may use any user priority they like. */
this->priority_mask = ~0xFul;
/* TODO: Here, Nintendo sets the kernel version to (current kernel version). */
/* How should we handle this? Not a MESOSPHERE_TODO because it's not critical. */
/* Parse the capabilities array. */
return this->SetCapabilities(caps, num_caps, page_table);
}
Result KCapabilities::SetCorePriorityCapability(const util::BitPack32 cap) {
/* We can't set core/priority if we've already set them. */
R_UNLESS(this->core_mask == 0, svc::ResultInvalidArgument());
R_UNLESS(this->priority_mask == 0, svc::ResultInvalidArgument());
/* Validate the core/priority. */
const auto min_core = cap.Get<CorePriority::MinimumCoreId>();
const auto max_core = cap.Get<CorePriority::MaximumCoreId>();
const auto max_prio = cap.Get<CorePriority::LowestThreadPriority>();
const auto min_prio = cap.Get<CorePriority::HighestThreadPriority>();
R_UNLESS(min_core <= max_core, svc::ResultInvalidCombination());
R_UNLESS(min_prio <= max_prio, svc::ResultInvalidCombination());
R_UNLESS(max_core < cpu::NumCores, svc::ResultInvalidCoreId());
MESOSPHERE_ASSERT(max_core < BITSIZEOF(u64));
MESOSPHERE_ASSERT(max_prio < BITSIZEOF(u64));
/* Set core mask. */
for (auto core_id = min_core; core_id <= max_core; core_id++) {
this->core_mask |= (1ul << core_id);
}
MESOSPHERE_ASSERT((this->core_mask & ((1ul << cpu::NumCores) - 1)) == this->core_mask);
/* Set priority mask. */
for (auto prio = min_prio; prio <= max_prio; prio++) {
this->priority_mask |= (1ul << prio);
}
/* We must have some core/priority we can use. */
R_UNLESS(this->core_mask != 0, svc::ResultInvalidArgument());
R_UNLESS(this->priority_mask != 0, svc::ResultInvalidArgument());
return ResultSuccess();
}
Result KCapabilities::SetSyscallMaskCapability(const util::BitPack32 cap, u32 &set_svc) {
/* Validate the index. */
const auto mask = cap.Get<SyscallMask::Mask>();
const auto index = cap.Get<SyscallMask::Index>();
const u32 index_flag = (1u << index);
R_UNLESS((set_svc & index_flag) == 0, svc::ResultInvalidCombination());
set_svc |= index_flag;
/* Set SVCs. */
for (size_t i = 0; i < SyscallMask::Mask::Count; i++) {
const u32 svc_id = SyscallMask::Mask::Count * index + i;
if (mask & (1u << i)) {
R_UNLESS(this->SetSvcAllowed(svc_id), svc::ResultOutOfRange());
}
}
return ResultSuccess();
}
Result KCapabilities::MapRange(const util::BitPack32 cap, const util::BitPack32 size_cap, KProcessPageTable *page_table) {
/* Validate reserved bits are unused. */
R_UNLESS(size_cap.Get<MapRangeSize::Reserved>() == 0, svc::ResultOutOfRange());
/* Get/validate address/size */
const u64 phys_addr = cap.Get<MapRange::Address>() * PageSize;
const size_t num_pages = size_cap.Get<MapRangeSize::Pages>();
const size_t size = num_pages * PageSize;
R_UNLESS(phys_addr == GetInteger(KPhysicalAddress(phys_addr)), svc::ResultInvalidAddress());
R_UNLESS(num_pages != 0, svc::ResultInvalidSize());
R_UNLESS(phys_addr < phys_addr + size, svc::ResultInvalidAddress());
R_UNLESS(((phys_addr + size - 1) & ~PhysicalMapAllowedMask) == 0, svc::ResultInvalidAddress());
/* Do the mapping. */
const KMemoryPermission perm = cap.Get<MapRange::ReadOnly>() ? KMemoryPermission_UserRead : KMemoryPermission_UserReadWrite;
if (size_cap.Get<MapRangeSize::Normal>()) {
return page_table->MapStatic(phys_addr, size, perm);
} else {
return page_table->MapIo(phys_addr, size, perm);
}
}
Result KCapabilities::MapIoPage(const util::BitPack32 cap, KProcessPageTable *page_table) {
/* Get/validate address/size */
const u64 phys_addr = cap.Get<MapIoPage::Address>() * PageSize;
const size_t num_pages = 1;
const size_t size = num_pages * PageSize;
R_UNLESS(phys_addr == GetInteger(KPhysicalAddress(phys_addr)), svc::ResultInvalidAddress());
R_UNLESS(num_pages != 0, svc::ResultInvalidSize());
R_UNLESS(phys_addr < phys_addr + size, svc::ResultInvalidAddress());
R_UNLESS(((phys_addr + size - 1) & ~PhysicalMapAllowedMask) == 0, svc::ResultInvalidAddress());
/* Do the mapping. */
return page_table->MapIo(phys_addr, size, KMemoryPermission_UserReadWrite);
}
Result KCapabilities::MapRegion(const util::BitPack32 cap, KProcessPageTable *page_table) {
/* Define the allowed memory regions. */
constexpr KMemoryRegionType MemoryRegions[] = {
KMemoryRegionType_None,
KMemoryRegionType_KernelTraceBuffer,
KMemoryRegionType_OnMemoryBootImage,
KMemoryRegionType_DTB,
};
/* Extract regions/read only. */
const RegionType types[3] = { cap.Get<MapRegion::Region0>(), cap.Get<MapRegion::Region1>(), cap.Get<MapRegion::Region2>(), };
const bool ro[3] = { cap.Get<MapRegion::ReadOnly0>(), cap.Get<MapRegion::ReadOnly1>(), cap.Get<MapRegion::ReadOnly2>(), };
for (size_t i = 0; i < util::size(types); i++) {
const auto type = types[i];
const auto perm = ro[i] ? KMemoryPermission_UserRead : KMemoryPermission_UserReadWrite;
switch (type) {
case RegionType::None:
break;
case RegionType::KernelTraceBuffer:
case RegionType::OnMemoryBootImage:
case RegionType::DTB:
R_TRY(page_table->MapRegion(MemoryRegions[static_cast<u32>(type)], perm));
default:
return svc::ResultNotFound();
}
}
return ResultSuccess();
}
Result KCapabilities::SetInterruptPairCapability(const util::BitPack32 cap) {
/* Extract interrupts. */
const u32 ids[2] = { cap.Get<InterruptPair::InterruptId0>(), cap.Get<InterruptPair::InterruptId1>(), };
for (size_t i = 0; i < util::size(ids); i++) {
if (ids[i] != PaddingInterruptId) {
R_UNLESS(Kernel::GetInterruptManager().IsInterruptDefined(ids[i]), svc::ResultOutOfRange());
R_UNLESS(this->SetInterruptAllowed(ids[i]), svc::ResultOutOfRange());
}
}
return ResultSuccess();
}
Result KCapabilities::SetProgramTypeCapability(const util::BitPack32 cap) {
/* Validate. */
R_UNLESS(cap.Get<ProgramType::Reserved>() == 0, svc::ResultReservedUsed());
this->program_type = cap.Get<ProgramType::Type>();
return ResultSuccess();
}
Result KCapabilities::SetKernelVersionCapability(const util::BitPack32 cap) {
/* Ensure we haven't set our version before. */
R_UNLESS(this->intended_kernel_version.Get<KernelVersion::MajorVersion>() == 0, svc::ResultInvalidArgument());
/* Set, ensure that we set a valid version. */
this->intended_kernel_version = cap;
R_UNLESS(this->intended_kernel_version.Get<KernelVersion::MajorVersion>() != 0, svc::ResultInvalidArgument());
return ResultSuccess();
}
Result KCapabilities::SetHandleTableCapability(const util::BitPack32 cap) {
/* Validate. */
R_UNLESS(cap.Get<HandleTable::Reserved>() == 0, svc::ResultReservedUsed());
this->handle_table_size = cap.Get<HandleTable::Size>();
return ResultSuccess();
}
Result KCapabilities::SetDebugFlagsCapability(const util::BitPack32 cap) {
/* Validate. */
R_UNLESS(cap.Get<DebugFlags::Reserved>() == 0, svc::ResultReservedUsed());
this->debug_capabilities.Set<DebugFlags::AllowDebug>(cap.Get<DebugFlags::AllowDebug>());
this->debug_capabilities.Set<DebugFlags::ForceDebug>(cap.Get<DebugFlags::ForceDebug>());
return ResultSuccess();
}
Result KCapabilities::SetCapability(const util::BitPack32 cap, u32 &set_flags, u32 &set_svc, KProcessPageTable *page_table) {
/* Validate this is a capability we can act on. */
const auto type = GetCapabilityType(cap);
R_UNLESS(type != CapabilityType::Invalid, svc::ResultInvalidArgument());
R_UNLESS(type != CapabilityType::Padding, ResultSuccess());
/* Check that we haven't already processed this capability. */
const auto flag = GetCapabilityFlag(type);
R_UNLESS(((set_flags & InitializeOnceFlags) & flag) == 0, svc::ResultInvalidCombination());
set_flags |= flag;
/* Process the capability. */
switch (type) {
case CapabilityType::CorePriority: return this->SetCorePriorityCapability(cap);
case CapabilityType::SyscallMask: return this->SetSyscallMaskCapability(cap, set_svc);
case CapabilityType::MapIoPage: return this->MapIoPage(cap, page_table);
case CapabilityType::MapRegion: return this->MapRegion(cap, page_table);
case CapabilityType::InterruptPair: return this->SetInterruptPairCapability(cap);
case CapabilityType::ProgramType: return this->SetProgramTypeCapability(cap);
case CapabilityType::KernelVersion: return this->SetKernelVersionCapability(cap);
case CapabilityType::HandleTable: return this->SetHandleTableCapability(cap);
case CapabilityType::DebugFlags: return this->SetDebugFlagsCapability(cap);
default: return svc::ResultInvalidArgument();
}
}
Result KCapabilities::SetCapabilities(const u32 *caps, s32 num_caps, KProcessPageTable *page_table) {
u32 set_flags = 0, set_svc = 0;
for (s32 i = 0; i < num_caps; i++) {
const util::BitPack32 cap = { caps[i] };
if (GetCapabilityType(cap) == CapabilityType::MapRange) {
/* Check that the pair cap exists. */
R_UNLESS((++i) < num_caps, svc::ResultInvalidCombination());
/* Check the pair cap is a map range cap. */
const util::BitPack32 size_cap = { caps[i] };
R_UNLESS(GetCapabilityType(size_cap) == CapabilityType::MapRange, svc::ResultInvalidCombination());
/* Map the range. */
R_TRY(this->MapRange(cap, size_cap, page_table));
} else {
R_TRY(this->SetCapability(cap, set_flags, set_svc, page_table));
}
}
return ResultSuccess();
}
}

View file

@ -629,6 +629,18 @@ namespace ams::kern {
return cur_block_address == GetHeapVirtualAddress(cur_addr) && cur_block_pages == (cur_size / PageSize);
}
Result KPageTableBase::MapIo(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm) {
MESOSPHERE_TODO_IMPLEMENT();
}
Result KPageTableBase::MapStatic(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm) {
MESOSPHERE_TODO_IMPLEMENT();
}
Result KPageTableBase::MapRegion(KMemoryRegionType region_type, KMemoryPermission perm) {
MESOSPHERE_TODO_IMPLEMENT();
}
Result KPageTableBase::MapPages(KProcessAddress *out_addr, size_t num_pages, size_t alignment, KPhysicalAddress phys_addr, bool is_pa_valid, KProcessAddress region_start, size_t region_num_pages, KMemoryState state, KMemoryPermission perm) {
MESOSPHERE_ASSERT(util::IsAligned(alignment, PageSize) && alignment >= PageSize);

View file

@ -58,7 +58,7 @@ namespace ams::svc {
R_DEFINE_ERROR_RESULT(SessionClosed, 123);
R_DEFINE_ERROR_RESULT(NotHandled, 124);
R_DEFINE_ERROR_RESULT(InvalidState, 125);
R_DEFINE_ERROR_RESULT(ReservedValue, 126);
R_DEFINE_ERROR_RESULT(ReservedUsed, 126);
R_DEFINE_ERROR_RESULT(NotSupported, 127);
R_DEFINE_ERROR_RESULT(Debug, 128);
R_DEFINE_ERROR_RESULT(ThreadNotOwned, 129);