2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-04-05 21:14:26 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/alignment.h"
|
2022-02-02 18:59:36 +01:00
|
|
|
#include "common/common_funcs.h"
|
2020-04-05 21:14:26 +02:00
|
|
|
#include "common/common_types.h"
|
2021-02-12 03:48:02 +01:00
|
|
|
#include "core/hle/kernel/k_page_bitmap.h"
|
2021-02-13 02:02:51 +01:00
|
|
|
#include "core/hle/kernel/memory_types.h"
|
2020-04-05 21:14:26 +02:00
|
|
|
|
2021-02-13 02:58:31 +01:00
|
|
|
namespace Kernel {
|
2020-04-05 21:14:26 +02:00
|
|
|
|
2022-10-29 22:55:30 +02:00
|
|
|
class KPageHeap {
|
2020-04-05 21:14:26 +02:00
|
|
|
public:
|
2022-02-02 18:59:36 +01:00
|
|
|
KPageHeap() = default;
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
constexpr PAddr GetAddress() const {
|
|
|
|
return m_heap_address;
|
2022-02-02 18:59:36 +01:00
|
|
|
}
|
2022-02-22 02:52:36 +01:00
|
|
|
constexpr size_t GetSize() const {
|
|
|
|
return m_heap_size;
|
2022-02-02 18:59:36 +01:00
|
|
|
}
|
2022-02-22 02:52:36 +01:00
|
|
|
constexpr PAddr GetEndAddress() const {
|
|
|
|
return this->GetAddress() + this->GetSize();
|
2022-02-02 18:59:36 +01:00
|
|
|
}
|
2022-02-22 02:52:36 +01:00
|
|
|
constexpr size_t GetPageOffset(PAddr block) const {
|
|
|
|
return (block - this->GetAddress()) / PageSize;
|
|
|
|
}
|
|
|
|
constexpr size_t GetPageOffsetToEnd(PAddr block) const {
|
|
|
|
return (this->GetEndAddress() - block) / PageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Initialize(PAddr heap_address, size_t heap_size, VAddr management_address,
|
|
|
|
size_t management_size) {
|
|
|
|
return this->Initialize(heap_address, heap_size, management_address, management_size,
|
|
|
|
MemoryBlockPageShifts.data(), NumMemoryBlockPageShifts);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetFreeSize() const {
|
|
|
|
return this->GetNumFreePages() * PageSize;
|
2022-02-02 18:59:36 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
void SetInitialUsedSize(size_t reserved_size) {
|
|
|
|
// Check that the reserved size is valid.
|
|
|
|
const size_t free_size = this->GetNumFreePages() * PageSize;
|
|
|
|
ASSERT(m_heap_size >= free_size + reserved_size);
|
2022-02-02 18:59:36 +01:00
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
// Set the initial used size.
|
|
|
|
m_initial_used_size = m_heap_size - free_size - reserved_size;
|
2022-02-02 18:59:36 +01:00
|
|
|
}
|
|
|
|
|
2022-10-29 22:55:30 +02:00
|
|
|
PAddr AllocateBlock(s32 index, bool random) {
|
|
|
|
if (random) {
|
|
|
|
const size_t block_pages = m_blocks[index].GetNumPages();
|
|
|
|
return this->AllocateByRandom(index, block_pages, block_pages);
|
|
|
|
} else {
|
|
|
|
return this->AllocateByLinearSearch(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PAddr AllocateAligned(s32 index, size_t num_pages, size_t align_pages) {
|
|
|
|
// TODO: linear search support?
|
|
|
|
return this->AllocateByRandom(index, num_pages, align_pages);
|
|
|
|
}
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
void Free(PAddr addr, size_t num_pages);
|
|
|
|
|
|
|
|
static size_t CalculateManagementOverheadSize(size_t region_size) {
|
|
|
|
return CalculateManagementOverheadSize(region_size, MemoryBlockPageShifts.data(),
|
|
|
|
NumMemoryBlockPageShifts);
|
|
|
|
}
|
2022-02-02 18:59:36 +01:00
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
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++) {
|
2022-10-29 22:55:30 +02:00
|
|
|
if (target_pages <= (static_cast<size_t>(1) << MemoryBlockPageShifts[i]) / PageSize) {
|
2020-04-05 21:14:26 +02:00
|
|
|
return static_cast<s32>(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
static constexpr s32 GetBlockIndex(size_t num_pages) {
|
|
|
|
for (s32 i = static_cast<s32>(NumMemoryBlockPageShifts) - 1; i >= 0; i--) {
|
2022-10-29 22:55:30 +02:00
|
|
|
if (num_pages >= (static_cast<size_t>(1) << MemoryBlockPageShifts[i]) / PageSize) {
|
2020-04-05 21:14:26 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
static constexpr size_t GetBlockSize(size_t index) {
|
2022-10-29 22:55:30 +02:00
|
|
|
return static_cast<size_t>(1) << MemoryBlockPageShifts[index];
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
static constexpr size_t GetBlockNumPages(size_t index) {
|
2020-04-05 21:14:26 +02:00
|
|
|
return GetBlockSize(index) / PageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-10-29 22:55:30 +02:00
|
|
|
class Block {
|
2020-04-05 21:14:26 +02:00
|
|
|
public:
|
2021-02-12 03:48:02 +01:00
|
|
|
Block() = default;
|
2020-04-05 21:14:26 +02:00
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
constexpr size_t GetShift() const {
|
|
|
|
return m_block_shift;
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
2022-02-22 02:52:36 +01:00
|
|
|
constexpr size_t GetNextShift() const {
|
|
|
|
return m_next_block_shift;
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
2022-02-22 02:52:36 +01:00
|
|
|
constexpr size_t GetSize() const {
|
|
|
|
return u64(1) << this->GetShift();
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
2022-02-22 02:52:36 +01:00
|
|
|
constexpr size_t GetNumPages() const {
|
|
|
|
return this->GetSize() / PageSize;
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
2022-02-22 02:52:36 +01:00
|
|
|
constexpr size_t GetNumFreeBlocks() const {
|
|
|
|
return m_bitmap.GetNumBits();
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
2022-02-22 02:52:36 +01:00
|
|
|
constexpr size_t GetNumFreePages() const {
|
|
|
|
return this->GetNumFreeBlocks() * this->GetNumPages();
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
u64* Initialize(PAddr addr, size_t size, size_t bs, size_t nbs, u64* bit_storage) {
|
|
|
|
// Set shifts.
|
|
|
|
m_block_shift = bs;
|
|
|
|
m_next_block_shift = nbs;
|
|
|
|
|
|
|
|
// Align up the address.
|
|
|
|
PAddr end = addr + size;
|
|
|
|
const size_t align = (m_next_block_shift != 0) ? (u64(1) << m_next_block_shift)
|
|
|
|
: (u64(1) << m_block_shift);
|
|
|
|
addr = Common::AlignDown(addr, align);
|
|
|
|
end = Common::AlignUp(end, align);
|
|
|
|
|
|
|
|
m_heap_address = addr;
|
|
|
|
m_end_offset = (end - addr) / (u64(1) << m_block_shift);
|
|
|
|
return m_bitmap.Initialize(bit_storage, m_end_offset);
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
PAddr PushBlock(PAddr address) {
|
|
|
|
// Set the bit for the free block.
|
|
|
|
size_t offset = (address - m_heap_address) >> this->GetShift();
|
|
|
|
m_bitmap.SetBit(offset);
|
2020-04-05 21:14:26 +02:00
|
|
|
|
2022-02-22 02:52:36 +01: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());
|
2020-04-05 21:14:26 +02:00
|
|
|
offset = Common::AlignDown(offset, diff);
|
2022-02-22 02:52:36 +01:00
|
|
|
if (m_bitmap.ClearRange(offset, diff)) {
|
|
|
|
return m_heap_address + (offset << this->GetShift());
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
// We couldn't coalesce, or we're already as big as possible.
|
|
|
|
return {};
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
PAddr PopBlock(bool random) {
|
|
|
|
// Find a free block.
|
|
|
|
s64 soffset = m_bitmap.FindFreeBlock(random);
|
2020-04-05 21:14:26 +02:00
|
|
|
if (soffset < 0) {
|
2022-02-22 02:52:36 +01:00
|
|
|
return {};
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
2022-02-22 02:52:36 +01:00
|
|
|
const size_t offset = static_cast<size_t>(soffset);
|
2020-04-05 21:14:26 +02:00
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
// Update our tracking and return it.
|
|
|
|
m_bitmap.ClearBit(offset);
|
|
|
|
return m_heap_address + (offset << this->GetShift());
|
2020-04-05 21:14:26 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
public:
|
|
|
|
static constexpr size_t CalculateManagementOverheadSize(size_t region_size,
|
|
|
|
size_t cur_block_shift,
|
|
|
|
size_t next_block_shift) {
|
|
|
|
const size_t cur_block_size = (u64(1) << cur_block_shift);
|
|
|
|
const size_t next_block_size = (u64(1) << next_block_shift);
|
|
|
|
const size_t align = (next_block_shift != 0) ? next_block_size : cur_block_size;
|
2021-02-12 03:48:02 +01:00
|
|
|
return KPageBitmap::CalculateManagementOverheadSize(
|
2020-04-05 21:14:26 +02:00
|
|
|
(align * 2 + Common::AlignUp(region_size, align)) / cur_block_size);
|
|
|
|
}
|
|
|
|
|
2022-02-02 18:59:36 +01:00
|
|
|
private:
|
2022-02-22 02:52:36 +01:00
|
|
|
KPageBitmap m_bitmap;
|
|
|
|
PAddr m_heap_address{};
|
|
|
|
uintptr_t m_end_offset{};
|
|
|
|
size_t m_block_shift{};
|
|
|
|
size_t m_next_block_shift{};
|
2022-02-02 18:59:36 +01:00
|
|
|
};
|
2020-04-05 21:14:26 +02:00
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
private:
|
|
|
|
void Initialize(PAddr heap_address, size_t heap_size, VAddr management_address,
|
|
|
|
size_t management_size, const size_t* block_shifts, size_t num_block_shifts);
|
|
|
|
size_t GetNumFreePages() const;
|
2020-04-05 21:14:26 +02:00
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
void FreeBlock(PAddr block, s32 index);
|
2020-04-05 21:14:26 +02:00
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
static constexpr size_t NumMemoryBlockPageShifts{7};
|
|
|
|
static constexpr std::array<size_t, NumMemoryBlockPageShifts> MemoryBlockPageShifts{
|
2022-02-02 18:59:36 +01:00
|
|
|
0xC, 0x10, 0x15, 0x16, 0x19, 0x1D, 0x1E,
|
|
|
|
};
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
private:
|
2022-10-29 22:55:30 +02:00
|
|
|
PAddr AllocateByLinearSearch(s32 index);
|
|
|
|
PAddr AllocateByRandom(s32 index, size_t num_pages, size_t align_pages);
|
|
|
|
|
2022-02-22 02:52:36 +01:00
|
|
|
static size_t CalculateManagementOverheadSize(size_t region_size, const size_t* block_shifts,
|
|
|
|
size_t num_block_shifts);
|
|
|
|
|
|
|
|
private:
|
|
|
|
PAddr m_heap_address{};
|
|
|
|
size_t m_heap_size{};
|
|
|
|
size_t m_initial_used_size{};
|
|
|
|
size_t m_num_blocks{};
|
2022-10-29 22:55:30 +02:00
|
|
|
std::array<Block, NumMemoryBlockPageShifts> m_blocks;
|
|
|
|
KPageBitmap::RandomBitGenerator m_rng;
|
2022-02-22 02:52:36 +01:00
|
|
|
std::vector<u64> m_management_data;
|
2020-04-05 21:14:26 +02:00
|
|
|
};
|
|
|
|
|
2021-02-13 02:58:31 +01:00
|
|
|
} // namespace Kernel
|