mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-17 00:24:23 -04:00
kern: change terminology metadata -> management
This commit is contained in:
parent
546e2de300
commit
ce95af89ef
7 changed files with 133 additions and 115 deletions
|
@ -59,6 +59,79 @@ namespace ams::kern {
|
|||
|
||||
namespace init {
|
||||
|
||||
namespace {
|
||||
|
||||
void SetupPoolPartitionMemoryRegionsImpl() {
|
||||
/* Start by identifying the extents of the DRAM memory region. */
|
||||
const auto dram_extents = KMemoryLayout::GetMainMemoryPhysicalExtents();
|
||||
|
||||
const uintptr_t pool_end = dram_extents.GetEndAddress() - KTraceBufferSize;
|
||||
|
||||
/* Get Application and Applet pool sizes. */
|
||||
const size_t application_pool_size = KSystemControl::Init::GetApplicationPoolSize();
|
||||
const size_t applet_pool_size = KSystemControl::Init::GetAppletPoolSize();
|
||||
const size_t unsafe_system_pool_min_size = KSystemControl::Init::GetMinimumNonSecureSystemPoolSize();
|
||||
|
||||
/* Find the start of the kernel DRAM region. */
|
||||
const KMemoryRegion *kernel_dram_region = KMemoryLayout::GetPhysicalMemoryRegionTree().FindFirstDerived(KMemoryRegionType_DramKernelBase);
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(kernel_dram_region != nullptr);
|
||||
|
||||
const uintptr_t kernel_dram_start = kernel_dram_region->GetAddress();
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(util::IsAligned(kernel_dram_start, CarveoutAlignment));
|
||||
|
||||
/* Find the start of the pool partitions region. */
|
||||
const KMemoryRegion *pool_partitions_region = KMemoryLayout::GetPhysicalMemoryRegionTree().FindByTypeAndAttribute(KMemoryRegionType_DramPoolPartition, 0);
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(pool_partitions_region != nullptr);
|
||||
const uintptr_t pool_partitions_start = pool_partitions_region->GetAddress();
|
||||
|
||||
/* Decide on starting addresses for our pools. */
|
||||
const uintptr_t application_pool_start = pool_end - application_pool_size;
|
||||
const uintptr_t applet_pool_start = application_pool_start - applet_pool_size;
|
||||
const uintptr_t unsafe_system_pool_start = std::min(kernel_dram_start + CarveoutSizeMax, util::AlignDown(applet_pool_start - unsafe_system_pool_min_size, CarveoutAlignment));
|
||||
const size_t unsafe_system_pool_size = applet_pool_start - unsafe_system_pool_start;
|
||||
|
||||
/* We want to arrange application pool depending on where the middle of dram is. */
|
||||
const uintptr_t dram_midpoint = (dram_extents.GetAddress() + dram_extents.GetEndAddress()) / 2;
|
||||
u32 cur_pool_attr = 0;
|
||||
size_t total_overhead_size = 0;
|
||||
if (dram_extents.GetEndAddress() <= dram_midpoint || dram_midpoint <= application_pool_start) {
|
||||
InsertPoolPartitionRegionIntoBothTrees(application_pool_start, application_pool_size, KMemoryRegionType_DramApplicationPool, KMemoryRegionType_VirtualDramApplicationPool, cur_pool_attr);
|
||||
total_overhead_size += KMemoryManager::CalculateManagementOverheadSize(application_pool_size);
|
||||
} else {
|
||||
const size_t first_application_pool_size = dram_midpoint - application_pool_start;
|
||||
const size_t second_application_pool_size = application_pool_start + application_pool_size - dram_midpoint;
|
||||
InsertPoolPartitionRegionIntoBothTrees(application_pool_start, first_application_pool_size, KMemoryRegionType_DramApplicationPool, KMemoryRegionType_VirtualDramApplicationPool, cur_pool_attr);
|
||||
InsertPoolPartitionRegionIntoBothTrees(dram_midpoint, second_application_pool_size, KMemoryRegionType_DramApplicationPool, KMemoryRegionType_VirtualDramApplicationPool, cur_pool_attr);
|
||||
total_overhead_size += KMemoryManager::CalculateManagementOverheadSize(first_application_pool_size);
|
||||
total_overhead_size += KMemoryManager::CalculateManagementOverheadSize(second_application_pool_size);
|
||||
}
|
||||
|
||||
/* Insert the applet pool. */
|
||||
InsertPoolPartitionRegionIntoBothTrees(applet_pool_start, applet_pool_size, KMemoryRegionType_DramAppletPool, KMemoryRegionType_VirtualDramAppletPool, cur_pool_attr);
|
||||
total_overhead_size += KMemoryManager::CalculateManagementOverheadSize(applet_pool_size);
|
||||
|
||||
/* Insert the nonsecure system pool. */
|
||||
InsertPoolPartitionRegionIntoBothTrees(unsafe_system_pool_start, unsafe_system_pool_size, KMemoryRegionType_DramSystemNonSecurePool, KMemoryRegionType_VirtualDramSystemNonSecurePool, cur_pool_attr);
|
||||
total_overhead_size += KMemoryManager::CalculateManagementOverheadSize(unsafe_system_pool_size);
|
||||
|
||||
/* Insert the pool management region. */
|
||||
total_overhead_size += KMemoryManager::CalculateManagementOverheadSize((unsafe_system_pool_start - pool_partitions_start) - total_overhead_size);
|
||||
const uintptr_t pool_management_start = unsafe_system_pool_start - total_overhead_size;
|
||||
const size_t pool_management_size = total_overhead_size;
|
||||
u32 pool_management_attr = 0;
|
||||
InsertPoolPartitionRegionIntoBothTrees(pool_management_start, pool_management_size, KMemoryRegionType_DramPoolManagement, KMemoryRegionType_VirtualDramPoolManagement, pool_management_attr);
|
||||
|
||||
/* Insert the system pool. */
|
||||
const uintptr_t system_pool_size = pool_management_start - pool_partitions_start;
|
||||
InsertPoolPartitionRegionIntoBothTrees(pool_partitions_start, system_pool_size, KMemoryRegionType_DramSystemPool, KMemoryRegionType_VirtualDramSystemPool, cur_pool_attr);
|
||||
}
|
||||
|
||||
void SetupPoolPartitionMemoryRegionsDeprecatedImpl() {
|
||||
MESOSPHERE_UNIMPLEMENTED();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SetupDevicePhysicalMemoryRegions() {
|
||||
/* TODO: Give these constexpr defines somewhere? */
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(SetupUartPhysicalMemoryRegion());
|
||||
|
@ -84,69 +157,13 @@ namespace ams::kern {
|
|||
}
|
||||
|
||||
void SetupPoolPartitionMemoryRegions() {
|
||||
/* Start by identifying the extents of the DRAM memory region. */
|
||||
const auto dram_extents = KMemoryLayout::GetMainMemoryPhysicalExtents();
|
||||
|
||||
const uintptr_t pool_end = dram_extents.GetEndAddress() - KTraceBufferSize;
|
||||
|
||||
/* Get Application and Applet pool sizes. */
|
||||
const size_t application_pool_size = KSystemControl::Init::GetApplicationPoolSize();
|
||||
const size_t applet_pool_size = KSystemControl::Init::GetAppletPoolSize();
|
||||
const size_t unsafe_system_pool_min_size = KSystemControl::Init::GetMinimumNonSecureSystemPoolSize();
|
||||
|
||||
/* Find the start of the kernel DRAM region. */
|
||||
const KMemoryRegion *kernel_dram_region = KMemoryLayout::GetPhysicalMemoryRegionTree().FindFirstDerived(KMemoryRegionType_DramKernelBase);
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(kernel_dram_region != nullptr);
|
||||
|
||||
const uintptr_t kernel_dram_start = kernel_dram_region->GetAddress();
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(util::IsAligned(kernel_dram_start, CarveoutAlignment));
|
||||
|
||||
/* Find the start of the pool partitions region. */
|
||||
const KMemoryRegion *pool_partitions_region = KMemoryLayout::GetPhysicalMemoryRegionTree().FindByTypeAndAttribute(KMemoryRegionType_DramPoolPartition, 0);
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(pool_partitions_region != nullptr);
|
||||
const uintptr_t pool_partitions_start = pool_partitions_region->GetAddress();
|
||||
|
||||
/* Decide on starting addresses for our pools. */
|
||||
const uintptr_t application_pool_start = pool_end - application_pool_size;
|
||||
const uintptr_t applet_pool_start = application_pool_start - applet_pool_size;
|
||||
const uintptr_t unsafe_system_pool_start = std::min(kernel_dram_start + CarveoutSizeMax, util::AlignDown(applet_pool_start - unsafe_system_pool_min_size, CarveoutAlignment));
|
||||
const size_t unsafe_system_pool_size = applet_pool_start - unsafe_system_pool_start;
|
||||
|
||||
/* We want to arrange application pool depending on where the middle of dram is. */
|
||||
const uintptr_t dram_midpoint = (dram_extents.GetAddress() + dram_extents.GetEndAddress()) / 2;
|
||||
u32 cur_pool_attr = 0;
|
||||
size_t total_overhead_size = 0;
|
||||
if (dram_extents.GetEndAddress() <= dram_midpoint || dram_midpoint <= application_pool_start) {
|
||||
InsertPoolPartitionRegionIntoBothTrees(application_pool_start, application_pool_size, KMemoryRegionType_DramApplicationPool, KMemoryRegionType_VirtualDramApplicationPool, cur_pool_attr);
|
||||
total_overhead_size += KMemoryManager::CalculateMetadataOverheadSize(application_pool_size);
|
||||
if (GetTargetFirmware() >= TargetFirmware_5_0_0) {
|
||||
/* On 5.0.0+, setup modern 4-pool-partition layout. */
|
||||
SetupPoolPartitionMemoryRegionsImpl();
|
||||
} else {
|
||||
const size_t first_application_pool_size = dram_midpoint - application_pool_start;
|
||||
const size_t second_application_pool_size = application_pool_start + application_pool_size - dram_midpoint;
|
||||
InsertPoolPartitionRegionIntoBothTrees(application_pool_start, first_application_pool_size, KMemoryRegionType_DramApplicationPool, KMemoryRegionType_VirtualDramApplicationPool, cur_pool_attr);
|
||||
InsertPoolPartitionRegionIntoBothTrees(dram_midpoint, second_application_pool_size, KMemoryRegionType_DramApplicationPool, KMemoryRegionType_VirtualDramApplicationPool, cur_pool_attr);
|
||||
total_overhead_size += KMemoryManager::CalculateMetadataOverheadSize(first_application_pool_size);
|
||||
total_overhead_size += KMemoryManager::CalculateMetadataOverheadSize(second_application_pool_size);
|
||||
/* On < 5.0.0, setup a legacy 2-pool layout for backwards compatibility. */
|
||||
SetupPoolPartitionMemoryRegionsDeprecatedImpl();
|
||||
}
|
||||
|
||||
/* Insert the applet pool. */
|
||||
InsertPoolPartitionRegionIntoBothTrees(applet_pool_start, applet_pool_size, KMemoryRegionType_DramAppletPool, KMemoryRegionType_VirtualDramAppletPool, cur_pool_attr);
|
||||
total_overhead_size += KMemoryManager::CalculateMetadataOverheadSize(applet_pool_size);
|
||||
|
||||
/* Insert the nonsecure system pool. */
|
||||
InsertPoolPartitionRegionIntoBothTrees(unsafe_system_pool_start, unsafe_system_pool_size, KMemoryRegionType_DramSystemNonSecurePool, KMemoryRegionType_VirtualDramSystemNonSecurePool, cur_pool_attr);
|
||||
total_overhead_size += KMemoryManager::CalculateMetadataOverheadSize(unsafe_system_pool_size);
|
||||
|
||||
/* Insert the metadata pool. */
|
||||
total_overhead_size += KMemoryManager::CalculateMetadataOverheadSize((unsafe_system_pool_start - pool_partitions_start) - total_overhead_size);
|
||||
const uintptr_t metadata_pool_start = unsafe_system_pool_start - total_overhead_size;
|
||||
const size_t metadata_pool_size = total_overhead_size;
|
||||
u32 metadata_pool_attr = 0;
|
||||
InsertPoolPartitionRegionIntoBothTrees(metadata_pool_start, metadata_pool_size, KMemoryRegionType_DramPoolManagement, KMemoryRegionType_VirtualDramPoolManagement, metadata_pool_attr);
|
||||
|
||||
/* Insert the system pool. */
|
||||
const uintptr_t system_pool_size = metadata_pool_start - pool_partitions_start;
|
||||
InsertPoolPartitionRegionIntoBothTrees(pool_partitions_start, system_pool_size, KMemoryRegionType_DramSystemPool, KMemoryRegionType_VirtualDramSystemPool, cur_pool_attr);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -31,10 +31,10 @@ namespace ams::kern {
|
|||
|
||||
}
|
||||
|
||||
void KMemoryManager::Initialize(KVirtualAddress metadata_region, size_t metadata_region_size) {
|
||||
/* Clear the metadata region to zero. */
|
||||
const KVirtualAddress metadata_region_end = metadata_region + metadata_region_size;
|
||||
std::memset(GetVoidPointer(metadata_region), 0, metadata_region_size);
|
||||
void KMemoryManager::Initialize(KVirtualAddress management_region, size_t management_region_size) {
|
||||
/* Clear the management region to zero. */
|
||||
const KVirtualAddress management_region_end = management_region + management_region_size;
|
||||
std::memset(GetVoidPointer(management_region), 0, management_region_size);
|
||||
|
||||
/* Traverse the virtual memory layout tree, initializing each manager as appropriate. */
|
||||
while (true) {
|
||||
|
@ -72,9 +72,9 @@ namespace ams::kern {
|
|||
Impl *manager = std::addressof(this->managers[this->num_managers++]);
|
||||
MESOSPHERE_ABORT_UNLESS(this->num_managers <= util::size(this->managers));
|
||||
|
||||
const size_t cur_size = manager->Initialize(region, pool, metadata_region, metadata_region_end);
|
||||
metadata_region += cur_size;
|
||||
MESOSPHERE_ABORT_UNLESS(metadata_region <= metadata_region_end);
|
||||
const size_t cur_size = manager->Initialize(region, pool, management_region, management_region_end);
|
||||
management_region += cur_size;
|
||||
MESOSPHERE_ABORT_UNLESS(management_region <= management_region_end);
|
||||
|
||||
/* Insert the manager into the pool list. */
|
||||
if (this->pool_managers_tail[pool] == nullptr) {
|
||||
|
@ -313,25 +313,25 @@ namespace ams::kern {
|
|||
return ResultSuccess();
|
||||
}
|
||||
|
||||
size_t KMemoryManager::Impl::Initialize(const KMemoryRegion *region, Pool p, KVirtualAddress metadata, KVirtualAddress metadata_end) {
|
||||
/* Calculate metadata sizes. */
|
||||
size_t KMemoryManager::Impl::Initialize(const KMemoryRegion *region, Pool p, KVirtualAddress management, KVirtualAddress management_end) {
|
||||
/* Calculate management sizes. */
|
||||
const size_t ref_count_size = (region->GetSize() / PageSize) * sizeof(u16);
|
||||
const size_t optimize_map_size = CalculateOptimizedProcessOverheadSize(region->GetSize());
|
||||
const size_t manager_size = util::AlignUp(optimize_map_size + ref_count_size, PageSize);
|
||||
const size_t page_heap_size = KPageHeap::CalculateMetadataOverheadSize(region->GetSize());
|
||||
const size_t total_metadata_size = manager_size + page_heap_size;
|
||||
MESOSPHERE_ABORT_UNLESS(manager_size <= total_metadata_size);
|
||||
MESOSPHERE_ABORT_UNLESS(metadata + total_metadata_size <= metadata_end);
|
||||
MESOSPHERE_ABORT_UNLESS(util::IsAligned(total_metadata_size, PageSize));
|
||||
const size_t page_heap_size = KPageHeap::CalculateManagementOverheadSize(region->GetSize());
|
||||
const size_t total_management_size = manager_size + page_heap_size;
|
||||
MESOSPHERE_ABORT_UNLESS(manager_size <= total_management_size);
|
||||
MESOSPHERE_ABORT_UNLESS(management + total_management_size <= management_end);
|
||||
MESOSPHERE_ABORT_UNLESS(util::IsAligned(total_management_size, PageSize));
|
||||
|
||||
/* Setup region. */
|
||||
this->pool = p;
|
||||
this->metadata_region = metadata;
|
||||
this->page_reference_counts = GetPointer<RefCount>(metadata + optimize_map_size);
|
||||
MESOSPHERE_ABORT_UNLESS(util::IsAligned(GetInteger(this->metadata_region), PageSize));
|
||||
this->management_region = management;
|
||||
this->page_reference_counts = GetPointer<RefCount>(management + optimize_map_size);
|
||||
MESOSPHERE_ABORT_UNLESS(util::IsAligned(GetInteger(this->management_region), PageSize));
|
||||
|
||||
/* Initialize the manager's KPageHeap. */
|
||||
this->heap.Initialize(region->GetAddress(), region->GetSize(), metadata + manager_size, page_heap_size);
|
||||
this->heap.Initialize(region->GetAddress(), region->GetSize(), management + manager_size, page_heap_size);
|
||||
|
||||
/* Free the memory to the heap. */
|
||||
this->heap.Free(region->GetAddress(), region->GetSize() / PageSize);
|
||||
|
@ -339,7 +339,7 @@ namespace ams::kern {
|
|||
/* Update the heap's used size. */
|
||||
this->heap.UpdateUsedSize();
|
||||
|
||||
return total_metadata_size;
|
||||
return total_management_size;
|
||||
}
|
||||
|
||||
void KMemoryManager::Impl::TrackUnoptimizedAllocation(KVirtualAddress block, size_t num_pages) {
|
||||
|
@ -348,7 +348,7 @@ namespace ams::kern {
|
|||
const size_t last = offset + num_pages - 1;
|
||||
|
||||
/* Track. */
|
||||
u64 *optimize_map = GetPointer<u64>(this->metadata_region);
|
||||
u64 *optimize_map = GetPointer<u64>(this->management_region);
|
||||
while (offset <= last) {
|
||||
/* Mark the page as not being optimized-allocated. */
|
||||
optimize_map[offset / BITSIZEOF(u64)] &= ~(u64(1) << (offset % BITSIZEOF(u64)));
|
||||
|
@ -363,7 +363,7 @@ namespace ams::kern {
|
|||
const size_t last = offset + num_pages - 1;
|
||||
|
||||
/* Track. */
|
||||
u64 *optimize_map = GetPointer<u64>(this->metadata_region);
|
||||
u64 *optimize_map = GetPointer<u64>(this->management_region);
|
||||
while (offset <= last) {
|
||||
/* Mark the page as being optimized-allocated. */
|
||||
optimize_map[offset / BITSIZEOF(u64)] |= (u64(1) << (offset % BITSIZEOF(u64)));
|
||||
|
@ -381,7 +381,7 @@ namespace ams::kern {
|
|||
const size_t last = offset + num_pages - 1;
|
||||
|
||||
/* Process. */
|
||||
u64 *optimize_map = GetPointer<u64>(this->metadata_region);
|
||||
u64 *optimize_map = GetPointer<u64>(this->management_region);
|
||||
while (offset <= last) {
|
||||
/* Check if the page has been optimized-allocated before. */
|
||||
if ((optimize_map[offset / BITSIZEOF(u64)] & (u64(1) << (offset % BITSIZEOF(u64)))) == 0) {
|
||||
|
@ -399,11 +399,11 @@ namespace ams::kern {
|
|||
return any_new;
|
||||
}
|
||||
|
||||
size_t KMemoryManager::Impl::CalculateMetadataOverheadSize(size_t region_size) {
|
||||
size_t KMemoryManager::Impl::CalculateManagementOverheadSize(size_t region_size) {
|
||||
const size_t ref_count_size = (region_size / PageSize) * sizeof(u16);
|
||||
const size_t optimize_map_size = (util::AlignUp((region_size / PageSize), BITSIZEOF(u64)) / BITSIZEOF(u64)) * sizeof(u64);
|
||||
const size_t manager_meta_size = util::AlignUp(optimize_map_size + ref_count_size, PageSize);
|
||||
const size_t page_heap_size = KPageHeap::CalculateMetadataOverheadSize(region_size);
|
||||
const size_t page_heap_size = KPageHeap::CalculateManagementOverheadSize(region_size);
|
||||
return manager_meta_size + page_heap_size;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,12 +17,12 @@
|
|||
|
||||
namespace ams::kern {
|
||||
|
||||
void KPageHeap::Initialize(KVirtualAddress address, size_t size, KVirtualAddress metadata_address, size_t metadata_size, const size_t *block_shifts, size_t num_block_shifts) {
|
||||
void KPageHeap::Initialize(KVirtualAddress address, size_t size, KVirtualAddress management_address, size_t management_size, const size_t *block_shifts, size_t num_block_shifts) {
|
||||
/* Check our assumptions. */
|
||||
MESOSPHERE_ASSERT(util::IsAligned(GetInteger(address), PageSize));
|
||||
MESOSPHERE_ASSERT(util::IsAligned(size, PageSize));
|
||||
MESOSPHERE_ASSERT(0 < num_block_shifts && num_block_shifts <= NumMemoryBlockPageShifts);
|
||||
const KVirtualAddress metadata_end = metadata_address + metadata_size;
|
||||
const KVirtualAddress management_end = management_address + management_size;
|
||||
|
||||
/* Set our members. */
|
||||
this->heap_address = address;
|
||||
|
@ -30,7 +30,7 @@ namespace ams::kern {
|
|||
this->num_blocks = num_block_shifts;
|
||||
|
||||
/* Setup bitmaps. */
|
||||
u64 *cur_bitmap_storage = GetPointer<u64>(metadata_address);
|
||||
u64 *cur_bitmap_storage = GetPointer<u64>(management_address);
|
||||
for (size_t i = 0; i < num_block_shifts; i++) {
|
||||
const size_t cur_block_shift = block_shifts[i];
|
||||
const size_t next_block_shift = (i != num_block_shifts - 1) ? block_shifts[i + 1] : 0;
|
||||
|
@ -38,7 +38,7 @@ namespace ams::kern {
|
|||
}
|
||||
|
||||
/* Ensure we didn't overextend our bounds. */
|
||||
MESOSPHERE_ABORT_UNLESS(KVirtualAddress(cur_bitmap_storage) <= metadata_end);
|
||||
MESOSPHERE_ABORT_UNLESS(KVirtualAddress(cur_bitmap_storage) <= management_end);
|
||||
}
|
||||
|
||||
size_t KPageHeap::GetNumFreePages() const {
|
||||
|
@ -122,12 +122,12 @@ namespace ams::kern {
|
|||
}
|
||||
}
|
||||
|
||||
size_t KPageHeap::CalculateMetadataOverheadSize(size_t region_size, const size_t *block_shifts, size_t num_block_shifts) {
|
||||
size_t KPageHeap::CalculateManagementOverheadSize(size_t region_size, const size_t *block_shifts, size_t num_block_shifts) {
|
||||
size_t overhead_size = 0;
|
||||
for (size_t i = 0; i < num_block_shifts; i++) {
|
||||
const size_t cur_block_shift = block_shifts[i];
|
||||
const size_t next_block_shift = (i != num_block_shifts - 1) ? block_shifts[i + 1] : 0;
|
||||
overhead_size += KPageHeap::Block::CalculateMetadataOverheadSize(region_size, cur_block_shift, next_block_shift);
|
||||
overhead_size += KPageHeap::Block::CalculateManagementOverheadSize(region_size, cur_block_shift, next_block_shift);
|
||||
}
|
||||
return util::AlignUp(overhead_size, PageSize);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue