2020-02-19 09:22:27 +00:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-02-19 09:22:27 +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 {
|
|
|
|
|
|
|
|
Result KCapabilities::Initialize(const u32 *caps, s32 num_caps, KProcessPageTable *page_table) {
|
2020-02-19 12:55:00 +00:00
|
|
|
/* We're initializing an initial process. */
|
2021-10-23 23:25:20 +01:00
|
|
|
m_svc_access_flags.Reset();
|
|
|
|
m_irq_access_flags.Reset();
|
|
|
|
m_debug_capabilities = {0};
|
|
|
|
m_handle_table_size = 0;
|
|
|
|
m_intended_kernel_version = {0};
|
|
|
|
m_program_type = 0;
|
2020-02-19 12:55:00 +00:00
|
|
|
|
|
|
|
/* Initial processes may run on all cores. */
|
2021-02-05 22:59:03 +00:00
|
|
|
m_core_mask = cpu::VirtualCoreMask;
|
2020-02-19 12:55:00 +00:00
|
|
|
|
|
|
|
/* Initial processes may use any user priority they like. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_priority_mask = ~0xFul;
|
2020-02-19 12:55:00 +00:00
|
|
|
|
2020-08-18 12:03:01 +01:00
|
|
|
/* Here, Nintendo sets the kernel version to the current kernel version. */
|
|
|
|
/* We will follow suit and set the version to the highest supported kernel version. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_intended_kernel_version.Set<KernelVersion::MajorVersion>(ams::svc::SupportedKernelMajorVersion);
|
|
|
|
m_intended_kernel_version.Set<KernelVersion::MinorVersion>(ams::svc::SupportedKernelMinorVersion);
|
2020-02-19 12:55:00 +00:00
|
|
|
|
|
|
|
/* Parse the capabilities array. */
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(this->SetCapabilities(caps, num_caps, page_table));
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 06:13:16 +01:00
|
|
|
Result KCapabilities::Initialize(svc::KUserPointer<const u32 *> user_caps, s32 num_caps, KProcessPageTable *page_table) {
|
|
|
|
/* We're initializing a user process. */
|
2021-10-23 23:25:20 +01:00
|
|
|
m_svc_access_flags.Reset();
|
|
|
|
m_irq_access_flags.Reset();
|
|
|
|
m_debug_capabilities = {0};
|
|
|
|
m_handle_table_size = 0;
|
|
|
|
m_intended_kernel_version = {0};
|
|
|
|
m_program_type = 0;
|
|
|
|
|
|
|
|
/* User processes must specify what cores/priorities they can use. */
|
|
|
|
m_core_mask = 0;
|
|
|
|
m_priority_mask = 0;
|
2020-07-22 06:13:16 +01:00
|
|
|
|
|
|
|
/* Parse the user capabilities array. */
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(this->SetCapabilities(user_caps, num_caps, page_table));
|
2020-07-22 06:13:16 +01:00
|
|
|
}
|
|
|
|
|
2020-02-19 12:55:00 +00:00
|
|
|
Result KCapabilities::SetCorePriorityCapability(const util::BitPack32 cap) {
|
|
|
|
/* We can't set core/priority if we've already set them. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_UNLESS(m_core_mask == 0, svc::ResultInvalidArgument());
|
|
|
|
R_UNLESS(m_priority_mask == 0, svc::ResultInvalidArgument());
|
2020-02-19 12:55:00 +00:00
|
|
|
|
|
|
|
/* 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>();
|
|
|
|
|
2021-02-05 22:59:03 +00:00
|
|
|
R_UNLESS(min_core <= max_core, svc::ResultInvalidCombination());
|
|
|
|
R_UNLESS(min_prio <= max_prio, svc::ResultInvalidCombination());
|
|
|
|
R_UNLESS(max_core < cpu::NumVirtualCores, svc::ResultInvalidCoreId());
|
2020-02-19 12:55:00 +00:00
|
|
|
|
|
|
|
MESOSPHERE_ASSERT(max_prio < BITSIZEOF(u64));
|
|
|
|
|
|
|
|
/* Set core mask. */
|
|
|
|
for (auto core_id = min_core; core_id <= max_core; core_id++) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_core_mask |= (1ul << core_id);
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
2021-02-05 22:59:03 +00:00
|
|
|
MESOSPHERE_ASSERT((m_core_mask & cpu::VirtualCoreMask) == m_core_mask);
|
2020-02-19 12:55:00 +00:00
|
|
|
|
|
|
|
/* Set priority mask. */
|
|
|
|
for (auto prio = min_prio; prio <= max_prio; prio++) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_priority_mask |= (1ul << prio);
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We must have some core/priority we can use. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_UNLESS(m_core_mask != 0, svc::ResultInvalidArgument());
|
|
|
|
R_UNLESS(m_priority_mask != 0, svc::ResultInvalidArgument());
|
2020-02-19 12:55:00 +00:00
|
|
|
|
2021-10-19 09:41:23 +01:00
|
|
|
/* Processes must not have access to kernel thread priorities. */
|
|
|
|
R_UNLESS((m_priority_mask & 0xF) == 0, svc::ResultInvalidArgument());
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KCapabilities::MapRange(const util::BitPack32 cap, const util::BitPack32 size_cap, KProcessPageTable *page_table) {
|
2021-10-22 17:33:53 +01:00
|
|
|
/* Get/validate address/size */
|
|
|
|
#if defined(MESOSPHERE_ENABLE_LARGE_PHYSICAL_ADDRESS_CAPABILITIES)
|
|
|
|
const u64 phys_addr = static_cast<u64>(cap.Get<MapRange::Address>() | (size_cap.Get<MapRangeSize::AddressHigh>() << MapRange::Address::Count)) * PageSize;
|
|
|
|
#else
|
|
|
|
const u64 phys_addr = static_cast<u64>(cap.Get<MapRange::Address>()) * PageSize;
|
|
|
|
|
2020-02-19 12:55:00 +00:00
|
|
|
/* Validate reserved bits are unused. */
|
|
|
|
R_UNLESS(size_cap.Get<MapRangeSize::Reserved>() == 0, svc::ResultOutOfRange());
|
2021-10-22 17:33:53 +01:00
|
|
|
#endif
|
2020-02-19 12:55:00 +00:00
|
|
|
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>()) {
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(page_table->MapStatic(phys_addr, size, perm));
|
2020-02-19 12:55:00 +00:00
|
|
|
} else {
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(page_table->MapIo(phys_addr, size, perm));
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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. */
|
2022-02-14 22:45:32 +00:00
|
|
|
R_RETURN(page_table->MapIo(phys_addr, size, KMemoryPermission_UserReadWrite));
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2020-08-17 22:20:24 +01:00
|
|
|
break;
|
2020-02-19 12:55:00 +00:00
|
|
|
default:
|
2022-02-14 22:45:32 +00:00
|
|
|
R_THROW(svc::ResultNotFound());
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2020-07-14 11:26:02 +01:00
|
|
|
R_UNLESS(this->SetInterruptPermitted(ids[i]), svc::ResultOutOfRange());
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KCapabilities::SetProgramTypeCapability(const util::BitPack32 cap) {
|
|
|
|
/* Validate. */
|
|
|
|
R_UNLESS(cap.Get<ProgramType::Reserved>() == 0, svc::ResultReservedUsed());
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_program_type = cap.Get<ProgramType::Type>();
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KCapabilities::SetKernelVersionCapability(const util::BitPack32 cap) {
|
|
|
|
/* Ensure we haven't set our version before. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_UNLESS(m_intended_kernel_version.Get<KernelVersion::MajorVersion>() == 0, svc::ResultInvalidArgument());
|
2020-02-19 12:55:00 +00:00
|
|
|
|
|
|
|
/* Set, ensure that we set a valid version. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_intended_kernel_version = cap;
|
|
|
|
R_UNLESS(m_intended_kernel_version.Get<KernelVersion::MajorVersion>() != 0, svc::ResultInvalidArgument());
|
2020-02-19 12:55:00 +00:00
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KCapabilities::SetHandleTableCapability(const util::BitPack32 cap) {
|
|
|
|
/* Validate. */
|
|
|
|
R_UNLESS(cap.Get<HandleTable::Reserved>() == 0, svc::ResultReservedUsed());
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_handle_table_size = cap.Get<HandleTable::Size>();
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KCapabilities::SetDebugFlagsCapability(const util::BitPack32 cap) {
|
|
|
|
/* Validate. */
|
|
|
|
R_UNLESS(cap.Get<DebugFlags::Reserved>() == 0, svc::ResultReservedUsed());
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_debug_capabilities.Set<DebugFlags::AllowDebug>(cap.Get<DebugFlags::AllowDebug>());
|
|
|
|
m_debug_capabilities.Set<DebugFlags::ForceDebug>(cap.Get<DebugFlags::ForceDebug>());
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2020-03-08 08:06:23 +00:00
|
|
|
|
|
|
|
/* If the type is padding, we have no work to do. */
|
|
|
|
R_SUCCEED_IF(type == CapabilityType::Padding);
|
2020-02-19 12:55:00 +00:00
|
|
|
|
|
|
|
/* 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) {
|
2022-02-14 22:45:32 +00:00
|
|
|
case CapabilityType::CorePriority: R_RETURN(this->SetCorePriorityCapability(cap));
|
|
|
|
case CapabilityType::SyscallMask: R_RETURN(this->SetSyscallMaskCapability(cap, set_svc));
|
|
|
|
case CapabilityType::MapIoPage: R_RETURN(this->MapIoPage(cap, page_table));
|
|
|
|
case CapabilityType::MapRegion: R_RETURN(this->MapRegion(cap, page_table));
|
|
|
|
case CapabilityType::InterruptPair: R_RETURN(this->SetInterruptPairCapability(cap));
|
|
|
|
case CapabilityType::ProgramType: R_RETURN(this->SetProgramTypeCapability(cap));
|
|
|
|
case CapabilityType::KernelVersion: R_RETURN(this->SetKernelVersionCapability(cap));
|
|
|
|
case CapabilityType::HandleTable: R_RETURN(this->SetHandleTableCapability(cap));
|
|
|
|
case CapabilityType::DebugFlags: R_RETURN(this->SetDebugFlagsCapability(cap));
|
|
|
|
default: R_THROW(svc::ResultInvalidArgument());
|
2020-02-19 12:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-02-19 09:22:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 06:13:16 +01:00
|
|
|
Result KCapabilities::SetCapabilities(svc::KUserPointer<const u32 *> user_caps, s32 num_caps, KProcessPageTable *page_table) {
|
|
|
|
u32 set_flags = 0, set_svc = 0;
|
|
|
|
|
|
|
|
for (s32 i = 0; i < num_caps; i++) {
|
|
|
|
/* Read the cap from userspace. */
|
|
|
|
u32 cap0;
|
|
|
|
R_TRY(user_caps.CopyArrayElementTo(std::addressof(cap0), i));
|
|
|
|
|
|
|
|
const util::BitPack32 cap = { cap0 };
|
|
|
|
if (GetCapabilityType(cap) == CapabilityType::MapRange) {
|
|
|
|
/* Check that the pair cap exists. */
|
|
|
|
R_UNLESS((++i) < num_caps, svc::ResultInvalidCombination());
|
|
|
|
|
|
|
|
/* Read the second cap from userspace. */
|
|
|
|
u32 cap1;
|
|
|
|
R_TRY(user_caps.CopyArrayElementTo(std::addressof(cap1), i));
|
|
|
|
|
|
|
|
/* Check the pair cap is a map range cap. */
|
|
|
|
const util::BitPack32 size_cap = { cap1 };
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-07-22 06:13:16 +01:00
|
|
|
}
|
|
|
|
|
2020-02-19 09:22:27 +00:00
|
|
|
}
|