mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2024-12-28 21:26:14 +00:00
c7026b9094
* Implement NCM * Modernize ncm_main * Remove unnecessary smExit * Give access to svcCallSecureMonitor * Stack size bump * Fix incorrect setup for NandUser's content storage entry * Fix a potential data abort when flushing the placeholder accessor cache * Fix HasFile and HasDirectory * Use r+b, not w+b * Misc fixes * errno begone * Fixed more stdio error handling * More main fixes * Various command improvements * Make dispatch tables great again * Fix logic inversion * Fixed content path generation * Bump heap size, fix CleanupAllPlaceHolder * Various fixes. Note: This contains debug stuff which will be removed later. I was getting tired of having to cherrypick tiny changes * Fixed placeholder/content deletion * Fixed incorrect content manager destruction * Prevent automatic placeholder creation on open * Fixed List implementation. Also lots of debug logging. * Removed debug code * Added a scope guard for WritePlaceHolder * Manually prevent placeholder/content appending * Revert "Removed debug code" This reverts commitd6ff261fcc
. * Always cache placeholder file. Switch to ftell for preventing appending * Universally use EnsureEnabled * Abstract away file writing logic * Misc cleanup * Refactor placeholder cacheing * Remove debug code (again) * Revert "Remove debug code (again)" This reverts commit168447d80e
. * Misc changes * Fixed file modes * Fixed ContentId/PlaceHolderId alignment * Improved type safety * Fixed reinitialization * Fixed doubleup on path creation * Remove debug code * Fixed 1.0.0 booting * Correct amount of add on content * Correct main thread stack size * lr: Introducing registered data * Reorder stratosphere Makefile * Move results to libstrat * lr: Cleanup lr_redirection * lr: lr_manager tweaks * lr: Imrpoved path handling and adjust ResolveAddOnContentPath order * lr: Organise types * Add eof newlines * lr: Eliminate unnecessary vars * lr: Unnecessary vars 2 electric boogaloo * lr: Various helpers * lr: RegisteredLocationResolver helpers * ncm: Move ncm_types to libstrat * ncm: Misc cleanup * Implement NCM * Modernize ncm_main * Remove unnecessary smExit * Give access to svcCallSecureMonitor * Stack size bump * Fix incorrect setup for NandUser's content storage entry * Fix a potential data abort when flushing the placeholder accessor cache * Fix HasFile and HasDirectory * Use r+b, not w+b * Misc fixes * errno begone * Fixed more stdio error handling * More main fixes * Various command improvements * Make dispatch tables great again * Fix logic inversion * Fixed content path generation * Bump heap size, fix CleanupAllPlaceHolder * Various fixes. Note: This contains debug stuff which will be removed later. I was getting tired of having to cherrypick tiny changes * Fixed placeholder/content deletion * Fixed incorrect content manager destruction * Prevent automatic placeholder creation on open * Fixed List implementation. Also lots of debug logging. * Removed debug code * Added a scope guard for WritePlaceHolder * Manually prevent placeholder/content appending * Revert "Removed debug code" This reverts commitd6ff261fcc
. * Always cache placeholder file. Switch to ftell for preventing appending * Universally use EnsureEnabled * Abstract away file writing logic * Misc cleanup * Refactor placeholder cacheing * Remove debug code (again) * Revert "Remove debug code (again)" This reverts commit168447d80e
. * Misc changes * Fixed file modes * Fixed ContentId/PlaceHolderId alignment * Improved type safety * Fixed reinitialization * Fixed doubleup on path creation * Remove debug code * Fixed 1.0.0 booting * Correct amount of add on content * Correct main thread stack size * lr: Introducing registered data * Reorder stratosphere Makefile * Move results to libstrat * lr: Cleanup lr_redirection * lr: lr_manager tweaks * lr: Imrpoved path handling and adjust ResolveAddOnContentPath order * lr: Organise types * Add eof newlines * lr: Eliminate unnecessary vars * lr: Unnecessary vars 2 electric boogaloo * lr: Various helpers * lr: RegisteredLocationResolver helpers * ncm: Move ncm_types to libstrat * ncm: Misc cleanup * Updated AddOnContentLocationResolver and RegisteredLocationResolver to 9.0.0 * Finished updating lr to 9.0.0 * Updated NCM to 9.0.0 * Fix libstrat includes * Fixed application launching * title_id_2 -> owner_tid * Updated to new-ipc * Change to using pure virtuals * Title Id -> Program Id * Fixed compilation against master * std::scoped_lock<> -> std::scoped_lock * Adopted R_UNLESS and R_CONVERT * Prefix namespace to Results * Adopt std::numeric_limits * Fixed incorrect error handling in ReadFile * Adopted AMS_ABORT_UNLESS * Adopt util::GenerateUuid() * Syntax improvements * ncm_types: Address review * Address more review comments * Updated copyrights * Address more feedback * More feedback addressed * More changes * Move dispatch tables out of interface files * Addressed remaining comments * lr: move into libstratosphere * ncm: Fix logic inversion * lr: Add comments * lr: Remove whitespace * ncm: Start addressing feedback * ncm: Cleanup InitializeContentManager * lr: support client-side usage * lr_service -> lr_api * ncm: Begin refactoring content manager * ncm: More content manager improvements * ncm: Content manager mount improvements * ldr: use lr bindings * lr bindings usage: minor fixes * ncm/lr: Pointer placement * ncm: placeholder accessor cleanup * ncm: minor fixes * ncm: refactor rights cache * ncm: content meta database cleanup * ncm: move content meta database impl out of interface file * ncm: Use const ContentMetaKey & * ncm: fix other non-const ContentMetaKey references * ncm: content meta database cleanup * ncm: content storage fixes for 2.0.0 * ncm: add missing end of file newlines * ncm: implement ContentMetaReader * ncm: client-side api * ncm: trim trailing spaces * ncm: FS_MAX_PATH-1 -> fs::EntryNameLengthMax * ncm: Use PathString and Path * fs: implement accessor wrappers for ncm * fs: implement user fs wrappers * fs: add MountSdCard * ncm: move to content manager impl * ncm: fix up main * kvdb: use fs:: * fs: Add wrappers needed for ncm * ncm: use fs bindings, other refactoring * ncm: minor fixes * fsa: fix ReadFile without size output * fs: add substorage, rom path tool * ncm: fix dangling fsdev usage * fs: fix bug in Commit * fs: fixed incorrect mode check * fs: implement Mount(System)Data * ncm: don't delete hos * results: add R_SUCCEED_IF * ams-except-ncm: use R_SUCCEED_IF * ncm: added comments * ncm: fix api definitions * ncm: use R_SUCCEED_IF * pm: think of the savings * ncm: employ kernel strats * ncm: Nintendo has 5 MiB of heap. Give ourselves 4 to be safe, pending analysis * ncm: refactor IDs, split types header into many headers * ams.mitm: use fs bindings instead of stdio * fs: SystemData uses SystemDataId * ncm: improve meta-db accuracy * ncm: inline getlatestkey * fs: improve UnsupportedOperation results * fs: modernize mount utils * ams: misc fixes for merge-errors * fs: improve unsupportedoperation results * git subrepo pull emummc subrepo: subdir: "emummc" merged: "d12dd546" upstream: origin: "https://github.com/m4xw/emuMMC" branch: "develop" commit: "d12dd546" git-subrepo: version: "0.4.1" origin: "???" commit: "???" * util: add boundedmap * ncm: minor style fixes * ncm: don't unmount if mounting fails * lr: bug fixes * ncm: implement ncm.for-initialize + ncm.for-safemode * lr: ncm::ProgramId::Invalid -> ncm::InvalidProgramId * ncm: fix open directory mode on 1.0.0 * ncm: fix fs use, implement more of < 4.0.0 for-initialize/safemode * ncm: implement packagedcontent -> content for building metadb * ncm: fix save data flag management * ncm: address some review suggestions (thanks @leoetlino!) * updater: use fs bindings * fs: implement MountCode * fs: prefer make_unique to operator new * ncm: implement remaining ContentMetaDatabaseBuilder functionality Co-authored-by: Michael Scire <SciresM@gmail.com>
261 lines
12 KiB
C++
261 lines
12 KiB
C++
/*
|
|
* Copyright (c) 2018-2020 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 {
|
|
|
|
Result KCapabilities::Initialize(const u32 *caps, s32 num_caps, KProcessPageTable *page_table) {
|
|
/* 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());
|
|
|
|
/* If the type is padding, we have no work to do. */
|
|
R_SUCCEED_IF(type == CapabilityType::Padding);
|
|
|
|
/* 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();
|
|
}
|
|
|
|
}
|