2026-01-13 00:27:31 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-03-02 15:20:28 -05:00
|
|
|
|
|
|
|
|
#include "common/page_table.h"
|
2024-01-07 13:59:48 -05:00
|
|
|
#include "common/scope_exit.h"
|
2019-03-02 15:20:28 -05:00
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
2020-04-08 22:49:51 -04:00
|
|
|
PageTable::PageTable() = default;
|
2019-03-02 15:20:28 -05:00
|
|
|
|
2020-11-17 19:58:41 -05:00
|
|
|
PageTable::~PageTable() noexcept = default;
|
2019-03-02 15:20:28 -05:00
|
|
|
|
2023-10-22 21:16:38 -04:00
|
|
|
bool PageTable::BeginTraversal(TraversalEntry* out_entry, TraversalContext* out_context,
|
|
|
|
|
Common::ProcessAddress address) const {
|
2024-01-07 13:59:48 -05:00
|
|
|
out_context->next_offset = GetInteger(address);
|
|
|
|
|
out_context->next_page = address / page_size;
|
2022-02-18 23:42:27 -08:00
|
|
|
|
2024-01-07 13:59:48 -05:00
|
|
|
return this->ContinueTraversal(out_entry, out_context);
|
2022-02-18 23:42:27 -08:00
|
|
|
}
|
|
|
|
|
|
2023-10-22 21:16:38 -04:00
|
|
|
bool PageTable::ContinueTraversal(TraversalEntry* out_entry, TraversalContext* context) const {
|
2022-02-18 23:42:27 -08:00
|
|
|
// Setup invalid defaults.
|
2023-10-22 21:16:38 -04:00
|
|
|
out_entry->phys_addr = 0;
|
|
|
|
|
out_entry->block_size = page_size;
|
2022-02-18 23:42:27 -08:00
|
|
|
// Validate that we can read the actual entry.
|
2026-01-13 00:27:31 +01:00
|
|
|
if (auto const page = context->next_page; page < entries.size()) {
|
|
|
|
|
// Validate that the entry is mapped.
|
|
|
|
|
if (auto const paddr = entries[page].addr; paddr != 0) {
|
|
|
|
|
// Populate the results.
|
|
|
|
|
out_entry->phys_addr = paddr + context->next_offset;
|
|
|
|
|
context->next_page += 1;
|
|
|
|
|
context->next_offset += page_size;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-02-18 23:42:27 -08:00
|
|
|
}
|
2026-01-13 00:27:31 +01:00
|
|
|
context->next_page += 1;
|
|
|
|
|
context->next_offset += page_size;
|
|
|
|
|
return false;
|
2022-02-18 23:42:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PageTable::Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits) {
|
2026-01-13 00:27:31 +01:00
|
|
|
auto const num_page_table_entries = 1ULL << (address_space_width_in_bits - page_size_in_bits);
|
|
|
|
|
entries.resize(num_page_table_entries);
|
2021-05-29 09:24:09 +02:00
|
|
|
current_address_space_width_in_bits = address_space_width_in_bits;
|
2022-02-18 23:42:27 -08:00
|
|
|
page_size = 1ULL << page_size_in_bits;
|
2019-03-02 15:20:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Common
|