2022-04-23 09:59:50 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-03-02 20:20:28 +00:00
|
|
|
|
|
|
|
#include "common/page_table.h"
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2020-04-09 03:49:51 +01:00
|
|
|
PageTable::PageTable() = default;
|
2019-03-02 20:20:28 +00:00
|
|
|
|
2020-11-18 00:58:41 +00:00
|
|
|
PageTable::~PageTable() noexcept = default;
|
2019-03-02 20:20:28 +00:00
|
|
|
|
2022-02-19 08:14:27 +00:00
|
|
|
bool PageTable::BeginTraversal(TraversalEntry& out_entry, TraversalContext& out_context,
|
2022-02-19 07:42:27 +00:00
|
|
|
u64 address) const {
|
|
|
|
// Setup invalid defaults.
|
2022-02-19 08:14:27 +00:00
|
|
|
out_entry.phys_addr = 0;
|
|
|
|
out_entry.block_size = page_size;
|
|
|
|
out_context.next_page = 0;
|
2022-02-19 07:42:27 +00:00
|
|
|
|
|
|
|
// Validate that we can read the actual entry.
|
|
|
|
const auto page = address / page_size;
|
|
|
|
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.
|
2022-02-19 08:14:27 +00:00
|
|
|
out_entry.phys_addr = phys_addr + address;
|
|
|
|
out_context.next_page = page + 1;
|
|
|
|
out_context.next_offset = address + page_size;
|
2022-02-19 07:42:27 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-19 08:14:27 +00:00
|
|
|
bool PageTable::ContinueTraversal(TraversalEntry& out_entry, TraversalContext& context) const {
|
2022-02-19 07:42:27 +00:00
|
|
|
// Setup invalid defaults.
|
2022-02-19 08:14:27 +00:00
|
|
|
out_entry.phys_addr = 0;
|
|
|
|
out_entry.block_size = page_size;
|
2022-02-19 07:42:27 +00:00
|
|
|
|
|
|
|
// Validate that we can read the actual entry.
|
2022-02-19 08:14:27 +00:00
|
|
|
const auto page = context.next_page;
|
2022-02-19 07:42:27 +00: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.
|
2022-02-19 08:14:27 +00:00
|
|
|
out_entry.phys_addr = phys_addr + context.next_offset;
|
|
|
|
context.next_page = page + 1;
|
|
|
|
context.next_offset += page_size;
|
2022-02-19 07:42:27 +00: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 20:20:28 +00:00
|
|
|
pointers.resize(num_page_table_entries);
|
2020-03-13 20:33:47 +00:00
|
|
|
backing_addr.resize(num_page_table_entries);
|
2021-05-29 08:24:09 +01:00
|
|
|
current_address_space_width_in_bits = address_space_width_in_bits;
|
2022-02-19 07:42:27 +00:00
|
|
|
page_size = 1ULL << page_size_in_bits;
|
2019-03-02 20:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Common
|