2020-01-29 09:49:04 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
2020-01-30 23:29:51 +00:00
|
|
|
#include <mesosphere/kern_common.hpp>
|
2020-02-07 01:40:57 +00:00
|
|
|
#include <mesosphere/kern_k_light_lock.hpp>
|
|
|
|
#include <mesosphere/kern_k_memory_layout.hpp>
|
2020-02-07 19:51:58 +00:00
|
|
|
#include <mesosphere/kern_k_page_heap.hpp>
|
2020-01-29 09:49:04 +00:00
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
2020-02-14 01:38:56 +00:00
|
|
|
class KPageGroup;
|
|
|
|
|
2020-01-29 09:49:04 +00:00
|
|
|
class KMemoryManager {
|
2020-02-07 01:40:57 +00:00
|
|
|
public:
|
|
|
|
enum Pool {
|
|
|
|
Pool_Application = 0,
|
|
|
|
Pool_Applet = 1,
|
|
|
|
Pool_System = 2,
|
|
|
|
Pool_SystemNonSecure = 3,
|
|
|
|
|
|
|
|
Pool_Count,
|
|
|
|
|
|
|
|
Pool_Shift = 4,
|
|
|
|
Pool_Mask = (0xF << Pool_Shift),
|
2020-04-26 09:49:59 +00:00
|
|
|
|
|
|
|
/* Aliases. */
|
|
|
|
Pool_Unsafe = Pool_Application,
|
2020-08-25 23:12:14 +00:00
|
|
|
Pool_Secure = Pool_System,
|
2020-02-07 01:40:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum Direction {
|
|
|
|
Direction_FromFront = 0,
|
|
|
|
Direction_FromBack = 1,
|
|
|
|
|
|
|
|
Direction_Shift = 0,
|
|
|
|
Direction_Mask = (0xF << Direction_Shift),
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr size_t MaxManagerCount = 10;
|
2020-01-29 09:49:04 +00:00
|
|
|
private:
|
|
|
|
class Impl {
|
2020-02-07 01:40:57 +00:00
|
|
|
private:
|
|
|
|
using RefCount = u16;
|
2020-07-22 05:13:16 +00:00
|
|
|
public:
|
2020-08-25 23:12:14 +00:00
|
|
|
static size_t CalculateManagementOverheadSize(size_t region_size);
|
2020-07-22 05:13:16 +00:00
|
|
|
|
|
|
|
static constexpr size_t CalculateOptimizedProcessOverheadSize(size_t region_size) {
|
|
|
|
return (util::AlignUp((region_size / PageSize), BITSIZEOF(u64)) / BITSIZEOF(u64)) * sizeof(u64);
|
|
|
|
}
|
2020-02-07 01:40:57 +00:00
|
|
|
private:
|
|
|
|
KPageHeap heap;
|
|
|
|
RefCount *page_reference_counts;
|
2020-08-25 23:12:14 +00:00
|
|
|
KVirtualAddress management_region;
|
2020-02-07 01:40:57 +00:00
|
|
|
Pool pool;
|
|
|
|
Impl *next;
|
|
|
|
Impl *prev;
|
|
|
|
public:
|
2020-08-25 23:12:14 +00:00
|
|
|
Impl() : heap(), page_reference_counts(), management_region(), pool(), next(), prev() { /* ... */ }
|
2020-02-07 01:40:57 +00:00
|
|
|
|
2020-08-25 23:12:14 +00:00
|
|
|
size_t Initialize(const KMemoryRegion *region, Pool pool, KVirtualAddress management_region, KVirtualAddress management_region_end);
|
2020-02-07 01:40:57 +00:00
|
|
|
|
2020-04-19 00:10:26 +00:00
|
|
|
KVirtualAddress AllocateBlock(s32 index, bool random) { return this->heap.AllocateBlock(index, random); }
|
2020-02-07 04:36:26 +00:00
|
|
|
void Free(KVirtualAddress addr, size_t num_pages) { this->heap.Free(addr, num_pages); }
|
|
|
|
|
2020-08-25 23:12:14 +00:00
|
|
|
void InitializeOptimizedMemory() { std::memset(GetVoidPointer(this->management_region), 0, CalculateOptimizedProcessOverheadSize(this->heap.GetSize())); }
|
2020-07-24 15:07:34 +00:00
|
|
|
|
|
|
|
void TrackUnoptimizedAllocation(KVirtualAddress block, size_t num_pages);
|
2020-08-03 19:28:14 +00:00
|
|
|
void TrackOptimizedAllocation(KVirtualAddress block, size_t num_pages);
|
2020-07-24 15:07:34 +00:00
|
|
|
|
2020-08-03 19:28:14 +00:00
|
|
|
bool ProcessOptimizedAllocation(KVirtualAddress block, size_t num_pages, u8 fill_pattern);
|
2020-02-07 04:36:26 +00:00
|
|
|
|
2020-07-24 02:26:46 +00:00
|
|
|
constexpr Pool GetPool() const { return this->pool; }
|
2020-02-07 12:58:35 +00:00
|
|
|
constexpr size_t GetSize() const { return this->heap.GetSize(); }
|
2020-02-07 04:36:26 +00:00
|
|
|
constexpr KVirtualAddress GetEndAddress() const { return this->heap.GetEndAddress(); }
|
|
|
|
|
2020-07-21 08:59:48 +00:00
|
|
|
size_t GetFreeSize() const { return this->heap.GetFreeSize(); }
|
|
|
|
|
2020-08-03 19:28:14 +00:00
|
|
|
constexpr size_t GetPageOffset(KVirtualAddress address) const { return this->heap.GetPageOffset(address); }
|
|
|
|
constexpr size_t GetPageOffsetToEnd(KVirtualAddress address) const { return this->heap.GetPageOffsetToEnd(address); }
|
|
|
|
|
2020-02-07 04:36:26 +00:00
|
|
|
constexpr void SetNext(Impl *n) { this->next = n; }
|
|
|
|
constexpr void SetPrev(Impl *n) { this->prev = n; }
|
|
|
|
constexpr Impl *GetNext() const { return this->next; }
|
|
|
|
constexpr Impl *GetPrev() const { return this->prev; }
|
|
|
|
|
2020-08-03 19:28:14 +00:00
|
|
|
void Open(KVirtualAddress address, size_t num_pages) {
|
|
|
|
size_t index = this->GetPageOffset(address);
|
2020-02-07 04:36:26 +00:00
|
|
|
const size_t end = index + num_pages;
|
|
|
|
while (index < end) {
|
2020-02-07 12:58:35 +00:00
|
|
|
const RefCount ref_count = (++this->page_reference_counts[index]);
|
2020-02-07 04:36:26 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(ref_count > 0);
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 19:28:14 +00:00
|
|
|
void Close(KVirtualAddress address, size_t num_pages) {
|
|
|
|
size_t index = this->GetPageOffset(address);
|
2020-02-07 12:58:35 +00:00
|
|
|
const size_t end = index + num_pages;
|
|
|
|
|
|
|
|
size_t free_start = 0;
|
|
|
|
size_t free_count = 0;
|
|
|
|
while (index < end) {
|
|
|
|
MESOSPHERE_ABORT_UNLESS(this->page_reference_counts[index] > 0);
|
|
|
|
const RefCount ref_count = (--this->page_reference_counts[index]);
|
|
|
|
|
|
|
|
/* Keep track of how many zero refcounts we see in a row, to minimize calls to free. */
|
|
|
|
if (ref_count == 0) {
|
|
|
|
if (free_count > 0) {
|
|
|
|
free_count++;
|
|
|
|
} else {
|
|
|
|
free_start = index;
|
|
|
|
free_count = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (free_count > 0) {
|
|
|
|
this->Free(this->heap.GetAddress() + free_start * PageSize, free_count);
|
|
|
|
free_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (free_count > 0) {
|
|
|
|
this->Free(this->heap.GetAddress() + free_start * PageSize, free_count);
|
2020-02-07 04:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-29 09:49:04 +00:00
|
|
|
};
|
2020-02-07 01:40:57 +00:00
|
|
|
private:
|
|
|
|
KLightLock pool_locks[Pool_Count];
|
|
|
|
Impl *pool_managers_head[Pool_Count];
|
|
|
|
Impl *pool_managers_tail[Pool_Count];
|
|
|
|
Impl managers[MaxManagerCount];
|
|
|
|
size_t num_managers;
|
|
|
|
u64 optimized_process_ids[Pool_Count];
|
|
|
|
bool has_optimized_process[Pool_Count];
|
2020-02-07 04:36:26 +00:00
|
|
|
private:
|
|
|
|
Impl &GetManager(KVirtualAddress address) {
|
|
|
|
return this->managers[KMemoryLayout::GetVirtualLinearRegion(address).GetAttributes()];
|
|
|
|
}
|
2020-02-14 01:38:56 +00:00
|
|
|
|
|
|
|
constexpr Impl *GetFirstManager(Pool pool, Direction dir) {
|
|
|
|
return dir == Direction_FromBack ? this->pool_managers_tail[pool] : this->pool_managers_head[pool];
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Impl *GetNextManager(Impl *cur, Direction dir) {
|
|
|
|
if (dir == Direction_FromBack) {
|
|
|
|
return cur->GetPrev();
|
|
|
|
} else {
|
|
|
|
return cur->GetNext();
|
|
|
|
}
|
|
|
|
}
|
2020-04-19 00:10:26 +00:00
|
|
|
|
2020-07-24 15:07:34 +00:00
|
|
|
Result AllocatePageGroupImpl(KPageGroup *out, size_t num_pages, Pool pool, Direction dir, bool unoptimized, bool random);
|
2020-02-07 01:40:57 +00:00
|
|
|
public:
|
2020-04-19 00:10:26 +00:00
|
|
|
KMemoryManager()
|
2020-02-07 01:40:57 +00:00
|
|
|
: pool_locks(), pool_managers_head(), pool_managers_tail(), managers(), num_managers(), optimized_process_ids(), has_optimized_process()
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
2020-08-25 23:12:14 +00:00
|
|
|
NOINLINE void Initialize(KVirtualAddress management_region, size_t management_region_size);
|
2020-02-07 04:36:26 +00:00
|
|
|
|
2020-07-22 05:13:16 +00:00
|
|
|
NOINLINE Result InitializeOptimizedMemory(u64 process_id, Pool pool);
|
2020-07-25 03:44:15 +00:00
|
|
|
NOINLINE void FinalizeOptimizedMemory(u64 process_id, Pool pool);
|
2020-07-22 05:13:16 +00:00
|
|
|
|
2020-02-14 01:38:56 +00:00
|
|
|
NOINLINE KVirtualAddress AllocateContinuous(size_t num_pages, size_t align_pages, u32 option);
|
|
|
|
NOINLINE Result Allocate(KPageGroup *out, size_t num_pages, u32 option);
|
2020-07-24 15:07:34 +00:00
|
|
|
NOINLINE Result AllocateForProcess(KPageGroup *out, size_t num_pages, u32 option, u64 process_id, u8 fill_pattern);
|
2020-02-07 04:36:26 +00:00
|
|
|
|
|
|
|
void Open(KVirtualAddress address, size_t num_pages) {
|
|
|
|
/* Repeatedly open references until we've done so for all pages. */
|
|
|
|
while (num_pages) {
|
|
|
|
auto &manager = this->GetManager(address);
|
2020-08-03 19:28:14 +00:00
|
|
|
const size_t cur_pages = std::min(num_pages, manager.GetPageOffsetToEnd(address));
|
|
|
|
|
|
|
|
{
|
|
|
|
KScopedLightLock lk(this->pool_locks[manager.GetPool()]);
|
|
|
|
manager.Open(address, cur_pages);
|
|
|
|
}
|
|
|
|
|
2020-02-07 04:36:26 +00:00
|
|
|
num_pages -= cur_pages;
|
2020-02-07 12:58:35 +00:00
|
|
|
address += cur_pages * PageSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close(KVirtualAddress address, size_t num_pages) {
|
|
|
|
/* Repeatedly close references until we've done so for all pages. */
|
|
|
|
while (num_pages) {
|
|
|
|
auto &manager = this->GetManager(address);
|
2020-08-03 19:28:14 +00:00
|
|
|
const size_t cur_pages = std::min(num_pages, manager.GetPageOffsetToEnd(address));
|
|
|
|
|
|
|
|
{
|
|
|
|
KScopedLightLock lk(this->pool_locks[manager.GetPool()]);
|
|
|
|
manager.Close(address, cur_pages);
|
|
|
|
}
|
|
|
|
|
2020-02-07 12:58:35 +00:00
|
|
|
num_pages -= cur_pages;
|
2020-02-07 04:36:26 +00:00
|
|
|
address += cur_pages * PageSize;
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 09:49:59 +00:00
|
|
|
|
|
|
|
size_t GetSize() {
|
|
|
|
size_t total = 0;
|
|
|
|
for (size_t i = 0; i < this->num_managers; i++) {
|
|
|
|
total += this->managers[i].GetSize();
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetSize(Pool pool) {
|
|
|
|
constexpr Direction GetSizeDirection = Direction_FromFront;
|
|
|
|
size_t total = 0;
|
|
|
|
for (auto *manager = this->GetFirstManager(pool, GetSizeDirection); manager != nullptr; manager = this->GetNextManager(manager, GetSizeDirection)) {
|
|
|
|
total += manager->GetSize();
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
2020-07-21 08:59:48 +00:00
|
|
|
|
|
|
|
size_t GetFreeSize() {
|
|
|
|
size_t total = 0;
|
|
|
|
for (size_t i = 0; i < this->num_managers; i++) {
|
|
|
|
total += this->managers[i].GetFreeSize();
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetFreeSize(Pool pool) {
|
|
|
|
constexpr Direction GetSizeDirection = Direction_FromFront;
|
|
|
|
size_t total = 0;
|
|
|
|
for (auto *manager = this->GetFirstManager(pool, GetSizeDirection); manager != nullptr; manager = this->GetNextManager(manager, GetSizeDirection)) {
|
|
|
|
total += manager->GetFreeSize();
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
2020-01-29 09:49:04 +00:00
|
|
|
public:
|
2020-08-25 23:12:14 +00:00
|
|
|
static size_t CalculateManagementOverheadSize(size_t region_size) {
|
|
|
|
return Impl::CalculateManagementOverheadSize(region_size);
|
2020-01-29 09:49:04 +00:00
|
|
|
}
|
2020-02-07 01:40:57 +00:00
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE u32 EncodeOption(Pool pool, Direction dir) {
|
|
|
|
return (pool << Pool_Shift) | (dir << Direction_Shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE Pool GetPool(u32 option) {
|
|
|
|
return static_cast<Pool>((option & Pool_Mask) >> Pool_Shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE Direction GetDirection(u32 option) {
|
|
|
|
return static_cast<Direction>((option & Direction_Mask) >> Direction_Shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr ALWAYS_INLINE std::tuple<Pool, Direction> DecodeOption(u32 option) {
|
|
|
|
return std::make_tuple(GetPool(option), GetDirection(option));
|
|
|
|
}
|
2020-01-29 09:49:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|