2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-03-02 21:20:28 +01:00
|
|
|
|
|
|
|
#include "common/page_table.h"
|
2024-01-07 19:59:48 +01:00
|
|
|
#include "common/scope_exit.h"
|
2019-03-02 21:20:28 +01:00
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2020-04-09 04:49:51 +02:00
|
|
|
PageTable::PageTable() = default;
|
2019-03-02 21:20:28 +01:00
|
|
|
|
2020-11-18 01:58:41 +01:00
|
|
|
PageTable::~PageTable() noexcept = default;
|
2019-03-02 21:20:28 +01:00
|
|
|
|
2023-10-23 03:16:38 +02:00
|
|
|
bool PageTable::BeginTraversal(TraversalEntry* out_entry, TraversalContext* out_context,
|
|
|
|
Common::ProcessAddress address) const {
|
2024-01-07 19:59:48 +01:00
|
|
|
out_context->next_offset = GetInteger(address);
|
|
|
|
out_context->next_page = address / page_size;
|
2022-02-19 08:42:27 +01:00
|
|
|
|
2024-01-07 19:59:48 +01:00
|
|
|
return this->ContinueTraversal(out_entry, out_context);
|
2022-02-19 08:42:27 +01:00
|
|
|
}
|
|
|
|
|
2023-10-23 03:16:38 +02:00
|
|
|
bool PageTable::ContinueTraversal(TraversalEntry* out_entry, TraversalContext* context) const {
|
2022-02-19 08:42:27 +01:00
|
|
|
// Setup invalid defaults.
|
2023-10-23 03:16:38 +02:00
|
|
|
out_entry->phys_addr = 0;
|
|
|
|
out_entry->block_size = page_size;
|
2022-02-19 08:42:27 +01:00
|
|
|
|
2024-01-07 19:59:48 +01:00
|
|
|
// Regardless of whether the page was mapped, advance on exit.
|
|
|
|
SCOPE_EXIT({
|
|
|
|
context->next_page += 1;
|
|
|
|
context->next_offset += page_size;
|
|
|
|
});
|
|
|
|
|
2022-02-19 08:42:27 +01:00
|
|
|
// Validate that we can read the actual entry.
|
2023-10-23 03:16:38 +02:00
|
|
|
const auto page = context->next_page;
|
2022-02-19 08:42:27 +01:00
|
|
|
if (page >= backing_addr.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate that the entry is mapped.
|
|
|
|
const auto phys_addr = backing_addr[page];
|
|
|
|
if (phys_addr == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the results.
|
2023-10-23 03:16:38 +02:00
|
|
|
out_entry->phys_addr = phys_addr + context->next_offset;
|
2022-02-19 08:42:27 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageTable::Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits) {
|
|
|
|
const std::size_t num_page_table_entries{1ULL
|
|
|
|
<< (address_space_width_in_bits - page_size_in_bits)};
|
2019-03-02 21:20:28 +01:00
|
|
|
pointers.resize(num_page_table_entries);
|
2020-03-13 21:33:47 +01:00
|
|
|
backing_addr.resize(num_page_table_entries);
|
2023-05-29 01:35:51 +02:00
|
|
|
blocks.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-19 08:42:27 +01:00
|
|
|
page_size = 1ULL << page_size_in_bits;
|
2019-03-02 21:20:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Common
|