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/>.
|
|
|
|
*/
|
|
|
|
#include <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
void KPageGroup::Finalize() {
|
2021-09-18 08:11:10 +01:00
|
|
|
KBlockInfo *cur = m_first_block;
|
|
|
|
while (cur != nullptr) {
|
|
|
|
KBlockInfo *next = cur->GetNext();
|
|
|
|
m_manager->Free(cur);
|
|
|
|
cur = next;
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
2021-09-18 08:11:10 +01:00
|
|
|
|
|
|
|
m_first_block = nullptr;
|
|
|
|
m_last_block = nullptr;
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-18 00:54:49 +01:00
|
|
|
void KPageGroup::CloseAndReset() {
|
|
|
|
auto &mm = Kernel::GetMemoryManager();
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
KBlockInfo *cur = m_first_block;
|
|
|
|
while (cur != nullptr) {
|
|
|
|
KBlockInfo *next = cur->GetNext();
|
|
|
|
mm.Close(cur->GetAddress(), cur->GetNumPages());
|
|
|
|
m_manager->Free(cur);
|
|
|
|
cur = next;
|
2021-09-18 00:54:49 +01:00
|
|
|
}
|
2021-09-18 08:11:10 +01:00
|
|
|
|
|
|
|
m_first_block = nullptr;
|
|
|
|
m_last_block = nullptr;
|
2021-09-18 00:54:49 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 12:58:35 +00:00
|
|
|
size_t KPageGroup::GetNumPages() const {
|
|
|
|
size_t num_pages = 0;
|
|
|
|
|
|
|
|
for (const auto &it : *this) {
|
|
|
|
num_pages += it.GetNumPages();
|
|
|
|
}
|
|
|
|
|
|
|
|
return num_pages;
|
|
|
|
}
|
|
|
|
|
2021-09-18 08:11:10 +01:00
|
|
|
Result KPageGroup::AddBlock(KPhysicalAddress addr, size_t num_pages) {
|
2020-02-07 12:58:35 +00:00
|
|
|
/* Succeed immediately if we're adding no pages. */
|
2020-03-08 08:06:23 +00:00
|
|
|
R_SUCCEED_IF(num_pages == 0);
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
/* Check for overflow. */
|
|
|
|
MESOSPHERE_ASSERT(addr < addr + num_pages * PageSize);
|
|
|
|
|
|
|
|
/* Try to just append to the last block. */
|
2021-09-18 08:11:10 +01:00
|
|
|
if (m_last_block != nullptr) {
|
|
|
|
R_SUCCEED_IF(m_last_block->TryConcatenate(addr, num_pages));
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a new block. */
|
2020-12-18 01:18:47 +00:00
|
|
|
KBlockInfo *new_block = m_manager->Allocate();
|
2020-02-07 12:58:35 +00:00
|
|
|
R_UNLESS(new_block != nullptr, svc::ResultOutOfResource());
|
|
|
|
|
|
|
|
/* Initialize the block. */
|
|
|
|
new_block->Initialize(addr, num_pages);
|
2021-09-18 08:11:10 +01:00
|
|
|
|
|
|
|
/* Add the block to our list. */
|
|
|
|
if (m_last_block != nullptr) {
|
|
|
|
m_last_block->SetNext(new_block);
|
|
|
|
} else {
|
|
|
|
m_first_block = new_block;
|
|
|
|
}
|
|
|
|
m_last_block = new_block;
|
2020-02-07 12:58:35 +00:00
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KPageGroup::Open() const {
|
|
|
|
auto &mm = Kernel::GetMemoryManager();
|
|
|
|
|
|
|
|
for (const auto &it : *this) {
|
|
|
|
mm.Open(it.GetAddress(), it.GetNumPages());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KPageGroup::Close() const {
|
|
|
|
auto &mm = Kernel::GetMemoryManager();
|
|
|
|
|
|
|
|
for (const auto &it : *this) {
|
|
|
|
mm.Close(it.GetAddress(), it.GetNumPages());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KPageGroup::IsEquivalentTo(const KPageGroup &rhs) const {
|
2021-09-18 08:11:10 +01:00
|
|
|
auto lit = this->begin();
|
|
|
|
auto rit = rhs.begin();
|
|
|
|
auto lend = this->end();
|
|
|
|
auto rend = rhs.end();
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
while (lit != lend && rit != rend) {
|
|
|
|
if (*lit != *rit) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
++lit;
|
|
|
|
++rit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lit == lend && rit == rend;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|