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-04-19 00:10:26 +00:00
|
|
|
#include <mesosphere/kern_k_page_bitmap.hpp>
|
2020-01-29 09:49:04 +00:00
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
class KPageHeap {
|
2020-02-07 01:40:57 +00:00
|
|
|
private:
|
|
|
|
static constexpr inline size_t MemoryBlockPageShifts[] = { 0xC, 0x10, 0x15, 0x16, 0x19, 0x1D, 0x1E };
|
|
|
|
static constexpr size_t NumMemoryBlockPageShifts = util::size(MemoryBlockPageShifts);
|
2020-02-07 04:36:26 +00:00
|
|
|
public:
|
|
|
|
static constexpr s32 GetAlignedBlockIndex(size_t num_pages, size_t align_pages) {
|
|
|
|
const size_t target_pages = std::max(num_pages, align_pages);
|
|
|
|
for (size_t i = 0; i < NumMemoryBlockPageShifts; i++) {
|
|
|
|
if (target_pages <= (size_t(1) << MemoryBlockPageShifts[i]) / PageSize) {
|
|
|
|
return static_cast<s32>(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr s32 GetBlockIndex(size_t num_pages) {
|
2020-02-14 01:38:56 +00:00
|
|
|
for (s32 i = static_cast<s32>(NumMemoryBlockPageShifts) - 1; i >= 0; i--) {
|
2020-02-07 04:36:26 +00:00
|
|
|
if (num_pages >= (size_t(1) << MemoryBlockPageShifts[i]) / PageSize) {
|
2020-02-14 01:38:56 +00:00
|
|
|
return i;
|
2020-02-07 04:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr size_t GetBlockSize(size_t index) {
|
|
|
|
return size_t(1) << MemoryBlockPageShifts[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr size_t GetBlockNumPages(size_t index) {
|
|
|
|
return GetBlockSize(index) / PageSize;
|
|
|
|
}
|
2020-01-29 09:49:04 +00:00
|
|
|
private:
|
|
|
|
class Block {
|
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
KPageBitmap m_bitmap;
|
|
|
|
KVirtualAddress m_heap_address;
|
|
|
|
uintptr_t m_end_offset;
|
|
|
|
size_t m_block_shift;
|
|
|
|
size_t m_next_block_shift;
|
2020-02-07 01:40:57 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
Block() : m_bitmap(), m_heap_address(), m_end_offset(), m_block_shift(), m_next_block_shift() { /* ... */ }
|
2020-02-07 01:40:57 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr size_t GetShift() const { return m_block_shift; }
|
|
|
|
constexpr size_t GetNextShift() const { return m_next_block_shift; }
|
2020-02-07 04:36:26 +00:00
|
|
|
constexpr size_t GetSize() const { return u64(1) << this->GetShift(); }
|
|
|
|
constexpr size_t GetNumPages() const { return this->GetSize() / PageSize; }
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr size_t GetNumFreeBlocks() const { return m_bitmap.GetNumBits(); }
|
2020-02-07 04:36:26 +00:00
|
|
|
constexpr size_t GetNumFreePages() const { return this->GetNumFreeBlocks() * this->GetNumPages(); }
|
|
|
|
|
2020-02-07 01:40:57 +00:00
|
|
|
u64 *Initialize(KVirtualAddress addr, size_t size, size_t bs, size_t nbs, u64 *bit_storage) {
|
|
|
|
/* Set shifts. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_block_shift = bs;
|
|
|
|
m_next_block_shift = nbs;
|
2020-02-07 01:40:57 +00:00
|
|
|
|
|
|
|
/* Align up the address. */
|
|
|
|
KVirtualAddress end = addr + size;
|
2020-12-18 01:18:47 +00:00
|
|
|
const size_t align = (m_next_block_shift != 0) ? (u64(1) << m_next_block_shift) : (u64(1) << m_block_shift);
|
2020-02-07 01:40:57 +00:00
|
|
|
addr = util::AlignDown(GetInteger(addr), align);
|
|
|
|
end = util::AlignUp(GetInteger(end), align);
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_heap_address = addr;
|
|
|
|
m_end_offset = (end - addr) / (u64(1) << m_block_shift);
|
|
|
|
return m_bitmap.Initialize(bit_storage, m_end_offset);
|
2020-02-07 01:40:57 +00:00
|
|
|
}
|
2020-02-07 04:36:26 +00:00
|
|
|
|
|
|
|
KVirtualAddress PushBlock(KVirtualAddress address) {
|
|
|
|
/* Set the bit for the free block. */
|
2020-12-18 01:18:47 +00:00
|
|
|
size_t offset = (address - m_heap_address) >> this->GetShift();
|
|
|
|
m_bitmap.SetBit(offset);
|
2020-02-07 04:36:26 +00:00
|
|
|
|
|
|
|
/* If we have a next shift, try to clear the blocks below this one and return the new address. */
|
|
|
|
if (this->GetNextShift()) {
|
|
|
|
const size_t diff = u64(1) << (this->GetNextShift() - this->GetShift());
|
|
|
|
offset = util::AlignDown(offset, diff);
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_bitmap.ClearRange(offset, diff)) {
|
|
|
|
return m_heap_address + (offset << this->GetShift());
|
2020-02-07 04:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We couldn't coalesce, or we're already as big as possible. */
|
|
|
|
return Null<KVirtualAddress>;
|
|
|
|
}
|
|
|
|
|
2020-04-19 00:10:26 +00:00
|
|
|
KVirtualAddress PopBlock(bool random) {
|
2020-02-07 04:36:26 +00:00
|
|
|
/* Find a free block. */
|
2020-12-18 01:18:47 +00:00
|
|
|
ssize_t soffset = m_bitmap.FindFreeBlock(random);
|
2020-02-07 04:36:26 +00:00
|
|
|
if (soffset < 0) {
|
|
|
|
return Null<KVirtualAddress>;
|
|
|
|
}
|
|
|
|
const size_t offset = static_cast<size_t>(soffset);
|
|
|
|
|
|
|
|
/* Update our tracking and return it. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_bitmap.ClearBit(offset);
|
|
|
|
return m_heap_address + (offset << this->GetShift());
|
2020-02-07 04:36:26 +00:00
|
|
|
}
|
2020-01-29 09:49:04 +00:00
|
|
|
public:
|
2020-08-25 23:12:14 +00:00
|
|
|
static constexpr size_t CalculateManagementOverheadSize(size_t region_size, size_t cur_block_shift, size_t next_block_shift) {
|
2020-02-07 04:36:26 +00:00
|
|
|
const size_t cur_block_size = (u64(1) << cur_block_shift);
|
|
|
|
const size_t next_block_size = (u64(1) << next_block_shift);
|
2020-01-29 09:49:04 +00:00
|
|
|
const size_t align = (next_block_shift != 0) ? next_block_size : cur_block_size;
|
2020-08-25 23:12:14 +00:00
|
|
|
return KPageBitmap::CalculateManagementOverheadSize((align * 2 + util::AlignUp(region_size, align)) / cur_block_size);
|
2020-01-29 09:49:04 +00:00
|
|
|
}
|
|
|
|
};
|
2020-02-07 01:40:57 +00:00
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
KVirtualAddress m_heap_address;
|
|
|
|
size_t m_heap_size;
|
|
|
|
size_t m_used_size;
|
|
|
|
size_t m_num_blocks;
|
|
|
|
Block m_blocks[NumMemoryBlockPageShifts];
|
2020-02-07 01:40:57 +00:00
|
|
|
private:
|
2020-08-25 23:12:14 +00:00
|
|
|
void Initialize(KVirtualAddress heap_address, size_t heap_size, KVirtualAddress management_address, size_t management_size, const size_t *block_shifts, size_t num_block_shifts);
|
2020-02-07 04:36:26 +00:00
|
|
|
size_t GetNumFreePages() const;
|
|
|
|
|
|
|
|
void FreeBlock(KVirtualAddress block, s32 index);
|
2020-01-29 09:49:04 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
KPageHeap() : m_heap_address(), m_heap_size(), m_used_size(), m_num_blocks(), m_blocks() { /* ... */ }
|
2020-02-07 01:40:57 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KVirtualAddress GetAddress() const { return m_heap_address; }
|
|
|
|
constexpr size_t GetSize() const { return m_heap_size; }
|
2020-02-07 12:58:35 +00:00
|
|
|
constexpr KVirtualAddress GetEndAddress() const { return this->GetAddress() + this->GetSize(); }
|
|
|
|
constexpr size_t GetPageOffset(KVirtualAddress block) const { return (block - this->GetAddress()) / PageSize; }
|
2020-07-24 15:07:34 +00:00
|
|
|
constexpr size_t GetPageOffsetToEnd(KVirtualAddress block) const { return (this->GetEndAddress() - block) / PageSize; }
|
2020-02-07 04:36:26 +00:00
|
|
|
|
2020-08-25 23:12:14 +00:00
|
|
|
void Initialize(KVirtualAddress heap_address, size_t heap_size, KVirtualAddress management_address, size_t management_size) {
|
|
|
|
return Initialize(heap_address, heap_size, management_address, management_size, MemoryBlockPageShifts, NumMemoryBlockPageShifts);
|
2020-02-07 01:40:57 +00:00
|
|
|
}
|
2020-02-07 04:36:26 +00:00
|
|
|
|
2020-07-21 08:59:48 +00:00
|
|
|
size_t GetFreeSize() const { return this->GetNumFreePages() * PageSize; }
|
2020-12-10 11:31:57 +00:00
|
|
|
void DumpFreeList() const;
|
2020-07-21 08:59:48 +00:00
|
|
|
|
2020-02-07 04:36:26 +00:00
|
|
|
void UpdateUsedSize() {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_used_size = m_heap_size - (this->GetNumFreePages() * PageSize);
|
2020-02-07 04:36:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 00:10:26 +00:00
|
|
|
KVirtualAddress AllocateBlock(s32 index, bool random);
|
2020-02-07 04:36:26 +00:00
|
|
|
void Free(KVirtualAddress addr, size_t num_pages);
|
2020-02-07 01:40:57 +00:00
|
|
|
private:
|
2020-08-25 23:12:14 +00:00
|
|
|
static size_t CalculateManagementOverheadSize(size_t region_size, const size_t *block_shifts, size_t num_block_shifts);
|
2020-02-07 01:40:57 +00:00
|
|
|
public:
|
2020-08-25 23:12:14 +00:00
|
|
|
static size_t CalculateManagementOverheadSize(size_t region_size) {
|
|
|
|
return CalculateManagementOverheadSize(region_size, MemoryBlockPageShifts, NumMemoryBlockPageShifts);
|
2020-02-07 01:40:57 +00:00
|
|
|
}
|
2020-01-29 09:49:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|