2020-02-07 12:58:35 +00:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-02-07 12:58:35 +00:00
|
|
|
*
|
|
|
|
* 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_typed_address.hpp>
|
|
|
|
#include <mesosphere/kern_slab_helpers.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
class KBlockInfoManager;
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
class KPageGroup;
|
|
|
|
|
|
|
|
class KBlockInfo {
|
|
|
|
private:
|
|
|
|
friend class KPageGroup;
|
2020-02-07 12:58:35 +00:00
|
|
|
private:
|
2021-10-23 23:25:20 +01:00
|
|
|
KBlockInfo *m_next;
|
|
|
|
u32 m_page_index;
|
|
|
|
u32 m_num_pages;
|
2020-02-07 12:58:35 +00:00
|
|
|
public:
|
2021-10-23 23:25:20 +01:00
|
|
|
KBlockInfo() : m_next(nullptr) { /* ... */ }
|
2021-09-18 08:11:10 +01:00
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE void Initialize(KPhysicalAddress addr, size_t np) {
|
|
|
|
MESOSPHERE_ASSERT(util::IsAligned(GetInteger(addr), PageSize));
|
|
|
|
MESOSPHERE_ASSERT(static_cast<u32>(np) == np);
|
2020-02-07 12:58:35 +00:00
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
m_page_index = GetInteger(addr) / PageSize;
|
|
|
|
m_num_pages = np;
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
constexpr ALWAYS_INLINE KPhysicalAddress GetAddress() const { return m_page_index * PageSize; }
|
|
|
|
constexpr ALWAYS_INLINE size_t GetNumPages() const { return m_num_pages; }
|
|
|
|
constexpr ALWAYS_INLINE size_t GetSize() const { return this->GetNumPages() * PageSize; }
|
|
|
|
constexpr ALWAYS_INLINE KPhysicalAddress GetEndAddress() const { return (m_page_index + m_num_pages) * PageSize; }
|
|
|
|
constexpr ALWAYS_INLINE KPhysicalAddress GetLastAddress() const { return this->GetEndAddress() - 1; }
|
2020-02-07 12:58:35 +00:00
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
constexpr ALWAYS_INLINE KBlockInfo *GetNext() const { return m_next; }
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE bool IsEquivalentTo(const KBlockInfo &rhs) const {
|
|
|
|
return m_page_index == rhs.m_page_index && m_num_pages == rhs.m_num_pages;
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
constexpr ALWAYS_INLINE bool operator==(const KBlockInfo &rhs) const {
|
2020-02-07 12:58:35 +00:00
|
|
|
return this->IsEquivalentTo(rhs);
|
|
|
|
}
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
constexpr ALWAYS_INLINE bool operator!=(const KBlockInfo &rhs) const {
|
2020-02-07 12:58:35 +00:00
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
constexpr ALWAYS_INLINE bool IsStrictlyBefore(KPhysicalAddress addr) const {
|
|
|
|
const KPhysicalAddress end = this->GetEndAddress();
|
2020-02-07 12:58:35 +00:00
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
if (m_page_index != 0 && end == Null<KPhysicalAddress>) {
|
2020-02-07 12:58:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return end < addr;
|
|
|
|
}
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
constexpr ALWAYS_INLINE bool operator<(KPhysicalAddress addr) const {
|
2020-02-07 12:58:35 +00:00
|
|
|
return this->IsStrictlyBefore(addr);
|
|
|
|
}
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
constexpr ALWAYS_INLINE bool TryConcatenate(KPhysicalAddress addr, size_t np) {
|
|
|
|
if (addr != Null<KPhysicalAddress> && addr == this->GetEndAddress()) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_num_pages += np;
|
2020-02-07 12:58:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-18 08:11:10 +01:00
|
|
|
private:
|
|
|
|
constexpr ALWAYS_INLINE void SetNext(KBlockInfo *next) {
|
|
|
|
m_next = next;
|
|
|
|
}
|
2020-02-07 12:58:35 +00:00
|
|
|
};
|
2021-09-18 08:11:10 +01:00
|
|
|
static_assert(sizeof(KBlockInfo) <= 0x10);
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
class KPageGroup {
|
|
|
|
public:
|
2021-09-18 08:11:10 +01:00
|
|
|
class Iterator {
|
|
|
|
public:
|
|
|
|
using iterator_category = std::forward_iterator_tag;
|
|
|
|
using value_type = const KBlockInfo;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using pointer = value_type *;
|
|
|
|
using reference = value_type &;
|
|
|
|
private:
|
|
|
|
pointer m_node;
|
|
|
|
public:
|
|
|
|
constexpr explicit ALWAYS_INLINE Iterator(pointer n) : m_node(n) { /* ... */ }
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE bool operator==(const Iterator &rhs) const { return m_node == rhs.m_node; }
|
|
|
|
constexpr ALWAYS_INLINE bool operator!=(const Iterator &rhs) const { return !(*this == rhs); }
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE pointer operator->() const { return m_node; }
|
|
|
|
constexpr ALWAYS_INLINE reference operator*() const { return *m_node; }
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE Iterator &operator++() {
|
|
|
|
m_node = m_node->GetNext();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE Iterator operator++(int) {
|
|
|
|
const Iterator it{*this};
|
|
|
|
++(*this);
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
};
|
2020-02-07 12:58:35 +00:00
|
|
|
private:
|
2021-09-18 08:11:10 +01:00
|
|
|
KBlockInfo *m_first_block;
|
|
|
|
KBlockInfo *m_last_block;
|
2020-12-18 01:18:47 +00:00
|
|
|
KBlockInfoManager *m_manager;
|
2020-02-07 12:58:35 +00:00
|
|
|
public:
|
2021-09-18 08:11:10 +01:00
|
|
|
explicit KPageGroup(KBlockInfoManager *m) : m_first_block(), m_last_block(), m_manager(m) { /* ... */ }
|
2020-02-14 01:38:56 +00:00
|
|
|
~KPageGroup() { this->Finalize(); }
|
2020-02-07 12:58:35 +00:00
|
|
|
|
2021-09-18 00:54:49 +01:00
|
|
|
void CloseAndReset();
|
2020-02-07 12:58:35 +00:00
|
|
|
void Finalize();
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
ALWAYS_INLINE Iterator begin() const { return Iterator{m_first_block}; }
|
|
|
|
ALWAYS_INLINE Iterator end() const { return Iterator{nullptr}; }
|
|
|
|
ALWAYS_INLINE bool empty() const { return m_first_block == nullptr; }
|
2020-02-07 12:58:35 +00:00
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
Result AddBlock(KPhysicalAddress addr, size_t num_pages);
|
2020-02-07 12:58:35 +00:00
|
|
|
void Open() const;
|
2022-10-12 06:37:43 +01:00
|
|
|
void OpenFirst() const;
|
2020-02-07 12:58:35 +00:00
|
|
|
void Close() const;
|
|
|
|
|
|
|
|
size_t GetNumPages() const;
|
|
|
|
|
|
|
|
bool IsEquivalentTo(const KPageGroup &rhs) const;
|
|
|
|
|
2024-03-28 09:50:37 +00:00
|
|
|
Result CopyRangeTo(KPageGroup &out, size_t offset, size_t size) const;
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
ALWAYS_INLINE bool operator==(const KPageGroup &rhs) const {
|
2020-02-07 12:58:35 +00:00
|
|
|
return this->IsEquivalentTo(rhs);
|
|
|
|
}
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
ALWAYS_INLINE bool operator!=(const KPageGroup &rhs) const {
|
2020-02-07 12:58:35 +00:00
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-14 01:38:56 +00:00
|
|
|
class KScopedPageGroup {
|
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
const KPageGroup *m_pg;
|
2020-02-14 01:38:56 +00:00
|
|
|
public:
|
2023-10-11 17:32:23 +01:00
|
|
|
explicit ALWAYS_INLINE KScopedPageGroup(const KPageGroup *gp, bool not_first = true) : m_pg(gp) {
|
|
|
|
if (m_pg) {
|
|
|
|
if (not_first) {
|
|
|
|
m_pg->Open();
|
|
|
|
} else {
|
|
|
|
m_pg->OpenFirst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
explicit ALWAYS_INLINE KScopedPageGroup(const KPageGroup &gp, bool not_first = true) : KScopedPageGroup(std::addressof(gp), not_first) { /* ... */ }
|
2020-12-18 01:18:47 +00:00
|
|
|
ALWAYS_INLINE ~KScopedPageGroup() { if (m_pg) { m_pg->Close(); } }
|
2020-02-14 01:38:56 +00:00
|
|
|
|
2020-02-20 03:38:20 +00:00
|
|
|
ALWAYS_INLINE void CancelClose() {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_pg = nullptr;
|
2020-02-20 03:38:20 +00:00
|
|
|
}
|
2020-02-14 01:38:56 +00:00
|
|
|
};
|
|
|
|
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|