core: device_manager: Add a simple class to manage device RAM.

This commit is contained in:
bunnei 2020-04-02 22:00:41 -04:00
parent f1b607829e
commit dc25c86556
5 changed files with 118 additions and 1 deletions

View file

@ -35,6 +35,8 @@ add_library(core STATIC
crypto/ctr_encryption_layer.h crypto/ctr_encryption_layer.h
crypto/xts_encryption_layer.cpp crypto/xts_encryption_layer.cpp
crypto/xts_encryption_layer.h crypto/xts_encryption_layer.h
device_memory.cpp
device_memory.h
file_sys/bis_factory.cpp file_sys/bis_factory.cpp
file_sys/bis_factory.h file_sys/bis_factory.h
file_sys/card_image.cpp file_sys/card_image.cpp

View file

@ -14,6 +14,7 @@
#include "core/core_manager.h" #include "core/core_manager.h"
#include "core/core_timing.h" #include "core/core_timing.h"
#include "core/cpu_manager.h" #include "core/cpu_manager.h"
#include "core/device_memory.h"
#include "core/file_sys/bis_factory.h" #include "core/file_sys/bis_factory.h"
#include "core/file_sys/card_image.h" #include "core/file_sys/card_image.h"
#include "core/file_sys/mode.h" #include "core/file_sys/mode.h"
@ -113,7 +114,7 @@ FileSys::VirtualFile GetGameFileFromPath(const FileSys::VirtualFilesystem& vfs,
} }
struct System::Impl { struct System::Impl {
explicit Impl(System& system) explicit Impl(System& system)
: kernel{system}, fs_controller{system}, memory{system}, : kernel{system}, device_memory{system}, fs_controller{system}, memory{system},
cpu_manager{system}, reporter{system}, applet_manager{system} {} cpu_manager{system}, reporter{system}, applet_manager{system} {}
CoreManager& CurrentCoreManager() { CoreManager& CurrentCoreManager() {
@ -337,6 +338,7 @@ struct System::Impl {
Timing::CoreTiming core_timing; Timing::CoreTiming core_timing;
Kernel::KernelCore kernel; Kernel::KernelCore kernel;
DeviceMemory device_memory;
/// RealVfsFilesystem instance /// RealVfsFilesystem instance
FileSys::VirtualFilesystem virtual_filesystem; FileSys::VirtualFilesystem virtual_filesystem;
/// ContentProviderUnion instance /// ContentProviderUnion instance
@ -472,6 +474,14 @@ Kernel::Process* System::CurrentProcess() {
return impl->kernel.CurrentProcess(); return impl->kernel.CurrentProcess();
} }
DeviceMemory& System::GetDeviceMemory() {
return impl->device_memory;
}
const DeviceMemory& System::GetDeviceMemory() const {
return impl->device_memory;
}
const Kernel::Process* System::CurrentProcess() const { const Kernel::Process* System::CurrentProcess() const {
return impl->kernel.CurrentProcess(); return impl->kernel.CurrentProcess();
} }

View file

@ -91,6 +91,7 @@ namespace Core {
class ARM_Interface; class ARM_Interface;
class CoreManager; class CoreManager;
class DeviceMemory;
class ExclusiveMonitor; class ExclusiveMonitor;
class FrameLimiter; class FrameLimiter;
class PerfStats; class PerfStats;
@ -256,6 +257,12 @@ public:
/// Gets the global scheduler /// Gets the global scheduler
const Kernel::GlobalScheduler& GlobalScheduler() const; const Kernel::GlobalScheduler& GlobalScheduler() const;
/// Gets the manager for the guest device memory
DeviceMemory& GetDeviceMemory();
/// Gets the manager for the guest device memory
const DeviceMemory& GetDeviceMemory() const;
/// Provides a pointer to the current process /// Provides a pointer to the current process
Kernel::Process* CurrentProcess(); Kernel::Process* CurrentProcess();

View file

@ -0,0 +1,50 @@
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#ifdef _WIN32
#include <windows.h>
#endif
#include "common/assert.h"
#include "core/core.h"
#include "core/device_memory.h"
#include "core/memory.h"
namespace Core {
constexpr u64 DramSize{4ULL * 1024 * 1024 * 1024};
DeviceMemory::DeviceMemory(System& system) : system{system} {
#ifdef _WIN32
base = static_cast<u8*>(
VirtualAlloc(nullptr, // System selects address
DramSize, // Size of allocation
MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, // Allocate reserved pages
PAGE_READWRITE)); // Protection = no access
#else
physical_memory.resize(DramSize);
base = physical_memory.data();
#endif
}
DeviceMemory::~DeviceMemory() {
#ifdef _WIN32
ASSERT(VirtualFree(base, DramSize, MEM_RELEASE));
#endif
}
PAddr DeviceMemory::GetPhysicalAddr(VAddr addr) {
u8* pointer{system.Memory().GetPointer(addr)};
ASSERT(pointer);
const uintptr_t offset{static_cast<uintptr_t>(pointer - GetPointer(DramMemoryMap::Base))};
return DramMemoryMap::Base + offset;
}
u8* DeviceMemory::GetPointer(PAddr addr) {
ASSERT(addr >= DramMemoryMap::Base);
ASSERT(addr < DramMemoryMap::Base + DramSize);
return base + (addr - DramMemoryMap::Base);
}
} // namespace Core

48
src/core/device_memory.h Normal file
View file

@ -0,0 +1,48 @@
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include "common/assert.h"
#include "common/common_funcs.h"
#include "core/hle/kernel/physical_memory.h"
namespace Core {
class System;
namespace DramMemoryMap {
constexpr u64 Base = 0x80000000ULL;
constexpr u64 Size = 0x100000000ULL;
constexpr u64 End = Base + Size;
constexpr u64 KernelReserveBase = Base + 0x60000;
constexpr u64 SlabHeapBase = KernelReserveBase + 0x85000;
constexpr u64 SlapHeapSize = 0xa21000;
constexpr u64 SlabHeapEnd = SlabHeapBase + SlapHeapSize;
}; // namespace DramMemoryMap
class DeviceMemory : NonCopyable {
public:
DeviceMemory(Core::System& system);
~DeviceMemory();
template <typename T>
PAddr GetPhysicalAddr(T* ptr) {
const auto ptr_addr{reinterpret_cast<uintptr_t>(ptr)};
const auto base_addr{reinterpret_cast<uintptr_t>(base)};
ASSERT(ptr_addr >= base_addr);
return ptr_addr - base_addr + DramMemoryMap::Base;
}
PAddr GetPhysicalAddr(VAddr addr);
u8* GetPointer(PAddr addr);
private:
u8* base{};
Kernel::PhysicalMemory physical_memory;
Core::System& system;
};
} // namespace Core