dynarmic/include/oaknut/dual_code_block.hpp
Merry 99c0a73f91 Squashed 'externals/oaknut/' changes from c24f918e5..d0488d932
d0488d932 oaknut: 2.0.0
40ad78bbf oaknut: Implement DualCodeBlock and related support
9f131cfb5 oaknut: add configuration for standalone installation
69799b43c oaknut: Test building for Android on CI
1d51f5512 oaknut: 1.2.2
918bd94f0 oaknut: Eliminate -Wconversion warnings
316d8869e oaknut: Fix edgecases in MOVP2R on +/-4GiB boundary
d8634eaa1 oaknut: Fix page boundary error in ADP
d0ca9a24e oaknut: Update README examples for CPU feature detection
dbeec268b oaknut: feature_detection_freebsd: Warn about incompatibility with earlier FreeBSD versions
86e5386e2 oaknut: feature_detect: Support NetBSD
df4cf2d48 oaknut: feature_detect: Support OpenBSD
99dfff25a oaknut: feature_detection: Read ID registers
319b3d2c9 oaknut: Add basic CPU feature detection
23e9ddb4c oaknut: CI: Don't run slow tests on OpenBSD
734f1bdb4 oaknut: CI: Use up-to-date qemu
f462c9774 oaknut: CI: Build on OpenBSD
19cd42204 oaknut: code_block: Add NetBSD and OpenBSD support
18b86a3ec oaknut: SystemReg: Add more EL0 accessible registers
53c43bf0c oaknut/tests: Reduce iterations for MOVP2R
cc37df19e oaknut: Test on FreeBSD
a66b32d26 oaknut: Fix crossing sign boundary in PageOffset
206468d72 oaknut: CI: Add macos-arm64 build
e6eecc3f9 oaknut: 1.2.1
4252d8f4a oaknut: CMakeLists: Warnings are errors on MSVC
408eed65f oaknut: arm64_encode_helpers: remove unreachable code
bfc8eedfb oaknut: arm64_encode_helpers: p maybe unused
ff4456eca oaknut: Avoid negation of unsigned values
b4ac8fd6c oaknut: Fix MOV for applications of MOVN
0575cadc4 oaknut: Disable certain functionality where absolute addressing is not available
394a3c8f0 oaknut: Appease MSVC
011183670 oaknut: 1.2.0
e83c9f327 oaknut: Add VectorCodeGenerator
5eb122cc5 oaknut: Tidy up public header
45c5a7b25 oaknut: Fix clang-format errors
36243256f oaknut: Add `const` qualifier to `AddrOffset` ctor
4af500cb5 oaknut: Add `ptr` accessor to `Label`
bccb06669 oaknut: CodeGenerator const correctness
da0590a86 oaknut: github: Update package repositories

git-subtree-dir: externals/oaknut
git-subtree-split: d0488d9320ae673167dd9117223e3453d5ff102f
2024-01-28 14:56:59 +00:00

165 lines
4.8 KiB
C++

// SPDX-FileCopyrightText: Copyright (c) 2024 merryhime <https://mary.rs>
// SPDX-License-Identifier: MIT
#pragma once
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <new>
#if defined(_WIN32)
# define NOMINMAX
# include <windows.h>
#elif defined(__APPLE__)
# include <mach/mach.h>
# include <mach/vm_map.h>
# include <TargetConditionals.h>
# include <libkern/OSCacheControl.h>
# include <pthread.h>
# include <sys/mman.h>
# include <unistd.h>
#else
# if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
# endif
# include <sys/mman.h>
# include <sys/types.h>
# include <unistd.h>
#endif
namespace oaknut {
class DualCodeBlock {
public:
explicit DualCodeBlock(std::size_t size)
: m_size(size)
{
#if defined(_WIN32)
m_wmem = m_xmem = (std::uint32_t*)VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (m_wmem == nullptr)
throw std::bad_alloc{};
#elif defined(__APPLE__)
m_wmem = (std::uint32_t*)mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (m_wmem == MAP_FAILED)
throw std::bad_alloc{};
vm_prot_t cur_prot, max_prot;
kern_return_t ret = vm_remap(mach_task_self(), (vm_address_t*)&m_xmem, size, 0, VM_FLAGS_ANYWHERE | VM_FLAGS_RANDOM_ADDR, mach_task_self(), (mach_vm_address_t)m_wmem, false, &cur_prot, &max_prot, VM_INHERIT_NONE);
if (ret != KERN_SUCCESS)
throw std::bad_alloc{};
mprotect(m_xmem, size, PROT_READ | PROT_EXEC);
#else
# if defined(__OpenBSD__)
char tmpl[] = "oaknut_dual_code_block.XXXXXXXXXX";
fd = shm_mkstemp(tmpl);
if (fd < 0)
throw std::bad_alloc{};
shm_unlink(tmpl);
# else
fd = memfd_create("oaknut_dual_code_block", 0);
if (fd < 0)
throw std::bad_alloc{};
# endif
int ret = ftruncate(fd, size);
if (ret != 0)
throw std::bad_alloc{};
m_wmem = (std::uint32_t*)mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
m_xmem = (std::uint32_t*)mmap(nullptr, size, PROT_READ | PROT_EXEC, MAP_SHARED, fd, 0);
if (m_wmem == MAP_FAILED || m_xmem == MAP_FAILED)
throw std::bad_alloc{};
#endif
}
~DualCodeBlock()
{
#if defined(_WIN32)
VirtualFree((void*)m_xmem, 0, MEM_RELEASE);
#elif defined(__APPLE__)
#else
munmap(m_wmem, m_size);
munmap(m_xmem, m_size);
close(fd);
#endif
}
DualCodeBlock(const DualCodeBlock&) = delete;
DualCodeBlock& operator=(const DualCodeBlock&) = delete;
DualCodeBlock(DualCodeBlock&&) = delete;
DualCodeBlock& operator=(DualCodeBlock&&) = delete;
/// Pointer to executable mirror of memory (permissions: R-X)
std::uint32_t* xptr() const
{
return m_xmem;
}
/// Pointer to writeable mirror of memory (permissions: RW-)
std::uint32_t* wptr() const
{
return m_wmem;
}
/// Invalidate should be used with executable memory pointers.
void invalidate(std::uint32_t* mem, std::size_t size)
{
#if defined(__APPLE__)
sys_icache_invalidate(mem, size);
#elif defined(_WIN32)
FlushInstructionCache(GetCurrentProcess(), mem, size);
#else
static std::size_t icache_line_size = 0x10000, dcache_line_size = 0x10000;
std::uint64_t ctr;
__asm__ volatile("mrs %0, ctr_el0"
: "=r"(ctr));
const std::size_t isize = icache_line_size = std::min<std::size_t>(icache_line_size, 4 << ((ctr >> 0) & 0xf));
const std::size_t dsize = dcache_line_size = std::min<std::size_t>(dcache_line_size, 4 << ((ctr >> 16) & 0xf));
const std::uintptr_t end = (std::uintptr_t)mem + size;
for (std::uintptr_t addr = ((std::uintptr_t)mem) & ~(dsize - 1); addr < end; addr += dsize) {
__asm__ volatile("dc cvau, %0"
:
: "r"(addr)
: "memory");
}
__asm__ volatile("dsb ish\n"
:
:
: "memory");
for (std::uintptr_t addr = ((std::uintptr_t)mem) & ~(isize - 1); addr < end; addr += isize) {
__asm__ volatile("ic ivau, %0"
:
: "r"(addr)
: "memory");
}
__asm__ volatile("dsb ish\nisb\n"
:
:
: "memory");
#endif
}
void invalidate_all()
{
invalidate(m_xmem, m_size);
}
protected:
#if !defined(_WIN32) && !defined(__APPLE__)
int fd = -1;
#endif
std::uint32_t* m_xmem = nullptr;
std::uint32_t* m_wmem = nullptr;
std::size_t m_size = 0;
};
} // namespace oaknut