2020-08-23 20:19:45 +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
|
|
|
|
#include <mesosphere/kern_common.hpp>
|
|
|
|
#include <mesosphere/kern_k_memory_region_type.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
class KMemoryRegionTree;
|
|
|
|
|
|
|
|
class KMemoryRegion : public util::IntrusiveRedBlackTreeBaseNode<KMemoryRegion> {
|
|
|
|
NON_COPYABLE(KMemoryRegion);
|
|
|
|
NON_MOVEABLE(KMemoryRegion);
|
|
|
|
private:
|
|
|
|
friend class KMemoryRegionTree;
|
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
uintptr_t m_address;
|
|
|
|
uintptr_t m_pair_address;
|
|
|
|
uintptr_t m_last_address;
|
|
|
|
u32 m_attributes;
|
|
|
|
u32 m_type_id;
|
2020-08-23 20:19:45 +00:00
|
|
|
public:
|
|
|
|
static constexpr ALWAYS_INLINE int Compare(const KMemoryRegion &lhs, const KMemoryRegion &rhs) {
|
|
|
|
if (lhs.GetAddress() < rhs.GetAddress()) {
|
|
|
|
return -1;
|
|
|
|
} else if (lhs.GetAddress() <= rhs.GetLastAddress()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ALWAYS_INLINE KMemoryRegion() : m_address(0), m_pair_address(0), m_last_address(0), m_attributes(0), m_type_id(0) { /* ... */ }
|
2020-12-02 00:42:25 +00:00
|
|
|
constexpr ALWAYS_INLINE KMemoryRegion(uintptr_t a, size_t la, uintptr_t p, u32 r, u32 t) :
|
2020-12-18 01:18:47 +00:00
|
|
|
m_address(a), m_pair_address(p), m_last_address(la), m_attributes(r), m_type_id(t)
|
2020-08-23 20:19:45 +00:00
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
2020-12-02 00:42:25 +00:00
|
|
|
constexpr ALWAYS_INLINE KMemoryRegion(uintptr_t a, size_t la, u32 r, u32 t) : KMemoryRegion(a, la, std::numeric_limits<uintptr_t>::max(), r, t) { /* ... */ }
|
2020-08-23 20:19:45 +00:00
|
|
|
private:
|
2020-12-02 00:42:25 +00:00
|
|
|
constexpr ALWAYS_INLINE void Reset(uintptr_t a, uintptr_t la, uintptr_t p, u32 r, u32 t) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_address = a;
|
|
|
|
m_pair_address = p;
|
|
|
|
m_last_address = la;
|
|
|
|
m_attributes = r;
|
|
|
|
m_type_id = t;
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
constexpr ALWAYS_INLINE uintptr_t GetAddress() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_address;
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE uintptr_t GetPairAddress() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_pair_address;
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 00:42:25 +00:00
|
|
|
constexpr ALWAYS_INLINE uintptr_t GetLastAddress() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_last_address;
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE uintptr_t GetEndAddress() const {
|
2020-12-02 00:42:25 +00:00
|
|
|
return this->GetLastAddress() + 1;
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 00:42:25 +00:00
|
|
|
constexpr ALWAYS_INLINE size_t GetSize() const {
|
|
|
|
return this->GetEndAddress() - this->GetAddress();
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE u32 GetAttributes() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_attributes;
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE u32 GetType() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_type_id;
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE void SetType(u32 type) {
|
|
|
|
MESOSPHERE_INIT_ABORT_UNLESS(this->CanDerive(type));
|
2020-12-18 01:18:47 +00:00
|
|
|
m_type_id = type;
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE bool Contains(uintptr_t address) const {
|
2020-12-02 01:14:23 +00:00
|
|
|
MESOSPHERE_INIT_ABORT_UNLESS(this->GetEndAddress() != 0);
|
2020-08-23 20:19:45 +00:00
|
|
|
return this->GetAddress() <= address && address <= this->GetLastAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE bool IsDerivedFrom(u32 type) const {
|
|
|
|
return (this->GetType() | type) == this->GetType();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE bool HasTypeAttribute(KMemoryRegionAttr attr) const {
|
|
|
|
return (this->GetType() | attr) == this->GetType();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE bool CanDerive(u32 type) const {
|
|
|
|
return (this->GetType() | type) == type;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE void SetPairAddress(uintptr_t a) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_pair_address = a;
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE void SetTypeAttribute(KMemoryRegionAttr attr) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_type_id |= attr;
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
static_assert(std::is_trivially_destructible<KMemoryRegion>::value);
|
|
|
|
|
|
|
|
class KMemoryRegionTree {
|
|
|
|
public:
|
|
|
|
struct DerivedRegionExtents {
|
|
|
|
const KMemoryRegion *first_region;
|
|
|
|
const KMemoryRegion *last_region;
|
|
|
|
|
|
|
|
constexpr DerivedRegionExtents() : first_region(nullptr), last_region(nullptr) { /* ... */ }
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE uintptr_t GetAddress() const {
|
|
|
|
return this->first_region->GetAddress();
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:42:25 +00:00
|
|
|
constexpr ALWAYS_INLINE uintptr_t GetLastAddress() const {
|
|
|
|
return this->last_region->GetLastAddress();
|
|
|
|
}
|
|
|
|
|
2020-08-23 20:19:45 +00:00
|
|
|
constexpr ALWAYS_INLINE uintptr_t GetEndAddress() const {
|
2020-12-02 00:42:25 +00:00
|
|
|
return this->GetLastAddress() + 1;
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE size_t GetSize() const {
|
|
|
|
return this->GetEndAddress() - this->GetAddress();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
using TreeType = util::IntrusiveRedBlackTreeBaseTraits<KMemoryRegion>::TreeType<KMemoryRegion>;
|
|
|
|
public:
|
|
|
|
using value_type = TreeType::value_type;
|
|
|
|
using size_type = TreeType::size_type;
|
|
|
|
using difference_type = TreeType::difference_type;
|
|
|
|
using pointer = TreeType::pointer;
|
|
|
|
using const_pointer = TreeType::const_pointer;
|
|
|
|
using reference = TreeType::reference;
|
|
|
|
using const_reference = TreeType::const_reference;
|
|
|
|
using iterator = TreeType::iterator;
|
|
|
|
using const_iterator = TreeType::const_iterator;
|
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
TreeType m_tree;
|
2020-08-23 20:19:45 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ALWAYS_INLINE KMemoryRegionTree() : m_tree() { /* ... */ }
|
2020-08-23 20:19:45 +00:00
|
|
|
public:
|
|
|
|
KMemoryRegion *FindModifiable(uintptr_t address) {
|
2020-12-02 00:42:25 +00:00
|
|
|
if (auto it = this->find(KMemoryRegion(address, address, 0, 0)); it != this->end()) {
|
2020-08-23 20:19:45 +00:00
|
|
|
return std::addressof(*it);
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const KMemoryRegion *Find(uintptr_t address) const {
|
2020-12-02 00:42:25 +00:00
|
|
|
if (auto it = this->find(KMemoryRegion(address, address, 0, 0)); it != this->cend()) {
|
2020-08-23 20:19:45 +00:00
|
|
|
return std::addressof(*it);
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const KMemoryRegion *FindByType(u32 type_id) const {
|
|
|
|
for (auto it = this->cbegin(); it != this->cend(); ++it) {
|
|
|
|
if (it->GetType() == type_id) {
|
|
|
|
return std::addressof(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const KMemoryRegion *FindByTypeAndAttribute(u32 type_id, u32 attr) const {
|
|
|
|
for (auto it = this->cbegin(); it != this->cend(); ++it) {
|
|
|
|
if (it->GetType() == type_id && it->GetAttributes() == attr) {
|
|
|
|
return std::addressof(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const KMemoryRegion *FindFirstDerived(u32 type_id) const {
|
|
|
|
for (auto it = this->cbegin(); it != this->cend(); it++) {
|
|
|
|
if (it->IsDerivedFrom(type_id)) {
|
|
|
|
return std::addressof(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const KMemoryRegion *FindLastDerived(u32 type_id) const {
|
|
|
|
const KMemoryRegion *region = nullptr;
|
|
|
|
for (auto it = this->begin(); it != this->end(); it++) {
|
|
|
|
if (it->IsDerivedFrom(type_id)) {
|
|
|
|
region = std::addressof(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DerivedRegionExtents GetDerivedRegionExtents(u32 type_id) const {
|
|
|
|
DerivedRegionExtents extents;
|
|
|
|
|
|
|
|
MESOSPHERE_INIT_ABORT_UNLESS(extents.first_region == nullptr);
|
|
|
|
MESOSPHERE_INIT_ABORT_UNLESS(extents.last_region == nullptr);
|
|
|
|
|
|
|
|
for (auto it = this->cbegin(); it != this->cend(); it++) {
|
|
|
|
if (it->IsDerivedFrom(type_id)) {
|
|
|
|
if (extents.first_region == nullptr) {
|
|
|
|
extents.first_region = std::addressof(*it);
|
|
|
|
}
|
|
|
|
extents.last_region = std::addressof(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MESOSPHERE_INIT_ABORT_UNLESS(extents.first_region != nullptr);
|
|
|
|
MESOSPHERE_INIT_ABORT_UNLESS(extents.last_region != nullptr);
|
|
|
|
|
|
|
|
return extents;
|
|
|
|
}
|
|
|
|
public:
|
2020-12-02 00:42:25 +00:00
|
|
|
NOINLINE void InsertDirectly(uintptr_t address, uintptr_t last_address, u32 attr = 0, u32 type_id = 0);
|
2020-08-23 20:19:45 +00:00
|
|
|
NOINLINE bool Insert(uintptr_t address, size_t size, u32 type_id, u32 new_attr = 0, u32 old_attr = 0);
|
|
|
|
|
|
|
|
NOINLINE KVirtualAddress GetRandomAlignedRegion(size_t size, size_t alignment, u32 type_id);
|
|
|
|
|
|
|
|
ALWAYS_INLINE KVirtualAddress GetRandomAlignedRegionWithGuard(size_t size, size_t alignment, u32 type_id, size_t guard_size) {
|
|
|
|
return this->GetRandomAlignedRegion(size + 2 * guard_size, alignment, type_id) + guard_size;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
/* Iterator accessors. */
|
|
|
|
iterator begin() {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.begin();
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator begin() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.begin();
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator end() {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.end();
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator end() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.end();
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator cbegin() const {
|
|
|
|
return this->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator cend() const {
|
|
|
|
return this->end();
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator iterator_to(reference ref) {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.iterator_to(ref);
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator iterator_to(const_reference ref) const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.iterator_to(ref);
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Content management. */
|
|
|
|
bool empty() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.empty();
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reference back() {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.back();
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_reference back() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.back();
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reference front() {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.front();
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_reference front() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.front();
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GCC over-eagerly inlines this operation. */
|
|
|
|
NOINLINE iterator insert(reference ref) {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.insert(ref);
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NOINLINE iterator erase(iterator it) {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.erase(it);
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator find(const_reference ref) const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.find(ref);
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator nfind(const_reference ref) const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_tree.nfind(ref);
|
2020-08-23 20:19:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|