|
|
@ -695,21 +695,10 @@ HostMemory::HostMemory(size_t backing_size_, size_t virtual_size_) |
|
|
backing_base = fallback_buffer->data(); |
|
|
backing_base = fallback_buffer->data(); |
|
|
virtual_base = nullptr; |
|
|
virtual_base = nullptr; |
|
|
#else
|
|
|
#else
|
|
|
bool has_memfd = true; // all platforms support it
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
// EXCEPT WINDOWS who may not (Windows 8.1, 7, Vista, etc)
|
|
|
|
|
|
DynamicLibrary kernelbase_dll("KernelBase"); |
|
|
|
|
|
has_memfd = kernelbase_dll.IsOpen(); |
|
|
|
|
|
if (has_memfd) { |
|
|
|
|
|
PFN_CreateFileMapping2 p{}; |
|
|
|
|
|
GetFuncAddress(kernelbase_dll, "CreateFileMapping2", p); |
|
|
|
|
|
has_memfd = p != nullptr; |
|
|
|
|
|
} |
|
|
|
|
|
#endif
|
|
|
|
|
|
// Try to allocate a fastmem arena.
|
|
|
// Try to allocate a fastmem arena.
|
|
|
// The implementation will fail with std::bad_alloc on errors.
|
|
|
// The implementation will fail with std::bad_alloc on errors.
|
|
|
impl = std::make_unique<HostMemory::Impl>(AlignUp(backing_size, PageAlignment), AlignUp(virtual_size, PageAlignment) + HugePageSize); |
|
|
impl = std::make_unique<HostMemory::Impl>(AlignUp(backing_size, PageAlignment), AlignUp(virtual_size, PageAlignment) + HugePageSize); |
|
|
if (has_memfd && impl->Init()) { |
|
|
|
|
|
|
|
|
if (impl->Init()) { |
|
|
backing_base = impl->backing_base; |
|
|
backing_base = impl->backing_base; |
|
|
virtual_base = impl->virtual_base; |
|
|
virtual_base = impl->virtual_base; |
|
|
if (virtual_base) { |
|
|
if (virtual_base) { |
|
|
@ -719,8 +708,7 @@ HostMemory::HostMemory(size_t backing_size_, size_t virtual_size_) |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
impl.reset(); |
|
|
impl.reset(); |
|
|
LOG_WARNING(HW_Memory, "Platform doesn't support fastmem"); |
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
|
LOG_WARNING(HW_Memory, "Platform can support fastmem, but can't create it"); |
|
|
fallback_buffer.emplace(backing_size); |
|
|
fallback_buffer.emplace(backing_size); |
|
|
backing_base = fallback_buffer->data(); |
|
|
backing_base = fallback_buffer->data(); |
|
|
virtual_base = nullptr; |
|
|
virtual_base = nullptr; |
|
|
@ -734,13 +722,8 @@ HostMemory::HostMemory(HostMemory&&) noexcept = default; |
|
|
|
|
|
|
|
|
HostMemory& HostMemory::operator=(HostMemory&&) noexcept = default; |
|
|
HostMemory& HostMemory::operator=(HostMemory&&) noexcept = default; |
|
|
|
|
|
|
|
|
#if defined(__OPENORBIS__) || defined(__managarm__) || defined(__wasi__) || defined(__EMSCRIPTEN__)
|
|
|
|
|
|
void HostMemory::Map(size_t virtual_offset, size_t host_offset, size_t length, MemoryPermission perms, bool separate_heap) {} |
|
|
|
|
|
void HostMemory::Unmap(size_t virtual_offset, size_t length, bool separate_heap) {} |
|
|
|
|
|
void HostMemory::Protect(size_t virtual_offset, size_t length, MemoryPermission perm) {} |
|
|
|
|
|
void HostMemory::EnableDirectMappedAddress() {} |
|
|
|
|
|
#else
|
|
|
|
|
|
void HostMemory::Map(size_t virtual_offset, size_t host_offset, size_t length, MemoryPermission perms, bool separate_heap) { |
|
|
void HostMemory::Map(size_t virtual_offset, size_t host_offset, size_t length, MemoryPermission perms, bool separate_heap) { |
|
|
|
|
|
#if !(defined(__OPENORBIS__) || defined(__managarm__) || defined(__wasi__) || defined(__EMSCRIPTEN__))
|
|
|
ASSERT(virtual_offset % PageAlignment == 0); |
|
|
ASSERT(virtual_offset % PageAlignment == 0); |
|
|
ASSERT(host_offset % PageAlignment == 0); |
|
|
ASSERT(host_offset % PageAlignment == 0); |
|
|
ASSERT(length % PageAlignment == 0); |
|
|
ASSERT(length % PageAlignment == 0); |
|
|
@ -750,9 +733,11 @@ void HostMemory::Map(size_t virtual_offset, size_t host_offset, size_t length, M |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
impl->Map(virtual_offset + virtual_base_offset, host_offset, length, perms); |
|
|
impl->Map(virtual_offset + virtual_base_offset, host_offset, length, perms); |
|
|
|
|
|
#endif
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void HostMemory::Unmap(size_t virtual_offset, size_t length, bool separate_heap) { |
|
|
void HostMemory::Unmap(size_t virtual_offset, size_t length, bool separate_heap) { |
|
|
|
|
|
#if !(defined(__OPENORBIS__) || defined(__managarm__) || defined(__wasi__) || defined(__EMSCRIPTEN__))
|
|
|
ASSERT(virtual_offset % PageAlignment == 0); |
|
|
ASSERT(virtual_offset % PageAlignment == 0); |
|
|
ASSERT(length % PageAlignment == 0); |
|
|
ASSERT(length % PageAlignment == 0); |
|
|
ASSERT(virtual_offset + length <= virtual_size); |
|
|
ASSERT(virtual_offset + length <= virtual_size); |
|
|
@ -760,9 +745,11 @@ void HostMemory::Unmap(size_t virtual_offset, size_t length, bool separate_heap) |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
impl->Unmap(virtual_offset + virtual_base_offset, length); |
|
|
impl->Unmap(virtual_offset + virtual_base_offset, length); |
|
|
|
|
|
#endif
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void HostMemory::Protect(size_t virtual_offset, size_t length, MemoryPermission perm) { |
|
|
void HostMemory::Protect(size_t virtual_offset, size_t length, MemoryPermission perm) { |
|
|
|
|
|
#if !(defined(__OPENORBIS__) || defined(__managarm__) || defined(__wasi__) || defined(__EMSCRIPTEN__))
|
|
|
ASSERT(virtual_offset % PageAlignment == 0); |
|
|
ASSERT(virtual_offset % PageAlignment == 0); |
|
|
ASSERT(length % PageAlignment == 0); |
|
|
ASSERT(length % PageAlignment == 0); |
|
|
ASSERT(virtual_offset + length <= virtual_size); |
|
|
ASSERT(virtual_offset + length <= virtual_size); |
|
|
@ -773,18 +760,20 @@ void HostMemory::Protect(size_t virtual_offset, size_t length, MemoryPermission |
|
|
const bool write = True(perm & MemoryPermission::Write); |
|
|
const bool write = True(perm & MemoryPermission::Write); |
|
|
const bool execute = True(perm & MemoryPermission::Execute); |
|
|
const bool execute = True(perm & MemoryPermission::Execute); |
|
|
impl->Protect(virtual_offset + virtual_base_offset, length, read, write, execute); |
|
|
impl->Protect(virtual_offset + virtual_base_offset, length, read, write, execute); |
|
|
|
|
|
#endif
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void HostMemory::ClearBackingRegion(size_t physical_offset, size_t length, u32 fill_value) { |
|
|
|
|
|
std::memset(backing_base + physical_offset, fill_value, length); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void HostMemory::EnableDirectMappedAddress() { |
|
|
void HostMemory::EnableDirectMappedAddress() { |
|
|
|
|
|
#if !(defined(__OPENORBIS__) || defined(__managarm__) || defined(__wasi__) || defined(__EMSCRIPTEN__))
|
|
|
if (impl) { |
|
|
if (impl) { |
|
|
impl->EnableDirectMappedAddress(); |
|
|
impl->EnableDirectMappedAddress(); |
|
|
virtual_size += reinterpret_cast<uintptr_t>(virtual_base); |
|
|
virtual_size += reinterpret_cast<uintptr_t>(virtual_base); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
|
|
|
void HostMemory::ClearBackingRegion(size_t physical_offset, size_t length, u32 fill_value) { |
|
|
|
|
|
std::memset(backing_base + physical_offset, fill_value, length); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} // namespace Common
|
|
|
} // namespace Common
|