2020-02-07 12:58:35 +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_slab_helpers.hpp>
|
|
|
|
#include <mesosphere/kern_k_dynamic_slab_heap.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
class PageTablePage {
|
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
u8 m_buffer[PageSize];
|
2021-06-17 20:03:46 +00:00
|
|
|
public:
|
|
|
|
ALWAYS_INLINE PageTablePage() { /* Do not initialize anything. */ }
|
2020-02-07 12:58:35 +00:00
|
|
|
};
|
|
|
|
static_assert(sizeof(PageTablePage) == PageSize);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-17 20:03:46 +00:00
|
|
|
class KPageTableManager : public KDynamicSlabHeap<impl::PageTablePage, true> {
|
2020-02-07 12:58:35 +00:00
|
|
|
public:
|
|
|
|
using RefCount = u16;
|
2020-02-15 08:00:35 +00:00
|
|
|
static constexpr size_t PageTableSize = sizeof(impl::PageTablePage);
|
|
|
|
static_assert(PageTableSize == PageSize);
|
2020-02-07 12:58:35 +00:00
|
|
|
private:
|
2021-06-17 20:03:46 +00:00
|
|
|
using BaseHeap = KDynamicSlabHeap<impl::PageTablePage, true>;
|
2020-02-07 12:58:35 +00:00
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
RefCount *m_ref_counts;
|
2020-02-07 12:58:35 +00:00
|
|
|
public:
|
|
|
|
static constexpr size_t CalculateReferenceCountSize(size_t size) {
|
|
|
|
return (size / PageSize) * sizeof(RefCount);
|
|
|
|
}
|
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KPageTableManager() : BaseHeap(), m_ref_counts() { /* ... */ }
|
2020-02-07 12:58:35 +00:00
|
|
|
private:
|
|
|
|
void Initialize(RefCount *rc) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_ref_counts = rc;
|
2020-02-07 12:58:35 +00:00
|
|
|
for (size_t i = 0; i < this->GetSize() / PageSize; i++) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_ref_counts[i] = 0;
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr RefCount *GetRefCountPointer(KVirtualAddress addr) const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return std::addressof(m_ref_counts[(addr - this->GetAddress()) / PageSize]);
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
public:
|
2020-04-19 07:35:05 +00:00
|
|
|
void Initialize(KDynamicPageManager *page_allocator, RefCount *rc) {
|
|
|
|
BaseHeap::Initialize(page_allocator);
|
2020-02-07 12:58:35 +00:00
|
|
|
this->Initialize(rc);
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:35:05 +00:00
|
|
|
void Initialize(KDynamicPageManager *page_allocator, size_t object_count, RefCount *rc) {
|
|
|
|
BaseHeap::Initialize(page_allocator, object_count);
|
2020-02-07 12:58:35 +00:00
|
|
|
this->Initialize(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
KVirtualAddress Allocate() {
|
|
|
|
return KVirtualAddress(BaseHeap::Allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Free(KVirtualAddress addr) {
|
2020-04-19 00:10:26 +00:00
|
|
|
/* Free the page. */
|
2020-02-07 12:58:35 +00:00
|
|
|
BaseHeap::Free(GetPointer<impl::PageTablePage>(addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
RefCount GetRefCount(KVirtualAddress addr) const {
|
|
|
|
MESOSPHERE_ASSERT(this->IsInRange(addr));
|
|
|
|
return *this->GetRefCountPointer(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Open(KVirtualAddress addr, int count) {
|
|
|
|
MESOSPHERE_ASSERT(this->IsInRange(addr));
|
|
|
|
|
|
|
|
*this->GetRefCountPointer(addr) += count;
|
|
|
|
|
|
|
|
MESOSPHERE_ABORT_UNLESS(this->GetRefCount(addr) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Close(KVirtualAddress addr, int count) {
|
|
|
|
MESOSPHERE_ASSERT(this->IsInRange(addr));
|
|
|
|
MESOSPHERE_ABORT_UNLESS(this->GetRefCount(addr) >= count);
|
|
|
|
|
|
|
|
*this->GetRefCountPointer(addr) -= count;
|
|
|
|
return this->GetRefCount(addr) == 0;
|
|
|
|
}
|
2020-02-14 01:38:56 +00:00
|
|
|
|
|
|
|
constexpr bool IsInPageTableHeap(KVirtualAddress addr) const {
|
|
|
|
return this->IsInRange(addr);
|
|
|
|
}
|
2020-02-07 12:58:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|