2022-06-30 01:27:49 +02:00
|
|
|
// SPDX-FileCopyrightText: 2021 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-11-10 17:37:17 +01:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/multi_level_page_table.h"
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
template <typename BaseAddr>
|
|
|
|
MultiLevelPageTable<BaseAddr>::MultiLevelPageTable(std::size_t address_space_bits_,
|
|
|
|
std::size_t first_level_bits_,
|
|
|
|
std::size_t page_bits_)
|
|
|
|
: address_space_bits{address_space_bits_},
|
|
|
|
first_level_bits{first_level_bits_}, page_bits{page_bits_} {
|
2022-02-05 18:15:26 +01:00
|
|
|
if (page_bits == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-10 17:37:17 +01:00
|
|
|
first_level_shift = address_space_bits - first_level_bits;
|
2021-11-11 21:24:40 +01:00
|
|
|
first_level_chunk_size = (1ULL << (first_level_shift - page_bits)) * sizeof(BaseAddr);
|
2021-11-10 17:37:17 +01:00
|
|
|
alloc_size = (1ULL << (address_space_bits - page_bits)) * sizeof(BaseAddr);
|
|
|
|
std::size_t first_level_size = 1ULL << first_level_bits;
|
|
|
|
first_level_map.resize(first_level_size, nullptr);
|
|
|
|
#ifdef _WIN32
|
|
|
|
void* base{VirtualAlloc(nullptr, alloc_size, MEM_RESERVE, PAGE_READWRITE)};
|
|
|
|
#else
|
2022-04-13 21:02:55 +02:00
|
|
|
void* base{mmap(nullptr, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)};
|
2021-11-10 17:37:17 +01:00
|
|
|
|
|
|
|
if (base == MAP_FAILED) {
|
|
|
|
base = nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ASSERT(base);
|
|
|
|
base_ptr = reinterpret_cast<BaseAddr*>(base);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename BaseAddr>
|
|
|
|
MultiLevelPageTable<BaseAddr>::~MultiLevelPageTable() noexcept {
|
|
|
|
if (!base_ptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
|
|
ASSERT(VirtualFree(base_ptr, 0, MEM_RELEASE));
|
|
|
|
#else
|
|
|
|
ASSERT(munmap(base_ptr, alloc_size) == 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename BaseAddr>
|
|
|
|
void MultiLevelPageTable<BaseAddr>::ReserveRange(u64 start, std::size_t size) {
|
|
|
|
const u64 new_start = start >> first_level_shift;
|
2021-11-11 21:24:40 +01:00
|
|
|
const u64 new_end = (start + size) >> first_level_shift;
|
2021-11-10 17:37:17 +01:00
|
|
|
for (u64 i = new_start; i <= new_end; i++) {
|
|
|
|
if (!first_level_map[i]) {
|
|
|
|
AllocateLevel(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename BaseAddr>
|
|
|
|
void MultiLevelPageTable<BaseAddr>::AllocateLevel(u64 level) {
|
2021-11-11 21:24:40 +01:00
|
|
|
void* ptr = reinterpret_cast<char *>(base_ptr) + level * first_level_chunk_size;
|
2021-11-10 17:37:17 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
void* base{VirtualAlloc(ptr, first_level_chunk_size, MEM_COMMIT, PAGE_READWRITE)};
|
|
|
|
#else
|
|
|
|
void* base{mmap(ptr, first_level_chunk_size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)};
|
|
|
|
|
|
|
|
if (base == MAP_FAILED) {
|
|
|
|
base = nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ASSERT(base);
|
|
|
|
|
|
|
|
first_level_map[level] = base;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Common
|