2022-06-29 23:27:49 +00:00
|
|
|
// SPDX-FileCopyrightText: 2021 Skyline Team and Contributors
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2021-11-14 19:55:52 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <concepts>
|
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
template <typename VaType, size_t AddressSpaceBits>
|
2023-01-29 20:54:13 +00:00
|
|
|
concept AddressSpaceValid = std::is_unsigned_v<VaType> && sizeof(VaType) * 8 >=
|
|
|
|
AddressSpaceBits;
|
2021-11-14 19:55:52 +00:00
|
|
|
|
|
|
|
struct EmptyStruct {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FlatAddressSpaceMap provides a generic VA->PA mapping implementation using a sorted vector
|
|
|
|
*/
|
|
|
|
template <typename VaType, VaType UnmappedVa, typename PaType, PaType UnmappedPa,
|
|
|
|
bool PaContigSplit, size_t AddressSpaceBits, typename ExtraBlockInfo = EmptyStruct>
|
2023-01-29 20:54:13 +00:00
|
|
|
requires AddressSpaceValid<VaType, AddressSpaceBits>
|
2022-04-13 19:02:55 +00:00
|
|
|
class FlatAddressSpaceMap {
|
2022-06-30 00:33:04 +00:00
|
|
|
public:
|
|
|
|
/// The maximum VA that this AS can technically reach
|
|
|
|
static constexpr VaType VaMaximum{(1ULL << (AddressSpaceBits - 1)) +
|
|
|
|
((1ULL << (AddressSpaceBits - 1)) - 1)};
|
|
|
|
|
|
|
|
explicit FlatAddressSpaceMap(VaType va_limit,
|
|
|
|
std::function<void(VaType, VaType)> unmap_callback = {});
|
|
|
|
|
|
|
|
FlatAddressSpaceMap() = default;
|
|
|
|
|
|
|
|
void Map(VaType virt, PaType phys, VaType size, ExtraBlockInfo extra_info = {}) {
|
|
|
|
std::scoped_lock lock(block_mutex);
|
|
|
|
MapLocked(virt, phys, size, extra_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unmap(VaType virt, VaType size) {
|
|
|
|
std::scoped_lock lock(block_mutex);
|
|
|
|
UnmapLocked(virt, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
VaType GetVALimit() const {
|
|
|
|
return va_limit;
|
|
|
|
}
|
2021-11-14 19:55:52 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* @brief Represents a block of memory in the AS, the physical mapping is contiguous until
|
|
|
|
* another block with a different phys address is hit
|
|
|
|
*/
|
|
|
|
struct Block {
|
2022-06-30 00:33:04 +00:00
|
|
|
/// VA of the block
|
|
|
|
VaType virt{UnmappedVa};
|
|
|
|
/// PA of the block, will increase 1-1 with VA until a new block is encountered
|
|
|
|
PaType phys{UnmappedPa};
|
|
|
|
[[no_unique_address]] ExtraBlockInfo extra_info;
|
2021-11-14 19:55:52 +00:00
|
|
|
|
|
|
|
Block() = default;
|
|
|
|
|
2022-06-30 00:33:04 +00:00
|
|
|
Block(VaType virt_, PaType phys_, ExtraBlockInfo extra_info_)
|
|
|
|
: virt(virt_), phys(phys_), extra_info(extra_info_) {}
|
2021-11-14 19:55:52 +00:00
|
|
|
|
2022-06-30 00:33:04 +00:00
|
|
|
bool Valid() const {
|
2021-11-14 19:55:52 +00:00
|
|
|
return virt != UnmappedVa;
|
|
|
|
}
|
|
|
|
|
2022-06-30 00:33:04 +00:00
|
|
|
bool Mapped() const {
|
2021-11-14 19:55:52 +00:00
|
|
|
return phys != UnmappedPa;
|
|
|
|
}
|
|
|
|
|
2022-06-30 00:33:04 +00:00
|
|
|
bool Unmapped() const {
|
2021-11-14 19:55:52 +00:00
|
|
|
return phys == UnmappedPa;
|
|
|
|
}
|
|
|
|
|
2022-06-30 00:33:04 +00:00
|
|
|
bool operator<(const VaType& p_virt) const {
|
|
|
|
return virt < p_virt;
|
2021-11-14 19:55:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Maps a PA range into the given AS region
|
2022-06-30 00:33:04 +00:00
|
|
|
* @note block_mutex MUST be locked when calling this
|
2021-11-14 19:55:52 +00:00
|
|
|
*/
|
2022-06-30 00:33:04 +00:00
|
|
|
void MapLocked(VaType virt, PaType phys, VaType size, ExtraBlockInfo extra_info);
|
2021-11-14 19:55:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Unmaps the given range and merges it with other unmapped regions
|
2022-06-30 00:33:04 +00:00
|
|
|
* @note block_mutex MUST be locked when calling this
|
2021-11-14 19:55:52 +00:00
|
|
|
*/
|
|
|
|
void UnmapLocked(VaType virt, VaType size);
|
|
|
|
|
2022-06-30 00:33:04 +00:00
|
|
|
std::mutex block_mutex;
|
|
|
|
std::vector<Block> blocks{Block{}};
|
2021-11-14 19:55:52 +00:00
|
|
|
|
2022-06-30 00:33:04 +00:00
|
|
|
/// a soft limit on the maximum VA of the AS
|
|
|
|
VaType va_limit{VaMaximum};
|
2021-11-14 19:55:52 +00:00
|
|
|
|
2022-06-30 00:33:04 +00:00
|
|
|
private:
|
|
|
|
/// Callback called when the mappings in an region have changed
|
|
|
|
std::function<void(VaType, VaType)> unmap_callback{};
|
2021-11-14 19:55:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FlatMemoryManager specialises FlatAddressSpaceMap to work as an allocator, with an
|
|
|
|
* initial, fast linear pass and a subsequent slower pass that iterates until it finds a free block
|
|
|
|
*/
|
|
|
|
template <typename VaType, VaType UnmappedVa, size_t AddressSpaceBits>
|
2023-01-29 20:54:13 +00:00
|
|
|
requires AddressSpaceValid<VaType, AddressSpaceBits>
|
2022-04-13 19:02:55 +00:00
|
|
|
class FlatAllocator
|
2021-11-14 19:55:52 +00:00
|
|
|
: public FlatAddressSpaceMap<VaType, UnmappedVa, bool, false, false, AddressSpaceBits> {
|
|
|
|
private:
|
|
|
|
using Base = FlatAddressSpaceMap<VaType, UnmappedVa, bool, false, false, AddressSpaceBits>;
|
|
|
|
|
|
|
|
public:
|
2022-06-30 00:36:39 +00:00
|
|
|
explicit FlatAllocator(VaType virt_start, VaType va_limit = Base::VaMaximum);
|
2021-11-14 19:55:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Allocates a region in the AS of the given size and returns its address
|
|
|
|
*/
|
|
|
|
VaType Allocate(VaType size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Marks the given region in the AS as allocated
|
|
|
|
*/
|
|
|
|
void AllocateFixed(VaType virt, VaType size);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Frees an AS region so it can be used again
|
|
|
|
*/
|
|
|
|
void Free(VaType virt, VaType size);
|
2022-06-30 00:33:04 +00:00
|
|
|
|
|
|
|
VaType GetVAStart() const {
|
2022-06-30 00:36:39 +00:00
|
|
|
return virt_start;
|
2022-06-30 00:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// The base VA of the allocator, no allocations will be below this
|
2022-06-30 00:36:39 +00:00
|
|
|
VaType virt_start;
|
2022-06-30 00:33:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The end address for the initial linear allocation pass
|
|
|
|
* Once this reaches the AS limit the slower allocation path will be used
|
|
|
|
*/
|
|
|
|
VaType current_linear_alloc_end;
|
2021-11-14 19:55:52 +00:00
|
|
|
};
|
|
|
|
} // namespace Common
|