2023-12-21 00:04:03 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-24 21:49:54 +01:00
|
|
|
#include <array>
|
|
|
|
#include <atomic>
|
2023-12-25 07:32:16 +01:00
|
|
|
#include <deque>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2023-12-21 00:04:03 +01:00
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2023-12-29 07:53:52 +01:00
|
|
|
#include "common/scratch_buffer.h"
|
2023-12-21 00:04:03 +01:00
|
|
|
#include "common/virtual_buffer.h"
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
class DeviceMemory;
|
|
|
|
|
|
|
|
namespace Memory {
|
|
|
|
class Memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename DTraits>
|
|
|
|
struct DeviceMemoryManagerAllocator;
|
|
|
|
|
|
|
|
template <typename Traits>
|
|
|
|
class DeviceMemoryManager {
|
|
|
|
using DeviceInterface = typename Traits::DeviceInterface;
|
2023-12-31 20:55:15 +01:00
|
|
|
using DeviceMethods = typename Traits::DeviceMethods;
|
2023-12-21 00:04:03 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
DeviceMemoryManager(const DeviceMemory& device_memory);
|
|
|
|
~DeviceMemoryManager();
|
|
|
|
|
2023-12-31 20:55:15 +01:00
|
|
|
void BindInterface(DeviceInterface* device_inter);
|
2023-12-21 00:04:03 +01:00
|
|
|
|
|
|
|
DAddr Allocate(size_t size);
|
|
|
|
void AllocateFixed(DAddr start, size_t size);
|
|
|
|
void Free(DAddr start, size_t size);
|
|
|
|
|
2023-12-30 09:37:16 +01:00
|
|
|
void Map(DAddr address, VAddr virtual_address, size_t size, size_t process_id,
|
|
|
|
bool track = false);
|
|
|
|
|
2023-12-21 00:04:03 +01:00
|
|
|
void Unmap(DAddr address, size_t size);
|
|
|
|
|
2023-12-30 09:37:16 +01:00
|
|
|
void TrackContinuityImpl(DAddr address, VAddr virtual_address, size_t size, size_t process_id);
|
|
|
|
void TrackContinuity(DAddr address, VAddr virtual_address, size_t size, size_t process_id) {
|
|
|
|
std::scoped_lock lk(mapping_guard);
|
|
|
|
TrackContinuityImpl(address, virtual_address, size, process_id);
|
|
|
|
}
|
|
|
|
|
2023-12-21 00:04:03 +01:00
|
|
|
// Write / Read
|
|
|
|
template <typename T>
|
|
|
|
T* GetPointer(DAddr address);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
const T* GetPointer(DAddr address) const;
|
|
|
|
|
2023-12-29 07:53:52 +01:00
|
|
|
template <typename Func>
|
|
|
|
void ApplyOpOnPAddr(PAddr address, Common::ScratchBuffer<u32>& buffer, Func&& operation) {
|
2023-12-25 07:32:16 +01:00
|
|
|
DAddr subbits = static_cast<DAddr>(address & page_mask);
|
2023-12-29 07:53:52 +01:00
|
|
|
const u32 base = compressed_device_addr[(address >> page_bits)];
|
|
|
|
if ((base >> MULTI_FLAG_BITS) == 0) [[likely]] {
|
|
|
|
const DAddr d_address = static_cast<DAddr>(base << page_bits) + subbits;
|
|
|
|
operation(d_address);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
InnerGatherDeviceAddresses(buffer, address);
|
|
|
|
for (u32 value : buffer) {
|
|
|
|
operation(static_cast<DAddr>(value << page_bits) + subbits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Func>
|
|
|
|
void ApplyOpOnPointer(const u8* p, Common::ScratchBuffer<u32>& buffer, Func&& operation) {
|
|
|
|
PAddr address = GetRawPhysicalAddr<u8>(p);
|
|
|
|
ApplyOpOnPAddr(address, buffer, operation);
|
2023-12-25 07:32:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PAddr GetPhysicalRawAddressFromDAddr(DAddr address) const {
|
|
|
|
PAddr subbits = static_cast<PAddr>(address & page_mask);
|
|
|
|
auto paddr = compressed_physical_ptr[(address >> page_bits)];
|
|
|
|
if (paddr == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (static_cast<PAddr>(paddr - 1) << page_bits) + subbits;
|
|
|
|
}
|
|
|
|
|
2023-12-21 00:04:03 +01:00
|
|
|
template <typename T>
|
|
|
|
void Write(DAddr address, T value);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T Read(DAddr address) const;
|
|
|
|
|
2023-12-30 09:37:16 +01:00
|
|
|
u8* GetSpan(const DAddr src_addr, const std::size_t size);
|
|
|
|
const u8* GetSpan(const DAddr src_addr, const std::size_t size) const;
|
2023-12-25 07:32:16 +01:00
|
|
|
|
2023-12-21 00:04:03 +01:00
|
|
|
void ReadBlock(DAddr address, void* dest_pointer, size_t size);
|
2023-12-25 07:32:16 +01:00
|
|
|
void ReadBlockUnsafe(DAddr address, void* dest_pointer, size_t size);
|
|
|
|
void WriteBlock(DAddr address, const void* src_pointer, size_t size);
|
|
|
|
void WriteBlockUnsafe(DAddr address, const void* src_pointer, size_t size);
|
2023-12-21 00:04:03 +01:00
|
|
|
|
|
|
|
size_t RegisterProcess(Memory::Memory* memory);
|
|
|
|
void UnregisterProcess(size_t id);
|
|
|
|
|
2023-12-24 21:49:54 +01:00
|
|
|
void UpdatePagesCachedCount(DAddr addr, size_t size, s32 delta);
|
|
|
|
|
2023-12-25 07:32:16 +01:00
|
|
|
static constexpr size_t AS_BITS = Traits::device_virtual_bits;
|
|
|
|
|
2023-12-21 00:04:03 +01:00
|
|
|
private:
|
|
|
|
static constexpr size_t device_virtual_bits = Traits::device_virtual_bits;
|
|
|
|
static constexpr size_t device_as_size = 1ULL << device_virtual_bits;
|
2023-12-31 20:55:15 +01:00
|
|
|
static constexpr size_t physical_min_bits = 32;
|
2023-12-21 00:04:03 +01:00
|
|
|
static constexpr size_t physical_max_bits = 33;
|
|
|
|
static constexpr size_t page_bits = 12;
|
2023-12-25 07:32:16 +01:00
|
|
|
static constexpr size_t page_size = 1ULL << page_bits;
|
|
|
|
static constexpr size_t page_mask = page_size - 1ULL;
|
2023-12-21 00:04:03 +01:00
|
|
|
static constexpr u32 physical_address_base = 1U << page_bits;
|
2023-12-29 07:53:52 +01:00
|
|
|
static constexpr u32 MULTI_FLAG_BITS = 31;
|
|
|
|
static constexpr u32 MULTI_FLAG = 1U << MULTI_FLAG_BITS;
|
|
|
|
static constexpr u32 MULTI_MASK = ~MULTI_FLAG;
|
2023-12-21 00:04:03 +01:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T* GetPointerFromRaw(PAddr addr) {
|
|
|
|
return reinterpret_cast<T*>(physical_base + addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
const T* GetPointerFromRaw(PAddr addr) const {
|
|
|
|
return reinterpret_cast<T*>(physical_base + addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
PAddr GetRawPhysicalAddr(const T* ptr) const {
|
|
|
|
return static_cast<PAddr>(reinterpret_cast<uintptr_t>(ptr) - physical_base);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WalkBlock(const DAddr addr, const std::size_t size, auto on_unmapped, auto on_memory,
|
|
|
|
auto increment);
|
|
|
|
|
2023-12-29 07:53:52 +01:00
|
|
|
void InnerGatherDeviceAddresses(Common::ScratchBuffer<u32>& buffer, PAddr address);
|
|
|
|
|
2023-12-21 00:04:03 +01:00
|
|
|
std::unique_ptr<DeviceMemoryManagerAllocator<Traits>> impl;
|
|
|
|
|
|
|
|
const uintptr_t physical_base;
|
2023-12-31 20:55:15 +01:00
|
|
|
DeviceInterface* device_inter;
|
2023-12-21 00:04:03 +01:00
|
|
|
Common::VirtualBuffer<u32> compressed_physical_ptr;
|
|
|
|
Common::VirtualBuffer<u32> compressed_device_addr;
|
2023-12-30 09:37:16 +01:00
|
|
|
Common::VirtualBuffer<u32> continuity_tracker;
|
2023-12-21 00:04:03 +01:00
|
|
|
|
2023-12-24 21:49:54 +01:00
|
|
|
// Process memory interfaces
|
|
|
|
|
2023-12-21 00:04:03 +01:00
|
|
|
std::deque<size_t> id_pool;
|
|
|
|
std::deque<Memory::Memory*> registered_processes;
|
2023-12-24 21:49:54 +01:00
|
|
|
|
|
|
|
// Memory protection management
|
|
|
|
|
|
|
|
static constexpr size_t guest_max_as_bits = 39;
|
|
|
|
static constexpr size_t guest_as_size = 1ULL << guest_max_as_bits;
|
|
|
|
static constexpr size_t guest_mask = guest_as_size - 1ULL;
|
|
|
|
static constexpr size_t process_id_start_bit = guest_max_as_bits;
|
|
|
|
|
|
|
|
std::pair<size_t, VAddr> ExtractCPUBacking(size_t page_index) {
|
|
|
|
auto content = cpu_backing_address[page_index];
|
|
|
|
const VAddr address = content & guest_mask;
|
|
|
|
const size_t process_id = static_cast<size_t>(content >> process_id_start_bit);
|
|
|
|
return std::make_pair(process_id, address);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCPUBacking(size_t page_index, VAddr address, size_t process_id) {
|
2023-12-30 08:20:29 +01:00
|
|
|
cpu_backing_address[page_index] = address | (process_id << process_id_start_bit);
|
2023-12-24 21:49:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Common::VirtualBuffer<VAddr> cpu_backing_address;
|
2023-12-30 08:20:29 +01:00
|
|
|
static constexpr size_t subentries = 8 / sizeof(u8);
|
2023-12-24 21:49:54 +01:00
|
|
|
static constexpr size_t subentries_mask = subentries - 1;
|
|
|
|
class CounterEntry final {
|
|
|
|
public:
|
|
|
|
CounterEntry() = default;
|
|
|
|
|
2023-12-30 08:20:29 +01:00
|
|
|
std::atomic_uint8_t& Count(std::size_t page) {
|
2023-12-24 21:49:54 +01:00
|
|
|
return values[page & subentries_mask];
|
|
|
|
}
|
|
|
|
|
2023-12-30 08:20:29 +01:00
|
|
|
const std::atomic_uint8_t& Count(std::size_t page) const {
|
2023-12-24 21:49:54 +01:00
|
|
|
return values[page & subentries_mask];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-12-30 08:20:29 +01:00
|
|
|
std::array<std::atomic_uint8_t, subentries> values{};
|
2023-12-24 21:49:54 +01:00
|
|
|
};
|
2023-12-30 08:20:29 +01:00
|
|
|
static_assert(sizeof(CounterEntry) == subentries * sizeof(u8),
|
2023-12-25 07:32:16 +01:00
|
|
|
"CounterEntry should be 8 bytes!");
|
2023-12-24 21:49:54 +01:00
|
|
|
|
2023-12-25 07:32:16 +01:00
|
|
|
static constexpr size_t num_counter_entries =
|
|
|
|
(1ULL << (device_virtual_bits - page_bits)) / subentries;
|
2023-12-24 21:49:54 +01:00
|
|
|
using CachedPages = std::array<CounterEntry, num_counter_entries>;
|
|
|
|
std::unique_ptr<CachedPages> cached_pages;
|
2023-12-25 07:32:16 +01:00
|
|
|
std::mutex counter_guard;
|
|
|
|
std::mutex mapping_guard;
|
2023-12-21 00:04:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Core
|