mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-12-13 15:19:11 +00:00
* SearchFree adjustments * Robust address validation I've adjusted IsValidAddress to take in a size, and check whether the whole range is contained in vma map. If no size is provided, the function reverts to the old form of address validation instead. * Map around gaps As is, this should work mostly. Only remaining issue is adding logic to pass the "mapped regions" to the guest vma map (and make such logic cross-platform). * Initialize vma_map using gaps This should allow memory code to catch any issues from address space gaps, and prevent non-fixed mappings from jumping to a location that isn't actually available. * Clang * Fix compile * Clang * Fix compile again * Set system_managed_base and system_managed_size based on Many places in our code use system_managed_base as the minimum mappable address, ensure this fact remains the same on Windows to prevent potential bugs. * Reduce address validation in SearchFree Allows SearchFree to function when a certain Windows GPU driver goes and reserves the whole system managed area. Since SearchFree is only called on flexible addresses, allowing this particular case, where addresses are in bounds, but there's not enough space to map, should be safe enough. * Bump address space size further To handle Madden NFL 16 (and any games like it) * More thorough logging of available memory regions Should help with spotting weirdness. * Formatting fixes * Clang * Slight reduction of user space Still large enough to handle EA's shenanigans, but small enough that Linux doesn't break. * Assert on VirtualQuery failure * Extra debugging information * Further reduce user space This will unfix most of EA's titles, but UFC will still work. Older windows versions support the high addresses, but trying to actually use them causes significant performance issues. * Extra debugging info Just in case other testers still run into issues. * Remove debug logging * Revert user space increases Technically this constant is still higher than before, but weird side effects of our old logic resulted in a max address somewhere around this in main. * address_space: Support expanded virtual memory space on macOS. Co-Authored-By: squidbus <175574877+squidbus@users.noreply.github.com> * Move address space constants to address_space.cpp This ensures that all code must use the calculated address space memory values instead of the constants, since the calculated values can differ based on the platform. This does require slight modification to thread state and gnmdriver code, since both were already using these constants directly. * Workaround Windows 10 limitations If a Windows 10 device is detected, use a lower value for USER_MAX to prevent system-wide hangs in VirtualAlloc2 calls. * Fix compile for Windows-Qt * Move tessellation_factors_ring_addr initialization to sceGnmGetTheTessellationFactorRingBufferBaseAddress * Set image base address on Windows This seems to work fine on Windows 11, needs testing from Windows 10 due to the previously discussed bugs. * Set Linux executable base to 0x700000000000 This allows Linux to map the full user space without any workarounds. Co-Authored-By: Marcin Mikołajczyk <2052578+mikusp@users.noreply.github.com> * Basic formatting changes * Reduce USER_MAX on Linux Seems like finding a reliable way to move shadPS4's position in memory is difficult, for now limit the user size so we aren't trying to overwrite ourselves. * Move memory and address_space variables. --------- Co-authored-by: squidbus <175574877+squidbus@users.noreply.github.com> Co-authored-by: Marcin Mikołajczyk <2052578+mikusp@users.noreply.github.com>
104 lines
3.2 KiB
C++
104 lines
3.2 KiB
C++
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
#include <boost/icl/separate_interval_set.hpp>
|
|
#include "common/arch.h"
|
|
#include "common/enum.h"
|
|
#include "common/types.h"
|
|
|
|
namespace Core {
|
|
|
|
enum class MemoryPermission : u32 {
|
|
None = 0,
|
|
Read = 1 << 0,
|
|
Write = 1 << 1,
|
|
ReadWrite = Read | Write,
|
|
Execute = 1 << 2,
|
|
ReadWriteExecute = Read | Write | Execute,
|
|
};
|
|
DECLARE_ENUM_FLAG_OPERATORS(MemoryPermission)
|
|
|
|
/**
|
|
* Represents the user virtual address space backed by a dmem memory block
|
|
*/
|
|
class AddressSpace {
|
|
public:
|
|
explicit AddressSpace();
|
|
~AddressSpace();
|
|
|
|
[[nodiscard]] u8* BackingBase() const noexcept {
|
|
return backing_base;
|
|
}
|
|
|
|
[[nodiscard]] VAddr SystemManagedVirtualBase() noexcept {
|
|
return reinterpret_cast<VAddr>(system_managed_base);
|
|
}
|
|
[[nodiscard]] const u8* SystemManagedVirtualBase() const noexcept {
|
|
return system_managed_base;
|
|
}
|
|
[[nodiscard]] size_t SystemManagedVirtualSize() const noexcept {
|
|
return system_managed_size;
|
|
}
|
|
|
|
[[nodiscard]] VAddr SystemReservedVirtualBase() noexcept {
|
|
return reinterpret_cast<VAddr>(system_reserved_base);
|
|
}
|
|
[[nodiscard]] const u8* SystemReservedVirtualBase() const noexcept {
|
|
return system_reserved_base;
|
|
}
|
|
[[nodiscard]] size_t SystemReservedVirtualSize() const noexcept {
|
|
return system_reserved_size;
|
|
}
|
|
|
|
[[nodiscard]] VAddr UserVirtualBase() noexcept {
|
|
return reinterpret_cast<VAddr>(user_base);
|
|
}
|
|
[[nodiscard]] const u8* UserVirtualBase() const noexcept {
|
|
return user_base;
|
|
}
|
|
[[nodiscard]] size_t UserVirtualSize() const noexcept {
|
|
return user_size;
|
|
}
|
|
|
|
/**
|
|
* @brief Maps memory to the specified virtual address.
|
|
* @param virtual_addr The base address to place the mapping.
|
|
* If zero is provided an address in system managed area is picked.
|
|
* @param size The size of the area to map.
|
|
* @param phys_addr The offset of the backing file handle to map.
|
|
* The same backing region may be aliased into different virtual regions.
|
|
* If zero is provided the mapping is considered as private.
|
|
* @return A pointer to the mapped memory.
|
|
*/
|
|
void* Map(VAddr virtual_addr, size_t size, u64 alignment = 0, PAddr phys_addr = -1,
|
|
bool exec = false);
|
|
|
|
/// Memory maps a specified file descriptor.
|
|
void* MapFile(VAddr virtual_addr, size_t size, size_t offset, u32 prot, uintptr_t fd);
|
|
|
|
/// Unmaps specified virtual memory area.
|
|
void Unmap(VAddr virtual_addr, size_t size, VAddr start_in_vma, VAddr end_in_vma,
|
|
PAddr phys_base, bool is_exec, bool has_backing, bool readonly_file);
|
|
|
|
void Protect(VAddr virtual_addr, size_t size, MemoryPermission perms);
|
|
|
|
// Returns an interval set containing all usable regions.
|
|
boost::icl::interval_set<VAddr> GetUsableRegions();
|
|
|
|
private:
|
|
struct Impl;
|
|
std::unique_ptr<Impl> impl;
|
|
u8* backing_base{};
|
|
u8* system_managed_base{};
|
|
size_t system_managed_size{};
|
|
u8* system_reserved_base{};
|
|
size_t system_reserved_size{};
|
|
u8* user_base{};
|
|
size_t user_size{};
|
|
};
|
|
|
|
} // namespace Core
|