From 93da8e0abfcdcc6e3cb5488a0db12373429f1377 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 2 Mar 2019 15:20:28 -0500 Subject: core: Move PageTable struct into Common. --- src/common/CMakeLists.txt | 4 +++ src/common/memory_hook.cpp | 11 +++++++ src/common/memory_hook.h | 47 +++++++++++++++++++++++++++ src/common/page_table.cpp | 29 +++++++++++++++++ src/common/page_table.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 171 insertions(+) create mode 100644 src/common/memory_hook.cpp create mode 100644 src/common/memory_hook.h create mode 100644 src/common/page_table.cpp create mode 100644 src/common/page_table.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index c538c6415..43ae8a9e7 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -92,10 +92,14 @@ add_library(common STATIC logging/text_formatter.cpp logging/text_formatter.h math_util.h + memory_hook.cpp + memory_hook.h microprofile.cpp microprofile.h microprofileui.h misc.cpp + page_table.cpp + page_table.h param_package.cpp param_package.h quaternion.h diff --git a/src/common/memory_hook.cpp b/src/common/memory_hook.cpp new file mode 100644 index 000000000..3986986d6 --- /dev/null +++ b/src/common/memory_hook.cpp @@ -0,0 +1,11 @@ +// Copyright 2018 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/memory_hook.h" + +namespace Common { + +MemoryHook::~MemoryHook() = default; + +} // namespace Common diff --git a/src/common/memory_hook.h b/src/common/memory_hook.h new file mode 100644 index 000000000..adaa4c2c5 --- /dev/null +++ b/src/common/memory_hook.h @@ -0,0 +1,47 @@ +// Copyright 2016 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +#include "common/common_types.h" + +namespace Common { + +/** + * Memory hooks have two purposes: + * 1. To allow reads and writes to a region of memory to be intercepted. This is used to implement + * texture forwarding and memory breakpoints for debugging. + * 2. To allow for the implementation of MMIO devices. + * + * A hook may be mapped to multiple regions of memory. + * + * If a std::nullopt or false is returned from a function, the read/write request is passed through + * to the underlying memory region. + */ +class MemoryHook { +public: + virtual ~MemoryHook(); + + virtual std::optional IsValidAddress(VAddr addr) = 0; + + virtual std::optional Read8(VAddr addr) = 0; + virtual std::optional Read16(VAddr addr) = 0; + virtual std::optional Read32(VAddr addr) = 0; + virtual std::optional Read64(VAddr addr) = 0; + + virtual bool ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size) = 0; + + virtual bool Write8(VAddr addr, u8 data) = 0; + virtual bool Write16(VAddr addr, u16 data) = 0; + virtual bool Write32(VAddr addr, u32 data) = 0; + virtual bool Write64(VAddr addr, u64 data) = 0; + + virtual bool WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size) = 0; +}; + +using MemoryHookPointer = std::shared_ptr; +} // namespace Common diff --git a/src/common/page_table.cpp b/src/common/page_table.cpp new file mode 100644 index 000000000..8eba1c3f1 --- /dev/null +++ b/src/common/page_table.cpp @@ -0,0 +1,29 @@ +// Copyright 2019 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/page_table.h" + +namespace Common { + +PageTable::PageTable(std::size_t page_size_in_bits) : page_size_in_bits{page_size_in_bits} {} + +PageTable::~PageTable() = default; + +void PageTable::Resize(std::size_t address_space_width_in_bits) { + const std::size_t num_page_table_entries = 1ULL + << (address_space_width_in_bits - page_size_in_bits); + + pointers.resize(num_page_table_entries); + attributes.resize(num_page_table_entries); + + // The default is a 39-bit address space, which causes an initial 1GB allocation size. If the + // vector size is subsequently decreased (via resize), the vector might not automatically + // actually reallocate/resize its underlying allocation, which wastes up to ~800 MB for + // 36-bit titles. Call shrink_to_fit to reduce capacity to what's actually in use. + + pointers.shrink_to_fit(); + attributes.shrink_to_fit(); +} + +} // namespace Common diff --git a/src/common/page_table.h b/src/common/page_table.h new file mode 100644 index 000000000..8339f2890 --- /dev/null +++ b/src/common/page_table.h @@ -0,0 +1,80 @@ +// Copyright 2019 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include "common/common_types.h" +#include "common/memory_hook.h" + +namespace Common { + +enum class PageType : u8 { + /// Page is unmapped and should cause an access error. + Unmapped, + /// Page is mapped to regular memory. This is the only type you can get pointers to. + Memory, + /// Page is mapped to regular memory, but also needs to check for rasterizer cache flushing and + /// invalidation + RasterizerCachedMemory, + /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions. + Special, +}; + +struct SpecialRegion { + enum class Type { + DebugHook, + IODevice, + } type; + + MemoryHookPointer handler; + + bool operator<(const SpecialRegion& other) const { + return std::tie(type, handler) < std::tie(other.type, other.handler); + } + + bool operator==(const SpecialRegion& other) const { + return std::tie(type, handler) == std::tie(other.type, other.handler); + } +}; + +/** + * A (reasonably) fast way of allowing switchable and remappable process address spaces. It loosely + * mimics the way a real CPU page table works. + */ +struct PageTable { + explicit PageTable(std::size_t page_size_in_bits); + ~PageTable(); + + /** + * Resizes the page table to be able to accomodate enough pages within + * a given address space. + * + * @param address_space_width_in_bits The address size width in bits. + */ + void Resize(std::size_t address_space_width_in_bits); + + /** + * Vector of memory pointers backing each page. An entry can only be non-null if the + * corresponding entry in the `attributes` vector is of type `Memory`. + */ + std::vector pointers; + + /** + * Contains MMIO handlers that back memory regions whose entries in the `attribute` vector is + * of type `Special`. + */ + boost::icl::interval_map> special_regions; + + /** + * Vector of fine grained page attributes. If it is set to any value other than `Memory`, then + * the corresponding entry in `pointers` MUST be set to null. + */ + std::vector attributes; + + const std::size_t page_size_in_bits{}; +}; + +} // namespace Common -- cgit v1.2.3